blob: 3be920e208a4eb899676f2d9239a5fffcb64c867 [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
Sean Callanan6c98ba72011-08-11 20:11:18 +000019our $llvm_clang_outfile = $ENV{SCRIPT_OUTPUT_FILE_0};
Chris Lattner24943d22010-06-08 16:52:24 +000020our ($llvm_clang_basename, $llvm_clang_dirname) = fileparse ($llvm_clang_outfile);
Chris Lattner24943d22010-06-08 16:52:24 +000021
22our $llvm_configuration = $ENV{LLVM_CONFIGURATION};
23
Sean Callanan06dc17f2012-09-24 22:25:51 +000024our $llvm_revision = "HEAD";
25our $clang_revision = "HEAD";
Sean Callanan047eea62011-08-18 18:18:33 +000026
Greg Clayton3e4238d2011-11-04 03:34:56 +000027our $SRCROOT = "$ENV{SRCROOT}";
Chris Lattner24943d22010-06-08 16:52:24 +000028our @archs = split (/\s+/, $ENV{ARCHS});
Greg Clayton3e4238d2011-11-04 03:34:56 +000029my $os_release = 11;
30
Jason Molendadaaa9d12012-04-02 08:56:42 +000031my $original_env_path = $ENV{PATH};
32
Greg Clayton3e4238d2011-11-04 03:34:56 +000033our %llvm_config_info = (
Sean Callanan4582fea2012-08-09 01:32:13 +000034 'Debug' => { configure_options => '--disable-optimized --disable-assertions --enable-libcpp', make_options => 'DEBUG_SYMBOLS=1'},
35 'Debug+Asserts' => { configure_options => '--disable-optimized --enable-assertions --enable-libcpp' , make_options => 'DEBUG_SYMBOLS=1'},
36 'Release' => { configure_options => '--enable-optimized --disable-assertions --enable-libcpp' , make_options => ''},
37 'Release+Debug' => { configure_options => '--enable-optimized --disable-assertions --enable-libcpp' , make_options => 'DEBUG_SYMBOLS=1'},
Greg Clayton3e4238d2011-11-04 03:34:56 +000038);
39
40our $llvm_config_href = undef;
41if (exists $llvm_config_info{"$llvm_configuration"})
42{
43 $llvm_config_href = $llvm_config_info{$llvm_configuration};
44}
45else
46{
47 die "Unsupported LLVM configuration: '$llvm_configuration'\n";
48}
Chris Lattner24943d22010-06-08 16:52:24 +000049
50our @archive_files = (
Sean Callanan47a5c4c2010-09-23 03:01:22 +000051 "$llvm_configuration/lib/libclang.a",
Chris Lattner24943d22010-06-08 16:52:24 +000052 "$llvm_configuration/lib/libclangAnalysis.a",
53 "$llvm_configuration/lib/libclangAST.a",
54 "$llvm_configuration/lib/libclangBasic.a",
55 "$llvm_configuration/lib/libclangCodeGen.a",
Sean Callanan6e12c7a2012-03-08 02:39:03 +000056 "$llvm_configuration/lib/libclangEdit.a",
Chris Lattner24943d22010-06-08 16:52:24 +000057 "$llvm_configuration/lib/libclangFrontend.a",
58 "$llvm_configuration/lib/libclangDriver.a",
Chris Lattner24943d22010-06-08 16:52:24 +000059 "$llvm_configuration/lib/libclangLex.a",
Chris Lattner24943d22010-06-08 16:52:24 +000060 "$llvm_configuration/lib/libclangParse.a",
61 "$llvm_configuration/lib/libclangSema.a",
Sean Callanan47a5c4c2010-09-23 03:01:22 +000062 "$llvm_configuration/lib/libclangSerialization.a",
Chris Lattner24943d22010-06-08 16:52:24 +000063 "$llvm_configuration/lib/libLLVMAnalysis.a",
64 "$llvm_configuration/lib/libLLVMArchive.a",
65 "$llvm_configuration/lib/libLLVMARMAsmParser.a",
66 "$llvm_configuration/lib/libLLVMARMAsmPrinter.a",
67 "$llvm_configuration/lib/libLLVMARMCodeGen.a",
Sean Callanan9b6898f2011-07-30 02:42:06 +000068 "$llvm_configuration/lib/libLLVMARMDesc.a",
Chris Lattner24943d22010-06-08 16:52:24 +000069 "$llvm_configuration/lib/libLLVMARMDisassembler.a",
70 "$llvm_configuration/lib/libLLVMARMInfo.a",
71 "$llvm_configuration/lib/libLLVMAsmParser.a",
72 "$llvm_configuration/lib/libLLVMAsmPrinter.a",
73 "$llvm_configuration/lib/libLLVMBitReader.a",
74 "$llvm_configuration/lib/libLLVMBitWriter.a",
75 "$llvm_configuration/lib/libLLVMCodeGen.a",
76 "$llvm_configuration/lib/libLLVMCore.a",
77 "$llvm_configuration/lib/libLLVMExecutionEngine.a",
78 "$llvm_configuration/lib/libLLVMInstCombine.a",
79 "$llvm_configuration/lib/libLLVMInstrumentation.a",
80 "$llvm_configuration/lib/libLLVMipa.a",
81 "$llvm_configuration/lib/libLLVMInterpreter.a",
82 "$llvm_configuration/lib/libLLVMipo.a",
83 "$llvm_configuration/lib/libLLVMJIT.a",
84 "$llvm_configuration/lib/libLLVMLinker.a",
85 "$llvm_configuration/lib/libLLVMMC.a",
86 "$llvm_configuration/lib/libLLVMMCParser.a",
Greg Claytonc9b60002010-07-21 16:57:26 +000087 "$llvm_configuration/lib/libLLVMMCDisassembler.a",
Sean Callananc0492742011-05-23 21:40:23 +000088 "$llvm_configuration/lib/libLLVMMCJIT.a",
89 "$llvm_configuration/lib/libLLVMObject.a",
90 "$llvm_configuration/lib/libLLVMRuntimeDyld.a",
Chris Lattner24943d22010-06-08 16:52:24 +000091 "$llvm_configuration/lib/libLLVMScalarOpts.a",
92 "$llvm_configuration/lib/libLLVMSelectionDAG.a",
93 "$llvm_configuration/lib/libLLVMSupport.a",
Chris Lattner24943d22010-06-08 16:52:24 +000094 "$llvm_configuration/lib/libLLVMTarget.a",
95 "$llvm_configuration/lib/libLLVMTransformUtils.a",
96 "$llvm_configuration/lib/libLLVMX86AsmParser.a",
97 "$llvm_configuration/lib/libLLVMX86AsmPrinter.a",
98 "$llvm_configuration/lib/libLLVMX86CodeGen.a",
Sean Callanan9b6898f2011-07-30 02:42:06 +000099 "$llvm_configuration/lib/libLLVMX86Desc.a",
Chris Lattner24943d22010-06-08 16:52:24 +0000100 "$llvm_configuration/lib/libLLVMX86Disassembler.a",
101 "$llvm_configuration/lib/libLLVMX86Info.a",
Sean Callanan279584c2011-03-15 00:17:19 +0000102 "$llvm_configuration/lib/libLLVMX86Utils.a",
Chris Lattner24943d22010-06-08 16:52:24 +0000103);
104
Greg Clayton3e4238d2011-11-04 03:34:56 +0000105if (-e "$llvm_srcroot/lib")
Chris Lattner24943d22010-06-08 16:52:24 +0000106{
Greg Clayton3e4238d2011-11-04 03:34:56 +0000107 print "Using existing llvm sources in: '$llvm_srcroot'\n";
108 print "Using standard LLVM build directory:\n SRC = '$llvm_srcroot'\n DST = '$llvm_dstroot'\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000109}
Greg Clayton3e4238d2011-11-04 03:34:56 +0000110else
111{
112 print "Checking out llvm sources from revision $llvm_revision...\n";
113 do_command ("cd '$SRCROOT' && svn co --quiet --revision $llvm_revision http://llvm.org/svn/llvm-project/llvm/trunk llvm", "checking out llvm from repository", 1);
114 print "Checking out clang sources from revision $clang_revision...\n";
115 do_command ("cd '$llvm_srcroot/tools' && svn co --quiet --revision $clang_revision http://llvm.org/svn/llvm-project/cfe/trunk clang", "checking out clang from repository", 1);
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000116 print "Applying any local patches to LLVM/Clang...";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000117
118 my @llvm_patches = bsd_glob("$ENV{SRCROOT}/scripts/llvm.*.diff");
119
120 foreach my $patch (@llvm_patches)
121 {
122 do_command ("cd '$llvm_srcroot' && patch -p0 < $patch");
123 }
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000124
125 my @clang_patches = bsd_glob("$ENV{SRCROOT}/scripts/clang.*.diff");
126
127 foreach my $patch (@clang_patches)
128 {
129 do_command ("cd '$llvm_srcroot/tools/clang' && patch -p0 < $patch");
130 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000131
132 print "Removing the llvm/test and llvm/tools/clang/test directories...\n";
133 do_command ("cd '$llvm_srcroot' && rm -rf test && rm -rf tools/clang/test ", "removing test directories", 1);
134}
Chris Lattner24943d22010-06-08 16:52:24 +0000135
136# If our output file already exists then we need not generate it again.
Greg Clayton64875582011-08-02 19:00:17 +0000137if (-e $llvm_clang_outfile)
Chris Lattner24943d22010-06-08 16:52:24 +0000138{
139 exit 0;
140}
141
142
143# Get our options
144
145our $debug = 1;
146
147sub parallel_guess
148{
149 my $cpus = `sysctl -n hw.availcpu`;
150 chomp ($cpus);
151 my $memsize = `sysctl -n hw.memsize`;
152 chomp ($memsize);
153 my $max_cpus_by_memory = int($memsize / (750 * 1024 * 1024));
154 return min($max_cpus_by_memory, $cpus);
155}
156sub build_llvm
157{
158 #my $extra_svn_options = $debug ? "" : "--quiet";
Chris Lattner24943d22010-06-08 16:52:24 +0000159 # Make the llvm build directory
160 my $arch_idx = 0;
161 foreach my $arch (@archs)
162 {
163 my $llvm_dstroot_arch = "${llvm_dstroot}/${arch}";
164
165 # if the arch destination root exists we have already built it
166 my $do_configure = 0;
167 my $do_make = 0;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000168 my $is_arm = $arch =~ /^arm/;
169
Chris Lattner24943d22010-06-08 16:52:24 +0000170 my $llvm_dstroot_arch_archive = "$llvm_dstroot_arch/$llvm_clang_basename";
171 print "LLVM architecture root for ${arch} exists at '$llvm_dstroot_arch'...";
172 if (-e $llvm_dstroot_arch)
173 {
174 print "YES\n";
175 $do_configure = !-e "$llvm_dstroot_arch/config.log";
176
177 # dstroot for llvm build exists, make sure all .a files are built
178 for my $llvm_lib (@archive_files)
179 {
180 if (!-e "$llvm_dstroot_arch/$llvm_lib")
181 {
182 print "missing archive: '$llvm_dstroot_arch/$llvm_lib'\n";
183 $do_make = 1;
184 }
185 }
186 if (!-e $llvm_dstroot_arch_archive)
187 {
188 $do_make = 1;
Greg Claytonc9b60002010-07-21 16:57:26 +0000189 }
190 else
191 {
192 print "LLVM architecture archive for ${arch} is '$llvm_dstroot_arch_archive'\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000193 }
194 }
195 else
196 {
197 print "NO\n";
198 do_command ("mkdir -p '$llvm_dstroot_arch'", "making llvm build directory '$llvm_dstroot_arch'", 1);
199 $do_configure = 1;
200 $do_make = 1;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000201
202 if ($is_arm)
Chris Lattner24943d22010-06-08 16:52:24 +0000203 {
Greg Clayton3e4238d2011-11-04 03:34:56 +0000204 my $llvm_dstroot_arch_bin = "${llvm_dstroot_arch}/bin";
205
206 if (!-d $llvm_dstroot_arch_bin)
207 {
208 do_command ("mkdir -p '$llvm_dstroot_arch_bin'", "making llvm build arch bin directory '$llvm_dstroot_arch_bin'", 1);
209 my @tools = ("ar", "nm", "ranlib", "strip", "lipo", "ld", "as");
210 my $script_mode = 0755;
211 my $prog;
212 for $prog (@tools)
213 {
214 chomp(my $actual_prog_path = `xcrun -sdk '$ENV{SDKROOT}' -find ${prog}`);
215 my $script_prog_path = "$llvm_dstroot_arch_bin/arm-apple-darwin${os_release}-${prog}";
216 open (SCRIPT, ">$script_prog_path") or die "Can't open $! for writing...\n";
217 print SCRIPT "#!/bin/sh\nexec '$actual_prog_path' \"\$\@\"\n";
218 close (SCRIPT);
219 chmod($script_mode, $script_prog_path);
220 }
221 # Tools that must have the "-arch" and "-sysroot" specified
222 my @arch_sysroot_tools = ("clang", "clang++", "gcc", "g++");
223 for $prog (@arch_sysroot_tools)
224 {
225 chomp(my $actual_prog_path = `xcrun -sdk '$ENV{SDKROOT}' -find ${prog}`);
226 my $script_prog_path = "$llvm_dstroot_arch_bin/arm-apple-darwin${os_release}-${prog}";
227 open (SCRIPT, ">$script_prog_path") or die "Can't open $! for writing...\n";
228 print SCRIPT "#!/bin/sh\nexec '$actual_prog_path' -arch ${arch} -isysroot '$ENV{SDKROOT}' \"\$\@\"\n";
229 close (SCRIPT);
230 chmod($script_mode, $script_prog_path);
231 }
Jason Molendadaaa9d12012-04-02 08:56:42 +0000232 my $new_path = "$original_env_path:$llvm_dstroot_arch_bin";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000233 print "Setting new environment PATH = '$new_path'\n";
234 $ENV{PATH} = $new_path;
235 }
Chris Lattner24943d22010-06-08 16:52:24 +0000236 }
237 }
Greg Clayton3e4238d2011-11-04 03:34:56 +0000238
Chris Lattner24943d22010-06-08 16:52:24 +0000239 if ($do_configure)
240 {
241 # Build llvm and clang
242 print "Configuring clang ($arch) in '$llvm_dstroot_arch'...\n";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000243 my $lldb_configuration_options = "--enable-targets=x86_64,arm $llvm_config_href->{configure_options}";
244
245 if ($is_arm)
246 {
247 $lldb_configuration_options .= " --host=arm-apple-darwin${os_release} --target=arm-apple-darwin${os_release} --build=i686-apple-darwin${os_release}";
248 }
249 else
250 {
251 $lldb_configuration_options .= " --build=$arch-apple-darwin${os_release}";
252 }
253 do_command ("cd '$llvm_dstroot_arch' && '$llvm_srcroot/configure' $lldb_configuration_options",
Chris Lattner24943d22010-06-08 16:52:24 +0000254 "configuring llvm build", 1);
255 }
256
257 if ($do_make)
258 {
259 # Build llvm and clang
260 my $num_cpus = parallel_guess();
261 print "Building clang using $num_cpus cpus ($arch)...\n";
Greg Clayton3e4238d2011-11-04 03:34:56 +0000262 my $extra_make_flags = '';
263 if ($is_arm)
264 {
265 $extra_make_flags = "UNIVERSAL=1 UNIVERSAL_ARCH=${arch} UNIVERSAL_SDK_PATH='$ENV{SDKROOT}'";
266 }
267 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus clang-only VERBOSE=1 $llvm_config_href->{make_options} NO_RUNTIME_LIBS=1 PROJECT_NAME='llvm' $extra_make_flags", "making llvm and clang", 1);
268 do_command ("cd '$llvm_dstroot_arch' && make -j$num_cpus tools-only VERBOSE=1 $llvm_config_href->{make_options} NO_RUNTIME_LIBS=1 PROJECT_NAME='llvm' $extra_make_flags EDIS_VERSION=1", "making libedis", 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000269 # Combine all .o files from a bunch of static libraries from llvm
270 # and clang into a single .a file.
271 create_single_llvm_arhive_for_arch ($llvm_dstroot_arch, 1);
272 }
273
Chris Lattner24943d22010-06-08 16:52:24 +0000274 ++$arch_idx;
275 }
Chris Lattner24943d22010-06-08 16:52:24 +0000276}
277
Chris Lattner24943d22010-06-08 16:52:24 +0000278#----------------------------------------------------------------------
279# quote the path if needed and realpath it if the -r option was
280# specified
281#----------------------------------------------------------------------
282sub finalize_path
283{
284 my $path = shift;
285 # Realpath all paths that don't start with "/"
286 $path =~ /^[^\/]/ and $path = abs_path($path);
287
288 # Quote the path if asked to, or if there are special shell characters
289 # in the path name
290 my $has_double_quotes = $path =~ /["]/;
291 my $has_single_quotes = $path =~ /[']/;
292 my $needs_quotes = $path =~ /[ \$\&\*'"]/;
293 if ($needs_quotes)
294 {
295 # escape and double quotes in the path
296 $has_double_quotes and $path =~ s/"/\\"/g;
297 $path = "\"$path\"";
298 }
299 return $path;
300}
301
302sub do_command
303{
304 my $cmd = shift;
305 my $description = @_ ? shift : "command";
306 my $die_on_fail = @_ ? shift : undef;
307 $debug and print "% $cmd\n";
308 system ($cmd);
309 if ($? == -1)
310 {
311 $debug and printf ("error: %s failed to execute: $!\n", $description);
312 $die_on_fail and $? and exit(1);
313 return $?;
314 }
315 elsif ($? & 127)
316 {
317 $debug and printf("error: %s child died with signal %d, %s coredump\n",
318 $description,
319 ($? & 127),
320 ($? & 128) ? 'with' : 'without');
321 $die_on_fail and $? and exit(1);
322 return $?;
323 }
324 else
325 {
326 my $exit = $? >> 8;
327 if ($exit)
328 {
329 $debug and printf("error: %s child exited with value %d\n", $description, $exit);
330 $die_on_fail and exit(1);
331 }
332 return $exit;
333 }
334}
335
336sub create_single_llvm_arhive_for_arch
337{
338 my $arch_dstroot = shift;
339 my $split_into_objects = shift;
340 my @object_dirs;
341 my $object_dir;
342 my $tmp_dir = $arch_dstroot;
343 my $arch_output_file = "$arch_dstroot/$llvm_clang_basename";
344 -e $arch_output_file and return;
345 my $files = "$arch_dstroot/files.txt";
346 open (FILES, ">$files") or die "Can't open $! for writing...\n";
347
348 for my $path (@archive_files)
349 {
350 my $archive_fullpath = finalize_path ("$arch_dstroot/$path");
351 if (-e $archive_fullpath)
352 {
353 if ($split_into_objects)
354 {
355 my ($archive_file, $archive_dir, $archive_ext) = fileparse($archive_fullpath, ('.a'));
356
357 $object_dir = "$tmp_dir/$archive_file";
358 push @object_dirs, $object_dir;
359
360 do_command ("cd '$tmp_dir'; mkdir '$archive_file'; cd '$archive_file'; ar -x $archive_fullpath");
361
362 my @objects = bsd_glob("$object_dir/*.o");
363
364 foreach my $object (@objects)
365 {
366 my ($o_file, $o_dir) = fileparse($object);
367 my $new_object = "$object_dir/${archive_file}-$o_file";
368 print FILES "$new_object\n";
369 do_command ("mv '$object' '$new_object'");
370 }
371 }
372 else
373 {
374 # just add the .a files into the file list
375 print FILES "$archive_fullpath\n";
376 }
377 }
Greg Clayton193e6d52010-07-13 22:26:45 +0000378 else
379 {
380 print "warning: archive doesn't exist: '$archive_fullpath'\n";
381 }
Chris Lattner24943d22010-06-08 16:52:24 +0000382 }
383 close (FILES);
384 do_command ("libtool -static -o '$arch_output_file' -filelist '$files'");
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000385 do_command ("ranlib '$arch_output_file'");
Chris Lattner24943d22010-06-08 16:52:24 +0000386
387 foreach $object_dir (@object_dirs)
388 {
389 do_command ("rm -rf '$object_dir'");
390 }
391 do_command ("rm -rf '$files'");
392}
393
394build_llvm();