#! /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/cross | |
TARGET=aarch64-linux | |
USE_NEWLIB=0 | |
LINUX_ARCH=arm64 | |
CONFIGURATION_OPTIONS="--disable-multilib" # --disable-threads --disable-shared | |
PARALLEL_MAKE=-j4 | |
BINUTILS_VERSION=binutils-2.24 | |
GCC_VERSION=gcc-4.9.2 | |
LINUX_KERNEL_VERSION=linux-3.17.2 | |
GLIBC_VERSION=glibc-2.20 | |
MPFR_VERSION=mpfr-3.1.2 | |
GMP_VERSION=gmp-6.0.0a | |
MPC_VERSION=mpc-1.0.2 | |
ISL_VERSION=isl-0.12.2 | |
CLOOG_VERSION=cloog-0.18.1 | |
export PATH=$INSTALL_PATH/bin:$PATH | |
# 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 everything | |
for f in *.tar*; do tar xfk $f; done | |
# Make symbolic links | |
cd $GCC_VERSION | |
ln -sf `ls -1d ../mpfr-*/` mpfr | |
ln -sf `ls -1d ../gmp-*/` gmp | |
ln -sf `ls -1d ../mpc-*/` mpc | |
ln -sf `ls -1d ../isl-*/` isl | |
ln -sf `ls -1d ../cloog-*/` cloog | |
cd .. | |
# 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 | |
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!' |
This comment has been minimized.
This comment has been minimized.
Good. |
This comment has been minimized.
This comment has been minimized.
thanks |
This comment has been minimized.
This comment has been minimized.
thanks! |
This comment has been minimized.
This comment has been minimized.
Thank you!!! |
This comment has been minimized.
This comment has been minimized.
How to build the cross compiler for x86_64? I changed TARGET to "x86_64-linux-gnu", and LINUX_ARCH to "x86_64" in the script, but I got the follow error at the stage building glibc: x86_64-linux-gnu-gcc -D_RPC_THREAD_SAFE_ -D_GNU_SOURCE -DIS_IN_build -include /net/ala-rsubbian-lx1/ala-rsubbian-lx11/bpang/SHARE/GCC520/GIT/x86_64/build-glibc/c |
This comment has been minimized.
This comment has been minimized.
Is there a version to build gcc 5.3.x? |
This comment has been minimized.
This comment has been minimized.
used this guide to compile GCC 6.1 with latest GLibc and 4.6 Kernel , thank you for your efforts |
This comment has been minimized.
This comment has been minimized.
@Vladinator You should be able to build 5.3.x with this. If not, I've created a similar script with slightly higher levels of abstraction which definitely builds GCC 4.9.x, 5.3.x and 6.1: https://github.com/pwaring/gcc-cross-compiler |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Anyone get this to build fine on amd64? @pwaring, I don't see support for amd64 in that, is it possible? My current version of this script with some improvements: @BaoshanPang - my version of this script above seems to work until a point:
|
This comment has been minimized.
This comment has been minimized.
@thijstriemstra The blog post you link to isn't mine so I can't edit it. |
This comment has been minimized.
This comment has been minimized.
Thanks |
This comment has been minimized.
This comment has been minimized.
I am also facing same issue like... make[4]: *** [subsegs.o] Error 1 |
This comment has been minimized.
This comment has been minimized.
I have issue with PATH_MAX undefined while compiling last part of gcc (part6)
my configuration are :
any hints to fix? |
This comment has been minimized.
This comment has been minimized.
How to make it works with "arm android"? |
This comment has been minimized.
This comment has been minimized.
Nice script! Just had to change a few things to build a gcc cross compiler for mips. |
This comment has been minimized.
This comment has been minimized.
I am facing the following error after make -j4: cc1: all warnings being treated as errors |
This comment has been minimized.
This comment has been minimized.
I'm building for a RaspberryPi3 running Raspbian, so I want to build my cross-compiler for a 32-bit Linux target. (I'm on Mac OS X Sierra 10.12.4, if that matters.) Should I use Fairly new to this, so in addition to knowing the proper 32-bit target identifier for the RPi3, I'd like to know how/where to find the list of valid targets. Thank you for this and the blog article! |
This comment has been minimized.
This comment has been minimized.
Incidentally I'm here looking at the script, because in following the steps on your blog, I got an error on Step 4. I'm on Mac OS X Sierra 10.12.4. No errors (modulo having to unpack linux-4.4.38 to a case-sensitive filesystem in order for it to install properly) following Steps 1-3. At Step 4, though, I got the following:
I've tried several Google searches and a number of variations of the above Any ideas on how to fix this? -- Some background, in case it helps:
|
This comment has been minimized.
This comment has been minimized.
Disable libsanitizer by passing If you want to compile on macOS do a Canadian Cross Compile with a macOS to Linux ABI handover. I'm not sure how to explain. |
This comment has been minimized.
This comment has been minimized.
Amazing script. I was able to create a cross compiler for ARM, and then create a Kernel and BusyBox to boot up in QEMU. I have converted the script to a makefile under kanj/MAKE_CROSS_LINUX for reference. |
This comment has been minimized.
This comment has been minimized.
Very interesting script. On this basis, i create conan package https://github.com/BetterCallBene/conan-cgcc. |
This comment has been minimized.
This comment has been minimized.
Is it possible to build multi lib gcc i.e. with aarch64 and arm? |
This comment has been minimized.
This comment has been minimized.
This doesn't work for me trying to build gcc-4.8.5. It fails on configuring glibc (2.12.2) as it is using the newly built cross-compiled-gcc which cannot find stdio.h. I can resolve this by not exporting the PATH to the new binaries but then the csu binaries built will be for the host architecture. Not really sure how to resolve it. |
This comment has been minimized.
This comment has been minimized.
Anyone trying to use something other than Linux as the build/host platform may be helped by looking at my fork of this script. I used MSys2 64 bit (a Cygwin fork) under Win7 64 bit; I yet to test the cross compiler produced; did it as a learning exercise. Edit2: Once, I deleted the installed files my version of the script stopped building with gcc/glibc errors. Tim S. |
This comment has been minimized.
Love U !!