Skip to content

Commit

Permalink
Respect rules_java as the source of truth for java toolchains
Browse files Browse the repository at this point in the history
Context: #18373

Currently the definitions of java tools, remote JDKs, and Java toolchains are duplicated in both Bazel sources and rules_java, and they go out of sync quite often. By default, the default toolchains shipped with the Bazel binary uses the one in Bazel sources except when Bzlmod is enabled.

bazelbuild/rules_java#110 syncs java toolchain related files from Bazel sources to rules_java and this PR removes most of the definitions in Bazel source and uses rules_java as the source of truth.

Significant changes in this PR:

- Removed all java tools definitions from distdir_deps.bzl
- Removed all remote JDK definitions from distdir_deps.bzl except the ones for to be embedded within Bazel binary.
- Changed `jdk.WORKSPACE.tmpl` to load java tools and JDKs from rules_java as WORKSPACE suffix.
- Removed the `jdk_http_archives` hack for some shell tests. That dates back to #8361, I don't think it's needed since we have the WORKSPACE suffix by default.
- When building Bazel with Bazel, we were using the java toolchains shipped in `@bazel_tools` of the Bazel binary, now we use the java toolchains from the `rules_java` defined in distdir_deps.bzl. This enables us to build Bazel itself with the latest java toolchain, therefore can detect issues earlier.
- Removed `_for_testing` suffixes for the shared repo hack for Bazel CI since the java tools and remote JDKs versions we load to build Bazel are the same as the ones shipped in the Bazel binary we built (via `jdk.WORKSPACE.tmpl`) and used in the integration tests. They all come from the rules_java version in distdir_deps.bzl.
- Removed the `test_WORKSPACE_files` hack for `workspace_resolved_test.sh`.
- Mocked rules_java in some integration tests to avoid needing to download it or its transitive dependencies.

Breaking changes:
- Loading `.bzl` files under `@bazel_tools//tools/jdk` in WORKSPACE now requires you to define `rules_java` in advance, the `rules_java` definition in WORKSPACE suffix cannot guarantee `@rules_java` exist because it's appended after the content of the user WORKSPACE file.

Closes #18423.

RELNOTES[INC]: Loading `.bzl` files under `@bazel_tools//tools/jdk` in WORKSPACE now requires `rules_java` to be defined in advance.

PiperOrigin-RevId: 536682532
Change-Id: Ia7475bfe134729ab1592b0ef0a31d28eb839b5ac
  • Loading branch information
meteorcloudy authored and copybara-github committed May 31, 2023
1 parent 3efaa32 commit 975866a
Show file tree
Hide file tree
Showing 40 changed files with 312 additions and 2,655 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bazel_dep(name = "stardoc", version = "0.5.3", repo_name = "io_bazel_skydoc")
bazel_dep(name = "zstd-jni", version = "1.5.2-3")
bazel_dep(name = "zlib", version = "1.2.13")
bazel_dep(name = "rules_cc", version = "0.0.6")
bazel_dep(name = "rules_java", version = "5.5.0")
bazel_dep(name = "rules_java", version = "6.0.0")
bazel_dep(name = "rules_proto", version = "5.3.0-21.7")
bazel_dep(name = "rules_jvm_external", version = "4.5")
bazel_dep(name = "rules_python", version = "0.19.0")
Expand Down
229 changes: 4 additions & 225 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
workspace(name = "io_bazel")

load("//tools/build_defs/repo:http.bzl", "http_archive")
load("//:distdir.bzl", "dist_http_archive", "dist_http_jar", "distdir_tar")
load("//:distdir.bzl", "dist_http_archive", "distdir_tar", "dist_http_jar")
load("//:distdir_deps.bzl", "DIST_DEPS")
load("//:repositories.bzl", "embedded_jdk_repositories")
load("//tools/jdk:jdk_build_file.bzl", "JDK_BUILD_TEMPLATE")

# These can be used as values for the patch_cmds and patch_cmds_win attributes
# of http_archive, in order to export the WORKSPACE file from the BUILD or
Expand Down Expand Up @@ -237,7 +236,6 @@ java_library(
],
)

# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
dist_http_archive(
name = "rules_cc",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
Expand All @@ -256,26 +254,6 @@ dist_http_archive(
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

# For testing, have an distdir_tar with all the archives implicit in every
# WORKSPACE, to that they don't have to be refetched for every test
# calling `bazel sync`.
distdir_tar(
name = "test_WORKSPACE_files",
archives = [
"android_tools_pkg-0.28.0.tar",
],
dirname = "test_WORKSPACE/distdir",
dist_deps = {dep: attrs for dep, attrs in DIST_DEPS.items() if "test_WORKSPACE_files" in attrs["used_in"]},
sha256 = {
"android_tools_pkg-0.28.0.tar": "db3b02421ae974e0b33573f3e4f658d5f89cc9a0b42baae0ba2ac08e25c0720a",
},
urls = {
"android_tools_pkg-0.28.0.tar": [
"https://mirror.bazel.build/bazel_android_tools/android_tools_pkg-0.28.0.tar",
],
},
)

dist_http_archive(
name = "io_bazel_skydoc",
)
Expand Down Expand Up @@ -309,208 +287,9 @@ dist_http_archive(
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

dist_http_archive(
name = "remotejdk11_linux_for_testing",
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = 11),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

dist_http_archive(
name = "remotejdk11_linux_aarch64_for_testing",
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = 11),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

dist_http_archive(
name = "remotejdk11_linux_ppc64le_for_testing",
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = 11),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

dist_http_archive(
name = "remotejdk11_linux_s390x_for_testing",
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = 11),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

dist_http_archive(
name = "remotejdk11_macos_for_testing",
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = 11),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

dist_http_archive(
name = "remotejdk11_macos_aarch64_for_testing",
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = 11),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

dist_http_archive(
name = "remotejdk11_win_for_testing",
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = 11),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

dist_http_archive(
name = "remotejdk11_win_arm64_for_testing",
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = 11),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)

[
dist_http_archive(
name = "remotejdk%s_%s_for_testing" % (version, os),
build_file_content = JDK_BUILD_TEMPLATE.format(RUNTIME_VERSION = version),
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_BAZEL_FILE_WIN,
)
for version in ("17", "20")
for os in ("linux", "macos", "macos_aarch64", "win") + (("linux_s390x", "win_arm64") if version != "20" else ())
]

# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
dist_http_archive(
name = "remote_java_tools_for_testing",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
dist_http_archive(
name = "remote_java_tools_linux_for_testing",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
dist_http_archive(
name = "remote_java_tools_windows_for_testing",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
dist_http_archive(
name = "remote_java_tools_darwin_x86_64_for_testing",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
dist_http_archive(
name = "remote_java_tools_darwin_arm64_for_testing",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "remote_java_tools_test",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "remote_java_tools_test_linux",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "remote_java_tools_test_windows",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "remote_java_tools_test_darwin_x86_64",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

# Used in src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "remote_java_tools_test_darwin_arm64",
patch_cmds = EXPORT_WORKSPACE_IN_BUILD_FILE,
patch_cmds_win = EXPORT_WORKSPACE_IN_BUILD_FILE_WIN,
)

dist_http_archive(
name = "openjdk11_linux_archive",
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "openjdk11_linux_s390x_archive",
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "openjdk11_darwin_archive",
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "openjdk11_darwin_aarch64_archive",
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "openjdk11_windows_archive",
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
dist_http_archive(
name = "openjdk11_windows_arm64_archive",
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
)

# This must be kept in sync with src/test/shell/bazel/testdata/jdk_http_archives.
[
dist_http_archive(
name = "openjdk%s_%s_archive" % (version, os),
build_file_content = """
java_runtime(name = 'runtime', srcs = glob(['**']), visibility = ['//visibility:public'])
exports_files(["WORKSPACE"], visibility = ["//visibility:public"])
""",
)
for version in ("17", "20")
for os in ("linux", "darwin", "darwin_aarch64", "windows") + (("linux_s390x", "windows_arm64") if version != "20" else ())
]
load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains")
rules_java_dependencies()
rules_java_toolchains()

load("@io_bazel_skydoc//:setup.bzl", "stardoc_repositories")

Expand Down
Loading

0 comments on commit 975866a

Please sign in to comment.