Sometimes, you need certain header files that aren’t available with the default MacOS toolchain. For example, I’m building a replacement for
caffeinate which completely disables sleep while the laptop lid is closed, so my stuff can keep compiling, rendering, and etc. while it’s in my backpack. But it needs a header that I found hidden deep within the depths of the MacOS kernel source code, AKA XNU.
Simply downloading XNU and trying to copy over the headers doesn’t work, because you need extra tools, which you have to build yourself. (It’s not available on Homebrew, unfortunately.)
Anyway, if you’re still reading this, it means you really want those headers, so let’s just skip the rest of the context and get to getting those headers.
- Download the latest Xcode. I use this Ruby script to download Xcode much faster than using the App Store, but you can also use a GUI and a command. You could use the App Store… but that’s boring!
- Download the source code you need
- Build & install dtrace tools
tar zxf dtrace-388.tar.gz
xcodebuild install -sdk macosx -target ctfconvert -target ctfdump -target ctfmerge ARCHS='x86_64 arm64' CODE_SIGNING_ALLOWED=NO VALID_ARCHS='x86_64 arm64' DSTROOT=$PWD/dst
sudo ditto "dst/usr/local/" "$(xcrun -sdk macosx -show-sdk-platform-path)/../../Toolchains/XcodeDefault.xctoolchain/"
- Build & install AvailabilityVersions
tar zxf AvailabilityVersions-112.tar.gz
sudo ditto dst/usr/local/libexec "$(xcrun -sdk macosx -show-sdk-path)/usr/local/libexec"
- Install the headers!
tar zxf xnu-8792.61.2.tar.gz
make SDKROOT=macosx ARCH_CONFIGS="X86_64 ARM64" installhdrs
sudo ditto BUILD/dst "$(xcrun -sdk macosx -show-sdk-path)"
Once you’re done, you’ll have access to the header files you need from the XNU kernel. While I wish the headers would just be included by default, installing them manually isn’t that bad. Once you’ve installed the tools & scripts, you only need to download XNU again to update the headers, there shouldn’t be any need to reinstall the tools again. Now just point your build tools at the headers and compile to your heart’s content!