e84572784aeffe77e30ecc7b2421003054975bed
[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
173 # Each "set" contains one or more libraries which must be included as a
174 # group (due to cyclic dependencies).  Sets are represented as a Perl array
175 # reference pointing to a list of internal library names.
176 my @SETS;
177
178 # Various mapping tables.
179 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
180 my %SET_DEPS;       # Maps sets to a list of libraries they depend on.
181 my %NAME_MAP;       # Maps human-entered names to internal names.
182
183 # Have our dependencies been loaded yet?
184 my $DEPENDENCIES_LOADED = 0;
185
186 # Given a list of human-friendly component names, translate them into a
187 # complete set of linker arguments.
188 sub expand_dependecies (@) {
189     my @libs = @_;
190     load_dependencies;
191     my @required_sets = find_all_required_sets(expand_names(@libs));
192     my @sorted_sets = topologically_sort_sets(@required_sets);
193
194     # Expand the library sets into libraries.
195     my @result;
196     foreach my $set (@sorted_sets) { push @result, @{$set}; }
197     return @result;
198 }
199
200 # Load in the raw dependency data stored at the end of this file.
201 sub load_dependencies {
202     return if $DEPENDENCIES_LOADED;
203     $DEPENDENCIES_LOADED = 1;
204     while (<DATA>) {
205         # Parse our line.
206         my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
207         die "Malformed dependency data" unless defined $deps;
208         my @libs = split(' ', $libs);
209         my @deps = split(' ', $deps);
210
211         # Record our dependency data.
212         my $set = \@libs;
213         push @SETS, $set;
214         foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
215         $SET_DEPS{$set} = \@deps;
216     }
217     build_name_map;
218 }
219
220 # Build a map converting human-friendly component names into internal
221 # library names.
222 sub build_name_map {
223     # Add entries for all the actual libraries.
224     foreach my $set (@SETS) {
225         foreach my $lib (sort @$set) {
226             my $short_name = $lib;
227             $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
228             $short_name =~ tr/A-Z/a-z/;
229             $NAME_MAP{$short_name} = [$lib];
230         }
231     }
232
233     # Add virtual entries.
234     $NAME_MAP{'native'}  = have_native_backend() ? [$ARCH] : [];
235     $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
236     $NAME_MAP{'engine'}  = find_best_engine;
237     $NAME_MAP{'all'}     = [name_map_entries];   # Must be last.
238 }
239
240 # Return true if we have a native backend to use.
241 sub have_native_backend {
242     my %BUILT;
243     foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
244     return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
245 }
246
247 # Find a working subclass of ExecutionEngine for this platform.
248 sub find_best_engine {
249     if (have_native_backend && $TARGET_HAS_JIT) {
250         return ['jit', 'native'];
251     } else {
252         return ['interpreter'];
253     }
254 }
255
256 # Get all the human-friendly component names.
257 sub name_map_entries {
258     load_dependencies;
259     return sort keys %NAME_MAP;
260 }
261
262 # Map human-readable names to internal library names.
263 sub expand_names (@) {
264     my @names = @_;
265     my @result;
266     foreach my $name (@names) {
267         if (defined $LIB_TO_SET_MAP{$name}) {
268             # We've hit bottom: An actual library name.
269             push @result, $name;
270         } elsif (defined $NAME_MAP{$name}) {
271             # We've found a short name to expand.
272             push @result, expand_names(@{$NAME_MAP{$name}});
273         } else {
274             print STDERR "llvm-config: unknown component name: $name\n";
275             exit(1);
276         }
277     }
278     return @result;
279 }
280
281 # Given a list of internal library names, return all sets of libraries which
282 # will need to be included by the linker (in no particular order).
283 sub find_all_required_sets (@) {
284     my @libs = @_;
285     my %sets_added;
286     my @result;
287     find_all_required_sets_helper(\%sets_added, \@result, @libs);
288     return @result;
289 }
290
291 # Recursive closures are pretty broken in Perl, so we're going to separate
292 # this function from find_all_required_sets and pass in the state we need
293 # manually, as references.  Yes, this is fairly unpleasant.
294 sub find_all_required_sets_helper ($$@) {
295     my ($sets_added, $result, @libs) = @_;
296     foreach my $lib (@libs) {
297         my $set = $LIB_TO_SET_MAP{$lib};
298         next if defined $$sets_added{$set};
299         $$sets_added{$set} = 1;
300         push @$result, $set;
301         find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
302     }
303 }
304
305 # Print a list of sets, with a label.  Used for debugging.
306 sub print_sets ($@) {
307     my ($label, @sets) = @_;
308     my @output;
309     foreach my $set (@sets) { push @output, join(',', @$set); }
310     print "$label: ", join(';', @output), "\n";
311 }
312
313 # Returns true if $lib is a key in $added.
314 sub has_lib_been_added ($$) {
315     my ($added, $lib) = @_;
316     return defined $$added{$LIB_TO_SET_MAP{$lib}};
317 }
318
319 # Returns true if all the dependencies of $set appear in $added.
320 sub have_all_deps_been_added ($$) {
321     my ($added, $set) = @_;
322     #print_sets("  Checking", $set);
323     #print_sets("     Wants", $SET_DEPS{$set});
324     foreach my $lib (@{$SET_DEPS{$set}}) {
325         return 0 unless has_lib_been_added($added, $lib);
326     }
327     return 1;
328 }
329
330 # Given a list of sets, topologically sort them using dependencies.
331 sub topologically_sort_sets (@) {
332     my @sets = @_;
333     my %added;
334     my @result;
335     SCAN: while (@sets) { # We'll delete items from @sets as we go.
336         #print_sets("So far", reverse(@result));
337         #print_sets("Remaining", @sets);
338         for (my $i = 0; $i < @sets; ++$i) {
339             my $set = $sets[$i];
340             if (have_all_deps_been_added(\%added, $set)) {
341                 push @result, $set;
342                 $added{$set} = 1;
343                 #print "Removing $i.\n";
344                 splice(@sets, $i, 1);
345                 next SCAN; # Restart our scan.
346             }
347         }
348         die "Can't find a library with no dependencies";
349     }
350     return reverse(@result);
351 }
352
353 # Our library dependency data will be added after the '__END__' token, and will
354 # be read through the magic <DATA> filehandle.
355 __END__