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 $LLVM_CONFIGTIME = q{@LLVM_CONFIGTIME@};
22 my $LLVM_SRC_ROOT = q{@abs_top_srcdir@};
23 my $LLVM_OBJ_ROOT = q{@abs_top_builddir@};
24 my $LLVM_ON_WIN32 = q{@LLVM_ON_WIN32@};
25 my $LLVM_ON_UNIX = q{@LLVM_ON_UNIX@};
26 my $LLVMGCCDIR = q{@LLVMGCCDIR@};
27 my $LLVMGCC = q{@LLVMGCC@};
28 my $LLVMGXX = q{@LLVMGXX@};
29 my $LLVMGCC_VERSION = q{@LLVMGCC_VERSION@};
30 my $LLVMGCC_MAJVERS = q{@LLVMGCC_MAJVERS@};
31 my $ENDIAN = q{@ENDIAN@};
32 my $SHLIBEXT = q{@SHLIBEXT@};
33 my $EXEEXT = q{@EXEEXT@};
35 my $ARCH = lc(q{@ARCH@});
36 my $TARGET_TRIPLE = q{@target@};
37 my $TARGETS_TO_BUILD = q{@TARGETS_TO_BUILD@};
38 my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
39 my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
40 #---- end autoconf values ----
42 #---- begin Makefile values ----
43 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
44 my $LDFLAGS = q{@LLVM_LDFLAGS@};
45 #---- end Makefile values ----
47 # Convert the current executable name into its directory (e.g. ".").
48 my ($PARTIALDIR) = ($0 =~ /^(.*)\/.*$/);
51 sub fix_library_names (@);
52 sub expand_dependecies (@);
55 # Parse our command-line arguments.
60 my $want_libnames = 0;
61 my $want_components = 0;
62 foreach my $arg (@ARGV) {
64 if ($arg eq "--version") {
65 $has_opt = 1; print "$VERSION\n";
66 } elsif ($arg eq "--use-current-dir-as-prefix") {
67 # Convert the scripts executable dir into a full absolute directory.
68 my $ABSDIR = `cd $PARTIALDIR/..; pwd`;
71 } elsif ($arg eq "--prefix") {
72 $has_opt = 1; print "$PREFIX\n";
73 } elsif ($arg eq "--bindir") {
74 $has_opt = 1; print "$PREFIX/bin\n";
75 } elsif ($arg eq "--includedir") {
76 $has_opt = 1; print "$PREFIX/include\n";
77 } elsif ($arg eq "--libdir") {
78 $has_opt = 1; print "$PREFIX/lib\n";
79 } elsif ($arg eq "--cxxflags") {
80 $has_opt = 1; print "-I$PREFIX/include $CXXFLAGS\n";
81 } elsif ($arg eq "--ldflags") {
82 $has_opt = 1; print "-L$PREFIX/lib $LDFLAGS\n";
83 } elsif ($arg eq "--libs") {
84 $has_opt = 1; $want_libs = 1;
85 } elsif ($arg eq "--libnames") {
86 $has_opt = 1; $want_libnames = 1;
87 } elsif ($arg eq "--components") {
88 $has_opt = 1; print join(' ', name_map_entries), "\n";
89 } elsif ($arg eq "--targets-built") {
90 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
95 push @components, $arg;
99 # If no options were specified, fail.
100 usage unless $has_opt;
102 # If no components were specified, default to 'all'.
103 if (@components == 0) {
104 push @components, 'all';
107 # Handle any arguments which require building our dependency graph.
108 if ($want_libs || $want_libnames) {
109 my @libs = expand_dependecies(@components);
111 print join(' ', fix_library_names(@libs)), "\n";
113 if ($want_libnames) {
114 print join(' ', @libs), "\n";
120 #==========================================================================
122 #==========================================================================
125 print STDERR <<__EOD__;
126 Usage: llvm-config <OPTION>... [<COMPONENT>...]
128 Get various configuration information needed to compile programs which use
129 LLVM. Typically called from 'configure' scripts. Examples:
130 llvm-config --cxxflags
131 llvm-config --ldflags
132 llvm-config --libs engine bcreader scalaropts
135 --version LLVM version.
136 --prefix Installation prefix.
137 --bindir Directory containing LLVM executables.
138 --includedir Directory containing LLVM headers.
139 --libdir Directory containing LLVM libraries.
140 --cxxflags C++ compiler flags for files that include LLVM headers.
141 --ldflags Linker flags.
142 --libs Libraries needed to link against LLVM components.
143 --libnames Bare library names for in-tree builds.
144 --components List of all possible components.
145 --targets-built List of all targets currently built.
147 all All LLVM libraries (default).
148 backend Either a native backend or the C backend.
149 engine Either a native JIT or a bytecode interpreter.
154 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
155 # files which can't be found using -L.
156 sub fix_library_names (@) {
159 foreach my $lib (@libs) {
160 # Transform the bare library name appropriately.
161 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
162 if (defined $basename) {
163 push @result, "-l$basename";
165 push @result, "$PREFIX/lib/$lib";
172 #==========================================================================
173 # Library Dependency Analysis
174 #==========================================================================
175 # Given a few human-readable library names, find all their dependencies
176 # and sort them into an order which the linker will like. If we packed
177 # our libraries into fewer archives, we could make the linker do much
178 # of this work for us.
180 # Libraries have two different types of names in this code: Human-friendly
181 # "component" names entered on the command-line, and the raw file names
182 # we use internally (and ultimately pass to the linker).
184 # To understand this code, you'll need a working knowledge of Perl 5,
185 # and possibly some quality time with 'man perlref'.
187 sub load_dependencies;
189 sub have_native_backend;
190 sub find_best_engine;
191 sub expand_names (@);
192 sub find_all_required_sets (@);
193 sub find_all_required_sets_helper ($$@);
195 # Each "set" contains one or more libraries which must be included as a
196 # group (due to cyclic dependencies). Sets are represented as a Perl array
197 # reference pointing to a list of internal library names.
200 # Various mapping tables.
201 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
202 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
203 my %NAME_MAP; # Maps human-entered names to internal names.
205 # Have our dependencies been loaded yet?
206 my $DEPENDENCIES_LOADED = 0;
208 # Given a list of human-friendly component names, translate them into a
209 # complete set of linker arguments.
210 sub expand_dependecies (@) {
213 my @required_sets = find_all_required_sets(expand_names(@libs));
214 my @sorted_sets = topologically_sort_sets(@required_sets);
216 # Expand the library sets into libraries.
218 foreach my $set (@sorted_sets) { push @result, @{$set}; }
222 # Load in the raw dependency data stored at the end of this file.
223 sub load_dependencies {
224 return if $DEPENDENCIES_LOADED;
225 $DEPENDENCIES_LOADED = 1;
228 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
229 die "Malformed dependency data" unless defined $deps;
230 my @libs = split(' ', $libs);
231 my @deps = split(' ', $deps);
233 # Record our dependency data.
236 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
237 $SET_DEPS{$set} = \@deps;
242 # Build a map converting human-friendly component names into internal
245 # Add entries for all the actual libraries.
246 foreach my $set (@SETS) {
247 foreach my $lib (sort @$set) {
248 my $short_name = $lib;
249 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
250 $short_name =~ tr/A-Z/a-z/;
251 $NAME_MAP{$short_name} = [$lib];
255 # Add virtual entries.
256 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
257 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
258 $NAME_MAP{'engine'} = find_best_engine;
259 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
262 # Return true if we have a native backend to use.
263 sub have_native_backend {
265 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
266 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
269 # Find a working subclass of ExecutionEngine for this platform.
270 sub find_best_engine {
271 if (have_native_backend && $TARGET_HAS_JIT) {
272 return ['jit', 'native'];
274 return ['interpreter'];
278 # Get all the human-friendly component names.
279 sub name_map_entries {
281 return sort keys %NAME_MAP;
284 # Map human-readable names to internal library names.
285 sub expand_names (@) {
288 foreach my $name (@names) {
289 if (defined $LIB_TO_SET_MAP{$name}) {
290 # We've hit bottom: An actual library name.
292 } elsif (defined $NAME_MAP{$name}) {
293 # We've found a short name to expand.
294 push @result, expand_names(@{$NAME_MAP{$name}});
296 print STDERR "llvm-config: unknown component name: $name\n";
303 # Given a list of internal library names, return all sets of libraries which
304 # will need to be included by the linker (in no particular order).
305 sub find_all_required_sets (@) {
309 find_all_required_sets_helper(\%sets_added, \@result, @libs);
313 # Recursive closures are pretty broken in Perl, so we're going to separate
314 # this function from find_all_required_sets and pass in the state we need
315 # manually, as references. Yes, this is fairly unpleasant.
316 sub find_all_required_sets_helper ($$@) {
317 my ($sets_added, $result, @libs) = @_;
318 foreach my $lib (@libs) {
319 my $set = $LIB_TO_SET_MAP{$lib};
320 next if defined $$sets_added{$set};
321 $$sets_added{$set} = 1;
323 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
327 # Print a list of sets, with a label. Used for debugging.
328 sub print_sets ($@) {
329 my ($label, @sets) = @_;
331 foreach my $set (@sets) { push @output, join(',', @$set); }
332 print "$label: ", join(';', @output), "\n";
335 # Returns true if $lib is a key in $added.
336 sub has_lib_been_added ($$) {
337 my ($added, $lib) = @_;
338 return defined $$added{$LIB_TO_SET_MAP{$lib}};
341 # Returns true if all the dependencies of $set appear in $added.
342 sub have_all_deps_been_added ($$) {
343 my ($added, $set) = @_;
344 #print_sets(" Checking", $set);
345 #print_sets(" Wants", $SET_DEPS{$set});
346 foreach my $lib (@{$SET_DEPS{$set}}) {
347 return 0 unless has_lib_been_added($added, $lib);
352 # Given a list of sets, topologically sort them using dependencies.
353 sub topologically_sort_sets (@) {
357 SCAN: while (@sets) { # We'll delete items from @sets as we go.
358 #print_sets("So far", reverse(@result));
359 #print_sets("Remaining", @sets);
360 for (my $i = 0; $i < @sets; ++$i) {
362 if (have_all_deps_been_added(\%added, $set)) {
365 #print "Removing $i.\n";
366 splice(@sets, $i, 1);
367 next SCAN; # Restart our scan.
370 die "Can't find a library with no dependencies";
372 return reverse(@result);
375 # Our library dependency data will be added after the '__END__' token, and will
376 # be read through the magic <DATA> filehandle.