Skip to content

Commit 2087572

Browse files
authored
feature: Build targets with consistent set of compiler flags [BUILD-406] (#10)
Targets created with swift_cc_defs.bzl will be built with a default set of compiler options.
1 parent 886c43f commit 2087572

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

swift_cc_defs.bzl

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,159 @@ UNIT = "unit"
44
# Name for a integration test
55
INTEGRATION = "integration"
66

7+
def _common_c_opts(nocopts, pedantic = False):
8+
# The following are set by default by Bazel:
9+
# -Wall, -Wunused-but-set-parameter, -Wno-free-heap-object
10+
copts = [
11+
"-Werror",
12+
"-Wcast-align",
13+
"-Wcast-qual",
14+
"-Wchar-subscripts",
15+
"-Wcomment",
16+
"-Wconversion",
17+
"-Wdisabled-optimization",
18+
"-Wextra",
19+
"-Wfloat-equal",
20+
"-Wformat",
21+
"-Wformat-security",
22+
"-Wformat-y2k",
23+
"-Wimplicit-fallthrough",
24+
"-Wimport",
25+
"-Winit-self",
26+
"-Winvalid-pch",
27+
"-Wmissing-braces",
28+
"-Wmissing-field-initializers",
29+
"-Wparentheses",
30+
"-Wpointer-arith",
31+
"-Wredundant-decls",
32+
"-Wreturn-type",
33+
"-Wsequence-point",
34+
"-Wshadow",
35+
"-Wsign-compare",
36+
"-Wstack-protector",
37+
"-Wswitch",
38+
"-Wswitch-default",
39+
"-Wswitch-enum",
40+
"-Wtrigraphs",
41+
"-Wuninitialized",
42+
"-Wunknown-pragmas",
43+
"-Wunreachable-code",
44+
"-Wunused",
45+
"-Wunused-function",
46+
"-Wunused-label",
47+
"-Wunused-parameter",
48+
"-Wunused-value",
49+
"-Wunused-variable",
50+
"-Wvolatile-register-var",
51+
"-Wwrite-strings",
52+
"-Wno-error=deprecated-declarations",
53+
#TODO: [BUILD-405] - Figure out why build breaks with this flag
54+
#"-Wmissing-include-dirs"
55+
]
56+
57+
# filter nocopts from the default list
58+
copts = [copt for copt in copts if copt not in nocopts]
59+
60+
if pedantic:
61+
copts.append("-pedantic")
62+
63+
return copts
64+
765
def swift_cc_library(**kwargs):
866
"""Wraps cc_library to enforce standards for a production library.
67+
68+
Primarily this consists of a default set of compiler options and
69+
language standards.
70+
71+
Production targets (swift_cc*), are compiled with the -pedantic flag.
72+
73+
Args:
74+
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_library
75+
76+
An additional attribute nocopts is supported. This
77+
attribute takes a list of flags to remove from the
78+
default compiler options. Use judiciously.
979
"""
80+
nocopts = kwargs.pop("nocopts", []) # pop because nocopts is a deprecated cc* attr.
81+
82+
copts = _common_c_opts(nocopts, pedantic = True)
83+
kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + copts
84+
1085
native.cc_library(**kwargs)
1186

1287
def swift_cc_tool_library(**kwargs):
1388
"""Wraps cc_library to enforce standards for a non-production library.
89+
90+
Primarily this consists of a default set of compiler options and
91+
language standards.
92+
93+
Non-production targets (swift_cc_tool*), are compiled without the
94+
-pedantic flag.
95+
96+
Args:
97+
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_library
98+
99+
An additional attribute nocopts is supported. This
100+
attribute takes a list of flags to remove from the
101+
default compiler options. Use judiciously.
14102
"""
103+
nocopts = kwargs.pop("nocopts", [])
104+
105+
copts = _common_c_opts(nocopts, pedantic = False)
106+
kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + copts
107+
15108
native.cc_library(**kwargs)
16109

17110
def swift_cc_binary(**kwargs):
18111
"""Wraps cc_binary to enforce standards for a production binary.
112+
113+
Primarily this consists of a default set of compiler options and
114+
language standards.
115+
116+
Production targets (swift_cc*), are compiled with the -pedantic flag.
117+
118+
Args:
119+
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_binary
120+
121+
An additional attribute nocopts is supported. This
122+
attribute takes a list of flags to remove from the
123+
default compiler options. Use judiciously.
19124
"""
125+
nocopts = kwargs.pop("nocopts", [])
126+
127+
copts = _common_c_opts(nocopts, pedantic = True)
128+
kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + copts
129+
20130
native.cc_binary(**kwargs)
21131

22132
def swift_cc_tool(**kwargs):
23133
"""Wraps cc_binary to enforce standards for a non-production binary.
134+
135+
Primarily this consists of a default set of compiler options and
136+
language standards.
137+
138+
Non-production targets (swift_cc_tool*), are compiled without the
139+
-pedantic flag.
140+
141+
Args:
142+
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_binary
143+
144+
An additional attribute nocopts is supported. This
145+
attribute takes a list of flags to remove from the
146+
default compiler options. Use judiciously.
24147
"""
148+
nocopts = kwargs.pop("nocopts", [])
149+
150+
copts = _common_c_opts(nocopts, pedantic = False)
151+
kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + copts
152+
25153
native.cc_binary(**kwargs)
26154

27155
def swift_cc_test_library(**kwargs):
28156
"""Wraps cc_library to enforce Swift test library conventions.
157+
158+
Args:
159+
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_test
29160
"""
30161
native.cc_library(**kwargs)
31162

@@ -38,6 +169,8 @@ def swift_cc_test(name, type, **kwargs):
38169
39170
These are passed to cc_test as tags which enables running
40171
these test types seperately: `bazel test --test_tag_filters=unit //...`
172+
173+
**kwargs: See https://bazel.build/reference/be/c-cpp#cc_test
41174
"""
42175

43176
if not (type == UNIT or type == INTEGRATION):

0 commit comments

Comments
 (0)