5 # Synopsis: Prints out compiler options needed to build against an installed
8 # Syntax: llvm-config OPTIONS... [COMPONENTS...]
10 # This file was written by Eric Kidd, and is placed into the public domain.
17 #---- begin autoconf values ----
18 my $PACKAGE_NAME = q{@PACKAGE_NAME@};
19 my $VERSION = q{@PACKAGE_VERSION@};
20 my $PREFIX = q{@LLVM_PREFIX@};
21 my $BINDIR = "$PREFIX/bin";
22 my $INCLUDEDIR = "$PREFIX/include";
23 my $LIBDIR = "$PREFIX/lib";
24 my $LLVM_CONFIGTIME = q{@LLVM_CONFIGTIME@};
25 my $LLVM_SRC_ROOT = q{@abs_top_srcdir@};
26 my $LLVM_OBJ_ROOT = q{@abs_top_builddir@};
27 my $LLVM_ON_WIN32 = q{@LLVM_ON_WIN32@};
28 my $LLVM_ON_UNIX = q{@LLVM_ON_UNIX@};
29 my $LLVMGCCDIR = q{@LLVMGCCDIR@};
30 my $LLVMGCC = q{@LLVMGCC@};
31 my $LLVMGXX = q{@LLVMGXX@};
32 my $LLVMGCC_VERSION = q{@LLVMGCC_VERSION@};
33 my $LLVMGCC_MAJVERS = q{@LLVMGCC_MAJVERS@};
34 my $ENDIAN = q{@ENDIAN@};
35 my $SHLIBEXT = q{@SHLIBEXT@};
36 my $EXEEXT = q{@EXEEXT@};
38 my $ARCH = lc(q{@ARCH@});
39 my $TARGET_TRIPLE = q{@target@};
40 my $TARGETS_TO_BUILD = q{@TARGETS_TO_BUILD@};
41 my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
42 my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
43 #---- end autoconf values ----
45 #---- begin Makefile values ----
46 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
47 my $LDFLAGS = q{@LLVM_LDFLAGS@};
48 #---- end Makefile values ----
50 # Convert the current executable name into its directory (e.g. ".").
51 my ($PARTIALDIR) = ($0 =~ /^(.*)\/.*$/);
54 sub fix_library_names (@);
55 sub expand_dependecies (@);
58 # Parse our command-line arguments.
63 my $want_libnames = 0;
64 my $want_components = 0;
65 foreach my $arg (@ARGV) {
67 if ($arg eq "--version") {
68 $has_opt = 1; print "$VERSION\n";
69 } elsif ($arg eq "--use-current-dir-as-prefix") {
70 # Convert the scripts executable dir into a full absolute directory.
71 my $ABSDIR = `cd $PARTIALDIR/..; pwd`;
74 } elsif ($arg eq "--prefix") {
75 $has_opt = 1; print "$PREFIX\n";
76 } elsif ($arg eq "--bindir") {
77 $has_opt = 1; print "$PREFIX/bin\n";
78 } elsif ($arg eq "--includedir") {
79 $has_opt = 1; print "$PREFIX/include\n";
80 } elsif ($arg eq "--libdir") {
81 $has_opt = 1; print "$PREFIX/lib\n";
82 } elsif ($arg eq "--cxxflags") {
83 $has_opt = 1; print "-I$PREFIX/include $CXXFLAGS\n";
84 } elsif ($arg eq "--ldflags") {
85 $has_opt = 1; print "-L$PREFIX/lib $LDFLAGS\n";
86 } elsif ($arg eq "--libs") {
87 $has_opt = 1; $want_libs = 1;
88 } elsif ($arg eq "--libnames") {
89 $has_opt = 1; $want_libnames = 1;
90 } elsif ($arg eq "--components") {
91 $has_opt = 1; print join(' ', name_map_entries), "\n";
92 } elsif ($arg eq "--targets-built") {
93 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
98 push @components, $arg;
102 # If no options were specified, fail.
103 usage unless $has_opt;
105 # If no components were specified, default to 'all'.
106 if (@components == 0) {
107 push @components, 'all';
110 # Handle any arguments which require building our dependency graph.
111 if ($want_libs || $want_libnames) {
112 my @libs = expand_dependecies(@components);
114 print join(' ', fix_library_names(@libs)), "\n";
116 if ($want_libnames) {
117 print join(' ', @libs), "\n";
123 #==========================================================================
125 #==========================================================================
128 print STDERR <<__EOD__;
129 Usage: llvm-config <OPTION>... [<COMPONENT>...]
131 Get various configuration information needed to compile programs which use
132 LLVM. Typically called from 'configure' scripts. Examples:
133 llvm-config --cxxflags
134 llvm-config --ldflags
135 llvm-config --libs engine bcreader scalaropts
138 --version LLVM version.
139 --prefix Installation prefix.
140 --bindir Directory containing LLVM executables.
141 --includedir Directory containing LLVM headers.
142 --libdir Directory containing LLVM libraries.
143 --cxxflags C++ compiler flags for files that include LLVM headers.
144 --ldflags Linker flags.
145 --libs Libraries needed to link against LLVM components.
146 --libnames Bare library names for in-tree builds.
147 --components List of all possible components.
148 --targets-built List of all targets currently built.
150 all All LLVM libraries (default).
151 backend Either a native backend or the C backend.
152 engine Either a native JIT or a bytecode interpreter.
157 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
158 # files which can't be found using -L.
159 sub fix_library_names (@) {
162 foreach my $lib (@libs) {
163 # Transform the bare library name appropriately.
164 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
165 if (defined $basename) {
166 push @result, "-l$basename";
168 push @result, "$PREFIX/lib/$lib";
175 #==========================================================================
176 # Library Dependency Analysis
177 #==========================================================================
178 # Given a few human-readable library names, find all their dependencies
179 # and sort them into an order which the linker will like. If we packed
180 # our libraries into fewer archives, we could make the linker do much
181 # of this work for us.
183 # Libraries have two different types of names in this code: Human-friendly
184 # "component" names entered on the command-line, and the raw file names
185 # we use internally (and ultimately pass to the linker).
187 # To understand this code, you'll need a working knowledge of Perl 5,
188 # and possibly some quality time with 'man perlref'.
190 sub load_dependencies;
192 sub have_native_backend;
193 sub find_best_engine;
194 sub expand_names (@);
195 sub find_all_required_sets (@);
196 sub find_all_required_sets_helper ($$@);
198 # Each "set" contains one or more libraries which must be included as a
199 # group (due to cyclic dependencies). Sets are represented as a Perl array
200 # reference pointing to a list of internal library names.
203 # Various mapping tables.
204 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
205 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
206 my %NAME_MAP; # Maps human-entered names to internal names.
208 # Have our dependencies been loaded yet?
209 my $DEPENDENCIES_LOADED = 0;
211 # Given a list of human-friendly component names, translate them into a
212 # complete set of linker arguments.
213 sub expand_dependecies (@) {
216 my @required_sets = find_all_required_sets(expand_names(@libs));
217 my @sorted_sets = topologically_sort_sets(@required_sets);
219 # Expand the library sets into libraries.
221 foreach my $set (@sorted_sets) { push @result, @{$set}; }
225 # Load in the raw dependency data stored at the end of this file.
226 sub load_dependencies {
227 return if $DEPENDENCIES_LOADED;
228 $DEPENDENCIES_LOADED = 1;
231 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
232 die "Malformed dependency data" unless defined $deps;
233 my @libs = split(' ', $libs);
234 my @deps = split(' ', $deps);
236 # Record our dependency data.
239 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
240 $SET_DEPS{$set} = \@deps;
245 # Build a map converting human-friendly component names into internal
248 # Add entries for all the actual libraries.
249 foreach my $set (@SETS) {
250 foreach my $lib (sort @$set) {
251 my $short_name = $lib;
252 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
253 $short_name =~ tr/A-Z/a-z/;
254 $NAME_MAP{$short_name} = [$lib];
258 # Add virtual entries.
259 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
260 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
261 $NAME_MAP{'engine'} = find_best_engine;
262 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
265 # Return true if we have a native backend to use.
266 sub have_native_backend {
268 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
269 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
272 # Find a working subclass of ExecutionEngine for this platform.
273 sub find_best_engine {
274 if (have_native_backend && $TARGET_HAS_JIT) {
275 return ['jit', 'native'];
277 return ['interpreter'];
281 # Get all the human-friendly component names.
282 sub name_map_entries {
284 return sort keys %NAME_MAP;
287 # Map human-readable names to internal library names.
288 sub expand_names (@) {
291 foreach my $name (@names) {
292 if (defined $LIB_TO_SET_MAP{$name}) {
293 # We've hit bottom: An actual library name.
295 } elsif (defined $NAME_MAP{$name}) {
296 # We've found a short name to expand.
297 push @result, expand_names(@{$NAME_MAP{$name}});
299 print STDERR "llvm-config: unknown component name: $name\n";
306 # Given a list of internal library names, return all sets of libraries which
307 # will need to be included by the linker (in no particular order).
308 sub find_all_required_sets (@) {
312 find_all_required_sets_helper(\%sets_added, \@result, @libs);
316 # Recursive closures are pretty broken in Perl, so we're going to separate
317 # this function from find_all_required_sets and pass in the state we need
318 # manually, as references. Yes, this is fairly unpleasant.
319 sub find_all_required_sets_helper ($$@) {
320 my ($sets_added, $result, @libs) = @_;
321 foreach my $lib (@libs) {
322 my $set = $LIB_TO_SET_MAP{$lib};
323 next if defined $$sets_added{$set};
324 $$sets_added{$set} = 1;
326 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
330 # Print a list of sets, with a label. Used for debugging.
331 sub print_sets ($@) {
332 my ($label, @sets) = @_;
334 foreach my $set (@sets) { push @output, join(',', @$set); }
335 print "$label: ", join(';', @output), "\n";
338 # Returns true if $lib is a key in $added.
339 sub has_lib_been_added ($$) {
340 my ($added, $lib) = @_;
341 return defined $$added{$LIB_TO_SET_MAP{$lib}};
344 # Returns true if all the dependencies of $set appear in $added.
345 sub have_all_deps_been_added ($$) {
346 my ($added, $set) = @_;
347 #print_sets(" Checking", $set);
348 #print_sets(" Wants", $SET_DEPS{$set});
349 foreach my $lib (@{$SET_DEPS{$set}}) {
350 return 0 unless has_lib_been_added($added, $lib);
355 # Given a list of sets, topologically sort them using dependencies.
356 sub topologically_sort_sets (@) {
360 SCAN: while (@sets) { # We'll delete items from @sets as we go.
361 #print_sets("So far", reverse(@result));
362 #print_sets("Remaining", @sets);
363 for (my $i = 0; $i < @sets; ++$i) {
365 if (have_all_deps_been_added(\%added, $set)) {
368 #print "Removing $i.\n";
369 splice(@sets, $i, 1);
370 next SCAN; # Restart our scan.
373 die "Can't find a library with no dependencies";
375 return reverse(@result);
378 # Our library dependency data will be added after the '__END__' token, and will
379 # be read through the magic <DATA> filehandle.