xref: /xnu-12377.81.4/tools/xcrun_cache.sh (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions#!/bin/sh
2*043036a2SApple OSS Distributions
3*043036a2SApple OSS Distributions#
4*043036a2SApple OSS Distributions# This shell-script is argument-compatible with xcrun(1) as invoked
5*043036a2SApple OSS Distributions# by xnu's Makefiles. Additionally, it supports caching tools
6*043036a2SApple OSS Distributions# in the local build directory. It is tightly coupled to exactly
7*043036a2SApple OSS Distributions# the queries that MakeInc.cmd makes, in exactly the order it makes
8*043036a2SApple OSS Distributions# them. ./tools/remote_build.sh invokes this indirectly in caching
9*043036a2SApple OSS Distributions# mode, so '$(XCRUN) -sdk foo -find bar' copies 'bar' from wherever
10*043036a2SApple OSS Distributions# it is on-disk into ./BUILD/BuildData, and returns that path to the
11*043036a2SApple OSS Distributions# caller. In '-u' mode on a remote build server, cache tools
12*043036a2SApple OSS Distributions# relative to the current build directory are returned without
13*043036a2SApple OSS Distributions# actually calling through to xcrun(1), since the remote build
14*043036a2SApple OSS Distributions# server may not even have Xcode installed.
15*043036a2SApple OSS Distributions#
16*043036a2SApple OSS Distributions
17*043036a2SApple OSS DistributionsSDKROOT=""
18*043036a2SApple OSS DistributionsFINDTOOL=""
19*043036a2SApple OSS DistributionsSDKQUERY=""
20*043036a2SApple OSS DistributionsVERBOSE=""
21*043036a2SApple OSS DistributionsOBJROOT=""
22*043036a2SApple OSS DistributionsCACHE=0
23*043036a2SApple OSS Distributions
24*043036a2SApple OSS Distributions# echo "Calling $0 $@" 1>&2
25*043036a2SApple OSS Distributions
26*043036a2SApple OSS Distributionswhile [ $# -gt 0 ]; do
27*043036a2SApple OSS Distributions    case "$1" in
28*043036a2SApple OSS Distributions	-c)
29*043036a2SApple OSS Distributions	    CACHE=1
30*043036a2SApple OSS Distributions	    shift
31*043036a2SApple OSS Distributions	    OBJROOT="$1"
32*043036a2SApple OSS Distributions	    shift
33*043036a2SApple OSS Distributions	    ;;
34*043036a2SApple OSS Distributions	-u)
35*043036a2SApple OSS Distributions	    CACHE=0
36*043036a2SApple OSS Distributions	    shift
37*043036a2SApple OSS Distributions	    OBJROOT="$1"
38*043036a2SApple OSS Distributions	    shift
39*043036a2SApple OSS Distributions	    ;;
40*043036a2SApple OSS Distributions	-sdk)
41*043036a2SApple OSS Distributions	    shift
42*043036a2SApple OSS Distributions	    SDKROOT="$1"
43*043036a2SApple OSS Distributions	    shift
44*043036a2SApple OSS Distributions	    ;;
45*043036a2SApple OSS Distributions	-verbose)
46*043036a2SApple OSS Distributions	    VERBOSE="$1"
47*043036a2SApple OSS Distributions	    shift
48*043036a2SApple OSS Distributions	    set -x
49*043036a2SApple OSS Distributions	    ;;
50*043036a2SApple OSS Distributions	-find)
51*043036a2SApple OSS Distributions	    shift
52*043036a2SApple OSS Distributions	    FINDTOOL="$1"
53*043036a2SApple OSS Distributions	    shift
54*043036a2SApple OSS Distributions	    ;;
55*043036a2SApple OSS Distributions	-show-sdk-path)
56*043036a2SApple OSS Distributions	    SDKQUERY="$1"
57*043036a2SApple OSS Distributions	    shift
58*043036a2SApple OSS Distributions	    ;;
59*043036a2SApple OSS Distributions	-show-sdk-platform-path)
60*043036a2SApple OSS Distributions	    SDKQUERY="$1"
61*043036a2SApple OSS Distributions	    shift
62*043036a2SApple OSS Distributions	    ;;
63*043036a2SApple OSS Distributions	-show-sdk-version)
64*043036a2SApple OSS Distributions	    SDKQUERY="$1"
65*043036a2SApple OSS Distributions	    shift
66*043036a2SApple OSS Distributions	    ;;
67*043036a2SApple OSS Distributions	*)
68*043036a2SApple OSS Distributions	    echo "Unrecognized argument $1" 1>&2
69*043036a2SApple OSS Distributions	    exit 1
70*043036a2SApple OSS Distributions    esac
71*043036a2SApple OSS Distributionsdone
72*043036a2SApple OSS Distributions
73*043036a2SApple OSS DistributionsCreateFile() {
74*043036a2SApple OSS Distributions    local string="$1"
75*043036a2SApple OSS Distributions    local filepath="$2"
76*043036a2SApple OSS Distributions    echo "${string}" > "${filepath}.new"
77*043036a2SApple OSS Distributions    cmp -s "${filepath}" "${filepath}.new"
78*043036a2SApple OSS Distributions    if [ $? -eq 0 ]; then
79*043036a2SApple OSS Distributions	rm "${filepath}.new"
80*043036a2SApple OSS Distributions    else
81*043036a2SApple OSS Distributions	mv "${filepath}.new" "${filepath}"
82*043036a2SApple OSS Distributions    fi
83*043036a2SApple OSS Distributions}
84*043036a2SApple OSS Distributions
85*043036a2SApple OSS Distributionsif [ $CACHE -eq 1 ]; then
86*043036a2SApple OSS Distributions
87*043036a2SApple OSS Distributions    if [ -n "$SDKQUERY" ]; then
88*043036a2SApple OSS Distributions	# MakeInc.cmd makes SDK queries up-front first. Generally the
89*043036a2SApple OSS Distributions	# SDKROOT that is an input to these are one of:
90*043036a2SApple OSS Distributions	# "macosx" => Host SDK
91*043036a2SApple OSS Distributions	# "iphonehostXXX" => iPhone Host SDK
92*043036a2SApple OSS Distributions	# other shortcut or full path => Target SDK
93*043036a2SApple OSS Distributions	#
94*043036a2SApple OSS Distributions	# Once an initial lookup is made, subsequent SDKROOTs for
95*043036a2SApple OSS Distributions	# that same SDK may use a full path or cached path
96*043036a2SApple OSS Distributions	SDKTYPE=""
97*043036a2SApple OSS Distributions	case "$SDKROOT" in
98*043036a2SApple OSS Distributions	    macosx)
99*043036a2SApple OSS Distributions		SDKTYPE="host"
100*043036a2SApple OSS Distributions		;;
101*043036a2SApple OSS Distributions	    iphonehost*)
102*043036a2SApple OSS Distributions		SDKTYPE="iphonehost"
103*043036a2SApple OSS Distributions		;;
104*043036a2SApple OSS Distributions	    *)
105*043036a2SApple OSS Distributions		if [ -f "$SDKROOT/.sdktype" ]; then
106*043036a2SApple OSS Distributions		    SDKTYPE=`cat "$SDKROOT/.sdktype"`
107*043036a2SApple OSS Distributions		else
108*043036a2SApple OSS Distributions		    SDKTYPE="target"
109*043036a2SApple OSS Distributions		fi
110*043036a2SApple OSS Distributions		;;
111*043036a2SApple OSS Distributions	esac
112*043036a2SApple OSS Distributions
113*043036a2SApple OSS Distributions	# A cached SDK path can be passed to xcrun, so
114*043036a2SApple OSS Distributions	# we need the original on-disk path
115*043036a2SApple OSS Distributions	if [ -f "$SDKROOT/.realsdkpath" ]; then
116*043036a2SApple OSS Distributions	    REALSDKROOT=`cat "$SDKROOT/.realsdkpath"`
117*043036a2SApple OSS Distributions	else
118*043036a2SApple OSS Distributions	    REALSDKROOT="$SDKROOT"
119*043036a2SApple OSS Distributions	fi
120*043036a2SApple OSS Distributions
121*043036a2SApple OSS Distributions	SDKPROPERTY=`/usr/bin/xcrun $VERBOSE -sdk "$REALSDKROOT" "$SDKQUERY"`
122*043036a2SApple OSS Distributions	if [ $? -ne 0 ]; then
123*043036a2SApple OSS Distributions	    exit $?
124*043036a2SApple OSS Distributions	fi
125*043036a2SApple OSS Distributions
126*043036a2SApple OSS Distributions	case $SDKQUERY in
127*043036a2SApple OSS Distributions	    -show-sdk-path)
128*043036a2SApple OSS Distributions		# Cache the SDK locally, and transform the resulting SDKPROPERTY
129*043036a2SApple OSS Distributions		if [ -z "$SDKPROPERTY" ]; then
130*043036a2SApple OSS Distributions		    SDKPROPERTY="/"
131*043036a2SApple OSS Distributions		    SDKNAME="Slash.sdk"
132*043036a2SApple OSS Distributions		else
133*043036a2SApple OSS Distributions		    SDKNAME=$(basename "${SDKPROPERTY}")
134*043036a2SApple OSS Distributions		fi
135*043036a2SApple OSS Distributions		mkdir -p "${OBJROOT}/BuildTools/${SDKNAME}"
136*043036a2SApple OSS Distributions		mkdir -p "${OBJROOT}/BuildTools/${SDKNAME}/usr/include"
137*043036a2SApple OSS Distributions		rsync -aq --exclude=c++ --exclude=php --exclude=soc "${SDKPROPERTY}/usr/include/" "${OBJROOT}/BuildTools/${SDKNAME}/usr/include/"
138*043036a2SApple OSS Distributions		if [ "$SDKTYPE" = "iphonehost" ]; then
139*043036a2SApple OSS Distributions		    mkdir -p "${OBJROOT}/BuildTools/${SDKNAME}/usr/local/lib/system"
140*043036a2SApple OSS Distributions		    rsync -aq "${SDKPROPERTY}/usr/local/lib/system/" "${OBJROOT}/BuildTools/${SDKNAME}/usr/local/lib/system/"
141*043036a2SApple OSS Distributions		else
142*043036a2SApple OSS Distributions		    mkdir -p "${OBJROOT}/BuildTools/${SDKNAME}/usr/lib"
143*043036a2SApple OSS Distributions		    rsync -aq "${SDKPROPERTY}/usr/lib/libSystem"* "${OBJROOT}/BuildTools/${SDKNAME}/usr/lib/"
144*043036a2SApple OSS Distributions		    rsync -aq "${SDKPROPERTY}/usr/lib/libc++"* "${OBJROOT}/BuildTools/${SDKNAME}/usr/lib/"
145*043036a2SApple OSS Distributions		    rsync -aq "${SDKPROPERTY}/usr/lib/libstdc++"* "${OBJROOT}/BuildTools/${SDKNAME}/usr/lib/"
146*043036a2SApple OSS Distributions		    mkdir -p "${OBJROOT}/BuildTools/${SDKNAME}/usr/lib/system"
147*043036a2SApple OSS Distributions		    rsync -aq --exclude=\*_debug.dylib --exclude=\*_profile.dylib "${SDKPROPERTY}/usr/lib/system/" "${OBJROOT}/BuildTools/${SDKNAME}/usr/lib/system/"
148*043036a2SApple OSS Distributions		fi
149*043036a2SApple OSS Distributions		if [ -f "${SDKPROPERTY}/usr/local/libexec/availability.pl" ]; then
150*043036a2SApple OSS Distributions		    mkdir -p "${OBJROOT}/BuildTools/${SDKNAME}/usr/local/libexec"
151*043036a2SApple OSS Distributions		    rsync -aq "${SDKPROPERTY}/usr/local/libexec/availability.pl" "${OBJROOT}/BuildTools/${SDKNAME}/usr/local/libexec/"
152*043036a2SApple OSS Distributions		fi
153*043036a2SApple OSS Distributions		CreateFile "${SDKPROPERTY}" "${OBJROOT}/BuildTools/${SDKNAME}/.realsdkpath"
154*043036a2SApple OSS Distributions		CreateFile "${SDKTYPE}" "${OBJROOT}/BuildTools/${SDKNAME}/.sdktype"
155*043036a2SApple OSS Distributions		CreateFile "BuildTools/${SDKNAME}" "${OBJROOT}/BuildTools/.${SDKTYPE}sdk"
156*043036a2SApple OSS Distributions		echo "${OBJROOT}/BuildTools/${SDKNAME}"
157*043036a2SApple OSS Distributions		exit 0
158*043036a2SApple OSS Distributions		;;
159*043036a2SApple OSS Distributions	    -show-sdk-platform-path)
160*043036a2SApple OSS Distributions		PLATFORMNAME=$(basename "${SDKPROPERTY}")
161*043036a2SApple OSS Distributions		mkdir -p "${OBJROOT}/BuildTools/${PLATFORMNAME}"
162*043036a2SApple OSS Distributions		if [ -f "${SDKPROPERTY}/usr/local/standalone/firmware/device_map.db" ]; then
163*043036a2SApple OSS Distributions		    mkdir -p "${OBJROOT}/BuildTools/${PLATFORMNAME}/usr/local/standalone/firmware"
164*043036a2SApple OSS Distributions		    rsync -aq "${SDKPROPERTY}/usr/local/standalone/firmware/device_map.db" \
165*043036a2SApple OSS Distributions			"${OBJROOT}/BuildTools/${PLATFORMNAME}/usr/local/standalone/firmware/"
166*043036a2SApple OSS Distributions		fi
167*043036a2SApple OSS Distributions		CreateFile "BuildTools/${PLATFORMNAME}" "${OBJROOT}/BuildTools/.targetplatform"
168*043036a2SApple OSS Distributions		echo "${OBJROOT}/BuildTools/${PLATFORMNAME}"
169*043036a2SApple OSS Distributions		exit 0
170*043036a2SApple OSS Distributions		;;
171*043036a2SApple OSS Distributions	    -show-sdk-version)
172*043036a2SApple OSS Distributions		CreateFile "${SDKPROPERTY}" "${OBJROOT}/BuildTools/.targetsdkversion"
173*043036a2SApple OSS Distributions		echo "${SDKPROPERTY}"
174*043036a2SApple OSS Distributions		exit 0
175*043036a2SApple OSS Distributions		;;
176*043036a2SApple OSS Distributions	esac
177*043036a2SApple OSS Distributions
178*043036a2SApple OSS Distributions    elif [ -n "$FINDTOOL" ]; then
179*043036a2SApple OSS Distributions
180*043036a2SApple OSS Distributions	# We assume SDK Queries have been performed first and subsequent
181*043036a2SApple OSS Distributions	# SDKROOTs used to find tools are all using cached SDKs in
182*043036a2SApple OSS Distributions	# the build directory, in which case metadata is present
183*043036a2SApple OSS Distributions
184*043036a2SApple OSS Distributions	if [ ! -f "$SDKROOT/.realsdkpath" ]; then
185*043036a2SApple OSS Distributions	    exit 1
186*043036a2SApple OSS Distributions	fi
187*043036a2SApple OSS Distributions	REALSDKROOT=`cat "$SDKROOT/.realsdkpath"`
188*043036a2SApple OSS Distributions
189*043036a2SApple OSS Distributions	if [ ! -f "$SDKROOT/.sdktype" ]; then
190*043036a2SApple OSS Distributions	    exit 1
191*043036a2SApple OSS Distributions	fi
192*043036a2SApple OSS Distributions	SDKTYPE=`cat "$SDKROOT/.sdktype"`
193*043036a2SApple OSS Distributions
194*043036a2SApple OSS Distributions	TOOLPATH=`/usr/bin/xcrun $VERBOSE -sdk "$REALSDKROOT" -find "$FINDTOOL"`
195*043036a2SApple OSS Distributions	if [ $? -ne 0 ]; then
196*043036a2SApple OSS Distributions	    exit $?
197*043036a2SApple OSS Distributions	fi
198*043036a2SApple OSS Distributions
199*043036a2SApple OSS Distributions	# Keep the parent directory when caching tools, along with Host vs. Target
200*043036a2SApple OSS Distributions	TOOLNAME=$(basename "${TOOLPATH}")
201*043036a2SApple OSS Distributions	TOOLDIR=$(basename $(dirname "${TOOLPATH}"))
202*043036a2SApple OSS Distributions	if [ "$SDKTYPE" = "host" ]; then
203*043036a2SApple OSS Distributions	    NEWTOOLPATH="${OBJROOT}/BuildTools/Host/${TOOLDIR}/${TOOLNAME}"
204*043036a2SApple OSS Distributions	    mkdir -p "${OBJROOT}/BuildTools/Host"
205*043036a2SApple OSS Distributions	    CreateFile "BuildTools/Host/${TOOLDIR}/${TOOLNAME}" "${OBJROOT}/BuildTools/Host/.${TOOLNAME}"
206*043036a2SApple OSS Distributions	else
207*043036a2SApple OSS Distributions	    NEWTOOLPATH="${OBJROOT}/BuildTools/Target/${TOOLDIR}/${TOOLNAME}"
208*043036a2SApple OSS Distributions	    mkdir -p "${OBJROOT}/BuildTools/Target"
209*043036a2SApple OSS Distributions	    CreateFile "BuildTools/Target/${TOOLDIR}/${TOOLNAME}" "${OBJROOT}/BuildTools/Target/.${TOOLNAME}"
210*043036a2SApple OSS Distributions	fi
211*043036a2SApple OSS Distributions	mkdir -p $(dirname "${NEWTOOLPATH}")
212*043036a2SApple OSS Distributions	rsync -aq "${TOOLPATH}" "${NEWTOOLPATH}"
213*043036a2SApple OSS Distributions	case "${TOOLNAME}" in
214*043036a2SApple OSS Distributions	    clang)
215*043036a2SApple OSS Distributions		mkdir -p $(dirname $(dirname "${NEWTOOLPATH}"))/lib/clang
216*043036a2SApple OSS Distributions		rsync -aq $(dirname "${TOOLPATH}")/ld $(dirname "${NEWTOOLPATH}")/ld
217*043036a2SApple OSS Distributions		rsync -aq $(dirname $(dirname "${TOOLPATH}"))/lib/clang/ $(dirname $(dirname "${NEWTOOLPATH}"))/lib/clang/
218*043036a2SApple OSS Distributions		rsync -aq $(dirname $(dirname "${TOOLPATH}"))/lib/libLTO.dylib $(dirname $(dirname "${NEWTOOLPATH}"))/lib/libLTO.dylib
219*043036a2SApple OSS Distributions		;;
220*043036a2SApple OSS Distributions	    bison)
221*043036a2SApple OSS Distributions		mkdir -p $(dirname $(dirname "${NEWTOOLPATH}"))/share/bison
222*043036a2SApple OSS Distributions		rsync -aq $(dirname $(dirname "${TOOLPATH}"))/share/bison/ $(dirname $(dirname "${NEWTOOLPATH}"))/share/bison/
223*043036a2SApple OSS Distributions		;;
224*043036a2SApple OSS Distributions	esac
225*043036a2SApple OSS Distributions
226*043036a2SApple OSS Distributions	echo "${NEWTOOLPATH}"
227*043036a2SApple OSS Distributions	exit 0
228*043036a2SApple OSS Distributions    else
229*043036a2SApple OSS Distributions	echo "Unrecognized option" 1>&2
230*043036a2SApple OSS Distributions	exit 1
231*043036a2SApple OSS Distributions    fi
232*043036a2SApple OSS Distributionsfi
233*043036a2SApple OSS Distributions
234*043036a2SApple OSS Distributions# When using cached SDK information, first try to do
235*043036a2SApple OSS Distributions# an initial classification, and then read properties from
236*043036a2SApple OSS Distributions# cached locations
237*043036a2SApple OSS DistributionsSDKTYPE=""
238*043036a2SApple OSS Distributionscase "$SDKROOT" in
239*043036a2SApple OSS Distributions    macosx)
240*043036a2SApple OSS Distributions	SDKTYPE="host"
241*043036a2SApple OSS Distributions	;;
242*043036a2SApple OSS Distributions    iphonehost*)
243*043036a2SApple OSS Distributions	SDKTYPE="iphonehost"
244*043036a2SApple OSS Distributions	;;
245*043036a2SApple OSS Distributions    *)
246*043036a2SApple OSS Distributions	if [ -f "$SDKROOT/.sdktype" ]; then
247*043036a2SApple OSS Distributions	    SDKTYPE=`cat "$SDKROOT/.sdktype"`
248*043036a2SApple OSS Distributions	else
249*043036a2SApple OSS Distributions	    SDKTYPE="target"
250*043036a2SApple OSS Distributions	fi
251*043036a2SApple OSS Distributions	;;
252*043036a2SApple OSS Distributionsesac
253*043036a2SApple OSS Distributions
254*043036a2SApple OSS Distributionsif [ -n "$FINDTOOL" ]; then
255*043036a2SApple OSS Distributions    TOOLNAME=$(basename "${FINDTOOL}")
256*043036a2SApple OSS Distributions    if [ "${SDKTYPE}" = "host" ]; then
257*043036a2SApple OSS Distributions	RELPATH=`cat ${OBJROOT}/BuildTools/Host/.${TOOLNAME}`
258*043036a2SApple OSS Distributions    else
259*043036a2SApple OSS Distributions	RELPATH=`cat ${OBJROOT}/BuildTools/Target/.${TOOLNAME}`
260*043036a2SApple OSS Distributions    fi
261*043036a2SApple OSS Distributions    echo "${OBJROOT}/${RELPATH}"
262*043036a2SApple OSS Distributionselse
263*043036a2SApple OSS Distributions    case $SDKQUERY in
264*043036a2SApple OSS Distributions	-show-sdk-path)
265*043036a2SApple OSS Distributions	    RELPATH=`cat ${OBJROOT}/BuildTools/.${SDKTYPE}sdk`
266*043036a2SApple OSS Distributions	    echo "${OBJROOT}/${RELPATH}"
267*043036a2SApple OSS Distributions	    ;;
268*043036a2SApple OSS Distributions	-show-sdk-platform-path)
269*043036a2SApple OSS Distributions	    RELPATH=`cat ${OBJROOT}/BuildTools/.targetplatform`
270*043036a2SApple OSS Distributions	    echo "${OBJROOT}/${RELPATH}"
271*043036a2SApple OSS Distributions	    ;;
272*043036a2SApple OSS Distributions	-show-sdk-version)
273*043036a2SApple OSS Distributions	    echo `cat ${OBJROOT}/BuildTools/.targetsdkversion`
274*043036a2SApple OSS Distributions	    ;;
275*043036a2SApple OSS Distributions    esac
276*043036a2SApple OSS Distributionsfi
277