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