Build LDC for Android

From D Wiki
Revision as of 10:04, 4 November 2015 by Joakim (talk | contribs) (First draft of how to build ldc/druntime/phobos)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This page will show you how to build a ldc cross-compiler for Android/ARM on linux, along with how to build and run both the druntime/phobos tests and an Android D app using the cross-compiler.

Almost all the druntime/phobos unit tests pass on Android/ARM. One of the native OpenGL sample apps from the Android NDK has been ported to D, I'll port some more soon. Remaining work to be done is listed last.

You can also try out dmd for Android/x86.


  • linux host, where you'll build ldc
    • You can use a virtual machine like VirtualBox/VMware, with at least 512 MB of memory and 1 GB of swap, particularly if building the phobos unit tests, and 10 GB of disk space.
  • C++ compiler and toolchain, to build ldc
  • Common development tools, such as CMake and git, and ldc uses libconfig++
  • ldc/druntime/phobos source
    • Get the source using git, as these Android patches have only been tested on the master branch of each repo.
  • llvm 3.6 source, either from the official release or git
    • llvm 3.7 or later will work too, but you'll have to update the small llvm patch so it still applies.
  • Android native toolchain, the NDK and optionally the SDK
    • The SDK is only necessary if you want to package a GUI app; the NDK is enough if you just want to build a command-line binary, such as a test runner. The "SDK Tools only" version of the SDK is all that's necessary, if you don't plan on using their IDE integration. I will only write about using the command-line tools. The SDK requires JDK 7: follow their instructions to make sure it's installed right.
  • Android/ARM, whether a device or emulator
    • The SDK comes with an emulator. I've only used actual hardware, so that's what I'll discuss.

Build llvm

Get the source for llvm, either the last official 3.6.2 release or a git repository like the official Android llvm, which has some modifications but shouldn't really change much. Download the patch for llvm, apply it, and then build llvm normally with the ARM target:

tar xvf llvm-3.6.2.src.tar.xz
cd llvm-3.6.2.src
curl -O
git apply android_tls

mkdir build
cd build
make -j5

Build ldc for Android/ARM

Clone the ldc repository, check out the same commits that I built ldc/druntime/phobos with, apply the Android patch, set the NDK environment variable to your NDK directory, and build ldc as usual:

cd ../..
git clone --recursive
cd ldc
git checkout -b android c769251cc
git submodule update
curl -O
git apply ldc_android_arm

mkdir build
cd build
export NDK=/path/to/your/android-ndk-r10e
cmake .. -DLLVM_CONFIG=../../llvm-3.6.2.src/build/bin/llvm-config
make ldc2 -j5

Download and apply the patch for druntime and the patch for phobos before building them:

cd ../runtime/druntime
curl -O
git apply druntime_ldc_arm

cd ../phobos
curl -O
git apply phobos_ldc_arm

cd ../../build
make druntime-ldc phobos2-ldc -j5

Build a command-line executable

Let's try building a classic benchmark.