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 $ARCH = lc(q{@ARCH@});
21 my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
22 my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
23 #---- end autoconf values ----
25 #---- begin Makefile values ----
26 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
27 my $LDFLAGS = q{@LLVM_LDFLAGS@};
28 #---- end Makefile values ----
30 # Convert the current executable name into its directory (e.g. ".").
31 my ($PARTIALDIR) = ($0 =~ /^(.*)\/.*$/);
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 "--use-current-dir-as-prefix") {
50 # Convert the scripts executable dir into a full absolute directory.
51 my $ABSDIR = `cd $PARTIALDIR/..; pwd`;
54 } elsif ($arg eq "--prefix") {
55 $has_opt = 1; print "$PREFIX\n";
56 } elsif ($arg eq "--bindir") {
57 $has_opt = 1; print "$PREFIX/bin\n";
58 } elsif ($arg eq "--includedir") {
59 $has_opt = 1; print "$PREFIX/include\n";
60 } elsif ($arg eq "--libdir") {
61 $has_opt = 1; print "$PREFIX/lib\n";
62 } elsif ($arg eq "--cxxflags") {
63 $has_opt = 1; print "-I$PREFIX/include $CXXFLAGS\n";
64 } elsif ($arg eq "--ldflags") {
65 $has_opt = 1; print "-L$PREFIX/lib $LDFLAGS\n";
66 } elsif ($arg eq "--libs") {
67 $has_opt = 1; $want_libs = 1;
68 } elsif ($arg eq "--libnames") {
69 $has_opt = 1; $want_libnames = 1;
70 } elsif ($arg eq "--components") {
71 $has_opt = 1; print join(' ', name_map_entries), "\n";
72 } elsif ($arg eq "--targets-built") {
73 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
78 push @components, $arg;
82 # If no options were specified, fail.
83 usage unless $has_opt;
85 # If no components were specified, default to 'all'.
86 if (@components == 0) {
87 push @components, 'all';
90 # Handle any arguments which require building our dependency graph.
91 if ($want_libs || $want_libnames) {
92 my @libs = expand_dependecies(@components);
94 print join(' ', fix_library_names(@libs)), "\n";
97 print join(' ', @libs), "\n";
103 #==========================================================================
105 #==========================================================================
108 print STDERR <<__EOD__;
109 Usage: llvm-config <OPTION>... [<COMPONENT>...]
111 Get various configuration information needed to compile programs which use
112 LLVM. Typically called from 'configure' scripts. Examples:
113 llvm-config --cxxflags
114 llvm-config --ldflags
115 llvm-config --libs engine bcreader scalaropts
118 --version LLVM version.
119 --prefix Installation prefix.
120 --bindir Directory containing LLVM executables.
121 --includedir Directory containing LLVM headers.
122 --libdir Directory containing LLVM libraries.
123 --cxxflags C++ compiler flags for files that include LLVM headers.
124 --ldflags Linker flags.
125 --libs Libraries needed to link against LLVM components.
126 --libnames Bare library names for in-tree builds.
127 --components List of all possible components.
128 --targets-built List of all targets currently built.
130 all All LLVM libraries (default).
131 backend Either a native backend or the C backend.
132 engine Either a native JIT or a bytecode interpreter.
137 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
138 # files which can't be found using -L.
139 sub fix_library_names (@) {
142 foreach my $lib (@libs) {
143 # Transform the bare library name appropriately.
144 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
145 if (defined $basename) {
146 push @result, "-l$basename";
148 push @result, "$PREFIX/lib/$lib";
155 #==========================================================================
156 # Library Dependency Analysis
157 #==========================================================================
158 # Given a few human-readable library names, find all their dependencies
159 # and sort them into an order which the linker will like. If we packed
160 # our libraries into fewer archives, we could make the linker do much
161 # of this work for us.
163 # Libraries have two different types of names in this code: Human-friendly
164 # "component" names entered on the command-line, and the raw file names
165 # we use internally (and ultimately pass to the linker).
167 # To understand this code, you'll need a working knowledge of Perl 5,
168 # and possibly some quality time with 'man perlref'.
170 sub load_dependencies;
172 sub have_native_backend;
173 sub find_best_engine;
174 sub expand_names (@);
175 sub find_all_required_sets (@);
176 sub find_all_required_sets_helper ($$@);
178 # Each "set" contains one or more libraries which must be included as a
179 # group (due to cyclic dependencies). Sets are represented as a Perl array
180 # reference pointing to a list of internal library names.
183 # Various mapping tables.
184 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
185 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
186 my %NAME_MAP; # Maps human-entered names to internal names.
188 # Have our dependencies been loaded yet?
189 my $DEPENDENCIES_LOADED = 0;
191 # Given a list of human-friendly component names, translate them into a
192 # complete set of linker arguments.
193 sub expand_dependecies (@) {
196 my @required_sets = find_all_required_sets(expand_names(@libs));
197 my @sorted_sets = topologically_sort_sets(@required_sets);
199 # Expand the library sets into libraries.
201 foreach my $set (@sorted_sets) { push @result, @{$set}; }
205 # Load in the raw dependency data stored at the end of this file.
206 sub load_dependencies {
207 return if $DEPENDENCIES_LOADED;
208 $DEPENDENCIES_LOADED = 1;
211 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
212 die "Malformed dependency data" unless defined $deps;
213 my @libs = split(' ', $libs);
214 my @deps = split(' ', $deps);
216 # Record our dependency data.
219 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
220 $SET_DEPS{$set} = \@deps;
225 # Build a map converting human-friendly component names into internal
228 # Add entries for all the actual libraries.
229 foreach my $set (@SETS) {
230 foreach my $lib (sort @$set) {
231 my $short_name = $lib;
232 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
233 $short_name =~ tr/A-Z/a-z/;
234 $NAME_MAP{$short_name} = [$lib];
238 # Add virtual entries.
239 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
240 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
241 $NAME_MAP{'engine'} = find_best_engine;
242 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
245 # Return true if we have a native backend to use.
246 sub have_native_backend {
248 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
249 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
252 # Find a working subclass of ExecutionEngine for this platform.
253 sub find_best_engine {
254 if (have_native_backend && $TARGET_HAS_JIT) {
255 return ['jit', 'native'];
257 return ['interpreter'];
261 # Get all the human-friendly component names.
262 sub name_map_entries {
264 return sort keys %NAME_MAP;
267 # Map human-readable names to internal library names.
268 sub expand_names (@) {
271 foreach my $name (@names) {
272 if (defined $LIB_TO_SET_MAP{$name}) {
273 # We've hit bottom: An actual library name.
275 } elsif (defined $NAME_MAP{$name}) {
276 # We've found a short name to expand.
277 push @result, expand_names(@{$NAME_MAP{$name}});
279 print STDERR "llvm-config: unknown component name: $name\n";
286 # Given a list of internal library names, return all sets of libraries which
287 # will need to be included by the linker (in no particular order).
288 sub find_all_required_sets (@) {
292 find_all_required_sets_helper(\%sets_added, \@result, @libs);
296 # Recursive closures are pretty broken in Perl, so we're going to separate
297 # this function from find_all_required_sets and pass in the state we need
298 # manually, as references. Yes, this is fairly unpleasant.
299 sub find_all_required_sets_helper ($$@) {
300 my ($sets_added, $result, @libs) = @_;
301 foreach my $lib (@libs) {
302 my $set = $LIB_TO_SET_MAP{$lib};
303 next if defined $$sets_added{$set};
304 $$sets_added{$set} = 1;
306 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
310 # Print a list of sets, with a label. Used for debugging.
311 sub print_sets ($@) {
312 my ($label, @sets) = @_;
314 foreach my $set (@sets) { push @output, join(',', @$set); }
315 print "$label: ", join(';', @output), "\n";
318 # Returns true if $lib is a key in $added.
319 sub has_lib_been_added ($$) {
320 my ($added, $lib) = @_;
321 return defined $$added{$LIB_TO_SET_MAP{$lib}};
324 # Returns true if all the dependencies of $set appear in $added.
325 sub have_all_deps_been_added ($$) {
326 my ($added, $set) = @_;
327 #print_sets(" Checking", $set);
328 #print_sets(" Wants", $SET_DEPS{$set});
329 foreach my $lib (@{$SET_DEPS{$set}}) {
330 return 0 unless has_lib_been_added($added, $lib);
335 # Given a list of sets, topologically sort them using dependencies.
336 sub topologically_sort_sets (@) {
340 SCAN: while (@sets) { # We'll delete items from @sets as we go.
341 #print_sets("So far", reverse(@result));
342 #print_sets("Remaining", @sets);
343 for (my $i = 0; $i < @sets; ++$i) {
345 if (have_all_deps_been_added(\%added, $set)) {
348 #print "Removing $i.\n";
349 splice(@sets, $i, 1);
350 next SCAN; # Restart our scan.
353 die "Can't find a library with no dependencies";
355 return reverse(@result);
358 # Our library dependency data will be added after the '__END__' token, and will
359 # be read through the magic <DATA> filehandle.