blob: 0310e2a365eb66cf1767f6a1521a7ebcd92a7871 [file] [log] [blame]
# Copyright (c) 2020 Project CHIP Authors
#
# 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_overrides/build.gni")
import("//build_overrides/pigweed.gni")
import("//build_overrides/pigweed_environment.gni")
import("${build_root}/chip/java/config.gni")
import("${build_root}/config/compiler/compiler.gni")
import("${build_root}/config/sysroot.gni")
import("${build_root}/config/target.gni")
if (current_os == "mac" || current_os == "ios") {
import("${build_root}/config/mac/mac_sdk.gni")
}
# To use different sanitizer options, use `gn args .` in the out folder and
# use settings like:
#
# is_clang=true
# is_debug=true
# optimize_for_size=false
# is_asan=true
# is_sanitize_fatal=false
#
declare_args() {
# Enable -Werror. This can be disabled if using a different compiler
# with unfixed or unsupported wanings.
treat_warnings_as_errors = true
# Enable Thread sanitizer
is_tsan = false
# Enable memory sanitizer
is_msan = false
# enable undefined behavior sanitizer
is_ubsan = false
# Exit on sanitize error. Generally standard libraries may get errors
# so not stopping on the first error is often useful
is_sanitize_fatal = true
# Enable or disable Runtime Type Information (RTTI).
# Defaults true on darwin because Darwin.framework uses it.
enable_rtti = current_os == "mac" || current_os == "ios"
# Enable or disable support for C++ exceptions.
enable_exceptions = false
}
if (current_cpu == "arm" || current_cpu == "arm64") {
import("${build_root}/config/arm.gni")
} else if (current_cpu == "x86" || current_cpu == "x86_64") {
import("${build_root}/config/x86.gni")
} else if (current_cpu == "riscv") {
import("${build_root}/config/riscv.gni")
}
config("release") {
defines = [ "NDEBUG" ]
}
config("debug_default") {
if (!is_debug) {
configs = [ ":release" ]
}
}
config("abi_default") {
cflags = []
if (current_os == "mac" || current_os == "ios") {
cflags += [
"-target",
mac_target_triple,
]
}
if (current_os != "mac" && current_os != "ios") {
if (current_cpu == "arm" || current_cpu == "arm64") {
if (current_os == "linux" && is_clang) {
if (current_cpu == "arm") {
cflags += [ "--target=armv7-linux-gnueabihf" ]
} else if (current_cpu == "arm64") {
cflags += [ "--target=aarch64-linux-gnu" ]
}
}
if (arm_arch != "") {
cflags += [ "-march=${arm_arch}" ]
}
if (arm_cpu != "") {
cflags += [ "-mcpu=${arm_cpu}" ]
}
if (arm_tune != "") {
cflags += [ "-mtune=${arm_tune}" ]
}
if (arm_abi != "") {
cflags += [ "-mabi=${arm_abi}" ]
}
if (arm_fpu != "") {
cflags += [ "-mfpu=${arm_fpu}" ]
}
if (arm_float_abi != "") {
cflags += [ "-mfloat-abi=${arm_float_abi}" ]
}
if (arm_use_thumb) {
cflags += [ "-mthumb" ]
}
}
if (current_cpu == "x86" || current_cpu == "x86_64") {
if (x86_arch != "") {
cflags += [ "-march=${x86_arch}" ]
}
if (x86_cpu != "") {
cflags += [ "-mcpu=${x86_cpu}" ]
}
if (x86_tune != "") {
cflags += [ "-mtune=${x86_tune}" ]
}
if (current_cpu == "x86_64") {
cflags += [
"-msse4.2",
"-mpopcnt",
"-m64",
]
} else if (current_cpu == "x86") {
cflags += [
"-mssse3",
"-mfpmath=sse",
"-m32",
]
}
}
if (current_cpu == "riscv") {
if (riscv_arch != "") {
cflags += [ "-march=${riscv_arch}" ]
}
if (riscv_cpu != "") {
cflags += [ "-mcpu=${riscv_cpu}" ]
}
if (riscv_tune != "") {
cflags += [ "-mtune=${riscv_tune}" ]
}
if (riscv_abi != "") {
cflags += [ "-mabi=${riscv_abi}" ]
}
if (riscv_fpu != "") {
cflags += [ "-mfpu=${riscv_fpu}" ]
}
if (riscv_float_abi != "") {
cflags += [ "-mfloat-abi=${riscv_float_abi}" ]
}
}
}
asmflags = cflags
ldflags = cflags
}
config("dead_code_elimination_default") {
# Delete unreferenced sections. Helpful with -ffunction-sections.
if (current_os == "mac" || current_os == "ios") {
ldflags = [ "-Wl,-dead_strip" ]
} else {
ldflags = [ "-Wl,--gc-sections" ]
}
}
config("target_default") {
if (current_toolchain == default_toolchain) {
defines = target_defines
cflags = target_cflags
cflags_c = target_cflags_c
cflags_cc = target_cflags_cc
ldflags = target_ldflags
}
}
config("optimize_zero") {
cflags = [ "-O0" ]
ldflags = cflags
}
config("optimize_debug") {
cflags = [ "-O${optimize_debug_level}" ]
ldflags = cflags
}
config("optimize_default") {
if (is_debug) {
if (optimize_debug) {
configs = [ ":optimize_debug" ]
} else {
configs = [ ":optimize_zero" ]
}
} else {
if (optimize_for_size) {
configs = [ "$dir_pw_build:optimize_size" ]
} else {
configs = [ "$dir_pw_build:optimize_speed" ]
}
if (current_os != "mac") {
ldflags = [
"-Wl,-O2",
"-Wl,--gc-sections",
]
}
}
}
config("disabled_warnings") {
cflags = [
"-Wno-deprecated-declarations",
"-Wno-missing-field-initializers",
"-Wno-unknown-warning-option",
"-Wno-unused-parameter",
]
if (!is_debug) {
# assert() causes unused variable warnings in release.
cflags += [ "-Wno-unused" ]
}
if (!is_clang) {
cflags += [
"-Wno-cast-function-type",
"-Wno-psabi",
"-Wno-maybe-uninitialized",
]
}
}
config("warnings_common") {
cflags = [ "-Wall" ]
ldflags = []
if (treat_warnings_as_errors) {
cflags += [ "-Werror" ]
ldflags += [ "-Werror" ]
}
if (current_os != "mac" && current_os != "ios") {
ldflags += [ "-Wl,--fatal-warnings" ]
}
if (current_os != "mac" && current_os != "ios" && current_os != "linux" &&
current_os != "win" && current_os != "tizen" && current_os != "webos" &&
current_os != "cmsis-rtos") {
cflags += [ "-Wstack-usage=8192" ]
}
}
config("strict_warnings") {
cflags = [
"-Wextra",
"-Wshadow",
"-Wunreachable-code",
"-Wvla",
"-Wformat",
"-Wformat-nonliteral",
"-Wformat-security",
]
if (current_os == "webos") {
cflags -= [
"-Wshadow",
"-Wvla",
]
}
if (current_os == "mac" || current_os == "ios") {
cflags += [ "-Wconversion" ]
}
# For now we can't enable -Wundef across the board. Enable it where
# we can. Ideally this would be checking chip_device_platform or so
# to be more fine-grained than current_os, but it's not clear that
# we can access that here.
if (current_os != "freertos" && current_os != "mbed" &&
# cmsis-rtos is OpenIOT
current_os != "cmsis-rtos" &&
# cyw30739 is one of the Infineon builds
current_os != "cyw30739") {
cflags += [ "-Wundef" ]
}
if (matter_enable_java_compilation) {
cflags -= [ "-Wshadow" ]
}
cflags_cc = [ "-Wnon-virtual-dtor" ]
if (current_os == "nuttx") {
cflags -= [ "-Wshadow" ]
cflags_cc -= [ "-Wnon-virtual-dtor" ]
}
configs = []
ldflags = []
if (is_clang) {
cflags += [
"-Wimplicit-fallthrough",
"-Wheader-hygiene",
"-Wshorten-64-to-32",
"-Wformat-type-confusion",
]
configs += [ "$dir_pw_build:clang_thread_safety_warnings" ]
# TODO: can make this back fatal in once pigweed updates can be taken again.
# See https://github.com/project-chip/connectedhomeip/pull/22079
#
# Currently `./scripts/build/build_examples.py --target linux-arm64-light-rpc-ipv6only-clang build`
# fails in third_party/pigweed/repo/pw_protobuf
if (current_cpu == "arm64" && current_os == "linux") {
cflags += [ "-Wno-error=shorten-64-to-32" ]
}
}
if (!is_asan && (current_os == "linux" || current_os == "android")) {
ldflags += [ "-Wl,-z,defs" ]
}
}
config("warnings_default") {
configs = [
":warnings_common",
":strict_warnings",
":disabled_warnings",
]
}
config("disabled_warnings_third_party") {
cflags = [
"-Wno-unused",
"-Wno-format",
"-Wno-address",
]
if (!is_clang) {
cflags += [ "-Wno-maybe-uninitialized" ]
}
}
config("warnings_third_party") {
configs = [
":warnings_common",
":disabled_warnings",
":disabled_warnings_third_party",
]
}
config("symbols_default") {
if (strip_symbols) {
cflags = [ "-s" ]
} else {
cflags = [ "-g${symbol_level}" ]
}
}
config("std_default") {
cflags_c = [ "-std=${c_standard}" ]
cflags_objc = [ "-std=${c_standard}" ]
cflags_cc = [ "-std=${cpp_standard}" ]
cflags_objcc = [ "-std=${cpp_standard}" ]
}
config("cosmetic_default") {
configs = [ "$dir_pw_build:colorize_output" ]
}
config("runtime_default") {
if (is_clang &&
current_os == "mac") { # Using Pigweed clang instead of Darwin host clang
# Without pw_env_setup_CIPD_PIGWEED defined the hostclang:no_system_libcpp
# config silently uses the system libc++, usually resulting in linker errors.
assert(
defined(pw_env_setup_CIPD_PIGWEED),
"//build_overrides/pigweed_environment.gni must define pw_env_setup_CIPD_PIGWEED when using pigweed clang")
configs = [
"$dir_pw_toolchain/host_clang:no_system_libcpp",
"$dir_pw_toolchain/host_clang:xcode_sysroot",
]
}
if (current_os == "linux" || current_os == "tizen" || current_os == "webos") {
libs = [
"atomic",
"dl",
"pthread",
"rt",
]
}
cflags = []
ldflags = []
if (sysroot != "") {
cflags += [ "--sysroot=${sysroot}" ]
ldflags += [ "--sysroot=${sysroot}" ]
}
cflags += runtime_library_cflags
ldflags += runtime_library_ldflags
}
# To use different sanitizer options, use `gn args .` in the out folder and
# use settings like:
#
# is_clang=true
# is_debug=true
# optimize_for_size=false
# is_asan=true
# is_sanitize_fatal=false
#
config("sanitize_address") {
defines = []
cflags = [
"-fsanitize-coverage=inline-8bit-counters,trace-cmp",
"-fno-sanitize-coverage=pc-table",
"-fsanitize=address",
"-fno-omit-frame-pointer",
]
ldflags = cflags
if ((target_os == "mac" || target_os == "ios") && !is_clang) {
defines += [ "_LIBCPP_HAS_NO_ASAN" ]
}
}
config("sanitize_thread") {
cflags = [ "-fsanitize=thread" ]
ldflags = cflags
}
config("sanitize_memory") {
cflags = [
"-fsanitize=memory",
"-fsanitize-memory-track-origins",
"-fno-omit-frame-pointer",
]
ldflags = cflags
}
config("sanitize_undefined_behavior") {
cflags = [
"-fvisibility=hidden",
"-fsanitize=undefined",
"-fsanitize=float-divide-by-zero",
]
if (is_clang) {
cflags += [
"-fsanitize=unsigned-integer-overflow",
"-fsanitize=implicit-conversion",
"-fsanitize=nullability",
]
}
#According to the LLVM UBSan documentation, sanitizing vptr is incompatible with the -fno-rtti flag.
if (!enable_rtti) {
cflags += [ "-fno-sanitize=vptr" ]
}
ldflags = cflags
}
config("sanitize_recover_all") {
cflags = [ "-fsanitize-recover=all" ]
ldflags = cflags
}
config("sanitize_default") {
configs = []
if (is_tsan) {
configs += [ ":sanitize_thread" ]
}
if (is_asan) {
configs += [ ":sanitize_address" ]
}
if (is_msan) {
configs += [ ":sanitize_memory" ]
}
if (is_ubsan) {
configs += [ ":sanitize_undefined_behavior" ]
}
if (!is_sanitize_fatal) {
configs += [ ":sanitize_recover_all" ]
}
}
config("libfuzzer_fuzzing") {
cflags = [ "-fsanitize=fuzzer" ]
ldflags = cflags
}
config("oss_fuzz") {
cflags = string_split(getenv("CFLAGS"))
cflags_cc = string_split(getenv("CXXFLAGS"))
ldflags = string_split(getenv("CXXFLAGS"))
ldflags += [ getenv("LIB_FUZZING_ENGINE") ]
}
config("fuzzing_default") {
configs = []
if (is_libfuzzer) {
configs += [ ":libfuzzer_fuzzing" ]
}
if (oss_fuzz) {
configs += [ ":oss_fuzz" ]
}
}
config("coverage") {
cflags = [ "--coverage" ]
ldflags = cflags
}
config("coverage_default") {
configs = []
if (use_coverage) {
configs += [ ":coverage" ]
}
}
config("no_rtti") {
cflags_cc = [ "-fno-rtti" ]
}
config("rtti") {
cflags_cc = [ "-frtti" ]
}
config("rtti_default") {
if (enable_rtti) {
configs = [ ":rtti" ]
} else {
configs = [ ":no_rtti" ]
}
}
config("no_exceptions") {
cflags = [ "-fno-exceptions" ]
}
config("exceptions") {
cflags = [ "-fexceptions" ]
}
config("exceptions_default") {
if (enable_exceptions) {
configs = [ ":exceptions" ]
} else {
configs = [ ":no_exceptions" ]
}
}
config("unwind_tables") {
cflags = [ "-funwind-tables" ]
}
config("no_unwind_tables") {
cflags = [
"-fno-unwind-tables",
"-fno-asynchronous-unwind-tables",
]
}
config("unwind_tables_default") {
if (exclude_unwind_tables) {
configs = [ ":no_unwind_tables" ]
} else {
configs = [ ":unwind_tables" ]
}
}
config("size_default") {
cflags = [
"-fno-common",
"-ffunction-sections",
"-fdata-sections",
]
# do not export functions in jni, or --gc-sections will not work and will be some undefined issues
if (current_os == "android") {
cflags += [ "-fvisibility=hidden" ]
}
}
config("stack_protector_default") {
cflags = [ "-fno-stack-protector" ]
}
config("prefix_mappings_default") {
cflags = []
foreach(prefix_mapping, prefix_mappings) {
cflags += [ "-fdebug-prefix-map=${prefix_mapping}" ]
}
}
config("pic_default") {
if (enable_pic) {
cflags = [ "-fPIC" ]
ldflags = cflags
}
}
config("pie_default") {
if (enable_pie) {
ldflags = [ "-pie" ]
}
}
config("aliasing_default") {
cflags = [ "-fno-strict-aliasing" ]
}
config("specs_default") {
if (current_cpu == "arm" && current_os == "freertos" && !is_clang) {
cflags = [
"--specs=nosys.specs",
"--specs=nano.specs",
]
libs = [ "nosys" ]
ldflags = cflags
}
}