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