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