7. Cross GCC, pass 1
The second step compiles GCC to run in the container and write programs for x86_64-kdos-linux-musl. It is a deliberately incomplete compiler. There is no C library for the target yet, so it is built with no libc headers, no shared libraries, no threads and no C++ standard library: enough to compile musl, and then, with musl in place, everything else 10_bootstrap builds. This page shows the commands of script/phases/00_cross/01_gcc.sh, explains each switch, the limits.h fix-up that follows the install, and what the compiler leaves in build/cross.
Source: the archives of the gcc, gmp, mpfr and mpc ports
Installs into: build/cross
Approximate build time: 7.8 SBU
Installation of cross GCC
GCC needs three mathematical libraries, GMP, MPFR and MPC, to fold constants at compile time. The step unpacks all four sources and links the three libraries into the GCC tree under the names GCC’s build looks for:
GCC_SRC=$(extract_port_source gcc)
GMP_SRC=$(extract_port_source gmp)
MPFR_SRC=$(extract_port_source mpfr)
MPC_SRC=$(extract_port_source mpc)
cd "$GCC_SRC"
# Link dependencies into GCC source tree
ln -s "$GMP_SRC" gmp
ln -s "$MPFR_SRC" mpfr
ln -s "$MPC_SRC" mpc
script/phases/00_cross/01_gcc.sh, lines 24–34
With a gmp, mpfr and mpc directory at the top of its source, GCC builds the three as part of itself and links them statically into its compiler proper. The cross compiler therefore depends on none of the container’s libraries for them.
Configure and build in a separate directory:
mkdir build && cd build
../configure \
--target=$KDOS_TARGET \
--with-sysroot=$SYSROOT \
--prefix=$CROSS_SYSROOT \
--without-headers \
--with-newlib \
--enable-languages=c,c++ \
--enable-default-pie \
--enable-default-ssp \
--enable-initfini-array \
--disable-nls \
--disable-shared \
--disable-multilib \
--disable-threads \
--disable-libatomic \
--disable-libgomp \
--disable-libquadmath \
--disable-libssp \
--disable-libvtv \
--disable-libstdcxx \
--disable-bootstrap
make all BUILD_INFO=no-info
make install BUILD_INFO=no-info
script/phases/00_cross/01_gcc.sh, lines 36–61
The meaning of the configure options
--target=$KDOS_TARGET,--with-sysroot=$SYSROOT,--prefix=$CROSS_SYSROOT-
As for binutils: a compiler for
x86_64-kdos-linux-musl, whose system root isbuild/fsand which is installed underbuild/cross. With the system root set, the compiler searchesbuild/fs/usr/includeandbuild/fs/usr/liband never the container’s directories. --without-headers,--with-newlib-
Build with no C library headers. Together the two define
inhibit_libcwhilelibgccis compiled, so the parts of it that need a C library are left out. Nothing of newlib is used:--with-newlibis the conventional way to say that the target’s C library is not available. --enable-languages=c,c++- Build the C and C++ front ends. C is what musl is written in. The C++ front end compiles libstdc++ once musl exists, and after it every C++ program of the bootstrap, GCC’s own native build among them.
--enable-default-pie,--enable-default-ssp-
Compile position-independent executables and use the stack protector unless told otherwise. Every program of the bootstrap is compiled with this compiler, so every one of them is hardened the way the packages that replace them are. musl provides the stack protector’s failure handler, so no separate
libsspis needed. --enable-initfini-array-
Run constructors and destructors through
.init_arrayand.fini_array. A cross build cannot test the target for this, but GCC’sconfig.gccpresumes it for every Linux target except uClibc, so on this triplet the switch states the default. --disable-nls- Build no message catalogues.
--disable-shared-
Build
libgccas a static archive only. A sharedlibgcc_swould have to be linked against a C library. --disable-multilib-
Build for 64-bit x86 alone. KDOS has no 32-bit C library to pair a 32-bit
libgccwith. --disable-threads-
There is no thread library to build against, so
libgccis built for a single thread. --disable-libatomic,--disable-libgomp,--disable-libquadmath,--disable-libssp,--disable-libvtv- Leave out the runtime libraries that need a C library to build. None of them is wanted from a compiler whose only task is the bootstrap.
--disable-libstdcxx-
Leave out the C++ standard library. It needs a C library, and step 030 builds it from the same source once musl is in
build/fs. --disable-bootstrap-
Build the compiler once, with the container’s compiler, instead of GCC’s three-stage build that compiles itself and compares the results. That build needs a compiler that writes programs for the machine it runs on, and this one writes them for
x86_64-kdos-linux-musl, so GCC’s configure already leaves bootstrapping off when the host and target differ; the switch states it. BUILD_INFO=no-info-
Given to both
makelines: run nomakeinfo, so no manual is regenerated from its Texinfo source. The release archive ships GCC’s manuals already built, andmake installstill copies those intobuild/cross/share/info.
The limits.h fix-up
cd ..
cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
`dirname $($KDOS_TARGET-gcc -print-libgcc-file-name)`/include/limits.h
ln -sfv "$CROSS_SYSROOT/bin/$KDOS_TARGET-gcc" "$CROSS_SYSROOT/bin/$KDOS_TARGET-cc"
script/phases/00_cross/01_gcc.sh, lines 63–67
GCC installs a limits.h of its own in its private include directory, and normally that header ends by including the system’s limits.h with #include_next. GCC decides which version to install by looking for the system header while it builds. Here it found none, because musl’s headers are not in build/fs yet, so it installed a self-contained header that never includes the system’s. Every program compiled afterwards would then miss what musl’s limits.h defines, such as PATH_MAX.
The cat writes the full header the way GCC’s build does when the system header exists: the limitx.h prologue, GCC’s own glimits.h, and the limity.h epilogue, into the include directory beside libgcc.a, which -print-libgcc-file-name locates. The #include_next finds musl’s limits.h from step 020 onwards.
The link then gives the compiler the name x86_64-kdos-linux-musl-cc. A configure script given --host looks for the target’s gcc first and its cc after it; with the link, either name finds this compiler. -f replaces a link already in build/cross, so the step can be run again over it.
Contents of cross GCC
Installed programs, in build/cross/bin, each prefixed x86_64-kdos-linux-musl-: c++, cc (the link), cpp, g++, gcc, gcc- followed by the version, gcc-ar, gcc-nm, gcc-ranlib, gcov, gcov-dump, gcov-tool and lto-dump.
Installed libraries, in build/cross/lib/gcc/x86_64-kdos-linux-musl/<version>: libgcc.a, libgcov.a, and the start and end files crtbegin*.o, crtend*.o, crtfastmath.o and crtprec*.o. build/cross/lib holds libcc1, the library through which GDB’s compile command drives GCC.
Installed directories: the compiler proper (cc1, cc1plus, collect2, lto1, lto-wrapper, the LTO plugin and g++-mapper-server, the C++ modules mapper) in build/cross/libexec/gcc/x86_64-kdos-linux-musl/<version>, and GCC’s private headers, limits.h among them, in the include directory beside libgcc.a. Step 030 adds the C++ headers in build/cross/x86_64-kdos-linux-musl/include/c++/<version>.
gcc,cc- The C compiler driver: runs the preprocessor, the compiler proper, the assembler and the linker.
g++,c++- The C++ compiler driver.
cpp- The C preprocessor.
gcc-ar,gcc-nm,gcc-ranlib-
Wrappers that hand
ar,nmandranlibthe LTO plugin, so archives of LTO objects get a symbol index. gcov,gcov-dump,gcov-tool- The coverage tools.
lto-dump- Prints the contents of LTO object files.
libgcc.a- The routines the compiler calls for operations the processor has no instruction for, and the unwinder.
Built again later
Native GCC, at the end of 10_bootstrap, is compiled by this compiler to run on KDOS. It is still configured for x86_64-kdos-linux-musl, and with musl in build/fs it builds what this one leaves out for want of a C library: shared libraries, POSIX threads and libstdc++. It still passes --disable-libatomic, --disable-libgomp, --disable-libquadmath, --disable-libssp and --disable-libvtv, and adds --disable-libsanitizer. It applies the gcc port’s t-linux64-lib.patch, and it installs with the port’s own paths (--libexecdir=/usr/lib).
The gcc port, in 20_selfhost, is the compiler KDOS ships. It is built inside the chroot with no triplet, so it becomes x86_64-pc-linux-musl (see Two target names). It adds the Fortran and LTO front ends (--enable-languages=c,c++,fortran,lto), POSIX threads, a hardened compiler binary (--enable-host-pie, --enable-host-bind-now), build IDs on everything it links (--enable-linker-build-id), and the system zlib. Its libgcc is built with -g0 (LIBGCC2_DEBUG_CFLAGS), its target libraries with CFLAGS_FOR_TARGET stripped of -std=, and it identifies itself as KDOS (--with-pkgversion). It passes none of the --disable-lib* switches, and it is the compiler every port from that point on is built with.
See also
- Part II: the cross toolchain and its environment
- Cross binutils: the assembler and linker this compiler drives
- musl libc: the first thing this compiler builds
- Build troubleshooting: the
-std=flag and GCC’s runtime libraries