Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 1 | # Copyright (C) 2017 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://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, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Primiano Tucci | 69132a1 | 2020-02-07 22:33:06 +0000 | [diff] [blame] | 15 | import("//gn/perfetto_check_build_deps.gni") |
Primiano Tucci | 7a40e4d | 2017-12-06 09:51:09 +0000 | [diff] [blame] | 16 | import("//gn/standalone/android.gni") |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 17 | import("//gn/standalone/libc++/libc++.gni") |
Primiano Tucci | 7a40e4d | 2017-12-06 09:51:09 +0000 | [diff] [blame] | 18 | import("//gn/standalone/sanitizers/sanitizers.gni") |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 19 | import("//gn/standalone/toolchain/msvc.gni") |
Primiano Tucci | 8b3feb4 | 2019-07-04 03:17:31 +0100 | [diff] [blame] | 20 | import("//gn/standalone/wasm.gni") |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 21 | |
Primiano Tucci | fbcfeeb | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 22 | # These warnings have been introduced with the newest version of clang (only in |
| 23 | # the hermetic build) and are enabled just with -Werror. |
Florian Mayer | 39e40d4 | 2020-07-16 14:52:07 +0200 | [diff] [blame] | 24 | hermetic_clang_suppressions = [ "-Wno-c99-designator" ] |
Primiano Tucci | fbcfeeb | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 25 | |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 26 | # We deal with three toolchains here: |
| 27 | # 1. Clang, used in most cases. |
| 28 | # 2. GCC, used in some Linux cases. |
| 29 | # 3. MSVC, used in some Windows cases. |
| 30 | # Clang vs gcc is typically not a problem: both support roughly the same |
| 31 | # switches. -Wno-unknown-warning-option fixes the mismatching ones. |
| 32 | # The situation on Windows is a bit trickier: clang-cl.exe really pretends to be |
| 33 | # cl.exe (MSVC), so we should use MSVC-style switches (e.g. /W2). However, |
| 34 | # clang-cl.exe still supports some -Wclang-style-switches for flags that don't |
| 35 | # have a corresponding version in MSVC. |
| 36 | # |
| 37 | # In the rules below, the conditionals should be interpreted as follows: |
| 38 | # is_win -> can be either clang-cl.exe or cl.exe (MSVC). Only MSVC-style |
| 39 | # switches (the common denominator) should be used. |
| 40 | # is_clang -> could be clang-on-linux, clang-on-mac or clang-cl.exe. |
| 41 | |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 42 | config("extra_warnings") { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 43 | if (is_win) { |
| 44 | cflags = [ |
| 45 | "/W2", |
| 46 | "/wd4244", # conversion from 'float' to 'int', possible loss of data |
| 47 | "/wd4267", # conversion from 'size_t' to 'int', possible loss of data |
| 48 | ] |
| 49 | if (is_clang) { |
| 50 | cflags += [ |
| 51 | "-Wno-float-equal", |
| 52 | "-Wno-unused-macros", |
| 53 | "-Wno-old-style-cast", |
| 54 | ] |
| 55 | } |
| 56 | } else { |
| 57 | # Clang or Gcc. On linux, Android and Mac. |
| 58 | cflags = [ |
| 59 | "-Wall", |
| 60 | "-Wextra", |
| 61 | "-Wpedantic", |
| 62 | ] |
| 63 | } |
Primiano Tucci | 8a5b9a8 | 2018-12-28 13:18:21 +0100 | [diff] [blame] | 64 | |
Lalit Maganti | b4eaf21 | 2020-01-16 18:34:41 +0000 | [diff] [blame] | 65 | # Disable variadic macro warning as we make extensive use of them in trace |
| 66 | # processor and client API. |
| 67 | if (is_clang) { |
Lalit Maganti | cf684cb | 2024-02-14 19:00:55 +0000 | [diff] [blame] | 68 | # Only enable -Weverything on hermetic clang as system clang might be quite |
| 69 | # out of date. |
| 70 | if (is_hermetic_clang && current_toolchain == host_toolchain && |
| 71 | !is_fuzzer) { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 72 | # Disable Weverything on fuzzers to avoid breakages when new versions of |
| 73 | # clang are rolled into OSS-fuzz. |
| 74 | cflags += [ "-Weverything" ] |
| 75 | } |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 76 | cflags += [ |
Sami Kyostila | 3a946ae | 2017-09-29 14:56:36 +0100 | [diff] [blame] | 77 | "-Wno-c++98-compat-pedantic", |
Primiano Tucci | 1333134 | 2017-10-25 17:08:13 +0100 | [diff] [blame] | 78 | "-Wno-c++98-compat", |
Primiano Tucci | 4bdc4c4 | 2018-05-10 15:52:33 +0100 | [diff] [blame] | 79 | "-Wno-disabled-macro-expansion", |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 80 | "-Wno-documentation-unknown-command", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 81 | "-Wno-gnu-include-next", |
| 82 | "-Wno-gnu-statement-expression", |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 83 | "-Wno-gnu-zero-variadic-macro-arguments", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 84 | "-Wno-padded", |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 85 | "-Wno-poison-system-directories", |
Primiano Tucci | 6c75b64 | 2024-03-14 15:21:05 +0000 | [diff] [blame] | 86 | "-Wno-pre-c11-compat", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 87 | "-Wno-reserved-id-macro", |
Primiano Tucci | 8652a85 | 2021-09-08 14:56:19 +0100 | [diff] [blame] | 88 | "-Wno-reserved-identifier", |
Manoj Gupta | e76dcfd | 2023-04-14 19:37:58 -0700 | [diff] [blame] | 89 | "-Wno-shadow-uncaptured-local", |
Primiano Tucci | 91c4a41 | 2019-03-20 09:49:32 +0000 | [diff] [blame] | 90 | "-Wno-unknown-sanitizers", |
Primiano Tucci | f68444a | 2020-08-05 11:53:39 +0200 | [diff] [blame] | 91 | "-Wno-unknown-warning-option", |
Anna Mayzner | 8e211f0 | 2023-02-06 16:59:12 +0000 | [diff] [blame] | 92 | "-Wno-unsafe-buffer-usage", |
Primiano Tucci | 6f07702 | 2024-02-15 18:19:16 +0000 | [diff] [blame] | 93 | |
| 94 | # TODO(primiano): -Wswitch-default could be useful but will require a mass |
| 95 | # codebase cleanup. |
| 96 | "-Wno-switch-default", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 97 | ] |
Kirill Timofeev | 6dd9c8b | 2024-11-01 00:41:20 +0000 | [diff] [blame] | 98 | |
| 99 | if (perfetto_thread_safety_annotations) { |
| 100 | cflags += [ |
| 101 | "-Wthread-safety", |
| 102 | "-Wno-thread-safety-negative", |
| 103 | ] |
| 104 | } |
Ivan Chong | 88f5bd2 | 2024-06-26 14:48:26 +0000 | [diff] [blame] | 105 | } else if (is_gcc) { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 106 | # Use return std::move(...) for compatibility with old GCC compilers. |
Daniele Di Proietto | 3b5b77c | 2022-06-20 11:05:17 +0100 | [diff] [blame] | 107 | cflags_cc = [ "-Wno-redundant-move" ] |
Lalit Maganti | 8e6f9f2 | 2022-11-03 00:56:16 +0000 | [diff] [blame] | 108 | |
| 109 | # Use after free detection in GCC is still not good enough: it still fails |
| 110 | # on very obvious false-positives in trace processor. |
Daniele Di Proietto | 3b5b77c | 2022-06-20 11:05:17 +0100 | [diff] [blame] | 111 | cflags_cc += [ "-Wno-use-after-free" ] |
Lalit Maganti | 03f5a97 | 2023-03-27 17:08:15 +0100 | [diff] [blame] | 112 | |
| 113 | # GCC 7's handling of uninitialized std::optional is flaky at best and |
| 114 | # causes many false positives. |
| 115 | # TODO(lalitm): remove this when we upgrade to a GCC version which is good |
| 116 | # enough to handle this. |
| 117 | cflags_cc += [ "-Wno-maybe-uninitialized" ] |
Lalit Maganti | af7ad1a | 2023-04-26 12:56:46 +0100 | [diff] [blame] | 118 | |
| 119 | # GCC's handling of detecting infinite recursion is flaky at best and |
| 120 | # causes some false positives. |
| 121 | # TODO(lalitm): remove this when we upgrade to a GCC version which is good |
| 122 | # enough to handle this. |
| 123 | cflags_cc += [ "-Wno-infinite-recursion" ] |
| 124 | |
| 125 | # GCC's handling of detecting non null arguments is flaky at best and |
| 126 | # causes some false positives. |
| 127 | # TODO(lalitm): remove this when we upgrade to a GCC version which is good |
| 128 | # enough to handle this. |
| 129 | cflags_cc += [ "-Wno-nonnull" ] |
Sami Kyostila | 2b626d9 | 2020-04-28 12:16:29 +0100 | [diff] [blame] | 130 | } |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 133 | config("no_exceptions") { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 134 | # Exceptions are disabled by default on Windows (Use /EHsc to enable them). |
| 135 | if (!is_win) { |
| 136 | cflags_cc = [ "-fno-exceptions" ] |
| 137 | } |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | config("no_rtti") { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 141 | if (is_win) { |
| 142 | cflags_cc = [ "/GR-" ] |
| 143 | } else { |
| 144 | cflags_cc = [ "-fno-rtti" ] |
| 145 | } |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Ryan Savitski | e65c405 | 2022-03-24 18:22:19 +0000 | [diff] [blame] | 148 | # Used in buildtools dependencies for standalone builds. |
Florian Mayer | 475bd7e | 2018-08-07 20:04:03 +0100 | [diff] [blame] | 149 | config("c++17") { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 150 | if (is_win) { |
| 151 | cflags_cc = [ "/std:c++17" ] |
| 152 | } else { |
| 153 | cflags_cc = [ "-std=c++17" ] |
| 154 | } |
Florian Mayer | 475bd7e | 2018-08-07 20:04:03 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Lalit Maganti | bdddf76 | 2022-11-02 20:23:03 +0000 | [diff] [blame] | 157 | # Used in buildtools dependencies for standalone builds. |
| 158 | config("c++20") { |
| 159 | visibility = [ "//buildtools:libc++config" ] |
| 160 | if (is_win) { |
| 161 | cflags_cc = [ "/std:c++20" ] |
| 162 | } else { |
| 163 | cflags_cc = [ "-std=c++20" ] |
| 164 | } |
| 165 | } |
| 166 | |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 167 | config("visibility_hidden") { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 168 | if (!is_win) { |
| 169 | cflags = [ "-fvisibility=hidden" ] |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | config("win32_lean_and_mean") { |
| 174 | if (is_win) { |
| 175 | defines = [ "WIN32_LEAN_AND_MEAN" ] |
| 176 | } |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 179 | config("default") { |
| 180 | asmflags = [] |
| 181 | cflags = [] |
| 182 | cflags_c = [] |
| 183 | cflags_cc = [] |
| 184 | defines = [] |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 185 | include_dirs = [] |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 186 | ldflags = [] |
| 187 | libs = [] |
| 188 | |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 189 | if ((is_android || is_linux) && !is_wasm) { |
Primiano Tucci | 0b3de74 | 2023-11-09 22:55:33 +0000 | [diff] [blame] | 190 | ldflags += [ |
| 191 | "-Wl,--build-id", |
| 192 | "-Wl,-z,max-page-size=16384", |
| 193 | ] |
Florian Mayer | 5d016a5 | 2020-09-14 21:06:36 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 196 | if (is_clang || !is_win) { # Clang or GCC, but not MSVC. |
| 197 | cflags += [ |
| 198 | "-fstrict-aliasing", |
| 199 | "-Wformat", |
| 200 | ] |
| 201 | } |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 202 | |
Daniele Di Proietto | b9a246e | 2022-09-05 10:59:55 +0100 | [diff] [blame] | 203 | if (is_clang && is_win) { |
| 204 | # clang-cl from version 16 does not like out-of-line definition of static |
| 205 | # constexpr, even in C++14 mode. Disable the deprecated warnings to work |
| 206 | # around the problem. |
| 207 | cflags += [ "-Wno-deprecated" ] |
| 208 | } |
| 209 | |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 210 | if (is_win) { |
| 211 | cflags += [ |
| 212 | "/bigobj", # Some of our files are bigger than the regular limits. |
| 213 | "/Gy", # Enable function-level linking. |
Primiano Tucci | 5f3008e | 2021-05-19 21:34:45 +0100 | [diff] [blame] | 214 | "/FS", # Preserve previous PDB behavior. |
| 215 | "/utf-8", # Assume UTF-8 by default to avoid code page dependencies. |
Lalit Maganti | 03d178e | 2022-11-03 16:18:51 +0000 | [diff] [blame] | 216 | "/Zc:__cplusplus", # Allow use of __cplusplus macro. |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 217 | ] |
| 218 | defines += [ |
| 219 | "_CRT_NONSTDC_NO_WARNINGS", |
| 220 | "_CRT_SECURE_NO_DEPRECATE", |
| 221 | "_CRT_SECURE_NO_WARNINGS", # Disables warnings on some POSIX-compat API. |
| 222 | "_SCL_SECURE_NO_DEPRECATE", |
| 223 | "NOMINMAX", |
| 224 | ] |
| 225 | if (!use_custom_libcxx) { |
| 226 | defines += [ "_HAS_EXCEPTIONS=0" ] # Disables exceptions in MSVC STL. |
| 227 | } |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 228 | } else if (!is_wasm) { # !is_win |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 229 | cflags += [ |
| 230 | "-g", |
| 231 | "-fPIC", |
| 232 | "-fstack-protector-strong", |
| 233 | ] |
| 234 | } |
| 235 | |
| 236 | # Treat warnings as errors, but give up on fuzzer builds. |
Florian Mayer | 7010f3a | 2019-09-09 10:48:12 +0100 | [diff] [blame] | 237 | if (!is_fuzzer) { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 238 | if (is_win) { |
| 239 | cflags += [ "/WX" ] |
| 240 | } else { |
| 241 | cflags += [ "-Werror" ] |
| 242 | } |
Florian Mayer | 7010f3a | 2019-09-09 10:48:12 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Hector Dearman | 32c32ad | 2017-10-18 11:53:32 +0100 | [diff] [blame] | 245 | if (is_clang) { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 246 | cflags += [ "-fcolor-diagnostics" ] |
| 247 | if (!is_win) { |
| 248 | cflags += [ "-fdiagnostics-show-template-tree" ] |
| 249 | } |
Hector Dearman | 32c32ad | 2017-10-18 11:53:32 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Primiano Tucci | fbcfeeb | 2020-01-28 00:46:43 +0000 | [diff] [blame] | 252 | if (is_hermetic_clang && is_linux && !is_wasm) { |
| 253 | cflags += hermetic_clang_suppressions |
| 254 | } else { |
| 255 | not_needed([ "hermetic_clang_suppressions" ]) |
| 256 | } |
| 257 | |
Lalit Maganti | 735d186 | 2022-11-07 11:56:17 +0000 | [diff] [blame] | 258 | if (non_hermetic_clang_stdlib != "") { |
| 259 | if (is_clang && !is_hermetic_clang && !is_wasm) { |
| 260 | cflags_cc += [ "-stdlib=" + non_hermetic_clang_stdlib ] |
| 261 | ldflags += [ "-stdlib=" + non_hermetic_clang_stdlib ] |
| 262 | } |
Lalit Maganti | 4e1484b | 2022-11-03 13:33:49 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Primiano Tucci | 8c3f40a | 2018-09-01 08:31:50 +0200 | [diff] [blame] | 265 | if (is_lto) { |
| 266 | cflags += [ "-flto=full" ] |
| 267 | ldflags += [ "-flto=full" ] |
| 268 | } |
| 269 | |
Primiano Tucci | 497c3ed | 2021-05-24 12:24:39 +0100 | [diff] [blame] | 270 | if (is_win) { |
| 271 | # We support only x86/x64 builds on Windows. |
| 272 | assert(current_cpu == "x64" || current_cpu == "x86") |
| 273 | } else if (current_cpu == "arm") { |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 274 | cflags += [ |
| 275 | "-march=armv7-a", |
| 276 | "-mfpu=neon", |
| 277 | "-mthumb", |
| 278 | ] |
Samuel Holland | 62a3147 | 2023-05-18 14:39:50 -0500 | [diff] [blame] | 279 | } else if (current_cpu == "riscv64") { |
| 280 | if (!is_clang) { |
| 281 | # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104338 |
Lalit Maganti | c5b13b3 | 2023-06-30 19:34:30 +0100 | [diff] [blame] | 282 | libs += [ "atomic" ] |
Samuel Holland | 62a3147 | 2023-05-18 14:39:50 -0500 | [diff] [blame] | 283 | } |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 284 | } else if (current_cpu == "x86") { |
| 285 | asmflags += [ "-m32" ] |
| 286 | cflags += [ |
| 287 | "-m32", |
| 288 | "-msse2", |
| 289 | "-mfpmath=sse", |
| 290 | ] |
Primiano Tucci | 2490903 | 2024-03-05 16:45:50 +0000 | [diff] [blame] | 291 | ldflags += [ "-m32" ] |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 292 | } else if (current_cpu == "arm64") { |
| 293 | cflags += [ "-fno-omit-frame-pointer" ] |
Primiano Tucci | 2277f06 | 2021-11-08 22:24:05 +0000 | [diff] [blame] | 294 | } else if (current_cpu == "x64") { |
| 295 | cflags += [ "-fno-omit-frame-pointer" ] # For perf profiling. |
| 296 | if (enable_perfetto_x64_cpu_opt) { |
| 297 | # When updating these flags, the CheckCpuOptimizations() in utils.cc must |
| 298 | # be updated accordingly. |
| 299 | cflags += [ |
Lalit Maganti | 25a4534 | 2022-06-14 14:56:48 +0100 | [diff] [blame] | 300 | "-mbmi", |
| 301 | "-mbmi2", |
Primiano Tucci | 682a998 | 2022-05-20 17:13:28 +0100 | [diff] [blame] | 302 | "-mavx2", |
Primiano Tucci | 2277f06 | 2021-11-08 22:24:05 +0000 | [diff] [blame] | 303 | "-mpopcnt", |
| 304 | "-msse4.2", |
| 305 | ] |
| 306 | } |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 307 | } |
| 308 | |
Hector Dearman | ff9f2eb | 2023-06-12 10:00:40 +0100 | [diff] [blame] | 309 | if (is_wasm) { |
| 310 | # As of writing (2023-06-12) WASM 128bit SIMD is supported on |
| 311 | # stable Chrome, Safari, and Firefox. See: |
| 312 | # - https://webassembly.org/roadmap/ |
| 313 | # - https://emscripten.org/docs/porting/simd.html |
| 314 | cflags += [ "-msimd128" ] |
| 315 | } |
| 316 | |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 317 | if (is_linux) { |
Chinglin Yu | 1ce890e | 2022-10-06 17:54:17 +0800 | [diff] [blame] | 318 | # Enable LFS (large file support) for stat() and other syscalls. |
| 319 | cflags += [ |
| 320 | "-D_FILE_OFFSET_BITS=64", |
| 321 | "-D_LARGEFILE_SOURCE", |
| 322 | "-D_LARGEFILE64_SOURCE", |
| 323 | ] |
Primiano Tucci | 7278dea | 2017-10-31 11:50:32 +0000 | [diff] [blame] | 324 | libs += [ |
| 325 | "pthread", |
| 326 | "rt", |
| 327 | ] |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 328 | } |
| 329 | |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 330 | if (is_win && !is_clang) { |
| 331 | # When using MSVC we need to manually pass the include dirs. clang-cl.exe |
| 332 | # doesn't need them because it's smart enough to figure out the right path |
| 333 | # by querying the registry on its own. |
| 334 | include_dirs = win_msvc_inc_dirs # Defined in msvc.gni. |
| 335 | } |
| 336 | |
Lalit Maganti | 209b5fa | 2023-11-09 15:52:15 +0000 | [diff] [blame] | 337 | if (is_win) { |
| 338 | cflags += [ "/Zi" ] |
| 339 | } |
Florian Mayer | 6aba66d | 2018-02-02 10:53:28 +0000 | [diff] [blame] | 340 | if (is_debug) { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 341 | if (is_win) { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 342 | if (is_clang) { |
| 343 | # Required to see symbols in windbg when building with clang-cl.exe. |
| 344 | cflags += [ "-gcodeview-ghash" ] |
| 345 | ldflags = [ "/DEBUG:GHASH" ] |
| 346 | } |
| 347 | } else { |
| 348 | libs += [ "dl" ] |
| 349 | } |
Florian Mayer | 6aba66d | 2018-02-02 10:53:28 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 352 | if (is_android) { |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 353 | asmflags += [ "--target=$android_abi_target" ] |
| 354 | cflags += [ |
Primiano Tucci | b45bfac | 2017-11-28 14:37:48 +0000 | [diff] [blame] | 355 | "--sysroot=$android_compile_sysroot", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 356 | "-DANDROID", |
Florian Mayer | 46ef922 | 2018-08-22 14:46:01 -0700 | [diff] [blame] | 357 | "-D__ANDROID_API__=${android_api_level}", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 358 | "--target=$android_abi_target", |
| 359 | ] |
Primiano Tucci | 60571c2 | 2024-02-22 18:23:56 +0000 | [diff] [blame] | 360 | cflags_cc += [ "-isystem$android_compile_sysroot/c++/v1" ] |
Lalit Maganti | 7690522 | 2022-05-20 14:24:14 +0100 | [diff] [blame] | 361 | |
Primiano Tucci | 60571c2 | 2024-02-22 18:23:56 +0000 | [diff] [blame] | 362 | android_lib_dir = "$android_compile_sysroot/usr/lib/$android_abi_target/$android_api_level" |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 363 | ldflags += [ |
| 364 | "-Wl,-z,nocopyreloc", |
Primiano Tucci | 60571c2 | 2024-02-22 18:23:56 +0000 | [diff] [blame] | 365 | "--sysroot=$android_compile_sysroot", |
| 366 | "-B${android_lib_dir}", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 367 | "--target=$android_abi_target", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 368 | "-Wl,--no-undefined", |
| 369 | "-Wl,-z,noexecstack", |
| 370 | "-Wl,-z,relro", |
| 371 | "-Wl,-z,now", |
| 372 | "-Wl,--warn-shared-textrel", |
| 373 | "-Wl,--fatal-warnings", |
Florian Mayer | 5592685 | 2021-02-17 14:42:32 +0000 | [diff] [blame] | 374 | |
Primiano Tucci | 60571c2 | 2024-02-22 18:23:56 +0000 | [diff] [blame] | 375 | # From NDK docs: "although the option uses the name "libstdc++" for |
| 376 | # historical reasons, this is correct for libc++ as well. |
| 377 | "-static-libstdc++", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 378 | ] |
Primiano Tucci | 60571c2 | 2024-02-22 18:23:56 +0000 | [diff] [blame] | 379 | lib_dirs = [ android_lib_dir ] |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Lalit Maganti | 209b5fa | 2023-11-09 15:52:15 +0000 | [diff] [blame] | 383 | config("debug_noopt") { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 384 | cflags = [] |
| 385 | if (is_win) { |
| 386 | cflags = [ "/Od" ] |
| 387 | } else { |
| 388 | cflags = [ "-O0" ] |
| 389 | } |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 390 | if (is_android || is_linux) { |
| 391 | cflags += [ "-funwind-tables" ] |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 395 | config("release") { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 396 | # Compiler flags for release builds. |
| 397 | if (is_win) { |
| 398 | cflags = [ |
| 399 | "/O2", |
| 400 | "/Zc:inline", |
| 401 | ] |
| 402 | } else if (is_android) { |
| 403 | cflags = [ "-O2" ] |
Florian Mayer | 2e48071 | 2018-12-07 16:25:41 +0000 | [diff] [blame] | 404 | } else if (is_fuzzer) { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 405 | cflags = [ "-O1" ] |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 406 | } else { |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 407 | cflags = [ "-O3" ] |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 408 | } |
Primiano Tucci | 2f81155 | 2020-12-07 16:48:22 +0100 | [diff] [blame] | 409 | if (!is_win) { |
| 410 | cflags += [ |
| 411 | "-fdata-sections", |
| 412 | "-ffunction-sections", |
| 413 | ] |
| 414 | } |
| 415 | |
| 416 | # Linker flags for release builds. |
| 417 | if (is_win) { |
| 418 | ldflags = [ |
| 419 | "/OPT:REF", |
| 420 | "/OPT:ICF", |
| 421 | "/INCREMENTAL:NO", |
| 422 | "/FIXED:NO", |
| 423 | ] |
| 424 | } else if (is_mac) { |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 425 | ldflags = [ "-dead_strip" ] |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 426 | } else if (!is_win && !is_wasm) { |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 427 | ldflags = [ |
Primiano Tucci | 5aab758 | 2017-12-07 12:22:03 +0000 | [diff] [blame] | 428 | "-Wl,--gc-sections", |
| 429 | "-Wl,--icf=all", |
| 430 | "-Wl,-O1", |
| 431 | ] |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 432 | } |
| 433 | defines = [ "NDEBUG" ] |
| 434 | } |
| 435 | |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 436 | config("shared_library") { |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 437 | if (is_android || is_linux) { |
Primiano Tucci | 0825bc8 | 2017-09-28 18:50:23 +0100 | [diff] [blame] | 438 | ldflags = [ "-fPIC" ] |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | config("executable") { |
Primiano Tucci | 7278dea | 2017-10-31 11:50:32 +0000 | [diff] [blame] | 443 | ldflags = [] |
| 444 | |
| 445 | # Android will refuse to run executables if they aren't position independent. |
| 446 | # Instead on Linux there isn't any need and they break ASan (goo.gl/paFR6K). |
Florian Mayer | a74aaf0 | 2021-01-27 17:05:38 +0000 | [diff] [blame] | 447 | # The OSS-Fuzz provided AFL library is not PIC, so we we cannot use -fPIE |
| 448 | # for the fuzzer executables. |
| 449 | if ((is_android || is_linux) && !is_wasm && !is_fuzzer) { |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 450 | asmflags = [ "-fPIE" ] |
| 451 | cflags = [ "-fPIE" ] |
Primiano Tucci | 7278dea | 2017-10-31 11:50:32 +0000 | [diff] [blame] | 452 | ldflags += [ "-pie" ] |
| 453 | } |
| 454 | |
| 455 | # -rpath stores the path to the linked shared libraries into the binary, so |
| 456 | # that they can be launched without passing any LD_LIBRARY_PATH. It's |
| 457 | # supported only by Linux, not Android. But concretely we need this only when |
| 458 | # use_custom_libcxx=true && custom_libcxx_is_static=false, which happens only |
| 459 | # on Linux right now. |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 460 | if (is_linux && !is_wasm) { |
Primiano Tucci | 7278dea | 2017-10-31 11:50:32 +0000 | [diff] [blame] | 461 | ldflags += [ |
| 462 | "-Wl,-rpath=\$ORIGIN/.", |
| 463 | "-Wl,-rpath-link=.", |
Primiano Tucci | ae2879e | 2017-09-27 11:02:09 +0900 | [diff] [blame] | 464 | ] |
| 465 | } |
| 466 | } |
Ryan Savitski | a3da9be | 2019-01-30 17:45:53 +0000 | [diff] [blame] | 467 | |
| 468 | # This config is only added to certain leaf target types (see BUILDCONFIG.gn). |
| 469 | # This allows us to remove the config (and thus the dependency) on a per-target |
| 470 | # basis. If the config was applied to source_sets, then they would unavoidably |
| 471 | # carry the dependency onto liblog to the final target. |
| 472 | config("android_liblog") { |
| 473 | if (is_android) { |
| 474 | libs = [ "log" ] |
| 475 | } |
| 476 | } |
Primiano Tucci | 00da64a | 2019-02-22 14:51:10 +0000 | [diff] [blame] | 477 | |
Primiano Tucci | 69132a1 | 2020-02-07 22:33:06 +0000 | [diff] [blame] | 478 | # Checks that tools/install-build-deps has been run since it last changed. |
| 479 | perfetto_check_build_deps("check_build_deps") { |
| 480 | args = [] |
| 481 | } |
| 482 | |
| 483 | perfetto_check_build_deps("check_build_deps_android") { |
| 484 | args = [ "--android" ] |
| 485 | } |