Skip to content

Commit

Permalink
Make target_compatible_with work better on alias() targets
Browse files Browse the repository at this point in the history
Currently the `target_compatible_with` on `alias()` targets only works
when a `select()` is involved. That's because `alias()` targets by
default don't have a toolchains evaluated.

This patch changes the behaviour by making it so all `alias()` targets
with a `target_compatible_with` attribute are skipped appropriately.
When `target_compatible_with` is present, then toolchains are
evaluated for `alias()` targets.

The implementation is basically an enhancement to @gregestren's
1c3a245 (bazelbuild#14310). In
addition to resolving toolchains when a `select()` is present, Bazel
will now also resolve toolchains when a `target_compatible_with`
attribute is set.
  • Loading branch information
philsc committed Apr 5, 2023
1 parent 177f0d4 commit a474298
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,10 @@ public boolean useToolchainResolution() {
return true;
} else if (mode == ToolchainResolutionMode.HAS_SELECT) {
RawAttributeMapper attr = RawAttributeMapper.of(this);
return (attr.has(RuleClass.CONFIG_SETTING_DEPS_ATTRIBUTE)
&& !attr.get(RuleClass.CONFIG_SETTING_DEPS_ATTRIBUTE, BuildType.LABEL_LIST).isEmpty());
return ((attr.has(RuleClass.CONFIG_SETTING_DEPS_ATTRIBUTE)
&& !attr.get(RuleClass.CONFIG_SETTING_DEPS_ATTRIBUTE, BuildType.LABEL_LIST).isEmpty()) ||
(attr.has(RuleClass.TARGET_COMPATIBLE_WITH_ATTR)
&& !attr.get(RuleClass.TARGET_COMPATIBLE_WITH_ATTR, BuildType.LABEL_LIST).isEmpty()));
} else {
return false;
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/shell/integration/target_compatible_with_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1584,4 +1584,38 @@ EOF
expect_not_log "${debug_message5}"
}

# Validate what happens when setting `target_compatible_with` directly on an
# alias(). This is a regression test for
# https:/bazelbuild/bazel/issues/17663.
function test_alias_incompatibility() {
cat >> target_skipping/BUILD <<'EOF'
filegroup(
name = "test_cc_filegroup",
srcs = ["test.cc"],
)
alias(
name = "test_cc_filegroup_alias",
actual = ":test_cc_filegroup",
target_compatible_with = [":foo3"],
)
cc_library(
name = "test_cc",
srcs = [":test_cc_filegroup_alias"],
)
EOF

echo > target_skipping/test.cc

cd target_skipping || fail "couldn't cd into workspace"
bazel build \
--show_result=10 \
--host_platform=@//target_skipping:foo1_bar1_platform \
--platforms=@//target_skipping:foo1_bar1_platform \
//target_skipping:test_cc &> "${TEST_log}" \
&& fail "Bazel passed unexpectedly"
expect_log_once 'ERROR: Target //target_skipping:test_cc is incompatible and cannot be built, but was explicitly requested.'
}

run_suite "target_compatible_with tests"

0 comments on commit a474298

Please sign in to comment.