blob: dee1851cb12ecdfe5de4ce300af59240ccb8e702 [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 ----
Reid Spencer2d2c2f22006-06-02 18:31:41 +000018my $PACKAGE_NAME = q{@PACKAGE_NAME@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000019my $VERSION = q{@PACKAGE_VERSION@};
20my $PREFIX = q{@LLVM_PREFIX@};
Reid Spencer2d2c2f22006-06-02 18:31:41 +000021my $BINDIR = "$PREFIX/bin";
22my $INCLUDEDIR = "$PREFIX/include";
23my $LIBDIR = "$PREFIX/lib";
24my $LLVM_CONFIGTIME = q{@LLVM_CONFIGTIME@};
25my $LLVM_SRC_ROOT = q{@abs_top_srcdir@};
26my $LLVM_OBJ_ROOT = q{@abs_top_builddir@};
27my $LLVM_ON_WIN32 = q{@LLVM_ON_WIN32@};
28my $LLVM_ON_UNIX = q{@LLVM_ON_UNIX@};
29my $LLVMGCCDIR = q{@LLVMGCCDIR@};
30my $LLVMGCC = q{@LLVMGCC@};
31my $LLVMGXX = q{@LLVMGXX@};
32my $LLVMGCC_VERSION = q{@LLVMGCC_VERSION@};
33my $LLVMGCC_MAJVERS = q{@LLVMGCC_MAJVERS@};
34my $ENDIAN = q{@ENDIAN@};
35my $SHLIBEXT = q{@SHLIBEXT@};
36my $EXEEXT = q{@EXEEXT@};
37my $OS = q{@OS@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000038my $ARCH = lc(q{@ARCH@});
Reid Spencer2d2c2f22006-06-02 18:31:41 +000039my $TARGET_TRIPLE = q{@target@};
40my $TARGETS_TO_BUILD = q{@TARGETS_TO_BUILD@};
Reid Spencerb195d9d2006-03-23 23:21:29 +000041my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000042my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
43#---- end autoconf values ----
44
45#---- begin Makefile values ----
46my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
47my $LDFLAGS = q{@LLVM_LDFLAGS@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000048#---- end Makefile values ----
49
Chris Lattnere02b97b2006-06-02 01:23:18 +000050# Convert the current executable name into its directory (e.g. ".").
51my ($PARTIALDIR) = ($0 =~ /^(.*)\/.*$/);
52
Reid Spencerf2722ca2006-03-22 15:59:55 +000053sub usage;
54sub fix_library_names (@);
55sub expand_dependecies (@);
56sub name_map_entries;
57
58# Parse our command-line arguments.
59usage if @ARGV == 0;
60my @components;
61my $has_opt = 0;
62my $want_libs = 0;
63my $want_libnames = 0;
64my $want_components = 0;
65foreach my $arg (@ARGV) {
66 if ($arg =~ /^-/) {
67 if ($arg eq "--version") {
68 $has_opt = 1; print "$VERSION\n";
Chris Lattnere02b97b2006-06-02 01:23:18 +000069 } elsif ($arg eq "--use-current-dir-as-prefix") {
70 # Convert the scripts executable dir into a full absolute directory.
71 my $ABSDIR = `cd $PARTIALDIR/..; pwd`;
72 chomp($ABSDIR);
73 $PREFIX = $ABSDIR;
Reid Spencerf2722ca2006-03-22 15:59:55 +000074 } elsif ($arg eq "--prefix") {
75 $has_opt = 1; print "$PREFIX\n";
76 } elsif ($arg eq "--bindir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000077 $has_opt = 1; print "$PREFIX/bin\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000078 } elsif ($arg eq "--includedir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000079 $has_opt = 1; print "$PREFIX/include\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000080 } elsif ($arg eq "--libdir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000081 $has_opt = 1; print "$PREFIX/lib\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000082 } elsif ($arg eq "--cxxflags") {
Chris Lattner411c33d2006-06-02 01:04:35 +000083 $has_opt = 1; print "-I$PREFIX/include $CXXFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000084 } elsif ($arg eq "--ldflags") {
Chris Lattner411c33d2006-06-02 01:04:35 +000085 $has_opt = 1; print "-L$PREFIX/lib $LDFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000086 } elsif ($arg eq "--libs") {
87 $has_opt = 1; $want_libs = 1;
88 } elsif ($arg eq "--libnames") {
89 $has_opt = 1; $want_libnames = 1;
90 } elsif ($arg eq "--components") {
91 $has_opt = 1; print join(' ', name_map_entries), "\n";
92 } elsif ($arg eq "--targets-built") {
93 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
94 } else {
95 usage();
96 }
97 } else {
98 push @components, $arg;
99 }
100}
101
102# If no options were specified, fail.
103usage unless $has_opt;
104
105# If no components were specified, default to 'all'.
106if (@components == 0) {
107 push @components, 'all';
108}
109
110# Handle any arguments which require building our dependency graph.
111if ($want_libs || $want_libnames) {
112 my @libs = expand_dependecies(@components);
113 if ($want_libs) {
114 print join(' ', fix_library_names(@libs)), "\n";
115 }
116 if ($want_libnames) {
117 print join(' ', @libs), "\n";
118 }
119}
120
121exit 0;
122
123#==========================================================================
124# Support Routines
125#==========================================================================
126
127sub usage {
128 print STDERR <<__EOD__;
129Usage: llvm-config <OPTION>... [<COMPONENT>...]
130
131Get various configuration information needed to compile programs which use
132LLVM. Typically called from 'configure' scripts. Examples:
133 llvm-config --cxxflags
134 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000135 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000136
137Options:
138 --version LLVM version.
139 --prefix Installation prefix.
140 --bindir Directory containing LLVM executables.
141 --includedir Directory containing LLVM headers.
142 --libdir Directory containing LLVM libraries.
143 --cxxflags C++ compiler flags for files that include LLVM headers.
144 --ldflags Linker flags.
145 --libs Libraries needed to link against LLVM components.
146 --libnames Bare library names for in-tree builds.
147 --components List of all possible components.
148 --targets-built List of all targets currently built.
149Typical components:
150 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000151 backend Either a native backend or the C backend.
152 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000153__EOD__
154 exit(1);
155}
156
157# Use -lfoo instead of libfoo.a whenever possible, and add directories to
158# files which can't be found using -L.
159sub fix_library_names (@) {
160 my @libs = @_;
161 my @result;
162 foreach my $lib (@libs) {
163 # Transform the bare library name appropriately.
164 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
165 if (defined $basename) {
166 push @result, "-l$basename";
167 } else {
Chris Lattner411c33d2006-06-02 01:04:35 +0000168 push @result, "$PREFIX/lib/$lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000169 }
170 }
171 return @result;
172}
173
174
175#==========================================================================
176# Library Dependency Analysis
177#==========================================================================
178# Given a few human-readable library names, find all their dependencies
179# and sort them into an order which the linker will like. If we packed
180# our libraries into fewer archives, we could make the linker do much
181# of this work for us.
182#
183# Libraries have two different types of names in this code: Human-friendly
184# "component" names entered on the command-line, and the raw file names
185# we use internally (and ultimately pass to the linker).
186#
187# To understand this code, you'll need a working knowledge of Perl 5,
188# and possibly some quality time with 'man perlref'.
189
190sub load_dependencies;
191sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000192sub have_native_backend;
193sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000194sub expand_names (@);
195sub find_all_required_sets (@);
196sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000197
198# Each "set" contains one or more libraries which must be included as a
199# group (due to cyclic dependencies). Sets are represented as a Perl array
200# reference pointing to a list of internal library names.
201my @SETS;
202
203# Various mapping tables.
204my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
205my %SET_DEPS; # Maps sets to a list of libraries they depend on.
206my %NAME_MAP; # Maps human-entered names to internal names.
207
208# Have our dependencies been loaded yet?
209my $DEPENDENCIES_LOADED = 0;
210
211# Given a list of human-friendly component names, translate them into a
212# complete set of linker arguments.
213sub expand_dependecies (@) {
214 my @libs = @_;
215 load_dependencies;
216 my @required_sets = find_all_required_sets(expand_names(@libs));
217 my @sorted_sets = topologically_sort_sets(@required_sets);
218
Chris Lattner06e752e2006-06-02 00:56:15 +0000219 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000220 my @result;
221 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000222 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000223}
224
225# Load in the raw dependency data stored at the end of this file.
226sub load_dependencies {
227 return if $DEPENDENCIES_LOADED;
228 $DEPENDENCIES_LOADED = 1;
229 while (<DATA>) {
230 # Parse our line.
231 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
232 die "Malformed dependency data" unless defined $deps;
233 my @libs = split(' ', $libs);
234 my @deps = split(' ', $deps);
235
236 # Record our dependency data.
237 my $set = \@libs;
238 push @SETS, $set;
239 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
240 $SET_DEPS{$set} = \@deps;
241 }
242 build_name_map;
243}
244
245# Build a map converting human-friendly component names into internal
246# library names.
247sub build_name_map {
248 # Add entries for all the actual libraries.
249 foreach my $set (@SETS) {
250 foreach my $lib (sort @$set) {
251 my $short_name = $lib;
252 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
253 $short_name =~ tr/A-Z/a-z/;
254 $NAME_MAP{$short_name} = [$lib];
255 }
256 }
257
258 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000259 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
260 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
261 $NAME_MAP{'engine'} = find_best_engine;
262 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000263}
264
Reid Spencerb195d9d2006-03-23 23:21:29 +0000265# Return true if we have a native backend to use.
266sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000267 my %BUILT;
268 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000269 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
270}
271
272# Find a working subclass of ExecutionEngine for this platform.
273sub find_best_engine {
274 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000275 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000276 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000277 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000278 }
279}
280
281# Get all the human-friendly component names.
282sub name_map_entries {
283 load_dependencies;
284 return sort keys %NAME_MAP;
285}
286
287# Map human-readable names to internal library names.
288sub expand_names (@) {
289 my @names = @_;
290 my @result;
291 foreach my $name (@names) {
292 if (defined $LIB_TO_SET_MAP{$name}) {
293 # We've hit bottom: An actual library name.
294 push @result, $name;
295 } elsif (defined $NAME_MAP{$name}) {
296 # We've found a short name to expand.
297 push @result, expand_names(@{$NAME_MAP{$name}});
298 } else {
299 print STDERR "llvm-config: unknown component name: $name\n";
300 exit(1);
301 }
302 }
303 return @result;
304}
305
306# Given a list of internal library names, return all sets of libraries which
307# will need to be included by the linker (in no particular order).
308sub find_all_required_sets (@) {
309 my @libs = @_;
310 my %sets_added;
311 my @result;
312 find_all_required_sets_helper(\%sets_added, \@result, @libs);
313 return @result;
314}
315
316# Recursive closures are pretty broken in Perl, so we're going to separate
317# this function from find_all_required_sets and pass in the state we need
318# manually, as references. Yes, this is fairly unpleasant.
319sub find_all_required_sets_helper ($$@) {
320 my ($sets_added, $result, @libs) = @_;
321 foreach my $lib (@libs) {
322 my $set = $LIB_TO_SET_MAP{$lib};
323 next if defined $$sets_added{$set};
324 $$sets_added{$set} = 1;
325 push @$result, $set;
326 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
327 }
328}
329
330# Print a list of sets, with a label. Used for debugging.
331sub print_sets ($@) {
332 my ($label, @sets) = @_;
333 my @output;
334 foreach my $set (@sets) { push @output, join(',', @$set); }
335 print "$label: ", join(';', @output), "\n";
336}
337
338# Returns true if $lib is a key in $added.
339sub has_lib_been_added ($$) {
340 my ($added, $lib) = @_;
341 return defined $$added{$LIB_TO_SET_MAP{$lib}};
342}
343
344# Returns true if all the dependencies of $set appear in $added.
345sub have_all_deps_been_added ($$) {
346 my ($added, $set) = @_;
347 #print_sets(" Checking", $set);
348 #print_sets(" Wants", $SET_DEPS{$set});
349 foreach my $lib (@{$SET_DEPS{$set}}) {
350 return 0 unless has_lib_been_added($added, $lib);
351 }
352 return 1;
353}
354
355# Given a list of sets, topologically sort them using dependencies.
356sub topologically_sort_sets (@) {
357 my @sets = @_;
358 my %added;
359 my @result;
360 SCAN: while (@sets) { # We'll delete items from @sets as we go.
361 #print_sets("So far", reverse(@result));
362 #print_sets("Remaining", @sets);
363 for (my $i = 0; $i < @sets; ++$i) {
364 my $set = $sets[$i];
365 if (have_all_deps_been_added(\%added, $set)) {
366 push @result, $set;
367 $added{$set} = 1;
368 #print "Removing $i.\n";
369 splice(@sets, $i, 1);
370 next SCAN; # Restart our scan.
371 }
372 }
373 die "Can't find a library with no dependencies";
374 }
375 return reverse(@result);
376}
377
Reid Spencerf2722ca2006-03-22 15:59:55 +0000378# Our library dependency data will be added after the '__END__' token, and will
379# be read through the magic <DATA> filehandle.
380__END__