blob: 4b9cef168c7026789fc322dce4bd8a177e5465c9 [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
69my $INCLUDEDIR = "$PREFIX/include";
70my $LIBDIR = "$PREFIX/lib";
71my $BINDIR = "$PREFIX/bin";
72$LIBDIR = "$ABS_RUN_DIR/lib";
73$BINDIR = "$ABS_RUN_DIR/bin";
74if ($ABS_RUN_DIR eq $ABS_OBJ_ROOT) {
75 # If we are running out of the build directory, the include dir is in the
76 # srcdir.
77 $INCLUDEDIR = "$LLVM_SRC_ROOT/include";
78} else {
79 $INCLUDEDIR = "$ABS_RUN_DIR/include";
80}
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";
118 } else {
119 usage();
120 }
121 } else {
122 push @components, $arg;
123 }
124}
125
126# If no options were specified, fail.
127usage unless $has_opt;
128
129# If no components were specified, default to 'all'.
130if (@components == 0) {
131 push @components, 'all';
132}
133
134# Handle any arguments which require building our dependency graph.
135if ($want_libs || $want_libnames) {
136 my @libs = expand_dependecies(@components);
137 if ($want_libs) {
138 print join(' ', fix_library_names(@libs)), "\n";
139 }
140 if ($want_libnames) {
141 print join(' ', @libs), "\n";
142 }
143}
144
145exit 0;
146
147#==========================================================================
148# Support Routines
149#==========================================================================
150
151sub usage {
152 print STDERR <<__EOD__;
153Usage: llvm-config <OPTION>... [<COMPONENT>...]
154
155Get various configuration information needed to compile programs which use
156LLVM. Typically called from 'configure' scripts. Examples:
157 llvm-config --cxxflags
158 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000159 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000160
161Options:
162 --version LLVM version.
163 --prefix Installation prefix.
164 --bindir Directory containing LLVM executables.
165 --includedir Directory containing LLVM headers.
166 --libdir Directory containing LLVM libraries.
167 --cxxflags C++ compiler flags for files that include LLVM headers.
168 --ldflags Linker flags.
169 --libs Libraries needed to link against LLVM components.
170 --libnames Bare library names for in-tree builds.
171 --components List of all possible components.
172 --targets-built List of all targets currently built.
173Typical components:
174 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000175 backend Either a native backend or the C backend.
176 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000177__EOD__
178 exit(1);
179}
180
181# Use -lfoo instead of libfoo.a whenever possible, and add directories to
182# files which can't be found using -L.
183sub fix_library_names (@) {
184 my @libs = @_;
185 my @result;
186 foreach my $lib (@libs) {
187 # Transform the bare library name appropriately.
188 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
189 if (defined $basename) {
190 push @result, "-l$basename";
191 } else {
Chris Lattner16ad6182006-06-02 21:48:10 +0000192 push @result, "$LIBDIR/$lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000193 }
194 }
195 return @result;
196}
197
198
199#==========================================================================
200# Library Dependency Analysis
201#==========================================================================
202# Given a few human-readable library names, find all their dependencies
203# and sort them into an order which the linker will like. If we packed
204# our libraries into fewer archives, we could make the linker do much
205# of this work for us.
206#
207# Libraries have two different types of names in this code: Human-friendly
208# "component" names entered on the command-line, and the raw file names
209# we use internally (and ultimately pass to the linker).
210#
211# To understand this code, you'll need a working knowledge of Perl 5,
212# and possibly some quality time with 'man perlref'.
213
214sub load_dependencies;
215sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000216sub have_native_backend;
217sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000218sub expand_names (@);
219sub find_all_required_sets (@);
220sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000221
222# Each "set" contains one or more libraries which must be included as a
223# group (due to cyclic dependencies). Sets are represented as a Perl array
224# reference pointing to a list of internal library names.
225my @SETS;
226
227# Various mapping tables.
228my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
229my %SET_DEPS; # Maps sets to a list of libraries they depend on.
230my %NAME_MAP; # Maps human-entered names to internal names.
231
232# Have our dependencies been loaded yet?
233my $DEPENDENCIES_LOADED = 0;
234
235# Given a list of human-friendly component names, translate them into a
236# complete set of linker arguments.
237sub expand_dependecies (@) {
238 my @libs = @_;
239 load_dependencies;
240 my @required_sets = find_all_required_sets(expand_names(@libs));
241 my @sorted_sets = topologically_sort_sets(@required_sets);
242
Chris Lattner06e752e2006-06-02 00:56:15 +0000243 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000244 my @result;
245 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000246 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000247}
248
249# Load in the raw dependency data stored at the end of this file.
250sub load_dependencies {
251 return if $DEPENDENCIES_LOADED;
252 $DEPENDENCIES_LOADED = 1;
253 while (<DATA>) {
254 # Parse our line.
255 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
256 die "Malformed dependency data" unless defined $deps;
257 my @libs = split(' ', $libs);
258 my @deps = split(' ', $deps);
259
260 # Record our dependency data.
261 my $set = \@libs;
262 push @SETS, $set;
263 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
264 $SET_DEPS{$set} = \@deps;
265 }
266 build_name_map;
267}
268
269# Build a map converting human-friendly component names into internal
270# library names.
271sub build_name_map {
272 # Add entries for all the actual libraries.
273 foreach my $set (@SETS) {
274 foreach my $lib (sort @$set) {
275 my $short_name = $lib;
276 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
277 $short_name =~ tr/A-Z/a-z/;
278 $NAME_MAP{$short_name} = [$lib];
279 }
280 }
281
282 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000283 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
284 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
285 $NAME_MAP{'engine'} = find_best_engine;
286 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000287}
288
Reid Spencerb195d9d2006-03-23 23:21:29 +0000289# Return true if we have a native backend to use.
290sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000291 my %BUILT;
292 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000293 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
294}
295
296# Find a working subclass of ExecutionEngine for this platform.
297sub find_best_engine {
298 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000299 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000300 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000301 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000302 }
303}
304
305# Get all the human-friendly component names.
306sub name_map_entries {
307 load_dependencies;
308 return sort keys %NAME_MAP;
309}
310
311# Map human-readable names to internal library names.
312sub expand_names (@) {
313 my @names = @_;
314 my @result;
315 foreach my $name (@names) {
316 if (defined $LIB_TO_SET_MAP{$name}) {
317 # We've hit bottom: An actual library name.
318 push @result, $name;
319 } elsif (defined $NAME_MAP{$name}) {
320 # We've found a short name to expand.
321 push @result, expand_names(@{$NAME_MAP{$name}});
322 } else {
323 print STDERR "llvm-config: unknown component name: $name\n";
324 exit(1);
325 }
326 }
327 return @result;
328}
329
330# Given a list of internal library names, return all sets of libraries which
331# will need to be included by the linker (in no particular order).
332sub find_all_required_sets (@) {
333 my @libs = @_;
334 my %sets_added;
335 my @result;
336 find_all_required_sets_helper(\%sets_added, \@result, @libs);
337 return @result;
338}
339
340# Recursive closures are pretty broken in Perl, so we're going to separate
341# this function from find_all_required_sets and pass in the state we need
342# manually, as references. Yes, this is fairly unpleasant.
343sub find_all_required_sets_helper ($$@) {
344 my ($sets_added, $result, @libs) = @_;
345 foreach my $lib (@libs) {
346 my $set = $LIB_TO_SET_MAP{$lib};
347 next if defined $$sets_added{$set};
348 $$sets_added{$set} = 1;
349 push @$result, $set;
350 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
351 }
352}
353
354# Print a list of sets, with a label. Used for debugging.
355sub print_sets ($@) {
356 my ($label, @sets) = @_;
357 my @output;
358 foreach my $set (@sets) { push @output, join(',', @$set); }
359 print "$label: ", join(';', @output), "\n";
360}
361
362# Returns true if $lib is a key in $added.
363sub has_lib_been_added ($$) {
364 my ($added, $lib) = @_;
365 return defined $$added{$LIB_TO_SET_MAP{$lib}};
366}
367
368# Returns true if all the dependencies of $set appear in $added.
369sub have_all_deps_been_added ($$) {
370 my ($added, $set) = @_;
371 #print_sets(" Checking", $set);
372 #print_sets(" Wants", $SET_DEPS{$set});
373 foreach my $lib (@{$SET_DEPS{$set}}) {
374 return 0 unless has_lib_been_added($added, $lib);
375 }
376 return 1;
377}
378
379# Given a list of sets, topologically sort them using dependencies.
380sub topologically_sort_sets (@) {
381 my @sets = @_;
382 my %added;
383 my @result;
384 SCAN: while (@sets) { # We'll delete items from @sets as we go.
385 #print_sets("So far", reverse(@result));
386 #print_sets("Remaining", @sets);
387 for (my $i = 0; $i < @sets; ++$i) {
388 my $set = $sets[$i];
389 if (have_all_deps_been_added(\%added, $set)) {
390 push @result, $set;
391 $added{$set} = 1;
392 #print "Removing $i.\n";
393 splice(@sets, $i, 1);
394 next SCAN; # Restart our scan.
395 }
396 }
397 die "Can't find a library with no dependencies";
398 }
399 return reverse(@result);
400}
401
Reid Spencerf2722ca2006-03-22 15:59:55 +0000402# Our library dependency data will be added after the '__END__' token, and will
403# be read through the magic <DATA> filehandle.
404__END__