blob: a534e01e0eb7ef2a8104b815621a935078334c20 [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Claytoneb0103f2011-04-07 22:46:35 +000016our $llvm_srcroot = $ENV{SCRIPT_INPUT_FILE_0};
17our $llvm_dstroot = $ENV{SCRIPT_INPUT_FILE_1};
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
Sean Callanan793d6332011-08-11 20:11:18 +000019our $llvm_clang_outfile = $ENV{SCRIPT_OUTPUT_FILE_0};
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020our ($llvm_clang_basename, $llvm_clang_dirname) = fileparse ($llvm_clang_outfile);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
22our $llvm_configuration = $ENV{LLVM_CONFIGURATION};
23
Sean Callanand5c17ed2011-11-15 02:11:17 +000024our $llvm_revision = "144573";
25our $clang_revision = "144573";
Sean Callanan6afe3902011-08-18 18:18:33 +000026
Greg Claytondce502e2011-11-04 03:34:56 +000027our $SRCROOT = "$ENV{SRCROOT}";
28our $llvm_dstroot_zip = "$SRCROOT/llvm.zip";
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029our @archs = split (/\s+/, $ENV{ARCHS});
Greg Claytondce502e2011-11-04 03:34:56 +000030my $os_release = 11;
31
32our %llvm_config_info = (
33 'Debug' => { configure_options => '--disable-optimized --disable-assertions', make_options => 'DEBUG_SYMBOLS=1'},
34 'Debug+Asserts' => { configure_options => '--disable-optimized --enable-assertions' , make_options => 'DEBUG_SYMBOLS=1'},
35 'Release' => { configure_options => '--enable-optimized --disable-assertions' , make_options => ''},
36 'Release+Debug' => { configure_options => '--enable-optimized --disable-assertions' , make_options => 'DEBUG_SYMBOLS=1'},
37);
38
39our $llvm_config_href = undef;
40if (exists $llvm_config_info{"$llvm_configuration"})
41{
42 $llvm_config_href = $llvm_config_info{$llvm_configuration};
43}
44else
45{
46 die "Unsupported LLVM configuration: '$llvm_configuration'\n";
47}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
49our @archive_files = (
Sean Callanane2ef6e32010-09-23 03:01:22 +000050 "$llvm_configuration/lib/libclang.a",
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 "$llvm_configuration/lib/libclangAnalysis.a",
52 "$llvm_configuration/lib/libclangAST.a",
53 "$llvm_configuration/lib/libclangBasic.a",
54 "$llvm_configuration/lib/libclangCodeGen.a",
55 "$llvm_configuration/lib/libclangFrontend.a",
56 "$llvm_configuration/lib/libclangDriver.a",
57 "$llvm_configuration/lib/libclangIndex.a",
58 "$llvm_configuration/lib/libclangLex.a",
59 "$llvm_configuration/lib/libclangRewrite.a",
60 "$llvm_configuration/lib/libclangParse.a",
61 "$llvm_configuration/lib/libclangSema.a",
Sean Callanane2ef6e32010-09-23 03:01:22 +000062 "$llvm_configuration/lib/libclangSerialization.a",
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063 "$llvm_configuration/lib/libEnhancedDisassembly.a",
64 "$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 Callanancc427fa2011-07-30 02:42:06 +000069 "$llvm_configuration/lib/libLLVMARMDesc.a",
Chris Lattner30fdc8d2010-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 Claytond1daf002010-07-21 16:57:26 +000088 "$llvm_configuration/lib/libLLVMMCDisassembler.a",
Sean Callanan79763a42011-05-23 21:40:23 +000089 "$llvm_configuration/lib/libLLVMMCJIT.a",
90 "$llvm_configuration/lib/libLLVMObject.a",
91 "$llvm_configuration/lib/libLLVMRuntimeDyld.a",
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 "$llvm_configuration/lib/libLLVMScalarOpts.a",
93 "$llvm_configuration/lib/libLLVMSelectionDAG.a",
94 "$llvm_configuration/lib/libLLVMSupport.a",
Chris Lattner30fdc8d2010-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 Callanancc427fa2011-07-30 02:42:06 +0000100 "$llvm_configuration/lib/libLLVMX86Desc.a",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 "$llvm_configuration/lib/libLLVMX86Disassembler.a",
102 "$llvm_configuration/lib/libLLVMX86Info.a",
Sean Callananfb0b7582011-03-15 00:17:19 +0000103 "$llvm_configuration/lib/libLLVMX86Utils.a",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104);
105
Greg Claytondce502e2011-11-04 03:34:56 +0000106if (-e "$llvm_srcroot/lib")
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107{
Greg Claytondce502e2011-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 Lattner30fdc8d2010-06-08 16:52:24 +0000110}
Greg Claytondce502e2011-11-04 03:34:56 +0000111elsif (-e $llvm_dstroot_zip)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112{
113 # Check for an old llvm source install (not the minimal zip based
114 # install by looking for a .svn file in the llvm directory
Greg Claytondce502e2011-11-04 03:34:56 +0000115 chomp(my $llvm_zip_md5 = `md5 -q '$llvm_dstroot_zip'`);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 my $llvm_zip_md5_file = "$ENV{SRCROOT}/llvm/$llvm_zip_md5";
117 if (!-e "$llvm_zip_md5_file")
118 {
Greg Claytondce502e2011-11-04 03:34:56 +0000119 print "Updating LLVM to use checkpoint from: '$llvm_dstroot_zip'...\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120 if (-d "$ENV{SRCROOT}/llvm")
121 {
122 do_command ("cd '$ENV{SRCROOT}' && rm -rf llvm", "removing old llvm repository", 1);
123 }
124 do_command ("cd '$ENV{SRCROOT}' && unzip -q llvm.zip && touch '$llvm_zip_md5_file'", "expanding llvm.zip", 1);
Greg Claytondce502e2011-11-04 03:34:56 +0000125
126 }
127 my $arch_idx = 0;
Sean Callananbfb237bc2011-11-04 22:46:46 +0000128
129 if (!-d "${llvm_dstroot}")
130 {
131 do_command ("mkdir -p '${llvm_dstroot}'", "Creating directory '${llvm_dstroot}'", 1);
132 }
133
Greg Claytondce502e2011-11-04 03:34:56 +0000134 foreach my $arch (@archs)
135 {
136 my $llvm_dstroot_arch = "${llvm_dstroot}/${arch}";
137 # Check for our symlink to our .a file
138 if (!-l "$llvm_dstroot_arch/$llvm_clang_basename")
139 {
140 # Symlink doesn't exist, make sure it isn't a normal file
141 if (-e "$llvm_dstroot_arch/$llvm_clang_basename")
142 {
143 # the .a file is a normal file which means it can't be from the
144 # zip file, we must remove the previous arch directory
145 do_command ("rm -rf '$llvm_dstroot_arch'", "Removing old '$llvm_dstroot_arch' directory", 1);
Sean Callananbfb237bc2011-11-04 22:46:46 +0000146 }
Greg Claytondce502e2011-11-04 03:34:56 +0000147 # Create a symlink to the .a file from the zip file
Sean Callananbfb237bc2011-11-04 22:46:46 +0000148 do_command ("cd '$llvm_dstroot' ; ln -s $ENV{SRCROOT}/llvm/$arch", "making llvm archive symlink", 1);
Greg Claytondce502e2011-11-04 03:34:56 +0000149 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 exit 0;
152}
Greg Claytondce502e2011-11-04 03:34:56 +0000153else
154{
155 print "Checking out llvm sources from revision $llvm_revision...\n";
156 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);
157 print "Checking out clang sources from revision $clang_revision...\n";
158 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 Callanan00f43622011-11-18 03:28:09 +0000159 print "Applying any local patches to LLVM/Clang...";
Greg Claytondce502e2011-11-04 03:34:56 +0000160
161 my @llvm_patches = bsd_glob("$ENV{SRCROOT}/scripts/llvm.*.diff");
162
163 foreach my $patch (@llvm_patches)
164 {
165 do_command ("cd '$llvm_srcroot' && patch -p0 < $patch");
166 }
Sean Callanan00f43622011-11-18 03:28:09 +0000167
168 my @clang_patches = bsd_glob("$ENV{SRCROOT}/scripts/clang.*.diff");
169
170 foreach my $patch (@clang_patches)
171 {
172 do_command ("cd '$llvm_srcroot/tools/clang' && patch -p0 < $patch");
173 }
Greg Claytondce502e2011-11-04 03:34:56 +0000174
175 print "Removing the llvm/test and llvm/tools/clang/test directories...\n";
176 do_command ("cd '$llvm_srcroot' && rm -rf test && rm -rf tools/clang/test ", "removing test directories", 1);
177}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178
179# If our output file already exists then we need not generate it again.
Greg Clayton819122d2011-08-02 19:00:17 +0000180if (-e $llvm_clang_outfile)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181{
182 exit 0;
183}
184
185
186# Get our options
187
188our $debug = 1;
189
190sub parallel_guess
191{
192 my $cpus = `sysctl -n hw.availcpu`;
193 chomp ($cpus);
194 my $memsize = `sysctl -n hw.memsize`;
195 chomp ($memsize);
196 my $max_cpus_by_memory = int($memsize / (750 * 1024 * 1024));
197 return min($max_cpus_by_memory, $cpus);
198}
199sub build_llvm
200{
201 #my $extra_svn_options = $debug ? "" : "--quiet";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 # Make the llvm build directory
203 my $arch_idx = 0;
204 foreach my $arch (@archs)
205 {
206 my $llvm_dstroot_arch = "${llvm_dstroot}/${arch}";
207
208 # if the arch destination root exists we have already built it
209 my $do_configure = 0;
210 my $do_make = 0;
Greg Claytondce502e2011-11-04 03:34:56 +0000211 my $is_arm = $arch =~ /^arm/;
212
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213 my $llvm_dstroot_arch_archive = "$llvm_dstroot_arch/$llvm_clang_basename";
214 print "LLVM architecture root for ${arch} exists at '$llvm_dstroot_arch'...";
215 if (-e $llvm_dstroot_arch)
216 {
217 print "YES\n";
218 $do_configure = !-e "$llvm_dstroot_arch/config.log";
219
220 # dstroot for llvm build exists, make sure all .a files are built
221 for my $llvm_lib (@archive_files)
222 {
223 if (!-e "$llvm_dstroot_arch/$llvm_lib")
224 {
225 print "missing archive: '$llvm_dstroot_arch/$llvm_lib'\n";
226 $do_make = 1;
227 }
228 }
229 if (!-e $llvm_dstroot_arch_archive)
230 {
231 $do_make = 1;
Greg Claytond1daf002010-07-21 16:57:26 +0000232 }
233 else
234 {
235 print "LLVM architecture archive for ${arch} is '$llvm_dstroot_arch_archive'\n";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 }
237 }
238 else
239 {
240 print "NO\n";
241 do_command ("mkdir -p '$llvm_dstroot_arch'", "making llvm build directory '$llvm_dstroot_arch'", 1);
242 $do_configure = 1;
243 $do_make = 1;
Greg Claytondce502e2011-11-04 03:34:56 +0000244
245 if ($is_arm)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 {
Greg Claytondce502e2011-11-04 03:34:56 +0000247 my $llvm_dstroot_arch_bin = "${llvm_dstroot_arch}/bin";
248
249 if (!-d $llvm_dstroot_arch_bin)
250 {
251 do_command ("mkdir -p '$llvm_dstroot_arch_bin'", "making llvm build arch bin directory '$llvm_dstroot_arch_bin'", 1);
252 my @tools = ("ar", "nm", "ranlib", "strip", "lipo", "ld", "as");
253 my $script_mode = 0755;
254 my $prog;
255 for $prog (@tools)
256 {
257 chomp(my $actual_prog_path = `xcrun -sdk '$ENV{SDKROOT}' -find ${prog}`);
258 my $script_prog_path = "$llvm_dstroot_arch_bin/arm-apple-darwin${os_release}-${prog}";
259 open (SCRIPT, ">$script_prog_path") or die "Can't open $! for writing...\n";
260 print SCRIPT "#!/bin/sh\nexec '$actual_prog_path' \"\$\@\"\n";
261 close (SCRIPT);
262 chmod($script_mode, $script_prog_path);
263 }
264 # Tools that must have the "-arch" and "-sysroot" specified
265 my @arch_sysroot_tools = ("clang", "clang++", "gcc", "g++");
266 for $prog (@arch_sysroot_tools)
267 {
268 chomp(my $actual_prog_path = `xcrun -sdk '$ENV{SDKROOT}' -find ${prog}`);
269 my $script_prog_path = "$llvm_dstroot_arch_bin/arm-apple-darwin${os_release}-${prog}";
270 open (SCRIPT, ">$script_prog_path") or die "Can't open $! for writing...\n";
271 print SCRIPT "#!/bin/sh\nexec '$actual_prog_path' -arch ${arch} -isysroot '$ENV{SDKROOT}' \"\$\@\"\n";
272 close (SCRIPT);
273 chmod($script_mode, $script_prog_path);
274 }
275 my $new_path = "$ENV{PATH}:$llvm_dstroot_arch_bin";
276 print "Setting new environment PATH = '$new_path'\n";
277 $ENV{PATH} = $new_path;
278 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 }
280 }
Greg Claytondce502e2011-11-04 03:34:56 +0000281
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 if ($do_configure)
283 {
284 # Build llvm and clang
285 print "Configuring clang ($arch) in '$llvm_dstroot_arch'...\n";
Greg Claytondce502e2011-11-04 03:34:56 +0000286 my $lldb_configuration_options = "--enable-targets=x86_64,arm $llvm_config_href->{configure_options}";
287
288 if ($is_arm)
289 {
290 $lldb_configuration_options .= " --host=arm-apple-darwin${os_release} --target=arm-apple-darwin${os_release} --build=i686-apple-darwin${os_release}";
291 }
292 else
293 {
294 $lldb_configuration_options .= " --build=$arch-apple-darwin${os_release}";
295 }
296 do_command ("cd '$llvm_dstroot_arch' && '$llvm_srcroot/configure' $lldb_configuration_options",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297 "configuring llvm build", 1);
298 }
299
300 if ($do_make)
301 {
302 # Build llvm and clang
303 my $num_cpus = parallel_guess();
304 print "Building clang using $num_cpus cpus ($arch)...\n";
Greg Claytondce502e2011-11-04 03:34:56 +0000305 my $extra_make_flags = '';
306 if ($is_arm)
307 {
308 $extra_make_flags = "UNIVERSAL=1 UNIVERSAL_ARCH=${arch} UNIVERSAL_SDK_PATH='$ENV{SDKROOT}'";
309 }
310 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);
311 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 Lattner30fdc8d2010-06-08 16:52:24 +0000312 # Combine all .o files from a bunch of static libraries from llvm
313 # and clang into a single .a file.
314 create_single_llvm_arhive_for_arch ($llvm_dstroot_arch, 1);
315 }
316
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 ++$arch_idx;
318 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319}
320
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321#----------------------------------------------------------------------
322# quote the path if needed and realpath it if the -r option was
323# specified
324#----------------------------------------------------------------------
325sub finalize_path
326{
327 my $path = shift;
328 # Realpath all paths that don't start with "/"
329 $path =~ /^[^\/]/ and $path = abs_path($path);
330
331 # Quote the path if asked to, or if there are special shell characters
332 # in the path name
333 my $has_double_quotes = $path =~ /["]/;
334 my $has_single_quotes = $path =~ /[']/;
335 my $needs_quotes = $path =~ /[ \$\&\*'"]/;
336 if ($needs_quotes)
337 {
338 # escape and double quotes in the path
339 $has_double_quotes and $path =~ s/"/\\"/g;
340 $path = "\"$path\"";
341 }
342 return $path;
343}
344
345sub do_command
346{
347 my $cmd = shift;
348 my $description = @_ ? shift : "command";
349 my $die_on_fail = @_ ? shift : undef;
350 $debug and print "% $cmd\n";
351 system ($cmd);
352 if ($? == -1)
353 {
354 $debug and printf ("error: %s failed to execute: $!\n", $description);
355 $die_on_fail and $? and exit(1);
356 return $?;
357 }
358 elsif ($? & 127)
359 {
360 $debug and printf("error: %s child died with signal %d, %s coredump\n",
361 $description,
362 ($? & 127),
363 ($? & 128) ? 'with' : 'without');
364 $die_on_fail and $? and exit(1);
365 return $?;
366 }
367 else
368 {
369 my $exit = $? >> 8;
370 if ($exit)
371 {
372 $debug and printf("error: %s child exited with value %d\n", $description, $exit);
373 $die_on_fail and exit(1);
374 }
375 return $exit;
376 }
377}
378
379sub create_single_llvm_arhive_for_arch
380{
381 my $arch_dstroot = shift;
382 my $split_into_objects = shift;
383 my @object_dirs;
384 my $object_dir;
385 my $tmp_dir = $arch_dstroot;
386 my $arch_output_file = "$arch_dstroot/$llvm_clang_basename";
387 -e $arch_output_file and return;
388 my $files = "$arch_dstroot/files.txt";
389 open (FILES, ">$files") or die "Can't open $! for writing...\n";
390
391 for my $path (@archive_files)
392 {
393 my $archive_fullpath = finalize_path ("$arch_dstroot/$path");
394 if (-e $archive_fullpath)
395 {
396 if ($split_into_objects)
397 {
398 my ($archive_file, $archive_dir, $archive_ext) = fileparse($archive_fullpath, ('.a'));
399
400 $object_dir = "$tmp_dir/$archive_file";
401 push @object_dirs, $object_dir;
402
403 do_command ("cd '$tmp_dir'; mkdir '$archive_file'; cd '$archive_file'; ar -x $archive_fullpath");
404
405 my @objects = bsd_glob("$object_dir/*.o");
406
407 foreach my $object (@objects)
408 {
409 my ($o_file, $o_dir) = fileparse($object);
410 my $new_object = "$object_dir/${archive_file}-$o_file";
411 print FILES "$new_object\n";
412 do_command ("mv '$object' '$new_object'");
413 }
414 }
415 else
416 {
417 # just add the .a files into the file list
418 print FILES "$archive_fullpath\n";
419 }
420 }
Greg Claytonb6f75df2010-07-13 22:26:45 +0000421 else
422 {
423 print "warning: archive doesn't exist: '$archive_fullpath'\n";
424 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 }
426 close (FILES);
427 do_command ("libtool -static -o '$arch_output_file' -filelist '$files'");
Sean Callanane2ef6e32010-09-23 03:01:22 +0000428 do_command ("ranlib '$arch_output_file'");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429
430 foreach $object_dir (@object_dirs)
431 {
432 do_command ("rm -rf '$object_dir'");
433 }
434 do_command ("rm -rf '$files'");
435}
436
437build_llvm();