Skip to content

Instantly share code, notes, and snippets.

@mmsatari
Forked from bijanebrahimi/cross-compiler.sh
Last active September 24, 2017 19:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmsatari/489e88502fbdf6e2851160b92a5cba12 to your computer and use it in GitHub Desktop.
Save mmsatari/489e88502fbdf6e2851160b92a5cba12 to your computer and use it in GitHub Desktop.
build a linux cross compiler for FreeBSD targets
#!/bin/bash
# http://marcelog.github.io/articles/cross_freebsd_compiler_in_linux.html
# http://linux-tips.com/t/how-to-build-a-freebsd-toolchain-for-linux/288
export TARGET=amd64-marcel-freebsd9.2
export PREFIX=/usr/cross-build
export TARGET_PREFIX=$PREFIX/$TARGET
export PATH=$PATH:$PREFIX/bin
mkdir -p $TARGET_PREFIX{,/lib,/include}
mkdir build-{binutils,gmp,mpfr,mpc,gcc}
# Copy FreeBSD /usr/{include,lib} in $TARGET_PREFIX
echo "Please copy FreeBSD's /usr/{include,lib} to $TARGET_PREFIX"
# BINUTILS
wget https://ftp.gnu.org/gnu/binutils/binutils-2.27.tar.gz
tar zxvf binutils-2.27.tar.gz
cd build-binutils
../binutils-2.27/configure --target=$TARGET --prefix=$PREFIX -v
make -j 4
sudo make install
cd ..
# GMP
wget https://ftp.gnu.org/gnu/gmp/gmp-6.1.1.tar.xz
tar xvf gmp-6.1.1.tar.xz
cd build-gmp
../gmp-6.1.1/configure --prefix=$PREFIX --enable-shared --enable-static --enable-mpbsd --enable-fft --enable-cxx --host=$TARGET
make -j 4
sudo make install
cd ..
# MPFR
wget https://ftp.gnu.org/gnu/mpfr/mpfr-3.1.5.tar.xz
tar xvf mpfr-3.1.5.tar.xz
cd build-mpfr
../mpfr-3.1.5/configure --prefix=$PREFIX --with-gnu-ld --with-gmp=$PREFIX --enable-static --enable-shared --host=$TARGET
make -j 4
sudo make install
cd ..
# MPC
wget https://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz
tar zxvf mpc-1.0.3.tar.gz
cd build-mpc
../mpc-1.0.3/configure --prefix=$PREFIX --with-gnu-ld --with-gmp=$PREFIX --with-mpfr=$PREFIX --enable-static --enable-shared --host=$TARGET
make -j 4
sudo make install
cd ..
# GCC
wget https://ftp.gnu.org/gnu/gcc/gcc-6.2.0/gcc-6.2.0.tar.gz
tar zxvf gcc-6.2.0.tar.gz
cd build-gcc
../gcc-6.2.0/configure --without-headers --with-gnu-as --with-gnu-ld --enable-languages=c,c++ --disable-nls --enable-libssp --enable-gold --enable-ld --target=$TARGET --prefix=$PREFIX --with-gmp=$PREFIX --with-mpc=$PREFIX --with-mpfr=$PREFIX --disable-libgomp
LD_LIBRARY_PATH=$PREFIX/lib make -j 4
sudo make install
cd ..
# DONE!
gcc helloworld.c -o helloworld-linux
LD_LIBRARY_PATH=$PREFIX/lib amd64-marcel-freebsd9.2-gcc helloworld.c -o helloworld-freebsd
# GDB
wget https://ftp.gnu.org/gnu/gdb/gdb-7.11.tar.xz
tar xvf gdb-7.11.tar.xz
../gdb-7.11.1/configure --prefix=$PREFIX --target=$TARGET
make -j 4
sudo make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment