Skip to content

Commit 4054654

Browse files
katretraversaro
authored andcommitted
Ensure the hard-coded execution platform label for a toolchain dependency is copied correctly.
Without this, toolchain dependencies do not get the same execution platform as the original target, and toolchain's exec dependencies are not necessarily executable in the original target's actions. PiperOrigin-RevId: 538541526 Change-Id: Icdcaa75c84d3a0f3f952aa491026e72ef88be099
1 parent 8747554 commit 4054654

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

src/main/java/com/google/devtools/build/lib/analysis/Dependency.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,17 @@ public Dependency build() {
9999

100100
protected abstract ImmutableList<String> getTransitionKeys();
101101

102+
@Nullable
103+
protected abstract Label getExecutionPlatformLabel();
104+
102105
/** Returns a copy of this Builder, with the values the same. */
103106
public Builder copy() {
104107
return Dependency.builder()
105108
.setLabel(getLabel())
106109
.setConfiguration(getConfiguration())
107110
.setAspects(getAspects())
108-
.setTransitionKeys(getTransitionKeys());
111+
.setTransitionKeys(getTransitionKeys())
112+
.setExecutionPlatformLabel(getExecutionPlatformLabel());
109113
}
110114
}
111115

src/test/shell/integration/toolchain_transition_test.sh

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,13 @@ register_toolchains(
318318
EOF
319319
}
320320

321-
function write_rule() {
321+
function write_rules() {
322322
local pkg="${1}"
323323
mkdir -p "${pkg}/rule"
324324
cat > "${pkg}/rule/rule.bzl" <<EOF
325325
load("//${pkg}/platform:platform.bzl", "ShowPlatformInfo", "describe_platform_info")
326326
327-
def _sample_impl(ctx):
327+
def _report(ctx):
328328
toolchain = ctx.toolchains["//${pkg}/toolchain:toolchain_type"]
329329
message = ctx.attr.message
330330
exec_platform = describe_platform_info(ctx.attr._exec[ShowPlatformInfo])
@@ -336,6 +336,10 @@ def _sample_impl(ctx):
336336
output = log,
337337
content = str,
338338
)
339+
return log
340+
341+
def _sample_impl(ctx):
342+
log = _report(ctx)
339343
return [DefaultInfo(files = depset([log]))]
340344
341345
sample = rule(
@@ -353,6 +357,41 @@ sample = rule(
353357
toolchains = ["//${pkg}/toolchain:toolchain_type"],
354358
incompatible_use_toolchain_transition = True,
355359
)
360+
361+
def _sample_test_impl(ctx):
362+
log = _report(ctx)
363+
# Write a fake test executable.
364+
executable = ctx.actions.declare_file(ctx.label.name)
365+
ctx.actions.write(
366+
output = executable,
367+
content = "\n".join([
368+
"#!/usr/bin/env bash",
369+
"exit 0",
370+
]),
371+
is_executable = True,
372+
)
373+
374+
return [DefaultInfo(
375+
executable = executable,
376+
files = depset([executable, log]),
377+
)]
378+
379+
sample_test = rule(
380+
implementation = _sample_test_impl,
381+
attrs = {
382+
"message": attr.string(),
383+
"_exec": attr.label(
384+
cfg = "exec",
385+
providers = [ShowPlatformInfo],
386+
default = Label("//${pkg}/platform")),
387+
},
388+
outputs = {
389+
"log": "%{name}.log",
390+
},
391+
toolchains = ["//${pkg}/toolchain:toolchain_type"],
392+
incompatible_use_toolchain_transition = True,
393+
test = True,
394+
)
356395
EOF
357396
cat > "${pkg}/rule/BUILD" <<EOF
358397
package(default_visibility = ["//visibility:public"])
@@ -365,7 +404,7 @@ function test_toolchain_transition() {
365404
write_constraints "${pkg}"
366405
write_platforms "${pkg}"
367406
write_toolchains "${pkg}"
368-
write_rule "${pkg}"
407+
write_rules "${pkg}"
369408

370409
mkdir -p "${pkg}"
371410
cat > "${pkg}/BUILD" <<EOF
@@ -398,6 +437,48 @@ EOF
398437
expect_log 'extra_lib: message: extra_lib foo, target_dep: target, tool_dep: exec-alpha'
399438
}
400439

440+
# Regression test for b/284495846.
441+
# Test rules use the test trimming transition, which means that toolchain
442+
# dependencies are in a different configuration that the actual parent target,
443+
# which caused an issue where the execution platform was not correctly forwarded
444+
# to the toolchain.
445+
function test_toolchain_transition_test_rule() {
446+
local -r pkg="${FUNCNAME[0]}"
447+
write_constraints "${pkg}"
448+
write_platforms "${pkg}"
449+
write_toolchains "${pkg}"
450+
write_rules "${pkg}"
451+
452+
mkdir -p "${pkg}"
453+
cat > "${pkg}/BUILD" <<EOF
454+
package(default_visibility = ["//visibility:public"])
455+
456+
load("//${pkg}/rule:rule.bzl", "sample_test")
457+
458+
# Use a test rul to trigger b/284495846
459+
sample_test(
460+
name = "sample_test",
461+
exec_compatible_with = [
462+
"//${pkg}/constraint:beta",
463+
],
464+
message = "Hello",
465+
)
466+
EOF
467+
468+
bazel build \
469+
--platforms="//${pkg}/platform:target" \
470+
--host_platform="//${pkg}/platform:host" \
471+
--use_target_platform_for_tests \
472+
"//${pkg}:sample_test" &> $TEST_log || fail "Build failed"
473+
474+
# Verify contents of sample_test.log.
475+
cat "bazel-bin/${pkg}/sample_test.log" >> $TEST_log
476+
# The execution platform should be beta.
477+
expect_log 'rule message: "Hello", exec platform: "exec-beta"'
478+
# The toolchain should have proper target and exec matching the top target.
479+
expect_log 'sample_toolchain: message: beta toolchain, target_dep: target, tool_dep: exec-beta'
480+
}
481+
401482
# Regression test for https://github.com/bazelbuild/bazel/issues/11993
402483
# This was causing cquery to not correctly generate ConfiguredTargetKeys for
403484
# toolchains, leading to the message "Targets were missing from graph"
@@ -406,7 +487,7 @@ function test_toolchain_transition_cquery() {
406487
write_constraints "${pkg}"
407488
write_platforms "${pkg}"
408489
write_toolchains "${pkg}"
409-
write_rule "${pkg}"
490+
write_rules "${pkg}"
410491

411492
mkdir -p "${pkg}"
412493
cat > "${pkg}/BUILD" <<EOF

0 commit comments

Comments
 (0)