Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Compiling D for ARM/Linux Raspberry Pi


Raspberry Pi is a small, low-cost computer that uses an ARM processor and the Linux operating system.

Getting ready

If you don't own a Raspberry Pi, you can emulate the hardware with QEMU. The process is described at http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/.

How to do it…

To compile D for an ARM/Linux Raspberry Pi, we need to execute the following steps:

  1. Download the GNU D compiler (GDC) cross-compiler from http://gdcproject.org/downloads/. The header gives the platform you are on. The table lists the targets. The target for Raspberry Pi is arm-linux-gnueabi.

  2. Compile the program. The command-line arguments of GDC that are based on the gcc compiler differ significantly from dmd in the following manner:

    • The output file will always be called a.out unless you specify a new name with the –o option.

    • You must always include the .d file extension when passing source files to the compiler.

    • Many of D's additional compile options need to be passed with –f and may have different names. For example, -fno-bounds-check works in the same way as –boundscheck=off on dmd. Refer to the GDC documentation for more info, or download the gdmd helper script that converts the flag syntax for you.

  3. Copy the executable to your Raspberry Pi.

  4. Run the program.

    Tip

    You can also download a native compiler for the Pi which runs directly on it and produces programs for it. The bottom of the GDC download page has a native compiler for ARM.

The code is as follows:

import std.stdio;
void main() { writeln("Hello, ARM!"); }
gdc hello.d –ohello
./gdc

The output is as follows:

Hello, ARM!

How it works…

GDC combines the D language portions of dmd (the dmd frontend) with the code generation portions of gcc (the gcc backend) to create a portable compiler with mature code generation. With GDC, D code can be generated for every target gcc supports.

Code generation is only half the story to using D. Using it on a new platform also requires that the runtime library be ported. The GDC team successfully ported the runtime to Linux on ARM with all tests passing in 2013 and 2014, enabling the full language and standard library on that platform. Most D code and libraries will now work on ARM/Linux targets such as Raspberry Pi.

There's more…

Android and iOS systems often have ARM processors too, but they do not run a standard Linux operating system. In the case of Android, it also does not have linker support for thread-local variables which D will have to emulate. Work is in progress towards porting the library to these operating systems and adding support to the language to enable direct access to Objective-C APIs to facilitate better integration on Apple systems.

See also

  • http://wiki.dlang.org/LDC is the LDC compiler, a D compiler based on the LLVM compiler backend. It also has growing support for ARM targets.