From 283b585cca321d1544f60caf281532051604b99a Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 14 May 2024 12:09:18 -0700 Subject: [PATCH] Expose the default no-op transition to Starlark users. This is the same transition as `cfg = "target"`. RELNOTES: A no-op transition is now available as `config.target()`. Part of #22248. PiperOrigin-RevId: 633665060 Change-Id: I63f81d1378b6af7ed4fb4b81ea1df3db9de526ad --- .../java/com/google/devtools/build/lib/analysis/BUILD | 2 ++ .../lib/analysis/config/transitions/NoTransition.java | 4 +++- .../build/lib/analysis/starlark/StarlarkConfig.java | 7 +++++++ .../lib/starlarkbuildapi/config/StarlarkConfigApi.java | 8 ++++++++ .../build/skydoc/fakebuildapi/config/FakeConfigApi.java | 6 ++++++ .../lib/starlark/StarlarkRuleClassFunctionsTest.java | 8 +++++++- .../com/google/devtools/build/lib/starlark/util/BUILD | 1 + .../build/lib/starlark/util/BazelEvaluationTestCase.java | 2 ++ 8 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/BUILD b/src/main/java/com/google/devtools/build/lib/analysis/BUILD index 5e4382bb74e09b..a7c21a6357cd5e 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/BUILD +++ b/src/main/java/com/google/devtools/build/lib/analysis/BUILD @@ -2167,6 +2167,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/events", "//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec", "//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant", + "//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config", "//third_party:auto_value", ], ) @@ -2599,6 +2600,7 @@ java_library( srcs = ["starlark/StarlarkConfig.java"], deps = [ ":config/execution_transition_factory", + ":config/transitions/no_transition", "//src/main/java/com/google/devtools/build/lib/packages", "//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config", "//src/main/java/net/starlark/java/eval", diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/NoTransition.java b/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/NoTransition.java index baa6e175a00256..704cfcb3f582c2 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/NoTransition.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/NoTransition.java @@ -18,6 +18,7 @@ import com.google.devtools.build.lib.analysis.config.BuildOptionsView; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant; +import com.google.devtools.build.lib.starlarkbuildapi.config.ConfigurationTransitionApi; /** No-op configuration transition. */ public final class NoTransition implements PatchTransition { @@ -47,7 +48,8 @@ public static boolean isInstance( /** A {@link TransitionFactory} implementation that generates the no transition. */ @AutoValue - abstract static class Factory implements TransitionFactory { + abstract static class Factory + implements TransitionFactory, ConfigurationTransitionApi { @Override public ConfigurationTransition create(T unused) { return INSTANCE; diff --git a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkConfig.java b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkConfig.java index e2fc73ec671f99..91ab18819bfe56 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkConfig.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkConfig.java @@ -20,7 +20,9 @@ import static com.google.devtools.build.lib.packages.Types.STRING_LIST; import com.google.devtools.build.lib.analysis.config.ExecutionTransitionFactory; +import com.google.devtools.build.lib.analysis.config.transitions.NoTransition; import com.google.devtools.build.lib.packages.BuildSetting; +import com.google.devtools.build.lib.starlarkbuildapi.config.ConfigurationTransitionApi; import com.google.devtools.build.lib.starlarkbuildapi.config.StarlarkConfigApi; import net.starlark.java.eval.EvalException; import net.starlark.java.eval.Printer; @@ -59,6 +61,11 @@ public ExecutionTransitionFactory exec(Object execGroupUnchecked) { : ExecutionTransitionFactory.createFactory((String) execGroupUnchecked); } + @Override + public ConfigurationTransitionApi target() { + return (ConfigurationTransitionApi) NoTransition.createFactory(); + } + @Override public void repr(Printer printer) { printer.append(""); diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config/StarlarkConfigApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config/StarlarkConfigApi.java index d15cba4200e56e..84e35538a3ed29 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config/StarlarkConfigApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config/StarlarkConfigApi.java @@ -158,6 +158,14 @@ interface BuildSettingApi extends StarlarkValue {} }) ExecTransitionFactoryApi exec(Object execGroupUnchecked); + @StarlarkMethod( + name = "target", + doc = + "Creates a target transition. This is a no-op transition intended for the case where a" + + " transition object is needed, but doesn't want to actually change anything." + + " Equivalent to cfg = \"target\" in attr.label().") + ConfigurationTransitionApi target(); + /** The api for exec transitions. */ @StarlarkBuiltin( name = "ExecTransitionFactory", diff --git a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/config/FakeConfigApi.java b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/config/FakeConfigApi.java index 5d768d153ed3aa..e02fc6d8babe3b 100644 --- a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/config/FakeConfigApi.java +++ b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/config/FakeConfigApi.java @@ -14,6 +14,7 @@ package com.google.devtools.build.skydoc.fakebuildapi.config; +import com.google.devtools.build.lib.starlarkbuildapi.config.ConfigurationTransitionApi; import com.google.devtools.build.lib.starlarkbuildapi.config.StarlarkConfigApi; import com.google.devtools.build.skydoc.fakebuildapi.config.ConfigApiFakes.FakeBuildSettingDescriptor; import com.google.devtools.build.skydoc.fakebuildapi.config.ConfigApiFakes.FakeExecTransitionFactory; @@ -47,6 +48,11 @@ public ExecTransitionFactoryApi exec(Object execGroup) { return new FakeExecTransitionFactory(); } + @Override + public ConfigurationTransitionApi target() { + return new FakeConfigurationTransition(); + } + @Override public void repr(Printer printer) {} } diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java index 66e7a56b892660..11f6ddb4d0828a 100644 --- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java +++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java @@ -1381,11 +1381,17 @@ public void testAttrCfgHostDisabled() throws Exception { } @Test - public void testAttrCfgTarget() throws Exception { + public void testAttrCfgTarget_string() throws Exception { Attribute attr = buildAttribute("a1", "attr.label(cfg = 'target', allow_files = True)"); assertThat(NoTransition.isInstance(attr.getTransitionFactory())).isTrue(); } + @Test + public void testAttrCfgTarget_object() throws Exception { + Attribute attr = buildAttribute("a1", "attr.label(cfg = config.target(), allow_files = True)"); + assertThat(NoTransition.isInstance(attr.getTransitionFactory())).isTrue(); + } + private void writeRuleCfgTestRule(String cfg) throws Exception { scratch.file( "rule_testing/rule.bzl", diff --git a/src/test/java/com/google/devtools/build/lib/starlark/util/BUILD b/src/test/java/com/google/devtools/build/lib/starlark/util/BUILD index d9a3e5ff2f219c..97fc9bef7c9e7c 100644 --- a/src/test/java/com/google/devtools/build/lib/starlark/util/BUILD +++ b/src/test/java/com/google/devtools/build/lib/starlark/util/BUILD @@ -21,6 +21,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib:runtime", "//src/main/java/com/google/devtools/build/lib:starlark_options_parser", "//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster", + "//src/main/java/com/google/devtools/build/lib/analysis:starlark/starlark_config", "//src/main/java/com/google/devtools/build/lib/cmdline", "//src/main/java/com/google/devtools/build/lib/events", "//src/main/java/com/google/devtools/build/lib/packages", diff --git a/src/test/java/com/google/devtools/build/lib/starlark/util/BazelEvaluationTestCase.java b/src/test/java/com/google/devtools/build/lib/starlark/util/BazelEvaluationTestCase.java index 721dc928a77284..bfe573a175eab2 100644 --- a/src/test/java/com/google/devtools/build/lib/starlark/util/BazelEvaluationTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/starlark/util/BazelEvaluationTestCase.java @@ -19,6 +19,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.analysis.starlark.StarlarkConfig; import com.google.devtools.build.lib.analysis.starlark.StarlarkGlobalsImpl; import com.google.devtools.build.lib.cmdline.BazelModuleContext; import com.google.devtools.build.lib.cmdline.Label; @@ -181,6 +182,7 @@ private Object newModule(ImmutableMap.Builder predeclared) { predeclared.putAll(StarlarkGlobalsImpl.INSTANCE.getFixedBzlToplevels()); predeclared.put("platform_common", new PlatformCommon()); predeclared.put("config_common", new ConfigStarlarkCommon()); + predeclared.put("config", new StarlarkConfig()); Starlark.addMethods(predeclared, new ConfigGlobalLibrary()); // Return the module's client data. (This one uses dummy values for tests.)