3 # Copyright (C) 2005, 2008, 2010, 2011, 2012, 2013, 2014 Apple Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 use Getopt::Long qw(:config pass_through);
30 use lib $FindBin::Bin;
36 my $llvmIncludePackage = "";
37 my $llvmLibraryPackage = "";
38 my $useFullLibPaths = 0;
39 my $preferSystemLLVMOverDrops = 0;
40 my $llvmSubdirectoryName = "llvm";
41 my $llvmPrefix = "/usr/local/LLVMForJavaScriptCore";
42 my $sdkName = ""; # Ideally we only use this for build commands, rather than deciding policies about what needs to get copied or built and where it needs to be placed.
46 my $programName = basename($0);
48 Usage: $programName [options]
49 --help Show this help message
50 --[no-]llvm Toggle copying LLVM drops (default: $llvm)
51 --[no-]wksi Toggle copying WebKitSystemInterface drops (default: $wksi)
52 --use-llvm-includes=<path> Get the LLVM inludes package from <path>
53 --use-llvm-libraries=<path> Get the LLVM libraries package from <path>
54 --[no-]use-full-lib-paths Toggle using full library paths
55 --[no-]prefer-system-llvm Toggle preferring the system LLVM over the binary drops (default: $preferSystemLLVMOverDrops)
56 --llvm-subdirectory=<name> Set the name of the LLVM subdirectory to search for (default: $llvmSubdirectoryName)
57 --llvm-prefix=<path> Set the prefix into which LLVM is installed (default: $llvmPrefix)
58 --sdk-name=<name> Set the name of the Xcode SDK to use.
59 --osx-version=<version> Set the OS X version to use when deciding what to copy.
60 --[no-]force Toggle forcing the copy - i.e. ignoring timestamps (default: $force)
67 'use-llvm-includes=s' => \$llvmIncludePackage,
68 'use-llvm-libraries=s' => \$llvmLibraryPackage,
69 'use-full-lib-paths!' => \$useFullLibPaths,
70 'prefer-system-llvm!' => \$preferSystemLLVMOverDrops,
71 'llvm-subdirectory=s' => \$llvmSubdirectoryName,
72 'llvm-prefix=s' => \$llvmPrefix,
73 'sdk-name=s' => \$sdkName,
74 'osx-version=s' => \$osxVersion,
83 my $productDir = shift @ARGV;
85 $productDir = File::Spec->rel2abs($productDir);
87 $productDir = $ENV{BUILT_PRODUCTS_DIR} || productDir();
91 $osxVersion = `sw_vers -productVersion | cut -d. -f-2`;
97 my $xcrunOptions = "";
99 $xcrunOptions .= " -sdk $sdkName";
101 my $ranlib = `xcrun $xcrunOptions -find ranlib`;
104 sub unpackIfNecessary
106 my ($targetDir, $sampleFile, $package, $hasLibraries) = @_;
107 if ($force || !-e $sampleFile || -M $sampleFile > -M $package) {
108 print "Unpacking $package into $targetDir\n";
109 (system("tar -C $targetDir -xmf $package") == 0) or die;
111 foreach my $library (`tar -tf $package`) {
113 print " Ranlib $library\n";
114 (system($ranlib, $targetDir . "/" . $library) == 0) or die;
124 my ($srcHeader, $header) = @_;
125 if ($force || !-e $header || -M $header > -M $srcHeader) {
126 print "Updating $header\n";
127 (system("ditto", $srcHeader, $header) == 0) or die;
132 (system("mkdir", "-p", "$productDir/usr/local/include") == 0) or die;
134 my $libraryDir = $useFullLibPaths ? "$productDir/usr/local/lib" : $productDir;
135 (system("mkdir", "-p", $libraryDir) == 0) or die;
137 my @librariesToCopy = (
138 "libWebKitSystemInterfaceMountainLion.a",
139 "libWebKitSystemInterfaceMavericks.a",
142 foreach my $libName (@librariesToCopy) {
143 my $srcLib = "WebKitLibraries/" . $libName;
144 my $lib = "$libraryDir/" . $libName;
145 if ($force || !-e $lib || -M $lib > -M $srcLib) {
146 print "Updating $lib\n";
147 (system("ditto", $srcLib, $lib) == 0) or die;
148 (system($ranlib, $lib) == 0) or die;
152 dittoHeaders("WebKitLibraries/WebKitSystemInterface.h", "$productDir/usr/local/include/WebKitSystemInterface.h");
156 (system("mkdir", "-p", "$productDir$llvmPrefix/include") == 0) or die;
158 my $libraryDir = $useFullLibPaths ? "$productDir$llvmPrefix/lib" : $productDir;
159 (system("mkdir", "-p", $libraryDir) == 0) or die;
161 # Determine where to get LLVM binaries and headers.
163 my $ownLLVMDirectory;
164 if (defined($ENV{LLVM_SOURCE_PATH})) {
165 print "Using LLVM from \$LLVM_SOURE_PATH: " . $ENV{LLVM_SOURCE_PATH} . "\n";
167 $ownLLVMDirectory = $ENV{LLVM_SOURCE_PATH};
168 } elsif (-d $llvmSubdirectoryName && -e "$llvmSubdirectoryName/LLVMBuild.txt") {
169 print "Using LLVM from $llvmSubdirectoryName subdirectory.\n";
171 $ownLLVMDirectory = sourceDir() . "/$llvmSubdirectoryName";
172 } elsif ($llvmLibraryPackage ne "" && $llvmIncludePackage ne "") {
173 # Command-line arguments override our other ways of finding the packages.
174 print "Using LLVM binary drops specified on command-line: $llvmLibraryPackage and $llvmIncludePackage.\n";
175 } elsif (defined($ENV{LLVM_LIBRARY_PACKAGE}) && defined($ENV{LLVM_INCLUDE_PACKAGE})) {
176 $llvmLibraryPackage = $ENV{LLVM_LIBRARY_PACKAGE};
177 $llvmIncludePackage = $ENV{LLVM_INCLUDE_PACKAGE};
178 print "Using LLVM binary drops specified by \$LLVM_LIBRARY_PACKAGE and \$LLVM_INCLUDE_PACKAGE: $llvmLibraryPackage and $llvmIncludePackage.\n";
179 } elsif ($preferSystemLLVMOverDrops) {
180 # Don't fall through to drop detection.
181 print "Using system LLVM.\n";
182 } elsif ($osxVersion eq "10.8") {
183 $llvmLibraryPackage = "WebKitLibraries/LLVMLibrariesMountainLion.tar.bz2";
184 $llvmIncludePackage = "WebKitLibraries/LLVMIncludesMountainLion.tar.bz2";
185 } elsif ($osxVersion eq "10.9") {
186 $llvmLibraryPackage = "WebKitLibraries/LLVMLibrariesMavericks.tar.bz2";
187 $llvmIncludePackage = "WebKitLibraries/LLVMIncludesMavericks.tar.bz2";
189 print "Don't know where to find LLVM!\n";
191 print "Try defining LLVM_LIBRARY_PACKAGE and LLVM_INCLUDE_PACKAGE or setting the\n";
192 print "--use-llvm-includes and --use-llvm-libraries options.\n";
194 print "Alternatively, you can check out llvm trunk into the WebKit directory:\n";
195 print "svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm\n";
201 my ($filename, $string) = @_;
202 open my $fileHandle, '<', $filename or die;
203 while (<$fileHandle>) {
204 return 1 if /^$string$/;
209 sub fileContentsEquals
211 my ($filename, $string) = @_;
212 open my $fileHandle, '<', $filename or die;
214 my $contents = <$fileHandle>;
215 return $contents eq $string;
218 my $shouldUpdateLLVMLibraryToken = 0;
221 if (!-e "$ownLLVMDirectory/wkLLVMBuild/Makefile.config") {
222 print("Configuring LLVM.\n");
223 (system("mkdir -p $ownLLVMDirectory/wkLLVMBuild"));
224 my $flags = "--enable-optimized=yes --enable-backtraces=no --enable-targets=x86_64 --enable-libcpp=yes --enable-zlib=no --enable-terminfo=no --enable-crash-overrides=no";
225 (system("(cd $ownLLVMDirectory/wkLLVMBuild && ../configure $flags)") == 0) or die;
228 print("Building LLVM.\n");
229 my $oldPath = $ENV{"PATH"};
230 chdir "$ownLLVMDirectory/wkLLVMBuild";
231 my $binariesDirectory = "binariesForLLVMBuild";
232 my $pathCommand = "";
233 if (-e $binariesDirectory) {
234 my $binariesPath = File::Spec->rel2abs($binariesDirectory);
235 print "Detected binaries directory; prepending to path: $binariesPath\n";
236 $pathCommand = "PATH=\\\$PWD/$binariesDirectory:\\\$PATH";
238 my $makeCommand = "env -i bash -l -c \"$pathCommand make -j `sysctl -n hw.activecpu`\"";
239 print $makeCommand . "\n";
240 (system($makeCommand) == 0) or die;
241 $ENV{"PATH"} = $oldPath;
244 my $ownLLVMBuildMode = "";
245 if (fileContains($ownLLVMDirectory . "/wkLLVMBuild/Makefile.config", "ENABLE_OPTIMIZED=1")) {
246 $ownLLVMBuildMode .= "Release";
248 $ownLLVMBuildMode .= "Debug";
251 if (fileContains($ownLLVMDirectory . "/wkLLVMBuild/Makefile.config", "DISABLE_ASSERTIONS=1")) {
254 $ownLLVMBuildMode .= "+Asserts";
257 my $librarySourceDirectory = "$ownLLVMDirectory/wkLLVMBuild/$ownLLVMBuildMode/lib";
258 my $libraryTargetDirectory = $libraryDir;
259 $shouldUpdateLLVMLibraryToken = 0;
260 print("Symlinking libraries from $librarySourceDirectory to $libraryTargetDirectory\n");
261 opendir (my $dirHandle, $librarySourceDirectory);
262 while (my $filename = readdir($dirHandle)) {
263 next if $filename !~ /\.a$/;
264 next if $filename =~ /libgtest/;
265 print " Symlink $filename\n";
266 my $sourceLibrary = "$librarySourceDirectory/$filename";
267 my $targetLibrary = "$libraryTargetDirectory/$filename";
268 my $ranlibToken = "$libraryTargetDirectory/.ranlibToken-$filename";
269 unlink($targetLibrary);
270 symlink($sourceLibrary, $targetLibrary);
273 || !fileContentsEquals($ranlibToken, $sourceLibrary)
274 || -M $ranlibToken > -M $sourceLibrary) {
275 print " Ranlib $filename\n";
276 (system($ranlib, $targetLibrary) == 0) or die;
277 (open my $fileHandle, ">", $ranlibToken) or die;
278 print {$fileHandle} "$sourceLibrary";
280 $shouldUpdateLLVMLibraryToken = 1;
284 } elsif (!$preferSystemLLVMOverDrops) {
285 $shouldUpdateLLVMLibraryToken =
286 unpackIfNecessary($libraryDir, "$libraryDir/libLLVMCore.a", $llvmLibraryPackage, 1);
289 (system("rm", "-f", "$productDir/ExtraIncludesForLocalLLVMBuild") == 0) or die;
291 (system("rm", "-rf", "$productDir$llvmPrefix/include/llvm") == 0) or die;
292 (system("rm", "-rf", "$productDir$llvmPrefix/include/llvm-c") == 0) or die;
293 symlink("$ownLLVMDirectory/include/llvm", "$productDir$llvmPrefix/include/llvm") or die;
294 symlink("$ownLLVMDirectory/include/llvm-c", "$productDir$llvmPrefix/include/llvm-c") or die;
295 symlink("$ownLLVMDirectory/wkLLVMBuild/include", "$productDir/ExtraIncludesForLocalLLVMBuild") or die;
296 } elsif (!$preferSystemLLVMOverDrops) {
297 unpackIfNecessary("$productDir$llvmPrefix/include", "$productDir$llvmPrefix/include/llvm-c/Core.h", $llvmIncludePackage, 0);
300 if ($shouldUpdateLLVMLibraryToken) {
301 (system("touch", "Source/JavaScriptCore/llvm/library/LLVMAnchor.cpp") == 0) or die;