Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cargo/private/cargo_build_script_wrapper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def cargo_build_script(
# it is translated to `target_compatible_with` to match the `cfg = "exec"`
# consumption of the target.
script_kwargs["target_compatible_with"] = exec_compatible_with
script_kwargs["exec_compatible_with"] = exec_compatible_with

binary_tags = depset(
(tags if tags else []) + ["manual"],
Expand Down Expand Up @@ -241,6 +242,7 @@ def cargo_build_script(
# This target executes the build script.
_build_script_run(
name = name,
exec_compatible_with = exec_compatible_with,
script = ":{}_".format(name),
data_runfiles = ":{}-".format(name),
data = data,
Expand Down
16 changes: 16 additions & 0 deletions cargo/tests/cargo_build_script/compatible_with/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("//cargo:defs.bzl", "cargo_build_script")
load("//rust:defs.bzl", "rust_library", "rust_test")
load(":exec_constraints_test.bzl", "exec_constraints_test")

environment_group(
name = "foo_environment",
Expand Down Expand Up @@ -39,3 +40,18 @@ rust_test(
edition = "2018",
deps = [":empty_build_script"],
)

cargo_build_script(
name = "exec_constrained_build_script",
testonly = True,
srcs = ["do_nothing.rs"],
edition = "2018",
exec_compatible_with = ["@platforms//os:linux"],
tags = ["manual"],
)

exec_constraints_test(
name = "exec_constraints_test",
build_script = "exec_constrained_build_script",
constraint = "@platforms//os:linux",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Check that a Cargo build script constrains both execution stages."""

def exec_constraints_test(name, build_script, constraint):
"""Assert macro expansion forwards the execution constraint to both actions.

Args:
name: Name of the generated test suite.
build_script: Build script target to inspect.
constraint: Expected execution constraint label.
"""
expected = str(Label(constraint))
for target in [build_script + "_", build_script]:
rule = native.existing_rule(target)
if rule == None:
fail("missing build script target " + target)
actual = rule.get("exec_compatible_with", [])
if len(actual) != 1 or str(actual[0]) != expected:
fail("{} must execute on {}, got {}".format(target, expected, actual))

native.test_suite(name = name, tests = [":test_compatible_with"])
Loading