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