Add a repository rule to proxy cc rules for maintaining compatibility with older Bazel versions
PiperOrigin-RevId: 785849527
Change-Id: I6a07e71dbeb72191443602d5f5f0a72927505dea
diff --git a/MODULE.bazel b/MODULE.bazel
index af3f1cd..c8e4f30 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -21,3 +21,7 @@
bazel_dep(name = "googletest", version = "1.15.2", dev_dependency = True)
bazel_dep(name = "rules_testing", version = "0.6.0", dev_dependency = True)
bazel_dep(name = "stardoc", version = "0.8.0", dev_dependency = True)
+
+# Compatibility layer
+compat = use_extension("//cc:extensions.bzl", "compatibility_proxy")
+use_repo(compat, "cc_compatibility_proxy")
diff --git a/WORKSPACE b/WORKSPACE
index ce6d15d..e11bae4 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -57,3 +57,18 @@
strip_prefix = "googletest-1.15.2",
url = "https://github.com/google/googletest/releases/download/v1.15.2/googletest-1.15.2.tar.gz",
)
+
+http_archive(
+ name = "bazel_features",
+ sha256 = "07bd2b18764cdee1e0d6ff42c9c0a6111ffcbd0c17f0de38e7f44f1519d1c0cd",
+ strip_prefix = "bazel_features-1.32.0",
+ url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.32.0/bazel_features-v1.32.0.tar.gz",
+)
+
+load("@bazel_features//:deps.bzl", "bazel_features_deps")
+
+bazel_features_deps()
+
+load("//cc:extensions.bzl", "compatibility_proxy_repo")
+
+compatibility_proxy_repo()
diff --git a/cc/cc_binary.bzl b/cc/cc_binary.bzl
index 0443038..ffaa0ce 100644
--- a/cc/cc_binary.bzl
+++ b/cc/cc_binary.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""cc_binary rule"""
-load("//cc/private/rules_impl:cc_binary.bzl", _cc_binary = "cc_binary")
+load("@cc_compatibility_proxy//:proxy.bzl", _cc_binary = "cc_binary")
# TODO(bazel-team): To avoid breaking changes, if the below are no longer
# forwarding to native rules, flag @bazel_tools//tools/cpp:link_extra_libs
diff --git a/cc/cc_import.bzl b/cc/cc_import.bzl
index 70f32f0..1762436 100644
--- a/cc/cc_import.bzl
+++ b/cc/cc_import.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""cc_import rule"""
-load("//cc/private/rules_impl:cc_import.bzl", _cc_import = "cc_import")
+load("@cc_compatibility_proxy//:proxy.bzl", _cc_import = "cc_import")
def cc_import(**kwargs):
_cc_import(**kwargs)
diff --git a/cc/cc_library.bzl b/cc/cc_library.bzl
index 2ef2833..c45c5fd 100644
--- a/cc/cc_library.bzl
+++ b/cc/cc_library.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""cc_library rule"""
-load("//cc/private/rules_impl:cc_library.bzl", _cc_library = "cc_library")
+load("@cc_compatibility_proxy//:proxy.bzl", _cc_library = "cc_library")
def cc_library(**kwargs):
_cc_library(**kwargs)
diff --git a/cc/cc_shared_library.bzl b/cc/cc_shared_library.bzl
index 7e6b08e..2cbc47f 100644
--- a/cc/cc_shared_library.bzl
+++ b/cc/cc_shared_library.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""cc_library rule"""
-load("//cc/private/rules_impl:cc_shared_library.bzl", _cc_shared_library = "cc_shared_library")
+load("@cc_compatibility_proxy//:proxy.bzl", _cc_shared_library = "cc_shared_library")
def cc_shared_library(**kwargs):
_cc_shared_library(**kwargs)
diff --git a/cc/cc_static_library.bzl b/cc/cc_static_library.bzl
index b7cc9f5..2db2bdb 100644
--- a/cc/cc_static_library.bzl
+++ b/cc/cc_static_library.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""cc_library rule"""
-load("//cc/private/rules_impl:cc_static_library.bzl", _cc_static_library = "cc_static_library")
+load("@cc_compatibility_proxy//:proxy.bzl", _cc_static_library = "cc_static_library")
def cc_static_library(**kwargs):
_cc_static_library(**kwargs)
diff --git a/cc/cc_test.bzl b/cc/cc_test.bzl
index 75ed83d..533bdb8 100644
--- a/cc/cc_test.bzl
+++ b/cc/cc_test.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""cc_test rule"""
-load("//cc/private/rules_impl:cc_test.bzl", _cc_test = "cc_test")
+load("@cc_compatibility_proxy//:proxy.bzl", _cc_test = "cc_test")
# TODO(bazel-team): To avoid breaking changes, if the below are no longer
# forwarding to native rules, flag @bazel_tools//tools/cpp:link_extra_libs
diff --git a/cc/extensions.bzl b/cc/extensions.bzl
index 0d73141..4b06794 100644
--- a/cc/extensions.bzl
+++ b/cc/extensions.bzl
@@ -25,3 +25,67 @@
return None
cc_configure_extension = module_extension(implementation = _cc_configure_extension_impl)
+
+def _compatibility_proxy_repo_impl(rctx):
+ rctx.file("BUILD", "")
+ bazel = native.bazel_version
+ if not bazel or bazel >= "9":
+ rctx.file(
+ "proxy.bzl",
+ """
+load("@rules_cc//cc/private/rules_impl:cc_binary.bzl", _cc_binary = "cc_binary")
+load("@rules_cc//cc/private/rules_impl:cc_import.bzl", _cc_import = "cc_import")
+load("@rules_cc//cc/private/rules_impl:cc_library.bzl", _cc_library = "cc_library")
+load("@rules_cc//cc/private/rules_impl:cc_shared_library.bzl", _cc_shared_library = "cc_shared_library")
+load("@rules_cc//cc/private/rules_impl:cc_static_library.bzl", _cc_static_library = "cc_static_library")
+load("@rules_cc//cc/private/rules_impl:cc_test.bzl", _cc_test = "cc_test")
+load("@rules_cc//cc/private/rules_impl:objc_import.bzl", _objc_import = "objc_import")
+load("@rules_cc//cc/private/rules_impl:objc_library.bzl", _objc_library = "objc_library")
+
+cc_binary = _cc_binary
+cc_import = _cc_import
+cc_library = _cc_library
+cc_shared_library = _cc_shared_library
+cc_static_library = _cc_static_library
+cc_test = _cc_test
+objc_import = _objc_import
+objc_library = _objc_library
+ """,
+ )
+ else:
+ rctx.file(
+ "proxy.bzl",
+ """
+cc_binary = native.cc_binary
+cc_import = native.cc_import
+cc_library = native.cc_library
+cc_shared_library = native.cc_shared_library
+cc_static_library = getattr(native, "cc_static_library", None) # only in Bazel 8+
+cc_test = native.cc_test
+objc_import = native.objc_import
+objc_library = native.objc_library
+ """,
+ )
+
+_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():
+ _compatibility_proxy_repo_rule(name = "cc_compatibility_proxy")
+
+def _compat_proxy_impl(module_ctx):
+ compatibility_proxy_repo()
+
+ # module_ctx.extension_metadata has the paramater `reproducible` as of Bazel 7.1.0. We can't
+ # test for it directly and would ideally use bazel_features to check for it, but don't want
+ # to add a dependency for as long as WORKSPACE is still around. Thus, test for it by
+ # checking the availability of another feature introduced in 7.1.0.
+ if hasattr(module_ctx, "watch"):
+ return module_ctx.extension_metadata(reproducible = True)
+ else:
+ return None
+
+compatibility_proxy = module_extension(_compat_proxy_impl)
diff --git a/cc/objc_import.bzl b/cc/objc_import.bzl
index 02117a4..465ffd8 100644
--- a/cc/objc_import.bzl
+++ b/cc/objc_import.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""objc_import rule"""
-load("//cc/private/rules_impl:objc_import.bzl", _objc_import = "objc_import")
+load("@cc_compatibility_proxy//:proxy.bzl", _objc_import = "objc_import")
def objc_import(**kwargs):
_objc_import(**kwargs)
diff --git a/cc/objc_library.bzl b/cc/objc_library.bzl
index 09118fc..3448f4e 100644
--- a/cc/objc_library.bzl
+++ b/cc/objc_library.bzl
@@ -13,7 +13,7 @@
# limitations under the License.
"""objc_library rule"""
-load("//cc/private/rules_impl:objc_library.bzl", _objc_library = "objc_library")
+load("@cc_compatibility_proxy//:proxy.bzl", _objc_library = "objc_library")
def objc_library(**kwargs):
_objc_library(**kwargs)