Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 1 | #!@PERL@ |
Chris Lattner | 584073a | 2006-06-02 18:58:21 +0000 | [diff] [blame] | 2 | ##===- tools/llvm-config ---------------------------------------*- perl -*-===## |
| 3 | # |
| 4 | # The LLVM Compiler Infrastructure |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 5 | # |
Chris Lattner | 584073a | 2006-06-02 18:58:21 +0000 | [diff] [blame] | 6 | # This file was developed by Eric Kidd and is distributed under |
| 7 | # the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 8 | # |
| 9 | ##===----------------------------------------------------------------------===## |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 10 | # |
| 11 | # Synopsis: Prints out compiler options needed to build against an installed |
| 12 | # copy of LLVM. |
| 13 | # |
Chris Lattner | 7f71e21 | 2006-04-13 04:21:31 +0000 | [diff] [blame] | 14 | # Syntax: llvm-config OPTIONS... [COMPONENTS...] |
Chris Lattner | 584073a | 2006-06-02 18:58:21 +0000 | [diff] [blame] | 15 | # |
| 16 | ##===----------------------------------------------------------------------===## |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 17 | |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 18 | use 5.006; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 19 | use strict; |
| 20 | use warnings; |
| 21 | |
| 22 | #---- begin autoconf values ---- |
Reid Spencer | 2d2c2f2 | 2006-06-02 18:31:41 +0000 | [diff] [blame] | 23 | my $PACKAGE_NAME = q{@PACKAGE_NAME@}; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 24 | my $VERSION = q{@PACKAGE_VERSION@}; |
| 25 | my $PREFIX = q{@LLVM_PREFIX@}; |
Reid Spencer | 2d2c2f2 | 2006-06-02 18:31:41 +0000 | [diff] [blame] | 26 | my $LLVM_CONFIGTIME = q{@LLVM_CONFIGTIME@}; |
| 27 | my $LLVM_SRC_ROOT = q{@abs_top_srcdir@}; |
| 28 | my $LLVM_OBJ_ROOT = q{@abs_top_builddir@}; |
| 29 | my $LLVM_ON_WIN32 = q{@LLVM_ON_WIN32@}; |
| 30 | my $LLVM_ON_UNIX = q{@LLVM_ON_UNIX@}; |
| 31 | my $LLVMGCCDIR = q{@LLVMGCCDIR@}; |
| 32 | my $LLVMGCC = q{@LLVMGCC@}; |
| 33 | my $LLVMGXX = q{@LLVMGXX@}; |
| 34 | my $LLVMGCC_VERSION = q{@LLVMGCC_VERSION@}; |
| 35 | my $LLVMGCC_MAJVERS = q{@LLVMGCC_MAJVERS@}; |
| 36 | my $ENDIAN = q{@ENDIAN@}; |
| 37 | my $SHLIBEXT = q{@SHLIBEXT@}; |
| 38 | my $EXEEXT = q{@EXEEXT@}; |
| 39 | my $OS = q{@OS@}; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 40 | my $ARCH = lc(q{@ARCH@}); |
Reid Spencer | 2d2c2f2 | 2006-06-02 18:31:41 +0000 | [diff] [blame] | 41 | my $TARGET_TRIPLE = q{@target@}; |
| 42 | my $TARGETS_TO_BUILD = q{@TARGETS_TO_BUILD@}; |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 43 | my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@}; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 44 | my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@}; |
| 45 | #---- end autoconf values ---- |
| 46 | |
Jeff Cohen | 02c91ef | 2007-03-28 04:45:02 +0000 | [diff] [blame] | 47 | # Must pretend x86_64 architecture is really x86, otherwise the native backend |
| 48 | # won't get linked in. |
| 49 | $ARCH = "x86" if $ARCH eq "x86_64"; |
| 50 | |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 51 | #---- begin Makefile values ---- |
Reid Spencer | f72538e | 2007-01-06 02:48:03 +0000 | [diff] [blame] | 52 | my $CFLAGS = q{@LLVM_CFLAGS@}; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 53 | my $CXXFLAGS = q{@LLVM_CXXFLAGS@}; |
| 54 | my $LDFLAGS = q{@LLVM_LDFLAGS@}; |
Reid Spencer | 1bc6864 | 2006-07-27 23:00:30 +0000 | [diff] [blame] | 55 | my $SYSTEM_LIBS = q{@LIBS@}; |
Chris Lattner | abdbae7 | 2006-06-02 19:13:29 +0000 | [diff] [blame] | 56 | my $LLVM_BUILDMODE = q{@LLVM_BUILDMODE@}; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 57 | #---- end Makefile values ---- |
| 58 | |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 59 | # Figure out where llvm-config is being run from. Primarily, we care if it has |
| 60 | # been installed, or is running from the build directory, which changes the |
| 61 | # locations of some files. |
| 62 | |
Chris Lattner | e02b97b | 2006-06-02 01:23:18 +0000 | [diff] [blame] | 63 | # Convert the current executable name into its directory (e.g. "."). |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 64 | my ($RUN_DIR) = ($0 =~ /^(.*)\/.*$/); |
| 65 | |
| 66 | # Turn the directory into an absolute directory on the file system, also pop up |
| 67 | # from "bin" into the build or prefix dir. |
| 68 | my $ABS_RUN_DIR = `cd $RUN_DIR/..; pwd`; |
| 69 | chomp($ABS_RUN_DIR); |
| 70 | |
| 71 | # Compute the absolute object directory build, e.g. "foo/llvm/Debug". |
Chris Lattner | 3e347f2 | 2006-06-06 23:54:15 +0000 | [diff] [blame] | 72 | my $ABS_OBJ_ROOT = "$LLVM_OBJ_ROOT/$LLVM_BUILDMODE"; |
| 73 | $ABS_OBJ_ROOT = `cd $ABS_OBJ_ROOT; pwd` if (-d $ABS_OBJ_ROOT); |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 74 | chomp($ABS_OBJ_ROOT); |
| 75 | |
Chris Lattner | 0cd059e | 2006-06-02 22:03:50 +0000 | [diff] [blame] | 76 | my $INCLUDEDIR = "$ABS_RUN_DIR/include"; |
| 77 | my $LIBDIR = "$ABS_RUN_DIR/lib"; |
| 78 | my $BINDIR = "$ABS_RUN_DIR/bin"; |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 79 | if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) { |
| 80 | # If we are running out of the build directory, the include dir is in the |
| 81 | # srcdir. |
| 82 | $INCLUDEDIR = "$LLVM_SRC_ROOT/include"; |
| 83 | } else { |
Chris Lattner | 0cd059e | 2006-06-02 22:03:50 +0000 | [diff] [blame] | 84 | # If installed, ignore the prefix the tree was configured with, use the |
| 85 | # current prefix. |
| 86 | $PREFIX = $ABS_RUN_DIR; |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | e02b97b | 2006-06-02 01:23:18 +0000 | [diff] [blame] | 88 | |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 89 | sub usage; |
| 90 | sub fix_library_names (@); |
Chris Lattner | d179de5 | 2006-06-06 22:38:29 +0000 | [diff] [blame] | 91 | sub fix_library_files (@); |
Reid Spencer | d8c20a9 | 2006-08-03 21:45:35 +0000 | [diff] [blame] | 92 | sub expand_dependencies (@); |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 93 | sub name_map_entries; |
| 94 | |
| 95 | # Parse our command-line arguments. |
| 96 | usage if @ARGV == 0; |
| 97 | my @components; |
| 98 | my $has_opt = 0; |
| 99 | my $want_libs = 0; |
| 100 | my $want_libnames = 0; |
Chris Lattner | d179de5 | 2006-06-06 22:38:29 +0000 | [diff] [blame] | 101 | my $want_libfiles = 0; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 102 | my $want_components = 0; |
| 103 | foreach my $arg (@ARGV) { |
| 104 | if ($arg =~ /^-/) { |
| 105 | if ($arg eq "--version") { |
| 106 | $has_opt = 1; print "$VERSION\n"; |
| 107 | } elsif ($arg eq "--prefix") { |
| 108 | $has_opt = 1; print "$PREFIX\n"; |
| 109 | } elsif ($arg eq "--bindir") { |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 110 | $has_opt = 1; print "$BINDIR\n"; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 111 | } elsif ($arg eq "--includedir") { |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 112 | $has_opt = 1; print "$INCLUDEDIR\n"; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 113 | } elsif ($arg eq "--libdir") { |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 114 | $has_opt = 1; print "$LIBDIR\n"; |
Reid Spencer | f72538e | 2007-01-06 02:48:03 +0000 | [diff] [blame] | 115 | } elsif ($arg eq "--cflags") { |
| 116 | $has_opt = 1; print "-I$INCLUDEDIR $CFLAGS\n"; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 117 | } elsif ($arg eq "--cxxflags") { |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 118 | $has_opt = 1; print "-I$INCLUDEDIR $CXXFLAGS\n"; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 119 | } elsif ($arg eq "--ldflags") { |
Reid Spencer | 1bc6864 | 2006-07-27 23:00:30 +0000 | [diff] [blame] | 120 | $has_opt = 1; print "-L$LIBDIR $LDFLAGS $SYSTEM_LIBS\n"; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 121 | } elsif ($arg eq "--libs") { |
| 122 | $has_opt = 1; $want_libs = 1; |
| 123 | } elsif ($arg eq "--libnames") { |
| 124 | $has_opt = 1; $want_libnames = 1; |
Chris Lattner | d179de5 | 2006-06-06 22:38:29 +0000 | [diff] [blame] | 125 | } elsif ($arg eq "--libfiles") { |
| 126 | $has_opt = 1; $want_libfiles = 1; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 127 | } elsif ($arg eq "--components") { |
| 128 | $has_opt = 1; print join(' ', name_map_entries), "\n"; |
| 129 | } elsif ($arg eq "--targets-built") { |
| 130 | $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n"; |
Reid Spencer | 3b87d6a | 2007-04-22 05:05:36 +0000 | [diff] [blame^] | 131 | } elsif ($arg eq "--host-target") { |
| 132 | $has_opt = 1; print "$TARGET_TRIPLE\n"; |
Chris Lattner | 0cd059e | 2006-06-02 22:03:50 +0000 | [diff] [blame] | 133 | } elsif ($arg eq "--build-mode") { |
| 134 | $has_opt = 1; print "$LLVM_BUILDMODE\n"; |
| 135 | } elsif ($arg eq "--obj-root") { |
Reid Spencer | b43ce5a | 2006-08-11 21:50:24 +0000 | [diff] [blame] | 136 | $has_opt = 1; print `cd $LLVM_OBJ_ROOT/; pwd`; |
Chris Lattner | 0cd059e | 2006-06-02 22:03:50 +0000 | [diff] [blame] | 137 | } elsif ($arg eq "--src-root") { |
Reid Spencer | b43ce5a | 2006-08-11 21:50:24 +0000 | [diff] [blame] | 138 | $has_opt = 1; print `cd $LLVM_SRC_ROOT/; pwd`; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 139 | } else { |
| 140 | usage(); |
| 141 | } |
| 142 | } else { |
| 143 | push @components, $arg; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | # If no options were specified, fail. |
| 148 | usage unless $has_opt; |
| 149 | |
| 150 | # If no components were specified, default to 'all'. |
| 151 | if (@components == 0) { |
| 152 | push @components, 'all'; |
| 153 | } |
| 154 | |
Chris Lattner | 54eae9e | 2006-09-04 05:35:23 +0000 | [diff] [blame] | 155 | # Force component names to lower case. |
| 156 | @components = map lc, @components; |
| 157 | |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 158 | # Handle any arguments which require building our dependency graph. |
Chris Lattner | d179de5 | 2006-06-06 22:38:29 +0000 | [diff] [blame] | 159 | if ($want_libs || $want_libnames || $want_libfiles) { |
Reid Spencer | d8c20a9 | 2006-08-03 21:45:35 +0000 | [diff] [blame] | 160 | my @libs = expand_dependencies(@components); |
Chris Lattner | d179de5 | 2006-06-06 22:38:29 +0000 | [diff] [blame] | 161 | print join(' ', fix_library_names(@libs)), "\n" if ($want_libs); |
| 162 | print join(' ', @libs), "\n" if ($want_libnames); |
| 163 | print join(' ', fix_library_files(@libs)), "\n" if ($want_libfiles); |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | exit 0; |
| 167 | |
| 168 | #========================================================================== |
| 169 | # Support Routines |
| 170 | #========================================================================== |
| 171 | |
| 172 | sub usage { |
| 173 | print STDERR <<__EOD__; |
| 174 | Usage: llvm-config <OPTION>... [<COMPONENT>...] |
| 175 | |
| 176 | Get various configuration information needed to compile programs which use |
| 177 | LLVM. Typically called from 'configure' scripts. Examples: |
| 178 | llvm-config --cxxflags |
| 179 | llvm-config --ldflags |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 180 | llvm-config --libs engine bcreader scalaropts |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 181 | |
| 182 | Options: |
Chris Lattner | 0cd059e | 2006-06-02 22:03:50 +0000 | [diff] [blame] | 183 | --version Print LLVM version. |
| 184 | --prefix Print the installation prefix. |
| 185 | --src-root Print the source root LLVM was built from. |
| 186 | --obj-root Print the object root used to build LLVM. |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 187 | --bindir Directory containing LLVM executables. |
| 188 | --includedir Directory containing LLVM headers. |
| 189 | --libdir Directory containing LLVM libraries. |
Reid Spencer | f72538e | 2007-01-06 02:48:03 +0000 | [diff] [blame] | 190 | --cflags C compiler flags for files that include LLVM headers. |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 191 | --cxxflags C++ compiler flags for files that include LLVM headers. |
Chris Lattner | 0cd059e | 2006-06-02 22:03:50 +0000 | [diff] [blame] | 192 | --ldflags Print Linker flags. |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 193 | --libs Libraries needed to link against LLVM components. |
| 194 | --libnames Bare library names for in-tree builds. |
Chris Lattner | d179de5 | 2006-06-06 22:38:29 +0000 | [diff] [blame] | 195 | --libfiles Fully qualified library filenames for makefile depends. |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 196 | --components List of all possible components. |
| 197 | --targets-built List of all targets currently built. |
Chris Lattner | 0cd059e | 2006-06-02 22:03:50 +0000 | [diff] [blame] | 198 | --build-mode Print build mode of LLVM tree (e.g. Debug or Release). |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 199 | Typical components: |
| 200 | all All LLVM libraries (default). |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 201 | backend Either a native backend or the C backend. |
| 202 | engine Either a native JIT or a bytecode interpreter. |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 203 | __EOD__ |
| 204 | exit(1); |
| 205 | } |
| 206 | |
| 207 | # Use -lfoo instead of libfoo.a whenever possible, and add directories to |
| 208 | # files which can't be found using -L. |
| 209 | sub fix_library_names (@) { |
| 210 | my @libs = @_; |
| 211 | my @result; |
| 212 | foreach my $lib (@libs) { |
| 213 | # Transform the bare library name appropriately. |
| 214 | my ($basename) = ($lib =~ /^lib([^.]*)\.a/); |
| 215 | if (defined $basename) { |
| 216 | push @result, "-l$basename"; |
| 217 | } else { |
Chris Lattner | 16ad618 | 2006-06-02 21:48:10 +0000 | [diff] [blame] | 218 | push @result, "$LIBDIR/$lib"; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | return @result; |
| 222 | } |
| 223 | |
Chris Lattner | d179de5 | 2006-06-06 22:38:29 +0000 | [diff] [blame] | 224 | # Turn the list of libraries into a list of files. |
| 225 | sub fix_library_files(@) { |
| 226 | my @libs = @_; |
| 227 | my @result; |
| 228 | foreach my $lib (@libs) { |
| 229 | # Transform the bare library name into a filename. |
| 230 | push @result, "$LIBDIR/$lib"; |
| 231 | } |
| 232 | return @result; |
| 233 | } |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 234 | |
| 235 | #========================================================================== |
| 236 | # Library Dependency Analysis |
| 237 | #========================================================================== |
| 238 | # Given a few human-readable library names, find all their dependencies |
| 239 | # and sort them into an order which the linker will like. If we packed |
| 240 | # our libraries into fewer archives, we could make the linker do much |
| 241 | # of this work for us. |
| 242 | # |
| 243 | # Libraries have two different types of names in this code: Human-friendly |
| 244 | # "component" names entered on the command-line, and the raw file names |
| 245 | # we use internally (and ultimately pass to the linker). |
| 246 | # |
| 247 | # To understand this code, you'll need a working knowledge of Perl 5, |
| 248 | # and possibly some quality time with 'man perlref'. |
| 249 | |
| 250 | sub load_dependencies; |
| 251 | sub build_name_map; |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 252 | sub have_native_backend; |
| 253 | sub find_best_engine; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 254 | sub expand_names (@); |
| 255 | sub find_all_required_sets (@); |
| 256 | sub find_all_required_sets_helper ($$@); |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 257 | |
| 258 | # Each "set" contains one or more libraries which must be included as a |
| 259 | # group (due to cyclic dependencies). Sets are represented as a Perl array |
| 260 | # reference pointing to a list of internal library names. |
| 261 | my @SETS; |
| 262 | |
| 263 | # Various mapping tables. |
| 264 | my %LIB_TO_SET_MAP; # Maps internal library names to their sets. |
| 265 | my %SET_DEPS; # Maps sets to a list of libraries they depend on. |
| 266 | my %NAME_MAP; # Maps human-entered names to internal names. |
| 267 | |
| 268 | # Have our dependencies been loaded yet? |
| 269 | my $DEPENDENCIES_LOADED = 0; |
| 270 | |
| 271 | # Given a list of human-friendly component names, translate them into a |
| 272 | # complete set of linker arguments. |
Reid Spencer | d8c20a9 | 2006-08-03 21:45:35 +0000 | [diff] [blame] | 273 | sub expand_dependencies (@) { |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 274 | my @libs = @_; |
| 275 | load_dependencies; |
| 276 | my @required_sets = find_all_required_sets(expand_names(@libs)); |
| 277 | my @sorted_sets = topologically_sort_sets(@required_sets); |
| 278 | |
Chris Lattner | 06e752e | 2006-06-02 00:56:15 +0000 | [diff] [blame] | 279 | # Expand the library sets into libraries. |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 280 | my @result; |
| 281 | foreach my $set (@sorted_sets) { push @result, @{$set}; } |
Chris Lattner | 06e752e | 2006-06-02 00:56:15 +0000 | [diff] [blame] | 282 | return @result; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | # Load in the raw dependency data stored at the end of this file. |
| 286 | sub load_dependencies { |
| 287 | return if $DEPENDENCIES_LOADED; |
| 288 | $DEPENDENCIES_LOADED = 1; |
| 289 | while (<DATA>) { |
| 290 | # Parse our line. |
Anton Korobeynikov | de9c02b | 2006-08-04 21:52:23 +0000 | [diff] [blame] | 291 | my ($libs, $deps) = /^\s*([^:]+):\s*(.*)\s*$/; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 292 | die "Malformed dependency data" unless defined $deps; |
| 293 | my @libs = split(' ', $libs); |
| 294 | my @deps = split(' ', $deps); |
| 295 | |
| 296 | # Record our dependency data. |
| 297 | my $set = \@libs; |
| 298 | push @SETS, $set; |
| 299 | foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; } |
| 300 | $SET_DEPS{$set} = \@deps; |
| 301 | } |
| 302 | build_name_map; |
| 303 | } |
| 304 | |
| 305 | # Build a map converting human-friendly component names into internal |
| 306 | # library names. |
| 307 | sub build_name_map { |
| 308 | # Add entries for all the actual libraries. |
| 309 | foreach my $set (@SETS) { |
| 310 | foreach my $lib (sort @$set) { |
| 311 | my $short_name = $lib; |
| 312 | $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/; |
| 313 | $short_name =~ tr/A-Z/a-z/; |
| 314 | $NAME_MAP{$short_name} = [$lib]; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | # Add virtual entries. |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 319 | $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : []; |
| 320 | $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend']; |
| 321 | $NAME_MAP{'engine'} = find_best_engine; |
| 322 | $NAME_MAP{'all'} = [name_map_entries]; # Must be last. |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 325 | # Return true if we have a native backend to use. |
| 326 | sub have_native_backend { |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 327 | my %BUILT; |
| 328 | foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; } |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 329 | return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH}; |
| 330 | } |
| 331 | |
| 332 | # Find a working subclass of ExecutionEngine for this platform. |
| 333 | sub find_best_engine { |
| 334 | if (have_native_backend && $TARGET_HAS_JIT) { |
Reid Spencer | 1c070fc | 2006-03-24 01:10:39 +0000 | [diff] [blame] | 335 | return ['jit', 'native']; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 336 | } else { |
Reid Spencer | b195d9d | 2006-03-23 23:21:29 +0000 | [diff] [blame] | 337 | return ['interpreter']; |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | # Get all the human-friendly component names. |
| 342 | sub name_map_entries { |
| 343 | load_dependencies; |
| 344 | return sort keys %NAME_MAP; |
| 345 | } |
| 346 | |
| 347 | # Map human-readable names to internal library names. |
| 348 | sub expand_names (@) { |
| 349 | my @names = @_; |
| 350 | my @result; |
| 351 | foreach my $name (@names) { |
| 352 | if (defined $LIB_TO_SET_MAP{$name}) { |
| 353 | # We've hit bottom: An actual library name. |
| 354 | push @result, $name; |
| 355 | } elsif (defined $NAME_MAP{$name}) { |
| 356 | # We've found a short name to expand. |
| 357 | push @result, expand_names(@{$NAME_MAP{$name}}); |
| 358 | } else { |
| 359 | print STDERR "llvm-config: unknown component name: $name\n"; |
| 360 | exit(1); |
| 361 | } |
| 362 | } |
| 363 | return @result; |
| 364 | } |
| 365 | |
| 366 | # Given a list of internal library names, return all sets of libraries which |
| 367 | # will need to be included by the linker (in no particular order). |
| 368 | sub find_all_required_sets (@) { |
| 369 | my @libs = @_; |
| 370 | my %sets_added; |
| 371 | my @result; |
| 372 | find_all_required_sets_helper(\%sets_added, \@result, @libs); |
| 373 | return @result; |
| 374 | } |
| 375 | |
| 376 | # Recursive closures are pretty broken in Perl, so we're going to separate |
| 377 | # this function from find_all_required_sets and pass in the state we need |
| 378 | # manually, as references. Yes, this is fairly unpleasant. |
| 379 | sub find_all_required_sets_helper ($$@) { |
| 380 | my ($sets_added, $result, @libs) = @_; |
| 381 | foreach my $lib (@libs) { |
| 382 | my $set = $LIB_TO_SET_MAP{$lib}; |
| 383 | next if defined $$sets_added{$set}; |
| 384 | $$sets_added{$set} = 1; |
| 385 | push @$result, $set; |
| 386 | find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}}); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | # Print a list of sets, with a label. Used for debugging. |
| 391 | sub print_sets ($@) { |
| 392 | my ($label, @sets) = @_; |
| 393 | my @output; |
| 394 | foreach my $set (@sets) { push @output, join(',', @$set); } |
| 395 | print "$label: ", join(';', @output), "\n"; |
| 396 | } |
| 397 | |
| 398 | # Returns true if $lib is a key in $added. |
| 399 | sub has_lib_been_added ($$) { |
| 400 | my ($added, $lib) = @_; |
| 401 | return defined $$added{$LIB_TO_SET_MAP{$lib}}; |
| 402 | } |
| 403 | |
| 404 | # Returns true if all the dependencies of $set appear in $added. |
| 405 | sub have_all_deps_been_added ($$) { |
| 406 | my ($added, $set) = @_; |
| 407 | #print_sets(" Checking", $set); |
| 408 | #print_sets(" Wants", $SET_DEPS{$set}); |
| 409 | foreach my $lib (@{$SET_DEPS{$set}}) { |
| 410 | return 0 unless has_lib_been_added($added, $lib); |
| 411 | } |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | # Given a list of sets, topologically sort them using dependencies. |
| 416 | sub topologically_sort_sets (@) { |
| 417 | my @sets = @_; |
| 418 | my %added; |
| 419 | my @result; |
| 420 | SCAN: while (@sets) { # We'll delete items from @sets as we go. |
| 421 | #print_sets("So far", reverse(@result)); |
| 422 | #print_sets("Remaining", @sets); |
| 423 | for (my $i = 0; $i < @sets; ++$i) { |
| 424 | my $set = $sets[$i]; |
| 425 | if (have_all_deps_been_added(\%added, $set)) { |
| 426 | push @result, $set; |
| 427 | $added{$set} = 1; |
| 428 | #print "Removing $i.\n"; |
| 429 | splice(@sets, $i, 1); |
| 430 | next SCAN; # Restart our scan. |
| 431 | } |
| 432 | } |
| 433 | die "Can't find a library with no dependencies"; |
| 434 | } |
| 435 | return reverse(@result); |
| 436 | } |
| 437 | |
Reid Spencer | f2722ca | 2006-03-22 15:59:55 +0000 | [diff] [blame] | 438 | # Our library dependency data will be added after the '__END__' token, and will |
| 439 | # be read through the magic <DATA> filehandle. |
| 440 | __END__ |