Some of these dependencies link the external library statically, some dynamically, and one ( openssl-sys) gives you the choice □ Running cargo tree -target armv7-unknown-linux-gnueabihf -prefix none | grep - '-sys ' | sort shows the *-sys dependencies which we’re going to need to link: alsa-sys v0.1.2 Throughout this article, we’re going to be working with spotifyd, an open-source background service that streams from Spotify. The fact that they all have the same suffix means we can use cargo tree -prefix none | grep - '-sys ' to enumerate them all ✨.Getting all the *-sys crates to build is the hard part of what we’re doing in this blog post.build a translation layer from the low-level, unsafe Rust bindings to something that’s more idiomatic, and doesn’t require unsafe blocks everywhere. Then, wrap the functionality from foo-sys in Rust-ier code, i.e.This is the ‘minimum viable’ binding to the library from Rust - it should basically attempt to map C functions directly to Rust and not translate anything to Rust types or paradigms. Make a crate named foo-sys, which defines the FFI for the foo (non-Rust) system library.The Rust ecosystem has a really nice convention for crates that do this: If we want to use them from our Rust code, we need to interoperate over a Foreign Function Interface – often referred to as FFI Interop 3. There are lots of great libraries that have been written in languages which aren’t Rust. Let’s talk about external libraries in Rust In the end, I provisioned an 8 CPU / 16 GB RAM / $0.238 per hour Droplet on Digital Ocean, which brought the clean build time down to 90 seconds – just remember to switch it off when you’re done! If you do decide to build on Mac, don’t forget to give your Docker VM a lot more resources than the meagre defaults, but honestly, you may as well just compile on the Raspberry Pi. I expected this to be the cause of a little bit of slowness, but clean builds on OSX took as long as on the much-lower power Raspberry Pi 2. On OSX, those technologies aren’t available, so Docker Desktop runs a Linux VM. Let’s get started! ⚠️ Warning: this is all terribly slow on OSX and probably Windows, use Linux insteadĭocker uses a whole bunch of Linux native technologies to provide isolation for a minimal performance cost. In this post, we’re going to make it work for a pretty simple project. Just because it’s fiddly, though, doesn’t mean it’s not tractable. This is a huge improvement on the former state of the art – just install two programs, and run the build command! But the simplicity starts to fall apart once you need to link external libraries – those that weren’t written in Rust and are (usually) distributed using a mechanism other than Rust’s cargo package manager. # Just replace the word 'cargo' with the word 'cross' in your # build command cross build -target =armv7-unknown-linux-gnueabihf ![]() Modern Rust makes it mostly not horrendous you can cross-compile a hello-world program in ~5 shell commands: With that out of the way… Cross-compiling pure Rust is really easyĬompiling code from one computer for another computer is called cross-compiling, and it’s historically been horrendous. If you’re thinking about setting up cross-compilation for your project, please carefully evaluate whether it’s really going to save you and your team time in the long run, and make sure you consider that an incremental build is probably much, much quicker than your initial build. It’s taken four days of intense trialling and erroring and footguns and now I’m writing about it to hopefully save you, Dear Reader, from trialling and erroring and footguns in the future. I decided to try and build the code on a much faster computer instead, and then copy the produced binary onto the Raspberry Pi. My Raspberry Pi (model 3+) has exactly 1GB of RAM and a very small CPU 1.īut hey, let’s give it a shot anyway.The Rust compiler requires a lot of RAM and CPU. ![]() I’m picking up a project again which, for a complicated set of reasons, requires me to make changes to a large Rust application which is going to run on the Raspberry Pi. FYI: this is part one of a two-part series! Part two is available here.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |