Skip to content

Commit

Permalink
Expose cc_common.get_execution_requirements
Browse files Browse the repository at this point in the history
This is used to control per-action execution requirements. Currently only used by objc and swift rules.

RELNOTES: None.
PiperOrigin-RevId: 250881803
  • Loading branch information
hlopko authored and irengrig committed Jun 18, 2019
1 parent 674c1a1 commit 384176d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ public String getToolForAction(
return featureConfiguration.getFeatureConfiguration().getToolPathForAction(actionName);
}

@Override
public SkylarkList<String> getExecutionRequirements(
FeatureConfigurationForStarlark featureConfiguration, String actionName) {
return SkylarkList.createImmutable(
featureConfiguration.getFeatureConfiguration().getToolRequirementsForAction(actionName));
}

@Override
public boolean isEnabled(
FeatureConfigurationForStarlark featureConfiguration, String featureName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,37 @@ FeatureConfigurationT configureFeatures(
name = "action_name",
doc =
"Name of the action. Has to be one of the names in "
+ "@bazel_tools//tools/build_defs/cc:action_names.bzl.",
+ "@bazel_tools//tools/build_defs/cc:action_names.bzl "
+ "(https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/"
+ "action_names.bzl)",
named = true,
positional = false),
})
String getToolForAction(FeatureConfigurationT featureConfiguration, String actionName);

@SkylarkCallable(
name = "get_execution_requirements",
doc = "Returns execution requirements for given action.",
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. Has to be one of the names in "
+ "@bazel_tools//tools/build_defs/cc:action_names.bzl "
+ "(https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/"
+ "action_names.bzl)",
named = true,
positional = false),
})
SkylarkList<String> getExecutionRequirements(
FeatureConfigurationT featureConfiguration, String actionName);

@SkylarkCallable(
name = "is_enabled",
doc = "Returns True if given feature is enabled in the feature configuration.",
Expand Down Expand Up @@ -181,7 +206,9 @@ FeatureConfigurationT configureFeatures(
name = "action_name",
doc =
"Name of the action. Has to be one of the names in "
+ "@bazel_tools//tools/build_defs/cc:action_names.bzl.",
+ "@bazel_tools//tools/build_defs/cc:action_names.bzl "
+ "(https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/"
+ "action_names.bzl)",
named = true,
positional = false),
@Param(
Expand Down Expand Up @@ -211,7 +238,9 @@ SkylarkList<String> getCommandLine(
name = "action_name",
doc =
"Name of the action. Has to be one of the names in "
+ "@bazel_tools//tools/build_defs/cc:action_names.bzl.",
+ "@bazel_tools//tools/build_defs/cc:action_names.bzl "
+ "(https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/"
+ "action_names.bzl)",
named = true,
positional = false),
@Param(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/skylarkbuildapi",
"//src/main/java/com/google/devtools/build/lib/skylarkbuildapi/cpp",
"//src/main/java/com/google/devtools/build/skydoc/fakebuildapi",
"//third_party:guava",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.devtools.build.skydoc.fakebuildapi.cpp;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkbuildapi.FileApi;
import com.google.devtools.build.lib.skylarkbuildapi.ProviderApi;
Expand Down Expand Up @@ -75,6 +76,12 @@ public String getToolForAction(FeatureConfigurationApi featureConfiguration, Str
return "";
}

@Override
public SkylarkList<String> getExecutionRequirements(
FeatureConfigurationApi featureConfiguration, String actionName) {
return SkylarkList.createImmutable(ImmutableList.of());
}

@Override
public boolean isEnabled(FeatureConfigurationApi featureConfiguration, String featureName) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ _FEATURE_NAMES = struct(
strip_debug_symbols = "strip_debug_symbols",
disable_pbh = "disable_pbh",
optional_cc_flags_feature = "optional_cc_flags_feature",
cpp_compile_with_requirements = "cpp_compile_with_requirements",
)

_disable_pbh_feature = feature(name = _FEATURE_NAMES.disable_pbh)
Expand Down Expand Up @@ -244,6 +245,8 @@ _header_module_compile_feature = feature(
],
)

_cpp_compile_with_requirements = feature(name = _FEATURE_NAMES.cpp_compile_with_requirements)

_header_module_codegen_feature = feature(
name = _FEATURE_NAMES.header_module_codegen,
implies = ["header_modules"],
Expand Down Expand Up @@ -972,6 +975,16 @@ _multiple_tools_action_config = action_config(
],
)

_cpp_compile_with_requirements_action_config = action_config(
action_name = "yolo_action_with_requirements",
tools = [
tool(
path = "yolo_tool",
execution_requirements = ["requires-yolo"],
),
],
)

_foo_feature = feature(
name = _FEATURE_NAMES.foo,
)
Expand Down Expand Up @@ -1185,6 +1198,7 @@ _feature_name_to_feature = {
_FEATURE_NAMES.strip_debug_symbols: _strip_debug_symbols_feature,
_FEATURE_NAMES.disable_pbh: _disable_pbh_feature,
_FEATURE_NAMES.optional_cc_flags_feature: _optional_cc_flags_feature,
_FEATURE_NAMES.cpp_compile_with_requirements: _cpp_compile_with_requirements,
"header_modules_feature_configuration": _header_modules_feature_configuration,
"env_var_feature_configuration": _env_var_feature_configuration,
"host_and_nonhost_configuration": _host_and_nonhost_configuration,
Expand Down Expand Up @@ -1315,10 +1329,13 @@ def _impl(ctx):
features = [default_compile_flags_feature, default_link_flags_feature]

should_add_multiple_tools_action_config = False
should_add_requirements = False

for name in ctx.attr.feature_names:
if name == _FEATURE_NAMES.change_tool:
should_add_multiple_tools_action_config = True
if name == _FEATURE_NAMES.cpp_compile_with_requirements:
should_add_requirements = True

features.extend(_get_features_for_configuration(name))

Expand All @@ -1345,6 +1362,9 @@ def _impl(ctx):
if should_add_multiple_tools_action_config:
action_configs.append(_multiple_tools_action_config)

if should_add_requirements:
action_configs.append(_cpp_compile_with_requirements_action_config)

make_variables = [
make_variable(name = name, value = value)
for name, value in ctx.attr.make_variables.items()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public boolean apply(Artifact artifact) {
"major_version: 'foo'\nminor_version:' foo'\n" + emptyToolchainForCpu("k8");

public static final String SIMPLE_COMPILE_FEATURE = "simple_compile_feature";
public static final String CPP_COMPILE_ACTION_WITH_REQUIREMENTS = "cpp_compile_with_requirements";

public static String emptyToolchainForCpu(String cpu, String... append) {
return Joiner.on("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.devtools.build.lib.packages.SkylarkProvider;
import com.google.devtools.build.lib.packages.StructImpl;
import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig;
import com.google.devtools.build.lib.packages.util.MockCcSupport;
import com.google.devtools.build.lib.packages.util.ResourceLoader;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.ActionConfig;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.ArtifactNamePattern;
Expand Down Expand Up @@ -233,6 +234,48 @@ public void testGetToolForAction() throws Exception {
.isEqualTo(featureConfiguration.getToolPathForAction(CppActionNames.CPP_COMPILE));
}

@Test
public void testExecutionRequirements() throws Exception {
AnalysisMock.get()
.ccSupport()
.setupCcToolchainConfig(
mockToolsConfig,
CcToolchainConfig.builder()
.withFeatures(MockCcSupport.CPP_COMPILE_ACTION_WITH_REQUIREMENTS));
scratch.file(
"a/BUILD",
"load(':rule.bzl', 'crule')",
"cc_toolchain_alias(name='alias')",
"crule(name='r')");

scratch.file(
"a/rule.bzl",
"load('//myinfo:myinfo.bzl', 'MyInfo')",
"def _impl(ctx):",
" toolchain = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo]",
" feature_configuration = cc_common.configure_features(",
" ctx = ctx,",
" cc_toolchain = toolchain,",
" )",
" return [MyInfo(",
" requirements = cc_common.get_execution_requirements(",
" feature_configuration = feature_configuration,",
" action_name = 'yolo_action_with_requirements'))]",
"crule = rule(",
" _impl,",
" attrs = { ",
" '_cc_toolchain': attr.label(default=Label('//a:alias'))",
" },",
" fragments = ['cpp'],",
");");

ConfiguredTarget r = getConfiguredTarget("//a:r");
@SuppressWarnings("unchecked")
SkylarkList<String> requirements =
(SkylarkList<String>) getMyInfoFromTarget(r).getValue("requirements");
assertThat(requirements).containsExactly("requires-yolo");
}

@Test
public void testFeatureConfigurationWithAdditionalEnabledFeature() throws Exception {
AnalysisMock.get()
Expand Down

0 comments on commit 384176d

Please sign in to comment.