Skip to content

Commit

Permalink
Expose the default no-op transition to Starlark users.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
katre authored and copybara-github committed May 14, 2024
1 parent 231dfc2 commit 283b585
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -47,7 +48,8 @@ public static <T extends TransitionFactory.Data> boolean isInstance(

/** A {@link TransitionFactory} implementation that generates the no transition. */
@AutoValue
abstract static class Factory<T extends TransitionFactory.Data> implements TransitionFactory<T> {
abstract static class Factory<T extends TransitionFactory.Data>
implements TransitionFactory<T>, ConfigurationTransitionApi {
@Override
public ConfigurationTransition create(T unused) {
return INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("<config>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>cfg = \"target\"</code> in <code>attr.label()</code>.")
ConfigurationTransitionApi target();

/** The api for exec transitions. */
@StarlarkBuiltin(
name = "ExecTransitionFactory",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,6 +182,7 @@ private Object newModule(ImmutableMap.Builder<String, Object> 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.)
Expand Down

0 comments on commit 283b585

Please sign in to comment.