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