Skip to content

Commit

Permalink
Force local_config_platform to redirect to the bundled platforms repo
Browse files Browse the repository at this point in the history
Similar to 77469e0, we redirect the redirects/aliases generated by `local_config_plaform` to the bundled platforms repo (`@internal_platforms_do_not_use`) if Bzlmod is not enabled. Same for the `@bazel_tools//tools:host_platform` alias.

Fixes #21877.

Closes #22021.

PiperOrigin-RevId: 625613724
Change-Id: I17940e5d4fbd8a7690d9199f80b80ceb62b53d1a
  • Loading branch information
Wyverald committed Apr 29, 2024
1 parent 09df65a commit 7bc30b3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/concurrent",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/packages/semantics",
"//src/main/java/com/google/devtools/build/lib/repository:repository_events",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repo_recorded_input",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_directory_value",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function",
"//src/main/java/com/google/devtools/build/lib/rules:repository/resolved_file_value",
"//src/main/java/com/google/devtools/build/lib/rules:repository/workspace_base_rule",
"//src/main/java/com/google/devtools/build/lib/rules:repository/workspace_configured_target_factory",
"//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/skyframe",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@

package com.google.devtools.build.lib.bazel.repository;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.bazel.ResolvedEvent;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.rules.repository.RepoRecordedInput;
import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
import com.google.devtools.build.lib.rules.repository.RepositoryFunction;
import com.google.devtools.build.lib.rules.repository.ResolvedFileValue;
import com.google.devtools.build.lib.skyframe.PrecomputedValue;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.XattrProvider;
import com.google.devtools.build.skyframe.SkyFunction.Environment;
import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
import com.google.devtools.build.skyframe.SkyKey;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.starlark.java.eval.StarlarkSemantics;

/** Create a local repository that describes the auto-detected host platform. */
public class LocalConfigPlatformFunction extends RepositoryFunction {
Expand All @@ -55,14 +55,25 @@ public RepositoryDirectoryValue.Builder fetch(
Environment env,
Map<RepoRecordedInput, String> recordedInputValues,
SkyKey key)
throws RepositoryFunctionException {
throws RepositoryFunctionException, InterruptedException {
StarlarkSemantics starlarkSemantics = PrecomputedValue.STARLARK_SEMANTICS.get(env);
if (starlarkSemantics == null) {
return null;
}
boolean enableBzlmod = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD);
// If Bzlmod is enabled, @platforms is definitely new enough to contain the new host platform
// and constraints bzl file. Otherwise, @platforms might be an older version, so we use the
// bundled @internal_platforms_do_not_use repo instead (see local_config_platform.WORKSPACE).
String platformsRepoName = enableBzlmod ? "@platforms" : "@internal_platforms_do_not_use";
String name = rule.getName();
try {
outputDirectory.createDirectoryAndParents();
RepositoryFunction.writeFile(outputDirectory, "WORKSPACE", workspaceFileContent(name));
RepositoryFunction.writeFile(outputDirectory, "MODULE.bazel", moduleFileContent(name));
RepositoryFunction.writeFile(outputDirectory, "BUILD.bazel", buildFileContent());
RepositoryFunction.writeFile(outputDirectory, "constraints.bzl", constraintFileContent());
RepositoryFunction.writeFile(
outputDirectory, "BUILD.bazel", buildFileContent(platformsRepoName));
RepositoryFunction.writeFile(
outputDirectory, "constraints.bzl", constraintFileContent(platformsRepoName));
} catch (IOException e) {
throw new RepositoryFunctionException(
new IOException("Could not create content for " + name + ": " + e.getMessage()),
Expand Down Expand Up @@ -96,47 +107,45 @@ public Object getResolvedInformation(XattrProvider xattrProvider) {
}

private static String workspaceFileContent(String repositoryName) {
return format(
ImmutableList.of(
"# DO NOT EDIT: automatically generated WORKSPACE file for local_config_platform",
"workspace(name = \"%s\")"),
return String.format(
"""
# DO NOT EDIT: automatically generated WORKSPACE file for local_config_platform
workspace(name = "%s")
""",
repositoryName);
}

private static String moduleFileContent(String repositoryName) {
return format(
ImmutableList.of(
"# DO NOT EDIT: automatically generated MODULE file for local_config_platform",
"module(name = \"%s\")",
// Try to keep this updated with the src/MODULE.tools file. (Due to MVS, even if this is
// not kept up to date, we'll use the latest version anyhow)
"bazel_dep(name = \"platforms\", version = \"0.0.7\")"),
return String.format(
"""
# DO NOT EDIT: automatically generated MODULE file for local_config_platform
module(name = "%s")
bazel_dep(name = "platforms", version = "0.0.7")
""",
repositoryName);
}

private static String buildFileContent() {
return format(
ImmutableList.of(
"# DO NOT EDIT: automatically generated BUILD file for local_config_platform",
"package(default_visibility = ['//visibility:public'])",
"alias(name = 'host', actual = '@platforms//host')",
"exports_files([",
" # Export constraints.bzl for use in downstream bzl_library targets.",
" 'constraints.bzl',",
"])"));
}

private static String constraintFileContent() {
return format(
ImmutableList.of(
"# DO NOT EDIT: automatically generated constraints list for local_config_platform",
"load('@platforms//host:constraints.bzl', _HOST_CONSTRAINTS='HOST_CONSTRAINTS')",
"HOST_CONSTRAINTS = _HOST_CONSTRAINTS"));
private static String buildFileContent(String platformsRepoName) {
return String.format(
"""
# DO NOT EDIT: automatically generated BUILD file for local_config_platform
package(default_visibility = ['//visibility:public'])
alias(name = 'host', actual = '%s//host')
exports_files([
# Export constraints.bzl for use in downstream bzl_library targets.
'constraints.bzl',
])
""",
platformsRepoName);
}

private static String format(List<String> lines, Object... params) {
// Add a newline between each line, and also after the final line.
String content = lines.stream().collect(Collectors.joining("\n", "", "\n"));
return String.format(content, params);
private static String constraintFileContent(String platformsRepoName) {
return String.format(
"""
# DO NOT EDIT: automatically generated constraints list for local_config_platform
load('%s//host:constraints.bzl', _HOST_CONSTRAINTS='HOST_CONSTRAINTS')
HOST_CONSTRAINTS = _HOST_CONSTRAINTS
""",
platformsRepoName);
}
}
4 changes: 3 additions & 1 deletion tools/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load(":build_defs.bzl", "BZLMOD_ENABLED")

package(default_visibility = ["//visibility:public"])

filegroup(
Expand Down Expand Up @@ -94,5 +96,5 @@ sh_binary(

alias(
name = "host_platform",
actual = "@platforms//host",
actual = "@platforms//host" if BZLMOD_ENABLED else "@internal_platforms_do_not_use//host",
)
2 changes: 2 additions & 0 deletions tools/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ def transition_java_language_8_filegroup(name, files, visibility):
files = files,
visibility = visibility,
)

BZLMOD_ENABLED = str(Label("@bazel_tools//:foo")).startswith("@@")

0 comments on commit 7bc30b3

Please sign in to comment.