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