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