-
Notifications
You must be signed in to change notification settings - Fork 54
/
build_defs.bzl
155 lines (133 loc) · 4.5 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
# 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")
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]
native.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,
deps = deps + PYBIND_DEPS,
**kwargs
)
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 = 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]
native.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]
native.cc_test(
name = name,
copts = copts + PYBIND_COPTS,
features = features + PYBIND_FEATURES,
tags = tags,
deps = deps + PYBIND_DEPS + [
"@rules_python//python/cc:current_py_cc_libs",
],
**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}",
)