-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathbuild_defs.bzl
More file actions
268 lines (235 loc) · 9.08 KB
/
build_defs.bzl
File metadata and controls
268 lines (235 loc) · 9.08 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Copyright (c) 2019 The Pybind Development Team. All rights reserved.
#
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
"""Build rules for pybind11."""
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
def _pybind_py_env_test_impl(ctx):
toolchain = ctx.toolchains["@rules_python//python:toolchain_type"]
py3_runtime = toolchain.py3_runtime
if not py3_runtime:
fail("No python3 runtime found in toolchain")
# On Windows, we cannot use the shell script wrapper.
if ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]):
# On Windows, we need to return an executable created by this rule.
# We create a symlink to the actual binary.
# We use the same extension as the original binary (usually .exe).
extension = ctx.executable.binary.extension
executable = ctx.actions.declare_file(ctx.label.name + ("." + extension if extension else ""))
ctx.actions.symlink(output = executable, target_file = ctx.executable.binary, is_executable = True)
return [
DefaultInfo(
executable = executable,
runfiles = ctx.runfiles(files = [executable])
.merge(ctx.attr.binary[DefaultInfo].default_runfiles)
.merge(ctx.runfiles(transitive_files = py3_runtime.files)),
),
]
interpreter = py3_runtime.interpreter
# Generate a wrapper script that sets PYTHONHOME and runs the C++ binary.
script = ctx.actions.declare_file(ctx.label.name + ".sh")
content = "#!/bin/bash\n"
content += "if [ -z \"$RUNFILES_DIR\" ]; then\n"
content += " if [ -d \"$0.runfiles\" ]; then\n"
content += " RUNFILES_DIR=\"$0.runfiles\"\n"
content += " else\n"
content += " RUNFILES_DIR=\"$(dirname \"$0\")/../..\"\n"
content += " fi\n"
content += "fi\n"
content += "INTERPRETER_PATH=\"$RUNFILES_DIR/" + ctx.workspace_name + "/" + interpreter.short_path + "\"\n"
content += "if [ ! -f \"$INTERPRETER_PATH\" ]; then\n"
content += " INTERPRETER_PATH=$(find \"$RUNFILES_DIR\" -path \"*/" + interpreter.short_path + "\" | head -n 1)\n"
content += "fi\n"
content += "export PYTHONHOME=$(dirname $(dirname $(readlink -f \"$INTERPRETER_PATH\")))\n"
content += "BINARY_PATH=\"$RUNFILES_DIR/" + ctx.workspace_name + "/" + ctx.executable.binary.short_path + "\"\n"
content += "if [ ! -f \"$BINARY_PATH\" ]; then\n"
content += " BINARY_PATH=$(find \"$RUNFILES_DIR\" -path \"*/" + ctx.executable.binary.short_path + "\" | head -n 1)\n"
content += "fi\n"
content += "exec \"$BINARY_PATH\" \"$@\"\n"
ctx.actions.write(script, content, is_executable = True)
runfiles = ctx.runfiles(files = [script, ctx.executable.binary])
runfiles = runfiles.merge(ctx.attr.binary[DefaultInfo].default_runfiles)
runfiles = runfiles.merge(ctx.runfiles(transitive_files = py3_runtime.files))
return [
DefaultInfo(
executable = script,
runfiles = runfiles,
),
]
pybind_py_env_test = rule(
implementation = _pybind_py_env_test_impl,
test = True,
attrs = {
"binary": attr.label(
executable = True,
cfg = "target",
mandatory = True,
),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
},
toolchains = ["@rules_python//python:toolchain_type"],
)
def register_extension_info(**kwargs):
pass
PYBIND_COPTS = select({
Label("@pybind11//:msvc_compiler"): [],
"//conditions:default": ["-fexceptions"],
})
PYBIND_FEATURES = [
"-use_header_modules", # Required for pybind11.
"-parse_headers",
]
PYBIND_DEPS = [
Label("@pybind11//:pybind11"),
"@rules_python//python/cc:current_py_cc_headers",
]
def pybind_extension(
name,
copts = [],
features = [],
linkopts = [],
tags = [],
deps = [],
**kwargs):
"""Builds a Python extension module using pybind11.
Args:
name: The name of the extension module.
copts: Compiler options for building the module.
features: Features required for building the module.
linkopts: Linker options for building the module.
tags: Tags for the module.
deps: Dependencies required for building the module.
**kwargs: Additional keyword arguments.
This can be directly used in Python with the import statement.
Assuming the name NAME, the following targets will be defined:
1. NAME.so - the shared/dynamic library for the extension module
2. NAME.pyd - a copy of NAME.so named for Python on Windows; see
https://github.com/pybind/pybind11_bazel/issues/74
3. NAME - an alias pointing to either NAME.so or NAME.pyd as per
the platform OS (not-Windows or Windows, respectively)
Generally, the user will "depend" on this extension module via the
data attribute of their py_* target; specifying NAME is preferred.
"""
# Mark common dependencies as required for build_cleaner.
tags = tags + ["req_dep=%s" % dep for dep in PYBIND_DEPS]
cc_binary(
name = name + ".so",
copts = copts + PYBIND_COPTS + select({
Label("@pybind11//:msvc_compiler"): [],
"//conditions:default": ["-fvisibility=hidden"],
}),
features = features + PYBIND_FEATURES,
linkopts = linkopts + select({
"@platforms//os:osx": ["-undefined", "dynamic_lookup"],
Label("@pybind11//:msvc_compiler"): [],
"//conditions:default": ["-Wl,-Bsymbolic"],
}),
linkshared = 1,
tags = tags,
target_compatible_with = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
deps = deps + PYBIND_DEPS,
**kwargs
)
cc_binary(
name = name + ".pyd",
copts = copts + PYBIND_COPTS + select({
Label("@pybind11//:msvc_compiler"): [],
"//conditions:default": ["-fvisibility=hidden"],
}),
features = features + PYBIND_FEATURES,
linkopts = linkopts + select({
"@platforms//os:osx": ["-undefined", "dynamic_lookup"],
Label("@pybind11//:msvc_compiler"): [],
"//conditions:default": ["-Wl,-Bsymbolic"],
}),
linkshared = 1,
tags = tags,
target_compatible_with = select({
"@platforms//os:windows": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = deps + PYBIND_DEPS,
**kwargs
)
native.alias(
name = name,
actual = select({
"@platforms//os:windows": name + ".pyd",
"//conditions:default": name + ".so",
}),
testonly = kwargs.get("testonly"),
visibility = kwargs.get("visibility"),
)
def pybind_library(
name,
copts = [],
features = [],
tags = [],
deps = [],
**kwargs):
"""Builds a pybind11 compatible library. This can be linked to a pybind_extension."""
# Mark common dependencies as required for build_cleaner.
tags = tags + ["req_dep=%s" % dep for dep in PYBIND_DEPS]
cc_library(
name = name,
copts = copts + PYBIND_COPTS,
features = features + PYBIND_FEATURES,
tags = tags,
deps = deps + PYBIND_DEPS,
**kwargs
)
def pybind_library_test(
name,
copts = [],
features = [],
tags = [],
deps = [],
**kwargs):
"""Builds a C++ test for a pybind_library."""
# Mark common dependencies as required for build_cleaner.
tags = tags + ["req_dep=%s" % dep for dep in PYBIND_DEPS]
# Pop test-only attributes that cc_binary doesn't support.
test_kwargs = {}
for attr in ["size", "timeout", "flaky", "shard_count", "local"]:
if attr in kwargs:
test_kwargs[attr] = kwargs.pop(attr)
# Build the actual C++ binary.
cc_binary(
name = name + "_bin",
copts = copts + PYBIND_COPTS,
features = features + PYBIND_FEATURES,
testonly = True,
visibility = ["//visibility:private"],
deps = deps + PYBIND_DEPS + [
"@rules_python//python/cc:current_py_cc_libs",
],
**kwargs
)
# Use a wrapper rule to set PYTHONHOME and run the binary.
pybind_py_env_test(
name = name,
binary = ":" + name + "_bin",
testonly = True,
tags = tags,
visibility = kwargs.get("visibility"),
**test_kwargs
)
# Register extension with build_cleaner.
register_extension_info(
extension = pybind_extension,
label_regex_for_dep = "{extension_name}",
)
register_extension_info(
extension = pybind_library,
label_regex_for_dep = "{extension_name}",
)
register_extension_info(
extension = pybind_library_test,
label_regex_for_dep = "{extension_name}",
)