blob: 5029541229346223441335eafbf1535dec133e57 [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@};
Reid Spencerf2722ca2006-03-22 15:59:55 +000050#---- end Makefile values ----
51
Chris Lattnere02b97b2006-06-02 01:23:18 +000052# Convert the current executable name into its directory (e.g. ".").
53my ($PARTIALDIR) = ($0 =~ /^(.*)\/.*$/);
54
Reid Spencerf2722ca2006-03-22 15:59:55 +000055sub usage;
56sub fix_library_names (@);
57sub expand_dependecies (@);
58sub name_map_entries;
59
60# Parse our command-line arguments.
61usage if @ARGV == 0;
62my @components;
63my $has_opt = 0;
64my $want_libs = 0;
65my $want_libnames = 0;
66my $want_components = 0;
67foreach my $arg (@ARGV) {
68 if ($arg =~ /^-/) {
69 if ($arg eq "--version") {
70 $has_opt = 1; print "$VERSION\n";
Chris Lattnere02b97b2006-06-02 01:23:18 +000071 } elsif ($arg eq "--use-current-dir-as-prefix") {
72 # Convert the scripts executable dir into a full absolute directory.
73 my $ABSDIR = `cd $PARTIALDIR/..; pwd`;
74 chomp($ABSDIR);
75 $PREFIX = $ABSDIR;
Reid Spencerf2722ca2006-03-22 15:59:55 +000076 } elsif ($arg eq "--prefix") {
77 $has_opt = 1; print "$PREFIX\n";
78 } elsif ($arg eq "--bindir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000079 $has_opt = 1; print "$PREFIX/bin\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000080 } elsif ($arg eq "--includedir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000081 $has_opt = 1; print "$PREFIX/include\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000082 } elsif ($arg eq "--libdir") {
Chris Lattner411c33d2006-06-02 01:04:35 +000083 $has_opt = 1; print "$PREFIX/lib\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000084 } elsif ($arg eq "--cxxflags") {
Chris Lattner411c33d2006-06-02 01:04:35 +000085 $has_opt = 1; print "-I$PREFIX/include $CXXFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000086 } elsif ($arg eq "--ldflags") {
Chris Lattner411c33d2006-06-02 01:04:35 +000087 $has_opt = 1; print "-L$PREFIX/lib $LDFLAGS\n";
Reid Spencerf2722ca2006-03-22 15:59:55 +000088 } elsif ($arg eq "--libs") {
89 $has_opt = 1; $want_libs = 1;
90 } elsif ($arg eq "--libnames") {
91 $has_opt = 1; $want_libnames = 1;
92 } elsif ($arg eq "--components") {
93 $has_opt = 1; print join(' ', name_map_entries), "\n";
94 } elsif ($arg eq "--targets-built") {
95 $has_opt = 1; print join(' ', @TARGETS_BUILT), "\n";
96 } else {
97 usage();
98 }
99 } else {
100 push @components, $arg;
101 }
102}
103
104# If no options were specified, fail.
105usage unless $has_opt;
106
107# If no components were specified, default to 'all'.
108if (@components == 0) {
109 push @components, 'all';
110}
111
112# Handle any arguments which require building our dependency graph.
113if ($want_libs || $want_libnames) {
114 my @libs = expand_dependecies(@components);
115 if ($want_libs) {
116 print join(' ', fix_library_names(@libs)), "\n";
117 }
118 if ($want_libnames) {
119 print join(' ', @libs), "\n";
120 }
121}
122
123exit 0;
124
125#==========================================================================
126# Support Routines
127#==========================================================================
128
129sub usage {
130 print STDERR <<__EOD__;
131Usage: llvm-config <OPTION>... [<COMPONENT>...]
132
133Get various configuration information needed to compile programs which use
134LLVM. Typically called from 'configure' scripts. Examples:
135 llvm-config --cxxflags
136 llvm-config --ldflags
Reid Spencerb195d9d2006-03-23 23:21:29 +0000137 llvm-config --libs engine bcreader scalaropts
Reid Spencerf2722ca2006-03-22 15:59:55 +0000138
139Options:
140 --version LLVM version.
141 --prefix Installation prefix.
142 --bindir Directory containing LLVM executables.
143 --includedir Directory containing LLVM headers.
144 --libdir Directory containing LLVM libraries.
145 --cxxflags C++ compiler flags for files that include LLVM headers.
146 --ldflags Linker flags.
147 --libs Libraries needed to link against LLVM components.
148 --libnames Bare library names for in-tree builds.
149 --components List of all possible components.
150 --targets-built List of all targets currently built.
151Typical components:
152 all All LLVM libraries (default).
Reid Spencerb195d9d2006-03-23 23:21:29 +0000153 backend Either a native backend or the C backend.
154 engine Either a native JIT or a bytecode interpreter.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000155__EOD__
156 exit(1);
157}
158
159# Use -lfoo instead of libfoo.a whenever possible, and add directories to
160# files which can't be found using -L.
161sub fix_library_names (@) {
162 my @libs = @_;
163 my @result;
164 foreach my $lib (@libs) {
165 # Transform the bare library name appropriately.
166 my ($basename) = ($lib =~ /^lib([^.]*)\.a/);
167 if (defined $basename) {
168 push @result, "-l$basename";
169 } else {
Chris Lattner411c33d2006-06-02 01:04:35 +0000170 push @result, "$PREFIX/lib/$lib";
Reid Spencerf2722ca2006-03-22 15:59:55 +0000171 }
172 }
173 return @result;
174}
175
176
177#==========================================================================
178# Library Dependency Analysis
179#==========================================================================
180# Given a few human-readable library names, find all their dependencies
181# and sort them into an order which the linker will like. If we packed
182# our libraries into fewer archives, we could make the linker do much
183# of this work for us.
184#
185# Libraries have two different types of names in this code: Human-friendly
186# "component" names entered on the command-line, and the raw file names
187# we use internally (and ultimately pass to the linker).
188#
189# To understand this code, you'll need a working knowledge of Perl 5,
190# and possibly some quality time with 'man perlref'.
191
192sub load_dependencies;
193sub build_name_map;
Reid Spencerb195d9d2006-03-23 23:21:29 +0000194sub have_native_backend;
195sub find_best_engine;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000196sub expand_names (@);
197sub find_all_required_sets (@);
198sub find_all_required_sets_helper ($$@);
Reid Spencerf2722ca2006-03-22 15:59:55 +0000199
200# Each "set" contains one or more libraries which must be included as a
201# group (due to cyclic dependencies). Sets are represented as a Perl array
202# reference pointing to a list of internal library names.
203my @SETS;
204
205# Various mapping tables.
206my %LIB_TO_SET_MAP; # Maps internal library names to their sets.
207my %SET_DEPS; # Maps sets to a list of libraries they depend on.
208my %NAME_MAP; # Maps human-entered names to internal names.
209
210# Have our dependencies been loaded yet?
211my $DEPENDENCIES_LOADED = 0;
212
213# Given a list of human-friendly component names, translate them into a
214# complete set of linker arguments.
215sub expand_dependecies (@) {
216 my @libs = @_;
217 load_dependencies;
218 my @required_sets = find_all_required_sets(expand_names(@libs));
219 my @sorted_sets = topologically_sort_sets(@required_sets);
220
Chris Lattner06e752e2006-06-02 00:56:15 +0000221 # Expand the library sets into libraries.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000222 my @result;
223 foreach my $set (@sorted_sets) { push @result, @{$set}; }
Chris Lattner06e752e2006-06-02 00:56:15 +0000224 return @result;
Reid Spencerf2722ca2006-03-22 15:59:55 +0000225}
226
227# Load in the raw dependency data stored at the end of this file.
228sub load_dependencies {
229 return if $DEPENDENCIES_LOADED;
230 $DEPENDENCIES_LOADED = 1;
231 while (<DATA>) {
232 # Parse our line.
233 my ($libs, $deps) = /^(^[^:]+): ?(.*)$/;
234 die "Malformed dependency data" unless defined $deps;
235 my @libs = split(' ', $libs);
236 my @deps = split(' ', $deps);
237
238 # Record our dependency data.
239 my $set = \@libs;
240 push @SETS, $set;
241 foreach my $lib (@libs) { $LIB_TO_SET_MAP{$lib} = $set; }
242 $SET_DEPS{$set} = \@deps;
243 }
244 build_name_map;
245}
246
247# Build a map converting human-friendly component names into internal
248# library names.
249sub build_name_map {
250 # Add entries for all the actual libraries.
251 foreach my $set (@SETS) {
252 foreach my $lib (sort @$set) {
253 my $short_name = $lib;
254 $short_name =~ s/^(lib)?LLVM([^.]*)\..*$/$2/;
255 $short_name =~ tr/A-Z/a-z/;
256 $NAME_MAP{$short_name} = [$lib];
257 }
258 }
259
260 # Add virtual entries.
Reid Spencerb195d9d2006-03-23 23:21:29 +0000261 $NAME_MAP{'native'} = have_native_backend() ? [$ARCH] : [];
262 $NAME_MAP{'backend'} = have_native_backend() ? ['native'] : ['cbackend'];
263 $NAME_MAP{'engine'} = find_best_engine;
264 $NAME_MAP{'all'} = [name_map_entries]; # Must be last.
Reid Spencerf2722ca2006-03-22 15:59:55 +0000265}
266
Reid Spencerb195d9d2006-03-23 23:21:29 +0000267# Return true if we have a native backend to use.
268sub have_native_backend {
Reid Spencerf2722ca2006-03-22 15:59:55 +0000269 my %BUILT;
270 foreach my $target (@TARGETS_BUILT) { $BUILT{$target} = 1; }
Reid Spencerb195d9d2006-03-23 23:21:29 +0000271 return defined $NAME_MAP{$ARCH} && defined $BUILT{$ARCH};
272}
273
274# Find a working subclass of ExecutionEngine for this platform.
275sub find_best_engine {
276 if (have_native_backend && $TARGET_HAS_JIT) {
Reid Spencer1c070fc2006-03-24 01:10:39 +0000277 return ['jit', 'native'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000278 } else {
Reid Spencerb195d9d2006-03-23 23:21:29 +0000279 return ['interpreter'];
Reid Spencerf2722ca2006-03-22 15:59:55 +0000280 }
281}
282
283# Get all the human-friendly component names.
284sub name_map_entries {
285 load_dependencies;
286 return sort keys %NAME_MAP;
287}
288
289# Map human-readable names to internal library names.
290sub expand_names (@) {
291 my @names = @_;
292 my @result;
293 foreach my $name (@names) {
294 if (defined $LIB_TO_SET_MAP{$name}) {
295 # We've hit bottom: An actual library name.
296 push @result, $name;
297 } elsif (defined $NAME_MAP{$name}) {
298 # We've found a short name to expand.
299 push @result, expand_names(@{$NAME_MAP{$name}});
300 } else {
301 print STDERR "llvm-config: unknown component name: $name\n";
302 exit(1);
303 }
304 }
305 return @result;
306}
307
308# Given a list of internal library names, return all sets of libraries which
309# will need to be included by the linker (in no particular order).
310sub find_all_required_sets (@) {
311 my @libs = @_;
312 my %sets_added;
313 my @result;
314 find_all_required_sets_helper(\%sets_added, \@result, @libs);
315 return @result;
316}
317
318# Recursive closures are pretty broken in Perl, so we're going to separate
319# this function from find_all_required_sets and pass in the state we need
320# manually, as references. Yes, this is fairly unpleasant.
321sub find_all_required_sets_helper ($$@) {
322 my ($sets_added, $result, @libs) = @_;
323 foreach my $lib (@libs) {
324 my $set = $LIB_TO_SET_MAP{$lib};
325 next if defined $$sets_added{$set};
326 $$sets_added{$set} = 1;
327 push @$result, $set;
328 find_all_required_sets_helper($sets_added, $result, @{$SET_DEPS{$set}});
329 }
330}
331
332# Print a list of sets, with a label. Used for debugging.
333sub print_sets ($@) {
334 my ($label, @sets) = @_;
335 my @output;
336 foreach my $set (@sets) { push @output, join(',', @$set); }
337 print "$label: ", join(';', @output), "\n";
338}
339
340# Returns true if $lib is a key in $added.
341sub has_lib_been_added ($$) {
342 my ($added, $lib) = @_;
343 return defined $$added{$LIB_TO_SET_MAP{$lib}};
344}
345
346# Returns true if all the dependencies of $set appear in $added.
347sub have_all_deps_been_added ($$) {
348 my ($added, $set) = @_;
349 #print_sets(" Checking", $set);
350 #print_sets(" Wants", $SET_DEPS{$set});
351 foreach my $lib (@{$SET_DEPS{$set}}) {
352 return 0 unless has_lib_been_added($added, $lib);
353 }
354 return 1;
355}
356
357# Given a list of sets, topologically sort them using dependencies.
358sub topologically_sort_sets (@) {
359 my @sets = @_;
360 my %added;
361 my @result;
362 SCAN: while (@sets) { # We'll delete items from @sets as we go.
363 #print_sets("So far", reverse(@result));
364 #print_sets("Remaining", @sets);
365 for (my $i = 0; $i < @sets; ++$i) {
366 my $set = $sets[$i];
367 if (have_all_deps_been_added(\%added, $set)) {
368 push @result, $set;
369 $added{$set} = 1;
370 #print "Removing $i.\n";
371 splice(@sets, $i, 1);
372 next SCAN; # Restart our scan.
373 }
374 }
375 die "Can't find a library with no dependencies";
376 }
377 return reverse(@result);
378}
379
Reid Spencerf2722ca2006-03-22 15:59:55 +0000380# Our library dependency data will be added after the '__END__' token, and will
381# be read through the magic <DATA> filehandle.
382__END__