blob: 11ad9e506cdbb6d3c222ec85021cd256c0b472af [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001#!/usr/bin/perl
2
3# This script will take a number ($ENV{SCRIPT_INPUT_FILE_COUNT}) of static archive files
4# and pull them apart into object files. These object files will be placed in a directory
5# named the same as the archive itself without the extension. Each object file will then
6# get renamed to start with the archive name and a '-' character (for archive.a(object.o)
7# the object file would becomde archive-object.o. Then all object files are re-made into
8# a single static library. This can help avoid name collisions when different archive
9# files might contain object files with the same name.
10
11use strict;
12use File::Basename;
13use File::Glob ':glob';
14use List::Util qw[min max];
15
Greg Claytonf15996e2011-04-07 22:46:35 +000016our $llvm_srcroot = $ENV{SCRIPT_INPUT_FILE_0};
17our $llvm_dstroot = $ENV{SCRIPT_INPUT_FILE_1};
Chris Lattner24943d22010-06-08 16:52:24 +000018
Sean Callanan6c98ba72011-08-11 20:11:18 +000019our $llvm_clang_outfile = $ENV{SCRIPT_OUTPUT_FILE_0};
Chris Lattner24943d22010-06-08 16:52:24 +000020our ($llvm_clang_basename, $llvm_clang_dirname) = fileparse ($llvm_clang_outfile);
Chris Lattner24943d22010-06-08 16:52:24 +000021
22our $llvm_configuration = $ENV{LLVM_CONFIGURATION};
23
Sean Callanan06dc17f2012-09-24 22:25:51 +000024our $llvm_revision = "HEAD";
25our $clang_revision = "HEAD";
Sean Callanan047eea62011-08-18 18:18:33 +000026
Greg Clayton3e4238d2011-11-04 03:34:56 +000027our $SRCROOT = "$ENV{SRCROOT}";
Chris Lattner24943d22010-06-08 16:52:24 +000028our @archs = split (/\s+/, $ENV{ARCHS});
Greg Clayton3e4238d2011-11-04 03:34:56 +000029my $os_release = 11;
30
Jason Molendadaaa9d12012-04-02 08:56:42 +000031my $original_env_path = $ENV{PATH};
32
Greg Clayton3e4238d2011-11-04 03:34:56 +000033our %llvm_config_info = (
Sean Callanan4582fea2012-08-09 01:32:13 +000034 'Debug' => { configure_options => '--disable-optimized --disable-assertions --enable-libcpp', make_options => 'DEBUG_SYMBOLS=1'},
35 'Debug+Asserts' => { configure_options => '--disable-optimized --enable-assertions --enable-libcpp' , make_options => 'DEBUG_SYMBOLS=1'},
36 'Release' => { configure_options => '--enable-optimized --disable-assertions --enable-libcpp' , make_options => ''},
37 'Release+Debug' => { configure_options => '--enable-optimized --disable-assertions --enable-libcpp' , make_options => 'DEBUG_SYMBOLS=1'},
Sean Callanan54e58052012-09-24 23:21:18 +000038 'Release+Asserts' => { configure_options => '--enable-optimized --enable-assertions --enable-libcpp' , make_options => ''},
Greg Clayton3e4238d2011-11-04 03:34:56 +000039);
40
41our $llvm_config_href = undef;
42if (exists $llvm_config_info{"$llvm_configuration"})
43{
44 $llvm_config_href = $llvm_config_info{$llvm_configuration};
45}
46else
47{
48 die "Unsupported LLVM configuration: '$llvm_configuration'\n";
49}
Chris Lattner24943d22010-06-08 16:52:24 +000050
51our @archive_files = (
Sean Callanan47a5c4c2010-09-23 03:01:22 +000052 "$llvm_configuration/lib/libclang.a",
Chris Lattner24943d22010-06-08 16:52:24 +000053 "$llvm_configuration/lib/libclangAnalysis.a",
54 "$llvm_configuration/lib/libclangAST.a",
55 "$llvm_configuration/lib/libclangBasic.a",
56 "$llvm_configuration/lib/libclangCodeGen.a",
Sean Callanan6e12c7a2012-03-08 02:39:03 +000057 "$llvm_configuration/lib/libclangEdit.a",
Chris Lattner24943d22010-06-08 16:52:24 +000058 "$llvm_configuration/lib/libclangFrontend.a",
59 "$llvm_configuration/lib/libclangDriver.a",
Chris Lattner24943d22010-06-08 16:52:24 +000060 "$llvm_configuration/lib/libclangLex.a",
Chris Lattner24943d22010-06-08 16:52:24 +000061 "$llvm_configuration/lib/libclangParse.a",
62 "$llvm_configuration/lib/libclangSema.a",
Sean Callanan47a5c4c2010-09-23 03:01:22 +000063 "$llvm_configuration/lib/libclangSerialization.a",
Chris Lattner24943d22010-06-08 16:52:24 +000064 "$llvm_configuration/lib/libLLVMAnalysis.a",
65 "$llvm_configuration/lib/libLLVMArchive.a",
66 "$llvm_configuration/lib/libLLVMARMAsmParser.a",
67 "$llvm_configuration/lib/libLLVMARMAsmPrinter.a",
68 "$llvm_configuration/lib/libLLVMARMCodeGen.a",
Sean Callanan9b6898f2011-07-30 02:42:06 +000069 "$llvm_configuration/lib/libLLVMARMDesc.a",
Chris Lattner24943d22010-06-08 16:52:24 +000070 "$llvm_configuration/lib/libLLVMARMDisassembler.a",
71 "$llvm_configuration/lib/libLLVMARMInfo.a",
72 "$llvm_configuration/lib/libLLVMAsmParser.a",
73 "$llvm_configuration/lib/libLLVMAsmPrinter.a",
74 "$llvm_configuration/lib/libLLVMBitReader.a",
75 "$llvm_configuration/lib/libLLVMBitWriter.a",
76 "$llvm_configuration/lib/libLLVMCodeGen.a",
77 "$llvm_configuration/lib/libLLVMCore.a",
78 "$llvm_configuration/lib/libLLVMExecutionEngine.a",
79 "$llvm_configuration/lib/libLLVMInstCombine.a",
80 "$llvm_configuration/lib/libLLVMInstrumentation.a",
81 "$llvm_configuration/lib/libLLVMipa.a",
82 "$llvm_configuration/lib/libLLVMInterpreter.a",
83 "$llvm_configuration/lib/libLLVMipo.a",
84 "$llvm_configuration/lib/libLLVMJIT.a",
85 "$llvm_configuration/lib/libLLVMLinker.a",
86 "$llvm_configuration/lib/libLLVMMC.a",
87 "$llvm_configuration/lib/libLLVMMCParser.a",
Greg Claytonc9b60002010-07-21 16:57:26 +000088 "$llvm_configuration/lib/libLLVMMCDisassembler.a",
Sean Callananc0492742011-05-23 21:40:23 +000089 "$llvm_configuration/lib/libLLVMMCJIT.a",
90 "$llvm_configuration/lib/libLLVMObject.a",
91 "$llvm_configuration/lib/libLLVMRuntimeDyld.a",
Chris Lattner24943d22010-06-08 16:52:24 +000092 "$llvm_configuration/lib/libLLVMScalarOpts.a",
93 "$llvm_configuration/lib/libLLVMSelectionDAG.a",
94 "$llvm_configuration/lib/libLLVMSupport.a",
Chris Lattner24943d22010-06-08 16:52:24 +000095 "$llvm_configuration/lib/libLLVMTarget.a",
96 "$llvm_configuration/lib/libLLVMTransformUtils.a",
97 "$llvm_configuration/lib/libLLVMX86AsmParser.a",
98 "$llvm_configuration/lib/libLLVMX86AsmPrinter.a",
99 "$llvm_configuration/lib/libLLVMX86CodeGen.a",
Sean Callanan9b6898f2011-07-30 02:42:06 +0000100 "$llvm_configuration/lib/libLLVMX86Desc.a",
Chris Lattner24943d22010-06-08 16:52:24 +0000101 "$llvm_configuration/lib/libLLVMX86Disassembler.a",
102 "$llvm_configuration/lib/libLLVMX86Info.a",
Sean Callanan279584c2011-03-15 00:17:19 +0000103 "$llvm_configuration/lib/libLLVMX86Utils.a",
Chris Lattner24943d22010-06-08 16:52:24 +0000104);
105
Greg Clayton3e4238d2011-11-04 03:34:56 +0000106if (-e "$llvm_srcroot/lib")
Chris Lattner24943d22010-06-08 16:52:24 +0000107{
Greg Clayton3e4238d2011-11-04 03:34:56 +0000108 print "Using existing llvm sources in: '$llvm_srcroot'\n";
109 print "Using standard LLVM build directory:\n SRC = '$llvm_srcroot'\n DST = '$llvm_dstroot'\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000110}
Greg Clayton3e4238d2011-11-04 03:34:56 +0000111else
112{
113 print "Checking out llvm sources from revision $llvm_revision...\n";
114 do_command ("cd '$SRCROOT' && svn co --quiet --revision $llvm_revision http://llvm.org/svn/llvm-project/llvm/trunk llvm", "checking out llvm from repository", 1);
115 print "Checking out clang sources from revision $clang_revision...\n";
116 do_command ("cd '$llvm_srcroot/tools' && svn co --quiet --revision $clang_revision http://llvm.org/svn/llvm-project/cfe/trunk clang", "checking out clang from repository", 1);
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000117 print "Applying any local patches to LLVM/Clang...";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000118
119 my @llvm_patches = bsd_glob("$ENV{SRCROOT}/scripts/llvm.*.diff");
120
121 foreach my $patch (@llvm_patches)
122 {
123 do_command ("cd '$llvm_srcroot' && patch -p0 < $patch");
124 }
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000125
126 my @clang_patches = bsd_glob("$ENV{SRCROOT}/scripts/clang.*.diff");
127
128 foreach my $patch (@clang_patches)
129 {
130 do_command ("cd '$llvm_srcroot/tools/clang' && patch -p0 < $patch");
131 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000132}
Chris Lattner24943d22010-06-08 16:52:24 +0000133
134# If our output file already exists then we need not generate it again.
Greg Clayton64875582011-08-02 19:00:17 +0000135if (-e $llvm_clang_outfile)
Chris Lattner24943d22010-06-08 16:52:24 +0000136{
137 exit 0;
138}
139
140
141# Get our options
142
143our $debug = 1;
144
145sub parallel_guess
146{
147 my $cpus = `sysctl -n hw.availcpu`;
148 chomp ($cpus);
149 my $memsize = `sysctl -n hw.memsize`;
150 chomp ($memsize);
151 my $max_cpus_by_memory = int($memsize / (750 * 1024 * 1024));
152 return min($max_cpus_by_memory, $cpus);
153}
154sub build_llvm
155{
156 #my $extra_svn_options = $debug ? "" : "--quiet";
Chris Lattner24943d22010-06-08 16:52:24 +0000157 # Make the llvm build directory
158 my $arch_idx = 0;
159 foreach my $arch (@archs)
160 {
161 my $llvm_dstroot_arch = "${llvm_dstroot}/${arch}";
162
163 # if the arch destination root exists we have already built it
164 my $do_configure = 0;
165 my $do_make = 0;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000166 my $is_arm = $arch =~ /^arm/;
167
Chris Lattner24943d22010-06-08 16:52:24 +0000168 my $llvm_dstroot_arch_archive = "$llvm_dstroot_arch/$llvm_clang_basename";
169 print "LLVM architecture root for ${arch} exists at '$llvm_dstroot_arch'...";
170 if (-e $llvm_dstroot_arch)
171 {
172 print "YES\n";
173 $do_configure = !-e "$llvm_dstroot_arch/config.log";
174
175 # dstroot for llvm build exists, make sure all .a files are built
176 for my $llvm_lib (@archive_files)
177 {
178 if (!-e "$llvm_dstroot_arch/$llvm_lib")
179 {
180 print "missing archive: '$llvm_dstroot_arch/$llvm_lib'\n";
181 $do_make = 1;
182 }
183 }
184 if (!-e $llvm_dstroot_arch_archive)
185 {
186 $do_make = 1;
Greg Claytonc9b60002010-07-21 16:57:26 +0000187 }
188 else
189 {
190 print "LLVM architecture archive for ${arch} is '$llvm_dstroot_arch_archive'\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000191 }
192 }
193 else
194 {
195 print "NO\n";
196 do_command ("mkdir -p '$llvm_dstroot_arch'", "making llvm build directory '$llvm_dstroot_arch'", 1);
197 $do_configure = 1;
198 $do_make = 1;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000199
200 if ($is_arm)
Chris Lattner24943d22010-06-08 16:52:24 +0000201 {
Greg Clayton3e4238d2011-11-04 03:34:56 +0000202 my $llvm_dstroot_arch_bin = "${llvm_dstroot_arch}/bin";
203
204 if (!-d $llvm_dstroot_arch_bin)
205 {
206 do_command ("mkdir -p '$llvm_dstroot_arch_bin'", "making llvm build arch bin directory '$llvm_dstroot_arch_bin'", 1);
207 my @tools = ("ar", "nm", "ranlib", "strip", "lipo", "ld", "as");
208 my $script_mode = 0755;
209 my $prog;
210 for $prog (@tools)
211 {
212 chomp(my $actual_prog_path = `xcrun -sdk '$ENV{SDKROOT}' -find ${prog}`);
213 my $script_prog_path = "$llvm_dstroot_arch_bin/arm-apple-darwin${os_release}-${prog}";
214 open (SCRIPT, ">$script_prog_path") or die "Can't open $! for writing...\n";
215 print SCRIPT "#!/bin/sh\nexec '$actual_prog_path' \"\$\@\"\n";
216 close (SCRIPT);
217 chmod($script_mode, $script_prog_path);
218 }
219 # Tools that must have the "-arch" and "-sysroot" specified
220 my @arch_sysroot_tools = ("clang", "clang++", "gcc", "g++");
221 for $prog (@arch_sysroot_tools)
222 {
223 chomp(my $actual_prog_path = `xcrun -sdk '$ENV{SDKROOT}' -find ${prog}`);
224 my $script_prog_path = "$llvm_dstroot_arch_bin/arm-apple-darwin${os_release}-${prog}";
225 open (SCRIPT, ">$script_prog_path") or die "Can't open $! for writing...\n";
226 print SCRIPT "#!/bin/sh\nexec '$actual_prog_path' -arch ${arch} -isysroot '$ENV{SDKROOT}' \"\$\@\"\n";
227 close (SCRIPT);
228 chmod($script_mode, $script_prog_path);
229 }
Jason Molendadaaa9d12012-04-02 08:56:42 +0000230 my $new_path = "$original_env_path:$llvm_dstroot_arch_bin";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000231 print "Setting new environment PATH = '$new_path'\n";
232 $ENV{PATH} = $new_path;
233 }
Chris Lattner24943d22010-06-08 16:52:24 +0000234 }
235 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000236
Chris Lattner24943d22010-06-08 16:52:24 +0000237 if ($do_configure)
238 {
239 # Build llvm and clang
240 print "Configuring clang ($arch) in '$llvm_dstroot_arch'...\n";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000241 my $lldb_configuration_options = "--enable-targets=x86_64,arm $llvm_config_href->{configure_options}";
242
243 if ($is_arm)
244 {
245 $lldb_configuration_options .= " --host=arm-apple-darwin${os_release} --target=arm-apple-darwin${os_release} --build=i686-apple-darwin${os_release}";
246 }
247 else
248 {
249 $lldb_configuration_options .= " --build=$arch-apple-darwin${os_release}";
250 }
251 do_command ("cd '$llvm_dstroot_arch' && '$llvm_srcroot/configure' $lldb_configuration_options",
Chris Lattner24943d22010-06-08 16:52:24 +0000252 "configuring llvm build", 1);
253 }
254
255 if ($do_make)
256 {
257 # Build llvm and clang
258 my $num_cpus = parallel_guess();
259 print "Building clang using $num_cpus cpus ($arch)...\n";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000260 my $extra_make_flags = '';
261 if ($is_arm)
262 {
263 $extra_make_flags = "UNIVERSAL=1 UNIVERSAL_ARCH=${arch} UNIVERSAL_SDK_PATH='$ENV{SDKROOT}'";
264 }
265 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus clang-only VERBOSE=1 $llvm_config_href->{make_options} NO_RUNTIME_LIBS=1 PROJECT_NAME='llvm' $extra_make_flags", "making llvm and clang", 1);
266 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus tools-only VERBOSE=1 $llvm_config_href->{make_options} NO_RUNTIME_LIBS=1 PROJECT_NAME='llvm' $extra_make_flags EDIS_VERSION=1", "making libedis", 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000267 # Combine all .o files from a bunch of static libraries from llvm
268 # and clang into a single .a file.
269 create_single_llvm_arhive_for_arch ($llvm_dstroot_arch, 1);
270 }
271
Chris Lattner24943d22010-06-08 16:52:24 +0000272 ++$arch_idx;
273 }
Chris Lattner24943d22010-06-08 16:52:24 +0000274}
275
Chris Lattner24943d22010-06-08 16:52:24 +0000276#----------------------------------------------------------------------
277# quote the path if needed and realpath it if the -r option was
278# specified
279#----------------------------------------------------------------------
280sub finalize_path
281{
282 my $path = shift;
283 # Realpath all paths that don't start with "/"
284 $path =~ /^[^\/]/ and $path = abs_path($path);
285
286 # Quote the path if asked to, or if there are special shell characters
287 # in the path name
288 my $has_double_quotes = $path =~ /["]/;
289 my $has_single_quotes = $path =~ /[']/;
290 my $needs_quotes = $path =~ /[ \$\&\*'"]/;
291 if ($needs_quotes)
292 {
293 # escape and double quotes in the path
294 $has_double_quotes and $path =~ s/"/\\"/g;
295 $path = "\"$path\"";
296 }
297 return $path;
298}
299
300sub do_command
301{
302 my $cmd = shift;
303 my $description = @_ ? shift : "command";
304 my $die_on_fail = @_ ? shift : undef;
305 $debug and print "% $cmd\n";
306 system ($cmd);
307 if ($? == -1)
308 {
309 $debug and printf ("error: %s failed to execute: $!\n", $description);
310 $die_on_fail and $? and exit(1);
311 return $?;
312 }
313 elsif ($? & 127)
314 {
315 $debug and printf("error: %s child died with signal %d, %s coredump\n",
316 $description,
317 ($? & 127),
318 ($? & 128) ? 'with' : 'without');
319 $die_on_fail and $? and exit(1);
320 return $?;
321 }
322 else
323 {
324 my $exit = $? >> 8;
325 if ($exit)
326 {
327 $debug and printf("error: %s child exited with value %d\n", $description, $exit);
328 $die_on_fail and exit(1);
329 }
330 return $exit;
331 }
332}
333
334sub create_single_llvm_arhive_for_arch
335{
336 my $arch_dstroot = shift;
337 my $split_into_objects = shift;
338 my @object_dirs;
339 my $object_dir;
340 my $tmp_dir = $arch_dstroot;
341 my $arch_output_file = "$arch_dstroot/$llvm_clang_basename";
342 -e $arch_output_file and return;
343 my $files = "$arch_dstroot/files.txt";
344 open (FILES, ">$files") or die "Can't open $! for writing...\n";
345
346 for my $path (@archive_files)
347 {
348 my $archive_fullpath = finalize_path ("$arch_dstroot/$path");
349 if (-e $archive_fullpath)
350 {
351 if ($split_into_objects)
352 {
353 my ($archive_file, $archive_dir, $archive_ext) = fileparse($archive_fullpath, ('.a'));
354
355 $object_dir = "$tmp_dir/$archive_file";
356 push @object_dirs, $object_dir;
357
358 do_command ("cd '$tmp_dir'; mkdir '$archive_file'; cd '$archive_file'; ar -x $archive_fullpath");
359
360 my @objects = bsd_glob("$object_dir/*.o");
361
362 foreach my $object (@objects)
363 {
364 my ($o_file, $o_dir) = fileparse($object);
365 my $new_object = "$object_dir/${archive_file}-$o_file";
366 print FILES "$new_object\n";
367 do_command ("mv '$object' '$new_object'");
368 }
369 }
370 else
371 {
372 # just add the .a files into the file list
373 print FILES "$archive_fullpath\n";
374 }
375 }
Greg Clayton193e6d52010-07-13 22:26:45 +0000376 else
377 {
378 print "warning: archive doesn't exist: '$archive_fullpath'\n";
379 }
Chris Lattner24943d22010-06-08 16:52:24 +0000380 }
381 close (FILES);
382 do_command ("libtool -static -o '$arch_output_file' -filelist '$files'");
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000383 do_command ("ranlib '$arch_output_file'");
Chris Lattner24943d22010-06-08 16:52:24 +0000384
385 foreach $object_dir (@object_dirs)
386 {
387 do_command ("rm -rf '$object_dir'");
388 }
389 do_command ("rm -rf '$files'");
390}
391
392build_llvm();