1# Copyright 2020 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15import("//build_overrides/pigweed.gni") 16import("//build_overrides/pigweed_environment.gni") 17 18import("toolchains.gni") 19 20config("coverage") { 21 cflags = [ 22 "-fprofile-instr-generate", 23 "-fcoverage-mapping", 24 ] 25 26 if (pw_toolchain_PROFILE_SOURCE_FILES != []) { 27 _profile_source_files = [] 28 foreach(file, pw_toolchain_PROFILE_SOURCE_FILES) { 29 file = rebase_path(file, root_build_dir) 30 file = string_replace(file, "/", "\/") 31 file = string_replace(file, ".", "\.") 32 _profile_source_files += [ "src:$file" ] 33 } 34 35 _profile_file = "$root_build_dir/profile-source-files.list" 36 write_file(_profile_file, _profile_source_files) 37 cflags += [ "-fprofile-list=" + rebase_path(_profile_file, root_build_dir) ] 38 } 39 40 ldflags = cflags 41} 42 43# See https://github.com/google/sanitizers 44config("sanitize_address") { 45 cflags = [ "-fsanitize=address" ] 46 ldflags = cflags 47} 48 49# This is a deprecated config, use "coverage" config instead. 50config("sanitize_coverage") { 51 configs = [ ":coverage" ] 52} 53 54config("sanitize_memory") { 55 cflags = [ 56 "-fsanitize=memory", 57 58 # Do not optimizes tail recursive calls to get better call stack. 59 "-fno-optimize-sibling-calls", 60 61 # Enable check after destruction detection. 62 "-fsanitize-memory-use-after-dtor", 63 ] 64 ldflags = cflags 65} 66 67config("sanitize_undefined") { 68 cflags = [ 69 "-fsanitize=undefined", 70 71 # Store the stack frame pointer in a register to get proper debug 72 # information. 73 "-fno-omit-frame-pointer", 74 75 # Exit the program on check failure. (The default is to continue execution, 76 # which prevents test frameworks from realizing the test has failed.) 77 "-fno-sanitize-recover=undefined", 78 ] 79 ldflags = cflags 80} 81 82# UBsan configuration that enables additional checks. These checks are 83# heuristic and may not correspond to undefined behavior. 84config("sanitize_undefined_heuristic") { 85 sanitizers = [ 86 # Base checks for undefined behaviour. 87 "undefined", 88 89 # Checks for undefined or suspicious integer behavior. 90 "integer", 91 92 # Checks for floating point division by zero. 93 "float-divide-by-zero", 94 95 # Checks for suspicious behavior of implicit conversions. 96 "implicit-conversion", 97 98 # Checks for null as function arg, lvalue and return type. 99 "nullability", 100 ] 101 cflags = [ 102 "-fsanitize=" + string_join(",", sanitizers), 103 104 # Store the stack frame pointer in a register to get proper debug 105 # information. 106 "-fno-omit-frame-pointer", 107 ] 108 ldflags = cflags 109} 110 111config("sanitize_thread") { 112 cflags = [ "-fsanitize=thread" ] 113 ldflags = cflags 114} 115 116config("no_ms_compatibility") { 117 if (current_os == "win") { 118 cflags = [ "-fno-ms-compatibility" ] 119 ldflags = cflags 120 } 121} 122 123# Locate XCode's sysroot for Clang. 124config("xcode_sysroot") { 125 if (current_os == "mac") { 126 _xcode_sysroot = exec_script("$dir_pw_build/py/pw_build/exec.py", 127 [ 128 "--", 129 "/usr/bin/xcrun", 130 "--show-sdk-path", 131 ], 132 "trim string") 133 cflags = [ "--sysroot=$_xcode_sysroot" ] 134 ldflags = cflags 135 } 136} 137 138config("linux_sysroot") { 139 if (current_os == "linux" && defined(pw_env_setup_CIPD_PIGWEED)) { 140 cflags = [ "--sysroot=" + 141 rebase_path(pw_env_setup_CIPD_PIGWEED, root_build_dir) + 142 "/clang_sysroot/" ] 143 ldflags = cflags 144 } 145} 146 147# The CIPD provided Clang/LLVM toolchain must link against the matched 148# libc++ which is also from CIPD. However, by default, Clang on Mac (but 149# not on Linux) will fall back to the system libc++, which is 150# incompatible due to an ABI change. 151# 152# Pull the appropriate paths from our Pigweed env setup. 153config("no_system_libcpp") { 154 if (current_os == "mac" && defined(pw_env_setup_CIPD_PIGWEED)) { 155 install_dir = pw_env_setup_CIPD_PIGWEED 156 assert(install_dir != "", 157 "You forgot to activate the Pigweed environment; " + 158 "did you source pw_env_setup/setup.sh?") 159 ldflags = [ 160 # Force dropping the system libc++ 161 "-nostdlib++", 162 163 # Use the libc++ from CIPD. 164 rebase_path(pw_env_setup_CIPD_PIGWEED + "/lib/libc++.a", root_build_dir), 165 ] 166 } 167} 168