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