Embedded Rust with Yocto. It’s simple!
The overall process of building a Rust application for an embedded device is very straightforward and appealing. All you need to do is create a .cargo/config.toml file with a build target declaration and a simple setup. It might look like this:
[target.armv5te-unknown-linux-musleabi]
linker = "rust-lld"Then you can build your project using the following command:
cargo build --target=armv5te-unknown-linux-musleabi --releaseAnd that’s it! You now have a static binary ready to be deployed to your embedded device. There are no dependencies or complex cross-compiler setups.
Integrating this process into the Yocto build system is another matter entirely.
Using a meta-rust layer is challenging. For now, let’s leave it as it is. Hopefully, there is an alternative called meta-rust-bin. This contains precompiled versions of Rust and Cargo. By the way, why do we need to compile every tool used by Yocto from sources every time we start a new project?
Never mind. I will explain how to use meta-rust-bin using an example build from my other project, framebuffer-animations.
First, we need to set up the meta-rust-bin layer in the Yocto project. Clone the layer repository:
git clone https://github.com/rust-embedded/meta-rust-bin.gitIn poky/build/conf/bblayers.conf, add a layer declaration:
BBLAYERS ?= " \
...
${BSPDIR}/meta-rust-bin \No other setup is needed.
Get Paweł Knioła’s stories in your inbox
Join Medium for free to get updates from this writer.
The BitBake recipe file for the project, framebuffer-animations_git.bb, is very simple and consists of well-known elements:
SUMMARY = "Plays GIF animations on a raw framebuffer device"
HOMEPAGE = "https://github.com/pawelkn/framebuffer-animations"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=ecaa322643ab5bcc0a9a3b7d6bebd18d"
SRC_URI = "git://github.com/pawelkn/framebuffer-animations.git;protocol=https;branch=master"
SRCREV="e08094673b71709a0c87f869df1995936f8d0732"
S = "${WORKDIR}/git"
inherit cargo_bin
# Skip already-stripped QA check for Rust binary
INSANE_SKIP:${PN} += "already-stripped"
# Enable network for the compile task allowing cargo to download dependencies
do_compile[network] = "1"To distinguish between meta-rust and meta-rust-bin, a cargo_bin inheritance is used.
The INSANE_SKIP variable is project-specific. The stripping step is declared in a Cargo.toml file and carried out in advance during the Cargo build. To avoid using INSANE_SKIP, a patch for Cargo.toml would be needed.
The do_compile[network] = “1” is required because Cargo needs internet access during compilation to download dependencies, which is normally not allowed.
After a straightforward build:
bitbake framebuffer-animationsThe binary is generated and ready to use. To test it without building the whole Yocto image navigate to the build directory and copy the output binary directly to the target.
cd poky/build/tmp/work/aarch64-poky-linux/framebuffer-animations/git-r0/
scp package/usr/bin/fba root@192.168.10.100:~/It should work right out of the box:
./fba --device /dev/fb0 --center /root/images/spaceship-640x480.gifThank you for reading! If you have any questions or need further assistance, feel free to ask.






