blob: fbc1668ee41f084f295210d533eda9fda1c6759d [file] [log] [blame]
# Copyright 2018 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("levels.gni")
config_group("compiler") {
# For GCC, the compiler command implies the target.
# For Clang, there's a single compiler command that takes `--target`.
if (!is_gcc && (current_cpu != host_cpu || current_os != host_os)) {
compiler_flags = [ "--target=${toolchain.target_tuple}" ]
}
if (defined(toolchain.version_string) && toolchain.version_string != "") {
# Nothing uses this define, but its presence on compilation command
# lines ensures that Ninja will rebuild things when a new compiler
# version arrives.
defines = [ "TOOLCHAIN_VERSION=${toolchain.version_string}" ]
}
# This is the default already on Fuchsia and maybe others, but never hurts.
cflags = [ "-fno-common" ]
public_deps = [
":color_diagnostics",
# TODO: "$current_os:compiler",
]
}
config_group("color_diagnostics") {
# GCC correctly defaults this from the terminal, so leave it alone.
if (!is_gcc) {
compiler_flags = [ "-fcolor-diagnostics" ]
}
# The macOS linker does not support `--color-diagnostics`.
if (current_os != "mac") {
ldflags = [ "-Wl,--color-diagnostics" ]
}
}
config_group("relative_paths") {
# Make builds independent of absolute file path. The file names embedded
# in debugging information will be expressed as relative to the build
# directory, e.g. "../.." for an "out/subdir" under //. This is
# consistent with the file names in __FILE__ expansions (e.g. in
# assertion messages), which the compiler doesn't provide a way to remap.
# That way source file names in logging and symbolization can all be
# treated the same way. This won't go well if $root_build_dir is not a
# subdirectory of //, but there isn't a better option to keep all source
# file name references uniformly relative to a single root.
absolute_path = rebase_path("//.")
relative_path = rebase_path("//.", root_build_dir)
# This makes sure that the DW_AT_comp_dir string (the current
# directory while running the compiler, which is the basis for all
# relative source file names in the DWARF info) is represented as
# relative to //.
compiler_flags = [ "-fdebug-prefix-map=$absolute_path=$relative_path" ]
if (!is_gcc) {
# This makes sure that include directories in the toolchain are
# represented as relative to the build directory (because that's how we
# invoke the compiler), rather than absolute. This can affect __FILE__
# expansions (e.g. assertions in system headers). We normally run a
# compiler that's someplace within the source tree, so its absolute
# installation path will have a prefix matching `absolute_path` and
# hence be mapped to `relative_path` in the debugging information, so
# this should actually be superfluous for purposes of the debugging
# information.
compiler_flags += [ "-no-canonical-prefixes" ]
}
}
config_group("nostdlib") {
compiler_flags = [ "-nostdlib" ]
if (!is_gcc) {
cflags = [ "-nostdlibinc" ]
}
}
config_group("data_sections") {
cflags = [ "-fdata-sections" ]
}
config_group("linker_gc") {
public_deps = [
":data_sections",
]
cflags = [ "-ffunction-sections" ]
ldflags = cflags
if (current_os == "mac") {
ldflags += [ "-Wl,-dead_strip" ]
} else {
ldflags += [ "-Wl,--gc-sections" ]
}
}
config_group("assert_level") {
assert(assert_level >= 0)
defines = [
"LK_DEBUGLEVEL=$assert_level",
"ZX_DEBUGLEVEL=$assert_level",
]
if (assert_level == 0) {
defines += [ "NDEBUG=1" ]
}
}
config_group("opt_level") {
if (opt_level == -1) {
compiler_flags = [ "-O0" ]
} else {
assert(opt_level >= 0)
if (opt_level == 0) {
if (is_gcc) {
compiler_flags = [ "-O0" ]
} else {
compiler_flags = [ "-Og" ]
}
} else if (opt_level == 4) {
if (is_gcc) {
compiler_flags = [ "-Os" ]
} else {
compiler_flags = [ "-Oz" ]
}
} else {
compiler_flags = [ "-O$opt_level" ]
}
}
public_deps = []
if (opt_level > 1) {
public_deps += [ ":icf" ]
}
# TODO(mcgrathr): Kernel can't be built without --gc-sections.
if (opt_level > 0 || is_kernel) {
compiler_flags += [ "-finline-functions" ]
public_deps += [ ":linker_gc" ]
}
if (opt_level > 2) {
# Enable optimal string merging.
ldflags = [ "-Wl,-O2" ]
}
}
config_group("symbol_level") {
assert(symbol_level >= 0 && symbol_level <= 2)
if (symbol_level == 0) {
cflags = [ "-g0" ]
} else if (symbol_level == 1) {
cflags = [ "-gline-tables-only" ]
} else if (symbol_level == 2) {
cflags = [ "-g3" ]
}
asmflags = cflags
ldflags = cflags
}
config_group("werror") {
cflags = [
"-Werror",
# Declarations marked as deprecated should not cause build failures.
# Rather they should emit warnings to notify developers about the use
# of deprecated interfaces.
"-Wno-error=deprecated-declarations",
# Do not add additional `-Wno-error=...` switches to this config!
]
}
config_group("default_warnings") {
cflags = [
"-Wall",
"-Wextra",
"-Wno-multichar",
"-Wno-unused-parameter",
"-Wno-unused-function",
"-Werror=unused-label",
"-Werror=return-type",
]
cflags_c = [
"-Werror=implicit-function-declaration",
"-Wstrict-prototypes",
"-Wwrite-strings",
]
cflags_cc = [
"-Wconversion",
"-Wno-sign-conversion",
]
if (is_gcc) {
cflags += [
"-Wno-nonnull-compare",
"-Wno-format-truncation",
]
} else {
cflags += [
"-Wno-address-of-packed-member",
"-Wimplicit-fallthrough",
]
}
}
config_group("icf") {
# This changes C/C++ semantics and might be incompatible with third-party
# code that relies on function pointers comparison.
ldflags = [ "-Wl,--icf=all" ]
}
config_group("exceptions") {
cflags_cc = [ "-fexceptions" ]
cflags_objcc = cflags_cc
}
config_group("no_exceptions") {
cflags_cc = [ "-fno-exceptions" ]
cflags_objcc = cflags_cc
}
config_group("rtti") {
cflags_cc = [ "-frtti" ]
cflags_objcc = cflags_cc
}
config_group("no_rtti") {
cflags_cc = [ "-fno-rtti" ]
cflags_objcc = cflags_cc
}
config_group("default_include_dirs") {
include_dirs = [
"$zx/system/public",
"$zx/system/private",
]
}
config_group("default_frame_pointers") {
if (opt_level < 2) {
public_deps = [
":frame_pointers",
]
} else {
public_deps = [
":no_frame_pointers",
]
}
}
config_group("frame_pointers") {
defines = [ "WITH_FRAME_POINTERS=1" ]
cflags = [ "-fno-omit-frame-pointer" ]
}
config_group("no_frame_pointers") {
defines = [ "WITH_FRAME_POINTERS=0" ]
cflags = [ "-fomit-frame-pointer" ]
}
config_group("thread_safety_annotations") {
if (!is_gcc) {
cflags = [ "-Wthread-safety" ]
defines = [ "_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS=1" ]
}
}
# Compile code appropriately to be linked into a shared library.
config_group("shared_library_config") {
if (current_os != "mac") {
cflags = [ "-fPIC" ]
# Assembly code can use `#ifdef __PIC__`.
asmflags = cflags
}
}
# Don't allow dangling undefined references in shared libraries.
# All references should be satisfied by link-time library dependencies.
config_group("no_undefined_symbols") {
if (current_os == "mac") {
ldflags = [ "-Wl,-undefined,error" ]
} else {
ldflags = [ "-Wl,-z,defs" ]
}
}