Building Rust Applications for ARM Devices

Compiling for different target architectures, operating systems, and binary file formats is often an enormous pain in the neck depending on which language and tools you’re using. In the past, I’ve had to set up multiple virtual machines all running concurrently just so I could build the same application for multiple operating systems.

Cargo is an incredibly powerful tool and it comes equipped with the ability to compile for different targets. The rustup command lets you add and remove targets and list all of the available targets. Cross-compilation in Rust is usually quite simple.

Before you can compile for a target other than the default for your operating system, you’ll need to install the native compiler for that environment. There’s a GitHub repository that’s kept up to date with instructions on how to configure your workstation—Linux, Windows, or macOS—for cross-compilation.[33]

The first thing you’ll need to do after ensuring your workstation has the native compilation tool chain is to add the appropriate target via rustup. The target for Raspberry Pi 2+ devices is armv7-unknown-linux-gnueabihf. The Raspberry Pi 1 is an ARM v6 device, while all newer ones are ARM v7. The second element in the target triple is the vendor (unknown in our case since it doesn’t matter). The third is the operating system and the fourth is the ABI (Application Binary Interface). Add the ARM v7 target with the following command:

 $ ​​rustup​​ ​​target​​ ​​add​​ ​​armv7-unknown-linux-gnueabihf
 info: downloading component 'rust-std' for 'armv7-unknown-linux-gnueabihf'
  50.5 MiB / 50.5 MiB (100 %) 6.5 MiB/s ETA: 0 s
 info: installing component 'rust-std' for 'armv7-unknown-linux-gnueabihf'

Next, configure Cargo for cross-compilation using the instructions on the GitHub repository. For Ubuntu Linux, it will look like this:

 $ ​​mkdir​​ ​​-p​​ ​​~/.cargo
 $ ​​cat​​ ​​>>~/.cargo/config​​ ​​<<EOF
 >​​ ​​[target.armv7-unknown-linux-gnueabihf]
 >​​ ​​linker​​ ​​=​​ ​​"arm-linux-gnueabihf-gcc"
 >​​ ​​EOF

Next, create a new Rust binary application using the same commands you’ve been using throughout the book. To compile this application so it will run on a Raspberry Pi, use the following command (which looks very similar to how we specify the WebAssembly target for wasm compilation):

 $ ​​cargo​​ ​​build​​ ​​--target​​ ​​armv7-unknown-linux-gnueabihf
 Compiling crosscompiledemo v0.1.0
  (file:///home/kevin/Code/Rust/wasmbook/khrust/Book/code/crosscompiledemo)
 Finished dev [unoptimized + debuginfo] target(s) in 0.58s

Finally, to verify that this new binary is actually an ARM v7 Linux binary and not a binary for the development workstation that ran the build, you can execute the following command:

 $ ​​file​​ ​​target/armv7-unknown-linux-gnueabihf/debug/crosscompiledemo
 target/armv7-unknown-linux-gnueabihf/debug/crosscompiledemo:
 ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV),
 dynamically linked, interpreter /lib/ld-linux-armhf.so.3,
 for GNU/Linux 3.2.0, BuildID[sha1]=fb1690370f7516f436c440d5083447d8fe06077a,
 with debug_info, not stripped

If you’ve got a Raspberry Pi (2 or newer) handy, you can take the single crosscompiledemo binary and scp that to the device and execute it. You should see the standard “Hello, World” text. Now that your workstation is set up to do cross-compilation for ARM v7, it’s time to build the indicator module host.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.212