GDC/Cross Compiler/Existing Sysroot

From D Wiki
Revision as of 10:36, 9 January 2016 by Jpf (talk | contribs) (Using a compiler from gdcproject.org/downloads: Add notes for debian, dub)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


This describes how to build or use a cross compiler if you have access to an existing system root (short sysroot). A sysroot is a folder which contains a minimal filesystem (especially libraries, the C library and header files). An example sysroot could look like this: 'sysroot/usr', 'sysroot/usr/lib', 'sysroot/usr/include'.

The sysroot

We assume that a sysroot is available at $SYSROOT. If the sysroot directory is inside the prefix where we'll install the compiler, the complete prefix directory can be moved to different locations. If it's not in the prefix directory GCC always uses the absolute path for the sysroot and if you move the sysroot the compiler will not work anymore.

Sysroot over SSH

If your target machine offers a fast SSH access it's possible to use the file system of the remote machine directly. This way the crosscompiler has full access to all libraries of the remote system. If a library is missing simply install it on the remote machine using the standard package manager.

You can mount a remote sysroot by using SSHFS:

sshfs -o idmap=user,follow_symlinks user@targethost:/ $SYSROOT

user@targethost is an SSH user / hostname account. $SYSROOT is the location where the sysroot will be mounted.

The follow_symlinks option is important. The remote filesystem will have absolute symlinks (/usr/lib/libdl.so --> /usr/lib/libdl.so.2) .follow_symlinks makes sure we really get access to the remote /usr/lib/libdl.so.2 and not to the local one.

Using a compiler from gdcproject.org/downloads

You can use the compilers from http://gdcproject.org/downloads with a SSH sysroot, but the system accessed over SSH must be similar to the system the cross-compilers "emulate". For example ArchlinuxARM systems are known to work, debian systems will not work (debian uses additional patches for binutils which were not compiled into the compilers from gdcproject.org).

To use such an compiler with the SSH sysroot, use the '--sysroot' switch:

./arm-gdcproject-linux-gnueabihf/bin/arm-gdcproject-linux-gnueabihf-gdc --sysroot=$SYSROOT test.d

Now you can link with any library installed on the remote machine:

./arm-gdcproject-linux-gnueabihf/bin/arm-gdcproject-linux-gnueabihf-gdc --sysroot=$SYSROOT test.d -lcurl


Note: When using a Debian based sysroot system, the compiler binaries from gdcproject.org might fail to find installed libraries. Use the following options for Debian to explicitly specify the path to libraries:

# Replace the arm-linux-gnueabihf depending on the compiler/target you use
./arm-gdcproject-linux-gnueabihf/bin/arm-gdcproject-linux-gnueabihf-gdc --sysroot=$SYSROOT -B$SYSROOT/usr/lib/arm-linux-gnueabihf test.d -lcurl

You may also have to explicitly pass the '-ldl' option for debian when using certain libraries.


You can also use dub to build any dub package with a cross compiler:

# Replace the arm-linux-gnueabihf depending on the compiler/target you use
DFLAGS="--sysroot=$SYSROOT -B$SYSROOT/usr/lib/arm-linux-gnueabi" dub build --compiler=arm-linux-gnueabi-gdc

Building a new compiler

Specifying a target and installation folder

export TARGET=arm-linux-gnueabi
export PREFIX={installation folder}
export SYSROOT={sysroot folder}

Replace {installation folder} with the absolute path to the folder the toolchain should be installed in.

GNU Binutils

# Delete existing binutils source archive and download a new one
#-------------------------------------------------------------------
export BINUTILS_NAME=binutils-2.24
export BINUTILS_SOURCE_ARCHIVE=$BINUTILS_NAME.tar.bz2
rm -f $BINUTILS_SOURCE_ARCHIVE
rm -rf $BINUTILS_NAME
wget http://ftpmirror.gnu.org/binutils/$BINUTILS_SOURCE_ARCHIVE

# Extract binutils
#-------------------------------------------------------------------
tar xjfv $BINUTILS_SOURCE_ARCHIVE

# Create binutils build directory
#-------------------------------------------------------------------
export BINUTILS_BUILD_DIR=binutils-build
rm -rf $BINUTILS_BUILD_DIR
mkdir $BINUTILS_BUILD_DIR

# Configure and build binutils
#-------------------------------------------------------------------
cd $BINUTILS_BUILD_DIR
../$BINUTILS_NAME/configure \
  --with-sysroot=$SYSROOT \
  --target=$TARGET   \
  --prefix=$PREFIX   \
  --disable-nls      \
  --disable-multilib \
  --with-gnu-as      \
  --with-gnu-ld      \
  --disable-libssp   
make -j4 all
make install
cd ..

Note: Debian targets

If you're target sysroot is a recent debian distribution (>= wheezy) you'll have to apply one of the debian patches to binutils. Download it from here: http://bazaar.launchpad.net/~doko/binutils/pkg-2.24-debian/view/head:/patches/129_multiarch_libpath.patch

You then have to set DEB_TARGET_MULTIARCH before building binutils:

export DEB_TARGET_MULTIARCH=arm-linux-gnueabi

The correct value can be obtained by executing this on the target:

dpkg-architecture -qDEB_HOST_MULTIARCH

Download GDC

# Download GDC
#-------------------------------------------------------------------
rm -rf gdc
mkdir gdc
git clone https://github.com/D-Programming-GDC/GDC.git gdc
cd gdc
git checkout gdc-4.8
cd ..

Download GCC

# Delete existing GCC source archive and download a new one
#-------------------------------------------------------------------
export GCC_NAME=gcc-4.8.2
export GCC_SOURCE_ARCHIVE=$GCC_NAME.tar.bz2
rm -f $GCC_SOURCE_ARCHIVE
rm -rf $GCC_NAME
wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/$GCC_NAME/$GCC_SOURCE_ARCHIVE

# Extract GCC
#-------------------------------------------------------------------
tar xjfv $GCC_SOURCE_ARCHIVE

Add GDC to GCC

# Add GDC to GCC
#-------------------------------------------------------------------
cd gdc
./setup-gcc.sh ../$GCC_NAME
cd ..

Build GCC

# Create GCC build directory
#-------------------------------------------------------------------
export GCC_BUILD_DIR=gcc-build
rm -rf $GCC_BUILD_DIR
mkdir $GCC_BUILD_DIR

# Configure and build GCC
#-------------------------------------------------------------------
cd $GCC_BUILD_DIR
../$GCC_NAME/configure --target=$TARGET --prefix=$PREFIX \
  --enable-languages=d     \
  --disable-bootstrap      \
  --disable-libssp         \
  --disable-libgomp        \
  --disable-libmudflap     \
  --disable-multilib       \
  --disable-decimal-float  \
  --disable-libffi         \
  --disable-libmudflap     \
  --disable-libquadmath    \
  --disable-libssp         \
  --disable-nls            \
  --with-gnu-as            \
  --with-gnu-ld            \
  --with-sysroot=$SYSROOT
make -j4
make install