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