TExcelTAD
Home › Monorepo Builds: Bazel (+ Buck2 & Pants) › Cross-Compiling: What Works Offline

Cross-Compiling: What Works Offline

With platforms and toolchains in place, cross-compiling should be one flag: --platforms=<target>. Whether it really is depends on whether a toolchain for that target exists and is registered. This lesson is honest about it: in the course image (no network, only the system GCC for x86-64) Go cross-compiles out of the box and C++ doesn't.

Language Cross-compiling with Bazel Why
Go works with --platforms=@rules_go//go/toolchain:<os>_<arch> the Go SDK can target every OS/arch; rules_go registers toolchains for all of them (pure Go, no cgo)
Java nothing to do bytecode is platform-independent; the runtime is chosen separately
Python pure Python is portable; native wheels need per-platform pip.parse settings interpreters and wheels are per platform
C/C++ needs a cross toolchain (compiler + sysroot) registered for the target the auto-configured toolchain only knows the host GCC
Rust rules_rust registers toolchains per target triple, plus a C linker for the target

Go: a real cross-compile

bash · try it yourself
mkdir ws && cd ws
cat > MODULE.bazel <<'EOF'
bazel_dep(name = "rules_go", version = "0.65.0")
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.27.3")
EOF
mkdir hello
printf 'load("@rules_go//go:def.bzl", "go_binary")\ngo_binary(name = "hello", srcs = ["main.go"])\n' > hello/BUILD.bazel
printf 'package main\n\nimport (\n\t"fmt"\n\t"runtime"\n)\n\nfunc main() { fmt.Println("hello from", runtime.GOOS+"/"+runtime.GOARCH) }\n' > hello/main.go
bazel build --platforms=@rules_go//go/toolchain:linux_arm64 //hello 2>&1 | grep -E 'up-to-date|bazel-bin|processes'
file -b bazel-bin/hello/hello_/hello | cut -d, -f1,2
./bazel-bin/hello/hello_/hello 2>&1 | head -1 | sed 's/.*: //'
bazel cquery --platforms=@rules_go//go/toolchain:linux_arm64 //hello --output=files 2>/dev/null
bazel cquery --platforms=@rules_go//go/toolchain:linux_arm64 --experimental_platform_in_output_dir //hello --output=files 2>/dev/null
Target //hello:hello up-to-date:
  bazel-bin/hello/hello_/hello
INFO: 12 processes: 8 internal, 4 processwrapper-sandbox.
ELF 64-bit LSB executable, ARM aarch64
Exec format error
bazel-out/k8-fastbuild/bin/hello/hello_/hello
bazel-out/linux_arm64-fastbuild/bin/hello/hello_/hello

The binary is an ARM64 ELF file; the x86-64 container can't execute it (Exec format error), which proves it really is a foreign binary. Note the output path: it still says k8-fastbuild. The directory name comes from the legacy --cpu value, not from the platform, so an ARM build in the same workspace replaces the host build's outputs (and the next host build rebuilds them). --experimental_platform_in_output_dir puts the platform's name in the path (linux_arm64-fastbuild), so both sets of outputs can coexist; teams that build for several platforms set it in .bazelrc.

C++: no toolchain, no build

The same request for a C++ target, with a platform made from the standard constraints:

bash · try it yourself
mkdir ws && cd ws
cat > MODULE.bazel <<'EOF'
bazel_dep(name = "rules_cc", version = "0.2.27")
bazel_dep(name = "platforms", version = "1.0.0")
EOF
mkdir -p app plat
printf 'load("@rules_cc//cc:defs.bzl", "cc_binary")\ncc_binary(name = "app", srcs = ["main.cc"])\n' > app/BUILD.bazel
echo 'int main() { return 0; }' > app/main.cc
cat > plat/BUILD.bazel <<'EOF'
platform(name = "linux_arm64", constraint_values = ["@platforms//os:linux", "@platforms//cpu:aarch64"])
EOF
bazel build --platforms=//plat:linux_arm64 //app 2>&1 | grep -E -A2 '^ERROR: .*While resolving' | sed 's#/home/learner/ws/##'
ls /usr/bin/ | grep -c aarch64
ERROR: app/BUILD.bazel:2:10: While resolving toolchains for target //app:app (7a95ca1): No matching toolchains found for types:
  @@bazel_tools//tools/cpp:toolchain_type
To debug, rerun with --toolchain_resolution_debug='@@bazel_tools//tools/cpp:toolchain_type'
0

Bazel refused instead of silently using the x86-64 GCC: there is no registered C++ toolchain whose target_compatible_with includes aarch64, and there is no ARM cross compiler in the image. To make it work you would:

  1. add a hermetic cross toolchain module (hermetic_cc_toolchain, toolchains_llvm with a sysroot, or a vendor toolchain) and register_toolchains(...) it, or write a cc_toolchain config for an installed cross GCC;
  2. build with --platforms=//plat:linux_arm64;
  3. run the result under QEMU or on real hardware (a cc_test for ARM can't run on an x86 CI agent unless an emulator is wired in as the test runner or remote executors are ARM).

Things that make cross builds go wrong

Symptom Cause
"No matching toolchains found" nothing registered for the target platform: read --toolchain_resolution_debug
x86 binary when you asked for ARM the rule doesn't use platforms (old rules using --cpu/--crosstool_top) or a genrule calls a compiler directly
A code generator fails with "Exec format error" it was built for the target, not the exec platform: put it in tools/cfg = "exec"
Different results on CI and laptops a non-hermetic toolchain picks up whatever compiler is installed
Tests "pass" without running target_compatible_with made them incompatible and //... skipped them; check the "skipped" count

Test yourself

Why does --platforms=@rules_go//go/toolchain:linux_arm64 work offline here while the C++ ARM build fails?

  1. Go doesn't use platforms
  2. The Go SDK already in the cache can target any GOOS/GOARCH, but no ARM C++ cross toolchain is registered
  3. C++ can't be cross-compiled with Bazel
  4. The C++ build needs a remote cache

Which flag makes ARM and host outputs land in different output directories?

  1. --cpu=arm
  2. --experimental_platform_in_output_dir
  3. --output_base=arm
  4. --disk_cache

A code generator used during an ARM build fails with 'Exec format error'. What is the likely fix?

  1. Use --spawn_strategy=local
  2. Make it an exec-configuration dependency (tools or cfg = "exec")
  3. Add the generator to srcs
  4. Disable the sandbox