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