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

Support cross-compiling when using Bazel platforms #869

Open
PiotrSikora opened this issue Feb 4, 2022 · 7 comments
Open

Support cross-compiling when using Bazel platforms #869

PiotrSikora opened this issue Feb 4, 2022 · 7 comments

Comments

@PiotrSikora
Copy link

PiotrSikora commented Feb 4, 2022

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 (using zig cc/zig c++ drop-in replacement for GCC/Clang).

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'
@jheaff1
Copy link
Collaborator

jheaff1 commented Feb 5, 2022

It’s strange that cross compiling doesn’t work for you; I can cross compile the zlib example for Android using the cmake rule just fine.

@PiotrSikora
Copy link
Author

@jheaff1 that's not a good example, because zlib's build doesn't depend on the target platform detection (i.e. the same C source files are compiled on all the platforms, see: madler/zlib's CMakeLists.txt).

Try anything that depends on ${CMAKE_SYSTEM_PROCESSOR} for selecting platform-specific files, and you'll see build failures, because cmake() will select files for the host and not the target platform.

@jheaff1
Copy link
Collaborator

jheaff1 commented Feb 5, 2022

Ah I see.

Perhaps once this issue is resolved, the CI of this repo should utilise the zig toolchain to cross compile libjpeg_turbo, which relies on ${CMAKE_SYSTEM_PROCESSOR}

@github-actions
Copy link

github-actions bot commented Aug 4, 2022

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_foreign_cc!

@cpsauer
Copy link

cpsauer commented Aug 5, 2022

Bump, since still open and useful.

Related: #289

@CareF
Copy link

CareF commented Dec 27, 2022

There seems to have an issue when tool_paths is using relative paths (which would be the case if the cc tools are managed by bazel as a third party dependency)

This seems to be related with bazelbuild/bazel#8438 (comment)

@jheaff1
Copy link
Collaborator

jheaff1 commented Dec 27, 2022

@CareF
To workaround the issue you could wrap the toolchain executables in a script that prepends the path with EXT_BUILD_ROOT, which is set by rules_foreign_cc. An example of this is here:

https://github.com/aspect-build/gcc-toolchain/blob/main/toolchain/wrapper.sh.tpl

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

No branches or pull requests

5 participants