blob: c79998354554c324cc96c5498f6d7fd6228c6891 [file] [log] [blame]
Reid Spencerb195d9d2006-03-23 23:21:29 +00001#!@PERL@
Chris Lattner584073a2006-06-02 18:58:21 +00002##===- tools/llvm-config ---------------------------------------*- perl -*-===##
3#
4# The LLVM Compiler Infrastructure
Reid Spencerf2722ca2006-03-22 15:59:55 +00005#
Chris Lattner584073a2006-06-02 18:58:21 +00006# This file was developed by Eric Kidd and is distributed under
7# the University of Illinois Open Source License. See LICENSE.TXT for details.
8#
9##===----------------------------------------------------------------------===##
Reid Spencerf2722ca2006-03-22 15:59:55 +000010#
11# Synopsis: Prints out compiler options needed to build against an installed
12# copy of LLVM.
13#
Chris Lattner7f71e212006-04-13 04:21:31 +000014# Syntax: llvm-config OPTIONS... [COMPONENTS...]
Chris Lattner584073a2006-06-02 18:58:21 +000015#
16##===----------------------------------------------------------------------===##
Reid Spencerf2722ca2006-03-22 15:59:55 +000017
Reid Spencerb195d9d2006-03-23 23:21:29 +000018use 5.006;
Reid Spencerf2722ca2006-03-22 15:59:55 +000019use strict;
20use warnings;
21
22#---- begin autoconf values ----
Reid Spencer2d2c2f22006-06-02 18:31:41 +000023my $PACKAGE_NAME = q{@PACKAGE_NAME@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000024my $VERSION = q{@PACKAGE_VERSION@};
25my $PREFIX = q{@LLVM_PREFIX@};
Reid Spencer2d2c2f22006-06-02 18:31:41 +000026my $LLVM_CONFIGTIME = q{@LLVM_CONFIGTIME@};
27my $LLVM_SRC_ROOT = q{@abs_top_srcdir@};
28my $LLVM_OBJ_ROOT = q{@abs_top_builddir@};
29my $LLVM_ON_WIN32 = q{@LLVM_ON_WIN32@};
30my $LLVM_ON_UNIX = q{@LLVM_ON_UNIX@};
31my $LLVMGCCDIR = q{@LLVMGCCDIR@};
32my $LLVMGCC = q{@LLVMGCC@};
33my $LLVMGXX = q{@LLVMGXX@};
34my $LLVMGCC_VERSION = q{@LLVMGCC_VERSION@};
35my $LLVMGCC_MAJVERS = q{@LLVMGCC_MAJVERS@};
36my $ENDIAN = q{@ENDIAN@};
37my $SHLIBEXT = q{@SHLIBEXT@};
38my $EXEEXT = q{@EXEEXT@};
39my $OS = q{@OS@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000040my $ARCH = lc(q{@ARCH@});
Reid Spencer2d2c2f22006-06-02 18:31:41 +000041my $TARGET_TRIPLE = q{@target@};
42my $TARGETS_TO_BUILD = q{@TARGETS_TO_BUILD@};
Reid Spencerb195d9d2006-03-23 23:21:29 +000043my $TARGET_HAS_JIT = q{@TARGET_HAS_JIT@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000044my @TARGETS_BUILT = map { lc($_) } qw{@TARGETS_TO_BUILD@};
45#---- end autoconf values ----
46
47#---- begin Makefile values ----
48my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
49my $LDFLAGS = q{@LLVM_LDFLAGS@};
Chris Lattnerabdbae72006-06-02 19:13:29 +000050my $LLVM_BUILDMODE = q{@LLVM_BUILDMODE@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000051#---- end Makefile values ----
52
Chris Lattner16ad6182006-06-02 21:48:10 +000053# Figure out where llvm-config is being run from. Primarily, we care if it has
54# been installed, or is running from the build directory, which changes the
55# locations of some files.
56
Chris Lattnere02b97b2006-06-02 01:23:18 +000057# Convert the current executable name into its directory (e.g. ".").
Chris Lattner16ad6182006-06-02 21:48:10 +000058my ($RUN_DIR) = ($0 =~ /^(.*)\/.*$/);
59
60# Turn the directory into an absolute directory on the file system, also pop up
61# from "bin" into the build or prefix dir.
62my $ABS_RUN_DIR = `cd $RUN_DIR/..; pwd`;
63chomp($ABS_RUN_DIR);
64
65# Compute the absolute object directory build, e.g. "foo/llvm/Debug".
66my $ABS_OBJ_ROOT = `cd $LLVM_OBJ_ROOT/$LLVM_BUILDMODE; pwd`;
67chomp($ABS_OBJ_ROOT);
68
Chris Lattner0cd059e2006-06-02 22:03:50 +000069my $INCLUDEDIR = "$ABS_RUN_DIR/include";
70my $LIBDIR = "$ABS_RUN_DIR/lib";
71my $BINDIR = "$ABS_RUN_DIR/bin";
Chris Lattner16ad6182006-06-02 21:48:10 +000072if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) {
73 # If we are running out of the build directory, the include dir is in the
74 # srcdir.
75 $INCLUDEDIR = "$LLVM_SRC_ROOT/include";
76} else {
Chris Lattner0cd059e2006-06-02 22:03:50 +000077 # If installed, ignore the prefix the tree was configured with, use the
78 # current prefix.
79 $PREFIX = $ABS_RUN_DIR;
Chris Lattner16ad6182006-06-02 21:48:10 +000080}
Chris Lattnere02b97b2006-06-02 01:23:18 +000081
Reid Spencerf2722ca2006-03-22 15:59:55 +000082sub usage;
83sub fix_library_names (@);
84sub expand_dependecies (@);
85sub name_map_entries;
86
87# Parse our command-line arguments.
88usage if @ARGV == 0;
89my @components;
90my $has_opt = 0;
91my $want_libs = 0;
92my $want_libnames = 0;
93my $want_components = 0;
94foreach my $arg (@ARGV) {
95 if ($arg =~ /^-/) {
96 if ($arg eq "--version") {
97 $has_opt = 1; print "$VERSION\n";
98 } elsif ($arg eq "--prefix") {
99 $has_opt = 1; print "$PREFIX\n";
100 } elsif ($arg eq "--bindir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000101 $has_opt = 1; print "$BINDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000102 } elsif ($arg eq "--includedir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000103 $has_opt = 1; print "$INCLUDEDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000104 } elsif ($arg eq "--libdir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000105 $has_opt = 1; print "$LIBDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000106 } elsif ($arg eq "--cxxflags") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000107 $has_opt = 1; print "-I$INCLUDEDIR $CXXFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000108 } elsif ($arg eq "--ldflags") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000109 $has_opt = 1; print "-L$LIBDIR $LDFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000110 } elsif ($arg eq "--libs") {
111 $has_opt = 1; $want_libs = 1;
112 } elsif ($arg eq "--libnames") {
113 $has_opt = 1; $want_libnames = 1;
114 } elsif ($arg eq "--components") {
115 $has_opt = 1; print join(' ', name_map_entries), "\n";
116 } elsif ($arg eq "--targets-built") {
117 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
Chris Lattner0cd059e2006-06-02 22:03:50 +0000118 } elsif ($arg eq "--build-mode") {
119 $has_opt = 1; print "$LLVM_BUILDMODE\n";
120 } elsif ($arg eq "--obj-root") {
121 $has_opt = 1; print `cd $LLVM_OBJ_ROOT/; pwd` . "\n";
122 } elsif ($arg eq "--src-root") {
123 $has_opt = 1; print `cd $LLVM_SRC_ROOT/; pwd` . "\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000124 } else {
125 usage();
126 }
127 } else {
128 push @components, $arg;
129 }
130}
131
132# If no options were specified, fail.
133usage unless $has_opt;
134
135# If no components were specified, default to 'all'.
136if (@components == 0) {
137 push @components, 'all';
138}
139
140# Handle any arguments which require building our dependency graph.
141if ($want_libs || $want_libnames) {
142 my @libs = expand_dependecies(@components);
143 if ($want_libs) {
144 print join(' ', fix_library_names(@libs)), "\n";
145 }
146 if ($want_libnames) {
147 print join(' ', @libs), "\n";
148 }
149}
150
151exit 0;
152
153#==========================================================================
154# Support Routines
155#==========================================================================
156
157sub usage {
158 print STDERR <<__EOD__;
159Usage: llvm-config <OPTION>... [<COMPONENT>...]
160
161Get various configuration information needed to compile programs which use
162LLVM. Typically called from 'configure' scripts. Examples:
163 llvm-config --cxxflags
164 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000165 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000166
167Options:
Chris Lattner0cd059e2006-06-02 22:03:50 +0000168 --version Print LLVM version.
169 --prefix Print the installation prefix.
170 --src-root Print the source root LLVM was built from.
171 --obj-root Print the object root used to build LLVM.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000172 --bindir Directory containing LLVM executables.
173 --includedir Directory containing LLVM headers.
174 --libdir Directory containing LLVM libraries.
175 --cxxflags C++ compiler flags for files that include LLVM headers.
Chris Lattner0cd059e2006-06-02 22:03:50 +0000176 --ldflags Print Linker flags.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000177 --libs Libraries needed to link against LLVM components.
178 --libnames Bare library names for in-tree builds.
179 --components List of all possible components.
180 --targets-built List of all targets currently built.
Chris Lattner0cd059e2006-06-02 22:03:50 +0000181 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).
Reid Spencerf2722ca2006-03-22 15:59:55 +0000182Typical components:
183 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000184 backend Either a native backend or the C backend.
185 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000186__EOD__
187 exit(1);
188}
189
190# Use -lfoo instead of libfoo.a whenever possible, and add directories to
191# files which can't be found using -L.
192sub fix_library_names (@) {
193 my @libs = @_;
194 my @result;
195 foreach my $lib (@libs) {
196 # Transform the bare library name appropriately.
197 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
198 if (defined $basename) {
199 push @result, "-l$basename";
200 } else {
Chris Lattner16ad6182006-06-02 21:48:10 +0000201 push @result, "$LIBDIR/$lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000202 }
203 }
204 return @result;
205}
206
207
208#==========================================================================
209# Library Dependency Analysis
210#==========================================================================
211# Given a few human-readable library names, find all their dependencies
212# and sort them into an order which the linker will like. If we packed
213# our libraries into fewer archives, we could make the linker do much
214# of this work for us.
215#
216# Libraries have two different types of names in this code: Human-friendly
217# "component" names entered on the command-line, and the raw file names
218# we use internally (and ultimately pass to the linker).
219#
220# To understand this code, you'll need a working knowledge of Perl 5,
221# and possibly some quality time with 'man perlref'.
222
223sub load_dependencies;
224sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000225sub have_native_backend;
226sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000227sub expand_names (@);
228sub find_all_required_sets (@);
229sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000230
231# Each "set" contains one or more libraries which must be included as a
232# group (due to cyclic dependencies). Sets are represented as a Perl array
233# reference pointing to a list of internal library names.
234my @SETS;
235
236# Various mapping tables.
237my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
238my %SET_DEPS; # Maps sets to a list of libraries they depend on.
239my %NAME_MAP; # Maps human-entered names to internal names.
240
241# Have our dependencies been loaded yet?
242my $DEPENDENCIES_LOADED = 0;
243
244# Given a list of human-friendly component names, translate them into a
245# complete set of linker arguments.
246sub expand_dependecies (@) {
247 my @libs = @_;
248 load_dependencies;
249 my @required_sets = find_all_required_sets(expand_names(@libs));
250 my @sorted_sets = topologically_sort_sets(@required_sets);
251
Chris Lattner06e752e2006-06-02 00:56:15 +0000252 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000253 my @result;
254 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000255 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000256}
257
258# Load in the raw dependency data stored at the end of this file.
259sub load_dependencies {
260 return if $DEPENDENCIES_LOADED;
261 $DEPENDENCIES_LOADED = 1;
262 while (<DATA>) {
263 # Parse our line.
264 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
265 die "Malformed dependency data" unless defined $deps;
266 my @libs = split(' ', $libs);
267 my @deps = split(' ', $deps);
268
269 # Record our dependency data.
270 my $set = \@libs;
271 push @SETS, $set;
272 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
273 $SET_DEPS{$set} = \@deps;
274 }
275 build_name_map;
276}
277
278# Build a map converting human-friendly component names into internal
279# library names.
280sub build_name_map {
281 # Add entries for all the actual libraries.
282 foreach my $set (@SETS) {
283 foreach my $lib (sort @$set) {
284 my $short_name = $lib;
285 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
286 $short_name =~ tr/A-Z/a-z/;
287 $NAME_MAP{$short_name} = [$lib];
288 }
289 }
290
291 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000292 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
293 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
294 $NAME_MAP{'engine'} = find_best_engine;
295 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000296}
297
Reid Spencerb195d9d2006-03-23 23:21:29 +0000298# Return true if we have a native backend to use.
299sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000300 my %BUILT;
301 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000302 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
303}
304
305# Find a working subclass of ExecutionEngine for this platform.
306sub find_best_engine {
307 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000308 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000309 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000310 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000311 }
312}
313
314# Get all the human-friendly component names.
315sub name_map_entries {
316 load_dependencies;
317 return sort keys %NAME_MAP;
318}
319
320# Map human-readable names to internal library names.
321sub expand_names (@) {
322 my @names = @_;
323 my @result;
324 foreach my $name (@names) {
325 if (defined $LIB_TO_SET_MAP{$name}) {
326 # We've hit bottom: An actual library name.
327 push @result, $name;
328 } elsif (defined $NAME_MAP{$name}) {
329 # We've found a short name to expand.
330 push @result, expand_names(@{$NAME_MAP{$name}});
331 } else {
332 print STDERR "llvm-config: unknown component name: $name\n";
333 exit(1);
334 }
335 }
336 return @result;
337}
338
339# Given a list of internal library names, return all sets of libraries which
340# will need to be included by the linker (in no particular order).
341sub find_all_required_sets (@) {
342 my @libs = @_;
343 my %sets_added;
344 my @result;
345 find_all_required_sets_helper(\%sets_added, \@result, @libs);
346 return @result;
347}
348
349# Recursive closures are pretty broken in Perl, so we're going to separate
350# this function from find_all_required_sets and pass in the state we need
351# manually, as references. Yes, this is fairly unpleasant.
352sub find_all_required_sets_helper ($$@) {
353 my ($sets_added, $result, @libs) = @_;
354 foreach my $lib (@libs) {
355 my $set = $LIB_TO_SET_MAP{$lib};
356 next if defined $$sets_added{$set};
357 $$sets_added{$set} = 1;
358 push @$result, $set;
359 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
360 }
361}
362
363# Print a list of sets, with a label. Used for debugging.
364sub print_sets ($@) {
365 my ($label, @sets) = @_;
366 my @output;
367 foreach my $set (@sets) { push @output, join(',', @$set); }
368 print "$label: ", join(';', @output), "\n";
369}
370
371# Returns true if $lib is a key in $added.
372sub has_lib_been_added ($$) {
373 my ($added, $lib) = @_;
374 return defined $$added{$LIB_TO_SET_MAP{$lib}};
375}
376
377# Returns true if all the dependencies of $set appear in $added.
378sub have_all_deps_been_added ($$) {
379 my ($added, $set) = @_;
380 #print_sets(" Checking", $set);
381 #print_sets(" Wants", $SET_DEPS{$set});
382 foreach my $lib (@{$SET_DEPS{$set}}) {
383 return 0 unless has_lib_been_added($added, $lib);
384 }
385 return 1;
386}
387
388# Given a list of sets, topologically sort them using dependencies.
389sub topologically_sort_sets (@) {
390 my @sets = @_;
391 my %added;
392 my @result;
393 SCAN: while (@sets) { # We'll delete items from @sets as we go.
394 #print_sets("So far", reverse(@result));
395 #print_sets("Remaining", @sets);
396 for (my $i = 0; $i < @sets; ++$i) {
397 my $set = $sets[$i];
398 if (have_all_deps_been_added(\%added, $set)) {
399 push @result, $set;
400 $added{$set} = 1;
401 #print "Removing $i.\n";
402 splice(@sets, $i, 1);
403 next SCAN; # Restart our scan.
404 }
405 }
406 die "Can't find a library with no dependencies";
407 }
408 return reverse(@result);
409}
410
Reid Spencerf2722ca2006-03-22 15:59:55 +0000411# Our library dependency data will be added after the '__END__' token, and will
412# be read through the magic <DATA> filehandle.
413__END__