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