Skip to content

Commit

Permalink
Platform transition added to Rust binary rules. (#2310)
Browse files Browse the repository at this point in the history
Without much insight into core Bazel's plans for supporting "flagless"
builds across platforms, this seems like the most direct way to select
the platform for a given Rust binary target (all the terminal rules)
that doesn't need additional work to forward providers.

Currently I'm using `platform_transition_binary`
(https://github.com/aspect-build/bazel-lib/blob/main/docs/transitions.md),
but it doesn't work with Rust Analyzer/Clippy/etc. presumably due to not
forwarding the providers. In the case of this particular rule, it also
doesn't work for something like `rust_shared_library()` which is
explicitly not an `executable = True` target.

@illicitonion brought to my attention `with_cfg`
(https://github.com/fmeum/with_cfg.bzl) which could be used as an
alternative by consumers of `rules_rust` instead of this PR. That feels
reasonable, though certainly more work for individual users and appears
to rely on a lot of Bazel black magic.

Along the same lines, this could wait on rule inheritance as presumably
a more formally supported `with_cfg` approach, which would also be up to
individual users and not `rules_rust`.
  • Loading branch information
rickvanprim authored Dec 13, 2023
1 parent c3581a5 commit 0e4be00
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 190 deletions.
16 changes: 10 additions & 6 deletions docs/defs.md

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions docs/flatten.md

Large diffs are not rendered by default.

161 changes: 20 additions & 141 deletions examples/nix_cross_compiling/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,150 +1,29 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("//bazel:transitions.bzl", "platform_transition_binary")

# Disabled targets need the user to supply a sysroot in `flake.nix` first.

build_test(
name = "nix_cross_compiling",
targets = [
# ":cc_binary_aarch64-apple-darwin",
# ":cc_binary_aarch64-apple-ios",
":cc_binary_aarch64-linux-android",
":cc_binary_aarch64-unknown-linux-gnu",
":cc_binary_wasm32-unknown-unknown",
":cc_binary_wasm32-wasi",
# ":cc_binary_x86_64-apple-darwin",
# ":cc_binary_x86_64-pc-windows-msvc",
":cc_binary_x86_64-unknown-linux-gnu",
":cc_binary_x86_64-unknown-nixos-gnu",
# ":rust_binary_aarch64-apple-darwin",
# ":rust_binary_aarch64-apple-ios",
":rust_binary_aarch64-linux-android",
":rust_binary_aarch64-unknown-linux-gnu",
":rust_binary_wasm32-unknown-unknown",
":rust_binary_wasm32-wasi",
# ":rust_binary_x86_64-apple-darwin",
# ":rust_binary_x86_64-pc-windows-msvc",
":rust_binary_x86_64-unknown-linux-gnu",
":rust_binary_x86_64-unknown-nixos-gnu",
# "//cc_binary:cc_binary_aarch64-apple-darwin",
# "//cc_binary:cc_binary_aarch64-apple-ios",
"//cc_binary:cc_binary_aarch64-linux-android",
"//cc_binary:cc_binary_aarch64-unknown-linux-gnu",
"//cc_binary:cc_binary_wasm32-unknown-unknown",
"//cc_binary:cc_binary_wasm32-wasi",
# "//cc_binary:cc_binary_x86_64-apple-darwin",
# "//cc_binary:cc_binary_x86_64-pc-windows-msvc",
"//cc_binary:cc_binary_x86_64-unknown-linux-gnu",
"//cc_binary:cc_binary_x86_64-unknown-nixos-gnu",
# "//rust_binary:rust_binary_aarch64-apple-darwin",
# "//rust_binary:rust_binary_aarch64-apple-ios",
"//rust_binary:rust_binary_aarch64-linux-android",
"//rust_binary:rust_binary_aarch64-unknown-linux-gnu",
"//rust_binary:rust_binary_wasm32-unknown-unknown",
"//rust_binary:rust_binary_wasm32-wasi",
# "//rust_binary:rust_binary_x86_64-apple-darwin",
# "//rust_binary:rust_binary_x86_64-pc-windows-msvc",
"//rust_binary:rust_binary_x86_64-unknown-linux-gnu",
"//rust_binary:rust_binary_x86_64-unknown-nixos-gnu",
],
)

# platform_transition_binary(
# name = "cc_binary_aarch64-apple-darwin",
# binary = "//cc_binary",
# target_platform = "//bazel/platforms:aarch64-apple-darwin",
# )

# platform_transition_binary(
# name = "cc_binary_aarch64-apple-ios",
# binary = "//cc_binary",
# target_platform = "//bazel/platforms:aarch64-apple-ios",
# )

platform_transition_binary(
name = "cc_binary_aarch64-linux-android",
binary = "//cc_binary",
target_platform = "//bazel/platforms:aarch64-linux-android",
)

platform_transition_binary(
name = "cc_binary_aarch64-unknown-linux-gnu",
binary = "//cc_binary",
target_platform = "//bazel/platforms:aarch64-unknown-linux-gnu",
)

platform_transition_binary(
name = "cc_binary_wasm32-unknown-unknown",
binary = "//cc_binary",
target_platform = "//bazel/platforms:wasm32-unknown-unknown",
)

platform_transition_binary(
name = "cc_binary_wasm32-wasi",
binary = "//cc_binary",
target_platform = "//bazel/platforms:wasm32-wasi",
)

# platform_transition_binary(
# name = "cc_binary_x86_64-apple-darwin",
# binary = "//cc_binary",
# target_platform = "//bazel/platforms:x86_64-apple-darwin",
# )

# platform_transition_binary(
# name = "cc_binary_x86_64-pc-windows-msvc",
# binary = "//cc_binary",
# target_platform = "//bazel/platforms:x86_64-pc-windows-msvc",
# )

platform_transition_binary(
name = "cc_binary_x86_64-unknown-linux-gnu",
binary = "//cc_binary",
target_platform = "//bazel/platforms:x86_64-unknown-linux-gnu",
)

platform_transition_binary(
name = "cc_binary_x86_64-unknown-nixos-gnu",
binary = "//cc_binary",
target_platform = "//bazel/platforms:x86_64-unknown-nixos-gnu",
)

# platform_transition_binary(
# name = "rust_binary_aarch64-apple-darwin",
# binary = "//rust_binary",
# target_platform = "//bazel/platforms:aarch64-apple-darwin",
# )

# platform_transition_binary(
# name = "rust_binary_aarch64-apple-ios",
# binary = "//rust_binary",
# target_platform = "//bazel/platforms:aarch64-apple-ios",
# )

platform_transition_binary(
name = "rust_binary_aarch64-linux-android",
binary = "//rust_binary",
target_platform = "//bazel/platforms:aarch64-linux-android",
)

platform_transition_binary(
name = "rust_binary_aarch64-unknown-linux-gnu",
binary = "//rust_binary",
target_platform = "//bazel/platforms:aarch64-unknown-linux-gnu",
)

platform_transition_binary(
name = "rust_binary_wasm32-unknown-unknown",
binary = "//rust_binary",
target_platform = "//bazel/platforms:wasm32-unknown-unknown",
)

platform_transition_binary(
name = "rust_binary_wasm32-wasi",
binary = "//rust_binary",
target_platform = "//bazel/platforms:wasm32-wasi",
)

# platform_transition_binary(
# name = "rust_binary_x86_64-apple-darwin",
# binary = "//rust_binary",
# target_platform = "//bazel/platforms:x86_64-apple-darwin",
# )

# platform_transition_binary(
# name = "rust_binary_x86_64-pc-windows-msvc",
# binary = "//rust_binary",
# target_platform = "//bazel/platforms:x86_64-pc-windows-msvc",
# )

platform_transition_binary(
name = "rust_binary_x86_64-unknown-linux-gnu",
binary = "//rust_binary",
target_platform = "//bazel/platforms:x86_64-unknown-linux-gnu",
)

platform_transition_binary(
name = "rust_binary_x86_64-unknown-nixos-gnu",
binary = "//rust_binary",
target_platform = "//bazel/platforms:x86_64-unknown-nixos-gnu",
)
63 changes: 63 additions & 0 deletions examples/nix_cross_compiling/cc_binary/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,70 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//bazel:transitions.bzl", "platform_transition_binary")

package(default_visibility = ["//visibility:public"])

# Disabled targets need the user to supply a sysroot in `flake.nix` first.

# platform_transition_binary(
# name = "cc_binary_aarch64-apple-darwin",
# binary = ":cc_binary",
# target_platform = "//bazel/platforms:aarch64-apple-darwin",
# )

# platform_transition_binary(
# name = "cc_binary_aarch64-apple-ios",
# binary = ":cc_binary",
# target_platform = "//bazel/platforms:aarch64-apple-ios",
# )

platform_transition_binary(
name = "cc_binary_aarch64-linux-android",
binary = ":cc_binary",
target_platform = "//bazel/platforms:aarch64-linux-android",
)

platform_transition_binary(
name = "cc_binary_aarch64-unknown-linux-gnu",
binary = ":cc_binary",
target_platform = "//bazel/platforms:aarch64-unknown-linux-gnu",
)

platform_transition_binary(
name = "cc_binary_wasm32-unknown-unknown",
binary = ":cc_binary",
target_platform = "//bazel/platforms:wasm32-unknown-unknown",
)

platform_transition_binary(
name = "cc_binary_wasm32-wasi",
binary = ":cc_binary",
target_platform = "//bazel/platforms:wasm32-wasi",
)

# platform_transition_binary(
# name = "cc_binary_x86_64-apple-darwin",
# binary = ":cc_binary",
# target_platform = "//bazel/platforms:x86_64-apple-darwin",
# )

# platform_transition_binary(
# name = "cc_binary_x86_64-pc-windows-msvc",
# binary = ":cc_binary",
# target_platform = "//bazel/platforms:x86_64-pc-windows-msvc",
# )

platform_transition_binary(
name = "cc_binary_x86_64-unknown-linux-gnu",
binary = ":cc_binary",
target_platform = "//bazel/platforms:x86_64-unknown-linux-gnu",
)

platform_transition_binary(
name = "cc_binary_x86_64-unknown-nixos-gnu",
binary = ":cc_binary",
target_platform = "//bazel/platforms:x86_64-unknown-nixos-gnu",
)

cc_binary(
name = "cc_binary",
srcs = select({
Expand Down
130 changes: 113 additions & 17 deletions examples/nix_cross_compiling/rust_binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,123 @@ load("@rules_rust//rust:defs.bzl", "rust_binary")

package(default_visibility = ["//visibility:public"])

# rust_binary(
# name = "rust_binary_aarch64-apple-darwin",
# srcs = ["rust_binary.rs"],
# platform = "//bazel/platforms:aarch64-apple-darwin",
# deps = [
# "//cc_library",
# "//rust_library",
# "@crate_index//:anyhow",
# "@crate_index//:tokio",
# ],
# )

# rust_binary(
# name = "rust_binary_aarch64-apple-ios",
# srcs = ["rust_binary.rs"],
# platform = "//bazel/platforms:aarch64-apple-ios",
# deps = [
# "//cc_library",
# "//rust_library",
# "@crate_index//:anyhow",
# "@crate_index//:tokio",
# ],
# )

rust_binary(
name = "rust_binary_aarch64-linux-android",
srcs = ["rust_binary.rs"],
platform = "//bazel/platforms:aarch64-linux-android",
deps = [
"//cc_library",
"//rust_library",
"@crate_index//:anyhow",
"@crate_index//:tokio",
],
)

rust_binary(
name = "rust_binary_aarch64-unknown-linux-gnu",
srcs = ["rust_binary.rs"],
platform = "//bazel/platforms:aarch64-unknown-linux-gnu",
deps = [
"//cc_library",
"//rust_library",
"@crate_index//:anyhow",
"@crate_index//:tokio",
],
)

rust_binary(
name = "rust_binary",
srcs = select({
# See comments in `rust_binary_wasm32-unknown-unknown.rs`.
"@platforms//os:none": ["rust_binary_wasm32-unknown-unknown.rs"],
# See comments in `rust_binary_wasm32-wasi.rs`.
"@platforms//os:wasi": ["rust_binary_wasm32-wasi.rs"],
"//conditions:default": ["rust_binary.rs"],
}),
tags = ["platform_missing"],
name = "rust_binary_wasm32-unknown-unknown",
srcs = ["rust_binary_wasm32-unknown-unknown.rs"], # See comments in `rust_binary_wasm32-unknown-unknown.rs`.
platform = "//bazel/platforms:wasm32-unknown-unknown",
deps = [
"//cc_library",
"//rust_library",
"@crate_index//:anyhow",
] + select({
"@platforms//os:none": [],
# To be removed once the `wasm32-wasi` version of `tokio` can `select()`
],
)

rust_binary(
name = "rust_binary_wasm32-wasi",
srcs = ["rust_binary_wasm32-wasi.rs"], # See comments in `rust_binary_wasm32-wasi.rs`.
platform = "//bazel/platforms:wasm32-wasi",
deps = [
"//cc_library",
"//rust_library",
"@crate_index//:anyhow",
# To be added once the `wasm32-wasi` version of `tokio` can `select()`
# over different features.
"@platforms//os:wasi": [],
"//conditions:default": [
"@crate_index//:tokio",
],
}),
# "@crate_index//:tokio",
],
)

# rust_binary(
# name = "rust_binary_x86_64-apple-darwin",
# srcs = ["rust_binary.rs"],
# platform = "//bazel/platforms:x86_64-apple-darwin",
# deps = [
# "//cc_library",
# "//rust_library",
# "@crate_index//:anyhow",
# "@crate_index//:tokio",
# ],
# )

# rust_binary(
# name = "rust_binary_x86_64-pc-windows-msvc",
# srcs = ["rust_binary.rs"],
# platform = "//bazel/platforms:x86_64-pc-windows-msvc",
# deps = [
# "//cc_library",
# "//rust_library",
# "@crate_index//:anyhow",
# "@crate_index//:tokio",
# ],
# )

rust_binary(
name = "rust_binary_x86_64-unknown-linux-gnu",
srcs = ["rust_binary.rs"],
platform = "//bazel/platforms:x86_64-unknown-linux-gnu",
deps = [
"//cc_library",
"//rust_library",
"@crate_index//:anyhow",
"@crate_index//:tokio",
],
)

rust_binary(
name = "rust_binary_x86_64-unknown-nixos-gnu",
srcs = ["rust_binary.rs"],
platform = "//bazel/platforms:x86_64-unknown-nixos-gnu",
deps = [
"//cc_library",
"//rust_library",
"@crate_index//:anyhow",
"@crate_index//:tokio",
],
)
Loading

0 comments on commit 0e4be00

Please sign in to comment.