Difference between revisions of "GDC/Installation/Android"

From D Wiki
Jump to: navigation, search
m
 
(2 intermediate revisions by one other user not shown)
Line 2: Line 2:
  
 
= GDC on Android =
 
= 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.
+
Note: This tutorial should give you a usable GDC for Android. GDC on Android is not supported and especially phobos may need some updates to be usable.
  
 
= Binaries =
 
= Binaries =
http://www.mediafire.com/?2cex2faqx327l
+
Not yet available
  
= Differences to normal GDC =
+
= Building a toolchain with GDC for the Android NDK =
* 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 ==
 
== 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
+
Install all packages required for building the NDK. Then:
 
 
=== Needed Packages ===
 
This tutorial was tested on Debian Testing. You'll need at least the following packages:
 
<syntaxhighlight lang="bash">
 
build-essential libncurses5-dev texinfo
 
</syntaxhighlight>
 
 
 
=== 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. 
 
<syntaxhighlight lang="bash">
 
BASE_DIR=/home/jpf/android-ndk
 
</syntaxhighlight>
 
 
 
Now let's download and patch the sources:
 
<syntaxhighlight lang="bash">
 
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 ../../
 
</syntaxhighlight>
 
  
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.
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
hg clone https://bitbucket.org/jpf/gdc
+
mkdir workdir
cd gdc
+
cd workdir
hg update android
+
git clone https://android.googlesource.com/platform/ndk
cd ../
+
git clone https://android.googlesource.com/platform/development
 
+
mkdir src
cd gcc/gcc-4.6.2/
+
mkdir dest
ln -s ../../../gdc/d gcc/d
+
./ndk/build/tools/download-toolchain-sources.sh /home/build/workdir/src
./gcc/d/setup-gcc.sh
+
./ndk/build/tools/gen-platforms.sh --minimal
cd ../../
 
  
wget https://raw.github.com/gist/1737240/08b7eaf3db7de800107c50ca84f379d587171818/gistfile1.diff -O build-d.patch
+
git clone git@github.com:D-Programming-GDC/GDC.git
cd build
+
cd GDC
patch -p1 -i ../build-d.patch
+
git checkout gdc-4.9
 +
./setup-gcc.sh /home/build/workdir/src/gcc/gcc-4.9
 
cd ../
 
cd ../
</syntaxhighlight>
 
 
=== Getting the android NDK ===
 
Now you'll have to download the official Android NDK r7 and extract it to BASE_DIR
 
<syntaxhighlight lang="bash">
 
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
 
</syntaxhighlight>
 
  
We'll have to apply a small patch as well:
+
./ndk/build/tools/build-gcc.sh /home/build/workdir/src /home/build/workdir/dest arm-linux-androideabi-4.9 --verbose --try-64
<syntaxhighlight lang="bash">
 
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 ../
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Note: Your BASE_DIR directory should now look like this:
+
=== Building unittests ===
 
 
|-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 ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
cd android-ndk-r7/build/tools/
+
cd /tmp/ndk-/build/toolchain/gcc-4.9/arm-linux-androideabi/libphobos/libdruntime
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
+
make unittest
 +
cd /tmp/ndk-/build/toolchain/gcc-4.9/arm-linux-androideabi/libphobos/src
 +
make unittest
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Once the build is finished, your toolchain is in BASE_DIR/android-ndk-r7/toolchains/arm-linux-androideabi-4.6.2/prebuilt/linux-x86
+
=== Debugging help ===
 
 
== 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:
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
gcc-mingw32 mingw32-binutils mingw32-runtime
+
adb push unittest /data/local/tmp
</syntaxhighlight>
+
# gdbserver is in standard NDK builds
 +
adb push prebuilt/android-arm/gdbserver/gdbserver /data/local/tmp
 +
adb forward tcp:5039 tcp:5039
  
=== Additional patches ===
+
adb shell
We'll also need one additional patch:
+
su
 
+
cd /data/local/tmp
<syntaxhighlight lang="bash">
+
./unittest
cd $BASE_DIR/toolchain-src
+
./gdbserver :5039 unittest
wget https://gist.github.com/raw/1752754/9d9c28a04a8c68192b5578a296fb2191954a545f/gistfile1.diff -O build-mingw.patch
 
cd build
 
patch -p3 -i ../build-mingw.patch
 
cd ../
 
</syntaxhighlight>
 
  
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.
+
./toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gdb unittest
<syntaxhighlight lang="bash">
+
<gdb> target extended-remote :5039
cd $BASE_DIR
+
<gdb> continue
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 ../
 
</syntaxhighlight>
 
  
=== Build the toolchain ===
+
# Usually android devices dont have hardware watchpoints, to disable:
<syntaxhighlight lang="bash">
+
<gdb> set can-use-hw-watchpoints 0
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
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
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 =
 
= 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
 
* phobos
** disabled for now
+
* shared libraries
 +
* GC: We currently scan the data segment using etext and _end symbols. This doesn't work for dynamic libraries and it sometimes doesn't work for static libraries: In some cases there's a gap between etext and the start of the data section. Accessing this gap results in a segfault on some systems. Solution is to implement sections_elf for Android.
 +
 
 +
[[Category:GDC Compiler]]
 +
[[Category:Android]]

Latest revision as of 03:14, 19 March 2018


GDC on Android

Note: This tutorial should give you a usable GDC for Android. GDC on Android is not supported and especially phobos may need some updates to be usable.

Binaries

Not yet available

Building a toolchain with GDC for the Android NDK

Linux host

Install all packages required for building the NDK. Then:

mkdir workdir
cd workdir
git clone https://android.googlesource.com/platform/ndk
git clone https://android.googlesource.com/platform/development
mkdir src
mkdir dest
./ndk/build/tools/download-toolchain-sources.sh /home/build/workdir/src
./ndk/build/tools/gen-platforms.sh --minimal

git clone git@github.com:D-Programming-GDC/GDC.git
cd GDC
git checkout gdc-4.9
./setup-gcc.sh /home/build/workdir/src/gcc/gcc-4.9
cd ../

./ndk/build/tools/build-gcc.sh /home/build/workdir/src /home/build/workdir/dest arm-linux-androideabi-4.9 --verbose --try-64

Building unittests

cd /tmp/ndk-/build/toolchain/gcc-4.9/arm-linux-androideabi/libphobos/libdruntime
make unittest
cd /tmp/ndk-/build/toolchain/gcc-4.9/arm-linux-androideabi/libphobos/src
make unittest

Debugging help

adb push unittest /data/local/tmp
# gdbserver is in standard NDK builds
adb push prebuilt/android-arm/gdbserver/gdbserver /data/local/tmp 
adb forward tcp:5039 tcp:5039

adb shell
su
cd /data/local/tmp
./unittest
./gdbserver :5039 unittest

./toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gdb unittest
<gdb> target extended-remote :5039
<gdb> continue

# Usually android devices dont have hardware watchpoints, to disable:
<gdb> set can-use-hw-watchpoints 0

TODO

  • phobos
  • shared libraries
  • GC: We currently scan the data segment using etext and _end symbols. This doesn't work for dynamic libraries and it sometimes doesn't work for static libraries: In some cases there's a gap between etext and the start of the data section. Accessing this gap results in a segfault on some systems. Solution is to implement sections_elf for Android.