5.12. Cross GCC-7.1.0 - Static

The GCC package contains the GNU compiler collection, which includes the C and C++ compilers.

5.12.1. Installation of Cross GCC Compiler with Static libgcc and no Threads

Here we will compile GCC, as a cross-compiler that will create executables for our target architecture, statically so that it will not need to look for Glibc's startfiles, which do not yet exist in /tools. We will use this cross-compiler, plus the cross-linker we have just installed with Binutils, to compile Glibc. After Glibc is installed into /tools, we can rebuild GCC so that it will then be able to build executables that link against the libraries in /tools.

Make a couple of essential adjustments to GCC's specs to ensure GCC uses our build environment:

patch -Np1 -i ../gcc-7.1.0-pure64_specs-1.patch

Change the StartFile Spec so that GCC looks in /tools:

echo -en '\n#undef STANDARD_STARTFILE_PREFIX_1\n#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"\n' >> gcc/config/linux.h
echo -en '\n#undef STANDARD_STARTFILE_PREFIX_2\n#define STANDARD_STARTFILE_PREFIX_2 ""\n' >> gcc/config/linux.h

We will create a dummy limits.h so the build will not use the one provided by the host distro:

touch /tools/include/limits.h

The GCC documentation recommends building GCC outside of the source directory in a dedicated build directory:

mkdir -v ../gcc-build
cd ../gcc-build

Prepare GCC for compilation:

AR=ar \
LDFLAGS="-Wl,-rpath,/cross-tools/lib" \
../gcc-7.1.0/configure \
    --prefix=/cross-tools \
    --build=${CLFS_HOST} \
    --host=${CLFS_HOST} \
    --target=${CLFS_TARGET} \
    --with-sysroot=${CLFS} \
    --with-local-prefix=/tools \
    --with-native-system-header-dir=/tools/include \
    --disable-shared \
    --with-mpfr=/cross-tools \
    --with-gmp=/cross-tools \
    --with-isl=/cross-tools \
    --with-mpc=/cross-tools \
    --without-headers \
    --with-newlib \
    --disable-decimal-float \
    --disable-libgomp \
    --disable-libssp \
    --disable-libatomic \
    --disable-libitm \
    --disable-libsanitizer \
    --disable-libquadmath \
    --disable-libvtv \
    --disable-libcilkrts \
    --disable-libcilkrts \
    --disable-threads \
    --disable-multilib \
    --enable-languages=c \
    --with-abi=64 \
    --with-glibc-version=2.25

The meaning of the new configure options:

--build=${CLFS_HOST}

This specifies the system on which the cross-compiler is being built.

--with-local-prefix=/tools

The purpose of this switch is to remove /usr/local/include from gcc's include search path. This is not absolutely essential, however, it helps to minimize the influence of the host system.

--with-native-system-headers-dir=/tools/include

This switch ensures that GCC will search for the system headers in /tools/include and that host system headers will not be searched.

--disable-shared

This tells GCC not to create a shared library.

--without-headers

Disables GCC from using the target's Libc when cross compiling.

--with-newlib

This causes GCC to enable the inhibit_libc flag, which prevents libgcc from building code that uses libc support.

--disable-decimal-float

Disables support for the C decimal floating point extension.

--disable-lib*

These options prevent GCC from building a number of libraries that are not needed at this time.

--disable-threads

This will prevent GCC from looking for the multi-thread include files, since they haven't been created for this architecture yet. GCC will be able to find the multi-thread information after the Glibc headers are created.

--with-system-zlib

This tells GCC to link to the system-installed zlib instead of the one in its source tree.

--enable-languages=c

This option ensures that only the C compiler is built.

--with-glibc-version=2.25

Needed when bootstrapping a cross toolchain without the header files available for building the initial bootstrap compiler.

Continue with compiling the package:

make all-gcc all-target-libgcc

The meaning of the new make options:

all-gcc all-target-libgcc

Compiles only the parts of GCC that are needed at this time, rather than the full package.

Install the package:

make install-gcc install-target-libgcc

Details on this package are located in Section 10.18.2, “Contents of GCC.”