forked from pybind/pybind11_bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_defs.bzl
108 lines (94 loc) · 2.75 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
# 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."""
def register_extension_info(**kwargs):
pass
PYBIND_COPTS = [
"-fexceptions",
]
PYBIND_FEATURES = [
"-use_header_modules", # Required for pybind11.
"-parse_headers",
]
PYBIND_DEPS = [
"@pybind11",
"@local_config_python//:python_headers",
]
# Builds a Python extension module using pybind11.
# This can be directly used in python with the import statement.
# This adds rules for a .so binary file, which must be built manually.
def pybind_extension(
name,
copts = [],
features = [],
tags = [],
deps = [],
**kwargs):
# 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 + ["-fvisibility=hidden"],
features = features + PYBIND_FEATURES,
linkopts = [
"-Wl,-Bsymbolic",
],
linkshared = 1,
tags = tags + ["local", "manual"],
deps = deps + PYBIND_DEPS,
**kwargs
)
# Builds a pybind11 compatible library. This can be linked to a pybind_extension.
def pybind_library(
name,
copts = [],
features = [],
tags = [],
deps = [],
**kwargs):
# 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
)
# Builds a C++ test for a pybind_library.
def pybind_library_test(
name,
copts = [],
features = [],
tags = [],
deps = [],
**kwargs):
# 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 + [
"//util/python:python_impl",
"//util/python:test_main",
],
**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}",
)