Skip to content

Instantly share code, notes, and snippets.

@stahta01
Forked from preshing/build_cross_gcc
Last active October 16, 2022 20:38
Show Gist options
  • Save stahta01/5d799d54069b851c7678b443efea25c6 to your computer and use it in GitHub Desktop.
Save stahta01/5d799d54069b851c7678b443efea25c6 to your computer and use it in GitHub Desktop.
A shell script to download packages for, configure, build and install a GCC cross-compiler.
#! /bin/bash
set -e
trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
trap 'echo FAILED COMMAND: $previous_command' EXIT
#-------------------------------------------------------------------------------------------
# This script will download packages for, configure, build and install a GCC cross-compiler.
# Customize the variables (INSTALL_PATH, TARGET, etc.) to your liking before running.
# If you get an error and need to resume the script from some point in the middle,
# just delete/comment the preceding lines before running it again.
#
# See: http://preshing.com/20141119/how-to-build-a-gcc-cross-compiler
#-------------------------------------------------------------------------------------------
INSTALL_PATH=/opt/local/cross
LINUX_ARCH=arm64
PARALLEL_MAKE=-j4
HAS_SYMLINK=0 # set to 1 for building under Linux or anywhere else that can do symlink
BUILD_CI_FS=1 # set to 0 for building under Linux or other case sensistive file systems
USE_NEWLIB=0
if [ $USE_NEWLIB -ne 0 ]; then
TARGET=aarch64-elf
CONFIGURATION_OPTIONS="--disable-multilib" # --disable-threads --disable-shared
else
TARGET=aarch64-linux
CONFIGURATION_OPTIONS="--disable-multilib" # --disable-threads --disable-shared
fi
BINUTILS_VERSION=binutils-2.29.1
GCC_VERSION=gcc-7.3.0
LINUX_KERNEL_VERSION=linux-3.17.2
GLIBC_VERSION=glibc-2.27
MPFR_VERSION=mpfr-3.1.4
GMP_VERSION=gmp-6.1.2
MPC_VERSION=mpc-1.0.3
ISL_VERSION=isl-0.16.1
CLOOG_VERSION=cloog-0.18.1
export PATH=$INSTALL_PATH/bin:$PATH
extract() {
local tarfile="$1"
local extracted="$(echo "$tarfile" | sed 's/\.tar.*$//')"
if [ ! -d "$extracted" ]; then
echo "Extracting ${tarfile}"
tar -xf $tarfile
fi
}
extract_to_gcc_folder() {
local tarfile="$1"
local subfolder="$(echo "$tarfile" | sed 's/-.*$//')"
if [ ! -d "$GCC_VERSION/$subfolder" ]; then
echo "Extracting ${tarfile} to $GCC_VERSION/$subfolder"
mkdir -p "$GCC_VERSION/$subfolder"
tar -x --strip-components=1 -f "$tarfile" -C "$GCC_VERSION/$subfolder"
fi
}
# Download packages
export http_proxy=$HTTP_PROXY https_proxy=$HTTP_PROXY ftp_proxy=$HTTP_PROXY
wget -nc https://ftp.gnu.org/gnu/binutils/$BINUTILS_VERSION.tar.gz
wget -nc https://ftp.gnu.org/gnu/gcc/$GCC_VERSION/$GCC_VERSION.tar.gz
if [ $USE_NEWLIB -ne 0 ]; then
wget -nc -O newlib-master.zip https://github.com/bminor/newlib/archive/master.zip || true
unzip -qo newlib-master.zip
else
wget -nc https://www.kernel.org/pub/linux/kernel/v3.x/$LINUX_KERNEL_VERSION.tar.xz
wget -nc https://ftp.gnu.org/gnu/glibc/$GLIBC_VERSION.tar.xz
fi
wget -nc https://ftp.gnu.org/gnu/mpfr/$MPFR_VERSION.tar.xz
wget -nc https://ftp.gnu.org/gnu/gmp/$GMP_VERSION.tar.xz
wget -nc https://ftp.gnu.org/gnu/mpc/$MPC_VERSION.tar.gz
wget -nc ftp://gcc.gnu.org/pub/gcc/infrastructure/$ISL_VERSION.tar.bz2
wget -nc ftp://gcc.gnu.org/pub/gcc/infrastructure/$CLOOG_VERSION.tar.gz
# Extract packages
extract $BINUTILS_VERSION.tar.gz
extract $GCC_VERSION.tar.gz
extract_to_gcc_folder $MPFR_VERSION.tar.xz
extract_to_gcc_folder $GMP_VERSION.tar.xz
extract_to_gcc_folder $MPC_VERSION.tar.gz
extract_to_gcc_folder $ISL_VERSION.tar.bz2
extract_to_gcc_folder $CLOOG_VERSION.tar.gz
if [ $USE_NEWLIB -eq 0 ]; then
extract $LINUX_KERNEL_VERSION.tar.xz || true
extract $GLIBC_VERSION.tar.xz || true
if [ $HAS_SYMLINK -eq 0 ]; then
cp $GLIBC_VERSION/benchtests/strcoll-inputs/filelist#en_US.UTF-8 $GLIBC_VERSION/benchtests/strcoll-inputs/filelist#C
fi
#else
# extract $NEWLIB_VERSION.tar.gz
fi
# Step 1. Binutils
mkdir -p build-binutils
cd build-binutils
../$BINUTILS_VERSION/configure --prefix=$INSTALL_PATH --target=$TARGET $CONFIGURATION_OPTIONS
make $PARALLEL_MAKE
make install
cd ..
# Step 2. Linux Kernel Headers
if [ $USE_NEWLIB -eq 0 ]; then
cd $LINUX_KERNEL_VERSION
make ARCH=$LINUX_ARCH INSTALL_HDR_PATH=$INSTALL_PATH/$TARGET headers_install
cd ..
fi
# Step 3. C/C++ Compilers
mkdir -p build-gcc
cd build-gcc
if [ $USE_NEWLIB -ne 0 ]; then
NEWLIB_OPTION=--with-newlib
fi
../$GCC_VERSION/configure --prefix=$INSTALL_PATH --target=$TARGET --enable-languages=c,c++ $CONFIGURATION_OPTIONS $NEWLIB_OPTION
make $PARALLEL_MAKE all-gcc
make install-gcc
cd ..
if [ $USE_NEWLIB -ne 0 ]; then
# Steps 4-6: Newlib
mkdir -p build-newlib
cd build-newlib
../newlib-master/configure --prefix=$INSTALL_PATH --target=$TARGET $CONFIGURATION_OPTIONS
make $PARALLEL_MAKE
make install
cd ..
else
# Step 4. Standard C Library Headers and Startup Files
cd $GLIBC_VERSION
if [ $BUILD_CI_FS -ne 0 ]; then
# Replace "oS" with "oZ" to avoid filename clashes
sed -i 's/.oS)/.oZ)/g; s/.oS$/.oZ/g; s/.oS =/.oZ =/g' Makeconfig
sed -i 's/.oS,/.oZ,/g; s/.oS +=/.oZ +=/g; s/.oS)/.oZ)/g' Makerules
sed -i 's/.oS)/.oZ)/g; s/.oS,/.oZ,/g' extra-lib.mk
sed -i 's/.oS)/.oZ)/g' nptl/Makefile
sed -i 's/.oS +=/.oZ +=/g' sysdeps/i386/i686/Makefile
sed -i 's/.oS,/.oZ,/g' sysdeps/ieee754/ldbl-opt/Makefile
sed -i 's/.oS +=/.oZ +=/g' sysdeps/sparc/sparc32/sparcv9/Makefile
sed -i 's/.oS +=/.oZ +=/g' sysdeps/sparc/sparc64/Makefile
sed -i 's/,oS}/,oZ}/g' scripts/check-local-headers.sh
fi
if [ $HAS_SYMLINK -eq 0 ]; then
# use copy because the rellns-sh has issues under msys2
sed -i 's|$(LN_S) `$(..)scripts/rellns-sh -p $< $@` $@|cp -p $< $@|' Makerules
fi
cd ..
mkdir -p build-glibc
cd build-glibc
../$GLIBC_VERSION/configure --prefix=$INSTALL_PATH/$TARGET --build=$MACHTYPE --host=$TARGET --target=$TARGET --with-headers=$INSTALL_PATH/$TARGET/include $CONFIGURATION_OPTIONS libc_cv_forced_unwind=yes
make install-bootstrap-headers=yes install-headers
make $PARALLEL_MAKE csu/subdir_lib
install csu/crt1.o csu/crti.o csu/crtn.o $INSTALL_PATH/$TARGET/lib
$TARGET-gcc -nostdlib -nostartfiles -shared -x c /dev/null -o $INSTALL_PATH/$TARGET/lib/libc.so
touch $INSTALL_PATH/$TARGET/include/gnu/stubs.h
cd ..
# Step 5. Compiler Support Library
cd build-gcc
make $PARALLEL_MAKE all-target-libgcc
make install-target-libgcc
cd ..
# Step 6. Standard C Library & the rest of Glibc
cd build-glibc
make $PARALLEL_MAKE
make install
cd ..
fi
# Step 7. Standard C++ Library & the rest of GCC
cd build-gcc
make $PARALLEL_MAKE all
make install
cd ..
trap - EXIT
echo 'Success!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment