Skip to content

Commit

Permalink
Add register_{execution_platforms,toolchains} directives to MODULE.ba…
Browse files Browse the repository at this point in the history
…zel files (bazelbuild#15852)

These are intended to replace the {execution_platforms,toolchains}_to_register attribtues on the module() directive, which are a bit harder to use since they have to be grouped up together near the top.

bazelbuild#15829

RELNOTES: Added new register_{execution_platforms,toolchains} directives to the MODULE.bazel file, to replace the {execution_platforms,toolchains}_to_register attributes on the module() directive.
PiperOrigin-RevId: 459714400
Change-Id: If8a25ef92ac695a3f012d54d0e189d8ae25d0833

Co-authored-by: Googler <wyv@google.com>
Co-authored-by: Chenchu K <ckolli@google.com>
  • Loading branch information
3 people committed Jul 18, 2022
1 parent 7bbdddd commit 0f05904
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.Maps;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Map;
import java.util.function.UnaryOperator;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -138,9 +139,7 @@ public static Builder builder() {
.setName("")
.setVersion(Version.EMPTY)
.setKey(ModuleKey.ROOT)
.setCompatibilityLevel(0)
.setExecutionPlatformsToRegister(ImmutableList.of())
.setToolchainsToRegister(ImmutableList.of());
.setCompatibilityLevel(0);
}

/**
Expand Down Expand Up @@ -168,11 +167,21 @@ public abstract static class Builder {
/** Optional; defaults to {@code 0}. */
public abstract Builder setCompatibilityLevel(int value);

/** Optional; defaults to an empty list. */
public abstract Builder setExecutionPlatformsToRegister(ImmutableList<String> value);
abstract ImmutableList.Builder<String> executionPlatformsToRegisterBuilder();

/** Optional; defaults to an empty list. */
public abstract Builder setToolchainsToRegister(ImmutableList<String> value);
@CanIgnoreReturnValue
public final Builder addExecutionPlatformsToRegister(Iterable<String> values) {
executionPlatformsToRegisterBuilder().addAll(values);
return this;
}

abstract ImmutableList.Builder<String> toolchainsToRegisterBuilder();

@CanIgnoreReturnValue
public final Builder addToolchainsToRegister(Iterable<String> values) {
toolchainsToRegisterBuilder().addAll(values);
return this;
}

public abstract Builder setDeps(ImmutableMap<String, ModuleKey> value);

Expand Down
Expand Up @@ -167,14 +167,16 @@ public void module(
} catch (ParseException e) {
throw new EvalException("Invalid version in module()", e);
}
// TODO(wyv): migrate users of execution_platforms_to_register and toolchains_to_register to
// register_execution_platforms and register_toolchains, and remove the former two attributes.
module
.setName(name)
.setVersion(parsedVersion)
.setCompatibilityLevel(compatibilityLevel.toInt("compatibility_level"))
.setExecutionPlatformsToRegister(
.addExecutionPlatformsToRegister(
checkAllAbsolutePatterns(
executionPlatformsToRegister, "execution_platforms_to_register"))
.setToolchainsToRegister(
.addToolchainsToRegister(
checkAllAbsolutePatterns(toolchainsToRegister, "toolchains_to_register"));
addRepoNameUsage(name, "as the current module name", thread.getCallerLocation());
}
Expand Down Expand Up @@ -246,6 +248,40 @@ public void bazelDep(
addRepoNameUsage(repoName, "by a bazel_dep", thread.getCallerLocation());
}

@StarlarkMethod(
name = "register_execution_platforms",
doc =
"Specifies already-defined execution platforms to be registered when this module is"
+ " selected. Should be absolute target patterns (ie. beginning with either"
+ " <code>@</code> or <code>//</code>). See <a href=\"${link toolchains}\">toolchain"
+ " resolution</a> for more information.",
extraPositionals =
@Param(
name = "platform_labels",
allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)},
doc = "The labels of the platforms to register."))
public void registerExecutionPlatforms(Sequence<?> platformLabels) throws EvalException {
module.addExecutionPlatformsToRegister(
checkAllAbsolutePatterns(platformLabels, "register_execution_platforms"));
}

@StarlarkMethod(
name = "register_toolchains",
doc =
"Specifies already-defined toolchains to be registered when this module is selected."
+ " Should be absolute target patterns (ie. beginning with either <code>@</code> or"
+ " <code>//</code>). See <a href=\"${link toolchains}\">toolchain resolution</a> for"
+ " more information.",
extraPositionals =
@Param(
name = "toolchain_labels",
allowedTypes = {@ParamType(type = Sequence.class, generic1 = String.class)},
doc = "The labels of the toolchains to register."))
public void registerToolchains(Sequence<?> toolchainLabels) throws EvalException {
module.addToolchainsToRegister(
checkAllAbsolutePatterns(toolchainLabels, "register_toolchains"));
}

@StarlarkMethod(
name = "use_extension",
doc =
Expand Down
Expand Up @@ -183,6 +183,8 @@ public void testRootModule() throws Exception {
")",
"bazel_dep(name='B',version='1.0')",
"bazel_dep(name='C',version='2.0',repo_name='see')",
"register_toolchains('//my:toolchain3', '//my:toolchain4')",
"register_execution_platforms('//my:platform3', '//my:platform4')",
"single_version_override(module_name='D',version='18')",
"local_path_override(module_name='E',path='somewhere/else')",
"multiple_version_override(module_name='F',versions=['1.0','2.0'])",
Expand All @@ -203,9 +205,12 @@ public void testRootModule() throws Exception {
.setVersion(Version.parse("0.1"))
.setKey(ModuleKey.ROOT)
.setCompatibilityLevel(4)
.setExecutionPlatformsToRegister(
ImmutableList.of("//my:platform", "//my:platform2"))
.setToolchainsToRegister(ImmutableList.of("//my:toolchain", "//my:toolchain2"))
.addExecutionPlatformsToRegister(
ImmutableList.of(
"//my:platform", "//my:platform2", "//my:platform3", "//my:platform4"))
.addToolchainsToRegister(
ImmutableList.of(
"//my:toolchain", "//my:toolchain2", "//my:toolchain3", "//my:toolchain4"))
.addDep("B", createModuleKey("B", "1.0"))
.addDep("see", createModuleKey("C", "2.0"))
.build());
Expand Down

0 comments on commit 0f05904

Please sign in to comment.