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