blob: 3adfc11996f74c854df06ae6e6aeb3ce7ae963c5 [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
28our $llvm_revision = "'{2010-06-01T13:00}'";
29our $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 = (
35 "$llvm_configuration/lib/libplugin_llvmc_Base.a",
36 "$llvm_configuration/lib/libplugin_llvmc_Clang.a",
37 "$llvm_configuration/lib/libclangAnalysis.a",
38 "$llvm_configuration/lib/libclangAST.a",
39 "$llvm_configuration/lib/libclangBasic.a",
40 "$llvm_configuration/lib/libclangCodeGen.a",
41 "$llvm_configuration/lib/libclangFrontend.a",
42 "$llvm_configuration/lib/libclangDriver.a",
43 "$llvm_configuration/lib/libclangIndex.a",
44 "$llvm_configuration/lib/libclangLex.a",
45 "$llvm_configuration/lib/libclangRewrite.a",
46 "$llvm_configuration/lib/libclangParse.a",
47 "$llvm_configuration/lib/libclangSema.a",
48 "$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",
73 "$llvm_configuration/lib/libLLVMScalarOpts.a",
74 "$llvm_configuration/lib/libLLVMSelectionDAG.a",
75 "$llvm_configuration/lib/libLLVMSupport.a",
76 "$llvm_configuration/lib/libLLVMSystem.a",
77 "$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",
84 "$llvm_configuration/lib/libclangChecker.a"
85);
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
93 create_single_llvm_arhive_for_arch ($llvm_dstroot, 0);
94 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;
203 }
204 }
205 else
206 {
207 print "NO\n";
208 do_command ("mkdir -p '$llvm_dstroot_arch'", "making llvm build directory '$llvm_dstroot_arch'", 1);
209 $do_configure = 1;
210 $do_make = 1;
211 }
212
213 # If this is the first architecture, then make a symbolic link
214 # for any header files that get generated.
215 if ($arch_idx == 0)
216 {
217 if (!-l "$llvm_dstroot/llvm")
218 {
219 do_command ("cd $llvm_dstroot && ln -s './${arch}' llvm");
220 }
221 }
222
223 if ($do_configure)
224 {
225 # Build llvm and clang
226 print "Configuring clang ($arch) in '$llvm_dstroot_arch'...\n";
227 my $lldb_configuration_options = '';
228 $llvm_configuration eq 'Release' and $lldb_configuration_options .= '--enable-optimized';
229 do_command ("cd '$llvm_dstroot_arch' && '$llvm_source_dir/llvm/configure' $lldb_configuration_options --enable-targets=x86,x86_64,arm --build=$arch-apple-darwin10 CC=\"$cc -arch $arch\" CXX=\"$cxx -arch $arch\"",
230 "configuring llvm build", 1);
231 }
232
233 if ($do_make)
234 {
235 # Build llvm and clang
236 my $num_cpus = parallel_guess();
237 print "Building clang using $num_cpus cpus ($arch)...\n";
238 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus clang-only VERBOSE=1 PROJECT_NAME='llvm'", "making llvm and clang", 1);
239 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus tools-only VERBOSE=1 PROJECT_NAME='llvm' EDIS_VERSION=1", "making libedis", 1);
240 # Combine all .o files from a bunch of static libraries from llvm
241 # and clang into a single .a file.
242 create_single_llvm_arhive_for_arch ($llvm_dstroot_arch, 1);
243 }
244
245 -f "$llvm_dstroot_arch_archive" and push @llvm_clang_slices, "$llvm_dstroot_arch_archive";
246 -f "$llvm_dstroot_arch/$llvm_configuration/lib/libEnhancedDisassembly.dylib" and push @libedis_slices, "$llvm_dstroot_arch/$llvm_configuration/lib/libEnhancedDisassembly.dylib";
247 ++$arch_idx;
248 }
249
250 # Combine all skinny slices of the LLVM/Clang combined archive
251 create_dstroot_file ($llvm_clang_basename, $llvm_clang_dirname, \@llvm_clang_slices, $llvm_clang_basename);
252
253 if (scalar(@libedis_slices))
254 {
255 # Combine all skinny slices of the libedis in SYMROOT
256 create_dstroot_file ($libedis_basename, $libedis_dirname, \@libedis_slices, $libedis_basename);
257
258 # Make dSYM for libedis in SYMROOT
259 do_command ("cd '$libedis_dirname' && dsymutil $libedis_basename", "making libedis dSYM", 1);
260
261 # strip debug symbols from libedis and copy into DSTROOT
262 -d "$ENV{DSTROOT}/Developer/usr/lib" or do_command ("mkdir -p '$ENV{DSTROOT}/Developer/usr/lib'", "Making directory '$ENV{DSTROOT}/Developer/usr/lib'", 1);
263 do_command ("cd '$libedis_dirname' && strip -Sx -o '$ENV{DSTROOT}/Developer/usr/lib/$libedis_basename' '$libedis_outfile'", "Stripping libedis and copying to DSTROOT", 1);
264 }
265}
266
267sub create_dstroot_file
268{
269 my $file = shift;
270 my $dir = shift;
271 my $fullpath = "$dir/$file"; # The path to the file to create
272 my $slice_aref = shift; # Array containing one or more skinny files that will be combined into $fullpath
273 my $what = shift; # Text describing the $fullpath
274
275 print "create_dstroot_file file = '$file', dir = '$dir', slices = (" . join (', ', @$slice_aref) . ") for what = '$what'\n";
276
277 if (-d $dir)
278 {
279 if (@$slice_aref > 0)
280 {
281 print "Creating and installing $what into '$fullpath'...\n";
282 my $lipo_command = "lipo -output '$fullpath' -create";
283 foreach (@$slice_aref) { $lipo_command .= " '$_'"; }
284 do_command ($lipo_command, "creating $what universal output file", 1);
285 }
286
287
288 if (!-e $fullpath)
289 {
290 # die "error: '$fullpath' is missing\n";
291 }
292 }
293 else
294 {
295 die "error: directory '$dir' doesn't exist to receive file '$file'\n";
296 }
297}
298#----------------------------------------------------------------------
299# quote the path if needed and realpath it if the -r option was
300# specified
301#----------------------------------------------------------------------
302sub finalize_path
303{
304 my $path = shift;
305 # Realpath all paths that don't start with "/"
306 $path =~ /^[^\/]/ and $path = abs_path($path);
307
308 # Quote the path if asked to, or if there are special shell characters
309 # in the path name
310 my $has_double_quotes = $path =~ /["]/;
311 my $has_single_quotes = $path =~ /[']/;
312 my $needs_quotes = $path =~ /[ \$\&\*'"]/;
313 if ($needs_quotes)
314 {
315 # escape and double quotes in the path
316 $has_double_quotes and $path =~ s/"/\\"/g;
317 $path = "\"$path\"";
318 }
319 return $path;
320}
321
322sub do_command
323{
324 my $cmd = shift;
325 my $description = @_ ? shift : "command";
326 my $die_on_fail = @_ ? shift : undef;
327 $debug and print "% $cmd\n";
328 system ($cmd);
329 if ($? == -1)
330 {
331 $debug and printf ("error: %s failed to execute: $!\n", $description);
332 $die_on_fail and $? and exit(1);
333 return $?;
334 }
335 elsif ($? & 127)
336 {
337 $debug and printf("error: %s child died with signal %d, %s coredump\n",
338 $description,
339 ($? & 127),
340 ($? & 128) ? 'with' : 'without');
341 $die_on_fail and $? and exit(1);
342 return $?;
343 }
344 else
345 {
346 my $exit = $? >> 8;
347 if ($exit)
348 {
349 $debug and printf("error: %s child exited with value %d\n", $description, $exit);
350 $die_on_fail and exit(1);
351 }
352 return $exit;
353 }
354}
355
356sub create_single_llvm_arhive_for_arch
357{
358 my $arch_dstroot = shift;
359 my $split_into_objects = shift;
360 my @object_dirs;
361 my $object_dir;
362 my $tmp_dir = $arch_dstroot;
363 my $arch_output_file = "$arch_dstroot/$llvm_clang_basename";
364 -e $arch_output_file and return;
365 my $files = "$arch_dstroot/files.txt";
366 open (FILES, ">$files") or die "Can't open $! for writing...\n";
367
368 for my $path (@archive_files)
369 {
370 my $archive_fullpath = finalize_path ("$arch_dstroot/$path");
371 if (-e $archive_fullpath)
372 {
373 if ($split_into_objects)
374 {
375 my ($archive_file, $archive_dir, $archive_ext) = fileparse($archive_fullpath, ('.a'));
376
377 $object_dir = "$tmp_dir/$archive_file";
378 push @object_dirs, $object_dir;
379
380 do_command ("cd '$tmp_dir'; mkdir '$archive_file'; cd '$archive_file'; ar -x $archive_fullpath");
381
382 my @objects = bsd_glob("$object_dir/*.o");
383
384 foreach my $object (@objects)
385 {
386 my ($o_file, $o_dir) = fileparse($object);
387 my $new_object = "$object_dir/${archive_file}-$o_file";
388 print FILES "$new_object\n";
389 do_command ("mv '$object' '$new_object'");
390 }
391 }
392 else
393 {
394 # just add the .a files into the file list
395 print FILES "$archive_fullpath\n";
396 }
397 }
398 }
399 close (FILES);
400 do_command ("libtool -static -o '$arch_output_file' -filelist '$files'");
401
402 foreach $object_dir (@object_dirs)
403 {
404 do_command ("rm -rf '$object_dir'");
405 }
406 do_command ("rm -rf '$files'");
407}
408
409build_llvm();