2 ##===- tools/llvm-config ---------------------------------------*- perl -*-===##
4 # The LLVM Compiler Infrastructure
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.
9 ##===----------------------------------------------------------------------===##
11 # Synopsis: Prints out compiler options needed to build against an installed
14 # Syntax: llvm-config OPTIONS... [COMPONENTS...]
16 ##===----------------------------------------------------------------------===##
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@};
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 my $EXPENSIVE_CHECKS = q{@EXPENSIVE_CHECKS@};
46 #---- end autoconf values ----
48 # Must pretend x86_64 architecture is really x86, otherwise the native backend
49 # won't get linked in.
50 $ARCH = "x86" if $ARCH eq "x86_64";
52 #---- begin Makefile values ----
53 my $CPPFLAGS = q{@LLVM_CPPFLAGS@};
54 my $CFLAGS = q{@LLVM_CFLAGS@};
55 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
56 my $LDFLAGS = q{@LLVM_LDFLAGS@};
57 my $SYSTEM_LIBS = q{@LIBS@};
58 my $LLVM_BUILDMODE = q{@LLVM_BUILDMODE@};
59 #---- end Makefile values ----
61 # Figure out where llvm-config is being run from. Primarily, we care if it has
62 # been installed, or is running from the build directory, which changes the
63 # locations of some files.
65 # Convert the current executable name into its directory (e.g. ".").
66 my ($RUN_DIR) = ($0 =~ /^(.*)\/.*$/);
68 # Turn the directory into an absolute directory on the file system, also pop up
69 # from "bin" into the build or prefix dir.
70 my $ABS_RUN_DIR = `cd $RUN_DIR/..; pwd`;
73 # Compute the absolute object directory build, e.g. "foo/llvm/Debug".
74 my $ABS_OBJ_ROOT = "$LLVM_OBJ_ROOT/$LLVM_BUILDMODE";
75 $ABS_OBJ_ROOT = `cd $ABS_OBJ_ROOT; pwd` if (-d $ABS_OBJ_ROOT);
78 my $INCLUDEDIR = "$ABS_RUN_DIR/include";
79 my $LIBDIR = "$ABS_RUN_DIR/lib";
80 my $BINDIR = "$ABS_RUN_DIR/bin";
81 if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) {
82 # If we are running out of the build directory, the include dir is in the
84 $INCLUDEDIR = "$LLVM_SRC_ROOT/include";
86 # If installed, ignore the prefix the tree was configured with, use the
88 $PREFIX = $ABS_RUN_DIR;
92 sub fix_library_names (@);
93 sub fix_library_files (@);
94 sub expand_dependencies (@);
97 # Parse our command-line arguments.
102 my $want_libnames = 0;
103 my $want_libfiles = 0;
104 my $want_components = 0;
105 foreach my $arg (@ARGV) {
107 if ($arg eq "--version") {
108 $has_opt = 1; print "$VERSION\n";
109 } elsif ($arg eq "--prefix") {
110 $has_opt = 1; print "$PREFIX\n";
111 } elsif ($arg eq "--bindir") {
112 $has_opt = 1; print "$BINDIR\n";
113 } elsif ($arg eq "--includedir") {
114 $has_opt = 1; print "$INCLUDEDIR\n";
115 } elsif ($arg eq "--libdir") {
116 $has_opt = 1; print "$LIBDIR\n";
117 } elsif ($arg eq "--cppflags") {
119 my $cppopts = get_cpp_opts();
121 } elsif ($arg eq "--cflags") {
123 my $cppopts = get_cpp_opts();
124 print "$cppopts $CFLAGS\n";
125 } elsif ($arg eq "--cxxflags") {
127 my $cppopts = get_cpp_opts();
128 print "$cppopts $CXXFLAGS\n";
129 } elsif ($arg eq "--ldflags") {
130 $has_opt = 1; print "-L$LIBDIR $LDFLAGS $SYSTEM_LIBS\n";
131 } elsif ($arg eq "--libs") {
132 $has_opt = 1; $want_libs = 1;
133 } elsif ($arg eq "--libnames") {
134 $has_opt = 1; $want_libnames = 1;
135 } elsif ($arg eq "--libfiles") {
136 $has_opt = 1; $want_libfiles = 1;
137 } elsif ($arg eq "--components") {
138 $has_opt = 1; print join(' ', name_map_entries), "\n";
139 } elsif ($arg eq "--targets-built") {
140 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
141 } elsif ($arg eq "--host-target") {
142 $has_opt = 1; print "$TARGET_TRIPLE\n";
143 } elsif ($arg eq "--build-mode") {
144 $has_opt = 1; print "$LLVM_BUILDMODE\n";
145 } elsif ($arg eq "--obj-root") {
146 $has_opt = 1; print `cd $LLVM_OBJ_ROOT/; pwd`;
147 } elsif ($arg eq "--src-root") {
148 $has_opt = 1; print `cd $LLVM_SRC_ROOT/; pwd`;
153 push @components, $arg;
157 # If no options were specified, fail.
158 usage unless $has_opt;
160 # If no components were specified, default to 'all'.
161 if (@components == 0) {
162 push @components, 'all';
165 # Force component names to lower case.
166 @components = map lc, @components;
168 # Handle any arguments which require building our dependency graph.
169 if ($want_libs || $want_libnames || $want_libfiles) {
170 my @libs = expand_dependencies(@components);
171 print join(' ', fix_library_names(@libs)), "\n" if ($want_libs);
172 print join(' ', @libs), "\n" if ($want_libnames);
173 print join(' ', fix_library_files(@libs)), "\n" if ($want_libfiles);
178 #==========================================================================
180 #==========================================================================
183 print STDERR <<__EOD__;
184 Usage: llvm-config <OPTION>... [<COMPONENT>...]
186 Get various configuration information needed to compile programs which use
187 LLVM. Typically called from 'configure' scripts. Examples:
188 llvm-config --cxxflags
189 llvm-config --ldflags
190 llvm-config --libs engine bcreader scalaropts
193 --version Print LLVM version.
194 --prefix Print the installation prefix.
195 --src-root Print the source root LLVM was built from.
196 --obj-root Print the object root used to build LLVM.
197 --bindir Directory containing LLVM executables.
198 --includedir Directory containing LLVM headers.
199 --libdir Directory containing LLVM libraries.
200 --cppflags C preprocessor flags for files that include LLVM headers.
201 --cflags C compiler flags for files that include LLVM headers.
202 --cxxflags C++ compiler flags for files that include LLVM headers.
203 --ldflags Print Linker flags.
204 --libs Libraries needed to link against LLVM components.
205 --libnames Bare library names for in-tree builds.
206 --libfiles Fully qualified library filenames for makefile depends.
207 --components List of all possible components.
208 --targets-built List of all targets currently built.
209 --host-target Target triple used to configure LLVM.
210 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).
212 all All LLVM libraries (default).
213 backend Either a native backend or the C backend.
214 engine Either a native JIT or a bytecode interpreter.
219 # Return cpp flags used to build llvm.
223 if ($EXPENSIVE_CHECKS eq "yes") {
224 $opts = "-D_GLIBCXX_DEBUG -I$INCLUDEDIR $CPPFLAGS";
227 $opts = "-I$INCLUDEDIR $CPPFLAGS";
233 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
234 # files which can't be found using -L.
235 sub fix_library_names (@) {
238 foreach my $lib (@libs) {
239 # Transform the bare library name appropriately.
240 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
241 if (defined $basename) {
242 push @result, "-l$basename";
244 push @result, "$LIBDIR/$lib";
250 # Turn the list of libraries into a list of files.
251 sub fix_library_files(@) {
254 foreach my $lib (@libs) {
255 # Transform the bare library name into a filename.
256 push @result, "$LIBDIR/$lib";
261 #==========================================================================
262 # Library Dependency Analysis
263 #==========================================================================
264 # Given a few human-readable library names, find all their dependencies
265 # and sort them into an order which the linker will like. If we packed
266 # our libraries into fewer archives, we could make the linker do much
267 # of this work for us.
269 # Libraries have two different types of names in this code: Human-friendly
270 # "component" names entered on the command-line, and the raw file names
271 # we use internally (and ultimately pass to the linker).
273 # To understand this code, you'll need a working knowledge of Perl 5,
274 # and possibly some quality time with 'man perlref'.
276 sub load_dependencies;
278 sub have_native_backend;
279 sub find_best_engine;
280 sub expand_names (@);
281 sub find_all_required_sets (@);
282 sub find_all_required_sets_helper ($$@);
284 # Each "set" contains one or more libraries which must be included as a
285 # group (due to cyclic dependencies). Sets are represented as a Perl array
286 # reference pointing to a list of internal library names.
289 # Various mapping tables.
290 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
291 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
292 my %NAME_MAP; # Maps human-entered names to internal names.
294 # Have our dependencies been loaded yet?
295 my $DEPENDENCIES_LOADED = 0;
297 # Given a list of human-friendly component names, translate them into a
298 # complete set of linker arguments.
299 sub expand_dependencies (@) {
302 my @required_sets = find_all_required_sets(expand_names(@libs));
303 my @sorted_sets = topologically_sort_sets(@required_sets);
305 # Expand the library sets into libraries.
307 foreach my $set (@sorted_sets) { push @result, @{$set}; }
311 # Load in the raw dependency data stored at the end of this file.
312 sub load_dependencies {
313 return if $DEPENDENCIES_LOADED;
314 $DEPENDENCIES_LOADED = 1;
317 my ($libs, $deps) = /^\s*([^:]+):\s*(.*)\s*$/;
318 die "Malformed dependency data" unless defined $deps;
319 my @libs = split(' ', $libs);
320 my @deps = split(' ', $deps);
322 # Record our dependency data.
325 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
326 $SET_DEPS{$set} = \@deps;
331 # Build a map converting human-friendly component names into internal
334 # Add entries for all the actual libraries.
335 foreach my $set (@SETS) {
336 foreach my $lib (sort @$set) {
337 my $short_name = $lib;
338 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
339 $short_name =~ tr/A-Z/a-z/;
340 $NAME_MAP{$short_name} = [$lib];
344 # Add virtual entries.
345 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
346 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
347 $NAME_MAP{'engine'} = find_best_engine;
348 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
351 # Return true if we have a native backend to use.
352 sub have_native_backend {
354 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
355 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
358 # Find a working subclass of ExecutionEngine for this platform.
359 sub find_best_engine {
360 if (have_native_backend && $TARGET_HAS_JIT) {
361 return ['jit', 'native'];
363 return ['interpreter'];
367 # Get all the human-friendly component names.
368 sub name_map_entries {
370 return sort keys %NAME_MAP;
373 # Map human-readable names to internal library names.
374 sub expand_names (@) {
377 foreach my $name (@names) {
378 if (defined $LIB_TO_SET_MAP{$name}) {
379 # We've hit bottom: An actual library name.
381 } elsif (defined $NAME_MAP{$name}) {
382 # We've found a short name to expand.
383 push @result, expand_names(@{$NAME_MAP{$name}});
385 print STDERR "llvm-config: unknown component name: $name\n";
392 # Given a list of internal library names, return all sets of libraries which
393 # will need to be included by the linker (in no particular order).
394 sub find_all_required_sets (@) {
398 find_all_required_sets_helper(\%sets_added, \@result, @libs);
402 # Recursive closures are pretty broken in Perl, so we're going to separate
403 # this function from find_all_required_sets and pass in the state we need
404 # manually, as references. Yes, this is fairly unpleasant.
405 sub find_all_required_sets_helper ($$@) {
406 my ($sets_added, $result, @libs) = @_;
407 foreach my $lib (@libs) {
408 my $set = $LIB_TO_SET_MAP{$lib};
409 next if defined $$sets_added{$set};
410 $$sets_added{$set} = 1;
412 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
416 # Print a list of sets, with a label. Used for debugging.
417 sub print_sets ($@) {
418 my ($label, @sets) = @_;
420 foreach my $set (@sets) { push @output, join(',', @$set); }
421 print "$label: ", join(';', @output), "\n";
424 # Returns true if $lib is a key in $added.
425 sub has_lib_been_added ($$) {
426 my ($added, $lib) = @_;
427 return defined $$added{$LIB_TO_SET_MAP{$lib}};
430 # Returns true if all the dependencies of $set appear in $added.
431 sub have_all_deps_been_added ($$) {
432 my ($added, $set) = @_;
433 #print_sets(" Checking", $set);
434 #print_sets(" Wants", $SET_DEPS{$set});
435 foreach my $lib (@{$SET_DEPS{$set}}) {
436 return 0 unless has_lib_been_added($added, $lib);
441 # Given a list of sets, topologically sort them using dependencies.
442 sub topologically_sort_sets (@) {
446 SCAN: while (@sets) { # We'll delete items from @sets as we go.
447 #print_sets("So far", reverse(@result));
448 #print_sets("Remaining", @sets);
449 for (my $i = 0; $i < @sets; ++$i) {
451 if (have_all_deps_been_added(\%added, $set)) {
454 #print "Removing $i.\n";
455 splice(@sets, $i, 1);
456 next SCAN; # Restart our scan.
459 die "Can't find a library with no dependencies";
461 return reverse(@result);
464 # Our library dependency data will be added after the '__END__' token, and will
465 # be read through the magic <DATA> filehandle.