| # Copyright 2018 The ChromiumOS Authors | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| declare_args() { | |
| # Set this to false if you'd like to disable -Werror. | |
| enable_werror = true | |
| # Set this to false if you want to disable Large File Support (LFS). | |
| enable_lfs = true | |
| } | |
| if (use.pigweed) { | |
| # TODO(b/267537074) - This will fix this workaround. | |
| # Pigweed defaults to use CXX17 standard, update pw variable | |
| # to match platform2 CXX20 standard. | |
| dir_pigweed = "//third_party/pigweed" | |
| import("$dir_pigweed/pw_toolchain/traits.gni") | |
| pw_toolchain_CXX_STANDARD = pw_toolchain_STANDARD.CXX20 | |
| } | |
| _cflags_no_exceptions = [ | |
| "-fno-exceptions", | |
| "-fno-unwind-tables", | |
| "-fno-asynchronous-unwind-tables", | |
| ] | |
| config("no_exceptions") { | |
| cflags_cc = _cflags_no_exceptions | |
| cflags_c = _cflags_no_exceptions | |
| } | |
| _gcc_cflags = [ "-Wno-psabi" ] | |
| config("gcc_cflags_c") { | |
| cflags_c = _gcc_cflags | |
| } | |
| config("gcc_cflags_cc") { | |
| cflags_cc = _gcc_cflags | |
| } | |
| # Note: DO NOT USE THIS. Use brillo/brillo_export.h instead to export symbols. | |
| # | |
| # Symbol visibility is set to be internal by default. Add this to your target's | |
| # configs to make its symbols visible. | |
| # Setting "-fvisibility=default" directly in your target's cflags doesn't work | |
| # because it will be overwritten by the "-fvisibility=internal" in the | |
| # compiler_defaults' cflags. | |
| config("visibility_default") { | |
| # gnlint: disable=GnLintVisibilityFlags | |
| cflags = [ "-fvisibility=default" ] | |
| } | |
| config("compiler_defaults") { | |
| defines = [] | |
| libs = [] | |
| include_dirs = [ | |
| "${root_gen_dir}/include", | |
| "${platform2_root}", | |
| ] | |
| cflags = [ | |
| "-Wall", | |
| "-Wunused", | |
| "-Wbool-operation", | |
| "-Wfree-nonheap-object", | |
| "-Wint-in-bool-context", | |
| "-Wstring-compare", | |
| "-Wstring-plus-int", | |
| "-Wxor-used-as-pow", | |
| "-Wdeprecated-declarations", | |
| # We use C99 array designators in C++ code. Our compilers support this | |
| # extension since C++ has no equivalent yet (as of C++20). | |
| "-Wno-c99-designator", | |
| "-Wno-unused-parameter", | |
| "-Wunreachable-code", | |
| "-Wunreachable-code-return", | |
| "-ggdb3", | |
| "-fstack-protector-strong", | |
| "-Wformat=2", | |
| "-fvisibility=internal", | |
| "-Wa,--noexecstack", | |
| "-Wimplicit-fallthrough", | |
| ] | |
| cflags_c = [ "-std=gnu17" ] + external_cppflags + external_cxxflags | |
| cc_version = "-std=gnu++20" | |
| cflags_cc = [ cc_version ] + external_cppflags + external_cxxflags | |
| ldflags = external_ldflags + [ | |
| "-Wl,-z,relro", | |
| "-Wl,-z,noexecstack", | |
| "-Wl,-z,now", | |
| "-Wl,--as-needed", | |
| ] | |
| if (link_stdcppfs) { | |
| libs += [ "stdc++fs" ] | |
| } | |
| if (enable_werror) { | |
| cflags += [ | |
| "-Werror", | |
| # TODO(b/217226507): reduce the scope of this error suppression or | |
| # fix each case. | |
| "-Wno-error=deprecated-enum-enum-conversion", | |
| # TODO(b/217226507): some lambda capturing (specifically tests with | |
| # gmock) implicitly capture this, but in C++20 it should to be | |
| # explicit. However, this feature is not available until C++20, so | |
| # updating the code requires C++20 enabled. | |
| # For transition, suppress the error, and once C++20 is stabilized | |
| # we can clean up the cases. | |
| "-Wno-error=deprecated-this-capture", | |
| ] | |
| } | |
| if (use.cros_host) { | |
| defines += [ "NDEBUG" ] | |
| } | |
| if (use.cros_debug) { | |
| # Initialize stack variables with values that will likely transform | |
| # logic bugs into crashes. | |
| cflags += [ "-ftrivial-auto-var-init=pattern" ] | |
| } | |
| if (enable_lfs) { | |
| defines += [ | |
| # Enable support for new LFS funcs (ftello/etc...). | |
| "_LARGEFILE_SOURCE", | |
| # Enable support for 64bit variants (off64_t/fseeko64/etc...). | |
| "_LARGEFILE64_SOURCE", | |
| # Default to 64bit variants (off_t is defined as off64_t). | |
| "_FILE_OFFSET_BITS=64", | |
| ] | |
| } | |
| # We only rely on `enable_exceptions` arg to control no_exceptions flags | |
| # (see BUILDCONFIG.gn) and ignore if the given external flags contain these | |
| # flags. As removing non-existing flags causes error on GN, we add them before | |
| # its removal to unconditionally remove them. | |
| cflags_cc += _cflags_no_exceptions | |
| cflags_c += _cflags_no_exceptions | |
| cflags_cc -= _cflags_no_exceptions | |
| cflags_c -= _cflags_no_exceptions | |
| if (!use.cros_host) { | |
| include_dirs += [ "${sysroot}/usr/include" ] | |
| cflags += [ "--sysroot=${sysroot}" ] | |
| ldflags += [ "--sysroot=${sysroot}" ] | |
| } | |
| if (use.profiling) { | |
| cflags += [ | |
| "-fprofile-instr-generate", | |
| "-fcoverage-mapping", | |
| ] | |
| ldflags += [ | |
| "-fprofile-instr-generate", | |
| "-fcoverage-mapping", | |
| ] | |
| } | |
| if (use.tcmalloc) { | |
| libs += [ "tcmalloc" ] | |
| } | |
| } | |
| config("lto_experiment") { | |
| # TODO(b/286954353): This is under experiment still. Please do not | |
| # use this in production yet until we evaluate more. | |
| if (use.lto_experiment) { | |
| cflags = [ "-flto" ] | |
| if (use.function_elimination_experiment) { | |
| cflags += [ | |
| "-ffunction-sections", | |
| "-fvirtual-function-elimination", | |
| ] | |
| ldflags = [ "-Wl,--gc-sections" ] | |
| } | |
| } | |
| } | |
| # Generates position independent executable. | |
| # This is a default config for executables and static libraries. | |
| config("pie") { | |
| cflags = [ "-fPIE" ] | |
| # ldflags are not pushed to dependents, so applying ldflags to source sets or | |
| # static libraries will be a no-op. You can just remove "pie" config from an | |
| # executable to make the executable not position independent. | |
| ldflags = [ "-pie" ] | |
| } | |
| # Use thin archive. It makes sense only for static libraries. | |
| # This is a default config for static libraries. | |
| # Remove this and add ":nouse_thin_archive" to unuse thin archive. | |
| config("use_thin_archive") { | |
| arflags = [ "rcsT" ] | |
| } | |
| # Don't use thin archive. It makes sense only for static libraries. | |
| config("nouse_thin_archive") { | |
| arflags = [ "rcs" ] | |
| } | |
| # Generates position independent code. | |
| # This is a default config for shared libraries. | |
| config("pic") { | |
| cflags = [ "-fPIC" ] | |
| } | |
| # This config should be at the top in the configs list, especially before | |
| # the config for libchrome-${libbase-ver} to avoid weird heap-buffer-overflow | |
| # error from happening when ASAN is enabled. | |
| # TODO(crbug.com/887845): Remove this note after library order issue is resolved. | |
| config("test") { | |
| # Don't worry about overlinking, ld.gold's --as-needed will | |
| # deal with that. | |
| # TODO(crbug.com/912432): Consider use {gtest,gmock}-config --ldflags. | |
| # '--ldflags' is not used here. It adds, e.g., -L/usr/lib64, | |
| # so the test executable will be linked against host libraries if exists. | |
| # Specifically, libchrome for host is linked. For host, it is built with | |
| # -DNDEBUG, but the target built without, which causes a link error. | |
| # cf) BaseInitLoggingImpl definition in | |
| # chromiumos/platform/libchrome/base/logging.h | |
| # gtest-config and gmock-config are not installed by gtest-1.8.1. | |
| ldflags = [ | |
| "-pthread", | |
| # Allow unit tests of a shared library to link against the depended .so | |
| # under the output directory, instead of the ones installed in the system | |
| # dir. -rpath and -rpath-link has higher priority than the system library | |
| # directory. | |
| "-Wl,-rpath=\$ORIGIN/", | |
| "-Wl,-rpath=\$ORIGIN/lib/", | |
| "-Wl,-rpath-link=lib/", | |
| ] | |
| if (!use.cros_host) { | |
| # The path where fake libraries are installed on the host. It is | |
| # for tests, implementing simple backend. | |
| _test_lib_dir = "build/lib" | |
| _test_usr_local_lib_dir = "usr/local/lib" | |
| ldflags += [ | |
| # The path where fake libraries are installed on the host. It is | |
| # for tests (e.g. fuzzer/unit tests) running on the host device. | |
| string_join("", | |
| [ | |
| "-Wl,-rpath=/", | |
| _test_lib_dir, | |
| ]), | |
| string_join("", | |
| [ | |
| "-Wl,-rpath=/", | |
| _test_lib_dir, | |
| "64", | |
| ]), | |
| string_join("", | |
| [ | |
| "-Wl,-rpath=/", | |
| _test_usr_local_lib_dir, | |
| ]), | |
| string_join("", | |
| [ | |
| "-Wl,-rpath=/", | |
| _test_usr_local_lib_dir, | |
| "64", | |
| ]), | |
| ] | |
| # To link against library that present an interface with a fake backend, | |
| # for unit testing: | |
| # To prevent such a library to be installed in the rootfs, the library | |
| # is installed in a directory in /build/$BOARD/. | |
| lib_dirs = [ | |
| string_join("", | |
| [ | |
| "${sysroot}/", | |
| _test_lib_dir, | |
| ]), | |
| string_join("", | |
| [ | |
| "${sysroot}/", | |
| _test_lib_dir, | |
| "64", | |
| ]), | |
| string_join("", | |
| [ | |
| "${sysroot}/", | |
| _test_usr_local_lib_dir, | |
| ]), | |
| string_join("", | |
| [ | |
| "${sysroot}/", | |
| _test_usr_local_lib_dir, | |
| "64", | |
| ]), | |
| ] | |
| } | |
| # Disable legacy APIs so people are forced to upgrade. | |
| defines = [ "GTEST_REMOVE_LEGACY_TEST_CASEAPI_" ] | |
| # gnlint: disable=GnLintCommonTesting | |
| libs = [ | |
| "gmock", | |
| "gtest", | |
| "pthread", | |
| ] | |
| } |