Building LDC runtime libraries
Starting with version 1.4, LDC ships with a small build tool to allow you to recompile the runtime standard library (and optionally the accompanying testrunners) the way you want, ldc-build-runtime.
Use cases
- Enabling Link-Time Optimization (LTO) for the runtime libraries by recompiling them with -flto. Linking against these may result in significant performance gains and smaller binaries.
- Enabling sanitizer checks via -fsanitize.
- Cross-compilation/linking, as a set of existing druntime/Phobos libraries for the selected target platform is required to cross-compile and link D executables and shared libraries.
- This scenario also includes porting LDC to a new target platform, i.e., working on the runtime libraries on an arbitrary already supported host platform while possibly patching the host LDC in parallel (ABI etc.) until the runtime libraries can be successfully cross-built to the new target and ideally all testrunners run successfully (in an emulator or natively on the target).
Prerequisites
- CMake
- Either Make or Ninja (recommended, enable with --ninja)
- C toolchain (compiler, linker and libraries): gcc, clang, Microsoft Visual C++, …
How it works
If run without special command-line options, ldc-build-runtime automates:
- Creating a build directory
- Downloading & extracting the LDC source archive matching the LDC version
- Invoking CMake to generate the Makefile for the runtime libraries
- Invoking Make/Ninja to build the runtime libraries
Basic usage
The primary aim is to allow specifying additional compiler/linker command-line options and customizing CMake variables.
Run ldc-build-runtime -h for the full list of command-line options.
ldc-build-runtime [--ninja] [-j4] [--testrunners] [--dFlags=…] [--cFlags=…] [--linkerFlags=…] [CMAKE_VAR1=value1] [CMAKE_VAR2=value2 …]
E.g., to prepare for Link-Time-Optimization between your user code and the runtime libraries, you can recompile the static runtime libraries via:
ldc-build-runtime --ninja --dFlags="-flto=full" BUILD_SHARED_LIBS=OFF
OSX only: a ThinLTO version of the standard library will result in linker errors due to this issue: https://github.com/ldc-developers/ldc/issues/2312 . (this is not an issue if gold or lld is used)
Usage for cross-compilation
CC=cross-gcc ldc-build-runtime [--ninja] [-j4] [--testrunners] --dFlags="-mtriple=…;…" [--cFlags=…] [--linkerFlags=…] --targetSystem=… …
For example, to cross-compile from Linux/x86_64 to Linux/ARM:
CC=arm-linux-gnueabihf-gcc ldc-build-runtime --ninja --dFlags="-w;-mtriple=arm-linux-gnueabihf" --targetSystem="Linux;UNIX"
From Windows to Linux/ARM, e.g., by using an official Raspberry PI toolchain:
set CC=arm-linux-gnueabihf-gcc ldc-build-runtime --ninja --dFlags=-w;-mtriple=arm-linux-gnueabihf --targetSystem=Linux;UNIX CMAKE_SYSTEM_NAME=Linux CMAKE_C_COMPILER_WORKS=True BUILD_SHARED_LIBS=OFF
Cross-compiling to Android/ARM using the Android NDK is more involved, so LDC features a target preset for it. First, you set the path to your NDK and cross-compiler, and then you simply specify the target preset (while still being able to tweak the individual flags):
export NDK=/path/to/your/android-ndk-r15c export CC=$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/clang ldc-build-runtime --ninja [--testrunners] --targetPreset=Android-arm