From 0f05904171d187e6abacb431b3d7494423b027ab Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 18 Jul 2022 21:01:08 +0200 Subject: [PATCH] Add register_{execution_platforms,toolchains} directives to MODULE.bazel files (#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. https://github.com/bazelbuild/bazel/issues/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 Co-authored-by: Chenchu K --- .../build/lib/bazel/bzlmod/Module.java | 23 +++++++---- .../lib/bazel/bzlmod/ModuleFileGlobals.java | 40 ++++++++++++++++++- .../bazel/bzlmod/ModuleFileFunctionTest.java | 11 +++-- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/Module.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/Module.java index 61f44dc131d48e..1bd7020c609791 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/Module.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/Module.java @@ -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; @@ -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); } /** @@ -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 value); + abstract ImmutableList.Builder executionPlatformsToRegisterBuilder(); - /** Optional; defaults to an empty list. */ - public abstract Builder setToolchainsToRegister(ImmutableList value); + @CanIgnoreReturnValue + public final Builder addExecutionPlatformsToRegister(Iterable values) { + executionPlatformsToRegisterBuilder().addAll(values); + return this; + } + + abstract ImmutableList.Builder toolchainsToRegisterBuilder(); + + @CanIgnoreReturnValue + public final Builder addToolchainsToRegister(Iterable values) { + toolchainsToRegisterBuilder().addAll(values); + return this; + } public abstract Builder setDeps(ImmutableMap value); diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java index 4a0d44fc5b61b3..396ec6ccc7e525 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java @@ -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()); } @@ -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" + + " @ or //). See toolchain" + + " resolution 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 @ or" + + " //). See toolchain resolution 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 = diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java index 223dfa6028df3f..deba48a57d85c3 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java @@ -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'])", @@ -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());