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