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