Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Feb 4, 2024

Conversation

petermoz
Copy link
Collaborator

@petermoz petermoz commented Feb 4, 2024

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...

@petermoz petermoz requested a review from jcard0na February 4, 2024 02:20
@jcard0na jcard0na merged commit 482f7ec into cardonabits:main Feb 4, 2024
@jcard0na
Copy link
Collaborator

jcard0na commented Feb 4, 2024

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
Copy link
Collaborator Author

petermoz commented Feb 4, 2024

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
Copy link
Collaborator

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
Copy link
Collaborator Author

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
Copy link
Collaborator

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
Copy link
Collaborator Author

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

@petermoz petermoz deleted the cross branch February 24, 2024 22:43
@jcard0na
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants