From 637276e4bb4b229e0151845feca1b7e28a089e91 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 10 Nov 2021 13:21:21 +0100 Subject: [PATCH 01/15] Update remote jdk and java tools dependencies --- java/repositories.bzl | 505 +++++++++++++++--------------------------- 1 file changed, 183 insertions(+), 322 deletions(-) diff --git a/java/repositories.bzl b/java/repositories.bzl index c093fe9..dd77ac6 100644 --- a/java/repositories.bzl +++ b/java/repositories.bzl @@ -28,469 +28,330 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//toolchains:remote_java_repository.bzl", "remote_java_repository") def java_tools_javac11_repos(): - maybe( - http_archive, - name = "remote_java_tools_linux", - sha256 = "355c27c603e8fc64bb0e2d7f809741f42576d5f4540f9ce28fd55922085af639", + http_archive( + name = "remote_java_tools", + sha256 = "b763ee80e5754e593fd6d5be6d7343f905bc8b73d661d36d842b024ca11b6793", urls = [ - "https://mirror.bazel.build/bazel_java_tools/releases/javac11/v10.5/java_tools_javac11_linux-v10.5.zip", - "https://github.com/bazelbuild/java_tools/releases/download/javac11_v10.5/java_tools_javac11_linux-v10.5.zip", + "https://mirror.bazel.build/bazel_java_tools/releases/java/v11.5/java_tools-v11.5.zip", + "https://github.com/bazelbuild/java_tools/releases/download/java_v11.5/java_tools-v11.5.zip", ], ) - maybe( - http_archive, - name = "remote_java_tools_windows", - sha256 = "0b4469ca1a9b3f26cb82fb0f4fd00096f0d839ec2fae097e7bdbb982e3a95a59", - urls = [ - "https://mirror.bazel.build/bazel_java_tools/releases/javac11/v10.5/java_tools_javac11_windows-v10.5.zip", - "https://github.com/bazelbuild/java_tools/releases/download/javac11_v10.5/java_tools_javac11_windows-v10.5.zip", - ], - ) - maybe( - http_archive, - name = "remote_java_tools_darwin", - sha256 = "95aae0a32a170c72a68abb0b9dd6bac7ea3e08c504a5d8c6e8bf7ac51628c98f", + + http_archive( + name = "remote_java_tools_linux", + sha256 = "ae1eca4546eac6487c6e565f9b409536609c273207220c51e5c94f2a058a5a56", urls = [ - "https://mirror.bazel.build/bazel_java_tools/releases/javac11/v10.5/java_tools_javac11_darwin-v10.5.zip", - "https://github.com/bazelbuild/java_tools/releases/download/javac11_v10.5/java_tools_javac11_darwin-v10.5.zip", + "https://mirror.bazel.build/bazel_java_tools/releases/java/v11.5/java_tools_linux-v11.5.zip", + "https://github.com/bazelbuild/java_tools/releases/download/java_v11.5/java_tools_linux-v11.5.zip", ], ) -def remote_jdk8_repos(): - """Imports OpenJDK 8 repositories. - The source-code for this OpenJDK can be found at: - https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/legacy8ujsse-Legacy8uJSSE_1_1_1.tar.gz - https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/openjsse-OpenJSSE_1_1_5.tar.gz - https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zsrc8.50.0.53-ca-fx-jdk8.0.275.zip - """ - maybe( - remote_java_repository, - name = "remote_jdk8_linux_aarch64", - exec_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:aarch64", - ], - sha256 = "f4072e82faa5a09fab2accf2892d4684324fc999d614583c3ff785e87c03963f", - strip_prefix = "zulu8.50.51.263-ca-jdk8.0.275-linux_aarch64", - urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zulu8.50.51.263-ca-jdk8.0.275-linux_aarch64.tar.gz", - "https://cdn.azul.com/zulu-embedded/bin/zulu8.50.51.263-ca-jdk8.0.275-linux_aarch64.tar.gz", - ], - version = "8", - ) - maybe( - remote_java_repository, - name = "remote_jdk8_linux", - exec_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - sha256 = "1db6b2fa642950ee1b4b1ec2b6bc8a9113d7a4cd723f79398e1ada7dab1c981c", - strip_prefix = "zulu8.50.0.51-ca-jdk8.0.275-linux_x64", + http_archive( + name = "remote_java_tools_windows", + sha256 = "36766802f7ec684cecb1a14c122428de6be9784e88419e2ab5912ad4b59a8c7d", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zulu8.50.0.51-ca-jdk8.0.275-linux_x64.tar.gz", - "https://cdn.azul.com/zulu/bin/zulu8.50.0.51-ca-jdk8.0.275-linux_x64.tar.gz", + "https://mirror.bazel.build/bazel_java_tools/releases/java/v11.5/java_tools_windows-v11.5.zip", + "https://github.com/bazelbuild/java_tools/releases/download/java_v11.5/java_tools_windows-v11.5.zip", ], - version = "8", ) - maybe( - remote_java_repository, - name = "remote_jdk8_macos", - exec_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], - sha256 = "b03176597734299c9a15b7c2cc770783cf14d121196196c1248e80c026b59c17", - strip_prefix = "zulu8.50.0.51-ca-jdk8.0.275-macosx_x64", + + http_archive( + name = "remote_java_tools_darwin", + sha256 = "792bc1352e736073b152528175ed424687f86a9f6f5f461f07d8b26806762738", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zulu8.50.0.51-ca-jdk8.0.275-macosx_x64.tar.gz", - "https://cdn.azul.com/zulu/bin/zulu8.50.0.51-ca-jdk8.0.275-macosx_x64.tar.gz", + "https://mirror.bazel.build/bazel_java_tools/releases/java/v11.5/java_tools_darwin-v11.5.zip", + "https://github.com/bazelbuild/java_tools/releases/download/java_v11.5/java_tools_darwin-v11.5.zip", ], - version = "8", ) - maybe( - remote_java_repository, - name = "remote_jdk8_windows", + +def remote_jdk11_repos(): + """Imports OpenJDK 11 repositories.""" + remote_java_repository( + name = "remotejdk11_linux", exec_compatible_with = [ - "@platforms//os:windows", + "@platforms//os:linux", "@platforms//cpu:x86_64", ], - sha256 = "49759b2bd2ab28231a21ff3a3bb45824ddef55d89b5b1a05a62e26a365da0774", - strip_prefix = "zulu8.50.0.51-ca-jdk8.0.275-win_x64", + sha256 = "b8e8a63b79bc312aa90f3558edbea59e71495ef1a9c340e38900dd28a1c579f3", + strip_prefix = "zulu11.50.19-ca-jdk11.0.12-linux_x64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zulu8.50.0.51-ca-jdk8.0.275-win_x64.zip", - "https://cdn.azul.com/zulu/bin/zulu8.50.0.51-ca-jdk8.0.275-win_x64.zip", + "https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-linux_x64.tar.gz", ], - version = "8", + version = "11", ) -def remote_jdk9_repos(): - """Imports OpenJDK 9 repositories. - - The source-code for this OpenJDK can be found at: - https://mirror.bazel.build/openjdk.linaro.org/releases/jdk9-src-1708.tar.xz - https://openjdk.linaro.org/releases/jdk9-src-1708.tar.xz - https://mirror.bazel.build/openjdk/azul-zulu-9.0.7.1-jdk9.0.7/zsrc9.0.7.1-jdk9.0.7.zip - """ - maybe( - remote_java_repository, - name = "remote_jdk9_linux_aarch64", + remote_java_repository( + name = "remotejdk11_linux_aarch64", exec_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:aarch64", ], - sha256 = "72e7843902b0395e2d30e1e9ad2a5f05f36a4bc62529828bcbc698d54aec6022", - strip_prefix = "jdk9-server-release-1708", + sha256 = "61254688067454d3ccf0ef25993b5dcab7b56c8129e53b73566c28a8dd4d48fb", + strip_prefix = "zulu11.50.19-ca-jdk11.0.12-linux_aarch64", urls = [ - # When you update this, also update the link to the source-code above. - "https://mirror.bazel.build/openjdk.linaro.org/releases/jdk9-server-release-170bazel_skylib8.tar.xz", - "http://openjdk.linaro.org/releases/jdk9-server-release-1708.tar.xz", + "https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-linux_aarch64.tar.gz", ], - version = "9", + version = "11", ) - maybe( - remote_java_repository, - name = "remote_jdk9_linux", + remote_java_repository( + name = "remotejdk11_linux_ppc64le", exec_compatible_with = [ "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], - sha256 = "45f2dfbee93b91b1468cf81d843fc6d9a47fef1f831c0b7ceff4f1eb6e6851c8", - strip_prefix = "zulu9.0.7.1-jdk9.0.7-linux_x64", - urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu-9.0.7.1-jdk9.0.7/zulu9.0.7.1-jdk9.0.7-linux_x64.tar.gz", - ], - version = "9", - ) - maybe( - remote_java_repository, - name = "remote_jdk9_macos", - exec_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], - strip_prefix = "zulu9.0.7.1-jdk9.0.7-macosx_x64", - urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu-9.0.7.1-jdk9.0.7/zulu9.0.7.1-jdk9.0.7-macosx_x64.tar.gz", - ], - version = "9", - ) - maybe( - remote_java_repository, - name = "remote_jdk9_windows", - exec_compatible_with = [ - "@platforms//os:windows", - "@platforms//cpu:x86_64", + "@platforms//cpu:ppc", ], - strip_prefix = "zulu9.0.7.1-jdk9.0.7-win_x64", + sha256 = "a417db0295b1f4b538ecbaf7c774f3a177fab9657a665940170936c0eca4e71a", + strip_prefix = "jdk-11.0.7+10", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu-9.0.7.1-jdk9.0.7/zulu9.0.7.1-jdk9.0.7-win_x64.zip", + "https://mirror.bazel.build/openjdk/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.7+10/OpenJDK11U-jdk_ppc64le_linux_hotspot_11.0.7_10.tar.gz", + "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.7+10/OpenJDK11U-jdk_ppc64le_linux_hotspot_11.0.7_10.tar.gz", ], - version = "9", + version = "11", ) -def remote_jdk10_repos(): - """Imports OpenJDK 10 repositories. - - The source-code for this OpenJDK can be found at: - https://openjdk.linaro.org/releases/jdk10-src-1804.tar.xz - https://mirror.bazel.build/openjdk.linaro.org/releases/jdk10-src-1804.tar.xz - https://mirror.bazel.build/openjdk/azul-zulu10.2+3-jdk10.0.1/zsrc10.2+3-jdk10.0.1.zip - """ - - maybe( - remote_java_repository, - name = "remote_jdk10_linux_aarch64", + remote_java_repository( + name = "remotejdk11_linux_s390x", exec_compatible_with = [ "@platforms//os:linux", - "@platforms//cpu:aarch64", + "@platforms//cpu:s390x", ], - sha256 = "b7098b7aaf6ee1ffd4a2d0371a0be26c5a5c87f6aebbe46fe9a92c90583a84be", - strip_prefix = "jdk10-server-release-1804", + sha256 = "d9b72e87a1d3ebc0c9552f72ae5eb150fffc0298a7cb841f1ce7bfc70dcd1059", + strip_prefix = "jdk-11.0.7+10", urls = [ - # When you update this, also update the link to the source-code above. - "https://mirror.bazel.build/openjdk.linaro.org/releases/jdk10-server-release-1804.tar.xz", - "http://openjdk.linaro.org/releases/jdk10-server-release-1804.tar.xz", + "https://mirror.bazel.build/github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.7+10/OpenJDK11U-jdk_s390x_linux_hotspot_11.0.7_10.tar.gz", + "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.7+10/OpenJDK11U-jdk_s390x_linux_hotspot_11.0.7_10.tar.gz", ], - version = "10", + version = "11", ) - maybe( - remote_java_repository, - name = "remote_jdk10_linux", + + remote_java_repository( + name = "remotejdk11_macos", exec_compatible_with = [ - "@platforms//os:linux", + "@platforms//os:macos", "@platforms//cpu:x86_64", ], - sha256 = "b3c2d762091a615b0c1424ebbd05d75cc114da3bf4f25a0dec5c51ea7e84146f", - strip_prefix = "zulu10.2+3-jdk10.0.1-linux_x64", + sha256 = "0b8c8b7cf89c7c55b7e2239b47201d704e8d2170884875b00f3103cf0662d6d7", + strip_prefix = "zulu11.50.19-ca-jdk11.0.12-macosx_x64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu10.2+3-jdk10.0.1/zulu10.2+3-jdk10.0.1-linux_x64.tar.gz", + "https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-macosx_x64.tar.gz", ], - version = "10", + version = "11", ) - maybe( - remote_java_repository, - name = "remote_jdk10_macos", + + remote_java_repository( + name = "remotejdk11_macos_aarch64", exec_compatible_with = [ "@platforms//os:macos", - "@platforms//cpu:x86_64", + "@platforms//cpu:aarch64", ], - sha256 = "7394d5f41804cfbdb47c609879c4e738bf53358484ea0995076190915b94c702", - strip_prefix = "zulu10.2+3-jdk10.0.1-macosx_x64", + sha256 = "e908a0b4c0da08d41c3e19230f819b364ff2e5f1dafd62d2cf991a85a34d3a17", + strip_prefix = "zulu11.50.19-ca-jdk11.0.12-macosx_aarch64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu10.2+3-jdk10.0.1/zulu10.2+3-jdk10.0.1-macosx_x64.tar.gz", + "https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-macosx_aarch64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu11.50.19-ca-jdk11.0.12-macosx_aarch64.tar.gz", ], - version = "10", + version = "11", ) - maybe( - remote_java_repository, - name = "remote_jdk10_windows", + + remote_java_repository( + name = "remotejdk11_win", exec_compatible_with = [ "@platforms//os:windows", "@platforms//cpu:x86_64", ], - sha256 = "fd9456b53dab8b9f504ed0f0e2f6305bd0815978d0e02a41643d111290bf940c", - strip_prefix = "zulu10.2+3-jdk10.0.1-win_x64", + sha256 = "42ae65e75d615a3f06a674978e1fa85fdf078cad94e553fee3e779b2b42bb015", + strip_prefix = "zulu11.50.19-ca-jdk11.0.12-win_x64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu10.2+3-jdk10.0.1/zulu10.2+3-jdk10.0.1-win_x64.zip", + "https://mirror.bazel.build/openjdk/azul-zulu11.50.19-ca-jdk11.0.12/zulu11.50.19-ca-jdk11.0.12-win_x64.zip", ], - version = "10", + version = "11", ) -def remote_jdk11_repos(): - """Imports OpenJDK 11 repositories. - - The source-code for this OpenJDK can be found at: - https://mirror.bazel.build/openjdk/azul-zulu11.43.55-ca-jdk11.0.9.1/zsrc11.43.55-jdk11.0.9.1.zip - https://mirror.bazel.build/openjdk/azul-zulu11.43.55-ca-jdk11.0.9.1/openjsse-OpenJSSE_1_1_5.tar.gz - https://mirror.bazel.build/openjdk/azul-zulu11.43.100-ca-jdk11.0.9.1/zulu11.43.100-ca-src-jdk11.0.9.1-linux_aarch64.zip - """ - - maybe( - remote_java_repository, - name = "remote_jdk11_linux_aarch64", - sha256 = "a452f1b9682d9f83c1c14e54d1446e1c51b5173a3a05dcb013d380f9508562e4", +def remote_jdk15_repos(): + """Imports OpenJDK 15 repositories.""" + remote_java_repository( + name = "remotejdk15_linux", exec_compatible_with = [ "@platforms//os:linux", - "@platforms//cpu:aarch64", + "@platforms//cpu:x86_64", ], - strip_prefix = "zulu11.37.48-ca-jdk11.0.6-linux_aarch64", + sha256 = "0a38f1138c15a4f243b75eb82f8ef40855afcc402e3c2a6de97ce8235011b1ad", + strip_prefix = "zulu15.27.17-ca-jdk15.0.0-linux_x64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu11.37.48-ca-jdk11.0.6/zulu11.37.48-ca-jdk11.0.6-linux_aarch64.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-linux_x64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-linux_x64.tar.gz", ], - version = "11", + version = "15", ) - maybe( - remote_java_repository, - name = "remote_jdk11_linux", - sha256 = "360626cc19063bc411bfed2914301b908a8f77a7919aaea007a977fa8fb3cde1", + + remote_java_repository( + name = "remotejdk15_macos", exec_compatible_with = [ - "@platforms//os:linux", + "@platforms//os:macos", "@platforms//cpu:x86_64", ], + sha256 = "f80b2e0512d9d8a92be24497334c974bfecc8c898fc215ce0e76594f00437482", + strip_prefix = "zulu15.27.17-ca-jdk15.0.0-macosx_x64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-linux_x64.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-macosx_x64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-macosx_x64.tar.gz", ], - version = "11", + version = "15", ) - maybe( - remote_java_repository, - name = "remote_jdk11_macos", - sha256 = "e1fe56769f32e2aaac95e0a8f86b5a323da5af3a3b4bba73f3086391a6cc056f", + + remote_java_repository( + name = "remotejdk15_macos_aarch64", exec_compatible_with = [ "@platforms//os:macos", - "@platforms//cpu:x86_64", + "@platforms//cpu:aarch64", ], - strip_prefix = "zulu11.37.17-ca-jdk11.0.6-macosx_x64", + sha256 = "2613c3f15eef6b6ecd0fd102da92282b985e4573905dc902f1783d8059c1efc5", + strip_prefix = "zulu15.29.15-ca-jdk15.0.2-macosx_aarch64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-macosx_x64.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu15.29.15-ca-jdk15.0.2-macosx_aarch64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu15.29.15-ca-jdk15.0.2-macosx_aarch64.tar.gz", ], - version = "11", + version = "15", ) - maybe( - remote_java_repository, - name = "remote_jdk11_windows", - sha256 = "a9695617b8374bfa171f166951214965b1d1d08f43218db9a2a780b71c665c18", + + remote_java_repository( + name = "remotejdk15_win", exec_compatible_with = [ "@platforms//os:windows", "@platforms//cpu:x86_64", ], - strip_prefix = "zulu11.37.17-ca-jdk11.0.6-win_x64", + sha256 = "f535a530151e6c20de8a3078057e332b08887cb3ba1a4735717357e72765cad6", + strip_prefix = "zulu15.27.17-ca-jdk15.0.0-win_x64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu11.37.17-ca-jdk11.0.6/zulu11.37.17-ca-jdk11.0.6-win_x64.zip", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-win_x64.zip", + "https://cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-win_x64.zip", ], - version = "11", + version = "15", ) - maybe( - remote_java_repository, - name = "remote_jdk11_linux_ppc64le", +def remote_jdk16_repos(): + """Imports OpenJDK 16 repositories.""" + remote_java_repository( + name = "remotejdk16_linux", exec_compatible_with = [ "@platforms//os:linux", - "@platforms//cpu:ppc", - ], - sha256 = "a417db0295b1f4b538ecbaf7c774f3a177fab9657a665940170936c0eca4e71a", - strip_prefix = "jdk-11.0.7+10", - urls = [ - "https://mirror.bazel.build/openjdk/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.7+10/OpenJDK11U-jdk_ppc64le_linux_hotspot_11.0.7_10.tar.gz", - "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.7+10/OpenJDK11U-jdk_ppc64le_linux_hotspot_11.0.7_10.tar.gz", - ], - version = "11", - ) - maybe( - remote_java_repository, - name = "remote_jdk11_linux_s390x", - exec_compatible_with = [ - "@platforms//os:linux", - "@platforms//cpu:s390x", + "@platforms//cpu:x86_64", ], - sha256 = "d9b72e87a1d3ebc0c9552f72ae5eb150fffc0298a7cb841f1ce7bfc70dcd1059", - strip_prefix = "jdk-11.0.7+10", + sha256 = "236b5ea97aff3cb312e743848d7efa77faf305170e41371a732ca93c1b797665", + strip_prefix = "zulu16.28.11-ca-jdk16.0.0-linux_x64", urls = [ - "https://mirror.bazel.build/github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.7+10/OpenJDK11U-jdk_s390x_linux_hotspot_11.0.7_10.tar.gz", - "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.7+10/OpenJDK11U-jdk_s390x_linux_hotspot_11.0.7_10.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu16.28.11-ca-jdk16.0.0-linux_x64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu16.28.11-ca-jdk16.0.0-linux_x64.tar.gz", ], - version = "11", + version = "16", ) -def remote_jdk12_repos(): - """Imports OpenJDK 12 repositories. - - The source-code for this OpenJDK can be found at: - https://mirror.bazel.build/openjdk/azul-zulu12.2.3-ca-jdk12.0.1/zsrc12.2.3-jdk12.0.1.zip - """ - maybe( - remote_java_repository, - name = "remote_jdk12_linux", + remote_java_repository( + name = "remotejdk16_macos", exec_compatible_with = [ - "@platforms//os:linux", + "@platforms//os:macos", "@platforms//cpu:x86_64", ], - sha256 = "529c99841d69e11a85aea967ccfb9d0fd40b98c5b68dbe1d059002655e0a9c13", - strip_prefix = "zulu12.2.3-ca-jdk12.0.1-linux_x64", + sha256 = "6d47ef22dc56ce1f5a102ed39e21d9a97320f0bb786818e2c686393109d79bc5", + strip_prefix = "zulu16.28.11-ca-jdk16.0.0-macosx_x64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu12.2.3-ca-jdk12.0.1/zulu12.2.3-ca-jdk12.0.1-linux_x64.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu16.28.11-ca-jdk16.0.0-macosx_x64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu16.28.11-ca-jdk16.0.0-macosx_x64.tar.gz", ], - version = "12", + version = "16", ) - maybe( - remote_java_repository, - name = "remote_jdk12_macos", + + remote_java_repository( + name = "remotejdk16_macos_aarch64", exec_compatible_with = [ "@platforms//os:macos", - "@platforms//cpu:x86_64", + "@platforms//cpu:aarch64", ], - sha256 = "67ca9d285056132ebb19fa237a14affda52132142e1171fe1c20e18974b3b8a5", - strip_prefix = "zulu12.2.3-ca-jdk12.0.1-macosx_x64", + sha256 = "c92131e83bc71474850e667bc4e05fca33662b8feb009a0547aa14e76b40e890", + strip_prefix = "zulu16.28.11-ca-jdk16.0.0-macosx_aarch64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu12.2.3-ca-jdk12.0.1/zulu12.2.3-ca-jdk12.0.1-macosx_x64.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu16.28.11-ca-jdk16.0.0-macosx_aarch64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu16.28.11-ca-jdk16.0.0-macosx_aarch64.tar.gz", ], - version = "12", + version = "16", ) - maybe( - remote_java_repository, - name = "remote_jdk12_windows", + + remote_java_repository( + name = "remotejdk16_win", exec_compatible_with = [ "@platforms//os:windows", "@platforms//cpu:x86_64", ], - sha256 = "cf28404c23c3aa1115363ba6e796c30580a768e1d7d6681a7d053e516008e00d", - strip_prefix = "zulu12.2.3-ca-jdk12.0.1-win_x64", + sha256 = "6cbf98ada27476526a5f6dff79fd5f2c15e2f671818e503bdf741eb6c8fed3d4", + strip_prefix = "zulu16.28.11-ca-jdk16.0.0-win_x64", urls = [ - "https://mirror.bazel.build/openjdk/azul-zulu12.2.3-ca-jdk12.0.1/zulu12.2.3-ca-jdk12.0.1-win_x64.zip", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu16.28.11-ca-jdk16.0.0-win_x64.zip", + "https://cdn.azul.com/zulu/bin/zulu16.28.11-ca-jdk16.0.0-win_x64.zip", ], - version = "12", + version = "16", ) -def remote_jdk14_repos(): - """Imports OpenJDK 14 repositories.""" - maybe( - remote_java_repository, - name = "remote_jdk14_linux", +def remote_jdk17_repos(): + """Imports OpenJDK 17 repositories.""" + remote_java_repository( + name = "remotejdk17_linux", exec_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64", ], - sha256 = "48bb8947034cd079ad1ef83335e7634db4b12a26743a0dc314b6b861480777aa", - strip_prefix = "zulu14.28.21-ca-jdk14.0.1-linux_x64", + sha256 = "37c4f8e48536cceae8c6c20250d6c385e176972532fd35759fa7d6015c965f56", + strip_prefix = "zulu17.28.13-ca-jdk17.0.0-linux_x64", urls = [ - "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-linux_x64.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.28.13-ca-jdk17.0.0-linux_x64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu17.28.13-ca-jdk17.0.0-linux_x64.tar.gz", ], - version = "14", - ) - maybe( - remote_java_repository, - name = "remote_jdk14_macos", - exec_compatible_with = [ - "@platforms//os:macos", - "@platforms//cpu:x86_64", - ], - sha256 = "088bd4d0890acc9f032b738283bf0f26b2a55c50b02d1c8a12c451d8ddf080dd", - strip_prefix = "zulu14.28.21-ca-jdk14.0.1-macosx_x64", - urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-macosx_x64.tar.gz"], - version = "14", - ) - maybe( - remote_java_repository, - name = "remote_jdk14_windows", - exec_compatible_with = [ - "@platforms//os:windows", - "@platforms//cpu:x86_64", - ], - sha256 = "9cb078b5026a900d61239c866161f0d9558ec759aa15c5b4c7e905370e868284", - strip_prefix = "zulu14.28.21-ca-jdk14.0.1-win_x64", - urls = ["https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu14.28.21-ca-jdk14.0.1-win_x64.zip"], - version = "14", + version = "17", ) -def remote_jdk15_repos(): - """Imports OpenJDK 15 repositories.""" - maybe( - remote_java_repository, - name = "remote_jdk15_linux", + remote_java_repository( + name = "remotejdk17_macos", exec_compatible_with = [ - "@platforms//os:linux", + "@platforms//os:macos", "@platforms//cpu:x86_64", ], - sha256 = "0a38f1138c15a4f243b75eb82f8ef40855afcc402e3c2a6de97ce8235011b1ad", - strip_prefix = "zulu15.27.17-ca-jdk15.0.0-linux_x64", + sha256 = "6029b1fe6853cecad22ab99ac0b3bb4fb8c903dd2edefa91c3abc89755bbd47d", + strip_prefix = "zulu17.28.13-ca-jdk17.0.0-macosx_x64", urls = [ - "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-linux_x64.tar.gz", - "https://cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-linux_x64.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.28.13-ca-jdk17.0.0-macosx_x64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu17.28.13-ca-jdk17.0.0-macosx_x64.tar.gz", ], - version = "15", + version = "17", ) - maybe( - remote_java_repository, - name = "remote_jdk15_macos", + + remote_java_repository( + name = "remotejdk17_macos_aarch64", exec_compatible_with = [ "@platforms//os:macos", - "@platforms//cpu:x86_64", + "@platforms//cpu:aarch64", ], - sha256 = "f80b2e0512d9d8a92be24497334c974bfecc8c898fc215ce0e76594f00437482", - strip_prefix = "zulu15.27.17-ca-jdk15.0.0-macosx_x64", + sha256 = "6b17f01f767ee7abf4704149ca4d86423aab9b16b68697b7d36e9b616846a8b0", + strip_prefix = "zulu17.28.13-ca-jdk17.0.0-macosx_aarch64", urls = [ - "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-macosx_x64.tar.gz", - "https://cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-macosx_x64.tar.gz", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.28.13-ca-jdk17.0.0-macosx_aarch64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu17.28.13-ca-jdk17.0.0-macosx_aarch64.tar.gz", ], - version = "15", + version = "17", ) - maybe( - remote_java_repository, - name = "remote_jdk15_windows", + + remote_java_repository( + name = "remotejdk17_win", exec_compatible_with = [ "@platforms//os:windows", "@platforms//cpu:x86_64", ], - sha256 = "f535a530151e6c20de8a3078057e332b08887cb3ba1a4735717357e72765cad6", - strip_prefix = "zulu15.27.17-ca-jdk15.0.0-win_x64", + sha256 = "f4437011239f3f0031c794bb91c02a6350bc941d4196bdd19c9f157b491815a3", + strip_prefix = "zulu17.28.13-ca-jdk17.0.0-win_x64", urls = [ - "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-win_x64.zip", - "https://cdn.azul.com/zulu/bin/zulu15.27.17-ca-jdk15.0.0-win_x64.zip", + "https://mirror.bazel.build/cdn.azul.com/zulu/bin/zulu17.28.13-ca-jdk17.0.0-win_x64.zip", + "https://cdn.azul.com/zulu/bin/zulu17.28.13-ca-jdk17.0.0-win_x64.zip", ], - version = "15", + version = "17", ) def bazel_skylib(): From 71621f38444d102ceec00a509672684fa0aff196 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 10 Nov 2021 13:16:51 +0100 Subject: [PATCH 02/15] Copy over files from @bazel_tools//tools/jdk --- toolchains/BUILD | 505 ++++++++++++++++++++++++++ toolchains/DumpPlatformClassPath.java | 259 +++++++++++++ toolchains/default_java_toolchain.bzl | 293 +++++++++++++++ toolchains/fail_rule.bzl | 35 ++ toolchains/java_toolchain_alias.bzl | 112 ++++++ toolchains/jdk.BUILD | 218 +++++++++++ toolchains/local_java_repository.bzl | 232 ++++++++++++ 7 files changed, 1654 insertions(+) create mode 100644 toolchains/DumpPlatformClassPath.java create mode 100644 toolchains/default_java_toolchain.bzl create mode 100644 toolchains/fail_rule.bzl create mode 100644 toolchains/java_toolchain_alias.bzl create mode 100644 toolchains/jdk.BUILD create mode 100644 toolchains/local_java_repository.bzl diff --git a/toolchains/BUILD b/toolchains/BUILD index 67d2c89..6f7fef4 100644 --- a/toolchains/BUILD +++ b/toolchains/BUILD @@ -1,3 +1,21 @@ +load( + "@bazel_tools//tools/jdk:default_java_toolchain.bzl", + "DEFAULT_TOOLCHAIN_CONFIGURATION", + "JVM8_TOOLCHAIN_CONFIGURATION", + "PREBUILT_TOOLCHAIN_CONFIGURATION", + "VANILLA_TOOLCHAIN_CONFIGURATION", + "bootclasspath", + "default_java_toolchain", + "java_runtime_files", +) +load( + "//tools/jdk:java_toolchain_alias.bzl", + "java_host_runtime_alias", + "java_runtime_alias", + "java_runtime_version_alias", + "java_toolchain_alias", +) + package(default_visibility = ["//visibility:public"]) licenses(["notice"]) @@ -7,3 +25,490 @@ filegroup( srcs = glob(["**"]), visibility = ["@//:__pkg__"], ) + + +load("//tools/python:private/defs.bzl", "py_binary", "py_test") + +package(default_visibility = ["//visibility:public"]) + +# Used to distinguish toolchains used for Java development, ie the JavaToolchainProvider. +toolchain_type(name = "toolchain_type") + +# Used to distinguish toolchains used for Java execution, ie the JavaRuntimeInfo. +toolchain_type(name = "runtime_toolchain_type") + +# Points to toolchain[":runtime_toolchain_type"] (was :legacy_current_java_runtime) +java_runtime_alias(name = "current_java_runtime") + +# Host configuration of ":current_java_runtime" +java_host_runtime_alias(name = "current_host_java_runtime") + +# Points to toolchain[":toolchain_type"] (was :legacy_current_java_toolchain) +java_toolchain_alias(name = "current_java_toolchain") + +# Aliases value of --plugins flag as a JavaPluginInfo +java_plugins_flag_alias( + name = "java_plugins_flag_alias", +) + +# This is necessary to get the *host* Java runtime. Depending on +# //tools/jdk:current_java_runtime from an attribute with the host transition +# does not work because the dependency is determined based on the configuration +# *before* the transition. +alias( + name = "java_runtime_alias", + actual = "//tools/jdk:current_java_runtime", +) + +# These individual jni_* targets are exposed for legacy reasons. +# Most users should depend on :jni. + +java_runtime_files( + name = "jni_header", + srcs = ["include/jni.h"], +) + +java_runtime_files( + name = "jni_md_header-darwin", + srcs = ["include/darwin/jni_md.h"], +) + +java_runtime_files( + name = "jni_md_header-linux", + srcs = ["include/linux/jni_md.h"], +) + +java_runtime_files( + name = "jni_md_header-windows", + srcs = ["include/win32/jni_md.h"], +) + +java_runtime_files( + name = "jni_md_header-freebsd", + srcs = ["include/freebsd/jni_md.h"], +) + +java_runtime_files( + name = "jni_md_header-openbsd", + srcs = ["include/openbsd/jni_md.h"], +) + +# The Java native interface. Depend on this package if you #include . +# +# See test_jni in third_party/bazel/src/test/shell/bazel/bazel_java_test.sh for +# an example of using Bazel to build a Java program that calls a C function. +# +# TODO(ilist): use //src:condition:linux when released in Bazel +cc_library( + name = "jni", + hdrs = [":jni_header"] + select({ + "//src/conditions:linux_aarch64": [":jni_md_header-linux"], + "//src/conditions:linux_ppc64le": [":jni_md_header-linux"], + "//src/conditions:linux_s390x": [":jni_md_header-linux"], + "//src/conditions:linux_mips64": [":jni_md_header-linux"], + "//src/conditions:linux_riscv64": [":jni_md_header-linux"], + "//src/conditions:linux_x86_64": [":jni_md_header-linux"], + "//src/conditions:darwin": [":jni_md_header-darwin"], + "//src/conditions:freebsd": [":jni_md_header-freebsd"], + "//src/conditions:openbsd": [":jni_md_header-openbsd"], + "//src/conditions:windows": [":jni_md_header-windows"], + "//conditions:default": [], + }), + includes = ["include"] + select({ + "//src/conditions:linux_aarch64": ["include/linux"], + "//src/conditions:linux_ppc64le": ["include/linux"], + "//src/conditions:linux_s390x": ["include/linux"], + "//src/conditions:linux_mips64": [":include/linux"], + "//src/conditions:linux_riscv64": [":include/linux"], + "//src/conditions:linux_x86_64": ["include/linux"], + "//src/conditions:darwin": ["include/darwin"], + "//src/conditions:freebsd": ["include/freebsd"], + "//src/conditions:openbsd": ["include/openbsd"], + "//src/conditions:windows": ["include/win32"], + "//conditions:default": [], + }), +) + +alias( + name = "java", + actual = "@local_jdk//:java", +) + +alias( + name = "jar", + actual = "@local_jdk//:jar", +) + +alias( + name = "javac", + actual = "@local_jdk//:javac", +) + +alias( + name = "javadoc", + actual = "@local_jdk//:javadoc", +) + +[ + ( + alias( + name = "ijar_prebuilt_binary_%s" % OS, + actual = "@remote_java_tools_%s//:ijar_prebuilt_binary" % OS, + visibility = ["//visibility:private"], + ), + alias( + name = "prebuilt_singlejar_%s" % OS, + actual = "@remote_java_tools_%s//:prebuilt_singlejar" % OS, + visibility = ["//visibility:private"], + ), + ) + for OS in [ + "linux", + "darwin", + "windows", + ] +] + +# On Windows, executables end in ".exe", but the label we reach it through +# must be platform-independent. Thus, we create a little filegroup that +# contains the appropriate platform-dependent file. +alias( + name = "ijar", + actual = ":ijar_prebuilt_binary_or_cc_binary", +) + +alias( + name = "ijar_prebuilt_binary_or_cc_binary", + actual = select({ + "//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", + "//src/conditions:darwin": ":ijar_prebuilt_binary_darwin", + "//src/conditions:windows": ":ijar_prebuilt_binary_windows", + "//conditions:default": "@remote_java_tools//:ijar_cc_binary", + }), +) + +alias( + name = "ijar_prebuilt_binary", + actual = select({ + "//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", + "//src/conditions:darwin": ":ijar_prebuilt_binary_darwin", + "//src/conditions:windows": ":ijar_prebuilt_binary_windows", + }), +) + +# On Windows, Java implementation of singlejar is used. We create a little +# filegroup that contains the appropriate platform-dependent file. +# Once https://github.com/bazelbuild/bazel/issues/2241 is fixed (that is, +# the native singlejar is used on windows), this file group can be reused since +# on Windows, executables end in ".exe", but the label we reach it through +# must be platform-independent. +alias( + name = "singlejar", + actual = ":singlejar_prebuilt_or_cc_binary", +) + +alias( + name = "singlejar_prebuilt_or_cc_binary", + actual = select({ + "//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", + "//src/conditions:darwin": ":prebuilt_singlejar_darwin", + "//src/conditions:windows": ":prebuilt_singlejar_windows", + "//conditions:default": "@remote_java_tools//:singlejar_cc_bin", + }), +) + +alias( + name = "prebuilt_singlejar", + actual = select({ + "//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", + "//src/conditions:darwin": ":prebuilt_singlejar_darwin", + "//src/conditions:windows": ":prebuilt_singlejar_windows", + }), +) + +exports_files(["BUILD.java_tools"]) + +alias( + name = "genclass", + actual = "@remote_java_tools//:GenClass", +) + +alias( + name = "GenClass_deploy.jar", + actual = "@remote_java_tools//:GenClass", +) + +alias( + name = "turbine", + actual = "@remote_java_tools//:Turbine", +) + +alias( + name = "turbine_deploy.jar", + actual = "@remote_java_tools//:Turbine", +) + +alias( + name = "turbine_direct", + actual = "@remote_java_tools//:TurbineDirect", +) + +alias( + name = "turbine_direct_binary_deploy.jar", + actual = "@remote_java_tools//:TurbineDirect", +) + +alias( + name = "javabuilder", + actual = "@remote_java_tools//:JavaBuilder", +) + +alias( + name = "JavaBuilder_deploy.jar", + actual = "@remote_java_tools//:JavaBuilder", +) + +alias( + name = "vanillajavabuilder", + actual = "@remote_java_tools//:VanillaJavaBuilder", +) + +alias( + name = "javac_jar", + actual = "@remote_java_tools//:javac_jar", +) + +alias( + name = "jdk_compiler_jar", + actual = "@remote_java_tools//:jdk_compiler_jar", +) + +alias( + name = "java_compiler_jar", + actual = "@remote_java_tools//:java_compiler_jar", +) + +alias( + name = "JacocoCoverageRunner", + actual = "@remote_java_tools//:jacoco_coverage_runner", +) + +alias( + name = "JacocoCoverage", + actual = "@remote_java_tools//:jacoco_coverage_runner", +) + +java_import( + name = "TestRunner", + jars = ["@remote_java_tools//:Runner"], +) + +alias( + name = "TestRunner_deploy.jar", + actual = "@remote_java_tools//:Runner", +) + +alias( + name = "proguard", + actual = "@remote_java_tools//:proguard", +) + +BOOTCLASS_JARS = [ + "rt.jar", + "resources.jar", + "jsse.jar", + "jce.jar", + "charsets.jar", +] + +# TODO(cushon): this isn't compatible with JDK 9 +alias( + name = "bootclasspath", + actual = "@local_jdk//:bootclasspath", +) + +alias( + name = "jre", + actual = "@local_jdk//:jre", +) + +alias( + name = "jdk", + actual = "@local_jdk//:jdk", +) + +alias( + name = "host_jdk", + actual = ":remote_jdk11", +) + +bootclasspath( + name = "platformclasspath", + src = "DumpPlatformClassPath.java", + host_javabase = "current_java_runtime", + target_javabase = "current_java_runtime", +) + +default_java_toolchain( + name = "toolchain", + configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, + toolchain_definition = False, +) + +alias( + name = "remote_toolchain", + actual = ":toolchain", +) + +RELEASES = (8, 9, 10, 11) + +[ + default_java_toolchain( + name = "toolchain_java%d" % release, + configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, + source_version = "%s" % release, + target_version = "%s" % release, + ) + for release in RELEASES +] + +# A toolchain that targets java 14. +default_java_toolchain( + name = "toolchain_jdk_14", + configuration = dict(), + java_runtime = "@bazel_tools//tools/jdk:remotejdk_14", + source_version = "14", + target_version = "14", +) + +# A toolchain that targets java 15. +default_java_toolchain( + name = "toolchain_jdk_15", + configuration = dict(), + java_runtime = "@bazel_tools//tools/jdk:remotejdk_15", + source_version = "15", + target_version = "15", +) + +# A toolchain that targets java 16. +default_java_toolchain( + name = "toolchain_jdk_16", + configuration = dict(), + java_runtime = "@bazel_tools//tools/jdk:remotejdk_16", + source_version = "16", + target_version = "16", +) + +# A toolchain that targets java 17. +default_java_toolchain( + name = "toolchain_jdk_17", + configuration = dict(), + java_runtime = "@bazel_tools//tools/jdk:remotejdk_17", + source_version = "17", + target_version = "17", +) + +# Deprecated, do not use. +# It will be removed after migration to Java toolchain resolution. +default_java_toolchain( + name = "toolchain_hostjdk8", + configuration = JVM8_TOOLCHAIN_CONFIGURATION, + java_runtime = ":current_host_java_runtime", + source_version = "8", + target_version = "8", + toolchain_definition = False, +) + +default_java_toolchain( + name = "prebuilt_toolchain", + configuration = PREBUILT_TOOLCHAIN_CONFIGURATION, + toolchain_definition = False, +) + +filegroup( + name = "bzl_srcs", + srcs = glob(["*.bzl"]), + visibility = ["//tools:__pkg__"], +) + +py_binary( + name = "proguard_whitelister", + srcs = [ + "proguard_whitelister.py", + ], + deps = [ + "//third_party/py/abseil", + ], +) + +py_test( + name = "proguard_whitelister_test", + srcs = ["proguard_whitelister_test.py"], + data = ["proguard_whitelister_test_input.pgcfg"], + deps = [ + ":proguard_whitelister", + ], +) + +# Aliases for JDKs, so that they are only downloaded when needed. +_JDKS = [ + "remotejdk11_macos", + "remotejdk11_macos_aarch64", + "remotejdk11_win", + "remotejdk11_linux_aarch64", + "remotejdk11_linux", + "remotejdk11_linux_ppc64le", + "remotejdk11_linux_s390x", + "remotejdk15_macos", + "remotejdk15_macos_aarch64", + "remotejdk15_win", + "remotejdk15_linux", + "remotejdk16_macos", + "remotejdk16_macos_aarch64", + "remotejdk16_win", + "remotejdk16_linux", + "remotejdk17_macos", + "remotejdk17_macos_aarch64", + "remotejdk17_win", + "remotejdk17_linux", +] + +[ + alias( + name = JDK, + actual = "@%s//:jdk" % JDK, + visibility = ["//visibility:private"], + ) + for JDK in _JDKS +] + +# A JDK 11 for use as a --host_javabase. +java_runtime_version_alias( + name = "remote_jdk11", + runtime_version = "remotejdk_11", + visibility = ["//visibility:public"], +) + +java_runtime_version_alias( + name = "remotejdk_15", + runtime_version = "remotejdk_15", + visibility = ["//visibility:public"], +) + +java_runtime_version_alias( + name = "remotejdk_16", + runtime_version = "remotejdk_16", + visibility = ["//visibility:public"], +) + +java_runtime_version_alias( + name = "remotejdk_17", + runtime_version = "remotejdk_17", + visibility = ["//visibility:public"], +) + +java_runtime_version_alias( + name = "jdk_8", + runtime_version = "8", + visibility = ["//visibility:public"], +) diff --git a/toolchains/DumpPlatformClassPath.java b/toolchains/DumpPlatformClassPath.java new file mode 100644 index 0000000..e23ca53 --- /dev/null +++ b/toolchains/DumpPlatformClassPath.java @@ -0,0 +1,259 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.sun.tools.javac.api.JavacTool; +import com.sun.tools.javac.util.Context; +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UncheckedIOException; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.EnumSet; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; +import java.util.zip.CRC32; +import java.util.zip.ZipEntry; +import javax.tools.JavaFileManager; +import javax.tools.JavaFileObject; +import javax.tools.JavaFileObject.Kind; +import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; + +/** + * Output a jar file containing all classes on the platform classpath of the given JDK release. + * + *

usage: DumpPlatformClassPath ? + */ +public class DumpPlatformClassPath { + + public static void main(String[] args) throws Exception { + if (args.length != 2) { + System.err.println("usage: DumpPlatformClassPath "); + System.exit(1); + } + Path output = Paths.get(args[0]); + Path targetJavabase = Paths.get(args[1]); + + int hostMajorVersion = hostMajorVersion(); + boolean ok; + if (hostMajorVersion == 8) { + ok = dumpJDK8BootClassPath(output, targetJavabase); + } else { + ok = dumpJDK9AndNewerBootClassPath(hostMajorVersion, output, targetJavabase); + } + System.exit(ok ? 0 : 1); + } + + // JDK 8 bootclasspath handling. + // * JDK 8 represents a bootclasspath as a search path of jars (rt.jar, etc.). + // * It does not support --release or --system. + static boolean dumpJDK8BootClassPath(Path output, Path targetJavabase) throws IOException { + List bootClassPathJars = getBootClassPathJars(targetJavabase); + writeClassPathJars(output, bootClassPathJars); + return true; + } + + // JDK > 8 --host_javabase bootclasspath handling. + // (The default --host_javabase is currently JDK 9.) + static boolean dumpJDK9AndNewerBootClassPath( + int hostMajorVersion, Path output, Path targetJavabase) throws IOException { + + // JDK 9 and newer support cross-compiling to older platform versions using the --system + // and --release flags. + // * --system takes the path to a JDK root for JDK 9 and up, and causes the compilation + // to target the APIs from that JDK. + // * --release takes a language level (e.g. '9') and uses the API information baked in to + // the host JDK (in lib/ct.sym). + + // Since --system only supports JDK >= 9, first check of the target JDK defines a JDK 8 + // bootclasspath. + List bootClassPathJars = getBootClassPathJars(targetJavabase); + if (!bootClassPathJars.isEmpty()) { + writeClassPathJars(output, bootClassPathJars); + return true; + } + + // Initialize a FileManager to process the --system argument, and then read the + // initialized bootclasspath data back out. + + Context context = new Context(); + JavacTool.create() + .getTask( + /* out = */ null, + /* fileManager = */ null, + /* diagnosticListener = */ null, + /* options = */ Arrays.asList("--system", String.valueOf(targetJavabase)), + /* classes = */ null, + /* compilationUnits = */ null, + context); + StandardJavaFileManager fileManager = + (StandardJavaFileManager) context.get(JavaFileManager.class); + + SortedMap entries = new TreeMap<>(); + for (JavaFileObject fileObject : + fileManager.list( + StandardLocation.PLATFORM_CLASS_PATH, + "", + EnumSet.of(Kind.CLASS), + /* recurse= */ true)) { + String binaryName = + fileManager.inferBinaryName(StandardLocation.PLATFORM_CLASS_PATH, fileObject); + entries.put(binaryName.replace('.', '/') + ".class", fileObject.openInputStream()); + } + writeEntries(output, entries); + return true; + } + + /** Writes the given entry names and data to a jar archive at the given path. */ + private static void writeEntries(Path output, Map entries) + throws IOException { + if (!entries.containsKey("java/lang/Object.class")) { + throw new AssertionError( + "\nCould not find java.lang.Object on bootclasspath; something has gone terribly wrong.\n" + + "Please file a bug: https://github.com/bazelbuild/bazel/issues"); + } + try (OutputStream os = Files.newOutputStream(output); + BufferedOutputStream bos = new BufferedOutputStream(os, 65536); + JarOutputStream jos = new JarOutputStream(bos)) { + entries.entrySet().stream() + .forEachOrdered( + entry -> { + try { + addEntry(jos, entry.getKey(), entry.getValue()); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } + } + + /** Collects the entries of the given jar files into a map from jar entry names to their data. */ + private static void writeClassPathJars(Path output, Collection paths) throws IOException { + List jars = new ArrayList<>(); + for (Path path : paths) { + jars.add(new JarFile(path.toFile())); + } + SortedMap entries = new TreeMap<>(); + for (JarFile jar : jars) { + jar.stream() + .filter(p -> p.getName().endsWith(".class")) + .forEachOrdered( + entry -> { + try { + entries.put(entry.getName(), jar.getInputStream(entry)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } + writeEntries(output, entries); + for (JarFile jar : jars) { + jar.close(); + } + } + + /** Returns paths to the entries of a JDK 8-style bootclasspath. */ + private static List getBootClassPathJars(Path javaHome) throws IOException { + List jars = new ArrayList<>(); + Path extDir = javaHome.resolve("jre/lib/ext"); + if (Files.exists(extDir)) { + for (Path extJar : Files.newDirectoryStream(extDir, "*.jar")) { + jars.add(extJar); + } + } + for (String jar : + Arrays.asList("rt.jar", "resources.jar", "jsse.jar", "jce.jar", "charsets.jar")) { + Path path = javaHome.resolve("jre/lib").resolve(jar); + if (Files.exists(path)) { + jars.add(path); + } + } + return jars; + } + + // Use a fixed timestamp for deterministic jar output. + private static final long FIXED_TIMESTAMP = + new GregorianCalendar(2010, 0, 1, 0, 0, 0).getTimeInMillis(); + + /** + * Add a jar entry to the given {@link JarOutputStream}, normalizing the entry timestamps to + * ensure deterministic build output. + */ + private static void addEntry(JarOutputStream jos, String name, InputStream input) + throws IOException { + JarEntry je = new JarEntry(name); + je.setTime(FIXED_TIMESTAMP); + je.setMethod(ZipEntry.STORED); + byte[] bytes = toByteArray(input); + // When targeting JDK >= 10, patch the major version so it will be accepted by javac 9 + // TODO(cushon): remove this after updating javac + if (bytes[7] > 53) { + bytes[7] = 53; + } + je.setSize(bytes.length); + CRC32 crc = new CRC32(); + crc.update(bytes); + je.setCrc(crc.getValue()); + jos.putNextEntry(je); + jos.write(bytes); + } + + private static byte[] toByteArray(InputStream is) throws IOException { + byte[] buffer = new byte[8192]; + ByteArrayOutputStream boas = new ByteArrayOutputStream(); + while (true) { + int r = is.read(buffer); + if (r == -1) { + break; + } + boas.write(buffer, 0, r); + } + return boas.toByteArray(); + } + + /** + * Returns the major version of the host Java runtime (e.g. '8' for JDK 8), using {@link + * Runtime#version} if it is available, and otherwise falling back to the {@code + * java.class.version} system. property. + */ + static int hostMajorVersion() { + try { + Method versionMethod = Runtime.class.getMethod("version"); + Object version = versionMethod.invoke(null); + return (int) version.getClass().getMethod("major").invoke(version); + } catch (ReflectiveOperationException e) { + // Runtime.version() isn't available on JDK 8; continue below + } + int version = (int) Double.parseDouble(System.getProperty("java.class.version")); + if (49 <= version && version <= 52) { + return version - (49 - 5); + } + throw new IllegalStateException( + "Unknown Java version: " + System.getProperty("java.specification.version")); + } +} diff --git a/toolchains/default_java_toolchain.bzl b/toolchains/default_java_toolchain.bzl new file mode 100644 index 0000000..ac8ed5d --- /dev/null +++ b/toolchains/default_java_toolchain.bzl @@ -0,0 +1,293 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Bazel rules for creating Java toolchains.""" + +JDK8_JVM_OPTS = [ + "-Xbootclasspath/p:$(location @remote_java_tools//:javac_jar)", +] + +# JVM options, without patching java.compiler and jdk.compiler modules. +BASE_JDK9_JVM_OPTS = [ + # Allow JavaBuilder to access internal javac APIs. + "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", + "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED", + "--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED", + "--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", + "--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED", + "--add-exports=jdk.compiler/com.sun.tools.javac.resources=ALL-UNNAMED", + "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", + "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", + "--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED", + "--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED", + "--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", + + # quiet warnings from com.google.protobuf.UnsafeUtil, + # see: https://github.com/google/protobuf/issues/3781 + # and: https://github.com/bazelbuild/bazel/issues/5599 + "--add-opens=java.base/java.nio=ALL-UNNAMED", + "--add-opens=java.base/java.lang=ALL-UNNAMED", +] + +JDK9_JVM_OPTS = BASE_JDK9_JVM_OPTS + [ + # override the javac in the JDK. + "--patch-module=java.compiler=$(location @remote_java_tools//:java_compiler_jar)", + "--patch-module=jdk.compiler=$(location @remote_java_tools//:jdk_compiler_jar)", +] + +DEFAULT_JAVACOPTS = [ + "-XDskipDuplicateBridges=true", + "-XDcompilePolicy=simple", + "-g", + "-parameters", + # https://github.com/bazelbuild/java_tools/issues/51#issuecomment-927940699 + "-XepOpt:ReturnValueIgnored:ObjectMethods=false", +] + +# java_toolchain parameters without specifying javac, java.compiler, +# jdk.compiler module, and jvm_opts +_BASE_TOOLCHAIN_CONFIGURATION = dict( + forcibly_disable_header_compilation = False, + genclass = ["@remote_java_tools//:GenClass"], + header_compiler = ["@remote_java_tools//:TurbineDirect"], + header_compiler_direct = ["@remote_java_tools//:TurbineDirect"], + ijar = ["@bazel_tools//tools/jdk:ijar"], + javabuilder = ["@remote_java_tools//:JavaBuilder"], + javac_supports_workers = True, + jacocorunner = "@remote_java_tools//:jacoco_coverage_runner_filegroup", + jvm_opts = BASE_JDK9_JVM_OPTS, + misc = DEFAULT_JAVACOPTS, + singlejar = ["@bazel_tools//tools/jdk:singlejar"], + # Code to enumerate target JVM boot classpath uses host JVM. Because + # java_runtime-s are involved, its implementation is in @bazel_tools. + bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"], + source_version = "8", + target_version = "8", + reduced_classpath_incompatible_processors = [ + "dagger.hilt.processor.internal.root.RootProcessor", # see b/21307381 + ], +) + +JVM8_TOOLCHAIN_CONFIGURATION = dict( + tools = ["@remote_java_tools//:javac_jar"], + jvm_opts = ["-Xbootclasspath/p:$(location @remote_java_tools//:javac_jar)"], + java_runtime = "@bazel_tools//tools/jdk:jdk_8", +) + +DEFAULT_TOOLCHAIN_CONFIGURATION = dict( + jvm_opts = [ + # Compact strings make JavaBuilder slightly slower. + "-XX:-CompactStrings", + ] + JDK9_JVM_OPTS, + turbine_jvm_opts = [ + # Turbine is not a worker and parallel GC is faster for short-lived programs. + "-XX:+UseParallelOldGC", + ], + tools = [ + "@remote_java_tools//:java_compiler_jar", + "@remote_java_tools//:jdk_compiler_jar", + ], + java_runtime = "@bazel_tools//tools/jdk:remote_jdk11", +) + +# The 'vanilla' toolchain is an unsupported alternative to the default. +# +# It does not provide any of the following features: +# * Error Prone +# * Strict Java Deps +# * Reduced Classpath Optimization +# +# It uses the version of internal javac from the `--host_javabase` JDK instead +# of providing a javac. Internal javac may not be source- or bug-compatible with +# the javac that is provided with other toolchains. +# +# However it does allow using a wider range of `--host_javabase`s, including +# versions newer than the current JDK. +VANILLA_TOOLCHAIN_CONFIGURATION = dict( + javabuilder = ["@remote_java_tools//:VanillaJavaBuilder"], + jvm_opts = [], +) + +# The new toolchain is using all the pre-built tools, including +# singlejar and ijar, even on remote execution. This toolchain +# should be used only when host and execution platform are the +# same, otherwise the binaries will not work on the execution +# platform. +PREBUILT_TOOLCHAIN_CONFIGURATION = dict( + jvm_opts = [ + # Compact strings make JavaBuilder slightly slower. + "-XX:-CompactStrings", + ] + JDK9_JVM_OPTS, + turbine_jvm_opts = [ + # Turbine is not a worker and parallel GC is faster for short-lived programs. + "-XX:+UseParallelOldGC", + ], + tools = [ + "@remote_java_tools//:java_compiler_jar", + "@remote_java_tools//:jdk_compiler_jar", + ], + ijar = ["@bazel_tools//tools/jdk:ijar_prebuilt_binary"], + singlejar = ["@bazel_tools//tools/jdk:prebuilt_singlejar"], + java_runtime = "@bazel_tools//tools/jdk:remote_jdk11", +) + +# The new toolchain is using all the tools from sources. +NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict( + jvm_opts = [ + # Compact strings make JavaBuilder slightly slower. + "-XX:-CompactStrings", + ] + JDK9_JVM_OPTS, + turbine_jvm_opts = [ + # Turbine is not a worker and parallel GC is faster for short-lived programs. + "-XX:+UseParallelOldGC", + ], + tools = [ + "@remote_java_tools//:java_compiler_jar", + "@remote_java_tools//:jdk_compiler_jar", + ], + ijar = ["@remote_java_tools//:ijar_cc_binary"], + singlejar = ["@remote_java_tools//:singlejar_cc_bin"], + java_runtime = "@bazel_tools//tools/jdk:remote_jdk11", +) + +def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, **kwargs): + """Defines a remote java_toolchain with appropriate defaults for Bazel.""" + + toolchain_args = dict(_BASE_TOOLCHAIN_CONFIGURATION) + toolchain_args.update(configuration) + toolchain_args.update(kwargs) + native.java_toolchain( + name = name, + **toolchain_args + ) + if toolchain_definition: + native.config_setting( + name = name + "_version_setting", + values = {"java_language_version": toolchain_args["source_version"]}, + visibility = ["//visibility:private"], + ) + native.toolchain( + name = name + "_definition", + toolchain_type = "@bazel_tools//tools/jdk:toolchain_type", + target_settings = [name + "_version_setting"], + toolchain = name, + ) + +def java_runtime_files(name, srcs): + """Copies the given sources out of the current Java runtime.""" + + native.filegroup( + name = name, + srcs = srcs, + ) + for src in srcs: + native.genrule( + name = "gen_%s" % src, + srcs = ["@bazel_tools//tools/jdk:current_java_runtime"], + toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"], + cmd = "cp $(JAVABASE)/%s $@" % src, + outs = [src], + ) + +def _bootclasspath_impl(ctx): + host_javabase = ctx.attr.host_javabase[java_common.JavaRuntimeInfo] + + # explicitly list output files instead of using TreeArtifact to work around + # https://github.com/bazelbuild/bazel/issues/6203 + classes = [ + "DumpPlatformClassPath.class", + ] + + class_outputs = [ + ctx.actions.declare_file("%s_classes/%s" % (ctx.label.name, clazz)) + for clazz in classes + ] + + args = ctx.actions.args() + args.add("-source") + args.add("8") + args.add("-target") + args.add("8") + args.add("-Xlint:-options") + args.add("-cp") + args.add("%s/lib/tools.jar" % host_javabase.java_home) + args.add("-d") + args.add(class_outputs[0].dirname) + args.add(ctx.file.src) + + ctx.actions.run( + executable = "%s/bin/javac" % host_javabase.java_home, + mnemonic = "JavaToolchainCompileClasses", + inputs = [ctx.file.src] + ctx.files.host_javabase, + outputs = class_outputs, + arguments = [args], + ) + + bootclasspath = ctx.outputs.output_jar + + inputs = class_outputs + ctx.files.host_javabase + + args = ctx.actions.args() + args.add("-XX:+IgnoreUnrecognizedVMOptions") + args.add("--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED") + args.add("--add-exports=jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED") + args.add("--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED") + args.add_joined( + "-cp", + [class_outputs[0].dirname, "%s/lib/tools.jar" % host_javabase.java_home], + join_with = ctx.configuration.host_path_separator, + ) + args.add("DumpPlatformClassPath") + args.add(bootclasspath) + + if ctx.attr.target_javabase: + inputs.extend(ctx.files.target_javabase) + args.add(ctx.attr.target_javabase[java_common.JavaRuntimeInfo].java_home) + + ctx.actions.run( + executable = str(host_javabase.java_executable_exec_path), + mnemonic = "JavaToolchainCompileBootClasspath", + inputs = inputs, + outputs = [bootclasspath], + arguments = [args], + ) + return [ + DefaultInfo(files = depset([bootclasspath])), + OutputGroupInfo(jar = [bootclasspath]), + ] + +_bootclasspath = rule( + implementation = _bootclasspath_impl, + attrs = { + "host_javabase": attr.label( + cfg = "host", + providers = [java_common.JavaRuntimeInfo], + ), + "src": attr.label( + cfg = "host", + allow_single_file = True, + ), + "target_javabase": attr.label( + providers = [java_common.JavaRuntimeInfo], + ), + "output_jar": attr.output(mandatory = True), + }, +) + +def bootclasspath(name, **kwargs): + _bootclasspath( + name = name, + output_jar = name + ".jar", + **kwargs + ) diff --git a/toolchains/fail_rule.bzl b/toolchains/fail_rule.bzl new file mode 100644 index 0000000..a8a3da2 --- /dev/null +++ b/toolchains/fail_rule.bzl @@ -0,0 +1,35 @@ +# Copyright 2020 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""A rule than fails during analysis.""" + +def _fail_rule_impl(ctx): + if ctx.attr.header: + fail("%s %s" % (ctx.attr.header, ctx.attr.message)) + else: + fail(ctx.attr.message) + +fail_rule = rule( + doc = "A rule that fails during analysis.", + implementation = _fail_rule_impl, + attrs = { + "header": attr.string( + doc = "Header of the message.", + ), + "message": attr.string( + mandatory = True, + doc = "Message to display.", + ), + }, +) diff --git a/toolchains/java_toolchain_alias.bzl b/toolchains/java_toolchain_alias.bzl new file mode 100644 index 0000000..ddf6d3f --- /dev/null +++ b/toolchains/java_toolchain_alias.bzl @@ -0,0 +1,112 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Experimental re-implementations of Java toolchain aliases using toolchain resolution.""" + +def _java_runtime_alias(ctx): + """An experimental implementation of java_runtime_alias using toolchain resolution.""" + toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"] + toolchain = toolchain_info.java_runtime + return [ + toolchain_info, + toolchain, + platform_common.TemplateVariableInfo({ + "JAVA": str(toolchain.java_executable_exec_path), + "JAVABASE": str(toolchain.java_home), + }), + # See b/65239471 for related discussion of handling toolchain runfiles/data. + DefaultInfo( + runfiles = ctx.runfiles(transitive_files = toolchain.files), + files = toolchain.files, + ), + ] + +java_runtime_alias = rule( + implementation = _java_runtime_alias, + toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"], + incompatible_use_toolchain_transition = True, +) + +def _java_host_runtime_alias(ctx): + """An experimental implementation of java_host_runtime_alias using toolchain resolution.""" + runtime = ctx.attr._runtime + java_runtime = runtime[java_common.JavaRuntimeInfo] + template_variable_info = runtime[platform_common.TemplateVariableInfo] + toolchain_info = platform_common.ToolchainInfo(java_runtime = java_runtime) + return [ + java_runtime, + template_variable_info, + toolchain_info, + runtime[DefaultInfo], + ] + +java_host_runtime_alias = rule( + implementation = _java_host_runtime_alias, + attrs = { + "_runtime": attr.label( + default = Label("@bazel_tools//tools/jdk:current_java_runtime"), + providers = [ + java_common.JavaRuntimeInfo, + platform_common.TemplateVariableInfo, + ], + cfg = "host", + ), + }, + provides = [ + java_common.JavaRuntimeInfo, + platform_common.TemplateVariableInfo, + platform_common.ToolchainInfo, + ], +) + +def _java_runtime_transition_impl(settings, attr): + return {"//command_line_option:java_runtime_version": attr.runtime_version} + +_java_runtime_transition = transition( + implementation = _java_runtime_transition_impl, + inputs = [], + outputs = ["//command_line_option:java_runtime_version"], +) + +java_runtime_version_alias = rule( + implementation = _java_runtime_alias, + toolchains = ["@bazel_tools//tools/jdk:runtime_toolchain_type"], + incompatible_use_toolchain_transition = True, + attrs = { + "runtime_version": attr.string(mandatory = True), + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), + }, + cfg = _java_runtime_transition, +) + +def _java_toolchain_alias(ctx): + """An experimental implementation of java_toolchain_alias using toolchain resolution.""" + toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"] + toolchain = toolchain_info.java + return struct( + providers = [ + toolchain_info, + toolchain, + ], + # Use the legacy provider syntax for compatibility with the native rules. + java_toolchain = toolchain, + ) + +java_toolchain_alias = rule( + implementation = _java_toolchain_alias, + toolchains = ["@bazel_tools//tools/jdk:toolchain_type"], + incompatible_use_toolchain_transition = True, +) diff --git a/toolchains/jdk.BUILD b/toolchains/jdk.BUILD new file mode 100644 index 0000000..6870771 --- /dev/null +++ b/toolchains/jdk.BUILD @@ -0,0 +1,218 @@ +load("@rules_java//java:defs.bzl", "java_import", "java_runtime") + +package(default_visibility = ["//visibility:public"]) + +exports_files(["BUILD.bazel"]) + +DEPRECATION_MESSAGE = ("Don't depend on targets in the JDK workspace;" + + " use @bazel_tools//tools/jdk:current_java_runtime instead" + + " (see https://github.com/bazelbuild/bazel/issues/5594)") + +filegroup( + name = "jni_header", + srcs = ["include/jni.h"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jni_md_header-darwin", + srcs = ["include/darwin/jni_md.h"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jni_md_header-linux", + srcs = ["include/linux/jni_md.h"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jni_md_header-freebsd", + srcs = ["include/freebsd/jni_md.h"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jni_md_header-openbsd", + srcs = ["include/openbsd/jni_md.h"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jni_md_header-windows", + srcs = ["include/win32/jni_md.h"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "java", + srcs = select({ + ":windows": ["bin/java.exe"], + "//conditions:default": ["bin/java"], + }), + data = [":jdk"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jar", + srcs = select({ + ":windows": ["bin/jar.exe"], + "//conditions:default": ["bin/jar"], + }), + data = [":jdk"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "javac", + srcs = select({ + ":windows": ["bin/javac.exe"], + "//conditions:default": ["bin/javac"], + }), + data = [":jdk"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "javadoc", + srcs = select({ + ":windows": ["bin/javadoc.exe"], + "//conditions:default": ["bin/javadoc"], + }), + data = [":jdk"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "xjc", + srcs = ["bin/xjc"], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "wsimport", + srcs = ["bin/wsimport"], + deprecation = DEPRECATION_MESSAGE, +) + +BOOTCLASS_JARS = [ + "rt.jar", + "resources.jar", + "jsse.jar", + "jce.jar", + "charsets.jar", +] + +# TODO(cushon): this isn't compatible with JDK 9 +filegroup( + name = "bootclasspath", + srcs = ["jre/lib/%s" % jar for jar in BOOTCLASS_JARS], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jre-bin", + srcs = select({ + # In some configurations, Java browser plugin is considered harmful and + # common antivirus software blocks access to npjp2.dll interfering with Bazel, + # so do not include it in JRE on Windows. + ":windows": glob( + ["jre/bin/**"], + allow_empty = True, + exclude = ["jre/bin/plugin2/**"], + ), + "//conditions:default": glob( + ["jre/bin/**"], + allow_empty = True, + ), + }), + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jre-lib", + srcs = glob( + ["jre/lib/**"], + allow_empty = True, + ), +) + +filegroup( + name = "jre", + srcs = [":jre-default"], +) + +filegroup( + name = "jre-default", + srcs = [ + ":jre-bin", + ":jre-lib", + ], + deprecation = DEPRECATION_MESSAGE, +) + +filegroup( + name = "jdk-bin", + srcs = glob( + ["bin/**"], + # The JDK on Windows sometimes contains a directory called + # "%systemroot%", which is not a valid label. + exclude = ["**/*%*/**"], + ), +) + +#This folder holds security policies +filegroup( + name = "jdk-conf", + srcs = glob( + ["conf/**"], + allow_empty = True, + ), +) + +filegroup( + name = "jdk-include", + srcs = glob(["include/**"]), +) + +filegroup( + name = "jdk-lib", + srcs = glob( + ["lib/**"], + exclude = [ + "lib/missioncontrol/**", + "lib/visualvm/**", + ], + ), +) + +java_runtime( + name = "jdk", + srcs = [ + ":jdk-bin", + ":jdk-conf", + ":jdk-include", + ":jdk-lib", + ":jre-default", + ], +) + +filegroup( + name = "langtools", + srcs = ["lib/tools.jar"], + deprecation = DEPRECATION_MESSAGE, +) + +java_import( + name = "langtools-neverlink", + deprecation = DEPRECATION_MESSAGE, + jars = ["lib/tools.jar"], + neverlink = 1, +) + +config_setting( + name = "windows", + values = {"cpu": "x64_windows"}, + visibility = ["//visibility:private"], +) diff --git a/toolchains/local_java_repository.bzl b/toolchains/local_java_repository.bzl new file mode 100644 index 0000000..4c0a433 --- /dev/null +++ b/toolchains/local_java_repository.bzl @@ -0,0 +1,232 @@ +# Copyright 2020 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Rules for importing and registering a local JDK.""" + +load(":default_java_toolchain.bzl", "JVM8_TOOLCHAIN_CONFIGURATION", "default_java_toolchain") + +def _detect_java_version(repository_ctx, java_bin): + properties_out = repository_ctx.execute([java_bin, "-XshowSettings:properties"]).stderr + # This returns an indented list of properties separated with newlines: + # " java.vendor.url.bug = ... \n" + # " java.version = 11.0.8\n" + # " java.version.date = 2020-11-05\" + + strip_properties = [property.strip() for property in properties_out.splitlines()] + version_property = [property for property in strip_properties if property.startswith("java.version = ")] + if len(version_property) != 1: + return None + + version_value = version_property[0][len("java.version = "):] + parts = version_value.split(".") + major = parts[0] + if len(parts) == 1: + return major + elif major == "1": # handles versions below 1.8 + minor = parts[1] + return minor + return major + +def local_java_runtime(name, java_home, version, runtime_name = None, visibility = ["//visibility:public"]): + """Defines a java_runtime target together with Java runtime and compile toolchain definitions. + + Java runtime toolchain is constrained by flag --java_runtime_version having + value set to either name or version argument. + + Java compile toolchains are created for --java_language_version flags values + between 8 and version (inclusive). Java compile toolchains use the same + (local) JDK for compilation. This requires a different configuration for JDK8 + than the newer versions. + + Args: + name: name of the target. + java_home: Path to the JDK. + version: Version of the JDK. + runtime_name: name of java_runtime target if it already exists. + visibility: Visibility that will be applied to the java runtime target + """ + if runtime_name == None: + runtime_name = name + native.java_runtime( + name = runtime_name, + java_home = java_home, + visibility = visibility, + ) + + native.config_setting( + name = name + "_name_setting", + values = {"java_runtime_version": name}, + visibility = ["//visibility:private"], + ) + native.config_setting( + name = name + "_version_setting", + values = {"java_runtime_version": version}, + visibility = ["//visibility:private"], + ) + native.config_setting( + name = name + "_name_version_setting", + values = {"java_runtime_version": name + "_" + version}, + visibility = ["//visibility:private"], + ) + native.alias( + name = name + "_settings_alias", + actual = select({ + name + "_name_setting": name + "_name_setting", + name + "_version_setting": name + "_version_setting", + "//conditions:default": name + "_name_version_setting", + }), + visibility = ["//visibility:private"], + ) + native.toolchain( + name = "runtime_toolchain_definition", + target_settings = [":%s_settings_alias" % name], + toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type", + toolchain = runtime_name, + ) + + if version == "8": + default_java_toolchain( + name = name + "_toolchain_java8", + configuration = JVM8_TOOLCHAIN_CONFIGURATION, + source_version = version, + target_version = version, + java_runtime = runtime_name, + ) + elif type(version) == type("") and version.isdigit() and int(version) > 8: + for version in range(8, int(version) + 1): + default_java_toolchain( + name = name + "_toolchain_java" + str(version), + source_version = str(version), + target_version = str(version), + java_runtime = runtime_name, + ) + + # else version is not recognized and no compilation toolchains are predefined + +def _local_java_repository_impl(repository_ctx): + """Repository rule local_java_repository implementation. + + Args: + repository_ctx: repository context + """ + java_home = repository_ctx.attr.java_home + java_home_path = repository_ctx.path(java_home) + if not java_home_path.exists: + fail('The path indicated by the "java_home" attribute "%s" (absolute: "%s") ' + + "does not exist." % (java_home, str(java_home_path))) + + repository_ctx.file( + "WORKSPACE", + "# DO NOT EDIT: automatically generated WORKSPACE file for local_java_repository\n" + + "workspace(name = \"{name}\")\n".format(name = repository_ctx.name), + ) + + extension = ".exe" if repository_ctx.os.name.lower().find("windows") != -1 else "" + java_bin = java_home_path.get_child("bin").get_child("java" + extension) + + if not java_bin.exists: + # Java binary does not exist + repository_ctx.file( + "BUILD.bazel", + _NOJDK_BUILD_TPL.format( + local_jdk = repository_ctx.name, + java_binary = "bin/java" + extension, + java_home = java_home, + ), + False, + ) + return + + # Detect version + version = repository_ctx.attr.version if repository_ctx.attr.version != "" else _detect_java_version(repository_ctx, java_bin) + + # Prepare BUILD file using "local_java_runtime" macro + build_file = "" + if repository_ctx.attr.build_file != None: + build_file = repository_ctx.read(repository_ctx.path(repository_ctx.attr.build_file)) + + runtime_name = '"jdk"' if repository_ctx.attr.build_file else None + local_java_runtime_macro = """ +local_java_runtime( + name = "%s", + runtime_name = %s, + java_home = "%s", + version = "%s", +) +""" % (repository_ctx.name, runtime_name, java_home, version) + + repository_ctx.file( + "BUILD.bazel", + 'load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_runtime")\n' + + build_file + + local_java_runtime_macro, + ) + + # Symlink all files + for file in repository_ctx.path(java_home).readdir(): + repository_ctx.symlink(file, file.basename) + +# Build file template, when JDK does not exist +_NOJDK_BUILD_TPL = '''load("@bazel_tools//tools/jdk:fail_rule.bzl", "fail_rule") +fail_rule( + name = "jdk", + header = "Auto-Configuration Error:", + message = ("Cannot find Java binary {java_binary} in {java_home}; either correct your JAVA_HOME, " + + "PATH or specify Java from remote repository (e.g. " + + "--java_runtime_version=remotejdk_11") +) +config_setting( + name = "localjdk_setting", + values = {{"java_runtime_version": "{local_jdk}"}}, + visibility = ["//visibility:private"], +) +toolchain( + name = "runtime_toolchain_definition", + target_settings = [":localjdk_setting"], + toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type", + toolchain = ":jdk", +) +''' + +_local_java_repository_rule = repository_rule( + implementation = _local_java_repository_impl, + local = True, + configure = True, + attrs = { + "java_home": attr.string(), + "version": attr.string(), + "build_file": attr.label(), + }, +) + +def local_java_repository(name, java_home, version = "", build_file = None): + """Registers a runtime toolchain for local JDK and creates an unregistered compile toolchain. + + Toolchain resolution is constrained with --java_runtime_version flag + having value of the "name" or "version" parameter. + + Java compile toolchains are created for --java_language_version flags values + between 8 and version (inclusive). Java compile toolchains use the same + (local) JDK for compilation. + + If there is no JDK "virtual" targets are created, which fail only when actually needed. + + Args: + name: A unique name for this rule. + java_home: Location of the JDK imported. + build_file: optionally BUILD file template + version: optionally java version + """ + _local_java_repository_rule(name = name, java_home = java_home, version = version, build_file = build_file) + native.register_toolchains("@" + name + "//:runtime_toolchain_definition") From 7a8910ba84ccddc603b6330cc7ca6e4a45d97f9a Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Wed, 10 Nov 2021 13:45:56 +0100 Subject: [PATCH 03/15] Rewire toolchain targets After this, we only reference toolchain type targets under @bazel_tools//tools/jdk - @bazel_tools//tools/jdk:toolchain_type - @bazel_tools//tools/jdk:runtime_toolchain_type --- toolchains/BUILD | 97 ++++++++++++--------------- toolchains/default_java_toolchain.bzl | 22 +++--- toolchains/java_toolchain_alias.bzl | 2 +- toolchains/local_java_repository.bzl | 11 ++- toolchains/remote_java_repository.bzl | 3 +- 5 files changed, 64 insertions(+), 71 deletions(-) diff --git a/toolchains/BUILD b/toolchains/BUILD index 6f7fef4..06f0448 100644 --- a/toolchains/BUILD +++ b/toolchains/BUILD @@ -1,5 +1,5 @@ load( - "@bazel_tools//tools/jdk:default_java_toolchain.bzl", + "//toolchains:default_java_toolchain.bzl", "DEFAULT_TOOLCHAIN_CONFIGURATION", "JVM8_TOOLCHAIN_CONFIGURATION", "PREBUILT_TOOLCHAIN_CONFIGURATION", @@ -9,12 +9,13 @@ load( "java_runtime_files", ) load( - "//tools/jdk:java_toolchain_alias.bzl", + "//toolchains:java_toolchain_alias.bzl", "java_host_runtime_alias", "java_runtime_alias", "java_runtime_version_alias", "java_toolchain_alias", ) +load("@rules_python//python:defs.bzl", "py_binary", "py_test") package(default_visibility = ["//visibility:public"]) @@ -26,16 +27,13 @@ filegroup( visibility = ["@//:__pkg__"], ) - -load("//tools/python:private/defs.bzl", "py_binary", "py_test") - -package(default_visibility = ["//visibility:public"]) - # Used to distinguish toolchains used for Java development, ie the JavaToolchainProvider. -toolchain_type(name = "toolchain_type") +# TODO: migrate away from using @bazel_tools//tools/jdk:toolchain_type ? +# toolchain_type(name = "toolchain_type") # Used to distinguish toolchains used for Java execution, ie the JavaRuntimeInfo. -toolchain_type(name = "runtime_toolchain_type") +# TODO: migrate away from using @bazel_tools//tools/jdk:runtime_toolchain_type ? +# toolchain_type(name = "runtime_toolchain_type") # Points to toolchain[":runtime_toolchain_type"] (was :legacy_current_java_runtime) java_runtime_alias(name = "current_java_runtime") @@ -57,7 +55,7 @@ java_plugins_flag_alias( # *before* the transition. alias( name = "java_runtime_alias", - actual = "//tools/jdk:current_java_runtime", + actual = ":current_java_runtime", ) # These individual jni_* targets are exposed for legacy reasons. @@ -102,29 +100,29 @@ java_runtime_files( cc_library( name = "jni", hdrs = [":jni_header"] + select({ - "//src/conditions:linux_aarch64": [":jni_md_header-linux"], - "//src/conditions:linux_ppc64le": [":jni_md_header-linux"], - "//src/conditions:linux_s390x": [":jni_md_header-linux"], - "//src/conditions:linux_mips64": [":jni_md_header-linux"], - "//src/conditions:linux_riscv64": [":jni_md_header-linux"], - "//src/conditions:linux_x86_64": [":jni_md_header-linux"], - "//src/conditions:darwin": [":jni_md_header-darwin"], - "//src/conditions:freebsd": [":jni_md_header-freebsd"], - "//src/conditions:openbsd": [":jni_md_header-openbsd"], - "//src/conditions:windows": [":jni_md_header-windows"], + "@bazel_tools//src/conditions:linux_aarch64": [":jni_md_header-linux"], + "@bazel_tools//src/conditions:linux_ppc64le": [":jni_md_header-linux"], + "@bazel_tools//src/conditions:linux_s390x": [":jni_md_header-linux"], + "@bazel_tools//src/conditions:linux_mips64": [":jni_md_header-linux"], + "@bazel_tools//src/conditions:linux_riscv64": [":jni_md_header-linux"], + "@bazel_tools//src/conditions:linux_x86_64": [":jni_md_header-linux"], + "@bazel_tools//src/conditions:darwin": [":jni_md_header-darwin"], + "@bazel_tools//src/conditions:freebsd": [":jni_md_header-freebsd"], + "@bazel_tools//src/conditions:openbsd": [":jni_md_header-openbsd"], + "@bazel_tools//src/conditions:windows": [":jni_md_header-windows"], "//conditions:default": [], }), includes = ["include"] + select({ - "//src/conditions:linux_aarch64": ["include/linux"], - "//src/conditions:linux_ppc64le": ["include/linux"], - "//src/conditions:linux_s390x": ["include/linux"], - "//src/conditions:linux_mips64": [":include/linux"], - "//src/conditions:linux_riscv64": [":include/linux"], - "//src/conditions:linux_x86_64": ["include/linux"], - "//src/conditions:darwin": ["include/darwin"], - "//src/conditions:freebsd": ["include/freebsd"], - "//src/conditions:openbsd": ["include/openbsd"], - "//src/conditions:windows": ["include/win32"], + "@bazel_tools//src/conditions:linux_aarch64": ["include/linux"], + "@bazel_tools//src/conditions:linux_ppc64le": ["include/linux"], + "@bazel_tools//src/conditions:linux_s390x": ["include/linux"], + "@bazel_tools//src/conditions:linux_mips64": [":include/linux"], + "@bazel_tools//src/conditions:linux_riscv64": [":include/linux"], + "@bazel_tools//src/conditions:linux_x86_64": ["include/linux"], + "@bazel_tools//src/conditions:darwin": ["include/darwin"], + "@bazel_tools//src/conditions:freebsd": ["include/freebsd"], + "@bazel_tools//src/conditions:openbsd": ["include/openbsd"], + "@bazel_tools//src/conditions:windows": ["include/win32"], "//conditions:default": [], }), ) @@ -180,9 +178,9 @@ alias( alias( name = "ijar_prebuilt_binary_or_cc_binary", actual = select({ - "//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", - "//src/conditions:darwin": ":ijar_prebuilt_binary_darwin", - "//src/conditions:windows": ":ijar_prebuilt_binary_windows", + "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", + "@bazel_tools//src/conditions:darwin": ":ijar_prebuilt_binary_darwin", + "@bazel_tools//src/conditions:windows": ":ijar_prebuilt_binary_windows", "//conditions:default": "@remote_java_tools//:ijar_cc_binary", }), ) @@ -190,9 +188,9 @@ alias( alias( name = "ijar_prebuilt_binary", actual = select({ - "//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", - "//src/conditions:darwin": ":ijar_prebuilt_binary_darwin", - "//src/conditions:windows": ":ijar_prebuilt_binary_windows", + "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", + "@bazel_tools//src/conditions:darwin": ":ijar_prebuilt_binary_darwin", + "@bazel_tools//src/conditions:windows": ":ijar_prebuilt_binary_windows", }), ) @@ -210,9 +208,9 @@ alias( alias( name = "singlejar_prebuilt_or_cc_binary", actual = select({ - "//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", - "//src/conditions:darwin": ":prebuilt_singlejar_darwin", - "//src/conditions:windows": ":prebuilt_singlejar_windows", + "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", + "@bazel_tools//src/conditions:darwin": ":prebuilt_singlejar_darwin", + "@bazel_tools//src/conditions:windows": ":prebuilt_singlejar_windows", "//conditions:default": "@remote_java_tools//:singlejar_cc_bin", }), ) @@ -220,9 +218,9 @@ alias( alias( name = "prebuilt_singlejar", actual = select({ - "//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", - "//src/conditions:darwin": ":prebuilt_singlejar_darwin", - "//src/conditions:windows": ":prebuilt_singlejar_windows", + "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", + "@bazel_tools//src/conditions:darwin": ":prebuilt_singlejar_darwin", + "@bazel_tools//src/conditions:windows": ":prebuilt_singlejar_windows", }), ) @@ -372,20 +370,11 @@ RELEASES = (8, 9, 10, 11) for release in RELEASES ] -# A toolchain that targets java 14. -default_java_toolchain( - name = "toolchain_jdk_14", - configuration = dict(), - java_runtime = "@bazel_tools//tools/jdk:remotejdk_14", - source_version = "14", - target_version = "14", -) - # A toolchain that targets java 15. default_java_toolchain( name = "toolchain_jdk_15", configuration = dict(), - java_runtime = "@bazel_tools//tools/jdk:remotejdk_15", + java_runtime = "//toolchains:remotejdk_15", source_version = "15", target_version = "15", ) @@ -394,7 +383,7 @@ default_java_toolchain( default_java_toolchain( name = "toolchain_jdk_16", configuration = dict(), - java_runtime = "@bazel_tools//tools/jdk:remotejdk_16", + java_runtime = "//toolchains:remotejdk_16", source_version = "16", target_version = "16", ) @@ -403,7 +392,7 @@ default_java_toolchain( default_java_toolchain( name = "toolchain_jdk_17", configuration = dict(), - java_runtime = "@bazel_tools//tools/jdk:remotejdk_17", + java_runtime = "//toolchains:remotejdk_17", source_version = "17", target_version = "17", ) diff --git a/toolchains/default_java_toolchain.bzl b/toolchains/default_java_toolchain.bzl index ac8ed5d..1e83e3c 100644 --- a/toolchains/default_java_toolchain.bzl +++ b/toolchains/default_java_toolchain.bzl @@ -62,16 +62,16 @@ _BASE_TOOLCHAIN_CONFIGURATION = dict( genclass = ["@remote_java_tools//:GenClass"], header_compiler = ["@remote_java_tools//:TurbineDirect"], header_compiler_direct = ["@remote_java_tools//:TurbineDirect"], - ijar = ["@bazel_tools//tools/jdk:ijar"], + ijar = ["//toolchains:ijar"], javabuilder = ["@remote_java_tools//:JavaBuilder"], javac_supports_workers = True, jacocorunner = "@remote_java_tools//:jacoco_coverage_runner_filegroup", jvm_opts = BASE_JDK9_JVM_OPTS, misc = DEFAULT_JAVACOPTS, - singlejar = ["@bazel_tools//tools/jdk:singlejar"], + singlejar = ["//toolchains:singlejar"], # Code to enumerate target JVM boot classpath uses host JVM. Because # java_runtime-s are involved, its implementation is in @bazel_tools. - bootclasspath = ["@bazel_tools//tools/jdk:platformclasspath"], + bootclasspath = ["//toolchains:platformclasspath"], source_version = "8", target_version = "8", reduced_classpath_incompatible_processors = [ @@ -82,7 +82,7 @@ _BASE_TOOLCHAIN_CONFIGURATION = dict( JVM8_TOOLCHAIN_CONFIGURATION = dict( tools = ["@remote_java_tools//:javac_jar"], jvm_opts = ["-Xbootclasspath/p:$(location @remote_java_tools//:javac_jar)"], - java_runtime = "@bazel_tools//tools/jdk:jdk_8", + java_runtime = "//toolchains:jdk_8", ) DEFAULT_TOOLCHAIN_CONFIGURATION = dict( @@ -98,7 +98,7 @@ DEFAULT_TOOLCHAIN_CONFIGURATION = dict( "@remote_java_tools//:java_compiler_jar", "@remote_java_tools//:jdk_compiler_jar", ], - java_runtime = "@bazel_tools//tools/jdk:remote_jdk11", + java_runtime = "//toolchains:remote_jdk11", ) # The 'vanilla' toolchain is an unsupported alternative to the default. @@ -137,9 +137,9 @@ PREBUILT_TOOLCHAIN_CONFIGURATION = dict( "@remote_java_tools//:java_compiler_jar", "@remote_java_tools//:jdk_compiler_jar", ], - ijar = ["@bazel_tools//tools/jdk:ijar_prebuilt_binary"], - singlejar = ["@bazel_tools//tools/jdk:prebuilt_singlejar"], - java_runtime = "@bazel_tools//tools/jdk:remote_jdk11", + ijar = ["//toolchains:ijar_prebuilt_binary"], + singlejar = ["//toolchains:prebuilt_singlejar"], + java_runtime = "//toolchains:remote_jdk11", ) # The new toolchain is using all the tools from sources. @@ -158,7 +158,7 @@ NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict( ], ijar = ["@remote_java_tools//:ijar_cc_binary"], singlejar = ["@remote_java_tools//:singlejar_cc_bin"], - java_runtime = "@bazel_tools//tools/jdk:remote_jdk11", + java_runtime = "//toolchains:remote_jdk11", ) def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, **kwargs): @@ -194,8 +194,8 @@ def java_runtime_files(name, srcs): for src in srcs: native.genrule( name = "gen_%s" % src, - srcs = ["@bazel_tools//tools/jdk:current_java_runtime"], - toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"], + srcs = ["//toolchains:current_java_runtime"], + toolchains = ["//toolchains:current_java_runtime"], cmd = "cp $(JAVABASE)/%s $@" % src, outs = [src], ) diff --git a/toolchains/java_toolchain_alias.bzl b/toolchains/java_toolchain_alias.bzl index ddf6d3f..27bf4e4 100644 --- a/toolchains/java_toolchain_alias.bzl +++ b/toolchains/java_toolchain_alias.bzl @@ -55,7 +55,7 @@ java_host_runtime_alias = rule( implementation = _java_host_runtime_alias, attrs = { "_runtime": attr.label( - default = Label("@bazel_tools//tools/jdk:current_java_runtime"), + default = Label("//toolchains:current_java_runtime"), providers = [ java_common.JavaRuntimeInfo, platform_common.TemplateVariableInfo, diff --git a/toolchains/local_java_repository.bzl b/toolchains/local_java_repository.bzl index 4c0a433..1745813 100644 --- a/toolchains/local_java_repository.bzl +++ b/toolchains/local_java_repository.bzl @@ -56,6 +56,12 @@ def local_java_runtime(name, java_home, version, runtime_name = None, visibility runtime_name: name of java_runtime target if it already exists. visibility: Visibility that will be applied to the java runtime target """ + + # The repository name in Bzlmod will be "..local_jdk" + # instead of "local_jdk", therefore we cannot just use it as the config_setting value, + # because it won't match the default value of --java_runtime_version (which is "local_jdk"). + name = name.split(".")[-1] + if runtime_name == None: runtime_name = name native.java_runtime( @@ -168,7 +174,7 @@ local_java_runtime( repository_ctx.file( "BUILD.bazel", - 'load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_runtime")\n' + + 'load("@rules_java//toolchains:local_java_repository.bzl", "local_java_runtime")\n' + build_file + local_java_runtime_macro, ) @@ -178,7 +184,7 @@ local_java_runtime( repository_ctx.symlink(file, file.basename) # Build file template, when JDK does not exist -_NOJDK_BUILD_TPL = '''load("@bazel_tools//tools/jdk:fail_rule.bzl", "fail_rule") +_NOJDK_BUILD_TPL = '''load("@rules_java//toolchains:fail_rule.bzl", "fail_rule") fail_rule( name = "jdk", header = "Auto-Configuration Error:", @@ -229,4 +235,3 @@ def local_java_repository(name, java_home, version = "", build_file = None): version: optionally java version """ _local_java_repository_rule(name = name, java_home = java_home, version = version, build_file = build_file) - native.register_toolchains("@" + name + "//:runtime_toolchain_definition") diff --git a/toolchains/remote_java_repository.bzl b/toolchains/remote_java_repository.bzl index 050903c..33c0d6e 100644 --- a/toolchains/remote_java_repository.bzl +++ b/toolchains/remote_java_repository.bzl @@ -47,7 +47,7 @@ def remote_java_repository(name, version, exec_compatible_with, prefix = "remote """ http_archive( name = name, - build_file = "@bazel_tools//tools/jdk:jdk.BUILD", + build_file = Label("//toolchains:jdk.BUILD"), **kwargs ) _toolchain_config( @@ -85,4 +85,3 @@ toolchain( toolchain = "@{repo}//:jdk".format(repo = name), ), ) - native.register_toolchains("@" + name + "_toolchain_config_repo//:toolchain") From 7f753bd46eefdacedbe37b528a875889044b88f8 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 11 Nov 2021 15:42:41 +0100 Subject: [PATCH 04/15] Run buildifier --- toolchains/BUILD | 37 +++++++++++---------------- toolchains/default_java_toolchain.bzl | 19 ++++++++++---- toolchains/java_toolchain_alias.bzl | 2 ++ 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/toolchains/BUILD b/toolchains/BUILD index 06f0448..e5f76a8 100644 --- a/toolchains/BUILD +++ b/toolchains/BUILD @@ -1,9 +1,11 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") +load("@rules_java//java:defs.bzl", "java_import") +load("@rules_python//python:defs.bzl", "py_binary", "py_test") load( "//toolchains:default_java_toolchain.bzl", "DEFAULT_TOOLCHAIN_CONFIGURATION", "JVM8_TOOLCHAIN_CONFIGURATION", "PREBUILT_TOOLCHAIN_CONFIGURATION", - "VANILLA_TOOLCHAIN_CONFIGURATION", "bootclasspath", "default_java_toolchain", "java_runtime_files", @@ -15,7 +17,6 @@ load( "java_runtime_version_alias", "java_toolchain_alias", ) -load("@rules_python//python:defs.bzl", "py_binary", "py_test") package(default_visibility = ["//visibility:public"]) @@ -100,27 +101,27 @@ java_runtime_files( cc_library( name = "jni", hdrs = [":jni_header"] + select({ + "@bazel_tools//src/conditions:darwin": [":jni_md_header-darwin"], + "@bazel_tools//src/conditions:freebsd": [":jni_md_header-freebsd"], "@bazel_tools//src/conditions:linux_aarch64": [":jni_md_header-linux"], - "@bazel_tools//src/conditions:linux_ppc64le": [":jni_md_header-linux"], - "@bazel_tools//src/conditions:linux_s390x": [":jni_md_header-linux"], "@bazel_tools//src/conditions:linux_mips64": [":jni_md_header-linux"], + "@bazel_tools//src/conditions:linux_ppc64le": [":jni_md_header-linux"], "@bazel_tools//src/conditions:linux_riscv64": [":jni_md_header-linux"], + "@bazel_tools//src/conditions:linux_s390x": [":jni_md_header-linux"], "@bazel_tools//src/conditions:linux_x86_64": [":jni_md_header-linux"], - "@bazel_tools//src/conditions:darwin": [":jni_md_header-darwin"], - "@bazel_tools//src/conditions:freebsd": [":jni_md_header-freebsd"], "@bazel_tools//src/conditions:openbsd": [":jni_md_header-openbsd"], "@bazel_tools//src/conditions:windows": [":jni_md_header-windows"], "//conditions:default": [], }), includes = ["include"] + select({ + "@bazel_tools//src/conditions:darwin": ["include/darwin"], + "@bazel_tools//src/conditions:freebsd": ["include/freebsd"], "@bazel_tools//src/conditions:linux_aarch64": ["include/linux"], - "@bazel_tools//src/conditions:linux_ppc64le": ["include/linux"], - "@bazel_tools//src/conditions:linux_s390x": ["include/linux"], "@bazel_tools//src/conditions:linux_mips64": [":include/linux"], + "@bazel_tools//src/conditions:linux_ppc64le": ["include/linux"], "@bazel_tools//src/conditions:linux_riscv64": [":include/linux"], + "@bazel_tools//src/conditions:linux_s390x": ["include/linux"], "@bazel_tools//src/conditions:linux_x86_64": ["include/linux"], - "@bazel_tools//src/conditions:darwin": ["include/darwin"], - "@bazel_tools//src/conditions:freebsd": ["include/freebsd"], "@bazel_tools//src/conditions:openbsd": ["include/openbsd"], "@bazel_tools//src/conditions:windows": ["include/win32"], "//conditions:default": [], @@ -178,8 +179,8 @@ alias( alias( name = "ijar_prebuilt_binary_or_cc_binary", actual = select({ - "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", "@bazel_tools//src/conditions:darwin": ":ijar_prebuilt_binary_darwin", + "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", "@bazel_tools//src/conditions:windows": ":ijar_prebuilt_binary_windows", "//conditions:default": "@remote_java_tools//:ijar_cc_binary", }), @@ -188,8 +189,8 @@ alias( alias( name = "ijar_prebuilt_binary", actual = select({ - "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", "@bazel_tools//src/conditions:darwin": ":ijar_prebuilt_binary_darwin", + "@bazel_tools//src/conditions:linux_x86_64": ":ijar_prebuilt_binary_linux", "@bazel_tools//src/conditions:windows": ":ijar_prebuilt_binary_windows", }), ) @@ -208,8 +209,8 @@ alias( alias( name = "singlejar_prebuilt_or_cc_binary", actual = select({ - "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", "@bazel_tools//src/conditions:darwin": ":prebuilt_singlejar_darwin", + "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", "@bazel_tools//src/conditions:windows": ":prebuilt_singlejar_windows", "//conditions:default": "@remote_java_tools//:singlejar_cc_bin", }), @@ -218,8 +219,8 @@ alias( alias( name = "prebuilt_singlejar", actual = select({ - "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", "@bazel_tools//src/conditions:darwin": ":prebuilt_singlejar_darwin", + "@bazel_tools//src/conditions:linux_x86_64": ":prebuilt_singlejar_linux", "@bazel_tools//src/conditions:windows": ":prebuilt_singlejar_windows", }), ) @@ -311,14 +312,6 @@ alias( actual = "@remote_java_tools//:proguard", ) -BOOTCLASS_JARS = [ - "rt.jar", - "resources.jar", - "jsse.jar", - "jce.jar", - "charsets.jar", -] - # TODO(cushon): this isn't compatible with JDK 9 alias( name = "bootclasspath", diff --git a/toolchains/default_java_toolchain.bzl b/toolchains/default_java_toolchain.bzl index 1e83e3c..49652c3 100644 --- a/toolchains/default_java_toolchain.bzl +++ b/toolchains/default_java_toolchain.bzl @@ -1,4 +1,4 @@ -# Copyright 2017 The Bazel Authors. All rights reserved. +# Copyright 2021 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -12,7 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Bazel rules for creating Java toolchains.""" +"""Rules for defining default_java_toolchain""" + +load("@rules_java//java:defs.bzl", "java_toolchain") JDK8_JVM_OPTS = [ "-Xbootclasspath/p:$(location @remote_java_tools//:javac_jar)", @@ -162,12 +164,19 @@ NONPREBUILT_TOOLCHAIN_CONFIGURATION = dict( ) def default_java_toolchain(name, configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, toolchain_definition = True, **kwargs): - """Defines a remote java_toolchain with appropriate defaults for Bazel.""" + """Defines a remote java_toolchain with appropriate defaults for Bazel. + + Args: + name: The name of the toolchain + configuration: Toolchain configuration + toolchain_definition: Whether to define toolchain target and its config setting + **kwargs: More arguments for the java_toolchain target + """ toolchain_args = dict(_BASE_TOOLCHAIN_CONFIGURATION) toolchain_args.update(configuration) toolchain_args.update(kwargs) - native.java_toolchain( + java_toolchain( name = name, **toolchain_args ) @@ -274,6 +283,7 @@ _bootclasspath = rule( cfg = "host", providers = [java_common.JavaRuntimeInfo], ), + "output_jar": attr.output(mandatory = True), "src": attr.label( cfg = "host", allow_single_file = True, @@ -281,7 +291,6 @@ _bootclasspath = rule( "target_javabase": attr.label( providers = [java_common.JavaRuntimeInfo], ), - "output_jar": attr.output(mandatory = True), }, ) diff --git a/toolchains/java_toolchain_alias.bzl b/toolchains/java_toolchain_alias.bzl index 27bf4e4..1dc15ee 100644 --- a/toolchains/java_toolchain_alias.bzl +++ b/toolchains/java_toolchain_alias.bzl @@ -96,6 +96,8 @@ def _java_toolchain_alias(ctx): """An experimental implementation of java_toolchain_alias using toolchain resolution.""" toolchain_info = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"] toolchain = toolchain_info.java + + # buildifier: disable=rule-impl-return return struct( providers = [ toolchain_info, From 2b69d9c455063694d22384759a38851bf636501f Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 11 Nov 2021 15:45:58 +0100 Subject: [PATCH 05/15] local_java_repository: support auto-detect java home --- toolchains/local_java_repository.bzl | 67 ++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/toolchains/local_java_repository.bzl b/toolchains/local_java_repository.bzl index 1745813..2c13e89 100644 --- a/toolchains/local_java_repository.bzl +++ b/toolchains/local_java_repository.bzl @@ -1,4 +1,4 @@ -# Copyright 2020 The Bazel Authors. All rights reserved. +# Copyright 2021 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,6 +14,7 @@ """Rules for importing and registering a local JDK.""" +load("@rules_java//java:defs.bzl", "java_runtime") load(":default_java_toolchain.bzl", "JVM8_TOOLCHAIN_CONFIGURATION", "default_java_toolchain") def _detect_java_version(repository_ctx, java_bin): @@ -64,7 +65,7 @@ def local_java_runtime(name, java_home, version, runtime_name = None, visibility if runtime_name == None: runtime_name = name - native.java_runtime( + java_runtime( name = runtime_name, java_home = java_home, visibility = visibility, @@ -120,17 +121,57 @@ def local_java_runtime(name, java_home, version, runtime_name = None, visibility # else version is not recognized and no compilation toolchains are predefined +def _is_macos(repository_ctx): + return repository_ctx.os.name.lower().find("mac os x") != -1 + +def _is_windows(repository_ctx): + return repository_ctx.os.name.lower().find("windows") != -1 + +def _with_os_extension(repository_ctx, binary): + return binary + (".exe" if _is_windows(repository_ctx) else "") + +def _determine_java_home(repository_ctx): + """Determine the java home path. + + If the `java_home` attribute is specified, then use the given path, + otherwise, try to detect the java home path on the system. + + Args: + repository_ctx: repository context + """ + java_home = repository_ctx.attr.java_home + if java_home: + java_home_path = repository_ctx.path(java_home) + if not java_home_path.exists: + fail('The path indicated by the "java_home" attribute "%s" (absolute: "%s") ' + + "does not exist." % (java_home, str(java_home_path))) + return java_home_path + if "JAVA_HOME" in repository_ctx.os.environ: + return repository_ctx.path(repository_ctx.os.environ["JAVA_HOME"]) + + if _is_macos(repository_ctx): + # Replicate GetSystemJavabase() in src/main/cpp/blaze_util_darwin.cc + result = repository_ctx.execute(["/usr/libexec/java_home", "-v", "1.11+"]) + if result.return_code == 0: + return repository_ctx.path(result.stdout.strip()) + else: + # Calculate java home by locating the javac binary + # javac should exists at ${JAVA_HOME}/bin/javac + # Replicate GetSystemJavabase() in src/main/cpp/blaze_util_linux.cc + # This logic should also work on Windows. + javac_path = repository_ctx.which(_with_os_extension(repository_ctx, "javac")) + if javac_path: + return javac_path.realpath.dirname.dirname + return repository_ctx.path("./nosystemjdk") + def _local_java_repository_impl(repository_ctx): """Repository rule local_java_repository implementation. Args: repository_ctx: repository context """ - java_home = repository_ctx.attr.java_home - java_home_path = repository_ctx.path(java_home) - if not java_home_path.exists: - fail('The path indicated by the "java_home" attribute "%s" (absolute: "%s") ' + - "does not exist." % (java_home, str(java_home_path))) + + java_home = _determine_java_home(repository_ctx) repository_ctx.file( "WORKSPACE", @@ -138,8 +179,7 @@ def _local_java_repository_impl(repository_ctx): "workspace(name = \"{name}\")\n".format(name = repository_ctx.name), ) - extension = ".exe" if repository_ctx.os.name.lower().find("windows") != -1 else "" - java_bin = java_home_path.get_child("bin").get_child("java" + extension) + java_bin = java_home.get_child("bin").get_child(_with_os_extension(repository_ctx, "java")) if not java_bin.exists: # Java binary does not exist @@ -147,7 +187,7 @@ def _local_java_repository_impl(repository_ctx): "BUILD.bazel", _NOJDK_BUILD_TPL.format( local_jdk = repository_ctx.name, - java_binary = "bin/java" + extension, + java_binary = _with_os_extension(repository_ctx, "bin/java"), java_home = java_home, ), False, @@ -180,7 +220,7 @@ local_java_runtime( ) # Symlink all files - for file in repository_ctx.path(java_home).readdir(): + for file in java_home.readdir(): repository_ctx.symlink(file, file.basename) # Build file template, when JDK does not exist @@ -209,14 +249,15 @@ _local_java_repository_rule = repository_rule( implementation = _local_java_repository_impl, local = True, configure = True, + environ = ["JAVA_HOME"], attrs = { + "build_file": attr.label(), "java_home": attr.string(), "version": attr.string(), - "build_file": attr.label(), }, ) -def local_java_repository(name, java_home, version = "", build_file = None): +def local_java_repository(name, java_home = "", version = "", build_file = None): """Registers a runtime toolchain for local JDK and creates an unregistered compile toolchain. Toolchain resolution is constrained with --java_runtime_version flag From fb38ac337ea0264b6b522062f0cec818f91fffc9 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 11 Nov 2021 15:47:22 +0100 Subject: [PATCH 06/15] Compelete rules_java_dependencies and rules_java_toolchains java_tools_javac11_repos is not yet loaded because it breaks compatibility with Bazel 4.2.1` --- java/repositories.bzl | 48 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/java/repositories.bzl b/java/repositories.bzl index dd77ac6..80ec481 100644 --- a/java/repositories.bzl +++ b/java/repositories.bzl @@ -25,6 +25,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") +load("//toolchains:local_java_repository.bzl", "local_java_repository") load("//toolchains:remote_java_repository.bzl", "remote_java_repository") def java_tools_javac11_repos(): @@ -46,7 +47,6 @@ def java_tools_javac11_repos(): ], ) - http_archive( name = "remote_java_tools_windows", sha256 = "36766802f7ec684cecb1a14c122428de6be9784e88419e2ab5912ad4b59a8c7d", @@ -65,6 +65,12 @@ def java_tools_javac11_repos(): ], ) +def local_jdk_repo(): + local_java_repository( + name = "local_jdk", + build_file = Label("//toolchains:jdk.BUILD"), + ) + def remote_jdk11_repos(): """Imports OpenJDK 11 repositories.""" remote_java_repository( @@ -363,19 +369,51 @@ def bazel_skylib(): sha256 = "1dde365491125a3db70731e25658dfdd3bc5dbdfd11b840b3e987ecf043c7ca0", ) +def rules_python(): + maybe( + http_archive, + name = "rules_python", + type = "tar.gz", + url = "https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz", + sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea", + ) + def rules_java_dependencies(): """An utility method to load all dependencies of rules_java. Loads the remote repositories used by default in Bazel. """ + local_jdk_repo() remote_jdk11_repos() - java_tools_javac11_repos() + remote_jdk15_repos() + remote_jdk16_repos() + remote_jdk17_repos() + + # TODO: load this will break compatibility with Bazel 4.2.1 + # java_tools_javac11_repos() bazel_skylib() + rules_python() -def rules_java_toolchains(): +def rules_java_toolchains(name = "toolchains"): """An utility method to load all Java toolchains. - It doesn't do anything at the moment. + Args: + name: The name of this macro (not used) """ - pass + JDK_VERSIONS = ["11", "15", "16", "17"] + PLATFORMS = ["linux", "macos", "macos_aarch64", "win"] + + # Remote JDK repos for those Linux platforms are only defined for JDK 11. + EXTRA_REMOTE_JDK11_REPOS = [ + "remotejdk11_linux_aarch64", + "remotejdk11_linux_ppc64le", + "remotejdk11_linux_s390x", + ] + + REMOTE_JDK_REPOS = [("remotejdk" + version + "_" + platform) for version in JDK_VERSIONS for platform in PLATFORMS] + EXTRA_REMOTE_JDK11_REPOS + native.register_toolchains("//toolchains:all") + + native.register_toolchains("@local_jdk//:runtime_toolchain_definition") + for name in REMOTE_JDK_REPOS: + native.register_toolchains("@" + name + "_toolchain_config_repo//:toolchain") From 44879e733ce41478b868b9f4783c14212e26bca9 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 11 Nov 2021 15:48:48 +0100 Subject: [PATCH 07/15] Add Bzlmod support --- MODULE.bazel | 46 +++++++++++++++++++++++++++++++++++++++++++++ WORKSPACE.bzlmod | 1 + java/extensions.bzl | 26 +++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 MODULE.bazel create mode 100644 WORKSPACE.bzlmod create mode 100644 java/extensions.bzl diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..a60ddb7 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,46 @@ +JDK_VERSIONS = ["11", "15", "16", "17"] +PLATFORMS = ["linux", "macos", "macos_aarch64", "win"] + +# Remote JDK repos for those Linux platforms are only defined for JDK 11. +EXTRA_REMOTE_JDK11_REPOS = [ + "remotejdk11_linux_aarch64", + "remotejdk11_linux_ppc64le", + "remotejdk11_linux_s390x", +] + +REMOTE_JDK_REPOS = [("remotejdk" + version + "_" + platform) for version in JDK_VERSIONS for platform in PLATFORMS] + EXTRA_REMOTE_JDK11_REPOS + +REMOTE_JAVA_TOOLCHAINS = [("@" + name + "_toolchain_config_repo//:toolchain") for name in REMOTE_JDK_REPOS] + +module( + name = "rules_java", + version = "4.0.0", + compatibility_level = 1, + toolchains_to_register = [ + "//toolchains:all", + "@local_jdk//:runtime_toolchain_definition", + ] + REMOTE_JAVA_TOOLCHAINS, +) + +bazel_dep(name = "bazel_skylib", version = "1.0.3") +bazel_dep(name = "platforms", version = "0.0.4") +bazel_dep(name = "rules_cc", version = "0.0.1") +bazel_dep(name = "rules_proto", version = "4.0.0") +bazel_dep(name = "rules_python", version = "0.4.0") + +toolchains = use_extension("//java:extensions.bzl", "toolchains") + +# Declare remote java tools repos +use_repo(toolchains, "remote_java_tools") +use_repo(toolchains, "remote_java_tools_linux") +use_repo(toolchains, "remote_java_tools_windows") +use_repo(toolchains, "remote_java_tools_darwin") + +# Declare local jdk repo +use_repo(toolchains, "local_jdk") + +# Declare all remote jdk toolchain config repos +[use_repo(toolchains, repo + "_toolchain_config_repo") for repo in REMOTE_JDK_REPOS] + +# Dev dependencies +bazel_dep(name = "rules_pkg", version = "0.5.1", dev_dependency = True) diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod new file mode 100644 index 0000000..8b8fba4 --- /dev/null +++ b/WORKSPACE.bzlmod @@ -0,0 +1 @@ +# A completely empty WORKSPACE file to replace the original WORKSPACE content diff --git a/java/extensions.bzl b/java/extensions.bzl new file mode 100644 index 0000000..fc04283 --- /dev/null +++ b/java/extensions.bzl @@ -0,0 +1,26 @@ +# Copyright 2021 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Module extensions for rules_java.""" + +load("//java:repositories.bzl", "java_tools_javac11_repos", "local_jdk_repo", "remote_jdk11_repos", "remote_jdk15_repos", "remote_jdk16_repos", "remote_jdk17_repos") + +def _toolchains_impl(ctx): + local_jdk_repo() + java_tools_javac11_repos() + remote_jdk11_repos() + remote_jdk15_repos() + remote_jdk16_repos() + remote_jdk17_repos() + +toolchains = module_extension(implementation = _toolchains_impl) From 512b226e752052fa150dacd44aaf9612b1ed7bdb Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 11 Nov 2021 15:49:52 +0100 Subject: [PATCH 08/15] Enable CI tests --- .bazelci/presubmit.yml | 39 +++++++++++++++++++++++----- .bazelrc | 6 +++++ examples/hello-world/BUILD | 7 +++++ examples/hello-world/HelloWorld.java | 19 ++++++++++++++ 4 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 .bazelrc create mode 100644 examples/hello-world/BUILD create mode 100644 examples/hello-world/HelloWorld.java diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index fde63a7..b416cb8 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -1,14 +1,41 @@ --- +build_targets: &build_targets + - "//..." + # TODO: Look into broken targets in //toolchains + - "-//toolchains/..." + +build_targets_bzlmod: &build_targets_bzlmod + - "//..." + - "-//toolchains/..." + # TODO(pcloudy): pkg_tar doesn't work with Bzlmod due to https://github.com/bazelbuild/bazel/issues/14259 + # Enable once the issue is fixed. + - "-//distro/..." + buildifier: version: latest warnings: "all" tasks: ubuntu2004: - build_targets: - - "//..." + build_targets: *build_targets macos: - build_targets: - - "//..." + build_targets: *build_targets windows: - build_targets: - - "//..." + build_targets: *build_targets + ubuntu2004_bzlmod: + bazel: last_green + platform: ubuntu2004 + build_flags: + - "--config=bzlmod" + build_targets: *build_targets_bzlmod + macos_bzlmod: + bazel: last_green + platform: macos + build_flags: + - "--config=bzlmod" + build_targets: *build_targets_bzlmod + windows_bzlmod: + bazel: last_green + platform: windows + build_flags: + - "--config=bzlmod" + build_targets: *build_targets_bzlmod diff --git a/.bazelrc b/.bazelrc new file mode 100644 index 0000000..1127338 --- /dev/null +++ b/.bazelrc @@ -0,0 +1,6 @@ +build:bzlmod --experimental_enable_bzlmod +# @local_config_cc and @local_config_xcode are no longer available as canonical repo name, +# therefore we have to override the default value of the following flags. +# TODO: Figure out a way to get rid of this. +build:bzlmod --crosstool_top=@rules_cc.0.0.1.cc_configure.local_config_cc//:toolchain +build:bzlmod --xcode_version_config=@rules_cc.0.0.1.cc_configure.local_config_xcode//:host_xcodes diff --git a/examples/hello-world/BUILD b/examples/hello-world/BUILD new file mode 100644 index 0000000..8fe9eef --- /dev/null +++ b/examples/hello-world/BUILD @@ -0,0 +1,7 @@ +load("//java:defs.bzl", "java_binary") + +java_binary( + name = "hello-world", + srcs = ["HelloWorld.java"], + main_class = "HelloWorld", +) diff --git a/examples/hello-world/HelloWorld.java b/examples/hello-world/HelloWorld.java new file mode 100644 index 0000000..b033ce7 --- /dev/null +++ b/examples/hello-world/HelloWorld.java @@ -0,0 +1,19 @@ +// Copyright 2021 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +public class HelloWorld { + public static void main (String[] args) { + System.out.println("Hello World!"); + } +} \ No newline at end of file From 024e84fdfec54b56273d2c7d276cda1183959d8c Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 11 Nov 2021 15:56:11 +0100 Subject: [PATCH 09/15] Remove java_plugins_flag_alias --- toolchains/BUILD | 5 ----- 1 file changed, 5 deletions(-) diff --git a/toolchains/BUILD b/toolchains/BUILD index e5f76a8..585452d 100644 --- a/toolchains/BUILD +++ b/toolchains/BUILD @@ -45,11 +45,6 @@ java_host_runtime_alias(name = "current_host_java_runtime") # Points to toolchain[":toolchain_type"] (was :legacy_current_java_toolchain) java_toolchain_alias(name = "current_java_toolchain") -# Aliases value of --plugins flag as a JavaPluginInfo -java_plugins_flag_alias( - name = "java_plugins_flag_alias", -) - # This is necessary to get the *host* Java runtime. Depending on # //tools/jdk:current_java_runtime from an attribute with the host transition # does not work because the dependency is determined based on the configuration From bd24ef786358943808fa4d2339391e738a981503 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Mon, 15 Nov 2021 18:03:09 +0100 Subject: [PATCH 10/15] Address review comments (part 1) --- .bazelrc | 2 +- MODULE.bazel | 4 +- examples/hello-world/HelloWorld.java | 2 +- java/repositories.bzl | 23 +--- toolchains/BUILD | 170 --------------------------- 5 files changed, 5 insertions(+), 196 deletions(-) diff --git a/.bazelrc b/.bazelrc index 1127338..e04bf92 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,6 +1,6 @@ build:bzlmod --experimental_enable_bzlmod # @local_config_cc and @local_config_xcode are no longer available as canonical repo name, # therefore we have to override the default value of the following flags. -# TODO: Figure out a way to get rid of this. +# TODO: Remove after fixing https://github.com/bazelbuild/bazel/issues/14279 build:bzlmod --crosstool_top=@rules_cc.0.0.1.cc_configure.local_config_cc//:toolchain build:bzlmod --xcode_version_config=@rules_cc.0.0.1.cc_configure.local_config_xcode//:host_xcodes diff --git a/MODULE.bazel b/MODULE.bazel index a60ddb7..fdb4090 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -22,11 +22,11 @@ module( ] + REMOTE_JAVA_TOOLCHAINS, ) -bazel_dep(name = "bazel_skylib", version = "1.0.3") bazel_dep(name = "platforms", version = "0.0.4") bazel_dep(name = "rules_cc", version = "0.0.1") +# rules_proto is required by @remote_java_tools, which is loaded via module extension. +# TODO: perhaps java tools should have its own Bazel module, then we don't need this in rules_java. bazel_dep(name = "rules_proto", version = "4.0.0") -bazel_dep(name = "rules_python", version = "0.4.0") toolchains = use_extension("//java:extensions.bzl", "toolchains") diff --git a/examples/hello-world/HelloWorld.java b/examples/hello-world/HelloWorld.java index b033ce7..7956769 100644 --- a/examples/hello-world/HelloWorld.java +++ b/examples/hello-world/HelloWorld.java @@ -14,6 +14,6 @@ public class HelloWorld { public static void main (String[] args) { - System.out.println("Hello World!"); + System.out.println("Hello World!\n"); } } \ No newline at end of file diff --git a/java/repositories.bzl b/java/repositories.bzl index 80ec481..5f40242 100644 --- a/java/repositories.bzl +++ b/java/repositories.bzl @@ -24,7 +24,6 @@ """Development and production dependencies of rules_java.""" load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//toolchains:local_java_repository.bzl", "local_java_repository") load("//toolchains:remote_java_repository.bzl", "remote_java_repository") @@ -360,24 +359,6 @@ def remote_jdk17_repos(): version = "17", ) -def bazel_skylib(): - maybe( - http_archive, - name = "bazel_skylib", - type = "tar.gz", - url = "https://github.com/bazelbuild/bazel-skylib/releases/download/0.9.0/bazel_skylib-0.9.0.tar.gz", - sha256 = "1dde365491125a3db70731e25658dfdd3bc5dbdfd11b840b3e987ecf043c7ca0", - ) - -def rules_python(): - maybe( - http_archive, - name = "rules_python", - type = "tar.gz", - url = "https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz", - sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea", - ) - def rules_java_dependencies(): """An utility method to load all dependencies of rules_java. @@ -392,8 +373,6 @@ def rules_java_dependencies(): # TODO: load this will break compatibility with Bazel 4.2.1 # java_tools_javac11_repos() - bazel_skylib() - rules_python() def rules_java_toolchains(name = "toolchains"): """An utility method to load all Java toolchains. @@ -412,8 +391,8 @@ def rules_java_toolchains(name = "toolchains"): ] REMOTE_JDK_REPOS = [("remotejdk" + version + "_" + platform) for version in JDK_VERSIONS for platform in PLATFORMS] + EXTRA_REMOTE_JDK11_REPOS - native.register_toolchains("//toolchains:all") + native.register_toolchains("//toolchains:all") native.register_toolchains("@local_jdk//:runtime_toolchain_definition") for name in REMOTE_JDK_REPOS: native.register_toolchains("@" + name + "_toolchain_config_repo//:toolchain") diff --git a/toolchains/BUILD b/toolchains/BUILD index 585452d..65afb7b 100644 --- a/toolchains/BUILD +++ b/toolchains/BUILD @@ -1,10 +1,7 @@ load("@rules_cc//cc:defs.bzl", "cc_library") -load("@rules_java//java:defs.bzl", "java_import") -load("@rules_python//python:defs.bzl", "py_binary", "py_test") load( "//toolchains:default_java_toolchain.bzl", "DEFAULT_TOOLCHAIN_CONFIGURATION", - "JVM8_TOOLCHAIN_CONFIGURATION", "PREBUILT_TOOLCHAIN_CONFIGURATION", "bootclasspath", "default_java_toolchain", @@ -45,15 +42,6 @@ java_host_runtime_alias(name = "current_host_java_runtime") # Points to toolchain[":toolchain_type"] (was :legacy_current_java_toolchain) java_toolchain_alias(name = "current_java_toolchain") -# This is necessary to get the *host* Java runtime. Depending on -# //tools/jdk:current_java_runtime from an attribute with the host transition -# does not work because the dependency is determined based on the configuration -# *before* the transition. -alias( - name = "java_runtime_alias", - actual = ":current_java_runtime", -) - # These individual jni_* targets are exposed for legacy reasons. # Most users should depend on :jni. @@ -123,26 +111,6 @@ cc_library( }), ) -alias( - name = "java", - actual = "@local_jdk//:java", -) - -alias( - name = "jar", - actual = "@local_jdk//:jar", -) - -alias( - name = "javac", - actual = "@local_jdk//:javac", -) - -alias( - name = "javadoc", - actual = "@local_jdk//:javadoc", -) - [ ( alias( @@ -220,114 +188,6 @@ alias( }), ) -exports_files(["BUILD.java_tools"]) - -alias( - name = "genclass", - actual = "@remote_java_tools//:GenClass", -) - -alias( - name = "GenClass_deploy.jar", - actual = "@remote_java_tools//:GenClass", -) - -alias( - name = "turbine", - actual = "@remote_java_tools//:Turbine", -) - -alias( - name = "turbine_deploy.jar", - actual = "@remote_java_tools//:Turbine", -) - -alias( - name = "turbine_direct", - actual = "@remote_java_tools//:TurbineDirect", -) - -alias( - name = "turbine_direct_binary_deploy.jar", - actual = "@remote_java_tools//:TurbineDirect", -) - -alias( - name = "javabuilder", - actual = "@remote_java_tools//:JavaBuilder", -) - -alias( - name = "JavaBuilder_deploy.jar", - actual = "@remote_java_tools//:JavaBuilder", -) - -alias( - name = "vanillajavabuilder", - actual = "@remote_java_tools//:VanillaJavaBuilder", -) - -alias( - name = "javac_jar", - actual = "@remote_java_tools//:javac_jar", -) - -alias( - name = "jdk_compiler_jar", - actual = "@remote_java_tools//:jdk_compiler_jar", -) - -alias( - name = "java_compiler_jar", - actual = "@remote_java_tools//:java_compiler_jar", -) - -alias( - name = "JacocoCoverageRunner", - actual = "@remote_java_tools//:jacoco_coverage_runner", -) - -alias( - name = "JacocoCoverage", - actual = "@remote_java_tools//:jacoco_coverage_runner", -) - -java_import( - name = "TestRunner", - jars = ["@remote_java_tools//:Runner"], -) - -alias( - name = "TestRunner_deploy.jar", - actual = "@remote_java_tools//:Runner", -) - -alias( - name = "proguard", - actual = "@remote_java_tools//:proguard", -) - -# TODO(cushon): this isn't compatible with JDK 9 -alias( - name = "bootclasspath", - actual = "@local_jdk//:bootclasspath", -) - -alias( - name = "jre", - actual = "@local_jdk//:jre", -) - -alias( - name = "jdk", - actual = "@local_jdk//:jdk", -) - -alias( - name = "host_jdk", - actual = ":remote_jdk11", -) - bootclasspath( name = "platformclasspath", src = "DumpPlatformClassPath.java", @@ -385,17 +245,6 @@ default_java_toolchain( target_version = "17", ) -# Deprecated, do not use. -# It will be removed after migration to Java toolchain resolution. -default_java_toolchain( - name = "toolchain_hostjdk8", - configuration = JVM8_TOOLCHAIN_CONFIGURATION, - java_runtime = ":current_host_java_runtime", - source_version = "8", - target_version = "8", - toolchain_definition = False, -) - default_java_toolchain( name = "prebuilt_toolchain", configuration = PREBUILT_TOOLCHAIN_CONFIGURATION, @@ -408,25 +257,6 @@ filegroup( visibility = ["//tools:__pkg__"], ) -py_binary( - name = "proguard_whitelister", - srcs = [ - "proguard_whitelister.py", - ], - deps = [ - "//third_party/py/abseil", - ], -) - -py_test( - name = "proguard_whitelister_test", - srcs = ["proguard_whitelister_test.py"], - data = ["proguard_whitelister_test_input.pgcfg"], - deps = [ - ":proguard_whitelister", - ], -) - # Aliases for JDKs, so that they are only downloaded when needed. _JDKS = [ "remotejdk11_macos", From 0337494dd6eb3f3336e7199de3f00568b6d7355e Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Mon, 15 Nov 2021 18:24:30 +0100 Subject: [PATCH 11/15] Address review comments (part 2) --- java/extensions.bzl | 4 ++-- java/repositories.bzl | 4 ++-- toolchains/BUILD | 6 ------ 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/java/extensions.bzl b/java/extensions.bzl index fc04283..5ec4036 100644 --- a/java/extensions.bzl +++ b/java/extensions.bzl @@ -13,11 +13,11 @@ # limitations under the License. """Module extensions for rules_java.""" -load("//java:repositories.bzl", "java_tools_javac11_repos", "local_jdk_repo", "remote_jdk11_repos", "remote_jdk15_repos", "remote_jdk16_repos", "remote_jdk17_repos") +load("//java:repositories.bzl", "java_tools_repos", "local_jdk_repo", "remote_jdk11_repos", "remote_jdk15_repos", "remote_jdk16_repos", "remote_jdk17_repos") def _toolchains_impl(ctx): local_jdk_repo() - java_tools_javac11_repos() + java_tools_repos() remote_jdk11_repos() remote_jdk15_repos() remote_jdk16_repos() diff --git a/java/repositories.bzl b/java/repositories.bzl index 5f40242..c2eef07 100644 --- a/java/repositories.bzl +++ b/java/repositories.bzl @@ -27,7 +27,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("//toolchains:local_java_repository.bzl", "local_java_repository") load("//toolchains:remote_java_repository.bzl", "remote_java_repository") -def java_tools_javac11_repos(): +def java_tools_repos(): http_archive( name = "remote_java_tools", sha256 = "b763ee80e5754e593fd6d5be6d7343f905bc8b73d661d36d842b024ca11b6793", @@ -372,7 +372,7 @@ def rules_java_dependencies(): remote_jdk17_repos() # TODO: load this will break compatibility with Bazel 4.2.1 - # java_tools_javac11_repos() + # java_tools_repos() def rules_java_toolchains(name = "toolchains"): """An utility method to load all Java toolchains. diff --git a/toolchains/BUILD b/toolchains/BUILD index 65afb7b..b793746 100644 --- a/toolchains/BUILD +++ b/toolchains/BUILD @@ -313,9 +313,3 @@ java_runtime_version_alias( runtime_version = "remotejdk_17", visibility = ["//visibility:public"], ) - -java_runtime_version_alias( - name = "jdk_8", - runtime_version = "8", - visibility = ["//visibility:public"], -) From d2d3b599ce16a7ac089a7c532c2d3b92416ac377 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Tue, 16 Nov 2021 10:17:44 +0100 Subject: [PATCH 12/15] Address review comments (part 3) --- java/repositories.bzl | 151 +++++++++++++++++++++++++++++++++++------- toolchains/BUILD | 38 ----------- 2 files changed, 126 insertions(+), 63 deletions(-) diff --git a/java/repositories.bzl b/java/repositories.bzl index c2eef07..07f7e0e 100644 --- a/java/repositories.bzl +++ b/java/repositories.bzl @@ -24,11 +24,13 @@ """Development and production dependencies of rules_java.""" load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//toolchains:local_java_repository.bzl", "local_java_repository") load("//toolchains:remote_java_repository.bzl", "remote_java_repository") def java_tools_repos(): - http_archive( + maybe( + http_archive, name = "remote_java_tools", sha256 = "b763ee80e5754e593fd6d5be6d7343f905bc8b73d661d36d842b024ca11b6793", urls = [ @@ -37,7 +39,8 @@ def java_tools_repos(): ], ) - http_archive( + maybe( + http_archive, name = "remote_java_tools_linux", sha256 = "ae1eca4546eac6487c6e565f9b409536609c273207220c51e5c94f2a058a5a56", urls = [ @@ -46,7 +49,8 @@ def java_tools_repos(): ], ) - http_archive( + maybe( + http_archive, name = "remote_java_tools_windows", sha256 = "36766802f7ec684cecb1a14c122428de6be9784e88419e2ab5912ad4b59a8c7d", urls = [ @@ -55,7 +59,8 @@ def java_tools_repos(): ], ) - http_archive( + maybe( + http_archive, name = "remote_java_tools_darwin", sha256 = "792bc1352e736073b152528175ed424687f86a9f6f5f461f07d8b26806762738", urls = [ @@ -65,14 +70,91 @@ def java_tools_repos(): ) def local_jdk_repo(): - local_java_repository( + maybe( + local_java_repository, name = "local_jdk", build_file = Label("//toolchains:jdk.BUILD"), ) +def remote_jdk8_repos(name = ""): + """Imports OpenJDK 8 repositories. + + Args: + name: The name of this macro (not used) + """ + maybe( + remote_java_repository, + name = "remote_jdk8_linux_aarch64", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], + sha256 = "f4072e82faa5a09fab2accf2892d4684324fc999d614583c3ff785e87c03963f", + strip_prefix = "zulu8.50.51.263-ca-jdk8.0.275-linux_aarch64", + urls = [ + "https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zulu8.50.51.263-ca-jdk8.0.275-linux_aarch64.tar.gz", + "https://cdn.azul.com/zulu-embedded/bin/zulu8.50.51.263-ca-jdk8.0.275-linux_aarch64.tar.gz", + ], + version = "8", + ) + maybe( + remote_java_repository, + name = "remote_jdk8_linux", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + sha256 = "1db6b2fa642950ee1b4b1ec2b6bc8a9113d7a4cd723f79398e1ada7dab1c981c", + strip_prefix = "zulu8.50.0.51-ca-jdk8.0.275-linux_x64", + urls = [ + "https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zulu8.50.0.51-ca-jdk8.0.275-linux_x64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu8.50.0.51-ca-jdk8.0.275-linux_x64.tar.gz", + ], + version = "8", + ) + maybe( + remote_java_repository, + name = "remote_jdk8_macos", + exec_compatible_with = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", + ], + sha256 = "b03176597734299c9a15b7c2cc770783cf14d121196196c1248e80c026b59c17", + strip_prefix = "zulu8.50.0.51-ca-jdk8.0.275-macosx_x64", + urls = [ + "https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zulu8.50.0.51-ca-jdk8.0.275-macosx_x64.tar.gz", + "https://cdn.azul.com/zulu/bin/zulu8.50.0.51-ca-jdk8.0.275-macosx_x64.tar.gz", + ], + version = "8", + ) + maybe( + remote_java_repository, + name = "remote_jdk8_windows", + exec_compatible_with = [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", + ], + sha256 = "49759b2bd2ab28231a21ff3a3bb45824ddef55d89b5b1a05a62e26a365da0774", + strip_prefix = "zulu8.50.0.51-ca-jdk8.0.275-win_x64", + urls = [ + "https://mirror.bazel.build/openjdk/azul-zulu-8.50.0.51-ca-jdk8.0.275/zulu8.50.0.51-ca-jdk8.0.275-win_x64.zip", + "https://cdn.azul.com/zulu/bin/zulu8.50.0.51-ca-jdk8.0.275-win_x64.zip", + ], + version = "8", + ) + REMOTE_JDK8_REPOS = [ + "remote_jdk8_linux_aarch64", + "remote_jdk8_linux", + "remote_jdk8_macos", + "remote_jdk8_windows", + ] + for name in REMOTE_JDK8_REPOS: + native.register_toolchains("@" + name + "_toolchain_config_repo//:toolchain") + def remote_jdk11_repos(): """Imports OpenJDK 11 repositories.""" - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk11_linux", exec_compatible_with = [ "@platforms//os:linux", @@ -86,7 +168,8 @@ def remote_jdk11_repos(): version = "11", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk11_linux_aarch64", exec_compatible_with = [ "@platforms//os:linux", @@ -100,7 +183,8 @@ def remote_jdk11_repos(): version = "11", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk11_linux_ppc64le", exec_compatible_with = [ "@platforms//os:linux", @@ -115,7 +199,8 @@ def remote_jdk11_repos(): version = "11", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk11_linux_s390x", exec_compatible_with = [ "@platforms//os:linux", @@ -130,7 +215,8 @@ def remote_jdk11_repos(): version = "11", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk11_macos", exec_compatible_with = [ "@platforms//os:macos", @@ -144,7 +230,8 @@ def remote_jdk11_repos(): version = "11", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk11_macos_aarch64", exec_compatible_with = [ "@platforms//os:macos", @@ -159,7 +246,8 @@ def remote_jdk11_repos(): version = "11", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk11_win", exec_compatible_with = [ "@platforms//os:windows", @@ -175,7 +263,8 @@ def remote_jdk11_repos(): def remote_jdk15_repos(): """Imports OpenJDK 15 repositories.""" - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk15_linux", exec_compatible_with = [ "@platforms//os:linux", @@ -190,7 +279,8 @@ def remote_jdk15_repos(): version = "15", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk15_macos", exec_compatible_with = [ "@platforms//os:macos", @@ -205,7 +295,8 @@ def remote_jdk15_repos(): version = "15", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk15_macos_aarch64", exec_compatible_with = [ "@platforms//os:macos", @@ -220,7 +311,8 @@ def remote_jdk15_repos(): version = "15", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk15_win", exec_compatible_with = [ "@platforms//os:windows", @@ -237,7 +329,8 @@ def remote_jdk15_repos(): def remote_jdk16_repos(): """Imports OpenJDK 16 repositories.""" - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk16_linux", exec_compatible_with = [ "@platforms//os:linux", @@ -252,7 +345,8 @@ def remote_jdk16_repos(): version = "16", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk16_macos", exec_compatible_with = [ "@platforms//os:macos", @@ -267,7 +361,8 @@ def remote_jdk16_repos(): version = "16", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk16_macos_aarch64", exec_compatible_with = [ "@platforms//os:macos", @@ -282,7 +377,8 @@ def remote_jdk16_repos(): version = "16", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk16_win", exec_compatible_with = [ "@platforms//os:windows", @@ -299,7 +395,8 @@ def remote_jdk16_repos(): def remote_jdk17_repos(): """Imports OpenJDK 17 repositories.""" - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk17_linux", exec_compatible_with = [ "@platforms//os:linux", @@ -314,7 +411,8 @@ def remote_jdk17_repos(): version = "17", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk17_macos", exec_compatible_with = [ "@platforms//os:macos", @@ -329,7 +427,8 @@ def remote_jdk17_repos(): version = "17", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk17_macos_aarch64", exec_compatible_with = [ "@platforms//os:macos", @@ -344,7 +443,8 @@ def remote_jdk17_repos(): version = "17", ) - remote_java_repository( + maybe( + remote_java_repository, name = "remotejdk17_win", exec_compatible_with = [ "@platforms//os:windows", @@ -371,7 +471,8 @@ def rules_java_dependencies(): remote_jdk16_repos() remote_jdk17_repos() - # TODO: load this will break compatibility with Bazel 4.2.1 + # TODO: load this will break compatibility with Bazel 4.2.1, + # Enable this when Bazel 5.0.0 is released. # java_tools_repos() def rules_java_toolchains(name = "toolchains"): diff --git a/toolchains/BUILD b/toolchains/BUILD index b793746..3c1f8b2 100644 --- a/toolchains/BUILD +++ b/toolchains/BUILD @@ -251,44 +251,6 @@ default_java_toolchain( toolchain_definition = False, ) -filegroup( - name = "bzl_srcs", - srcs = glob(["*.bzl"]), - visibility = ["//tools:__pkg__"], -) - -# Aliases for JDKs, so that they are only downloaded when needed. -_JDKS = [ - "remotejdk11_macos", - "remotejdk11_macos_aarch64", - "remotejdk11_win", - "remotejdk11_linux_aarch64", - "remotejdk11_linux", - "remotejdk11_linux_ppc64le", - "remotejdk11_linux_s390x", - "remotejdk15_macos", - "remotejdk15_macos_aarch64", - "remotejdk15_win", - "remotejdk15_linux", - "remotejdk16_macos", - "remotejdk16_macos_aarch64", - "remotejdk16_win", - "remotejdk16_linux", - "remotejdk17_macos", - "remotejdk17_macos_aarch64", - "remotejdk17_win", - "remotejdk17_linux", -] - -[ - alias( - name = JDK, - actual = "@%s//:jdk" % JDK, - visibility = ["//visibility:private"], - ) - for JDK in _JDKS -] - # A JDK 11 for use as a --host_javabase. java_runtime_version_alias( name = "remote_jdk11", From d69177cc12f1db1a32e669fd8524cf9d0765ef18 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Tue, 16 Nov 2021 10:25:19 +0100 Subject: [PATCH 13/15] Small fixes --- MODULE.bazel | 1 - WORKSPACE.bzlmod | 3 ++- java/extensions.bzl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index fdb4090..5f0377e 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -25,7 +25,6 @@ module( bazel_dep(name = "platforms", version = "0.0.4") bazel_dep(name = "rules_cc", version = "0.0.1") # rules_proto is required by @remote_java_tools, which is loaded via module extension. -# TODO: perhaps java tools should have its own Bazel module, then we don't need this in rules_java. bazel_dep(name = "rules_proto", version = "4.0.0") toolchains = use_extension("//java:extensions.bzl", "toolchains") diff --git a/WORKSPACE.bzlmod b/WORKSPACE.bzlmod index 8b8fba4..b021fa6 100644 --- a/WORKSPACE.bzlmod +++ b/WORKSPACE.bzlmod @@ -1 +1,2 @@ -# A completely empty WORKSPACE file to replace the original WORKSPACE content +# A completely empty WORKSPACE file to replace the original WORKSPACE content when enabling Bzlmod. +# No WORKSPACE prefix or suffix are added for this file. diff --git a/java/extensions.bzl b/java/extensions.bzl index 5ec4036..899f22e 100644 --- a/java/extensions.bzl +++ b/java/extensions.bzl @@ -16,8 +16,8 @@ load("//java:repositories.bzl", "java_tools_repos", "local_jdk_repo", "remote_jdk11_repos", "remote_jdk15_repos", "remote_jdk16_repos", "remote_jdk17_repos") def _toolchains_impl(ctx): - local_jdk_repo() java_tools_repos() + local_jdk_repo() remote_jdk11_repos() remote_jdk15_repos() remote_jdk16_repos() From b4de2cc051d6d8dd6c3aa71f06534142d5484129 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Tue, 16 Nov 2021 14:02:28 +0100 Subject: [PATCH 14/15] New line at HelloWorld.java --- examples/hello-world/HelloWorld.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello-world/HelloWorld.java b/examples/hello-world/HelloWorld.java index 7956769..20331e4 100644 --- a/examples/hello-world/HelloWorld.java +++ b/examples/hello-world/HelloWorld.java @@ -16,4 +16,4 @@ public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World!\n"); } -} \ No newline at end of file +} From 21e04534c06542fcd608e9c98e6d77b732a680a7 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Tue, 16 Nov 2021 14:30:54 +0100 Subject: [PATCH 15/15] Rename hello world package --- examples/{hello-world => hello_world}/BUILD | 2 +- examples/{hello-world => hello_world}/HelloWorld.java | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename examples/{hello-world => hello_world}/BUILD (81%) rename examples/{hello-world => hello_world}/HelloWorld.java (100%) diff --git a/examples/hello-world/BUILD b/examples/hello_world/BUILD similarity index 81% rename from examples/hello-world/BUILD rename to examples/hello_world/BUILD index 8fe9eef..96b2a5f 100644 --- a/examples/hello-world/BUILD +++ b/examples/hello_world/BUILD @@ -1,7 +1,7 @@ load("//java:defs.bzl", "java_binary") java_binary( - name = "hello-world", + name = "hello_world", srcs = ["HelloWorld.java"], main_class = "HelloWorld", ) diff --git a/examples/hello-world/HelloWorld.java b/examples/hello_world/HelloWorld.java similarity index 100% rename from examples/hello-world/HelloWorld.java rename to examples/hello_world/HelloWorld.java