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.
5 # =============================================================================
7 # =============================================================================
9 # There are two main things to set: "os" and "cpu". The "toolchain" is the name
10 # of the GN thing that encodes combinations of these things.
12 # Users typically only set the variables "target_os" and "target_cpu" in "gn
13 # args", the rest are set up by our build and internal to GN.
15 # There are three different types of each of these things: The "host"
16 # represents the computer doing the compile and never changes. The "target"
17 # represents the main thing we're trying to build. The "current" represents
18 # which configuration is currently being defined, which can be either the
19 # host, the target, or something completely different (like nacl). GN will
20 # run the same build file multiple times for the different required
21 # configuration in the same build.
23 # This gives the following variables:
24 # - host_os, host_cpu, host_toolchain
25 # - target_os, target_cpu, default_toolchain
26 # - current_os, current_cpu, current_toolchain.
28 # Note the default_toolchain isn't symmetrical (you would expect
29 # target_toolchain). This is because the "default" toolchain is a GN built-in
30 # concept, and "target" is something our build sets up that's symmetrical with
31 # its GYP counterpart. Potentially the built-in default_toolchain variable
32 # could be renamed in the future.
34 # When writing build files, to do something only for the host:
35 # if (current_toolchain == host_toolchain) { ...
37 if (target_os == "") {
41 if (target_cpu == "") {
42 if (target_os == "android") {
43 # If we're building for Android, we should assume that we want to
44 # build for ARM by default, not the host_cpu (which is likely x64).
45 # This allows us to not have to specify both target_os and target_cpu
46 # on the command line.
53 if (current_cpu == "") {
54 current_cpu = target_cpu
56 if (current_os == "") {
57 current_os = target_os
60 # =============================================================================
62 # =============================================================================
64 # This block lists input arguments to the build, along with their default
67 # If a value is specified on the command line, it will overwrite the defaults
68 # given in a declare_args block, otherwise the default will be used.
70 # YOU SHOULD ALMOST NEVER NEED TO ADD FLAGS TO THIS FILE. GN allows any file in
71 # the build to declare build flags. If you need a flag for a single component,
72 # you can just declare it in the corresponding BUILD.gn file. If you need a
73 # flag in multiple components, there are a few options:
75 # - If your feature is a single target, say //components/foo, and the targets
76 # depending on foo need to have some define set if foo is enabled: (1) Write
77 # a declare_args block in foo's BUILD.gn file listing your enable_foo build
78 # flag. (2) Write a config in that file listing the define, and list that
79 # config in foo's public_configs. This will propagate that define to all the
80 # targets depending on foo. (3) When foo is not enabled, just make it expand
81 # to an empty group (or whatever's appropriate for the "off" state of your
84 # - If a semi-random set of targets need to know about a define: (1) In the
85 # lowest level of the build that knows about this feature, add a declare_args
86 # block in the build file for your enable flag. (2) Write a config that adds
87 # a define conditionally based on that build flags. (3) Manually add that
88 # config to the "configs" applying to the targets that need the define.
90 # - If a semi-random set of targets need to know about the build flag (to do
91 # file inclusion or exclusion, more than just defines): (1) Write a .gni file
92 # in the lowest-level directory that knows about the feature. (2) Put the
93 # declare_args block with your build flag in that .gni file. (3) Import that
94 # .gni file from the BUILD.gn files that need the flag.
98 # - Use boolean values when possible. If you need a default value that expands
99 # to some complex thing in the default case (like the location of the
100 # compiler which would be computed by a script), use a default value of -1 or
101 # the empty string. Outside of the declare_args block, conditionally expand
102 # the default value as necessary.
104 # - Use a name like "use_foo" or "is_foo" (whatever is more appropriate for
105 # your feature) rather than just "foo".
107 # - Write good comments directly above the declaration with no blank line.
108 # These comments will appear as documentation in "gn args --list".
110 # - Don't call exec_script inside declare_args. This will execute the script
111 # even if the value is overridden, which is wasteful. See first bullet.
114 # How many symbols to include in the build. This affects the performance of
115 # the build since the symbols are large and dealing with them is slow.
116 # 2 means regular build with symbols.
117 # 1 means minimal symbols, usually enough for backtraces only.
118 # 0 means no symbols.
119 # -1 means auto-set (off in release, regular in debug).
123 is_component_build = false
128 # Whether we're a traditional desktop unix.
129 is_desktop_linux = current_os == "linux" && current_os != "chromeos"
131 # Set to true when compiling with the Clang compiler. Typically this is used
132 # to configure warnings.
133 is_clang = current_os == "mac" || current_os == "ios" ||
134 current_os == "linux" || current_os == "chromeos"
136 # Compile for Address Sanitizer to find memory bugs.
139 # Compile for Leak Sanitizer to find leaks.
142 # Compile for Memory Sanitizer to find uninitialized reads.
145 # Compile for Thread Sanitizer to find threading bugs.
148 if (current_os == "chromeos") {
149 # Allows the target toolchain to be injected as arguments. This is needed
150 # to support the CrOS build system which supports per-build-configuration
152 cros_use_custom_toolchain = false
155 # DON'T ADD MORE FLAGS HERE. Read the comment above.
158 # =============================================================================
160 # =============================================================================
162 # We set these various is_FOO booleans for convenience in writing OS-based
165 # - is_android, is_chromeos, is_ios, and is_win should be obvious.
166 # - is_mac is set only for desktop Mac. It is not set on iOS.
167 # - is_posix is true for mac and any Unix-like system (basically everything
169 # - is_linux is true for desktop Linux and ChromeOS, but not Android (which is
170 # generally too different despite being based on the Linux kernel).
172 # Do not add more is_* variants here for random lesser-used Unix systems like
173 # aix or one of the BSDs. If you need to check these, just check the
174 # current_os value directly.
176 if (current_os == "win") {
185 } else if (current_os == "mac") {
194 } else if (current_os == "android") {
203 } else if (current_os == "chromeos") {
212 } else if (current_os == "nacl") {
213 # current_os == "nacl" will be passed by the nacl toolchain definition.
214 # It is not set by default or on the command line. We treat is as a
224 } else if (current_os == "ios") {
233 } else if (current_os == "linux") {
244 # =============================================================================
246 # =============================================================================
248 # These patterns filter out platform-specific files when assigning to the
249 # sources variable. The magic variable |sources_assignment_filter| is applied
250 # to each assignment or appending to the sources variable and matches are
251 # automatcally removed.
253 # Note that the patterns are NOT regular expressions. Only "*" and "\b" (path
254 # boundary = end of string or slash) are supported, and the entire string
255 # muct match the pattern (so you need "*.cc" to match all .cc files, for
258 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
260 sources_assignment_filter = []
262 sources_assignment_filter += [
265 "*_posix_unittest.h",
266 "*_posix_unittest.cc",
271 sources_assignment_filter += [
281 sources_assignment_filter += [
292 "*_cocoa_unittest.h",
293 "*_cocoa_unittest.cc",
294 "*_cocoa_unittest.mm",
299 sources_assignment_filter += [
309 if (!is_mac && !is_ios) {
310 sources_assignment_filter += [ "*.mm" ]
313 sources_assignment_filter += [
316 "*_linux_unittest.h",
317 "*_linux_unittest.cc",
322 sources_assignment_filter += [
325 "*_android_unittest.h",
326 "*_android_unittest.cc",
331 sources_assignment_filter += [
334 "*_chromeos_unittest.h",
335 "*_chromeos_unittest.cc",
340 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
343 # Actually save this list.
345 # These patterns are executed for every file in the source tree of every run.
346 # Therefore, adding more patterns slows down the build for everybody. We should
347 # only add automatic patterns for configurations affecting hundreds of files
348 # across many projects in the tree.
350 # Therefore, we only add rules to this list corresponding to platforms on the
351 # Chromium waterfall. This is not for non-officially-supported platforms
352 # (FreeBSD, etc.) toolkits, (X11, GTK, etc.), or features. For these cases,
353 # write a conditional in the target to remove the file(s) from the list when
354 # your platform/toolkit/feature doesn't apply.
355 set_sources_assignment_filter(sources_assignment_filter)
357 # =============================================================================
359 # =============================================================================
361 # These Sanitizers all imply using the Clang compiler. On Windows they either
362 # don't work or work differently.
363 if (!is_clang && (is_asan || is_lsan || is_tsan || is_msan)) {
367 # =============================================================================
369 # =============================================================================
371 # Set up the default configuration for every build target of the given type.
372 # The values configured here will be automatically set on the scope of the
373 # corresponding target. Target definitions can add or remove to the settings
376 # Holds all configs used for making native executables and libraries, to avoid
377 # duplication in each target below.
378 _native_compiler_configs = [
379 "//build/config:feature_flags",
380 "//build/config/compiler:compiler",
381 "//build/config/compiler:compiler_arm_fpu",
382 "//build/config/compiler:chromium_code",
383 "//build/config/compiler:default_include_dirs",
384 "//build/config/compiler:no_rtti",
385 "//build/config/compiler:runtime_library",
388 _native_compiler_configs += [
389 "//build/config/win:lean_and_mean",
390 "//build/config/win:nominmax",
391 "//build/config/win:sdk",
392 "//build/config/win:unicode",
393 "//build/config/win:winver",
397 _native_compiler_configs += [
398 "//build/config/gcc:no_exceptions",
399 "//build/config/gcc:symbol_visibility_hidden",
404 _native_compiler_configs += [ "//build/config/linux:sdk" ]
406 _native_compiler_configs += [ "//build/config/mac:sdk" ]
408 _native_compiler_configs += [ "//build/config/ios:sdk" ]
409 } else if (is_android) {
410 _native_compiler_configs += [ "//build/config/android:sdk" ]
414 _native_compiler_configs += [
415 "//build/config/clang:find_bad_constructs",
416 "//build/config/clang:extra_warnings",
420 # Optimizations and debug checking.
422 _native_compiler_configs += [ "//build/config:debug" ]
423 _default_optimization_config = "//build/config/compiler:no_optimize"
425 _native_compiler_configs += [ "//build/config:release" ]
426 _default_optimization_config = "//build/config/compiler:optimize"
428 _native_compiler_configs += [ _default_optimization_config ]
430 # If it wasn't manually set, set to an appropriate default.
431 if (symbol_level == -1) {
432 # Linux is slowed by having symbols as part of the target binary, whereas
433 # Mac and Windows have them separate, so in Release Linux, default them off.
434 if (is_debug || !is_linux) {
436 } else if (is_asan || is_lsan || is_tsan || is_msan) {
437 # Sanitizers require symbols for filename suppressions to work.
445 if (symbol_level == 2) {
446 _default_symbols_config = "//build/config/compiler:symbols"
447 } else if (symbol_level == 1) {
448 _default_symbols_config = "//build/config/compiler:minimal_symbols"
449 } else if (symbol_level == 0) {
450 _default_symbols_config = "//build/config/compiler:no_symbols"
452 assert(false, "Bad value for symbol_level.")
454 _native_compiler_configs += [ _default_symbols_config ]
456 # Windows linker setup for EXEs and DLLs.
458 _windows_linker_configs = [
459 "//build/config/win:default_incremental_linking",
460 "//build/config/win:sdk_link",
461 "//build/config/win:common_linker_setup",
463 # Default to console-mode apps. Most of our targets are tests and such
464 # that shouldn't use the windows subsystem.
465 "//build/config/win:console",
469 # Executable defaults.
470 _executable_configs =
471 _native_compiler_configs + [ "//build/config:default_libs" ]
473 _executable_configs += _windows_linker_configs
475 _executable_configs += [
476 "//build/config/mac:mac_dynamic_flags",
477 "//build/config/mac:mac_executable_flags",
479 } else if (is_linux || is_android) {
480 _executable_configs += [ "//build/config/gcc:executable_ldconfig" ]
482 _executable_configs += [ "//build/config/android:executable_config" ]
485 set_defaults("executable") {
486 configs = _executable_configs
489 # Static library defaults.
490 set_defaults("static_library") {
491 configs = _native_compiler_configs
494 # Shared library defaults (also for components in component mode).
495 _shared_library_configs =
496 _native_compiler_configs + [ "//build/config:default_libs" ]
498 _shared_library_configs += _windows_linker_configs
500 _shared_library_configs += [ "//build/config/mac:mac_dynamic_flags" ]
501 } else if (is_android) {
502 # Strip native JNI exports from shared libraries by default. Binaries that
503 # want this can remove this config.
504 _shared_library_configs +=
505 [ "//build/config/android:hide_native_jni_exports" ]
507 set_defaults("shared_library") {
508 configs = _shared_library_configs
510 if (is_component_build) {
511 set_defaults("component") {
512 configs = _shared_library_configs
516 # Source set defaults (also for components in non-component mode).
517 set_defaults("source_set") {
518 configs = _native_compiler_configs
520 if (!is_component_build) {
521 set_defaults("component") {
522 configs = _native_compiler_configs
527 set_defaults("test") {
529 configs = _shared_library_configs
531 configs = _executable_configs
535 # ==============================================================================
537 # ==============================================================================
539 # Here we set the default toolchain, as well as the variable host_toolchain
540 # which will identify the toolchain corresponding to the local system when
541 # doing cross-compiles. When not cross-compiling, this will be the same as the
545 # On windows we use the same toolchain for host and target by default.
547 host_toolchain = "//build/toolchain/win:clang_$current_cpu"
549 host_toolchain = "//build/toolchain/win:$current_cpu"
551 set_default_toolchain("$host_toolchain")
552 } else if (is_android) {
553 if (host_os == "linux") {
554 # Use clang for the x86/64 Linux host builds.
555 if (host_cpu == "x86" || host_cpu == "x64") {
556 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
558 host_toolchain = "//build/toolchain/linux:$host_cpu"
560 } else if (host_os == "mac") {
561 host_toolchain = "//build/toolchain/mac:clang_$host_cpu"
563 assert(false, "Unknown host for android cross compile")
565 set_default_toolchain("//build/toolchain/android:$current_cpu")
566 } else if (is_linux) {
568 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
569 set_default_toolchain("//build/toolchain/linux:clang_$current_cpu")
571 host_toolchain = "//build/toolchain/linux:$host_cpu"
572 set_default_toolchain("//build/toolchain/linux:$current_cpu")
574 if (is_chromeos && cros_use_custom_toolchain) {
575 set_default_toolchain("//build/toolchain/cros:target")
578 host_toolchain = "//build/toolchain/mac:clang_x64"
579 set_default_toolchain(host_toolchain)
581 host_toolchain = "//build/toolchain/mac:clang_x64"
582 set_default_toolchain("//build/toolchain/mac:clang_$current_cpu")
583 } else if (is_nacl) {
584 # TODO(GYP): This will need to change when we get NaCl working
585 # on multiple platforms, but this whole block of code (how we define
586 # host_toolchain) needs to be reworked regardless to key off of host_os
587 # and host_cpu rather than the is_* variables.
588 host_toolchain = "//build/toolchain/linux:clang_x64"
591 # ==============================================================================
593 # ==============================================================================
595 # TODO(brettw) erase this once the built-in "component" function is removed.
596 if (is_component_build) {
597 component_mode = "shared_library"
599 component_mode = "source_set"
602 template("component") {
603 if (is_component_build) {
604 shared_library(target_name) {
605 # Configs will always be defined since we set_defaults for a component
606 # above. We want to use those rather than whatever came with the nested
607 # shared/static library inside the component.
608 configs = [] # Prevent list overwriting warning.
609 configs = invoker.configs
611 # The sources assignment filter will have already been applied when the
612 # code was originally executed. We don't want to apply it again, since
613 # the original target may have override it for some assignments.
614 set_sources_assignment_filter([])
616 if (defined(invoker.all_dependent_configs)) {
617 all_dependent_configs = invoker.all_dependent_configs
619 if (defined(invoker.allow_circular_includes_from)) {
620 allow_circular_includes_from = invoker.allow_circular_includes_from
622 if (defined(invoker.cflags)) {
623 cflags = invoker.cflags
625 if (defined(invoker.cflags_c)) {
626 cflags_c = invoker.cflags_c
628 if (defined(invoker.cflags_cc)) {
629 cflags_cc = invoker.cflags_cc
631 if (defined(invoker.cflags_objc)) {
632 cflags_objc = invoker.cflags_objc
634 if (defined(invoker.cflags_objcc)) {
635 cflags_objcc = invoker.cflags_objcc
637 if (defined(invoker.check_includes)) {
638 check_includes = invoker.check_includes
640 if (defined(invoker.data)) {
643 if (defined(invoker.data_deps)) {
644 data_deps = invoker.data_deps
646 if (defined(invoker.datadeps)) {
647 datadeps = invoker.datadeps
649 if (defined(invoker.defines)) {
650 defines = invoker.defines
653 # All shared libraries must have the sanitizer deps to properly link in
654 # asan mode (this target will be empty in other cases).
655 if (defined(invoker.deps)) {
656 deps = invoker.deps + [ "//build/config/sanitizers:deps" ]
659 "//build/config/sanitizers:deps",
662 if (defined(invoker.direct_dependent_configs)) {
663 direct_dependent_configs = invoker.direct_dependent_configs
665 if (defined(invoker.forward_dependent_configs_from)) {
666 forward_dependent_configs_from = invoker.forward_dependent_configs_from
668 if (defined(invoker.include_dirs)) {
669 include_dirs = invoker.include_dirs
671 if (defined(invoker.ldflags)) {
672 ldflags = invoker.ldflags
674 if (defined(invoker.lib_dirs)) {
675 lib_dirs = invoker.lib_dirs
677 if (defined(invoker.libs)) {
680 if (defined(invoker.output_extension)) {
681 output_extension = invoker.output_extension
683 if (defined(invoker.output_name)) {
684 output_name = invoker.output_name
686 if (defined(invoker.public)) {
687 public = invoker.public
689 if (defined(invoker.public_configs)) {
690 public_configs = invoker.public_configs
692 if (defined(invoker.public_deps)) {
693 public_deps = invoker.public_deps
695 if (defined(invoker.sources)) {
696 sources = invoker.sources
698 if (defined(invoker.testonly)) {
699 testonly = invoker.testonly
701 if (defined(invoker.visibility)) {
702 visibility = invoker.visibility
706 source_set(target_name) {
708 configs = [] # Prevent list overwriting warning.
709 configs = invoker.configs
712 set_sources_assignment_filter([])
714 if (defined(invoker.all_dependent_configs)) {
715 all_dependent_configs = invoker.all_dependent_configs
717 if (defined(invoker.allow_circular_includes_from)) {
718 allow_circular_includes_from = invoker.allow_circular_includes_from
720 if (defined(invoker.cflags)) {
721 cflags = invoker.cflags
723 if (defined(invoker.cflags_c)) {
724 cflags_c = invoker.cflags_c
726 if (defined(invoker.cflags_cc)) {
727 cflags_cc = invoker.cflags_cc
729 if (defined(invoker.cflags_objc)) {
730 cflags_objc = invoker.cflags_objc
732 if (defined(invoker.cflags_objcc)) {
733 cflags_objcc = invoker.cflags_objcc
735 if (defined(invoker.check_includes)) {
736 check_includes = invoker.check_includes
738 if (defined(invoker.data)) {
741 if (defined(invoker.data_deps)) {
742 data_deps = invoker.data_deps
744 if (defined(invoker.datadeps)) {
745 datadeps = invoker.datadeps
747 if (defined(invoker.defines)) {
748 defines = invoker.defines
750 if (defined(invoker.deps)) {
753 if (defined(invoker.direct_dependent_configs)) {
754 direct_dependent_configs = invoker.direct_dependent_configs
756 if (defined(invoker.forward_dependent_configs_from)) {
757 forward_dependent_configs_from = invoker.forward_dependent_configs_from
759 if (defined(invoker.include_dirs)) {
760 include_dirs = invoker.include_dirs
762 if (defined(invoker.ldflags)) {
763 ldflags = invoker.ldflags
765 if (defined(invoker.lib_dirs)) {
766 lib_dirs = invoker.lib_dirs
768 if (defined(invoker.libs)) {
771 if (defined(invoker.output_extension)) {
772 output_extension = invoker.output_extension
774 if (defined(invoker.output_name)) {
775 output_name = invoker.output_name
777 if (defined(invoker.public)) {
778 public = invoker.public
780 if (defined(invoker.public_configs)) {
781 public_configs = invoker.public_configs
783 if (defined(invoker.public_deps)) {
784 public_deps = invoker.public_deps
786 if (defined(invoker.sources)) {
787 sources = invoker.sources
789 if (defined(invoker.testonly)) {
790 testonly = invoker.testonly
792 if (defined(invoker.visibility)) {
793 visibility = invoker.visibility