blob: 3ada7ce6c2fb230d7141cd3398c45e3ed407075c [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 Callanane6c6f492011-03-26 00:52:28 +000029our $llvm_revision = "128303";
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",
Chris Lattner24943d22010-06-08 16:52:24 +000073 "$llvm_configuration/lib/libLLVMScalarOpts.a",
74 "$llvm_configuration/lib/libLLVMSelectionDAG.a",
75 "$llvm_configuration/lib/libLLVMSupport.a",
Chris Lattner24943d22010-06-08 16:52:24 +000076 "$llvm_configuration/lib/libLLVMTarget.a",
77 "$llvm_configuration/lib/libLLVMTransformUtils.a",
78 "$llvm_configuration/lib/libLLVMX86AsmParser.a",
79 "$llvm_configuration/lib/libLLVMX86AsmPrinter.a",
80 "$llvm_configuration/lib/libLLVMX86CodeGen.a",
81 "$llvm_configuration/lib/libLLVMX86Disassembler.a",
82 "$llvm_configuration/lib/libLLVMX86Info.a",
Sean Callanan279584c2011-03-15 00:17:19 +000083 "$llvm_configuration/lib/libLLVMX86Utils.a",
Chris Lattner24943d22010-06-08 16:52:24 +000084);
85
Greg Claytonf15996e2011-04-07 22:46:35 +000086if (-e "$llvm_srcroot/lib")
Chris Lattner24943d22010-06-08 16:52:24 +000087{
88 print "Using standard LLVM build directory...\n";
89 # LLVM in the "lldb" root is a symlink which indicates we are using a
90 # standard LLVM build directory where everything is built into the
91 # same folder
Greg Clayton48fbdf72010-10-12 04:29:14 +000092 create_single_llvm_arhive_for_arch ($llvm_dstroot, 1);
Chris Lattner24943d22010-06-08 16:52:24 +000093 my $llvm_dstroot_archive = "$llvm_dstroot/$llvm_clang_basename";
94 push @llvm_clang_slices, $llvm_dstroot_archive;
95 create_dstroot_file ($llvm_clang_basename, $llvm_clang_dirname, \@llvm_clang_slices, $llvm_clang_basename);
96 my $llvm_dstroot_edis = "$llvm_dstroot/$llvm_configuration/lib/libEnhancedDisassembly.dylib";
97 if (-f $llvm_dstroot_edis)
98 {
99 push @libedis_slices, $llvm_dstroot_edis;
100 create_dstroot_file ($libedis_basename, $libedis_dirname, \@libedis_slices, $libedis_basename);
101 }
102 exit 0;
103}
104
105
106if ($ENV{CONFIGURATION} eq "Debug" or $ENV{CONFIGURATION} eq "Release")
107{
108 # Check for an old llvm source install (not the minimal zip based
109 # install by looking for a .svn file in the llvm directory
110 chomp(my $llvm_zip_md5 = `md5 -q $ENV{SRCROOT}/llvm.zip`);
111 my $llvm_zip_md5_file = "$ENV{SRCROOT}/llvm/$llvm_zip_md5";
112 if (!-e "$llvm_zip_md5_file")
113 {
114 print "Updating LLVM to use checkpoint from: '$ENV{SRCROOT}/llvm.zip'...\n";
115 if (-d "$ENV{SRCROOT}/llvm")
116 {
117 do_command ("cd '$ENV{SRCROOT}' && rm -rf llvm", "removing old llvm repository", 1);
118 }
119 do_command ("cd '$ENV{SRCROOT}' && unzip -q llvm.zip && touch '$llvm_zip_md5_file'", "expanding llvm.zip", 1);
120 }
121
122 # We use the stuff in "lldb/llvm" for non B&I builds
123 if (!-e $libedis_outfile)
124 {
125 print "Copying '$ENV{SRCROOT}/llvm/$libedis_basename' to '$libedis_outfile'...\n";
126 do_command ("cp '$ENV{SRCROOT}/llvm/$libedis_basename' '$libedis_outfile'", "copying libedis", 1);
127 }
128 exit 0;
129}
130
131# If our output file already exists then we need not generate it again.
132if (-e $llvm_clang_outfile and -e $libedis_outfile)
133{
134 exit 0;
135}
136
137
138# Get our options
139
140our $debug = 1;
141
142sub parallel_guess
143{
144 my $cpus = `sysctl -n hw.availcpu`;
145 chomp ($cpus);
146 my $memsize = `sysctl -n hw.memsize`;
147 chomp ($memsize);
148 my $max_cpus_by_memory = int($memsize / (750 * 1024 * 1024));
149 return min($max_cpus_by_memory, $cpus);
150}
151sub build_llvm
152{
153 #my $extra_svn_options = $debug ? "" : "--quiet";
154 my $svn_options = "--quiet --revision $llvm_revision";
155 if (-d "$llvm_source_dir/llvm")
156 {
157 print "Using existing llvm sources in: '$llvm_source_dir/llvm'\n";
158 # print "Updating llvm to revision $llvm_revision\n";
159 # do_command ("cd '$llvm_source_dir/llvm' && svn update $svn_options", "updating llvm from repository", 1);
160 # print "Updating clang to revision $llvm_revision\n";
161 # do_command ("cd '$llvm_source_dir/llvm/tools/clang' && svn update $svn_options", "updating clang from repository", 1);
162 }
163 else
164 {
165 print "Checking out llvm sources from revision $llvm_revision...\n";
166 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);
167 print "Checking out clang sources from revision $llvm_revision...\n";
168 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);
169 print "Removing the llvm/test directory...\n";
170 do_command ("cd '$llvm_source_dir' && rm -rf llvm/test", "removing test directory", 1);
171 }
172
173 # Make the llvm build directory
174 my $arch_idx = 0;
175 foreach my $arch (@archs)
176 {
177 my $llvm_dstroot_arch = "${llvm_dstroot}/${arch}";
178
179 # if the arch destination root exists we have already built it
180 my $do_configure = 0;
181 my $do_make = 0;
182
183 my $llvm_dstroot_arch_archive = "$llvm_dstroot_arch/$llvm_clang_basename";
184 print "LLVM architecture root for ${arch} exists at '$llvm_dstroot_arch'...";
185 if (-e $llvm_dstroot_arch)
186 {
187 print "YES\n";
188 $do_configure = !-e "$llvm_dstroot_arch/config.log";
189
190 # dstroot for llvm build exists, make sure all .a files are built
191 for my $llvm_lib (@archive_files)
192 {
193 if (!-e "$llvm_dstroot_arch/$llvm_lib")
194 {
195 print "missing archive: '$llvm_dstroot_arch/$llvm_lib'\n";
196 $do_make = 1;
197 }
198 }
199 if (!-e $llvm_dstroot_arch_archive)
200 {
201 $do_make = 1;
Greg Claytonc9b60002010-07-21 16:57:26 +0000202 }
203 else
204 {
205 print "LLVM architecture archive for ${arch} is '$llvm_dstroot_arch_archive'\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000206 }
207 }
208 else
209 {
210 print "NO\n";
211 do_command ("mkdir -p '$llvm_dstroot_arch'", "making llvm build directory '$llvm_dstroot_arch'", 1);
212 $do_configure = 1;
213 $do_make = 1;
214 }
215
216 # If this is the first architecture, then make a symbolic link
217 # for any header files that get generated.
218 if ($arch_idx == 0)
219 {
220 if (!-l "$llvm_dstroot/llvm")
221 {
222 do_command ("cd $llvm_dstroot && ln -s './${arch}' llvm");
223 }
224 }
225
226 if ($do_configure)
227 {
228 # Build llvm and clang
229 print "Configuring clang ($arch) in '$llvm_dstroot_arch'...\n";
230 my $lldb_configuration_options = '';
Sean Callanan961abeb2010-07-08 02:13:18 +0000231 $llvm_configuration eq 'Release' and $lldb_configuration_options .= '--enable-optimized --disable-assertions';
Greg Claytonf15996e2011-04-07 22:46:35 +0000232 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 +0000233 "configuring llvm build", 1);
234 }
235
236 if ($do_make)
237 {
238 # Build llvm and clang
239 my $num_cpus = parallel_guess();
240 print "Building clang using $num_cpus cpus ($arch)...\n";
241 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus clang-only VERBOSE=1 PROJECT_NAME='llvm'", "making llvm and clang", 1);
242 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus tools-only VERBOSE=1 PROJECT_NAME='llvm' EDIS_VERSION=1", "making libedis", 1);
243 # Combine all .o files from a bunch of static libraries from llvm
244 # and clang into a single .a file.
245 create_single_llvm_arhive_for_arch ($llvm_dstroot_arch, 1);
246 }
247
248 -f "$llvm_dstroot_arch_archive" and push @llvm_clang_slices, "$llvm_dstroot_arch_archive";
249 -f "$llvm_dstroot_arch/$llvm_configuration/lib/libEnhancedDisassembly.dylib" and push @libedis_slices, "$llvm_dstroot_arch/$llvm_configuration/lib/libEnhancedDisassembly.dylib";
250 ++$arch_idx;
251 }
252
253 # Combine all skinny slices of the LLVM/Clang combined archive
254 create_dstroot_file ($llvm_clang_basename, $llvm_clang_dirname, \@llvm_clang_slices, $llvm_clang_basename);
255
256 if (scalar(@libedis_slices))
257 {
258 # Combine all skinny slices of the libedis in SYMROOT
259 create_dstroot_file ($libedis_basename, $libedis_dirname, \@libedis_slices, $libedis_basename);
260
261 # Make dSYM for libedis in SYMROOT
262 do_command ("cd '$libedis_dirname' && dsymutil $libedis_basename", "making libedis dSYM", 1);
263
264 # strip debug symbols from libedis and copy into DSTROOT
265 -d "$ENV{DSTROOT}/Developer/usr/lib" or do_command ("mkdir -p '$ENV{DSTROOT}/Developer/usr/lib'", "Making directory '$ENV{DSTROOT}/Developer/usr/lib'", 1);
266 do_command ("cd '$libedis_dirname' && strip -Sx -o '$ENV{DSTROOT}/Developer/usr/lib/$libedis_basename' '$libedis_outfile'", "Stripping libedis and copying to DSTROOT", 1);
267 }
268}
269
270sub create_dstroot_file
271{
272 my $file = shift;
273 my $dir = shift;
274 my $fullpath = "$dir/$file"; # The path to the file to create
275 my $slice_aref = shift; # Array containing one or more skinny files that will be combined into $fullpath
276 my $what = shift; # Text describing the $fullpath
277
278 print "create_dstroot_file file = '$file', dir = '$dir', slices = (" . join (', ', @$slice_aref) . ") for what = '$what'\n";
279
280 if (-d $dir)
281 {
282 if (@$slice_aref > 0)
283 {
284 print "Creating and installing $what into '$fullpath'...\n";
285 my $lipo_command = "lipo -output '$fullpath' -create";
286 foreach (@$slice_aref) { $lipo_command .= " '$_'"; }
287 do_command ($lipo_command, "creating $what universal output file", 1);
288 }
289
290
291 if (!-e $fullpath)
292 {
293 # die "error: '$fullpath' is missing\n";
294 }
295 }
296 else
297 {
298 die "error: directory '$dir' doesn't exist to receive file '$file'\n";
299 }
300}
301#----------------------------------------------------------------------
302# quote the path if needed and realpath it if the -r option was
303# specified
304#----------------------------------------------------------------------
305sub finalize_path
306{
307 my $path = shift;
308 # Realpath all paths that don't start with "/"
309 $path =~ /^[^\/]/ and $path = abs_path($path);
310
311 # Quote the path if asked to, or if there are special shell characters
312 # in the path name
313 my $has_double_quotes = $path =~ /["]/;
314 my $has_single_quotes = $path =~ /[']/;
315 my $needs_quotes = $path =~ /[ \$\&\*'"]/;
316 if ($needs_quotes)
317 {
318 # escape and double quotes in the path
319 $has_double_quotes and $path =~ s/"/\\"/g;
320 $path = "\"$path\"";
321 }
322 return $path;
323}
324
325sub do_command
326{
327 my $cmd = shift;
328 my $description = @_ ? shift : "command";
329 my $die_on_fail = @_ ? shift : undef;
330 $debug and print "% $cmd\n";
331 system ($cmd);
332 if ($? == -1)
333 {
334 $debug and printf ("error: %s failed to execute: $!\n", $description);
335 $die_on_fail and $? and exit(1);
336 return $?;
337 }
338 elsif ($? & 127)
339 {
340 $debug and printf("error: %s child died with signal %d, %s coredump\n",
341 $description,
342 ($? & 127),
343 ($? & 128) ? 'with' : 'without');
344 $die_on_fail and $? and exit(1);
345 return $?;
346 }
347 else
348 {
349 my $exit = $? >> 8;
350 if ($exit)
351 {
352 $debug and printf("error: %s child exited with value %d\n", $description, $exit);
353 $die_on_fail and exit(1);
354 }
355 return $exit;
356 }
357}
358
359sub create_single_llvm_arhive_for_arch
360{
361 my $arch_dstroot = shift;
362 my $split_into_objects = shift;
363 my @object_dirs;
364 my $object_dir;
365 my $tmp_dir = $arch_dstroot;
366 my $arch_output_file = "$arch_dstroot/$llvm_clang_basename";
367 -e $arch_output_file and return;
368 my $files = "$arch_dstroot/files.txt";
369 open (FILES, ">$files") or die "Can't open $! for writing...\n";
370
371 for my $path (@archive_files)
372 {
373 my $archive_fullpath = finalize_path ("$arch_dstroot/$path");
374 if (-e $archive_fullpath)
375 {
376 if ($split_into_objects)
377 {
378 my ($archive_file, $archive_dir, $archive_ext) = fileparse($archive_fullpath, ('.a'));
379
380 $object_dir = "$tmp_dir/$archive_file";
381 push @object_dirs, $object_dir;
382
383 do_command ("cd '$tmp_dir'; mkdir '$archive_file'; cd '$archive_file'; ar -x $archive_fullpath");
384
385 my @objects = bsd_glob("$object_dir/*.o");
386
387 foreach my $object (@objects)
388 {
389 my ($o_file, $o_dir) = fileparse($object);
390 my $new_object = "$object_dir/${archive_file}-$o_file";
391 print FILES "$new_object\n";
392 do_command ("mv '$object' '$new_object'");
393 }
394 }
395 else
396 {
397 # just add the .a files into the file list
398 print FILES "$archive_fullpath\n";
399 }
400 }
Greg Clayton193e6d52010-07-13 22:26:45 +0000401 else
402 {
403 print "warning: archive doesn't exist: '$archive_fullpath'\n";
404 }
Chris Lattner24943d22010-06-08 16:52:24 +0000405 }
406 close (FILES);
407 do_command ("libtool -static -o '$arch_output_file' -filelist '$files'");
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000408 do_command ("ranlib '$arch_output_file'");
Chris Lattner24943d22010-06-08 16:52:24 +0000409
410 foreach $object_dir (@object_dirs)
411 {
412 do_command ("rm -rf '$object_dir'");
413 }
414 do_command ("rm -rf '$files'");
415}
416
417build_llvm();