xref: /aosp_15_r20/external/bazelbuild-rules_rust/examples/bzlmod/cross_compile/MODULE.bazel (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1module(
2    name = "hello_cross",
3    version = "0.0.0",
4)
5
6# https://github.com/bazelbuild/rules_rust/releases
7bazel_dep(name = "rules_rust", version = "0.46.0")
8local_path_override(
9    module_name = "rules_rust",
10    path = "../../..",
11)
12
13# Rules for cross compilation
14bazel_dep(name = "toolchains_musl", version = "0.1.16", dev_dependency = True)
15
16# https://github.com/bazelbuild/platforms/releases
17bazel_dep(name = "platforms", version = "0.0.10")
18
19# https://github.com/bazel-contrib/toolchains_llvm
20bazel_dep(name = "toolchains_llvm", version = "1.0.0")
21
22# https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/repo/http.bzl
23http_archive = use_repo_rule("@bazel_tools//:http.bzl", "http_archive")
24
25# Both, cross compilation and MUSL still need a C/C++ toolchain with sysroot.
26_BUILD_FILE_CONTENT = """
27filegroup(
28  name = "{name}",
29  srcs = glob(["*/**"]),
30  visibility = ["//visibility:public"],
31)
32"""
33
34# Download sysroot
35# https://commondatastorage.googleapis.com/chrome-linux-sysroot/
36http_archive(
37    name = "org_chromium_sysroot_linux_x64",
38    build_file_content = _BUILD_FILE_CONTENT.format(name = "sysroot"),
39    sha256 = "f6b758d880a6df264e2581788741623320d548508f07ffc2ae6a29d0c13d647d",
40    urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/2e7ada854015a4cc60fc812112d261af44213ed0/debian_bullseye_amd64_sysroot.tar.xz"],
41)
42
43http_archive(
44    name = "org_chromium_sysroot_linux_aarch64",
45    build_file_content = _BUILD_FILE_CONTENT.format(name = "sysroot"),
46    sha256 = "902d1a40a5fd8c3764a36c8d377af5945a92e3d264c6252855bda4d7ef81d3df",
47    urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/41a6c8dec4c4304d6509e30cbaf9218dffb4438e/debian_bullseye_arm64_sysroot.tar.xz"],
48)
49
50# LLVM setup
51# https://github.com/bazel-contrib/toolchains_llvm/tree/0d302de75f6ace071ac616fb274481eedcc20e5a?tab=readme-ov-file#sysroots
52llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
53
54# LLVM Versions and platforms
55# https://github.com/bazel-contrib/toolchains_llvm/blob/master/toolchain/internal/llvm_distributions.bzl
56LLVM_VERSIONS = {
57    "": "16.0.0",
58    "darwin-aarch64": "16.0.3",
59    "darwin-x86_64": "15.0.7",
60}
61
62# Host LLVM toolchain.
63llvm.toolchain(
64    name = "llvm_toolchain",
65    llvm_versions = LLVM_VERSIONS,
66)
67use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")
68
69# X86 LLVM Toolchain with sysroot.
70# https://github.com/bazel-contrib/toolchains_llvm/blob/master/tests/WORKSPACE.bzlmod
71llvm.toolchain(
72    name = "llvm_toolchain_x86_with_sysroot",
73    llvm_versions = LLVM_VERSIONS,
74)
75llvm.sysroot(
76    name = "llvm_toolchain_x86_with_sysroot",
77    label = "@org_chromium_sysroot_linux_x64//:sysroot",
78    targets = ["linux-x86_64"],
79)
80use_repo(llvm, "llvm_toolchain_x86_with_sysroot")
81
82#
83# ARM (aarch64) LLVM Toolchain with sysroot.
84# https://github.com/bazelbuild/rules_rust/blob/main/examples/bzlmod/cross_compile/WORKSPACE.bzlmod
85llvm.toolchain(
86    name = "llvm_toolchain_aarch64_with_sysroot",
87    llvm_versions = LLVM_VERSIONS,
88)
89llvm.sysroot(
90    name = "llvm_toolchain_aarch64_with_sysroot",
91    label = "@org_chromium_sysroot_linux_aarch64//:sysroot",
92    targets = ["linux-aarch64"],
93)
94use_repo(llvm, "llvm_toolchain_aarch64_with_sysroot")
95
96# Register all LLVM toolchains
97register_toolchains("@llvm_toolchain//:all")
98
99# Rust toolchain
100RUST_EDITION = "2021"
101
102RUST_VERSION = "1.79.0"
103
104rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
105rust.toolchain(
106    edition = RUST_EDITION,
107    extra_target_triples = [
108        "aarch64-unknown-linux-gnu",
109        "x86_64-unknown-linux-gnu",
110    ],
111    versions = [RUST_VERSION],
112)
113use_repo(rust, "rust_toolchains")
114
115register_toolchains("@rust_toolchains//:all")
116