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