blob: 89efde40e280533c1e2f76505cab3e512d522a77 [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 ($$@);
172sub maybe_fix_core (@);
173
174# Each "set" contains one or more libraries which must be included as a
175# group (due to cyclic dependencies). Sets are represented as a Perl array
176# reference pointing to a list of internal library names.
177my @SETS;
178
179# Various mapping tables.
180my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
181my %SET_DEPS; # Maps sets to a list of libraries they depend on.
182my %NAME_MAP; # Maps human-entered names to internal names.
183
184# Have our dependencies been loaded yet?
185my $DEPENDENCIES_LOADED = 0;
186
187# Given a list of human-friendly component names, translate them into a
188# complete set of linker arguments.
189sub expand_dependecies (@) {
190 my @libs = @_;
191 load_dependencies;
192 my @required_sets = find_all_required_sets(expand_names(@libs));
193 my @sorted_sets = topologically_sort_sets(@required_sets);
194
195 # Expand the library sets into libraries, and apply any
196 # platform-specific hackery.
197 my @result;
198 foreach my $set (@sorted_sets) { push @result, @{$set}; }
199 return maybe_fix_core(@result);
200}
201
202# Load in the raw dependency data stored at the end of this file.
203sub load_dependencies {
204 return if $DEPENDENCIES_LOADED;
205 $DEPENDENCIES_LOADED = 1;
206 while (<DATA>) {
207 # Parse our line.
208 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
209 die "Malformed dependency data" unless defined $deps;
210 my @libs = split(' ', $libs);
211 my @deps = split(' ', $deps);
212
213 # Record our dependency data.
214 my $set = \@libs;
215 push @SETS, $set;
216 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
217 $SET_DEPS{$set} = \@deps;
218 }
219 build_name_map;
220}
221
222# Build a map converting human-friendly component names into internal
223# library names.
224sub build_name_map {
225 # Add entries for all the actual libraries.
226 foreach my $set (@SETS) {
227 foreach my $lib (sort @$set) {
228 my $short_name = $lib;
229 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
230 $short_name =~ tr/A-Z/a-z/;
231 $NAME_MAP{$short_name} = [$lib];
232 }
233 }
234
235 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000236 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
237 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
238 $NAME_MAP{'engine'} = find_best_engine;
239 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000240}
241
Reid Spencerb195d9d2006-03-23 23:21:29 +0000242# Return true if we have a native backend to use.
243sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000244 my %BUILT;
245 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000246 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
247}
248
249# Find a working subclass of ExecutionEngine for this platform.
250sub find_best_engine {
251 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000252 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000253 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000254 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000255 }
256}
257
258# Get all the human-friendly component names.
259sub name_map_entries {
260 load_dependencies;
261 return sort keys %NAME_MAP;
262}
263
264# Map human-readable names to internal library names.
265sub expand_names (@) {
266 my @names = @_;
267 my @result;
268 foreach my $name (@names) {
269 if (defined $LIB_TO_SET_MAP{$name}) {
270 # We've hit bottom: An actual library name.
271 push @result, $name;
272 } elsif (defined $NAME_MAP{$name}) {
273 # We've found a short name to expand.
274 push @result, expand_names(@{$NAME_MAP{$name}});
275 } else {
276 print STDERR "llvm-config: unknown component name: $name\n";
277 exit(1);
278 }
279 }
280 return @result;
281}
282
283# Given a list of internal library names, return all sets of libraries which
284# will need to be included by the linker (in no particular order).
285sub find_all_required_sets (@) {
286 my @libs = @_;
287 my %sets_added;
288 my @result;
289 find_all_required_sets_helper(\%sets_added, \@result, @libs);
290 return @result;
291}
292
293# Recursive closures are pretty broken in Perl, so we're going to separate
294# this function from find_all_required_sets and pass in the state we need
295# manually, as references. Yes, this is fairly unpleasant.
296sub find_all_required_sets_helper ($$@) {
297 my ($sets_added, $result, @libs) = @_;
298 foreach my $lib (@libs) {
299 my $set = $LIB_TO_SET_MAP{$lib};
300 next if defined $$sets_added{$set};
301 $$sets_added{$set} = 1;
302 push @$result, $set;
303 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
304 }
305}
306
307# Print a list of sets, with a label. Used for debugging.
308sub print_sets ($@) {
309 my ($label, @sets) = @_;
310 my @output;
311 foreach my $set (@sets) { push @output, join(',', @$set); }
312 print "$label: ", join(';', @output), "\n";
313}
314
315# Returns true if $lib is a key in $added.
316sub has_lib_been_added ($$) {
317 my ($added, $lib) = @_;
318 return defined $$added{$LIB_TO_SET_MAP{$lib}};
319}
320
321# Returns true if all the dependencies of $set appear in $added.
322sub have_all_deps_been_added ($$) {
323 my ($added, $set) = @_;
324 #print_sets(" Checking", $set);
325 #print_sets(" Wants", $SET_DEPS{$set});
326 foreach my $lib (@{$SET_DEPS{$set}}) {
327 return 0 unless has_lib_been_added($added, $lib);
328 }
329 return 1;
330}
331
332# Given a list of sets, topologically sort them using dependencies.
333sub topologically_sort_sets (@) {
334 my @sets = @_;
335 my %added;
336 my @result;
337 SCAN: while (@sets) { # We'll delete items from @sets as we go.
338 #print_sets("So far", reverse(@result));
339 #print_sets("Remaining", @sets);
340 for (my $i = 0; $i < @sets; ++$i) {
341 my $set = $sets[$i];
342 if (have_all_deps_been_added(\%added, $set)) {
343 push @result, $set;
344 $added{$set} = 1;
345 #print "Removing $i.\n";
346 splice(@sets, $i, 1);
347 next SCAN; # Restart our scan.
348 }
349 }
350 die "Can't find a library with no dependencies";
351 }
352 return reverse(@result);
353}
354
355# Nasty hack to work around the fact that LLVMCore changes file type on
356# certain platforms.
357sub maybe_fix_core (@) {
358 my @libs = @_;
359 my @result;
360 foreach my $lib (@libs) {
Chris Lattner65749942006-06-02 00:30:31 +0000361 push @result, $lib;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000362 }
363 return @result;
364}
365
366# Our library dependency data will be added after the '__END__' token, and will
367# be read through the magic <DATA> filehandle.
368__END__