Do you have any rust application and want to cross-compile it for some other hardware using Yocto? If yes, you are at the right place. In this article, we will tell you how to make a yocto recipe for rust applications easily using cargo bitbake.
Writing yocto recipes manually for rust software is a hectic task. You might stick yourself in a limbo of recursive dependencies versions required by the applications. But the good news is that rust provides an amazing utility known as cargo-bitbake that can save us from this headache and provides us with an amazing yocto recipe in few simple steps.
In this tutorial, we will tell you how to generate a recipe for your software using cargo-bitbake. So, let’s get started!
Step 1: Install Pre-Requisites:
First of all, open the terminal and install the following dependencies in your system.
sudo apt install build-essential pkg-config libssl-dev
Step 2: Install Rust:
Install rust in your system using the following commands
sudo apt update && sudo apt upgrade
curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env
rustc --version
sudo apt update && sudo apt upgrade
For detailed information about the rust installation, click here!
Step 3: Install cargo-bitbake utility:
cargo-bitbake utility is used to generate a yocto recipe for rust applications. You can install this utility in your system using the below command
sudo apt install build-essential pkg-config libssl-dev
cargo install cargo-bitbake
Step 4: Generate a Yocto recipe using Cargo-bitbake:
Now that all the installations are done let’s come to main course. For this tutorial, lets clone any random rust project from github. For example, we cloned a random cgmath rust library from the github as shown below
git clone https://github.com/rustgd/cgmath.git
cd cgmath/
ls
You can see in the screenshot that we cloned the software and moved to the root directory of the project. The structure of almost all the rust projects are same where Cargo.toml is always in root directory of project and source files in src folders.
Now while standing in the root folder of rust project, run the following command
cargo bitbake
After running the cargo bitbake command, you can see that it has generated the yocto recipe with the project name cgmath_0.18.0.bb.
Lets see what is inside the yocto recipe. You can see in the screenshot below that cargo bitbake have made a robust recipe for us incorporating all the dependencies required by the software. It has also included the SRC_URI if your rust project is hosted on git. Amazing isnt it
Step 5: Use The Recipe in Yocto:
Copy the recipe and place it at suitable location in your embedded linux based yocto project. After that, run the following to bake the recipe for your target system
bitbake cgmath
Hurray! You are done. Thats the easy peasy procedure to make a yocto recipe for Rust Applications using Cargo-Bitbake. Hope you liked the tutorial. If you still have any query, feel free to ask in comments section and we will get back to you!