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