blob: 74bf7a9870eb98aec7022a838a429af2384e199c [file] [log] [blame]
Reid Spencerb195d9d2006-03-23 23:21:29 +00001#!@PERL@
Reid Spencerf2722ca2006-03-22 15:59:55 +00002#
3# Program: llvm-config
4#
5# Synopsis: Prints out compiler options needed to build against an installed
6# copy of LLVM.
7#
8# Syntax: lllvm-config OPTIONS... [COMPONENTS...]
9#
10# This file was written by Eric Kidd, and is placed into the public domain.
11#
12
Reid Spencerb195d9d2006-03-23 23:21:29 +000013use 5.006;
Reid Spencerf2722ca2006-03-22 15:59:55 +000014use strict;
15use warnings;
16
17#---- begin autoconf values ----
18my $VERSION = q{@PACKAGE_VERSION@};
19my $PREFIX = q{@LLVM_PREFIX@};
20my $BINDIR = q{@LLVM_BINDIR@};
21my $INCLUDEDIR = q{@LLVM_INCLUDEDIR@};
22my $LIBDIR = q{@LLVM_LIBDIR@};
23my $ARCH = lc(q{@ARCH@});
Reid Spencerb195d9d2006-03-23 23:21:29 +000024my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000025my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
26#---- end autoconf values ----
27
28#---- begin Makefile values ----
29my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
30my $LDFLAGS = q{@LLVM_LDFLAGS@};
31my $CORE_IS_ARCHIVE = q{@CORE_IS_ARCHIVE@};
32#---- end Makefile values ----
33
34sub usage;
35sub fix_library_names (@);
36sub expand_dependecies (@);
37sub name_map_entries;
38
39# Parse our command-line arguments.
40usage if @ARGV == 0;
41my @components;
42my $has_opt = 0;
43my $want_libs = 0;
44my $want_libnames = 0;
45my $want_components = 0;
46foreach my $arg (@ARGV) {
47 if ($arg =~ /^-/) {
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";
70 } else {
71 usage();
72 }
73 } else {
74 push @components, $arg;
75 }
76}
77
78# If no options were specified, fail.
79usage unless $has_opt;
80
81# If no components were specified, default to 'all'.
82if (@components == 0) {
83 push @components, 'all';
84}
85
86# Handle any arguments which require building our dependency graph.
87if ($want_libs || $want_libnames) {
88 my @libs = expand_dependecies(@components);
89 if ($want_libs) {
90 print join(' ', fix_library_names(@libs)), "\n";
91 }
92 if ($want_libnames) {
93 print join(' ', @libs), "\n";
94 }
95}
96
97exit 0;
98
99#==========================================================================
100# Support Routines
101#==========================================================================
102
103sub usage {
104 print STDERR <<__EOD__;
105Usage: llvm-config <OPTION>... [<COMPONENT>...]
106
107Get various configuration information needed to compile programs which use
108LLVM. Typically called from 'configure' scripts. Examples:
109 llvm-config --cxxflags
110 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000111 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000112
113Options:
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.
125Typical components:
126 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000127 backend Either a native backend or the C backend.
128 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000129__EOD__
130 exit(1);
131}
132
133# Use -lfoo instead of libfoo.a whenever possible, and add directories to
134# files which can't be found using -L.
135sub fix_library_names (@) {
136 my @libs = @_;
137 my @result;
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";
143 } else {
144 push @result, "$LIBDIR/$lib";
145 }
146 }
147 return @result;
148}
149
150
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.
158#
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).
162#
163# To understand this code, you'll need a working knowledge of Perl 5,
164# and possibly some quality time with 'man perlref'.
165
166sub load_dependencies;
167sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000168sub have_native_backend;
169sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000170sub expand_names (@);
171sub find_all_required_sets (@);
172sub find_all_required_sets_helper ($$@);
173sub maybe_fix_core (@);
174
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.
178my @SETS;
179
180# Various mapping tables.
181my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
182my %SET_DEPS; # Maps sets to a list of libraries they depend on.
183my %NAME_MAP; # Maps human-entered names to internal names.
184
185# Have our dependencies been loaded yet?
186my $DEPENDENCIES_LOADED = 0;
187
188# Given a list of human-friendly component names, translate them into a
189# complete set of linker arguments.
190sub expand_dependecies (@) {
191 my @libs = @_;
192 load_dependencies;
193 my @required_sets = find_all_required_sets(expand_names(@libs));
194 my @sorted_sets = topologically_sort_sets(@required_sets);
195
196 # Expand the library sets into libraries, and apply any
197 # platform-specific hackery.
198 my @result;
199 foreach my $set (@sorted_sets) { push @result, @{$set}; }
200 return maybe_fix_core(@result);
201}
202
203# Load in the raw dependency data stored at the end of this file.
204sub load_dependencies {
205 return if $DEPENDENCIES_LOADED;
206 $DEPENDENCIES_LOADED = 1;
207 while (<DATA>) {
208 # Parse our line.
209 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
210 die "Malformed dependency data" unless defined $deps;
211 my @libs = split(' ', $libs);
212 my @deps = split(' ', $deps);
213
214 # Record our dependency data.
215 my $set = \@libs;
216 push @SETS, $set;
217 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
218 $SET_DEPS{$set} = \@deps;
219 }
220 build_name_map;
221}
222
223# Build a map converting human-friendly component names into internal
224# library names.
225sub build_name_map {
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];
233 }
234 }
235
236 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000237 $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.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000241}
242
Reid Spencerb195d9d2006-03-23 23:21:29 +0000243# Return true if we have a native backend to use.
244sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000245 my %BUILT;
246 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000247 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
248}
249
250# Find a working subclass of ExecutionEngine for this platform.
251sub find_best_engine {
252 if (have_native_backend && $TARGET_HAS_JIT) {
253 # XXX - Right now, if we omit the interpreter, we get a linker
254 # error complaining about
255 # __ZN4llvm11Interpreter6createEPNS_6ModuleEPNS_17IntrinsicLoweringE.
256 # This needs investigation.
257 return ['jit', 'native', 'interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000258 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000259 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000260 }
261}
262
263# Get all the human-friendly component names.
264sub name_map_entries {
265 load_dependencies;
266 return sort keys %NAME_MAP;
267}
268
269# Map human-readable names to internal library names.
270sub expand_names (@) {
271 my @names = @_;
272 my @result;
273 foreach my $name (@names) {
274 if (defined $LIB_TO_SET_MAP{$name}) {
275 # We've hit bottom: An actual library name.
276 push @result, $name;
277 } elsif (defined $NAME_MAP{$name}) {
278 # We've found a short name to expand.
279 push @result, expand_names(@{$NAME_MAP{$name}});
280 } else {
281 print STDERR "llvm-config: unknown component name: $name\n";
282 exit(1);
283 }
284 }
285 return @result;
286}
287
288# Given a list of internal library names, return all sets of libraries which
289# will need to be included by the linker (in no particular order).
290sub find_all_required_sets (@) {
291 my @libs = @_;
292 my %sets_added;
293 my @result;
294 find_all_required_sets_helper(\%sets_added, \@result, @libs);
295 return @result;
296}
297
298# Recursive closures are pretty broken in Perl, so we're going to separate
299# this function from find_all_required_sets and pass in the state we need
300# manually, as references. Yes, this is fairly unpleasant.
301sub find_all_required_sets_helper ($$@) {
302 my ($sets_added, $result, @libs) = @_;
303 foreach my $lib (@libs) {
304 my $set = $LIB_TO_SET_MAP{$lib};
305 next if defined $$sets_added{$set};
306 $$sets_added{$set} = 1;
307 push @$result, $set;
308 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
309 }
310}
311
312# Print a list of sets, with a label. Used for debugging.
313sub print_sets ($@) {
314 my ($label, @sets) = @_;
315 my @output;
316 foreach my $set (@sets) { push @output, join(',', @$set); }
317 print "$label: ", join(';', @output), "\n";
318}
319
320# Returns true if $lib is a key in $added.
321sub has_lib_been_added ($$) {
322 my ($added, $lib) = @_;
323 return defined $$added{$LIB_TO_SET_MAP{$lib}};
324}
325
326# Returns true if all the dependencies of $set appear in $added.
327sub have_all_deps_been_added ($$) {
328 my ($added, $set) = @_;
329 #print_sets(" Checking", $set);
330 #print_sets(" Wants", $SET_DEPS{$set});
331 foreach my $lib (@{$SET_DEPS{$set}}) {
332 return 0 unless has_lib_been_added($added, $lib);
333 }
334 return 1;
335}
336
337# Given a list of sets, topologically sort them using dependencies.
338sub topologically_sort_sets (@) {
339 my @sets = @_;
340 my %added;
341 my @result;
342 SCAN: while (@sets) { # We'll delete items from @sets as we go.
343 #print_sets("So far", reverse(@result));
344 #print_sets("Remaining", @sets);
345 for (my $i = 0; $i < @sets; ++$i) {
346 my $set = $sets[$i];
347 if (have_all_deps_been_added(\%added, $set)) {
348 push @result, $set;
349 $added{$set} = 1;
350 #print "Removing $i.\n";
351 splice(@sets, $i, 1);
352 next SCAN; # Restart our scan.
353 }
354 }
355 die "Can't find a library with no dependencies";
356 }
357 return reverse(@result);
358}
359
360# Nasty hack to work around the fact that LLVMCore changes file type on
361# certain platforms.
362sub maybe_fix_core (@) {
363 my @libs = @_;
364 my @result;
365 foreach my $lib (@libs) {
366 if ($lib eq "LLVMCore.o" && $CORE_IS_ARCHIVE) {
367 push @result, "libLLVMCore.a";
368 } else {
369 push @result, $lib;
370 }
371 }
372 return @result;
373}
374
375# Our library dependency data will be added after the '__END__' token, and will
376# be read through the magic <DATA> filehandle.
377__END__