From 70970af9590a49b310312b2d01e85ac4fb72fff7 Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 12 Sep 2024 07:57:23 -0700 Subject: [PATCH] Switch `rules_java` rule macro redirects from `native.*` to those defined locally Using the rules requires a bazel version that contains all the changes in https://github.com/bazelbuild/bazel/pull/23591 PiperOrigin-RevId: 673847088 Change-Id: Iaaf824fbae4dced297baa6a336b2e7b8b6c3ccb5 --- .bazelci/presubmit.yml | 74 +++++++++++---------- .bazelrc | 6 +- MODULE.bazel | 10 ++- java/BUILD | 5 ++ java/bazel/BUILD.bazel | 6 ++ java/bazel/common/BUILD.bazel | 7 ++ java/common/rules/BUILD | 4 +- java/common/rules/impl/BUILD | 18 +++++ java/common/rules/impl/java_binary_impl.bzl | 2 +- java/java_binary.bzl | 5 +- java/java_import.bzl | 5 +- java/java_library.bzl | 5 +- java/java_plugin.bzl | 5 +- java/java_test.bzl | 5 +- test/repo/.bazelrc | 7 ++ test/repo/BUILD.bazel | 8 ++- test/repo/MODULE.bazel | 39 +++++++++++ test/repo/WORKSPACE.bzlmod | 5 -- 18 files changed, 159 insertions(+), 57 deletions(-) create mode 100644 test/repo/.bazelrc diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 1a493180..179245e5 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -21,11 +21,17 @@ test_targets: &test_targets buildifier: latest tasks: +# TODO: add config for 8.0.0 once released +# Bazel 7.x +# TODO: fix versions after 7.4.0 ubuntu2004: + name: "Bazel 7.x" + bazel: "9e3fa2a2e98db4aa10918a981325cfa9473e4ed8" build_targets: *build_targets test_targets: *test_targets ubuntu2004_integration: - name: "Integration Tests" + name: "Bazel 7.x Integration" + bazel: "9e3fa2a2e98db4aa10918a981325cfa9473e4ed8" platform: ubuntu2004 working_directory: "test/repo" shell_commands: @@ -35,14 +41,30 @@ tasks: - "//:bin_deploy.jar" - "@rules_java//java/..." macos: + name: "Bazel 7.x" + bazel: "9e3fa2a2e98db4aa10918a981325cfa9473e4ed8" build_targets: *build_targets windows: + name: "Bazel 7.x" + bazel: "9e3fa2a2e98db4aa10918a981325cfa9473e4ed8" build_targets: *build_targets +# Bazel@HEAD ubuntu2004_head: name: "Bazel@HEAD" bazel: last_green platform: ubuntu2004 build_targets: *build_targets + ubuntu2004_integration_head: + name: "Bazel@HEAD Integration" + bazel: last_green + platform: ubuntu2004 + working_directory: "test/repo" + shell_commands: + - sh setup.sh + build_targets: + - "//..." + - "//:bin_deploy.jar" + - "@rules_java//java/..." macos_head: name: "Bazel@HEAD" bazel: last_green @@ -53,36 +75,20 @@ tasks: bazel: last_green platform: windows build_targets: *build_targets - ubuntu2004_bzlmod: - name: "bzlmod" - platform: ubuntu2004 - build_flags: - - "--config=bzlmod" - build_targets: *build_targets_bzlmod - macos_bzlmod: - name: "bzlmod" - platform: macos - build_flags: - - "--config=bzlmod" - build_targets: *build_targets_bzlmod - windows_bzlmod: - name: "bzlmod" - platform: windows - build_flags: - - "--config=bzlmod" - build_targets: *build_targets_bzlmod - ubuntu2004_bazel630: - name: "Bazel 6.3.0" - bazel: 6.3.0 - platform: ubuntu2004 - build_targets: *build_targets - macos_bazel630: - name: "Bazel 6.3.0" - bazel: 6.3.0 - platform: macos - build_targets: *build_targets - windows_bazel630: - name: "Bazel 6.3.0" - bazel: 6.3.0 - platform: windows - build_targets: *build_targets +# Bazel 6.x +# TODO: re-enable if possible to redirect to native rule impl +# ubuntu2004_bazel630: +# name: "Bazel 6.3.0" +# bazel: 6.3.0 +# platform: ubuntu2004 +# build_targets: *build_targets +# macos_bazel630: +# name: "Bazel 6.3.0" +# bazel: 6.3.0 +# platform: macos +# build_targets: *build_targets +# windows_bazel630: +# name: "Bazel 6.3.0" +# bazel: 6.3.0 +# platform: windows +# build_targets: *build_targets diff --git a/.bazelrc b/.bazelrc index 5d84eb8f..03c47778 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,3 +1,5 @@ -build:bzlmod --experimental_enable_bzlmod +common --incompatible_disallow_empty_glob +common --experimental_rule_extension_api -common --incompatible_disallow_empty_glob \ No newline at end of file +# TODO: remove once https://github.com/bazelbuild/bazel/commit/384e381fa841537987691f170ed9408dcfeed2ea is available +common --experimental_google_legacy_api diff --git a/MODULE.bazel b/MODULE.bazel index f66bbd18..42fffc53 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -11,7 +11,7 @@ bazel_dep(name = "bazel_features", version = "1.11.0") bazel_dep(name = "bazel_skylib", version = "1.6.1") # Required by @remote_java_tools, which is loaded via module extension. -bazel_dep(name = "rules_proto", version = "4.0.0") +bazel_dep(name = "rules_proto", version = "6.0.2") bazel_dep(name = "rules_license", version = "0.0.3") bazel_dep(name = "abseil-cpp", version = "20230802.1", repo_name = "com_google_absl") @@ -97,3 +97,11 @@ single_version_override( module_name = "rules_python", version = "0.24.0", ) + +# TODO: drop this & update version from BCR above +archive_override( + module_name = "rules_cc", + integrity = "sha256-ZvxtrlaB2jZk56tUDasvs4b1ThcPMaEa64u+MZSwf1E=", + strip_prefix = "rules_cc-0.0.10-rc2", + urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.10-rc2/rules_cc-0.0.10-rc2.tar.gz"], +) diff --git a/java/BUILD b/java/BUILD index 77848c92..5660699f 100644 --- a/java/BUILD +++ b/java/BUILD @@ -7,6 +7,8 @@ licenses(["notice"]) filegroup( name = "srcs", srcs = glob(["**"]) + [ + "//java/bazel:srcs", + "//java/bazel/common:srcs", "//java/bazel/rules:srcs", "//java/common:srcs", "//java/private:srcs", @@ -39,8 +41,10 @@ bzl_library( ], visibility = ["//visibility:public"], deps = [ + "//java/bazel/common", # copybara-use-repo-external-label "//java/bazel/rules", # copybara-use-repo-external-label "//java/common", + "//java/common/rules:core_rules", "//java/private", ], ) @@ -66,6 +70,7 @@ filegroup( ":core_rules", ":java_single_jar", ":rules", + "//java/bazel:for_bazel_tests", # copybara-use-repo-external-label "//java/bazel/rules:for_bazel_tests", # copybara-use-repo-external-label "//java/common:for_bazel_tests", "//java/private:for_bazel_tests", diff --git a/java/bazel/BUILD.bazel b/java/bazel/BUILD.bazel index c3abda26..21bd34c7 100644 --- a/java/bazel/BUILD.bazel +++ b/java/bazel/BUILD.bazel @@ -59,3 +59,9 @@ filegroup( testonly = 1, visibility = ["//java:__pkg__"], ) + +filegroup( + name = "srcs", + srcs = glob(["**"]), + visibility = ["//java:__pkg__"], +) diff --git a/java/bazel/common/BUILD.bazel b/java/bazel/common/BUILD.bazel index c0237707..dd3bc064 100644 --- a/java/bazel/common/BUILD.bazel +++ b/java/bazel/common/BUILD.bazel @@ -3,4 +3,11 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") bzl_library( name = "common", srcs = glob(["*.bzl"]), + visibility = ["//java:__pkg__"], +) + +filegroup( + name = "srcs", + srcs = glob(["**"]), + visibility = ["//java:__pkg__"], ) diff --git a/java/common/rules/BUILD b/java/common/rules/BUILD index 5bbc94ed..33f6eca9 100644 --- a/java/common/rules/BUILD +++ b/java/common/rules/BUILD @@ -39,10 +39,9 @@ bzl_library( deps = [ ":android_lint_bzl", ":rule_util_bzl", - # TODO: re-enable after rules_cc release - # "@rules_cc//cc/common", "//java/common", "@bazel_skylib//lib:paths", + "@rules_cc//cc/common", ], ) @@ -52,6 +51,7 @@ filegroup( srcs = [ "BUILD", ":core_rules", + "//java/common/rules/impl:for_bazel_tests", ], visibility = ["//java/common:__pkg__"], ) diff --git a/java/common/rules/impl/BUILD b/java/common/rules/impl/BUILD index e755396a..f9c86267 100644 --- a/java/common/rules/impl/BUILD +++ b/java/common/rules/impl/BUILD @@ -1,3 +1,5 @@ +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + package(default_visibility = ["//visibility:public"]) filegroup( @@ -5,3 +7,19 @@ filegroup( srcs = glob(["**"]), visibility = ["//java/common/rules:__pkg__"], ) + +bzl_library( + name = "impl", + srcs = glob(["*.bzl"]), + visibility = ["//java:__subpackages__"], +) + +filegroup( + name = "for_bazel_tests", + testonly = 1, + srcs = [ + "BUILD", + ":impl", + ], + visibility = ["//java/common/rules:__pkg__"], +) diff --git a/java/common/rules/impl/java_binary_impl.bzl b/java/common/rules/impl/java_binary_impl.bzl index 03f2cbe2..dd402c78 100644 --- a/java/common/rules/impl/java_binary_impl.bzl +++ b/java/common/rules/impl/java_binary_impl.bzl @@ -16,11 +16,11 @@ load("@rules_cc//cc/common:cc_common.bzl", "cc_common") load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") +load("@rules_proto//proto:defs.bzl", "ProtoInfo") load("//java/common:java_common.bzl", "java_common") load("//java/common:java_info.bzl", "JavaInfo") load("//java/common:java_semantics.bzl", "semantics") load("//java/common/rules/impl:basic_java_library_impl.bzl", "basic_java_library", "collect_deps") -load("//third_party/protobuf/bazel/common:proto_info.bzl", "ProtoInfo") load(":java_binary_deploy_jar.bzl", "create_deploy_archive") load(":java_helper.bzl", "helper") diff --git a/java/java_binary.bzl b/java/java_binary.bzl index 7184ecbd..18a3fed4 100644 --- a/java/java_binary.bzl +++ b/java/java_binary.bzl @@ -13,6 +13,8 @@ # limitations under the License. """java_binary rule""" +load("//java/bazel/rules:bazel_java_binary_wrapper.bzl", _java_binary = "java_binary") + def java_binary(**attrs): """Bazel java_binary rule. @@ -22,5 +24,4 @@ def java_binary(**attrs): **attrs: Rule attributes """ - # buildifier: disable=native-java - native.java_binary(**attrs) + _java_binary(**attrs) diff --git a/java/java_import.bzl b/java/java_import.bzl index 926c37a6..89f60309 100644 --- a/java/java_import.bzl +++ b/java/java_import.bzl @@ -13,6 +13,8 @@ # limitations under the License. """java_import rule""" +load("//java/bazel/rules:bazel_java_import.bzl", _java_import = "java_import") + def java_import(**attrs): """Bazel java_import rule. @@ -22,5 +24,4 @@ def java_import(**attrs): **attrs: Rule attributes """ - # buildifier: disable=native-java - native.java_import(**attrs) + _java_import(**attrs) diff --git a/java/java_library.bzl b/java/java_library.bzl index 9308bbc4..90c72676 100644 --- a/java/java_library.bzl +++ b/java/java_library.bzl @@ -13,6 +13,8 @@ # limitations under the License. """java_library rule""" +load("//java/bazel/rules:bazel_java_library.bzl", _java_library = "java_library") + def java_library(**attrs): """Bazel java_library rule. @@ -22,5 +24,4 @@ def java_library(**attrs): **attrs: Rule attributes """ - # buildifier: disable=native-java - native.java_library(**attrs) + _java_library(**attrs) diff --git a/java/java_plugin.bzl b/java/java_plugin.bzl index d12e0e18..130dc625 100644 --- a/java/java_plugin.bzl +++ b/java/java_plugin.bzl @@ -13,6 +13,8 @@ # limitations under the License. """java_plugin rule""" +load("//java/bazel/rules:bazel_java_plugin.bzl", _java_plugin = "java_plugin") + def java_plugin(**attrs): """Bazel java_plugin rule. @@ -22,5 +24,4 @@ def java_plugin(**attrs): **attrs: Rule attributes """ - # buildifier: disable=native-java - native.java_plugin(**attrs) + _java_plugin(**attrs) diff --git a/java/java_test.bzl b/java/java_test.bzl index 0aeac91b..48a66551 100644 --- a/java/java_test.bzl +++ b/java/java_test.bzl @@ -13,6 +13,8 @@ # limitations under the License. """java_test rule""" +load("//java/bazel/rules:bazel_java_test.bzl", _java_test = "java_test") + def java_test(**attrs): """Bazel java_test rule. @@ -22,5 +24,4 @@ def java_test(**attrs): **attrs: Rule attributes """ - # buildifier: disable=native-java - native.java_test(**attrs) + _java_test(**attrs) diff --git a/test/repo/.bazelrc b/test/repo/.bazelrc new file mode 100644 index 00000000..1146197f --- /dev/null +++ b/test/repo/.bazelrc @@ -0,0 +1,7 @@ +build:bzlmod --experimental_enable_bzlmod + +common --incompatible_disallow_empty_glob +common --experimental_rule_extension_api + +# TODO: remove once https://github.com/bazelbuild/bazel/commit/384e381fa841537987691f170ed9408dcfeed2ea is available +common --experimental_google_legacy_api diff --git a/test/repo/BUILD.bazel b/test/repo/BUILD.bazel index 1e71e527..6b7da400 100644 --- a/test/repo/BUILD.bazel +++ b/test/repo/BUILD.bazel @@ -1,5 +1,5 @@ -load("@rules_java//java:java_binary.bzl", "java_binary") # copybara-use-repo-external-label -load("@rules_java//java:java_library.bzl", "java_library") # copybara-use-repo-external-label +load("@rules_java//java:defs.bzl", "java_binary", "java_library") # copybara-use-repo-external-label +load("@rules_java//toolchains:default_java_toolchain.bzl", "default_java_toolchain") # copybara-use-repo-external-label java_library( name = "lib", @@ -11,3 +11,7 @@ java_binary( main_class = "Main", runtime_deps = [":lib"], ) + +default_java_toolchain( + name = "my_funky_toolchain", +) diff --git a/test/repo/MODULE.bazel b/test/repo/MODULE.bazel index 9289fcd5..c4f81a40 100644 --- a/test/repo/MODULE.bazel +++ b/test/repo/MODULE.bazel @@ -5,3 +5,42 @@ archive_override( module_name = "rules_java", urls = ["file:///tmp/rules_java-HEAD.tar.gz"], ) + +java_toolchains = use_extension("@rules_java//java:extensions.bzl", "toolchains") +use_repo( + java_toolchains, + "local_jdk", + "remote_java_tools", + "remote_java_tools_darwin_arm64", + "remote_java_tools_darwin_x86_64", + "remote_java_tools_linux", + "remote_java_tools_windows", + "remotejdk11_linux", + "remotejdk11_linux_aarch64", + "remotejdk11_linux_ppc64le", + "remotejdk11_linux_s390x", + "remotejdk11_macos", + "remotejdk11_macos_aarch64", + "remotejdk11_win", + "remotejdk11_win_arm64", + "remotejdk17_linux", + "remotejdk17_linux_s390x", + "remotejdk17_macos", + "remotejdk17_macos_aarch64", + "remotejdk17_win", + "remotejdk17_win_arm64", + "remotejdk21_linux", + "remotejdk21_macos", + "remotejdk21_macos_aarch64", + "remotejdk21_win", +) + +# TODO: drop this & update version from BCR above +archive_override( + module_name = "rules_cc", + integrity = "sha256-ZvxtrlaB2jZk56tUDasvs4b1ThcPMaEa64u+MZSwf1E=", + strip_prefix = "rules_cc-0.0.10-rc2", + urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.10-rc2/rules_cc-0.0.10-rc2.tar.gz"], +) + +register_toolchains("//:all") diff --git a/test/repo/WORKSPACE.bzlmod b/test/repo/WORKSPACE.bzlmod index 67a41c3f..e69de29b 100644 --- a/test/repo/WORKSPACE.bzlmod +++ b/test/repo/WORKSPACE.bzlmod @@ -1,5 +0,0 @@ -load("@rules_java//java:repositories.bzl", "rules_java_dependencies", "rules_java_toolchains") - -rules_java_dependencies() - -rules_java_toolchains()