Skip to content

Commit 90c211a

Browse files
authored
Refactor local_runtime_repo_setup for ABI3 support
1 parent dab89c4 commit 90c211a

File tree

1 file changed

+57
-27
lines changed

1 file changed

+57
-27
lines changed

bazel/repo_rules/local_runtime_repo_setup.bzl

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ def define_local_runtime_toolchain_impl(
3232
major,
3333
minor,
3434
micro,
35+
abi_flags,
36+
os,
37+
implementation_name,
3538
interpreter_path,
3639
interface_library,
3740
libraries,
38-
implementation_name,
39-
os,
40-
abi_flags):
41+
abi3_interface_library,
42+
abi3_libraries):
4143
"""Defines a toolchain implementation for a local Python runtime.
4244
4345
Generates public targets:
@@ -54,16 +56,20 @@ def define_local_runtime_toolchain_impl(
5456
major: `str` The major Python version, e.g. `3` of `3.9.1`.
5557
minor: `str` The minor Python version, e.g. `9` of `3.9.1`.
5658
micro: `str` The micro Python version, e.g. "1" of `3.9.1`.
59+
abi_flags: `str` The abi flags, as returned by `sys.abiflags`.
60+
os: `str` A label to the OS constraint (e.g. `@platforms//os:linux`) for
61+
this runtime.
62+
implementation_name: `str` The implementation name, as returned by
63+
`sys.implementation.name`.
5764
interpreter_path: `str` Absolute path to the interpreter.
5865
interface_library: `str` Path to the interface library.
5966
e.g. "lib/python312.lib"
6067
libraries: `list[str]` Path[s] to the python libraries.
6168
e.g. ["lib/python312.dll"] or ["lib/python312.so"]
62-
implementation_name: `str` The implementation name, as returned by
63-
`sys.implementation.name`.
64-
os: `str` A label to the OS constraint (e.g. `@platforms//os:linux`) for
65-
this runtime.
66-
abi_flags: `str` The abi flags, as returned by `sys.abiflags`.
69+
abi3_interface_library: `str` Path to the interface library.
70+
e.g. "lib/python3.lib"
71+
abi3_libraries: `list[str]` Path[s] to the python libraries.
72+
e.g. ["lib/python3.dll"] or ["lib/python3.so"]
6773
"""
6874
major_minor = "{}.{}".format(major, minor)
6975
major_minor_micro = "{}.{}".format(major_minor, micro)
@@ -73,43 +79,66 @@ def define_local_runtime_toolchain_impl(
7379
# However not all python installations (such as manylinux) include shared or static libraries,
7480
# so only create the import library when interface_library is set.
7581
full_abi_deps = []
76-
abi3_deps = []
7782
if interface_library:
7883
cc_import(
79-
name = "_python_interface_library",
84+
name = "interface",
8085
interface_library = interface_library,
86+
system_provided = True,
87+
)
88+
full_abi_deps.append(":interface")
89+
90+
abi3_deps = []
91+
if abi3_interface_library:
92+
cc_import(
93+
name = "abi3_interface",
94+
interface_library = abi3_interface_library,
8195
system_provided = 1,
8296
)
83-
if interface_library.endswith("{}.lib".format(major)):
84-
abi3_deps = [":_python_interface_library"]
85-
else:
86-
full_abi_deps = [":_python_interface_library"]
97+
abi3_deps.append(":abi3_interface")
8798

88-
cc_library(
89-
name = "_python_headers_abi3",
90-
# NOTE: Keep in sync with watch_tree() called in local_runtime_repo
99+
native.filegroup(
100+
name = "includes",
91101
srcs = native.glob(
92102
include = ["include/**/*.h"],
93103
exclude = ["include/numpy/**"], # numpy headers are handled separately
94104
allow_empty = True, # A Python install may not have C headers
95105
),
106+
)
107+
108+
# abi3 libraries.
109+
cc_library(
110+
name = "python_headers_abi3",
96111
deps = abi3_deps,
112+
hdrs = [":includes"],
97113
includes = ["include"],
98114
)
115+
99116
cc_library(
100-
name = "_python_headers",
101-
deps = [":_python_headers_abi3"] + full_abi_deps,
117+
name = "libpython{}".format(major),
118+
hdrs = [":includes"],
119+
srcs = abi3_libraries + native.glob(
120+
include = ["dlls/**"],
121+
allow_empty = True,
122+
),
102123
)
103124

125+
# full abi libraries.
104126
cc_library(
105-
name = "_libpython",
106-
hdrs = [":_python_headers"],
107-
srcs = libraries,
108-
deps = [],
127+
name = "python_headers",
128+
deps = [":python_headers_abi3"] + full_abi_deps,
129+
)
130+
131+
cc_library(
132+
name = "libpython{}".format(major_minor),
133+
hdrs = [":includes"],
134+
srcs = libraries + native.glob(
135+
include = ["dlls/**"],
136+
allow_empty = True,
137+
),
109138
)
110139

111140
py_runtime(
112-
name = "_py3_runtime",
141+
name = "py3_runtime",
113142
interpreter_path = interpreter_path,
114143
python_version = "PY3",
115144
interpreter_version_info = {
@@ -124,7 +153,7 @@ def define_local_runtime_toolchain_impl(
124153
py_runtime_pair(
125154
name = "python_runtimes",
126155
py2_runtime = None,
127-
py3_runtime = ":_py3_runtime",
156+
py3_runtime = ":py3_runtime",
128157
visibility = ["//visibility:public"],
129158
)
130159

@@ -136,8 +165,9 @@ def define_local_runtime_toolchain_impl(
136165

137166
py_cc_toolchain(
138167
name = "py_cc_toolchain",
139-
headers = ":_python_headers",
140-
libs = ":_libpython",
168+
headers = ":python_headers",
169+
# headers_abi3 = ":python_headers_abi3",
170+
libs = ":libpython{}".format(major_minor),
141171
python_version = major_minor_micro,
142172
visibility = ["//visibility:public"],
143173
)

0 commit comments

Comments
 (0)