GDC on Android
Note: This tutorial should give you a usable GDC for Android. GDC on Android is not supported in any way, and especially druntime and phobos may need some updates to be usable.
Binaries
http://www.mediafire.com/?2cex2faqx327l
Differences to normal GDC
- version(Android) is defined
- doesn't link -lpthread and -lrt by default (not available on android)
Note: Android/bionic does not claim to be posix compliant, so there's some work needed to make druntime & phobos work
Building a toolchain with gdc for the Android NDK
Linux host
The official Android NDK r7 uses an old GCC version (4.4.3) which isn't supported by GDC. We'll build a new toolchain based on GCC 4.6.2
Needed Packages
This tutorial was tested on Debian Testing. You'll need at least the following packages:
build-essential libncurses5-dev texinfo
Getting the sources
First we'll need a directory in which we'll put all our files. Just choose any directory and replace all instances of $BASE_DIR in the tutorial with your directory.
BASE_DIR=/home/jpf/android-ndk
Now let's download and patch the sources:
cd $BASE_DIR mkdir toolchain-src cd toolchain-src git clone https://android.googlesource.com/toolchain/build.git mkdir gmp && cd gmp wget ftp://ftp.gmplib.org/pub/gmp-5.0.3/gmp-5.0.3.tar.bz2 cd ../ mkdir gdb && cd gdb wget ftp://ftp.gnu.org/gnu/gdb/gdb-7.4.tar.bz2 tar xjf gdb-7.4.tar.bz2 cd ../ mkdir mpc && cd mpc wget http://www.multiprecision.org/mpc/download/mpc-0.9.tar.gz cd ../ mkdir mpfr && cd mpfr wget http://www.mpfr.org/mpfr-3.0.1/mpfr-3.0.1.tar.bz2 cd ../ mkdir binutils cd binutils/ wget http://sourceware.mirrors.tds.net/pub/sourceware.org/binutils/snapshots/binutils-2.22.51.tar.bz2 tar xjf binutils-2.22.51.tar.bz2 cd ../ wget http://gcc.gnu.org/bugzilla/attachment.cgi?id=24879 -O gcc-android.patch mkdir gcc cd gcc/ wget ftp://ftp.gnu.org//gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.bz2 tar xjf gcc-4.6.2.tar.bz2 cd gcc-4.6.2 patch -p1 -i ../../gcc-android.patch cd ../../
We'll now patch GCC to add support for D. Note: Skip this code snippet to simply build a GCC 4.6.2 toolchain without D. It's recommended to get a toolchain without D working first.
hg clone https://bitbucket.org/jpf/gdc cd gdc hg update android cd ../ cd gcc/gcc-4.6.2/ ln -s ../../../gdc/d gcc/d ./gcc/d/setup-gcc.sh cd ../../ wget https://raw.github.com/gist/1737240/08b7eaf3db7de800107c50ca84f379d587171818/gistfile1.diff -O build-d.patch cd build patch -p1 -i ../build-d.patch cd ../
Getting the android NDK
Now you'll have to download the official Android NDK r7 and extract it to BASE_DIR
cd $BASE_DIR wget http://dl.google.com/android/ndk/android-ndk-r7-linux-x86.tar.bz2 tar xjf android-ndk-r7-linux-x86.tar.bz2
We'll have to apply a small patch as well:
wget https://raw.github.com/gist/1737276/4af76432e838d7356d068568009f6adc3829cc01/gistfile1.diff -O build-mpc.patch cd android-ndk-r7 patch -p1 -i ../build-mpc.patch cd ../
Note: Your BASE_DIR directory should now look like this:
|-toolchain-src |---gdb |-----gdb-7.4 |---mpfr |---gcc |-----gcc-4.6.2 |---binutils |-----binutils-2.22.51 |---gdc |---gmp |---build |---mpc |-android-ndk-r7
Build the toolchain
cd android-ndk-r7/build/tools/ NUM_JOBS=1 DFLAGS="-fno-section-anchors" ./build-gcc.sh --gmp-version=5.0.3 --mpfr-version=3.0.1 --mpc-version=0.9 --binutils-version=2.22.51 --gdb-version=7.4 $BASE_DIR/toolchain-src $BASE_DIR/android-ndk-r7 arm-linux-androideabi-4.6.2
Once the build is finished, your toolchain is in BASE_DIR/android-ndk-r7/toolchains/arm-linux-androideabi-4.6.2/prebuilt/linux-x86
Windows host
We'll compile the windows toolchain on a linux host. Debian Testing was used to test this tutorial.
Follow the linux build instructions
Follow the linux instructions till the "Build the toolchain" section.
Additional packages
We'll need the following additional packages for the windows build:
gcc-mingw32 mingw32-binutils mingw32-runtime
Additional patches
We'll also need one additional patch:
cd $BASE_DIR/toolchain-src wget https://gist.github.com/raw/1752754/9d9c28a04a8c68192b5578a296fb2191954a545f/gistfile1.diff -O build-mingw.patch cd build patch -p3 -i ../build-mingw.patch cd ../
Depending on your build system, you'll have to apply another patch: if you have a **i386-linux-gnu-gcc** executable, you must not apply this patch. But if your gcc is called i486-linux-gnu-gcc you must apply it.
cd $BASE_DIR wget https://gist.github.com/raw/1752778/64e05067505211842a4bcb6dc574c799ac273b74/gistfile1.diff -O build-i486-host.patch cd android-ndk-r7 patch -p2 -i ../build-i486-host.patch cd ../
Build the toolchain
cd android-ndk-r7/build/tools/ NUM_JOBS=1 DFLAGS="-fno-section-anchors" ./build-gcc.sh --gmp-version=5.0.3 --mpfr-version=3.0.1 --mpc-version=0.9 --binutils-version=2.22.51 --gdb-version=7.4 --mingw $BASE_DIR/toolchain-src $BASE_DIR/android-ndk-r7 arm-linux-androideabi-4.6.2
Once the build is finished, your toolchain is in BASE_DIR/android-ndk-r7/toolchains/arm-linux-androideabi-4.6.2/prebuilt/windows
BTW: This build system can now be used to build both windows & linux binaries. Just add or remove --mingw from the ./build-gcc.sh command.
TODO
- Compiler
- Fix #120
- Run unittests
- druntime
Uses ~get_stack_base instead of ~libc_stack_end. It should be tested to verify it is working correctly
- Building gcc/config/unix.d fails, so the patch simply skips that file. This should be investigated further and fixed correctly.
- druntime depends on some functions which are not in bionic. It builds, but user apps don't link.
- phobos
- disabled for now
