cardonabits / haxo-rs

Software for the haxophone
MIT License
41 stars 10 forks source link

Added docker file that cross-compiles for Raspberry Pi Zero #33

Closed petermoz closed 4 months ago

petermoz commented 4 months ago

At the moment, https://github.com/cross-rs/cross doesn't support RPi Zero, due to it being the armv6 architecture. Trying to build a cross-rs image with packages from the raspian archive resulted in lots of problems with glibc, so this didn't work either.

Eventually, I pieced together parts from https://capnfabs.net/posts/cross-compiling-rust-apps-linker-shenanigans-multistrap-chroot/ and https://earthly.dev/blog/cross-compiling-raspberry-pi/ to come up with a docker file that uses multistrap to generate a RPi compatible sysroot, and then gets a linker from https://github.com/tttapa/docker-arm-cross-toolchain

The result is a docker image that you can build once with docker build and then call docker run to build (or incrementally rebuild) the haxo binary.

I imagine support for newer RPis will be much simpler to add, but for now at least (I have a RPi Zero 2 coming in the mail :)) I don't have anything else to test on, so I think it's worth submitting this as is.

This is much faster than building on the RPi Zero directly, and I've tested it on Mac M1 and Linux. Mac M1 is much slower than Linux because the container itself is amd64, which means that the Mac is natively arm, emulating amd64, to cross compile to arm. There's probably a better way, but at least it works...

jcard0na commented 4 months ago

Amazing work, @petermoz I just tested this on my linux workstation (AMD Ryzen 7 5700U, archlinux) and it was so fast (the compilation step took 16 seconds) that I had to triple check to convince myself that the actual executable had been built. I cannot test it on a RPi Zero at the moment, but I don't see any reason for postponing the merge.

petermoz commented 4 months ago

Thanks for merging, and cool to see all the CI pipeline stuff you're doing!

I just tested this on my linux workstation (AMD Ryzen 7 5700U, archlinux) and it was so fast (the compilation step took 16 seconds) that I had to triple check to convince myself that the actual executable had been built.

Hah yeah I had a similar reaction myself. It's going to make any subsequent development for the Pi Zero so much easier

jcard0na commented 4 months ago

Hi @petermoz,

I imagine support for newer RPis will be much simpler to add, but for now at least (I have a RPi Zero 2 coming in the mail :))

Did you receive your RPi Zero 2 yet? Do you plan to create another docker file for it? If no, would you mind dumping some notes into an issue with some guidance on how to do that? Maybe someone will pick that challenge. The RPi Zero 2 has become my default architecture at the moment, I would love to add it to the CI workflow. Thanks!

petermoz commented 4 months ago

Hi @jcard0na, yes it arrived, but unfortunately I haven't even managed to solder on the headers yet.

Is there a chance the Pi Zero 1 binary is compatible with the Pi Zero 2 without any changes? I thought it might be, so I copied the same binary across to a fresh raspian install on the Pi Zero 2 and it seems to start ok. Of course it crashes when it can't find the haxo hardware, so I can't check if it fully works - maybe you've discovered an issue further into the code?

jcard0na commented 4 months ago

Is there a chance the Pi Zero 1 binary is compatible with the Pi Zero 2 without any changes?

Oh, I did not think of that. I'll try that next week. Thanks!

petermoz commented 4 months ago

Had a chance to put everything together today, and the same binary works fine on the RPi Zero 2 :)

I have no idea if a binary compiled for armv7 would be any faster

jcard0na commented 4 months ago

Oh, thanks for checking.

I have no idea if a binary compiled for armv7 would be any faster

I doubt there is a meaningful difference as we don't do any floating-point arithmetic.