Programming in D tutorial on Embedded Linux ARM devices
Contents
Programming in D tutorial on Embedded Linux ARM devices
1. Introduction
D is a great systems-programming language with clean syntax and great modelling power. Traditionally, Linux based embedded devices are programmed using C or C++. Python and Java are more populator today, but fail due to large runtime size and resource requirements. Programming in D will be comfortable, since it is like a dynamic language, but has native code performance, and has full ABI compatibility with C, making it very suitable as a “Linux systems-programming language”.
This simple tutorial will introduce to programming in D on Embedded ARM Linux step by step.
2. Preparing your ARM GCC toolchain
The very first thing is to make your toolchain prepared. Although clang has supported ARM for a very long time, GCC is still the premier choice for compiling to ARM Linux systems.
If you are a ARM Linux application programmer, you probably already have ARM GCC installed, and tested some hello world prgrams on your ARM board.
However, if you haven't installed ARM GCC, it is quite easy.
Arch Linux:
$ pacman -S arm-linux-gnueabihf-gcc
Debian:
$ apt install gcc-arm-linux-gnueabihf
After the toolchain installed, you need to make a simple test make sure that ARM GCC is generating arm executable properly. Create a file, test.c, that contains the following, using your favourite text editor.
#include <stdio.h>
int main()
{
float a = 3.14;
int b = 2;
printf("hello world! 3.14 * 2 = %f\n";, a*b);
return 0;
}
Then compile using:
$ arm-linux-gnueabihf-gcc test.c -o test
Copy the compiled program to your ARM target board:
$ scp test user@armboard:/home/user
On the ARM target run the test executable.
ARM $ ./test hello world! 3.14*2 = 6.280000
⚠If your ARM GCC toolchain was compiled by yocto, you may need to call GCC using $CC after you sourced the environment setup script.
3. Installing LDC2
D has 3 compiler implementations. Currently, only GDC and LDC support ARM and CPUs other than x86.
GDC support for arm can be enabled if you are compiling your toolchain by yourself, by passing –enable-languages=c,d,cpp
when configuring (i.e.,./configure -–enable-languages=c,d,cpp
. Or, you can download an ARM enabled GDC build from the [GDC project home page].
As of the writing of this tutorial, the latest GDC release (6.3.0) is not as good as LDC2, for ARM targets.
LDC has better support for ARM targets, and is more actively developed that GDC. And, as it is based on LLVM, LDC does not need a cross-compiling toolchain prepared for an individual target.
We recommend downloading the latest LDC2 release from the [GitHub releases page]
For ArchLinux or Gentoo Linux users, you can install the latest LDC compiler just using pacman
or emerge
If you downloaded an LDC2 stand-alone release from the GitHub page, say: [ldc2-1.8.0-linux-x86_64.tar.xz] you need to do the following:
mkdir your-devel-toolchain-dir tar xf ldc2-x.y.z-linux-x86_64.tar.xz -C your-devel-toolchain-dir
The compiler will be installed to a dir named ldc2-1.8.0-linux-x86_64
) which has it’s own bin
, etc
, lib
sub directories. As it’s a stand-alone distribution, you need to add ldc2-1.8.0-linux-x86_64/bin
to your PATH env.
export PATH=${your-devel-toolchain-dir}/ldc2-1.8.0-linux-x86_64/bin:${PATH}
Now, you could invoke ldc2 directly:
$ ldc2 --version LDC - the LLVM D compiler (1.8.0): based on DMD v2.078.3 and LLVM 5.0.1 built with LDC - the LLVM D compiler (1.8.0) Default target: x86_64-unknown-linux-gnu Host CPU: skylake http://dlang.org - http://wiki.dlang.org/LDC Registered Targets: aarch64 - AArch64 (little endian) aarch64_be - AArch64 (big endian) arm - ARM arm64 - ARM64 (little endian) ......
As you can see from the ldc2 version output, arm64 is a registered target, but it’s not completely supported right now. It’s a little bit disappointing, but don’t worry, we have ARM well supported when the target is gcc+glibc+linux. And ARM and ARMhf executable can directly run on an arm64 target when arm64 has multilib support.
4. compiling the D runtime for ARM target.
before doing this, you need to make sure cmake is installed on your host.
sudo apt install cmake
Then, just type the commands bellow to make your D arm runtime:
$ CC=arm-linux-gnueabihf-gcc ldc-build-runtime --dFlags="-w;-mtriple=arm-linux-gnueabihf" --targetSystem="Linux;UNIX" ------------------------------------------------------------------- Creating build directory: ldc-build-runtime.tmp Downloading LDC source archive: https://github.com/ldc-developers/ldc/releases/download/v1.8.0/ldc-1.8.0-src.zip Invoking: ["cmake", "-DLDC_EXE_FULL=/media/Devel/Changhong/IOT/imx6u-devel/toolchain/ldc2-1.8.0-linux-x86_64/bin/ldc2", "-DD_VERSION=2", "-DDMDFE_MINOR_VERSION=0", "-DDMDFE_PATCH_VERSION=78", "-DLDC_TARGET_PRESET=", "-DTARGET_SYSTEM=Linux;UNIX", "-DD_FLAGS=-w;-mtriple=arm-linux-gnueabihf", "-DRT_CFLAGS=", "-DLD_FLAGS=", "/media/Devel/Changhong/IOT/imx6u-devel/toolchain/ldc2Armhf_runtime/ldc-build-runtime.tmp/ldc-src/runtime"] ......
The ldc-build-runtime tool will download ldc compiler source and making the build.
NOTE: yocto built toolchain need to use a different manner:
CC=arm-poky-linux-gnueabi-gcc ldc-build-runtime --dFlags="-w;-mtriple=arm-poky-linux-gnueabi;-float-abi=hard;-mcpu=cortex-a7" --targetSystem="Linux;UNIX"
for yocto toolchain the above arm-poky-linux-gnueabi-gcc is just a shell script adapter.
export SDKTARGETSYSROOT=your-target-root-fs-dir ${POKYGCCBINPATH}/arm-poky-linux-gnueabi-gcc -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=$SDKTARGETSYSROOT $@
After build, you will get result like bellow:
$ ls ldc-build-runtime.tmp $ ls ldc-build-runtime.tmp/ CMakeCache.txt cmake_install.cmake ldc-src lib objects objects-debug-shared CMakeFiles dummy.c ldc-src.zip Makefile objects-debug objects-shared
The runtime lib is right sit in ldc-build-runtime.tmp/lib
, the dir name ldc-build-runtime.tmp
is temp, you can rename it to a ldc_arm_runtime
or something you like.
Then, we will make a simple custom command using alias for future comfortable.
alias ldcarm='ldc2 -mtriple=arm-linux-gnueabihf -gcc=arm-linux-gnueabihf-gcc -L=-L${LDC2ARMRUNTIME}/lib'
${LDC2ARMRUNTIME} is the runtime dir you just prepared before.
5. the D hello world on ARM !
$ vim test.d import std.stdio; void main() { writeln("hello world from D!"); writeln("3.14 * 2 = ", 3.14*2); }
Then compile the test program like this:
ldcarm test.d -of testd
-of
tells ldc to output the compiled executable using testd
filename rother than default test
.
Now, copy the testd to your arm target, type ./testd
and make a breath.
$ ./testd hello world from D! 3.14 * 2 = 6.28
Wow! we finally reach the point! Congratulations!
6. the dub managed project
For complex testing, we will use dub to init a vibe.d hello world project.
$ dub init vibeex --type=vibe.d Package recipe format (sdl/json) [json]: Name [vibeex]: Description [A simple vibe.d server application.]: Author name [dbh]: License [proprietary]: Copyright string [Copyright © 2018, dbh]: Add dependency (leave empty to skip) []: Successfully created an empty project in '/media/Devel/Changhong/IOT/imx6u-devel/project/vibeex'. Package successfully created in vibeex
The dub tool initialized a vibe.d example project, but the vibe.d dependency version is outdated. we need to tweak it.
$ vim vibeex/dub.json { "name": "vibeex", "authors": [ "dbh" ], "dependencies": { "vibe-d": "~>0.7.30" }, "description": "A simple vibe.d server application.", "copyright": "Copyright © 2018, dbh", "license": "proprietary" }
update 0.7.30 to 0.8.3.
when building using dub, the alias method setup ldc-arm will not work. we need to turn this into a shell script adapter:
#!/bin/sh LDC2ARMRUNTIME=/media/Devel/Changhong/IOT/imx6u-devel/toolchain/ldc2Armhf_runtime ldc2 -mtriple=arm-linux-gnueabihf -gcc=arm-linux-gnueabihf-gcc -L=-L${LDC2ARMRUNTIME}/lib $@
for yocto toolchain, you will have an adapter like bellow:
$ cat ldc-yocto-arm #!/bin/sh LDC2ARMRUNTIME=/media/Devel/Changhong/IOT/imx6u-devel/toolchain/ldc2Armhf_runtime ARMTARGETROOT=/media/Devel/Changhong/IOT/imx6u-devel/toolchain/yocto/targetroot ldc2 -mtriple=arm-poky-linux-gnueabi -float-abi=hard -mcpu=cortex-a7 -gcc=arm-poky-linux-gnueabi-gcc -L=-L${LDC2ARMRUNTIME}/lib -Xcc=--sysroot=$ARMTARGETROOT $@
NOTE: if your toolchain does not have some library for the arm target present, dub will fail like this:
Then compile the project using commands below:
$ dub build --compiler=ldc-arm Performing "debug" build using ldc-arm for arm, arm_hardfloat. taggedalgebraic 0.10.9: building configuration "library"... eventcore 0.8.30: building configuration "epoll"... stdx-allocator 2.77.0: building configuration "library"... vibe-core 1.4.0: building configuration "epoll"... vibe-d:utils 0.8.3: building configuration "library"... vibe-d:data 0.8.3: building configuration "library"... vibe-d:crypto 0.8.3: building configuration "library"... diet-ng 1.4.5: building configuration "library"... vibe-d:stream 0.8.3: building configuration "library"... vibe-d:textfilter 0.8.3: building configuration "library"... vibe-d:inet 0.8.3: building configuration "library"... vibe-d:tls 0.8.3: building configuration "openssl"... vibe-d:http 0.8.3: building configuration "library"... vibe-d:mail 0.8.3: building configuration "library"... vibe-d:mongodb 0.8.3: building configuration "library"... vibe-d:redis 0.8.3: building configuration "library"... vibe-d:web 0.8.3: building configuration "library"... vibe-d 0.8.3: building configuration "vibe-core"... vibeex ~master: building configuration "application"...
however, sometime we would have no luck, and got something like this:
$ dub build --compiler=ldc-arm Performing "debug" build using ldc-arm for arm, arm_hardfloat. taggedalgebraic 0.10.9: building configuration "library"... eventcore 0.8.30: building configuration "epoll"... stdx-allocator 2.77.0: building configuration "library"... vibe-core 1.4.0: building configuration "epoll"... vibe-d:utils 0.8.3: building configuration "library"... vibe-d:data 0.8.3: building configuration "library"... vibe-d:crypto 0.8.3: building configuration "library"... diet-ng 1.4.5: building configuration "library"... vibe-d:stream 0.8.3: building configuration "library"... vibe-d:textfilter 0.8.3: building configuration "library"... vibe-d:inet 0.8.3: building configuration "library"... vibe-d:tls 0.8.3: building configuration "openssl"... vibe-d:http 0.8.3: building configuration "library"... vibe-d:mail 0.8.3: building configuration "library"... vibe-d:mongodb 0.8.3: building configuration "library"... vibe-d:redis 0.8.3: building configuration "library"... vibe-d:web 0.8.3: building configuration "library"... vibe-d 0.8.3: building configuration "vibe-core"... hellovibe ~master: building configuration "application"... /usr/lib/gcc-cross/arm-linux-gnueabihf/6/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lssl /usr/lib/gcc-cross/arm-linux-gnueabihf/6/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lcrypto /usr/lib/gcc-cross/arm-linux-gnueabihf/6/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lz collect2: error: ld returned 1 exit status Error: /usr/bin/arm-linux-gnueabihf-gcc failed with status: 1 ldc-arm failed with exit code 1.
This means that all library path does not includes libssl, libcrypto, libz. You need to find or build a toolchain with extra lib built-in, or, just cross-compiling the desired c library and install to your toolchain sysroot path.
Let’s run the vibe.d example on arm target:
root@armhost:~# ls -lh total 25M -rwxr-xr-x 1 root root 24M Mar 12 05:53 vibeex root@armhost:~# ./vibeex [main(----) INF] Listening for requests on http://[::1]:8080/ [main(----) INF] Listening for requests on http://127.0.0.1:8080/ [main(----) INF] Please open http://127.0.0.1:8080/ in your browser. Vibe was run as root, and no user/group has been specified for privilege lowering. Running with full permissions.
Yeah! we finally did that!