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