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