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