What? Do I really need to configure C++ compiler to compile rust? Cross compilation of the rust code with bazel. Part 1.

We deploy to several different platforms.

  • x86_64 - Linux containers
  • armv7 - linux boards 
  • arm64 - linux boards
  • mipsel - linux boards


 For the development we use x86_64 and arm64 mac OS and x86_64 linux laptops.

Obviously we need to cross-compile our software. Till now we are mostly doing it using `cross-rs` for cross-compiling rust. It works well, only problem is that it is not part of our bazel build, and therefore it is running only when needed, takes more time etc.

In the next weeks I plan to support cross-compilation for our target platforms using bazel.

Cross-compilation support matrix
Host OS x86_64 linux armv7 linux mips linux x86_64 mac os
x86_64 linux ✅ native



x86_64 mac OS 🔄 in progress

✅ native
arm64 mac OS


 

I'll be updating demo-repository as I progress.

In this post I'll start from some basic experiments/configurations.

What? Do I really need to configure C++ compiler to compile rust?

We start with a simple repository that implements one rust project. https://github.com/golovasteek/bazel_cross_compilation/tree/199d1fadb9c36bd96f63bf1b2eff1c3f631a466e
% tree
.
├── LICENSE
├── WORKSPACE
└── rust
    ├── BUILD.bazel
    ├── Cargo.lock
    ├── Cargo.toml
    └── pure_rust_example
        ├── BUILD.bazel
        ├── Cargo.toml
        └── src
            └── main.rs
It can be compiled and executed with bazel: 
`bazel run //rust/pure_rust_example:hello_world`
 
Let's try to cross-compile it. 

Bazel documentation, suggests to use 'platforms' to specify the target system.
Let's define our x86_64_linux platform in ./BUILD.bazel:
 
platform(
name = "x86_64-linux",
constraint_values = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
)

We can ran it like
bazel build //rust/pure_rust_example:hello_world --platforms=//:x86_64-linux

Bazel understands it but fails to compile
 ERROR: /Users/evgenypetrov/work/bazel_cross_compilation/rust/pure_rust_examp
le/BUILD.bazel:4:12: While resolving toolchains for target //rust/pure_rust_example:hel
lo_world: No matching toolchains found for types @bazel_tools//tools/cpp:toolchain_type, @rules_rust//rust:toolchain_type

So, there is missing configuration for the rust toolchain for this platform (toolchain of type "@rules_rust//rust:toolchain_type") and surprisingly "@bazel_tools//tools/cpp:toolchain_type" that is apparently a c++ toolchain.

Luckily, Rust rules for bazel allow to specify extra target platform, when we registering toolchains. It can be done in the rust_register_toolchains call 

rust_register_toolchains(
edition = "2021",
extra_target_triples = [
"x86_64-unknown-linux-musl",
],
)
 
If we try to build now, it doesn't complain about rust toolchain any more, but the c++ toolchain still missing.

So today we created a pure rust repository, added Bazel build files to it. Configured target platform for cross-compilation, and tried to cross-compile.
The rust-toolchain required for the cross-compilation we managed to configure, but the c++ needs to be configured. This would be a topic for the next post.

Comments

Popular posts from this blog

Левиафан

Key West, FL

How prototype Arduino-based fan controller and break 3 boards