Build GCC

Today, let's try to build the MOST RECENT GCC 9.1. We strictly follow the TERRIFIC blog How to Build a GCC Cross-Compiler with trivial modifications for my x86_64 desktop instead of aarch64 cross compile.

Preparations

Packages Required on HOST

Some packages are required to install on the host computer, including: * g++ * make * gawk * bison * flex * expect * gnat etc.

Get ALL Packages for Building GCC

A wget-list.txt file is provided to download ALL required packages up-to-date. After having downloaded this single text file, the UNIQUE command to download all files is as follows:

1
➜  gcc ✗ wget -i wget-list.txt

You can of course download the files one by one. All concerned files are listed below: * https://gnu.freemirror.org/gnu/binutils/binutils-2.32.tar.gz * https://gnu.freemirror.org/gnu//gcc/gcc-9.1.0/gcc-9.1.0.tar.gz * https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.1.12.tar.gz * https://ftp.gnu.org/glibc/glibc-2.29.tar.xz * https://ftp.gnu.org/mpfr/mpfr-4.0.2.tar.xz * https://ftp.gnu.org/gmp/gmp-6.1.2.tar.xz * https://ftp.gnu.org/mpc/mpc-1.1.0.tar.gz * ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2 * ftp://gcc.gnu.org/pub/gcc/infrastructure/cloog-0.18.1.tar.gz

Then, we extract all .tar files in whatever way you prefer.

1
2
3
4
5
6
7
➜  cd gcc-9.1.0
➜ gcc-9.1.0 ln -s ../mpfr-4.0.2 mpfr
➜ gcc-9.1.0 ln -s ../gmp-6.1.2 gmp
➜ gcc-9.1.0 ln -s ../mpc-1.1.0 mpc
➜ gcc-9.1.0 ln -s ../isl-0.18 isl
➜ gcc-9.1.0 ln -s ../cloog-0.18.1 cloog
➜ gcc-9.1.0 cd ..

Prepare Build Target Folder

1
2
➜  gcc mkdir -p /opt/native
➜ gcc export PATH=/opt/native/bin:$PATH

Start Building

Binutils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
➜  gcc mkdir build-binutils
➜ gcc cd build-binutils
➜ build-binutils ../binutils-2.32/configure --prefix=/opt/native --target=x86_64-linux-gnu --enable-vtable-verify
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether ln works... yes
checking whether ln -s works... yes
checking for a sed that does not truncate output... /usr/bin/sed
checking for gawk... gawk
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking whether g++ accepts -static-libstdc++ -static-libgcc... yes
checking for gnatbind... gnatbind
checking for gnatmake... gnatmake
checking whether compiler driver understands Ada... yes
checking how to compare bootstrapped objects... cmp --ignore-initial=16 $$f1 $$f2
checking for objdir... .libs
checking for isl 0.15 or later... no
required isl version is 0.15 or later
checking for default BUILD_CONFIG...
checking for --enable-vtable-verify... yes
checking for bison... bison -y
checking for bison... bison
checking for gm4... no
checking for gnum4... no
checking for m4... m4
checking for flex... flex
checking for flex... flex
checking for makeinfo... makeinfo
checking for expect... expect
checking for runtest... no
checking for ar... ar
checking for as... as
checking for dlltool... no
checking for ld... ld
checking for lipo... no
checking for nm... nm
checking for ranlib... ranlib
checking for strip... strip
checking for windres... no
checking for windmc... no
checking for objcopy... objcopy
checking for objdump... objdump
checking for readelf... readelf
checking for x86_64-linux-gnu-cc... no
checking for cc... cc
checking for x86_64-linux-gnu-c++... no
checking for c++... c++
checking for x86_64-linux-gnu-gcc... x86_64-linux-gnu-gcc
checking for x86_64-linux-gnu-gfortran... x86_64-linux-gnu-gfortran
checking for x86_64-linux-gnu-gccgo... no
checking for gccgo... no
checking for x86_64-linux-gnu-ar... x86_64-linux-gnu-ar
checking for x86_64-linux-gnu-as... x86_64-linux-gnu-as
checking for x86_64-linux-gnu-dlltool... no
checking for dlltool... no
checking for x86_64-linux-gnu-ld... x86_64-linux-gnu-ld
checking for x86_64-linux-gnu-lipo... no
checking for lipo... no
checking for x86_64-linux-gnu-nm... x86_64-linux-gnu-nm
checking for x86_64-linux-gnu-objcopy... x86_64-linux-gnu-objcopy
checking for x86_64-linux-gnu-objdump... x86_64-linux-gnu-objdump
checking for x86_64-linux-gnu-ranlib... x86_64-linux-gnu-ranlib
checking for x86_64-linux-gnu-readelf... x86_64-linux-gnu-readelf
checking for x86_64-linux-gnu-strip... x86_64-linux-gnu-strip
checking for x86_64-linux-gnu-windres... no
checking for windres... no
checking for x86_64-linux-gnu-windmc... no
checking for windmc... no
checking where to find the target ar... just compiled
checking where to find the target as... just compiled
checking where to find the target cc... host tool
checking where to find the target c++... host tool
checking where to find the target c++ for libstdc++... host tool
checking where to find the target dlltool... just compiled
checking where to find the target gcc... host tool
checking where to find the target gfortran... host tool
checking where to find the target gccgo... host tool
checking where to find the target ld... just compiled
checking where to find the target lipo... host tool
checking where to find the target nm... just compiled
checking where to find the target objcopy... just compiled
checking where to find the target objdump... just compiled
checking where to find the target ranlib... just compiled
checking where to find the target readelf... just compiled
checking where to find the target strip... just compiled
checking where to find the target windres... just compiled
checking where to find the target windmc... just compiled
checking whether to enable maintainer-specific portions of Makefiles... no
configure: creating ./config.status
config.status: creating Makefile
➜ build-binutils make -j4
......
➜ build-binutils make install
➜ build-binutils cd ..

Linux Kernel Headers

1
2
3
➜  gcc cd linux-5.1.12
➜ linux-5.1.12 make ARCH=x86 INSTALL_HDR_PATH=/opt/native/linux headers_install
➜ linux-5.1.12 cd ..

C/C++ Compilers

1
2
3
4
5
6
➜  gcc mkdir -p build-gcc
➜ gcc cd build-gcc
➜ build-gcc ../gcc-9.1.0/configure --prefix=/opt/native --target=x86_64-linux-gnu --enable-languages=c,c++,fortran,go --disable-multilib --enable-vtable-verify
➜ build-gcc make -j4 all-gcc
➜ build-gcc make install-gcc
➜ build-gcc cd ..

GLibc

1
2
3
4
5
6
7
8
9
10
➜  gcc mkdir -p build-glibc
➜ gcc cd build-glibc
➜ build-glibc ../glibc-2.29/configure --prefix=/opt/native/linux --build=$MACHTYPE --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-headers=/opt/native/linux/include --disable-multilib --with-selinux=no --enable-vtable-verify libc_cv_forced_unwind=yes
➜ build-glibc make install-bootstrap-headers=yes install-headers
➜ build-glibc make -j4 csu/subdir_lib
➜ build-glibc mkdir /opt/native/linux/lib
➜ build-glibc install csu/crt1.o csu/crti.o csu/crtn.o /opt/native/linux/lib
➜ build-glibc /opt/native/bin/x86_64-linux-gnu-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o /opt/native/linux/lib/libc.so
➜ build-glibc touch /opt/native/linux/include/gnu/stubs.h
➜ build-glibc cd ..

Compiler Support Library

1
2
3
4
➜  gcc cd build-gcc
➜ build-gcc make -j4 all-target-libgcc
➜ build-gcc make install-target-libgcc
➜ build-gcc cd ..

Build Standard C Library

1
2
3
4
➜  gcc cd build-glibc
➜ build-glibc make -j4
➜ build-glibc make install
➜ build-glibc cd ..

Build Standard C++ Library

1
2
3
4
➜  gcc cd build-gcc
➜ build-gcc make -j4
➜ build-gcc make install
➜ build-gcc cd ..

Finishing and Test

Check GCC Version

1
2
3
4
5
6
7
8
➜  build-gcc x86_64-linux-gnu-c++ -v
Using built-in specs.
COLLECT_GCC=x86_64-linux-gnu-c++
COLLECT_LTO_WRAPPER=/opt/native/libexec/gcc/x86_64-linux-gnu/9.1.0/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../gcc-9.1.0/configure --prefix=/opt/native --target=x86_64-linux-gnu --enable-languages=c,c++,python,fortran,go --disable-multilib --enable-vtable-verify
Thread model: posix
gcc version 9.1.0 (GCC)

Run Hello World

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜  GCC cat test.cpp 
#include <iostream>

int main(int argc, char** argv)
{
std::cout << "Hello World!" << std::endl;
return EXIT_SUCCESS;
}
➜ GCC x86_64-linux-gnu-c++ -std=c++17 test.cpp
➜ GCC x86_64-linux-gnu-c++ -std=c++17 test.cpp -o test
➜ GCC ls
a.out test test.cpp
➜ GCC ./test
Hello World!
➜ GCC