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" || current_os == "winrt_81" ||
177 current_os == "winrt_81_phone" || current_os == "winrt_10") {
186 } else if (current_os == "mac") {
195 } else if (current_os == "android") {
204 } else if (current_os == "chromeos") {
213 } else if (current_os == "nacl") {
214 # current_os == "nacl" will be passed by the nacl toolchain definition.
215 # It is not set by default or on the command line. We treat is as a
225 } else if (current_os == "ios") {
234 } else if (current_os == "linux") {
245 # =============================================================================
247 # =============================================================================
249 # These patterns filter out platform-specific files when assigning to the
250 # sources variable. The magic variable |sources_assignment_filter| is applied
251 # to each assignment or appending to the sources variable and matches are
252 # automatcally removed.
254 # Note that the patterns are NOT regular expressions. Only "*" and "\b" (path
255 # boundary = end of string or slash) are supported, and the entire string
256 # muct match the pattern (so you need "*.cc" to match all .cc files, for
259 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
261 sources_assignment_filter = []
263 sources_assignment_filter += [
266 "*_posix_unittest.h",
267 "*_posix_unittest.cc",
272 sources_assignment_filter += [
282 sources_assignment_filter += [
293 "*_cocoa_unittest.h",
294 "*_cocoa_unittest.cc",
295 "*_cocoa_unittest.mm",
300 sources_assignment_filter += [
310 if (!is_mac && !is_ios) {
311 sources_assignment_filter += [ "*.mm" ]
314 sources_assignment_filter += [
317 "*_linux_unittest.h",
318 "*_linux_unittest.cc",
323 sources_assignment_filter += [
326 "*_android_unittest.h",
327 "*_android_unittest.cc",
332 sources_assignment_filter += [
335 "*_chromeos_unittest.h",
336 "*_chromeos_unittest.cc",
341 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
344 # Actually save this list.
346 # These patterns are executed for every file in the source tree of every run.
347 # Therefore, adding more patterns slows down the build for everybody. We should
348 # only add automatic patterns for configurations affecting hundreds of files
349 # across many projects in the tree.
351 # Therefore, we only add rules to this list corresponding to platforms on the
352 # Chromium waterfall. This is not for non-officially-supported platforms
353 # (FreeBSD, etc.) toolkits, (X11, GTK, etc.), or features. For these cases,
354 # write a conditional in the target to remove the file(s) from the list when
355 # your platform/toolkit/feature doesn't apply.
356 set_sources_assignment_filter(sources_assignment_filter)
358 # =============================================================================
360 # =============================================================================
362 # These Sanitizers all imply using the Clang compiler. On Windows they either
363 # don't work or work differently.
364 if (!is_clang && (is_asan || is_lsan || is_tsan || is_msan)) {
368 # =============================================================================
370 # =============================================================================
372 # Set up the default configuration for every build target of the given type.
373 # The values configured here will be automatically set on the scope of the
374 # corresponding target. Target definitions can add or remove to the settings
377 # Holds all configs used for making native executables and libraries, to avoid
378 # duplication in each target below.
379 _native_compiler_configs = [
380 "//build/config:feature_flags",
381 "//build/config/compiler:compiler",
382 "//build/config/compiler:compiler_arm_fpu",
383 "//build/config/compiler:chromium_code",
384 "//build/config/compiler:default_include_dirs",
385 "//build/config/compiler:no_rtti",
386 "//build/config/compiler:runtime_library",
389 _native_compiler_configs += [
390 "//build/config/win:lean_and_mean",
391 "//build/config/win:nominmax",
392 "//build/config/win:sdk",
393 "//build/config/win:unicode",
394 "//build/config/win:winver",
397 if (current_os == "winrt_81" || current_os == "winrt_81_phone" ||
398 current_os == "winrt_10") {
399 _native_compiler_configs += [ "//build/config/win:target_winrt" ]
402 _native_compiler_configs += [
403 "//build/config/gcc:no_exceptions",
404 "//build/config/gcc:symbol_visibility_hidden",
409 _native_compiler_configs += [ "//build/config/linux:sdk" ]
411 _native_compiler_configs += [ "//build/config/mac:sdk" ]
413 _native_compiler_configs += [ "//build/config/ios:sdk" ]
414 } else if (is_android) {
415 _native_compiler_configs += [ "//build/config/android:sdk" ]
419 _native_compiler_configs += [
420 "//build/config/clang:find_bad_constructs",
421 "//build/config/clang:extra_warnings",
425 # Optimizations and debug checking.
427 _native_compiler_configs += [ "//build/config:debug" ]
428 _default_optimization_config = "//build/config/compiler:no_optimize"
430 _native_compiler_configs += [ "//build/config:release" ]
431 _default_optimization_config = "//build/config/compiler:optimize"
433 _native_compiler_configs += [ _default_optimization_config ]
435 # If it wasn't manually set, set to an appropriate default.
436 if (symbol_level == -1) {
437 # Linux is slowed by having symbols as part of the target binary, whereas
438 # Mac and Windows have them separate, so in Release Linux, default them off.
439 if (is_debug || !is_linux) {
441 } else if (is_asan || is_lsan || is_tsan || is_msan) {
442 # Sanitizers require symbols for filename suppressions to work.
450 if (symbol_level == 2) {
451 _default_symbols_config = "//build/config/compiler:symbols"
452 } else if (symbol_level == 1) {
453 _default_symbols_config = "//build/config/compiler:minimal_symbols"
454 } else if (symbol_level == 0) {
455 _default_symbols_config = "//build/config/compiler:no_symbols"
457 assert(false, "Bad value for symbol_level.")
459 _native_compiler_configs += [ _default_symbols_config ]
461 # Windows linker setup for EXEs and DLLs.
463 _windows_linker_configs = [
464 "//build/config/win:default_incremental_linking",
465 "//build/config/win:sdk_link",
466 "//build/config/win:common_linker_setup",
468 # Default to console-mode apps. Most of our targets are tests and such
469 # that shouldn't use the windows subsystem.
470 "//build/config/win:console",
474 # Executable defaults.
475 _executable_configs =
476 _native_compiler_configs + [ "//build/config:default_libs" ]
478 _executable_configs += _windows_linker_configs
480 _executable_configs += [
481 "//build/config/mac:mac_dynamic_flags",
482 "//build/config/mac:mac_executable_flags",
484 } else if (is_linux || is_android) {
485 _executable_configs += [ "//build/config/gcc:executable_ldconfig" ]
487 _executable_configs += [ "//build/config/android:executable_config" ]
490 set_defaults("executable") {
491 configs = _executable_configs
494 # Static library defaults.
495 set_defaults("static_library") {
496 configs = _native_compiler_configs
499 # Shared library defaults (also for components in component mode).
500 _shared_library_configs =
501 _native_compiler_configs + [ "//build/config:default_libs" ]
503 _shared_library_configs += _windows_linker_configs
505 _shared_library_configs += [ "//build/config/mac:mac_dynamic_flags" ]
506 } else if (is_android) {
507 # Strip native JNI exports from shared libraries by default. Binaries that
508 # want this can remove this config.
509 _shared_library_configs +=
510 [ "//build/config/android:hide_native_jni_exports" ]
512 set_defaults("shared_library") {
513 configs = _shared_library_configs
515 if (is_component_build) {
516 set_defaults("component") {
517 configs = _shared_library_configs
521 # Source set defaults (also for components in non-component mode).
522 set_defaults("source_set") {
523 configs = _native_compiler_configs
525 if (!is_component_build) {
526 set_defaults("component") {
527 configs = _native_compiler_configs
532 set_defaults("test") {
534 configs = _shared_library_configs
536 configs = _executable_configs
540 # ==============================================================================
542 # ==============================================================================
544 # Here we set the default toolchain, as well as the variable host_toolchain
545 # which will identify the toolchain corresponding to the local system when
546 # doing cross-compiles. When not cross-compiling, this will be the same as the
550 # On windows we use the same toolchain for host and target by default.
552 host_toolchain = "//build/toolchain/win:clang_$current_cpu"
554 host_toolchain = "//build/toolchain/win:$current_cpu"
557 if (current_os == "win") {
558 set_default_toolchain("$host_toolchain")
559 } else if (current_cpu == "x64") { # WinRT x64
560 set_default_toolchain("//build/toolchain/win:winrt_x64")
561 } else if (current_cpu == "x86") { # WinRT x86
562 set_default_toolchain("//build/toolchain/win:winrt_x86")
564 } else if (is_android) {
565 if (host_os == "linux") {
566 # Use clang for the x86/64 Linux host builds.
567 if (host_cpu == "x86" || host_cpu == "x64") {
568 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
570 host_toolchain = "//build/toolchain/linux:$host_cpu"
572 } else if (host_os == "mac") {
573 host_toolchain = "//build/toolchain/mac:clang_$host_cpu"
575 assert(false, "Unknown host for android cross compile")
578 set_default_toolchain("//build/toolchain/android:clang_$current_cpu")
580 set_default_toolchain("//build/toolchain/android:$current_cpu")
582 } else if (is_linux) {
584 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
585 set_default_toolchain("//build/toolchain/linux:clang_$current_cpu")
587 host_toolchain = "//build/toolchain/linux:$host_cpu"
588 set_default_toolchain("//build/toolchain/linux:$current_cpu")
590 if (is_chromeos && cros_use_custom_toolchain) {
591 set_default_toolchain("//build/toolchain/cros:target")
594 host_toolchain = "//build/toolchain/mac:clang_x64"
595 set_default_toolchain(host_toolchain)
597 host_toolchain = "//build/toolchain/mac:clang_x64"
598 set_default_toolchain("//build/toolchain/mac:ios_clang_arm")
599 } else if (is_nacl) {
600 # TODO(GYP): This will need to change when we get NaCl working
601 # on multiple platforms, but this whole block of code (how we define
602 # host_toolchain) needs to be reworked regardless to key off of host_os
603 # and host_cpu rather than the is_* variables.
604 host_toolchain = "//build/toolchain/linux:clang_x64"
607 # ==============================================================================
609 # ==============================================================================
611 if (is_component_build) {
612 _component_mode = "shared_library"
614 _component_mode = "source_set"
616 template("component") {
617 target(_component_mode, target_name) {
618 forward_variables_from(invoker, "*")
620 # All shared libraries must have the sanitizer deps to properly link in
621 # asan mode (this target will be empty in other cases).
622 if (!defined(deps)) {
625 deps += [ "//build/config/sanitizers:deps" ]