# Copyright (c) 2022-2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import("$build_root/config/compiler/compiler.gni") import("$build_root/config/sanitizers/sanitizers.gni") import("$build_root/toolchain/toolchain.gni") if (current_cpu == "arm" || current_cpu == "arm64") { import("$build_root/config/arm.gni") } declare_args() { treat_warnings_as_errors = true } config("rtti") { cflags_cc = [ "-frtti" ] } config("no_rtti") { # Some sanitizer configs may require RTTI to be left enabled globally cflags_cc = [ "-fno-rtti" ] cflags_objcc = cflags_cc } config("exceptions") { cflags_cc = [ "-fexceptions" ] } common_optimize_on_cflags = [] common_optimize_on_ldflags = [] if (is_ohos) { common_optimize_on_ldflags += [ "-Wl,--warn-shared-textrel", # Warn in case of text relocations. ] } if (!(is_mac || is_ios)) { # Non-Mac Posix flags. common_optimize_on_ldflags += [ # Specifically tell the linker to perform optimizations. # -O2 enables string tail merge optimization in gold and lld. "-Wl,-O2", ] if (!is_mingw) { common_optimize_on_ldflags += [ "-Wl,--gc-sections" ] } common_optimize_on_cflags += [ # Put data and code in their own sections, so that unused symbols # can be removed at link time with --gc-sections. "-fdata-sections", "-ffunction-sections", # Don't emit the GCC version ident directives, they just end up in the # .comment section taking up binary size. "-fno-ident", ] } # Turn off optimizations. config("no_optimize") { if (is_ohos_or_android) { ldflags = common_optimize_on_ldflags cflags = common_optimize_on_cflags # On ohos we kind of optimize some things that don't affect debugging # much even when optimization is disabled to get the binary size down. if (is_clang) { cflags += [ "-Oz" ] } else { cflags += [ "-Os" ] } } } # Default "optimization on" config. config("optimize") { ldflags = common_optimize_on_ldflags cflags = common_optimize_on_cflags if (optimize_for_size) { # Favor size over speed. if (is_clang) { cflags += [ "-O2" ] } else { cflags += [ "-Os" ] } } else { cflags += [ "-O2" ] } } # The default optimization applied to all targets. This will be equivalent to # either "optimize" or "no_optimize", depending on the build flags. config("default_optimization") { if (is_debug) { configs = [ ":no_optimize" ] } else { configs = [ ":optimize" ] } } # default_warnings ------------------------------------------------------------ # # Collects all warning flags that are used by default.This way these # flags are guaranteed to appear on the compile command line after -Wall. config("default_warnings") { cflags = [] cflags_cc = [] ldflags = [] # Suppress warnings about ABI changes on ARM (Clang doesn't give this # warning). if (current_cpu == "arm" && !is_clang) { cflags += [ "-Wno-psabi" ] } if (!is_clang) { cflags_cc += [ # See comment for -Wno-c++11-narrowing. "-Wno-narrowing", ] # -Wunused-local-typedefs is broken in gcc, # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63872 cflags += [ "-Wno-unused-local-typedefs" ] # Don't warn about "maybe" uninitialized. Clang doesn't include this # in -Wall but gcc does, and it gives false positives. cflags += [ "-Wno-maybe-uninitialized" ] cflags += [ "-Wno-deprecated-declarations" ] # -Wcomment gives too many false positives in the case a # backslash ended comment line is followed by a new line of # comments # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61638 cflags += [ "-Wno-comments" ] } # Common Clang and GCC warning setup. if (!is_win || is_clang) { cflags += [ # Disables. "-Wno-missing-field-initializers", # "struct foo f = {0};" "-Wno-unused-parameter", # Unused function parameters. ] } if (is_mingw) { cflags += [ "-Wno-error=c99-designator", "-Wno-error=anon-enum-enum-conversion", "-Wno-error=implicit-fallthrough", "-Wno-error=sizeof-array-div", "-Wno-error=reorder-init-list", "-Wno-error=range-loop-construct", "-Wno-error=deprecated-copy", "-Wno-error=implicit-int-float-conversion", "-Wno-error=inconsistent-dllimport", "-Wno-error=abstract-final-class", "-Wno-error=sign-compare", "-Wno-unknown-warning-option", ] } if (target_cpu == "mipsel") { cflags += [ "-Wno-extra-semi", "-Wno-error=tautological-constant-out-of-range-compare", "-Wno-shift-count-overflow", "-Wno-constant-conversion", ] } if (is_clang) { cflags += [ # This warns on using ints as initializers for floats in # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|), "-Wno-c++11-narrowing", "-Wno-unneeded-internal-declaration", ] if (current_os == "android") { # Disable calls to out-of-line helpers, don't implement atomic operations for these calls. # (Keep consistent with the default of clang15 in directory prebuilts.) cflags += [ "-mno-outline-atomics" ] } if (use_musl) { cflags += [ "-Wno-error=c99-designator", "-Wno-error=anon-enum-enum-conversion", "-Wno-error=implicit-fallthrough", "-Wno-error=sizeof-array-div", "-Wno-error=reorder-init-list", "-Wno-error=range-loop-construct", "-Wno-error=deprecated-copy", "-Wno-error=implicit-int-float-conversion", "-Wno-error=inconsistent-dllimport", "-Wno-error=unknown-warning-option", "-Wno-error=abstract-final-class", "-Wno-error=sign-compare", "-Wno-error=int-in-bool-context", "-Wno-error=xor-used-as-pow", "-Wno-error=return-stack-address", "-Wno-error=dangling-gsl", "-Wno-unused-but-set-variable", "-Wno-deprecated-declarations", "-Wno-unused-but-set-parameter", "-Wno-null-pointer-subtraction", "-Wno-unqualified-std-cast-call", ] } } } config("compiler") { asmflags = [] cflags = [] cflags_c = [] cflags_cc = [] cflags_objc = [] cflags_objcc = [] ldflags = [] defines = [] configs = [] inputs = [] # System-specific flags. If your compiler flags apply to one of the # categories here, add it to the associated file to keep this shared config # smaller. if (is_android) { configs += [ "$build_root/config/aosp:compiler" ] } if (is_mingw) { configs += [ "$build_root/config/mingw:compiler" ] cflags += [ "-fno-stack-protector" ] } else if (is_ohos) { configs += [ "$build_root/config/ohos:compiler" ] } if (is_linux || is_ohos || is_android) { cflags += [ "-fPIC" ] ldflags += [ "-fPIC" ] if (!is_clang) { # Use pipes for communicating between sub-processes. Faster. # (This flag doesn't do anything with Clang.) cflags += [ "-pipe" ] } ldflags += [ "-Wl,-z,noexecstack", "-Wl,-z,now", "-Wl,-z,relro", ] # Compiler instrumentation can introduce dependencies in DSOs to symbols in # the executable they are loaded into, so they are unresolved at link-time. if (is_ohos || (!using_sanitizer && !is_safestack)) { ldflags += [ "-Wl,-z,defs", "-Wl,--as-needed", ] } # # Change default thread stack size to 2MB for asan. if (is_ohos && using_sanitizer) { ldflags += [ "-Wl,-z,stack-size=2097152" ] } } if (is_posix && use_lld) { # Explicitly select LLD, or else some link actions would fail and print # "/usr/bin/ld ... {SharedLibrayName}.so ... not found". ldflags += [ "-fuse-ld=lld" ] if (current_cpu == "arm64") { # Reduce the page size from 65536 in order to reduce binary size slightly # by shrinking the alignment gap between segments. This also causes all # segments to be mapped adjacently, which breakpad relies on. ldflags += [ "-Wl,-z,max-page-size=4096" ] } } configs += [ # See the definitions below. ":compiler_cpu_abi", ":compiler_codegen", ] if (is_ohos && is_clang && (target_cpu == "arm" || target_cpu == "arm64")) { ldflags += [ "-Wl,--pack-dyn-relocs=android+relr" ] } if (is_linux) { cflags += [ "-pthread" ] # Do not use the -pthread ldflag here since it becomes a no-op # when using -nodefaultlibs, which would cause an unused argument # error. "-lpthread" is added in $build_root/config:default_libs. } if (is_clang) { cflags += [ "-fcolor-diagnostics" ] cflags += [ "-fmerge-all-constants" ] } use_xcode_clang = false if (is_clang && !use_xcode_clang) { cflags += [ "-Xclang", "-mllvm", "-Xclang", "-instcombine-lower-dbg-declare=0", ] } if (is_clang) { cflags += [ "-no-canonical-prefixes" ] } # specify language standard version. if (is_linux || is_ohos || is_android || current_os == "aix") { if (is_clang) { cflags_cc += [ "-std=c++17" ] } else { # In standards-conforming mode, some features like macro "##__VA_ARGS__" # are not supported by gcc. Add flags to support these features. cflags_cc += [ "-std=gnu++17" ] } } else if (!is_win && !is_mingw) { cflags_cc += [ "-std=c++17" ] } } # This provides the basic options to select the target CPU and ABI. # It is factored out of "compiler" so that special cases can use this # without using everything that "compiler" brings in. Options that # tweak code generation for a particular CPU do not belong here! # See "compiler_codegen", below. config("compiler_cpu_abi") { cflags = [] ldflags = [] defines = [] if (is_posix && !is_mac) { # CPU architecture. We may or may not be doing a cross compile now, so for # simplicity we always explicitly set the architecture. if (current_cpu == "x64") { cflags += [ "-m64", "-march=x86-64", ] ldflags += [ "-m64" ] } else if (current_cpu == "x86") { cflags += [ "-m32", "-msse2", "-mfpmath=sse", "-mmmx", ] ldflags += [ "-m32" ] } else if (current_cpu == "arm") { if (is_clang && !is_ohos) { cflags += [ "--target=arm-linux-gnueabihf" ] ldflags += [ "--target=arm-linux-gnueabihf" ] } cflags += [ "-march=$arm_arch", "-mfloat-abi=$arm_float_abi", ] if (arm_tune != "") { cflags += [ "-mtune=$arm_tune" ] } } else if (current_cpu == "arm64") { if (is_clang && !is_ohos && !is_android) { cflags += [ "--target=aarch64-linux-gnu" ] ldflags += [ "--target=aarch64-linux-gnu" ] } if (is_clang && (is_ohos || is_android)) { ldflags += [ "-Wl,--hash-style=gnu" ] } if (!is_android) { cflags += [ "-march=$arm_arch", "-mfloat-abi=$arm_float_abi", "-mfpu=$arm_fpu", ] } else { cflags += [ "-march=$arm_arch" ] } ldflags += [ "-march=$arm_arch" ] } } asmflags = cflags if (current_cpu == "arm64") { asmflags += [ "-march=armv8.2-a+dotprod+fp16" ] } } config("compiler_codegen") { configs = [] cflags = [] if (is_posix && !is_mac) { if (current_cpu == "x86") { if (is_clang) { cflags += [ "-momit-leaf-frame-pointer" ] } } else if (current_cpu == "arm") { if (is_ohos && !is_clang) { # Clang doesn't support these flags. cflags += [ "-fno-tree-sra", "-fno-caller-saves", ] } } } asmflags = cflags } config("default_include_dirs") { include_dirs = [ "//", root_gen_dir, ] } config("runtime_config") { configs = [] if (is_posix) { configs += [ "$build_root/config/posix:runtime_config" ] } # System-specific flags. If your compiler flags apply to one of the # categories here, add it to the associated file to keep this shared config # smaller. if (is_ohos) { configs += [ "$build_root/config/ohos:runtime_config" ] } if (is_android) { configs += [ "$build_root/config/aosp:runtime_config" ] } } config("ark_code") { cflags = [ "-Wall" ] if (treat_warnings_as_errors) { cflags += [ "-Werror" ] ldflags = [ "-Werror" ] } if (is_clang) { # Enable extra warnings for ark_code when we control the compiler. cflags += [ "-Wextra" ] } if (is_clang) { cflags += [ "-Wimplicit-fallthrough", "-Wthread-safety", ] } configs = [ ":default_warnings" ] }