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

Part V. Self-hosting

This part is for anyone following the build past the bootstrap, or trying to understand where the compiler on a KDOS machine came from. It covers 20_selfhost, the first phase that runs inside the chroot and the first in which kpkg, the KDOS package manager, builds anything.

The phase’s list is short: the fortify headers (headers that wrap musl’s string and I/O functions with checks against the size of the destination, so an overrun stops the program instead of writing past the end), tar, the C library, zlib, the assembler and linker, the compiler, and the few tools their builds run. Most of them are ports rebuilt over the files Part III installed by hand; fortify-headers, zlib and m4 have no bootstrap step and arrive here for the first time.

This introduction explains:

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

What self-hosting means here

When Part III ends, the root already holds a native GCC: it runs on KDOS and produces programs for KDOS. But that compiler, and the musl and binutils beside it, were compiled in the build container by the cross compiler of Part II. This phase uses the bootstrap compiler, running inside the chroot, to compile musl, binutils and GCC again from their ports. When it ends, the C library, the assembler, the linker and the compiler that every later phase uses were compiled on KDOS, inside KDOS, and no file of theirs came out of the container. That is what the book means by self-hosting; the comparison with how other distributions produce their toolchains is in How KDOS differs.

Two limits are worth knowing exactly:

Ownership

The phase has a second job, which matters as much as the first. Nothing the bootstrap installed belongs to a package: there is no database entry for /usr/lib/libc.so or /usr/bin/gcc, so nothing could upgrade, verify or remove them. When kpkg installs a package over a path that no package claims, it adopts the path instead of refusing it, and the path enters the new package’s manifest (see Who owns a file and Adopting: kpkgadd). Each port of this phase turns a set of hand-installed files into package files. 30_foundation does the same for the rest of the bootstrap’s programs; What the bootstrap leaves lists them.

Adoption covers only a path the port installs again. A bootstrap file that the port does not install stays unowned for good. That is why the bootstrap’s binutils and GCC steps delete the libtool archives and the libstdc++ debugger script they install, files no package will ever claim, and why the gcc package carries the install hook described below.

The environment

Every step of the phase runs kpkg install <port> in the chroot with this environment loaded, from script/phases/20_selfhost/phase.env:

export CHROOT=1

source "${BASH_SOURCE[0]%/*}/../../env/chroot.env"

# This phase builds the native compiler every later phase names, with only the
# bootstrap gcc installed: its configures find their own compiler and C++ mode.
unset CC CXX ac_cv_prog_cxx_cxx11

script/phases/20_selfhost/phase.env, lines 23–29

CHROOT=1 makes the orchestrator run the phase’s steps through the chroot wrapper. It is read from the file’s own text, which is why it is set here and not in the shared file (see The phase environment).

script/env/chroot.env is the environment every chroot phase shares, described in Entering the chroot. Several of its lines do nothing yet in this phase, because what they wait for is not installed:

The release flags themselves do apply. Every port of this phase is compiled with the CFLAGS, CXXFLAGS and LDFLAGS of the release flags, and fortify-headers comes first so that -D_FORTIFY_SOURCE=2 has the headers it needs from the second port on.

The last line is the one this phase adds. chroot.env names the compiler outright, CC=gcc and CXX=g++, so that no configure script can switch to clang once clang is installed, and it sets ac_cv_prog_cxx_cxx11 to empty, which tells an autoconf configure that the C++ compiler needs no extra option. This phase is building that compiler, with only the bootstrap GCC installed, so it unsets all three, and each configure finds its own compiler and chooses its own C++ mode.

The list and its order

The phase’s whole list is one file:

#   20_selfhost: Self-Hosting Bootstrap
# ---------------------------------
#
# The first packages kpkg builds inside the chroot: the C library, the
# compilers and the tools their builds run, rebuilt natively over what
# 10_bootstrap installed, so every later phase compiles with a toolchain that
# built itself.
#
# fortify-headers is first: chroot.env compiles every port with
# -D_FORTIFY_SOURCE=2 against /usr/include/fortify, and a port built before
# those headers exist is built unchecked.

fortify-headers
tar
musl
zlib
binutils
diffutils
m4
gawk
gcc

script/phases/20_selfhost/packages.txt

kdosbuild hands the list to kpkgdepends, which orders it: each name in turn, with the ports it depends on ahead of it. Each port in that order is then one kpkg install step, and a port already installed from an unchanged recipe is skipped when its step comes. Ports come in that the list never names: xxhash ahead of binutils, and gmp, mpfr and mpc, which GCC links. gawk depends on GMP and MPFR as well, so those two arrive ahead of gawk, and MPC follows them just before GCC. No phase’s list names any of them, so this phase is the only one that installs them. ncurses and readline come in ahead of gawk the same way, and 30_foundation names them again. 20_selfhost is the one package phase that the rule every phase installs exactly its list does not hold to.

What each listed port is to the phase:

Port Why it is here
fortify-headers The checking wrappers in /usr/include/fortify that the release flags compile every later port against. A port built before them is built unchecked
tar kpkg runs tar to unpack every later source, to pack every package and to install it, so it is the first of the bootstrap’s programs to become a package
musl The C library, rebuilt natively with --enable-optimize, and with the do-nothing ldconfig script that install scripts call
zlib Linked by binutils and GCC, both of which are configured --with-system-zlib
binutils The assembler and linker, native to the new triplet and installed with tooldir=/usr
diffutils, m4, gawk Tools the GMP and GCC builds run: GMP’s configure asks for m4, GCC’s makefiles run $(AWK) scripts, and its move-if-change compares files with cmp
gcc The C, C++ and Fortran compiler that every later phase uses

The bootstrap built diffutils and gawk as well, gawk configured --without-readline and --without-mpfr. Here both become packages compiled with the release flags, and gawk is configured --with-mpfr and --with-readline, which is why GMP, MPFR and readline come in ahead of it. The rest of the bootstrap’s userland waits for 30_foundation.

The triplet changes

The bootstrap’s tools were built for a target the build names itself: script/env/host.env sets KDOS_TARGET=x86_64-kdos-linux-musl, and the bootstrap compiles with the tools of that name. The binutils and GCC steps of Part III installed copies of themselves under that name too: target-named drivers such as /usr/bin/x86_64-kdos-linux-musl-gcc, a tool directory /usr/x86_64-kdos-linux-musl, and GCC’s private tree /usr/lib/gcc/x86_64-kdos-linux-musl.

The ports of this phase pass no --build, --host or --target. Their configure scripts ask the source’s own config.guess, which on an x86_64 Linux machine prints x86_64-pc-linux- followed by the C library it detects, here musl. Every package from here on is native to x86_64-pc-linux-musl; the configure logs of binutils and GCC in this phase say

checking build system type... x86_64-pc-linux-musl
checking host system type... x86_64-pc-linux-musl

and the packaged compiler keeps its private files in /usr/lib/gcc/x86_64-pc-linux-musl and answers gcc -dumpmachine with that name. The LLVM ports of Part VII take their default target from the same answer, $(cc -dumpmachine).

The new name has a consequence for ownership. The packaged GCC writes /usr/bin/gcc, /usr/lib/libstdc++.so and the other shared names over the bootstrap’s files and adopts them, but nothing it installs carries the old name, so the bootstrap’s target-named trees are never adopted and never replaced.

What the gcc hook removes

The gcc port is the one port of the phase with an install hook. It is a postinstall.sh beside the recipe, which travels inside the package and runs each time the package is installed (see postinstall.sh):

root="${PKG_ROOT:-/}"
rm -rf "$root/usr/lib/gcc/x86_64-kdos-linux-musl" \
	"$root/usr/x86_64-kdos-linux-musl" \
	"$root/usr/include/c++/$version/x86_64-kdos-linux-musl"
rm -f "$root"/usr/bin/x86_64-kdos-linux-musl-* \
	"$root/usr/share/info/gccinstall.info"

ports/core/toolchain/gcc/postinstall.sh

It deletes the bootstrap compiler’s private tree, its binutils tool directory, the C++ headers it kept under its own name, every target-named program in /usr/bin and the gccinstall.info manual. No package owns any of it, so without the hook a second toolchain would stay in the root for good and reach the image. The hook works on PKG_ROOT, the root kpkgadd is installing into. rm -f and rm -rf pass over a path that does not exist, so on every later install of gcc the hook finds nothing and changes nothing.

$version in the hook is the recipe’s version: kpkg writes the recipe’s metadata, name and version among it, at the head of the hook it puts into the package. The C++ header path therefore names the version being installed, and it matches the bootstrap’s because 100_gcc.sh takes its source and version from the same port (see How a step finds its source).

When the phase is over

The tree that 20_selfhost leaves has a package database with the phase’s ports in it, a compiler and C library that are packages, and no file with x86_64-kdos-linux-musl in its path. Everything else the bootstrap installed is still unowned: the minimal builds of bash, coreutils, grep, sed, findutils, patch, make, xz and gzip; the Linux API headers, which the linux port of 60_kernel takes over; and kpkg and kinstall, the package manager and the installer, which no phase’s list names. Part VI rebuilds the userland as full ports before anything needs what the bootstrap left out.

In a run that writes snapshots, one given --snapshot or with snapshot writing switched on in the startup picker, the phase archives fs as its snapshot when it ends, so a later failure can restart from this tree. A run writes none otherwise (see Snapshots).

See also