blob: 19c802937c6315b9fa4eb5730519e8f96e8b7b96 [file] [log] [blame]
# Copyright (C) 2017 The Android Open Source Project
#
# 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("//gn/perfetto_check_build_deps.gni")
import("//gn/standalone/android.gni")
import("//gn/standalone/libc++/libc++.gni")
import("//gn/standalone/sanitizers/sanitizers.gni")
import("//gn/standalone/toolchain/msvc.gni")
import("//gn/standalone/wasm.gni")
# These warnings have been introduced with the newest version of clang (only in
# the hermetic build) and are enabled just with -Werror.
hermetic_clang_suppressions = [ "-Wno-c99-designator" ]
# We deal with three toolchains here:
# 1. Clang, used in most cases.
# 2. GCC, used in some Linux cases.
# 3. MSVC, used in some Windows cases.
# Clang vs gcc is typically not a problem: both support roughly the same
# switches. -Wno-unknown-warning-option fixes the mismatching ones.
# The situation on Windows is a bit trickier: clang-cl.exe really pretends to be
# cl.exe (MSVC), so we should use MSVC-style switches (e.g. /W2). However,
# clang-cl.exe still supports some -Wclang-style-switches for flags that don't
# have a corresponding version in MSVC.
#
# In the rules below, the conditionals should be interpreted as follows:
# is_win -> can be either clang-cl.exe or cl.exe (MSVC). Only MSVC-style
# switches (the common denominator) should be used.
# is_clang -> could be clang-on-linux, clang-on-mac or clang-cl.exe.
config("extra_warnings") {
if (is_win) {
cflags = [
"/W2",
"/wd4244", # conversion from 'float' to 'int', possible loss of data
"/wd4267", # conversion from 'size_t' to 'int', possible loss of data
]
if (is_clang) {
cflags += [
"-Wno-float-equal",
"-Wno-unused-macros",
"-Wno-old-style-cast",
]
}
} else {
# Clang or Gcc. On linux, Android and Mac.
cflags = [
"-Wall",
"-Wextra",
"-Wpedantic",
]
}
# Disable variadic macro warning as we make extensive use of them in trace
# processor and client API.
if (is_clang) {
if (!is_fuzzer) {
# Disable Weverything on fuzzers to avoid breakages when new versions of
# clang are rolled into OSS-fuzz.
cflags += [ "-Weverything" ]
}
cflags += [
"-Wno-c++98-compat-pedantic",
"-Wno-c++98-compat",
"-Wno-disabled-macro-expansion",
"-Wno-documentation-unknown-command",
"-Wno-gnu-include-next",
"-Wno-gnu-statement-expression",
"-Wno-gnu-zero-variadic-macro-arguments",
"-Wno-padded",
"-Wno-poison-system-directories",
"-Wno-reserved-id-macro",
"-Wno-reserved-identifier",
"-Wno-shadow-uncaptured-local",
"-Wno-unknown-sanitizers",
"-Wno-unknown-warning-option",
"-Wno-unsafe-buffer-usage",
]
} else if (!is_clang && !is_win) {
# Use return std::move(...) for compatibility with old GCC compilers.
cflags_cc = [ "-Wno-redundant-move" ]
# Use after free detection in GCC is still not good enough: it still fails
# on very obvious false-positives in trace processor.
cflags_cc += [ "-Wno-use-after-free" ]
# GCC 7's handling of uninitialized std::optional is flaky at best and
# causes many false positives.
# TODO(lalitm): remove this when we upgrade to a GCC version which is good
# enough to handle this.
cflags_cc += [ "-Wno-maybe-uninitialized" ]
# GCC's handling of detecting infinite recursion is flaky at best and
# causes some false positives.
# TODO(lalitm): remove this when we upgrade to a GCC version which is good
# enough to handle this.
cflags_cc += [ "-Wno-infinite-recursion" ]
# GCC's handling of detecting non null arguments is flaky at best and
# causes some false positives.
# TODO(lalitm): remove this when we upgrade to a GCC version which is good
# enough to handle this.
cflags_cc += [ "-Wno-nonnull" ]
}
}
config("no_exceptions") {
# Exceptions are disabled by default on Windows (Use /EHsc to enable them).
if (!is_win) {
cflags_cc = [ "-fno-exceptions" ]
}
}
config("no_rtti") {
if (is_win) {
cflags_cc = [ "/GR-" ]
} else {
cflags_cc = [ "-fno-rtti" ]
}
}
# Used in buildtools dependencies for standalone builds.
config("c++17") {
if (is_win) {
cflags_cc = [ "/std:c++17" ]
} else {
cflags_cc = [ "-std=c++17" ]
}
}
# Used in buildtools dependencies for standalone builds.
config("c++20") {
visibility = [ "//buildtools:libc++config" ]
if (is_win) {
cflags_cc = [ "/std:c++20" ]
} else {
cflags_cc = [ "-std=c++20" ]
}
}
config("visibility_hidden") {
if (!is_win) {
cflags = [ "-fvisibility=hidden" ]
}
}
config("win32_lean_and_mean") {
if (is_win) {
defines = [ "WIN32_LEAN_AND_MEAN" ]
}
}
config("default") {
asmflags = []
cflags = []
cflags_c = []
cflags_cc = []
defines = []
include_dirs = []
ldflags = []
libs = []
if ((is_android || is_linux) && !is_wasm) {
ldflags += [ "-Wl,--build-id" ]
}
if (is_clang || !is_win) { # Clang or GCC, but not MSVC.
cflags += [
"-fstrict-aliasing",
"-Wformat",
]
}
if (is_clang && is_win) {
# clang-cl from version 16 does not like out-of-line definition of static
# constexpr, even in C++14 mode. Disable the deprecated warnings to work
# around the problem.
cflags += [ "-Wno-deprecated" ]
}
if (is_win) {
cflags += [
"/bigobj", # Some of our files are bigger than the regular limits.
"/Gy", # Enable function-level linking.
"/FS", # Preserve previous PDB behavior.
"/utf-8", # Assume UTF-8 by default to avoid code page dependencies.
"/Zc:__cplusplus", # Allow use of __cplusplus macro.
]
defines += [
"_CRT_NONSTDC_NO_WARNINGS",
"_CRT_SECURE_NO_DEPRECATE",
"_CRT_SECURE_NO_WARNINGS", # Disables warnings on some POSIX-compat API.
"_SCL_SECURE_NO_DEPRECATE",
"NOMINMAX",
]
if (!use_custom_libcxx) {
defines += [ "_HAS_EXCEPTIONS=0" ] # Disables exceptions in MSVC STL.
}
} else if (!is_wasm) { # !is_win
cflags += [
"-g",
"-fPIC",
"-fstack-protector-strong",
]
}
# Treat warnings as errors, but give up on fuzzer builds.
if (!is_fuzzer) {
if (is_win) {
cflags += [ "/WX" ]
} else {
cflags += [ "-Werror" ]
}
}
if (is_clang) {
cflags += [ "-fcolor-diagnostics" ]
if (!is_win) {
cflags += [ "-fdiagnostics-show-template-tree" ]
}
}
if (is_hermetic_clang && is_linux && !is_wasm) {
cflags += hermetic_clang_suppressions
} else {
not_needed([ "hermetic_clang_suppressions" ])
}
if (non_hermetic_clang_stdlib != "") {
if (is_clang && !is_hermetic_clang && !is_wasm) {
cflags_cc += [ "-stdlib=" + non_hermetic_clang_stdlib ]
ldflags += [ "-stdlib=" + non_hermetic_clang_stdlib ]
}
}
if (is_lto) {
cflags += [ "-flto=full" ]
ldflags += [ "-flto=full" ]
}
if (is_win) {
# We support only x86/x64 builds on Windows.
assert(current_cpu == "x64" || current_cpu == "x86")
} else if (current_cpu == "arm") {
cflags += [
"-march=armv7-a",
"-mfpu=neon",
"-mthumb",
]
} else if (current_cpu == "riscv64") {
if (!is_clang) {
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104338
libs += [ "atomic" ]
}
} else if (current_cpu == "x86") {
asmflags += [ "-m32" ]
cflags += [
"-m32",
"-msse2",
"-mfpmath=sse",
]
ldflags += [
"-m32",
"-lgcc",
]
} else if (current_cpu == "arm64") {
cflags += [ "-fno-omit-frame-pointer" ]
} else if (current_cpu == "x64") {
cflags += [ "-fno-omit-frame-pointer" ] # For perf profiling.
if (enable_perfetto_x64_cpu_opt) {
# When updating these flags, the CheckCpuOptimizations() in utils.cc must
# be updated accordingly.
cflags += [
"-mbmi",
"-mbmi2",
"-mavx2",
"-mpopcnt",
"-msse4.2",
]
}
}
if (is_wasm) {
# As of writing (2023-06-12) WASM 128bit SIMD is supported on
# stable Chrome, Safari, and Firefox. See:
# - https://webassembly.org/roadmap/
# - https://emscripten.org/docs/porting/simd.html
cflags += [ "-msimd128" ]
}
if (is_linux) {
# Enable LFS (large file support) for stat() and other syscalls.
cflags += [
"-D_FILE_OFFSET_BITS=64",
"-D_LARGEFILE_SOURCE",
"-D_LARGEFILE64_SOURCE",
]
libs += [
"pthread",
"rt",
]
}
if (is_win && !is_clang) {
# When using MSVC we need to manually pass the include dirs. clang-cl.exe
# doesn't need them because it's smart enough to figure out the right path
# by querying the registry on its own.
include_dirs = win_msvc_inc_dirs # Defined in msvc.gni.
}
if (is_debug) {
if (is_win) {
cflags += [ "/Z7" ]
if (is_clang) {
# Required to see symbols in windbg when building with clang-cl.exe.
cflags += [ "-gcodeview-ghash" ]
ldflags = [ "/DEBUG:GHASH" ]
}
} else {
libs += [ "dl" ]
}
}
if (is_android) {
asmflags += [ "--target=$android_abi_target" ]
cflags += [
"--sysroot=$android_compile_sysroot",
"-DANDROID",
"-D__ANDROID_API__=${android_api_level}",
"--target=$android_abi_target",
]
cflags_cc += [
"-isystem$android_ndk_root/sources/cxx-stl/llvm-libc++/include",
"-isystem$android_ndk_root/sources/android/support/include",
"-isystem$android_ndk_root/sources/cxx-stl/llvm-libc++abi/include",
]
# Keep these include paths *after* the above to prevent the wrong include
# being used for some headers (e.g. math.h).
# We need to add to each language (cflags_c, cflags_cc etc.) separately as
# we added to cflags directly, these inludes would end up before the
# languge and thus the above includes.
sysroot_cflags = [
"-isystem$android_compile_sysroot/$android_compile_sysroot_subdir",
"-isystem$android_compile_sysroot",
]
cflags_c += sysroot_cflags
cflags_cc += sysroot_cflags
ldflags += [
"-Wl,-z,nocopyreloc",
"-gcc-toolchain",
"$android_toolchain_root",
"--sysroot=$android_ndk_root/$android_link_sysroot_subdir",
"--target=$android_abi_target",
"-Wl,--exclude-libs,libunwind.a",
"-Wl,--exclude-libs,libgcc.a",
"-Wl,--exclude-libs,libc++_static.a",
"-Wl,--no-undefined",
"-Wl,-z,noexecstack",
"-Wl,-z,relro",
"-Wl,-z,now",
"-Wl,--warn-shared-textrel",
"-Wl,--fatal-warnings",
# New NDKs need setting this to not give "unable to find library -lc++".
# See https://github.com/android/ndk/issues/951#issuecomment-501017894.
"-nostdlib++",
]
lib_dirs = [
"$android_ndk_root/sources/cxx-stl/llvm-libc++/libs/$android_app_abi",
]
libs += [
"gcc",
"c++_static",
"c++abi",
]
if (current_cpu == "arm") {
# New NDKs don't have libandroid_support for arm64.
libs += [ "android_support" ]
}
}
}
config("debug_symbols") {
cflags = []
if (is_win) {
cflags = [ "/Od" ]
} else {
cflags = [ "-O0" ]
}
if (is_android || is_linux) {
cflags += [ "-funwind-tables" ]
}
}
config("release") {
# Compiler flags for release builds.
if (is_win) {
cflags = [
"/O2",
"/Zc:inline",
]
} else if (is_android) {
cflags = [ "-O2" ]
} else if (is_fuzzer) {
cflags = [ "-O1" ]
} else {
cflags = [ "-O3" ]
}
if (!is_win) {
cflags += [
"-fdata-sections",
"-ffunction-sections",
]
}
# Linker flags for release builds.
if (is_win) {
ldflags = [
"/OPT:REF",
"/OPT:ICF",
"/INCREMENTAL:NO",
"/FIXED:NO",
]
} else if (is_mac) {
ldflags = [ "-dead_strip" ]
} else if (!is_win && !is_wasm) {
ldflags = [
"-Wl,--gc-sections",
"-Wl,--icf=all",
"-Wl,-O1",
]
}
defines = [ "NDEBUG" ]
}
config("shared_library") {
if (is_android || is_linux) {
ldflags = [ "-fPIC" ]
}
}
config("executable") {
ldflags = []
# Android will refuse to run executables if they aren't position independent.
# Instead on Linux there isn't any need and they break ASan (goo.gl/paFR6K).
# The OSS-Fuzz provided AFL library is not PIC, so we we cannot use -fPIE
# for the fuzzer executables.
if ((is_android || is_linux) && !is_wasm && !is_fuzzer) {
asmflags = [ "-fPIE" ]
cflags = [ "-fPIE" ]
ldflags += [ "-pie" ]
}
# -rpath stores the path to the linked shared libraries into the binary, so
# that they can be launched without passing any LD_LIBRARY_PATH. It's
# supported only by Linux, not Android. But concretely we need this only when
# use_custom_libcxx=true && custom_libcxx_is_static=false, which happens only
# on Linux right now.
if (is_linux && !is_wasm) {
ldflags += [
"-Wl,-rpath=\$ORIGIN/.",
"-Wl,-rpath-link=.",
]
}
}
# This config is only added to certain leaf target types (see BUILDCONFIG.gn).
# This allows us to remove the config (and thus the dependency) on a per-target
# basis. If the config was applied to source_sets, then they would unavoidably
# carry the dependency onto liblog to the final target.
config("android_liblog") {
if (is_android) {
libs = [ "log" ]
}
}
# Checks that tools/install-build-deps has been run since it last changed.
perfetto_check_build_deps("check_build_deps") {
args = []
}
perfetto_check_build_deps("check_build_deps_android") {
args = [ "--android" ]
}