Skip to content

Commit 2b15b96

Browse files
hvadehracopybara-github
authored andcommitted
Add a repository rule to proxy cc rules for maintaining compatibility with older Bazel versions
PiperOrigin-RevId: 785849527 Change-Id: I6a07e71dbeb72191443602d5f5f0a72927505dea
1 parent cbee84a commit 2b15b96

File tree

11 files changed

+91
-8
lines changed

11 files changed

+91
-8
lines changed

MODULE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ bazel_dep(name = "rules_shell", version = "0.2.0", dev_dependency = True)
2121
bazel_dep(name = "googletest", version = "1.15.2", dev_dependency = True)
2222
bazel_dep(name = "rules_testing", version = "0.6.0", dev_dependency = True)
2323
bazel_dep(name = "stardoc", version = "0.8.0", dev_dependency = True)
24+
25+
# Compatibility layer
26+
compat = use_extension("//cc:extensions.bzl", "compatibility_proxy")
27+
use_repo(compat, "cc_compatibility_proxy")

WORKSPACE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,18 @@ http_archive(
5757
strip_prefix = "googletest-1.15.2",
5858
url = "https://github.com/google/googletest/releases/download/v1.15.2/googletest-1.15.2.tar.gz",
5959
)
60+
61+
http_archive(
62+
name = "bazel_features",
63+
sha256 = "07bd2b18764cdee1e0d6ff42c9c0a6111ffcbd0c17f0de38e7f44f1519d1c0cd",
64+
strip_prefix = "bazel_features-1.32.0",
65+
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.32.0/bazel_features-v1.32.0.tar.gz",
66+
)
67+
68+
load("@bazel_features//:deps.bzl", "bazel_features_deps")
69+
70+
bazel_features_deps()
71+
72+
load("//cc:extensions.bzl", "compatibility_proxy_repo")
73+
74+
compatibility_proxy_repo()

cc/cc_binary.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""cc_binary rule"""
1515

16-
load("//cc/private/rules_impl:cc_binary.bzl", _cc_binary = "cc_binary")
16+
load("@cc_compatibility_proxy//:proxy.bzl", _cc_binary = "cc_binary")
1717

1818
# TODO(bazel-team): To avoid breaking changes, if the below are no longer
1919
# forwarding to native rules, flag @bazel_tools//tools/cpp:link_extra_libs

cc/cc_import.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""cc_import rule"""
1515

16-
load("//cc/private/rules_impl:cc_import.bzl", _cc_import = "cc_import")
16+
load("@cc_compatibility_proxy//:proxy.bzl", _cc_import = "cc_import")
1717

1818
def cc_import(**kwargs):
1919
_cc_import(**kwargs)

cc/cc_library.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""cc_library rule"""
1515

16-
load("//cc/private/rules_impl:cc_library.bzl", _cc_library = "cc_library")
16+
load("@cc_compatibility_proxy//:proxy.bzl", _cc_library = "cc_library")
1717

1818
def cc_library(**kwargs):
1919
_cc_library(**kwargs)

cc/cc_shared_library.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""cc_library rule"""
1515

16-
load("//cc/private/rules_impl:cc_shared_library.bzl", _cc_shared_library = "cc_shared_library")
16+
load("@cc_compatibility_proxy//:proxy.bzl", _cc_shared_library = "cc_shared_library")
1717

1818
def cc_shared_library(**kwargs):
1919
_cc_shared_library(**kwargs)

cc/cc_static_library.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""cc_library rule"""
1515

16-
load("//cc/private/rules_impl:cc_static_library.bzl", _cc_static_library = "cc_static_library")
16+
load("@cc_compatibility_proxy//:proxy.bzl", _cc_static_library = "cc_static_library")
1717

1818
def cc_static_library(**kwargs):
1919
_cc_static_library(**kwargs)

cc/cc_test.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""cc_test rule"""
1515

16-
load("//cc/private/rules_impl:cc_test.bzl", _cc_test = "cc_test")
16+
load("@cc_compatibility_proxy//:proxy.bzl", _cc_test = "cc_test")
1717

1818
# TODO(bazel-team): To avoid breaking changes, if the below are no longer
1919
# forwarding to native rules, flag @bazel_tools//tools/cpp:link_extra_libs

cc/extensions.bzl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,67 @@ def _cc_configure_extension_impl(ctx):
2525
return None
2626

2727
cc_configure_extension = module_extension(implementation = _cc_configure_extension_impl)
28+
29+
def _compatibility_proxy_repo_impl(rctx):
30+
rctx.file("BUILD", "")
31+
bazel = native.bazel_version
32+
if not bazel or bazel >= "9":
33+
rctx.file(
34+
"proxy.bzl",
35+
"""
36+
load("@rules_cc//cc/private/rules_impl:cc_binary.bzl", _cc_binary = "cc_binary")
37+
load("@rules_cc//cc/private/rules_impl:cc_import.bzl", _cc_import = "cc_import")
38+
load("@rules_cc//cc/private/rules_impl:cc_library.bzl", _cc_library = "cc_library")
39+
load("@rules_cc//cc/private/rules_impl:cc_shared_library.bzl", _cc_shared_library = "cc_shared_library")
40+
load("@rules_cc//cc/private/rules_impl:cc_static_library.bzl", _cc_static_library = "cc_static_library")
41+
load("@rules_cc//cc/private/rules_impl:cc_test.bzl", _cc_test = "cc_test")
42+
load("@rules_cc//cc/private/rules_impl:objc_import.bzl", _objc_import = "objc_import")
43+
load("@rules_cc//cc/private/rules_impl:objc_library.bzl", _objc_library = "objc_library")
44+
45+
cc_binary = _cc_binary
46+
cc_import = _cc_import
47+
cc_library = _cc_library
48+
cc_shared_library = _cc_shared_library
49+
cc_static_library = _cc_static_library
50+
cc_test = _cc_test
51+
objc_import = _objc_import
52+
objc_library = _objc_library
53+
""",
54+
)
55+
else:
56+
rctx.file(
57+
"proxy.bzl",
58+
"""
59+
cc_binary = native.cc_binary
60+
cc_import = native.cc_import
61+
cc_library = native.cc_library
62+
cc_shared_library = native.cc_shared_library
63+
cc_static_library = getattr(native, "cc_static_library", None) # only in Bazel 8+
64+
cc_test = native.cc_test
65+
objc_import = native.objc_import
66+
objc_library = native.objc_library
67+
""",
68+
)
69+
70+
_compatibility_proxy_repo_rule = repository_rule(
71+
_compatibility_proxy_repo_impl,
72+
# force reruns on server restarts to use correct native.bazel_version
73+
local = True,
74+
)
75+
76+
def compatibility_proxy_repo():
77+
_compatibility_proxy_repo_rule(name = "cc_compatibility_proxy")
78+
79+
def _compat_proxy_impl(module_ctx):
80+
compatibility_proxy_repo()
81+
82+
# module_ctx.extension_metadata has the paramater `reproducible` as of Bazel 7.1.0. We can't
83+
# test for it directly and would ideally use bazel_features to check for it, but don't want
84+
# to add a dependency for as long as WORKSPACE is still around. Thus, test for it by
85+
# checking the availability of another feature introduced in 7.1.0.
86+
if hasattr(module_ctx, "watch"):
87+
return module_ctx.extension_metadata(reproducible = True)
88+
else:
89+
return None
90+
91+
compatibility_proxy = module_extension(_compat_proxy_impl)

cc/objc_import.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
"""objc_import rule"""
1515

16-
load("//cc/private/rules_impl:objc_import.bzl", _objc_import = "objc_import")
16+
load("@cc_compatibility_proxy//:proxy.bzl", _objc_import = "objc_import")
1717

1818
def objc_import(**kwargs):
1919
_objc_import(**kwargs)

0 commit comments

Comments
 (0)