-
Notifications
You must be signed in to change notification settings - Fork 146
/
xnu-build
executable file
·402 lines (345 loc) · 12.2 KB
/
xnu-build
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/bin/sh
#
# Copyright (c) 2004-2010, Apple Computer, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
# its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
PREFIX=/usr/local
PWDP=$(pwd -P)
COMMONFILE=buildscripts/darwinbuild.common
DARWIN_BUILDROOT=$(pwd)
build=""
action="install"
target=""
configuration=""
version=""
nosource="YES"
USE_CHROOT=NO
INSTALL_XCODE="NO"
###
### Include some common subroutines
###
. "$COMMONFILE"
shopt -s nullglob
###
### DarwinBuild must be run as root. Enforce this.
###
if [ "$EUID" != "0" ]; then
echo "Error: xnu-build must be run as root." 1>&2
exit 1
fi
projnam=xnu
project=$(git tag -l | tail -n1)
if [ ! $project ]; then
echo "You must check out this project from git."
exit 1
fi
###
### We do our building in private/var/tmp since it's
### likely to be out of the way of our dependencies
### and is supposed to be writable by everyone.
###
vartmp="private/var/tmp"
mkdir -p "$BuildRoot/$vartmp"
chmod 1777 "$BuildRoot/$vartmp"
###
### Define the SRCROOT, OBJROOT, SYMROOT, and DSTROOT.
### This script refers to the absolute paths of the build
### directory, and should use REAL_SRCROOT, etc.
### If USE_CHROOT, then the environment variables will have
### the BuildRoot prefix omitted because the chroot
### will make all paths relative to that point.
###
DARWIN_BUILDROOT="/private/var/tmp/BuildRoot"
project_tag=$(git describe --dirty)
buildtool="make"
version="${project/$projnam-/}"
build_version=$(($(GetBuildVersion $DARWIN_BUILDROOT/{Logs,Symbols,Headers,Roots}/$projnam/$project_tag.*) + 1))
REAL_SRCROOT="/SourceCache/$projnam/$project_tag"
if [ -e $REAL_SRCROOT ]; then
rm -rf $REAL_SRCROOT
fi
git clone $(pwd) $REAL_SRCROOT
REAL_OBJROOT="$BuildRoot/$vartmp/$projnam/Objects/$project_tag~$build_version"
REAL_SYMROOT="$BuildRoot/$vartmp/$projnam/Symbols/$project_tag~$build_version"
REAL_DSTROOT="$BuildRoot/$vartmp/$projnam/Roots/$project_tag~$build_version"
mkdir -p $REAL_SRCROOT $REAL_OBJROOT $REAL_SYMROOT $REAL_DSTROOT
###
### Read in the environment
###
LOG="$DARWIN_BUILDROOT/Logs/$projnam/$project_tag.log~$build_version"
TRACELOG="$BuildRoot/private/var/tmp/$projnam/$project_tag.trace~$build_version"
mkdir -p "$DARWIN_BUILDROOT/Logs/$projnam"
if [ "$USE_CHROOT" == "YES" ]; then
prefix="$BuildRoot"
else
prefix=""
fi
export SRCROOT="${REAL_SRCROOT/$prefix/}"
export OBJROOT="${REAL_OBJROOT/$prefix/}"
export SYMROOT="${REAL_SYMROOT/$prefix/}"
export DSTROOT="${REAL_DSTROOT/$prefix/}"
TARGET_CONFIGS=$(cat machine_configuration | xargs)
# Hide this variable from the unset
export -n SRCROOT OBJROOT SYMROOT DSTROOT PLATFORM
export -n DARWIN_BUILDROOT DARWINBUILD_BUILD DARWINXREF_DB_FILE
export -n USE_CHROOT INSTALL_XCODE TARGET_CONFIGS EXTERNAL_BUILD_FLAGS
# Screen sets this to a multi-line value which causes trouble
unset TERMCAP
OLDIFS="$IFS"
IFS=$'\n'
for X in $(printenv) ; do
X=${X/=*/}
eval "unset $X"
done
IFS="$OLDIFS"
export SRCROOT OBJROOT SYMROOT DSTROOT
export DARWIN_BUILDROOT DARWINBUILD_BUILD DARWINXREF_DB_FILE
export USE_CHROOT INSTALL_XCODE
export PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin
export SHELL="/bin/sh"
export HOME="/var/root"
export LOGNAME="root"
export USER="root"
export GROUP="wheel"
build_string=""
if [ "$buildtool" == "xcodebuild" -a "$target" != "" ]; then
build_string="$build_string -target \"$target\""
elif [ "$target" != "" ]; then
action="$target"
fi
#
# append build configuration to $action, if any
# this is only applicable to xcodebuild
#
if [ "$buildtool" == "xcodebuild" -a "$configuration" != "" ]; then
build_string="$build_string -configuration \"$configuration\""
fi
###
### Write out the build script that will be used.
### This may or may not be executed in a chroot.
### We want things like the gcc version to be picked
### up from the chroot.
###
SCRIPT="$BuildRoot/$vartmp/$projnam/build-$project_tag~$build_version.sh"
cat <<-EOF > $SCRIPT
#!/bin/sh
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
echo "BUILDING $project_tag~$build_version on \$(date 2>/dev/null)"
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
echo 'Build configuration:'
echo " Build host: \$(hostname 2>/dev/null)"
echo ' Build tool: $buildtool'
echo ' Build action: $action'
echo " Build number: $build"
echo " Host kernel version: \$(uname -v 2>/dev/null)"
echo " cc version: \$(gcc -v 2>&1 | tail -n 1 2>/dev/null)"
# Panther cctools unlinks -o target, so don't use /dev/null
echo " cctools version: \$(as -v -o /.devnull < /dev/null 2>&1 | cut -c 22- 2>/dev/null)"
EOF
if [ "$logdeps" == "YES" ]; then
if [ "$USE_CHROOT" == "YES" ]; then
if [ "$DARWINTRACE" -nt "$BuildRoot/usr/lib/darwintrace.dylib" ]; then
mkdir -p "$BuildRoot/usr/lib"
cp "$DARWINTRACE" \
"$BuildRoot/usr/lib/darwintrace.dylib"
fi
echo "export DARWINTRACE_LOG=\"${TRACELOG/$BuildRoot/}\"" >> $SCRIPT
echo "export DYLD_INSERT_LIBRARIES=/usr/lib/darwintrace.dylib" >> $SCRIPT
else
echo "export DARWINTRACE_LOG=\"${TRACELOG}\"" >> $SCRIPT
echo "export DYLD_INSERT_LIBRARIES=$DARWINTRACE" >> $SCRIPT
fi
echo "export DYLD_FORCE_FLAT_NAMESPACE=1" >> $SCRIPT
fi
if [ "$INSTALL_XCODE" == "YES" ]; then
echo "*** Redirecting ..."
echo "DARWINTRACE_REDIRECT=\"${BuildRoot}\""
echo "DARWINTRACE_LOG=\"${TRACELOG}\""
echo "DYLD_INSERT_LIBRARIES=$DARWINTRACE"
echo "DYLD_FORCE_FLAT_NAMESPACE=1"
echo ""
echo "export DARWINTRACE_REDIRECT=\"${BuildRoot}\"" >> $SCRIPT
echo "export DARWINTRACE_LOG=\"${TRACELOG}\"" >> $SCRIPT
echo "export DYLD_INSERT_LIBRARIES=$DARWINTRACE" >> $SCRIPT
echo "export DYLD_FORCE_FLAT_NAMESPACE=1" >> $SCRIPT
fi
if [ "$buildtool" == "xcodebuild" ]; then
cat <<-EOF >> $SCRIPT
echo " xcode version: \$(sh -c \\\"$buildtool -version\\\")"
EOF
else
cat <<-EOF >> $SCRIPT
echo " make version: \$(sh -c \\\"$buildtool -version\\\" | head -1 2>/dev/null)"
EOF
fi
cat <<-EOF >> $SCRIPT
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
echo 'Build parameters:'
echo SRCROOT: '$SRCROOT'
echo OBJROOT: '$OBJROOT'
echo SYMROOT: '$SYMROOT'
echo DSTROOT: '$DSTROOT'
EOF
###
### Add in the build environment variables from darwinxref
###
build_string="$build_string $EXTERNAL_BUILD_FLAGS \"SRCROOT=$SRCROOT\" \"OBJROOT=$OBJROOT\" \"SYMROOT=$SYMROOT\" \"DSTROOT=$DSTROOT\" \"TARGET_CONFIGS=$TARGET_CONFIGS\""
OLDIFS="$IFS"
IFS=$'\n'
for X in \
"RC_ProjectName=$projnam" \
"RC_ProjectSourceVersion=$version" \
"RC_ProjectNameAndSourceVersion=$project" \
"RC_ProjectBuildVersion=$build_version" \
$XREF_ENV ; do
echo "echo '${X/=*/}: ${X/*=/}'" >> $SCRIPT
eval "export -- \"$X\""
build_string="$build_string \"$X\""
done
IFS="$OLDIFS"
cat <<-EOF >> $SCRIPT
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
echo 'Environment variables:'
EOF
OLDIFS="$IFS"
IFS=$'\n'
for X in $(printenv | sort) ; do
echo "echo '$X'" >> $SCRIPT
done
IFS="$OLDIFS"
cat <<-EOF >> $SCRIPT
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
echo $buildtool $action '$build_string' \< /dev/null
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
echo ''
echo 'Build log begins here:'
echo ''
cd '$SRCROOT'
EOF
cat <<-EOF >> $SCRIPT
if [ -x /bin/date ]; then
START_DATE=\$(date "+%s")
fi
$buildtool $action $build_string < /dev/null
EXIT_STATUS="\$?"
if [ -x /bin/date ]; then
END_DATE=\$(date "+%s")
ELAPSED_TIME=\$((\$END_DATE - \$START_DATE))
fi
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
if [ -x /bin/date ]; then
echo " BUILD TIME: \$((\$ELAPSED_TIME / 3600))h \$((\$ELAPSED_TIME / 60))m \$((\$ELAPSED_TIME % 60))s"
fi
echo "EXIT STATUS: \$EXIT_STATUS"
exit \$EXIT_STATUS
EOF
chmod ugo+x $SCRIPT
ExitHandler() {
### Once for fdsec
[ -z "$(echo $BuildRoot/dev/*)" ] || umount "$BuildRoot/dev"
### Twice for devfs
[ -z "$(echo $BuildRoot/dev/*)" ] || umount "$BuildRoot/dev"
### Thrice for volfs
[ -z "$(echo $BuildRoot/.vol/*)" ] || chroot "$BuildRoot" umount /.vol
}
if [ "$USE_CHROOT" == "YES" ] ; then
###
### Mount devfs in the chroot
### We'll make /dev/null our de-facto test for the existence of devfs
###
### Warning:
### This must be done *BEFORE* the call to chroot, otherwise there is
### no way to unmount the filesystem except rebooting.
###
echo "*** Mounting special filesystems ..."
trap ExitHandler EXIT
if [ ! -c "$BuildRoot/dev/null" ]; then
echo "Mounting devfs ..."
mkdir -p "$BuildRoot/dev"
mount -t devfs stdin "$BuildRoot/dev"
mount -t fdesc -o union stdin "$BuildRoot/dev"
else
echo "devfs appears to exist ..."
fi
if [ -x /sbin/mount_volfs ]; then
# volfs is no longer present (nor needed) on Leopard.
if [ -z "$(echo $BuildRoot/.vol/*)" ]; then
echo "Mounting volfs ..."
[ -d "$BuildRoot/sbin" ] || mkdir -p "$BuildRoot/sbin"
[ -x "$BuildRoot/sbin/mount_volfs" ] || \
cp /sbin/mount_volfs "$BuildRoot/sbin/"
[ -x "$BuildRoot/sbin/umount" ] || \
cp /sbin/umount "$BuildRoot/sbin/"
[ -d "$BuildRoot/.vol" ] || mkdir -p "$BuildRoot/.vol"
## If the directory is empty, assume volfs not mounted
chroot "$BuildRoot" /sbin/mount_volfs "/.vol"
else
echo "volfs appears to be mounted ..."
fi
fi
###
### Actually invoke the build tool here
###
chroot -u root -g wheel $BuildRoot $vartmp/$projnam/build-$project_tag~$build_version.sh 2>&1 | tee -a "$LOG";
EXIT_STATUS="${PIPESTATUS[0]}"
else
###
### Actually invoke the build tool here
###
$BuildRoot/$vartmp/$projnam/build-$project_tag~$build_version.sh 2>&1 | tee -a "$LOG"
EXIT_STATUS="${PIPESTATUS[0]}"
###
### Clean up the logging
###
if [ "$logdeps" == "YES" ]; then
export -n DYLD_INSERT_LIBRARIES DYLD_FORCE_FLAT_NAMESPACE DARWINTRACE_LOG
fi
fi
if [ "$EXIT_STATUS" == "0" ]; then
###
### Building was successful, copy the results out of the
### build root and into the Root cache
###
if [ "$action" == "installhdrs" ]; then
### Output the manifest
MANIFEST="/tmp/$projnam.$$"
"$DARWINXREF" register "$projnam" "$REAL_DSTROOT" | tee "$MANIFEST"
SHA1=$(cat "$MANIFEST" | $DIGEST)
mkdir -p "$REAL_DSTROOT/usr/local/darwinbuild/receipts"
cp "$MANIFEST" "$REAL_DSTROOT/usr/local/darwinbuild/receipts/$SHA1"
ln -s "$SHA1" "$REAL_DSTROOT/usr/local/darwinbuild/receipts/$projnam.hdrs"
rm -f "$MANIFEST"
mkdir -p "$DARWIN_BUILDROOT/Headers/$projnam/$project.hdrs~$build_version"
ditto "$REAL_DSTROOT" "$DARWIN_BUILDROOT/Headers/$projnam/$project.hdrs~$build_version"
else
mkdir -p "$DARWIN_BUILDROOT/Roots/$projnam/"
rm -f "$DARWIN_BUILDROOT/Roots/$projnam/$project_tag.root"
rm -f "$DARWIN_BUILDROOT/Roots/$projnam/$project_tag.sym"
ln -s "$REAL_DSTROOT" "$DARWIN_BUILDROOT/Roots/$projnam/$project_tag.root"
ln -s "$REAL_SYMROOT" "$DARWIN_BUILDROOT/Roots/$projnam/$project_tag.sym"
fi
fi
exit $EXIT_STATUS