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-musland 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 thegccpackage installs to, so20_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-strongon 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.envwith the-std=gnu99cut 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 thelibstdc++headers030_libstdc++.shinstalled 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_sthis build has just made. The cross compiler was built with no shared libraries, so without thislibstdc++would be linked against the staticlibgccalone.
The meaning of the make variables
BUILD_INFO=no-info- Do not build or install the Texinfo manuals.
LIBGCC2_DEBUG_CFLAGS=-g0-
Compile
libgccwithout debugging information instead of with its default-g.
Links and clean-up
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:
- Its triple. Configured with no
--hostor--targetinside the chroot, it is native tox86_64-pc-linux-musl. What this step installed underx86_64-kdos-linux-musl(the target-named programs in/usr/bin,/usr/lib/gcc/x86_64-kdos-linux-musl,/usr/x86_64-kdos-linux-musland the C++ headers under that name) no package replaces, so the package’s install hook,postinstall.sh, deletes it. - Its languages.
c,c++,fortran,lto, with the manual pages and info manuals installed by hand. - Its locale names. It also applies
libstdcxx-generic-locale-names.patch, so its libstdc++ accepts any locale name musl’snewlocaleaccepts, andstd::locale("")works under a UTF-8LANG; the libstdc++ this step builds accepts onlyCandPOSIX. - Its runtimes. None of the six
--disable-lib…switches is passed, so the packaged compiler shipslibatomic,libgomp,libitmand the sanitizer runtimes such aslibasan. - Its libraries. GMP, MPFR and MPC come from their packages instead of the source tree, and
--with-system-zlibuses the packaged zlib.--without-zstdand--without-islkeep two more optional libraries out. - Its own hardening.
--enable-host-pieand--enable-host-bind-nowbuild GCC’s own programs as PIE with immediate binding;--enable-linker-build-idmakes every link it drives carry a build ID.--enable-threads=posixand--enable-__cxa_atexitare stated rather than left to defaults, and--disable-bootstrapbuilds the compiler once instead of three times. - Its flags.
CXXFLAGS_FOR_TARGETcarries no-nostdinc++,LDFLAGS_FOR_TARGETis the chroot’s$LDFLAGS, and-with-pkgversion="KDOS"putsKDOSingcc --version. - Its leftovers.
ccis an absolute link to/usr/bin/gcc, and the gdb script is moved to/usr/share/gdb/auto-load/usr/lib/rather than deleted.
See also
- Binutils, the step before this one, and Make, the step after it
- The gcc port: its version, source, patch and contents
- The toolchain rebuilt as packages: why the native compiler’s triple changes, and what the install hook removes