1*43a90889SApple OSS Distributions#!/usr/bin/perl 2*43a90889SApple OSS Distributions# 3*43a90889SApple OSS Distributions# Copyright (c) 2010 Apple Inc. All rights reserved. 4*43a90889SApple OSS Distributions# 5*43a90889SApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 6*43a90889SApple OSS Distributions# 7*43a90889SApple OSS Distributions# This file contains Original Code and/or Modifications of Original Code 8*43a90889SApple OSS Distributions# as defined in and that are subject to the Apple Public Source License 9*43a90889SApple OSS Distributions# Version 2.0 (the 'License'). You may not use this file except in 10*43a90889SApple OSS Distributions# compliance with the License. The rights granted to you under the License 11*43a90889SApple OSS Distributions# may not be used to create, or enable the creation or redistribution of, 12*43a90889SApple OSS Distributions# unlawful or unlicensed copies of an Apple operating system, or to 13*43a90889SApple OSS Distributions# circumvent, violate, or enable the circumvention or violation of, any 14*43a90889SApple OSS Distributions# terms of an Apple operating system software license agreement. 15*43a90889SApple OSS Distributions# 16*43a90889SApple OSS Distributions# Please obtain a copy of the License at 17*43a90889SApple OSS Distributions# http://www.opensource.apple.com/apsl/ and read it before using this file. 18*43a90889SApple OSS Distributions# 19*43a90889SApple OSS Distributions# The Original Code and all software distributed under the License are 20*43a90889SApple OSS Distributions# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 21*43a90889SApple OSS Distributions# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 22*43a90889SApple OSS Distributions# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 23*43a90889SApple OSS Distributions# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 24*43a90889SApple OSS Distributions# Please see the License for the specific language governing rights and 25*43a90889SApple OSS Distributions# limitations under the License. 26*43a90889SApple OSS Distributions# 27*43a90889SApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 28*43a90889SApple OSS Distributions# 29*43a90889SApple OSS Distributions 30*43a90889SApple OSS Distributionsuse warnings; 31*43a90889SApple OSS Distributionsuse strict; 32*43a90889SApple OSS Distributions 33*43a90889SApple OSS Distributionsuse Data::Dumper; 34*43a90889SApple OSS Distributionsuse File::Spec; 35*43a90889SApple OSS Distributionsuse IO::File; 36*43a90889SApple OSS Distributionsuse File::Basename (); 37*43a90889SApple OSS Distributions 38*43a90889SApple OSS Distributionsmy $basename = File::Basename::basename($0); 39*43a90889SApple OSS Distributions 40*43a90889SApple OSS Distributionssub usage { 41*43a90889SApple OSS Distributions print "$basename: <source list> <output archive>"; 42*43a90889SApple OSS Distributions exit 1; 43*43a90889SApple OSS Distributions} 44*43a90889SApple OSS Distributions 45*43a90889SApple OSS Distributionsusage unless scalar(@ARGV) == 2; 46*43a90889SApple OSS Distributions 47*43a90889SApple OSS Distributionsmy $sourceList = $ARGV[0]; 48*43a90889SApple OSS Distributionsmy $outputFile = $ARGV[1]; 49*43a90889SApple OSS Distributions 50*43a90889SApple OSS Distributionsmy $f = IO::File->new($sourceList, 'r'); 51*43a90889SApple OSS Distributionsdie "$basename: $sourceList: $!\n" unless defined($f); 52*43a90889SApple OSS Distributions 53*43a90889SApple OSS Distributionsmy @objects; 54*43a90889SApple OSS Distributionsmy @archs = split / /, $ENV{"ARCHS"}; 55*43a90889SApple OSS Distributionsmy @sources = <$f>; 56*43a90889SApple OSS Distributionschomp @sources; 57*43a90889SApple OSS Distributions 58*43a90889SApple OSS Distributionsundef $f; 59*43a90889SApple OSS Distributions 60*43a90889SApple OSS Distributions# compiler options 61*43a90889SApple OSS Distributionschomp(my $CC = `xcrun -sdk "$ENV{'SDKROOT'}" -find cc`); 62*43a90889SApple OSS Distributionsmy @CFLAGS = ( 63*43a90889SApple OSS Distributions "-x assembler-with-cpp", 64*43a90889SApple OSS Distributions "-c", 65*43a90889SApple OSS Distributions "-isysroot", $ENV{'SDKROOT'} || "/", 66*43a90889SApple OSS Distributions "-I".$ENV{"SDKROOT"}."/".$ENV{"SDK_INSTALL_HEADERS_ROOT"}."/usr/include", 67*43a90889SApple OSS Distributions "-I".$ENV{"SDKROOT"}."/".$ENV{"SDK_INSTALL_HEADERS_ROOT"}."/usr/local/include", 68*43a90889SApple OSS Distributions "-I".$ENV{"SDKROOT"}."/".$ENV{"SDK_INSTALL_HEADERS_ROOT"}."/System/Library/Frameworks/System.framework/PrivateHeaders", 69*43a90889SApple OSS Distributions); 70*43a90889SApple OSS Distributions 71*43a90889SApple OSS Distributionschomp(my $LIBTOOL = `xcrun -sdk "$ENV{'SDKROOT'}" -find libtool`); 72*43a90889SApple OSS Distributionsmy @LIBTOOLFLAGS = ( 73*43a90889SApple OSS Distributions "-static", 74*43a90889SApple OSS Distributions); 75*43a90889SApple OSS Distributions 76*43a90889SApple OSS Distributions# architectures 77*43a90889SApple OSS Distributionsfor my $arch (@archs) { 78*43a90889SApple OSS Distributions push(@CFLAGS, "-arch $arch"); 79*43a90889SApple OSS Distributions} 80*43a90889SApple OSS Distributions 81*43a90889SApple OSS Distributions# do each compile 82*43a90889SApple OSS Distributionsmy $jobs = `sysctl -n hw.ncpu` + 2; 83*43a90889SApple OSS Distributions 84*43a90889SApple OSS Distributionsfor my $src (@sources) { 85*43a90889SApple OSS Distributions if ($jobs == 0) { 86*43a90889SApple OSS Distributions if (wait != -1) { 87*43a90889SApple OSS Distributions $jobs++; 88*43a90889SApple OSS Distributions } else { 89*43a90889SApple OSS Distributions printf "wait exited with -1 (no children) and exhausted allowed jobs. Exiting.\n"; 90*43a90889SApple OSS Distributions exit 1; 91*43a90889SApple OSS Distributions } 92*43a90889SApple OSS Distributions 93*43a90889SApple OSS Distributions if ($? != 0) { 94*43a90889SApple OSS Distributions printf "$CC exited with value %d\n", $? >> 8; 95*43a90889SApple OSS Distributions exit 1; 96*43a90889SApple OSS Distributions } 97*43a90889SApple OSS Distributions } 98*43a90889SApple OSS Distributions 99*43a90889SApple OSS Distributions (my $o = $src) =~ s/\.s$/\.o/; 100*43a90889SApple OSS Distributions my $compileCommand = "$CC " . join(' ', @CFLAGS) . " -o $o $src"; 101*43a90889SApple OSS Distributions printf $compileCommand . "\n"; 102*43a90889SApple OSS Distributions 103*43a90889SApple OSS Distributions $jobs--; 104*43a90889SApple OSS Distributions my $pid = fork(); 105*43a90889SApple OSS Distributions if ($pid == 0) { 106*43a90889SApple OSS Distributions exec($compileCommand); 107*43a90889SApple OSS Distributions } 108*43a90889SApple OSS Distributions push(@objects, $o); 109*43a90889SApple OSS Distributions} 110*43a90889SApple OSS Distributions 111*43a90889SApple OSS Distributionswhile (wait != -1) { 112*43a90889SApple OSS Distributions if ($? != 0) { 113*43a90889SApple OSS Distributions printf "$CC exited with value %d\n", $? >> 8; 114*43a90889SApple OSS Distributions exit 1; 115*43a90889SApple OSS Distributions } 116*43a90889SApple OSS Distributions} 117*43a90889SApple OSS Distributions 118*43a90889SApple OSS Distributionsprintf "Finished assembly, beginning link.\n"; 119*43a90889SApple OSS Distributions 120*43a90889SApple OSS Distributions# final link 121*43a90889SApple OSS Distributions 122*43a90889SApple OSS Distributionsif (-f $outputFile) { 123*43a90889SApple OSS Distributions unlink($outputFile); 124*43a90889SApple OSS Distributions} 125*43a90889SApple OSS Distributions 126*43a90889SApple OSS Distributionsmy $linkCommand = "$LIBTOOL " . join(' ', @LIBTOOLFLAGS) . " -o $outputFile " . join(' ', @objects); 127*43a90889SApple OSS Distributions 128*43a90889SApple OSS Distributionsprintf $linkCommand . "\n"; 129*43a90889SApple OSS Distributionssystem($linkCommand); 130*43a90889SApple OSS Distributionsif ($? != 0) { 131*43a90889SApple OSS Distributions print "$LIBTOOL exited with value %d\n", $? >> 8; 132*43a90889SApple OSS Distributions exit 1; 133*43a90889SApple OSS Distributions} 134