Cross compilation for ARM v7. Cross compilation of the rust code with bazel. Part 3.
This is the third part of the series "Cross compilation of rust with bazel". Previous topics:
- What? Do I really need to configure C++ compiler to compile rust? Cross compilation of the rust code with bazel. Part 1.
- Compilation from macOS(Intel) to linux x86_64 finaly works. Cross compilation of the rust code with bazel. Part 2.
Next challenge is to cross-compile for armv7.
This part we did together with my colleague Dima and we did it on linux.
First thing, that we need is to add the target platform into our root BUILD.bazel
And rust toolchain for the new target in the WORKSPACE file:
And as we remember from the part 1, we need to add a C++ toolchain as well.
We need to do something similar to what we did for the macOS->Linux crosscompilation toolchain in Part 2.
- get the C++ toolchain binary and sysroot
- add toolchain definition to bazel BUILD.bazel files.
Our host system is Linux now, so we can get a toolchain from the cross compilation docker files.
We just run a container ghcr.io/cross-rs/armv7-unknown-linux-musleabihf:edge and copy the content of /usr/local directory from there into our repo.
Why /usr/local? Because after looking onto content of this container we figured out that it contains all tools that we need, and the structure of the directory is similar to the toolchain from the Part 2
Now we can copy the the BUILD.bazel file from the other toolchain, and adjust it to for the new architecture:
And register the toolchain in the workspace:
Now we can test it:
$ bazel build \
//rust/pure_rust_example:hello_world \
--platforms=//:armv7-linux-musleabihf \
--incompatible_enable_cc_toolchain_resolution
Unfortunatelly, it fails with error:
error: could not exec the linker `toolchains/thirdparty/x86_64-unknown-linux-musl/bin/x86_64-unknown-linux-musl-gcc`
But wait, isn't it part of our toolchain, for crosscompiling from macOS to linux? Why it is used here, if we want to compile for ARM?
If we look at the long command that actually failed. Then we can see that it was building some "process_wrapper" tool, apparently needed for building rust with bazel.
Since our current system is x86_64-linux, the toolchain resolved for it is actually the toolchain, that we used in int the previous chapter. To resolve it we need to specify, that our old toolchain is only compatible with mac os:
And now it builds.
Target //rust/pure_rust_example:hello_world up-to-date:
bazel-bin/rust/pure_rust_example/hello_world
We can copy the result to some host with armv7 architecture and ensure that it works. I've tested it on one of my development boards and it does.
The result of this chapter corresponds to this git revision.
PS
There was a couple of things, that I've noticed while doing this part:
- the linux toolchain has files with different only in the upper/lower case. This means on macOS they may ovewrite each other. Because mac OS by default has case-insensitive file names.
- the linux toolchain is huge and has huge files, and it doesn't work with github without git-lfs. It is not a great idea to store the toochain in the repository.
Comments
Post a Comment