Skip to content

Instantly share code, notes, and snippets.

@bazad
Created November 8, 2018 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bazad/028bcda699fbd1e23341e791870486e6 to your computer and use it in GitHub Desktop.
Save bazad/028bcda699fbd1e23341e791870486e6 to your computer and use it in GitHub Desktop.
A script to build XNU version 4570.71.2 (macOS High Sierra 10.13.6) on macOS 10.13.6 with Xcode 9.4.1.
#! /bin/bash
#
# build-xnu-4570.71.2.sh
# Brandon Azad
#
# A script showing how to build XNU version 4570.71.2 (which corresponds to
# macOS 10.13.6) on macOS High Sierra 10.13.6 with Xcode 9.4.1.
#
# Note: This process will OVERWRITE files in Xcode's MacOSX10.13.sdk. Make a
# backup of this directory first!
#
# Note: This script will NOT work with Xcode 10 and later.
#
XNU="xnu-4570.71.2"
XCODE="Xcode 9.4.1"
DTRACE="dtrace-262.50.12"
AVAILABILITY_VERSIONS="AvailabilityVersions-32.60.1"
LIBPLATFORM="libplatform-161.50.1"
LIBDISPATCH="libdispatch-913.60.2"
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.
curl https://opensource.apple.com/tarballs/xnu/${XNU}.tar.gz | tar -x
curl https://opensource.apple.com/tarballs/dtrace/${DTRACE}.tar.gz | tar -x
curl https://opensource.apple.com/tarballs/AvailabilityVersions/${AVAILABILITY_VERSIONS}.tar.gz | tar -x
curl https://opensource.apple.com/tarballs/libplatform/${LIBPLATFORM}.tar.gz | tar -x
curl https://opensource.apple.com/tarballs/libdispatch/${LIBDISPATCH}.tar.gz | tar -x
# Build and install ctf utilities. This adds the ctf tools to
# ${TOOLCHAINPATH}/usr/local/bin.
cd ${DTRACE}
mkdir -p obj dst sym
xcodebuild install -target ctfconvert -target ctfdump -target ctfmerge ARCHS="x86_64" SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst"
sudo ditto "${PWD}/dst/${TOOLCHAINPATH}" "${TOOLCHAINPATH}"
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
# MacOSX10.13.sdk!
cd ${XNU}
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"
# HACK: The subsequent build command fails with a missing file error. We create
# that file so that the build continues.
touch libsyscall/os/thread_self_restrict.h
xcodebuild installhdrs -project libsyscall/Libsyscall.xcodeproj -sdk macosx ARCHS="x86_64 i386" 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}
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="x86_64 i386" SRCROOT="${PWD}" OBJROOT="${PWD}/obj" SYMROOT="${PWD}/sym" DSTROOT="${PWD}/dst"
sudo ditto "${PWD}/dst/usr/local" "${SDKPATH}/usr/local"
cd ..
# Patch XNU. This corrects a build error.
cat <<"EOF" > ${XNU}.patch
diff --git a/bsd/net/if_ipsec.c b/bsd/net/if_ipsec.c
index 0984205..7d5bb7f 100644
--- a/bsd/net/if_ipsec.c
+++ b/bsd/net/if_ipsec.c
@@ -370,6 +370,8 @@ ipsec_interface_isvalid (ifnet_t interface)
return 1;
}
+#if IPSEC_NEXUS
+
boolean_t
ipsec_interface_needs_netagent(ifnet_t interface)
{
@@ -388,6 +390,8 @@ ipsec_interface_needs_netagent(ifnet_t interface)
return (pcb->ipsec_needs_netagent == true);
}
+#endif // IPSEC_NEXUS
+
static errno_t
ipsec_ifnet_set_attrs(ifnet_t ifp)
{
EOF
cd ${XNU}
patch -p1 < ../${XNU}.patch
cd ..
# Build XNU. We set BUILD_WERROR to 0 to disable -Werror during build, since
# XNU definitely does not build without errors.
cd ${XNU}
make SDKROOT=macosx ARCH_CONFIGS=X86_64 KERNEL_CONFIGS="RELEASE" BUILD_WERROR=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment