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.
115 is_component_build = false
120 # Whether we're a traditional desktop unix.
121 is_desktop_linux = current_os == "linux" && current_os != "chromeos"
123 # Set to true when compiling with the Clang compiler. Typically this is used
124 # to configure warnings.
125 is_clang = current_os == "mac" || current_os == "ios" ||
126 current_os == "linux" || current_os == "chromeos"
128 if (current_os == "chromeos") {
129 # Allows the target toolchain to be injected as arguments. This is needed
130 # to support the CrOS build system which supports per-build-configuration
132 cros_use_custom_toolchain = false
135 # DON'T ADD MORE FLAGS HERE. Read the comment above.
138 # This flag indicates if the build is using the new optimization and symbol
139 # level configs, or the old ones.
141 # This is part of a multi-repo landing. When all required deps (WebKit and
142 # ffmpeg) have been updated to handle this flag, we can change the optimization
143 # and symbol level configs and set this flag to true. Then delete the users of
144 # the flag, wait for all deps rolls, then remove the flag.
146 # In the old scheme, this file sets the appropriate optimization or symbol
147 # level config on targets depending on what the current build setup is. This
148 # requires that targets know what the default is, and if they want to change it,
149 # they have to figure out which one to remove depending on the state of the
150 # current build. It also means that symbol_level and the sanitizer flags need
153 # In the new scheme, this file sets a "default_symbols" and
154 # "default_optimization" configs, which then expands to the right thing
155 # depending on the current build. This means less information has to be global,
156 # and there's only one config to remove that's the same in all cases for
157 # targets that want to override it. It also means that the associated flags can
159 using_new_global_compiler_configs = true
161 # To assist in a multi-sided landing of updating the default optimization
162 # config. When false, this file sets up the default optimization config to be
163 # either "no_optimize" (debug) or "optimize" (release). When true it will
164 # always set the config "default_optimization".
166 # TODO(brettw) remove this when all repos are updated.
167 using_new_optimization_config = true
169 # To assist in a multi-sided landing of updating the default optimization
170 # config. When false, this file sets up the default optimization config to be
171 # either "no_optimize" (debug) or "optimize" (release). When true it will
172 # always set the config "default_optimization".
174 # TODO(brettw) remove this when all repos are updated.
175 using_new_optimization_config = true
177 # =============================================================================
179 # =============================================================================
181 # We set these various is_FOO booleans for convenience in writing OS-based
184 # - is_android, is_chromeos, is_ios, and is_win should be obvious.
185 # - is_mac is set only for desktop Mac. It is not set on iOS.
186 # - is_posix is true for mac and any Unix-like system (basically everything
188 # - is_linux is true for desktop Linux and ChromeOS, but not Android (which is
189 # generally too different despite being based on the Linux kernel).
191 # Do not add more is_* variants here for random lesser-used Unix systems like
192 # aix or one of the BSDs. If you need to check these, just check the
193 # current_os value directly.
195 if (current_os == "win" || current_os == "winrt_81" ||
196 current_os == "winrt_81_phone" || current_os == "winrt_10") {
205 } else if (current_os == "mac") {
214 } else if (current_os == "android") {
223 } else if (current_os == "chromeos") {
232 } else if (current_os == "nacl") {
233 # current_os == "nacl" will be passed by the nacl toolchain definition.
234 # It is not set by default or on the command line. We treat is as a
244 } else if (current_os == "ios") {
253 } else if (current_os == "linux") {
264 # =============================================================================
266 # =============================================================================
268 # These patterns filter out platform-specific files when assigning to the
269 # sources variable. The magic variable |sources_assignment_filter| is applied
270 # to each assignment or appending to the sources variable and matches are
271 # automatcally removed.
273 # Note that the patterns are NOT regular expressions. Only "*" and "\b" (path
274 # boundary = end of string or slash) are supported, and the entire string
275 # muct match the pattern (so you need "*.cc" to match all .cc files, for
278 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
280 sources_assignment_filter = []
282 sources_assignment_filter += [
285 "*_posix_unittest.h",
286 "*_posix_unittest.cc",
291 sources_assignment_filter += [
301 sources_assignment_filter += [
312 "*_cocoa_unittest.h",
313 "*_cocoa_unittest.cc",
314 "*_cocoa_unittest.mm",
319 sources_assignment_filter += [
329 if (!is_mac && !is_ios) {
330 sources_assignment_filter += [ "*.mm" ]
333 sources_assignment_filter += [
336 "*_linux_unittest.h",
337 "*_linux_unittest.cc",
342 sources_assignment_filter += [
345 "*_android_unittest.h",
346 "*_android_unittest.cc",
351 sources_assignment_filter += [
354 "*_chromeos_unittest.h",
355 "*_chromeos_unittest.cc",
360 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
363 # Actually save this list.
365 # These patterns are executed for every file in the source tree of every run.
366 # Therefore, adding more patterns slows down the build for everybody. We should
367 # only add automatic patterns for configurations affecting hundreds of files
368 # across many projects in the tree.
370 # Therefore, we only add rules to this list corresponding to platforms on the
371 # Chromium waterfall. This is not for non-officially-supported platforms
372 # (FreeBSD, etc.) toolkits, (X11, GTK, etc.), or features. For these cases,
373 # write a conditional in the target to remove the file(s) from the list when
374 # your platform/toolkit/feature doesn't apply.
375 set_sources_assignment_filter(sources_assignment_filter)
377 # =============================================================================
379 # =============================================================================
381 # Set up the default configuration for every build target of the given type.
382 # The values configured here will be automatically set on the scope of the
383 # corresponding target. Target definitions can add or remove to the settings
386 # Holds all configs used for making native executables and libraries, to avoid
387 # duplication in each target below.
388 _native_compiler_configs = [
389 "//build/config:feature_flags",
390 "//build/config/compiler:compiler",
391 "//build/config/compiler:compiler_arm_fpu",
392 "//build/config/compiler:chromium_code",
393 "//build/config/compiler:default_include_dirs",
394 "//build/config/compiler:default_optimization",
395 "//build/config/compiler:default_symbols",
396 "//build/config/compiler:no_rtti",
397 "//build/config/compiler:runtime_library",
400 _native_compiler_configs += [
401 "//build/config/win:lean_and_mean",
402 "//build/config/win:nominmax",
403 "//build/config/win:sdk",
404 "//build/config/win:unicode",
405 "//build/config/win:winver",
408 if (current_os == "winrt_81" || current_os == "winrt_81_phone" ||
409 current_os == "winrt_10") {
410 _native_compiler_configs += [ "//build/config/win:target_winrt" ]
413 _native_compiler_configs += [
414 "//build/config/gcc:no_exceptions",
415 "//build/config/gcc:symbol_visibility_hidden",
420 _native_compiler_configs += [ "//build/config/linux:sdk" ]
422 _native_compiler_configs += [ "//build/config/mac:sdk" ]
424 } else if (is_android) {
425 _native_compiler_configs += [ "//build/config/android:sdk" ]
428 if (is_clang && !is_nacl) {
429 _native_compiler_configs += [
430 "//build/config/clang:find_bad_constructs",
431 "//build/config/clang:extra_warnings",
435 # Debug/release-related defines.
437 _native_compiler_configs += [ "//build/config:debug" ]
439 _native_compiler_configs += [ "//build/config:release" ]
442 # Windows linker setup for EXEs and DLLs.
444 _windows_linker_configs = [
445 "//build/config/win:default_incremental_linking",
446 "//build/config/win:sdk_link",
447 "//build/config/win:common_linker_setup",
449 # Default to console-mode apps. Most of our targets are tests and such
450 # that shouldn't use the windows subsystem.
451 "//build/config/win:console",
455 # Executable defaults.
456 _executable_configs =
457 _native_compiler_configs + [ "//build/config:default_libs" ]
459 _executable_configs += _windows_linker_configs
461 _executable_configs += [
462 "//build/config/mac:mac_dynamic_flags",
463 "//build/config/mac:mac_executable_flags",
465 } else if (is_linux || is_android) {
466 _executable_configs += [ "//build/config/gcc:executable_ldconfig" ]
468 _executable_configs += [ "//build/config/android:executable_config" ]
471 set_defaults("executable") {
472 configs = _executable_configs
475 # Static library defaults.
476 set_defaults("static_library") {
477 configs = _native_compiler_configs
480 # Shared library defaults (also for components in component mode).
481 _shared_library_configs =
482 _native_compiler_configs + [ "//build/config:default_libs" ]
484 _shared_library_configs += _windows_linker_configs
486 _shared_library_configs += [ "//build/config/mac:mac_dynamic_flags" ]
487 } else if (is_android) {
488 # Strip native JNI exports from shared libraries by default. Binaries that
489 # want this can remove this config.
490 _shared_library_configs +=
491 [ "//build/config/android:hide_native_jni_exports" ]
493 set_defaults("shared_library") {
494 configs = _shared_library_configs
496 if (is_component_build) {
497 set_defaults("component") {
498 configs = _shared_library_configs
502 # Source set defaults (also for components in non-component mode).
503 set_defaults("source_set") {
504 configs = _native_compiler_configs
506 if (!is_component_build) {
507 set_defaults("component") {
508 configs = _native_compiler_configs
513 set_defaults("test") {
515 configs = _shared_library_configs
517 configs = _executable_configs
521 # ==============================================================================
523 # ==============================================================================
525 # Here we set the default toolchain, as well as the variable host_toolchain
526 # which will identify the toolchain corresponding to the local system when
527 # doing cross-compiles. When not cross-compiling, this will be the same as the
531 # On windows we use the same toolchain for host and target by default.
533 host_toolchain = "//build/toolchain/win:clang_$current_cpu"
535 host_toolchain = "//build/toolchain/win:$current_cpu"
538 if (current_os == "win") {
539 set_default_toolchain("$host_toolchain")
540 } else if (current_cpu == "x64") { # WinRT x64
541 set_default_toolchain("//build/toolchain/win:winrt_x64")
542 } else if (current_cpu == "x86") { # WinRT x86
543 set_default_toolchain("//build/toolchain/win:winrt_x86")
545 } else if (is_android) {
546 if (host_os == "linux") {
547 # Use clang for the x86/64 Linux host builds.
548 if (host_cpu == "x86" || host_cpu == "x64") {
549 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
551 host_toolchain = "//build/toolchain/linux:$host_cpu"
553 } else if (host_os == "mac") {
554 host_toolchain = "//build/toolchain/mac:clang_$host_cpu"
556 assert(false, "Unknown host for android cross compile")
559 set_default_toolchain("//build/toolchain/android:clang_$current_cpu")
561 set_default_toolchain("//build/toolchain/android:$current_cpu")
563 } else if (is_linux) {
565 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
566 set_default_toolchain("//build/toolchain/linux:clang_$current_cpu")
568 host_toolchain = "//build/toolchain/linux:$host_cpu"
569 set_default_toolchain("//build/toolchain/linux:$current_cpu")
571 if (is_chromeos && cros_use_custom_toolchain) {
572 set_default_toolchain("//build/toolchain/cros:target")
575 host_toolchain = "//build/toolchain/mac:clang_x64"
576 set_default_toolchain(host_toolchain)
578 host_toolchain = "//build/toolchain/mac:clang_x64"
579 set_default_toolchain("//build/toolchain/mac:ios_clang_arm")
580 } else if (is_nacl) {
581 # TODO(GYP): This will need to change when we get NaCl working
582 # on multiple platforms, but this whole block of code (how we define
583 # host_toolchain) needs to be reworked regardless to key off of host_os
584 # and host_cpu rather than the is_* variables.
585 host_toolchain = "//build/toolchain/linux:clang_x64"
588 # ==============================================================================
590 # ==============================================================================
592 if (is_component_build) {
593 _component_mode = "shared_library"
595 _component_mode = "source_set"
597 template("component") {
598 target(_component_mode, target_name) {
599 forward_variables_from(invoker, "*")
601 # All shared libraries must have the sanitizer deps to properly link in
602 # asan mode (this target will be empty in other cases).
603 if (!defined(deps)) {
606 deps += [ "//build/config/sanitizers:deps" ]