xref: /aosp_15_r20/external/cronet/build/config/clang/BUILD.gn (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2013 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/buildflag_header.gni")
6import("//build/config/rust.gni")
7import("//build/toolchain/goma.gni")
8import("clang.gni")
9
10if (is_ios) {
11  import("//build/config/ios/config.gni")  # For `target_environment`
12}
13
14config("find_bad_constructs") {
15  if (clang_use_chrome_plugins) {
16    cflags = []
17
18    # The plugin is built directly into clang, so there's no need to load it
19    # dynamically.
20    cflags += [
21      "-Xclang",
22      "-add-plugin",
23      "-Xclang",
24      "find-bad-constructs",
25
26      "-Xclang",
27      "-plugin-arg-find-bad-constructs",
28      "-Xclang",
29      "raw-ref-template-as-trivial-member",
30
31      "-Xclang",
32      "-plugin-arg-find-bad-constructs",
33      "-Xclang",
34      "check-stack-allocated",
35
36      # TODO(danakj): Delete this after the next clang roll.
37      "-Xclang",
38      "-plugin-arg-find-bad-constructs",
39      "-Xclang",
40      "check-allow-auto-typedefs-better-nested",
41
42      "-Xclang",
43      "-plugin-arg-find-bad-constructs",
44      "-Xclang",
45      "check-raw-ptr-to-stack-allocated",
46
47      "-Xclang",
48      "-plugin-arg-find-bad-constructs",
49      "-Xclang",
50      "disable-check-raw-ptr-to-stack-allocated-error",
51
52      # TODO(https://crbug.com/1504043): Remove when raw_ptr check has been
53      # enabled for the dawn repo.
54      "-Xclang",
55      "-plugin-arg-find-bad-constructs",
56      "-Xclang",
57      "raw-ptr-exclude-path=/third_party/dawn/",
58    ]
59
60    if (is_linux || is_chromeos || is_android || is_fuchsia) {
61      cflags += [
62        "-Xclang",
63        "-plugin-arg-find-bad-constructs",
64        "-Xclang",
65        "check-ipc",
66      ]
67    }
68
69    if (enable_check_raw_ptr_fields) {
70      cflags += [
71        "-Xclang",
72        "-plugin-arg-find-bad-constructs",
73        "-Xclang",
74        "check-raw-ptr-fields",
75      ]
76    }
77
78    if (enable_check_raw_ref_fields) {
79      cflags += [
80        "-Xclang",
81        "-plugin-arg-find-bad-constructs",
82        "-Xclang",
83        "check-raw-ref-fields",
84      ]
85    }
86  }
87}
88
89# A plugin for incrementally applying the -Wunsafe-buffer-usage warning.
90#
91# To use the plugin, the project must specify a path as
92# `clang_unsafe_buffers_paths` in the `//.gn` file. This path points to a text
93# file that controls where the warning is checked.
94#
95# See //build/config/unsafe_buffers_paths.txt for an example file, this it the
96# file used by Chromium.
97config("unsafe_buffers") {
98  if (clang_use_chrome_plugins && clang_unsafe_buffers_paths != "" &&
99      !use_goma) {
100    cflags = [
101      "-Xclang",
102      "-add-plugin",
103      "-Xclang",
104      "unsafe-buffers",
105
106      "-Xclang",
107      "-plugin-arg-unsafe-buffers",
108      "-Xclang",
109      rebase_path(clang_unsafe_buffers_paths, root_build_dir),
110    ]
111  }
112}
113
114buildflag_header("unsafe_buffers_buildflags") {
115  header = "unsafe_buffers_buildflags.h"
116  flags = [ "UNSAFE_BUFFERS_WARNING_ENABLED=$clang_use_chrome_plugins" ]
117}
118
119# Enables some extra Clang-specific warnings. Some third-party code won't
120# compile with these so may want to remove this config.
121config("extra_warnings") {
122  cflags = [
123    "-Wheader-hygiene",
124
125    # Warns when a const char[] is converted to bool.
126    "-Wstring-conversion",
127
128    "-Wtautological-overlap-compare",
129  ]
130}
131
132group("llvm-symbolizer_data") {
133  if (is_win) {
134    data = [ "$clang_base_path/bin/llvm-symbolizer.exe" ]
135  } else {
136    data = [ "$clang_base_path/bin/llvm-symbolizer" ]
137  }
138}
139
140template("clang_lib") {
141  if (!defined(invoker.libname)) {
142    not_needed(invoker, "*")
143    config(target_name) {
144    }
145  } else {
146    config(target_name) {
147      _dir = ""
148      _libname = invoker.libname
149      _prefix = "lib"
150      _suffix = ""
151      _ext = "a"
152      if (is_win) {
153        _dir = "windows"
154        _prefix = ""
155        _ext = "lib"
156        if (current_cpu == "x64") {
157          _suffix = "-x86_64"
158        } else if (current_cpu == "x86") {
159          _suffix = "-i386"
160        } else if (current_cpu == "arm64") {
161          _suffix = "-aarch64"
162        } else {
163          assert(false)  # Unhandled cpu type
164        }
165      } else if (is_apple) {
166        _dir = "darwin"
167      } else if (is_linux || is_chromeos) {
168        if (current_cpu == "x64") {
169          _dir = "x86_64-unknown-linux-gnu"
170        } else if (current_cpu == "x86") {
171          _dir = "i386-unknown-linux-gnu"
172        } else if (current_cpu == "arm") {
173          _dir = "armv7-unknown-linux-gnueabihf"
174        } else if (current_cpu == "arm64") {
175          _dir = "aarch64-unknown-linux-gnu"
176        } else {
177          assert(false)  # Unhandled cpu type
178        }
179      } else if (is_fuchsia) {
180        if (current_cpu == "x64") {
181          _dir = "x86_64-unknown-fuchsia"
182        } else if (current_cpu == "arm64") {
183          _dir = "aarch64-unknown-fuchsia"
184        } else {
185          assert(false)  # Unhandled cpu type
186        }
187      } else if (is_android) {
188        _dir = "linux"
189        if (current_cpu == "x64") {
190          _suffix = "-x86_64-android"
191        } else if (current_cpu == "x86") {
192          _suffix = "-i686-android"
193        } else if (current_cpu == "arm") {
194          _suffix = "-arm-android"
195        } else if (current_cpu == "arm64") {
196          _suffix = "-aarch64-android"
197        } else if (current_cpu == "riscv64") {
198          _suffix = "-riscv64-android"
199        } else {
200          assert(false)  # Unhandled cpu type
201        }
202      } else {
203        assert(false)  # Unhandled target platform
204      }
205
206      _clang_lib_dir = "$clang_base_path/lib/clang/$clang_version/lib"
207      _lib_file = "${_prefix}clang_rt.${_libname}${_suffix}.${_ext}"
208      libs = [ "$_clang_lib_dir/$_dir/$_lib_file" ]
209    }
210  }
211}
212
213# Adds a dependency on the Clang runtime library clang_rt.builtins.
214clang_lib("compiler_builtins") {
215  if (!toolchain_has_rust) {
216    # Since there's no Rust in the toolchain, there's no concern that we'll use
217    # the Rust stdlib's intrinsics here.
218    #
219    # Don't define libname which makes this target do nothing.
220  } else if (is_mac) {
221    libname = "osx"
222  } else if (is_ios) {
223    if (target_environment == "simulator") {
224      libname = "iossim"
225    } else if (target_environment == "catalyst") {
226      libname = "osx"
227    } else {
228      libname = "ios"
229    }
230  } else {
231    libname = "builtins"
232  }
233}
234
235# Adds a dependency on the Clang runtime library clang_rt.profile.
236clang_lib("compiler_profile") {
237  if (!toolchain_has_rust) {
238    # This is only used when `toolchain_has_rust` to support Rust linking.
239    #
240    # Don't define libname which makes this target do nothing.
241  } else if (is_mac) {
242    libname = "profile_osx"
243  } else if (is_ios) {
244    if (target_environment == "simulator") {
245      libname = "profile_iossim"
246    } else if (target_environment == "catalyst") {
247      # We don't enable clang coverage on iOS device builds, and the library is
248      # not part of the Clang package tarball as a result.
249      #
250      # Don't define libname which makes this target do nothing.
251    } else {
252      # We don't enable clang coverage on iOS device builds, and the library is
253      # not part of the Clang package tarball as a result.
254      #
255      # Don't define libname which makes this target do nothing.
256    }
257  } else {
258    libname = "profile"
259  }
260}
261