• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
18# See https://github.com/google/sanitizers
19config("sanitize_address") {
20  cflags = [ "-fsanitize=address" ]
21  ldflags = cflags
22}
23
24config("sanitize_memory") {
25  cflags = [
26    "-fsanitize=memory",
27
28    # Do not optimizes tail recursive calls to get better call stack.
29    "-fno-optimize-sibling-calls",
30
31    # Enable check after destruction detection.
32    "-fsanitize-memory-use-after-dtor",
33  ]
34  ldflags = cflags
35}
36
37config("sanitize_undefined") {
38  cflags = [
39    "-fsanitize=undefined",
40
41    # Store the stack frame pointer in a register to get proper debug
42    # information.
43    "-fno-omit-frame-pointer",
44
45    # Exit the program on check failure. (The default is to continue execution,
46    # which prevents test frameworks from realizing the test has failed.)
47    "-fno-sanitize-recover=undefined",
48  ]
49  ldflags = cflags
50}
51
52# UBsan configuration that enables additional checks. These checks are
53# heuristic and may not correspond to undefined behavior.
54config("sanitize_undefined_heuristic") {
55  sanitizers = [
56    # Base checks for undefined behaviour.
57    "undefined",
58
59    # Checks for undefined or suspicious integer behavior.
60    "integer",
61
62    # Checks for floating point division by zero.
63    "float-divide-by-zero",
64
65    # Checks for suspicious behavior of implicit conversions.
66    "implicit-conversion",
67
68    # Checks for null as function arg, lvalue and return type.
69    "nullability",
70  ]
71  cflags = [
72    "-fsanitize=" + string_join(",", sanitizers),
73
74    # Store the stack frame pointer in a register to get proper debug
75    # information.
76    "-fno-omit-frame-pointer",
77  ]
78  ldflags = cflags
79}
80
81config("sanitize_thread") {
82  cflags = [ "-fsanitize=thread" ]
83  ldflags = cflags
84}
85
86config("sanitize_coverage") {
87  cflags = [
88    "-fprofile-instr-generate",
89    "-fcoverage-mapping",
90  ]
91  ldflags = cflags
92}
93
94# Locate XCode's sysroot for Clang.
95config("xcode_sysroot") {
96  if (current_os == "mac") {
97    _xcode_sysroot = exec_script("$dir_pw_build/py/pw_build/exec.py",
98                                 [
99                                   "--",
100                                   "/usr/bin/xcrun",
101                                   "--show-sdk-path",
102                                 ],
103                                 "trim string")
104    cflags = [ "--sysroot=$_xcode_sysroot" ]
105    ldflags = cflags
106  }
107}
108
109config("linux_sysroot") {
110  if (current_os == "linux") {
111    cflags = [ "--sysroot=" +
112               rebase_path(pw_env_setup_CIPD_PIGWEED, root_build_dir) +
113               "/clang_sysroot/" ]
114    ldflags = cflags
115  }
116}
117
118# The CIPD provided Clang/LLVM toolchain must link against the matched
119# libc++ which is also from CIPD. However, by default, Clang on Mac (but
120# not on Linux) will fall back to the system libc++, which is
121# incompatible due to an ABI change.
122#
123# Pull the appropriate paths from our Pigweed env setup.
124config("no_system_libcpp") {
125  if (current_os == "mac") {
126    install_dir = pw_env_setup_CIPD_PIGWEED
127    assert(install_dir != "",
128           "You forgot to activate the Pigweed environment; " +
129               "did you source pw_env_setup/setup.sh?")
130    ldflags = [
131      # Force dropping the system libc++
132      "-nostdlib++",
133
134      # Use the libc++ from CIPD.
135      rebase_path(pw_env_setup_CIPD_PIGWEED + "/lib/libc++.a", root_build_dir),
136    ]
137  }
138}
139