Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use transitioned target platform for platform_transition_test #965

Merged
merged 1 commit into from
Oct 15, 2024
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
15 changes: 5 additions & 10 deletions lib/tests/transitions/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
load("//lib:diff_test.bzl", "diff_test")
load(
"//lib:transitions.bzl",
Expand Down Expand Up @@ -135,22 +136,16 @@ platform_transition_binary(
platform_transition_test(
name = "transitioned_go_test_x86_64",
binary = ":test_transition_test",
# only run this test on x86_64 platforms
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
# only run this test on an x86_64 host
target_compatible_with = [] if sorted(HOST_CONSTRAINTS) == ["@platforms//cpu:x86_64", "@platforms//os:linux"] else ["@platforms//:incompatible"],
target_platform = "x86_64_linux",
)

platform_transition_test(
name = "transitioned_go_test_arm64",
binary = ":test_transition_test",
# only run this test on arm64 platforms
target_compatible_with = [
"@platforms//cpu:arm64",
"@platforms//os:linux",
],
# only run this test on an x86_64 host
target_compatible_with = [] if sorted(HOST_CONSTRAINTS) == ["@platforms//cpu:aarch64", "@platforms//os:linux"] else ["@platforms//:incompatible"],
target_platform = "arm64_linux",
)

Expand Down
41 changes: 27 additions & 14 deletions lib/transitions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ def _platform_transition_binary_impl(ctx):
# forwarding DefaultInfo.

result = []
binary = ctx.attr.binary[0]

# ctx.attr.binary is a singleton list if this rule uses an outgoing transition.
if type(ctx.attr.binary) == type([]):
binary = ctx.attr.binary[0]
else:
binary = ctx.attr.binary

default_info = binary[DefaultInfo]
files = default_info.files
Expand Down Expand Up @@ -87,28 +92,36 @@ def _platform_transition_binary_impl(ctx):

return result

_platform_transition_attrs = {
"basename": attr.string(),
"binary": attr.label(allow_files = True, cfg = _transition_platform),
"target_platform": attr.label(
doc = "The target platform to transition the binary.",
mandatory = True,
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
}
def _get_platform_transition_attrs(binary_cfg):
return {
"basename": attr.string(),
"binary": attr.label(allow_files = True, cfg = binary_cfg),
"target_platform": attr.label(
doc = "The target platform to transition the binary.",
mandatory = True,
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
}

platform_transition_binary = rule(
implementation = _platform_transition_binary_impl,
attrs = _platform_transition_attrs,
# Use an outgoing transition since the target platform of the
# platform_transition_binary doesn't matter and it results in a more
# intuitive output path (matching an untransitioned binary).
attrs = _get_platform_transition_attrs(binary_cfg = _transition_platform),
executable = True,
doc = "Transitions the binary to use the provided platform.",
)

platform_transition_test = rule(
implementation = _platform_transition_binary_impl,
attrs = _platform_transition_attrs,
attrs = _get_platform_transition_attrs(binary_cfg = "target"),
# Use an incoming transition since the target platform of the
# platform_transition_test does matter for the exec platform resolution of
# the test action.
cfg = _transition_platform,
test = True,
doc = "Transitions the test to use the provided platform.",
)
Loading