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.
16 #---- begin autoconf values ----
17 my $VERSION = q{@PACKAGE_VERSION@};
18 my $PREFIX = q{@LLVM_PREFIX@};
19 my $BINDIR = q{@LLVM_BINDIR@};
20 my $INCLUDEDIR = q{@LLVM_INCLUDEDIR@};
21 my $LIBDIR = q{@LLVM_LIBDIR@};
22 my $ARCH = lc(q{@ARCH@});
23 my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
24 #---- end autoconf values ----
26 #---- begin Makefile values ----
27 my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
28 my $LDFLAGS = q{@LLVM_LDFLAGS@};
29 my $CORE_IS_ARCHIVE = q{@CORE_IS_ARCHIVE@};
30 #---- end Makefile values ----
33 sub fix_library_names (@);
34 sub expand_dependecies (@);
37 # Parse our command-line arguments.
42 my $want_libnames = 0;
43 my $want_components = 0;
44 foreach my $arg (@ARGV) {
46 if ($arg eq "--version") {
47 $has_opt = 1; print "$VERSION\n";
48 } elsif ($arg eq "--prefix") {
49 $has_opt = 1; print "$PREFIX\n";
50 } elsif ($arg eq "--bindir") {
51 $has_opt = 1; print "$BINDIR\n";
52 } elsif ($arg eq "--includedir") {
53 $has_opt = 1; print "$INCLUDEDIR\n";
54 } elsif ($arg eq "--libdir") {
55 $has_opt = 1; print "$LIBDIR\n";
56 } elsif ($arg eq "--cxxflags") {
57 $has_opt = 1; print "-I$INCLUDEDIR $CXXFLAGS\n";
58 } elsif ($arg eq "--ldflags") {
59 $has_opt = 1; print "-L$LIBDIR $LDFLAGS\n";
60 } elsif ($arg eq "--libs") {
61 $has_opt = 1; $want_libs = 1;
62 } elsif ($arg eq "--libnames") {
63 $has_opt = 1; $want_libnames = 1;
64 } elsif ($arg eq "--components") {
65 $has_opt = 1; print join(' ', name_map_entries), "\n";
66 } elsif ($arg eq "--targets-built") {
67 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
72 push @components, $arg;
76 # If no options were specified, fail.
77 usage unless $has_opt;
79 # If no components were specified, default to 'all'.
80 if (@components == 0) {
81 push @components, 'all';
84 # Handle any arguments which require building our dependency graph.
85 if ($want_libs || $want_libnames) {
86 my @libs = expand_dependecies(@components);
88 print join(' ', fix_library_names(@libs)), "\n";
91 print join(' ', @libs), "\n";
97 #==========================================================================
99 #==========================================================================
102 print STDERR <<__EOD__;
103 Usage: llvm-config <OPTION>... [<COMPONENT>...]
105 Get various configuration information needed to compile programs which use
106 LLVM. Typically called from 'configure' scripts. Examples:
107 llvm-config --cxxflags
108 llvm-config --ldflags
109 llvm-config --libs jitplus
112 --version LLVM version.
113 --prefix Installation prefix.
114 --bindir Directory containing LLVM executables.
115 --includedir Directory containing LLVM headers.
116 --libdir Directory containing LLVM libraries.
117 --cxxflags C++ compiler flags for files that include LLVM headers.
118 --ldflags Linker flags.
119 --libs Libraries needed to link against LLVM components.
120 --libnames Bare library names for in-tree builds.
121 --components List of all possible components.
122 --targets-built List of all targets currently built.
124 all All LLVM libraries (default).
125 native A native-code backend for this platform, if any.
126 jitplus All libraries needed to use the LLVM JIT examples.
131 # Use -lfoo instead of libfoo.a whenever possible, and add directories to
132 # files which can't be found using -L.
133 sub fix_library_names (@) {
136 foreach my $lib (@libs) {
137 # Transform the bare library name appropriately.
138 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
139 if (defined $basename) {
140 push @result, "-l$basename";
142 push @result, "$LIBDIR/$lib";
149 #==========================================================================
150 # Library Dependency Analysis
151 #==========================================================================
152 # Given a few human-readable library names, find all their dependencies
153 # and sort them into an order which the linker will like. If we packed
154 # our libraries into fewer archives, we could make the linker do much
155 # of this work for us.
157 # Libraries have two different types of names in this code: Human-friendly
158 # "component" names entered on the command-line, and the raw file names
159 # we use internally (and ultimately pass to the linker).
161 # To understand this code, you'll need a working knowledge of Perl 5,
162 # and possibly some quality time with 'man perlref'.
164 sub load_dependencies;
166 sub find_native_platform;
167 sub expand_names (@);
168 sub find_all_required_sets (@);
169 sub find_all_required_sets_helper ($$@);
170 sub maybe_fix_core (@);
172 # Each "set" contains one or more libraries which must be included as a
173 # group (due to cyclic dependencies). Sets are represented as a Perl array
174 # reference pointing to a list of internal library names.
177 # Various mapping tables.
178 my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
179 my %SET_DEPS; # Maps sets to a list of libraries they depend on.
180 my %NAME_MAP; # Maps human-entered names to internal names.
182 # Have our dependencies been loaded yet?
183 my $DEPENDENCIES_LOADED = 0;
185 # Given a list of human-friendly component names, translate them into a
186 # complete set of linker arguments.
187 sub expand_dependecies (@) {
190 my @required_sets = find_all_required_sets(expand_names(@libs));
191 my @sorted_sets = topologically_sort_sets(@required_sets);
193 # Expand the library sets into libraries, and apply any
194 # platform-specific hackery.
196 foreach my $set (@sorted_sets) { push @result, @{$set}; }
197 return maybe_fix_core(@result);
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'} = find_native_platform;
235 $NAME_MAP{'jitplus'} = ['native', 'jit', 'bcreader', 'scalaropts'];
236 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
239 # Figure our what native platform we should use, if any.
240 sub find_native_platform {
242 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
243 if (defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH}) {
250 # Get all the human-friendly component names.
251 sub name_map_entries {
253 return sort keys %NAME_MAP;
256 # Map human-readable names to internal library names.
257 sub expand_names (@) {
260 foreach my $name (@names) {
261 if (defined $LIB_TO_SET_MAP{$name}) {
262 # We've hit bottom: An actual library name.
264 } elsif (defined $NAME_MAP{$name}) {
265 # We've found a short name to expand.
266 push @result, expand_names(@{$NAME_MAP{$name}});
268 print STDERR "llvm-config: unknown component name: $name\n";
275 # Given a list of internal library names, return all sets of libraries which
276 # will need to be included by the linker (in no particular order).
277 sub find_all_required_sets (@) {
281 find_all_required_sets_helper(\%sets_added, \@result, @libs);
285 # Recursive closures are pretty broken in Perl, so we're going to separate
286 # this function from find_all_required_sets and pass in the state we need
287 # manually, as references. Yes, this is fairly unpleasant.
288 sub find_all_required_sets_helper ($$@) {
289 my ($sets_added, $result, @libs) = @_;
290 foreach my $lib (@libs) {
291 my $set = $LIB_TO_SET_MAP{$lib};
292 next if defined $$sets_added{$set};
293 $$sets_added{$set} = 1;
295 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
299 # Print a list of sets, with a label. Used for debugging.
300 sub print_sets ($@) {
301 my ($label, @sets) = @_;
303 foreach my $set (@sets) { push @output, join(',', @$set); }
304 print "$label: ", join(';', @output), "\n";
307 # Returns true if $lib is a key in $added.
308 sub has_lib_been_added ($$) {
309 my ($added, $lib) = @_;
310 return defined $$added{$LIB_TO_SET_MAP{$lib}};
313 # Returns true if all the dependencies of $set appear in $added.
314 sub have_all_deps_been_added ($$) {
315 my ($added, $set) = @_;
316 #print_sets(" Checking", $set);
317 #print_sets(" Wants", $SET_DEPS{$set});
318 foreach my $lib (@{$SET_DEPS{$set}}) {
319 return 0 unless has_lib_been_added($added, $lib);
324 # Given a list of sets, topologically sort them using dependencies.
325 sub topologically_sort_sets (@) {
329 SCAN: while (@sets) { # We'll delete items from @sets as we go.
330 #print_sets("So far", reverse(@result));
331 #print_sets("Remaining", @sets);
332 for (my $i = 0; $i < @sets; ++$i) {
334 if (have_all_deps_been_added(\%added, $set)) {
337 #print "Removing $i.\n";
338 splice(@sets, $i, 1);
339 next SCAN; # Restart our scan.
342 die "Can't find a library with no dependencies";
344 return reverse(@result);
347 # Nasty hack to work around the fact that LLVMCore changes file type on
349 sub maybe_fix_core (@) {
352 foreach my $lib (@libs) {
353 if ($lib eq "LLVMCore.o" && $CORE_IS_ARCHIVE) {
354 push @result, "libLLVMCore.a";
362 # Our library dependency data will be added after the '__END__' token, and will
363 # be read through the magic <DATA> filehandle.