/
c_rules.build_defs
274 lines (250 loc) · 12.7 KB
/
c_rules.build_defs
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
269
270
271
272
273
274
"""Rules to build C targets.
These are just wrappers around the C++ rules; the logic of the two is identical
other than using different config settings.
Both C and C++ can be mixed and matched arbitrarily, but of course to do so
you must pay attention to interoperability when needed (e.g. 'extern "C"' and so forth).
"""
def c_library(name:str, srcs:list=[], hdrs:list=[], private_hdrs:list=[], deps:list=[],
visibility:list=None, test_only:bool&testonly=False, compiler_flags:list&cflags&copts=[],
linker_flags:list&ldflags&linkopts=[], pkg_config_libs:list=[], pkg_config_cflags:list=[],
includes:list=[], defines:list|dict=[], alwayslink:bool=False):
"""Generate a C library target.
Args:
name (str): Name of the rule
srcs (list): C source files to compile.
hdrs (list): Header files. These will be made available to dependent rules, so the distinction
between srcs and hdrs is important.
private_hdrs (list): Header files that are available only to this rule and not exported to
dependent rules.
deps (list): Dependent rules.
visibility (list): Visibility declaration for this rule.
test_only (bool): If True, is only available to other test rules.
compiler_flags (list): Flags to pass to the compiler.
linker_flags (list): Flags to pass to the linker; these will not be used here but will be
picked up by a c_binary or c_test rule.
pkg_config_libs (list): Libraries to declare a dependency on using pkg-config. Again, the ldflags
will be picked up by cc_binary or cc_test rules.
pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`.
includes (list): List of include directories to be added to the compiler's path.
defines (list | dict): List of tokens to define in the preprocessor.
Alternatively can be a dict of name -> value to define, in which case
values are surrounded by quotes.
alwayslink (bool): If True, any binaries / tests using this library will link in all symbols,
even if they don't directly reference them. This is useful for e.g. having
static members that register themselves at construction time.
"""
return cc_library(
name = name,
srcs = srcs,
hdrs = hdrs,
private_hdrs = private_hdrs,
deps = deps,
visibility = visibility,
test_only = test_only,
compiler_flags = compiler_flags,
linker_flags = linker_flags,
pkg_config_libs = pkg_config_libs,
pkg_config_cflags=pkg_config_cflags,
includes = includes,
defines = defines,
alwayslink = alwayslink,
_c = True,
)
def c_object(name:str, src:str, hdrs:list=[], private_hdrs:list=[], out:str=None, test_only:bool&testonly=False,
compiler_flags:list&cflags&copts=[], linker_flags:list&ldflags&linkopts=[],
pkg_config_libs:list=[], pkg_config_cflags:list=[], includes:list=[], defines:list|dict=[],
alwayslink:bool=False, visibility:list=None, deps:list=[]):
"""Generate a C object file from a single source.
N.B. This is fairly low-level; for most use cases c_library should be preferred.
Args:
name (str): Name of the rule
src (str): C or C++ source file to compile. This can be another rule, but if so it must
have exactly one output.
hdrs (list): Header files. These will be made available to dependent rules, so the distinction
between srcs and hdrs is important.
private_hdrs (list): Header files that are available only to this rule and not exported to
dependent rules.
out (str): Name of the output file. Defaults to name + .o.
deps (list): Dependent rules.
visibility (list): Visibility declaration for this rule.
test_only (bool): If True, is only available to other test rules.
compiler_flags (list): Flags to pass to the compiler.
linker_flags (list): Flags to pass to the linker; these will not be used here but will be
picked up by a c_binary or c_test rule.
pkg_config_libs (list): Libraries to declare a dependency on using pkg-config. Again, the ldflags
will be picked up by cc_binary or cc_test rules.
pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`. Again, the ldflags
will be picked up by cc_binary or cc_test rules.
includes (list): List of include directories to be added to the compiler's path.
defines (list | dict): List of tokens to define in the preprocessor.
Alternatively can be a dict of name -> value to define, in which case
values are surrounded by quotes.
alwayslink (bool): If True, any binaries / tests using this library will link in all symbols,
even if they don't directly reference them. This is useful for e.g. having
static members that register themselves at construction time.
"""
return cc_object(
name = name,
src = src,
hdrs = hdrs,
private_hdrs = private_hdrs,
out = out,
deps = deps,
visibility = visibility,
test_only = test_only,
compiler_flags = compiler_flags,
linker_flags = linker_flags,
pkg_config_libs = pkg_config_libs,
pkg_config_cflags=pkg_config_cflags,
includes = includes,
defines = defines,
alwayslink = alwayslink,
_c = True,
)
def c_static_library(name:str, srcs:list=[], hdrs:list=[], compiler_flags:list&cflags&copts=[], linker_flags:list&ldflags&linkopts=[],
deps:list=[], visibility:list=None, test_only:bool&testonly=False, pkg_config_libs:list=[], pkg_config_cflags:list=[]):
"""Generates a C static library (.a).
This is essentially just a collection of other c_library rules into a single archive.
Optionally this rule can have sources of its own, but it's quite reasonable just to use
it as a collection of other rules.
Args:
name (str): Name of the rule
srcs (list): C or C++ source files to compile.
hdrs (list): Header files.
compiler_flags (list): Flags to pass to the compiler.
linker_flags (list): Flags to pass to the linker.
deps (list): Dependent rules.
visibility (list): Visibility declaration for this rule.
test_only (bool): If True, is only available to other test rules.
pkg_config_libs (list): Libraries to declare a dependency on using pkg-config
pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`
"""
return cc_static_library(
name = name,
srcs = srcs,
hdrs = hdrs,
deps = deps,
visibility = visibility,
test_only = test_only,
compiler_flags = compiler_flags,
linker_flags = linker_flags,
pkg_config_libs = pkg_config_libs,
pkg_config_cflags = pkg_config_cflags,
_c = True,
)
def c_shared_object(name:str, srcs:list=[], hdrs:list=[], out:str='', compiler_flags:list&cflags&copts=[],
linker_flags:list&ldflags&linkopts=[], deps:list=[], visibility:list=None, test_only:bool&testonly=False,
pkg_config_libs:list=[], pkg_config_cflags:list=[], includes:list=[]):
"""Generates a C shared object (.so) with its dependencies linked in.
Args:
name (str): Name of the rule
srcs (list): C or C++ source files to compile.
hdrs (list): Header files. These will be made available to dependent rules, so the distinction
between srcs and hdrs is important.
out (str): Name of the output .so. Defaults to name + .so.
compiler_flags (list): Flags to pass to the compiler.
linker_flags (list): Flags to pass to the linker.
deps (list): Dependent rules.
visibility (list): Visibility declaration for this rule.
test_only (bool): If True, is only available to other test rules.
pkg_config_libs (list): Libraries to declare a dependency on using pkg-config
pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`
includes (list): Include directories to be added to the compiler's lookup path.
"""
return cc_shared_object(
name = name,
srcs = srcs,
hdrs = hdrs,
out = out,
deps = deps,
visibility = visibility,
test_only = test_only,
compiler_flags = compiler_flags,
linker_flags = linker_flags,
pkg_config_libs = pkg_config_libs,
pkg_config_cflags = pkg_config_cflags,
includes = includes,
_c = True,
)
def c_binary(name:str, srcs:list=[], hdrs:list=[], private_hdrs:list=[], compiler_flags:list&cflags&copts=[],
linker_flags:list&ldflags&linkopts=[], deps:list=[], visibility:list=None, pkg_config_libs:list=[],
pkg_config_cflags:list=[], test_only:bool&testonly=False, static:bool=False):
"""Builds a binary from a collection of C rules.
Args:
name (str): Name of the rule
srcs (list): C source files to compile.
hdrs (list): Header files.
private_hdrs (list): Header files that are available only to this rule and not exported to
dependent rules.
compiler_flags (list): Flags to pass to the compiler.
linker_flags (list): Flags to pass to the linker.
deps (list): Dependent rules.
visibility (list): Visibility declaration for this rule.
pkg_config_libs (list): Libraries to declare a dependency on using pkg-config
pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`
test_only (bool): If True, this rule can only be used by tests.
static (bool): If True, the binary will be linked statically.
"""
return cc_binary(
name = name,
srcs = srcs,
hdrs = hdrs,
private_hdrs = private_hdrs,
deps = deps,
visibility = visibility,
test_only = test_only,
compiler_flags = compiler_flags,
linker_flags = linker_flags,
pkg_config_libs = pkg_config_libs,
pkg_config_cflags = pkg_config_cflags,
static = static,
_c = True,
)
def c_test(name:str, srcs:list=[], hdrs:list=[], compiler_flags:list&cflags&copts=[], linker_flags:list&ldflags&linkopts=[],
pkg_config_libs:list=[], pkg_config_cflags:list=[], deps:list=[], worker:str='', data:list=[], visibility:list=None, flags:str='',
labels:list&features&tags=[], flaky:bool|int=0, test_outputs:list=None, size:str=None, timeout:int=0,
sandbox:bool=None):
"""Defines a C test target.
Note that you must supply your own main() and test framework (ala cc_test when
write_main=False).
Args:
name (str): Name of the rule
srcs (list): C or C++ source files to compile.
hdrs (list): Header files.
compiler_flags (list): Flags to pass to the compiler.
linker_flags (list): Flags to pass to the linker.
pkg_config_libs (list): Libraries to declare a dependency on using pkg-config
pkg_config_cflags (list): Libraries to declare a dependency on using `pkg-config --cflags`
deps (list): Dependent rules.
data (list): Runtime data files for this test.
visibility (list): Visibility declaration for this rule.
flags (str): Flags to apply to the test invocation.
labels (list): Labels to attach to this test.
flaky (bool | int): If true the test will be marked as flaky and automatically retried.
test_outputs (list): Extra test output files to generate from this test.
size (str): Test size (enormous, large, medium or small).
timeout (int): Length of time in seconds to allow the test to run for before killing it.
sandbox (bool): Sandbox the test on Linux to restrict access to namespaces such as network.
"""
return cc_test(
name = name,
srcs = srcs,
hdrs = hdrs,
deps = deps,
worker = worker,
visibility = visibility,
compiler_flags = compiler_flags,
linker_flags = linker_flags,
pkg_config_libs = pkg_config_libs,
pkg_config_cflags = pkg_config_cflags,
data = data,
flags = flags,
labels = labels,
flaky = flaky,
test_outputs = test_outputs,
size = size,
timeout = timeout,
sandbox = sandbox,
_c = True,
write_main = False,
)