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 # This flag indicates if the build is using the new optimization and symbol
159 # level configs, or the old ones.
161 # This is part of a multi-repo landing. When all required deps (WebKit and
162 # ffmpeg) have been updated to handle this flag, we can change the optimization
163 # and symbol level configs and set this flag to true. Then delete the users of
164 # the flag, wait for all deps rolls, then remove the flag.
166 # In the old scheme, this file sets the appropriate optimization or symbol
167 # level config on targets depending on what the current build setup is. This
168 # requires that targets know what the default is, and if they want to change it,
169 # they have to figure out which one to remove depending on the state of the
170 # current build. It also means that symbol_level and the sanitizer flags need
173 # In the new scheme, this file sets a "default_symbols" and
174 # "default_optimization" configs, which then expands to the right thing
175 # depending on the current build. This means less information has to be global,
176 # and there's only one config to remove that's the same in all cases for
177 # targets that want to override it. It also means that the associated flags can
179 using_new_global_compiler_configs = false
181 # =============================================================================
183 # =============================================================================
185 # We set these various is_FOO booleans for convenience in writing OS-based
188 # - is_android, is_chromeos, is_ios, and is_win should be obvious.
189 # - is_mac is set only for desktop Mac. It is not set on iOS.
190 # - is_posix is true for mac and any Unix-like system (basically everything
192 # - is_linux is true for desktop Linux and ChromeOS, but not Android (which is
193 # generally too different despite being based on the Linux kernel).
195 # Do not add more is_* variants here for random lesser-used Unix systems like
196 # aix or one of the BSDs. If you need to check these, just check the
197 # current_os value directly.
199 if (current_os == "win" || current_os == "winrt_81" ||
200 current_os == "winrt_81_phone" || current_os == "winrt_10") {
209 } else if (current_os == "mac") {
218 } else if (current_os == "android") {
227 } else if (current_os == "chromeos") {
236 } else if (current_os == "nacl") {
237 # current_os == "nacl" will be passed by the nacl toolchain definition.
238 # It is not set by default or on the command line. We treat is as a
248 } else if (current_os == "ios") {
257 } else if (current_os == "linux") {
268 # =============================================================================
270 # =============================================================================
272 # These patterns filter out platform-specific files when assigning to the
273 # sources variable. The magic variable |sources_assignment_filter| is applied
274 # to each assignment or appending to the sources variable and matches are
275 # automatcally removed.
277 # Note that the patterns are NOT regular expressions. Only "*" and "\b" (path
278 # boundary = end of string or slash) are supported, and the entire string
279 # muct match the pattern (so you need "*.cc" to match all .cc files, for
282 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
284 sources_assignment_filter = []
286 sources_assignment_filter += [
289 "*_posix_unittest.h",
290 "*_posix_unittest.cc",
295 sources_assignment_filter += [
305 sources_assignment_filter += [
316 "*_cocoa_unittest.h",
317 "*_cocoa_unittest.cc",
318 "*_cocoa_unittest.mm",
323 sources_assignment_filter += [
333 if (!is_mac && !is_ios) {
334 sources_assignment_filter += [ "*.mm" ]
337 sources_assignment_filter += [
340 "*_linux_unittest.h",
341 "*_linux_unittest.cc",
346 sources_assignment_filter += [
349 "*_android_unittest.h",
350 "*_android_unittest.cc",
355 sources_assignment_filter += [
358 "*_chromeos_unittest.h",
359 "*_chromeos_unittest.cc",
364 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
367 # Actually save this list.
369 # These patterns are executed for every file in the source tree of every run.
370 # Therefore, adding more patterns slows down the build for everybody. We should
371 # only add automatic patterns for configurations affecting hundreds of files
372 # across many projects in the tree.
374 # Therefore, we only add rules to this list corresponding to platforms on the
375 # Chromium waterfall. This is not for non-officially-supported platforms
376 # (FreeBSD, etc.) toolkits, (X11, GTK, etc.), or features. For these cases,
377 # write a conditional in the target to remove the file(s) from the list when
378 # your platform/toolkit/feature doesn't apply.
379 set_sources_assignment_filter(sources_assignment_filter)
381 # =============================================================================
383 # =============================================================================
385 # These Sanitizers all imply using the Clang compiler. On Windows they either
386 # don't work or work differently.
387 if (!is_clang && (is_asan || is_lsan || is_tsan || is_msan)) {
391 # =============================================================================
393 # =============================================================================
395 # Set up the default configuration for every build target of the given type.
396 # The values configured here will be automatically set on the scope of the
397 # corresponding target. Target definitions can add or remove to the settings
400 # Holds all configs used for making native executables and libraries, to avoid
401 # duplication in each target below.
402 _native_compiler_configs = [
403 "//build/config:feature_flags",
404 "//build/config/compiler:compiler",
405 "//build/config/compiler:compiler_arm_fpu",
406 "//build/config/compiler:chromium_code",
407 "//build/config/compiler:default_include_dirs",
408 "//build/config/compiler:no_rtti",
409 "//build/config/compiler:runtime_library",
412 _native_compiler_configs += [
413 "//build/config/win:lean_and_mean",
414 "//build/config/win:nominmax",
415 "//build/config/win:sdk",
416 "//build/config/win:unicode",
417 "//build/config/win:winver",
420 if (current_os == "winrt_81" || current_os == "winrt_81_phone" ||
421 current_os == "winrt_10") {
422 _native_compiler_configs += [ "//build/config/win:target_winrt" ]
425 _native_compiler_configs += [
426 "//build/config/gcc:no_exceptions",
427 "//build/config/gcc:symbol_visibility_hidden",
432 _native_compiler_configs += [ "//build/config/linux:sdk" ]
434 _native_compiler_configs += [ "//build/config/mac:sdk" ]
436 _native_compiler_configs += [ "//build/config/ios:sdk" ]
437 } else if (is_android) {
438 _native_compiler_configs += [ "//build/config/android:sdk" ]
441 if (is_clang && !is_nacl) {
442 _native_compiler_configs += [
443 "//build/config/clang:find_bad_constructs",
444 "//build/config/clang:extra_warnings",
448 # Optimizations and debug checking.
450 _native_compiler_configs += [ "//build/config:debug" ]
451 _default_optimization_config = "//build/config/compiler:no_optimize"
453 _native_compiler_configs += [ "//build/config:release" ]
454 _default_optimization_config = "//build/config/compiler:optimize"
456 _native_compiler_configs += [ _default_optimization_config ]
458 # If it wasn't manually set, set to an appropriate default.
459 if (symbol_level == -1) {
460 # Linux is slowed by having symbols as part of the target binary, whereas
461 # Mac and Windows have them separate, so in Release Linux, default them off.
462 if (is_debug || !is_linux) {
464 } else if (is_asan || is_lsan || is_tsan || is_msan) {
465 # Sanitizers require symbols for filename suppressions to work.
473 if (symbol_level == 2) {
474 _default_symbols_config = "//build/config/compiler:symbols"
475 } else if (symbol_level == 1) {
476 _default_symbols_config = "//build/config/compiler:minimal_symbols"
477 } else if (symbol_level == 0) {
478 _default_symbols_config = "//build/config/compiler:no_symbols"
480 assert(false, "Bad value for symbol_level.")
482 _native_compiler_configs += [ _default_symbols_config ]
484 # Windows linker setup for EXEs and DLLs.
486 _windows_linker_configs = [
487 "//build/config/win:default_incremental_linking",
488 "//build/config/win:sdk_link",
489 "//build/config/win:common_linker_setup",
491 # Default to console-mode apps. Most of our targets are tests and such
492 # that shouldn't use the windows subsystem.
493 "//build/config/win:console",
497 # Executable defaults.
498 _executable_configs =
499 _native_compiler_configs + [ "//build/config:default_libs" ]
501 _executable_configs += _windows_linker_configs
503 _executable_configs += [
504 "//build/config/mac:mac_dynamic_flags",
505 "//build/config/mac:mac_executable_flags",
507 } else if (is_linux || is_android) {
508 _executable_configs += [ "//build/config/gcc:executable_ldconfig" ]
510 _executable_configs += [ "//build/config/android:executable_config" ]
513 set_defaults("executable") {
514 configs = _executable_configs
517 # Static library defaults.
518 set_defaults("static_library") {
519 configs = _native_compiler_configs
522 # Shared library defaults (also for components in component mode).
523 _shared_library_configs =
524 _native_compiler_configs + [ "//build/config:default_libs" ]
526 _shared_library_configs += _windows_linker_configs
528 _shared_library_configs += [ "//build/config/mac:mac_dynamic_flags" ]
529 } else if (is_android) {
530 # Strip native JNI exports from shared libraries by default. Binaries that
531 # want this can remove this config.
532 _shared_library_configs +=
533 [ "//build/config/android:hide_native_jni_exports" ]
535 set_defaults("shared_library") {
536 configs = _shared_library_configs
538 if (is_component_build) {
539 set_defaults("component") {
540 configs = _shared_library_configs
544 # Source set defaults (also for components in non-component mode).
545 set_defaults("source_set") {
546 configs = _native_compiler_configs
548 if (!is_component_build) {
549 set_defaults("component") {
550 configs = _native_compiler_configs
555 set_defaults("test") {
557 configs = _shared_library_configs
559 configs = _executable_configs
563 # ==============================================================================
565 # ==============================================================================
567 # Here we set the default toolchain, as well as the variable host_toolchain
568 # which will identify the toolchain corresponding to the local system when
569 # doing cross-compiles. When not cross-compiling, this will be the same as the
573 # On windows we use the same toolchain for host and target by default.
575 host_toolchain = "//build/toolchain/win:clang_$current_cpu"
577 host_toolchain = "//build/toolchain/win:$current_cpu"
580 if (current_os == "win") {
581 set_default_toolchain("$host_toolchain")
582 } else if (current_cpu == "x64") { # WinRT x64
583 set_default_toolchain("//build/toolchain/win:winrt_x64")
584 } else if (current_cpu == "x86") { # WinRT x86
585 set_default_toolchain("//build/toolchain/win:winrt_x86")
587 } else if (is_android) {
588 if (host_os == "linux") {
589 # Use clang for the x86/64 Linux host builds.
590 if (host_cpu == "x86" || host_cpu == "x64") {
591 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
593 host_toolchain = "//build/toolchain/linux:$host_cpu"
595 } else if (host_os == "mac") {
596 host_toolchain = "//build/toolchain/mac:clang_$host_cpu"
598 assert(false, "Unknown host for android cross compile")
601 set_default_toolchain("//build/toolchain/android:clang_$current_cpu")
603 set_default_toolchain("//build/toolchain/android:$current_cpu")
605 } else if (is_linux) {
607 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
608 set_default_toolchain("//build/toolchain/linux:clang_$current_cpu")
610 host_toolchain = "//build/toolchain/linux:$host_cpu"
611 set_default_toolchain("//build/toolchain/linux:$current_cpu")
613 if (is_chromeos && cros_use_custom_toolchain) {
614 set_default_toolchain("//build/toolchain/cros:target")
617 host_toolchain = "//build/toolchain/mac:clang_x64"
618 set_default_toolchain(host_toolchain)
620 host_toolchain = "//build/toolchain/mac:clang_x64"
621 set_default_toolchain("//build/toolchain/mac:ios_clang_arm")
622 } else if (is_nacl) {
623 # TODO(GYP): This will need to change when we get NaCl working
624 # on multiple platforms, but this whole block of code (how we define
625 # host_toolchain) needs to be reworked regardless to key off of host_os
626 # and host_cpu rather than the is_* variables.
627 host_toolchain = "//build/toolchain/linux:clang_x64"
630 # ==============================================================================
632 # ==============================================================================
634 if (is_component_build) {
635 _component_mode = "shared_library"
637 _component_mode = "source_set"
639 template("component") {
640 target(_component_mode, target_name) {
641 forward_variables_from(invoker, "*")
643 # All shared libraries must have the sanitizer deps to properly link in
644 # asan mode (this target will be empty in other cases).
645 if (!defined(deps)) {
648 deps += [ "//build/config/sanitizers:deps" ]