Since Rust 1.6 was released,
libcore is now stable, and
nostd is now a stable feature. This means we can now build Rust libraries for our embedded firmware using the official stable version of the compiler!
There are a few caveats. We still will need a nightly to build
libcore and other components in our cross-compiling environment, but once we have that all setup we can do the bulk of our development in Rust libraries using the stable version.
The basic process is as follows:
1. Install multirust.
Install multirust by following the instructions here. Multirust makes it much easier to fetch, install and switch between, particular versions of Rust.
More detailed information is on the github readme here.
2. Install latest stable version of Rust.
multirust default stable
3. Install a nightly version of Rust.
You’ll want the nightly that corresponds with stable version that you have. You can find the associated dates on the rust release notes page. So for Rust 1.8 we’ll need the 2016-04-14 nightly.
multirust override nightly-2016-04-14
Having a version of the nightly that matches our stable will let us build core libraries with the nightly that can be used by stable.
4. Clone the Rust repo.
Check out the commit hash that matches your nightly version of Rust. The commit for mine is
2b6020723, so we can do this:
git clone firstname.lastname@example.org:rust-lang/rust.git cd rust git checkout 2b6020723115e77ebe94f228c0c9b977b9199c6e cd ..
5. Use the nightly build to build libcore for your target platform.
You will need a target specification for the target triple you want to build for and put it in the root of your project. I’m using this one, for an ARM Cortex-M4.
Then to build
libcore, you can do this:
mkdir libcore-thumbv7em rustc -C opt-level=2 -Z no-landing-pads --target thumbv7em-none-eabi -g rust/src/libcore/lib.rs --out-dir libcore-thumbv7em
6. Build binaries linking against your new libcore.
Switch back to stable Rust, and build your libraries.
multirust override stable rustc --crate-type lib -C opt-level=2 -Z no-landing-pads --target thumbv7em-none-eabi -g --emit obj -L libcore-thumbv7em -o my_rust_file.o my_rust_file.rs
And there you have it! This will give you an object file that you can link into your final binary.
Some more general documentation on cross compiling with rust can be found here