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