Testing out the new cross-compilation support in ldc master

Joakim dlang at joakim.fea.st
Mon Sep 18 08:10:59 UTC 2017


On Friday, 18 August 2017 at 07:34:04 UTC, Joakim wrote:
> 5. Finally, you use this new tool to cross-compile the standard 
> library and possibly the test runners for your target.  Here's 
> an example from the comments of that PR, for Android/ARM, all 
> one command that I've expanded for legibility:
>
> CC=/home/joakim/ndk-r15c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
>
> ./bin/ldc-build-runtime --ldcSrcDir=/home/joakim/ldc 
> BUILD_SHARED_LIBS=OFF C_SYSTEM_LIBS="m;c"
>
> --cFlags="-gcc-toolchain;/home/joakim/ndk-r15c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64;-fpic;-ffunction-sections;-funwind-tables;-fstack-protector-strong;-Wno-invalid-command-line-argument;-Wno-unused-command-line-argument;-no-canonical-prefixes;-fno-integrated-as;-target;armv7-none-linux-androideabi;-march=armv7-a;-mfloat-abi=softfp;-mfpu=vfpv3-d16;-mthumb;-Os;-g;-DNDEBUG;-fomit-frame-pointer;-fno-strict-aliasing;-DANDROID;-Wa,--noexecstack;-Wformat;-Werror=format-security;-isystem;/home/joakim/ndk-r15c/platforms/android-21/arch-arm/usr/include"
>
> --linkerFlags="-Wl,-z,nocopyreloc;--sysroot=/home/joakim/ndk-r15c/platforms/android-21/arch-arm;-lgcc;-gcc-toolchain;/home/joakim/ndk-r15c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64;-target;armv7-none-linux-androideabi;-no-canonical-prefixes;-fuse-ld=bfd;-Wl,--fix-cortex-a8;-Wl,--no-undefined;-Wl,-z,noexecstack;-Wl,-z,relro;-Wl,-z,now;-Wl,--warn-shared-textrel;-Wl,--fatal-warnings;-fPIE;-pie"
>
> --dFlags="-w;-mtriple=armv7-none-linux-android" --ninja 
> --testrunners
>
> CC specifies the path to your C cross-compiler, which is 
> assumed to invoke the right linker for your target.  For 
> Android/ARM, replace all uses of /home/joakim/ndk-r15c with the 
> path to your NDK.  I think you have to specify --ldcSrcDir, the 
> path to your ldc source, since you're building from the git 
> repo.  The Android port doesn't support building the stdlib as 
> a shared library yet, so I turn it off.  It also uses a 
> different set of C system libraries, because Android includes 
> pthreads in libc, for example.
>
> The --cFlags and --linkerFlags were taken from running the 
> Android ndk-build script in verbose mode.  Use --dFlags to set 
> the llvm target triple for your platform, as I do here for 
> Android/ARM.  Leave off --ninja if you'd like to use Make 
> instead, same for --testrunners if you don't want to run the 
> stdlib tests.  Run ./ldc-build-runtime -h to see the full list 
> of flags.

I have updated the Android wiki page with full instructions on 
using the 1.4 release to cross-compile to Android/ARM from linux 
and Windows PCs:

https://wiki.dlang.org/Build_D_for_Android

The giant ldc-build-runtime command above has been integrated 
into the CMake config and boiled down to just `ldc-build-runtime 
--targetPreset=Android-arm`:

https://wiki.dlang.org/Build_D_for_Android#linux

If someone can try cross-compiling from a mac, which I haven't 
had in almost a decade, by using the Android NDK and either 
filling in the equivalent commands on the wiki or posting them 
here, that should complete it.

> Once that builds, assuming all goes well, try downloading 
> druntime-test-runner and phobos2-test-runner to a terminal 
> emulator on your Android/ARM device and running them, if you 
> built the test runners.  Otherwise, you can follow the 
> instructions here to cross-compile a sample D program, though 
> you'll need to add the -mtriple=armv7-none-linux-android flag 
> when calling ldc:
>
> https://wiki.dlang.org/Build_D_for_Android#Build_a_command-line_executable

It's not so simple to build the samples, I've updated that page 
with all that was needed, including using ldc with -Xcc.

> I'd like to make it easier to cross-compile for supported 
> platforms, by being able to pass a target OS and architecture 
> alone to this build tool and CMake, which would call some 
> default C and linker flags in the official CMake config.  You 
> will still be able to override the defaults with custom 
> C/linker flags.  I'll submit a PR for Android/ARM, hopefully 
> we'll have more defaults for Linux/PPC, Linux/armhf, and other 
> more well-tested platforms.  I hope you're not waiting on that 
> PR to push the ldc 1.4 beta out, kinke.

Let's get cross-compilation presets for other supported platforms 
in before the 1.5 release:

https://github.com/ldc-developers/ldc/blob/master/runtime/PresetRuntimeConfiguration.cmake

For example, it'd be good to get kinke's linux->Windows 
cross-compilation config in there, and instructions on setting it 
up and using it into the ldc-build-runtime wiki page.  The same 
for Linux/armhf, which I think some have been cross-compiling.


More information about the digitalmars-d-ldc mailing list