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