blob: 938d2d7e405835438b35acc8dfd8fa2675e9b244 [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_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")
}
declare_args() {
# Enable -Werror. This can be disabled if using a different compiler
# with unfixed or unsupported wanings.
treat_warnings_as_errors = true
}
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-unknown-warning-option",
"-Wno-maybe-uninitialized",
"-Wno-missing-field-initializers",
"-Wno-unused-parameter",
]
if (!is_debug) {
# assert() causes unused variable warnings in release.
cflags += [ "-Wno-unused" ]
}
if (!is_clang) {
cflags += [
"-Wno-psabi",
"-Wno-cast-function-type",
]
}
}
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") {
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",
]
}
cflags_cc = [ "-Wnon-virtual-dtor" ]
ldflags = []
if (is_clang) {
cflags += [
"-Wimplicit-fallthrough",
"-Wheader-hygiene",
"-Wshorten-64-to-32",
"-Wformat-type-confusion",
]
# 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-maybe-uninitialized",
"-Wno-address",
]
}
config("warnings_third_party") {
configs = [
":warnings_common",
":disabled_warnings",
":disabled_warnings_third_party",
]
}
config("symbols_default") {
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) {
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 = [
"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
#
declare_args() {
# 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
}
config("sanitize_address") {
cflags = [
"-fsanitize=address",
"-fno-omit-frame-pointer",
]
ldflags = cflags
}
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",
"-fsanitize=unsigned-integer-overflow",
"-fsanitize=implicit-conversion",
"-fsanitize=nullability",
"-fno-sanitize=vptr,function",
]
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("fuzzing_default") {
configs = []
if (is_libfuzzer) {
configs += [ ":libfuzzer_fuzzing" ]
}
}
config("coverage") {
cflags = [ "--coverage" ]
ldflags = cflags
}
config("coverage_default") {
configs = []
if (use_coverage) {
configs += [ ":coverage" ]
}
}
declare_args() {
# Enable Runtime Type Information (RTTI)
enable_rtti = current_os == "mac" || current_os == "ios"
}
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") {
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("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
}
}