Skip to content

Commit b2adc9a

Browse files
committed
add current_py_cc_headers_abi3 target
1 parent 3c287cf commit b2adc9a

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

python/cc/BUILD.bazel

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
44
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
5-
load("//python/private:current_py_cc_headers.bzl", "current_py_cc_headers")
5+
load("//python/private:current_py_cc_headers.bzl", "current_py_cc_headers", "current_py_cc_headers_abi3")
66
load("//python/private:current_py_cc_libs.bzl", "current_py_cc_libs")
77

88
package(
@@ -20,6 +20,17 @@ current_py_cc_headers(
2020
visibility = ["//visibility:public"],
2121
)
2222

23+
# This target provides the C ABI3 headers for whatever the current toolchain is
24+
# for the consuming rule. It basically acts like a cc_library by forwarding
25+
# on the providers for the underlying cc_library that the toolchain is using.
26+
current_py_cc_headers_abi3(
27+
name = "current_py_cc_headers_abi3",
28+
# Building this directly will fail unless a py cc toolchain is registered,
29+
# and it's only under bzlmod that one is registered by default.
30+
tags = [] if BZLMOD_ENABLED else ["manual"],
31+
visibility = ["//visibility:public"],
32+
)
33+
2334
# This target provides the C libraries for whatever the current toolchain is for
2435
# the consuming rule. It basically acts like a cc_library by forwarding on the
2536
# providers for the underlying cc_library that the toolchain is using.

python/private/current_py_cc_headers.bzl

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Implementation of current_py_cc_headers rule."""
15+
"""Implementation of current_py_cc_headers and current_py_cc_headers_abi3 rules.
16+
"""
1617

1718
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
1819

@@ -24,7 +25,7 @@ current_py_cc_headers = rule(
2425
implementation = _current_py_cc_headers_impl,
2526
toolchains = ["//python/cc:toolchain_type"],
2627
provides = [CcInfo],
27-
doc = """\
28+
doc = """
2829
Provides the currently active Python toolchain's C headers.
2930
3031
This is a wrapper around the underlying `cc_library()` for the
@@ -41,3 +42,35 @@ cc_library(
4142
```
4243
""",
4344
)
45+
46+
def _current_py_cc_headers_abi3_impl(ctx):
47+
py_cc_toolchain = ctx.toolchains["//python/cc:toolchain_type"].py_cc_toolchain
48+
if not py_cc_toolchain.headers_abi3:
49+
fail(
50+
"The current python toolchain does not provide ABI3 headers".format(
51+
py_cc_toolchain.python_version,
52+
),
53+
)
54+
return py_cc_toolchain.headers_abi3.providers_map.values()
55+
56+
current_py_cc_headers_abi3 = rule(
57+
implementation = _current_py_cc_headers_abi3_impl,
58+
toolchains = ["//python/cc:toolchain_type"],
59+
provides = [CcInfo],
60+
doc = """
61+
Provides the currently active Python toolchain's C ABI3 headers.
62+
63+
This is a wrapper around the underlying `cc_library()` for the
64+
C ABI3 headers for the consuming target's currently active Python toolchain.
65+
66+
To use, simply depend on this target where you would have wanted the
67+
toolchain's underlying `:python_headers_abi3` target:
68+
69+
```starlark
70+
cc_library(
71+
name = "foo",
72+
deps = ["@rules_python//python/cc:current_py_cc_headers_abi3"]
73+
)
74+
```
75+
""",
76+
)

0 commit comments

Comments
 (0)