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