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