5 # Synopsis: Prints out compiler options needed to build against an installed
8 # Syntax: lllvm-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 = q{@LLVM_BINDIR@};
21 my $INCLUDEDIR = q{@LLVM_INCLUDEDIR@};
22 my $LIBDIR = q{@LLVM_LIBDIR@};
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 my $CORE_IS_ARCHIVE = q{@CORE_IS_ARCHIVE@};
32 #---- end Makefile values ----
35 sub fix_library_names (@);
36 sub expand_dependecies (@);
39 # Parse our command-line arguments.
44 my $want_libnames = 0;
45 my $want_components = 0;
46 foreach my $arg (@ARGV) {
48 if ($arg eq "--version") {
49 $has_opt = 1; print "$VERSION\n";
50 } elsif ($arg eq "--prefix") {
51 $has_opt = 1; print "$PREFIX\n";
52 } elsif ($arg eq "--bindir") {
53 $has_opt = 1; print "$BINDIR\n";
54 } elsif ($arg eq "--includedir") {
55 $has_opt = 1; print "$INCLUDEDIR\n";
56 } elsif ($arg eq "--libdir") {
57 $has_opt = 1; print "$LIBDIR\n";
58 } elsif ($arg eq "--cxxflags") {
59 $has_opt = 1; print "-I$INCLUDEDIR $CXXFLAGS\n";
60 } elsif ($arg eq "--ldflags") {
61 $has_opt = 1; print "-L$LIBDIR $LDFLAGS\n";
62 } elsif ($arg eq "--libs") {
63 $has_opt = 1; $want_libs = 1;
64 } elsif ($arg eq "--libnames") {
65 $has_opt = 1; $want_libnames = 1;
66 } elsif ($arg eq "--components") {
67 $has_opt = 1; print join(' ', name_map_entries), "\n";
68 } elsif ($arg eq "--targets-built") {
69 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
74 push @components, $arg;
78 # If no options were specified, fail.
79 usage unless $has_opt;
81 # If no components were specified, default to 'all'.
82 if (@components == 0) {
83 push @components, 'all';
86 # Handle any arguments which require building our dependency graph.
87 if ($want_libs || $want_libnames) {
88 my @libs = expand_dependecies(@components);
90 print join(' ', fix_library_names(@libs)), "\n";
93 print join(' ', @libs), "\n";
99 #==========================================================================
101 #==========================================================================
104 print STDERR <<__EOD__;
105 Usage: llvm-config <OPTION>... [<COMPONENT>...]
107 Get various configuration information needed to compile programs which use
108 LLVM. Typically called from 'configure' scripts. Examples:
109 llvm-config --cxxflags
110 llvm-config --ldflags
111 llvm-config --libs engine bcreader scalaropts
114 --version LLVM version.
115 --prefix Installation prefix.
116 --bindir Directory containing LLVM executables.
117 --includedir Directory containing LLVM headers.
118 --libdir Directory containing LLVM libraries.
119 --cxxflags C++ compiler flags for files that include LLVM headers.
120 --ldflags Linker flags.
121 --libs Libraries needed to link against LLVM components.
122 --libnames Bare library names for in-tree builds.
123 --components List of all possible components.
124 --targets-built List of all targets currently built.
126 all All LLVM libraries (default).
127 backend Either a native backend or the C backend.
128 engine Either a native JIT or a bytecode interpreter.
133 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
134 # files which can't be found using -L.
135 sub fix_library_names (@) {
138 foreach my $lib (@libs) {
139 # Transform the bare library name appropriately.
140 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
141 if (defined $basename) {
142 push @result, "-l$basename";
144 push @result, "$LIBDIR/$lib";
151 #==========================================================================
152 # Library Dependency Analysis
153 #==========================================================================
154 # Given a few human-readable library names, find all their dependencies
155 # and sort them into an order which the linker will like. If we packed
156 # our libraries into fewer archives, we could make the linker do much
157 # of this work for us.
159 # Libraries have two different types of names in this code: Human-friendly
160 # "component" names entered on the command-line, and the raw file names
161 # we use internally (and ultimately pass to the linker).
163 # To understand this code, you'll need a working knowledge of Perl 5,
164 # and possibly some quality time with 'man perlref'.
166 sub load_dependencies;
168 sub have_native_backend;
169 sub find_best_engine;
170 sub expand_names (@);
171 sub find_all_required_sets (@);
172 sub find_all_required_sets_helper ($$@);
173 sub maybe_fix_core (@);
175 # Each "set" contains one or more libraries which must be included as a
176 # group (due to cyclic dependencies). Sets are represented as a Perl array
177 # reference pointing to a list of internal library names.
180 # Various mapping tables.
181 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
182 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
183 my %NAME_MAP; # Maps human-entered names to internal names.
185 # Have our dependencies been loaded yet?
186 my $DEPENDENCIES_LOADED = 0;
188 # Given a list of human-friendly component names, translate them into a
189 # complete set of linker arguments.
190 sub expand_dependecies (@) {
193 my @required_sets = find_all_required_sets(expand_names(@libs));
194 my @sorted_sets = topologically_sort_sets(@required_sets);
196 # Expand the library sets into libraries, and apply any
197 # platform-specific hackery.
199 foreach my $set (@sorted_sets) { push @result, @{$set}; }
200 return maybe_fix_core(@result);
203 # Load in the raw dependency data stored at the end of this file.
204 sub load_dependencies {
205 return if $DEPENDENCIES_LOADED;
206 $DEPENDENCIES_LOADED = 1;
209 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
210 die "Malformed dependency data" unless defined $deps;
211 my @libs = split(' ', $libs);
212 my @deps = split(' ', $deps);
214 # Record our dependency data.
217 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
218 $SET_DEPS{$set} = \@deps;
223 # Build a map converting human-friendly component names into internal
226 # Add entries for all the actual libraries.
227 foreach my $set (@SETS) {
228 foreach my $lib (sort @$set) {
229 my $short_name = $lib;
230 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
231 $short_name =~ tr/A-Z/a-z/;
232 $NAME_MAP{$short_name} = [$lib];
236 # Add virtual entries.
237 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
238 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
239 $NAME_MAP{'engine'} = find_best_engine;
240 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
243 # Return true if we have a native backend to use.
244 sub have_native_backend {
246 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
247 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
250 # Find a working subclass of ExecutionEngine for this platform.
251 sub find_best_engine {
252 if (have_native_backend && $TARGET_HAS_JIT) {
253 return ['jit', 'native'];
255 return ['interpreter'];
259 # Get all the human-friendly component names.
260 sub name_map_entries {
262 return sort keys %NAME_MAP;
265 # Map human-readable names to internal library names.
266 sub expand_names (@) {
269 foreach my $name (@names) {
270 if (defined $LIB_TO_SET_MAP{$name}) {
271 # We've hit bottom: An actual library name.
273 } elsif (defined $NAME_MAP{$name}) {
274 # We've found a short name to expand.
275 push @result, expand_names(@{$NAME_MAP{$name}});
277 print STDERR "llvm-config: unknown component name: $name\n";
284 # Given a list of internal library names, return all sets of libraries which
285 # will need to be included by the linker (in no particular order).
286 sub find_all_required_sets (@) {
290 find_all_required_sets_helper(\%sets_added, \@result, @libs);
294 # Recursive closures are pretty broken in Perl, so we're going to separate
295 # this function from find_all_required_sets and pass in the state we need
296 # manually, as references. Yes, this is fairly unpleasant.
297 sub find_all_required_sets_helper ($$@) {
298 my ($sets_added, $result, @libs) = @_;
299 foreach my $lib (@libs) {
300 my $set = $LIB_TO_SET_MAP{$lib};
301 next if defined $$sets_added{$set};
302 $$sets_added{$set} = 1;
304 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
308 # Print a list of sets, with a label. Used for debugging.
309 sub print_sets ($@) {
310 my ($label, @sets) = @_;
312 foreach my $set (@sets) { push @output, join(',', @$set); }
313 print "$label: ", join(';', @output), "\n";
316 # Returns true if $lib is a key in $added.
317 sub has_lib_been_added ($$) {
318 my ($added, $lib) = @_;
319 return defined $$added{$LIB_TO_SET_MAP{$lib}};
322 # Returns true if all the dependencies of $set appear in $added.
323 sub have_all_deps_been_added ($$) {
324 my ($added, $set) = @_;
325 #print_sets(" Checking", $set);
326 #print_sets(" Wants", $SET_DEPS{$set});
327 foreach my $lib (@{$SET_DEPS{$set}}) {
328 return 0 unless has_lib_been_added($added, $lib);
333 # Given a list of sets, topologically sort them using dependencies.
334 sub topologically_sort_sets (@) {
338 SCAN: while (@sets) { # We'll delete items from @sets as we go.
339 #print_sets("So far", reverse(@result));
340 #print_sets("Remaining", @sets);
341 for (my $i = 0; $i < @sets; ++$i) {
343 if (have_all_deps_been_added(\%added, $set)) {
346 #print "Removing $i.\n";
347 splice(@sets, $i, 1);
348 next SCAN; # Restart our scan.
351 die "Can't find a library with no dependencies";
353 return reverse(@result);
356 # Nasty hack to work around the fact that LLVMCore changes file type on
358 sub maybe_fix_core (@) {
361 foreach my $lib (@libs) {
362 if ($lib eq "LLVMCore.o" && $CORE_IS_ARCHIVE) {
363 push @result, "libLLVMCore.a";
371 # Our library dependency data will be added after the '__END__' token, and will
372 # be read through the magic <DATA> filehandle.