• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 Google Inc.
2#
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6declare_args() {
7  extra_asmflags = []
8  extra_cflags = []
9  extra_cflags_c = []
10  extra_cflags_cc = []
11  extra_ldflags = []
12
13  malloc = ""
14}
15
16if (is_ios) {
17  if (is_tvos) {
18    sdk = "appletvos"
19    if (target_cpu == "x86" || target_cpu == "x64") {
20      sdk = "appletvsimulator"
21    }
22  } else {
23    sdk = "iphoneos"
24    if (target_cpu == "x86" || target_cpu == "x64") {
25      sdk = "iphonesimulator"
26    }
27  }
28  ios_sysroot = exec_script("find_ios_sysroot.py", [ sdk ], "trim string")
29}
30
31config("default") {
32  asmflags = []
33  cflags = []
34  cflags_c = []
35  cflags_cc = []
36  defines = []
37  ldflags = []
38  libs = []
39
40  if (werror) {
41    if (is_win) {
42      cflags += [ "/WX" ]
43    } else {
44      cflags += [ "-Werror" ]
45    }
46  }
47
48  if (is_win) {
49    if (is_clang && target_cpu == "arm64") {
50      cflags += [ "--target=arm64-windows" ]
51    }
52    cflags += [
53      "/bigobj",  # Some of our files are bigger than the regular limits.
54      "/utf-8",  # Set Source and Executable character sets to UTF-8.
55    ]
56    if (is_clang) {
57      cflags += [ "-fms-compatibility-version=19" ]  # 2015
58    }
59    defines += [
60      "_CRT_SECURE_NO_WARNINGS",  # Disables warnings about sscanf().
61      "_HAS_EXCEPTIONS=0",  # Disables exceptions in MSVC STL.
62      "WIN32_LEAN_AND_MEAN",
63      "NOMINMAX",
64    ]
65
66    _include_dirs = [
67      "$win_vc/Tools/MSVC/$win_toolchain_version/include",
68      "$win_sdk/Include/$win_sdk_version/shared",
69      "$win_sdk/Include/$win_sdk_version/ucrt",
70      "$win_sdk/Include/$win_sdk_version/um",
71      "$win_sdk/Include/$win_sdk_version/winrt",
72    ]
73
74    if (is_clang) {
75      foreach(dir, _include_dirs) {
76        cflags += [
77          "-imsvc",
78          dir,
79        ]
80      }
81    } else {
82      include_dirs = _include_dirs
83    }
84
85    lib_dirs = [
86      "$win_sdk/Lib/$win_sdk_version/ucrt/$target_cpu",
87      "$win_sdk/Lib/$win_sdk_version/um/$target_cpu",
88      "$win_vc/Tools/MSVC/$win_toolchain_version/lib/$target_cpu",
89    ]
90  } else {
91    cflags += [
92      "-fstrict-aliasing",
93      "-fPIC",
94    ]
95    cflags_cc += [ "-std=c++14" ]
96
97    # The main idea is to slim the exported API, but these flags also improve link time on Mac.
98    # These would make stack traces worse on Linux, so we don't just set them willy-nilly.
99    if (is_component_build || is_ios || is_mac) {
100      cflags += [ "-fvisibility=hidden" ]
101      cflags_cc += [ "-fvisibility-inlines-hidden" ]
102    }
103  }
104
105  if (current_cpu == "arm") {
106    cflags += [
107      "-march=armv7-a",
108      "-mfpu=neon",
109      "-mthumb",
110    ]
111  } else if (current_cpu == "loongson3a") {
112    asmflags += [ "-march=loongson3a" ]
113    cflags += [
114      "-march=loongson3a",
115
116      # Causes an internal compiler error.
117      "-DSKCMS_PORTABLE",
118    ]
119  } else if (current_cpu == "mips64el") {
120    asmflags += [ "-march=mips64" ]
121    cflags += [ "-march=mips64" ]
122  } else if (current_cpu == "x86" && !is_win) {
123    asmflags += [ "-m32" ]
124    cflags += [
125      "-m32",
126      "-msse2",
127      "-mfpmath=sse",
128    ]
129    ldflags += [ "-m32" ]
130  }
131
132  if (malloc != "" && !is_win) {
133    cflags += [
134      "-fno-builtin-malloc",
135      "-fno-builtin-calloc",
136      "-fno-builtin-realloc",
137      "-fno-builtin-free",
138    ]
139    libs += [ malloc ]
140  }
141
142  if (is_android) {
143    cflags += [ "--sysroot=$ndk/sysroot" ]
144    cflags_cc += [ "-isystem$ndk/sources/cxx-stl/llvm-libc++/include" ]
145    ldflags += [ "-static-libstdc++" ]
146  }
147
148  if (is_ios) {
149    _target = target_cpu
150    if (target_cpu == "arm") {
151      _target = "armv7"
152    } else if (target_cpu == "x86") {
153      _target = "i386"
154    } else if (target_cpu == "x64") {
155      _target = "x86_64"
156    }
157    asmflags += [
158      "-isysroot",
159      ios_sysroot,
160      "-arch",
161      _target,
162    ]
163    cflags += [
164      "-isysroot",
165      ios_sysroot,
166      "-arch",
167      _target,
168    ]
169    cflags_cc += [ "-stdlib=libc++" ]
170    ldflags += [
171      "-isysroot",
172      ios_sysroot,
173      "-arch",
174      _target,
175      "-stdlib=libc++",
176    ]
177    libs += [ "objc" ]
178  }
179
180  if (is_linux) {
181    libs += [ "pthread" ]
182    if (is_debug && sanitize == "") {
183      defines += [ "_GLIBCXX_DEBUG" ]
184    }
185  }
186  if (is_mac) {
187    # Disable linker warnings.  They're usually just annoyances like,
188    #   ld: warning: text-based stub file
189    #     /System/Library/Frameworks/foo.framework/foo.tbd and library file
190    #     /System/Library/Frameworks/foo.framework/foo are out of sync.
191    #     Falling back to library file for linking.
192    ldflags += [ "-Wl,-w" ]
193  }
194
195  if (sanitize != "" && sanitize != "MSVC") {
196    # You can either pass the sanitizers directly, e.g. "address,undefined",
197    # or pass one of the couple common aliases used by the bots.
198    sanitizers = sanitize
199
200    if (sanitize == "ASAN" || sanitize == "UBSAN") {
201      # ASAN implicitly runs all UBSAN checks also.
202      sanitizers = "undefined"
203      if (sanitize == "ASAN") {
204        sanitizers += ",address"
205      }
206
207      if (is_android) {
208        # TODO(mtklein): work out UBSAN link errors
209        sanitizers = "address"
210      }
211    } else if (sanitize == "TSAN") {
212      sanitizers = "thread"
213    } else if (sanitize == "MSAN") {
214      sanitizers = "memory"
215    }
216
217    _blacklist = rebase_path("../tools/xsan.blacklist")
218
219    cflags += [
220      "-fsanitize=$sanitizers",
221      "-fno-sanitize-recover=$sanitizers",
222      "-fsanitize-blacklist=$_blacklist",
223    ]
224    ldflags += [ "-fsanitize=$sanitizers" ]
225
226    if (is_win) {
227      cflags += [ "/FI$_blacklist" ]
228    } else {
229      cflags += [
230        "-include$_blacklist",
231        "-fno-omit-frame-pointer",
232      ]
233    }
234
235    if (is_linux) {
236      cflags_cc += [ "-stdlib=libc++" ]
237      ldflags += [ "-stdlib=libc++" ]
238    }
239
240    if (sanitizers == "memory") {
241      cflags += [ "-fsanitize-memory-track-origins" ]
242    }
243  }
244}
245
246config("no_exceptions") {
247  # Exceptions are disabled by default on Windows.  (Use /EHsc to enable them.)
248  if (!is_win) {
249    cflags_cc = [ "-fno-exceptions" ]
250  }
251}
252
253config("warnings") {
254  cflags = []
255  cflags_cc = []
256  cflags_objc = []
257  cflags_objcc = []
258  if (is_win) {
259    cflags += [
260      "/W3",  # Turn on lots of warnings.
261
262      # Disable a bunch of warnings:
263      "/wd4244",  # conversion from 'float' to 'int', possible loss of data
264      "/wd4267",  # conversion from 'size_t' to 'int', possible loss of data
265      "/wd4800",  # forcing value to bool 'true' or 'false' (performance
266                  # warning)
267
268      # Probably only triggers when /EHsc is enabled.
269      "/wd4291",  # no matching operator delete found;
270                  # memory will not be freed if initialization throws an
271                  # exception
272
273      # These only show up in shared builds:
274      "/wd4251",  # class 'type' needs to have dll-interface to be used by
275                  # clients of class 'type2'
276      "/wd4275",  # non dll-interface class 'base' used as base for
277                  # dll-interface class 'derived'
278    ]
279  } else {
280    cflags += [
281      "-Wall",
282      "-Wextra",
283      "-Winit-self",
284      "-Wpointer-arith",
285      "-Wsign-compare",
286      "-Wvla",
287
288      "-Wno-deprecated-declarations",
289      "-Wno-maybe-uninitialized",
290    ]
291    cflags_cc += [
292      "-Wnon-virtual-dtor",
293      "-Wno-noexcept-type",
294    ]
295  }
296
297  if (is_clang) {
298    cflags += [
299      "-fcolor-diagnostics",
300      "-Weverything",
301      "-Wno-unknown-warning-option",  # Let older Clangs ignore newer Clangs'
302                                      # warnings.
303    ]
304
305    if (target_cpu == "arm" && is_ios) {
306      # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit iOS.
307      # We're pretty sure it's actually 8-byte alignment.
308      cflags += [ "-Wno-over-aligned" ]
309    }
310    if (target_cpu == "x86" && is_android) {
311      # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit x86 Android builds.
312      # We're pretty sure it's actually 8-byte alignment.  See OverAlignedTest.cpp for more info.
313      cflags += [ "-Wno-over-aligned" ]
314    }
315
316    # Shouldn't be necessary for local builds. With distributed Windows builds, files may lose
317    # their case during copy, causing case-sensitivity mismatch on remote machines.
318    cflags += [
319      "-Wno-nonportable-include-path",
320      "-Wno-nonportable-system-include-path",
321    ]
322
323    # TODO: These would all be really great warnings to turn on.
324    cflags += [
325      "-Wno-cast-align",
326      "-Wno-cast-qual",
327      "-Wno-conversion",
328      "-Wno-disabled-macro-expansion",
329      "-Wno-documentation",
330      "-Wno-documentation-unknown-command",
331      "-Wno-double-promotion",
332      "-Wno-exit-time-destructors",  # TODO: OK outside libskia
333      "-Wno-float-equal",
334      "-Wno-format-nonliteral",
335      "-Wno-global-constructors",  # TODO: OK outside libskia
336      "-Wno-missing-prototypes",
337      "-Wno-missing-variable-declarations",
338      "-Wno-pedantic",
339      "-Wno-reserved-id-macro",
340      "-Wno-shadow",
341      "-Wno-shift-sign-overflow",
342      "-Wno-signed-enum-bitfield",
343      "-Wno-switch-enum",
344      "-Wno-undef",
345      "-Wno-unreachable-code",
346      "-Wno-unreachable-code-break",
347      "-Wno-unreachable-code-return",
348      "-Wno-unused-macros",
349      "-Wno-unused-member-function",
350      "-Wno-unused-template",
351      "-Wno-zero-as-null-pointer-constant",
352      "-Wno-thread-safety-negative",
353    ]
354    cflags_cc += [
355      "-Wno-abstract-vbase-init",
356      "-Wno-weak-vtables",
357    ]
358
359    # We are unlikely to want to fix these.
360    cflags += [
361      "-Wno-bad-function-cast",
362      "-Wno-covered-switch-default",
363      "-Wno-deprecated",
364      "-Wno-missing-noreturn",
365      "-Wno-old-style-cast",
366      "-Wno-padded",
367      "-Wno-newline-eof",
368    ]
369    cflags_cc += [
370      "-Wno-c++98-compat",
371      "-Wno-c++98-compat-pedantic",
372      "-Wno-undefined-func-template",
373      "-Wno-return-std-move-in-c++11",
374    ]
375    cflags_objc += [
376      "-Wno-direct-ivar-access",
377      "-Wno-objc-interface-ivars",
378    ]
379    cflags_objcc += [
380      "-Wno-direct-ivar-access",
381      "-Wno-objcc-interface-ivars",
382    ]
383  }
384  if (!is_win || is_clang) {
385    cflags += [ "-Wno-implicit-fallthrough" ]
386  }
387}
388config("warnings_except_public_headers") {
389  if (!is_win || is_clang) {
390    cflags = [ "-Wno-unused-parameter" ]
391  }
392}
393
394config("extra_flags") {
395  asmflags = extra_asmflags
396  cflags = extra_cflags
397  cflags_c = extra_cflags_c
398  cflags_cc = extra_cflags_cc
399  ldflags = extra_ldflags
400}
401
402config("debug_symbols") {
403  # It's annoying to wait for full debug symbols to push over
404  # to Android devices.  -gline-tables-only is a lot slimmer.
405  if (is_android) {
406    cflags = [
407      "-gline-tables-only",
408      "-funwind-tables",  # Helps make in-process backtraces fuller.
409    ]
410  } else if (is_win) {
411    cflags = [ "/Z7" ]
412    if (is_clang) {
413      cflags += [ "-gcodeview-ghash" ]
414      ldflags = [ "/DEBUG:GHASH" ]
415    } else {
416      ldflags = [ "/DEBUG:FASTLINK" ]
417    }
418  } else {
419    cflags = [ "-g" ]
420  }
421}
422
423config("no_rtti") {
424  if (sanitize != "ASAN") {  # -fsanitize=vptr requires RTTI
425    if (is_win) {
426      cflags_cc = [ "/GR-" ]
427    } else {
428      cflags_cc = [ "-fno-rtti" ]
429    }
430  }
431}
432
433config("optimize") {
434  if (is_win) {
435    cflags = [
436      "/O2",
437      "/Zc:inline",
438    ]
439    ldflags = [
440      "/OPT:ICF",
441      "/OPT:REF",
442    ]
443  } else {
444    cflags = [ "-O3" ]
445    if (is_mac || is_ios) {
446      ldflags = [ "-dead_strip" ]
447    } else {
448      cflags += [
449        "-fdata-sections",
450        "-ffunction-sections",
451      ]
452      ldflags = [ "-Wl,--gc-sections" ]
453    }
454    if (target_cpu == "wasm") {
455      # The compiler asks us to add an optimization flag to both cflags
456      # and ldflags to cut down on the local variables,
457      # for performance reasons.
458      # The "linking" step is the conversion to javascript.
459      ldflags += [ "-O3" ]
460    }
461  }
462}
463
464config("NDEBUG") {
465  defines = [ "NDEBUG" ]
466}
467
468config("msvc_rtc") {
469  defines = [ "_ALLOW_RTCc_IN_STL" ]
470  cflags = [
471    "/RTCcsu",
472    "/guard:cf",
473  ]
474  ldflags = [ "/guard:cf" ]
475}
476
477config("executable") {
478  if (is_android) {
479    ldflags = [
480      "-pie",
481      "-rdynamic",
482    ]
483  } else if (is_mac) {
484    ldflags = [ "-Wl,-rpath,@loader_path/." ]
485  } else if (is_linux) {
486    ldflags = [
487      "-rdynamic",
488      "-Wl,-rpath,\$ORIGIN",
489    ]
490  } else if (is_win) {
491    ldflags = [
492      "/SUBSYSTEM:CONSOLE",  # Quiet "no subsystem specified; CONSOLE assumed".
493      "/INCREMENTAL:NO",  # Quiet warnings about failing to incrementally link
494                          # by never trying to.
495    ]
496  }
497}
498