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:c_compile_actions", 92 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 93 ], 94 args = ["-no-canonical-prefixes"], 95) 96 97# Enables colors in compiler diagnostics. This uses the GCC spelling of the 98# flag, which Clang supports as an undocumented extension. 99cc_args( 100 name = "color_diagnostics", 101 actions = [ 102 "@rules_cc//cc/toolchains/actions:assembly_actions", 103 "@rules_cc//cc/toolchains/actions:c_compile_actions", 104 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 105 "@rules_cc//cc/toolchains/actions:link_actions", 106 ], 107 args = ["-fdiagnostics-color"], 108) 109 110cc_args( 111 name = "debugging", 112 actions = [ 113 "@rules_cc//cc/toolchains/actions:c_compile_actions", 114 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 115 ], 116 args = ["-g"], 117) 118 119# Compile without runtime type information (RTTI). This produces smaller binaries. 120cc_args( 121 name = "no_rtti", 122 actions = ["@rules_cc//cc/toolchains/actions:cpp_compile_actions"], 123 args = ["-fno-rtti"], 124) 125 126# Optimization level option 127cc_args( 128 name = "o2", 129 actions = [ 130 "@rules_cc//cc/toolchains/actions:c_compile_actions", 131 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 132 "@rules_cc//cc/toolchains/actions:link_actions", 133 ], 134 args = ["-O2"], 135) 136 137# Optimization aggressively for size rather than speed option 138cc_args( 139 name = "oz", 140 actions = [ 141 "@rules_cc//cc/toolchains/actions:c_compile_actions", 142 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 143 "@rules_cc//cc/toolchains/actions:link_actions", 144 ], 145 args = ["-Oz"], 146) 147 148# Standard compiler flags to reduce output binary size. 149cc_args( 150 name = "reduced_size", 151 actions = [ 152 "@rules_cc//cc/toolchains/actions:c_compile_actions", 153 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 154 "@rules_cc//cc/toolchains/actions:link_actions", 155 ], 156 args = [ 157 "-fno-common", 158 "-fno-exceptions", 159 "-ffunction-sections", 160 "-fdata-sections", 161 ], 162) 163 164cc_feature( 165 name = "c++17_feature", 166 args = [":c++17"], 167 feature_name = "c++17", 168) 169 170# Compile for the C++17 standard. 171cc_args( 172 name = "c++17", 173 actions = [ 174 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 175 "@rules_cc//cc/toolchains/actions:link_actions", 176 ], 177 args = ["-std=c++17"], 178) 179 180cc_feature( 181 name = "c++20_feature", 182 args = [":c++20"], 183 feature_name = "c++20", 184) 185 186# Compile for the C++20 standard. 187cc_args( 188 name = "c++20", 189 actions = [ 190 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 191 "@rules_cc//cc/toolchains/actions:link_actions", 192 ], 193 args = ["-std=c++20"], 194) 195 196cc_args( 197 name = "asan", 198 actions = [ 199 "@rules_cc//cc/toolchains/actions:c_compile_actions", 200 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 201 "@rules_cc//cc/toolchains/actions:link_actions", 202 ], 203 args = [ 204 "-fsanitize=address", 205 "-DADDRESS_SANITIZER", 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 ], 221) 222 223# TODO: https://pwbug.dev/346388161 - Push this to upstream rules_cc. 224cc_args( 225 name = "tsan", 226 actions = [ 227 "@rules_cc//cc/toolchains/actions:c_compile_actions", 228 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 229 "@rules_cc//cc/toolchains/actions:link_actions", 230 ], 231 args = [ 232 "-fsanitize=thread", 233 "-DTHREAD_SANITIZER", 234 ], 235) 236 237cc_args( 238 name = "fuzztest", 239 actions = [ 240 "@rules_cc//cc/toolchains/actions:c_compile_actions", 241 "@rules_cc//cc/toolchains/actions:cpp_compile_actions", 242 "@rules_cc//cc/toolchains/actions:link_actions", 243 ], 244 args = [ 245 "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION", 246 "-UNDEBUG", 247 "-D_LIBCPP_ENABLE_ASSERTIONS=1", 248 ] + select({ 249 "@platforms//cpu:x86_64": ["-mcrc32"], 250 "//conditions:default": [], 251 }), 252) 253