Skip to content

Instantly share code, notes, and snippets.

@AndersonIncorp
Last active November 30, 2021 05:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndersonIncorp/82b1c3de8bb3a9732dafc3353f8205ed to your computer and use it in GitHub Desktop.
Save AndersonIncorp/82b1c3de8bb3a9732dafc3353f8205ed to your computer and use it in GitHub Desktop.
OS X to Linux gcc cross compiler build (arch linux x86_64 as target)
#!/bin/bash
set -e
# Prerequirements
# brew install gcc (gmp libmpc mpfr isl)
# copy /usr/lib && /usr/include from arch linux into your CC_ROOT.
# /usr/local/linux/usr/include
# /usr/local/linux/usr/lib
# /usr/local/linux/usr/local/include
# arch linux is target enviroment for this cross compiler
# x86_64 binutils && gcc
# https://git.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/binutils
# https://git.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/gcc
CC_ROOT=/usr/local/linux #installation folder
CC_TARGET=x86_64-pc-linux-gnu #i686-pc-linux-gnu
GIT_BINUTILS=git://sourceware.org/git/binutils-gdb.git #commit=2870b1ba
GIT_BINUTILS_HASH=2870b1ba
GIT_GCC=git://gcc.gnu.org/git/gcc.git #commit=4ca53f06ff7d346ef8021a23108f23a5406a0417
GIT_GCC_HASH=4ca53f06ff7d346ef8021a23108f23a5406a0417
# Folders
mkdir -p $CC_ROOT/src
cd $CC_ROOT/src
# Download binutils
if [[ ! -d binutils-gdb ]]; then
git clone $GIT_BINUTILS
cd binutils-gdb
git checkout $GIT_BINUTILS_HASH
cd ..
fi
# Download gcc
if [[ ! -d gcc ]]; then
git clone $GIT_GCC
cd gcc
git checkout $GIT_GCC_HASH
cd ..
fi
# binutils configure && make
export PATH="/usr/local/bin:$PATH"
export CC=/usr/local/bin/gcc-6
export CXX=/usr/local/bin/g++-6
export CPP=/usr/local/bin/cpp-6
export LD=/usr/local/bin/gcc-6
export PREFIX=$CC_ROOT
export TARGET=$CC_TARGET
export HOST=$(gcc-6 -dumpmachine 2>&1) #x86_64-apple-darwin16.3.0
if [[ ! -d $CC_ROOT/$CC_TARGET/bin ]]; then
mkdir -p ./binutils-build
cd ./binutils-build
../binutils-gdb/configure \
--host=$HOST \
--target=$TARGET \
--prefix=$PREFIX \
--with-sysroot=$PREFIX \
--enable-threads \
--enable-shared \
--with-pic \
--enable-ld=default \
--enable-gold \
--enable-plugins \
--enable-deterministic-archives \
--disable-werror \
--disable-gdb \
--disable-nls
make -j 8
make install
cd ..
fi
# gcc configure && make
export PATH="$PREFIX/bin:$PATH"
export GMP_PATH=$(brew --prefix gmp)
export MPFR_PATH=$(brew --prefix mpfr)
export MPC_PATH=$(brew --prefix libmpc)
export ISL_PATH=$(brew --prefix isl)
mkdir -p ./gcc-build
cd ./gcc-build
../gcc/configure \
--target=$TARGET \
--prefix=$PREFIX \
--with-sysroot=$PREFIX \
--with-gmp=$GMP_PATH \
--with-mpfr=$MPFR_PATH \
--with-mpc=$MPC_PATH \
--with-isl=$ISL_PATH \
--without-headers \
--enable-languages=c,c++ \
--enable-shared \
--enable-threads=posix \
--enable-libmpx \
--with-system-zlib \
--enable-__cxa_atexit \
--disable-libunwind-exceptions \
--enable-clocale=gnu \
--disable-libstdcxx-pch \
--disable-libssp \
--enable-gnu-unique-object \
--enable-linker-build-id \
--enable-lto \
--enable-plugin \
--enable-install-libiberty \
--with-linker-hash-style=gnu \
--enable-gnu-indirect-function \
--disable-multilib \
--disable-werror \
--enable-checking=release
make -j 8
make install
cd ..
#!/bin/sh
set -e
# Sync target /usr/include && /usr/lib && /usr/local/include
CC_ROOT=/usr/local/linux
TARGET=root@192.168.1.146
rsync -avzh --progress --delete --include='include' --include='include/**' --include='lib' --include='lib/**' --include='local' --include='local/**' --exclude='*' $TARGET:/usr/ $CC_ROOT/usr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment