blob: 930d25e998292d216b62c0052f7dfbf3d8c6319b [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";
Chris Lattner2e1f0ed2006-06-02 23:43:27 +000098 } elsif ($arg eq "--use-current-dir-as-prefix") {
99 # NOOP, remove!
Reid Spencerf2722ca2006-03-22 15:59:55 +0000100 } elsif ($arg eq "--prefix") {
101 $has_opt = 1; print "$PREFIX\n";
102 } elsif ($arg eq "--bindir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000103 $has_opt = 1; print "$BINDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000104 } elsif ($arg eq "--includedir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000105 $has_opt = 1; print "$INCLUDEDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000106 } elsif ($arg eq "--libdir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000107 $has_opt = 1; print "$LIBDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000108 } elsif ($arg eq "--cxxflags") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000109 $has_opt = 1; print "-I$INCLUDEDIR $CXXFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000110 } elsif ($arg eq "--ldflags") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000111 $has_opt = 1; print "-L$LIBDIR $LDFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000112 } elsif ($arg eq "--libs") {
113 $has_opt = 1; $want_libs = 1;
114 } elsif ($arg eq "--libnames") {
115 $has_opt = 1; $want_libnames = 1;
116 } elsif ($arg eq "--components") {
117 $has_opt = 1; print join(' ', name_map_entries), "\n";
118 } elsif ($arg eq "--targets-built") {
119 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
Chris Lattner0cd059e2006-06-02 22:03:50 +0000120 } elsif ($arg eq "--build-mode") {
121 $has_opt = 1; print "$LLVM_BUILDMODE\n";
122 } elsif ($arg eq "--obj-root") {
123 $has_opt = 1; print `cd $LLVM_OBJ_ROOT/; pwd` . "\n";
124 } elsif ($arg eq "--src-root") {
125 $has_opt = 1; print `cd $LLVM_SRC_ROOT/; pwd` . "\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000126 } else {
127 usage();
128 }
129 } else {
130 push @components, $arg;
131 }
132}
133
134# If no options were specified, fail.
135usage unless $has_opt;
136
137# If no components were specified, default to 'all'.
138if (@components == 0) {
139 push @components, 'all';
140}
141
142# Handle any arguments which require building our dependency graph.
143if ($want_libs || $want_libnames) {
144 my @libs = expand_dependecies(@components);
145 if ($want_libs) {
146 print join(' ', fix_library_names(@libs)), "\n";
147 }
148 if ($want_libnames) {
149 print join(' ', @libs), "\n";
150 }
151}
152
153exit 0;
154
155#==========================================================================
156# Support Routines
157#==========================================================================
158
159sub usage {
160 print STDERR <<__EOD__;
161Usage: llvm-config <OPTION>... [<COMPONENT>...]
162
163Get various configuration information needed to compile programs which use
164LLVM. Typically called from 'configure' scripts. Examples:
165 llvm-config --cxxflags
166 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000167 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000168
169Options:
Chris Lattner0cd059e2006-06-02 22:03:50 +0000170 --version Print LLVM version.
171 --prefix Print the installation prefix.
172 --src-root Print the source root LLVM was built from.
173 --obj-root Print the object root used to build LLVM.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000174 --bindir Directory containing LLVM executables.
175 --includedir Directory containing LLVM headers.
176 --libdir Directory containing LLVM libraries.
177 --cxxflags C++ compiler flags for files that include LLVM headers.
Chris Lattner0cd059e2006-06-02 22:03:50 +0000178 --ldflags Print Linker flags.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000179 --libs Libraries needed to link against LLVM components.
180 --libnames Bare library names for in-tree builds.
181 --components List of all possible components.
182 --targets-built List of all targets currently built.
Chris Lattner0cd059e2006-06-02 22:03:50 +0000183 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).
Reid Spencerf2722ca2006-03-22 15:59:55 +0000184Typical components:
185 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000186 backend Either a native backend or the C backend.
187 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000188__EOD__
189 exit(1);
190}
191
192# Use -lfoo instead of libfoo.a whenever possible, and add directories to
193# files which can't be found using -L.
194sub fix_library_names (@) {
195 my @libs = @_;
196 my @result;
197 foreach my $lib (@libs) {
198 # Transform the bare library name appropriately.
199 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
200 if (defined $basename) {
201 push @result, "-l$basename";
202 } else {
Chris Lattner16ad6182006-06-02 21:48:10 +0000203 push @result, "$LIBDIR/$lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000204 }
205 }
206 return @result;
207}
208
209
210#==========================================================================
211# Library Dependency Analysis
212#==========================================================================
213# Given a few human-readable library names, find all their dependencies
214# and sort them into an order which the linker will like. If we packed
215# our libraries into fewer archives, we could make the linker do much
216# of this work for us.
217#
218# Libraries have two different types of names in this code: Human-friendly
219# "component" names entered on the command-line, and the raw file names
220# we use internally (and ultimately pass to the linker).
221#
222# To understand this code, you'll need a working knowledge of Perl 5,
223# and possibly some quality time with 'man perlref'.
224
225sub load_dependencies;
226sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000227sub have_native_backend;
228sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000229sub expand_names (@);
230sub find_all_required_sets (@);
231sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000232
233# Each "set" contains one or more libraries which must be included as a
234# group (due to cyclic dependencies). Sets are represented as a Perl array
235# reference pointing to a list of internal library names.
236my @SETS;
237
238# Various mapping tables.
239my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
240my %SET_DEPS; # Maps sets to a list of libraries they depend on.
241my %NAME_MAP; # Maps human-entered names to internal names.
242
243# Have our dependencies been loaded yet?
244my $DEPENDENCIES_LOADED = 0;
245
246# Given a list of human-friendly component names, translate them into a
247# complete set of linker arguments.
248sub expand_dependecies (@) {
249 my @libs = @_;
250 load_dependencies;
251 my @required_sets = find_all_required_sets(expand_names(@libs));
252 my @sorted_sets = topologically_sort_sets(@required_sets);
253
Chris Lattner06e752e2006-06-02 00:56:15 +0000254 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000255 my @result;
256 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000257 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000258}
259
260# Load in the raw dependency data stored at the end of this file.
261sub load_dependencies {
262 return if $DEPENDENCIES_LOADED;
263 $DEPENDENCIES_LOADED = 1;
264 while (<DATA>) {
265 # Parse our line.
266 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
267 die "Malformed dependency data" unless defined $deps;
268 my @libs = split(' ', $libs);
269 my @deps = split(' ', $deps);
270
271 # Record our dependency data.
272 my $set = \@libs;
273 push @SETS, $set;
274 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
275 $SET_DEPS{$set} = \@deps;
276 }
277 build_name_map;
278}
279
280# Build a map converting human-friendly component names into internal
281# library names.
282sub build_name_map {
283 # Add entries for all the actual libraries.
284 foreach my $set (@SETS) {
285 foreach my $lib (sort @$set) {
286 my $short_name = $lib;
287 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
288 $short_name =~ tr/A-Z/a-z/;
289 $NAME_MAP{$short_name} = [$lib];
290 }
291 }
292
293 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000294 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
295 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
296 $NAME_MAP{'engine'} = find_best_engine;
297 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000298}
299
Reid Spencerb195d9d2006-03-23 23:21:29 +0000300# Return true if we have a native backend to use.
301sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000302 my %BUILT;
303 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000304 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
305}
306
307# Find a working subclass of ExecutionEngine for this platform.
308sub find_best_engine {
309 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000310 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000311 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000312 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000313 }
314}
315
316# Get all the human-friendly component names.
317sub name_map_entries {
318 load_dependencies;
319 return sort keys %NAME_MAP;
320}
321
322# Map human-readable names to internal library names.
323sub expand_names (@) {
324 my @names = @_;
325 my @result;
326 foreach my $name (@names) {
327 if (defined $LIB_TO_SET_MAP{$name}) {
328 # We've hit bottom: An actual library name.
329 push @result, $name;
330 } elsif (defined $NAME_MAP{$name}) {
331 # We've found a short name to expand.
332 push @result, expand_names(@{$NAME_MAP{$name}});
333 } else {
334 print STDERR "llvm-config: unknown component name: $name\n";
335 exit(1);
336 }
337 }
338 return @result;
339}
340
341# Given a list of internal library names, return all sets of libraries which
342# will need to be included by the linker (in no particular order).
343sub find_all_required_sets (@) {
344 my @libs = @_;
345 my %sets_added;
346 my @result;
347 find_all_required_sets_helper(\%sets_added, \@result, @libs);
348 return @result;
349}
350
351# Recursive closures are pretty broken in Perl, so we're going to separate
352# this function from find_all_required_sets and pass in the state we need
353# manually, as references. Yes, this is fairly unpleasant.
354sub find_all_required_sets_helper ($$@) {
355 my ($sets_added, $result, @libs) = @_;
356 foreach my $lib (@libs) {
357 my $set = $LIB_TO_SET_MAP{$lib};
358 next if defined $$sets_added{$set};
359 $$sets_added{$set} = 1;
360 push @$result, $set;
361 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
362 }
363}
364
365# Print a list of sets, with a label. Used for debugging.
366sub print_sets ($@) {
367 my ($label, @sets) = @_;
368 my @output;
369 foreach my $set (@sets) { push @output, join(',', @$set); }
370 print "$label: ", join(';', @output), "\n";
371}
372
373# Returns true if $lib is a key in $added.
374sub has_lib_been_added ($$) {
375 my ($added, $lib) = @_;
376 return defined $$added{$LIB_TO_SET_MAP{$lib}};
377}
378
379# Returns true if all the dependencies of $set appear in $added.
380sub have_all_deps_been_added ($$) {
381 my ($added, $set) = @_;
382 #print_sets(" Checking", $set);
383 #print_sets(" Wants", $SET_DEPS{$set});
384 foreach my $lib (@{$SET_DEPS{$set}}) {
385 return 0 unless has_lib_been_added($added, $lib);
386 }
387 return 1;
388}
389
390# Given a list of sets, topologically sort them using dependencies.
391sub topologically_sort_sets (@) {
392 my @sets = @_;
393 my %added;
394 my @result;
395 SCAN: while (@sets) { # We'll delete items from @sets as we go.
396 #print_sets("So far", reverse(@result));
397 #print_sets("Remaining", @sets);
398 for (my $i = 0; $i < @sets; ++$i) {
399 my $set = $sets[$i];
400 if (have_all_deps_been_added(\%added, $set)) {
401 push @result, $set;
402 $added{$set} = 1;
403 #print "Removing $i.\n";
404 splice(@sets, $i, 1);
405 next SCAN; # Restart our scan.
406 }
407 }
408 die "Can't find a library with no dependencies";
409 }
410 return reverse(@result);
411}
412
Reid Spencerf2722ca2006-03-22 15:59:55 +0000413# Our library dependency data will be added after the '__END__' token, and will
414# be read through the magic <DATA> filehandle.
415__END__