From 3af93c9c4cc820a6848f3d774a82eebf69f04e16 Mon Sep 17 00:00:00 2001 From: hlopko Date: Mon, 7 Jan 2019 12:43:09 -0800 Subject: [PATCH] Add cc_common.action_is_enabled This will help users who want to use CROSSTOOL with other than C++ rules, plus it will enable users to make rules forward compatible for incompatible CROSSTOOL changes. Progress towards https://github.com/bazelbuild/bazel/issues/5883 RELNOTES: None. PiperOrigin-RevId: 228216650 --- .../build/lib/rules/cpp/CcModule.java | 5 +++ .../lib/skylarkbuildapi/cpp/CcModuleApi.java | 18 +++++++++ .../skydoc/fakebuildapi/cpp/FakeCcModule.java | 5 +++ .../lib/rules/cpp/SkylarkCcCommonTest.java | 37 +++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java index 49415ee60890f4..2d0e4cf5852dc1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java @@ -146,6 +146,11 @@ public boolean isEnabled(FeatureConfiguration featureConfiguration, String featu return featureConfiguration.isEnabled(featureName); } + @Override + public boolean actionIsEnabled(FeatureConfiguration featureConfiguration, String actionName) { + return featureConfiguration.actionIsConfigured(actionName); + } + @Override public SkylarkList getCommandLine( FeatureConfiguration featureConfiguration, diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcModuleApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcModuleApi.java index 6ecc176cf0bca5..a2b0030991d486 100644 --- a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcModuleApi.java +++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp/CcModuleApi.java @@ -125,6 +125,24 @@ FeatureConfigurationT configureFeatures( }) boolean isEnabled(FeatureConfigurationT featureConfiguration, String featureName); + @SkylarkCallable( + name = "action_is_enabled", + doc = "Returns True if given action_config is enabled in the feature configuration.", + parameters = { + @Param( + name = "feature_configuration", + doc = "Feature configuration to be queried.", + positional = false, + named = true, + type = FeatureConfigurationApi.class), + @Param( + name = "action_name", + doc = "Name of the action_config.", + named = true, + positional = false), + }) + boolean actionIsEnabled(FeatureConfigurationT featureConfiguration, String actionName); + @SkylarkCallable( name = "get_memory_inefficient_command_line", doc = diff --git a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java index ce29fc3c16e3b0..5397655456cd1d 100644 --- a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java +++ b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java @@ -69,6 +69,11 @@ public boolean isEnabled(FeatureConfigurationApi featureConfiguration, String fe return false; } + @Override + public boolean actionIsEnabled(FeatureConfigurationApi featureConfiguration, String actionName) { + return false; + } + @Override public SkylarkList getCommandLine(FeatureConfigurationApi featureConfiguration, String actionName, CcToolchainVariablesApi variables) { diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java index ec14f672e5f2e1..10e43e3cbafe3e 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/SkylarkCcCommonTest.java @@ -271,6 +271,43 @@ public void testGetEnvironment() throws Exception { CppActionNames.CPP_COMPILE, CcToolchainVariables.EMPTY)); } + @Test + public void testActionIsEnabled() throws Exception { + scratch.file( + "a/BUILD", + "load(':rule.bzl', 'crule')", + "cc_toolchain_alias(name='alias')", + "crule(name='r')"); + + scratch.file( + "a/rule.bzl", + "def _impl(ctx):", + " toolchain = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]", + " feature_configuration = cc_common.configure_features(cc_toolchain = toolchain)", + " return struct(", + " enabled_action = cc_common.action_is_enabled(", + " feature_configuration = feature_configuration,", + " action_name = 'c-compile'),", + " disabled_action = cc_common.action_is_enabled(", + " feature_configuration = feature_configuration,", + " action_name = 'wololoo'))", + "crule = rule(", + " _impl,", + " attrs = { ", + " '_cc_toolchain': attr.label(default=Label('//a:alias'))", + " },", + " fragments = ['cpp'],", + ");"); + + ConfiguredTarget r = getConfiguredTarget("//a:r"); + @SuppressWarnings("unchecked") + boolean enabledActionIsEnabled = (boolean) r.get("enabled_action"); + @SuppressWarnings("unchecked") + boolean disabledActionIsDisabled = (boolean) r.get("disabled_action"); + assertThat(enabledActionIsEnabled).isTrue(); + assertThat(disabledActionIsDisabled).isFalse(); + } + @Test public void testIsEnabled() throws Exception { scratch.file(