Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The CMake Tree

Read This First

Nix is the build system, and CMake is a tool that Nix drives. Running cmake on the tree yourself isn’t supported, and we don’t plan to change that.

From the outside a CMake tree looks like something you should be able to configure and build, so here’s why you can’t.

Apple ships these components as Xcode projects, and getting them to build anywhere else meant porting those projects to CMake. An Xcode project encodes a lot that CMake has no direct equivalent for. The port forces some choices inside CMake that we wouldn’t make otherwise, and it leaves gaps that extra glue on the Nix side fills. The result works, and it works reliably when Nix drives it, but the order, the toolchain wiring, the generated headers and the install components all depend on the surrounding derivation setting things up correctly.

So build with nix build. We won’t act on bug reports against a hand-rolled cmake configure. The dev shell lets you work on one component at a time, but it doesn’t produce a system.

CODING_STYLE.md asks for “the CMake build system only” in in-tree code. That means we don’t want a second build system in the tree. It doesn’t make a bare CMake build a supported product.

You’ll still need to read and change this layer, so the rest of this page describes it.

Top-Level Rules

CMakeLists.txt at the root sets a few rules up front:

  • Only Clang is allowed. GCC is rejected, and so is the Xcode generator.
  • CMAKE_MACOSX_MIN_VERSION is 26.5.
  • Objective-C is turned on for the whole project, because libdispatch’s object.m needs the frontend.

It then builds several symlink farms under generated-headers/, for objc, Foundation, mach-o, dlfcn and CoreFoundation. They let in-tree sources use the flat <CoreFoundation/CFAvailability.h> style of include that Apple frameworks expect, without every consumer needing its own include paths.

Configuration Switches

VariableMeaning
PUREDARWIN_ARCHx86_64, arm64, armv6 or riscv64. Falls back to CMAKE_OSX_ARCHITECTURES
PUREDARWIN_ARM64_MACHINE_CONFIGThe arm64 machine, for example VIRT, T8010 or BCM2837
PUREDARWIN_ARM32_MACHINE_CONFIGBCM2835
PUREDARWIN_ENABLE_KERNELBuild src/Kernel
PUREDARWIN_ENABLE_LIBRARIESBuild src/Libraries
PUREDARWIN_ENABLE_USERSPACEBuild src/Userspace
PUREDARWIN_PREBUILT_LIBSYSTEM_ROOTUse an already-built libSystem
PUREDARWIN_ENABLE_SELFHOST_CCTOOLSAlso build the self-hosting cctools variants

build.nix

build.nix is the one derivation that wraps this tree. Every in-tree component package is that same derivation with different arguments:

ArgumentEffect
buildTargetsWhich Ninja targets to build
enableKernel, enableLibraries, enableUserspace, enableTools, enableProjectsWhich subtrees to configure
installKernel, installKexts, installKextNames, installLibSystem, installBaseSystem, installXnuHeaders, installUserland, installUserlandTargetsOnlyWhat ends up in $out
xnuKernelConfigRELEASE or DEVELOPMENT
puredarwinArchSelects the cross toolchain and target triple
prebuiltLibSystemReuse an existing libSystem build
extraCmakeFlagsPer-target flags, such as the machine config

Some of a component’s system files are put in place here too. The libSystem install rule stages dyld, launchd_real, notifyd and its LaunchDaemon plist, along with the guest C headers under pd-guest-headers/, which image.nix later moves to /usr/include.

The cmake/ Helper Modules

ModuleWhat it does
kext.cmakeBuilds a kext bundle: Info.plist, the Mach-O kmod, the bundle layout
mig.cmakeRuns MIG to generate Mach IPC stubs
iig.cmakeRuns iig for IOKit interface definitions
crosscompile.cmakeCross-compilation plumbing
nix-toolchain.cmakeThe toolchain file the Nix derivations and dev shell point at
prebuilt_libsystem.cmakeWires an external libSystem in place of building one
circular.cmakeHandles the circular dependencies between libSystem components
install_helpers.cmake, bundle_resources.cmake, asm.cmake, suppress_warnings.cmakeSupporting utilities