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