blob: 8874a24abe91dcecf57f9fb878030ad5475f9997 [file] [log] [blame]
Primiano Tucciae2879e2017-09-27 11:02:09 +09001# 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 Tucci69132a12020-02-07 22:33:06 +000015import("//gn/perfetto_check_build_deps.gni")
Primiano Tucci7a40e4d2017-12-06 09:51:09 +000016import("//gn/standalone/android.gni")
Primiano Tucci2f811552020-12-07 16:48:22 +010017import("//gn/standalone/libc++/libc++.gni")
Primiano Tucci7a40e4d2017-12-06 09:51:09 +000018import("//gn/standalone/sanitizers/sanitizers.gni")
Primiano Tucci2f811552020-12-07 16:48:22 +010019import("//gn/standalone/toolchain/msvc.gni")
Primiano Tucci8b3feb42019-07-04 03:17:31 +010020import("//gn/standalone/wasm.gni")
Primiano Tucciae2879e2017-09-27 11:02:09 +090021
Primiano Tuccifbcfeeb2020-01-28 00:46:43 +000022# These warnings have been introduced with the newest version of clang (only in
23# the hermetic build) and are enabled just with -Werror.
Florian Mayer39e40d42020-07-16 14:52:07 +020024hermetic_clang_suppressions = [ "-Wno-c99-designator" ]
Primiano Tuccifbcfeeb2020-01-28 00:46:43 +000025
Primiano Tucci2f811552020-12-07 16:48:22 +010026# 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 Tucciae2879e2017-09-27 11:02:09 +090042config("extra_warnings") {
Primiano Tucci2f811552020-12-07 16:48:22 +010043 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 Tucci8a5b9a82018-12-28 13:18:21 +010064
Lalit Magantib4eaf212020-01-16 18:34:41 +000065 # Disable variadic macro warning as we make extensive use of them in trace
66 # processor and client API.
67 if (is_clang) {
Primiano Tucci2f811552020-12-07 16:48:22 +010068 if (!is_fuzzer) {
69 # Disable Weverything on fuzzers to avoid breakages when new versions of
70 # clang are rolled into OSS-fuzz.
71 cflags += [ "-Weverything" ]
72 }
Primiano Tucciae2879e2017-09-27 11:02:09 +090073 cflags += [
Sami Kyostila3a946ae2017-09-29 14:56:36 +010074 "-Wno-c++98-compat-pedantic",
Primiano Tucci13331342017-10-25 17:08:13 +010075 "-Wno-c++98-compat",
Primiano Tucci4bdc4c42018-05-10 15:52:33 +010076 "-Wno-disabled-macro-expansion",
Primiano Tucci2f811552020-12-07 16:48:22 +010077 "-Wno-documentation-unknown-command",
Primiano Tucciae2879e2017-09-27 11:02:09 +090078 "-Wno-gnu-include-next",
79 "-Wno-gnu-statement-expression",
Primiano Tucci2f811552020-12-07 16:48:22 +010080 "-Wno-gnu-zero-variadic-macro-arguments",
Primiano Tucciae2879e2017-09-27 11:02:09 +090081 "-Wno-padded",
Primiano Tucci2f811552020-12-07 16:48:22 +010082 "-Wno-poison-system-directories",
Primiano Tucciae2879e2017-09-27 11:02:09 +090083 "-Wno-reserved-id-macro",
Primiano Tucci8652a852021-09-08 14:56:19 +010084 "-Wno-reserved-identifier",
Primiano Tucci91c4a412019-03-20 09:49:32 +000085 "-Wno-unknown-sanitizers",
Primiano Tuccif68444a2020-08-05 11:53:39 +020086 "-Wno-unknown-warning-option",
Primiano Tucciae2879e2017-09-27 11:02:09 +090087 ]
Primiano Tucci2f811552020-12-07 16:48:22 +010088 } else if (!is_clang && !is_win) {
89 # Use return std::move(...) for compatibility with old GCC compilers.
Sami Kyostila2b626d92020-04-28 12:16:29 +010090 cflags += [ "-Wno-redundant-move" ]
91 }
Primiano Tucciae2879e2017-09-27 11:02:09 +090092}
93
Primiano Tucci0825bc82017-09-28 18:50:23 +010094config("no_exceptions") {
Primiano Tucci2f811552020-12-07 16:48:22 +010095 # Exceptions are disabled by default on Windows (Use /EHsc to enable them).
96 if (!is_win) {
97 cflags_cc = [ "-fno-exceptions" ]
98 }
Primiano Tucci0825bc82017-09-28 18:50:23 +010099}
100
101config("no_rtti") {
Primiano Tucci2f811552020-12-07 16:48:22 +0100102 if (is_win) {
103 cflags_cc = [ "/GR-" ]
104 } else {
105 cflags_cc = [ "-fno-rtti" ]
106 }
Primiano Tucci0825bc82017-09-28 18:50:23 +0100107}
108
Florian Mayer475bd7e2018-08-07 20:04:03 +0100109config("c++11") {
Primiano Tucci2f811552020-12-07 16:48:22 +0100110 # C++11 is the default on Windows.
111 if (!is_win) {
112 cflags_cc = [ "-std=c++11" ]
Sami Kyostilaf43e6502021-01-06 14:18:49 +0000113 } else if (is_win && !is_clang) {
114 # Enable standards-conforming compiler behavior.
115 cflags_cc = [ "/permissive-" ]
Primiano Tucci2f811552020-12-07 16:48:22 +0100116 }
Florian Mayer475bd7e2018-08-07 20:04:03 +0100117}
118
Ryan Savitskie65c4052022-03-24 18:22:19 +0000119# Used in buildtools dependencies for standalone builds.
120config("c++14") {
121 if (is_win) {
122 cflags_cc = [ "/std:c++14" ]
123 } else {
124 cflags_cc = [ "-std=c++14" ]
125 }
126}
127
128# Used in buildtools dependencies for standalone builds.
Florian Mayer475bd7e2018-08-07 20:04:03 +0100129config("c++17") {
Primiano Tucci2f811552020-12-07 16:48:22 +0100130 if (is_win) {
131 cflags_cc = [ "/std:c++17" ]
132 } else {
133 cflags_cc = [ "-std=c++17" ]
134 }
Florian Mayer475bd7e2018-08-07 20:04:03 +0100135}
136
Primiano Tucci5aab7582017-12-07 12:22:03 +0000137config("visibility_hidden") {
Primiano Tucci2f811552020-12-07 16:48:22 +0100138 if (!is_win) {
139 cflags = [ "-fvisibility=hidden" ]
140 }
141}
142
143config("win32_lean_and_mean") {
144 if (is_win) {
145 defines = [ "WIN32_LEAN_AND_MEAN" ]
146 }
Primiano Tucci5aab7582017-12-07 12:22:03 +0000147}
148
Primiano Tucciae2879e2017-09-27 11:02:09 +0900149config("default") {
150 asmflags = []
151 cflags = []
152 cflags_c = []
153 cflags_cc = []
154 defines = []
Primiano Tucci2f811552020-12-07 16:48:22 +0100155 include_dirs = []
Primiano Tucciae2879e2017-09-27 11:02:09 +0900156 ldflags = []
157 libs = []
158
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100159 if ((is_android || is_linux) && !is_wasm) {
Florian Mayer5d016a52020-09-14 21:06:36 +0100160 ldflags += [ "-Wl,--build-id" ]
161 }
162
Primiano Tucci2f811552020-12-07 16:48:22 +0100163 if (is_clang || !is_win) { # Clang or GCC, but not MSVC.
164 cflags += [
165 "-fstrict-aliasing",
166 "-Wformat",
167 ]
168 }
Primiano Tucciae2879e2017-09-27 11:02:09 +0900169
Daniele Di Proiettob9a246e2022-09-05 10:59:55 +0100170 if (is_clang && is_win) {
171 # clang-cl from version 16 does not like out-of-line definition of static
172 # constexpr, even in C++14 mode. Disable the deprecated warnings to work
173 # around the problem.
174 cflags += [ "-Wno-deprecated" ]
175 }
176
Primiano Tucci2f811552020-12-07 16:48:22 +0100177 if (is_win) {
178 cflags += [
179 "/bigobj", # Some of our files are bigger than the regular limits.
180 "/Gy", # Enable function-level linking.
Primiano Tucci5f3008e2021-05-19 21:34:45 +0100181 "/FS", # Preserve previous PDB behavior.
182 "/utf-8", # Assume UTF-8 by default to avoid code page dependencies.
Primiano Tucci2f811552020-12-07 16:48:22 +0100183 ]
184 defines += [
185 "_CRT_NONSTDC_NO_WARNINGS",
186 "_CRT_SECURE_NO_DEPRECATE",
187 "_CRT_SECURE_NO_WARNINGS", # Disables warnings on some POSIX-compat API.
188 "_SCL_SECURE_NO_DEPRECATE",
189 "NOMINMAX",
190 ]
191 if (!use_custom_libcxx) {
192 defines += [ "_HAS_EXCEPTIONS=0" ] # Disables exceptions in MSVC STL.
193 }
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100194 } else if (!is_wasm) { # !is_win
Primiano Tucci2f811552020-12-07 16:48:22 +0100195 cflags += [
196 "-g",
197 "-fPIC",
198 "-fstack-protector-strong",
199 ]
200 }
201
202 # Treat warnings as errors, but give up on fuzzer builds.
Florian Mayer7010f3a2019-09-09 10:48:12 +0100203 if (!is_fuzzer) {
Primiano Tucci2f811552020-12-07 16:48:22 +0100204 if (is_win) {
205 cflags += [ "/WX" ]
206 } else {
207 cflags += [ "-Werror" ]
208 }
Florian Mayer7010f3a2019-09-09 10:48:12 +0100209 }
210
Hector Dearman32c32ad2017-10-18 11:53:32 +0100211 if (is_clang) {
Primiano Tucci2f811552020-12-07 16:48:22 +0100212 cflags += [ "-fcolor-diagnostics" ]
213 if (!is_win) {
214 cflags += [ "-fdiagnostics-show-template-tree" ]
215 }
Hector Dearman32c32ad2017-10-18 11:53:32 +0100216 }
217
Primiano Tuccifbcfeeb2020-01-28 00:46:43 +0000218 if (is_hermetic_clang && is_linux && !is_wasm) {
219 cflags += hermetic_clang_suppressions
220 } else {
221 not_needed([ "hermetic_clang_suppressions" ])
222 }
223
Primiano Tucci8c3f40a2018-09-01 08:31:50 +0200224 if (is_lto) {
225 cflags += [ "-flto=full" ]
226 ldflags += [ "-flto=full" ]
227 }
228
Primiano Tucci497c3ed2021-05-24 12:24:39 +0100229 if (is_win) {
230 # We support only x86/x64 builds on Windows.
231 assert(current_cpu == "x64" || current_cpu == "x86")
232 } else if (current_cpu == "arm") {
Primiano Tucciae2879e2017-09-27 11:02:09 +0900233 cflags += [
234 "-march=armv7-a",
235 "-mfpu=neon",
236 "-mthumb",
237 ]
238 } else if (current_cpu == "x86") {
239 asmflags += [ "-m32" ]
240 cflags += [
241 "-m32",
242 "-msse2",
243 "-mfpmath=sse",
244 ]
Florian Mayer27985ac2019-09-13 16:56:56 +0100245 ldflags += [
246 "-m32",
247 "-lgcc",
248 ]
Primiano Tucci5aab7582017-12-07 12:22:03 +0000249 } else if (current_cpu == "arm64") {
250 cflags += [ "-fno-omit-frame-pointer" ]
Primiano Tucci2277f062021-11-08 22:24:05 +0000251 } else if (current_cpu == "x64") {
252 cflags += [ "-fno-omit-frame-pointer" ] # For perf profiling.
253 if (enable_perfetto_x64_cpu_opt) {
254 # When updating these flags, the CheckCpuOptimizations() in utils.cc must
255 # be updated accordingly.
256 cflags += [
Lalit Maganti25a45342022-06-14 14:56:48 +0100257 "-mbmi",
258 "-mbmi2",
Primiano Tucci682a9982022-05-20 17:13:28 +0100259 "-mavx2",
Primiano Tucci2277f062021-11-08 22:24:05 +0000260 "-mpopcnt",
261 "-msse4.2",
262 ]
263 }
Primiano Tucciae2879e2017-09-27 11:02:09 +0900264 }
265
266 if (is_linux) {
Primiano Tucci7278dea2017-10-31 11:50:32 +0000267 libs += [
268 "pthread",
269 "rt",
270 ]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900271 }
272
Primiano Tucci2f811552020-12-07 16:48:22 +0100273 if (is_win && !is_clang) {
274 # When using MSVC we need to manually pass the include dirs. clang-cl.exe
275 # doesn't need them because it's smart enough to figure out the right path
276 # by querying the registry on its own.
277 include_dirs = win_msvc_inc_dirs # Defined in msvc.gni.
278 }
279
Florian Mayer6aba66d2018-02-02 10:53:28 +0000280 if (is_debug) {
Primiano Tucci2f811552020-12-07 16:48:22 +0100281 if (is_win) {
282 cflags += [ "/Z7" ]
283 if (is_clang) {
284 # Required to see symbols in windbg when building with clang-cl.exe.
285 cflags += [ "-gcodeview-ghash" ]
286 ldflags = [ "/DEBUG:GHASH" ]
287 }
288 } else {
289 libs += [ "dl" ]
290 }
Florian Mayer6aba66d2018-02-02 10:53:28 +0000291 }
292
Primiano Tucci0825bc82017-09-28 18:50:23 +0100293 if (is_android) {
Primiano Tucciae2879e2017-09-27 11:02:09 +0900294 asmflags += [ "--target=$android_abi_target" ]
295 cflags += [
Primiano Tuccib45bfac2017-11-28 14:37:48 +0000296 "--sysroot=$android_compile_sysroot",
Primiano Tucciae2879e2017-09-27 11:02:09 +0900297 "-DANDROID",
Florian Mayer46ef9222018-08-22 14:46:01 -0700298 "-D__ANDROID_API__=${android_api_level}",
Primiano Tucciae2879e2017-09-27 11:02:09 +0900299 "--target=$android_abi_target",
300 ]
301 cflags_cc += [
Lalit Maganti76905222022-05-20 14:24:14 +0100302 "-isystem$android_ndk_root/sources/cxx-stl/llvm-libc++/include",
303 "-isystem$android_ndk_root/sources/android/support/include",
304 "-isystem$android_ndk_root/sources/cxx-stl/llvm-libc++abi/include",
Primiano Tucciae2879e2017-09-27 11:02:09 +0900305 ]
Lalit Maganti76905222022-05-20 14:24:14 +0100306
307 # Keep these include paths *after* the above to prevent the wrong include
308 # being used for some headers (e.g. math.h).
309 # We need to add to each language (cflags_c, cflags_cc etc.) separately as
310 # we added to cflags directly, these inludes would end up before the
311 # languge and thus the above includes.
312 sysroot_cflags = [
313 "-isystem$android_compile_sysroot/$android_compile_sysroot_subdir",
314 "-isystem$android_compile_sysroot",
315 ]
316 cflags_c += sysroot_cflags
317 cflags_cc += sysroot_cflags
318
Primiano Tucciae2879e2017-09-27 11:02:09 +0900319 ldflags += [
320 "-Wl,-z,nocopyreloc",
321 "-gcc-toolchain",
322 "$android_toolchain_root",
Primiano Tuccib45bfac2017-11-28 14:37:48 +0000323 "--sysroot=$android_ndk_root/$android_link_sysroot_subdir",
Primiano Tucciae2879e2017-09-27 11:02:09 +0900324 "--target=$android_abi_target",
325 "-Wl,--exclude-libs,libunwind.a",
326 "-Wl,--exclude-libs,libgcc.a",
327 "-Wl,--exclude-libs,libc++_static.a",
Primiano Tucciae2879e2017-09-27 11:02:09 +0900328 "-Wl,--no-undefined",
329 "-Wl,-z,noexecstack",
330 "-Wl,-z,relro",
331 "-Wl,-z,now",
332 "-Wl,--warn-shared-textrel",
333 "-Wl,--fatal-warnings",
Florian Mayer55926852021-02-17 14:42:32 +0000334
335 # New NDKs need setting this to not give "unable to find library -lc++".
336 # See https://github.com/android/ndk/issues/951#issuecomment-501017894.
337 "-nostdlib++",
Primiano Tucciae2879e2017-09-27 11:02:09 +0900338 ]
Florian Mayer475bd7e2018-08-07 20:04:03 +0100339 lib_dirs = [
340 "$android_ndk_root/sources/cxx-stl/llvm-libc++/libs/$android_app_abi",
341 ]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900342 libs += [
343 "gcc",
344 "c++_static",
345 "c++abi",
Primiano Tucciae2879e2017-09-27 11:02:09 +0900346 ]
Florian Mayeraa5316b2018-08-20 17:45:12 -0700347
348 if (current_cpu == "arm") {
349 # New NDKs don't have libandroid_support for arm64.
350 libs += [ "android_support" ]
351 }
Primiano Tucciae2879e2017-09-27 11:02:09 +0900352 }
353}
354
355config("debug_symbols") {
Primiano Tucci2f811552020-12-07 16:48:22 +0100356 cflags = []
357 if (is_win) {
358 cflags = [ "/Od" ]
359 } else {
360 cflags = [ "-O0" ]
361 }
Primiano Tucci5aab7582017-12-07 12:22:03 +0000362 if (is_android || is_linux) {
363 cflags += [ "-funwind-tables" ]
Primiano Tucci0825bc82017-09-28 18:50:23 +0100364 }
365}
366
Primiano Tucciae2879e2017-09-27 11:02:09 +0900367config("release") {
Primiano Tucci2f811552020-12-07 16:48:22 +0100368 # Compiler flags for release builds.
369 if (is_win) {
370 cflags = [
371 "/O2",
372 "/Zc:inline",
373 ]
374 } else if (is_android) {
375 cflags = [ "-O2" ]
Florian Mayer2e480712018-12-07 16:25:41 +0000376 } else if (is_fuzzer) {
Primiano Tucci2f811552020-12-07 16:48:22 +0100377 cflags = [ "-O1" ]
Primiano Tucci5aab7582017-12-07 12:22:03 +0000378 } else {
Primiano Tucci2f811552020-12-07 16:48:22 +0100379 cflags = [ "-O3" ]
Primiano Tucci5aab7582017-12-07 12:22:03 +0000380 }
Primiano Tucci2f811552020-12-07 16:48:22 +0100381 if (!is_win) {
382 cflags += [
383 "-fdata-sections",
384 "-ffunction-sections",
385 ]
386 }
387
388 # Linker flags for release builds.
389 if (is_win) {
390 ldflags = [
391 "/OPT:REF",
392 "/OPT:ICF",
393 "/INCREMENTAL:NO",
394 "/FIXED:NO",
395 ]
396 } else if (is_mac) {
Primiano Tucciae2879e2017-09-27 11:02:09 +0900397 ldflags = [ "-dead_strip" ]
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100398 } else if (!is_win && !is_wasm) {
Primiano Tucci5aab7582017-12-07 12:22:03 +0000399 ldflags = [
Primiano Tucci5aab7582017-12-07 12:22:03 +0000400 "-Wl,--gc-sections",
401 "-Wl,--icf=all",
402 "-Wl,-O1",
403 ]
Primiano Tucciae2879e2017-09-27 11:02:09 +0900404 }
405 defines = [ "NDEBUG" ]
406}
407
Primiano Tucci0825bc82017-09-28 18:50:23 +0100408config("shared_library") {
Primiano Tucciae2879e2017-09-27 11:02:09 +0900409 if (is_android || is_linux) {
Primiano Tucci0825bc82017-09-28 18:50:23 +0100410 ldflags = [ "-fPIC" ]
411 }
412}
413
414config("executable") {
Primiano Tucci7278dea2017-10-31 11:50:32 +0000415 ldflags = []
416
417 # Android will refuse to run executables if they aren't position independent.
418 # Instead on Linux there isn't any need and they break ASan (goo.gl/paFR6K).
Florian Mayera74aaf02021-01-27 17:05:38 +0000419 # The OSS-Fuzz provided AFL library is not PIC, so we we cannot use -fPIE
420 # for the fuzzer executables.
421 if ((is_android || is_linux) && !is_wasm && !is_fuzzer) {
Primiano Tucciae2879e2017-09-27 11:02:09 +0900422 asmflags = [ "-fPIE" ]
423 cflags = [ "-fPIE" ]
Primiano Tucci7278dea2017-10-31 11:50:32 +0000424 ldflags += [ "-pie" ]
425 }
426
427 # -rpath stores the path to the linked shared libraries into the binary, so
428 # that they can be launched without passing any LD_LIBRARY_PATH. It's
429 # supported only by Linux, not Android. But concretely we need this only when
430 # use_custom_libcxx=true && custom_libcxx_is_static=false, which happens only
431 # on Linux right now.
Primiano Tucci7e9865c2021-01-10 23:55:15 +0100432 if (is_linux && !is_wasm) {
Primiano Tucci7278dea2017-10-31 11:50:32 +0000433 ldflags += [
434 "-Wl,-rpath=\$ORIGIN/.",
435 "-Wl,-rpath-link=.",
Primiano Tucciae2879e2017-09-27 11:02:09 +0900436 ]
437 }
438}
Ryan Savitskia3da9be2019-01-30 17:45:53 +0000439
440# This config is only added to certain leaf target types (see BUILDCONFIG.gn).
441# This allows us to remove the config (and thus the dependency) on a per-target
442# basis. If the config was applied to source_sets, then they would unavoidably
443# carry the dependency onto liblog to the final target.
444config("android_liblog") {
445 if (is_android) {
446 libs = [ "log" ]
447 }
448}
Primiano Tucci00da64a2019-02-22 14:51:10 +0000449
Primiano Tucci69132a12020-02-07 22:33:06 +0000450# Checks that tools/install-build-deps has been run since it last changed.
451perfetto_check_build_deps("check_build_deps") {
452 args = []
453}
454
455perfetto_check_build_deps("check_build_deps_android") {
456 args = [ "--android" ]
457}