blob: e83024e2e7046fb1bfc0fbaebb1543fd0048618e [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".
Chris Lattner3e347f22006-06-06 23:54:15 +000066my $ABS_OBJ_ROOT = "$LLVM_OBJ_ROOT/$LLVM_BUILDMODE";
67$ABS_OBJ_ROOT = `cd $ABS_OBJ_ROOT; pwd` if (-d $ABS_OBJ_ROOT);
Chris Lattner16ad6182006-06-02 21:48:10 +000068chomp($ABS_OBJ_ROOT);
69
Chris Lattner0cd059e2006-06-02 22:03:50 +000070my $INCLUDEDIR = "$ABS_RUN_DIR/include";
71my $LIBDIR = "$ABS_RUN_DIR/lib";
72my $BINDIR = "$ABS_RUN_DIR/bin";
Chris Lattner16ad6182006-06-02 21:48:10 +000073if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) {
74 # If we are running out of the build directory, the include dir is in the
75 # srcdir.
76 $INCLUDEDIR = "$LLVM_SRC_ROOT/include";
77} else {
Chris Lattner0cd059e2006-06-02 22:03:50 +000078 # If installed, ignore the prefix the tree was configured with, use the
79 # current prefix.
80 $PREFIX = $ABS_RUN_DIR;
Chris Lattner16ad6182006-06-02 21:48:10 +000081}
Chris Lattnere02b97b2006-06-02 01:23:18 +000082
Reid Spencerf2722ca2006-03-22 15:59:55 +000083sub usage;
84sub fix_library_names (@);
Chris Lattnerd179de52006-06-06 22:38:29 +000085sub fix_library_files (@);
Reid Spencerf2722ca2006-03-22 15:59:55 +000086sub expand_dependecies (@);
87sub name_map_entries;
88
89# Parse our command-line arguments.
90usage if @ARGV == 0;
91my @components;
92my $has_opt = 0;
93my $want_libs = 0;
94my $want_libnames = 0;
Chris Lattnerd179de52006-06-06 22:38:29 +000095my $want_libfiles = 0;
Reid Spencerf2722ca2006-03-22 15:59:55 +000096my $want_components = 0;
97foreach my $arg (@ARGV) {
98 if ($arg =~ /^-/) {
99 if ($arg eq "--version") {
100 $has_opt = 1; print "$VERSION\n";
101 } elsif ($arg eq "--prefix") {
102 $has_opt = 1; print "$PREFIX\n";
103 } elsif ($arg eq "--bindir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000104 $has_opt = 1; print "$BINDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000105 } elsif ($arg eq "--includedir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000106 $has_opt = 1; print "$INCLUDEDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000107 } elsif ($arg eq "--libdir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000108 $has_opt = 1; print "$LIBDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000109 } elsif ($arg eq "--cxxflags") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000110 $has_opt = 1; print "-I$INCLUDEDIR $CXXFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000111 } elsif ($arg eq "--ldflags") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000112 $has_opt = 1; print "-L$LIBDIR $LDFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000113 } elsif ($arg eq "--libs") {
114 $has_opt = 1; $want_libs = 1;
115 } elsif ($arg eq "--libnames") {
116 $has_opt = 1; $want_libnames = 1;
Chris Lattnerd179de52006-06-06 22:38:29 +0000117 } elsif ($arg eq "--libfiles") {
118 $has_opt = 1; $want_libfiles = 1;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000119 } elsif ($arg eq "--components") {
120 $has_opt = 1; print join(' ', name_map_entries), "\n";
121 } elsif ($arg eq "--targets-built") {
122 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
Chris Lattner0cd059e2006-06-02 22:03:50 +0000123 } elsif ($arg eq "--build-mode") {
124 $has_opt = 1; print "$LLVM_BUILDMODE\n";
125 } elsif ($arg eq "--obj-root") {
126 $has_opt = 1; print `cd $LLVM_OBJ_ROOT/; pwd` . "\n";
127 } elsif ($arg eq "--src-root") {
128 $has_opt = 1; print `cd $LLVM_SRC_ROOT/; pwd` . "\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000129 } else {
130 usage();
131 }
132 } else {
133 push @components, $arg;
134 }
135}
136
137# If no options were specified, fail.
138usage unless $has_opt;
139
140# If no components were specified, default to 'all'.
141if (@components == 0) {
142 push @components, 'all';
143}
144
145# Handle any arguments which require building our dependency graph.
Chris Lattnerd179de52006-06-06 22:38:29 +0000146if ($want_libs || $want_libnames || $want_libfiles) {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000147 my @libs = expand_dependecies(@components);
Chris Lattnerd179de52006-06-06 22:38:29 +0000148 print join(' ', fix_library_names(@libs)), "\n" if ($want_libs);
149 print join(' ', @libs), "\n" if ($want_libnames);
150 print join(' ', fix_library_files(@libs)), "\n" if ($want_libfiles);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000151}
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.
Chris Lattnerd179de52006-06-06 22:38:29 +0000181 --libfiles Fully qualified library filenames for makefile depends.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000182 --components List of all possible components.
183 --targets-built List of all targets currently built.
Chris Lattner0cd059e2006-06-02 22:03:50 +0000184 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).
Reid Spencerf2722ca2006-03-22 15:59:55 +0000185Typical components:
186 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000187 backend Either a native backend or the C backend.
188 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000189__EOD__
190 exit(1);
191}
192
193# Use -lfoo instead of libfoo.a whenever possible, and add directories to
194# files which can't be found using -L.
195sub fix_library_names (@) {
196 my @libs = @_;
197 my @result;
198 foreach my $lib (@libs) {
199 # Transform the bare library name appropriately.
200 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
201 if (defined $basename) {
202 push @result, "-l$basename";
203 } else {
Chris Lattner16ad6182006-06-02 21:48:10 +0000204 push @result, "$LIBDIR/$lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000205 }
206 }
207 return @result;
208}
209
Chris Lattnerd179de52006-06-06 22:38:29 +0000210# Turn the list of libraries into a list of files.
211sub fix_library_files(@) {
212 my @libs = @_;
213 my @result;
214 foreach my $lib (@libs) {
215 # Transform the bare library name into a filename.
216 push @result, "$LIBDIR/$lib";
217 }
218 return @result;
219}
Reid Spencerf2722ca2006-03-22 15:59:55 +0000220
221#==========================================================================
222# Library Dependency Analysis
223#==========================================================================
224# Given a few human-readable library names, find all their dependencies
225# and sort them into an order which the linker will like. If we packed
226# our libraries into fewer archives, we could make the linker do much
227# of this work for us.
228#
229# Libraries have two different types of names in this code: Human-friendly
230# "component" names entered on the command-line, and the raw file names
231# we use internally (and ultimately pass to the linker).
232#
233# To understand this code, you'll need a working knowledge of Perl 5,
234# and possibly some quality time with 'man perlref'.
235
236sub load_dependencies;
237sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000238sub have_native_backend;
239sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000240sub expand_names (@);
241sub find_all_required_sets (@);
242sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000243
244# Each "set" contains one or more libraries which must be included as a
245# group (due to cyclic dependencies). Sets are represented as a Perl array
246# reference pointing to a list of internal library names.
247my @SETS;
248
249# Various mapping tables.
250my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
251my %SET_DEPS; # Maps sets to a list of libraries they depend on.
252my %NAME_MAP; # Maps human-entered names to internal names.
253
254# Have our dependencies been loaded yet?
255my $DEPENDENCIES_LOADED = 0;
256
257# Given a list of human-friendly component names, translate them into a
258# complete set of linker arguments.
259sub expand_dependecies (@) {
260 my @libs = @_;
261 load_dependencies;
262 my @required_sets = find_all_required_sets(expand_names(@libs));
263 my @sorted_sets = topologically_sort_sets(@required_sets);
264
Chris Lattner06e752e2006-06-02 00:56:15 +0000265 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000266 my @result;
267 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000268 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000269}
270
271# Load in the raw dependency data stored at the end of this file.
272sub load_dependencies {
273 return if $DEPENDENCIES_LOADED;
274 $DEPENDENCIES_LOADED = 1;
275 while (<DATA>) {
276 # Parse our line.
277 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
278 die "Malformed dependency data" unless defined $deps;
279 my @libs = split(' ', $libs);
280 my @deps = split(' ', $deps);
281
282 # Record our dependency data.
283 my $set = \@libs;
284 push @SETS, $set;
285 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
286 $SET_DEPS{$set} = \@deps;
287 }
288 build_name_map;
289}
290
291# Build a map converting human-friendly component names into internal
292# library names.
293sub build_name_map {
294 # Add entries for all the actual libraries.
295 foreach my $set (@SETS) {
296 foreach my $lib (sort @$set) {
297 my $short_name = $lib;
298 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
299 $short_name =~ tr/A-Z/a-z/;
300 $NAME_MAP{$short_name} = [$lib];
301 }
302 }
303
304 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000305 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
306 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
307 $NAME_MAP{'engine'} = find_best_engine;
308 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000309}
310
Reid Spencerb195d9d2006-03-23 23:21:29 +0000311# Return true if we have a native backend to use.
312sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000313 my %BUILT;
314 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000315 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
316}
317
318# Find a working subclass of ExecutionEngine for this platform.
319sub find_best_engine {
320 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000321 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000322 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000323 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000324 }
325}
326
327# Get all the human-friendly component names.
328sub name_map_entries {
329 load_dependencies;
330 return sort keys %NAME_MAP;
331}
332
333# Map human-readable names to internal library names.
334sub expand_names (@) {
335 my @names = @_;
336 my @result;
337 foreach my $name (@names) {
338 if (defined $LIB_TO_SET_MAP{$name}) {
339 # We've hit bottom: An actual library name.
340 push @result, $name;
341 } elsif (defined $NAME_MAP{$name}) {
342 # We've found a short name to expand.
343 push @result, expand_names(@{$NAME_MAP{$name}});
344 } else {
345 print STDERR "llvm-config: unknown component name: $name\n";
346 exit(1);
347 }
348 }
349 return @result;
350}
351
352# Given a list of internal library names, return all sets of libraries which
353# will need to be included by the linker (in no particular order).
354sub find_all_required_sets (@) {
355 my @libs = @_;
356 my %sets_added;
357 my @result;
358 find_all_required_sets_helper(\%sets_added, \@result, @libs);
359 return @result;
360}
361
362# Recursive closures are pretty broken in Perl, so we're going to separate
363# this function from find_all_required_sets and pass in the state we need
364# manually, as references. Yes, this is fairly unpleasant.
365sub find_all_required_sets_helper ($$@) {
366 my ($sets_added, $result, @libs) = @_;
367 foreach my $lib (@libs) {
368 my $set = $LIB_TO_SET_MAP{$lib};
369 next if defined $$sets_added{$set};
370 $$sets_added{$set} = 1;
371 push @$result, $set;
372 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
373 }
374}
375
376# Print a list of sets, with a label. Used for debugging.
377sub print_sets ($@) {
378 my ($label, @sets) = @_;
379 my @output;
380 foreach my $set (@sets) { push @output, join(',', @$set); }
381 print "$label: ", join(';', @output), "\n";
382}
383
384# Returns true if $lib is a key in $added.
385sub has_lib_been_added ($$) {
386 my ($added, $lib) = @_;
387 return defined $$added{$LIB_TO_SET_MAP{$lib}};
388}
389
390# Returns true if all the dependencies of $set appear in $added.
391sub have_all_deps_been_added ($$) {
392 my ($added, $set) = @_;
393 #print_sets(" Checking", $set);
394 #print_sets(" Wants", $SET_DEPS{$set});
395 foreach my $lib (@{$SET_DEPS{$set}}) {
396 return 0 unless has_lib_been_added($added, $lib);
397 }
398 return 1;
399}
400
401# Given a list of sets, topologically sort them using dependencies.
402sub topologically_sort_sets (@) {
403 my @sets = @_;
404 my %added;
405 my @result;
406 SCAN: while (@sets) { # We'll delete items from @sets as we go.
407 #print_sets("So far", reverse(@result));
408 #print_sets("Remaining", @sets);
409 for (my $i = 0; $i < @sets; ++$i) {
410 my $set = $sets[$i];
411 if (have_all_deps_been_added(\%added, $set)) {
412 push @result, $set;
413 $added{$set} = 1;
414 #print "Removing $i.\n";
415 splice(@sets, $i, 1);
416 next SCAN; # Restart our scan.
417 }
418 }
419 die "Can't find a library with no dependencies";
420 }
421 return reverse(@result);
422}
423
Reid Spencerf2722ca2006-03-22 15:59:55 +0000424# Our library dependency data will be added after the '__END__' token, and will
425# be read through the magic <DATA> filehandle.
426__END__