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