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