Skip to content

Instantly share code, notes, and snippets.

@danielbank
Last active September 11, 2019 23:57
Show Gist options
  • Select an option

  • Save danielbank/2d3effb6c9d9873a1e6e5458fc322e33 to your computer and use it in GitHub Desktop.

Select an option

Save danielbank/2d3effb6c9d9873a1e6e5458fc322e33 to your computer and use it in GitHub Desktop.
Cross Compiling Rust to Your Pi

Cross Compiling Rust to Your Pi

Why Cross-Compile?

  • It's faster to build your code on your laptop than the Pi

  • It's not always possible to build on a Pi:

# On the Pi Zero
Compiling tract-core v0.3.4-pre (/home/pi/workspace/tract/core)
memory allocation of 37748736 bytes failed
error: Could not compile `tract-core`.

Setup

  • Install Docker

  • Add Rust targets

rustup target add armv7-unknown-linux-gnueabihf # Pi
rustup target add arm-unknown-linux-gnueabihf # Pi 0
rustup target list
  • Clone cross and build it
git clone git@github.com:rust-embedded/cross.git
cd cross
cargo build --release
# You may get some warning about `$HOME`, just disregard that.
cd target/release

Cross Compilation

  • Docker has to be running or you will get errors!

  • Go to the code directory you want to build

cd ~/workspace/tract/examples
  • Build the code with cross
/Users/DanielBank/workspace/cross/target/release/cross build --release --target=armv7-unknown-linux-gnueabihf --manifest-path=examples/tensorflow-mobilenet-v2/Cargo.toml

Errata

Broken Commands

# The following don't work:

# Well, this worked the first time I built my code but it gave me a cryptic error on subsequent attempts
/Users/DanielBank/workspace/cross/target/release/cross build --release --target=armv7-unknown-linux-gnueabihf

# This just builds the code for your laptop's architecture  because you didn't specify a target
/Users/DanielBank/workspace/cross/target/release/cross build --release
  • More about that cryptic error
error: failed to load source for a dependency on \`tract-core\`

Jacob Rosenthal  [28 days ago]
I think Ive seen this before with cross and local crates... but i know we built this last time so...not sure what were missing

Jacob Rosenthal  [28 days ago]
and those cargo.toml dont seem to have changed so presumably you didnt pull an upstream change...

Jacob Rosenthal  [28 days ago]
So I think the cross docker is mapping from the directory we call from, so cant see up a directory (..) no idea how we got it to work last time then??.. we could run the example from the main directory, but --example doesnt work because they use a virtual workspace

Your Mileage May Vary with Different Architectures

  • Issues with Pi Zero Architecture

cross-rs/cross#261

/Users/DanielBank/workspace/cross/target/release/cross build --release --target=arm-unknown-linux-gnueabihf --manifest-path=examples/tensorflow-mobilenet-v2/Cargo.toml

Unable to find image 'japaric/arm-unknown-linux-gnueabihf:latest' locally

Rust-Cross vs Cross

The repo you want to clone: https://github.com/rust-embedded/cross

Don't be fooled by this one: https://github.com/japaric/rust-cross

Tract / Snips Voice Interaction Base Kit

Tract

https://github.com/snipsco/tract

https://github.com/snipsco/tract/tree/master/examples/tensorflow-mobilenet-v2

# Just so I am not flailing around trying to remember commands
cd /Users/DanielBank/workspace/tract/target/armv7-unknown-linux-gnueabihf
scp ./tract-mobilenet-v2-example pi@<IP>:compiled-tract/tract-mobilenet-v2-example

Snips Voice Interaction Base Kit

sam devices
sam connect <IP>
sam test speaker
sam setup audio
sam install assistant

Nightly

rustup default nightly rustup default stable

Pedro Suggestions

  • Specs on a RPI... memory... disk space... nah you can just get a better sd card
@danielbank
Copy link
Author

Ideally, get the toolchain and don't need to do any docker stuff

@danielbank
Copy link
Author

Phillip Oppermans blog on building from scratch

@danielbank
Copy link
Author

cargo install --force --git https://github.com/rust-embedded/cross cross

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment