xref: /aosp_15_r20/external/pigweed/pw_toolchain/host_clang/toolchains.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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")
16
17import("$dir_pw_build/defaults.gni")
18import("$dir_pw_toolchain/clang_tools.gni")
19
20declare_args() {
21  # Sets the sanitizer to pass to clang. Valid values are "address", "memory",
22  # "thread", "undefined", "undefined_heuristic".
23  pw_toolchain_SANITIZERS = []
24
25  # Indicates if this toolchain supports generating coverage reports from
26  # pw_test targets.
27  #
28  # For example, the static analysis toolchains that run `clang-tidy` instead
29  # of the test binary itself cannot generate coverage reports.
30  #
31  # This is typically set by individual toolchains and not by GN args.
32  pw_toolchain_COVERAGE_ENABLED = false
33
34  # List of source files to selectively collect coverage.
35  pw_toolchain_PROFILE_SOURCE_FILES = []
36
37  # Indicates if this toolchain supports building fuzzers. This is typically
38  # set by individual toolchains and not by GN args.
39  pw_toolchain_FUZZING_ENABLED = false
40
41  # Indicates if this build is a part of OSS-Fuzz, which needs to be able to
42  # provide its own compiler and flags. This violates the build hermeticisim and
43  # should only be used for OSS-Fuzz.
44  pw_toolchain_OSS_FUZZ_ENABLED = false
45}
46
47# Specifies the tools used by host Clang toolchains.
48_host_clang_toolchain = {
49  forward_variables_from(pw_toolchain_clang_tools, "*")
50  is_host_toolchain = true
51  static_analysis = {
52    # Enable static analysis for host clang based toolchains.
53    enabled = true
54  }
55}
56
57# Common default scope shared by all host Clang toolchains.
58_defaults = {
59  # TODO: b/234888755 - amend toolchain declaration process to
60  # remove this hack.
61  default_configs = []
62  default_configs = pigweed_default_configs + [
63                      "$dir_pw_build:link_with_lld",
64                      "$dir_pw_build:extra_debugging",
65                      "$dir_pw_toolchain/host_clang:no_system_libcpp",
66                      "$dir_pw_toolchain/host_clang:xcode_sysroot",
67                      "$dir_pw_toolchain/host_clang:no_ms_compatibility",
68                    ]
69
70  # OSS-Fuzz uses -stdlib=libc++, which isn't included in the CIPD-provided
71  # Linux sysroot (it instead provides libstdc++).
72  if (!pw_toolchain_OSS_FUZZ_ENABLED) {
73    default_configs += [ "$dir_pw_toolchain/host_clang:linux_sysroot" ]
74  }
75
76  pw_build_LINK_DEPS = [ dir_pw_libc ]
77}
78
79pw_toolchain_host_clang = {
80  debug = {
81    name = "host_clang_debug"
82    forward_variables_from(_host_clang_toolchain, "*")
83    defaults = {
84      forward_variables_from(_defaults, "*")
85      default_configs += [ "$dir_pw_build:optimize_debugging" ]
86      foreach(sanitizer, pw_toolchain_SANITIZERS) {
87        default_configs +=
88            [ "$dir_pw_toolchain/host_clang:sanitize_$sanitizer" ]
89      }
90    }
91  }
92
93  speed_optimized = {
94    name = "host_clang_speed_optimized"
95    forward_variables_from(_host_clang_toolchain, "*")
96    defaults = {
97      forward_variables_from(_defaults, "*")
98      default_configs += [ "$dir_pw_build:optimize_speed" ]
99      foreach(sanitizer, pw_toolchain_SANITIZERS) {
100        default_configs +=
101            [ "$dir_pw_toolchain/host_clang:sanitize_$sanitizer" ]
102      }
103    }
104  }
105
106  size_optimized = {
107    name = "host_clang_size_optimized"
108    forward_variables_from(_host_clang_toolchain, "*")
109    defaults = {
110      forward_variables_from(_defaults, "*")
111      default_configs += [ "$dir_pw_build:optimize_size_clang" ]
112      foreach(sanitizer, pw_toolchain_SANITIZERS) {
113        default_configs +=
114            [ "$dir_pw_toolchain/host_clang:sanitize_$sanitizer" ]
115      }
116    }
117  }
118
119  fuzz = {
120    name = "host_clang_fuzz"
121    cc = ""
122    cxx = ""
123    forward_variables_from(_host_clang_toolchain,
124                           "*",
125                           [
126                             "cc",
127                             "cxx",
128                           ])
129
130    # OSS-Fuzz sets compiler paths. See
131    # google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements.
132    if (pw_toolchain_OSS_FUZZ_ENABLED) {
133      cc = getenv("CC")
134      cxx = getenv("CXX")
135    }
136    if (cc == "") {
137      cc = _host_clang_toolchain.cc
138    }
139    if (cxx == "") {
140      cxx = _host_clang_toolchain.cxx
141    }
142    defaults = {
143      forward_variables_from(_defaults, "*")
144
145      pw_toolchain_FUZZING_ENABLED = true
146      if (pw_toolchain_OSS_FUZZ_ENABLED) {
147        default_configs += [ "$dir_pw_fuzzer:oss_fuzz_instrumentation" ]
148      } else {
149        default_configs += [ "$dir_pw_fuzzer:instrumentation" ]
150      }
151
152      # Fuzz faster.
153      default_configs += [ "$dir_pw_build:optimize_speed" ]
154
155      # Default to ASan for fuzzing, which is what we typically care about.
156      if (pw_toolchain_SANITIZERS == []) {
157        pw_toolchain_SANITIZERS = [ "address" ]
158      }
159      foreach(sanitizer, pw_toolchain_SANITIZERS) {
160        default_configs +=
161            [ "$dir_pw_toolchain/host_clang:sanitize_$sanitizer" ]
162      }
163    }
164  }
165
166  asan = {
167    name = "host_clang_asan"
168    forward_variables_from(_host_clang_toolchain, "*")
169    defaults = {
170      forward_variables_from(_defaults, "*")
171
172      # Use debug mode to get proper debug information.
173      default_configs += [ "$dir_pw_build:optimize_debugging" ]
174      default_configs += [ "$dir_pw_toolchain/host_clang:sanitize_address" ]
175    }
176  }
177
178  ubsan = {
179    name = "host_clang_ubsan"
180    forward_variables_from(_host_clang_toolchain, "*")
181    defaults = {
182      forward_variables_from(_defaults, "*")
183
184      # Use debug mode to get proper debug information.
185      default_configs += [ "$dir_pw_build:optimize_debugging" ]
186      default_configs += [ "$dir_pw_toolchain/host_clang:sanitize_undefined" ]
187    }
188  }
189
190  ubsan_heuristic = {
191    name = "host_clang_ubsan_heuristic"
192    forward_variables_from(_host_clang_toolchain, "*")
193    defaults = {
194      forward_variables_from(_defaults, "*")
195
196      # Use debug mode to get proper debug information.
197      default_configs += [ "$dir_pw_build:optimize_debugging" ]
198      default_configs +=
199          [ "$dir_pw_toolchain/host_clang:sanitize_undefined_heuristic" ]
200    }
201  }
202
203  msan = {
204    name = "host_clang_msan"
205    forward_variables_from(_host_clang_toolchain, "*")
206    defaults = {
207      forward_variables_from(_defaults, "*")
208
209      # Use debug mode to get proper debug information.
210      default_configs += [ "$dir_pw_build:optimize_debugging" ]
211      default_configs += [ "$dir_pw_toolchain/host_clang:sanitize_memory" ]
212    }
213  }
214
215  tsan = {
216    name = "host_clang_tsan"
217    forward_variables_from(_host_clang_toolchain, "*")
218    defaults = {
219      forward_variables_from(_defaults, "*")
220
221      # Use debug mode to get proper debug information.
222      default_configs += [ "$dir_pw_build:optimize_debugging" ]
223      default_configs += [ "$dir_pw_toolchain/host_clang:sanitize_thread" ]
224    }
225  }
226
227  coverage = {
228    name = "host_clang_coverage"
229    forward_variables_from(_host_clang_toolchain, "*")
230    defaults = {
231      forward_variables_from(_defaults, "*")
232
233      # Use debug mode to get proper debug information.
234      default_configs += [ "$dir_pw_build:optimize_debugging" ]
235      default_configs += [ "$dir_pw_toolchain/host_clang:coverage" ]
236
237      # Enable PW toolchain arguments for coverage. This will only apply to
238      # binaries built using this toolchain.
239      pw_toolchain_COVERAGE_ENABLED = true
240    }
241  }
242}
243
244# Describes host clang toolchains.
245pw_toolchain_host_clang_list = [
246  pw_toolchain_host_clang.debug,
247  pw_toolchain_host_clang.speed_optimized,
248  pw_toolchain_host_clang.size_optimized,
249  pw_toolchain_host_clang.fuzz,
250  pw_toolchain_host_clang.asan,
251  pw_toolchain_host_clang.ubsan,
252  pw_toolchain_host_clang.ubsan_heuristic,
253  pw_toolchain_host_clang.msan,
254  pw_toolchain_host_clang.tsan,
255  pw_toolchain_host_clang.coverage,
256]
257