Skip to content

Commit

Permalink
Updated Bazelmod cross compilation to catch MacOS sys dependency issu…
Browse files Browse the repository at this point in the history
…es (#2926)

As discussed in issue
#2917,
this updated cross compilation example serves as a bridge until the
exact cause of the sys crates not building on Mac issue has been found
and better regression testing has been added.

Also, I took the freedom to trim and streamline the LLVM setup making it
easier to configure while preserving the property that it caches failing
sys crate builds. Updated the Readme accordingly.

---------

Signed-off-by: Marvin Hansen <[email protected]>
  • Loading branch information
marvin-hansen authored Oct 21, 2024
1 parent f213d28 commit 1134392
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 149 deletions.
10 changes: 9 additions & 1 deletion .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,14 @@ tasks:
- "//:all"
test_targets:
- "//..."
macos_bzlmod_cross_compile:
name: Cross compilation with bzlmod on macOS
platform: macos
working_directory: examples/bzlmod/cross_compile
build_targets:
- "//:all"
test_targets:
- "//..."
bzlmod_ffi:
name: FFI with bzlmod
platform: ubuntu2004
Expand Down Expand Up @@ -858,7 +866,7 @@ tasks:
working_directory: examples/bzlmod/proto
build_targets:
- "//..."
bzlmod_proto:
bzlmod_protoc:
name: Proto and Prost with prebuilt protoc with bzlmod
platform: ubuntu2004
working_directory: examples/bzlmod/proto_with_toolchain
Expand Down
21 changes: 21 additions & 0 deletions examples/bzlmod/cross_compile/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Required on windows
startup --windows_enable_symlinks
build:windows --enable_runfiles

# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips.
build --incompatible_disallow_empty_glob

# Required for cargo_build_script support before Bazel 7
build --incompatible_merge_fixed_and_default_shell_env

## Cross compilation configuration
# Debug toolchain resolution
# build --toolchain_resolution_debug='@bazel_tools//tools/cpp:toolchain_type

# Required to support cross compilation from different host systems i.e Mac and Linux
# https://bazel.build/reference/command-line-reference#flag--enable_platform_specific_config
common --enable_platform_specific_config

# Required to resolve C/C++ toolchain.
# https://github.com/bazel-contrib/musl-toolchain?tab=readme-ov-file#setup
build --incompatible_enable_cc_toolchain_resolution
3 changes: 2 additions & 1 deletion examples/bzlmod/cross_compile/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/bazel-*
.DS_Store
.DS_Store
target-bzl
15 changes: 12 additions & 3 deletions examples/bzlmod/cross_compile/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,30 @@ filegroup(
rust_binary(
name = "hello_world_host",
srcs = ["src/main.rs"],
deps = [],
deps = [
"@crates//:lz4-sys",
"@crates//:mimalloc",
],
)

rust_binary(
name = "hello_world_x86_64",
srcs = ["src/main.rs"],
platform = "//build/platforms:linux-x86_64",
deps = [],
deps = [
"@crates//:lz4-sys",
"@crates//:mimalloc",
],
)

rust_binary(
name = "hello_world_aarch64",
srcs = ["src/main.rs"],
platform = "//build/platforms:linux-aarch64",
deps = [],
deps = [
"@crates//:lz4-sys",
"@crates//:mimalloc",
],
)

# Test if the host binary works.
Expand Down
122 changes: 59 additions & 63 deletions examples/bzlmod/cross_compile/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,84 @@ module(
version = "0.0.0",
)

###############################################################################
# Bazel Dependencies # https://registry.bazel.build/
###############################################################################
# Get latest rules_rust release from:
# https://github.com/bazelbuild/rules_rust/releases
bazel_dep(name = "rules_rust", version = "0.46.0")
bazel_dep(name = "rules_rust", version = "0.52.0")
local_path_override(
module_name = "rules_rust",
path = "../../..",
)

# Rules for cross compilation
bazel_dep(name = "toolchains_musl", version = "0.1.16", dev_dependency = True)

# https://github.com/bazelbuild/platforms/releases
bazel_dep(name = "platforms", version = "0.0.10")

# https://github.com/bazel-contrib/toolchains_llvm
bazel_dep(name = "toolchains_llvm", version = "1.0.0")
bazel_dep(name = "toolchains_llvm", version = "1.2.0", dev_dependency = True)

# https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/repo/http.bzl
http_archive = use_repo_rule("@bazel_tools//:http.bzl", "http_archive")

# Both, cross compilation and MUSL still need a C/C++ toolchain with sysroot.
_BUILD_FILE_CONTENT = """
filegroup(
name = "{name}",
srcs = glob(["*/**"]),
visibility = ["//visibility:public"],
)
"""
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# Download sysroot
###############################################################################
# LLVM Toolchain #
###############################################################################
# INTEL/AMD64 Sysroot. LastModified: 2024-04-26T19:15
# https://commondatastorage.googleapis.com/chrome-linux-sysroot/
http_archive(
name = "org_chromium_sysroot_linux_x64",
build_file_content = _BUILD_FILE_CONTENT.format(name = "sysroot"),
sha256 = "f6b758d880a6df264e2581788741623320d548508f07ffc2ae6a29d0c13d647d",
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/2e7ada854015a4cc60fc812112d261af44213ed0/debian_bullseye_amd64_sysroot.tar.xz"],
name = "sysroot_linux_x64",
build_file = "//build/sysroot:BUILD.bazel",
sha256 = "5df5be9357b425cdd70d92d4697d07e7d55d7a923f037c22dc80a78e85842d2c",
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/4f611ec025be98214164d4bf9fbe8843f58533f7/debian_bullseye_amd64_sysroot.tar.xz"],
)

# ARM 64 Sysroot. LastModified: 2024-04-26T18:33
# https://commondatastorage.googleapis.com/chrome-linux-sysroot/
http_archive(
name = "org_chromium_sysroot_linux_aarch64",
build_file_content = _BUILD_FILE_CONTENT.format(name = "sysroot"),
sha256 = "902d1a40a5fd8c3764a36c8d377af5945a92e3d264c6252855bda4d7ef81d3df",
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/41a6c8dec4c4304d6509e30cbaf9218dffb4438e/debian_bullseye_arm64_sysroot.tar.xz"],
name = "sysroot_linux_aarch64",
build_file = "//build/sysroot:BUILD.bazel",
sha256 = "d303cf3faf7804c9dd24c9b6b167d0345d41d7fe4bfb7d34add3ab342f6a236c",
urls = ["https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/906cc7c6bf47d4bd969a3221fc0602c6b3153caa/debian_bullseye_arm64_sysroot.tar.xz"],
)

# LLVM setup
# https://github.com/bazel-contrib/toolchains_llvm/tree/0d302de75f6ace071ac616fb274481eedcc20e5a?tab=readme-ov-file#sysroots
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")

# LLVM Versions and platforms
# https://github.com/bazel-contrib/toolchains_llvm/blob/master/toolchain/internal/llvm_distributions.bzl
LLVM_VERSIONS = {
"": "16.0.0",
"darwin-aarch64": "16.0.3",
"darwin-x86_64": "15.0.7",
}

# Host LLVM toolchain.
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency = True)
llvm.toolchain(
name = "llvm_toolchain",
llvm_versions = LLVM_VERSIONS,
)
use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")

# X86 LLVM Toolchain with sysroot.
# https://github.com/bazel-contrib/toolchains_llvm/blob/master/tests/WORKSPACE.bzlmod
llvm.toolchain(
name = "llvm_toolchain_x86_with_sysroot",
llvm_versions = LLVM_VERSIONS,
llvm_versions = {
"": "16.0.0",
"darwin-x86_64": "15.0.7", # For CI runner only; remove if you don't have an Intel Mac.
},
stdlib = {
"linux-aarch64": "stdc++",
"linux-x86_64": "stdc++",
},
)
llvm.sysroot(
name = "llvm_toolchain_x86_with_sysroot",
label = "@org_chromium_sysroot_linux_x64//:sysroot",
name = "llvm_toolchain",
label = "@sysroot_linux_x64//:sysroot",
targets = ["linux-x86_64"],
)
use_repo(llvm, "llvm_toolchain_x86_with_sysroot")

#
# ARM (aarch64) LLVM Toolchain with sysroot.
# https://github.com/bazelbuild/rules_rust/blob/main/examples/bzlmod/cross_compile/WORKSPACE.bzlmod
llvm.toolchain(
name = "llvm_toolchain_aarch64_with_sysroot",
llvm_versions = LLVM_VERSIONS,
)
llvm.sysroot(
name = "llvm_toolchain_aarch64_with_sysroot",
label = "@org_chromium_sysroot_linux_aarch64//:sysroot",
name = "llvm_toolchain",
label = "@sysroot_linux_aarch64//:sysroot",
targets = ["linux-aarch64"],
)
use_repo(llvm, "llvm_toolchain_aarch64_with_sysroot")
use_repo(llvm, "llvm_toolchain")

# Register all LLVM toolchains
register_toolchains("@llvm_toolchain//:all")
register_toolchains(
"@llvm_toolchain//:all",
dev_dependency = True,
)

# Rust toolchain
RUST_EDITION = "2021"
###############################################################################
# Rust Toolchain #
###############################################################################
RUST_EDITION = "2021" # NOTE: 2024 will be released with Rust 1.86.0

RUST_VERSION = "1.79.0"
RUST_VERSION = "1.81.0"

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
Expand All @@ -113,3 +94,18 @@ rust.toolchain(
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

###############################################################################
# Rust Dependencies #
###############################################################################
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate.spec(
package = "mimalloc",
version = "0.1.43",
)
crate.spec(
package = "lz4-sys",
version = "1.11.1",
)
crate.from_specs()
use_repo(crate, "crates")
Loading

0 comments on commit 1134392

Please sign in to comment.