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