Programming in D tutorial on Embedded Linux ARM devices

From D Wiki
Revision as of 11:51, 16 March 2018 by Johann Lermer (talk | contribs) (fixed typo (populator -> popular))
Jump to: navigation, search

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 popular 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.

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.

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.

The recommended way to install LDC is by using the install script (works on POSIX and POSIX like systems):

curl https://dlang.org/install.sh | bash -s ldc

You can also search your operating system's packages for the LDC package.

Verify that LDC was installed correctly:

$ 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.

Compiling the D runtime for the ARM target

Before doing this, you need to make sure ,cmake is installed on your host. If not, please install it. Then, just type the commands bellow to compile your D 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 the build, you will get something 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 sitting in ldc-build-runtime.tmp/lib , the directory named ldc-build-runtime.tmp is temporary, you can rename it to a ldc_arm_runtime or something you like.

Then, we will make a simple alias for ease of use.

alias ldcarm='ldc2 -mtriple=arm-linux-gnueabihf -gcc=arm-linux-gnueabihf-gcc -L=-L${LDC2ARMRUNTIME}/lib'

where ${LDC2ARMRUNTIME} is the runtime directory you prepared earlier.

The D hello world on ARM

With your favourite text editor, just like before with c, make a file called test.d with your favourite text editor. It should contain the following code:

import std.stdio;
void main()
{
    float a = 3.14;
    int b = 2;
    writeln("hello world from D!";);
    writeln("3.14 * 2 = ", a * b);
}

Then compile the test program like this:

ldcarm test.d -of testd

-of tells ldc to output the compiled executable with the file name testd instead of the default, which would derive the name from the input file.

Now, copy the testd file you just created to your arm target, and take a deep breath before executing it.

$ ./testd
hello world from D!
3.14 * 2 = 6.28

Wow! We finally reach the point! Congratulations!

Letting Dub manage the project

For 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. Edit the file vibeex/dub.json with your favourite text editor, changing the dependency version from 0.7.30 to 0.8.3. The file should look something like this:

{
    "name": "vibeex",
    "authors": [
        "dbh"
    ],
    "dependencies": {
        "vibe-d": "~>0.8.30"
    },
    "description": "A simple vibe.d server application.",
    "copyright": "Copyright © 2018, dbh",
    "license": "proprietary"
}

when building using dub, the alias we created (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, with errors 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 just get unlucky, and end up with 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 the library path does not include libssl, libcrypto and libz. You need to find or build a toolchain with the extra libraries built-in, or, just cross-compile the desired C library and install it to your toolchain's 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 it!