1# Copyright 2023 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( 16 "//cc_toolchain:defs.bzl", 17 "pw_cc_flag_set", 18) 19 20package(default_visibility = ["//visibility:public"]) 21 22licenses(["notice"]) 23 24# Optimization level option 25pw_cc_flag_set( 26 name = "o2", 27 actions = [ 28 "@pw_toolchain//actions:all_c_compiler_actions", 29 "@pw_toolchain//actions:all_cpp_compiler_actions", 30 "@pw_toolchain//actions:all_link_actions", 31 ], 32 flags = ["-O2"], 33) 34 35# Optimization aggressively for size rather than speed option 36pw_cc_flag_set( 37 name = "oz", 38 actions = [ 39 "@pw_toolchain//actions:all_c_compiler_actions", 40 "@pw_toolchain//actions:all_cpp_compiler_actions", 41 "@pw_toolchain//actions:all_link_actions", 42 ], 43 flags = ["-Oz"], 44) 45 46# Prevent relative paths from being converted to absolute paths. 47pw_cc_flag_set( 48 name = "no_canonical_prefixes", 49 actions = [ 50 "@pw_toolchain//actions:all_c_compiler_actions", 51 "@pw_toolchain//actions:all_cpp_compiler_actions", 52 ], 53 flags = ["-no-canonical-prefixes"], 54) 55 56# Compile without runtime type information (RTTI). This produces smaller binaries. 57pw_cc_flag_set( 58 name = "no_rtti", 59 actions = ["@pw_toolchain//actions:all_cpp_compiler_actions"], 60 flags = ["-fno-rtti"], 61) 62 63# Allow uses of the register keyword, which may appear in C headers. 64pw_cc_flag_set( 65 name = "wno_register", 66 actions = ["@pw_toolchain//actions:all_cpp_compiler_actions"], 67 flags = ["-Wno-register"], 68) 69 70# Compile for the C++17 standard. 71pw_cc_flag_set( 72 name = "c++17", 73 actions = [ 74 "@pw_toolchain//actions:all_cpp_compiler_actions", 75 "@pw_toolchain//actions:all_link_actions", 76 ], 77 flags = ["-std=c++17"], 78) 79 80# Compile for the C++20 standard. 81pw_cc_flag_set( 82 name = "c++20", 83 actions = [ 84 "@pw_toolchain//actions:all_cpp_compiler_actions", 85 "@pw_toolchain//actions:all_link_actions", 86 ], 87 flags = ["-std=c++20"], 88) 89 90# Issue a warning when a class appears to be polymorphic, yet it declares a 91# non-virtual destructor 92pw_cc_flag_set( 93 name = "wnon_virtual_dtor", 94 actions = ["@pw_toolchain//actions:all_cpp_compiler_actions"], 95 flags = ["-Wnon-virtual-dtor"], 96) 97 98# Standard compiler flags to reduce output binary size. 99pw_cc_flag_set( 100 name = "reduced_size", 101 actions = [ 102 "@pw_toolchain//actions:all_c_compiler_actions", 103 "@pw_toolchain//actions:all_cpp_compiler_actions", 104 ], 105 flags = [ 106 "-fno-common", 107 "-fno-exceptions", 108 "-ffunction-sections", 109 "-fdata-sections", 110 ], 111) 112 113pw_cc_flag_set( 114 name = "debugging", 115 actions = [ 116 "@pw_toolchain//actions:all_c_compiler_actions", 117 "@pw_toolchain//actions:all_cpp_compiler_actions", 118 ], 119 flags = ["-g"], 120) 121 122pw_cc_flag_set( 123 name = "asan", 124 actions = [ 125 "@pw_toolchain//actions:all_compiler_actions", 126 "@pw_toolchain//actions:all_link_actions", 127 ], 128 flags = [ 129 "-fsanitize=address", 130 "-DADDRESS_SANITIZER", 131 ], 132) 133 134# TODO: https://pwbug.dev/346388161 - Push this to upstream rules_cc. 135pw_cc_flag_set( 136 name = "ubsan", 137 actions = [ 138 "@pw_toolchain//actions:all_compiler_actions", 139 "@pw_toolchain//actions:all_link_actions", 140 ], 141 flags = [ 142 "-fsanitize=undefined", 143 "-DUNDEFINED_SANITIZER", 144 ], 145) 146 147# TODO: https://pwbug.dev/346388161 - Push this to upstream rules_cc. 148pw_cc_flag_set( 149 name = "tsan", 150 actions = [ 151 "@pw_toolchain//actions:all_compiler_actions", 152 "@pw_toolchain//actions:all_link_actions", 153 ], 154 flags = [ 155 "-fsanitize=thread", 156 "-DTHREAD_SANITIZER", 157 ], 158) 159 160pw_cc_flag_set( 161 name = "fuzztest", 162 actions = [ 163 "@pw_toolchain//actions:all_compiler_actions", 164 "@pw_toolchain//actions:all_link_actions", 165 ], 166 flags = [ 167 "-mcrc32", 168 "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION", 169 "-UNDEBUG", 170 "-D_LIBCPP_ENABLE_ASSERTIONS=1", 171 ], 172) 173