Add an internal module extension to support Bazel 7 (and possibly 6 as well)

Allows dropping use of `--experimental_rule_extension_api`

PiperOrigin-RevId: 691029609
Change-Id: Ica6e9168d6403e84b51870c312cfc6fa6c75cdc5
diff --git a/.bazelrc b/.bazelrc
index b55a2af..5ef382d 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,2 +1 @@
 common --incompatible_disallow_empty_glob
-common --experimental_rule_extension_api
diff --git a/MODULE.bazel b/MODULE.bazel
index 91262ab..0b4dcde 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -92,3 +92,7 @@
 # Dev dependencies
 bazel_dep(name = "rules_pkg", version = "0.9.1", dev_dependency = True)
 bazel_dep(name = "stardoc", version = "0.7.1", dev_dependency = True)
+
+# Compatibility layer
+compat = use_extension("//java:extensions.bzl", "compatibility_proxy")
+use_repo(compat, "compatibility_proxy")
diff --git a/java/extensions.bzl b/java/extensions.bzl
index f456f3f..0530aa2 100644
--- a/java/extensions.bzl
+++ b/java/extensions.bzl
@@ -16,6 +16,7 @@
 load("@bazel_features//:features.bzl", "bazel_features")
 load(
     "//java:repositories.bzl",
+    "compatibility_proxy_repo",
     "java_tools_repos",
     "local_jdk_repo",
     "remote_jdk11_repos",
@@ -38,3 +39,8 @@
         return None
 
 toolchains = module_extension(_toolchains_impl)
+
+def _compat_proxy_impl(_unused):
+    compatibility_proxy_repo()
+
+compatibility_proxy = module_extension(_compat_proxy_impl)
diff --git a/java/java_binary.bzl b/java/java_binary.bzl
index 18a3fed..3c86ed2 100644
--- a/java/java_binary.bzl
+++ b/java/java_binary.bzl
@@ -13,7 +13,7 @@
 # limitations under the License.
 """java_binary rule"""
 
-load("//java/bazel/rules:bazel_java_binary_wrapper.bzl", _java_binary = "java_binary")
+load("@compatibility_proxy//:proxy.bzl", _java_binary = "java_binary")
 
 def java_binary(**attrs):
     """Bazel java_binary rule.
diff --git a/java/java_import.bzl b/java/java_import.bzl
index 89f6030..7dfef83 100644
--- a/java/java_import.bzl
+++ b/java/java_import.bzl
@@ -13,7 +13,7 @@
 # limitations under the License.
 """java_import rule"""
 
-load("//java/bazel/rules:bazel_java_import.bzl", _java_import = "java_import")
+load("@compatibility_proxy//:proxy.bzl", _java_import = "java_import")
 
 def java_import(**attrs):
     """Bazel java_import rule.
diff --git a/java/java_library.bzl b/java/java_library.bzl
index 90c7267..81e0ba1 100644
--- a/java/java_library.bzl
+++ b/java/java_library.bzl
@@ -13,7 +13,7 @@
 # limitations under the License.
 """java_library rule"""
 
-load("//java/bazel/rules:bazel_java_library.bzl", _java_library = "java_library")
+load("@compatibility_proxy//:proxy.bzl", _java_library = "java_library")
 
 def java_library(**attrs):
     """Bazel java_library rule.
diff --git a/java/java_plugin.bzl b/java/java_plugin.bzl
index 130dc62..e73843a 100644
--- a/java/java_plugin.bzl
+++ b/java/java_plugin.bzl
@@ -13,7 +13,7 @@
 # limitations under the License.
 """java_plugin rule"""
 
-load("//java/bazel/rules:bazel_java_plugin.bzl", _java_plugin = "java_plugin")
+load("@compatibility_proxy//:proxy.bzl", _java_plugin = "java_plugin")
 
 def java_plugin(**attrs):
     """Bazel java_plugin rule.
diff --git a/java/java_test.bzl b/java/java_test.bzl
index 48a6655..d11f10b 100644
--- a/java/java_test.bzl
+++ b/java/java_test.bzl
@@ -13,7 +13,7 @@
 # limitations under the License.
 """java_test rule"""
 
-load("//java/bazel/rules:bazel_java_test.bzl", _java_test = "java_test")
+load("@compatibility_proxy//:proxy.bzl", _java_test = "java_test")
 
 def java_test(**attrs):
     """Bazel java_test rule.
diff --git a/java/repositories.bzl b/java/repositories.bzl
index 50e7b76..712560f 100644
--- a/java/repositories.bzl
+++ b/java/repositories.bzl
@@ -390,3 +390,45 @@
                 "@" + item.name + "_toolchain_config_repo//:toolchain",
                 "@" + item.name + "_toolchain_config_repo//:bootstrap_runtime_toolchain",
             )
+
+def _compatibility_proxy_repo_impl(rctx):
+    # TODO: use @bazel_features
+    bazel = native.bazel_version
+    rctx.file("BUILD.bazel", "")
+    if not bazel or bazel >= "8":
+        rctx.file(
+            "proxy.bzl",
+            """
+load("@rules_java//java/bazel/rules:bazel_java_binary_wrapper.bzl", _java_binary = "java_binary") # copybara-use-repo-external-label
+load("@rules_java//java/bazel/rules:bazel_java_import.bzl", _java_import = "java_import") # copybara-use-repo-external-label
+load("@rules_java//java/bazel/rules:bazel_java_library.bzl", _java_library = "java_library") # copybara-use-repo-external-label
+load("@rules_java//java/bazel/rules:bazel_java_plugin.bzl", _java_plugin = "java_plugin") # copybara-use-repo-external-label
+load("@rules_java//java/bazel/rules:bazel_java_test.bzl", _java_test = "java_test") # copybara-use-repo-external-label
+
+java_binary = _java_binary
+java_import = _java_import
+java_library = _java_library
+java_plugin = _java_plugin
+java_test = _java_test
+            """,
+        )
+    else:
+        rctx.file(
+            "proxy.bzl",
+            """
+java_binary = native.java_binary
+java_import = native.java_import
+java_library = native.java_library
+java_plugin = native.java_plugin
+java_test = native.java_test
+            """,
+        )
+
+_compatibility_proxy_repo_rule = repository_rule(
+    _compatibility_proxy_repo_impl,
+    # force reruns on server restarts to use correct native.bazel_version
+    local = True,
+)
+
+def compatibility_proxy_repo():
+    maybe(_compatibility_proxy_repo_rule, name = "compatibility_proxy")
diff --git a/test/repo/.bazelrc b/test/repo/.bazelrc
index 0a78015..e8aef35 100644
--- a/test/repo/.bazelrc
+++ b/test/repo/.bazelrc
@@ -1,4 +1,3 @@
 build:bzlmod --experimental_enable_bzlmod
 
 common --incompatible_disallow_empty_glob
-common --experimental_rule_extension_api