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