2 ##===- tools/llvm-config ---------------------------------------*- perl -*-===##
4 # The LLVM Compiler Infrastructure
6 # This file is distributed under the University of Illinois Open Source
7 # 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 $ARCH = lc(q{@ARCH@});
30 my $TARGET_TRIPLE = q{@target@};
31 my $TARGETS_TO_BUILD = q{@TARGETS_TO_BUILD@};
32 my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
33 my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
34 #---- end autoconf values ----
36 # Must pretend x86_64 architecture is really x86, otherwise the native backend
37 # won't get linked in.
38 $ARCH = "x86" if $ARCH eq "x86_64";
40 #---- begin Makefile values ----
41 my $CPPFLAGS = q{@LLVM_CPPFLAGS@};
42 my $CFLAGS = q{@LLVM_CFLAGS@};
43 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
44 my $LDFLAGS = q{@LLVM_LDFLAGS@};
45 my $SYSTEM_LIBS = q{@LIBS@};
46 my $LLVM_BUILDMODE = q{@LLVM_BUILDMODE@};
47 #---- end Makefile values ----
49 # Figure out where llvm-config is being run from. Primarily, we care if it has
50 # been installed, or is running from the build directory, which changes the
51 # locations of some files.
53 # Convert the current executable name into its directory (e.g. ".").
54 my ($RUN_DIR) = ($0 =~ /^(.*)\/.*$/);
56 # Find the unix pwd program: we don't want to use the bash builtin, as it does
57 # not look through symlinks etc.
58 my $PWD = `which pwd`;
60 $PWD = "pwd" if (!-e $PWD);
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`;
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);
72 my $INCLUDEDIR = "$ABS_RUN_DIR/include";
73 my $INCLUDEOPTION = "-I$INCLUDEDIR";
74 my $LIBDIR = "$ABS_RUN_DIR/lib";
75 my $BINDIR = "$ABS_RUN_DIR/bin";
76 if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) {
77 # If we are running out of the build directory, the include dir is in the
79 $INCLUDEDIR = "$LLVM_SRC_ROOT/include";
80 # We need include files from both the srcdir and objdir.
81 $INCLUDEOPTION = "-I$INCLUDEDIR -I$LLVM_OBJ_ROOT/include"
83 # If installed, ignore the prefix the tree was configured with, use the
85 $PREFIX = $ABS_RUN_DIR;
89 sub fix_library_names (@);
90 sub fix_library_files (@);
91 sub expand_dependencies (@);
94 # Parse our command-line arguments.
99 my $want_libnames = 0;
100 my $want_libfiles = 0;
101 my $want_components = 0;
102 foreach my $arg (@ARGV) {
104 if ($arg eq "--version") {
105 $has_opt = 1; print "$VERSION\n";
106 } elsif ($arg eq "--prefix") {
107 $has_opt = 1; print "$PREFIX\n";
108 } elsif ($arg eq "--bindir") {
109 $has_opt = 1; print "$BINDIR\n";
110 } elsif ($arg eq "--includedir") {
111 $has_opt = 1; print "$INCLUDEDIR\n";
112 } elsif ($arg eq "--libdir") {
113 $has_opt = 1; print "$LIBDIR\n";
114 } elsif ($arg eq "--cppflags") {
115 $has_opt = 1; print "$INCLUDEOPTION $CPPFLAGS\n";
116 } elsif ($arg eq "--cflags") {
117 $has_opt = 1; print "$INCLUDEOPTION $CFLAGS\n";
118 } elsif ($arg eq "--cxxflags") {
119 $has_opt = 1; print "$INCLUDEOPTION $CXXFLAGS\n";
120 } elsif ($arg eq "--ldflags") {
121 $has_opt = 1; print "-L$LIBDIR $LDFLAGS $SYSTEM_LIBS\n";
122 } elsif ($arg eq "--libs") {
123 $has_opt = 1; $want_libs = 1;
124 } elsif ($arg eq "--libnames") {
125 $has_opt = 1; $want_libnames = 1;
126 } elsif ($arg eq "--libfiles") {
127 $has_opt = 1; $want_libfiles = 1;
128 } elsif ($arg eq "--components") {
129 $has_opt = 1; print join(' ', name_map_entries), "\n";
130 } elsif ($arg eq "--targets-built") {
131 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
132 } elsif ($arg eq "--host-target") {
133 $has_opt = 1; print "$TARGET_TRIPLE\n";
134 } elsif ($arg eq "--build-mode") {
135 $has_opt = 1; print "$LLVM_BUILDMODE\n";
136 } elsif ($arg eq "--obj-root") {
137 $has_opt = 1; print `cd $LLVM_OBJ_ROOT/; $PWD`;
138 } elsif ($arg eq "--src-root") {
139 $has_opt = 1; print `cd $LLVM_SRC_ROOT/; $PWD`;
144 push @components, $arg;
148 # If no options were specified, fail.
149 usage unless $has_opt;
151 # If no components were specified, default to 'all'.
152 if (@components == 0) {
153 push @components, 'all';
156 # Force component names to lower case.
157 @components = map lc, @components;
159 # Handle any arguments which require building our dependency graph.
160 if ($want_libs || $want_libnames || $want_libfiles) {
161 my @libs = expand_dependencies(@components);
162 print join(' ', fix_library_names(@libs)), "\n" if ($want_libs);
163 print join(' ', @libs), "\n" if ($want_libnames);
164 print join(' ', fix_library_files(@libs)), "\n" if ($want_libfiles);
169 #==========================================================================
171 #==========================================================================
174 print STDERR <<__EOD__;
175 Usage: llvm-config <OPTION>... [<COMPONENT>...]
177 Get various configuration information needed to compile programs which use
178 LLVM. Typically called from 'configure' scripts. Examples:
179 llvm-config --cxxflags
180 llvm-config --ldflags
181 llvm-config --libs engine bcreader scalaropts
184 --version Print LLVM version.
185 --prefix Print the installation prefix.
186 --src-root Print the source root LLVM was built from.
187 --obj-root Print the object root used to build LLVM.
188 --bindir Directory containing LLVM executables.
189 --includedir Directory containing LLVM headers.
190 --libdir Directory containing LLVM libraries.
191 --cppflags C preprocessor flags for files that include LLVM headers.
192 --cflags C compiler flags for files that include LLVM headers.
193 --cxxflags C++ compiler flags for files that include LLVM headers.
194 --ldflags Print Linker flags.
195 --libs Libraries needed to link against LLVM components.
196 --libnames Bare library names for in-tree builds.
197 --libfiles Fully qualified library filenames for makefile depends.
198 --components List of all possible components.
199 --targets-built List of all targets currently built.
200 --host-target Target triple used to configure LLVM.
201 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).
203 all All LLVM libraries (default).
204 backend Either a native backend or the C backend.
205 engine Either a native JIT or a bytecode interpreter.
210 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
211 # files which can't be found using -L.
212 sub fix_library_names (@) {
215 foreach my $lib (@libs) {
216 # Transform the bare library name appropriately.
217 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
218 if (defined $basename) {
219 push @result, "-l$basename";
221 push @result, "$LIBDIR/$lib";
227 # Turn the list of libraries into a list of files.
228 sub fix_library_files(@) {
231 foreach my $lib (@libs) {
232 # Transform the bare library name into a filename.
233 push @result, "$LIBDIR/$lib";
238 #==========================================================================
239 # Library Dependency Analysis
240 #==========================================================================
241 # Given a few human-readable library names, find all their dependencies
242 # and sort them into an order which the linker will like. If we packed
243 # our libraries into fewer archives, we could make the linker do much
244 # of this work for us.
246 # Libraries have two different types of names in this code: Human-friendly
247 # "component" names entered on the command-line, and the raw file names
248 # we use internally (and ultimately pass to the linker).
250 # To understand this code, you'll need a working knowledge of Perl 5,
251 # and possibly some quality time with 'man perlref'.
253 sub load_dependencies;
255 sub have_native_backend;
256 sub find_best_engine;
257 sub expand_names (@);
258 sub find_all_required_sets (@);
259 sub find_all_required_sets_helper ($$@);
261 # Each "set" contains one or more libraries which must be included as a
262 # group (due to cyclic dependencies). Sets are represented as a Perl array
263 # reference pointing to a list of internal library names.
266 # Various mapping tables.
267 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
268 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
269 my %NAME_MAP; # Maps human-entered names to internal names.
271 # Have our dependencies been loaded yet?
272 my $DEPENDENCIES_LOADED = 0;
274 # Given a list of human-friendly component names, translate them into a
275 # complete set of linker arguments.
276 sub expand_dependencies (@) {
279 my @required_sets = find_all_required_sets(expand_names(@libs));
280 my @sorted_sets = topologically_sort_sets(@required_sets);
282 # Expand the library sets into libraries.
284 foreach my $set (@sorted_sets) { push @result, @{$set}; }
288 # Load in the raw dependency data stored at the end of this file.
289 sub load_dependencies {
290 return if $DEPENDENCIES_LOADED;
291 $DEPENDENCIES_LOADED = 1;
294 my ($libs, $deps) = /^\s*([^:]+):\s*(.*)\s*$/;
295 die "Malformed dependency data" unless defined $deps;
296 my @libs = split(' ', $libs);
297 my @deps = split(' ', $deps);
299 # Record our dependency data.
302 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
303 $SET_DEPS{$set} = \@deps;
308 # Build a map converting human-friendly component names into internal
311 # Add entries for all the actual libraries.
312 foreach my $set (@SETS) {
313 foreach my $lib (sort @$set) {
314 my $short_name = $lib;
315 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
316 $short_name =~ tr/A-Z/a-z/;
317 $NAME_MAP{$short_name} = [$lib];
321 # Add target-specific entries
322 foreach my $target (@TARGETS_BUILT) {
323 # FIXME: Temporary, until we don't switch all targets
324 if (defined $NAME_MAP{$target.'asmprinter'}) {
325 $NAME_MAP{$target} = [$target.'info',
326 $target.'asmprinter',
329 $NAME_MAP{$target} = [$target.'info',
330 $NAME_MAP{$target}[0]]
333 if (defined $NAME_MAP{$target.'asmparser'}) {
334 push @{$NAME_MAP{$target}},$target.'asmparser'
337 if (defined $NAME_MAP{$target.'disassembler'}) {
338 push @{$NAME_MAP{$target}},$target.'disassembler'
342 # Add virtual entries.
343 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
344 $NAME_MAP{'nativecodegen'} = have_native_backend() ? [$ARCH.'codegen'] : [];
345 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
346 $NAME_MAP{'engine'} = find_best_engine;
347 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
350 # Return true if we have a native backend to use.
351 sub have_native_backend {
353 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
354 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
357 # Find a working subclass of ExecutionEngine for this platform.
358 sub find_best_engine {
359 if (have_native_backend && $TARGET_HAS_JIT) {
360 return ['jit', 'native'];
362 return ['interpreter'];
366 # Get all the human-friendly component names.
367 sub name_map_entries {
369 return sort keys %NAME_MAP;
372 # Map human-readable names to internal library names.
373 sub expand_names (@) {
376 foreach my $name (@names) {
377 if (defined $LIB_TO_SET_MAP{$name}) {
378 # We've hit bottom: An actual library name.
380 } elsif (defined $NAME_MAP{$name}) {
381 # We've found a short name to expand.
382 push @result, expand_names(@{$NAME_MAP{$name}});
384 print STDERR "llvm-config: unknown component name: $name\n";
391 # Given a list of internal library names, return all sets of libraries which
392 # will need to be included by the linker (in no particular order).
393 sub find_all_required_sets (@) {
397 find_all_required_sets_helper(\%sets_added, \@result, @libs);
401 # Recursive closures are pretty broken in Perl, so we're going to separate
402 # this function from find_all_required_sets and pass in the state we need
403 # manually, as references. Yes, this is fairly unpleasant.
404 sub find_all_required_sets_helper ($$@) {
405 my ($sets_added, $result, @libs) = @_;
406 foreach my $lib (@libs) {
407 my $set = $LIB_TO_SET_MAP{$lib};
408 next if defined $$sets_added{$set};
409 $$sets_added{$set} = 1;
411 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
415 # Print a list of sets, with a label. Used for debugging.
416 sub print_sets ($@) {
417 my ($label, @sets) = @_;
419 foreach my $set (@sets) { push @output, join(',', @$set); }
420 print "$label: ", join(';', @output), "\n";
423 # Returns true if $lib is a key in $added.
424 sub has_lib_been_added ($$) {
425 my ($added, $lib) = @_;
426 return defined $$added{$LIB_TO_SET_MAP{$lib}};
429 # Returns true if all the dependencies of $set appear in $added.
430 sub have_all_deps_been_added ($$) {
431 my ($added, $set) = @_;
432 #print_sets(" Checking", $set);
433 #print_sets(" Wants", $SET_DEPS{$set});
434 foreach my $lib (@{$SET_DEPS{$set}}) {
435 return 0 unless has_lib_been_added($added, $lib);
440 # Given a list of sets, topologically sort them using dependencies.
441 sub topologically_sort_sets (@) {
445 SCAN: while (@sets) { # We'll delete items from @sets as we go.
446 #print_sets("So far", reverse(@result));
447 #print_sets("Remaining", @sets);
448 for (my $i = 0; $i < @sets; ++$i) {
450 if (have_all_deps_been_added(\%added, $set)) {
453 #print "Removing $i.\n";
454 splice(@sets, $i, 1);
455 next SCAN; # Restart our scan.
458 die "Can't find a library with no dependencies";
460 return reverse(@result);
463 # Our library dependency data will be added after the '__END__' token, and will
464 # be read through the magic <DATA> filehandle.