Skip to content

Confused about cross-compiling #608

@rileym-arcules

Description

@rileym-arcules

I'm trying to cross-compile my project to from host linux-x86_64 to target linux-aarch64.

My understanding from the examples/test MODULE.bazel and README is that all I should need to do is provide a sysroot for the linux-aarch64 target, but that doesn't work? I created each sysroot manually using docker export after installing clang-16 in a corresponding ubuntu:22.04 container.

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")

LLVM_VERSION = "16.0.4"

HOSTS = ["linux-x86_64"]
TARGETS = ["linux-x86_64", "linux-aarch64"]

llvm.toolchain(
    name = "llvm_toolchain",
    llvm_version = LLVM_VERSION,
)

llvm.sysroot(
    name = "llvm_toolchain",
    label = "//:linux-amd64-sysroot-bazel.tar.gz",
    targets = ["linux-x86_64"],
)

llvm.sysroot(
    name = "llvm_toolchain",
    label = "//:linux-arm64-sysroot-bazel.tar.gz",
    targets = ["linux-aarch64"],
)

use_repo(llvm, "llvm_toolchain")
register_toolchains("@llvm_toolchain//:all")

build output:

$ bazel build --toolchain_resolution_debug=@@bazel_tools//tools/cpp:toolchain_type --platforms=@toolchains_llvm//platforms:linux-aarch64 :workspace
INFO: Invocation ID: 71c45898-9f4b-47b6-bded-a4dcd9fb13d1
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform @@toolchains_llvm+//platforms:linux-aarch64
      ToolchainResolution:   Rejected toolchain @@toolchains_llvm++llvm+llvm_toolchain//:cc-clang-riscv32-none; mismatching values: riscv32, none
      ToolchainResolution:   Rejected toolchain @@toolchains_llvm++llvm+llvm_toolchain//:cc-clang-x86_64-linux; mismatching values: x86_64
      ToolchainResolution:   Rejected toolchain @@toolchains_llvm++llvm+llvm_toolchain//:cc-clang-x86_64-none; mismatching values: x86_64, none
      ToolchainResolution:   Rejected toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Rejected toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-k8; mismatching values: x86_64
      // rejected apple_support toolchains
      ToolchainResolution: No @@bazel_tools//tools/cpp:toolchain_type toolchain found for target platform @@toolchains_llvm+//platforms:linux-aarch64.
ERROR: /home/rileym/.cache/bazel/_bazel_rileym/567c132ed103c5789b82d69c88fbdf82/external/rules_cc+/cc/compiler/BUILD:50:14: While resolving toolchains for target @@rules_cc+//cc/compiler:compiler (7ea8d52): 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'
For more information on platforms or toolchains see https://bazel.build/concepts/platforms-intro.
ERROR: /home/rileym/.cache/bazel/_bazel_rileym/567c132ed103c5789b82d69c88fbdf82/external/gperftools+/BUILD.bazel:223:11: errors encountered resolving select() keys for @@gperftools+//:tcmalloc
ERROR: Analysis of target '//:workspace' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.263s, Critical Path: 0.04s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

So I then tried individual toolchains for the different platforms,

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")

LLVM_VERSION = "16.0.4"

HOSTS = ["linux-x86_64"]
TARGETS = ["linux-x86_64", "linux-aarch64"]

llvm.toolchain(
    name = "llvm_toolchain-linux-amd64",
    llvm_version = LLVM_VERSION,
    exec_os = "linux",
    exec_arch = "x86_64",
)

llvm.sysroot(
    name = "llvm_toolchain-linux-amd64",
    label = "//:linux-amd64-sysroot-bazel.tar.gz",
    targets = ["linux-x86_64"],
)

use_repo(llvm, "llvm_toolchain-linux-amd64")
register_toolchains("@llvm_toolchain-linux-amd64//:cc-toolchain-x86_64-linux")

llvm.toolchain(
    name = "llvm_toolchain-linux-arm64",
    llvm_version = LLVM_VERSION,
    exec_os = "linux",
    exec_arch = "aarch64",
)

llvm.sysroot(
    name = "llvm_toolchain-linux-arm64",
    label = "//:linux-arm64-sysroot-bazel.tar.gz",
    targets = ["linux-aarch64"],
)

use_repo(llvm, "llvm_toolchain-linux-arm64")
register_toolchains("@llvm_toolchain-linux-arm64//:cc-toolchain-aarch64-linux")

which seems to get me a little further but still no build.
I don't know why the exec platform isn't resolved to the host platform?

build output:

$ bazel build --toolchain_resolution_debug=@@bazel_tools//tools/cpp:toolchain_type --platforms=@toolchains_llvm//platforms:linux-aarch64 :workspace
INFO: Invocation ID: f20d517c-9b30-4701-8cf1-a52037725227
INFO: ToolchainResolution: Performing resolution of @@bazel_tools//tools/cpp:toolchain_type for target platform @@toolchains_llvm+//platforms:linux-aarch64
      ToolchainResolution:   Rejected toolchain @@toolchains_llvm++llvm+llvm_toolchain-linux-amd64//:cc-clang-x86_64-linux; mismatching values: x86_64
      ToolchainResolution:   Toolchain @@toolchains_llvm++llvm+llvm_toolchain-linux-arm64//:cc-clang-aarch64-linux is compatible with target platform, searching for execution platforms:
      ToolchainResolution:     Incompatible execution platform @@platforms//host:host; mismatching values: aarch64
      ToolchainResolution:   Rejected toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: armv7, android
      ToolchainResolution:   Rejected toolchain @@rules_cc++cc_configure_extension+local_config_cc//:cc-compiler-k8; mismatching values: x86_64
      // rejected apple_support toolchains
      ToolchainResolution: No @@bazel_tools//tools/cpp:toolchain_type toolchain found for target platform @@toolchains_llvm+//platforms:linux-aarch64.
INFO: ToolchainResolution: Target platform @@toolchains_llvm+//platforms:linux-aarch64: Selected execution platform @@platforms//host:host, 
ERROR: /home/rileym/.cache/bazel/_bazel_rileym/567c132ed103c5789b82d69c88fbdf82/external/rules_cc+/cc/BUILD:146:19: in cc_toolchain_alias rule @@rules_cc+//cc:current_cc_toolchain: 
Traceback (most recent call last):
        File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
        File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 165, column 13, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@rules_cc+//cc:current_cc_toolchain, Platform: @@toolchains_llvm+//platforms:linux-aarch64, Exec platform: @@bazel_tools//tools:host_platform
ERROR: /home/rileym/.cache/bazel/_bazel_rileym/567c132ed103c5789b82d69c88fbdf82/external/rules_cc+/cc/BUILD:146:19: Analysis of target '@@rules_cc+//cc:current_cc_toolchain' (config: 7ea8d52) failed
ERROR: Analysis of target '//:workspace' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.336s, Critical Path: 0.04s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

How is this supposed to work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions