blob: b0c74dfd39b0b815555240ce12d06fc76ecc63c4 [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@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000020my $ARCH = lc(q{@ARCH@});
Reid Spencerb195d9d2006-03-23 23:21:29 +000021my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000022my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
23#---- end autoconf values ----
24
25#---- begin Makefile values ----
26my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
27my $LDFLAGS = q{@LLVM_LDFLAGS@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000028#---- end Makefile values ----
29
Chris Lattnere02b97b2006-06-02 01:23:18 +000030# Convert the current executable name into its directory (e.g. ".").
31my ($PARTIALDIR) = ($0 =~ /^(.*)\/.*$/);
32
Reid Spencerf2722ca2006-03-22 15:59:55 +000033sub 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";
Chris Lattnere02b97b2006-06-02 01:23:18 +000049 } 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`;
52 chomp($ABSDIR);
53 $PREFIX = $ABSDIR;
Reid Spencerf2722ca2006-03-22 15:59:55 +000054 } elsif ($arg eq "--prefix") {
55 $has_opt = 1; print "$PREFIX\n";
56 } elsif ($arg eq "--bindir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000057 $has_opt = 1; print "$PREFIX/bin\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000058 } elsif ($arg eq "--includedir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000059 $has_opt = 1; print "$PREFIX/include\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000060 } elsif ($arg eq "--libdir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000061 $has_opt = 1; print "$PREFIX/lib\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000062 } elsif ($arg eq "--cxxflags") {
Chris Lattner411c33d2006-06-02 01:04:35 +000063 $has_opt = 1; print "-I$PREFIX/include $CXXFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000064 } elsif ($arg eq "--ldflags") {
Chris Lattner411c33d2006-06-02 01:04:35 +000065 $has_opt = 1; print "-L$PREFIX/lib $LDFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000066 } 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";
74 } else {
75 usage();
76 }
77 } else {
78 push @components, $arg;
79 }
80}
81
82# If no options were specified, fail.
83usage unless $has_opt;
84
85# If no components were specified, default to 'all'.
86if (@components == 0) {
87 push @components, 'all';
88}
89
90# Handle any arguments which require building our dependency graph.
91if ($want_libs || $want_libnames) {
92 my @libs = expand_dependecies(@components);
93 if ($want_libs) {
94 print join(' ', fix_library_names(@libs)), "\n";
95 }
96 if ($want_libnames) {
97 print join(' ', @libs), "\n";
98 }
99}
100
101exit 0;
102
103#==========================================================================
104# Support Routines
105#==========================================================================
106
107sub usage {
108 print STDERR <<__EOD__;
109Usage: llvm-config <OPTION>... [<COMPONENT>...]
110
111Get various configuration information needed to compile programs which use
112LLVM. Typically called from 'configure' scripts. Examples:
113 llvm-config --cxxflags
114 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000115 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000116
117Options:
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.
129Typical components:
130 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000131 backend Either a native backend or the C backend.
132 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000133__EOD__
134 exit(1);
135}
136
137# Use -lfoo instead of libfoo.a whenever possible, and add directories to
138# files which can't be found using -L.
139sub fix_library_names (@) {
140 my @libs = @_;
141 my @result;
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";
147 } else {
Chris Lattner411c33d2006-06-02 01:04:35 +0000148 push @result, "$PREFIX/lib/$lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000149 }
150 }
151 return @result;
152}
153
154
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.
162#
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).
166#
167# To understand this code, you'll need a working knowledge of Perl 5,
168# and possibly some quality time with 'man perlref'.
169
170sub load_dependencies;
171sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000172sub have_native_backend;
173sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000174sub expand_names (@);
175sub find_all_required_sets (@);
176sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000177
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.
181my @SETS;
182
183# Various mapping tables.
184my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
185my %SET_DEPS; # Maps sets to a list of libraries they depend on.
186my %NAME_MAP; # Maps human-entered names to internal names.
187
188# Have our dependencies been loaded yet?
189my $DEPENDENCIES_LOADED = 0;
190
191# Given a list of human-friendly component names, translate them into a
192# complete set of linker arguments.
193sub expand_dependecies (@) {
194 my @libs = @_;
195 load_dependencies;
196 my @required_sets = find_all_required_sets(expand_names(@libs));
197 my @sorted_sets = topologically_sort_sets(@required_sets);
198
Chris Lattner06e752e2006-06-02 00:56:15 +0000199 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000200 my @result;
201 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000202 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000203}
204
205# Load in the raw dependency data stored at the end of this file.
206sub load_dependencies {
207 return if $DEPENDENCIES_LOADED;
208 $DEPENDENCIES_LOADED = 1;
209 while (<DATA>) {
210 # Parse our line.
211 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
212 die "Malformed dependency data" unless defined $deps;
213 my @libs = split(' ', $libs);
214 my @deps = split(' ', $deps);
215
216 # Record our dependency data.
217 my $set = \@libs;
218 push @SETS, $set;
219 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
220 $SET_DEPS{$set} = \@deps;
221 }
222 build_name_map;
223}
224
225# Build a map converting human-friendly component names into internal
226# library names.
227sub build_name_map {
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];
235 }
236 }
237
238 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000239 $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.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000243}
244
Reid Spencerb195d9d2006-03-23 23:21:29 +0000245# Return true if we have a native backend to use.
246sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000247 my %BUILT;
248 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000249 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
250}
251
252# Find a working subclass of ExecutionEngine for this platform.
253sub find_best_engine {
254 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000255 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000256 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000257 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000258 }
259}
260
261# Get all the human-friendly component names.
262sub name_map_entries {
263 load_dependencies;
264 return sort keys %NAME_MAP;
265}
266
267# Map human-readable names to internal library names.
268sub expand_names (@) {
269 my @names = @_;
270 my @result;
271 foreach my $name (@names) {
272 if (defined $LIB_TO_SET_MAP{$name}) {
273 # We've hit bottom: An actual library name.
274 push @result, $name;
275 } elsif (defined $NAME_MAP{$name}) {
276 # We've found a short name to expand.
277 push @result, expand_names(@{$NAME_MAP{$name}});
278 } else {
279 print STDERR "llvm-config: unknown component name: $name\n";
280 exit(1);
281 }
282 }
283 return @result;
284}
285
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).
288sub find_all_required_sets (@) {
289 my @libs = @_;
290 my %sets_added;
291 my @result;
292 find_all_required_sets_helper(\%sets_added, \@result, @libs);
293 return @result;
294}
295
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.
299sub 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;
305 push @$result, $set;
306 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
307 }
308}
309
310# Print a list of sets, with a label. Used for debugging.
311sub print_sets ($@) {
312 my ($label, @sets) = @_;
313 my @output;
314 foreach my $set (@sets) { push @output, join(',', @$set); }
315 print "$label: ", join(';', @output), "\n";
316}
317
318# Returns true if $lib is a key in $added.
319sub has_lib_been_added ($$) {
320 my ($added, $lib) = @_;
321 return defined $$added{$LIB_TO_SET_MAP{$lib}};
322}
323
324# Returns true if all the dependencies of $set appear in $added.
325sub 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);
331 }
332 return 1;
333}
334
335# Given a list of sets, topologically sort them using dependencies.
336sub topologically_sort_sets (@) {
337 my @sets = @_;
338 my %added;
339 my @result;
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) {
344 my $set = $sets[$i];
345 if (have_all_deps_been_added(\%added, $set)) {
346 push @result, $set;
347 $added{$set} = 1;
348 #print "Removing $i.\n";
349 splice(@sets, $i, 1);
350 next SCAN; # Restart our scan.
351 }
352 }
353 die "Can't find a library with no dependencies";
354 }
355 return reverse(@result);
356}
357
Reid Spencerf2722ca2006-03-22 15:59:55 +0000358# Our library dependency data will be added after the '__END__' token, and will
359# be read through the magic <DATA> filehandle.
360__END__