blob: e84572784aeffe77e30ecc7b2421003054975bed [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#
Chris Lattner7f71e212006-04-13 04:21:31 +00008# Syntax: llvm-config OPTIONS... [COMPONENTS...]
Reid Spencerf2722ca2006-03-22 15:59:55 +00009#
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@};
Chris Lattner65749942006-06-02 00:30:31 +000020my $BINDIR = "$PREFIX/bin";
21my $INCLUDEDIR = "$PREFIX/include";
22my $LIBDIR = "$PREFIX/lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +000023my $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@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000031#---- end Makefile values ----
32
33sub usage;
34sub fix_library_names (@);
35sub expand_dependecies (@);
36sub name_map_entries;
37
38# Parse our command-line arguments.
39usage if @ARGV == 0;
40my @components;
41my $has_opt = 0;
42my $want_libs = 0;
43my $want_libnames = 0;
44my $want_components = 0;
45foreach my $arg (@ARGV) {
46 if ($arg =~ /^-/) {
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";
69 } else {
70 usage();
71 }
72 } else {
73 push @components, $arg;
74 }
75}
76
77# If no options were specified, fail.
78usage unless $has_opt;
79
80# If no components were specified, default to 'all'.
81if (@components == 0) {
82 push @components, 'all';
83}
84
85# Handle any arguments which require building our dependency graph.
86if ($want_libs || $want_libnames) {
87 my @libs = expand_dependecies(@components);
88 if ($want_libs) {
89 print join(' ', fix_library_names(@libs)), "\n";
90 }
91 if ($want_libnames) {
92 print join(' ', @libs), "\n";
93 }
94}
95
96exit 0;
97
98#==========================================================================
99# Support Routines
100#==========================================================================
101
102sub usage {
103 print STDERR <<__EOD__;
104Usage: llvm-config <OPTION>... [<COMPONENT>...]
105
106Get various configuration information needed to compile programs which use
107LLVM. Typically called from 'configure' scripts. Examples:
108 llvm-config --cxxflags
109 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000110 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000111
112Options:
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.
124Typical components:
125 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000126 backend Either a native backend or the C backend.
127 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000128__EOD__
129 exit(1);
130}
131
132# Use -lfoo instead of libfoo.a whenever possible, and add directories to
133# files which can't be found using -L.
134sub fix_library_names (@) {
135 my @libs = @_;
136 my @result;
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";
142 } else {
143 push @result, "$LIBDIR/$lib";
144 }
145 }
146 return @result;
147}
148
149
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.
157#
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).
161#
162# To understand this code, you'll need a working knowledge of Perl 5,
163# and possibly some quality time with 'man perlref'.
164
165sub load_dependencies;
166sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000167sub have_native_backend;
168sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000169sub expand_names (@);
170sub find_all_required_sets (@);
171sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000172
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.
176my @SETS;
177
178# Various mapping tables.
179my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
180my %SET_DEPS; # Maps sets to a list of libraries they depend on.
181my %NAME_MAP; # Maps human-entered names to internal names.
182
183# Have our dependencies been loaded yet?
184my $DEPENDENCIES_LOADED = 0;
185
186# Given a list of human-friendly component names, translate them into a
187# complete set of linker arguments.
188sub expand_dependecies (@) {
189 my @libs = @_;
190 load_dependencies;
191 my @required_sets = find_all_required_sets(expand_names(@libs));
192 my @sorted_sets = topologically_sort_sets(@required_sets);
193
Chris Lattner06e752e2006-06-02 00:56:15 +0000194 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000195 my @result;
196 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000197 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000198}
199
200# Load in the raw dependency data stored at the end of this file.
201sub load_dependencies {
202 return if $DEPENDENCIES_LOADED;
203 $DEPENDENCIES_LOADED = 1;
204 while (<DATA>) {
205 # Parse our line.
206 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
207 die "Malformed dependency data" unless defined $deps;
208 my @libs = split(' ', $libs);
209 my @deps = split(' ', $deps);
210
211 # Record our dependency data.
212 my $set = \@libs;
213 push @SETS, $set;
214 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
215 $SET_DEPS{$set} = \@deps;
216 }
217 build_name_map;
218}
219
220# Build a map converting human-friendly component names into internal
221# library names.
222sub build_name_map {
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];
230 }
231 }
232
233 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000234 $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.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000238}
239
Reid Spencerb195d9d2006-03-23 23:21:29 +0000240# Return true if we have a native backend to use.
241sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000242 my %BUILT;
243 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000244 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
245}
246
247# Find a working subclass of ExecutionEngine for this platform.
248sub find_best_engine {
249 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000250 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000251 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000252 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000253 }
254}
255
256# Get all the human-friendly component names.
257sub name_map_entries {
258 load_dependencies;
259 return sort keys %NAME_MAP;
260}
261
262# Map human-readable names to internal library names.
263sub expand_names (@) {
264 my @names = @_;
265 my @result;
266 foreach my $name (@names) {
267 if (defined $LIB_TO_SET_MAP{$name}) {
268 # We've hit bottom: An actual library name.
269 push @result, $name;
270 } elsif (defined $NAME_MAP{$name}) {
271 # We've found a short name to expand.
272 push @result, expand_names(@{$NAME_MAP{$name}});
273 } else {
274 print STDERR "llvm-config: unknown component name: $name\n";
275 exit(1);
276 }
277 }
278 return @result;
279}
280
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).
283sub find_all_required_sets (@) {
284 my @libs = @_;
285 my %sets_added;
286 my @result;
287 find_all_required_sets_helper(\%sets_added, \@result, @libs);
288 return @result;
289}
290
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.
294sub 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;
300 push @$result, $set;
301 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
302 }
303}
304
305# Print a list of sets, with a label. Used for debugging.
306sub print_sets ($@) {
307 my ($label, @sets) = @_;
308 my @output;
309 foreach my $set (@sets) { push @output, join(',', @$set); }
310 print "$label: ", join(';', @output), "\n";
311}
312
313# Returns true if $lib is a key in $added.
314sub has_lib_been_added ($$) {
315 my ($added, $lib) = @_;
316 return defined $$added{$LIB_TO_SET_MAP{$lib}};
317}
318
319# Returns true if all the dependencies of $set appear in $added.
320sub 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);
326 }
327 return 1;
328}
329
330# Given a list of sets, topologically sort them using dependencies.
331sub topologically_sort_sets (@) {
332 my @sets = @_;
333 my %added;
334 my @result;
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) {
339 my $set = $sets[$i];
340 if (have_all_deps_been_added(\%added, $set)) {
341 push @result, $set;
342 $added{$set} = 1;
343 #print "Removing $i.\n";
344 splice(@sets, $i, 1);
345 next SCAN; # Restart our scan.
346 }
347 }
348 die "Can't find a library with no dependencies";
349 }
350 return reverse(@result);
351}
352
Reid Spencerf2722ca2006-03-22 15:59:55 +0000353# Our library dependency data will be added after the '__END__' token, and will
354# be read through the magic <DATA> filehandle.
355__END__