📜 ⬆️ ⬇️

We write on D for Raspberry Pi


Dlang, or simply D is a young programming language with a long history. Despite the fact that a language with such a name appeared a long time ago, what is now called D2 or just D, appeared recently and only slightly resembles its predecessor. Writing to D is very convenient, and performance is not inferior to C ++, so it is not surprising that he got to ARM and its mobile representatives from Android and iOS. In addition, there is a growing interest in the Internet of things and simply portable devices.
The article describes the problem of cross-compiling dlang code for Raspberry Pi. There is nothing difficult in this, and there are no pitfalls. This publication is a simple manual to start using D on different devices in general and Raspberry Pi in particular.

Hello, World


We will need:

Despite the fact that you can collect the source directly on the Raspberry Pi, it is better to do it on a separate, more powerful computer. On the raspberry itself, there is too little memory, and crosspiling under the Raspberry Pi is a simple matter. First we need a dlang compiler for the Raspberry Pi. There are two options: LDC (LLVM based D Compiler) and GDC (GNU D Compiler). It turned out to be easier to find a suitable version of GDC. There is an LDC under ARM and it can also be used, it just so happens that I use GDC.
Go to the GDC website in the downloads section.
Download the x86_64 build for the target platform arm-linux-gnueabihf (here, carefully, the GDC itself will run on x86, and the target armhf; not to be confused with the GDC for arm, which runs on the device).
Download, unpack from the console.
wget http://gdcproject.org/downloads/binaries/5.2.0/x86_64-linux-gnu/gdc-5.2.0-arm-linux-gnueabihf+2.066.1.tar.xz 

Unpacking
 tar -xf gdc-5.2.0-arm-linux-gnueabihf+2.066.1.tar.xz 

for convenience, I have renamed the folder
 mv arm-unknown-linux-gnueabihf gdc-arm 


For permanent use, I copied the folder with gdc to / usr / bin and made the alias gdc-arm, but for the example of the article we will do everything locally in the folder. And so create a shortcut link:
 ln -s gdc-arm/bin/arm-linux-gnueabihf-gdc gdc 

Generally speaking, this is enough to collect D code under the Raspberry PI. Let's check on the example of Hello, world. So we write the source:
 import std.stdio; void main() { writeln("Hello, World!"); } 

And save to hello.d. Compile:
 ./gdc hello.d -o hello 

Now we copy on a device we go on it and we check. I looked like this:
 scp hello pi@192.168.1.85/~/dlang/hello 

And on the device:
 pi@raspberrypi:~/dlang $ ./hello Hello, World! 

Complicating the task of linking libraries


Everything described above is trivial and works out of the box. Now we collect something more difficult. For example, “Hello, vibe.d”, that is, a primitive application on a web framework, vibe.d. There is one difference from the usual HelloWorld - you need to link the libraries. Cross compilation dlang in this issue is no different from C, C ++ and other compiled languages. Therefore, you can use any convenient approach to cross-combining on the Raspberry Pi.
Libraries are needed for a specific architecture and their best source is the repository installed on the Raspberry distribution. This is usually https://www.raspbian.org/RaspbianRepository . The easiest way to use the library right from the device itself. Just do not collect on it (very bad idea, very slowly and forever in a swap), but just use the files. A good idea would be to use sshfs (peeped here: wiki.dlang.org/GDC/Cross_Compiler/Existing_Sysroot ). The main advantage of this approach is absolute universality and stability. No matter what distribution kit is installed, ideally suitable libraries will be taken. There are no conflicts and inconsistencies between versions of the distribution kit or libraries.
Why not pick up the Raspbian as the source of the packages on the build machine itself
Unfortunately, all attempts to connect this source and use the libraries did not succeed. After much torment, very similar to these: answers.launchpad.net/ubuntu/+source/build-essential/+question/250970, the idea was abandoned.

Why not get the ubuntu repository for arm
There are some connection problems. Although it was still possible to connect and even assemble later, I received Illegal Instruction at launch. The armhf architecture also has a bunch of varieties and flags, and we still need to figure out and find those repositories that fit Raspberry Pi.

Create a folder into which we mount the entire Raspberry Pi file system and write it into the environment variable:
 mkdir rpi echo $RPIROOT export RPIROOT=~/test_pi/rpi/ echo $RPIROOT /home/user/test_pi/rpi/ 

Mount via sshfs:
 sshfs -o idmap=user,follow_symlinks pi@192.168.1.85:/ $RPIROOT 

Since we need to specify the architecture and search paths of the lib, to shorten the record we will create another shortcut. Create a gdc-rpi file and write the script there to launch GDC with the necessary flags.
 #!/bin/bash ~/test_pi/gdc -march=armv6j -mfpu=vfp -mfloat-abi=hard --sysroot=$RPIROOT -B$RPIROOT/usr/lib/arm-linux-gnueabihf "$@" 

Here is a little more detail: -march = armv6j -mfpu = vfp -mfloat-abi = hard - these are the flags of the architecture of the Raspberry Pi processor. sysroot is a root device, -B is a place to search for lib, we have it on the actual device in usr / lib
Add launching rights and check:
 user@ubuntu:~/test_pi$ chmod 777 gdc-rpi user@ubuntu:~/test_pi$ ./gdc-rpi gdc: fatal error: no input files compilation terminated. 

Everything works, now we need a DUB on the build machine (this is a build system and for one dependency manager). We download and put it in any convenient way described here .
Create a simple vibe.d project:
 dub init -tvibe.d test_vibe_pi 

DUB will create a folder with a minimum project inside. Now we collect:
 cd test_vibe_pi/ dub build --compiler=../gdc-rpi 

At the exit, we have a test_vibe_pi file. If there is no something like libcurl, then go to the device and apt-get we put everything you need. I already had everything after past experiments.
Copy it to the device and check:
 cp test_vibe_pi $RPIROOT/home/pi/dlang 

On the Raspberry Pi:
 pi@raspberrypi:~/dlang $ ./test_vibe_pi Listening for requests on http://[::1]:8080/ Listening for requests on http://127.0.0.1:8080/ Please open http://127.0.0.1:8080/ in your browser. 

Everything, the minimum saytik, giving “Hello, World!” To any request for 127.0.0.1 : 8080 /, is ready.

For those who want to collect their projects directly on the device


Collecting code on such a weak device is a bad idea, but sometimes it is more convenient to roll out a project on a device in the form of sources. GDC for Raspberry is ready, you can go to the ARM section gdcproject.org/downloads and download the armhf version. For the assembly, you will most likely need DUB and you will have to build it, because the ready-made binary from the site does not run on Raspberry Pi B +.
We download the source from https://code.dlang.org/download (you can from github, that's how you want it). We unpack, we put in the folder with a convenient naming
 user@ubuntu:~/test_pi$ wget https://github.com/rejectedsoftware/dub/archive/v0.9.24.tar.gz user@ubuntu:~/test_pi$ tar -xf v0.9.24.tar.gz user@ubuntu:~/test_pi$ mv dub-0.9.24/ dub user@ubuntu:~/test_pi$ cd dub 

For building under Linux using GDC there is a separate script: build-gdc.sh. He expects that gdc is in the system, or the environment variable GDC is set. Use the variable. Just specify the path to our shortcut script and run:
 GDC=../gdc-rpi ./build-gdc.sh Generating version file... ./build-gdc.sh: 15: ./build-gdc.sh: git: not found Running ../gdc-rpi... DUB has been built as bin/dub. You may want to run sudo ln -s /home/user/test_pi/dub/bin/dub /usr/local/bin now. 

If this output is received and no linking errors, then now there is a working DUB for the Raspberry Pi. We copy on a device and we check.
 user@ubuntu:~/test_pi/dub$ cd .. user@ubuntu:~/test_pi$ cp dub/bin/dub rpi/home/pi/dlang/dub-test/dub 

On the device:
 pi@raspberrypi:~/dlang/dub-test $ ./dub Neither a package description file, nor source/app.d was found in /home/pi/dlang/dub-test Please run DUB from the root directory of an existing package, or run "dub init --help" to get information on creating a new package. Failed to find a package named ''. 

DUB started and rightly noticed the absence of the project in the folder. We really do not have a project, and the main goal has been achieved - DUB works on Raspberry Pi.

That's all, we can collect any projects on D under Rapberry Pi. For example, you can run the server for a smart home. There is support for MQTT in the form of a plugin for vibe.d, as well as the ability to use any existing C library.

')

Source: https://habr.com/ru/post/278775/


All Articles