xref: /xnu-8792.61.2/tools/remote_build.sh (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1*42e22086SApple OSS Distributions#!/bin/bash
2*42e22086SApple OSS Distributions#
3*42e22086SApple OSS Distributions# Script that rsyncs a source/build tree to a remote server, performs a build,
4*42e22086SApple OSS Distributions# and copies the result back
5*42e22086SApple OSS Distributions#
6*42e22086SApple OSS Distributions
7*42e22086SApple OSS Distributions# This script is invoked instead of the initial recursive make(1) in ./Makefile.
8*42e22086SApple OSS Distributions# First it must cache all binaries that might be used during the build by
9*42e22086SApple OSS Distributions# calling "make print_exports" (any target would work) with an overriden xcrun(1)
10*42e22086SApple OSS Distributions# which caches tools an SDKs into ./BUILD/obj/BuildTools. When the combined
11*42e22086SApple OSS Distributions# source+build tree is rsync-ed to the remote server, we run a script to
12*42e22086SApple OSS Distributions# re-initiate the build using an overriden xcrun(1) which hands back
13*42e22086SApple OSS Distributions# cached tools in ./BUILD/obj/BuildTools instead of whatever Xcode tools are on
14*42e22086SApple OSS Distributions# the remote system (or if no Xcode tools are installed remotely). Finally,
15*42e22086SApple OSS Distributions# the build results are copied back locally.
16*42e22086SApple OSS Distributions#
17*42e22086SApple OSS Distributions
18*42e22086SApple OSS Distributionsfunction die() {
19*42e22086SApple OSS Distributions    echo "$1" 1>&2
20*42e22086SApple OSS Distributions    exit 1
21*42e22086SApple OSS Distributions}
22*42e22086SApple OSS Distributions
23*42e22086SApple OSS Distributions
24*42e22086SApple OSS DistributionsTARGET=
25*42e22086SApple OSS Distributionsdeclare -a ARGS
26*42e22086SApple OSS Distributionsdeclare -a REMOTEARGS
27*42e22086SApple OSS Distributionsindex=0
28*42e22086SApple OSS Distributionsfor arg in "$@"; do
29*42e22086SApple OSS Distributions    case $arg in
30*42e22086SApple OSS Distributions	_REMOTEBUILD_TARGET=*)
31*42e22086SApple OSS Distributions	    TARGET=`echo $arg | awk -F= '{print $2}'`
32*42e22086SApple OSS Distributions	    continue
33*42e22086SApple OSS Distributions	    ;;
34*42e22086SApple OSS Distributions	_REMOTEBUILD_MAKE=*)
35*42e22086SApple OSS Distributions	    MAKE=`echo $arg | awk -F= '{print $2}'`
36*42e22086SApple OSS Distributions	    continue
37*42e22086SApple OSS Distributions	    ;;
38*42e22086SApple OSS Distributions	REMOTEBUILD=*)
39*42e22086SApple OSS Distributions	    # Don't restart another remote build remotely
40*42e22086SApple OSS Distributions	    ;;
41*42e22086SApple OSS Distributions	SRCROOT=*)
42*42e22086SApple OSS Distributions	    continue
43*42e22086SApple OSS Distributions	    ;;
44*42e22086SApple OSS Distributions	OBJROOT=*)
45*42e22086SApple OSS Distributions	    continue
46*42e22086SApple OSS Distributions	    ;;
47*42e22086SApple OSS Distributions	SYMROOT=*)
48*42e22086SApple OSS Distributions	    continue
49*42e22086SApple OSS Distributions	    ;;
50*42e22086SApple OSS Distributions	DSTROOT=*)
51*42e22086SApple OSS Distributions	    continue
52*42e22086SApple OSS Distributions	    ;;
53*42e22086SApple OSS Distributions	CCHROOT=*)
54*42e22086SApple OSS Distributions	    continue
55*42e22086SApple OSS Distributions	    ;;
56*42e22086SApple OSS Distributions	RC_XBS=*)
57*42e22086SApple OSS Distributions	    # Remote build isn't chrooted or special in any way
58*42e22086SApple OSS Distributions	    arg="VERBOSE=YES"
59*42e22086SApple OSS Distributions	    continue
60*42e22086SApple OSS Distributions	    ;;
61*42e22086SApple OSS Distributions	VERBOSE=YES)
62*42e22086SApple OSS Distributions	    set -x
63*42e22086SApple OSS Distributions	    ;;
64*42e22086SApple OSS Distributions    esac
65*42e22086SApple OSS Distributions    ARGS[$index]="$arg"
66*42e22086SApple OSS Distributions    REMOTEARGS[$index]="\"$arg\""
67*42e22086SApple OSS Distributions    index=$(($index+1))
68*42e22086SApple OSS Distributionsdone
69*42e22086SApple OSS Distributions
70*42e22086SApple OSS Distributions
71*42e22086SApple OSS DistributionsRSYNC_ARGS="-azvh"
72*42e22086SApple OSS DistributionsARGS[$index]="REMOTEBUILD="
73*42e22086SApple OSS DistributionsREMOTEARGS[$index]="\"REMOTEBUILD=\""
74*42e22086SApple OSS Distributions
75*42e22086SApple OSS Distributions# For some targets like installsrc, we can't to a remote build
76*42e22086SApple OSS DistributionsSKIPREMOTE=0
77*42e22086SApple OSS Distributionscase $TARGET in
78*42e22086SApple OSS Distributions    clean)
79*42e22086SApple OSS Distributions	SKIPREMOTE=1
80*42e22086SApple OSS Distributions	;;
81*42e22086SApple OSS Distributions    installsrc)
82*42e22086SApple OSS Distributions	SKIPREMOTE=1
83*42e22086SApple OSS Distributions	;;
84*42e22086SApple OSS Distributions    installopensource)
85*42e22086SApple OSS Distributions	SKIPREMOTE=1
86*42e22086SApple OSS Distributions	;;
87*42e22086SApple OSS Distributions    cscope)
88*42e22086SApple OSS Distributions	SKIPREMOTE=1
89*42e22086SApple OSS Distributions	;;
90*42e22086SApple OSS Distributions    tags)
91*42e22086SApple OSS Distributions	SKIPREMOTE=1
92*42e22086SApple OSS Distributions	;;
93*42e22086SApple OSS Distributions    help)
94*42e22086SApple OSS Distributions	SKIPREMOTE=1
95*42e22086SApple OSS Distributions	;;
96*42e22086SApple OSS Distributionsesac
97*42e22086SApple OSS Distributions
98*42e22086SApple OSS Distributionsif [ $SKIPREMOTE -eq 1 ]; then
99*42e22086SApple OSS Distributions    exec "$MAKE" "$TARGET" "${ARGS[@]}"
100*42e22086SApple OSS Distributionsfi
101*42e22086SApple OSS Distributions
102*42e22086SApple OSS DistributionsSRC="$(pwd -P)"
103*42e22086SApple OSS DistributionsSRCNAME="$(basename $SRC)"
104*42e22086SApple OSS Distributions
105*42e22086SApple OSS Distributions# Pick up build locations passed in the environment
106*42e22086SApple OSS DistributionsOBJROOT="${OBJROOT}"
107*42e22086SApple OSS DistributionsSYMROOT="${SYMROOT}"
108*42e22086SApple OSS DistributionsDSTROOT="${DSTROOT}"
109*42e22086SApple OSS Distributions
110*42e22086SApple OSS Distributionsif [ -z "${OBJROOT}" ]; then
111*42e22086SApple OSS Distributions    die "OBJROOT not set in environment"
112*42e22086SApple OSS Distributionsfi
113*42e22086SApple OSS Distributionsmkdir -p "${OBJROOT}" || die "Could not create ${OBJROOT}"
114*42e22086SApple OSS Distributions
115*42e22086SApple OSS Distributionsif [ -z "${SYMROOT}" ]; then
116*42e22086SApple OSS Distributions    die "SYMROOT not set in environment"
117*42e22086SApple OSS Distributionsfi
118*42e22086SApple OSS Distributionsmkdir -p "${SYMROOT}" || die "Could not create ${SYMROOT}"
119*42e22086SApple OSS Distributions
120*42e22086SApple OSS Distributionsif [ -z "${DSTROOT}" ]; then
121*42e22086SApple OSS Distributions    die "DSTROOT not set in environment"
122*42e22086SApple OSS Distributionsfi
123*42e22086SApple OSS Distributionsmkdir -p "${DSTROOT}" || die "Could not create ${DSTROOT}"
124*42e22086SApple OSS Distributions
125*42e22086SApple OSS Distributionsif [ "$REMOTEBUILD" = "$SPECIALREMOTEBUILD" ]; then
126*42e22086SApple OSS Distributions    :
127*42e22086SApple OSS Distributionselse
128*42e22086SApple OSS Distributions    DOINSTALLSRC=0
129*42e22086SApple OSS Distributions    REMOTE_SRCREL="./"
130*42e22086SApple OSS Distributions    BUILDTOOLSDIR="$OBJROOT"
131*42e22086SApple OSS Distributions    REMOTE_BUILDTOOLSREL="./BUILD/obj"
132*42e22086SApple OSS Distributions    BUILDSCRIPTDIR="$OBJROOT"
133*42e22086SApple OSS Distributions    REMOTE_BUILDSCRIPTREL="./BUILD/obj"
134*42e22086SApple OSS Distributions    BUILDSCRIPTNAME="build.sh"
135*42e22086SApple OSS Distributions    if [ ! -d "${OBJROOT}/SETUP" ]; then
136*42e22086SApple OSS Distributions    RSYNC_DELETE_EXCLUDED="--delete-excluded"
137*42e22086SApple OSS Distributions    else
138*42e22086SApple OSS Distributions    RSYNC_DELETE_EXCLUDED=""
139*42e22086SApple OSS Distributions    fi
140*42e22086SApple OSS Distributions    if [ ! -e "${SYMROOT}/" ]; then
141*42e22086SApple OSS Distributions	RSYNC_DELETE_SYMROOT=1
142*42e22086SApple OSS Distributions    else
143*42e22086SApple OSS Distributions	RSYNC_DELETE_SYMROOT=0
144*42e22086SApple OSS Distributions    fi
145*42e22086SApple OSS Distributions    if [ ! -e "${DSTROOT}/" ]; then
146*42e22086SApple OSS Distributions	RSYNC_DELETE_DSTROOT=1
147*42e22086SApple OSS Distributions    else
148*42e22086SApple OSS Distributions	RSYNC_DELETE_DSTROOT=0
149*42e22086SApple OSS Distributions    fi
150*42e22086SApple OSS Distributions    TARBUILDDIRS=0
151*42e22086SApple OSS Distributionsfi
152*42e22086SApple OSS Distributions
153*42e22086SApple OSS Distributionsecho "Caching build tools..." 1>&2
154*42e22086SApple OSS Distributionsmkdir -p "${BUILDTOOLSDIR}" || die "Could not create BUILDTOOLSDIR"
155*42e22086SApple OSS Distributions$MAKE print_exports "${ARGS[@]}" XCRUN="${SRC}/tools/xcrun_cache.sh -c \"${BUILDTOOLSDIR}\"" >/dev/null || die "Could not cache build tools"
156*42e22086SApple OSS Distributions
157*42e22086SApple OSS Distributions# Cache the make(1) binary itself
158*42e22086SApple OSS DistributionsMAKE_SDKROOT=`"${SRC}/tools/xcrun_cache.sh" -u "${BUILDTOOLSDIR}" -sdk / -show-sdk-path`
159*42e22086SApple OSS Distributions"${SRC}/tools/xcrun_cache.sh" -c "${BUILDTOOLSDIR}" -sdk "${MAKE_SDKROOT}" -find make >/dev/null || die "Could not cache make"
160*42e22086SApple OSS Distributions
161*42e22086SApple OSS Distributions# Create a canned build script that can restart the build on the remote server.
162*42e22086SApple OSS Distributionsmkdir -p "${BUILDSCRIPTDIR}" || die "Could not create BUILDSCRIPTDIR"
163*42e22086SApple OSS Distributionscat > "${BUILDSCRIPTDIR}/${BUILDSCRIPTNAME}" <<EOF
164*42e22086SApple OSS Distributions#!/bin/sh
165*42e22086SApple OSS Distributionsmkdir -p /private/tmp
166*42e22086SApple OSS Distributionsmkdir -p /private/var/tmp
167*42e22086SApple OSS Distributionsmkdir -p "\${TMPDIR}"
168*42e22086SApple OSS Distributionscd "${REMOTE_SRCREL}"
169*42e22086SApple OSS Distributionsmkdir -p ./BUILD/obj
170*42e22086SApple OSS Distributionsmkdir -p ./BUILD/sym
171*42e22086SApple OSS Distributionsmkdir -p ./BUILD/dst
172*42e22086SApple OSS DistributionsMAKE=\`\$PWD/tools/xcrun_cache.sh -u "\$PWD/${REMOTE_BUILDTOOLSREL}" -sdk / -find make\`
173*42e22086SApple OSS Distributionsif [ -z "\${MAKE}" ]; then exit 1; fi
174*42e22086SApple OSS Distributions\${MAKE} ${TARGET} ${REMOTEARGS[@]} XCRUN="\$PWD/tools/xcrun_cache.sh -u \"\$PWD/${REMOTE_BUILDTOOLSREL}\""
175*42e22086SApple OSS Distributionsret=\$?
176*42e22086SApple OSS Distributionsif [ \$ret -eq 0 ]; then
177*42e22086SApple OSS Distributionsif [ ${TARBUILDDIRS} -eq 1 ]; then
178*42e22086SApple OSS Distributionstar jcf ./BUILD/obj.tar.bz2 --exclude=\*.o --exclude=\*.cpo --exclude=\*.d --exclude=\*.cpd --exclude=\*.non_lto --exclude=\*.ctf --exclude=conf -C ./BUILD/obj . || exit 1
179*42e22086SApple OSS Distributionstar jcf ./BUILD/sym.tar.bz2 -C ./BUILD/sym . || exit 1
180*42e22086SApple OSS Distributionstar jcf ./BUILD/dst.tar.bz2 -C ./BUILD/dst . || exit 1
181*42e22086SApple OSS Distributionsfi
182*42e22086SApple OSS Distributionsfi
183*42e22086SApple OSS Distributionsexit \$ret
184*42e22086SApple OSS DistributionsEOF
185*42e22086SApple OSS Distributionschmod a+x "${BUILDSCRIPTDIR}/${BUILDSCRIPTNAME}"
186*42e22086SApple OSS Distributions#echo "Build script is:"
187*42e22086SApple OSS Distributions#cat "${BUILDSCRIPTDIR}/${BUILDSCRIPTNAME}"
188*42e22086SApple OSS Distributions
189*42e22086SApple OSS Distributionsmkdir -p "${BUILDTOOLSDIR}/empty"
190*42e22086SApple OSS Distributions
191*42e22086SApple OSS Distributionsif [ "$REMOTEBUILD" = "$SPECIALREMOTEBUILD" ]; then
192*42e22086SApple OSS Distributions    :
193*42e22086SApple OSS Distributionselse
194*42e22086SApple OSS Distributions
195*42e22086SApple OSS Distributions    REMOTEBUILD="$REMOTEBUILD"
196*42e22086SApple OSS Distributions    REMOTEBUILDPATH="$REMOTEBUILDPATH"
197*42e22086SApple OSS Distributions
198*42e22086SApple OSS Distributions    if [ -z "$REMOTEBUILDPATH" ]; then
199*42e22086SApple OSS Distributions	WHOAMI=`whoami`
200*42e22086SApple OSS Distributions	case "${REMOTEBUILD}" in
201*42e22086SApple OSS Distributions	    *@*)
202*42e22086SApple OSS Distributions		WHOAMI=`echo "${REMOTEBUILD}" | awk -F@ '{print $1}'`
203*42e22086SApple OSS Distributions		;;
204*42e22086SApple OSS Distributions	esac
205*42e22086SApple OSS Distributions	REMOTEBUILDPATH="/tmp/$WHOAMI"
206*42e22086SApple OSS Distributions    fi
207*42e22086SApple OSS Distributions
208*42e22086SApple OSS Distributions# Construct a unique remote path
209*42e22086SApple OSS Distributions    eval `stat -s "${SRC}"`
210*42e22086SApple OSS Distributions
211*42e22086SApple OSS Distributions    REMOTEBUILDPATH="${REMOTEBUILDPATH}/$st_ino/${SRCNAME}/"
212*42e22086SApple OSS Distributions    echo "Remote path is ${REMOTEBUILD}:${REMOTEBUILDPATH}" 1>&2
213*42e22086SApple OSS Distributions
214*42e22086SApple OSS Distributions    ssh $REMOTEBUILD "mkdir -p \"${REMOTEBUILDPATH}/BUILD/\"{obj,sym,dst}" || die "Could not make remote build directory"
215*42e22086SApple OSS Distributions
216*42e22086SApple OSS Distributions    # Copy source only
217*42e22086SApple OSS Distributions    rsync $RSYNC_ARGS --delete --exclude=\*~ --exclude=.svn --exclude=.git --exclude=/BUILD . $REMOTEBUILD:"${REMOTEBUILDPATH}" || die "Could not rsync source tree"
218*42e22086SApple OSS Distributions
219*42e22086SApple OSS Distributions    # Copy partial OBJROOT (just build tools and build script), and optionally delete everything else
220*42e22086SApple OSS Distributions    rsync $RSYNC_ARGS --delete $RSYNC_DELETE_EXCLUDED --include=/build.sh --include=/BuildTools --include=/BuildTools/\*\* --exclude=\* "${OBJROOT}/" $REMOTEBUILD:"${REMOTEBUILDPATH}/BUILD/obj/" || die "Could not rsync build tree"
221*42e22086SApple OSS Distributions
222*42e22086SApple OSS Distributions    # Delete remote SYMROOT if it has been deleted locally
223*42e22086SApple OSS Distributions    if [ "$RSYNC_DELETE_SYMROOT" -eq 1 ]; then
224*42e22086SApple OSS Distributions	rsync $RSYNC_ARGS --delete "${BUILDTOOLSDIR}/empty/" $REMOTEBUILD:"${REMOTEBUILDPATH}/BUILD/sym/" || die "Could not rsync delete SYMROOT"
225*42e22086SApple OSS Distributions    fi
226*42e22086SApple OSS Distributions
227*42e22086SApple OSS Distributions    # Delete remote DSTROOT if it has been deleted locally
228*42e22086SApple OSS Distributions    if [ "$RSYNC_DELETE_DSTROOT" -eq 1 ]; then
229*42e22086SApple OSS Distributions	rsync $RSYNC_ARGS --delete "${BUILDTOOLSDIR}/empty/" $REMOTEBUILD:"${REMOTEBUILDPATH}/BUILD/dst/" || die "Could not rsync delete DSTROOT"
230*42e22086SApple OSS Distributions    fi
231*42e22086SApple OSS Distributions
232*42e22086SApple OSS Distributions    # Start the build
233*42e22086SApple OSS Distributions    echo ssh $REMOTEBUILD "/bin/bash -c 'cd \"${REMOTEBUILDPATH}\" && ${REMOTE_BUILDSCRIPTREL}/${BUILDSCRIPTNAME}'" 1>&2
234*42e22086SApple OSS Distributions    ssh $REMOTEBUILD "/bin/bash -c 'cd \"${REMOTEBUILDPATH}\" && ${REMOTE_BUILDSCRIPTREL}/${BUILDSCRIPTNAME}'" || die "Could not complete remote build"
235*42e22086SApple OSS Distributions
236*42e22086SApple OSS Distributions    # Copy back build results except for object files (which might be several GB)
237*42e22086SApple OSS Distributions    echo "Copying results back..."
238*42e22086SApple OSS Distributions    rsync $RSYNC_ARGS --no-o --no-g --exclude=\*.o --exclude=\*.cpo --exclude=\*.d --exclude=\*.cpd --exclude=\*.non_lto --exclude=\*.ctf $REMOTEBUILD:"${REMOTEBUILDPATH}/BUILD/obj/" "${OBJROOT}/" || die "Could not rsync build results"
239*42e22086SApple OSS Distributions    rsync $RSYNC_ARGS --no-o --no-g $REMOTEBUILD:"${REMOTEBUILDPATH}/BUILD/sym/" "${SYMROOT}/" || die "Could not rsync build results"
240*42e22086SApple OSS Distributions    rsync $RSYNC_ARGS --no-o --no-g $REMOTEBUILD:"${REMOTEBUILDPATH}/BUILD/dst/" "${DSTROOT}/" || die "Could not rsync build results"
241*42e22086SApple OSS Distributions
242*42e22086SApple OSS Distributionsfi
243*42e22086SApple OSS Distributions
244*42e22086SApple OSS Distributionsexit 0
245