1# Copyright (c) 2022-2023 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14import("$build_root/config/compiler/compiler.gni") 15import("$build_root/config/sanitizers/sanitizers.gni") 16import("$build_root/toolchain/toolchain.gni") 17if (current_cpu == "arm" || current_cpu == "arm64") { 18 import("$build_root/config/arm.gni") 19} 20 21declare_args() { 22 treat_warnings_as_errors = true 23} 24 25use_rtti = use_cfi_diag || is_ubsan_vptr || is_ubsan_security 26 27config("rtti") { 28 if (is_win) { 29 cflags_cc = [ "/GR" ] 30 } else { 31 cflags_cc = [ "-frtti" ] 32 } 33} 34 35config("no_rtti") { 36 # Some sanitizer configs may require RTTI to be left enabled globally 37 if (!use_rtti) { 38 if (is_win) { 39 cflags_cc = [ "/GR-" ] 40 } else { 41 cflags_cc = [ "-fno-rtti" ] 42 cflags_objcc = cflags_cc 43 } 44 } 45} 46 47config("exceptions") { 48 cflags_cc = [ "-fexceptions" ] 49} 50 51common_optimize_on_cflags = [] 52common_optimize_on_ldflags = [] 53 54if (is_ohos) { 55 common_optimize_on_ldflags += [ 56 "-Wl,--warn-shared-textrel", # Warn in case of text relocations. 57 ] 58} 59 60if (!(is_mac || is_ios)) { 61 # Non-Mac Posix flags. 62 63 common_optimize_on_ldflags += [ 64 # Specifically tell the linker to perform optimizations. 65 # -O2 enables string tail merge optimization in gold and lld. 66 "-Wl,-O2", 67 ] 68 if (!is_mingw) { 69 common_optimize_on_ldflags += [ "-Wl,--gc-sections" ] 70 } 71 72 common_optimize_on_cflags += [ 73 # Put data and code in their own sections, so that unused symbols 74 # can be removed at link time with --gc-sections. 75 "-fdata-sections", 76 "-ffunction-sections", 77 78 # Don't emit the GCC version ident directives, they just end up in the 79 # .comment section taking up binary size. 80 "-fno-ident", 81 ] 82} 83 84# Turn off optimizations. 85config("no_optimize") { 86 if (is_ohos_or_android) { 87 ldflags = common_optimize_on_ldflags 88 cflags = common_optimize_on_cflags 89 90 # On ohos we kind of optimize some things that don't affect debugging 91 # much even when optimization is disabled to get the binary size down. 92 if (is_clang) { 93 cflags += [ "-Oz" ] 94 } else { 95 cflags += [ "-Os" ] 96 } 97 } 98} 99 100# Default "optimization on" config. 101config("optimize") { 102 ldflags = common_optimize_on_ldflags 103 cflags = common_optimize_on_cflags 104 if (optimize_for_size) { 105 # Favor size over speed. 106 if (is_clang) { 107 cflags += [ "-O2" ] 108 } else { 109 cflags += [ "-Os" ] 110 } 111 } else { 112 cflags += [ "-O2" ] 113 } 114} 115 116# The default optimization applied to all targets. This will be equivalent to 117# either "optimize" or "no_optimize", depending on the build flags. 118config("default_optimization") { 119 if (is_debug) { 120 configs = [ ":no_optimize" ] 121 } else { 122 configs = [ ":optimize" ] 123 } 124} 125 126# default_warnings ------------------------------------------------------------ 127# 128# Collects all warning flags that are used by default.This way these 129# flags are guaranteed to appear on the compile command line after -Wall. 130config("default_warnings") { 131 cflags = [] 132 cflags_cc = [] 133 ldflags = [] 134 135 # Suppress warnings about ABI changes on ARM (Clang doesn't give this 136 # warning). 137 if (current_cpu == "arm" && !is_clang) { 138 cflags += [ "-Wno-psabi" ] 139 } 140 141 if (!is_clang) { 142 cflags_cc += [ 143 # See comment for -Wno-c++11-narrowing. 144 "-Wno-narrowing", 145 ] 146 147 # -Wunused-local-typedefs is broken in gcc, 148 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63872 149 cflags += [ "-Wno-unused-local-typedefs" ] 150 151 # Don't warn about "maybe" uninitialized. Clang doesn't include this 152 # in -Wall but gcc does, and it gives false positives. 153 cflags += [ "-Wno-maybe-uninitialized" ] 154 cflags += [ "-Wno-deprecated-declarations" ] 155 156 # -Wcomment gives too many false positives in the case a 157 # backslash ended comment line is followed by a new line of 158 # comments 159 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61638 160 cflags += [ "-Wno-comments" ] 161 } 162 163 # Common Clang and GCC warning setup. 164 if (!is_win || is_clang) { 165 cflags += [ 166 # Disables. 167 "-Wno-missing-field-initializers", # "struct foo f = {0};" 168 "-Wno-unused-parameter", # Unused function parameters. 169 ] 170 } 171 172 if (is_mingw) { 173 cflags += [ 174 "-Wno-error=c99-designator", 175 "-Wno-error=anon-enum-enum-conversion", 176 "-Wno-error=implicit-fallthrough", 177 "-Wno-error=sizeof-array-div", 178 "-Wno-error=reorder-init-list", 179 "-Wno-error=range-loop-construct", 180 "-Wno-error=deprecated-copy", 181 "-Wno-error=implicit-int-float-conversion", 182 "-Wno-error=inconsistent-dllimport", 183 "-Wno-error=abstract-final-class", 184 "-Wno-error=sign-compare", 185 "-Wno-unknown-warning-option", 186 ] 187 } 188 189 if (target_cpu == "mipsel") { 190 cflags += [ 191 "-Wno-extra-semi", 192 "-Wno-error=tautological-constant-out-of-range-compare", 193 "-Wno-shift-count-overflow", 194 "-Wno-constant-conversion", 195 ] 196 } 197 198 if (is_clang) { 199 cflags += [ 200 # This warns on using ints as initializers for floats in 201 # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|), 202 "-Wno-c++11-narrowing", 203 "-Wno-unneeded-internal-declaration", 204 ] 205 if (current_os == "android") { 206 # Disable calls to out-of-line helpers, don't implement atomic operations for these calls. 207 # (Keep consistent with the default of clang15 in directory prebuilts.) 208 cflags += [ "-mno-outline-atomics" ] 209 } 210 if (use_musl) { 211 cflags += [ 212 "-Wno-error=c99-designator", 213 "-Wno-error=anon-enum-enum-conversion", 214 "-Wno-error=implicit-fallthrough", 215 "-Wno-error=sizeof-array-div", 216 "-Wno-error=reorder-init-list", 217 "-Wno-error=range-loop-construct", 218 "-Wno-error=deprecated-copy", 219 "-Wno-error=implicit-int-float-conversion", 220 "-Wno-error=inconsistent-dllimport", 221 "-Wno-error=unknown-warning-option", 222 "-Wno-error=abstract-final-class", 223 "-Wno-error=sign-compare", 224 "-Wno-error=int-in-bool-context", 225 "-Wno-error=xor-used-as-pow", 226 "-Wno-error=return-stack-address", 227 "-Wno-error=dangling-gsl", 228 "-Wno-unused-but-set-variable", 229 "-Wno-deprecated-declarations", 230 "-Wno-unused-but-set-parameter", 231 "-Wno-null-pointer-subtraction", 232 "-Wno-unqualified-std-cast-call", 233 ] 234 } 235 } 236} 237 238config("compiler") { 239 asmflags = [] 240 cflags = [] 241 cflags_c = [] 242 cflags_cc = [] 243 cflags_objc = [] 244 cflags_objcc = [] 245 ldflags = [] 246 defines = [] 247 configs = [] 248 inputs = [] 249 250 # System-specific flags. If your compiler flags apply to one of the 251 # categories here, add it to the associated file to keep this shared config 252 # smaller. 253 if (is_android) { 254 configs += [ "$build_root/config/aosp:compiler" ] 255 } 256 if (is_mingw) { 257 configs += [ "$build_root/config/mingw:compiler" ] 258 cflags += [ "-fno-stack-protector" ] 259 } else if (is_ohos) { 260 configs += [ "$build_root/config/ohos:compiler" ] 261 } else if (is_mac) { 262 configs += [ "$build_root/config/mac:compiler" ] 263 } 264 if (is_linux || is_ohos || is_android) { 265 cflags += [ "-fPIC" ] 266 ldflags += [ "-fPIC" ] 267 if (!is_clang) { 268 # Use pipes for communicating between sub-processes. Faster. 269 # (This flag doesn't do anything with Clang.) 270 cflags += [ "-pipe" ] 271 } 272 273 ldflags += [ 274 "-Wl,-z,noexecstack", 275 "-Wl,-z,now", 276 "-Wl,-z,relro", 277 ] 278 279 # Compiler instrumentation can introduce dependencies in DSOs to symbols in 280 # the executable they are loaded into, so they are unresolved at link-time. 281 if (is_ohos || (!using_sanitizer && !is_safestack)) { 282 ldflags += [ 283 "-Wl,-z,defs", 284 "-Wl,--as-needed", 285 ] 286 } 287 288 # # Change default thread stack size to 2MB for asan. 289 if (is_ohos && using_sanitizer) { 290 ldflags += [ "-Wl,-z,stack-size=2097152" ] 291 } 292 } 293 294 if (is_posix && use_lld) { 295 # Explicitly select LLD, or else some link actions would fail and print 296 # "/usr/bin/ld ... {SharedLibrayName}.so ... not found". 297 ldflags += [ "-fuse-ld=lld" ] 298 if (current_cpu == "arm64") { 299 # Reduce the page size from 65536 in order to reduce binary size slightly 300 # by shrinking the alignment gap between segments. This also causes all 301 # segments to be mapped adjacently, which breakpad relies on. 302 ldflags += [ "-Wl,-z,max-page-size=4096" ] 303 } 304 } 305 306 configs += [ 307 # See the definitions below. 308 ":compiler_cpu_abi", 309 ":compiler_codegen", 310 ] 311 312 if (is_ohos && is_clang && (target_cpu == "arm" || target_cpu == "arm64")) { 313 ldflags += [ "-Wl,--pack-dyn-relocs=android+relr" ] 314 } 315 316 if (is_linux) { 317 cflags += [ "-pthread" ] 318 # Do not use the -pthread ldflag here since it becomes a no-op 319 # when using -nodefaultlibs, which would cause an unused argument 320 # error. "-lpthread" is added in $build_root/config:default_libs. 321 } 322 if (is_clang) { 323 cflags += [ "-fcolor-diagnostics" ] 324 cflags += [ "-fmerge-all-constants" ] 325 } 326 327 use_xcode_clang = false 328 if (is_clang && !use_xcode_clang) { 329 cflags += [ 330 "-Xclang", 331 "-mllvm", 332 "-Xclang", 333 "-instcombine-lower-dbg-declare=0", 334 ] 335 } 336 337 if (is_clang) { 338 cflags += [ "-no-canonical-prefixes" ] 339 } 340 341 # specify language standard version. 342 if (is_linux || is_ohos || is_android || current_os == "aix") { 343 if (is_clang) { 344 cflags_cc += [ "-std=c++17" ] 345 } else { 346 # In standards-conforming mode, some features like macro "##__VA_ARGS__" 347 # are not supported by gcc. Add flags to support these features. 348 cflags_cc += [ "-std=gnu++17" ] 349 } 350 } else if (!is_win && !is_mingw) { 351 cflags_cc += [ "-std=c++17" ] 352 } 353} 354 355# This provides the basic options to select the target CPU and ABI. 356# It is factored out of "compiler" so that special cases can use this 357# without using everything that "compiler" brings in. Options that 358# tweak code generation for a particular CPU do not belong here! 359# See "compiler_codegen", below. 360config("compiler_cpu_abi") { 361 cflags = [] 362 ldflags = [] 363 defines = [] 364 365 if (is_posix && !is_mac) { 366 # CPU architecture. We may or may not be doing a cross compile now, so for 367 # simplicity we always explicitly set the architecture. 368 if (current_cpu == "x64") { 369 cflags += [ 370 "-m64", 371 "-march=x86-64", 372 ] 373 ldflags += [ "-m64" ] 374 } else if (current_cpu == "x86") { 375 cflags += [ 376 "-m32", 377 "-msse2", 378 "-mfpmath=sse", 379 "-mmmx", 380 ] 381 ldflags += [ "-m32" ] 382 } else if (current_cpu == "arm") { 383 if (is_clang && !is_ohos) { 384 cflags += [ "--target=arm-linux-gnueabihf" ] 385 ldflags += [ "--target=arm-linux-gnueabihf" ] 386 } 387 cflags += [ 388 "-march=$arm_arch", 389 "-mfloat-abi=$arm_float_abi", 390 ] 391 if (arm_tune != "") { 392 cflags += [ "-mtune=$arm_tune" ] 393 } 394 } else if (current_cpu == "arm64") { 395 if (is_clang && !is_ohos && !is_android) { 396 cflags += [ "--target=aarch64-linux-gnu" ] 397 ldflags += [ "--target=aarch64-linux-gnu" ] 398 } 399 if (is_clang && (is_ohos || is_android)) { 400 ldflags += [ "-Wl,--hash-style=gnu" ] 401 } 402 if (!is_android) { 403 cflags += [ 404 "-march=$arm_arch", 405 "-mfloat-abi=$arm_float_abi", 406 "-mfpu=$arm_fpu", 407 ] 408 } else { 409 cflags += [ "-march=$arm_arch" ] 410 } 411 ldflags += [ "-march=$arm_arch" ] 412 } 413 } 414 415 asmflags = cflags 416 if (current_cpu == "arm64") { 417 asmflags += [ "-march=armv8.2-a+dotprod+fp16" ] 418 } 419} 420 421config("compiler_codegen") { 422 configs = [] 423 cflags = [] 424 425 if (is_posix && !is_mac) { 426 if (current_cpu == "x86") { 427 if (is_clang) { 428 cflags += [ "-momit-leaf-frame-pointer" ] 429 } 430 } else if (current_cpu == "arm") { 431 if (is_ohos && !is_clang) { 432 # Clang doesn't support these flags. 433 cflags += [ 434 "-fno-tree-sra", 435 "-fno-caller-saves", 436 ] 437 } 438 } 439 } 440 441 asmflags = cflags 442} 443 444config("default_include_dirs") { 445 include_dirs = [ 446 "//", 447 root_gen_dir, 448 ] 449} 450 451config("runtime_config") { 452 configs = [] 453 if (is_posix) { 454 configs += [ "$build_root/config/posix:runtime_config" ] 455 } 456 457 # System-specific flags. If your compiler flags apply to one of the 458 # categories here, add it to the associated file to keep this shared config 459 # smaller. 460 if (is_ohos) { 461 configs += [ "$build_root/config/ohos:runtime_config" ] 462 } 463 464 if (is_android) { 465 configs += [ "$build_root/config/aosp:runtime_config" ] 466 } 467 468 if (is_mac) { 469 configs += [ "$build_root/config/mac:runtime_config" ] 470 } 471} 472 473config("ark_code") { 474 cflags = [ "-Wall" ] 475 if (treat_warnings_as_errors) { 476 cflags += [ "-Werror" ] 477 ldflags = [ "-Werror" ] 478 } 479 if (is_clang) { 480 # Enable extra warnings for ark_code when we control the compiler. 481 cflags += [ "-Wextra" ] 482 } 483 484 if (is_clang) { 485 cflags += [ 486 "-Wimplicit-fallthrough", 487 "-Wthread-safety", 488 ] 489 } 490 491 configs = [ ":default_warnings" ] 492} 493