1# Copyright 2018 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import("//build/config/arm.gni") 6import("//build/config/sysroot.gni") 7import("//build_overrides/build.gni") 8 9declare_args() { 10 # Enable OSP_DCHECKs to be compiled in, even if it's not a debug build. 11 dcheck_always_on = false 12 13 # Enable address sanitizer. 14 is_asan = false 15 16 # Enable thread sanitizer. 17 is_tsan = false 18 19 # Enables clang's source-based coverage (requires is_clang=true). 20 # NOTE: This will slow down the build and increase binary size 21 # significantly. For more details, see: 22 # https://chromium.googlesource.com/chromium/src/+/refs/heads/master/docs/clang_code_coverage_wrapper.md 23 use_coverage = false 24} 25 26assert(!use_coverage || is_clang) 27 28config("compiler_defaults") { 29 cflags = [] 30 if (is_posix && !is_mac) { 31 cflags += [ "-fPIC" ] 32 } 33 34 ldflags = [] 35 if (is_linux) { 36 ldflags += [ "-pthread" ] 37 } 38 if (is_posix && !is_mac) { 39 ldflags += [ "-Wl,--fatal-warnings" ] 40 41 if (is_clang) { 42 ldflags += [ 43 "-stdlib=libstdc++", 44 "-latomic", 45 ] 46 } 47 } 48} 49 50config("compiler_cpu_abi") { 51 cflags = [] 52 ldflags = [] 53 54 if (current_cpu == "x64") { 55 # These are explicitly specified in case of cross-compiling. 56 cflags += [ "-m64" ] 57 ldflags += [ "-m64" ] 58 } else if (current_cpu == "x86") { 59 cflags += [ "-m32" ] 60 ldflags += [ "-m32" ] 61 } else if (current_cpu == "arm") { 62 cflags += [ 63 "--target=arm-linux-gnueabihf", 64 "-march=$arm_arch", 65 "-mfloat-abi=$arm_float_abi", 66 "-mtune=$arm_tune", 67 ] 68 ldflags += [ "--target=arm-linux-gnueabihf" ] 69 } else if (current_cpu == "arm64") { 70 cflags += [ "--target=aarch64-linux-gnu" ] 71 ldflags += [ "--target=aarch64-linux-gnu" ] 72 } 73} 74 75config("no_exceptions") { 76 # -fno-exceptions causes the compiler to choose the implementation of the STL 77 # that uses abort() calls instead of throws, as well as issue compile errors 78 # for throw calls in user ccode. The no*unwind-tables flags disable generation 79 # of static unwind tables that are typically used for exceptions, resulting 80 # in typically smaller object sizes. For some information, see: 81 # https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-funwind-tables-1203 82 cflags_cc = [ 83 "-fno-exceptions", 84 "-fno-unwind-tables", 85 "-fno-asynchronous-unwind-tables", 86 ] 87 88 cflags_objcc = [ 89 "-fno-exceptions", 90 "-fno-unwind-tables", 91 "-fno-asynchronous-unwind-tables", 92 ] 93} 94 95config("no_rtti") { 96 cflags_cc = [ "-fno-rtti" ] 97 cflags_objcc = [ "-fno-rtti" ] 98} 99 100config("openscreen_code") { 101 cflags = [ 102 "-Wall", 103 "-Werror", 104 "-fno-strict-aliasing", # See http://crbug.com/32204 105 ] 106 107 cflags_cc = [ "-std=c++14" ] 108 cflags_objcc = [ "-std=c++14" ] 109 110 ldflags = [ "-Werror" ] 111 112 if (is_gcc) { 113 cflags += [ "-Wno-maybe-uninitialized" ] 114 cflags_cc += [ 115 # GCC 7.2 reports noexcept errors that are not found in GCC 8. 116 # TODO(crbug.com/openscreen/40): Remove this when bots are upgraded to 117 # >= GCC 8.3. 118 "-Wno-noexcept-type", 119 ] 120 } 121 122 if (is_clang) { 123 cflags += [ 124 "-Wextra", 125 "-fcolor-diagnostics", 126 127 # Warn on missing break statements at the end of switch cases. 128 "-Wimplicit-fallthrough", 129 130 # Unused function parameters are okay (e.g., empty virtual method 131 # overrides). 132 "-Wno-unused-parameter", 133 134 # Permit code like "struct foo f = {0};". 135 "-Wno-missing-field-initializers", 136 137 # Warn on signed versus unsigned comparisons. 138 "-Wsign-compare", 139 140 # Warn on extra semi-colons. 141 "-Wextra-semi", 142 143 # Warn on unused results. 144 "-Wunused-result", 145 ] 146 147 # Generate warning if code generates exit-time destructors, since such 148 # things are in violation of Chromium C++ Don'ts. 149 cflags_cc += [ "-Wexit-time-destructors" ] 150 cflags_objcc += [ "-Wexit-time-destructors" ] 151 152 ldflags += [ "-fcolor-diagnostics" ] 153 } 154 155 defines = [] 156 if (dcheck_always_on) { 157 defines += [ "DCHECK_ALWAYS_ON" ] 158 } else if (!is_debug) { 159 # C-style assertions in the standard library should match the behavior of 160 # OSP_DCHECKs. Thus, turn them off whenever OSP_DCHECKs would be turned off. 161 defines += [ "NDEBUG" ] 162 } 163} 164 165config("default_optimization") { 166 cflags = [] 167 defines = [] 168 if (is_debug) { 169 cflags += [ 170 "-O0", 171 "-g3", # Include extra debugging information. 172 "-fstack-protector-strong", 173 ] 174 defines += [ "_DEBUG" ] 175 } else { 176 cflags += [ 177 "-O2", 178 "-g1", # Minimal symbols (enough for stack traces). 179 "-fstack-protector", 180 ] 181 } 182} 183 184config("symbol_visibility_hidden") { 185 cflags = [ "-fvisibility=hidden" ] 186} 187 188config("symbol_visibility_default") { 189 cflags = [ "-fvisibility=default" ] 190} 191 192config("default_sanitizers") { 193 # NOTE: This is not an artificial restriction; clang doesn't allow these to be 194 # used together. 195 assert(!is_asan || !is_tsan) 196 197 cflags = [] 198 ldflags = [] 199 if (is_asan) { 200 cflags += [ "-fsanitize=address" ] 201 ldflags += [ "-fsanitize=address" ] 202 } else if (is_tsan) { 203 cflags += [ "-fsanitize=thread" ] 204 ldflags += [ "-fsanitize=thread" ] 205 } 206 207 if (use_libfuzzer) { 208 cflags += [ "-fsanitize=fuzzer-no-link" ] 209 if (!is_asan) { 210 ldflags += [ "-fsanitize=address" ] 211 } 212 } 213} 214 215config("default_coverage") { 216 cflags = [] 217 ldflags = [] 218 219 # TODO(issuetracker.google.com/155348032): Limit the use of these flags 220 if (use_coverage) { 221 cflags += [ 222 "-fprofile-instr-generate", 223 "-fcoverage-mapping", 224 ] 225 ldflags += [ "-fprofile-instr-generate" ] 226 } 227} 228 229config("sysroot_runtime_libraries") { 230 if (sysroot != "") { 231 assert(is_clang) 232 sysroot_path = rebase_path(sysroot, root_build_dir) 233 flags = [ "--sysroot=" + sysroot_path ] 234 hash = exec_script("//build/scripts/install-sysroot.py", 235 [ 236 "--print-hash", 237 "$current_cpu", 238 "$sysroot_platform", 239 ], 240 "trim string", 241 [ "//build/scripts/sysroots.json" ]) 242 243 # GN uses this to know that the sysroot is "dirty" 244 defines = [ "SYSROOT_HASH=$hash" ] 245 ldflags = flags 246 cflags = flags 247 248 ld_paths = exec_script("//build/scripts/sysroot_ld_path.py", 249 [ sysroot_path ], 250 "list lines") 251 foreach(ld_path, ld_paths) { 252 ld_path = rebase_path(ld_path, root_build_dir) 253 ldflags += [ "-L" + ld_path ] 254 } 255 } 256} 257 258config("operating_system_defines") { 259 defines = [] 260 if (is_linux) { 261 defines += [ "OS_LINUX" ] 262 } else if (is_mac) { 263 defines += [ "MAC_OSX" ] 264 } 265} 266