Posts

Showing posts with the label bazel cross-compilation from scratch

Scaling number of supported platforms. Cross compilation of the rust code with bazel. Part 5

Image
  This is the 5th part of Basel cross-compilation series. Previous posts: What? Do I really need to configure C++ compiler to compile rust? Compilation from macOS(Intel) to linux x86_64 finaly works.  Cross compilation for ARM v7.  Fetching toolchains at build time.   All the build files in the previous steps looked pretty much the same, the only differences were: URLs exec architecture target architecture some compiler flags (like paths) Of cause we don't want to support a separate build file for every host and target combination. The way around it maybe to generate it from the template. The easiest way would be to use string formatting int the *.bzl file. So we create `toolchains/cpp_toolchains.bzl`, and store the BUILD file content in the variable there: BUILD_FILE_TEMPLATE = """ load("@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl", "cc_toolchain_config") load...

Fetching toolchains at build time. Cross compilation of the rust code with bazel. Part 4

Adding toolchain binaries directly int the repository isn't actually a good idea: the repository is getting bigger and bigger. It takes a while to check it out even in a the case, you want just to take a look. So to add new toolchain, we will try to fetch it on "runtime", i.e. when we execute some bazel command. Today we will add a toolchain, that compiles aarch64 binaries from macOS. For fetching something from internet, bazel has an "http_archive" rule, we have already used it to fetch the rust_rules in the Part 1. We can fetch arbitrary archives with this rule, in our case the toolchain tarball. http_archive ( name = "aarch64-unknown-linux-musl" , sha256 = "4fbe95500c327828b437f380bb15851e9a8126cf95180fbf15b76b78e0322ae3" , urls = [ "https://github.com/messense/homebrew-macos-cross-toolchains/releases/download/v11.2.0-1/aarch64-unknown-linux-musl-x86_64-darwin.tar.gz" ], strip_prefix = "aarch64-unknown-linux-...

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 platform ( name = "armv7-linux-musleabihf" , constraint_values = [ "@platforms//cpu:armv7" , "@platforms//os:linux" , ], ) And rust toolchain for the new target in the WORKSPACE file: rust_register_toolchains ( edition = "2021" , extra_target_triples = [ "x86_64-unknown-linux-musl" , "armv7-unknown-linux-musleabihf" , # NEW ], ) And as we remember from the part 1 ,...

Compilation from macOS(Intel) to linux x86_64 finaly works. Cross compilation of the rust code with bazel. Part 2.

This is the continuation of cross-compilation  topic that I've started in previous part What? Do I really need to configure C++ compiler to compile rust? Cross compilation of the rust code with bazel. Part 1. Since we need to add C++ cross-compilation toolchain, let's add the pure C++ project. This will help us to test the C++ toolchain separately I've added a typical C++ hello world impliemntaion and a BUILD file for it: cc_binary ( name = "hello-world" , srcs = [ "main.cpp" ], ) And our goal is to make it compilable for linux by command: bazel build //cpp/hello_world:hello-world --platforms=//:x86_64-linux --incompatible_enable_cc_toolchain_resolution If we just try it, we get and error,  (23:01:17) ERROR: /private/var/tmp/_bazel_evgenypetrov/e83d964deb47c21842622d1b7ffa55fc/external/bazel_tools/tools/cpp/BUILD:58:19: in cc_toolchain_alias rule @baze l_tools//tools/cpp:current_cc_toolchain: Unable to find a CC toolchain using toolchain resolution. ...

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