Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / build / config / compiler / BUILD.gn
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
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("//build/config/android/config.gni")
6 if (cpu_arch == "arm") {
7   import("//build/config/arm.gni")
8 }
9
10 # compiler ---------------------------------------------------------------------
11 #
12 # Base compiler configuration.
13 #
14 # See also "runtime_library" below for related stuff and a discusison about
15 # where stuff should go. Put warning related stuff in the "warnings" config.
16
17 config("compiler") {
18   cflags = []
19   cflags_c = []
20   cflags_cc = []
21   ldflags = []
22   defines = []
23   include_dirs = []
24
25   include_dirs += [ "//", root_gen_dir ]
26
27   # In general, Windows is totally different, but all the other builds share
28   # some common GCC configuration. This section sets up Windows and the common
29   # GCC flags, and then we handle the other non-Windows platforms specifically
30   # below.
31   if (is_win) {
32     # Windows compiler flags setup.
33     # -----------------------------
34     cflags += [
35       "/Gy",  # Enable function-level linking.
36       "/GS",  # Enable buffer security checking.
37       "/EHsc",  # Assume C functions can't throw exceptions and don't catch
38                 # structured exceptions (only C++ ones).
39     ]
40   } else {
41     # Common GCC compiler flags setup.
42     # --------------------------------
43     cflags += [
44       "-fno-strict-aliasing",  # See http://crbug.com/32204
45       "-fvisibility=hidden",
46     ]
47     cflags_cc += [
48       "-fno-exceptions",
49       "-fno-threadsafe-statics",
50       "-fvisibility-inlines-hidden",
51     ]
52
53     # Stack protection.
54     if (is_mac) {
55       cflags += [ "-fstack-protector-all" ]
56     } else if (is_linux) {
57       cflags += [ "-fstack-protector", "--param=ssp-buffer-size=4" ]
58     }
59   }
60
61   # Mac-specific compiler flags setup.
62   # ----------------------------------
63   if (is_mac || is_ios) {
64     # These flags are shared between the C compiler and linker.
65     common_mac_flags = []
66
67     # CPU architecture.
68     if (cpu_arch == "x64") {
69       common_mac_flags += [ "-arch x86_64" ]
70     } else if (cpu_arch == "x86") {
71       common_mac_flags += [ "-arch i386" ]
72     }
73
74     cflags += common_mac_flags
75
76     # Without this, the constructors and destructors of a C++ object inside
77     # an Objective C struct won't be called, which is very bad.
78     cflags_objcc = [ "-fobjc-call-cxx-cdtors", ]
79
80     cflags_c += [ "-std=c99" ]
81     cflags_cc += [ "-std=gnu++11" ]
82
83     ldflags += common_mac_flags + [
84       "-L.",
85
86       # TODO(brettW) I don't understand these options.
87       "-Wl,-rpath,@loader_path/.",
88       "-Wl,-rpath,@loader_path/../../..",
89     ]
90   } else if (is_posix) {
91     # Non-Mac Posix compiler flags setup.
92     # -----------------------------------
93
94     # CPU architecture. We may or may not be doing a cross compile now, so for
95     # simplicity we always explicitly set the architecture.
96     if (cpu_arch == "x64") {
97       cflags += [ "-m64" ]
98       ldflags += [ "-m64" ]
99     } else if (cpu_arch == "x86") {
100       cflags += [ "-m32" ]
101       ldflags += [ "-m32" ]
102     } else if (cpu_arch == "arm") {
103       # Don't set the compiler flags for the WebView build. These will come
104       # from the Android build system.
105       if (!is_android_webview_build) {
106         cflags += [
107           "-march=$arm_arch",
108           "-mfpu=$arm_fpu",
109           "-mfloat-abi=$arm_float_abi",
110         ]
111         if (arm_tune != "") {
112           cflags += [ "-mtune=$arm_tune" ]
113         }
114         if (arm_use_thumb) {
115           cflags += [ "-mthumb" ]
116           if (is_android && !is_clang) {  # Clang doesn't support this option.
117             cflags += [ "-mthumb-interwork" ]
118           }
119         }
120       }
121     }
122
123     defines += [ "_FILE_OFFSET_BITS=64" ]
124
125     # Omit unwind support in official builds to save space. We can use breakpad
126     # for these builds.
127     if (is_chrome_branded && is_official_build) {
128       cflags += [
129         "-fno-unwind-tables",
130         "-fno-asynchronous-unwind-tables",
131       ]
132     } else {
133       cflags += [ "-funwind-tables" ]
134     }
135   }
136
137   # Linux-specific compiler flags setup.
138   # ------------------------------------
139   if (is_linux) {
140     cflags += [
141       "-fPIC",
142       "-pipe",  # Use pipes for communicating between sub-processes. Faster.
143     ]
144     if (!is_android) {
145       cflags += [ "-pthread" ]
146     }
147
148     if (cpu_arch == "x64") {
149       # Use gold for linking on 64-bit Linux only (on 32-bit it runs out of
150       # address space, and it doesn't support cross-compiling).
151       gold_path = rebase_path("//third_party/gold", ".", root_build_dir)
152       ldflags += [
153         "-B$gold_path",
154
155         # There seems to be a conflict of --icf and -pie in gold which can
156         # generate crashy binaries. As a security measure, -pie takes
157         # precendence for now.
158         # TODO(brettw) common.gypi has this only for target toolset.
159         #"-Wl,--icf=safe",
160         "-Wl,--icf=none",
161
162         # Experimentation found that using four linking threads
163         # saved ~20% of link time.
164         # https://groups.google.com/a/chromium.org/group/chromium-dev/browse_thread/thread/281527606915bb36
165         # Only apply this to the target linker, since the host
166         # linker might not be gold, but isn't used much anyway.
167         "-Wl,--threads",
168         "-Wl,--thread-count=4",
169       ]
170     }
171
172     ldflags += [
173       "-fPIC",
174       "-pthread",
175       "-Wl,-z,noexecstack",
176       "-Wl,-z,now",
177       "-Wl,-z,relro",
178     ]
179   }
180
181   # Clang-specific compiler flags setup.
182   # ------------------------------------
183   if (is_clang) {
184     cflags += [
185       "-fcolor-diagnostics",
186     ]
187     cflags_cc += [
188       "-std=gnu++11",
189     ]
190   }
191
192   # Android-specific flags setup.
193   # -----------------------------
194   if (is_android) {
195     cflags += [
196       "-ffunction-sections",
197       "-funwind-tables",
198       "-fno-short-enums",
199     ]
200     if (!is_clang) {
201       # Clang doesn't support this one.
202       cflags += [ "-finline-limit=64" ]
203     }
204     if (is_android_webview_build) {
205       # Android predefines this as 1; undefine it here so Chromium can redefine
206       # it later to be 2 for chromium code and unset for third party code. This
207       # works because cflags are added before defines.
208       # TODO(brettw) the above comment seems incorrect. We specify defines
209       # before cflags on our compiler command lines.
210       cflags += [ "-U_FORTIFY_SOURCE" ]
211     }
212
213     if (is_asan) {
214       # Android build relies on -Wl,--gc-sections removing unreachable code.
215       # ASan instrumentation for globals inhibits this and results in a library
216       # with unresolvable relocations.
217       # TODO(eugenis): find a way to reenable this.
218       cflags += [ "-mllvm -asan-globals=0" ]
219     }
220
221     defines += [ "ANDROID" ]
222     if (!is_android_webview_build) {
223       # The NDK has these things, but doesn't define the constants
224       # to say that it does. Define them here instead.
225       defines += [ "HAVE_SYS_UIO_H" ]
226     }
227
228     ldflags += [
229       "-Wl,--no-undefined",
230       # Don't export symbols from statically linked libraries.
231       "-Wl,--exclude-libs=ALL",
232     ]
233     if (cpu_arch == "arm") {
234       ldflags += [
235         # Enable identical code folding to reduce size.
236         "-Wl,--icf=safe",
237       ]
238     }
239
240     if (is_clang) {
241       if (cpu_arch == "arm") {
242         cflags += [
243           "-target arm-linux-androideabi",
244           "-mllvm -arm-enable-ehabi",
245         ]
246         ldflags += [ "-target arm-linux-androideabi" ]
247       } else if (cpu_arch == "x86") {
248         cflags += [ "-target x86-linux-androideabi" ]
249         ldflags += [ "-target x86-linux-androideabi" ]
250       }
251     }
252   }
253 }
254
255 # runtime_library -------------------------------------------------------------
256 #
257 # Sets the runtime library and associated options.
258 #
259 # How do you determine what should go in here vs. "compiler" above? Consider if
260 # a target might choose to use a different runtime library (ignore for a moment
261 # if this is possible or reasonable on your system). If such a target would want
262 # to change or remove your option, put it in the runtime_library config. If a
263 # target wants the option regardless, put it in the compiler config.
264
265 config("runtime_library") {
266   cflags = []
267   defines = []
268   ldflags = []
269   lib_dirs = []
270   libs = []
271
272   if (is_component_build) {
273     # Component mode: dynamic CRT.
274     defines += [ "COMPONENT_BUILD" ]
275     if (is_win) {
276       # Since the library is shared, it requires exceptions or will give errors
277       # about things not matching, so keep exceptions on.
278       if (is_debug) {
279         cflags += [ "/MDd" ]
280       } else {
281         cflags += [ "/MD" ]
282       }
283     }
284   } else {
285     # Static CRT.
286     if (is_win) {
287       # We don't use exceptions, and when we link statically we can just get
288       # rid of them entirely.
289       defines += [ "_HAS_EXCEPTIONS=0" ]
290       if (is_debug) {
291         cflags += [ "/MTd" ]
292       } else {
293         cflags += [ "/MT" ]
294       }
295     }
296   }
297
298   if (is_win) {
299     defines += [
300       "__STD_C",
301       "__STDC_CONSTANT_MACROS",
302       "__STDC_FORMAT_MACROS",
303       "_CRT_RAND_S",
304       "_CRT_SECURE_NO_DEPRECATE",
305       "_SCL_SECURE_NO_DEPRECATE",
306       "_UNICODE",
307       "UNICODE",
308     ]
309   }
310
311   # Stlport setup. Android uses a different (smaller) version of the STL.
312   if (is_android) {
313     if (is_clang) {
314       # Work around incompatibilities between bionic and clang headers.
315       defines += [
316         "__compiler_offsetof=__builtin_offsetof",
317         "nan=__builtin_nan",
318       ]
319     }
320
321     defines += [
322       "USE_STLPORT=1",
323       "_STLP_USE_PTR_SPECIALIZATIONS=1",
324       "__GNU_SOURCE=1",  # Necessary for clone().
325     ]
326
327     ldflags += [
328       "-nostdlib",
329     ]
330
331
332     # NOTE: The stlport header include paths below are specified in cflags
333     # rather than include_dirs because they need to come after include_dirs.
334     # Think of them like system headers, but don't use '-isystem' because the
335     # arm-linux-androideabi-4.4.3 toolchain (circa Gingerbread) will exhibit
336     # strange errors. The include ordering here is important; change with
337     # caution.
338     if (use_system_stlport) {
339       cflags += [
340         # For libstdc++/include, which is used by stlport.
341         "-I$android_src/bionic",
342         "-I$android_src/external/stlport/stlport",
343       ]
344       libs += [
345         "stlport",
346       ]
347     } else {
348       android_stlport_root = "$android_ndk_root/sources/cxx-stl/stlport"
349
350       cflags += [ "-I$android_stlport_root/stlport" ]
351       lib_dirs += [ "$android_stlport_root/libs/$android_app_abi" ]
352
353       if (component_mode == "shared_library") {
354         libs += [ "stlport_shared" ]
355       } else {
356         libs += [ "stlport_static" ]
357       }
358     }
359   }
360 }
361
362 # chromium_code ---------------------------------------------------------------
363 #
364 # Toggles between higher and lower warnings for code that is (or isn't)
365 # part of Chromium.
366
367 config("chromium_code") {
368   if (is_win) {
369     cflags = [
370       "/W4",  # Warning level 4.
371       "/WX",  # Treat warnings as errors.
372     ]
373   } else {
374     cflags = [
375       "-Wall",
376       "-Werror",
377
378       # GCC turns on -Wsign-compare for C++ under -Wall, but clang doesn't,
379       # so we specify it explicitly.
380       # TODO(fischman): remove this if http://llvm.org/PR10448 obsoletes it.
381       # http://code.google.com/p/chromium/issues/detail?id=90453
382       "-Wsign-compare",
383     ]
384
385     # In Chromium code, we define __STDC_foo_MACROS in order to get the
386     # C99 macros on Mac and Linux.
387     defines = [
388       "__STDC_CONSTANT_MACROS",
389       "__STDC_FORMAT_MACROS",
390     ]
391
392     # TODO(brettw) this should also be enabled on Linux but some files
393     # currently fail.
394     if (is_mac) {
395       cflags += [ "-Wextra" ]
396     }
397   }
398 }
399 config("no_chromium_code") {
400   if (is_win) {
401     cflags = [
402       "/W3",  # Warning level 3.
403       "/wd4800",  # Disable warning when forcing value to bool.
404     ]
405     defines = [
406       "_CRT_NONSTDC_NO_WARNINGS",
407       "_CRT_NONSTDC_NO_DEPRECATE",
408     ]
409   }
410
411   if (is_android_webview_build) {
412     # There is a class of warning which:
413     #  1) Android always enables and also treats as errors
414     #  2) Chromium ignores in third party code
415     # So we re-enable those warnings when building Android.
416     cflags = [
417       "-Wno-address",
418       "-Wno-format-security",
419       "-Wno-return-type",
420       "-Wno-sequence-point",
421     ]
422     cflags_cc = [ "-Wno-non-virtual-dtor" ]
423   }
424 }
425
426 # rtti ------------------------------------------------------------------------
427 #
428 # Allows turning Run-Time Type Identification on or off.
429
430 config("rtti") {
431   if (is_win) {
432     cflags_cc = [ "/GR" ]
433   }
434 }
435 config("no_rtti") {
436   if (is_win) {
437     cflags_cc = [ "/GR-" ]
438   } else {
439     cflags_cc = [ "-fno-rtti" ]
440   }
441 }
442
443 # Warnings ---------------------------------------------------------------------
444 #
445 # This is where we disable various warnings that we've decided aren't
446 # worthwhile, and enable special warnings.
447
448 config("default_warnings") {
449   if (is_win) {
450     # Please keep ordered and add names if you add more.
451     cflags = [
452       "/wd4018",  # Comparing signed and unsigned values.
453       "/wd4100",  # Unreferenced formal function parameter.
454       "/wd4121",  # Alignment of a member was sensitive to packing.
455       "/wd4125",  # Decimal digit terminates octal escape sequence.
456       "/wd4127",  # Conditional expression is constant.
457       "/wd4130",  # Logical operation on address of string constant.
458       "/wd4189",  # A variable was declared and initialized but never used.
459       "/wd4201",  # Nonstandard extension used: nameless struct/union.
460       "/wd4238",  # Nonstandard extension used: class rvalue used as lvalue.
461       "/wd4244",  # Conversion: possible loss of data.
462       "/wd4245",  # Conversion: signed/unsigned mismatch,
463       "/wd4251",  # Class needs to have dll-interface.
464       "/wd4310",  # Cast truncates constant value.
465       "/wd4351",  # Elements of array will be default initialized.
466       "/wd4355",  # 'this' used in base member initializer list.
467       "/wd4396",  # Inline friend template thing.
468       "/wd4428",  # Universal character name encountered in source.
469       "/wd4481",  # Nonstandard extension: override specifier.
470       "/wd4503",  # Decorated name length exceeded, name was truncated.
471       "/wd4505",  # Unreferenced local function has been removed.
472       "/wd4510",  # Default constructor could not be generated.
473       "/wd4512",  # Assignment operator could not be generated.
474       "/wd4530",  # Exception handler used, but unwind semantics not enabled.
475       "/wd4610",  # Class can never be instantiated, constructor required.
476       "/wd4611",  # C++ object destruction and 'catch'.
477       "/wd4701",  # Potentially uninitialized local variable name used.
478       "/wd4702",  # Unreachable code.
479       "/wd4706",  # Assignment within conditional expression.
480       "/wd4819",  # Character not in the current code page.
481     ]
482   } else {
483     # Common GCC warning setup.
484     cflags = [
485       # Enables.
486       "-Wendif-labels",  # Weird old-style text after an #endif.
487
488       # Disables.
489       "-Wno-missing-field-initializers",  # "struct foo f = {0};"
490       "-Wno-unused-parameter",  # Unused function parameters.
491       "-Wno-write-strings",
492     ]
493
494     if (is_mac) {
495       cflags += [
496         "-Wnewline-eof",
497       ]
498     }
499
500     # TODO(brettw) Ones below here should be clang-only when we have a flag
501     # for it.
502     if (is_clang) {
503       cflags += [
504         "-Wheader-hygiene",
505
506         # This warns on using ints as initializers for floats in
507         # initializer lists (e.g. |int a = f(); CGSize s = { a, a };|),
508         # which happens in several places in chrome code. Not sure if
509         # this is worth fixing.
510         "-Wno-c++11-narrowing",
511
512         # Don't die on dtoa code that uses a char as an array index.
513         # This is required solely for base/third_party/dmg_fp/dtoa.cc.
514         # TODO(brettw) move this to that project then!
515         "-Wno-char-subscripts",
516
517         # Warns on switches on enums that cover all enum values but
518         # also contain a default: branch. Chrome is full of that.
519         "-Wno-covered-switch-default",
520
521         # Clang considers the `register` keyword as deprecated, but e.g.
522         # code generated by flex (used in angle) contains that keyword.
523         # http://crbug.com/255186
524         "-Wno-deprecated-register",
525
526         # Clang spots more unused functions.
527         "-Wno-unused-function",
528
529         # Warns when a const char[] is converted to bool.
530         "-Wstring-conversion",
531       ]
532     }
533
534     if (is_android) {
535       # Disable any additional warnings enabled by the Android build system but
536       # which chromium does not build cleanly with (when treating warning as
537       # errors).
538       cflags += [
539         "-Wno-extra",
540         "-Wno-ignored-qualifiers",
541         "-Wno-type-limits",
542       ]
543       cflags_cc = [
544         # Disabling c++0x-compat should be handled in WebKit, but
545         # this currently doesn't work because gcc_version is not set
546         # correctly when building with the Android build system.
547         # TODO(torne): Fix this in WebKit.
548         "-Wno-error=c++0x-compat",
549         # Other things unrelated to -Wextra:
550         "-Wno-non-virtual-dtor",
551         "-Wno-sign-promo",
552       ]
553     }
554   }
555 }
556
557 # This will generate warnings when using Clang if code generates exit-time
558 # destructors, which will slow down closing the program.
559 # TODO(thakis): Make this a blacklist instead, http://crbug.com/101600
560 config("wexit_time_destructors") {
561   if (is_clang) {
562     cflags = [ "-Wexit-time-destructors" ]
563   }
564 }
565
566 # Optimization -----------------------------------------------------------------
567 #
568 # Note that BUILDCONFIG.gn sets up a variable "default_optimization_config"
569 # which it will assign to the config it implicitly applies to every target. If
570 # you want to override the optimization level for your target, remove this
571 # config (which will expand differently for debug or release builds), and then
572 # add back the one you want to override it with:
573 #
574 #   configs -= default_optimization_config
575 #   configs += [ "//build/config/compiler/optimize_max" ]
576
577 # Default "optimization on" config. On Windows, this favors size over speed.
578 #
579 # IF YOU CHANGE THIS also consider whether optimize_max should be updated.
580 config("optimize") {
581   if (is_win) {
582     cflags = [
583       "/O2",
584       "/Ob2",  # Both explicit and auto inlining.
585       "/Oy-",  # Disable omitting frame pointers, must be after /O2.
586       "/Os",   # Favor size over speed.
587     ]
588   } else {
589     if (is_ios) {
590       cflags = [ "-Os" ]
591     } else {
592       cflags = [ "-O2" ]
593     }
594   }
595 }
596
597 # Turn off optimizations.
598 config("no_optimize") {
599   if (is_win) {
600     cflags = [
601       "/Od",  # Disable optimization.
602       "/Ob0",  # Disable all inlining (on by default).
603       "/RTC1",  # Runtime checks for stack frame and uninitialized variables.
604     ]
605   } else {
606     cflags = [ "-O0" ]
607   }
608 }
609
610 # On Windows, turns up the optimization level. This implies whole program
611 # optimization and link-time code generation which is very expensive and should
612 # be used sparingly. For non-Windows, this is the same as "optimize".
613 config("optimize_max") {
614   if (is_win) {
615     cflags = [
616       "/O2",
617       "/Ob2",  # Both explicit and auto inlining.
618       "/Oy-",  # Disable omitting frame pointers, must be after /O2.
619       "/Ot",   # Favor speed over size.
620       "/GL",   # Whole program optimization.
621     ]
622   } else {
623     if (is_ios) {
624       cflags = [ "-Os" ]
625     } else {
626       cflags = [ "-O2" ]
627     }
628   }
629 }
630
631 # Symbols ----------------------------------------------------------------------
632
633 # TODO(brettw) Since this sets ldflags on Windows which is inherited across
634 # static library boundaries, if you want to remove the default symbol config
635 # and set a different one on a target, you also have to do it for all static
636 # libraries that go into that target, which is messed up. Either we need a
637 # more flexible system for defining linker flags, or we need to separate this
638 # out into a "symbols_linker" config that is only applied to DLLs and EXEs.
639 config("symbols") {
640   if (is_win) {
641     cflags = [ "/Zi" ]  # Produce PDB file, no edit and continue.
642     ldflags = [ "/DEBUG" ]
643   } else {
644     cflags = [ "-g2" ]
645   }
646 }
647
648 config("minimal_symbols") {
649   if (is_win) {
650     # Linker symbols for backtraces only.
651     ldflags = [ "/DEBUG" ]
652   } else {
653     cflags = [ "-g1" ]
654   }
655 }
656
657 config("no_symbols") {
658   if (!is_win) {
659     cflags = [ "-g0" ]
660   }
661 }
662