Skip to content

Instantly share code, notes, and snippets.

@kuvaldini
Last active December 8, 2021 14:25
Show Gist options
  • Save kuvaldini/faafead8426be3bbdbad4b41fbf18669 to your computer and use it in GitHub Desktop.
Save kuvaldini/faafead8426be3bbdbad4b41fbf18669 to your computer and use it in GitHub Desktop.
Build Cross Compiler

Building Cross Compiler

  1. Environment

export TARGET=arm-linux-gnueabihf
export CROSS_PREFIX=/opt/crosstools/$TARGET
mkdir -p $CROSS_PREFIX
mkdir ~/src ~/build

  1. Build binutils - pass 1

cd ~/src
git clone git://sourceware.org/git/binutils-gdb.git --depth 1 --single-branch -b binutils-2_32

mkdir -p ~/build/binutils-arm-linux-gnueabihf
cd $_
~/src/binutils-gdb/configure  \
    --prefix=/opt/crosstools/arm-linux-gnueabihf  \
    --target=arm-linux-gnueabihf  \
    --disable-nls  \
    --disable-werror  \
    --with-lib-path=/opt/crosstools/arm-linux-gnueabihf/lib  \
    --with-sysroot=/opt/crosstools/arm-linux-gnueabihf/SYSROOT
make -sj$(nproc)
make install

  1. Build GCC-8.3.0 - pass 1

cd ~/src
git clone git://gcc.gnu.org/git/gcc.git --depth 1 --single-branch -b gcc-8_3_0-release
cd gcc
curl -L ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2 | tar xf -
curl -L ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2 | tar xf -
curl -L ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz | tar xf -
curl -L ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2 | tar xf -

http://www.linuxfromscratch.org/lfs/view/development/chapter05/gcc-pass1.html tell about fixes in configuration header for cross-compilation.

OR

cd ~/src
curl -LO https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-a/8.2-2019.01/gcc-arm-src-snapshot-8.2-2019.01.tar.xz
tar -xf gcc-arm-src-snapshot-8.2-2019.01.tar.xz
cd gcc-arm-src-snapshot-8.2-2019.01

Prepare GCC for compilation:

mkdir -p ~/build/gcc-arm-linux-gnueabihf
cd $_
~/src/gcc/configure                              \
    --target=arm-linux-gnueabihf                   \
    --prefix=/opt/crosstools/arm-linux-gnueabihf   \
    --disable-nls                                  \
    --with-glibc-version=2.11                      \
    --with-newlib                                  \
    --without-headers                              \
    --disable-shared                               \
    --disable-multilib                             \
    --disable-decimal-float                        \
    --disable-threads                              \
    --disable-libatomic                            \
    --disable-libgomp                              \
    --disable-libmpx                               \
    --disable-libquadmath                          \
    --disable-libssp                               \
    --disable-libvtv                               \
    --disable-libstdcxx                            \
    --enable-languages=c,c++                       \
    --with-local-prefix=/opt/crosstools/arm-linux-gnueabihf                     \
    --with-native-system-header-dir=/opt/crosstools/arm-linux-gnueabihf/include \
    --with-sysroot=/opt/crosstools/arm-linux-gnueabihf/SYSROOT

The meaning of the configure options:

  • --with-newlib
    Since a working C library is not yet available, this ensures that the inhibit_libc constant is defined when building libgcc. This prevents the compiling of any code that requires libc support.
  • --without-headers
    When creating a complete cross-compiler, GCC requires standard headers compatible with the target system. For our purposes these headers will not be needed. This switch prevents GCC from looking for them.
  • --with-local-prefix=/tools
    The local prefix is the location in the system that GCC will search for locally installed include files. The default is /usr/local. Setting this to /tools helps keep the host location of /usr/local out of this GCC's search path.
  • --with-native-system-header-dir=/tools/include
    By default GCC searches /usr/include for system headers. In conjunction with the sysroot switch, this would normally translate to $LFS/usr/include. However the headers that will be installed in the next two sections will go to $LFS/tools/include. This switch ensures that gcc will find them correctly. In the second pass of GCC, this same switch will ensure that no headers from the host system are found.
  • --disable-shared
    This switch forces GCC to link its internal libraries statically. We do this to avoid possible issues with the host system.
  • --disable-decimal-float, --disable-threads, --disable-libatomic, --disable-libgomp, --disable-libmpx, --disable-libquadmath, --disable-libssp, --disable-libvtv, --disable-libstdcxx
    These switches disable support for the decimal floating point extension, threading, libatomic, libgomp (Gnu OpenMP), libmpx, libquadmath, libssp (Stack Smashing Protector https://wiki.osdev.org/Stack_Smashing_Protector), libvtv, and the C++ standard library respectively. These features will fail to compile when building a cross-compiler and are not necessary for the task of cross-compiling the temporary libc.
  • --disable-multilib
    On x86_64, LFS does not yet support a multilib configuration. This switch is harmless for x86.
  • --enable-languages=c,c++
    This option ensures that only the C and C++ compilers are built. These are the only languages needed now.
make -sj$(nproc)
make install

  1. Linux-3.10 headers

cd ~/src
cp -r /amba-sdk/ambarella/kernel/linux-3.10 ~/src/
cd linux-3.10
make mrproper
make INSTALL_HDR_PATH=dest headers_install
cp -rv dest/include/* /opt/crosstools/arm-linux-gnueabihf/include/

  1. glibc-2.29

cd ~/src
git clone git://sourceware.org/git/glibc.git --depth 1 --single-branch -b glibc-2.29
mkdir -p ~/build/glibc-arm-linux-gnueabihf
cd $_
~/src/glibc/configure  \
    --prefix=/opt/crosstools/arm-linux-gnueabihf  \
    --host=arm-linux-gnueabihf  \
    --enable-kernel=3.10  \
    --with-headers=/opt/crosstools/arm-linux-gnueabihf/include

LINKS

Linux From Scratch - Version SVN-20190312

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment