blob: 975ce134652637421b636ef1a4b4d46486557d1c [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
19our $libedis_outfile = $ENV{SCRIPT_OUTPUT_FILE_0};
20our ($libedis_basename, $libedis_dirname) = fileparse ($libedis_outfile);
21our @libedis_slices; # Skinny mach-o slices for libEnhancedDisassembly.dylib
22
23our $llvm_clang_outfile = $ENV{SCRIPT_OUTPUT_FILE_1};
24our ($llvm_clang_basename, $llvm_clang_dirname) = fileparse ($llvm_clang_outfile);
25our @llvm_clang_slices; # paths to the single architecture static libraries (archives)
26
27our $llvm_configuration = $ENV{LLVM_CONFIGURATION};
28
Sean Callananc0492742011-05-23 21:40:23 +000029our $llvm_revision = "131657";
Chris Lattner24943d22010-06-08 16:52:24 +000030our $llvm_source_dir = "$ENV{SRCROOT}";
Chris Lattner24943d22010-06-08 16:52:24 +000031our @archs = split (/\s+/, $ENV{ARCHS});
32
33our @archive_files = (
Sean Callanan47a5c4c2010-09-23 03:01:22 +000034 "$llvm_configuration/lib/libclang.a",
Chris Lattner24943d22010-06-08 16:52:24 +000035 "$llvm_configuration/lib/libclangAnalysis.a",
36 "$llvm_configuration/lib/libclangAST.a",
37 "$llvm_configuration/lib/libclangBasic.a",
38 "$llvm_configuration/lib/libclangCodeGen.a",
39 "$llvm_configuration/lib/libclangFrontend.a",
40 "$llvm_configuration/lib/libclangDriver.a",
41 "$llvm_configuration/lib/libclangIndex.a",
42 "$llvm_configuration/lib/libclangLex.a",
43 "$llvm_configuration/lib/libclangRewrite.a",
44 "$llvm_configuration/lib/libclangParse.a",
45 "$llvm_configuration/lib/libclangSema.a",
Sean Callanan47a5c4c2010-09-23 03:01:22 +000046 "$llvm_configuration/lib/libclangSerialization.a",
Chris Lattner24943d22010-06-08 16:52:24 +000047 "$llvm_configuration/lib/libCompilerDriver.a",
48 "$llvm_configuration/lib/libEnhancedDisassembly.a",
49 "$llvm_configuration/lib/libLLVMAnalysis.a",
50 "$llvm_configuration/lib/libLLVMArchive.a",
51 "$llvm_configuration/lib/libLLVMARMAsmParser.a",
52 "$llvm_configuration/lib/libLLVMARMAsmPrinter.a",
53 "$llvm_configuration/lib/libLLVMARMCodeGen.a",
54 "$llvm_configuration/lib/libLLVMARMDisassembler.a",
55 "$llvm_configuration/lib/libLLVMARMInfo.a",
56 "$llvm_configuration/lib/libLLVMAsmParser.a",
57 "$llvm_configuration/lib/libLLVMAsmPrinter.a",
58 "$llvm_configuration/lib/libLLVMBitReader.a",
59 "$llvm_configuration/lib/libLLVMBitWriter.a",
60 "$llvm_configuration/lib/libLLVMCodeGen.a",
61 "$llvm_configuration/lib/libLLVMCore.a",
62 "$llvm_configuration/lib/libLLVMExecutionEngine.a",
63 "$llvm_configuration/lib/libLLVMInstCombine.a",
64 "$llvm_configuration/lib/libLLVMInstrumentation.a",
65 "$llvm_configuration/lib/libLLVMipa.a",
66 "$llvm_configuration/lib/libLLVMInterpreter.a",
67 "$llvm_configuration/lib/libLLVMipo.a",
68 "$llvm_configuration/lib/libLLVMJIT.a",
69 "$llvm_configuration/lib/libLLVMLinker.a",
70 "$llvm_configuration/lib/libLLVMMC.a",
71 "$llvm_configuration/lib/libLLVMMCParser.a",
Greg Claytonc9b60002010-07-21 16:57:26 +000072 "$llvm_configuration/lib/libLLVMMCDisassembler.a",
Sean Callananc0492742011-05-23 21:40:23 +000073 "$llvm_configuration/lib/libLLVMMCJIT.a",
74 "$llvm_configuration/lib/libLLVMObject.a",
75 "$llvm_configuration/lib/libLLVMRuntimeDyld.a",
Chris Lattner24943d22010-06-08 16:52:24 +000076 "$llvm_configuration/lib/libLLVMScalarOpts.a",
77 "$llvm_configuration/lib/libLLVMSelectionDAG.a",
78 "$llvm_configuration/lib/libLLVMSupport.a",
Chris Lattner24943d22010-06-08 16:52:24 +000079 "$llvm_configuration/lib/libLLVMTarget.a",
80 "$llvm_configuration/lib/libLLVMTransformUtils.a",
81 "$llvm_configuration/lib/libLLVMX86AsmParser.a",
82 "$llvm_configuration/lib/libLLVMX86AsmPrinter.a",
83 "$llvm_configuration/lib/libLLVMX86CodeGen.a",
84 "$llvm_configuration/lib/libLLVMX86Disassembler.a",
85 "$llvm_configuration/lib/libLLVMX86Info.a",
Sean Callanan279584c2011-03-15 00:17:19 +000086 "$llvm_configuration/lib/libLLVMX86Utils.a",
Chris Lattner24943d22010-06-08 16:52:24 +000087);
88
Greg Clayton17cd9952011-04-22 03:55:06 +000089if ($ENV{CONFIGURATION} ne "BuildAndIntegration" and -e "$llvm_srcroot/lib")
Chris Lattner24943d22010-06-08 16:52:24 +000090{
91 print "Using standard LLVM build directory...\n";
92 # LLVM in the "lldb" root is a symlink which indicates we are using a
93 # standard LLVM build directory where everything is built into the
94 # same folder
Greg Clayton48fbdf72010-10-12 04:29:14 +000095 create_single_llvm_arhive_for_arch ($llvm_dstroot, 1);
Chris Lattner24943d22010-06-08 16:52:24 +000096 my $llvm_dstroot_archive = "$llvm_dstroot/$llvm_clang_basename";
97 push @llvm_clang_slices, $llvm_dstroot_archive;
98 create_dstroot_file ($llvm_clang_basename, $llvm_clang_dirname, \@llvm_clang_slices, $llvm_clang_basename);
99 my $llvm_dstroot_edis = "$llvm_dstroot/$llvm_configuration/lib/libEnhancedDisassembly.dylib";
100 if (-f $llvm_dstroot_edis)
101 {
102 push @libedis_slices, $llvm_dstroot_edis;
103 create_dstroot_file ($libedis_basename, $libedis_dirname, \@libedis_slices, $libedis_basename);
104 }
105 exit 0;
106}
107
108
109if ($ENV{CONFIGURATION} eq "Debug" or $ENV{CONFIGURATION} eq "Release")
110{
111 # Check for an old llvm source install (not the minimal zip based
112 # install by looking for a .svn file in the llvm directory
113 chomp(my $llvm_zip_md5 = `md5 -q $ENV{SRCROOT}/llvm.zip`);
114 my $llvm_zip_md5_file = "$ENV{SRCROOT}/llvm/$llvm_zip_md5";
115 if (!-e "$llvm_zip_md5_file")
116 {
117 print "Updating LLVM to use checkpoint from: '$ENV{SRCROOT}/llvm.zip'...\n";
118 if (-d "$ENV{SRCROOT}/llvm")
119 {
120 do_command ("cd '$ENV{SRCROOT}' && rm -rf llvm", "removing old llvm repository", 1);
121 }
122 do_command ("cd '$ENV{SRCROOT}' && unzip -q llvm.zip && touch '$llvm_zip_md5_file'", "expanding llvm.zip", 1);
123 }
124
125 # We use the stuff in "lldb/llvm" for non B&I builds
126 if (!-e $libedis_outfile)
127 {
128 print "Copying '$ENV{SRCROOT}/llvm/$libedis_basename' to '$libedis_outfile'...\n";
129 do_command ("cp '$ENV{SRCROOT}/llvm/$libedis_basename' '$libedis_outfile'", "copying libedis", 1);
130 }
131 exit 0;
132}
133
134# If our output file already exists then we need not generate it again.
135if (-e $llvm_clang_outfile and -e $libedis_outfile)
136{
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";
157 my $svn_options = "--quiet --revision $llvm_revision";
158 if (-d "$llvm_source_dir/llvm")
159 {
160 print "Using existing llvm sources in: '$llvm_source_dir/llvm'\n";
161 # print "Updating llvm to revision $llvm_revision\n";
162 # do_command ("cd '$llvm_source_dir/llvm' && svn update $svn_options", "updating llvm from repository", 1);
163 # print "Updating clang to revision $llvm_revision\n";
164 # do_command ("cd '$llvm_source_dir/llvm/tools/clang' && svn update $svn_options", "updating clang from repository", 1);
165 }
166 else
167 {
168 print "Checking out llvm sources from revision $llvm_revision...\n";
169 do_command ("cd '$llvm_source_dir' && svn co $svn_options http://llvm.org/svn/llvm-project/llvm/trunk llvm", "checking out llvm from repository", 1);
170 print "Checking out clang sources from revision $llvm_revision...\n";
171 do_command ("cd '$llvm_source_dir/llvm/tools' && svn co $svn_options http://llvm.org/svn/llvm-project/cfe/trunk clang", "checking out clang from repository", 1);
172 print "Removing the llvm/test directory...\n";
173 do_command ("cd '$llvm_source_dir' && rm -rf llvm/test", "removing test directory", 1);
174 }
175
176 # Make the llvm build directory
177 my $arch_idx = 0;
178 foreach my $arch (@archs)
179 {
180 my $llvm_dstroot_arch = "${llvm_dstroot}/${arch}";
181
182 # if the arch destination root exists we have already built it
183 my $do_configure = 0;
184 my $do_make = 0;
185
186 my $llvm_dstroot_arch_archive = "$llvm_dstroot_arch/$llvm_clang_basename";
187 print "LLVM architecture root for ${arch} exists at '$llvm_dstroot_arch'...";
188 if (-e $llvm_dstroot_arch)
189 {
190 print "YES\n";
191 $do_configure = !-e "$llvm_dstroot_arch/config.log";
192
193 # dstroot for llvm build exists, make sure all .a files are built
194 for my $llvm_lib (@archive_files)
195 {
196 if (!-e "$llvm_dstroot_arch/$llvm_lib")
197 {
198 print "missing archive: '$llvm_dstroot_arch/$llvm_lib'\n";
199 $do_make = 1;
200 }
201 }
202 if (!-e $llvm_dstroot_arch_archive)
203 {
204 $do_make = 1;
Greg Claytonc9b60002010-07-21 16:57:26 +0000205 }
206 else
207 {
208 print "LLVM architecture archive for ${arch} is '$llvm_dstroot_arch_archive'\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000209 }
210 }
211 else
212 {
213 print "NO\n";
214 do_command ("mkdir -p '$llvm_dstroot_arch'", "making llvm build directory '$llvm_dstroot_arch'", 1);
215 $do_configure = 1;
216 $do_make = 1;
217 }
218
219 # If this is the first architecture, then make a symbolic link
220 # for any header files that get generated.
221 if ($arch_idx == 0)
222 {
223 if (!-l "$llvm_dstroot/llvm")
224 {
225 do_command ("cd $llvm_dstroot && ln -s './${arch}' llvm");
226 }
227 }
228
229 if ($do_configure)
230 {
231 # Build llvm and clang
232 print "Configuring clang ($arch) in '$llvm_dstroot_arch'...\n";
233 my $lldb_configuration_options = '';
Sean Callanan961abeb2010-07-08 02:13:18 +0000234 $llvm_configuration eq 'Release' and $lldb_configuration_options .= '--enable-optimized --disable-assertions';
Greg Claytonf15996e2011-04-07 22:46:35 +0000235 do_command ("cd '$llvm_dstroot_arch' && '$llvm_source_dir/llvm/configure' $lldb_configuration_options --enable-targets=x86_64,arm --build=$arch-apple-darwin10",
Chris Lattner24943d22010-06-08 16:52:24 +0000236 "configuring llvm build", 1);
237 }
238
239 if ($do_make)
240 {
241 # Build llvm and clang
242 my $num_cpus = parallel_guess();
243 print "Building clang using $num_cpus cpus ($arch)...\n";
244 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus clang-only VERBOSE=1 PROJECT_NAME='llvm'", "making llvm and clang", 1);
245 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus tools-only VERBOSE=1 PROJECT_NAME='llvm' EDIS_VERSION=1", "making libedis", 1);
246 # Combine all .o files from a bunch of static libraries from llvm
247 # and clang into a single .a file.
248 create_single_llvm_arhive_for_arch ($llvm_dstroot_arch, 1);
249 }
250
251 -f "$llvm_dstroot_arch_archive" and push @llvm_clang_slices, "$llvm_dstroot_arch_archive";
252 -f "$llvm_dstroot_arch/$llvm_configuration/lib/libEnhancedDisassembly.dylib" and push @libedis_slices, "$llvm_dstroot_arch/$llvm_configuration/lib/libEnhancedDisassembly.dylib";
253 ++$arch_idx;
254 }
255
256 # Combine all skinny slices of the LLVM/Clang combined archive
257 create_dstroot_file ($llvm_clang_basename, $llvm_clang_dirname, \@llvm_clang_slices, $llvm_clang_basename);
258
259 if (scalar(@libedis_slices))
260 {
261 # Combine all skinny slices of the libedis in SYMROOT
262 create_dstroot_file ($libedis_basename, $libedis_dirname, \@libedis_slices, $libedis_basename);
263
264 # Make dSYM for libedis in SYMROOT
265 do_command ("cd '$libedis_dirname' && dsymutil $libedis_basename", "making libedis dSYM", 1);
266
267 # strip debug symbols from libedis and copy into DSTROOT
268 -d "$ENV{DSTROOT}/Developer/usr/lib" or do_command ("mkdir -p '$ENV{DSTROOT}/Developer/usr/lib'", "Making directory '$ENV{DSTROOT}/Developer/usr/lib'", 1);
269 do_command ("cd '$libedis_dirname' && strip -Sx -o '$ENV{DSTROOT}/Developer/usr/lib/$libedis_basename' '$libedis_outfile'", "Stripping libedis and copying to DSTROOT", 1);
270 }
271}
272
273sub create_dstroot_file
274{
275 my $file = shift;
276 my $dir = shift;
277 my $fullpath = "$dir/$file"; # The path to the file to create
278 my $slice_aref = shift; # Array containing one or more skinny files that will be combined into $fullpath
279 my $what = shift; # Text describing the $fullpath
280
281 print "create_dstroot_file file = '$file', dir = '$dir', slices = (" . join (', ', @$slice_aref) . ") for what = '$what'\n";
282
283 if (-d $dir)
284 {
285 if (@$slice_aref > 0)
286 {
287 print "Creating and installing $what into '$fullpath'...\n";
288 my $lipo_command = "lipo -output '$fullpath' -create";
289 foreach (@$slice_aref) { $lipo_command .= " '$_'"; }
290 do_command ($lipo_command, "creating $what universal output file", 1);
291 }
292
293
294 if (!-e $fullpath)
295 {
296 # die "error: '$fullpath' is missing\n";
297 }
298 }
299 else
300 {
301 die "error: directory '$dir' doesn't exist to receive file '$file'\n";
302 }
303}
304#----------------------------------------------------------------------
305# quote the path if needed and realpath it if the -r option was
306# specified
307#----------------------------------------------------------------------
308sub finalize_path
309{
310 my $path = shift;
311 # Realpath all paths that don't start with "/"
312 $path =~ /^[^\/]/ and $path = abs_path($path);
313
314 # Quote the path if asked to, or if there are special shell characters
315 # in the path name
316 my $has_double_quotes = $path =~ /["]/;
317 my $has_single_quotes = $path =~ /[']/;
318 my $needs_quotes = $path =~ /[ \$\&\*'"]/;
319 if ($needs_quotes)
320 {
321 # escape and double quotes in the path
322 $has_double_quotes and $path =~ s/"/\\"/g;
323 $path = "\"$path\"";
324 }
325 return $path;
326}
327
328sub do_command
329{
330 my $cmd = shift;
331 my $description = @_ ? shift : "command";
332 my $die_on_fail = @_ ? shift : undef;
333 $debug and print "% $cmd\n";
334 system ($cmd);
335 if ($? == -1)
336 {
337 $debug and printf ("error: %s failed to execute: $!\n", $description);
338 $die_on_fail and $? and exit(1);
339 return $?;
340 }
341 elsif ($? & 127)
342 {
343 $debug and printf("error: %s child died with signal %d, %s coredump\n",
344 $description,
345 ($? & 127),
346 ($? & 128) ? 'with' : 'without');
347 $die_on_fail and $? and exit(1);
348 return $?;
349 }
350 else
351 {
352 my $exit = $? >> 8;
353 if ($exit)
354 {
355 $debug and printf("error: %s child exited with value %d\n", $description, $exit);
356 $die_on_fail and exit(1);
357 }
358 return $exit;
359 }
360}
361
362sub create_single_llvm_arhive_for_arch
363{
364 my $arch_dstroot = shift;
365 my $split_into_objects = shift;
366 my @object_dirs;
367 my $object_dir;
368 my $tmp_dir = $arch_dstroot;
369 my $arch_output_file = "$arch_dstroot/$llvm_clang_basename";
370 -e $arch_output_file and return;
371 my $files = "$arch_dstroot/files.txt";
372 open (FILES, ">$files") or die "Can't open $! for writing...\n";
373
374 for my $path (@archive_files)
375 {
376 my $archive_fullpath = finalize_path ("$arch_dstroot/$path");
377 if (-e $archive_fullpath)
378 {
379 if ($split_into_objects)
380 {
381 my ($archive_file, $archive_dir, $archive_ext) = fileparse($archive_fullpath, ('.a'));
382
383 $object_dir = "$tmp_dir/$archive_file";
384 push @object_dirs, $object_dir;
385
386 do_command ("cd '$tmp_dir'; mkdir '$archive_file'; cd '$archive_file'; ar -x $archive_fullpath");
387
388 my @objects = bsd_glob("$object_dir/*.o");
389
390 foreach my $object (@objects)
391 {
392 my ($o_file, $o_dir) = fileparse($object);
393 my $new_object = "$object_dir/${archive_file}-$o_file";
394 print FILES "$new_object\n";
395 do_command ("mv '$object' '$new_object'");
396 }
397 }
398 else
399 {
400 # just add the .a files into the file list
401 print FILES "$archive_fullpath\n";
402 }
403 }
Greg Clayton193e6d52010-07-13 22:26:45 +0000404 else
405 {
406 print "warning: archive doesn't exist: '$archive_fullpath'\n";
407 }
Chris Lattner24943d22010-06-08 16:52:24 +0000408 }
409 close (FILES);
410 do_command ("libtool -static -o '$arch_output_file' -filelist '$files'");
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000411 do_command ("ranlib '$arch_output_file'");
Chris Lattner24943d22010-06-08 16:52:24 +0000412
413 foreach $object_dir (@object_dirs)
414 {
415 do_command ("rm -rf '$object_dir'");
416 }
417 do_command ("rm -rf '$files'");
418}
419
420build_llvm();