Skip to content

Instantly share code, notes, and snippets.

@knightsc
Created April 11, 2020 19:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knightsc/e6d001d42cd475793dc5d0c5a79666a9 to your computer and use it in GitHub Desktop.
Save knightsc/e6d001d42cd475793dc5d0c5a79666a9 to your computer and use it in GitHub Desktop.
A script to build XNU version 4903.241.1 (macOS Mojave 10.14.3).
#! /bin/bash
#
# build-xnu-4903.270.47.sh
# Scott Knight
#
# Based on the script by Brandon Azad
# https://gist.github.com/bazad/654959120a423b226dc564073b435453
#
# A script showing how to build XNU version 4903.270.47 on macOS Mojave
# 10.14.6 with Xcode 10.3
#
# Note: This process will OVERWRITE files in Xcode's MacOSX10.14.sdk. Make a
# backup of this directory first! Or set BACKUP_SDK=1
#
# Set the working directory.
WORKDIR="${WORKDIR:-build-xnu-4903.270.47}"
# Set a permissive umask just in case.
umask 022
# Print commands and exit on failure.
set -ex
# 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-4903.270.47.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/dtrace/dtrace-284.250.4.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/AvailabilityVersions/AvailabilityVersions-33.200.7.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/libplatform/libplatform-177.270.1.tar.gz | tar -xf-
curl https://opensource.apple.com/tarballs/libdispatch/libdispatch-1008.270.1.tar.gz | tar -xf-
# Build and install ctf utilities. This adds the ctf tools to
# ${TOOLCHAINPATH}/usr/local/bin.
cd dtrace-284.250.4
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/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain" "${TOOLCHAINPATH}"
cd ..
# Install AvailabilityVersions. This writes to ${SDKPATH}/usr/local/libexec.
cd AvailabilityVersions-33.200.7
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-4903.270.47
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"
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-177.270.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-1008.270.1
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-4903.270.47
make SDKROOT=macosx ARCH_CONFIGS=X86_64 KERNEL_CONFIGS="DEBUG"
@knightsc
Copy link
Author

The following branch has commits to fix prelinking issues. Even if prelinking fails though the kernel should boot.

https://github.com/knightsc/darwin-xnu/commits/mojave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment