From d97392fb03a1deacbe655883564530ab856073c1 Mon Sep 17 00:00:00 2001 From: jcater Date: Thu, 6 Jun 2019 09:35:45 -0700 Subject: [PATCH] Add an exec_tools attribute for genrule, which allows specifying tools that should be configured with the execution transition, instead of the host transition. Note that this attribute is for migration from host to execution configurations, and will eventually be deprecated and removed once that migration is complete. PiperOrigin-RevId: 251867407 --- .../build/lib/rules/genrule/GenRuleBase.java | 1 + .../lib/rules/genrule/GenRuleBaseRule.java | 24 +++++++++++++++++++ .../genrule/GenRuleConfiguredTargetTest.java | 20 ++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java index 0a909df7da9aa7..37f048c46f853d 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java +++ b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java @@ -217,6 +217,7 @@ public String toString() { protected CommandHelper.Builder commandHelperBuilder(RuleContext ruleContext) { return CommandHelper.builder(ruleContext) .addHostToolDependencies("tools") + .addToolDependencies("exec_tools") .addHostToolDependencies("toolchains"); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBaseRule.java b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBaseRule.java index 6d2af27520d2a2..5d5442acd128ab 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBaseRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBaseRule.java @@ -23,6 +23,7 @@ import com.google.devtools.build.lib.analysis.BaseRuleClasses; import com.google.devtools.build.lib.analysis.RuleDefinition; import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; +import com.google.devtools.build.lib.analysis.config.ExecutionTransitionFactory; import com.google.devtools.build.lib.analysis.config.HostTransition; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.AttributeMap; @@ -84,6 +85,29 @@ public RuleClass build( attr("tools", LABEL_LIST) .cfg(HostTransition.createFactory()) .allowedFileTypes(FileTypeSet.ANY_FILE)) + + /* + A list of tool dependencies for this rule. This behaves exactly like the + tools attribute, except that these dependencies + will be configured for the rule's execution platform instead of the host configuration. + This means that dependencies in exec_tools are not subject to the same + limitations as dependencies in tools. In particular, they are not required to + use the host configuration for their own transitive dependencies. See + tools for further details. + +

+ Note that eventually the host configuration will be replaced by the execution + configuration. When that happens, this attribute will be deprecated in favor of + tools. Until then, this attribute allows users to selectively migrate + dependencies to the execution configuration. +

+ */ + .add( + attr("exec_tools", LABEL_LIST) + .cfg(new ExecutionTransitionFactory()) + .allowedFileTypes(FileTypeSet.ANY_FILE) + .dontCheckConstraints()) + /* A list of files generated by this rule.

diff --git a/src/test/java/com/google/devtools/build/lib/bazel/rules/genrule/GenRuleConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/bazel/rules/genrule/GenRuleConfiguredTargetTest.java index f3ca1f44641faa..60ce456121f57c 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/rules/genrule/GenRuleConfiguredTargetTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/rules/genrule/GenRuleConfiguredTargetTest.java @@ -614,4 +614,24 @@ public void testDuplicateLocalFlags() throws Exception { getConfiguredTarget("//foo:g"); assertNoEvents(); } + + @Test + public void testExecToolsAreExecConfiguration() throws Exception { + scratch.file( + "config/BUILD", + "genrule(name='src', outs=['src.out'], cmd=':')", + "genrule(name='exec_tool', outs=['exec_tool.out'], cmd=':')", + "genrule(name='config', ", + " srcs=[':src'], exec_tools=[':exec_tool'], outs=['out'],", + " cmd='$(location :exec_tool)')"); + + ConfiguredTarget parentTarget = getConfiguredTarget("//config"); + + // Cannot use getDirectPrerequisites, as this re-configures that target incorrectly. + Artifact out = Iterables.getFirst(getFilesToBuild(parentTarget), null); + assertThat(getGeneratingAction(out).getTools()).hasSize(1); + Artifact execTool = getOnlyElement(getGeneratingAction(out).getTools()); + // This is the output dir fragment for the execution transition. + assertThat(execTool.getExecPathString()).contains("-exec-"); + } }