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