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 ##===----------------------------------------------------------------------===##
23 #---- begin autoconf values ----
24 my $PACKAGE_NAME = q{@PACKAGE_NAME@};
25 my $VERSION = q{@PACKAGE_VERSION@};
26 my $PREFIX = q{@LLVM_PREFIX@};
27 my $LLVM_CONFIGTIME = q{@LLVM_CONFIGTIME@};
28 my $LLVM_SRC_ROOT = q{@abs_top_srcdir@};
29 my $LLVM_OBJ_ROOT = q{@abs_top_builddir@};
30 my $ARCH = lc(q{@ARCH@});
31 my $TARGET_TRIPLE = q{@target@};
32 my $TARGETS_TO_BUILD = q{@TARGETS_TO_BUILD@};
33 my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
34 my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
35 #---- end autoconf values ----
37 # Must pretend x86_64 architecture is really x86, otherwise the native backend
38 # won't get linked in.
39 $ARCH = "x86" if $ARCH eq "x86_64";
41 #---- begin Makefile values ----
42 my $CPPFLAGS = q{@LLVM_CPPFLAGS@};
43 my $CFLAGS = q{@LLVM_CFLAGS@};
44 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
45 my $LDFLAGS = q{@LLVM_LDFLAGS@};
46 my $SYSTEM_LIBS = q{@LIBS@};
47 my $LLVM_BUILDMODE = q{@LLVM_BUILDMODE@};
48 my $LLVM_OBJ_SUFFIX = q{@LLVM_OBJ_SUFFIX@};
49 #---- end Makefile values ----
51 # Figure out where llvm-config is being run from. Primarily, we care if it has
52 # been installed, or is running from the build directory, which changes the
53 # locations of some files.
55 # Convert the current executable name into its directory (e.g. ".").
56 my ($RUN_DIR) = ($0 =~ /^(.*)\/.*$/);
58 # Turn the directory into an absolute directory on the file system, also pop up
59 # from "bin" into the build or prefix dir.
60 my $ABS_RUN_DIR = abs_path("$RUN_DIR/..");
63 # Compute the absolute object directory build, e.g. "foo/llvm/Debug".
64 my $ABS_OBJ_ROOT = "$LLVM_OBJ_ROOT$LLVM_OBJ_SUFFIX";
65 $ABS_OBJ_ROOT = abs_path("$ABS_OBJ_ROOT") if (-d $ABS_OBJ_ROOT);
68 my $INCLUDEDIR = "$ABS_RUN_DIR/include";
69 my $INCLUDEOPTION = "-I$INCLUDEDIR";
70 my $LIBDIR = "$ABS_RUN_DIR/lib";
71 my $BINDIR = "$ABS_RUN_DIR/bin";
72 if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) {
73 # If we are running out of the build directory, the include dir is in the
75 $INCLUDEDIR = "$LLVM_SRC_ROOT/include";
76 # We need include files from both the srcdir and objdir.
77 $INCLUDEOPTION = "-I$INCLUDEDIR -I$LLVM_OBJ_ROOT/include"
79 # If installed, ignore the prefix the tree was configured with, use the
81 $PREFIX = $ABS_RUN_DIR;
85 sub fix_library_names (@);
86 sub fix_library_files (@);
87 sub expand_dependencies (@);
90 # Parse our command-line arguments.
95 my $want_libnames = 0;
96 my $want_libfiles = 0;
97 my $want_components = 0;
98 foreach my $arg (@ARGV) {
100 if ($arg eq "--version") {
101 $has_opt = 1; print "$VERSION\n";
102 } elsif ($arg eq "--prefix") {
103 $has_opt = 1; print "$PREFIX\n";
104 } elsif ($arg eq "--bindir") {
105 $has_opt = 1; print "$BINDIR\n";
106 } elsif ($arg eq "--includedir") {
107 $has_opt = 1; print "$INCLUDEDIR\n";
108 } elsif ($arg eq "--libdir") {
109 $has_opt = 1; print "$LIBDIR\n";
110 } elsif ($arg eq "--cppflags") {
111 $has_opt = 1; print "$INCLUDEOPTION $CPPFLAGS\n";
112 } elsif ($arg eq "--cflags") {
113 $has_opt = 1; print "$INCLUDEOPTION $CFLAGS\n";
114 } elsif ($arg eq "--cxxflags") {
115 $has_opt = 1; print "$INCLUDEOPTION $CXXFLAGS\n";
116 } elsif ($arg eq "--ldflags") {
117 $has_opt = 1; print "-L$LIBDIR $LDFLAGS $SYSTEM_LIBS\n";
118 } elsif ($arg eq "--libs") {
119 $has_opt = 1; $want_libs = 1;
120 } elsif ($arg eq "--libnames") {
121 $has_opt = 1; $want_libnames = 1;
122 } elsif ($arg eq "--libfiles") {
123 $has_opt = 1; $want_libfiles = 1;
124 } elsif ($arg eq "--components") {
125 $has_opt = 1; print join(' ', name_map_entries), "\n";
126 } elsif ($arg eq "--targets-built") {
127 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
128 } elsif ($arg eq "--host-target") {
129 $has_opt = 1; print "$TARGET_TRIPLE\n";
130 } elsif ($arg eq "--build-mode") {
131 $has_opt = 1; print "$LLVM_BUILDMODE\n";
132 } elsif ($arg eq "--obj-root") {
133 $has_opt = 1; print abs_path("$LLVM_OBJ_ROOT/");
134 } elsif ($arg eq "--src-root") {
135 $has_opt = 1; print abs_path("$LLVM_SRC_ROOT/");
140 push @components, $arg;
144 # If no options were specified, fail.
145 usage unless $has_opt;
147 # If no components were specified, default to 'all'.
148 if (@components == 0) {
149 push @components, 'all';
152 # Force component names to lower case.
153 @components = map lc, @components;
155 # Handle any arguments which require building our dependency graph.
156 if ($want_libs || $want_libnames || $want_libfiles) {
157 my @libs = expand_dependencies(@components);
158 print join(' ', fix_library_names(@libs)), "\n" if ($want_libs);
159 print join(' ', @libs), "\n" if ($want_libnames);
160 print join(' ', fix_library_files(@libs)), "\n" if ($want_libfiles);
165 #==========================================================================
167 #==========================================================================
170 print STDERR <<__EOD__;
171 Usage: llvm-config <OPTION>... [<COMPONENT>...]
173 Get various configuration information needed to compile programs which use
174 LLVM. Typically called from 'configure' scripts. Examples:
175 llvm-config --cxxflags
176 llvm-config --ldflags
177 llvm-config --libs engine bcreader scalaropts
180 --version Print LLVM version.
181 --prefix Print the installation prefix.
182 --src-root Print the source root LLVM was built from.
183 --obj-root Print the object root used to build LLVM.
184 --bindir Directory containing LLVM executables.
185 --includedir Directory containing LLVM headers.
186 --libdir Directory containing LLVM libraries.
187 --cppflags C preprocessor flags for files that include LLVM headers.
188 --cflags C compiler flags for files that include LLVM headers.
189 --cxxflags C++ compiler flags for files that include LLVM headers.
190 --ldflags Print Linker flags.
191 --libs Libraries needed to link against LLVM components.
192 --libnames Bare library names for in-tree builds.
193 --libfiles Fully qualified library filenames for makefile depends.
194 --components List of all possible components.
195 --targets-built List of all targets currently built.
196 --host-target Target triple used to configure LLVM.
197 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).
199 all All LLVM libraries (default).
200 backend Either a native backend or the C backend.
201 engine Either a native JIT or a bitcode interpreter.
206 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
207 # files which can't be found using -L.
208 sub fix_library_names (@) {
211 foreach my $lib (@libs) {
212 # Transform the bare library name appropriately.
213 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
214 if (defined $basename) {
215 push @result, "-l$basename";
217 push @result, "$LIBDIR/$lib";
223 # Turn the list of libraries into a list of files.
224 sub fix_library_files(@) {
227 foreach my $lib (@libs) {
228 # Transform the bare library name into a filename.
229 push @result, "$LIBDIR/$lib";
234 #==========================================================================
235 # Library Dependency Analysis
236 #==========================================================================
237 # Given a few human-readable library names, find all their dependencies
238 # and sort them into an order which the linker will like. If we packed
239 # our libraries into fewer archives, we could make the linker do much
240 # of this work for us.
242 # Libraries have two different types of names in this code: Human-friendly
243 # "component" names entered on the command-line, and the raw file names
244 # we use internally (and ultimately pass to the linker).
246 # To understand this code, you'll need a working knowledge of Perl 5,
247 # and possibly some quality time with 'man perlref'.
249 sub load_dependencies;
251 sub have_native_backend;
252 sub find_best_engine;
253 sub expand_names (@);
254 sub find_all_required_sets (@);
255 sub find_all_required_sets_helper ($$@);
257 # Each "set" contains one or more libraries which must be included as a
258 # group (due to cyclic dependencies). Sets are represented as a Perl array
259 # reference pointing to a list of internal library names.
262 # Various mapping tables.
263 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
264 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
265 my %NAME_MAP; # Maps human-entered names to internal names.
267 # Have our dependencies been loaded yet?
268 my $DEPENDENCIES_LOADED = 0;
270 # Given a list of human-friendly component names, translate them into a
271 # complete set of linker arguments.
272 sub expand_dependencies (@) {
275 my @required_sets = find_all_required_sets(expand_names(@libs));
276 my @sorted_sets = topologically_sort_sets(@required_sets);
278 # Expand the library sets into libraries.
280 foreach my $set (@sorted_sets) { push @result, @{$set}; }
284 # Load in the raw dependency data stored at the end of this file.
285 sub load_dependencies {
286 return if $DEPENDENCIES_LOADED;
287 $DEPENDENCIES_LOADED = 1;
290 my ($libs, $deps) = /^\s*([^:]+):\s*(.*)\s*$/;
291 die "Malformed dependency data" unless defined $deps;
292 my @libs = split(' ', $libs);
293 my @deps = split(' ', $deps);
295 # Record our dependency data.
298 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
299 $SET_DEPS{$set} = \@deps;
304 # Build a map converting human-friendly component names into internal
307 # Add entries for all the actual libraries.
308 foreach my $set (@SETS) {
309 foreach my $lib (sort @$set) {
310 my $short_name = $lib;
311 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
312 $short_name =~ tr/A-Z/a-z/;
313 $NAME_MAP{$short_name} = [$lib];
317 # Add target-specific entries
319 foreach my $target (@TARGETS_BUILT) {
320 # FIXME: Temporary, until we don't switch all targets
321 if (defined $NAME_MAP{$target.'asmprinter'}) {
322 $NAME_MAP{$target} = [$target.'info',
323 $target.'asmprinter',
325 } elsif (defined $NAME_MAP{$target.'codegen'}) {
326 $NAME_MAP{$target} = [$target.'info',
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'
341 push @all_targets, $target;
344 # Add virtual entries.
345 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
346 $NAME_MAP{'nativecodegen'} = have_native_backend() ? [$ARCH.'codegen'] : [];
347 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
348 $NAME_MAP{'engine'} = find_best_engine;
349 $NAME_MAP{'all-targets'} = \@all_targets;
350 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
353 # Return true if we have a native backend to use.
354 sub have_native_backend {
356 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
357 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
360 # Find a working subclass of ExecutionEngine for this platform.
361 sub find_best_engine {
362 if (have_native_backend && $TARGET_HAS_JIT) {
363 return ['jit', 'native'];
365 return ['interpreter'];
369 # Get all the human-friendly component names.
370 sub name_map_entries {
372 return sort keys %NAME_MAP;
375 # Map human-readable names to internal library names.
376 sub expand_names (@) {
379 foreach my $name (@names) {
380 if (defined $LIB_TO_SET_MAP{$name}) {
381 # We've hit bottom: An actual library name.
383 } elsif (defined $NAME_MAP{$name}) {
384 # We've found a short name to expand.
385 push @result, expand_names(@{$NAME_MAP{$name}});
387 print STDERR "llvm-config: unknown component name: $name\n";
394 # Given a list of internal library names, return all sets of libraries which
395 # will need to be included by the linker (in no particular order).
396 sub find_all_required_sets (@) {
400 find_all_required_sets_helper(\%sets_added, \@result, @libs);
404 # Recursive closures are pretty broken in Perl, so we're going to separate
405 # this function from find_all_required_sets and pass in the state we need
406 # manually, as references. Yes, this is fairly unpleasant.
407 sub find_all_required_sets_helper ($$@) {
408 my ($sets_added, $result, @libs) = @_;
409 foreach my $lib (@libs) {
410 my $set = $LIB_TO_SET_MAP{$lib};
411 next if defined $$sets_added{$set};
412 $$sets_added{$set} = 1;
414 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
418 # Print a list of sets, with a label. Used for debugging.
419 sub print_sets ($@) {
420 my ($label, @sets) = @_;
422 foreach my $set (@sets) { push @output, join(',', @$set); }
423 print "$label: ", join(';', @output), "\n";
426 # Returns true if $lib is a key in $added.
427 sub has_lib_been_added ($$) {
428 my ($added, $lib) = @_;
429 return defined $$added{$LIB_TO_SET_MAP{$lib}};
432 # Returns true if all the dependencies of $set appear in $added.
433 sub have_all_deps_been_added ($$) {
434 my ($added, $set) = @_;
435 #print_sets(" Checking", $set);
436 #print_sets(" Wants", $SET_DEPS{$set});
437 foreach my $lib (@{$SET_DEPS{$set}}) {
438 return 0 unless has_lib_been_added($added, $lib);
443 # Given a list of sets, topologically sort them using dependencies.
444 sub topologically_sort_sets (@) {
448 SCAN: while (@sets) { # We'll delete items from @sets as we go.
449 #print_sets("So far", reverse(@result));
450 #print_sets("Remaining", @sets);
451 for (my $i = 0; $i < @sets; ++$i) {
453 if (have_all_deps_been_added(\%added, $set)) {
456 #print "Removing $i.\n";
457 splice(@sets, $i, 1);
458 next SCAN; # Restart our scan.
461 die "Can't find a library with no dependencies";
463 return reverse(@result);
466 # Our library dependency data will be added after the '__END__' token, and will
467 # be read through the magic <DATA> filehandle.