1# Copyright (C) 2017 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://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, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import("//gn/perfetto_check_build_deps.gni") 16import("//gn/standalone/android.gni") 17import("//gn/standalone/libc++/libc++.gni") 18import("//gn/standalone/sanitizers/sanitizers.gni") 19import("//gn/standalone/toolchain/msvc.gni") 20import("//gn/standalone/wasm.gni") 21 22# These warnings have been introduced with the newest version of clang (only in 23# the hermetic build) and are enabled just with -Werror. 24hermetic_clang_suppressions = [ "-Wno-c99-designator" ] 25 26# We deal with three toolchains here: 27# 1. Clang, used in most cases. 28# 2. GCC, used in some Linux cases. 29# 3. MSVC, used in some Windows cases. 30# Clang vs gcc is typically not a problem: both support roughly the same 31# switches. -Wno-unknown-warning-option fixes the mismatching ones. 32# The situation on Windows is a bit trickier: clang-cl.exe really pretends to be 33# cl.exe (MSVC), so we should use MSVC-style switches (e.g. /W2). However, 34# clang-cl.exe still supports some -Wclang-style-switches for flags that don't 35# have a corresponding version in MSVC. 36# 37# In the rules below, the conditionals should be interpreted as follows: 38# is_win -> can be either clang-cl.exe or cl.exe (MSVC). Only MSVC-style 39# switches (the common denominator) should be used. 40# is_clang -> could be clang-on-linux, clang-on-mac or clang-cl.exe. 41 42config("extra_warnings") { 43 if (is_win) { 44 cflags = [ 45 "/W2", 46 "/wd4244", # conversion from 'float' to 'int', possible loss of data 47 "/wd4267", # conversion from 'size_t' to 'int', possible loss of data 48 ] 49 if (is_clang) { 50 cflags += [ 51 "-Wno-float-equal", 52 "-Wno-unused-macros", 53 "-Wno-old-style-cast", 54 ] 55 } 56 } else { 57 # Clang or Gcc. On linux, Android and Mac. 58 cflags = [ 59 "-Wall", 60 "-Wextra", 61 "-Wpedantic", 62 ] 63 } 64 65 # Disable variadic macro warning as we make extensive use of them in trace 66 # processor and client API. 67 if (is_clang) { 68 # Only enable -Weverything on hermetic clang as system clang might be quite 69 # out of date. 70 if (is_hermetic_clang && current_toolchain == host_toolchain && 71 !is_fuzzer) { 72 # Disable Weverything on fuzzers to avoid breakages when new versions of 73 # clang are rolled into OSS-fuzz. 74 cflags += [ "-Weverything" ] 75 } 76 cflags += [ 77 "-Wno-c++98-compat-pedantic", 78 "-Wno-c++98-compat", 79 "-Wno-disabled-macro-expansion", 80 "-Wno-documentation-unknown-command", 81 "-Wno-gnu-include-next", 82 "-Wno-gnu-statement-expression", 83 "-Wno-gnu-zero-variadic-macro-arguments", 84 "-Wno-padded", 85 "-Wno-poison-system-directories", 86 "-Wno-pre-c11-compat", 87 "-Wno-reserved-id-macro", 88 "-Wno-reserved-identifier", 89 "-Wno-shadow-uncaptured-local", 90 "-Wno-unknown-sanitizers", 91 "-Wno-unknown-warning-option", 92 "-Wno-unsafe-buffer-usage", 93 94 # TODO(primiano): -Wswitch-default could be useful but will require a mass 95 # codebase cleanup. 96 "-Wno-switch-default", 97 ] 98 99 if (perfetto_thread_safety_annotations) { 100 cflags += [ 101 "-Wthread-safety", 102 "-Wno-thread-safety-negative", 103 ] 104 } 105 } else if (is_gcc) { 106 # Use return std::move(...) for compatibility with old GCC compilers. 107 cflags_cc = [ "-Wno-redundant-move" ] 108 109 # Use after free detection in GCC is still not good enough: it still fails 110 # on very obvious false-positives in trace processor. 111 cflags_cc += [ "-Wno-use-after-free" ] 112 113 # GCC 7's handling of uninitialized std::optional is flaky at best and 114 # causes many false positives. 115 # TODO(lalitm): remove this when we upgrade to a GCC version which is good 116 # enough to handle this. 117 cflags_cc += [ "-Wno-maybe-uninitialized" ] 118 119 # GCC's handling of detecting infinite recursion is flaky at best and 120 # causes some false positives. 121 # TODO(lalitm): remove this when we upgrade to a GCC version which is good 122 # enough to handle this. 123 cflags_cc += [ "-Wno-infinite-recursion" ] 124 125 # GCC's handling of detecting non null arguments is flaky at best and 126 # causes some false positives. 127 # TODO(lalitm): remove this when we upgrade to a GCC version which is good 128 # enough to handle this. 129 cflags_cc += [ "-Wno-nonnull" ] 130 } 131} 132 133config("no_exceptions") { 134 # Exceptions are disabled by default on Windows (Use /EHsc to enable them). 135 if (!is_win) { 136 cflags_cc = [ "-fno-exceptions" ] 137 } 138} 139 140config("no_rtti") { 141 if (is_win) { 142 cflags_cc = [ "/GR-" ] 143 } else { 144 cflags_cc = [ "-fno-rtti" ] 145 } 146} 147 148# Used in buildtools dependencies for standalone builds. 149config("c++17") { 150 if (is_win) { 151 cflags_cc = [ "/std:c++17" ] 152 } else { 153 cflags_cc = [ "-std=c++17" ] 154 } 155} 156 157# Used in buildtools dependencies for standalone builds. 158config("c++20") { 159 visibility = [ "//buildtools:libc++config" ] 160 if (is_win) { 161 cflags_cc = [ "/std:c++20" ] 162 } else { 163 cflags_cc = [ "-std=c++20" ] 164 } 165} 166 167config("visibility_hidden") { 168 if (!is_win) { 169 cflags = [ "-fvisibility=hidden" ] 170 } 171} 172 173config("win32_lean_and_mean") { 174 if (is_win) { 175 defines = [ "WIN32_LEAN_AND_MEAN" ] 176 } 177} 178 179config("default") { 180 asmflags = [] 181 cflags = [] 182 cflags_c = [] 183 cflags_cc = [] 184 defines = [] 185 include_dirs = [] 186 ldflags = [] 187 libs = [] 188 189 if ((is_android || is_linux) && !is_wasm) { 190 ldflags += [ 191 "-Wl,--build-id", 192 "-Wl,-z,max-page-size=16384", 193 ] 194 } 195 196 if (is_clang || !is_win) { # Clang or GCC, but not MSVC. 197 cflags += [ 198 "-fstrict-aliasing", 199 "-Wformat", 200 ] 201 } 202 203 if (is_clang && is_win) { 204 # clang-cl from version 16 does not like out-of-line definition of static 205 # constexpr, even in C++14 mode. Disable the deprecated warnings to work 206 # around the problem. 207 cflags += [ "-Wno-deprecated" ] 208 } 209 210 if (is_win) { 211 cflags += [ 212 "/bigobj", # Some of our files are bigger than the regular limits. 213 "/Gy", # Enable function-level linking. 214 "/FS", # Preserve previous PDB behavior. 215 "/utf-8", # Assume UTF-8 by default to avoid code page dependencies. 216 "/Zc:__cplusplus", # Allow use of __cplusplus macro. 217 ] 218 defines += [ 219 "_CRT_NONSTDC_NO_WARNINGS", 220 "_CRT_SECURE_NO_DEPRECATE", 221 "_CRT_SECURE_NO_WARNINGS", # Disables warnings on some POSIX-compat API. 222 "_SCL_SECURE_NO_DEPRECATE", 223 "NOMINMAX", 224 ] 225 if (!use_custom_libcxx) { 226 defines += [ "_HAS_EXCEPTIONS=0" ] # Disables exceptions in MSVC STL. 227 } 228 } else if (!is_wasm) { # !is_win 229 cflags += [ 230 "-g", 231 "-fPIC", 232 "-fstack-protector-strong", 233 ] 234 } 235 236 # Treat warnings as errors, but give up on fuzzer builds. 237 if (!is_fuzzer) { 238 if (is_win) { 239 cflags += [ "/WX" ] 240 } else { 241 cflags += [ "-Werror" ] 242 } 243 } 244 245 if (is_clang) { 246 cflags += [ "-fcolor-diagnostics" ] 247 if (!is_win) { 248 cflags += [ "-fdiagnostics-show-template-tree" ] 249 } 250 } 251 252 if (is_hermetic_clang && is_linux && !is_wasm) { 253 cflags += hermetic_clang_suppressions 254 } else { 255 not_needed([ "hermetic_clang_suppressions" ]) 256 } 257 258 if (non_hermetic_clang_stdlib != "") { 259 if (is_clang && !is_hermetic_clang && !is_wasm) { 260 cflags_cc += [ "-stdlib=" + non_hermetic_clang_stdlib ] 261 ldflags += [ "-stdlib=" + non_hermetic_clang_stdlib ] 262 } 263 } 264 265 if (is_lto) { 266 cflags += [ "-flto=full" ] 267 ldflags += [ "-flto=full" ] 268 } 269 270 if (is_win) { 271 # We support only x86/x64 builds on Windows. 272 assert(current_cpu == "x64" || current_cpu == "x86") 273 } else if (current_cpu == "arm") { 274 cflags += [ 275 "-march=armv7-a", 276 "-mfpu=neon", 277 "-mthumb", 278 ] 279 } else if (current_cpu == "riscv64") { 280 if (!is_clang) { 281 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104338 282 libs += [ "atomic" ] 283 } 284 } else if (current_cpu == "x86") { 285 asmflags += [ "-m32" ] 286 cflags += [ 287 "-m32", 288 "-msse2", 289 "-mfpmath=sse", 290 ] 291 ldflags += [ "-m32" ] 292 } else if (current_cpu == "arm64") { 293 cflags += [ "-fno-omit-frame-pointer" ] 294 } else if (current_cpu == "x64") { 295 cflags += [ "-fno-omit-frame-pointer" ] # For perf profiling. 296 if (enable_perfetto_x64_cpu_opt) { 297 # When updating these flags, the CheckCpuOptimizations() in utils.cc must 298 # be updated accordingly. 299 cflags += [ 300 "-mbmi", 301 "-mbmi2", 302 "-mavx2", 303 "-mpopcnt", 304 "-msse4.2", 305 ] 306 } 307 } 308 309 if (is_wasm) { 310 # As of writing (2023-06-12) WASM 128bit SIMD is supported on 311 # stable Chrome, Safari, and Firefox. See: 312 # - https://webassembly.org/roadmap/ 313 # - https://emscripten.org/docs/porting/simd.html 314 cflags += [ "-msimd128" ] 315 } 316 317 if (is_linux) { 318 # Enable LFS (large file support) for stat() and other syscalls. 319 cflags += [ 320 "-D_FILE_OFFSET_BITS=64", 321 "-D_LARGEFILE_SOURCE", 322 "-D_LARGEFILE64_SOURCE", 323 ] 324 libs += [ 325 "pthread", 326 "rt", 327 ] 328 } 329 330 if (is_win && !is_clang) { 331 # When using MSVC we need to manually pass the include dirs. clang-cl.exe 332 # doesn't need them because it's smart enough to figure out the right path 333 # by querying the registry on its own. 334 include_dirs = win_msvc_inc_dirs # Defined in msvc.gni. 335 } 336 337 if (is_win) { 338 cflags += [ "/Zi" ] 339 } 340 if (is_debug) { 341 if (is_win) { 342 if (is_clang) { 343 # Required to see symbols in windbg when building with clang-cl.exe. 344 cflags += [ "-gcodeview-ghash" ] 345 ldflags = [ "/DEBUG:GHASH" ] 346 } 347 } else { 348 libs += [ "dl" ] 349 } 350 } 351 352 if (is_android) { 353 asmflags += [ "--target=$android_abi_target" ] 354 cflags += [ 355 "--sysroot=$android_compile_sysroot", 356 "-DANDROID", 357 "-D__ANDROID_API__=${android_api_level}", 358 "--target=$android_abi_target", 359 ] 360 cflags_cc += [ "-isystem$android_compile_sysroot/c++/v1" ] 361 362 android_lib_dir = "$android_compile_sysroot/usr/lib/$android_abi_target/$android_api_level" 363 ldflags += [ 364 "-Wl,-z,nocopyreloc", 365 "--sysroot=$android_compile_sysroot", 366 "-B${android_lib_dir}", 367 "--target=$android_abi_target", 368 "-Wl,--no-undefined", 369 "-Wl,-z,noexecstack", 370 "-Wl,-z,relro", 371 "-Wl,-z,now", 372 "-Wl,--warn-shared-textrel", 373 "-Wl,--fatal-warnings", 374 375 # From NDK docs: "although the option uses the name "libstdc++" for 376 # historical reasons, this is correct for libc++ as well. 377 "-static-libstdc++", 378 ] 379 lib_dirs = [ android_lib_dir ] 380 } 381} 382 383config("debug_noopt") { 384 cflags = [] 385 if (is_win) { 386 cflags = [ "/Od" ] 387 } else { 388 cflags = [ "-O0" ] 389 } 390 if (is_android || is_linux) { 391 cflags += [ "-funwind-tables" ] 392 } 393} 394 395config("release") { 396 # Compiler flags for release builds. 397 if (is_win) { 398 cflags = [ 399 "/O2", 400 "/Zc:inline", 401 ] 402 } else if (is_android) { 403 cflags = [ "-O2" ] 404 } else if (is_fuzzer) { 405 cflags = [ "-O1" ] 406 } else { 407 cflags = [ "-O3" ] 408 } 409 if (!is_win) { 410 cflags += [ 411 "-fdata-sections", 412 "-ffunction-sections", 413 ] 414 } 415 416 # Linker flags for release builds. 417 if (is_win) { 418 ldflags = [ 419 "/OPT:REF", 420 "/OPT:ICF", 421 "/INCREMENTAL:NO", 422 "/FIXED:NO", 423 ] 424 } else if (is_mac) { 425 ldflags = [ "-dead_strip" ] 426 } else if (!is_win && !is_wasm) { 427 ldflags = [ 428 "-Wl,--gc-sections", 429 "-Wl,--icf=all", 430 "-Wl,-O1", 431 ] 432 } 433 defines = [ "NDEBUG" ] 434} 435 436config("shared_library") { 437 if (is_android || is_linux) { 438 ldflags = [ "-fPIC" ] 439 } 440} 441 442config("executable") { 443 ldflags = [] 444 445 # Android will refuse to run executables if they aren't position independent. 446 # Instead on Linux there isn't any need and they break ASan (goo.gl/paFR6K). 447 # The OSS-Fuzz provided AFL library is not PIC, so we we cannot use -fPIE 448 # for the fuzzer executables. 449 if ((is_android || is_linux) && !is_wasm && !is_fuzzer) { 450 asmflags = [ "-fPIE" ] 451 cflags = [ "-fPIE" ] 452 ldflags += [ "-pie" ] 453 } 454 455 # -rpath stores the path to the linked shared libraries into the binary, so 456 # that they can be launched without passing any LD_LIBRARY_PATH. It's 457 # supported only by Linux, not Android. But concretely we need this only when 458 # use_custom_libcxx=true && custom_libcxx_is_static=false, which happens only 459 # on Linux right now. 460 if (is_linux && !is_wasm) { 461 ldflags += [ 462 "-Wl,-rpath=\$ORIGIN/.", 463 "-Wl,-rpath-link=.", 464 ] 465 } 466} 467 468# This config is only added to certain leaf target types (see BUILDCONFIG.gn). 469# This allows us to remove the config (and thus the dependency) on a per-target 470# basis. If the config was applied to source_sets, then they would unavoidably 471# carry the dependency onto liblog to the final target. 472config("android_liblog") { 473 if (is_android) { 474 libs = [ "log" ] 475 } 476} 477 478# Checks that tools/install-build-deps has been run since it last changed. 479perfetto_check_build_deps("check_build_deps") { 480 args = [] 481} 482 483perfetto_check_build_deps("check_build_deps_android") { 484 args = [ "--android" ] 485} 486