blob: 4fd457b13be1ff5b47c226f1a439b4793e0fcf7e [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 Callanan6e12c7a2012-03-08 02:39:03 +000024our $llvm_revision = "152265";
25our $clang_revision = "152265";
Sean Callanan047eea62011-08-18 18:18:33 +000026
Greg Clayton3e4238d2011-11-04 03:34:56 +000027our $SRCROOT = "$ENV{SRCROOT}";
28our $llvm_dstroot_zip = "$SRCROOT/llvm.zip";
Chris Lattner24943d22010-06-08 16:52:24 +000029our @archs = split (/\s+/, $ENV{ARCHS});
Greg Clayton3e4238d2011-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 Lattner24943d22010-06-08 16:52:24 +000048
49our @archive_files = (
Sean Callanan47a5c4c2010-09-23 03:01:22 +000050 "$llvm_configuration/lib/libclang.a",
Chris Lattner24943d22010-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",
Sean Callanan6e12c7a2012-03-08 02:39:03 +000055 "$llvm_configuration/lib/libclangEdit.a",
Chris Lattner24943d22010-06-08 16:52:24 +000056 "$llvm_configuration/lib/libclangFrontend.a",
57 "$llvm_configuration/lib/libclangDriver.a",
58 "$llvm_configuration/lib/libclangIndex.a",
59 "$llvm_configuration/lib/libclangLex.a",
60 "$llvm_configuration/lib/libclangRewrite.a",
61 "$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/libEnhancedDisassembly.a",
65 "$llvm_configuration/lib/libLLVMAnalysis.a",
66 "$llvm_configuration/lib/libLLVMArchive.a",
67 "$llvm_configuration/lib/libLLVMARMAsmParser.a",
68 "$llvm_configuration/lib/libLLVMARMAsmPrinter.a",
69 "$llvm_configuration/lib/libLLVMARMCodeGen.a",
Sean Callanan9b6898f2011-07-30 02:42:06 +000070 "$llvm_configuration/lib/libLLVMARMDesc.a",
Chris Lattner24943d22010-06-08 16:52:24 +000071 "$llvm_configuration/lib/libLLVMARMDisassembler.a",
72 "$llvm_configuration/lib/libLLVMARMInfo.a",
73 "$llvm_configuration/lib/libLLVMAsmParser.a",
74 "$llvm_configuration/lib/libLLVMAsmPrinter.a",
75 "$llvm_configuration/lib/libLLVMBitReader.a",
76 "$llvm_configuration/lib/libLLVMBitWriter.a",
77 "$llvm_configuration/lib/libLLVMCodeGen.a",
78 "$llvm_configuration/lib/libLLVMCore.a",
79 "$llvm_configuration/lib/libLLVMExecutionEngine.a",
80 "$llvm_configuration/lib/libLLVMInstCombine.a",
81 "$llvm_configuration/lib/libLLVMInstrumentation.a",
82 "$llvm_configuration/lib/libLLVMipa.a",
83 "$llvm_configuration/lib/libLLVMInterpreter.a",
84 "$llvm_configuration/lib/libLLVMipo.a",
85 "$llvm_configuration/lib/libLLVMJIT.a",
86 "$llvm_configuration/lib/libLLVMLinker.a",
87 "$llvm_configuration/lib/libLLVMMC.a",
88 "$llvm_configuration/lib/libLLVMMCParser.a",
Greg Claytonc9b60002010-07-21 16:57:26 +000089 "$llvm_configuration/lib/libLLVMMCDisassembler.a",
Sean Callananc0492742011-05-23 21:40:23 +000090 "$llvm_configuration/lib/libLLVMMCJIT.a",
91 "$llvm_configuration/lib/libLLVMObject.a",
92 "$llvm_configuration/lib/libLLVMRuntimeDyld.a",
Chris Lattner24943d22010-06-08 16:52:24 +000093 "$llvm_configuration/lib/libLLVMScalarOpts.a",
94 "$llvm_configuration/lib/libLLVMSelectionDAG.a",
95 "$llvm_configuration/lib/libLLVMSupport.a",
Chris Lattner24943d22010-06-08 16:52:24 +000096 "$llvm_configuration/lib/libLLVMTarget.a",
97 "$llvm_configuration/lib/libLLVMTransformUtils.a",
98 "$llvm_configuration/lib/libLLVMX86AsmParser.a",
99 "$llvm_configuration/lib/libLLVMX86AsmPrinter.a",
100 "$llvm_configuration/lib/libLLVMX86CodeGen.a",
Sean Callanan9b6898f2011-07-30 02:42:06 +0000101 "$llvm_configuration/lib/libLLVMX86Desc.a",
Chris Lattner24943d22010-06-08 16:52:24 +0000102 "$llvm_configuration/lib/libLLVMX86Disassembler.a",
103 "$llvm_configuration/lib/libLLVMX86Info.a",
Sean Callanan279584c2011-03-15 00:17:19 +0000104 "$llvm_configuration/lib/libLLVMX86Utils.a",
Chris Lattner24943d22010-06-08 16:52:24 +0000105);
106
Greg Clayton3e4238d2011-11-04 03:34:56 +0000107if (-e "$llvm_srcroot/lib")
Chris Lattner24943d22010-06-08 16:52:24 +0000108{
Greg Clayton3e4238d2011-11-04 03:34:56 +0000109 print "Using existing llvm sources in: '$llvm_srcroot'\n";
110 print "Using standard LLVM build directory:\n SRC = '$llvm_srcroot'\n DST = '$llvm_dstroot'\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000111}
Greg Clayton3e4238d2011-11-04 03:34:56 +0000112elsif (-e $llvm_dstroot_zip)
Chris Lattner24943d22010-06-08 16:52:24 +0000113{
114 # Check for an old llvm source install (not the minimal zip based
115 # install by looking for a .svn file in the llvm directory
Greg Clayton3e4238d2011-11-04 03:34:56 +0000116 chomp(my $llvm_zip_md5 = `md5 -q '$llvm_dstroot_zip'`);
Chris Lattner24943d22010-06-08 16:52:24 +0000117 my $llvm_zip_md5_file = "$ENV{SRCROOT}/llvm/$llvm_zip_md5";
118 if (!-e "$llvm_zip_md5_file")
119 {
Greg Clayton3e4238d2011-11-04 03:34:56 +0000120 print "Updating LLVM to use checkpoint from: '$llvm_dstroot_zip'...\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000121 if (-d "$ENV{SRCROOT}/llvm")
122 {
123 do_command ("cd '$ENV{SRCROOT}' && rm -rf llvm", "removing old llvm repository", 1);
124 }
125 do_command ("cd '$ENV{SRCROOT}' && unzip -q llvm.zip && touch '$llvm_zip_md5_file'", "expanding llvm.zip", 1);
Greg Clayton3e4238d2011-11-04 03:34:56 +0000126
127 }
128 my $arch_idx = 0;
Sean Callanan7537dce2011-11-04 22:46:46 +0000129
130 if (!-d "${llvm_dstroot}")
131 {
132 do_command ("mkdir -p '${llvm_dstroot}'", "Creating directory '${llvm_dstroot}'", 1);
133 }
134
Greg Clayton3e4238d2011-11-04 03:34:56 +0000135 foreach my $arch (@archs)
136 {
137 my $llvm_dstroot_arch = "${llvm_dstroot}/${arch}";
138 # Check for our symlink to our .a file
139 if (!-l "$llvm_dstroot_arch/$llvm_clang_basename")
140 {
141 # Symlink doesn't exist, make sure it isn't a normal file
142 if (-e "$llvm_dstroot_arch/$llvm_clang_basename")
143 {
144 # the .a file is a normal file which means it can't be from the
145 # zip file, we must remove the previous arch directory
146 do_command ("rm -rf '$llvm_dstroot_arch'", "Removing old '$llvm_dstroot_arch' directory", 1);
Sean Callanan7537dce2011-11-04 22:46:46 +0000147 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000148 # Create a symlink to the .a file from the zip file
Sean Callanan7537dce2011-11-04 22:46:46 +0000149 do_command ("cd '$llvm_dstroot' ; ln -s $ENV{SRCROOT}/llvm/$arch", "making llvm archive symlink", 1);
Greg Clayton3e4238d2011-11-04 03:34:56 +0000150 }
Chris Lattner24943d22010-06-08 16:52:24 +0000151 }
Chris Lattner24943d22010-06-08 16:52:24 +0000152 exit 0;
153}
Greg Clayton3e4238d2011-11-04 03:34:56 +0000154else
155{
156 print "Checking out llvm sources from revision $llvm_revision...\n";
157 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);
158 print "Checking out clang sources from revision $clang_revision...\n";
159 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 +0000160 print "Applying any local patches to LLVM/Clang...";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000161
162 my @llvm_patches = bsd_glob("$ENV{SRCROOT}/scripts/llvm.*.diff");
163
164 foreach my $patch (@llvm_patches)
165 {
166 do_command ("cd '$llvm_srcroot' && patch -p0 < $patch");
167 }
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000168
169 my @clang_patches = bsd_glob("$ENV{SRCROOT}/scripts/clang.*.diff");
170
171 foreach my $patch (@clang_patches)
172 {
173 do_command ("cd '$llvm_srcroot/tools/clang' && patch -p0 < $patch");
174 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000175
176 print "Removing the llvm/test and llvm/tools/clang/test directories...\n";
177 do_command ("cd '$llvm_srcroot' && rm -rf test && rm -rf tools/clang/test ", "removing test directories", 1);
178}
Chris Lattner24943d22010-06-08 16:52:24 +0000179
180# If our output file already exists then we need not generate it again.
Greg Clayton64875582011-08-02 19:00:17 +0000181if (-e $llvm_clang_outfile)
Chris Lattner24943d22010-06-08 16:52:24 +0000182{
183 exit 0;
184}
185
186
187# Get our options
188
189our $debug = 1;
190
191sub parallel_guess
192{
193 my $cpus = `sysctl -n hw.availcpu`;
194 chomp ($cpus);
195 my $memsize = `sysctl -n hw.memsize`;
196 chomp ($memsize);
197 my $max_cpus_by_memory = int($memsize / (750 * 1024 * 1024));
198 return min($max_cpus_by_memory, $cpus);
199}
200sub build_llvm
201{
202 #my $extra_svn_options = $debug ? "" : "--quiet";
Chris Lattner24943d22010-06-08 16:52:24 +0000203 # Make the llvm build directory
204 my $arch_idx = 0;
205 foreach my $arch (@archs)
206 {
207 my $llvm_dstroot_arch = "${llvm_dstroot}/${arch}";
208
209 # if the arch destination root exists we have already built it
210 my $do_configure = 0;
211 my $do_make = 0;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000212 my $is_arm = $arch =~ /^arm/;
213
Chris Lattner24943d22010-06-08 16:52:24 +0000214 my $llvm_dstroot_arch_archive = "$llvm_dstroot_arch/$llvm_clang_basename";
215 print "LLVM architecture root for ${arch} exists at '$llvm_dstroot_arch'...";
216 if (-e $llvm_dstroot_arch)
217 {
218 print "YES\n";
219 $do_configure = !-e "$llvm_dstroot_arch/config.log";
220
221 # dstroot for llvm build exists, make sure all .a files are built
222 for my $llvm_lib (@archive_files)
223 {
224 if (!-e "$llvm_dstroot_arch/$llvm_lib")
225 {
226 print "missing archive: '$llvm_dstroot_arch/$llvm_lib'\n";
227 $do_make = 1;
228 }
229 }
230 if (!-e $llvm_dstroot_arch_archive)
231 {
232 $do_make = 1;
Greg Claytonc9b60002010-07-21 16:57:26 +0000233 }
234 else
235 {
236 print "LLVM architecture archive for ${arch} is '$llvm_dstroot_arch_archive'\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000237 }
238 }
239 else
240 {
241 print "NO\n";
242 do_command ("mkdir -p '$llvm_dstroot_arch'", "making llvm build directory '$llvm_dstroot_arch'", 1);
243 $do_configure = 1;
244 $do_make = 1;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000245
246 if ($is_arm)
Chris Lattner24943d22010-06-08 16:52:24 +0000247 {
Greg Clayton3e4238d2011-11-04 03:34:56 +0000248 my $llvm_dstroot_arch_bin = "${llvm_dstroot_arch}/bin";
249
250 if (!-d $llvm_dstroot_arch_bin)
251 {
252 do_command ("mkdir -p '$llvm_dstroot_arch_bin'", "making llvm build arch bin directory '$llvm_dstroot_arch_bin'", 1);
253 my @tools = ("ar", "nm", "ranlib", "strip", "lipo", "ld", "as");
254 my $script_mode = 0755;
255 my $prog;
256 for $prog (@tools)
257 {
258 chomp(my $actual_prog_path = `xcrun -sdk '$ENV{SDKROOT}' -find ${prog}`);
259 my $script_prog_path = "$llvm_dstroot_arch_bin/arm-apple-darwin${os_release}-${prog}";
260 open (SCRIPT, ">$script_prog_path") or die "Can't open $! for writing...\n";
261 print SCRIPT "#!/bin/sh\nexec '$actual_prog_path' \"\$\@\"\n";
262 close (SCRIPT);
263 chmod($script_mode, $script_prog_path);
264 }
265 # Tools that must have the "-arch" and "-sysroot" specified
266 my @arch_sysroot_tools = ("clang", "clang++", "gcc", "g++");
267 for $prog (@arch_sysroot_tools)
268 {
269 chomp(my $actual_prog_path = `xcrun -sdk '$ENV{SDKROOT}' -find ${prog}`);
270 my $script_prog_path = "$llvm_dstroot_arch_bin/arm-apple-darwin${os_release}-${prog}";
271 open (SCRIPT, ">$script_prog_path") or die "Can't open $! for writing...\n";
272 print SCRIPT "#!/bin/sh\nexec '$actual_prog_path' -arch ${arch} -isysroot '$ENV{SDKROOT}' \"\$\@\"\n";
273 close (SCRIPT);
274 chmod($script_mode, $script_prog_path);
275 }
276 my $new_path = "$ENV{PATH}:$llvm_dstroot_arch_bin";
277 print "Setting new environment PATH = '$new_path'\n";
278 $ENV{PATH} = $new_path;
279 }
Chris Lattner24943d22010-06-08 16:52:24 +0000280 }
281 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000282
Chris Lattner24943d22010-06-08 16:52:24 +0000283 if ($do_configure)
284 {
285 # Build llvm and clang
286 print "Configuring clang ($arch) in '$llvm_dstroot_arch'...\n";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000287 my $lldb_configuration_options = "--enable-targets=x86_64,arm $llvm_config_href->{configure_options}";
288
289 if ($is_arm)
290 {
291 $lldb_configuration_options .= " --host=arm-apple-darwin${os_release} --target=arm-apple-darwin${os_release} --build=i686-apple-darwin${os_release}";
292 }
293 else
294 {
295 $lldb_configuration_options .= " --build=$arch-apple-darwin${os_release}";
296 }
297 do_command ("cd '$llvm_dstroot_arch' && '$llvm_srcroot/configure' $lldb_configuration_options",
Chris Lattner24943d22010-06-08 16:52:24 +0000298 "configuring llvm build", 1);
299 }
300
301 if ($do_make)
302 {
303 # Build llvm and clang
304 my $num_cpus = parallel_guess();
305 print "Building clang using $num_cpus cpus ($arch)...\n";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000306 my $extra_make_flags = '';
307 if ($is_arm)
308 {
309 $extra_make_flags = "UNIVERSAL=1 UNIVERSAL_ARCH=${arch} UNIVERSAL_SDK_PATH='$ENV{SDKROOT}'";
310 }
311 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);
312 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 +0000313 # Combine all .o files from a bunch of static libraries from llvm
314 # and clang into a single .a file.
315 create_single_llvm_arhive_for_arch ($llvm_dstroot_arch, 1);
316 }
317
Chris Lattner24943d22010-06-08 16:52:24 +0000318 ++$arch_idx;
319 }
Chris Lattner24943d22010-06-08 16:52:24 +0000320}
321
Chris Lattner24943d22010-06-08 16:52:24 +0000322#----------------------------------------------------------------------
323# quote the path if needed and realpath it if the -r option was
324# specified
325#----------------------------------------------------------------------
326sub finalize_path
327{
328 my $path = shift;
329 # Realpath all paths that don't start with "/"
330 $path =~ /^[^\/]/ and $path = abs_path($path);
331
332 # Quote the path if asked to, or if there are special shell characters
333 # in the path name
334 my $has_double_quotes = $path =~ /["]/;
335 my $has_single_quotes = $path =~ /[']/;
336 my $needs_quotes = $path =~ /[ \$\&\*'"]/;
337 if ($needs_quotes)
338 {
339 # escape and double quotes in the path
340 $has_double_quotes and $path =~ s/"/\\"/g;
341 $path = "\"$path\"";
342 }
343 return $path;
344}
345
346sub do_command
347{
348 my $cmd = shift;
349 my $description = @_ ? shift : "command";
350 my $die_on_fail = @_ ? shift : undef;
351 $debug and print "% $cmd\n";
352 system ($cmd);
353 if ($? == -1)
354 {
355 $debug and printf ("error: %s failed to execute: $!\n", $description);
356 $die_on_fail and $? and exit(1);
357 return $?;
358 }
359 elsif ($? & 127)
360 {
361 $debug and printf("error: %s child died with signal %d, %s coredump\n",
362 $description,
363 ($? & 127),
364 ($? & 128) ? 'with' : 'without');
365 $die_on_fail and $? and exit(1);
366 return $?;
367 }
368 else
369 {
370 my $exit = $? >> 8;
371 if ($exit)
372 {
373 $debug and printf("error: %s child exited with value %d\n", $description, $exit);
374 $die_on_fail and exit(1);
375 }
376 return $exit;
377 }
378}
379
380sub create_single_llvm_arhive_for_arch
381{
382 my $arch_dstroot = shift;
383 my $split_into_objects = shift;
384 my @object_dirs;
385 my $object_dir;
386 my $tmp_dir = $arch_dstroot;
387 my $arch_output_file = "$arch_dstroot/$llvm_clang_basename";
388 -e $arch_output_file and return;
389 my $files = "$arch_dstroot/files.txt";
390 open (FILES, ">$files") or die "Can't open $! for writing...\n";
391
392 for my $path (@archive_files)
393 {
394 my $archive_fullpath = finalize_path ("$arch_dstroot/$path");
395 if (-e $archive_fullpath)
396 {
397 if ($split_into_objects)
398 {
399 my ($archive_file, $archive_dir, $archive_ext) = fileparse($archive_fullpath, ('.a'));
400
401 $object_dir = "$tmp_dir/$archive_file";
402 push @object_dirs, $object_dir;
403
404 do_command ("cd '$tmp_dir'; mkdir '$archive_file'; cd '$archive_file'; ar -x $archive_fullpath");
405
406 my @objects = bsd_glob("$object_dir/*.o");
407
408 foreach my $object (@objects)
409 {
410 my ($o_file, $o_dir) = fileparse($object);
411 my $new_object = "$object_dir/${archive_file}-$o_file";
412 print FILES "$new_object\n";
413 do_command ("mv '$object' '$new_object'");
414 }
415 }
416 else
417 {
418 # just add the .a files into the file list
419 print FILES "$archive_fullpath\n";
420 }
421 }
Greg Clayton193e6d52010-07-13 22:26:45 +0000422 else
423 {
424 print "warning: archive doesn't exist: '$archive_fullpath'\n";
425 }
Chris Lattner24943d22010-06-08 16:52:24 +0000426 }
427 close (FILES);
428 do_command ("libtool -static -o '$arch_output_file' -filelist '$files'");
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000429 do_command ("ranlib '$arch_output_file'");
Chris Lattner24943d22010-06-08 16:52:24 +0000430
431 foreach $object_dir (@object_dirs)
432 {
433 do_command ("rm -rf '$object_dir'");
434 }
435 do_command ("rm -rf '$files'");
436}
437
438build_llvm();