• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15import("//gn/perfetto_check_build_deps.gni")
16import("//gn/standalone/android.gni")
17import("//gn/standalone/libc++/libc++.gni")
18import("//gn/standalone/sanitizers/sanitizers.gni")
19import("//gn/standalone/toolchain/msvc.gni")
20import("//gn/standalone/wasm.gni")
21
22# These warnings have been introduced with the newest version of clang (only in
23# the hermetic build) and are enabled just with -Werror.
24hermetic_clang_suppressions = [ "-Wno-c99-designator" ]
25
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
42config("extra_warnings") {
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  }
64
65  # Disable variadic macro warning as we make extensive use of them in trace
66  # processor and client API.
67  if (is_clang) {
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) {
72      # Disable Weverything on fuzzers to avoid breakages when new versions of
73      # clang are rolled into OSS-fuzz.
74      cflags += [ "-Weverything" ]
75    }
76    cflags += [
77      "-Wno-c++98-compat-pedantic",
78      "-Wno-c++98-compat",
79      "-Wno-disabled-macro-expansion",
80      "-Wno-documentation-unknown-command",
81      "-Wno-gnu-include-next",
82      "-Wno-gnu-statement-expression",
83      "-Wno-gnu-zero-variadic-macro-arguments",
84      "-Wno-padded",
85      "-Wno-poison-system-directories",
86      "-Wno-pre-c11-compat",
87      "-Wno-reserved-id-macro",
88      "-Wno-reserved-identifier",
89      "-Wno-shadow-uncaptured-local",
90      "-Wno-unknown-sanitizers",
91      "-Wno-unknown-warning-option",
92      "-Wno-unsafe-buffer-usage",
93
94      # TODO(primiano): -Wswitch-default could be useful but will require a mass
95      # codebase cleanup.
96      "-Wno-switch-default",
97    ]
98  } else if (!is_clang && !is_win) {
99    # Use return std::move(...) for compatibility with old GCC compilers.
100    cflags_cc = [ "-Wno-redundant-move" ]
101
102    # Use after free detection in GCC is still not good enough: it still fails
103    # on very obvious false-positives in trace processor.
104    cflags_cc += [ "-Wno-use-after-free" ]
105
106    # GCC 7's handling of uninitialized std::optional is flaky at best and
107    # causes many false positives.
108    # TODO(lalitm): remove this when we upgrade to a GCC version which is good
109    # enough to handle this.
110    cflags_cc += [ "-Wno-maybe-uninitialized" ]
111
112    # GCC's handling of detecting infinite recursion is flaky at best and
113    # causes some false positives.
114    # TODO(lalitm): remove this when we upgrade to a GCC version which is good
115    # enough to handle this.
116    cflags_cc += [ "-Wno-infinite-recursion" ]
117
118    # GCC's handling of detecting non null arguments is flaky at best and
119    # causes some false positives.
120    # TODO(lalitm): remove this when we upgrade to a GCC version which is good
121    # enough to handle this.
122    cflags_cc += [ "-Wno-nonnull" ]
123  }
124}
125
126config("no_exceptions") {
127  # Exceptions are disabled by default on Windows (Use /EHsc to enable them).
128  if (!is_win) {
129    cflags_cc = [ "-fno-exceptions" ]
130  }
131}
132
133config("no_rtti") {
134  if (is_win) {
135    cflags_cc = [ "/GR-" ]
136  } else {
137    cflags_cc = [ "-fno-rtti" ]
138  }
139}
140
141# Used in buildtools dependencies for standalone builds.
142config("c++17") {
143  if (is_win) {
144    cflags_cc = [ "/std:c++17" ]
145  } else {
146    cflags_cc = [ "-std=c++17" ]
147  }
148}
149
150# Used in buildtools dependencies for standalone builds.
151config("c++20") {
152  visibility = [ "//buildtools:libc++config" ]
153  if (is_win) {
154    cflags_cc = [ "/std:c++20" ]
155  } else {
156    cflags_cc = [ "-std=c++20" ]
157  }
158}
159
160config("visibility_hidden") {
161  if (!is_win) {
162    cflags = [ "-fvisibility=hidden" ]
163  }
164}
165
166config("win32_lean_and_mean") {
167  if (is_win) {
168    defines = [ "WIN32_LEAN_AND_MEAN" ]
169  }
170}
171
172config("default") {
173  asmflags = []
174  cflags = []
175  cflags_c = []
176  cflags_cc = []
177  defines = []
178  include_dirs = []
179  ldflags = []
180  libs = []
181
182  if ((is_android || is_linux) && !is_wasm) {
183    ldflags += [
184      "-Wl,--build-id",
185      "-Wl,-z,max-page-size=16384",
186    ]
187  }
188
189  if (is_clang || !is_win) {  # Clang or GCC, but not MSVC.
190    cflags += [
191      "-fstrict-aliasing",
192      "-Wformat",
193    ]
194  }
195
196  if (is_clang && is_win) {
197    # clang-cl from version 16 does not like out-of-line definition of static
198    # constexpr, even in C++14 mode. Disable the deprecated warnings to work
199    # around the problem.
200    cflags += [ "-Wno-deprecated" ]
201  }
202
203  if (is_win) {
204    cflags += [
205      "/bigobj",  # Some of our files are bigger than the regular limits.
206      "/Gy",  # Enable function-level linking.
207      "/FS",  # Preserve previous PDB behavior.
208      "/utf-8",  # Assume UTF-8 by default to avoid code page dependencies.
209      "/Zc:__cplusplus",  # Allow use of __cplusplus macro.
210    ]
211    defines += [
212      "_CRT_NONSTDC_NO_WARNINGS",
213      "_CRT_SECURE_NO_DEPRECATE",
214      "_CRT_SECURE_NO_WARNINGS",  # Disables warnings on some POSIX-compat API.
215      "_SCL_SECURE_NO_DEPRECATE",
216      "NOMINMAX",
217    ]
218    if (!use_custom_libcxx) {
219      defines += [ "_HAS_EXCEPTIONS=0" ]  # Disables exceptions in MSVC STL.
220    }
221  } else if (!is_wasm) {  # !is_win
222    cflags += [
223      "-g",
224      "-fPIC",
225      "-fstack-protector-strong",
226    ]
227  }
228
229  # Treat warnings as errors, but give up on fuzzer builds.
230  if (!is_fuzzer) {
231    if (is_win) {
232      cflags += [ "/WX" ]
233    } else {
234      cflags += [ "-Werror" ]
235    }
236  }
237
238  if (is_clang) {
239    cflags += [ "-fcolor-diagnostics" ]
240    if (!is_win) {
241      cflags += [ "-fdiagnostics-show-template-tree" ]
242    }
243  }
244
245  if (is_hermetic_clang && is_linux && !is_wasm) {
246    cflags += hermetic_clang_suppressions
247  } else {
248    not_needed([ "hermetic_clang_suppressions" ])
249  }
250
251  if (non_hermetic_clang_stdlib != "") {
252    if (is_clang && !is_hermetic_clang && !is_wasm) {
253      cflags_cc += [ "-stdlib=" + non_hermetic_clang_stdlib ]
254      ldflags += [ "-stdlib=" + non_hermetic_clang_stdlib ]
255    }
256  }
257
258  if (is_lto) {
259    cflags += [ "-flto=full" ]
260    ldflags += [ "-flto=full" ]
261  }
262
263  if (is_win) {
264    # We support only x86/x64 builds on Windows.
265    assert(current_cpu == "x64" || current_cpu == "x86")
266  } else if (current_cpu == "arm") {
267    cflags += [
268      "-march=armv7-a",
269      "-mfpu=neon",
270      "-mthumb",
271    ]
272  } else if (current_cpu == "riscv64") {
273    if (!is_clang) {
274      # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104338
275      libs += [ "atomic" ]
276    }
277  } else if (current_cpu == "x86") {
278    asmflags += [ "-m32" ]
279    cflags += [
280      "-m32",
281      "-msse2",
282      "-mfpmath=sse",
283    ]
284    ldflags += [ "-m32" ]
285  } else if (current_cpu == "arm64") {
286    cflags += [ "-fno-omit-frame-pointer" ]
287  } else if (current_cpu == "x64") {
288    cflags += [ "-fno-omit-frame-pointer" ]  # For perf profiling.
289    if (enable_perfetto_x64_cpu_opt) {
290      # When updating these flags, the CheckCpuOptimizations() in utils.cc must
291      # be updated accordingly.
292      cflags += [
293        "-mbmi",
294        "-mbmi2",
295        "-mavx2",
296        "-mpopcnt",
297        "-msse4.2",
298      ]
299    }
300  }
301
302  if (is_wasm) {
303    # As of writing (2023-06-12) WASM 128bit SIMD is supported on
304    # stable Chrome, Safari, and Firefox. See:
305    # - https://webassembly.org/roadmap/
306    # - https://emscripten.org/docs/porting/simd.html
307    cflags += [ "-msimd128" ]
308  }
309
310  if (is_linux) {
311    # Enable LFS (large file support) for stat() and other syscalls.
312    cflags += [
313      "-D_FILE_OFFSET_BITS=64",
314      "-D_LARGEFILE_SOURCE",
315      "-D_LARGEFILE64_SOURCE",
316    ]
317    libs += [
318      "pthread",
319      "rt",
320    ]
321  }
322
323  if (is_win && !is_clang) {
324    # When using MSVC we need to manually pass the include dirs. clang-cl.exe
325    # doesn't need them because it's smart enough to figure out the right path
326    # by querying the registry on its own.
327    include_dirs = win_msvc_inc_dirs  # Defined in msvc.gni.
328  }
329
330  if (is_win) {
331    cflags += [ "/Zi" ]
332  }
333  if (is_debug) {
334    if (is_win) {
335      if (is_clang) {
336        # Required to see symbols in windbg when building with clang-cl.exe.
337        cflags += [ "-gcodeview-ghash" ]
338        ldflags = [ "/DEBUG:GHASH" ]
339      }
340    } else {
341      libs += [ "dl" ]
342    }
343  }
344
345  if (is_android) {
346    asmflags += [ "--target=$android_abi_target" ]
347    cflags += [
348      "--sysroot=$android_compile_sysroot",
349      "-DANDROID",
350      "-D__ANDROID_API__=${android_api_level}",
351      "--target=$android_abi_target",
352    ]
353    cflags_cc += [ "-isystem$android_compile_sysroot/c++/v1" ]
354
355    android_lib_dir = "$android_compile_sysroot/usr/lib/$android_abi_target/$android_api_level"
356    ldflags += [
357      "-Wl,-z,nocopyreloc",
358      "--sysroot=$android_compile_sysroot",
359      "-B${android_lib_dir}",
360      "--target=$android_abi_target",
361      "-Wl,--no-undefined",
362      "-Wl,-z,noexecstack",
363      "-Wl,-z,relro",
364      "-Wl,-z,now",
365      "-Wl,--warn-shared-textrel",
366      "-Wl,--fatal-warnings",
367
368      # From NDK docs: "although the option uses the name "libstdc++" for
369      # historical reasons, this is correct for libc++ as well.
370      "-static-libstdc++",
371    ]
372    lib_dirs = [ android_lib_dir ]
373  }
374}
375
376config("debug_noopt") {
377  cflags = []
378  if (is_win) {
379    cflags = [ "/Od" ]
380  } else {
381    cflags = [ "-O0" ]
382  }
383  if (is_android || is_linux) {
384    cflags += [ "-funwind-tables" ]
385  }
386}
387
388config("release") {
389  # Compiler flags for release builds.
390  if (is_win) {
391    cflags = [
392      "/O2",
393      "/Zc:inline",
394    ]
395  } else if (is_android) {
396    cflags = [ "-O2" ]
397  } else if (is_fuzzer) {
398    cflags = [ "-O1" ]
399  } else {
400    cflags = [ "-O3" ]
401  }
402  if (!is_win) {
403    cflags += [
404      "-fdata-sections",
405      "-ffunction-sections",
406    ]
407  }
408
409  # Linker flags for release builds.
410  if (is_win) {
411    ldflags = [
412      "/OPT:REF",
413      "/OPT:ICF",
414      "/INCREMENTAL:NO",
415      "/FIXED:NO",
416    ]
417  } else if (is_mac) {
418    ldflags = [ "-dead_strip" ]
419  } else if (!is_win && !is_wasm) {
420    ldflags = [
421      "-Wl,--gc-sections",
422      "-Wl,--icf=all",
423      "-Wl,-O1",
424    ]
425  }
426  defines = [ "NDEBUG" ]
427}
428
429config("shared_library") {
430  if (is_android || is_linux) {
431    ldflags = [ "-fPIC" ]
432  }
433}
434
435config("executable") {
436  ldflags = []
437
438  # Android will refuse to run executables if they aren't position independent.
439  # Instead on Linux there isn't any need and they break ASan (goo.gl/paFR6K).
440  # The OSS-Fuzz provided AFL library is not PIC, so we we cannot use -fPIE
441  # for the fuzzer executables.
442  if ((is_android || is_linux) && !is_wasm && !is_fuzzer) {
443    asmflags = [ "-fPIE" ]
444    cflags = [ "-fPIE" ]
445    ldflags += [ "-pie" ]
446  }
447
448  # -rpath stores the path to the linked shared libraries into the binary, so
449  # that they can be launched without passing any LD_LIBRARY_PATH. It's
450  # supported only by Linux, not Android. But concretely we need this only when
451  # use_custom_libcxx=true && custom_libcxx_is_static=false, which happens only
452  # on Linux right now.
453  if (is_linux && !is_wasm) {
454    ldflags += [
455      "-Wl,-rpath=\$ORIGIN/.",
456      "-Wl,-rpath-link=.",
457    ]
458  }
459}
460
461# This config is only added to certain leaf target types (see BUILDCONFIG.gn).
462# This allows us to remove the config (and thus the dependency) on a per-target
463# basis. If the config was applied to source_sets, then they would unavoidably
464# carry the dependency onto liblog to the final target.
465config("android_liblog") {
466  if (is_android) {
467    libs = [ "log" ]
468  }
469}
470
471# Checks that tools/install-build-deps has been run since it last changed.
472perfetto_check_build_deps("check_build_deps") {
473  args = []
474}
475
476perfetto_check_build_deps("check_build_deps_android") {
477  args = [ "--android" ]
478}
479