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 $VERSION = q{@PACKAGE_VERSION@};
19 my $PREFIX = q{@LLVM_PREFIX@};
20 my $BINDIR = "$PREFIX/bin";
21 my $INCLUDEDIR = "$PREFIX/include";
22 my $LIBDIR = "$PREFIX/lib";
23 my $ARCH = lc(q{@ARCH@});
24 my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
25 my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
26 #---- end autoconf values ----
28 #---- begin Makefile values ----
29 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
30 my $LDFLAGS = q{@LLVM_LDFLAGS@};
31 #---- end Makefile values ----
34 sub fix_library_names (@);
35 sub expand_dependecies (@);
38 # Parse our command-line arguments.
43 my $want_libnames = 0;
44 my $want_components = 0;
45 foreach my $arg (@ARGV) {
47 if ($arg eq "--version") {
48 $has_opt = 1; print "$VERSION\n";
49 } elsif ($arg eq "--prefix") {
50 $has_opt = 1; print "$PREFIX\n";
51 } elsif ($arg eq "--bindir") {
52 $has_opt = 1; print "$BINDIR\n";
53 } elsif ($arg eq "--includedir") {
54 $has_opt = 1; print "$INCLUDEDIR\n";
55 } elsif ($arg eq "--libdir") {
56 $has_opt = 1; print "$LIBDIR\n";
57 } elsif ($arg eq "--cxxflags") {
58 $has_opt = 1; print "-I$INCLUDEDIR $CXXFLAGS\n";
59 } elsif ($arg eq "--ldflags") {
60 $has_opt = 1; print "-L$LIBDIR $LDFLAGS\n";
61 } elsif ($arg eq "--libs") {
62 $has_opt = 1; $want_libs = 1;
63 } elsif ($arg eq "--libnames") {
64 $has_opt = 1; $want_libnames = 1;
65 } elsif ($arg eq "--components") {
66 $has_opt = 1; print join(' ', name_map_entries), "\n";
67 } elsif ($arg eq "--targets-built") {
68 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
73 push @components, $arg;
77 # If no options were specified, fail.
78 usage unless $has_opt;
80 # If no components were specified, default to 'all'.
81 if (@components == 0) {
82 push @components, 'all';
85 # Handle any arguments which require building our dependency graph.
86 if ($want_libs || $want_libnames) {
87 my @libs = expand_dependecies(@components);
89 print join(' ', fix_library_names(@libs)), "\n";
92 print join(' ', @libs), "\n";
98 #==========================================================================
100 #==========================================================================
103 print STDERR <<__EOD__;
104 Usage: llvm-config <OPTION>... [<COMPONENT>...]
106 Get various configuration information needed to compile programs which use
107 LLVM. Typically called from 'configure' scripts. Examples:
108 llvm-config --cxxflags
109 llvm-config --ldflags
110 llvm-config --libs engine bcreader scalaropts
113 --version LLVM version.
114 --prefix Installation prefix.
115 --bindir Directory containing LLVM executables.
116 --includedir Directory containing LLVM headers.
117 --libdir Directory containing LLVM libraries.
118 --cxxflags C++ compiler flags for files that include LLVM headers.
119 --ldflags Linker flags.
120 --libs Libraries needed to link against LLVM components.
121 --libnames Bare library names for in-tree builds.
122 --components List of all possible components.
123 --targets-built List of all targets currently built.
125 all All LLVM libraries (default).
126 backend Either a native backend or the C backend.
127 engine Either a native JIT or a bytecode interpreter.
132 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
133 # files which can't be found using -L.
134 sub fix_library_names (@) {
137 foreach my $lib (@libs) {
138 # Transform the bare library name appropriately.
139 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
140 if (defined $basename) {
141 push @result, "-l$basename";
143 push @result, "$LIBDIR/$lib";
150 #==========================================================================
151 # Library Dependency Analysis
152 #==========================================================================
153 # Given a few human-readable library names, find all their dependencies
154 # and sort them into an order which the linker will like. If we packed
155 # our libraries into fewer archives, we could make the linker do much
156 # of this work for us.
158 # Libraries have two different types of names in this code: Human-friendly
159 # "component" names entered on the command-line, and the raw file names
160 # we use internally (and ultimately pass to the linker).
162 # To understand this code, you'll need a working knowledge of Perl 5,
163 # and possibly some quality time with 'man perlref'.
165 sub load_dependencies;
167 sub have_native_backend;
168 sub find_best_engine;
169 sub expand_names (@);
170 sub find_all_required_sets (@);
171 sub find_all_required_sets_helper ($$@);
173 # Each "set" contains one or more libraries which must be included as a
174 # group (due to cyclic dependencies). Sets are represented as a Perl array
175 # reference pointing to a list of internal library names.
178 # Various mapping tables.
179 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
180 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
181 my %NAME_MAP; # Maps human-entered names to internal names.
183 # Have our dependencies been loaded yet?
184 my $DEPENDENCIES_LOADED = 0;
186 # Given a list of human-friendly component names, translate them into a
187 # complete set of linker arguments.
188 sub expand_dependecies (@) {
191 my @required_sets = find_all_required_sets(expand_names(@libs));
192 my @sorted_sets = topologically_sort_sets(@required_sets);
194 # Expand the library sets into libraries.
196 foreach my $set (@sorted_sets) { push @result, @{$set}; }
200 # Load in the raw dependency data stored at the end of this file.
201 sub load_dependencies {
202 return if $DEPENDENCIES_LOADED;
203 $DEPENDENCIES_LOADED = 1;
206 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
207 die "Malformed dependency data" unless defined $deps;
208 my @libs = split(' ', $libs);
209 my @deps = split(' ', $deps);
211 # Record our dependency data.
214 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
215 $SET_DEPS{$set} = \@deps;
220 # Build a map converting human-friendly component names into internal
223 # Add entries for all the actual libraries.
224 foreach my $set (@SETS) {
225 foreach my $lib (sort @$set) {
226 my $short_name = $lib;
227 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
228 $short_name =~ tr/A-Z/a-z/;
229 $NAME_MAP{$short_name} = [$lib];
233 # Add virtual entries.
234 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
235 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
236 $NAME_MAP{'engine'} = find_best_engine;
237 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
240 # Return true if we have a native backend to use.
241 sub have_native_backend {
243 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
244 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
247 # Find a working subclass of ExecutionEngine for this platform.
248 sub find_best_engine {
249 if (have_native_backend && $TARGET_HAS_JIT) {
250 return ['jit', 'native'];
252 return ['interpreter'];
256 # Get all the human-friendly component names.
257 sub name_map_entries {
259 return sort keys %NAME_MAP;
262 # Map human-readable names to internal library names.
263 sub expand_names (@) {
266 foreach my $name (@names) {
267 if (defined $LIB_TO_SET_MAP{$name}) {
268 # We've hit bottom: An actual library name.
270 } elsif (defined $NAME_MAP{$name}) {
271 # We've found a short name to expand.
272 push @result, expand_names(@{$NAME_MAP{$name}});
274 print STDERR "llvm-config: unknown component name: $name\n";
281 # Given a list of internal library names, return all sets of libraries which
282 # will need to be included by the linker (in no particular order).
283 sub find_all_required_sets (@) {
287 find_all_required_sets_helper(\%sets_added, \@result, @libs);
291 # Recursive closures are pretty broken in Perl, so we're going to separate
292 # this function from find_all_required_sets and pass in the state we need
293 # manually, as references. Yes, this is fairly unpleasant.
294 sub find_all_required_sets_helper ($$@) {
295 my ($sets_added, $result, @libs) = @_;
296 foreach my $lib (@libs) {
297 my $set = $LIB_TO_SET_MAP{$lib};
298 next if defined $$sets_added{$set};
299 $$sets_added{$set} = 1;
301 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
305 # Print a list of sets, with a label. Used for debugging.
306 sub print_sets ($@) {
307 my ($label, @sets) = @_;
309 foreach my $set (@sets) { push @output, join(',', @$set); }
310 print "$label: ", join(';', @output), "\n";
313 # Returns true if $lib is a key in $added.
314 sub has_lib_been_added ($$) {
315 my ($added, $lib) = @_;
316 return defined $$added{$LIB_TO_SET_MAP{$lib}};
319 # Returns true if all the dependencies of $set appear in $added.
320 sub have_all_deps_been_added ($$) {
321 my ($added, $set) = @_;
322 #print_sets(" Checking", $set);
323 #print_sets(" Wants", $SET_DEPS{$set});
324 foreach my $lib (@{$SET_DEPS{$set}}) {
325 return 0 unless has_lib_been_added($added, $lib);
330 # Given a list of sets, topologically sort them using dependencies.
331 sub topologically_sort_sets (@) {
335 SCAN: while (@sets) { # We'll delete items from @sets as we go.
336 #print_sets("So far", reverse(@result));
337 #print_sets("Remaining", @sets);
338 for (my $i = 0; $i < @sets; ++$i) {
340 if (have_all_deps_been_added(\%added, $set)) {
343 #print "Removing $i.\n";
344 splice(@sets, $i, 1);
345 next SCAN; # Restart our scan.
348 die "Can't find a library with no dependencies";
350 return reverse(@result);
353 # Our library dependency data will be added after the '__END__' token, and will
354 # be read through the magic <DATA> filehandle.