blob: a6cf5f82736904e2540388d40ad97400df4e99d4 [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
Jeff Cohen02c91ef2007-03-28 04:45:02 +000047# Must pretend x86_64 architecture is really x86, otherwise the native backend
48# won't get linked in.
49$ARCH = "x86" if $ARCH eq "x86_64";
50
Reid Spencerf2722ca2006-03-22 15:59:55 +000051#---- begin Makefile values ----
Reid Spencerf72538e2007-01-06 02:48:03 +000052my $CFLAGS = q{@LLVM_CFLAGS@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000053my $CXXFLAGS = q{@LLVM_CXXFLAGS@};
54my $LDFLAGS = q{@LLVM_LDFLAGS@};
Reid Spencer1bc68642006-07-27 23:00:30 +000055my $SYSTEM_LIBS = q{@LIBS@};
Chris Lattnerabdbae72006-06-02 19:13:29 +000056my $LLVM_BUILDMODE = q{@LLVM_BUILDMODE@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000057#---- end Makefile values ----
58
Chris Lattner16ad6182006-06-02 21:48:10 +000059# Figure out where llvm-config is being run from. Primarily, we care if it has
60# been installed, or is running from the build directory, which changes the
61# locations of some files.
62
Chris Lattnere02b97b2006-06-02 01:23:18 +000063# Convert the current executable name into its directory (e.g. ".").
Chris Lattner16ad6182006-06-02 21:48:10 +000064my ($RUN_DIR) = ($0 =~ /^(.*)\/.*$/);
65
66# Turn the directory into an absolute directory on the file system, also pop up
67# from "bin" into the build or prefix dir.
68my $ABS_RUN_DIR = `cd $RUN_DIR/..; pwd`;
69chomp($ABS_RUN_DIR);
70
71# Compute the absolute object directory build, e.g. "foo/llvm/Debug".
Chris Lattner3e347f22006-06-06 23:54:15 +000072my $ABS_OBJ_ROOT = "$LLVM_OBJ_ROOT/$LLVM_BUILDMODE";
73$ABS_OBJ_ROOT = `cd $ABS_OBJ_ROOT; pwd` if (-d $ABS_OBJ_ROOT);
Chris Lattner16ad6182006-06-02 21:48:10 +000074chomp($ABS_OBJ_ROOT);
75
Chris Lattner0cd059e2006-06-02 22:03:50 +000076my $INCLUDEDIR = "$ABS_RUN_DIR/include";
77my $LIBDIR = "$ABS_RUN_DIR/lib";
78my $BINDIR = "$ABS_RUN_DIR/bin";
Chris Lattner16ad6182006-06-02 21:48:10 +000079if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) {
80 # If we are running out of the build directory, the include dir is in the
81 # srcdir.
82 $INCLUDEDIR = "$LLVM_SRC_ROOT/include";
83} else {
Chris Lattner0cd059e2006-06-02 22:03:50 +000084 # If installed, ignore the prefix the tree was configured with, use the
85 # current prefix.
86 $PREFIX = $ABS_RUN_DIR;
Chris Lattner16ad6182006-06-02 21:48:10 +000087}
Chris Lattnere02b97b2006-06-02 01:23:18 +000088
Reid Spencerf2722ca2006-03-22 15:59:55 +000089sub usage;
90sub fix_library_names (@);
Chris Lattnerd179de52006-06-06 22:38:29 +000091sub fix_library_files (@);
Reid Spencerd8c20a92006-08-03 21:45:35 +000092sub expand_dependencies (@);
Reid Spencerf2722ca2006-03-22 15:59:55 +000093sub name_map_entries;
94
95# Parse our command-line arguments.
96usage if @ARGV == 0;
97my @components;
98my $has_opt = 0;
99my $want_libs = 0;
100my $want_libnames = 0;
Chris Lattnerd179de52006-06-06 22:38:29 +0000101my $want_libfiles = 0;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000102my $want_components = 0;
103foreach my $arg (@ARGV) {
104 if ($arg =~ /^-/) {
105 if ($arg eq "--version") {
106 $has_opt = 1; print "$VERSION\n";
107 } elsif ($arg eq "--prefix") {
108 $has_opt = 1; print "$PREFIX\n";
109 } elsif ($arg eq "--bindir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000110 $has_opt = 1; print "$BINDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000111 } elsif ($arg eq "--includedir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000112 $has_opt = 1; print "$INCLUDEDIR\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000113 } elsif ($arg eq "--libdir") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000114 $has_opt = 1; print "$LIBDIR\n";
Reid Spencerf72538e2007-01-06 02:48:03 +0000115 } elsif ($arg eq "--cflags") {
116 $has_opt = 1; print "-I$INCLUDEDIR $CFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000117 } elsif ($arg eq "--cxxflags") {
Chris Lattner16ad6182006-06-02 21:48:10 +0000118 $has_opt = 1; print "-I$INCLUDEDIR $CXXFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000119 } elsif ($arg eq "--ldflags") {
Reid Spencer1bc68642006-07-27 23:00:30 +0000120 $has_opt = 1; print "-L$LIBDIR $LDFLAGS $SYSTEM_LIBS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000121 } elsif ($arg eq "--libs") {
122 $has_opt = 1; $want_libs = 1;
123 } elsif ($arg eq "--libnames") {
124 $has_opt = 1; $want_libnames = 1;
Chris Lattnerd179de52006-06-06 22:38:29 +0000125 } elsif ($arg eq "--libfiles") {
126 $has_opt = 1; $want_libfiles = 1;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000127 } elsif ($arg eq "--components") {
128 $has_opt = 1; print join(' ', name_map_entries), "\n";
129 } elsif ($arg eq "--targets-built") {
130 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
Chris Lattner0cd059e2006-06-02 22:03:50 +0000131 } elsif ($arg eq "--build-mode") {
132 $has_opt = 1; print "$LLVM_BUILDMODE\n";
133 } elsif ($arg eq "--obj-root") {
Reid Spencerb43ce5a2006-08-11 21:50:24 +0000134 $has_opt = 1; print `cd $LLVM_OBJ_ROOT/; pwd`;
Chris Lattner0cd059e2006-06-02 22:03:50 +0000135 } elsif ($arg eq "--src-root") {
Reid Spencerb43ce5a2006-08-11 21:50:24 +0000136 $has_opt = 1; print `cd $LLVM_SRC_ROOT/; pwd`;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000137 } else {
138 usage();
139 }
140 } else {
141 push @components, $arg;
142 }
143}
144
145# If no options were specified, fail.
146usage unless $has_opt;
147
148# If no components were specified, default to 'all'.
149if (@components == 0) {
150 push @components, 'all';
151}
152
Chris Lattner54eae9e2006-09-04 05:35:23 +0000153# Force component names to lower case.
154@components = map lc, @components;
155
Reid Spencerf2722ca2006-03-22 15:59:55 +0000156# Handle any arguments which require building our dependency graph.
Chris Lattnerd179de52006-06-06 22:38:29 +0000157if ($want_libs || $want_libnames || $want_libfiles) {
Reid Spencerd8c20a92006-08-03 21:45:35 +0000158 my @libs = expand_dependencies(@components);
Chris Lattnerd179de52006-06-06 22:38:29 +0000159 print join(' ', fix_library_names(@libs)), "\n" if ($want_libs);
160 print join(' ', @libs), "\n" if ($want_libnames);
161 print join(' ', fix_library_files(@libs)), "\n" if ($want_libfiles);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000162}
163
164exit 0;
165
166#==========================================================================
167# Support Routines
168#==========================================================================
169
170sub usage {
171 print STDERR <<__EOD__;
172Usage: llvm-config <OPTION>... [<COMPONENT>...]
173
174Get various configuration information needed to compile programs which use
175LLVM. Typically called from 'configure' scripts. Examples:
176 llvm-config --cxxflags
177 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000178 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000179
180Options:
Chris Lattner0cd059e2006-06-02 22:03:50 +0000181 --version Print LLVM version.
182 --prefix Print the installation prefix.
183 --src-root Print the source root LLVM was built from.
184 --obj-root Print the object root used to build LLVM.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000185 --bindir Directory containing LLVM executables.
186 --includedir Directory containing LLVM headers.
187 --libdir Directory containing LLVM libraries.
Reid Spencerf72538e2007-01-06 02:48:03 +0000188 --cflags C compiler flags for files that include LLVM headers.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000189 --cxxflags C++ compiler flags for files that include LLVM headers.
Chris Lattner0cd059e2006-06-02 22:03:50 +0000190 --ldflags Print Linker flags.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000191 --libs Libraries needed to link against LLVM components.
192 --libnames Bare library names for in-tree builds.
Chris Lattnerd179de52006-06-06 22:38:29 +0000193 --libfiles Fully qualified library filenames for makefile depends.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000194 --components List of all possible components.
195 --targets-built List of all targets currently built.
Chris Lattner0cd059e2006-06-02 22:03:50 +0000196 --build-mode Print build mode of LLVM tree (e.g. Debug or Release).
Reid Spencerf2722ca2006-03-22 15:59:55 +0000197Typical components:
198 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000199 backend Either a native backend or the C backend.
200 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000201__EOD__
202 exit(1);
203}
204
205# Use -lfoo instead of libfoo.a whenever possible, and add directories to
206# files which can't be found using -L.
207sub fix_library_names (@) {
208 my @libs = @_;
209 my @result;
210 foreach my $lib (@libs) {
211 # Transform the bare library name appropriately.
212 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
213 if (defined $basename) {
214 push @result, "-l$basename";
215 } else {
Chris Lattner16ad6182006-06-02 21:48:10 +0000216 push @result, "$LIBDIR/$lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000217 }
218 }
219 return @result;
220}
221
Chris Lattnerd179de52006-06-06 22:38:29 +0000222# Turn the list of libraries into a list of files.
223sub fix_library_files(@) {
224 my @libs = @_;
225 my @result;
226 foreach my $lib (@libs) {
227 # Transform the bare library name into a filename.
228 push @result, "$LIBDIR/$lib";
229 }
230 return @result;
231}
Reid Spencerf2722ca2006-03-22 15:59:55 +0000232
233#==========================================================================
234# Library Dependency Analysis
235#==========================================================================
236# Given a few human-readable library names, find all their dependencies
237# and sort them into an order which the linker will like. If we packed
238# our libraries into fewer archives, we could make the linker do much
239# of this work for us.
240#
241# Libraries have two different types of names in this code: Human-friendly
242# "component" names entered on the command-line, and the raw file names
243# we use internally (and ultimately pass to the linker).
244#
245# To understand this code, you'll need a working knowledge of Perl 5,
246# and possibly some quality time with 'man perlref'.
247
248sub load_dependencies;
249sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000250sub have_native_backend;
251sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000252sub expand_names (@);
253sub find_all_required_sets (@);
254sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000255
256# Each "set" contains one or more libraries which must be included as a
257# group (due to cyclic dependencies). Sets are represented as a Perl array
258# reference pointing to a list of internal library names.
259my @SETS;
260
261# Various mapping tables.
262my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
263my %SET_DEPS; # Maps sets to a list of libraries they depend on.
264my %NAME_MAP; # Maps human-entered names to internal names.
265
266# Have our dependencies been loaded yet?
267my $DEPENDENCIES_LOADED = 0;
268
269# Given a list of human-friendly component names, translate them into a
270# complete set of linker arguments.
Reid Spencerd8c20a92006-08-03 21:45:35 +0000271sub expand_dependencies (@) {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000272 my @libs = @_;
273 load_dependencies;
274 my @required_sets = find_all_required_sets(expand_names(@libs));
275 my @sorted_sets = topologically_sort_sets(@required_sets);
276
Chris Lattner06e752e2006-06-02 00:56:15 +0000277 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000278 my @result;
279 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000280 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000281}
282
283# Load in the raw dependency data stored at the end of this file.
284sub load_dependencies {
285 return if $DEPENDENCIES_LOADED;
286 $DEPENDENCIES_LOADED = 1;
287 while (<DATA>) {
288 # Parse our line.
Anton Korobeynikovde9c02b2006-08-04 21:52:23 +0000289 my ($libs, $deps) = /^\s*([^:]+):\s*(.*)\s*$/;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000290 die "Malformed dependency data" unless defined $deps;
291 my @libs = split(' ', $libs);
292 my @deps = split(' ', $deps);
293
294 # Record our dependency data.
295 my $set = \@libs;
296 push @SETS, $set;
297 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
298 $SET_DEPS{$set} = \@deps;
299 }
300 build_name_map;
301}
302
303# Build a map converting human-friendly component names into internal
304# library names.
305sub build_name_map {
306 # Add entries for all the actual libraries.
307 foreach my $set (@SETS) {
308 foreach my $lib (sort @$set) {
309 my $short_name = $lib;
310 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
311 $short_name =~ tr/A-Z/a-z/;
312 $NAME_MAP{$short_name} = [$lib];
313 }
314 }
315
316 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000317 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
318 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
319 $NAME_MAP{'engine'} = find_best_engine;
320 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000321}
322
Reid Spencerb195d9d2006-03-23 23:21:29 +0000323# Return true if we have a native backend to use.
324sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000325 my %BUILT;
326 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000327 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
328}
329
330# Find a working subclass of ExecutionEngine for this platform.
331sub find_best_engine {
332 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000333 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000334 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000335 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000336 }
337}
338
339# Get all the human-friendly component names.
340sub name_map_entries {
341 load_dependencies;
342 return sort keys %NAME_MAP;
343}
344
345# Map human-readable names to internal library names.
346sub expand_names (@) {
347 my @names = @_;
348 my @result;
349 foreach my $name (@names) {
350 if (defined $LIB_TO_SET_MAP{$name}) {
351 # We've hit bottom: An actual library name.
352 push @result, $name;
353 } elsif (defined $NAME_MAP{$name}) {
354 # We've found a short name to expand.
355 push @result, expand_names(@{$NAME_MAP{$name}});
356 } else {
357 print STDERR "llvm-config: unknown component name: $name\n";
358 exit(1);
359 }
360 }
361 return @result;
362}
363
364# Given a list of internal library names, return all sets of libraries which
365# will need to be included by the linker (in no particular order).
366sub find_all_required_sets (@) {
367 my @libs = @_;
368 my %sets_added;
369 my @result;
370 find_all_required_sets_helper(\%sets_added, \@result, @libs);
371 return @result;
372}
373
374# Recursive closures are pretty broken in Perl, so we're going to separate
375# this function from find_all_required_sets and pass in the state we need
376# manually, as references. Yes, this is fairly unpleasant.
377sub find_all_required_sets_helper ($$@) {
378 my ($sets_added, $result, @libs) = @_;
379 foreach my $lib (@libs) {
380 my $set = $LIB_TO_SET_MAP{$lib};
381 next if defined $$sets_added{$set};
382 $$sets_added{$set} = 1;
383 push @$result, $set;
384 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
385 }
386}
387
388# Print a list of sets, with a label. Used for debugging.
389sub print_sets ($@) {
390 my ($label, @sets) = @_;
391 my @output;
392 foreach my $set (@sets) { push @output, join(',', @$set); }
393 print "$label: ", join(';', @output), "\n";
394}
395
396# Returns true if $lib is a key in $added.
397sub has_lib_been_added ($$) {
398 my ($added, $lib) = @_;
399 return defined $$added{$LIB_TO_SET_MAP{$lib}};
400}
401
402# Returns true if all the dependencies of $set appear in $added.
403sub have_all_deps_been_added ($$) {
404 my ($added, $set) = @_;
405 #print_sets(" Checking", $set);
406 #print_sets(" Wants", $SET_DEPS{$set});
407 foreach my $lib (@{$SET_DEPS{$set}}) {
408 return 0 unless has_lib_been_added($added, $lib);
409 }
410 return 1;
411}
412
413# Given a list of sets, topologically sort them using dependencies.
414sub topologically_sort_sets (@) {
415 my @sets = @_;
416 my %added;
417 my @result;
418 SCAN: while (@sets) { # We'll delete items from @sets as we go.
419 #print_sets("So far", reverse(@result));
420 #print_sets("Remaining", @sets);
421 for (my $i = 0; $i < @sets; ++$i) {
422 my $set = $sets[$i];
423 if (have_all_deps_been_added(\%added, $set)) {
424 push @result, $set;
425 $added{$set} = 1;
426 #print "Removing $i.\n";
427 splice(@sets, $i, 1);
428 next SCAN; # Restart our scan.
429 }
430 }
431 die "Can't find a library with no dependencies";
432 }
433 return reverse(@result);
434}
435
Reid Spencerf2722ca2006-03-22 15:59:55 +0000436# Our library dependency data will be added after the '__END__' token, and will
437# be read through the magic <DATA> filehandle.
438__END__