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