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

26. GCC

This step builds the compiler that compiles everything in 20_selfhost. The cross GCC of Part II runs only in the container, and once the build moves inside build/fs the container is out of reach. 100_gcc.sh uses that cross compiler to build a native GCC for C and C++: one that runs on KDOS and produces programs for KDOS. It is built from the gcc port’s source tarball, with the GMP, MPFR and MPC sources compiled into it, and installs into build/fs with the C++ standard library, libgcc and the compiler’s own programs. It is by far the longest step of the phase.

Source: the archives of the gcc, gmp, mpfr and mpc ports, and t-linux64-lib.patch from the gcc port
Installs into: build/fs/usr
Approximate build time: 11.5 SBU

Installation of GCC

The step unpacks four port tarballs, gcc’s and those of the three mathematical libraries GCC needs at compile time, and applies one patch from the gcc port’s directory:

GCC_SRC=$(extract_port_source gcc)
GCC_VER=$(get_port_version gcc)
GMP_SRC=$(extract_port_source gmp)
MPFR_SRC=$(extract_port_source mpfr)
MPC_SRC=$(extract_port_source mpc)

cd "$GCC_SRC"

# The native gcc's t-linux64 patch: the runtime goes to /usr/lib, and
# /usr/lib64 is only a link to it.
patch -p1 --no-backup-if-mismatch -i "$(port_dir gcc)/t-linux64-lib.patch"

# Link dependencies into GCC source tree
ln -s "$GMP_SRC" gmp
ln -s "$MPFR_SRC" mpfr
ln -s "$MPC_SRC" mpc

script/phases/10_bootstrap/100_gcc.sh, lines 24–39

t-linux64-lib.patch changes one line of gcc/config/i386/t-linux64: the directory GCC uses for 64-bit libraries becomes ../lib instead of ../lib64. The runtime libraries (libstdc++.so, libgcc_s.so and the rest) therefore install to /usr/lib, and the compiler searches there. On KDOS /usr/lib64 is only a link to lib, which the fs/ overlay creates, so the libraries have one real directory and every program finds them there.

The three ln -s lines put GMP, MPFR and MPC inside the GCC source tree under the names GCC’s top-level configure looks for. GCC then builds them as part of itself and links them statically into its own programs, so the root needs no separate libgmp, libmpfr or libmpc.

GCC is then configured and compiled in a separate build directory:

mkdir -p build && cd build

../configure \
    --host=$KDOS_TARGET \
    --target=$KDOS_TARGET \
    --with-build-sysroot=$SYSROOT \
    --prefix=/usr \
    --libexecdir=/usr/lib      \
    --enable-default-pie       \
    --enable-default-ssp       \
    --disable-nls              \
    --disable-multilib         \
    --disable-libatomic        \
    --disable-libgomp          \
    --disable-libquadmath      \
    --disable-libsanitizer     \
    --disable-libssp           \
    --disable-libvtv           \
    --enable-languages=c,c++   \
    CFLAGS_FOR_TARGET="${CFLAGS/-std=gnu[0-9][0-9]/}" \
    CXXFLAGS_FOR_TARGET="$CXXFLAGS -nostdinc++" \
    LDFLAGS_FOR_TARGET=-L$PWD/$KDOS_TARGET/libgcc

make BUILD_INFO=no-info LIBGCC2_DEBUG_CFLAGS=-g0
make DESTDIR=$SYSROOT install BUILD_INFO=no-info LIBGCC2_DEBUG_CFLAGS=-g0

script/phases/10_bootstrap/100_gcc.sh, lines 41–65

The meaning of the configure options

--host=$KDOS_TARGET --target=$KDOS_TARGET
The compiler runs on x86_64-kdos-linux-musl and produces code for it. The build machine, the container, is guessed and differs from both, so GCC is built by the cross compiler but is itself a native compiler: a program it compiles runs on the same system it runs on.
--with-build-sysroot=$SYSROOT
While GCC builds its target libraries, look for the C library’s headers and libraries in build/fs. The cross compiler already knows its system root, but GCC’s build runs other tools that do not, and this tells them. It is a build-time setting only: the installed compiler looks in /.
--prefix=/usr, --libexecdir=/usr/lib
The drivers go to /usr/bin, and the internal programs (cc1, cc1plus, collect2, lto-wrapper) to /usr/lib/gcc/. These are the paths the gcc package installs to, so 20_selfhost’s package replaces this step’s files under the same names.
--enable-default-pie
Every program the compiler links is a position-independent executable unless told otherwise, so the kernel can load it at a random address.
--enable-default-ssp
Every compile has -fstack-protector-strong on by default.
--disable-nls
Build no message catalogues: diagnostics are in English only.
--disable-multilib
Build only the 64-bit libraries; KDOS has no 32-bit userland.
--disable-libatomic, --disable-libgomp, --disable-libquadmath, --disable-libsanitizer, --disable-libssp, --disable-libvtv
Do not build the atomic-operations, OpenMP, quad-precision maths, sanitizer, stack-protector and vtable-verification runtimes. The bootstrap compiler needs C and C++ with their core runtimes only, the packaged compiler builds these, and the stack-protector support functions are musl’s own.
--enable-languages=c,c++
The C and C++ front ends only.

The meaning of the target-library variables

CFLAGS_FOR_TARGET="${CFLAGS/-std=gnu[0-9][0-9]/}"
The flags for the libraries GCC builds for its target, taken from script/env/host.env with the -std=gnu99 cut out. The target libraries choose their own C standard, and a global -std= would override that choice.
CXXFLAGS_FOR_TARGET="$CXXFLAGS -nostdinc++"
The C++ flags for the same libraries, with the compiler’s standard C++ include directories switched off. The cross compiler that builds libstdc++ would otherwise find the libstdc++ headers 030_libstdc++.sh installed for it, instead of the headers of the library being built.
LDFLAGS_FOR_TARGET=-L$PWD/$KDOS_TARGET/libgcc
The target libraries link against the shared libgcc_s this build has just made. The cross compiler was built with no shared libraries, so without this libstdc++ would be linked against the static libgcc alone.

The meaning of the make variables

BUILD_INFO=no-info
Do not build or install the Texinfo manuals.
LIBGCC2_DEBUG_CFLAGS=-g0
Compile libgcc without debugging information instead of with its default -g.
ln -sv gcc $SYSROOT/usr/bin/cc

# No libtool archive outlives this step. Each names this build directory in
# a -L and the runtime directory as an RPATH, and libtool copies both into
# every C++ library linked through it. kpkg drops the .la files of a package,
# but these are installed outside any package, so nothing later removes them.
# The gdb script goes with them: no package owns it either.
rm -f $SYSROOT/usr/lib{,64}/lib{stdc++{,exp,fs},supc++,itm}.la \
    $SYSROOT/usr/lib{,64}/libstdc++.so.*-gdb.py \
    $SYSROOT/usr/lib/gcc/$KDOS_TARGET/$GCC_VER/liblto_plugin.la

script/phases/10_bootstrap/100_gcc.sh, lines 67–76

ln -sv gcc $SYSROOT/usr/bin/cc makes cc, the compiler name make’s built-in rules and many makefiles call, run GCC.

The rm -f line removes the libtool archives (.la) for libstdc++, libstdc++exp, libstdc++fs, libsupc++, libitm and the LTO plugin, and the libstdc++ pretty-printer script for gdb. Each .la names this build directory in a -L and the runtime directory as an RPATH, and libtool copies both into every C++ library linked through it. kpkg drops the .la files of a package, but these are installed outside any package, so nothing later removes them. No package owns the gdb script either.

Last, the step removes all four source trees and touches build/mark/bootstrap/gcc.

Built again later

20_selfhost builds the gcc port last on its list, after the separate gmp, mpfr and mpc ports it depends on, and 30_foundation names it again. That build is the compiler every later phase uses. It applies the same t-linux64-lib.patch and shares this step’s --prefix, --libexecdir, --enable-default-pie, --enable-default-ssp, --disable-nls, --disable-multilib, the -std= cut from CFLAGS_FOR_TARGET and LIBGCC2_DEBUG_CFLAGS=-g0. It differs in these ways:

See also