LSC(tp): Add `load()` statements for the builtin top-level java symbols
BEGIN_PUBLIC
Add `load()` statements for the Bazel builtin top-level java symbols
Loads are being added in preparation for moving the symbols out of Bazel and into `rules_java`.
END_PUBLIC
PiperOrigin-RevId: 624087139
Change-Id: I3d98ec19cf1c2cd7e587e4a3e6117c49d7ffd2e8
diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/tests.bzl b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/tests.bzl
index c0bbec5..4997c8a 100644
--- a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/tests.bzl
+++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/tests.bzl
@@ -13,6 +13,8 @@
# limitations under the License.
"""Helpers to create golden tests, to minimize code duplication."""
+load("@rules_java//java/common:java_info.bzl", "JavaInfo")
+
def _compile_time_jars(ctx):
jars = depset([], transitive = [dep[JavaInfo].transitive_compile_time_jars for dep in ctx.attr.deps])
return [DefaultInfo(
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/mock/BUILD
index ccba505..bf38cae 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BUILD
@@ -20,6 +20,7 @@
]),
data = [
"//tools/jdk:srcs",
+ "@rules_java//java:rules",
"@rules_java//toolchains:srcs",
],
resources = [
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
index 177e77f..a2e5923 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
@@ -621,70 +621,6 @@
config.create("embedded_tools/objcproto/empty.cc");
config.create("embedded_tools/objcproto/well_known_type.proto");
- config.create("rules_java_workspace/WORKSPACE", "workspace(name = 'rules_java')");
- config.create("rules_java_workspace/MODULE.bazel", "module(name = 'rules_java')");
- config.create("rules_java_workspace/java/BUILD");
- config.create("rules_java_workspace/toolchains/BUILD");
- java.nio.file.Path path =
- Paths.get(runfiles.rlocation("rules_java/toolchains/java_toolchain_alias.bzl"));
- if (Files.exists(path)) {
- config.create(
- "rules_java_workspace/toolchains/java_toolchain_alias.bzl",
- MoreFiles.asCharSource(path, UTF_8).read());
- }
- config.create(
- "rules_java_workspace/toolchains/local_java_repository.bzl",
- """
- def local_java_repository(**attrs):
- pass
- """);
- config.create("rules_java_workspace/toolchains/jdk_build_file.bzl", "JDK_BUILD_TEMPLATE = ''");
- config.create(
- "rules_java_workspace/java/defs.bzl",
- """
- def java_binary(**attrs):
- native.java_binary(**attrs)
-
- def java_library(**attrs):
- native.java_library(**attrs)
-
- def java_import(**attrs):
- native.java_import(**attrs)
- """);
- config.create(
- "rules_java_workspace/java/repositories.bzl",
- """
- def rules_java_dependencies():
- pass
-
- def rules_java_toolchains():
- native.register_toolchains("//java/toolchains/runtime:all")
- native.register_toolchains("//java/toolchains/javac:all")
- """);
-
- config.create(
- "rules_java_workspace/java/toolchains/runtime/BUILD",
- """
- toolchain_type(name = "toolchain_type")
-
- toolchain(
- name = "local_jdk",
- toolchain = "@bazel_tools//tools/jdk:jdk",
- toolchain_type = "@rules_java//java/toolchains/runtime:toolchain_type",
- )
- """);
- config.create(
- "rules_java_workspace/java/toolchains/javac/BUILD",
- """
- toolchain_type(name = "toolchain_type")
-
- toolchain(
- name = "javac_toolchain",
- toolchain = "@bazel_tools//tools/jdk:toolchain",
- toolchain_type = "@rules_java//java/toolchains/javac:toolchain_type",
- )
- """);
-
config.create("third_party/bazel_rules/rules_proto/WORKSPACE");
config.create("third_party/bazel_rules/rules_proto/MODULE.bazel", "module(name='rules_proto')");
@@ -702,6 +638,7 @@
MockPlatformSupport.setup(config);
ccSupport().setup(config);
+ javaSupport().setupRulesJava(config, runfiles::rlocation);
pySupport().setup(config);
ShellConfiguration.injectShellExecutableFinder(
BazelRuleClassProvider::getDefaultPathFromOptions, BazelRuleClassProvider.SHELL_EXECUTABLE);
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/AbstractMockJavaSupport.java b/src/test/java/com/google/devtools/build/lib/analysis/util/AbstractMockJavaSupport.java
index dd9b3a0..fe2078b 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/util/AbstractMockJavaSupport.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/util/AbstractMockJavaSupport.java
@@ -13,18 +13,100 @@
// limitations under the License.
package com.google.devtools.build.lib.analysis.util;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.io.MoreFiles;
import com.google.devtools.build.lib.packages.util.MockToolsConfig;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.function.Function;
public abstract class AbstractMockJavaSupport {
public static final AbstractMockJavaSupport BAZEL =
new AbstractMockJavaSupport() {
@Override
- public void setupRulesJava(MockToolsConfig mockToolsConfig) {
- // nothing special required
+ public void setupRulesJava(
+ MockToolsConfig config, Function<String, String> runfilesResolver) throws IOException {
+ config.create("rules_java_workspace/WORKSPACE", "workspace(name = 'rules_java')");
+ config.create("rules_java_workspace/MODULE.bazel", "module(name = 'rules_java')");
+ config.create("rules_java_workspace/java/BUILD");
+ config.create("rules_java_workspace/java/common/BUILD");
+ config.create("rules_java_workspace/java/private/BUILD");
+ config.create("rules_java_workspace/java/toolchains/BUILD");
+ config.create("rules_java_workspace/toolchains/BUILD");
+ ImmutableList<String> toolsToCopy =
+ ImmutableList.of(
+ "java/defs.bzl",
+ "java/java_binary.bzl",
+ "java/java_import.bzl",
+ "java/java_library.bzl",
+ "java/java_plugin.bzl",
+ "java/java_test.bzl",
+ "java/common/java_common.bzl",
+ "java/common/java_info.bzl",
+ "java/common/java_plugin_info.bzl",
+ "java/private/native.bzl",
+ "java/toolchains/java_package_configuration.bzl",
+ "java/toolchains/java_runtime.bzl",
+ "java/toolchains/java_toolchain.bzl",
+ "toolchains/java_toolchain_alias.bzl");
+ for (String relativePath : toolsToCopy) {
+ Path path = Path.of(runfilesResolver.apply("rules_java/" + relativePath));
+ if (Files.exists(path)) {
+ config.create(
+ "rules_java_workspace/" + relativePath,
+ MoreFiles.asCharSource(path, UTF_8).read());
+ }
+ }
+ // mocks
+ config.create(
+ "rules_java_workspace/toolchains/local_java_repository.bzl",
+ """
+ def local_java_repository(**attrs):
+ pass
+ """);
+ config.create(
+ "rules_java_workspace/toolchains/jdk_build_file.bzl", "JDK_BUILD_TEMPLATE = ''");
+ config.create(
+ "rules_java_workspace/java/repositories.bzl",
+ """
+ def rules_java_dependencies():
+ pass
+
+ def rules_java_toolchains():
+ native.register_toolchains("//java/toolchains/runtime:all")
+ native.register_toolchains("//java/toolchains/javac:all")
+ """);
+
+ config.create(
+ "rules_java_workspace/java/toolchains/runtime/BUILD",
+ """
+ toolchain_type(name = "toolchain_type")
+
+ toolchain(
+ name = "local_jdk",
+ toolchain = "@bazel_tools//tools/jdk:jdk",
+ toolchain_type = "@rules_java//java/toolchains/runtime:toolchain_type",
+ )
+ """);
+ config.create(
+ "rules_java_workspace/java/toolchains/javac/BUILD",
+ """
+ toolchain_type(name = "toolchain_type")
+
+ toolchain(
+ name = "javac_toolchain",
+ toolchain = "@bazel_tools//tools/jdk:toolchain",
+ toolchain_type = "@rules_java//java/toolchains/javac:toolchain_type",
+ )
+ """);
}
};
- public abstract void setupRulesJava(MockToolsConfig mockToolsConfig) throws IOException;
+ public abstract void setupRulesJava(
+ MockToolsConfig mockToolsConfig, Function<String, String> runfilesResolver)
+ throws IOException;
}
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/BindTest.java b/src/test/java/com/google/devtools/build/lib/starlark/BindTest.java
index c4dfa94..8d819a5 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/BindTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/BindTest.java
@@ -28,6 +28,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
+import java.util.function.Function;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -39,6 +40,7 @@
@Before
public final void createFiles() throws Exception {
+ analysisMock.javaSupport().setupRulesJava(mockToolsConfig, Function.identity());
setupStarlarkRules(scratch);
scratch.file(
"test/BUILD",
@@ -94,8 +96,10 @@
Artifact giraffeArtifact =
ActionsTestUtil.getFirstArtifactEndingWith(getFilesToBuild(giraffeTarget), "giraffe.jar");
ConfiguredTarget safariTarget = getConfiguredTarget("//test:safari");
- Action safariAction = getGeneratingAction(
- ActionsTestUtil.getFirstArtifactEndingWith(getFilesToBuild(safariTarget), "safari.jar"));
+ Action safariAction =
+ getGeneratingAction(
+ ActionsTestUtil.getFirstArtifactEndingWith(
+ getFilesToBuild(safariTarget), "safari.jar"));
assertThat(safariAction.getInputs().toList()).contains(giraffeArtifact);
}
diff --git a/tools/android/defs.bzl b/tools/android/defs.bzl
index 22baafc..e14b601 100644
--- a/tools/android/defs.bzl
+++ b/tools/android/defs.bzl
@@ -14,6 +14,8 @@
"""Provides rules to run ijar and single_jar from java_toolchain."""
+load("@rules_java//java/common:java_common.bzl", "java_common")
+
def _run_ijar(ctx):
ijar_jar = java_common.run_ijar(
ctx.actions,
diff --git a/tools/build_defs/inspect/struct_to_dict.bzl b/tools/build_defs/inspect/struct_to_dict.bzl
index d46fe27..eca7726 100644
--- a/tools/build_defs/inspect/struct_to_dict.bzl
+++ b/tools/build_defs/inspect/struct_to_dict.bzl
@@ -18,6 +18,8 @@
specified depth are copied literally.
"""
+load("@rules_java//java/common:java_info.bzl", "JavaInfo")
+
def struct_to_dict(x, depth = 5):
root = {}
queue = [(root, x)]
diff --git a/tools/build_rules/java_rules_skylark.bzl b/tools/build_rules/java_rules_skylark.bzl
index 7cba9e4..d3b5858 100644
--- a/tools/build_rules/java_rules_skylark.bzl
+++ b/tools/build_rules/java_rules_skylark.bzl
@@ -19,6 +19,8 @@
ready.
"""
+load("@rules_java//java/common:java_common.bzl", "java_common")
+
_JarsInfo = provider(fields = ["compile_time_jars", "runtime_jars"])
def _join_paths(separator, files):
diff --git a/tools/build_rules/utilities.bzl b/tools/build_rules/utilities.bzl
index f817bef..3445d8f 100644
--- a/tools/build_rules/utilities.bzl
+++ b/tools/build_rules/utilities.bzl
@@ -15,6 +15,8 @@
"""This rule exposes the source jar of a java_*_library rule as a label."""
+load("@rules_java//java/common:java_info.bzl", "JavaInfo")
+
def _java_library_srcs_impl(ctx):
if len(ctx.attr.deps) != 1:
fail("Only one deps value supported", "deps")