Skip to content

Commit 949145a

Browse files
katreCopybara-Service
authored and
Copybara-Service
committed
Expose the --incompatible_enable_cc_toolchain_resolution flag to Starlark and
update find_cpp_toolchain to use it. Part of #7260. PiperOrigin-RevId: 231400764
1 parent 4e6080a commit 949145a

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public class CcModule
8787
CcCompilationContext,
8888
CcLinkingContext,
8989
LibraryToLinkWrapper,
90-
CcToolchainVariables> {
90+
CcToolchainVariables,
91+
SkylarkRuleContext> {
9192

9293
private enum RegisterActions {
9394
ALWAYS,
@@ -1508,4 +1509,9 @@ private static void getLegacyArtifactNamePatterns(
15081509
private static <T> T nullIfNone(Object object, Class<T> type) {
15091510
return object != Runtime.NONE ? type.cast(object) : null;
15101511
}
1512+
1513+
@Override
1514+
public boolean isCcToolchainResolutionEnabled(SkylarkRuleContext skylarkRuleContext) {
1515+
return CppHelper.useToolchainResolution(skylarkRuleContext.getRuleContext());
1516+
}
15111517
}

src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/BazelCcModuleApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public interface BazelCcModuleApi<
4848
CcCompilationContextT,
4949
LinkingContextT,
5050
LibraryToLinkWrapperT,
51-
CcToolchainVariablesT> {
51+
CcToolchainVariablesT,
52+
SkylarkRuleContextT> {
5253

5354
@SkylarkCallable(
5455
name = "compile",

src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcModuleApi.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.devtools.build.lib.skylarkbuildapi.FileApi;
1919
import com.google.devtools.build.lib.skylarkbuildapi.ProviderApi;
2020
import com.google.devtools.build.lib.skylarkbuildapi.SkylarkActionFactoryApi;
21+
import com.google.devtools.build.lib.skylarkbuildapi.SkylarkRuleContextApi;
2122
import com.google.devtools.build.lib.skylarkinterface.Param;
2223
import com.google.devtools.build.lib.skylarkinterface.ParamType;
2324
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
@@ -39,7 +40,8 @@ public interface CcModuleApi<
3940
CompilationContextT extends CcCompilationContextApi,
4041
LinkingContextT extends CcLinkingContextApi,
4142
LibraryToLinkWrapperT extends LibraryToLinkWrapperApi,
42-
CcToolchainVariablesT extends CcToolchainVariablesApi> {
43+
CcToolchainVariablesT extends CcToolchainVariablesApi,
44+
SkylarkRuleContextT extends SkylarkRuleContextApi> {
4345

4446
@SkylarkCallable(
4547
name = "CcToolchainInfo",
@@ -669,4 +671,18 @@ CompilationContextT createCcCompilationContext(
669671
type = CcToolchainProviderApi.class)
670672
})
671673
String legacyCcFlagsMakeVariable(CcToolchainProviderT ccToolchain);
674+
675+
@SkylarkCallable(
676+
name = "is_cc_toolchain_resolution_enabled_do_not_use",
677+
documented = false,
678+
parameters = {
679+
@Param(
680+
name = "ctx",
681+
positional = false,
682+
named = true,
683+
type = SkylarkRuleContextApi.class,
684+
doc = "The rule context."),
685+
},
686+
doc = "Returns true if the --incompatible_enable_cc_toolchain_resolution flag is enabled.")
687+
boolean isCcToolchainResolutionEnabled(SkylarkRuleContextT ruleContext);
672688
}

src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ public String legacyCcFlagsMakeVariable(CcToolchainProviderApi ccToolchain) {
156156
return "";
157157
}
158158

159+
@Override
160+
public boolean isCcToolchainResolutionEnabled(SkylarkRuleContextApi ruleContext) {
161+
return false;
162+
}
163+
159164
@Override
160165
public CompilationInfoApi compile(
161166
SkylarkRuleContextApi skylarkRuleContext,

src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4636,4 +4636,41 @@ public void testGetLegacyCcFlagsMakeVariable() throws Exception {
46364636

46374637
assertThat(ccFlags).isEqualTo("-test-cflag1 -testcflag2");
46384638
}
4639+
4640+
private boolean toolchainResolutionEnabled() throws Exception {
4641+
scratch.file(
4642+
"a/rule.bzl",
4643+
"def _impl(ctx):",
4644+
" toolchain_resolution_enabled = cc_common.is_cc_toolchain_resolution_enabled_do_not_use(",
4645+
" ctx = ctx)",
4646+
" return struct(",
4647+
" toolchain_resolution_enabled = toolchain_resolution_enabled)",
4648+
"toolchain_resolution_enabled = rule(",
4649+
" _impl,",
4650+
");");
4651+
4652+
scratch.file(
4653+
"a/BUILD",
4654+
"load(':rule.bzl', 'toolchain_resolution_enabled')",
4655+
"toolchain_resolution_enabled(name='r')");
4656+
4657+
ConfiguredTarget r = getConfiguredTarget("//a:r");
4658+
@SuppressWarnings("unchecked") // Use an extra variable in order to suppress the warning.
4659+
boolean toolchainResolutionEnabled = (boolean) r.get("toolchain_resolution_enabled");
4660+
return toolchainResolutionEnabled;
4661+
}
4662+
4663+
@Test
4664+
public void testIsToolchainResolutionEnabled_disabled() throws Exception {
4665+
useConfiguration("--incompatible_enable_cc_toolchain_resolution=false");
4666+
4667+
assertThat(toolchainResolutionEnabled()).isFalse();
4668+
}
4669+
4670+
@Test
4671+
public void testIsToolchainResolutionEnabled_enabled() throws Exception {
4672+
useConfiguration("--incompatible_enable_cc_toolchain_resolution");
4673+
4674+
assertThat(toolchainResolutionEnabled()).isTrue();
4675+
}
46394676
}

tools/cpp/toolchain_utils.bzl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ def find_cpp_toolchain(ctx):
3333
Returns:
3434
A CcToolchainProvider.
3535
"""
36-
if not hasattr(ctx.attr, "_cc_toolchain"):
37-
fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.")
3836

39-
if Label("@bazel_tools//tools/cpp:toolchain_type") in ctx.fragments.platform.enabled_toolchain_types:
37+
# Check the incompatible flag for toolchain resolution.
38+
if hasattr(cc_common, "is_cc_toolchain_resolution_enabled_do_not_use") and cc_common.is_cc_toolchain_resolution_enabled_do_not_use(ctx = ctx):
4039
return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]
41-
else:
40+
41+
if Label("//tools/cpp:toolchain_type") in ctx.fragments.platform.enabled_toolchain_types:
42+
return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]
43+
44+
# Fall back to the legacy implicit attribute lookup.
45+
if hasattr(ctx.attr, "_cc_toolchain"):
4246
return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]
47+
48+
# We didn't find anything.
49+
fail("In order to use find_cpp_toolchain, you must include the '@bazel_tools//tools/cpp:toolchain_type' in the toolchains argument to your rule.")

0 commit comments

Comments
 (0)