forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defs.bzl
205 lines (186 loc) · 7.44 KB
/
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
load("//tools/lint:cpplint.bzl", "cpplint_extra")
load("//tools/skylark:drake_cc.bzl", "drake_cc_googletest", "drake_cc_library")
# This file contains macros that help abbreviate patterns that frequently
# appear in the solvers folder.
def _sync_conditions(condition1, condition2):
"""Asserts that one and only one condition is given. Sets the other
condition to "//conditions:default" and returns them in the same order
as they were given.
"""
if int(bool(condition1)) + int(bool(condition2)) != 1:
fail("Specify exactly one of opt-in or opt-out")
return (
condition1 or "//conditions:default",
condition2 or "//conditions:default",
)
def drake_cc_variant_library(
name,
*,
opt_in_condition = None,
opt_out_condition = None,
srcs_always,
srcs_enabled,
srcs_disabled,
hdrs,
deps,
implementation_deps_always,
implementation_deps_enabled,
internal = False,
visibility = None):
"""Declares a library with a uniform set of header files (typically just
one header file) but with two different cc file implementations, depending
on a configuration setting. This is how we turn on/off specific solver
back-end implementations, e.g., `snopt_solver.cc` vs `no_snopt.cc`.
The same hdrs are used unconditionally. The deps should list the
dependencies for the header file(s), and thus are also unconditional.
Exactly one of opt_in_condition or opt_out_condition must be provided, to
specify the configuration setting that chooses which cc files to use.
Open-source solvers are usually ON by default (so, will use opt_out_...).
Commercial solvers are usually OFF by default (so, will use opt_in_...).
The sources listed in srcs_always contain definitions that are appropriate
whether or not a back-end is enabled. This usually contains things such as
the solver name, ID, and attribute support. The implementation_deps_always
lists the dependencies of these files.
The sources listed in srcs_enabled contain the code of the fully-featured
implementation. The implementation_deps_enabled lists the dependencies of
these files.
The sources listed in srcs_disabled contain the alternative (stub)
definitions that report failure (e.g., returning false or throwing).
This rule enables linting of all of the mentioned source files, even if
the compiler will not build them in the current configuration. We want all
files to be lint-free, even when the commercial solvers are disabled.
"""
opt_in_condition, opt_out_condition = _sync_conditions(
opt_in_condition,
opt_out_condition,
)
drake_cc_library(
name = name,
srcs = select({
opt_in_condition: srcs_always + srcs_enabled,
opt_out_condition: srcs_always + srcs_disabled,
}),
hdrs = hdrs,
deps = deps,
implementation_deps = select({
opt_in_condition: (
implementation_deps_always + implementation_deps_enabled
),
opt_out_condition: implementation_deps_always,
}),
internal = internal,
visibility = visibility,
)
cpplint_extra(
name = name + "_cpplint",
srcs = hdrs + srcs_always + srcs_enabled + srcs_disabled,
)
def drake_cc_optional_library(
name,
*,
opt_in_condition = None,
opt_out_condition = None,
srcs,
hdrs,
copts = None,
visibility = ["//visibility:private"],
deps = None,
implementation_deps = None):
"""Declares a private library (package-local, not installed) guarded by a
configuration setting. When the configuration is disabled, the library is
totally empty (but still a valid library label). This is used for helper
or utility code that's called by a fully-feataured back-end implementation.
Exactly one of opt_in_condition or opt_out_condition must be provided, to
specify the configuration setting that chooses which cc files to use.
Open-source solvers are usually ON by default (so, will use opt_out_...).
Commercial solvers are usually OFF by default (so, will use opt_in_...).
This rule enables linting of all of the mentioned source files, even if
the compiler will not build them in the current configuration. We want all
files to be lint-free, even when the commercial solvers are disabled.
"""
opt_in_condition, opt_out_condition = _sync_conditions(
opt_in_condition,
opt_out_condition,
)
drake_cc_library(
name = name,
srcs = select({
opt_in_condition: srcs,
opt_out_condition: [],
}),
hdrs = select({
opt_in_condition: hdrs,
opt_out_condition: [],
}),
install_hdrs_exclude = select({
opt_in_condition: hdrs,
opt_out_condition: [],
}),
copts = select({
opt_in_condition: copts or [],
opt_out_condition: [],
}),
tags = ["exclude_from_package"],
visibility = visibility,
deps = select({
opt_in_condition: deps or [],
opt_out_condition: [],
}),
implementation_deps = None if implementation_deps == None else select({
opt_in_condition: implementation_deps,
opt_out_condition: [],
}),
)
cpplint_extra(
name = name + "_cpplint",
srcs = hdrs + srcs,
)
def drake_cc_optional_googletest(
name,
*,
opt_in_condition = None,
opt_out_condition = None,
tags = None,
deps,
use_default_main = True):
"""Declares a test that is not even compiled under certain configurations.
This is intended only for testing of drake_cc_optional_library targets,
where the header file(s) are not even present under certain configurations.
For testing a drake_cc_variant_library, the header(s) should always be
available, so there is no reason we avoid compiling the unit test.
Exactly one of opt_in_condition or opt_out_condition must be provided, to
specify the configuration setting that chooses which cc files to use.
Open-source solvers are usually ON by default (so, will use opt_out_...).
Commercial solvers are usually OFF by default (so, will use opt_in_...).
This rule enables linting of all of the mentioned source files, even if
the compiler will not build them in the current configuration. We want all
files to be lint-free, even when the commercial solvers are disabled.
"""
opt_in_condition, opt_out_condition = _sync_conditions(
opt_in_condition,
opt_out_condition,
)
srcs = ["test/{}.cc".format(name)]
if use_default_main:
opt_out_deps = []
else:
# The `srcs` provide a main() function, but in the opt_out case we
# won't be linking them; to cope, we'll fall back to the default main.
opt_out_deps = ["//common/test_utilities:drake_cc_googletest_main"]
drake_cc_googletest(
name = name,
srcs = select({
opt_in_condition: srcs,
opt_out_condition: [],
}),
tags = tags,
deps = select({
opt_in_condition: deps,
opt_out_condition: opt_out_deps,
}),
use_default_main = use_default_main,
)
cpplint_extra(
name = name + "_cpplint",
srcs = srcs,
)