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 # =============================================================================
140 # =============================================================================
142 # We set these various is_FOO booleans for convenience in writing OS-based
145 # - is_android, is_chromeos, is_ios, and is_win should be obvious.
146 # - is_mac is set only for desktop Mac. It is not set on iOS.
147 # - is_posix is true for mac and any Unix-like system (basically everything
149 # - is_linux is true for desktop Linux and ChromeOS, but not Android (which is
150 # generally too different despite being based on the Linux kernel).
152 # Do not add more is_* variants here for random lesser-used Unix systems like
153 # aix or one of the BSDs. If you need to check these, just check the
154 # current_os value directly.
156 if (current_os == "win" || current_os == "winrt_81" ||
157 current_os == "winrt_81_phone" || current_os == "winrt_10") {
166 } else if (current_os == "mac") {
175 } else if (current_os == "android") {
184 } else if (current_os == "chromeos") {
193 } else if (current_os == "nacl") {
194 # current_os == "nacl" will be passed by the nacl toolchain definition.
195 # It is not set by default or on the command line. We treat is as a
205 } else if (current_os == "ios") {
214 } else if (current_os == "linux") {
225 # =============================================================================
227 # =============================================================================
229 # These patterns filter out platform-specific files when assigning to the
230 # sources variable. The magic variable |sources_assignment_filter| is applied
231 # to each assignment or appending to the sources variable and matches are
232 # automatcally removed.
234 # Note that the patterns are NOT regular expressions. Only "*" and "\b" (path
235 # boundary = end of string or slash) are supported, and the entire string
236 # muct match the pattern (so you need "*.cc" to match all .cc files, for
239 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
241 sources_assignment_filter = []
243 sources_assignment_filter += [
246 "*_posix_unittest.h",
247 "*_posix_unittest.cc",
252 sources_assignment_filter += [
262 sources_assignment_filter += [
273 "*_cocoa_unittest.h",
274 "*_cocoa_unittest.cc",
275 "*_cocoa_unittest.mm",
280 sources_assignment_filter += [
290 if (!is_mac && !is_ios) {
291 sources_assignment_filter += [ "*.mm" ]
294 sources_assignment_filter += [
297 "*_linux_unittest.h",
298 "*_linux_unittest.cc",
303 sources_assignment_filter += [
306 "*_android_unittest.h",
307 "*_android_unittest.cc",
312 sources_assignment_filter += [
315 "*_chromeos_unittest.h",
316 "*_chromeos_unittest.cc",
321 # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call
324 # Actually save this list.
326 # These patterns are executed for every file in the source tree of every run.
327 # Therefore, adding more patterns slows down the build for everybody. We should
328 # only add automatic patterns for configurations affecting hundreds of files
329 # across many projects in the tree.
331 # Therefore, we only add rules to this list corresponding to platforms on the
332 # Chromium waterfall. This is not for non-officially-supported platforms
333 # (FreeBSD, etc.) toolkits, (X11, GTK, etc.), or features. For these cases,
334 # write a conditional in the target to remove the file(s) from the list when
335 # your platform/toolkit/feature doesn't apply.
336 set_sources_assignment_filter(sources_assignment_filter)
338 # =============================================================================
340 # =============================================================================
342 # Set up the default configuration for every build target of the given type.
343 # The values configured here will be automatically set on the scope of the
344 # corresponding target. Target definitions can add or remove to the settings
347 # Holds all configs used for making native executables and libraries, to avoid
348 # duplication in each target below.
349 _native_compiler_configs = [
350 "//build/config:feature_flags",
351 "//build/config/compiler:compiler",
352 "//build/config/compiler:compiler_arm_fpu",
353 "//build/config/compiler:chromium_code",
354 "//build/config/compiler:default_include_dirs",
355 "//build/config/compiler:default_optimization",
356 "//build/config/compiler:default_symbols",
357 "//build/config/compiler:no_rtti",
358 "//build/config/compiler:runtime_library",
361 _native_compiler_configs += [
362 "//build/config/win:lean_and_mean",
363 "//build/config/win:nominmax",
364 "//build/config/win:sdk",
365 "//build/config/win:unicode",
366 "//build/config/win:winver",
369 if (current_os == "winrt_81" || current_os == "winrt_81_phone" ||
370 current_os == "winrt_10") {
371 _native_compiler_configs += [ "//build/config/win:target_winrt" ]
374 _native_compiler_configs += [
375 "//build/config/gcc:no_exceptions",
376 "//build/config/gcc:symbol_visibility_hidden",
381 _native_compiler_configs += [ "//build/config/linux:sdk" ]
383 _native_compiler_configs += [ "//build/config/mac:sdk" ]
385 _native_compiler_configs += [ "//build/config/ios:sdk" ]
386 } else if (is_android) {
387 _native_compiler_configs += [ "//build/config/android:sdk" ]
390 if (is_clang && !is_nacl) {
391 _native_compiler_configs += [
392 "//build/config/clang:find_bad_constructs",
393 "//build/config/clang:extra_warnings",
397 # Debug/release-related defines.
399 _native_compiler_configs += [ "//build/config:debug" ]
401 _native_compiler_configs += [ "//build/config:release" ]
404 # Windows linker setup for EXEs and DLLs.
406 _windows_linker_configs = [
407 "//build/config/win:default_incremental_linking",
408 "//build/config/win:sdk_link",
409 "//build/config/win:common_linker_setup",
411 # Default to console-mode apps. Most of our targets are tests and such
412 # that shouldn't use the windows subsystem.
413 "//build/config/win:console",
417 # Executable defaults.
418 _executable_configs =
419 _native_compiler_configs + [ "//build/config:default_libs" ]
421 _executable_configs += _windows_linker_configs
423 _executable_configs += [
424 "//build/config/mac:mac_dynamic_flags",
425 "//build/config/mac:mac_executable_flags",
427 } else if (is_linux || is_android) {
428 _executable_configs += [ "//build/config/gcc:executable_ldconfig" ]
430 _executable_configs += [ "//build/config/android:executable_config" ]
433 set_defaults("executable") {
434 configs = _executable_configs
437 # Static library defaults.
438 set_defaults("static_library") {
439 configs = _native_compiler_configs
442 # Shared library defaults (also for components in component mode).
443 _shared_library_configs =
444 _native_compiler_configs + [ "//build/config:default_libs" ]
446 _shared_library_configs += _windows_linker_configs
448 _shared_library_configs += [ "//build/config/mac:mac_dynamic_flags" ]
449 } else if (is_android) {
450 # Strip native JNI exports from shared libraries by default. Binaries that
451 # want this can remove this config.
452 _shared_library_configs +=
453 [ "//build/config/android:hide_native_jni_exports" ]
455 set_defaults("shared_library") {
456 configs = _shared_library_configs
458 if (is_component_build) {
459 set_defaults("component") {
460 configs = _shared_library_configs
464 # Source set defaults (also for components in non-component mode).
465 set_defaults("source_set") {
466 configs = _native_compiler_configs
468 if (!is_component_build) {
469 set_defaults("component") {
470 configs = _native_compiler_configs
475 set_defaults("test") {
477 configs = _shared_library_configs
479 configs = _executable_configs
483 # ==============================================================================
485 # ==============================================================================
487 # Here we set the default toolchain, as well as the variable host_toolchain
488 # which will identify the toolchain corresponding to the local system when
489 # doing cross-compiles. When not cross-compiling, this will be the same as the
493 # On windows we use the same toolchain for host and target by default.
495 host_toolchain = "//build/toolchain/win:clang_$current_cpu"
497 host_toolchain = "//build/toolchain/win:$current_cpu"
500 if (current_os == "win") {
501 set_default_toolchain("$host_toolchain")
502 } else if (current_cpu == "x64") { # WinRT x64
503 set_default_toolchain("//build/toolchain/win:winrt_x64")
504 } else if (current_cpu == "x86") { # WinRT x86
505 set_default_toolchain("//build/toolchain/win:winrt_x86")
507 } else if (is_android) {
508 if (host_os == "linux") {
509 # Use clang for the x86/64 Linux host builds.
510 if (host_cpu == "x86" || host_cpu == "x64") {
511 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
513 host_toolchain = "//build/toolchain/linux:$host_cpu"
515 } else if (host_os == "mac") {
516 host_toolchain = "//build/toolchain/mac:clang_$host_cpu"
518 assert(false, "Unknown host for android cross compile")
521 set_default_toolchain("//build/toolchain/android:clang_$current_cpu")
523 set_default_toolchain("//build/toolchain/android:$current_cpu")
525 } else if (is_linux) {
527 host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
528 set_default_toolchain("//build/toolchain/linux:clang_$current_cpu")
530 host_toolchain = "//build/toolchain/linux:$host_cpu"
531 set_default_toolchain("//build/toolchain/linux:$current_cpu")
533 if (is_chromeos && cros_use_custom_toolchain) {
534 set_default_toolchain("//build/toolchain/cros:target")
537 host_toolchain = "//build/toolchain/mac:clang_x64"
538 set_default_toolchain(host_toolchain)
540 host_toolchain = "//build/toolchain/mac:clang_x64"
541 set_default_toolchain("//build/toolchain/mac:ios_clang_arm")
542 } else if (is_nacl) {
543 # TODO(GYP): This will need to change when we get NaCl working
544 # on multiple platforms, but this whole block of code (how we define
545 # host_toolchain) needs to be reworked regardless to key off of host_os
546 # and host_cpu rather than the is_* variables.
547 host_toolchain = "//build/toolchain/linux:clang_x64"
550 # ==============================================================================
552 # ==============================================================================
554 if (is_component_build) {
555 _component_mode = "shared_library"
557 _component_mode = "source_set"
559 template("component") {
560 target(_component_mode, target_name) {
561 forward_variables_from(invoker, "*")
563 # All shared libraries must have the sanitizer deps to properly link in
564 # asan mode (this target will be empty in other cases).
565 if (!defined(deps)) {
568 deps += [ "//build/config/sanitizers:deps" ]