• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
16load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")
17
18package(default_visibility = ["//visibility:public"])
19
20# The common set of warnings for clang and arm_gcc.
21cc_args(
22    name = "common_warnings",
23    actions = [
24        "@rules_cc//cc/toolchains/actions:c_compile_actions",
25        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
26    ],
27    args = [
28        "-Wall",
29        "-Wextra",
30        "-Wimplicit-fallthrough",
31        "-Wcast-qual",
32        "-Wpointer-arith",
33        "-Wshadow",
34        "-Wredundant-decls",
35        # TODO: b/259746255 - Enable implicit conversion warnings once fixed.
36        # "-Wconversion",
37        # Make all warnings errors, except for the exemptions below.
38        "-Werror",
39        "-Wno-error=cpp",  # preprocessor #warning statement
40        "-Wno-error=deprecated-declarations",  # [[deprecated]] attribute
41    ],
42)
43
44# This config contains warnings that are enabled for upstream Pigweed.
45# This config MUST NOT be used downstream to allow for warnings to be
46# added in the future without breaking downstream.
47cc_args(
48    name = "internal_strict_warnings_flags",
49    actions = [
50        "@rules_cc//cc/toolchains/actions:c_compile_actions",
51        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
52    ],
53    args = [
54        "-Wswitch-enum",
55        "-Wextra-semi",
56    ] + select({
57        "@platforms//os:windows": [],
58        # TODO: b/243069432 - Enable pedantic warnings on Windows once passing.
59        "//conditions:default": [":pedantic_warnings"],
60    }),
61    visibility = ["//:__subpackages__"],
62)
63
64# Add `--features=internal_strict_warnings` to your Bazel invocation to enable.
65cc_feature(
66    name = "internal_strict_warnings",
67    args = [":internal_strict_warnings_flags"],
68    feature_name = "internal_strict_warnings",
69    visibility = ["//:__subpackages__"],
70)
71
72# Allow uses of the register keyword, which may appear in C headers.
73cc_args(
74    name = "wno_register",
75    actions = ["@rules_cc//cc/toolchains/actions:cpp_compile_actions"],
76    args = ["-Wno-register"],
77)
78
79# Issue a warning when a class appears to be polymorphic, yet it declares a
80# non-virtual destructor
81cc_args(
82    name = "wnon_virtual_dtor",
83    actions = ["@rules_cc//cc/toolchains/actions:cpp_compile_actions"],
84    args = ["-Wnon-virtual-dtor"],
85)
86
87# Prevent relative paths from being converted to absolute paths.
88cc_args(
89    name = "no_canonical_prefixes",
90    actions = [
91        "@rules_cc//cc/toolchains/actions:compile_actions",
92    ],
93    args = ["-no-canonical-prefixes"],
94)
95
96# Enables colors in compiler diagnostics. This uses the GCC spelling of the
97# flag, which Clang supports as an undocumented extension.
98cc_args(
99    name = "color_diagnostics",
100    actions = [
101        "@rules_cc//cc/toolchains/actions:assembly_actions",
102        "@rules_cc//cc/toolchains/actions:c_compile_actions",
103        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
104        "@rules_cc//cc/toolchains/actions:link_actions",
105    ],
106    args = ["-fdiagnostics-color"],
107)
108
109cc_args(
110    name = "debugging",
111    actions = [
112        "@rules_cc//cc/toolchains/actions:c_compile_actions",
113        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
114    ],
115    args = ["-g"],
116)
117
118# Compile without runtime type information (RTTI). This produces smaller binaries.
119cc_args(
120    name = "no_rtti",
121    actions = ["@rules_cc//cc/toolchains/actions:cpp_compile_actions"],
122    args = ["-fno-rtti"],
123)
124
125# Optimization level option
126cc_args(
127    name = "o2",
128    actions = [
129        "@rules_cc//cc/toolchains/actions:c_compile_actions",
130        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
131        "@rules_cc//cc/toolchains/actions:link_actions",
132    ],
133    args = ["-O2"],
134)
135
136# Optimization aggressively for size rather than speed option
137cc_args(
138    name = "oz",
139    actions = [
140        "@rules_cc//cc/toolchains/actions:c_compile_actions",
141        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
142        "@rules_cc//cc/toolchains/actions:link_actions",
143    ],
144    args = ["-Oz"],
145)
146
147# Standard compiler flags to reduce output binary size.
148cc_args(
149    name = "reduced_size",
150    actions = [
151        "@rules_cc//cc/toolchains/actions:c_compile_actions",
152        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
153        "@rules_cc//cc/toolchains/actions:link_actions",
154    ],
155    args = [
156        "-fno-common",
157        "-fno-exceptions",
158        "-ffunction-sections",
159        "-fdata-sections",
160    ],
161)
162
163cc_feature(
164    name = "c++17_feature",
165    args = [":c++17"],
166    feature_name = "c++17",
167)
168
169# Compile for the C++17 standard.
170cc_args(
171    name = "c++17",
172    actions = [
173        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
174        "@rules_cc//cc/toolchains/actions:link_actions",
175    ],
176    args = ["-std=c++17"],
177)
178
179cc_feature(
180    name = "c++20_feature",
181    args = [":c++20"],
182    feature_name = "c++20",
183)
184
185# Compile for the C++20 standard.
186cc_args(
187    name = "c++20",
188    actions = [
189        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
190        "@rules_cc//cc/toolchains/actions:link_actions",
191    ],
192    args = ["-std=c++20"],
193)
194
195cc_args(
196    name = "asan",
197    actions = [
198        "@rules_cc//cc/toolchains/actions:c_compile_actions",
199        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
200        "@rules_cc//cc/toolchains/actions:link_actions",
201    ],
202    args = [
203        "-fsanitize=address",
204        "-DADDRESS_SANITIZER",
205        "-fno-sanitize-recover=all",
206    ],
207)
208
209# TODO: https://pwbug.dev/346388161 - Push this to upstream rules_cc.
210cc_args(
211    name = "ubsan",
212    actions = [
213        "@rules_cc//cc/toolchains/actions:c_compile_actions",
214        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
215        "@rules_cc//cc/toolchains/actions:link_actions",
216    ],
217    args = [
218        "-fsanitize=undefined",
219        "-DUNDEFINED_SANITIZER",
220        "-fno-sanitize-recover=all",
221    ],
222)
223
224# TODO: https://pwbug.dev/346388161 - Push this to upstream rules_cc.
225cc_args(
226    name = "tsan",
227    actions = [
228        "@rules_cc//cc/toolchains/actions:c_compile_actions",
229        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
230        "@rules_cc//cc/toolchains/actions:link_actions",
231    ],
232    args = [
233        "-fsanitize=thread",
234        "-DTHREAD_SANITIZER",
235        "-fno-sanitize-recover=all",
236    ],
237)
238
239cc_args(
240    name = "fuzztest",
241    actions = [
242        "@rules_cc//cc/toolchains/actions:c_compile_actions",
243        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
244        "@rules_cc//cc/toolchains/actions:link_actions",
245    ],
246    args = [
247        "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION",
248        "-UNDEBUG",
249        "-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG",
250    ] + select({
251        "@platforms//cpu:x86_64": ["-mcrc32"],
252        "//conditions:default": [],
253    }),
254)
255
256cc_args(
257    name = "layering_check",
258    actions = [
259        "@rules_cc//cc/toolchains/actions:c_compile_actions",
260        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
261        "@rules_cc//cc/toolchains/actions:cpp_header_parsing",
262        "@rules_cc//cc/toolchains/actions:cpp_module_compile",
263    ],
264    args = [
265        "-fmodules-strict-decluse",
266        "-Wprivate-header",
267    ],
268)
269
270cc_args(
271    name = "module_name",
272    actions = [
273        "@rules_cc//cc/toolchains/actions:c_compile_actions",
274        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
275        "@rules_cc//cc/toolchains/actions:cpp_header_parsing",
276        "@rules_cc//cc/toolchains/actions:cpp_module_compile",
277    ],
278    args = select({
279        "@platforms//os:macos": [
280            "-Xclang",
281        ],
282        "//conditions:default": [],
283    }) + ["-fmodule-name={module_name}"],
284    format = {
285        "module_name": "@rules_cc//cc/toolchains/variables:module_name",
286    },
287    requires_not_none = "@rules_cc//cc/toolchains/variables:module_name",
288)
289
290cc_args(
291    name = "module_map_file",
292    actions = [
293        "@rules_cc//cc/toolchains/actions:c_compile_actions",
294        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
295        "@rules_cc//cc/toolchains/actions:cpp_header_parsing",
296        "@rules_cc//cc/toolchains/actions:cpp_module_compile",
297    ],
298    args = select({
299        "@platforms//os:macos": [
300            "-Xclang",
301        ],
302        "//conditions:default": [],
303    }) + ["-fmodule-map-file={module_map_file}"],
304    format = {
305        "module_map_file": "@rules_cc//cc/toolchains/variables:module_map_file",
306    },
307    requires_not_none = "@rules_cc//cc/toolchains/variables:module_map_file",
308)
309
310cc_args(
311    name = "dependent_module_map_files",
312    actions = [
313        "@rules_cc//cc/toolchains/actions:c_compile_actions",
314        "@rules_cc//cc/toolchains/actions:cpp_compile_actions",
315        "@rules_cc//cc/toolchains/actions:cpp_header_parsing",
316        "@rules_cc//cc/toolchains/actions:cpp_module_compile",
317    ],
318    args = select({
319        "@platforms//os:macos": ["-Xclang"],
320        "//conditions:default": [],
321    }) + [
322        "-fmodule-map-file={dependent_module_map_files}",
323    ],
324    format = {
325        "dependent_module_map_files": "@rules_cc//cc/toolchains/variables:dependent_module_map_files",
326    },
327    iterate_over = "@rules_cc//cc/toolchains/variables:dependent_module_map_files",
328)
329