-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_defs.bzl
240 lines (215 loc) · 8.14 KB
/
build_defs.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""
Build defs for nanobind.
The ``nanobind_extension`` corresponds to a ``cc_binary``,
the ``nanobind_library`` to a ``cc_library``,
the ``nanobind_shared_library`` to a ``cc_shared_library``,
the ``nanobind_stubgen`` to a ``py_binary``,
and the ``nanobind_test`` to a ``cc_test``.
For creating Python bindings, the most likely case is a ``nanobind_extension``
built using the C++ source files containing the nanobind module definition,
which can then be included e.g. as a `data` input in a ``native.py_library``.
"""
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load(
"@nanobind_bazel//:helpers.bzl",
"extension_name",
"nb_common_opts",
"nb_sizeopts",
)
load("@rules_python//python:py_binary.bzl", "py_binary")
NANOBIND_COPTS = nb_common_opts() + nb_sizeopts()
NANOBIND_DEPS = [Label("@nanobind//:nanobind")]
def nanobind_extension(
name,
domain = "",
srcs = [],
copts = [],
deps = [],
local_defines = [],
**kwargs):
"""A C++ Python extension library built with nanobind.
Given a name $NAME, defines the following targets:
1. $NAME.so, a shared object library for use on Linux/Mac.
2. $NAME.abi3.so, a copy of $NAME.so for Linux/Mac,
indicating that it is compatible with the Python stable ABI.
3. $NAME.pyd, a copy of $NAME.so for use on Windows.
4. $NAME, an alias pointing to the appropriate library
depending on the target platform.
Args:
name: str
A name for this target. This becomes the Python module name
used by the resulting nanobind extension.
domain: str, default ''
The nanobind domain to set for this extension. A nanobind domain is
an optional attribute that can be set to scope extension code to a named
domain, which avoids conflicts with other extensions.
srcs: list
A list of sources and headers to go into this target.
copts: list
A list of compiler optimizations. Augmented with nanobind-specific
compiler optimizations by default.
deps: list
A list of dependencies of this extension.
local_defines: list
A list of preprocessor defines to set for this target.
Augmented with -DNB_DOMAIN=$DOMAIN if the domain argument is given.
**kwargs: Any
Keyword arguments matching the cc_binary rule arguments, to be passed
directly to the resulting cc_binary target.
"""
if domain != "":
NANOBIND_DOMAIN = ["NB_DOMAIN={}".format(domain)]
else:
NANOBIND_DOMAIN = []
native.cc_binary(
name = name + ".so",
srcs = srcs,
copts = copts + NANOBIND_COPTS,
deps = deps + NANOBIND_DEPS,
local_defines = local_defines + NANOBIND_DOMAIN,
linkshared = True, # Python extensions need to be shared libs.
**kwargs
)
copy_file(
name = name + "_copy_so_to_abi3_so",
src = name + ".so",
out = name + ".abi3.so",
testonly = kwargs.get("testonly"),
visibility = kwargs.get("visibility"),
)
copy_file(
name = name + "_copy_so_to_pyd",
src = name + ".so",
out = name + ".pyd",
testonly = kwargs.get("testonly"),
visibility = kwargs.get("visibility"),
)
native.alias(
name = name,
actual = extension_name(name),
testonly = kwargs.get("testonly"),
visibility = kwargs.get("visibility"),
)
def nanobind_library(
name,
copts = [],
deps = [],
**kwargs):
native.cc_library(
name = name,
copts = copts + NANOBIND_COPTS,
deps = deps + NANOBIND_DEPS,
**kwargs
)
def nanobind_shared_library(
name,
deps = [],
**kwargs):
"""A shared library containing nanobind as a static dependency.
Using a shared nanobind library is useful when partitioning C++ binding
code over multiple extensions, where linking all of them statically would
produce much larger bindings than necessary.
Args:
name: str
A name for this target. On Linux/MacOS, the target name determines
the name of the resulting shared object file via lib${name}.so, e.g.
a `cc_shared_library(name = "nanobind-tensorflow")` produces the
shared object file libnanobind-tensorflow.so.
deps: list
A list of static dependencies for this shared library. By default,
a statically built nanobind is included.
**kwargs: Any
Additional keyword arguments passed directly to the `cc_shared_library`
rule. For a comprehensive list, see the Bazel documentation at
https://bazel.build/reference/be/c-cpp#cc_shared_library.
"""
native.cc_shared_library(
name = name,
deps = deps + NANOBIND_DEPS,
**kwargs
)
def nanobind_stubgen(
name,
module,
output_file = None,
imports = [],
pattern_file = None,
marker_file = None,
include_private_members = False,
exclude_docstrings = False):
"""Creates a stub file containing Python type annotations for a nanobind extension.
Args:
name: str
Name of this stub generation target, unused.
module: Label
Label of the extension module for which the stub file should be
generated.
output_file: str or None
Output file path for the generated stub, relative to $(BINDIR).
If none is given, the stub will be placed under the same location
as the module in your source tree.
imports: list
List of modules to import for stub generation.
pattern_file: Label or None
Label of a pattern file used for programmatically editing generated stubs.
For more information, consider the documentation under
https://nanobind.readthedocs.io/en/latest/typing.html#pattern-files.
marker_file: str or None
An empty typing marker file to add to the project, most often named
"py.typed". Must be given relative to your Python project root.
include_private_members: bool
Whether to include private module members, i.e. those starting and/or
ending with an underscore ("_").
exclude_docstrings: bool
Whether to exclude all docstrings of all module members from the generated
stub file.
"""
STUBGEN_WRAPPER = Label("@nanobind_bazel//:stubgen_wrapper.py")
loc = "$(rlocationpath {})"
# stubgen wrapper dependencies: nanobind.stubgen, typing_extensions (via nanobind),
# rules_python runfiles (unused, needed later when giving an explicit output path)
deps = [
Label("@nanobind//:stubgen"),
Label("@pypi__typing_extensions//:lib"),
Label("@rules_python//python/runfiles"),
]
data = [module]
args = ["-m " + loc.format(module)]
# to be searchable by path expansion, a file must be
# declared by a rule beforehand. This might not be the
# case for a generated stub, so we just give the raw name here
if output_file:
args.append("-o {}".format(output_file))
# Add pattern and marker files.
# The pattern file must exist in the Bazel repo, so
# we pass its label to the py_binary's data dependencies.
# The marker file can be generated on the fly, however.
if pattern_file:
data.append(pattern_file)
args.append("-p " + loc.format(pattern_file))
if marker_file:
args.append("-M {}".format(marker_file))
if include_private_members:
args.append("--include-private")
if exclude_docstrings:
args.append("--exclude-docstrings")
py_binary(
name = name,
srcs = [STUBGEN_WRAPPER],
main = STUBGEN_WRAPPER,
deps = deps,
data = data,
imports = imports,
args = args,
)
def nanobind_test(
name,
copts = [],
deps = [],
**kwargs):
native.cc_test(
name = name,
copts = copts + NANOBIND_COPTS,
deps = deps + NANOBIND_DEPS,
**kwargs
)