Update to llvm-config tool, by Erik Kidd:
[oota-llvm.git] / tools / llvm-config / llvm-config.in.in
1 #!@PERL@
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
13 use 5.006;
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@});
24 my $TARGET_HAS_JIT      = q{@TARGET_HAS_JIT@};
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
111   llvm-config --libs engine bcreader scalaropts
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).
127   backend                Either a native backend or the C backend.
128   engine                 Either a native JIT or a bytecode interpreter.
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;
168 sub have_native_backend;
169 sub find_best_engine;
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.
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.
241 }
242
243 # Return true if we have a native backend to use.
244 sub have_native_backend {
245     my %BUILT;
246     foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
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) {
253         # XXX - Right now, if we omit the interpreter, we get a linker
254         # error complaining about
255         # __ZN4llvm11Interpreter6createEPNS_6ModuleEPNS_17IntrinsicLoweringE.
256         # This needs investigation.
257         return ['jit', 'native', 'interpreter'];
258     } else {
259         return ['interpreter'];
260     }
261 }
262
263 # Get all the human-friendly component names.
264 sub name_map_entries {
265     load_dependencies;
266     return sort keys %NAME_MAP;
267 }
268
269 # Map human-readable names to internal library names.
270 sub expand_names (@) {
271     my @names = @_;
272     my @result;
273     foreach my $name (@names) {
274         if (defined $LIB_TO_SET_MAP{$name}) {
275             # We've hit bottom: An actual library name.
276             push @result, $name;
277         } elsif (defined $NAME_MAP{$name}) {
278             # We've found a short name to expand.
279             push @result, expand_names(@{$NAME_MAP{$name}});
280         } else {
281             print STDERR "llvm-config: unknown component name: $name\n";
282             exit(1);
283         }
284     }
285     return @result;
286 }
287
288 # Given a list of internal library names, return all sets of libraries which
289 # will need to be included by the linker (in no particular order).
290 sub find_all_required_sets (@) {
291     my @libs = @_;
292     my %sets_added;
293     my @result;
294     find_all_required_sets_helper(\%sets_added, \@result, @libs);
295     return @result;
296 }
297
298 # Recursive closures are pretty broken in Perl, so we're going to separate
299 # this function from find_all_required_sets and pass in the state we need
300 # manually, as references.  Yes, this is fairly unpleasant.
301 sub find_all_required_sets_helper ($$@) {
302     my ($sets_added, $result, @libs) = @_;
303     foreach my $lib (@libs) {
304         my $set = $LIB_TO_SET_MAP{$lib};
305         next if defined $$sets_added{$set};
306         $$sets_added{$set} = 1;
307         push @$result, $set;
308         find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
309     }
310 }
311
312 # Print a list of sets, with a label.  Used for debugging.
313 sub print_sets ($@) {
314     my ($label, @sets) = @_;
315     my @output;
316     foreach my $set (@sets) { push @output, join(',', @$set); }
317     print "$label: ", join(';', @output), "\n";
318 }
319
320 # Returns true if $lib is a key in $added.
321 sub has_lib_been_added ($$) {
322     my ($added, $lib) = @_;
323     return defined $$added{$LIB_TO_SET_MAP{$lib}};
324 }
325
326 # Returns true if all the dependencies of $set appear in $added.
327 sub have_all_deps_been_added ($$) {
328     my ($added, $set) = @_;
329     #print_sets("  Checking", $set);
330     #print_sets("     Wants", $SET_DEPS{$set});
331     foreach my $lib (@{$SET_DEPS{$set}}) {
332         return 0 unless has_lib_been_added($added, $lib);
333     }
334     return 1;
335 }
336
337 # Given a list of sets, topologically sort them using dependencies.
338 sub topologically_sort_sets (@) {
339     my @sets = @_;
340     my %added;
341     my @result;
342     SCAN: while (@sets) { # We'll delete items from @sets as we go.
343         #print_sets("So far", reverse(@result));
344         #print_sets("Remaining", @sets);
345         for (my $i = 0; $i < @sets; ++$i) {
346             my $set = $sets[$i];
347             if (have_all_deps_been_added(\%added, $set)) {
348                 push @result, $set;
349                 $added{$set} = 1;
350                 #print "Removing $i.\n";
351                 splice(@sets, $i, 1);
352                 next SCAN; # Restart our scan.
353             }
354         }
355         die "Can't find a library with no dependencies";
356     }
357     return reverse(@result);
358 }
359
360 # Nasty hack to work around the fact that LLVMCore changes file type on
361 # certain platforms.
362 sub maybe_fix_core (@) {
363     my @libs = @_;
364     my @result;
365     foreach my $lib (@libs) {
366         if ($lib eq "LLVMCore.o" && $CORE_IS_ARCHIVE) {
367             push @result, "libLLVMCore.a";
368         } else {
369             push @result, $lib;
370         }
371     }
372     return @result;
373 }
374
375 # Our library dependency data will be added after the '__END__' token, and will
376 # be read through the magic <DATA> filehandle.
377 __END__