bazel-contrib / bazel-contrib/rules_foreign_cc

Support cross-compiling when using Bazel platforms

Open
#869 8 comments 4 reactions 0 assignees View on GitHub
Dominant language
Starlark
Stars
737
Forks
270
PR merge metrics
No merged PRs in 30d

Description

Bazel has a decent support for building for a different target platforms (CPU/OS), but it's missing official toolchains to make it really easy for the end users to cross-compile.

Luckily for us, one such toolchain recently appeared: [bazel-zig-cc](https://git.sr.ht/~motiejus/bazel-zig-cc) (using `zig cc`/`zig c++` [drop-in replacement for GCC/Clang](https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html)).

When cross-compiling for a different target (e.g. using `--platforms @zig_sdk//:linux_aarch64_platform`), which sets appropriate `@platforms` constraints, most of the Bazel rules "just work", however rules using `cmake()` are building for the host platform, since none of the standard cross-compiling CMake variables are set by `rules_foreign_cc`:
```
$ bazel build --incompatible_enable_cc_toolchain_resolution --platforms @zig_sdk//:linux_aarch64_platform --extra_toolchains @zig_sdk//:aarch64-linux-gnu.2.33_toolchain ...
[...]
-- DEBUG: CMAKE_CROSSCOMPILING='FALSE'
-- DEBUG: CMAKE_C_COMPILER_TARGET=''
-- DEBUG: CMAKE_SYSTEM_PROCESSOR='x86_64'
```

Contributor guide

Open the contributing guide

Research direction

Start at the cmake() rule and trace how rules_foreign_cc passes platform information into CMake. Reproduce the issue with the Bazel command and bazel-zig-cc toolchain shown, then verify that CMAKE_CROSSCOMPILING, CMAKE_C_COMPILER_TARGET, and CMAKE_SYSTEM_PROCESSOR reflect the selected target platform.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, zig
Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.