-
Notifications
You must be signed in to change notification settings - Fork 29
/
build.sh
executable file
·571 lines (530 loc) · 24.5 KB
/
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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/usr/bin/env bash
# CREDIT: https://github.com/pwn0rz/xnu-build
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
# Colors
export ESC_SEQ="\x1b["
export COL_RESET=$ESC_SEQ"39;49;00m"
export COL_RED=$ESC_SEQ"31;01m"
export COL_GREEN=$ESC_SEQ"32;01m"
export COL_YELLOW=$ESC_SEQ"33;01m"
export COL_BLUE=$ESC_SEQ"34;01m"
export COL_MAGENTA=$ESC_SEQ"35;01m"
export COL_CYAN=$ESC_SEQ"36;01m"
function running() {
echo -e "$COL_MAGENTA ⇒ $COL_RESET""$1"
}
function info() {
echo -e "$COL_BLUE[info] $COL_RESET""$1"
}
function error() {
echo -e "$COL_RED[error] $COL_RESET""$1"
}
# Config
: ${KERNEL_CONFIG:=RELEASE}
: ${ARCH_CONFIG:=ARM64}
: ${MACHINE_CONFIG:=VMAPPLE}
: ${MACOS_VERSION:=""}
: ${JSONDB:=0}
: ${BUILDKC:=0}
: ${CODEQL:=0}
: ${KC_FILTER:='com.apple.driver.SEPHibernation|com.apple.driver.ExclavesAudioKext|com.apple.driver.AppleH11ANEInterface|com.apple.driver.AppleFirmwareKit|com.apple.driver.AppleARMWatchdogTimer'}
WORK_DIR="$PWD"
CACHE_DIR="${WORK_DIR}/.cache"
BUILD_DIR="${WORK_DIR}/build"
FAKEROOT_DIR="${WORK_DIR}/fakeroot"
DSTROOT="${FAKEROOT_DIR}"
HAVE_WE_INSTALLED_HEADERS_YET="${FAKEROOT_DIR}/.xnu_headers_installed"
KERNEL_FRAMEWORK_ROOT='/System/Library/Frameworks/Kernel.framework/Versions/A'
KC_VARIANT=$(echo "$KERNEL_CONFIG" | tr '[:upper:]' '[:lower:]')
KERNEL_TYPE="${KC_VARIANT}.$(echo "$MACHINE_CONFIG" | tr '[:upper:]' '[:lower:]')"
help() {
echo 'Usage: build.sh [-h] [--clean] [--kc]
This script builds the macOS XNU kernel
Where:
-h|--help show this help text
-c|--clean cleans build artifacts and cloned repos
-k|--kc create kernel collection (via kmutil create)
'
exit 0
}
clean() {
running "Cleaning build directories and extra repos..."
declare -a paths_to_delete=(
"${BUILD_DIR}"
"${FAKEROOT_DIR}"
"${WORK_DIR}/xnu"
"${WORK_DIR}/bootstrap_cmds"
"${WORK_DIR}/dtrace"
"${WORK_DIR}/AvailabilityVersions"
"${WORK_DIR}/Libsystem"
"${WORK_DIR}/libplatform"
"${WORK_DIR}/libdispatch"
)
for path in "${paths_to_delete[@]}"; do
info "Will delete ${path}"
done
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
for path in "${paths_to_delete[@]}"; do
info "Deleting ${path}"
rm -rf "${path}"
done
fi
}
install_deps() {
if [ ! -x "$(command -v jq)" ] || [ ! -x "$(command -v gum)" ] || [ ! -x "$(command -v xcodes)" ] || [ ! -x "$(command -v cmake)" ] || [ ! -x "$(command -v ninja)" ]; then
running "Installing dependencies"
if [ ! -x "$(command -v brew)" ]; then
error "Please install homebrew - https://brew.sh (or install 'jq', 'gum' and 'xcodes' manually)"
read -p "Install homebrew now? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
running "Installing homebrew"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
exit 1
fi
fi
brew install jq gum xcodes bash cmake ninja
fi
if compgen -G "/Applications/Xcode*.app" >/dev/null; then
info "Xcode is already installed: $(xcode-select -p)"
else
running "Installing XCode"
ipsw download dev --more --output /tmp
XCODE_VERSION=$(ls /tmp/Xcode_*.xip | sed -E 's/.*Xcode_(.*).xip/\1/')
xcodes install "${XCODE_VERSION}" --experimental-unxip --color --select --path "/tmp/Xcode_${XCODE_VERSION}.xip"
# xcodebuild -downloadAllPlatforms
xcodebuild -runFirstLaunch
fi
}
install_ipsw() {
if [ ! -x "$(command -v ipsw)" ]; then
running "Installing ipsw..."
brew install blacktop/tap/ipsw
fi
}
choose_xnu() {
if [ -z "$MACOS_VERSION" ]; then
gum style --border normal --margin "1" --padding "1 2" --border-foreground 212 "Choose $(gum style --foreground 212 'macOS') version to build:"
MACOS_VERSION=$(gum choose "12.5" "13.0" "13.1" "13.2" "13.3" "13.4" "13.5" "14.0" "14.1" "14.2" "14.3" "14.4" "14.5" "14.6" "15.0" "15.1")
fi
TIGHTBEAMC="tightbeamc-not-supported"
case ${MACOS_VERSION} in
'12.5')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-125/release.json'
KDK_NAME='Kernel Debug Kit 12.5 build 21G72'
KDKROOT='/Library/Developer/KDKs/KDK_12.5_21G72.kdk'
RC_DARWIN_KERNEL_VERSION='22.6.0'
;;
'13.0')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-130/release.json'
KDK_NAME='Kernel Debug Kit 13.0 build 22A380'
KDKROOT='/Library/Developer/KDKs/KDK_13.0_22A380.kdk'
RC_DARWIN_KERNEL_VERSION='22.1.0'
;;
'13.1')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-131/release.json'
KDK_NAME='Kernel Debug Kit 13.1 build 22C65'
KDKROOT='/Library/Developer/KDKs/KDK_13.1_22C65.kdk'
RC_DARWIN_KERNEL_VERSION='22.2.0'
;;
'13.2')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-132/release.json'
KDK_NAME='Kernel Debug Kit 13.2 build 22D49'
KDKROOT='/Library/Developer/KDKs/KDK_13.2_22D49.kdk'
RC_DARWIN_KERNEL_VERSION='22.3.0'
;;
'13.3')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-133/release.json'
KDK_NAME='Kernel Debug Kit 13.3 build 22E252'
KDKROOT='/Library/Developer/KDKs/KDK_13.3_22E252.kdk'
RC_DARWIN_KERNEL_VERSION='22.4.0'
;;
'13.4')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-134/release.json'
KDK_NAME='Kernel Debug Kit 13.4 build 22F66'
KDKROOT='/Library/Developer/KDKs/KDK_13.4_22F66.kdk'
RC_DARWIN_KERNEL_VERSION='22.5.0'
;;
'13.5')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-135/release.json'
KDK_NAME='Kernel Debug Kit 13.5 build 22G74'
KDKROOT='/Library/Developer/KDKs/KDK_13.5_22G74.kdk'
RC_DARWIN_KERNEL_VERSION='22.6.0'
;;
'14.0')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-140/release.json'
KDK_NAME='Kernel Debug Kit 14.0 build 23A344'
KDKROOT='/Library/Developer/KDKs/KDK_14.0_23A344.kdk'
RC_DARWIN_KERNEL_VERSION='23.0.0'
;;
'14.1')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-141/release.json'
KDK_NAME='Kernel Debug Kit 14.1 build 23B74'
KDKROOT='/Library/Developer/KDKs/KDK_14.1_23B74.kdk'
RC_DARWIN_KERNEL_VERSION='23.1.0'
;;
'14.2')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-142/release.json'
KDK_NAME='Kernel Debug Kit 14.2 build 23C64'
KDKROOT='/Library/Developer/KDKs/KDK_14.2_23C64.kdk'
RC_DARWIN_KERNEL_VERSION='23.2.0'
;;
'14.3')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-143/release.json'
KDK_NAME='Kernel Debug Kit 14.3 build 23D56'
KDKROOT='/Library/Developer/KDKs/KDK_14.3_23D56.kdk'
RC_DARWIN_KERNEL_VERSION='23.3.0'
;;
'14.4')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-144/release.json'
KDK_NAME='Kernel Debug Kit 14.4 build 23E214'
KDKROOT='/Library/Developer/KDKs/KDK_14.4_23E214.kdk'
RC_DARWIN_KERNEL_VERSION='23.4.0'
;;
'14.5')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-145/release.json'
KDK_NAME='Kernel Debug Kit 14.5 build 23F79'
KDKROOT='/Library/Developer/KDKs/KDK_14.5_23F79.kdk'
RC_DARWIN_KERNEL_VERSION='23.5.0'
;;
'14.6')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-146/release.json'
KDK_NAME='Kernel Debug Kit 14.6 build 23G80'
KDKROOT='/Library/Developer/KDKs/KDK_14.6_23G80.kdk'
RC_DARWIN_KERNEL_VERSION='23.6.0'
;;
'15.0')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-150/release.json'
KDK_NAME='Kernel Debug Kit 15.0 build 24A335'
KDKROOT='/Library/Developer/KDKs/KDK_15.0_24A335.kdk'
RC_DARWIN_KERNEL_VERSION='24.0.0'
;;
'15.1')
RELEASE_URL='https://raw.githubusercontent.com/apple-oss-distributions/distribution-macOS/macos-151/release.json'
KDK_NAME='Kernel Debug Kit 15.1 build 24B83'
KDKROOT='/Library/Developer/KDKs/KDK_15.1_24B83.kdk'
RC_DARWIN_KERNEL_VERSION='24.1.0'
;;
*)
error "Invalid xnu version"
exit 1
;;
esac
info "Building XNU for macOS ${MACOS_VERSION}"
if [ ! -d "$KDKROOT" ]; then
KDK_URL=$(curl -s "https://raw.githubusercontent.com/dortania/KdkSupportPkg/gh-pages/manifest.json" | jq -r --arg KDK_NAME "$KDK_NAME" '.[] | select(.name==$KDK_NAME) | .url')
running "Downloading '$KDK_NAME' to /tmp"
curl --progress-bar --max-time 900 --connect-timeout 60 -L -o /tmp/KDK.dmg "${KDK_URL}"
running "Installing KDK"
hdiutil attach /tmp/KDK.dmg
if [ ! -d " /Library/Developer/KDKs" ]; then
sudo mkdir -p /Library/Developer/KDKs
sudo chmod 755 /Library/Developer/KDKs
fi
sudo installer -pkg '/Volumes/Kernel Debug Kit/KernelDebugKit.pkg' -target /
hdiutil detach '/Volumes/Kernel Debug Kit'
ls -lah /Library/Developer/KDKs
fi
}
venv() {
if [ ! -d "${WORK_DIR}/venv" ]; then
running "Creating virtual environment"
python3 -m venv "${WORK_DIR}/venv"
fi
info "Activating virtual environment"
source "${WORK_DIR}/venv/bin/activate"
}
get_xnu() {
if [ ! -d "${WORK_DIR}/xnu" ]; then
running "⬇️ Cloning xnu"
XNU_VERSION=$(curl -s $RELEASE_URL | jq -r '.projects[] | select(.project=="xnu") | .tag')
git clone --branch "${XNU_VERSION}" https://github.com/apple-oss-distributions/xnu.git "${WORK_DIR}/xnu"
fi
if [ -f "${CACHE_DIR}/${MACOS_VERSION}/compile_commands.json" ]; then
info "Restoring cached ${CACHE_DIR}/${MACOS_VERSION}/compile_commands.json"
cp -f "${CACHE_DIR}/${MACOS_VERSION}/compile_commands.json" "${WORK_DIR}/xnu"
fi
}
patches() {
running "🩹 Patching xnu files"
# xnu headers patch
sed -i '' 's|^AVAILABILITY_PL="${SDKROOT}/${DRIVERKITROOT}|AVAILABILITY_PL="${FAKEROOT_DIR}|g' "${WORK_DIR}/xnu/bsd/sys/make_symbol_aliasing.sh"
# libsyscall patch
sed -i '' 's|^#include.*BSD.xcconfig.*||g' "${WORK_DIR}/xnu/libsyscall/Libsyscall.xcconfig"
# xnu build patch
sed -i '' 's|^LDFLAGS_KERNEL_SDK = -L$(SDKROOT).*|LDFLAGS_KERNEL_SDK = -L$(FAKEROOT_DIR)/usr/local/lib/kernel -lfirehose_kernel|g' "${WORK_DIR}/xnu/makedefs/MakeInc.def"
sed -i '' 's|^INCFLAGS_SDK = -I$(SDKROOT)|INCFLAGS_SDK = -I$(FAKEROOT_DIR)|g' "${WORK_DIR}/xnu/makedefs/MakeInc.def"
# specify location of mig (bootstrap_cmds)
sed -i '' 's|export MIG := $(shell $(XCRUN) -sdk $(SDKROOT) -find mig)|export MIG := $(shell find $(FAKEROOT_DIR) -name "mig")|g' "${WORK_DIR}/xnu/makedefs/MakeInc.cmd"
sed -i '' 's|export MIGCOM := $(shell $(XCRUN) -sdk $(SDKROOT) -find migcom)|export MIGCOM := $(shell find $(FAKEROOT_DIR) -name "migcom")|g' "${WORK_DIR}/xnu/makedefs/MakeInc.cmd"
# Don't apply patches when building CodeQL database to keep code pure
if [ "$CODEQL" -eq "0" ]; then
PATCH_DIR=""
case ${MACOS_VERSION} in
'12.5' | '13.0' | '13.1' | '13.2' | '13.3' | '13.4' | '13.5' | '14.0' | '14.1' | '14.2' | '14.3')
PATCH_DIR="${WORK_DIR}/patches"
;;
'14.4' | '14.5')
PATCH_DIR="${WORK_DIR}/patches/14.4"
;;
'14.6' | '15.0' | '15.1')
PATCH_DIR="${WORK_DIR}/patches/15.0"
;;
*)
error "Invalid xnu version"
exit 1
;;
esac
cd "${WORK_DIR}/xnu"
for SCRIPT in "${PATCH_DIR}"/*.sh; do
running "Running script: ${SCRIPT}"
bash "${SCRIPT}"
done
for PATCH in "${PATCH_DIR}"/*.patch; do
if git apply --check "$PATCH" 2> /dev/null; then
running "Applying patch: ${PATCH}"
git apply "$PATCH"
fi
done
cd "${WORK_DIR}"
fi
}
build_bootstrap_cmds() {
if [ ! "$(find "${FAKEROOT_DIR}" -name 'mig' | wc -l)" -gt 0 ]; then
running "📦 Building bootstrap_cmds"
if [ ! -d "${WORK_DIR}/bootstrap_cmds" ]; then
BOOTSTRAP_VERSION=$(curl -s $RELEASE_URL | jq -r '.projects[] | select(.project=="bootstrap_cmds") | .tag')
git clone --branch "${BOOTSTRAP_VERSION}" https://github.com/apple-oss-distributions/bootstrap_cmds.git "${WORK_DIR}/bootstrap_cmds"
fi
SRCROOT="${WORK_DIR}/bootstrap_cmds"
OBJROOT="${BUILD_DIR}/bootstrap_cmds.obj"
SYMROOT="${BUILD_DIR}/bootstrap_cmds.sym"
sed -i '' 's|-o root -g wheel||g' "${WORK_DIR}/bootstrap_cmds/xcodescripts/install-mig.sh"
CLONED_BOOTSTRAP_VERSION=$(cd "${WORK_DIR}/bootstrap_cmds"; git describe --always 2>/dev/null)
cd "${SRCROOT}"
xcodebuild install -sdk macosx -project mig.xcodeproj ARCHS="arm64 x86_64" CODE_SIGN_IDENTITY="-" OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}" RC_ProjectNameAndSourceVersion="${CLONED_BOOTSTRAP_VERSION}"
cd "${WORK_DIR}"
fi
}
build_dtrace() {
if [ ! "$(find "${FAKEROOT_DIR}" -name 'ctfmerge' | wc -l)" -gt 0 ]; then
running "📦 Building dtrace"
if [ ! -d "${WORK_DIR}/dtrace" ]; then
DTRACE_VERSION=$(curl -s $RELEASE_URL | jq -r '.projects[] | select(.project=="dtrace") | .tag')
git clone --branch "${DTRACE_VERSION}" https://github.com/apple-oss-distributions/dtrace.git "${WORK_DIR}/dtrace"
fi
SRCROOT="${WORK_DIR}/dtrace"
OBJROOT="${BUILD_DIR}/dtrace.obj"
SYMROOT="${BUILD_DIR}/dtrace.sym"
cd "${SRCROOT}"
xcodebuild install -sdk macosx -target ctfconvert -target ctfdump -target ctfmerge ARCHS="arm64 x86_64" CODE_SIGN_IDENTITY="-" OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}"
cd "${WORK_DIR}"
fi
}
build_availabilityversions() {
if [ ! "$(find "${FAKEROOT_DIR}" -name 'availability.pl' | wc -l)" -gt 0 ]; then
running "📦 Building AvailabilityVersions"
if [ ! -d "${WORK_DIR}/AvailabilityVersions" ]; then
AVAILABILITYVERSIONS_VERSION=$(curl -s $RELEASE_URL | jq -r '.projects[] | select(.project=="AvailabilityVersions") | .tag')
git clone --branch "${AVAILABILITYVERSIONS_VERSION}" https://github.com/apple-oss-distributions/AvailabilityVersions.git "${WORK_DIR}/AvailabilityVersions"
fi
SRCROOT="${WORK_DIR}/AvailabilityVersions"
OBJROOT="${BUILD_DIR}/"
SYMROOT="${BUILD_DIR}/"
cd "${SRCROOT}"
make install -j8 OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}"
cd "${WORK_DIR}"
fi
}
xnu_headers() {
if [ ! -f "${HAVE_WE_INSTALLED_HEADERS_YET}" ]; then
running "Installing xnu headers"
SRCROOT="${WORK_DIR}/xnu"
OBJROOT="${BUILD_DIR}/xnu-hdrs.obj"
SYMROOT="${BUILD_DIR}/xnu-hdrs.sym"
cd "${SRCROOT}"
make installhdrs SDKROOT=macosx ARCH_CONFIGS="X86_64 ARM64" OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}" FAKEROOT_DIR="${FAKEROOT_DIR}" KDKROOT="${KDKROOT}" TIGHTBEAMC=${TIGHTBEAMC} RC_DARWIN_KERNEL_VERSION=${RC_DARWIN_KERNEL_VERSION}
cd "${WORK_DIR}"
touch "${HAVE_WE_INSTALLED_HEADERS_YET}"
fi
}
libsystem_headers() {
if [ ! -d "${FAKEROOT_DIR}/System/Library/Frameworks/System.framework" ]; then
running "Installing Libsystem headers"
if [ ! -d "${WORK_DIR}/Libsystem" ]; then
LIBSYSTEM_VERSION=$(curl -s $RELEASE_URL | jq -r '.projects[] | select(.project=="Libsystem") | .tag')
git clone --branch "${LIBSYSTEM_VERSION}" https://github.com/apple-oss-distributions/Libsystem.git "${WORK_DIR}/Libsystem"
fi
sed -i '' 's|^#include.*BSD.xcconfig.*||g' "${WORK_DIR}/Libsystem/Libsystem.xcconfig"
SRCROOT="${WORK_DIR}/Libsystem"
OBJROOT="${BUILD_DIR}/Libsystem.obj"
SYMROOT="${BUILD_DIR}/Libsystem.sym"
cd "${SRCROOT}"
xcodebuild installhdrs -sdk macosx ARCHS="arm64 arm64e" VALID_ARCHS="arm64 arm64e" OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}" FAKEROOT_DIR="${FAKEROOT_DIR}"
cd "${WORK_DIR}"
fi
}
libsyscall_headers() {
if [ ! -f "${FAKEROOT_DIR}/usr/include/os/proc.h" ]; then
running "Installing libsyscall headers"
SRCROOT="${WORK_DIR}/xnu/libsyscall"
OBJROOT="${BUILD_DIR}/libsyscall.obj"
SYMROOT="${BUILD_DIR}/libsyscall.sym"
cd "${SRCROOT}"
xcodebuild installhdrs -sdk macosx TARGET_CONFIGS="$KERNEL_CONFIG $ARCH_CONFIG $MACHINE_CONFIG" ARCHS="arm64 arm64e" VALID_ARCHS="arm64 arm64e" OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}" FAKEROOT_DIR="${FAKEROOT_DIR}"
cd "${WORK_DIR}"
fi
}
build_libplatform() {
if [ ! -f "${FAKEROOT_DIR}/usr/local/include/_simple.h" ]; then
running "📦 Building libplatform"
if [ ! -d "${WORK_DIR}/libplatform" ]; then
LIBPLATFORM_VERSION=$(curl -s $RELEASE_URL | jq -r '.projects[] | select(.project=="libplatform") | .tag')
git clone --branch "${LIBPLATFORM_VERSION}" https://github.com/apple-oss-distributions/libplatform.git "${WORK_DIR}/libplatform"
fi
SRCROOT="${WORK_DIR}/libplatform"
cd "${SRCROOT}"
ditto "${SRCROOT}/include" "${DSTROOT}/usr/local/include"
ditto "${SRCROOT}/private" "${DSTROOT}/usr/local/include"
cd "${WORK_DIR}"
fi
}
build_libdispatch() {
if [ ! -f "${FAKEROOT_DIR}/usr/local/lib/kernel/libfirehose_kernel.a" ]; then
running "📦 Building libdispatch"
if [ ! -d "${WORK_DIR}/libdispatch" ]; then
LIBDISPATCH_VERSION=$(curl -s $RELEASE_URL | jq -r '.projects[] | select(.project=="libdispatch") | .tag')
git clone --branch "${LIBDISPATCH_VERSION}" https://github.com/apple-oss-distributions/libdispatch.git "${WORK_DIR}/libdispatch"
fi
SRCROOT="${WORK_DIR}/libdispatch"
OBJROOT="${BUILD_DIR}/libfirehose_kernel.obj"
SYMROOT="${BUILD_DIR}/libfirehose_kernel.sym"
# libfirehose_kernel patch
sed -i '' 's|$(SDKROOT)/System/Library/Frameworks/Kernel.framework/PrivateHeaders|$(FAKEROOT_DIR)/System/Library/Frameworks/Kernel.framework/PrivateHeaders|g' "${SRCROOT}/xcodeconfig/libfirehose_kernel.xcconfig"
sed -i '' 's|$(SDKROOT)/usr/local/include|$(FAKEROOT_DIR)/usr/local/include|g' "${SRCROOT}/xcodeconfig/libfirehose_kernel.xcconfig"
cd "${SRCROOT}"
xcodebuild install -target libfirehose_kernel -sdk macosx ARCHS="x86_64 arm64e" VALID_ARCHS="x86_64 arm64e" OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}" FAKEROOT_DIR="${FAKEROOT_DIR}"
cd "${WORK_DIR}"
mv "${FAKEROOT_DIR}/usr/local/lib/kernel/liblibfirehose_kernel.a" "${FAKEROOT_DIR}/usr/local/lib/kernel/libfirehose_kernel.a"
fi
}
build_xnu() {
if [ ! -f "${BUILD_DIR}/xnu.obj/kernel.${KERNEL_TYPE}" ]; then
if [ "$JSONDB" -ne "0" ]; then
running "📦 Building XNU kernel with JSON compilation database"
if [ ! -d "${KDKROOT}" ]; then
error "KDKROOT not found: ${KDKROOT} - please install from the Developer Portal"
exit 1
fi
SRCROOT="${WORK_DIR}/xnu"
OBJROOT="${BUILD_DIR}/xnu-compiledb.obj"
SYMROOT="${BUILD_DIR}/xnu-compiledb.sym"
rm -rf "${OBJROOT}"
rm -rf "${SYMROOT}"
cd "${SRCROOT}"
make SDKROOT=macosx TARGET_CONFIGS="$KERNEL_CONFIG $ARCH_CONFIG $MACHINE_CONFIG" LOGCOLORS=y BUILD_WERROR=0 BUILD_LTO=0 BUILD_JSON_COMPILATION_DATABASE=1 SRCROOT="${SRCROOT}" OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}" FAKEROOT_DIR="${FAKEROOT_DIR}" KDKROOT="${KDKROOT}" TIGHTBEAMC=${TIGHTBEAMC} RC_DARWIN_KERNEL_VERSION=${RC_DARWIN_KERNEL_VERSION} || true
JSON_COMPILE_DB="$(find "${OBJROOT}" -name compile_commands.json)"
info "JSON compilation database: ${JSON_COMPILE_DB}"
cp -f "${JSON_COMPILE_DB}" "${SRCROOT}"
mkdir -p "${CACHE_DIR}/${MACOS_VERSION}"
info "Caching JSON compilation database in: ${CACHE_DIR}/${MACOS_VERSION}"
cp -f "${JSON_COMPILE_DB}" "${CACHE_DIR}/${MACOS_VERSION}"
else
running "📦 Building XNU kernel TARGET_CONFIGS=\"$KERNEL_CONFIG $ARCH_CONFIG $MACHINE_CONFIG\""
if [ ! -d "${KDKROOT}" ]; then
error "KDKROOT not found: ${KDKROOT} - please install from the Developer Portal"
exit 1
fi
SRCROOT="${WORK_DIR}/xnu"
OBJROOT="${BUILD_DIR}/xnu.obj"
SYMROOT="${BUILD_DIR}/xnu.sym"
cd "${SRCROOT}"
make install -j8 VERBOSE=YES SDKROOT=macosx TARGET_CONFIGS="$KERNEL_CONFIG $ARCH_CONFIG $MACHINE_CONFIG" CONCISE=0 LOGCOLORS=y BUILD_WERROR=0 BUILD_LTO=0 SRCROOT="${SRCROOT}" OBJROOT="${OBJROOT}" SYMROOT="${SYMROOT}" DSTROOT="${DSTROOT}" FAKEROOT_DIR="${FAKEROOT_DIR}" KDKROOT="${KDKROOT}" TIGHTBEAMC=${TIGHTBEAMC} RC_DARWIN_KERNEL_VERSION=${RC_DARWIN_KERNEL_VERSION}
cd "${WORK_DIR}"
fi
else
info "📦 XNU kernel.${KERNEL_TYPE} already built"
fi
}
version_lte() {
[ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]
}
version_lt() {
[ "$1" = "$2" ] && return 1 || version_lte "$1" "$2"
}
build_kc() {
if [ -f "${BUILD_DIR}/xnu.obj/kernel.${KERNEL_TYPE}" ]; then
running "📦 Building kernel collection for kernel.${KERNEL_TYPE}"
KDK_FLAG=""
if version_lte 13.0 "$(sw_vers -productVersion | grep -Eo '[0-9]+\.[0-9]+')"; then
KDK_FLAG="--kdk ${KDKROOT}" # Newer versions of kmutil support the --kdk option
fi
if [ "$ARCH_CONFIG" == "ARM64" ]; then
kmutil create -v -V "${KC_VARIANT}" -a arm64e -n boot -s none \
${KDK_FLAG} \
-B "${DSTROOT}/oss-xnu.macOS.${MACOS_VERSION}.kc.$(echo "$MACHINE_CONFIG" | tr '[:upper:]' '[:lower:]')" \
-k "${BUILD_DIR}/xnu.obj/kernel.${KERNEL_TYPE}" \
-x $(ipsw kernel kmutil inspect -x --filter "${KC_FILTER}") # this will skip KC_FILTER regex (and other KEXTs with them as dependencies)
# -x $(kmutil inspect -V release --no-header | grep apple | grep -v "SEPHibernation" | awk '{print " -b "$1; }')
else
kmutil create -v -V "${KC_VARIANT}" -a x86_64 -n boot sys -s none \
${KDK_FLAG} \
-B "${DSTROOT}/BootKernelExtensions.${MACOS_VERSION}.$(echo "$MACHINE_CONFIG" | tr '[:upper:]' '[:lower:]').kc" \
-S "${DSTROOT}/SystemKernelExtensions.${MACOS_VERSION}.$(echo "$MACHINE_CONFIG" | tr '[:upper:]' '[:lower:]').kc" \
-k "${BUILD_DIR}/xnu.obj/kernel.${KERNEL_TYPE}" \
--elide-identifier com.apple.ExclaveKextClient \
-x $(ipsw kernel kmutil inspect -x --filter "${KC_FILTER}") # this will skip KC_FILTER regex (and other KEXTs with them as dependencies)
# -x $(kmutil inspect -V release --no-header | grep apple | grep -v "SEPHibernation" | awk '{print " -b "$1; }')
fi
echo " 🎉 KC Build Done!"
fi
}
main() {
# Parse arguments
while test $# -gt 0; do
case "$1" in
-h | --help)
help
;;
-c | --clean)
clean
shift
;;
-k | --kc)
BUILDKC=1
shift
;;
*)
break
;;
esac
done
install_deps
choose_xnu
get_xnu
patches
venv
build_bootstrap_cmds
build_dtrace
build_availabilityversions
xnu_headers
libsystem_headers
libsyscall_headers
build_libplatform
build_libdispatch
build_xnu
echo " 🎉 XNU Build Done!"
if [ "$BUILDKC" -ne "0" ]; then
install_ipsw
build_kc
fi
}
main "$@"