Difference between revisions of "Cross-compiling with LDC"
m (→Limitations) |
m (→Tweaking the LDC configuration file: Reformat as table and minimally revise Android sections) |
||
Line 73: | Line 73: | ||
These settings will be used automatically when specifying a matching target triple, so that e.g. <code>ldc2 -mtriple=x86_64-windows-msvc foo.d</code> is enough to generate a <tt>foo.exe</tt> Win64 executable. | These settings will be used automatically when specifying a matching target triple, so that e.g. <code>ldc2 -mtriple=x86_64-windows-msvc foo.d</code> is enough to generate a <tt>foo.exe</tt> Win64 executable. | ||
− | + | Exemplary sections: | |
+ | {| class="wikitable" style="background-color: white;" | ||
+ | |- | ||
+ | | '''Win64'''<br><br>Assuming the <tt>lib</tt> directory from the prebuilt LDC windows-x64<br>package was copied to <tt><LDC root>/lib-win64</tt>. | ||
+ | | | ||
<pre> | <pre> | ||
"x86_64-.*-windows-msvc": | "x86_64-.*-windows-msvc": | ||
Line 86: | Line 90: | ||
}; | }; | ||
</pre> | </pre> | ||
− | + | |- | |
− | + | | '''Linux AArch64'''<br><br>Assuming the <tt>lib</tt> directory from the prebuilt LDC linux-aarch64<br>package was copied to <tt><LDC root>/lib-aarch64</tt> and the<br>cross-gcc executable is found via <tt>aarch64-linux-gnu-gcc</tt>. | |
+ | | | ||
<pre> | <pre> | ||
"aarch64-.*-linux-gnu": | "aarch64-.*-linux-gnu": | ||
Line 101: | Line 106: | ||
}; | }; | ||
</pre> | </pre> | ||
− | + | |- | |
− | + | | '''Android ARMv7-A'''<br><br>Assuming the <tt>lib</tt> directory from the prebuilt LDC android-armv7a<br>package was copied to <tt><LDC root>/lib-armv7a</tt> and the<br>Android NDK r21 has been unzipped into <tt>/home/me</tt>. | |
+ | | | ||
<pre> | <pre> | ||
"armv7a-.*-linux-android": | "armv7a-.*-linux-android": | ||
Line 109: | Line 115: | ||
"-defaultlib=phobos2-ldc,druntime-ldc", | "-defaultlib=phobos2-ldc,druntime-ldc", | ||
"-link-defaultlib-shared=false", | "-link-defaultlib-shared=false", | ||
− | "-gcc=/home/me/android-ndk- | + | "-gcc=/home/me/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang", |
− | |||
"-mcpu=cortex-a8", | "-mcpu=cortex-a8", | ||
]; | ]; | ||
Line 119: | Line 124: | ||
}; | }; | ||
</pre> | </pre> | ||
− | + | |- | |
− | + | | '''Android AArch64'''<br><br>Assuming the <tt>lib</tt> directory from the prebuilt LDC android-aarch64<br>package was copied to <tt><LDC root>/lib-aarch64</tt> and the<br>Android NDK r21 has been unzipped into <tt>/home/me</tt>. | |
+ | | | ||
<pre> | <pre> | ||
"aarch64-.*-linux-android": | "aarch64-.*-linux-android": | ||
Line 127: | Line 133: | ||
"-defaultlib=phobos2-ldc,druntime-ldc", | "-defaultlib=phobos2-ldc,druntime-ldc", | ||
"-link-defaultlib-shared=false", | "-link-defaultlib-shared=false", | ||
− | "-gcc=/home/me/android-ndk- | + | "-gcc=/home/me/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang", |
− | |||
]; | ]; | ||
lib-dirs = [ | lib-dirs = [ | ||
Line 136: | Line 141: | ||
}; | }; | ||
</pre> | </pre> | ||
+ | |- | ||
+ | |} | ||
== Cross-compiling with dub == | == Cross-compiling with dub == |
Revision as of 23:41, 15 May 2020
LDC is an implicit cross-compiler, i.e., if you have an LDC binary, you can cross-compile, and you don't need multiple LDC executables for each host → target combination. This page shows how to set up LDC for cross-compilation and -linking.
Contents
The -mtriple command-line option
The fundamental instrument for cross-compilation is LDC's -mtriple
command-line option (the corresponding clang switch is -target). It defines the target for code-generation, incl. architecture and operating system.
Here are some popular triples:
x86_64-linux-gnu
: Linux x86_64 with glibcx86_64-apple-darwin
: macOSx86_64-windows-msvc
: Windows x64i686-linux-musl
: Linux x86 with musli686-windows-msvc
: Windows x86armv6-linux-gnueabihf
: Linux ARMv6 with glibc and hard-float ABIarmv7a-unknown-linux-androideabi
: Android ARMv7-Aaarch64-linux-android
: Android AArch64aarch64-linux-gnu
: Linux AArch64 with glibcarm64-apple-ios12.0
: iOS arm64 ≥ 12.0wasm32-unknown-unknown-webassembly
: 32-bit WebAssembly
Run ldc2 -version
to check your default/host triple (Default target).
Cross-compiling
The supported targets depend on how the LLVM linked against your LDC was built (target architectures enabled in the CMake build command-line).
Running ldc2 -version
shows the list of enabled/registered LLVM backends.
ldc2 -mtriple=x86_64-windows-msvc -c foo.d
: generates a foo.obj COFF object file for Win64ldc2 -mtriple=x86_64-apple-darwin -c foo.d
: generates a foo.o Mach-O object file for macOSldc2 -mtriple=aarch64-linux-gnu -c foo.d
: generates a foo.o ELF object file for Linux AArch64
Generating static libraries
Generating suited static libraries (-lib
) works for all targets, on all hosts.
ldc2 -mtriple=x86_64-linux-gnu -lib foo.o
: generates a libfoo.a archiveldc2 -mtriple=x86_64-windows-msvc -lib foo.obj
: generates a foo.lib library
As long as your LLVM features the backend for your target, you can obviously directly cross-compile and -archive a library:
ldc2 -mtriple=… -lib foo.d
Cross-linking requires more work, as we have these requirements:
- Default libraries (e.g., druntime and Phobos) for the target, as (non-betterC) binaries are linked against these default libraries.
- C runtime libraries (and startup object files etc.) for the target, as druntime and Phobos don't completely reinvent the wheel and build on top of a C runtime (glibc, musl, Visual C++, Bionic, …). This also applies to
-betterC
binaries. - A cross-linker.
Default libraries
If there's a prebuilt LDC package for your desired target, then the simplest variant is to download it and copy the lib[32,64] subdirectories to your host LDC installation (or wherever you like).
If there's no prebuilt LDC package or you prefer cross-compiling druntime and Phobos yourself, check out Building LDC runtime libraries.
C runtime libraries and cross-linker
Windows targets
The WinSDK and Visual C++ libraries are included in the prebuilt LDC Windows packages since v1.13 (in the mingw subdirectory in the lib directories), so you're already set after copying the lib directories in the previous step.
For Windows targets, LLVM's LLD works nicely as cross-linker on all hosts. Official prebuilt LDC packages feature an integrated LLD and use it by default for Windows targets on non-Windows hosts.
Non-Apple POSIX targets
The preferred way is to install a gcc/clang toolchain for cross-compilation, e.g., the gcc-aarch64-linux-gnu package on Debian/Ubuntu hosts when targeting Linux AArch64, or the NDK for Android targets. These toolchains include the C libraries as well as a cross-linker-driver (e.g., aarch64-linux-gnu-gcc or aarch64-linux-android21-clang) configured for those libs and startup object files etc.
Tweaking the LDC configuration file
LDC needs information about where to find the target's libraries and which cross-linker to use. While you can specify that information on the command-line, it's tedious and error-prone, so the preferred way is to extend the etc/ldc2.conf configuration file by appending a section for your target triple.
These settings will be used automatically when specifying a matching target triple, so that e.g. ldc2 -mtriple=x86_64-windows-msvc foo.d
is enough to generate a foo.exe Win64 executable.
Exemplary sections:
Win64 Assuming the lib directory from the prebuilt LDC windows-x64 package was copied to <LDC root>/lib-win64. |
"x86_64-.*-windows-msvc": { switches = [ "-defaultlib=phobos2-ldc,druntime-ldc", "-link-defaultlib-shared=false", ]; lib-dirs = [ "%%ldcbinarypath%%/../lib-win64", ]; }; |
Linux AArch64 Assuming the lib directory from the prebuilt LDC linux-aarch64 package was copied to <LDC root>/lib-aarch64 and the cross-gcc executable is found via aarch64-linux-gnu-gcc. |
"aarch64-.*-linux-gnu": { switches = [ "-defaultlib=phobos2-ldc,druntime-ldc", "-gcc=aarch64-linux-gnu-gcc", ]; lib-dirs = [ "%%ldcbinarypath%%/../lib-aarch64", ]; rpath = "%%ldcbinarypath%%/../lib-aarch64"; }; |
Android ARMv7-A Assuming the lib directory from the prebuilt LDC android-armv7a package was copied to <LDC root>/lib-armv7a and the Android NDK r21 has been unzipped into /home/me. |
"armv7a-.*-linux-android": { switches = [ "-defaultlib=phobos2-ldc,druntime-ldc", "-link-defaultlib-shared=false", "-gcc=/home/me/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang", "-mcpu=cortex-a8", ]; lib-dirs = [ "%%ldcbinarypath%%/../lib-armv7a", ]; rpath = ""; }; |
Android AArch64 Assuming the lib directory from the prebuilt LDC android-aarch64 package was copied to <LDC root>/lib-aarch64 and the Android NDK r21 has been unzipped into /home/me. |
"aarch64-.*-linux-android": { switches = [ "-defaultlib=phobos2-ldc,druntime-ldc", "-link-defaultlib-shared=false", "-gcc=/home/me/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang", ]; lib-dirs = [ "%%ldcbinarypath%%/../lib-aarch64", ]; rpath = ""; }; |
Cross-compiling with dub
Starting with dub v1.18 (or v1.17+ bundled with official LDC v1.18 packages), you can cross-compile entire dub projects incl. their dependencies by simply adding --arch=<LDC triple>
in the dub command-line, after setting up your etc/ldc2.conf configuration file as detailed above.
Limitations
LDC doesn't have a software compile-time real for arbitrary target real
precision yet, but uses the host's real for storage and CTFE (exception: it uses 80-bit x87 compile-time precision on Windows/MSVC hosts). So if the target real features a higher precision (e.g., 32-bit ARM → x86, x86 → non-Apple AArch64), real.max
will overflow to infinity, real.min_normal
may underflow to 0, CTFE computations will be performed with lower host precision etc.