-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefs.bzl
More file actions
256 lines (193 loc) · 8.78 KB
/
defs.bzl
File metadata and controls
256 lines (193 loc) · 8.78 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
# Copyright (C) 2022 Swift Navigation Inc.
# Contact: Swift Navigation <dev@swift-nav.com>
#
# This source is subject to the license found in the file 'LICENSE' which must
# be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
"""Swift wrappers for native cc rules."""
load(":utils.bzl", "construct_local_include")
# Name for a unit test
UNIT = "unit"
# Name for a integration test
INTEGRATION = "integration"
def _common_c_opts(nocopts, pedantic = False):
# The following are set by default by Bazel:
# -Wall, -Wunused-but-set-parameter, -Wno-free-heap-object
copts = [
"-Werror",
"-Wcast-align",
"-Wcast-qual",
"-Wchar-subscripts",
"-Wcomment",
"-Wconversion",
"-Wdisabled-optimization",
"-Wextra",
"-Wfloat-equal",
"-Wformat",
"-Wformat-security",
"-Wformat-y2k",
"-Wimplicit-fallthrough",
"-Wimport",
"-Winit-self",
"-Winvalid-pch",
"-Wmissing-braces",
"-Wmissing-field-initializers",
"-Wparentheses",
"-Wpointer-arith",
"-Wredundant-decls",
"-Wreturn-type",
"-Wsequence-point",
"-Wshadow",
"-Wsign-compare",
"-Wstack-protector",
"-Wswitch",
"-Wswitch-default",
"-Wswitch-enum",
"-Wtrigraphs",
"-Wuninitialized",
"-Wunknown-pragmas",
"-Wunreachable-code",
"-Wunused",
"-Wunused-function",
"-Wunused-label",
"-Wunused-parameter",
"-Wunused-value",
"-Wunused-variable",
"-Wvolatile-register-var",
"-Wwrite-strings",
"-Wno-error=deprecated-declarations",
#TODO: [BUILD-405] - Figure out why build breaks with this flag
#"-Wmissing-include-dirs"
]
# filter nocopts from the default list
copts = [copt for copt in copts if copt not in nocopts]
if pedantic:
copts.append("-pedantic")
return copts
def _construct_local_includes(local_includes):
return [construct_local_include(path) for path in local_includes]
def swift_cc_library(**kwargs):
"""Wraps cc_library to enforce standards for a production library.
Primarily this consists of a default set of compiler options and
language standards.
Production targets (swift_cc*), are compiled with the -pedantic flag.
Args:
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_library
The following additional attributes are supported:
local_includes: List of local (non-public) include paths. Prefer
this to passing local includes using copts. Paths are expected to
be relative to the package this macro is called from.
nocopts: List of flags to remove from the default compile
options. Use judiciously.
"""
local_includes = _construct_local_includes(kwargs.pop("local_includes", []))
nocopts = kwargs.pop("nocopts", []) # pop because nocopts is a deprecated cc* attr.
copts = _common_c_opts(nocopts, pedantic = True)
copts = local_includes + copts
kwargs["copts"] = copts + (kwargs["copts"] if "copts" in kwargs else [])
native.cc_library(**kwargs)
def swift_cc_tool_library(**kwargs):
"""Wraps cc_library to enforce standards for a non-production library.
Primarily this consists of a default set of compiler options and
language standards.
Non-production targets (swift_cc_tool*), are compiled without the
-pedantic flag.
Args:
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_library
The following additional attributes are supported:
local_includes: List of local (non-public) include paths. Prefer
this to passing local includes using copts. Paths are expected to
be relative to the package this macro is called from.
nocopts: List of flags to remove from the default compile
options. Use judiciously.
"""
local_includes = _construct_local_includes(kwargs.pop("local_includes", []))
nocopts = kwargs.pop("nocopts", [])
copts = _common_c_opts(nocopts, pedantic = False)
copts = local_includes + copts
kwargs["copts"] = copts + (kwargs["copts"] if "copts" in kwargs else [])
native.cc_library(**kwargs)
def swift_cc_binary(**kwargs):
"""Wraps cc_binary to enforce standards for a production binary.
Primarily this consists of a default set of compiler options and
language standards.
Production targets (swift_cc*), are compiled with the -pedantic flag.
Args:
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_binary
The following additional attributes are supported:
local_includes: List of local (non-public) include paths. Prefer
this to passing local includes using copts. Paths are expected to
be relative to the package this macro is called from.
nocopts: List of flags to remove from the default compile
options. Use judiciously.
"""
local_includes = _construct_local_includes(kwargs.pop("local_includes", []))
nocopts = kwargs.pop("nocopts", [])
copts = _common_c_opts(nocopts, pedantic = True)
copts = local_includes + copts
kwargs["copts"] = copts + (kwargs["copts"] if "copts" in kwargs else [])
native.cc_binary(**kwargs)
def swift_cc_tool(**kwargs):
"""Wraps cc_binary to enforce standards for a non-production binary.
Primarily this consists of a default set of compiler options and
language standards.
Non-production targets (swift_cc_tool*), are compiled without the
-pedantic flag.
Args:
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_binary
The following additional attributes are supported:
local_includes: List of local (non-public) include paths. Prefer
this to passing local includes using copts. Paths are expected to
be relative to the package this macro is called from.
nocopts: List of flags to remove from the default compile
options. Use judiciously.
"""
nocopts = kwargs.pop("nocopts", [])
copts = _common_c_opts(nocopts, pedantic = False)
kwargs["copts"] = copts + (kwargs["copts"] if "copts" in kwargs else [])
native.cc_binary(**kwargs)
def swift_cc_test_library(**kwargs):
"""Wraps cc_library to enforce Swift test library conventions.
Args:
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_library
The following additional attributes are supported:
local_includes: List of local (non-public) include paths. Prefer
this to passing local includes using copts. Paths are expected to
be relative to the package this macro is called from.
"""
local_includes = _construct_local_includes(kwargs.pop("local_includes", []))
nocopts = kwargs.pop("nocopts", [])
copts = _common_c_opts(nocopts, pedantic = False)
copts = local_includes + copts
kwargs["copts"] = copts + (kwargs["copts"] if "copts" in kwargs else [])
native.cc_library(**kwargs)
def swift_cc_test(name, type, common_c_opts = False, **kwargs):
"""Wraps cc_test to enforce Swift testing conventions.
Args:
name: A unique name for this rule.
type: Specifies whether the test is a unit or integration test.
common_c_opts: Bool flag that indicates if common compilation flags will be added. Default False.
These are passed to cc_test as tags which enables running
these test types seperately: `bazel test --test_tag_filters=unit //...`
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_test
The following additional attributes are supported:
local_includes: List of local (non-public) include paths. Prefer
this to passing local includes using copts. Paths are expected to
be relative to the package this macro is called from.
"""
if not (type == UNIT or type == INTEGRATION):
fail("The 'type' attribute must be either UNIT or INTEGRATION")
local_includes = _construct_local_includes(kwargs.pop("local_includes", []))
copts = []
if common_c_opts:
nocopts = kwargs.pop("nocopts", [])
copts = _common_c_opts(nocopts, pedantic = False)
copts = local_includes + copts
kwargs["copts"] = copts + (kwargs["copts"] if "copts" in kwargs else [])
kwargs["name"] = name
kwargs["tags"] = (kwargs["tags"] if "tags" in kwargs else []) + [type]
kwargs["linkstatic"] = (kwargs["linkstatic"] if "linkstatic" in kwargs else True)
native.cc_test(**kwargs)