# Copyright (c) 2013 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # ============================================================================= # PLATFORM SELECTION # ============================================================================= # # There are two main things to set: "os" and "cpu". The "toolchain" is the name # of the GN thing that encodes combinations of these things. # # Users typically only set the variables "target_os" and "target_cpu" in "gn # args", the rest are set up by our build and internal to GN. # # There are three different types of each of these things: The "host" # represents the computer doing the compile and never changes. The "target" # represents the main thing we're trying to build. The "current" represents # which configuration is currently being defined, which can be either the # host, the target, or something completely different (like nacl). GN will # run the same build file multiple times for the different required # configuration in the same build. # # This gives the following variables: # - host_os, host_cpu, host_toolchain # - target_os, target_cpu, default_toolchain # - current_os, current_cpu, current_toolchain. # # Note the default_toolchain isn't symmetrical (you would expect # target_toolchain). This is because the "default" toolchain is a GN built-in # concept, and "target" is something our build sets up that's symmetrical with # its GYP counterpart. Potentially the built-in default_toolchain variable # could be renamed in the future. # # When writing build files, to do something only for the host: # if (current_toolchain == host_toolchain) { ... if (target_os == "") { target_os = host_os } if (target_cpu == "") { if (target_os == "android") { # If we're building for Android, we should assume that we want to # build for ARM by default, not the host_cpu (which is likely x64). # This allows us to not have to specify both target_os and target_cpu # on the command line. target_cpu = "arm" } else { target_cpu = host_cpu } } if (current_cpu == "") { current_cpu = target_cpu } if (current_os == "") { current_os = target_os } # ============================================================================= # BUILD FLAGS # ============================================================================= # # This block lists input arguments to the build, along with their default # values. # # If a value is specified on the command line, it will overwrite the defaults # given in a declare_args block, otherwise the default will be used. # # YOU SHOULD ALMOST NEVER NEED TO ADD FLAGS TO THIS FILE. GN allows any file in # the build to declare build flags. If you need a flag for a single component, # you can just declare it in the corresponding BUILD.gn file. If you need a # flag in multiple components, there are a few options: # # - If your feature is a single target, say //components/foo, and the targets # depending on foo need to have some define set if foo is enabled: (1) Write # a declare_args block in foo's BUILD.gn file listing your enable_foo build # flag. (2) Write a config in that file listing the define, and list that # config in foo's public_configs. This will propagate that define to all the # targets depending on foo. (3) When foo is not enabled, just make it expand # to an empty group (or whatever's appropriate for the "off" state of your # feature. # # - If a semi-random set of targets need to know about a define: (1) In the # lowest level of the build that knows about this feature, add a declare_args # block in the build file for your enable flag. (2) Write a config that adds # a define conditionally based on that build flags. (3) Manually add that # config to the "configs" applying to the targets that need the define. # # - If a semi-random set of targets need to know about the build flag (to do # file inclusion or exclusion, more than just defines): (1) Write a .gni file # in the lowest-level directory that knows about the feature. (2) Put the # declare_args block with your build flag in that .gni file. (3) Import that # .gni file from the BUILD.gn files that need the flag. # # Other advice: # # - Use boolean values when possible. If you need a default value that expands # to some complex thing in the default case (like the location of the # compiler which would be computed by a script), use a default value of -1 or # the empty string. Outside of the declare_args block, conditionally expand # the default value as necessary. # # - Use a name like "use_foo" or "is_foo" (whatever is more appropriate for # your feature) rather than just "foo". # # - Write good comments directly above the declaration with no blank line. # These comments will appear as documentation in "gn args --list". # # - Don't call exec_script inside declare_args. This will execute the script # even if the value is overridden, which is wasteful. See first bullet. declare_args() { # Component build. is_component_build = false # Debug build. is_debug = true # Whether we're a traditional desktop unix. is_desktop_linux = current_os == "linux" && current_os != "chromeos" # Set to true when compiling with the Clang compiler. Typically this is used # to configure warnings. is_clang = current_os == "mac" || current_os == "ios" || current_os == "linux" || current_os == "chromeos" # Allows the path to a custom target toolchain to be injected as a single # argument, and set as the default toolchain. custom_toolchain = "" # DON'T ADD MORE FLAGS HERE. Read the comment above. } # ============================================================================= # OS DEFINITIONS # ============================================================================= # # We set these various is_FOO booleans for convenience in writing OS-based # conditions. # # - is_android, is_chromeos, is_ios, and is_win should be obvious. # - is_mac is set only for desktop Mac. It is not set on iOS. # - is_posix is true for mac and any Unix-like system (basically everything # except Windows). # - is_linux is true for desktop Linux and ChromeOS, but not Android (which is # generally too different despite being based on the Linux kernel). # # Do not add more is_* variants here for random lesser-used Unix systems like # aix or one of the BSDs. If you need to check these, just check the # current_os value directly. if (current_os == "win" || current_os == "winrt_81" || current_os == "winrt_81_phone" || current_os == "winrt_10") { is_android = false is_chromeos = false is_ios = false is_linux = false is_mac = false is_nacl = false is_posix = false is_win = true } else if (current_os == "mac") { is_android = false is_chromeos = false is_ios = false is_linux = false is_mac = true is_nacl = false is_posix = true is_win = false } else if (current_os == "android") { is_android = true is_chromeos = false is_ios = false is_linux = false is_mac = false is_nacl = false is_posix = true is_win = false } else if (current_os == "chromeos") { is_android = false is_chromeos = true is_ios = false is_linux = true is_mac = false is_nacl = false is_posix = true is_win = false } else if (current_os == "nacl") { # current_os == "nacl" will be passed by the nacl toolchain definition. # It is not set by default or on the command line. We treat is as a # Posix variant. is_android = false is_chromeos = false is_ios = false is_linux = false is_mac = false is_nacl = true is_posix = true is_win = false } else if (current_os == "ios") { is_android = false is_chromeos = false is_ios = true is_linux = false is_mac = false is_nacl = false is_posix = true is_win = false } else if (current_os == "linux") { is_android = false is_chromeos = false is_ios = false is_linux = true is_mac = false is_nacl = false is_posix = true is_win = false } # ============================================================================= # SOURCES FILTERS # ============================================================================= # # These patterns filter out platform-specific files when assigning to the # sources variable. The magic variable |sources_assignment_filter| is applied # to each assignment or appending to the sources variable and matches are # automatcally removed. # # Note that the patterns are NOT regular expressions. Only "*" and "\b" (path # boundary = end of string or slash) are supported, and the entire string # muct match the pattern (so you need "*.cc" to match all .cc files, for # example). # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call # below. sources_assignment_filter = [] if (!is_posix) { sources_assignment_filter += [ "*_posix.h", "*_posix.cc", "*_posix_unittest.h", "*_posix_unittest.cc", "*\bposix/*", ] } if (!is_win) { sources_assignment_filter += [ "*_win.cc", "*_win.h", "*_win_unittest.cc", "*\bwin/*", "*.def", "*.rc", ] } if (!is_mac) { sources_assignment_filter += [ "*_mac.h", "*_mac.cc", "*_mac.mm", "*_mac_unittest.h", "*_mac_unittest.cc", "*_mac_unittest.mm", "*\bmac/*", "*_cocoa.h", "*_cocoa.cc", "*_cocoa.mm", "*_cocoa_unittest.h", "*_cocoa_unittest.cc", "*_cocoa_unittest.mm", "*\bcocoa/*", ] } if (!is_ios) { sources_assignment_filter += [ "*_ios.h", "*_ios.cc", "*_ios.mm", "*_ios_unittest.h", "*_ios_unittest.cc", "*_ios_unittest.mm", "*\bios/*", ] } if (!is_mac && !is_ios) { sources_assignment_filter += [ "*.mm" ] } if (!is_linux) { sources_assignment_filter += [ "*_linux.h", "*_linux.cc", "*_linux_unittest.h", "*_linux_unittest.cc", "*\blinux/*", ] } if (!is_android) { sources_assignment_filter += [ "*_android.h", "*_android.cc", "*_android_unittest.h", "*_android_unittest.cc", "*\bandroid/*", ] } if (!is_chromeos) { sources_assignment_filter += [ "*_chromeos.h", "*_chromeos.cc", "*_chromeos_unittest.h", "*_chromeos_unittest.cc", "*\bchromeos/*", ] } # DO NOT ADD MORE PATTERNS TO THIS LIST, see set_sources_assignment_filter call # below. # Actually save this list. # # These patterns are executed for every file in the source tree of every run. # Therefore, adding more patterns slows down the build for everybody. We should # only add automatic patterns for configurations affecting hundreds of files # across many projects in the tree. # # Therefore, we only add rules to this list corresponding to platforms on the # Chromium waterfall. This is not for non-officially-supported platforms # (FreeBSD, etc.) toolkits, (X11, GTK, etc.), or features. For these cases, # write a conditional in the target to remove the file(s) from the list when # your platform/toolkit/feature doesn't apply. set_sources_assignment_filter(sources_assignment_filter) # ============================================================================= # TARGET DEFAULTS # ============================================================================= # # Set up the default configuration for every build target of the given type. # The values configured here will be automatically set on the scope of the # corresponding target. Target definitions can add or remove to the settings # here as needed. # Holds all configs used for making native executables and libraries, to avoid # duplication in each target below. _native_compiler_configs = [ "//build/config:feature_flags", "//build/config/compiler:compiler", "//build/config/compiler:compiler_arm_fpu", "//build/config/compiler:chromium_code", "//build/config/compiler:default_include_dirs", "//build/config/compiler:default_optimization", "//build/config/compiler:default_symbols", "//build/config/compiler:no_rtti", "//build/config/compiler:runtime_library", "//build/config/sanitizers:default_sanitizer_flags", ] if (is_win) { _native_compiler_configs += [ "//build/config/win:lean_and_mean", "//build/config/win:nominmax", "//build/config/win:unicode", "//build/config/win:winver", ] } if (current_os == "winrt_81" || current_os == "winrt_81_phone" || current_os == "winrt_10") { _native_compiler_configs += [ "//build/config/win:target_winrt" ] } if (is_posix) { _native_compiler_configs += [ "//build/config/gcc:no_exceptions", "//build/config/gcc:symbol_visibility_hidden", ] } if (is_android) { _native_compiler_configs += [ "//build/config/android:default_cygprofile_instrumentation" ] } if (is_clang && !is_nacl) { _native_compiler_configs += [ "//build/config/clang:find_bad_constructs", "//build/config/clang:extra_warnings", ] } # Debug/release-related defines. if (is_debug) { _native_compiler_configs += [ "//build/config:debug" ] } else { _native_compiler_configs += [ "//build/config:release" ] } # Windows linker setup for EXEs and DLLs. if (is_win) { _windows_linker_configs = [ "//build/config/win:default_incremental_linking", "//build/config/win:sdk_link", "//build/config/win:common_linker_setup", # Default to console-mode apps. Most of our targets are tests and such # that shouldn't use the windows subsystem. "//build/config/win:console", ] } # Executable defaults. _executable_configs = _native_compiler_configs + [ "//build/config:default_libs" ] if (is_win) { _executable_configs += _windows_linker_configs } else if (is_mac) { _executable_configs += [ "//build/config/mac:mac_dynamic_flags", "//build/config/mac:mac_executable_flags", ] } else if (is_linux || is_android) { _executable_configs += [ "//build/config/gcc:executable_ldconfig" ] if (is_android) { _executable_configs += [ "//build/config/android:executable_config" ] } } set_defaults("executable") { configs = _executable_configs } # Static library defaults. set_defaults("static_library") { configs = _native_compiler_configs } # Shared library defaults (also for components in component mode). _shared_library_configs = _native_compiler_configs + [ "//build/config:default_libs" ] if (is_win) { _shared_library_configs += _windows_linker_configs } else if (is_mac) { _shared_library_configs += [ "//build/config/mac:mac_dynamic_flags" ] } else if (is_android) { # Strip native JNI exports from shared libraries by default. Binaries that # want this can remove this config. _shared_library_configs += [ "//build/config/android:hide_native_jni_exports" ] } set_defaults("shared_library") { configs = _shared_library_configs } if (is_component_build) { set_defaults("component") { configs = _shared_library_configs } } # Source set defaults (also for components in non-component mode). set_defaults("source_set") { configs = _native_compiler_configs } if (!is_component_build) { set_defaults("component") { configs = _native_compiler_configs } } # Test defaults. set_defaults("test") { if (is_android) { configs = _shared_library_configs } else { configs = _executable_configs } } # ============================================================================== # TOOLCHAIN SETUP # ============================================================================== # # Here we set the default toolchain, as well as the variable host_toolchain # which will identify the toolchain corresponding to the local system when # doing cross-compiles. When not cross-compiling, this will be the same as the # default toolchain. _default_toolchain = "" if (is_win) { # On windows we use the same toolchain for host and target by default. if (is_clang) { host_toolchain = "//build/toolchain/win:clang_$current_cpu" } else { host_toolchain = "//build/toolchain/win:$current_cpu" } if (current_os == "win") { _default_toolchain = host_toolchain } else if (current_cpu == "x64") { # WinRT x64 _default_toolchain = "//build/toolchain/win:winrt_x64" } else if (current_cpu == "x86") { # WinRT x86 _default_toolchain = "//build/toolchain/win:winrt_x86" } } else if (is_android) { if (host_os == "linux") { # Use clang for the x86/64 Linux host builds. if (host_cpu == "x86" || host_cpu == "x64") { host_toolchain = "//build/toolchain/linux:clang_$host_cpu" } else { host_toolchain = "//build/toolchain/linux:$host_cpu" } } else if (host_os == "mac") { host_toolchain = "//build/toolchain/mac:clang_$host_cpu" } else { assert(false, "Unknown host for android cross compile") } if (is_clang) { _default_toolchain = "//build/toolchain/android:clang_$current_cpu" } else { _default_toolchain = "//build/toolchain/android:$current_cpu" } } else if (is_linux) { if (is_clang) { host_toolchain = "//build/toolchain/linux:clang_$host_cpu" _default_toolchain = "//build/toolchain/linux:clang_$current_cpu" } else { host_toolchain = "//build/toolchain/linux:$host_cpu" _default_toolchain = "//build/toolchain/linux:$current_cpu" } } else if (is_mac) { host_toolchain = "//build/toolchain/mac:clang_x64" _default_toolchain = host_toolchain } else if (is_ios) { host_toolchain = "//build/toolchain/mac:clang_x64" _default_toolchain = "//build/toolchain/mac:ios_clang_arm" } else if (is_nacl) { # TODO(GYP): This will need to change when we get NaCl working # on multiple platforms, but this whole block of code (how we define # host_toolchain) needs to be reworked regardless to key off of host_os # and host_cpu rather than the is_* variables. host_toolchain = "//build/toolchain/linux:clang_x64" } # If a custom toolchain has been set in the args, set it as default. Otherwise, # set the default toolchain for the platform (if any). if (custom_toolchain != "") { set_default_toolchain(custom_toolchain) } else if (_default_toolchain != "") { set_default_toolchain(_default_toolchain) } # ============================================================================== # COMPONENT SETUP # ============================================================================== if (is_component_build) { _component_mode = "shared_library" } else { _component_mode = "source_set" } template("component") { target(_component_mode, target_name) { forward_variables_from(invoker, "*") # All shared libraries must have the sanitizer deps to properly link in # asan mode (this target will be empty in other cases). if (!defined(deps)) { deps = [] } deps += [ "//build/config/sanitizers:deps" ] } }