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