Skip to content

Instantly share code, notes, and snippets.

@zhuowei
Forked from knightsc/build-xnu-6153.11.26.sh
Last active June 1, 2021 14:07
Show Gist options
  • Save zhuowei/69c886423642cd77fd2c010f4d54b1c4 to your computer and use it in GitHub Desktop.
Save zhuowei/69c886423642cd77fd2c010f4d54b1c4 to your computer and use it in GitHub Desktop.
A script to build XNU version xnu-6153.141.1 (macOS Catalina 10.15.6), based on Scott Knight's script
#! /bin/bash
# build-xnu-6153.141.1.sh
# tested with Xcode 11.7 on macOS 11
# Changes:
# update to 10.15.6's kernel
# specify an Xcode
# force host OS version to 10.15 so Xcode's clang doesn't complain
#
# based on
#
# build-xnu-6153.11.26.sh
# Scott Knight
#
# Based on the script by Brandon Azad
# https://gist.github.com/bazad/654959120a423b226dc564073b435453
#
# A script showing how to build XNU version 6153.11.26 on macOS Catalina
# 10.15 with Xcode 11.13.1.
#
# Note: This process will OVERWRITE files in Xcode's MacOSX10.15.sdk. Make a
# backup of this directory first! Or set BACKUP_SDK=1
#
# Set the working directory.
WORKDIR="${WORKDIR:-build-xnu}"
# Set a permissive umask just in case.
umask 022
# Print commands and exit on failure.
set -ex
export DEVELOPER_DIR="/Users/zhuowei/Downloads/Xcode.app/Contents/Developer"
# Get the SDK path and toolchain path.
SDKPATH="$(xcrun --sdk macosx --show-sdk-path)"
TOOLCHAINPATH="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain"
[ -d "${SDKPATH}" ] && [ -d "${TOOLCHAINPATH}" ]
# Create the working directory.
mkdir "${WORKDIR}"
cd "${WORKDIR}"
# Back up the SDK if that option is given.
if [ -n "${BACKUP_SDK}" ]; then
sudo ditto "${SDKPATH}" "$(basename "${SDKPATH}")"
fi
# Download XNU and some additional sources we will need to help build.
curl https://opensource.apple.com/tarballs/xnu/xnu-6153.141.1.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/dtrace/dtrace-338.100.1.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/AvailabilityVersions/AvailabilityVersions-45.11.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/libplatform/libplatform-220.100.1.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/libdispatch/libdispatch-1173.100.2.tar.gz | tar -xf-
# Build and install ctf utilities. This adds the ctf tools to
# ${TOOLCHAINPATH}/usr/local/bin.
cd dtrace-338.100.1
cd include/llvm-Support
rm PointerLikeTypeTraits.h
curl https://gist.githubusercontent.com/knightsc/cf46670ea023168cdfe98b4a295f2cf4/raw/00f0b13c00983e4010ba0019eeeecb0ba9a381e7/PointerLikeTypeTraits.h > PointerLikeTypeTraits.h
curl https://gist.githubusercontent.com/knightsc/fe2cbe276a006fe601b704cd5286047f/raw/bb44a1b8cbfa22a8d814368189a193394b9cfe4c/DataTypes.h > DataTypes.h
cd ../..
mkdir -p obj dst sym
xcodebuild install -target ctfconvert -target ctfdump -target ctfmerge -UseModernBuildSystem=NO ARCHS="x86_64" SDKROOT=macosx SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst"
# TODO: Get the XcodeDefault.toolchain path programmatically.
sudo ditto "${PWD}/dst/${TOOLCHAINPATH}" "${TOOLCHAINPATH}"
cd ..
# Install AvailabilityVersions. This writes to ${SDKPATH}/usr/local/libexec.
cd AvailabilityVersions-45.11
mkdir -p dst
make install SRCROOT="${PWD}" DSTROOT="${PWD}/dst"
sudo ditto "${PWD}/dst/usr/local" "${SDKPATH}/usr/local"
cd ..
# Install the XNU headers we'll need for libdispatch. This OVERWRITES files in
# MacOSX10.14.sdk!
cd xnu-6153.141.1
mkdir -p BUILD.hdrs/obj BUILD.hdrs/sym BUILD.hdrs/dst
make installhdrs SDKROOT=macosx ARCH_CONFIGS=X86_64 SRCROOT="${PWD}" OBJROOT="${PWD}/BUILD.hdrs/obj" SYMROOT="${PWD}/BUILD.hdrs/sym" DSTROOT="${PWD}/BUILD.hdrs/dst" HOST_OS_VERSION="10.15"
xcodebuild installhdrs -project libsyscall/Libsyscall.xcodeproj -sdk macosx -UseModernBuildSystem=NO ARCHS="x86_64" SRCROOT="${PWD}/libsyscall" OBJROOT="${PWD}/BUILD.hdrs/obj" SYMROOT="${PWD}/BUILD.hdrs/sym" DSTROOT="${PWD}/BUILD.hdrs/dst"
# Set permissions correctly before dittoing over MacOSX10.13.sdk.
sudo chown -R root:wheel BUILD.hdrs/dst/
sudo ditto BUILD.hdrs/dst "${SDKPATH}"
cd ..
# Install libplatform headers to ${SDKPATH}/usr/local/include.
cd libplatform-220.100.1
sudo ditto "${PWD}/include" "${SDKPATH}/usr/local/include"
sudo ditto "${PWD}/private" "${SDKPATH}/usr/local/include"
cd ..
# Build and install libdispatch's libfirehose_kernel target to
# ${SDKPATH}/usr/local.
cd libdispatch-1173.100.2
mkdir -p obj sym dst
xcodebuild install -project libdispatch.xcodeproj -target libfirehose_kernel -sdk macosx -UseModernBuildSystem=NO ARCHS="x86_64" SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst"
sudo ditto "${PWD}/dst/usr/local" "${SDKPATH}/usr/local"
cd ..
# Build XNU.
cd xnu-6153.141.1
make SDKROOT=macosx ARCH_CONFIGS=X86_64 KERNEL_CONFIGS="DEBUG" HOST_OS_VERSION="10.15"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment