Skip to content

Commit

Permalink
Expose config.no_config to Starlark.
Browse files Browse the repository at this point in the history
This allows Starlark rules to create data-only non-configured dependencies, to reduce fan-out across configurations.

RELNOTES: A no-config transition is now available as `config.no_config()`.
PiperOrigin-RevId: 689926488
Change-Id: I37625596642a825c745276db29309ec11a2ac998
  • Loading branch information
katre authored and copybara-github committed Oct 25, 2024
1 parent 3f70ca1 commit ea477ac
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
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 @@ -2221,6 +2221,7 @@ java_library(
":config/transitions/transition_factory",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config:configuration_transition_api",
"//third_party:auto_value",
"//third_party:guava",
],
Expand Down Expand Up @@ -2642,6 +2643,7 @@ java_library(
srcs = ["starlark/StarlarkConfig.java"],
deps = [
":config/execution_transition_factory",
":config/transitions/no_config_transition",
":config/transitions/no_transition",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/config",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.devtools.build.lib.analysis.config.FragmentOptions;
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;

/**
* Transitions to a stable, empty configuration for rules that don't rely on configuration.
Expand All @@ -46,6 +47,15 @@ public class NoConfigTransition implements PatchTransition {
private static final TransitionFactory<? extends TransitionFactory.Data> FACTORY_INSTANCE =
new AutoValue_NoConfigTransition_Factory<>();

/**
* Returns {@code true} if the given {@link TransitionFactory} is an instance of the no
* transition.
*/
public static <T extends TransitionFactory.Data> boolean isInstance(
TransitionFactory<T> instance) {
return instance instanceof Factory;
}

private NoConfigTransition() {}

@Override
Expand All @@ -67,7 +77,8 @@ public static <T extends TransitionFactory.Data> TransitionFactory<T> getFactory

/** A {@link TransitionFactory} implementation that generates the 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 PatchTransition create(T unused) {
return INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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.NoConfigTransition;
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;
Expand Down Expand Up @@ -66,6 +67,11 @@ public ConfigurationTransitionApi target() {
return (ConfigurationTransitionApi) NoTransition.getFactory();
}

@Override
public ConfigurationTransitionApi none() {
return (ConfigurationTransitionApi) NoConfigTransition.getFactory();
}

@Override
public void repr(Printer printer) {
printer.append("<config>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ interface BuildSettingApi extends StarlarkValue {}
+ " Equivalent to <code>cfg = \"target\"</code> in <code>attr.label()</code>.")
ConfigurationTransitionApi target();

@StarlarkMethod(
name = "none",
doc =
"Creates a no_config transition. This is a transition that unsets all flags, intended for"
+ " the case where a dependency is data-only and contains no code that needs to be"
+ " built, but should only be analyzed once.")
ConfigurationTransitionApi none();

/** The api for exec transitions. */
@StarlarkBuiltin(
name = "ExecTransitionFactory",
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/starlark/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_configuration",
"//src/main/java/com/google/devtools/build/lib/analysis:config/toolchain_type_requirement",
"//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/no_config_transition",
"//src/main/java/com/google/devtools/build/lib/analysis:config/transitions/no_transition",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/analysis:starlark/starlark_config",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.analysis.config.ToolchainTypeRequirement;
import com.google.devtools.build.lib.analysis.config.transitions.NoConfigTransition;
import com.google.devtools.build.lib.analysis.config.transitions.NoTransition;
import com.google.devtools.build.lib.analysis.starlark.StarlarkAttrModule;
import com.google.devtools.build.lib.analysis.starlark.StarlarkConfig;
Expand Down Expand Up @@ -1377,6 +1378,12 @@ public void testAttrCfgTarget_object() throws Exception {
assertThat(NoTransition.isInstance(attr.getTransitionFactory())).isTrue();
}

@Test
public void testAttrCfgNone() throws Exception {
Attribute attr = buildAttribute("a1", "attr.label(cfg = config.none(), allow_files = True)");
assertThat(NoConfigTransition.isInstance(attr.getTransitionFactory())).isTrue();
}

private void writeRuleCfgTestRule(String cfg) throws Exception {
scratch.file(
"rule_testing/rule.bzl",
Expand Down

0 comments on commit ea477ac

Please sign in to comment.