1*61c4878aSAndroid Build Coastguard Worker# Copyright 2020 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 Workerimport("//build_overrides/pigweed.gni") 16*61c4878aSAndroid Build Coastguard Worker 17*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_build/cc_blob_library.gni") 18*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_build/python.gni") 19*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_build/relative_source_file_names.gni") 20*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_docgen/docs.gni") 21*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_toolchain/generate_toolchain.gni") 22*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_toolchain/traits.gni") 23*61c4878aSAndroid Build Coastguard Workerimport("$dir_pw_unit_test/test.gni") 24*61c4878aSAndroid Build Coastguard Workerimport("target_types.gni") 25*61c4878aSAndroid Build Coastguard Worker 26*61c4878aSAndroid Build Coastguard Worker# IMPORTANT: The compilation flags in this file must be kept in sync with 27*61c4878aSAndroid Build Coastguard Worker# the CMake flags pw_build/CMakeLists.txt. 28*61c4878aSAndroid Build Coastguard Worker 29*61c4878aSAndroid Build Coastguard Worker# TODO: b/301262374 - Provide a better way to detect the compiler type. 30*61c4878aSAndroid Build Coastguard Workeris_clang = defined(pw_toolchain_SCOPE.cc) && 31*61c4878aSAndroid Build Coastguard Worker get_path_info(pw_toolchain_SCOPE.cc, "file") == "clang" 32*61c4878aSAndroid Build Coastguard Worker 33*61c4878aSAndroid Build Coastguard Workerconfig("colorize_output") { 34*61c4878aSAndroid Build Coastguard Worker if (pw_build_COLORIZE_OUTPUT) { 35*61c4878aSAndroid Build Coastguard Worker cflags = [ 36*61c4878aSAndroid Build Coastguard Worker # Colorize output. Ninja's Clang invocation disables color by default. 37*61c4878aSAndroid Build Coastguard Worker "-fdiagnostics-color", 38*61c4878aSAndroid Build Coastguard Worker ] 39*61c4878aSAndroid Build Coastguard Worker ldflags = cflags 40*61c4878aSAndroid Build Coastguard Worker } 41*61c4878aSAndroid Build Coastguard Worker} 42*61c4878aSAndroid Build Coastguard Worker 43*61c4878aSAndroid Build Coastguard Workerconfig("debugging") { 44*61c4878aSAndroid Build Coastguard Worker # Enable debug symbol generation. This has no effect on final code size. 45*61c4878aSAndroid Build Coastguard Worker cflags = [ "-g" ] 46*61c4878aSAndroid Build Coastguard Worker rustflags = [ 47*61c4878aSAndroid Build Coastguard Worker "-Cdebuginfo=2", 48*61c4878aSAndroid Build Coastguard Worker "-Clink-arg=-g", 49*61c4878aSAndroid Build Coastguard Worker ] 50*61c4878aSAndroid Build Coastguard Worker} 51*61c4878aSAndroid Build Coastguard Worker 52*61c4878aSAndroid Build Coastguard Workerconfig("extra_debugging") { 53*61c4878aSAndroid Build Coastguard Worker # Include things like macro expansion in debug info. 54*61c4878aSAndroid Build Coastguard Worker cflags = [ "-g3" ] 55*61c4878aSAndroid Build Coastguard Worker rustflags = [ 56*61c4878aSAndroid Build Coastguard Worker "-Cdebuginfo=2", 57*61c4878aSAndroid Build Coastguard Worker "-Clink-arg=-g3", 58*61c4878aSAndroid Build Coastguard Worker ] 59*61c4878aSAndroid Build Coastguard Worker} 60*61c4878aSAndroid Build Coastguard Worker 61*61c4878aSAndroid Build Coastguard Worker# Optimization levels 62*61c4878aSAndroid Build Coastguard Workerconfig("optimize_debugging") { 63*61c4878aSAndroid Build Coastguard Worker cflags = [ "-Og" ] 64*61c4878aSAndroid Build Coastguard Worker ldflags = cflags 65*61c4878aSAndroid Build Coastguard Worker rustflags = [ "-Clink-arg=-Og" ] 66*61c4878aSAndroid Build Coastguard Worker} 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard Workerconfig("optimize_speed") { 69*61c4878aSAndroid Build Coastguard Worker cflags = [ "-O2" ] 70*61c4878aSAndroid Build Coastguard Worker ldflags = cflags 71*61c4878aSAndroid Build Coastguard Worker rustflags = [ 72*61c4878aSAndroid Build Coastguard Worker "-Copt-level=2", 73*61c4878aSAndroid Build Coastguard Worker "-Clink-arg=-O2", 74*61c4878aSAndroid Build Coastguard Worker ] 75*61c4878aSAndroid Build Coastguard Worker} 76*61c4878aSAndroid Build Coastguard Worker 77*61c4878aSAndroid Build Coastguard Workerconfig("optimize_more_speed") { 78*61c4878aSAndroid Build Coastguard Worker cflags = [ "-O3" ] 79*61c4878aSAndroid Build Coastguard Worker ldflags = cflags 80*61c4878aSAndroid Build Coastguard Worker rustflags = [ 81*61c4878aSAndroid Build Coastguard Worker "-Copt-level=3", 82*61c4878aSAndroid Build Coastguard Worker "-Clink-arg=-O3", 83*61c4878aSAndroid Build Coastguard Worker ] 84*61c4878aSAndroid Build Coastguard Worker} 85*61c4878aSAndroid Build Coastguard Worker 86*61c4878aSAndroid Build Coastguard Workerconfig("optimize_size") { 87*61c4878aSAndroid Build Coastguard Worker cflags = [ "-Os" ] 88*61c4878aSAndroid Build Coastguard Worker ldflags = cflags 89*61c4878aSAndroid Build Coastguard Worker rustflags = [ 90*61c4878aSAndroid Build Coastguard Worker "-Copt-level=s", 91*61c4878aSAndroid Build Coastguard Worker "-Clink-arg=-Os", 92*61c4878aSAndroid Build Coastguard Worker ] 93*61c4878aSAndroid Build Coastguard Worker} 94*61c4878aSAndroid Build Coastguard Worker 95*61c4878aSAndroid Build Coastguard Workerconfig("enable_clang_fixed_point_types") { 96*61c4878aSAndroid Build Coastguard Worker if (is_clang) { 97*61c4878aSAndroid Build Coastguard Worker cflags = [ "-ffixed-point" ] 98*61c4878aSAndroid Build Coastguard Worker } 99*61c4878aSAndroid Build Coastguard Worker} 100*61c4878aSAndroid Build Coastguard Worker 101*61c4878aSAndroid Build Coastguard Workerconfig("enable_clang_mlinliner") { 102*61c4878aSAndroid Build Coastguard Worker cflags = [ 103*61c4878aSAndroid Build Coastguard Worker # Enable ML inliner. 104*61c4878aSAndroid Build Coastguard Worker "-mllvm", 105*61c4878aSAndroid Build Coastguard Worker "-enable-ml-inliner=release", 106*61c4878aSAndroid Build Coastguard Worker ] 107*61c4878aSAndroid Build Coastguard Worker} 108*61c4878aSAndroid Build Coastguard Worker 109*61c4878aSAndroid Build Coastguard Workerconfig("enable_clang_gvn_sink_hoist") { 110*61c4878aSAndroid Build Coastguard Worker cflags = [ 111*61c4878aSAndroid Build Coastguard Worker # Enable GVN sink/hoist. 112*61c4878aSAndroid Build Coastguard Worker "-mllvm", 113*61c4878aSAndroid Build Coastguard Worker "-enable-gvn-sink=1", 114*61c4878aSAndroid Build Coastguard Worker "-mllvm", 115*61c4878aSAndroid Build Coastguard Worker "-enable-gvn-hoist=1", 116*61c4878aSAndroid Build Coastguard Worker ] 117*61c4878aSAndroid Build Coastguard Worker} 118*61c4878aSAndroid Build Coastguard Worker 119*61c4878aSAndroid Build Coastguard Workerconfig("optimize_size_clang") { 120*61c4878aSAndroid Build Coastguard Worker cflags = [ "-Oz" ] 121*61c4878aSAndroid Build Coastguard Worker ldflags = [ 122*61c4878aSAndroid Build Coastguard Worker # Identical Code Folding optimization 123*61c4878aSAndroid Build Coastguard Worker "-Wl,--icf=all", 124*61c4878aSAndroid Build Coastguard Worker 125*61c4878aSAndroid Build Coastguard Worker # LLD does not recognize `Oz` which is a compiler front-end flag. 126*61c4878aSAndroid Build Coastguard Worker "-Wl,-O2", 127*61c4878aSAndroid Build Coastguard Worker ] 128*61c4878aSAndroid Build Coastguard Worker ldflags += cflags 129*61c4878aSAndroid Build Coastguard Worker rustflags = [] 130*61c4878aSAndroid Build Coastguard Worker foreach(f, ldflags) { 131*61c4878aSAndroid Build Coastguard Worker rustflags += [ "-Clink-arg=$f" ] 132*61c4878aSAndroid Build Coastguard Worker } 133*61c4878aSAndroid Build Coastguard Worker} 134*61c4878aSAndroid Build Coastguard Worker 135*61c4878aSAndroid Build Coastguard Worker# Standard compiler flags to reduce output binary size. 136*61c4878aSAndroid Build Coastguard Workerconfig("reduced_size") { 137*61c4878aSAndroid Build Coastguard Worker cflags = [ 138*61c4878aSAndroid Build Coastguard Worker "-fno-common", 139*61c4878aSAndroid Build Coastguard Worker "-fno-exceptions", 140*61c4878aSAndroid Build Coastguard Worker "-ffunction-sections", 141*61c4878aSAndroid Build Coastguard Worker "-fdata-sections", 142*61c4878aSAndroid Build Coastguard Worker "-fomit-frame-pointer", 143*61c4878aSAndroid Build Coastguard Worker ] 144*61c4878aSAndroid Build Coastguard Worker cflags_cc = [ "-fno-rtti" ] 145*61c4878aSAndroid Build Coastguard Worker 146*61c4878aSAndroid Build Coastguard Worker if (current_os == "mac" || current_os == "ios") { 147*61c4878aSAndroid Build Coastguard Worker # Delete unreferenced sections. Helpful with -ffunction-sections. 148*61c4878aSAndroid Build Coastguard Worker ldflags = [ "-Wl,-dead_strip" ] 149*61c4878aSAndroid Build Coastguard Worker rustflags = [ "-Clink-arg=-Wl,-dead_strip" ] 150*61c4878aSAndroid Build Coastguard Worker } else { 151*61c4878aSAndroid Build Coastguard Worker # Delete unreferenced sections. Helpful with -ffunction-sections. 152*61c4878aSAndroid Build Coastguard Worker ldflags = [ "-Wl,--gc-sections" ] 153*61c4878aSAndroid Build Coastguard Worker rustflags = [ "-Clink-arg=-Wl,--gc-sections" ] 154*61c4878aSAndroid Build Coastguard Worker } 155*61c4878aSAndroid Build Coastguard Worker} 156*61c4878aSAndroid Build Coastguard Worker 157*61c4878aSAndroid Build Coastguard Workerconfig("rust_edition_2015") { 158*61c4878aSAndroid Build Coastguard Worker rustflags = [ "--edition=2015" ] 159*61c4878aSAndroid Build Coastguard Worker} 160*61c4878aSAndroid Build Coastguard Worker 161*61c4878aSAndroid Build Coastguard Workerconfig("rust_edition_2018") { 162*61c4878aSAndroid Build Coastguard Worker rustflags = [ "--edition=2018" ] 163*61c4878aSAndroid Build Coastguard Worker} 164*61c4878aSAndroid Build Coastguard Worker 165*61c4878aSAndroid Build Coastguard Workerconfig("rust_edition_2021") { 166*61c4878aSAndroid Build Coastguard Worker rustflags = [ "--edition=2021" ] 167*61c4878aSAndroid Build Coastguard Worker} 168*61c4878aSAndroid Build Coastguard Worker 169*61c4878aSAndroid Build Coastguard Workerconfig("strict_warnings") { 170*61c4878aSAndroid Build Coastguard Worker cflags = [ 171*61c4878aSAndroid Build Coastguard Worker "-Wall", 172*61c4878aSAndroid Build Coastguard Worker "-Wextra", 173*61c4878aSAndroid Build Coastguard Worker "-Wimplicit-fallthrough", 174*61c4878aSAndroid Build Coastguard Worker "-Wcast-qual", 175*61c4878aSAndroid Build Coastguard Worker "-Wundef", 176*61c4878aSAndroid Build Coastguard Worker "-Wpointer-arith", 177*61c4878aSAndroid Build Coastguard Worker 178*61c4878aSAndroid Build Coastguard Worker # Make all warnings errors, except for the exemptions below. 179*61c4878aSAndroid Build Coastguard Worker "-Werror", 180*61c4878aSAndroid Build Coastguard Worker "-Wno-error=cpp", # preprocessor #warning statement 181*61c4878aSAndroid Build Coastguard Worker "-Wno-error=deprecated-declarations", # [[deprecated]] attribute 182*61c4878aSAndroid Build Coastguard Worker ] 183*61c4878aSAndroid Build Coastguard Worker cflags_cc = [ "-Wnon-virtual-dtor" ] 184*61c4878aSAndroid Build Coastguard Worker} 185*61c4878aSAndroid Build Coastguard Worker 186*61c4878aSAndroid Build Coastguard Worker# Thread safety warnings are only supported by Clang. 187*61c4878aSAndroid Build Coastguard Workerconfig("clang_thread_safety_warnings") { 188*61c4878aSAndroid Build Coastguard Worker cflags = [ "-Wthread-safety" ] 189*61c4878aSAndroid Build Coastguard Worker defines = [ "_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS=1" ] 190*61c4878aSAndroid Build Coastguard Worker} 191*61c4878aSAndroid Build Coastguard Worker 192*61c4878aSAndroid Build Coastguard Worker# This config contains warnings that we don't necessarily recommend projects 193*61c4878aSAndroid Build Coastguard Worker# enable, but are enabled for upstream Pigweed for maximum project 194*61c4878aSAndroid Build Coastguard Worker# compatibility. 195*61c4878aSAndroid Build Coastguard Workerconfig("extra_strict_warnings") { 196*61c4878aSAndroid Build Coastguard Worker cflags = [ 197*61c4878aSAndroid Build Coastguard Worker "-Wshadow", 198*61c4878aSAndroid Build Coastguard Worker "-Wredundant-decls", 199*61c4878aSAndroid Build Coastguard Worker ] 200*61c4878aSAndroid Build Coastguard Worker cflags_c = [ "-Wstrict-prototypes" ] 201*61c4878aSAndroid Build Coastguard Worker} 202*61c4878aSAndroid Build Coastguard Worker 203*61c4878aSAndroid Build Coastguard Worker# This config contains warnings that are enabled for upstream Pigweed. 204*61c4878aSAndroid Build Coastguard Worker# This config MUST NOT be used downstream to allow for warnings to be 205*61c4878aSAndroid Build Coastguard Worker# added in the future without breaking downstream. 206*61c4878aSAndroid Build Coastguard Workerconfig("internal_strict_warnings") { 207*61c4878aSAndroid Build Coastguard Worker configs = [ 208*61c4878aSAndroid Build Coastguard Worker ":internal_strict_warnings_core", 209*61c4878aSAndroid Build Coastguard Worker ":shadow_all", 210*61c4878aSAndroid Build Coastguard Worker ] 211*61c4878aSAndroid Build Coastguard Worker} 212*61c4878aSAndroid Build Coastguard Worker 213*61c4878aSAndroid Build Coastguard Worker# Core flags from ":internal_strict_warnings" that are enabled throughout 214*61c4878aSAndroid Build Coastguard Worker# Pigweed. These are split out so ":shadow_all" can be disabled where needed. 215*61c4878aSAndroid Build Coastguard Workerconfig("internal_strict_warnings_core") { 216*61c4878aSAndroid Build Coastguard Worker cflags = [ "-Wswitch-enum" ] 217*61c4878aSAndroid Build Coastguard Worker 218*61c4878aSAndroid Build Coastguard Worker if (is_clang) { 219*61c4878aSAndroid Build Coastguard Worker cflags += [ "-Wextra-semi" ] 220*61c4878aSAndroid Build Coastguard Worker } else { 221*61c4878aSAndroid Build Coastguard Worker # TODO: b/306734552 - On GCC, this only works as a C++ flag. 222*61c4878aSAndroid Build Coastguard Worker cflags_cc = [ "-Wextra-semi" ] 223*61c4878aSAndroid Build Coastguard Worker } 224*61c4878aSAndroid Build Coastguard Worker 225*61c4878aSAndroid Build Coastguard Worker # TODO: b/243069432 - Enable pedantic warnings on Windows when they pass. 226*61c4878aSAndroid Build Coastguard Worker if (host_os != "win") { 227*61c4878aSAndroid Build Coastguard Worker configs = [ ":pedantic_warnings" ] 228*61c4878aSAndroid Build Coastguard Worker } 229*61c4878aSAndroid Build Coastguard Worker} 230*61c4878aSAndroid Build Coastguard Worker 231*61c4878aSAndroid Build Coastguard Worker# Keep "-Wshadow-all" separate so it can be disabled individually. 232*61c4878aSAndroid Build Coastguard Workerconfig("shadow_all") { 233*61c4878aSAndroid Build Coastguard Worker if (is_clang) { 234*61c4878aSAndroid Build Coastguard Worker cflags = [ "-Wshadow-all" ] 235*61c4878aSAndroid Build Coastguard Worker } 236*61c4878aSAndroid Build Coastguard Worker} 237*61c4878aSAndroid Build Coastguard Worker 238*61c4878aSAndroid Build Coastguard Workerconfig("pedantic_warnings") { 239*61c4878aSAndroid Build Coastguard Worker cflags = [ 240*61c4878aSAndroid Build Coastguard Worker # Enable -Wpedantic, but disable a few warnings. 241*61c4878aSAndroid Build Coastguard Worker "-Wpedantic", 242*61c4878aSAndroid Build Coastguard Worker 243*61c4878aSAndroid Build Coastguard Worker # Allow designated initializers, which were added in C++20 but widely 244*61c4878aSAndroid Build Coastguard Worker # supported prior and permitted by the Google style guide. 245*61c4878aSAndroid Build Coastguard Worker "-Wno-c++20-designator", 246*61c4878aSAndroid Build Coastguard Worker 247*61c4878aSAndroid Build Coastguard Worker # Allow empty ... arguments in macros, which are permitted in C++20 but 248*61c4878aSAndroid Build Coastguard Worker # widely supported prior. 249*61c4878aSAndroid Build Coastguard Worker "-Wno-gnu-zero-variadic-macro-arguments", 250*61c4878aSAndroid Build Coastguard Worker ] 251*61c4878aSAndroid Build Coastguard Worker 252*61c4878aSAndroid Build Coastguard Worker if (pw_toolchain_CXX_STANDARD < pw_toolchain_STANDARD.CXX17) { 253*61c4878aSAndroid Build Coastguard Worker # Allow C++17 attributes in C++14, since Pigweed targets C++17 or newer. 254*61c4878aSAndroid Build Coastguard Worker cflags += [ "-Wno-c++17-attribute-extensions" ] 255*61c4878aSAndroid Build Coastguard Worker } 256*61c4878aSAndroid Build Coastguard Worker 257*61c4878aSAndroid Build Coastguard Worker # TODO: b/333712899 - Enable C23 extension warnings. 258*61c4878aSAndroid Build Coastguard Worker cflags += [ "-Wno-c23-extensions" ] 259*61c4878aSAndroid Build Coastguard Worker 260*61c4878aSAndroid Build Coastguard Worker # TODO: b/335021928 - Enable C++ 20 extension warnings. 261*61c4878aSAndroid Build Coastguard Worker cflags += [ "-Wno-c++20-extensions" ] 262*61c4878aSAndroid Build Coastguard Worker 263*61c4878aSAndroid Build Coastguard Worker # TODO: b/335328444 - Enable C++ 20 extension warnings. 264*61c4878aSAndroid Build Coastguard Worker cflags += [ "-Wno-deprecated-pragma" ] 265*61c4878aSAndroid Build Coastguard Worker 266*61c4878aSAndroid Build Coastguard Worker cflags += [ 267*61c4878aSAndroid Build Coastguard Worker # TODO: b/356904203 - Unblock Clang roll. Missing template argument 268*61c4878aSAndroid Build Coastguard Worker # warning introduced by Clang produces warnings in third party code. 269*61c4878aSAndroid Build Coastguard Worker # Remove this warning suppression after addressing the root cause. 270*61c4878aSAndroid Build Coastguard Worker "-Wno-missing-template-arg-list-after-template-kw", 271*61c4878aSAndroid Build Coastguard Worker 272*61c4878aSAndroid Build Coastguard Worker # TODO: b/356689444 - Remove no-unknown-warning-option after Clang roll. 273*61c4878aSAndroid Build Coastguard Worker "-Wno-unknown-warning-option", 274*61c4878aSAndroid Build Coastguard Worker ] 275*61c4878aSAndroid Build Coastguard Worker} 276*61c4878aSAndroid Build Coastguard Worker 277*61c4878aSAndroid Build Coastguard Worker# Numeric conversion warnings. 278*61c4878aSAndroid Build Coastguard Worker# 279*61c4878aSAndroid Build Coastguard Worker# Originally Pigweed didn't enable this, but we ultimately decided to turn it 280*61c4878aSAndroid Build Coastguard Worker# on since it caused issues with downstream project that enable this warning. 281*61c4878aSAndroid Build Coastguard Worker# 282*61c4878aSAndroid Build Coastguard Worker# b/259746255 tracks converting everything to build with this warning. 283*61c4878aSAndroid Build Coastguard Workerconfig("conversion_warnings") { 284*61c4878aSAndroid Build Coastguard Worker # TODO: b/260629756 - Remove Windows restriction once fixed for Windows + GCC. 285*61c4878aSAndroid Build Coastguard Worker if (host_os != "win") { 286*61c4878aSAndroid Build Coastguard Worker cflags = [ "-Wconversion" ] 287*61c4878aSAndroid Build Coastguard Worker } 288*61c4878aSAndroid Build Coastguard Worker} 289*61c4878aSAndroid Build Coastguard Worker 290*61c4878aSAndroid Build Coastguard Workerconfig("cpp14") { 291*61c4878aSAndroid Build Coastguard Worker cflags_cc = [ "-std=c++14" ] 292*61c4878aSAndroid Build Coastguard Worker} 293*61c4878aSAndroid Build Coastguard Worker 294*61c4878aSAndroid Build Coastguard Workerconfig("cpp17") { 295*61c4878aSAndroid Build Coastguard Worker cflags_cc = [ 296*61c4878aSAndroid Build Coastguard Worker "-std=c++17", 297*61c4878aSAndroid Build Coastguard Worker 298*61c4878aSAndroid Build Coastguard Worker # Allow uses of the register keyword, which may appear in C headers. 299*61c4878aSAndroid Build Coastguard Worker "-Wno-register", 300*61c4878aSAndroid Build Coastguard Worker ] 301*61c4878aSAndroid Build Coastguard Worker} 302*61c4878aSAndroid Build Coastguard Worker 303*61c4878aSAndroid Build Coastguard Workerconfig("cpp20") { 304*61c4878aSAndroid Build Coastguard Worker cflags_cc = [ 305*61c4878aSAndroid Build Coastguard Worker "-std=c++20", 306*61c4878aSAndroid Build Coastguard Worker "-Wno-register", 307*61c4878aSAndroid Build Coastguard Worker ] 308*61c4878aSAndroid Build Coastguard Worker} 309*61c4878aSAndroid Build Coastguard Worker 310*61c4878aSAndroid Build Coastguard Worker# Selects the C++ standard to used based on the pw_toolchain_CXX_STANDARD 311*61c4878aSAndroid Build Coastguard Worker# toolchain trait. 312*61c4878aSAndroid Build Coastguard Workerconfig("toolchain_cpp_standard") { 313*61c4878aSAndroid Build Coastguard Worker if (pw_toolchain_CXX_STANDARD == pw_toolchain_STANDARD.CXX14) { 314*61c4878aSAndroid Build Coastguard Worker configs = [ ":cpp14" ] 315*61c4878aSAndroid Build Coastguard Worker } else if (pw_toolchain_CXX_STANDARD == pw_toolchain_STANDARD.CXX17) { 316*61c4878aSAndroid Build Coastguard Worker configs = [ ":cpp17" ] 317*61c4878aSAndroid Build Coastguard Worker } else if (pw_toolchain_CXX_STANDARD == pw_toolchain_STANDARD.CXX20) { 318*61c4878aSAndroid Build Coastguard Worker configs = [ ":cpp20" ] 319*61c4878aSAndroid Build Coastguard Worker } else { 320*61c4878aSAndroid Build Coastguard Worker assert(false, 321*61c4878aSAndroid Build Coastguard Worker "Unexpected pw_toolchain_CXX_STANDARD value: " + 322*61c4878aSAndroid Build Coastguard Worker pw_toolchain_CXX_STANDARD) 323*61c4878aSAndroid Build Coastguard Worker } 324*61c4878aSAndroid Build Coastguard Worker} 325*61c4878aSAndroid Build Coastguard Worker 326*61c4878aSAndroid Build Coastguard Worker# Removes system-dependent prefixes from macros like __FILE__ and debug symbols. 327*61c4878aSAndroid Build Coastguard Worker# 328*61c4878aSAndroid Build Coastguard Worker# All compilation is relative to root_build_dir. The root_build_dir path is 329*61c4878aSAndroid Build Coastguard Worker# arbitrary, so it must be removed to make builds reproducible. This config 330*61c4878aSAndroid Build Coastguard Worker# remaps paths as if compilation occurred from the repository root instead of 331*61c4878aSAndroid Build Coastguard Worker# root_build_dir. Paths that include root_build_dir are updated to standardize 332*61c4878aSAndroid Build Coastguard Worker# on out/ as the build directory. 333*61c4878aSAndroid Build Coastguard Worker# 334*61c4878aSAndroid Build Coastguard Worker# If an ELF is built from a directory other than "out/", debuggers will be able 335*61c4878aSAndroid Build Coastguard Worker# to locate in-tree sources, but may have issues finding generated sources in 336*61c4878aSAndroid Build Coastguard Worker# the output directory. This can be worked around in a few ways: 337*61c4878aSAndroid Build Coastguard Worker# 338*61c4878aSAndroid Build Coastguard Worker# - Use GDB's `set substitute-path <from> <to>` option to remap paths. 339*61c4878aSAndroid Build Coastguard Worker# - Rebuild from out/. 340*61c4878aSAndroid Build Coastguard Worker# - Symlink out/ to the build directory. 341*61c4878aSAndroid Build Coastguard Worker# - Temporarily disable these transformations. 342*61c4878aSAndroid Build Coastguard Worker# 343*61c4878aSAndroid Build Coastguard Worker# If an ELF is built from "out/", debuggers will be able to locate all sources, 344*61c4878aSAndroid Build Coastguard Worker# including generated sources, when run from the repo root. 345*61c4878aSAndroid Build Coastguard Workerconfig("relative_paths") { 346*61c4878aSAndroid Build Coastguard Worker # Apply a series of file-prefix-map transformations. Only the first matching 347*61c4878aSAndroid Build Coastguard Worker # option applies. 348*61c4878aSAndroid Build Coastguard Worker # 349*61c4878aSAndroid Build Coastguard Worker # GCC applies these in reverse order due to building iterating through a 350*61c4878aSAndroid Build Coastguard Worker # recursively constructed linked list: 351*61c4878aSAndroid Build Coastguard Worker # inclusive-language: disable 352*61c4878aSAndroid Build Coastguard Worker # https://github.com/gcc-mirror/gcc/blob/master/gcc/file-prefix-map.cc#L41. 353*61c4878aSAndroid Build Coastguard Worker # inclusive-language: enable 354*61c4878aSAndroid Build Coastguard Worker # 355*61c4878aSAndroid Build Coastguard Worker # Clang currently does not have a set forwards or backwards application order 356*61c4878aSAndroid Build Coastguard Worker # due to storing them in a std::map (that reorders lexicogrpahically by key): 357*61c4878aSAndroid Build Coastguard Worker # https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Basic/CodeGenOptions.h#L209. 358*61c4878aSAndroid Build Coastguard Worker # 359*61c4878aSAndroid Build Coastguard Worker # TODO: b/278906020 - Tracks merging in an upstream change to LLVM to that will 360*61c4878aSAndroid Build Coastguard Worker # enforce clang applying these transformations in order. Until then, there 361*61c4878aSAndroid Build Coastguard Worker # will be issues with the debug and coverage information while using build 362*61c4878aSAndroid Build Coastguard Worker # directories that are more than a single directory from the root project 363*61c4878aSAndroid Build Coastguard Worker # directory. This is due to "=out/" always being applied first due to the 364*61c4878aSAndroid Build Coastguard Worker # left-hand side of the = being "", which is always ordered first in the 365*61c4878aSAndroid Build Coastguard Worker # std::map. 366*61c4878aSAndroid Build Coastguard Worker _transformations = [ 367*61c4878aSAndroid Build Coastguard Worker # Remap absolute prefixes for files in the output directory to out/. 368*61c4878aSAndroid Build Coastguard Worker rebase_path(root_build_dir) + "/=out/", 369*61c4878aSAndroid Build Coastguard Worker 370*61c4878aSAndroid Build Coastguard Worker # Remove the path to the out directory so the ELF is relative to the root. 371*61c4878aSAndroid Build Coastguard Worker rebase_path(root_build_dir) + "=", 372*61c4878aSAndroid Build Coastguard Worker 373*61c4878aSAndroid Build Coastguard Worker # Remove any absolute paths to the repo root. 374*61c4878aSAndroid Build Coastguard Worker rebase_path("//") + "=", 375*61c4878aSAndroid Build Coastguard Worker 376*61c4878aSAndroid Build Coastguard Worker # Remove the relative path from the build directory to the root, which is 377*61c4878aSAndroid Build Coastguard Worker # the prefix of in-tree sources. 378*61c4878aSAndroid Build Coastguard Worker rebase_path("//", root_build_dir) + "=", 379*61c4878aSAndroid Build Coastguard Worker 380*61c4878aSAndroid Build Coastguard Worker # Prepend out/ to any unmatched files, which are in the output directory. 381*61c4878aSAndroid Build Coastguard Worker "=out/", 382*61c4878aSAndroid Build Coastguard Worker 383*61c4878aSAndroid Build Coastguard Worker # Repeat the replacements in reverse order since GCC applies them backwards. 384*61c4878aSAndroid Build Coastguard Worker rebase_path("//", root_build_dir) + "=", 385*61c4878aSAndroid Build Coastguard Worker rebase_path("//") + "=", 386*61c4878aSAndroid Build Coastguard Worker rebase_path(root_build_dir) + "=", 387*61c4878aSAndroid Build Coastguard Worker rebase_path(root_build_dir) + "/=out/", 388*61c4878aSAndroid Build Coastguard Worker ] 389*61c4878aSAndroid Build Coastguard Worker 390*61c4878aSAndroid Build Coastguard Worker cflags = [] 391*61c4878aSAndroid Build Coastguard Worker 392*61c4878aSAndroid Build Coastguard Worker foreach(transform, _transformations) { 393*61c4878aSAndroid Build Coastguard Worker cflags += [ "-ffile-prefix-map=" + transform ] 394*61c4878aSAndroid Build Coastguard Worker } 395*61c4878aSAndroid Build Coastguard Worker 396*61c4878aSAndroid Build Coastguard Worker # Write the transformations to a well known path so that other utilities 397*61c4878aSAndroid Build Coastguard Worker # that need to present file names that match the compiler's __FILE__ 398*61c4878aSAndroid Build Coastguard Worker # macro can apply the same transformation. 399*61c4878aSAndroid Build Coastguard Worker write_file(pw_build_RELATIVE_PATH_TRANSFORM_JSON, _transformations, "json") 400*61c4878aSAndroid Build Coastguard Worker} 401*61c4878aSAndroid Build Coastguard Worker 402*61c4878aSAndroid Build Coastguard Workerconfig("link_with_lld") { 403*61c4878aSAndroid Build Coastguard Worker ldflags = [ "-fuse-ld=lld" ] 404*61c4878aSAndroid Build Coastguard Worker} 405*61c4878aSAndroid Build Coastguard Worker 406*61c4878aSAndroid Build Coastguard Worker# This group is linked into all pw_executable, pw_static_library, and 407*61c4878aSAndroid Build Coastguard Worker# pw_shared_library targets. This makes it possible to ensure symbols are 408*61c4878aSAndroid Build Coastguard Worker# defined without a dependency on them. 409*61c4878aSAndroid Build Coastguard Worker# 410*61c4878aSAndroid Build Coastguard Worker# pw_build_LINK_DEPS should only be used when necessary. For example, 411*61c4878aSAndroid Build Coastguard Worker# pw_assert relies on pw_build_LINK_DEPS to avoid circular dependencies 412*61c4878aSAndroid Build Coastguard Worker# in GN. In almost all other cases, build targets should explicitly depend on 413*61c4878aSAndroid Build Coastguard Worker# the other targets they use. 414*61c4878aSAndroid Build Coastguard Worker# 415*61c4878aSAndroid Build Coastguard Worker# pw_build_TOOLCHAIN_LINK_DEPS is used to define dependencies introduced by 416*61c4878aSAndroid Build Coastguard Worker# the various toolchains created under pw_toolchain module in GN. For example, 417*61c4878aSAndroid Build Coastguard Worker# LLVM compiler-rt builtin libraries, or the arm-none-eabi newlib interface 418*61c4878aSAndroid Build Coastguard Worker# stubs. 419*61c4878aSAndroid Build Coastguard Workergroup("link_deps") { 420*61c4878aSAndroid Build Coastguard Worker deps = pw_build_LINK_DEPS + pw_build_TOOLCHAIN_LINK_DEPS 421*61c4878aSAndroid Build Coastguard Worker} 422*61c4878aSAndroid Build Coastguard Worker 423*61c4878aSAndroid Build Coastguard Worker# This empty target is used as the default value for module configurations. 424*61c4878aSAndroid Build Coastguard Worker# Projects may set pw_build_DEFAULT_MODULE_CONFIG to a different GN target that 425*61c4878aSAndroid Build Coastguard Worker# overrides modules' configuration options via macro definitions or a header 426*61c4878aSAndroid Build Coastguard Worker# forcibly included with `-include`. 427*61c4878aSAndroid Build Coastguard Workergroup("empty") { 428*61c4878aSAndroid Build Coastguard Worker} 429*61c4878aSAndroid Build Coastguard Worker 430*61c4878aSAndroid Build Coastguard Workerconfig("default_config") { 431*61c4878aSAndroid Build Coastguard Worker include_dirs = [ "public" ] 432*61c4878aSAndroid Build Coastguard Worker} 433*61c4878aSAndroid Build Coastguard Worker 434*61c4878aSAndroid Build Coastguard Worker# Linker script utility PW_MUST_PLACE 435*61c4878aSAndroid Build Coastguard Workerpw_source_set("must_place") { 436*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":default_config" ] 437*61c4878aSAndroid Build Coastguard Worker public = [ "public/pw_build/must_place.ld.h" ] 438*61c4878aSAndroid Build Coastguard Worker} 439*61c4878aSAndroid Build Coastguard Worker 440*61c4878aSAndroid Build Coastguard Workerpw_source_set("linker_symbol") { 441*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":default_config" ] 442*61c4878aSAndroid Build Coastguard Worker public = [ "public/pw_build/linker_symbol.h" ] 443*61c4878aSAndroid Build Coastguard Worker public_deps = [ "$dir_pw_third_party/fuchsia:stdcompat" ] 444*61c4878aSAndroid Build Coastguard Worker} 445*61c4878aSAndroid Build Coastguard Worker 446*61c4878aSAndroid Build Coastguard Workerpw_doc_group("docs") { 447*61c4878aSAndroid Build Coastguard Worker sources = [ 448*61c4878aSAndroid Build Coastguard Worker "bazel.rst", 449*61c4878aSAndroid Build Coastguard Worker "cmake.rst", 450*61c4878aSAndroid Build Coastguard Worker "docs.rst", 451*61c4878aSAndroid Build Coastguard Worker "gn.rst", 452*61c4878aSAndroid Build Coastguard Worker "linker_scripts.rst", 453*61c4878aSAndroid Build Coastguard Worker "project_builder.rst", 454*61c4878aSAndroid Build Coastguard Worker "python.rst", 455*61c4878aSAndroid Build Coastguard Worker ] 456*61c4878aSAndroid Build Coastguard Worker} 457*61c4878aSAndroid Build Coastguard Worker 458*61c4878aSAndroid Build Coastguard Workerconfig("module_config_test_config") { 459*61c4878aSAndroid Build Coastguard Worker visibility = [ ":*" ] 460*61c4878aSAndroid Build Coastguard Worker defines = [ "PW_THREAD_FREERTOS_CONFIG_JOINING_ENABLED=1" ] 461*61c4878aSAndroid Build Coastguard Worker} 462*61c4878aSAndroid Build Coastguard Worker 463*61c4878aSAndroid Build Coastguard Worker# Pigweed upstream does not use the default toolchain, but other projects may 464*61c4878aSAndroid Build Coastguard Worker# use it. To support using pw_build from the default toolchain without fully 465*61c4878aSAndroid Build Coastguard Worker# configuring the Pigweed build, only instantiate the pw_build tests for a 466*61c4878aSAndroid Build Coastguard Worker# non-default toolchain. 467*61c4878aSAndroid Build Coastguard Workerif (current_toolchain != default_toolchain) { 468*61c4878aSAndroid Build Coastguard Worker pw_test("cc_blob_library_test") { 469*61c4878aSAndroid Build Coastguard Worker sources = [ "cc_blob_library_test.cc" ] 470*61c4878aSAndroid Build Coastguard Worker deps = [ ":test_blob" ] 471*61c4878aSAndroid Build Coastguard Worker } 472*61c4878aSAndroid Build Coastguard Worker 473*61c4878aSAndroid Build Coastguard Worker pw_test("linker_symbol_test") { 474*61c4878aSAndroid Build Coastguard Worker sources = [ "linker_symbol_test.cc" ] 475*61c4878aSAndroid Build Coastguard Worker enable_if = current_os == "linux" 476*61c4878aSAndroid Build Coastguard Worker deps = [ ":linker_symbol" ] 477*61c4878aSAndroid Build Coastguard Worker inputs = [ "linker_symbol_test.ld" ] 478*61c4878aSAndroid Build Coastguard Worker ldflags = [ 479*61c4878aSAndroid Build Coastguard Worker "-T", 480*61c4878aSAndroid Build Coastguard Worker rebase_path("linker_symbol_test.ld", root_build_dir), 481*61c4878aSAndroid Build Coastguard Worker ] 482*61c4878aSAndroid Build Coastguard Worker } 483*61c4878aSAndroid Build Coastguard Worker 484*61c4878aSAndroid Build Coastguard Worker pw_test("module_config_test") { 485*61c4878aSAndroid Build Coastguard Worker public_configs = [ ":module_config_test_config" ] 486*61c4878aSAndroid Build Coastguard Worker sources = [ "module_config_test.cc" ] 487*61c4878aSAndroid Build Coastguard Worker deps = [ "$dir_pw_thread_freertos:config" ] 488*61c4878aSAndroid Build Coastguard Worker } 489*61c4878aSAndroid Build Coastguard Worker 490*61c4878aSAndroid Build Coastguard Worker pw_cc_blob_library("test_blob") { 491*61c4878aSAndroid Build Coastguard Worker out_header = "pw_build/test_blob.h" 492*61c4878aSAndroid Build Coastguard Worker namespace = "test::ns" 493*61c4878aSAndroid Build Coastguard Worker blobs = [ 494*61c4878aSAndroid Build Coastguard Worker { 495*61c4878aSAndroid Build Coastguard Worker file_path = "test_blob_0123.bin" 496*61c4878aSAndroid Build Coastguard Worker symbol_name = "kFirstBlob0123" 497*61c4878aSAndroid Build Coastguard Worker alignas = 512 498*61c4878aSAndroid Build Coastguard Worker }, 499*61c4878aSAndroid Build Coastguard Worker { 500*61c4878aSAndroid Build Coastguard Worker file_path = "test_blob_0123.bin" 501*61c4878aSAndroid Build Coastguard Worker symbol_name = "kSecondBlob0123" 502*61c4878aSAndroid Build Coastguard Worker }, 503*61c4878aSAndroid Build Coastguard Worker ] 504*61c4878aSAndroid Build Coastguard Worker visibility = [ ":*" ] 505*61c4878aSAndroid Build Coastguard Worker } 506*61c4878aSAndroid Build Coastguard Worker 507*61c4878aSAndroid Build Coastguard Worker # file_prefix_map_test verifies that the -ffile-prefix-map option is applied 508*61c4878aSAndroid Build Coastguard Worker # correctly. 509*61c4878aSAndroid Build Coastguard Worker 510*61c4878aSAndroid Build Coastguard Worker # File paths should be relative to the root of the GN build. 511*61c4878aSAndroid Build Coastguard Worker _test_header = rebase_path("pw_build_private/file_prefix_map_test.h", "//") 512*61c4878aSAndroid Build Coastguard Worker _path_test_define = [ "PW_BUILD_EXPECTED_HEADER_PATH=\"$_test_header\"" ] 513*61c4878aSAndroid Build Coastguard Worker 514*61c4878aSAndroid Build Coastguard Worker pw_test("file_prefix_map_test") { 515*61c4878aSAndroid Build Coastguard Worker _source_path = rebase_path("file_prefix_map_test.cc", "//") 516*61c4878aSAndroid Build Coastguard Worker 517*61c4878aSAndroid Build Coastguard Worker sources = [ "file_prefix_map_test.cc" ] 518*61c4878aSAndroid Build Coastguard Worker 519*61c4878aSAndroid Build Coastguard Worker defines = _path_test_define + 520*61c4878aSAndroid Build Coastguard Worker [ "PW_BUILD_EXPECTED_SOURCE_PATH=\"$_source_path\"" ] 521*61c4878aSAndroid Build Coastguard Worker 522*61c4878aSAndroid Build Coastguard Worker deps = [ ":file_prefix_map_generated_file" ] 523*61c4878aSAndroid Build Coastguard Worker } 524*61c4878aSAndroid Build Coastguard Worker 525*61c4878aSAndroid Build Coastguard Worker # Generated file paths should be relative to the build directory. 526*61c4878aSAndroid Build Coastguard Worker copy("generate_file_prefix_map_test_source") { 527*61c4878aSAndroid Build Coastguard Worker sources = [ "file_prefix_map_test.cc" ] 528*61c4878aSAndroid Build Coastguard Worker outputs = [ get_label_info(":file_prefix_map_test", "target_gen_dir") + 529*61c4878aSAndroid Build Coastguard Worker "/file_prefix_map_test_generated.cc" ] 530*61c4878aSAndroid Build Coastguard Worker visibility = [ ":*" ] 531*61c4878aSAndroid Build Coastguard Worker } 532*61c4878aSAndroid Build Coastguard Worker 533*61c4878aSAndroid Build Coastguard Worker pw_source_set("file_prefix_map_generated_file") { 534*61c4878aSAndroid Build Coastguard Worker public = [ "pw_build_private/file_prefix_map_test.h" ] 535*61c4878aSAndroid Build Coastguard Worker sources = get_target_outputs(":generate_file_prefix_map_test_source") 536*61c4878aSAndroid Build Coastguard Worker deps = [ ":generate_file_prefix_map_test_source" ] 537*61c4878aSAndroid Build Coastguard Worker 538*61c4878aSAndroid Build Coastguard Worker _source_path = rebase_path(sources[0], root_build_dir) 539*61c4878aSAndroid Build Coastguard Worker 540*61c4878aSAndroid Build Coastguard Worker # The source file is prefixed with out/ since it's generated. 541*61c4878aSAndroid Build Coastguard Worker defines = _path_test_define + 542*61c4878aSAndroid Build Coastguard Worker [ "PW_BUILD_EXPECTED_SOURCE_PATH=\"out/$_source_path\"" ] 543*61c4878aSAndroid Build Coastguard Worker include_dirs = [ "." ] # Allow accessing file_prefix_map_test.h 544*61c4878aSAndroid Build Coastguard Worker visibility = [ ":*" ] 545*61c4878aSAndroid Build Coastguard Worker } 546*61c4878aSAndroid Build Coastguard Worker 547*61c4878aSAndroid Build Coastguard Worker pw_source_set("empty_main") { 548*61c4878aSAndroid Build Coastguard Worker sources = [ "empty_main.cc" ] 549*61c4878aSAndroid Build Coastguard Worker visibility = [ ":*" ] 550*61c4878aSAndroid Build Coastguard Worker } 551*61c4878aSAndroid Build Coastguard Worker 552*61c4878aSAndroid Build Coastguard Worker pw_test_group("tests") { 553*61c4878aSAndroid Build Coastguard Worker tests = [ 554*61c4878aSAndroid Build Coastguard Worker ":cc_blob_library_test", 555*61c4878aSAndroid Build Coastguard Worker ":file_prefix_map_test", 556*61c4878aSAndroid Build Coastguard Worker ":linker_symbol_test", 557*61c4878aSAndroid Build Coastguard Worker ] 558*61c4878aSAndroid Build Coastguard Worker } 559*61c4878aSAndroid Build Coastguard Worker} 560