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