Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

Part VII. Compilers

This part covers the phase 31_compilers, which builds the large compilers and the language toolchains the rest of the tree is written in:

Read it if you are building KDOS and want to know why these ports are a phase of their own, how they depend on one another and why LLVM is installed twice. The last section is for anyone who wants to know how far “built from source” goes: it names every compiler in the tree that starts from a copy someone else compiled.

The package pages follow, in chapters by shelf, in the shelves’ own order, and within a shelf in the order kpkgdepends, the dependency sorter, builds them.

Why a phase of its own

The header of the phase’s package list, script/phases/31_compilers/packages.txt, gives the rule:

#
# The ports whose dependency closure reaches one of the large compilers: LLVM
# and clang with their runtimes and pinned 21 series, rust, go, ghc, zig and
# nodejs, and what builds on them. They take most of the hours before 40_lang,
# so in a run that writes snapshots (`--snapshot`) 30_foundation is a restore
# point in front of them.
#
# This list names every port the phase installs and nothing it does not;
# testing/phaseclosure.py fails when a dependency pulls a port in from outside
# it, or when a shelf section names a port an earlier phase installs.

script/phases/31_compilers/packages.txt, lines 12–21

A port belongs here when its dependency closure (the port, its dependencies, their dependencies and so on) reaches one of the large compilers, and nothing in Part VI does. The split is about time. These ports take most of the hours between the chroot and 40_lang. In a run that writes snapshots, 30_foundation’s snapshot sits in front of them, so a failure here restores that snapshot instead of building the foundation again (see The foundation and the compilers, and Snapshots for when one is written).

The phase’s environment is the same as 30_foundation’s: its phase.env sets CHROOT=1 and the same LD_LIBRARY_PATH, and sources script/env/chroot.env. Two settings in chroot.env matter more here than anywhere before:

How the ports depend on one another

The order kpkgdepends gives the phase follows from a few dependencies:

The package list adds one constraint the recipes do not state. libunwind, LLVM’s unwinder, is built in this phase, ahead of 40_lang, whose libunwind-nongnu owns /usr/include/libunwind.h and libunwind.so. The libunwind package installs under a private prefix, its recipe’s _prefix of /usr/lib/llvm-libunwind, so by the time libunwind-nongnu is installed it holds none of those paths; if it held them, that install would be a file conflict.

LLVM and clang

The current LLVM series is a set of ports that all carry upstream’s single llvm-project source archive, each configuring its own directory of it. The llvm port sets LLVM’s host and default target to the system’s own, $(cc -dumpmachine), which is x86_64-pc-linux-musl since Part V, and compiler-rt builds its runtimes for the same triplet. The package list’s comments say what each is for: clang and llvm serve the projects that prefer LLVM, openmp is the libomp runtime that clang -fopenmp links, and compiler-rt is clang’s own runtime libraries: builtins, the profile runtime for source-based coverage, and the UndefinedBehaviorSanitizer. lldb is not here; it is a 41_system port.

The C and C++ compiler of the system stays GCC. Nothing in the environment makes clang the default, and a port uses clang only when its recipe asks for it.

A second LLVM for Zig

Zig is built against one major series of LLVM, and its own CMake check, cmake/Findllvm.cmake in the Zig source, refuses any llvm-config outside that series. The tree’s LLVM moves on with upstream, so Zig gets a slot: llvm21, clang21 and lld21 install a second LLVM, clang and lld under a private prefix, /usr/lib/llvm21, that nothing else searches. The prefix is the recipe helper _prefix, derived from the version as /usr/lib/llvm${version%%.*}, so a bump to another series moves it.

zig asks for the slot by name, in its depends line and on its CMake line:

cmake -B build -G Ninja \
	-D CMAKE_INSTALL_PREFIX=/usr \
	-D CMAKE_BUILD_TYPE=Release \
	-D CMAKE_PREFIX_PATH=/usr/lib/llvm21 \
	-D CMAKE_C_FLAGS_RELEASE="$CFLAGS -DNDEBUG" \
	-D CMAKE_CXX_FLAGS_RELEASE="$CXXFLAGS -DNDEBUG" \
	-D ZIG_TARGET_TRIPLE=native \
	-D ZIG_TARGET_MCPU=baseline \
	-D ZIG_USE_LLVM_CONFIG=ON \
	-D ZIG_STATIC_LLVM=OFF \
	-D ZIG_STATIC_ZLIB=ON \
	-D ZIG_STATIC_ZSTD=ON \
	-D ZIG_PIE=ON \
	-Wno-dev
CCACHE_DISABLE=1 cmake --build build
DESTDIR=$PKG cmake --install build

ports/core/lang/zig/build.sh, lines 12–27

CMAKE_PREFIX_PATH=/usr/lib/llvm21 makes Zig’s llvm-config search look in the prefix before PATH, so it finds the slot’s llvm-config and not the system’s. ZIG_STATIC_LLVM=OFF links the slot’s shared libraries, and the zig binary records /usr/lib/llvm21/lib as its run path. Every other port that looks for LLVM, on PATH or through CMake’s default search, still finds the system one. How the slot is kept from colliding with the system LLVM, file by file, is in A second version beside the first.

Trust and binary bootstraps

Rust, Go, Zig, GHC, OpenJDK and OCaml are each written in their own language, so building one needs a working copy of it first. That copy is the bootstrap seed. KDOS builds every shipped compiler from source, but these start from a seed that upstream compiled.

This section names every port in the tree that does, in this phase and in later ones. A seed reaches the build in one of two ways: as a source = line in a recipe that fetches an upstream build rather than source, or inside the source archive of a self-hosting compiler, which can carry a compiler image of its own. The two tables below cover both. Prebuilt files that are not compilers, such as firmware or Wine’s Gecko engine, are another class, listed in the inventory linked at the end of this section.

Seeds fetched beside the source

Port Phase The seed What it builds
rust 31_compilers Upstream’s stage-0 rustc, rust-std and cargo for x86_64-unknown-linux-musl, three source = lines The Rust compiler and cargo. The seed’s date and version are the ones the Rust source names in its own src/stage0 file
go 31_compilers Upstream’s linux-amd64 Go release, given to make.bash as GOROOT_BOOTSTRAP The Go toolchain
ghc 31_compilers Upstream’s statically linked GHC for Alpine, installed into the build directory and never further Hadrian, GHC’s build system, and GHC’s first stage, which then compiles the second
openjdk 42_graphics Adoptium’s Temurin JDK for Alpine (musl), given to configure as --with-boot-jdk The class library and tools of the JDK that ships
kodi 44_apps The Apache Groovy binary distribution and the Commons Lang and Commons Text jars Nothing that ships: Kodi generates its Python add-on bindings with a Groovy script, and Groovy is not a port

Rust’s recipe shows the shape. The helpers name the seed’s date and version, and the seed is three more source = lines with three more hashes:

_date       = 2026-07-16
_rust       = 1.97.1
_cargo      = $_rust
_triplet    = x86_64-unknown-linux-musl

source      = https://static.rust-lang.org/dist/rustc-$version-src.tar.xz
source      = https://static.rust-lang.org/dist/$_date/rust-std-$_rust-$_triplet.tar.xz
source      = https://static.rust-lang.org/dist/$_date/rustc-$_rust-$_triplet.tar.xz
source      = https://static.rust-lang.org/dist/$_date/cargo-$_cargo-$_triplet.tar.xz
sha256      = be1816e7f6c40abb90245ad6e024bed2a7e88d7dda4561e4d5470207df616b9f  rust-1.98.1.tar.xz
sha256      = 623a1315bdbddc5e7a807aaf7e1911d1435a294758e170b8758f49fc57a3e67c  cargo-1.97.1-x86_64-unknown-linux-musl.tar.xz
sha256      = 51d83178680556f73a5fa8ad865b76a1ff541867445c00fc65dc67246bc2de66  rust-std-1.97.1-x86_64-unknown-linux-musl.tar.xz
sha256      = 1b1f926d3b5bf8036c858180dbc9518c6c317154718ee5c5460bb23d23bacf5d  rustc-1.97.1-x86_64-unknown-linux-musl.tar.xz

ports/core/lang/rust/kpkgbuild, lines 15–27

The first sha256 = line names rust-<version>.tar.xz, not the URL’s rustc-…-src.tar.xz: kpkg saves the first source as <name>-<version>.<ext> whatever the URL’s basename is, and a later one under its basename (see Sources, and what each one becomes).

Its build.sh copies the three archives to where Rust’s bootstrap looks before it would download them:

mkdir -p build/cache/$_date
cp $PORT_SRC/rust-std-$_rust-$_triplet.tar.xz build/cache/$_date/
cp $PORT_SRC/rustc-$_rust-$_triplet.tar.xz build/cache/$_date/
cp $PORT_SRC/cargo-$_cargo-$_triplet.tar.xz build/cache/$_date/

ports/core/lang/rust/build.sh, lines 15–18

Seeds inside the source archive

Port Phase The seed What it builds
zig 31_compilers stage1/zig1.wasm, a WebAssembly build of the compiler The build translates it to C with the wasm2c in the same directory, compiles that, and uses the result to start Zig’s own bootstrap
ocaml 40_lang boot/ocamlc and boot/ocamllex, bytecode images of the compiler and the lexer generator They run on the ocamlrun the build compiles first, and the build compiles every compiler it installs from source

Two archives carry precompiled code that is not a seed, and their ports keep it out of the package. guile’s tarball ships bytecode for its own Scheme modules under prebuilt/, and the port’s build.sh deletes every one of those .go files before configure, so Guile compiles its modules itself. go’s source tarball carries upstream-compiled .syso objects for the race detector and the BoringCrypto module, and its build.sh deletes them from what it installs.

What is built from the seeds

No seed is installed. What is built from source makes it a rule that a seed never reaches the package: what ships is what the seed built, the rustc, cargo, go, zig, ghc, java and ocamlc of the packages, and everything they compile in turn. In this phase that is cabal-install and pandoc, compiled by the packaged GHC from the Hackage sources their recipes vendor; cbindgen, bindgen and cargo-c, compiled by the packaged Rust; and Ruby’s YJIT and Node’s Temporal support, compiled by the same Rust. In later phases it is every Rust, Go, Haskell, Java and OCaml port in the tree. None of those has a seed of its own; each trusts the seed of its compiler.

What “from source” means, then

A seed is a source like any other in two ways. Each is pinned by a sha256 = line in its recipe, its own line for a seed fetched beside the source and the source archive’s line for one inside it, and each is fetched by make fetch and kept in the source archive under that hash (see Sources and the archive), so the build stays offline and a changed seed fails the checksum. And no seed’s own bytes are in the image.

It differs in one way that matters. A seed is a program nobody here compiled, and it compiles the first stage of a compiler that is then shipped. What the seed did to that stage can be checked only as far as the compiler’s own bootstrap checks it; it cannot be read from this tree. Every shipped Rust, Go, Haskell, Java, OCaml and Zig program is built by a compiler whose ancestry passes through one of these seeds. The C toolchain has an ancestor of the same kind: the compiler of the build container, which compiles the cross compiler of Part II, and through it everything after.

The complete inventory of what the tree installs without compiling it here, of which the seeds are one class, is in What is not built from source, with the seeds under Bootstrap seeds.

See also