diff --git a/docs/defs.md b/docs/defs.md index 293ac29f39..b50bf31733 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -93,7 +93,7 @@ Add additional rustc_flags from the command line with `--@rules_rust//:extra_rus
rust_binary(name, aliases, compile_data, crate_features, crate_name, crate_root, crate_type, data, deps, edition, experimental_use_cc_common_link, linker_script, malloc, out_binary, - proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, stamp, version) + platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, stamp, version)Builds a Rust binary crate. @@ -202,6 +202,7 @@ is available under the key `dsym_folder` in `OutputGroupInfo`. | linker_script | Link script to forward into linker via rustc options. | Label | optional |
None
|
| malloc | Override the default dependency on malloc
.@bazel_tools//tools/cpp:malloc"
, which is an empty library and the resulting binary will use libc's malloc
. This label must refer to a cc_library
rule. | Label | optional | @bazel_tools//tools/cpp:malloc
|
| out_binary | Force a target, regardless of it's crate_type
, to always mark the file as executable. This attribute is only used to support wasm targets but is expected to be removed following a resolution to https://github.com/bazelbuild/rules_rust/issues/771. | Boolean | optional | False
|
+| platform | Optional platform to transition the binary to. | Label | optional | None
|
| proc_macro_deps | List of rust_proc_macro
targets used to help build this library target. | List of labels | optional | []
|
| rustc_env | Dictionary of additional "key": "value"
environment variables to set for rustc.{}
|
| rustc_env_files | Files containing additional environment variables to set for rustc.NAME=value
, and newlines may be included in a value by ending a line with a trailing back-slash (\\
).stamp
attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. NAME={WORKSPACE_STATUS_VARIABLE}
. | List of labels | optional | []
|
@@ -402,8 +403,8 @@ Builds a Rust proc-macro crate.
rust_shared_library(name, aliases, compile_data, crate_features, crate_name, crate_root, data, deps, - edition, experimental_use_cc_common_link, malloc, proc_macro_deps, rustc_env, - rustc_env_files, rustc_flags, srcs, stamp, version) + edition, experimental_use_cc_common_link, malloc, platform, proc_macro_deps, + rustc_env, rustc_env_files, rustc_flags, srcs, stamp, version)Builds a Rust shared library. @@ -433,6 +434,7 @@ When building the whole binary in Bazel, use `rust_library` instead. | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional |
""
|
| experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | -1
|
| malloc | Override the default dependency on malloc
.@bazel_tools//tools/cpp:malloc"
, which is an empty library and the resulting binary will use libc's malloc
. This label must refer to a cc_library
rule. | Label | optional | @bazel_tools//tools/cpp:malloc
|
+| platform | Optional platform to transition the shared library to. | Label | optional | None
|
| proc_macro_deps | List of rust_proc_macro
targets used to help build this library target. | List of labels | optional | []
|
| rustc_env | Dictionary of additional "key": "value"
environment variables to set for rustc.{}
|
| rustc_env_files | Files containing additional environment variables to set for rustc.NAME=value
, and newlines may be included in a value by ending a line with a trailing back-slash (\\
).stamp
attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. NAME={WORKSPACE_STATUS_VARIABLE}
. | List of labels | optional | []
|
@@ -448,8 +450,8 @@ When building the whole binary in Bazel, use `rust_library` instead.
rust_static_library(name, aliases, compile_data, crate_features, crate_name, crate_root, data, deps, - edition, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, stamp, - version) + edition, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, + stamp, version)Builds a Rust static library. @@ -477,6 +479,7 @@ When building the whole binary in Bazel, use `rust_library` instead. | data | List of files used by this rule at compile time and runtime.
compile_data
over data
, to prevent the data also being included in the runfiles. | List of labels | optional | []
|
| deps | List of other libraries to be linked to this library target.rust_library
targets or cc_library
targets if linking a native library. | List of labels | optional | []
|
| edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | ""
|
+| platform | Optional platform to transition the static library to. | Label | optional | None
|
| proc_macro_deps | List of rust_proc_macro
targets used to help build this library target. | List of labels | optional | []
|
| rustc_env | Dictionary of additional "key": "value"
environment variables to set for rustc.{}
|
| rustc_env_files | Files containing additional environment variables to set for rustc.NAME=value
, and newlines may be included in a value by ending a line with a trailing back-slash (\\
).stamp
attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. NAME={WORKSPACE_STATUS_VARIABLE}
. | List of labels | optional | []
|
@@ -492,7 +495,7 @@ When building the whole binary in Bazel, use `rust_library` instead.
rust_test(name, aliases, compile_data, crate, crate_features, crate_name, crate_root, data, deps, - edition, env, experimental_use_cc_common_link, malloc, proc_macro_deps, rustc_env, + edition, env, experimental_use_cc_common_link, malloc, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, stamp, use_libtest_harness, version)@@ -633,6 +636,7 @@ Run the test with `bazel test //hello_lib:greeting_test`. | env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to
$(rootpath)
, $(execpath)
, location, and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | {}
|
| experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | -1
|
| malloc | Override the default dependency on malloc
.@bazel_tools//tools/cpp:malloc"
, which is an empty library and the resulting binary will use libc's malloc
. This label must refer to a cc_library
rule. | Label | optional | @bazel_tools//tools/cpp:malloc
|
+| platform | Optional platform to transition the test to. | Label | optional | None
|
| proc_macro_deps | List of rust_proc_macro
targets used to help build this library target. | List of labels | optional | []
|
| rustc_env | Dictionary of additional "key": "value"
environment variables to set for rustc.{}
|
| rustc_env_files | Files containing additional environment variables to set for rustc.NAME=value
, and newlines may be included in a value by ending a line with a trailing back-slash (\\
).stamp
attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. NAME={WORKSPACE_STATUS_VARIABLE}
. | List of labels | optional | []
|
diff --git a/docs/flatten.md b/docs/flatten.md
index 00af38c3ca..6ffedec623 100644
--- a/docs/flatten.md
+++ b/docs/flatten.md
@@ -234,7 +234,7 @@ A toolchain for [rust-analyzer](https://rust-analyzer.github.io/).
rust_binary(name, aliases, compile_data, crate_features, crate_name, crate_root, crate_type, data, deps, edition, experimental_use_cc_common_link, linker_script, malloc, out_binary, - proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, stamp, version) + platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, stamp, version)Builds a Rust binary crate. @@ -343,6 +343,7 @@ is available under the key `dsym_folder` in `OutputGroupInfo`. | linker_script | Link script to forward into linker via rustc options. | Label | optional |
None
|
| malloc | Override the default dependency on malloc
.@bazel_tools//tools/cpp:malloc"
, which is an empty library and the resulting binary will use libc's malloc
. This label must refer to a cc_library
rule. | Label | optional | @bazel_tools//tools/cpp:malloc
|
| out_binary | Force a target, regardless of it's crate_type
, to always mark the file as executable. This attribute is only used to support wasm targets but is expected to be removed following a resolution to https://github.com/bazelbuild/rules_rust/issues/771. | Boolean | optional | False
|
+| platform | Optional platform to transition the binary to. | Label | optional | None
|
| proc_macro_deps | List of rust_proc_macro
targets used to help build this library target. | List of labels | optional | []
|
| rustc_env | Dictionary of additional "key": "value"
environment variables to set for rustc.{}
|
| rustc_env_files | Files containing additional environment variables to set for rustc.NAME=value
, and newlines may be included in a value by ending a line with a trailing back-slash (\\
).stamp
attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. NAME={WORKSPACE_STATUS_VARIABLE}
. | List of labels | optional | []
|
@@ -904,8 +905,8 @@ rust_binary(
rust_shared_library(name, aliases, compile_data, crate_features, crate_name, crate_root, data, deps, - edition, experimental_use_cc_common_link, malloc, proc_macro_deps, rustc_env, - rustc_env_files, rustc_flags, srcs, stamp, version) + edition, experimental_use_cc_common_link, malloc, platform, proc_macro_deps, + rustc_env, rustc_env_files, rustc_flags, srcs, stamp, version)Builds a Rust shared library. @@ -935,6 +936,7 @@ When building the whole binary in Bazel, use `rust_library` instead. | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional |
""
|
| experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | -1
|
| malloc | Override the default dependency on malloc
.@bazel_tools//tools/cpp:malloc"
, which is an empty library and the resulting binary will use libc's malloc
. This label must refer to a cc_library
rule. | Label | optional | @bazel_tools//tools/cpp:malloc
|
+| platform | Optional platform to transition the shared library to. | Label | optional | None
|
| proc_macro_deps | List of rust_proc_macro
targets used to help build this library target. | List of labels | optional | []
|
| rustc_env | Dictionary of additional "key": "value"
environment variables to set for rustc.{}
|
| rustc_env_files | Files containing additional environment variables to set for rustc.NAME=value
, and newlines may be included in a value by ending a line with a trailing back-slash (\\
).stamp
attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. NAME={WORKSPACE_STATUS_VARIABLE}
. | List of labels | optional | []
|
@@ -950,8 +952,8 @@ When building the whole binary in Bazel, use `rust_library` instead.
rust_static_library(name, aliases, compile_data, crate_features, crate_name, crate_root, data, deps, - edition, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, stamp, - version) + edition, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, + stamp, version)Builds a Rust static library. @@ -979,6 +981,7 @@ When building the whole binary in Bazel, use `rust_library` instead. | data | List of files used by this rule at compile time and runtime.
compile_data
over data
, to prevent the data also being included in the runfiles. | List of labels | optional | []
|
| deps | List of other libraries to be linked to this library target.rust_library
targets or cc_library
targets if linking a native library. | List of labels | optional | []
|
| edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | ""
|
+| platform | Optional platform to transition the static library to. | Label | optional | None
|
| proc_macro_deps | List of rust_proc_macro
targets used to help build this library target. | List of labels | optional | []
|
| rustc_env | Dictionary of additional "key": "value"
environment variables to set for rustc.{}
|
| rustc_env_files | Files containing additional environment variables to set for rustc.NAME=value
, and newlines may be included in a value by ending a line with a trailing back-slash (\\
).stamp
attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. NAME={WORKSPACE_STATUS_VARIABLE}
. | List of labels | optional | []
|
@@ -1013,7 +1016,7 @@ A dedicated filegroup-like rule for Rust stdlib artifacts.
rust_test(name, aliases, compile_data, crate, crate_features, crate_name, crate_root, data, deps, - edition, env, experimental_use_cc_common_link, malloc, proc_macro_deps, rustc_env, + edition, env, experimental_use_cc_common_link, malloc, platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, srcs, stamp, use_libtest_harness, version)@@ -1154,6 +1157,7 @@ Run the test with `bazel test //hello_lib:greeting_test`. | env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to
$(rootpath)
, $(execpath)
, location, and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | Dictionary: String -> String | optional | {}
|
| experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | -1
|
| malloc | Override the default dependency on malloc
.@bazel_tools//tools/cpp:malloc"
, which is an empty library and the resulting binary will use libc's malloc
. This label must refer to a cc_library
rule. | Label | optional | @bazel_tools//tools/cpp:malloc
|
+| platform | Optional platform to transition the test to. | Label | optional | None
|
| proc_macro_deps | List of rust_proc_macro
targets used to help build this library target. | List of labels | optional | []
|
| rustc_env | Dictionary of additional "key": "value"
environment variables to set for rustc.{}
|
| rustc_env_files | Files containing additional environment variables to set for rustc.NAME=value
, and newlines may be included in a value by ending a line with a trailing back-slash (\\
).stamp
attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. NAME={WORKSPACE_STATUS_VARIABLE}
. | List of labels | optional | []
|
diff --git a/examples/nix_cross_compiling/BUILD.bazel b/examples/nix_cross_compiling/BUILD.bazel
index 702f9ca35b..257a94728e 100644
--- a/examples/nix_cross_compiling/BUILD.bazel
+++ b/examples/nix_cross_compiling/BUILD.bazel
@@ -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",
-)
diff --git a/examples/nix_cross_compiling/cc_binary/BUILD.bazel b/examples/nix_cross_compiling/cc_binary/BUILD.bazel
index 68085d41c8..f68b240eea 100644
--- a/examples/nix_cross_compiling/cc_binary/BUILD.bazel
+++ b/examples/nix_cross_compiling/cc_binary/BUILD.bazel
@@ -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({
diff --git a/examples/nix_cross_compiling/rust_binary/BUILD.bazel b/examples/nix_cross_compiling/rust_binary/BUILD.bazel
index 71368e7b1e..dcc1b4a6fb 100644
--- a/examples/nix_cross_compiling/rust_binary/BUILD.bazel
+++ b/examples/nix_cross_compiling/rust_binary/BUILD.bazel
@@ -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",
+ ],
)
diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl
index 7e5b76aada..fc118251fe 100644
--- a/rust/private/rust.bzl
+++ b/rust/private/rust.bzl
@@ -35,6 +35,20 @@ load(
"transform_sources",
)
+# Based on https://github.com/bazelbuild/bazel/discussions/18628#discussioncomment-6669880
+# Once this officially exists, delete this copy.
+def _change_platform():
+ def _change_platform_impl(settings, attr):
+ return {
+ "//command_line_option:platforms": str(attr.platform) if attr.platform else settings["//command_line_option:platforms"],
+ }
+
+ return transition(
+ implementation = _change_platform_impl,
+ inputs = ["//command_line_option:platforms"],
+ outputs = ["//command_line_option:platforms"],
+ )
+
# TODO(marco): Separate each rule into its own file.
def _assert_no_deprecated_attributes(_ctx):
@@ -569,7 +583,7 @@ _common_attrs = {
The order that these files will be processed is unspecified, so
multiple definitions of a particular variable are discouraged.
- Note that the variables here are subject to
+ Note that the variables here are subject to
[workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status)
stamping should the `stamp` attribute be enabled. Stamp variables
should be wrapped in brackets in order to be resolved. E.g.
@@ -582,7 +596,7 @@ _common_attrs = {
List of compiler flags passed to `rustc`.
These strings are subject to Make variable expansion for predefined
- source/output path variables like `$location`, `$execpath`, and
+ source/output path variables like `$location`, `$execpath`, and
`$rootpath`. This expansion is useful if you wish to pass a generated
file of arguments to rustc: `@$(location //package:target)`.
"""),
@@ -736,7 +750,7 @@ _rust_test_attrs = dict({
mandatory = False,
default = True,
doc = dedent("""\
- Whether to use `libtest`. For targets using this flag, individual tests can be run by using the
+ Whether to use `libtest`. For targets using this flag, individual tests can be run by using the
[--test_arg](https://docs.bazel.build/versions/4.0.0/command-line-reference.html#flag--test_arg) flag.
E.g. `bazel test //src:rust_test --test_arg=foo::test::test_fn`.
"""),
@@ -838,9 +852,19 @@ rust_library = rule(
rust_static_library = rule(
implementation = _rust_static_library_impl,
- attrs = dict(_common_attrs.items()),
+ attrs = dict(_common_attrs.items() + {
+ "platform": attr.label(
+ doc = "Optional platform to transition the static library to.",
+ default = None,
+ providers = [platform_common.PlatformInfo],
+ ),
+ "_allowlist_function_transition": attr.label(
+ default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
+ ),
+ }.items()),
fragments = ["cpp"],
host_fragments = ["cpp"],
+ cfg = _change_platform(),
toolchains = [
str(Label("//rust:toolchain_type")),
"@bazel_tools//tools/cpp:toolchain_type",
@@ -861,13 +885,20 @@ rust_static_library = rule(
rust_shared_library = rule(
implementation = _rust_shared_library_impl,
- attrs = dict(
- _common_attrs.items() + _experimental_use_cc_common_link_attrs.items() + {
- "_use_grep_includes": attr.bool(default = True),
- }.items(),
- ),
+ attrs = dict(_common_attrs.items() + _experimental_use_cc_common_link_attrs.items() + {
+ "platform": attr.label(
+ doc = "Optional platform to transition the shared library to.",
+ default = None,
+ providers = [platform_common.PlatformInfo],
+ ),
+ "_allowlist_function_transition": attr.label(
+ default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
+ ),
+ "_use_grep_includes": attr.bool(default = True),
+ }.items()),
fragments = ["cpp"],
host_fragments = ["cpp"],
+ cfg = _change_platform(),
toolchains = [
str(Label("//rust:toolchain_type")),
"@bazel_tools//tools/cpp:toolchain_type",
@@ -910,11 +941,11 @@ rust_proc_macro = rule(
_allowlist_function_transition = attr.label(default = Label("//tools/allowlists/function_transition_allowlist")),
deps = attr.label_list(
doc = dedent("""\
- List of other libraries to be linked to this library target.
+ List of other libraries to be linked to this library target.
- These can be either other `rust_library` targets or `cc_library` targets if
- linking a native library.
- """),
+ These can be either other `rust_library` targets or `cc_library` targets if
+ linking a native library.
+ """),
cfg = _proc_macro_dep_transition,
),
),
@@ -927,7 +958,7 @@ rust_proc_macro = rule(
incompatible_use_toolchain_transition = True,
doc = dedent("""\
Builds a Rust proc-macro crate.
- """),
+ """),
)
_rust_binary_attrs = dict({
@@ -961,10 +992,20 @@ _rust_binary_attrs = dict({
rust_binary = rule(
implementation = _rust_binary_impl,
provides = _common_providers,
- attrs = dict(_common_attrs.items() + _rust_binary_attrs.items()),
+ attrs = dict(_common_attrs.items() + _rust_binary_attrs.items() + {
+ "platform": attr.label(
+ doc = "Optional platform to transition the binary to.",
+ default = None,
+ providers = [platform_common.PlatformInfo],
+ ),
+ "_allowlist_function_transition": attr.label(
+ default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
+ ),
+ }.items()),
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
+ cfg = _change_platform(),
toolchains = [
str(Label("//rust:toolchain_type")),
"@bazel_tools//tools/cpp:toolchain_type",
@@ -1096,10 +1137,20 @@ def _common_attrs_for_binary_without_process_wrapper(attrs):
rust_binary_without_process_wrapper = rule(
implementation = _rust_binary_impl,
provides = _common_providers,
- attrs = _common_attrs_for_binary_without_process_wrapper(_common_attrs.items() + _rust_binary_attrs.items()),
+ attrs = _common_attrs_for_binary_without_process_wrapper(_common_attrs.items() + _rust_binary_attrs.items() + {
+ "platform": attr.label(
+ doc = "Optional platform to transition the binary to.",
+ default = None,
+ providers = [platform_common.PlatformInfo],
+ ),
+ "_allowlist_function_transition": attr.label(
+ default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
+ ),
+ }.items()),
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
+ cfg = _change_platform(),
toolchains = [
str(Label("//rust:toolchain_type")),
"@bazel_tools//tools/cpp:toolchain_type",
@@ -1123,11 +1174,20 @@ rust_library_without_process_wrapper = rule(
rust_test = rule(
implementation = _rust_test_impl,
provides = _common_providers,
- attrs = dict(_common_attrs.items() +
- _rust_test_attrs.items()),
+ attrs = dict(_common_attrs.items() + _rust_test_attrs.items() + {
+ "platform": attr.label(
+ doc = "Optional platform to transition the test to.",
+ default = None,
+ providers = [platform_common.PlatformInfo],
+ ),
+ "_allowlist_function_transition": attr.label(
+ default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
+ ),
+ }.items()),
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
+ cfg = _change_platform(),
test = True,
toolchains = [
str(Label("//rust:toolchain_type")),