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