Skip to content

Instantly share code, notes, and snippets.

@qu0dy
Last active December 10, 2022 15:47
Show Gist options
  • Save qu0dy/57145e3ca850536169566c1560fd23cd to your computer and use it in GitHub Desktop.
Save qu0dy/57145e3ca850536169566c1560fd23cd to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# build-xnu-8792.41.9.sh
# 0x3c3e
# clone https://github.com/apple-oss-distributions/CoreOSMakefiles
# into Xcode.app/Contents/Developer/Makefiles/CoreOS
ARCHS="x86_64"
XCODE="Xcode 14.1"
XNU="8792.41.9"
DTRACE="388"
AVAILABILITY_VERSIONS="111.0.1"
LIBPLATFORM="288"
LIBDISPATCH="1412"
KERNEL_CONFIGS="RELEASE"
BACKUP_SDK=1
error() {
echo "Error: $@"
exit 1
}
# Set a permissive umask just in case.
umask 022
# Print commands and exit on failure.
set -ex
# Check that we have the right Xcode.
XCODE_VERSION="$(xcodebuild -version | head -n 1)"
if [ "$XCODE_VERSION" != "$XCODE" ]; then
error "$XCODE_VERSION detected! Need $XCODE."
fi
# Set the working directory.
WORKDIR="${WORKDIR:-build-${XNU}}"
# 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.
SDK_DIR="$(basename ${SDKPATH})"
if [ -n "${BACKUP_SDK}" ] && ! [ -e "${SDK_DIR}" ]; then
sudo ditto "${SDKPATH}" "${SDK_DIR}"
fi
# Download XNU and some additional sources we will need to help build.
XNU="xnu-${XNU}"
DTRACE="dtrace-${DTRACE}"
AVAILABILITY_VERSIONS="AvailabilityVersions-${AVAILABILITY_VERSIONS}"
LIBPLATFORM="libplatform-${LIBPLATFORM}"
LIBDISPATCH="libdispatch-${LIBDISPATCH}"
curl -L "https://github.com/apple-oss-distributions/xnu/archive/${XNU}.tar.gz" | tar -x
curl -L "https://github.com/apple-oss-distributions/dtrace/archive/${DTRACE}.tar.gz" | tar -x
curl -L "https://github.com/apple-oss-distributions/AvailabilityVersions/archive/${AVAILABILITY_VERSIONS}.tar.gz" | tar -x
curl -L "https://github.com/apple-oss-distributions/libplatform/archive/${LIBPLATFORM}.tar.gz" | tar -x
curl -L "https://github.com/apple-oss-distributions/libdispatch/archive/${LIBDISPATCH}.tar.gz" | tar -x
XNU="xnu-${XNU}"
DTRACE="dtrace-${DTRACE}"
AVAILABILITY_VERSIONS="AvailabilityVersions-${AVAILABILITY_VERSIONS}"
LIBPLATFORM="libplatform-${LIBPLATFORM}"
LIBDISPATCH="libdispatch-${LIBDISPATCH}"
# Build and install ctf utilities. This adds the ctf tools to
# ${TOOLCHAINPATH}/usr/local/bin.
cd ${DTRACE}
mkdir -p obj dst sym
xcodebuild install -sdk macosx -target ctfconvert -target ctfdump -target ctfmerge ARCHS="${ARCHS}" SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst" CODE_SIGN_IDENTITY="-"
sudo ditto "${PWD}/dst/usr/local" "${TOOLCHAINPATH}/usr/local"
cd ..
# Install AvailabilityVersions. This writes to ${SDKPATH}/usr/local/libexec.
cd ${AVAILABILITY_VERSIONS}
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
# SDKPATH!
cd ${XNU}
mkdir -p BUILD.hdrs/obj BUILD.hdrs/sym BUILD.hdrs/dst
make installhdrs SDKROOT=macosx ARCH_CONFIGS=${ARCHS} 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 ARCHS="${ARCHS}" SRCROOT="${PWD}/libsyscall" OBJROOT="${PWD}/BUILD.hdrs/obj" SYMROOT="${PWD}/BUILD.hdrs/sym" DSTROOT="${PWD}/BUILD.hdrs/dst" CODE_SIGN_IDENTITY="-"
# Set permissions correctly before dittoing over SDKPATH.
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}
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}
mkdir -p obj sym dst
xcodebuild install -project libdispatch.xcodeproj -target libfirehose_kernel -sdk macosx ARCHS="${ARCHS}" SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst" CODE_SIGN_IDENTITY="-" NATIVE_ARCH_ACTUAL="${ARCHS}"
mv "${PWD}/dst/usr/local/lib/kernel/liblibfirehose_kernel.a" "${PWD}/dst/usr/local/lib/kernel/libfirehose_kernel.a"
sudo ditto "${PWD}/dst/usr/local" "${SDKPATH}/usr/local"
cd ..
# Build XNU. We set BUILD_WERROR to 0 to disable -Werror during build, since
# XNU definitely does not build without errors.
cd ${XNU}
# is there a better way?
sudo chmod 777 "${SDKPATH}/usr/include/sys/unistd.h"
sudo sed -i '' 's/__API_AVAILABLE/;\/\/__API_AVAILABLE/' "${SDKPATH}/usr/include/sys/unistd.h"
python3 -m venv venv
source venv/bin/activate
make ARCH_CONFIGS="${ARCHS}" KERNEL_CONFIGS="${KERNEL_CONFIGS}" BUILD_WERROR=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment