Skip to content

Commit

Permalink
Expose the --incompatible_enable_cc_toolchain_resolution flag to Star…
Browse files Browse the repository at this point in the history
…lark and

update find_cpp_toolchain to use it.

Part of bazelbuild#7260.

PiperOrigin-RevId: 231400764
  • Loading branch information
katre authored and weixiao-huang committed Jan 31, 2019
1 parent c8ae1bb commit 38b659a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public class CcModule
CcCompilationContext,
CcLinkingContext,
LibraryToLinkWrapper,
CcToolchainVariables> {
CcToolchainVariables,
SkylarkRuleContext> {

private enum RegisterActions {
ALWAYS,
Expand Down Expand Up @@ -1508,4 +1509,9 @@ private static void getLegacyArtifactNamePatterns(
private static <T> T nullIfNone(Object object, Class<T> type) {
return object != Runtime.NONE ? type.cast(object) : null;
}

@Override
public boolean isCcToolchainResolutionEnabled(SkylarkRuleContext skylarkRuleContext) {
return CppHelper.useToolchainResolution(skylarkRuleContext.getRuleContext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public interface BazelCcModuleApi<
CcCompilationContextT,
LinkingContextT,
LibraryToLinkWrapperT,
CcToolchainVariablesT> {
CcToolchainVariablesT,
SkylarkRuleContextT> {

@SkylarkCallable(
name = "compile",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.devtools.build.lib.skylarkbuildapi.FileApi;
import com.google.devtools.build.lib.skylarkbuildapi.ProviderApi;
import com.google.devtools.build.lib.skylarkbuildapi.SkylarkActionFactoryApi;
import com.google.devtools.build.lib.skylarkbuildapi.SkylarkRuleContextApi;
import com.google.devtools.build.lib.skylarkinterface.Param;
import com.google.devtools.build.lib.skylarkinterface.ParamType;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
Expand All @@ -39,7 +40,8 @@ public interface CcModuleApi<
CompilationContextT extends CcCompilationContextApi,
LinkingContextT extends CcLinkingContextApi,
LibraryToLinkWrapperT extends LibraryToLinkWrapperApi,
CcToolchainVariablesT extends CcToolchainVariablesApi> {
CcToolchainVariablesT extends CcToolchainVariablesApi,
SkylarkRuleContextT extends SkylarkRuleContextApi> {

@SkylarkCallable(
name = "CcToolchainInfo",
Expand Down Expand Up @@ -669,4 +671,18 @@ CompilationContextT createCcCompilationContext(
type = CcToolchainProviderApi.class)
})
String legacyCcFlagsMakeVariable(CcToolchainProviderT ccToolchain);

@SkylarkCallable(
name = "is_cc_toolchain_resolution_enabled_do_not_use",
documented = false,
parameters = {
@Param(
name = "ctx",
positional = false,
named = true,
type = SkylarkRuleContextApi.class,
doc = "The rule context."),
},
doc = "Returns true if the --incompatible_enable_cc_toolchain_resolution flag is enabled.")
boolean isCcToolchainResolutionEnabled(SkylarkRuleContextT ruleContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public String legacyCcFlagsMakeVariable(CcToolchainProviderApi ccToolchain) {
return "";
}

@Override
public boolean isCcToolchainResolutionEnabled(SkylarkRuleContextApi ruleContext) {
return false;
}

@Override
public CompilationInfoApi compile(
SkylarkRuleContextApi skylarkRuleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4636,4 +4636,41 @@ public void testGetLegacyCcFlagsMakeVariable() throws Exception {

assertThat(ccFlags).isEqualTo("-test-cflag1 -testcflag2");
}

private boolean toolchainResolutionEnabled() throws Exception {
scratch.file(
"a/rule.bzl",
"def _impl(ctx):",
" toolchain_resolution_enabled = cc_common.is_cc_toolchain_resolution_enabled_do_not_use(",
" ctx = ctx)",
" return struct(",
" toolchain_resolution_enabled = toolchain_resolution_enabled)",
"toolchain_resolution_enabled = rule(",
" _impl,",
");");

scratch.file(
"a/BUILD",
"load(':rule.bzl', 'toolchain_resolution_enabled')",
"toolchain_resolution_enabled(name='r')");

ConfiguredTarget r = getConfiguredTarget("//a:r");
@SuppressWarnings("unchecked") // Use an extra variable in order to suppress the warning.
boolean toolchainResolutionEnabled = (boolean) r.get("toolchain_resolution_enabled");
return toolchainResolutionEnabled;
}

@Test
public void testIsToolchainResolutionEnabled_disabled() throws Exception {
useConfiguration("--incompatible_enable_cc_toolchain_resolution=false");

assertThat(toolchainResolutionEnabled()).isFalse();
}

@Test
public void testIsToolchainResolutionEnabled_enabled() throws Exception {
useConfiguration("--incompatible_enable_cc_toolchain_resolution");

assertThat(toolchainResolutionEnabled()).isTrue();
}
}
15 changes: 11 additions & 4 deletions tools/cpp/toolchain_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ def find_cpp_toolchain(ctx):
Returns:
A CcToolchainProvider.
"""
if not hasattr(ctx.attr, "_cc_toolchain"):
fail("In order to use find_cpp_toolchain, you must define the '_cc_toolchain' attribute on your rule or aspect.")

if Label("@bazel_tools//tools/cpp:toolchain_type") in ctx.fragments.platform.enabled_toolchain_types:
# Check the incompatible flag for toolchain resolution.
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):
return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]
else:

if Label("//tools/cpp:toolchain_type") in ctx.fragments.platform.enabled_toolchain_types:
return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"]

# Fall back to the legacy implicit attribute lookup.
if hasattr(ctx.attr, "_cc_toolchain"):
return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]

# We didn't find anything.
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 comments on commit 38b659a

Please sign in to comment.