Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1 | # GN Reference |
| 2 | |
| 3 | *This page is automatically generated from* `gn help --markdown all`. |
| 4 | |
| 5 | ## Contents |
| 6 | |
| 7 | * [Commands](#commands) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 8 | * [analyze: Analyze which targets are affected by a list of files.](#cmd_analyze) |
| 9 | * [args: Display or configure arguments declared by the build.](#cmd_args) |
| 10 | * [check: Check header dependencies.](#cmd_check) |
| 11 | * [clean: Cleans the output directory.](#cmd_clean) |
RJ Ascani | 4e260f1 | 2020-10-19 17:41:51 -0700 | [diff] [blame] | 12 | * [clean_stale: Cleans the stale output files from the output directory.](#cmd_clean_stale) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 13 | * [desc: Show lots of insightful information about a target or config.](#cmd_desc) |
Julie Hockett | d69a9c3 | 2019-01-23 14:36:18 -0800 | [diff] [blame] | 14 | * [format: Format .gn files.](#cmd_format) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 15 | * [gen: Generate ninja files.](#cmd_gen) |
| 16 | * [help: Does what you think.](#cmd_help) |
| 17 | * [ls: List matching targets.](#cmd_ls) |
| 18 | * [meta: List target metadata collection results.](#cmd_meta) |
Brett Wilson | 7439709 | 2020-03-18 16:34:14 -0700 | [diff] [blame] | 19 | * [outputs: Which files a source/target make.](#cmd_outputs) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 20 | * [path: Find paths between two targets.](#cmd_path) |
| 21 | * [refs: Find stuff referencing a target or file.](#cmd_refs) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 22 | * [Target declarations](#targets) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 23 | * [action: Declare a target that runs a script a single time.](#func_action) |
| 24 | * [action_foreach: Declare a target that runs a script over a set of files.](#func_action_foreach) |
| 25 | * [bundle_data: [iOS/macOS] Declare a target without output.](#func_bundle_data) |
| 26 | * [copy: Declare a target that copies files.](#func_copy) |
| 27 | * [create_bundle: [iOS/macOS] Build an iOS or macOS bundle.](#func_create_bundle) |
| 28 | * [executable: Declare an executable target.](#func_executable) |
| 29 | * [generated_file: Declare a generated_file target.](#func_generated_file) |
| 30 | * [group: Declare a named group of targets.](#func_group) |
| 31 | * [loadable_module: Declare a loadable module target.](#func_loadable_module) |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 32 | * [rust_library: Declare a Rust library target.](#func_rust_library) |
Petr Hosek | fe36c7c | 2019-11-26 14:38:08 -0800 | [diff] [blame] | 33 | * [rust_proc_macro: Declare a Rust procedural macro target.](#func_rust_proc_macro) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 34 | * [shared_library: Declare a shared library target.](#func_shared_library) |
| 35 | * [source_set: Declare a source set target.](#func_source_set) |
| 36 | * [static_library: Declare a static library target.](#func_static_library) |
Antonio Sartori | ecaaf4b | 2022-07-01 05:57:08 +0000 | [diff] [blame] | 37 | * [target: Declare a target with the given programmatic type.](#func_target) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 38 | * [Buildfile functions](#functions) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 39 | * [assert: Assert an expression is true at generation time.](#func_assert) |
| 40 | * [config: Defines a configuration object.](#func_config) |
| 41 | * [declare_args: Declare build arguments.](#func_declare_args) |
| 42 | * [defined: Returns whether an identifier is defined.](#func_defined) |
| 43 | * [exec_script: Synchronously run a script and return the output.](#func_exec_script) |
Sylvain Defresne | aff489a | 2020-03-11 18:27:43 +0100 | [diff] [blame] | 44 | * [filter_exclude: Remove values that match a set of patterns.](#func_filter_exclude) |
| 45 | * [filter_include: Remove values that do not match a set of patterns.](#func_filter_include) |
Aaron Wood | e05c0aa | 2024-02-14 13:40:53 -0800 | [diff] [blame] | 46 | * [filter_labels_exclude: Remove labels that match a set of patterns.](#func_filter_labels_exclude) |
| 47 | * [filter_labels_include: Remove labels that do not match a set of patterns.](#func_filter_labels_include) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 48 | * [foreach: Iterate over a list.](#func_foreach) |
| 49 | * [forward_variables_from: Copies variables from a different scope.](#func_forward_variables_from) |
| 50 | * [get_label_info: Get an attribute from a target's label.](#func_get_label_info) |
| 51 | * [get_path_info: Extract parts of a file or directory name.](#func_get_path_info) |
| 52 | * [get_target_outputs: [file list] Get the list of outputs from a target.](#func_get_target_outputs) |
| 53 | * [getenv: Get an environment variable.](#func_getenv) |
| 54 | * [import: Import a file into the current scope.](#func_import) |
Aaron Wood | e05c0aa | 2024-02-14 13:40:53 -0800 | [diff] [blame] | 55 | * [label_matches: Returns whether a label matches any of a list of patterns.](#func_label_matches) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 56 | * [not_needed: Mark variables from scope as not needed.](#func_not_needed) |
Andrew Grieve | f98b6d7 | 2025-02-25 08:54:51 -0800 | [diff] [blame] | 57 | * [path_exists: Returns whether the given path exists.](#func_path_exists) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 58 | * [pool: Defines a pool object.](#func_pool) |
| 59 | * [print: Prints to the console.](#func_print) |
Aaron Wood | 24062bb | 2022-04-25 20:37:48 -0700 | [diff] [blame] | 60 | * [print_stack_trace: Prints a stack trace.](#func_print_stack_trace) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 61 | * [process_file_template: Do template expansion over a list of files.](#func_process_file_template) |
| 62 | * [read_file: Read a file into a variable.](#func_read_file) |
| 63 | * [rebase_path: Rebase a file or directory to another location.](#func_rebase_path) |
| 64 | * [set_default_toolchain: Sets the default toolchain name.](#func_set_default_toolchain) |
| 65 | * [set_defaults: Set default values for a target type.](#func_set_defaults) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 66 | * [split_list: Splits a list into N different sub-lists.](#func_split_list) |
Keir Mierle | 6ae6330 | 2019-11-08 23:02:18 -0800 | [diff] [blame] | 67 | * [string_join: Concatenates a list of strings with a separator.](#func_string_join) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 68 | * [string_replace: Replaces substring in the given string.](#func_string_replace) |
Keir Mierle | 6ae6330 | 2019-11-08 23:02:18 -0800 | [diff] [blame] | 69 | * [string_split: Split string into a list of strings.](#func_string_split) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 70 | * [template: Define a template rule.](#func_template) |
| 71 | * [tool: Specify arguments to a toolchain tool.](#func_tool) |
| 72 | * [toolchain: Defines a toolchain.](#func_toolchain) |
| 73 | * [write_file: Write a file to disk.](#func_write_file) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 74 | * [Built-in predefined variables](#predefined_variables) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 75 | * [current_cpu: [string] The processor architecture of the current toolchain.](#var_current_cpu) |
| 76 | * [current_os: [string] The operating system of the current toolchain.](#var_current_os) |
| 77 | * [current_toolchain: [string] Label of the current toolchain.](#var_current_toolchain) |
| 78 | * [default_toolchain: [string] Label of the default toolchain.](#var_default_toolchain) |
James Robinson | 61377e3 | 2020-02-13 15:20:07 -0800 | [diff] [blame] | 79 | * [gn_version: [number] The version of gn.](#var_gn_version) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 80 | * [host_cpu: [string] The processor architecture that GN is running on.](#var_host_cpu) |
| 81 | * [host_os: [string] The operating system that GN is running on.](#var_host_os) |
| 82 | * [invoker: [string] The invoking scope inside a template.](#var_invoker) |
| 83 | * [python_path: [string] Absolute path of Python.](#var_python_path) |
| 84 | * [root_build_dir: [string] Directory where build commands are run.](#var_root_build_dir) |
| 85 | * [root_gen_dir: [string] Directory for the toolchain's generated files.](#var_root_gen_dir) |
| 86 | * [root_out_dir: [string] Root directory for toolchain output files.](#var_root_out_dir) |
| 87 | * [target_cpu: [string] The desired cpu architecture for the build.](#var_target_cpu) |
| 88 | * [target_gen_dir: [string] Directory for a target's generated files.](#var_target_gen_dir) |
| 89 | * [target_name: [string] The name of the current target.](#var_target_name) |
| 90 | * [target_os: [string] The desired operating system for the build.](#var_target_os) |
| 91 | * [target_out_dir: [string] Directory for target output files.](#var_target_out_dir) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 92 | * [Variables you set in targets](#target_variables) |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 93 | * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 94 | * [all_dependent_configs: [label list] Configs to be forced on dependents.](#var_all_dependent_configs) |
| 95 | * [allow_circular_includes_from: [label list] Permit includes from deps.](#var_allow_circular_includes_from) |
| 96 | * [arflags: [string list] Arguments passed to static_library archiver.](#var_arflags) |
| 97 | * [args: [string list] Arguments passed to an action.](#var_args) |
| 98 | * [asmflags: [string list] Flags passed to the assembler.](#var_asmflags) |
| 99 | * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#var_assert_no_deps) |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 100 | * [bridge_header: [string] Path to C/Objective-C compatibility header.](#var_bridge_header) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 101 | * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#var_bundle_contents_dir) |
| 102 | * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#var_bundle_deps_filter) |
| 103 | * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#var_bundle_executable_dir) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 104 | * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#var_bundle_resources_dir) |
| 105 | * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#var_bundle_root_dir) |
| 106 | * [cflags: [string list] Flags passed to all C compiler variants.](#var_cflags) |
| 107 | * [cflags_c: [string list] Flags passed to the C compiler.](#var_cflags_c) |
| 108 | * [cflags_cc: [string list] Flags passed to the C++ compiler.](#var_cflags_cc) |
| 109 | * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#var_cflags_objc) |
| 110 | * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#var_cflags_objcc) |
| 111 | * [check_includes: [boolean] Controls whether a target's files are checked.](#var_check_includes) |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 112 | * [code_signing_args: [string list] [deprecated] Args for the post-processing script.](#var_code_signing_args) |
| 113 | * [code_signing_outputs: [file list] [deprecated] Outputs of the post-processing step.](#var_code_signing_outputs) |
| 114 | * [code_signing_script: [file name] [deprecated] Script for the post-processing step.](#var_code_signing_script) |
| 115 | * [code_signing_sources: [file list] [deprecated] Sources for the post-processing step.](#var_code_signing_sources) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 116 | * [complete_static_lib: [boolean] Links all deps into a static library.](#var_complete_static_lib) |
| 117 | * [configs: [label list] Configs applying to this target or config.](#var_configs) |
| 118 | * [contents: Contents to write to file.](#var_contents) |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 119 | * [crate_name: [string] The name for the compiled crate.](#var_crate_name) |
| 120 | * [crate_root: [string] The root source file for a binary or library.](#var_crate_root) |
| 121 | * [crate_type: [string] The type of linkage to use on a shared_library.](#var_crate_type) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 122 | * [data: [file list] Runtime data file dependencies.](#var_data) |
| 123 | * [data_deps: [label list] Non-linked dependencies.](#var_data_deps) |
| 124 | * [data_keys: [string list] Keys from which to collect metadata.](#var_data_keys) |
| 125 | * [defines: [string list] C preprocessor defines.](#var_defines) |
| 126 | * [depfile: [string] File name for input dependencies for actions.](#var_depfile) |
| 127 | * [deps: [label list] Private linked dependencies.](#var_deps) |
Petr Hosek | a141386 | 2020-01-03 12:54:33 -0800 | [diff] [blame] | 128 | * [externs: [scope] Set of Rust crate-dependency pairs.](#var_externs) |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 129 | * [framework_dirs: [directory list] Additional framework search directories.](#var_framework_dirs) |
| 130 | * [frameworks: [name list] Name of frameworks that must be linked.](#var_frameworks) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 131 | * [friend: [label pattern list] Allow targets to include private headers.](#var_friend) |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 132 | * [gen_deps: [label list] Declares targets that should generate when this one does.](#var_gen_deps) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 133 | * [include_dirs: [directory list] Additional include directories.](#var_include_dirs) |
| 134 | * [inputs: [file list] Additional compile-time dependencies.](#var_inputs) |
| 135 | * [ldflags: [string list] Flags passed to the linker.](#var_ldflags) |
| 136 | * [lib_dirs: [directory list] Additional library directories.](#var_lib_dirs) |
| 137 | * [libs: [string list] Additional libraries to link.](#var_libs) |
| 138 | * [metadata: [scope] Metadata of this target.](#var_metadata) |
Joseph Ryan | ad1e5ce | 2023-04-26 13:48:01 -0700 | [diff] [blame] | 139 | * [mnemonic: [string] Prefix displayed when ninja runs this action.](#var_mnemonic) |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 140 | * [module_name: [string] The name for the compiled module.](#var_module_name) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 141 | * [output_conversion: Data format for generated_file targets.](#var_output_conversion) |
| 142 | * [output_dir: [directory] Directory to put output file in.](#var_output_dir) |
| 143 | * [output_extension: [string] Value to use for the output's file extension.](#var_output_extension) |
| 144 | * [output_name: [string] Name for the output file other than the default.](#var_output_name) |
| 145 | * [output_prefix_override: [boolean] Don't use prefix for output name.](#var_output_prefix_override) |
| 146 | * [outputs: [file list] Output files for actions and copy targets.](#var_outputs) |
| 147 | * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#var_partial_info_plist) |
Petr Hosek | 7a6231e | 2022-10-22 23:14:18 +0000 | [diff] [blame] | 148 | * [pool: [string] Label of the pool used by binary targets and actions.](#var_pool) |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 149 | * [post_processing_args: [string list] Args for the post-processing script.](#var_post_processing_args) |
| 150 | * [post_processing_outputs: [file list] Outputs of the post-processing step.](#var_post_processing_outputs) |
| 151 | * [post_processing_script: [file name] Script for the post-processing step.](#var_post_processing_script) |
| 152 | * [post_processing_sources: [file list] Sources for the post-processing step.](#var_post_processing_sources) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 153 | * [precompiled_header: [string] Header file to precompile.](#var_precompiled_header) |
| 154 | * [precompiled_header_type: [string] "gcc" or "msvc".](#var_precompiled_header_type) |
| 155 | * [precompiled_source: [file name] Source file to precompile.](#var_precompiled_source) |
Sylvain Defresne | e5aee7b | 2023-06-08 10:08:10 -0400 | [diff] [blame] | 156 | * [product_type: [string] Product type for the bundle.](#var_product_type) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 157 | * [public: [file list] Declare public header files for a target.](#var_public) |
| 158 | * [public_configs: [label list] Configs applied to dependents.](#var_public_configs) |
| 159 | * [public_deps: [label list] Declare public dependencies.](#var_public_deps) |
| 160 | * [rebase: [boolean] Rebase collected metadata as files.](#var_rebase) |
| 161 | * [response_file_contents: [string list] Contents of .rsp file for actions.](#var_response_file_contents) |
Max Ihlenfeldt | 5110a7f | 2024-01-22 11:56:47 +0100 | [diff] [blame] | 162 | * [rustflags: [string list] Flags passed to the Rust compiler.](#var_rustflags) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 163 | * [script: [file name] Script file for actions.](#var_script) |
| 164 | * [sources: [file list] Source files for a target.](#var_sources) |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 165 | * [swiftflags: [string list] Flags passed to the swift compiler.](#var_swiftflags) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 166 | * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly) |
Sylvain Defresne | e5aee7b | 2023-06-08 10:08:10 -0400 | [diff] [blame] | 167 | * [transparent: [bool] True if the bundle is transparent.](#var_transparent) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 168 | * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility) |
| 169 | * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys) |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 170 | * [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 171 | * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps) |
Harley Li | 0a9affb | 2020-06-03 10:38:42 -0400 | [diff] [blame] | 172 | * [xcasset_compiler_flags: [string list] Flags passed to xcassets compiler](#var_xcasset_compiler_flags) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 173 | * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes) |
| 174 | * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 175 | * [Other help topics](#other) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 176 | * all: Print all the help at once |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 177 | * [buildargs: How build arguments work.](#buildargs) |
| 178 | * [dotfile: Info about the toplevel .gn file.](#dotfile) |
| 179 | * [execution: Build graph and execution overview.](#execution) |
| 180 | * [grammar: Language and grammar for GN build files.](#grammar) |
Julie Hockett | 81ee196 | 2019-05-13 11:24:42 -0700 | [diff] [blame] | 181 | * [input_conversion: Processing input from exec_script and read_file.](#io_conversion) |
Sylvain Defresne | aff489a | 2020-03-11 18:27:43 +0100 | [diff] [blame] | 182 | * [file_pattern: Matching more than one file.](#file_pattern) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 183 | * [label_pattern: Matching more than one label.](#label_pattern) |
| 184 | * [labels: About labels.](#labels) |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 185 | * [metadata_collection: About metadata and its collection.](#metadata_collection) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 186 | * [ninja_rules: How Ninja build rules are named.](#ninja_rules) |
| 187 | * [nogncheck: Annotating includes for checking.](#nogncheck) |
Julie Hockett | 81ee196 | 2019-05-13 11:24:42 -0700 | [diff] [blame] | 188 | * [output_conversion: Specifies how to transform a value to output.](#io_conversion) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 189 | * [runtime_deps: How runtime dependency computation works.](#runtime_deps) |
| 190 | * [source_expansion: Map sources to outputs for scripts.](#source_expansion) |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 191 | * [switches: Show available command-line switches.](#switch_list) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 192 | |
| 193 | ## <a name="commands"></a>Commands |
| 194 | |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 195 | ### <a name="cmd_analyze"></a>**gn analyze <out_dir> <input_path> <output_path>** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 196 | |
| 197 | ``` |
| 198 | Analyze which targets are affected by a list of files. |
| 199 | |
| 200 | This command takes three arguments: |
| 201 | |
| 202 | out_dir is the path to the build directory. |
| 203 | |
| 204 | input_path is a path to a file containing a JSON object with three fields: |
| 205 | |
| 206 | - "files": A list of the filenames to check. |
| 207 | |
| 208 | - "test_targets": A list of the labels for targets that are needed to run |
| 209 | the tests we wish to run. |
| 210 | |
Nico Weber | dba0172 | 2021-04-06 08:56:46 -0400 | [diff] [blame] | 211 | - "additional_compile_targets" (optional): A list of the labels for targets |
| 212 | that we wish to rebuild, but aren't necessarily needed for testing. The |
| 213 | important difference between this field and "test_targets" is that if an |
| 214 | item in the additional_compile_targets list refers to a group, then any |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 215 | dependencies of that group will be returned if they are out of date, but |
| 216 | the group itself does not need to be. If the dependencies themselves are |
| 217 | groups, the same filtering is repeated. This filtering can be used to |
| 218 | avoid rebuilding dependencies of a group that are unaffected by the input |
| 219 | files. The list may also contain the string "all" to refer to a |
| 220 | pseudo-group that contains every root target in the build graph. |
| 221 | |
| 222 | This filtering behavior is also known as "pruning" the list of compile |
| 223 | targets. |
| 224 | |
Nico Weber | dba0172 | 2021-04-06 08:56:46 -0400 | [diff] [blame] | 225 | If "additional_compile_targets" is absent, it defaults to the empty list. |
| 226 | |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 227 | If input_path is -, input is read from stdin. |
| 228 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 229 | output_path is a path indicating where the results of the command are to be |
| 230 | written. The results will be a file containing a JSON object with one or more |
| 231 | of following fields: |
| 232 | |
| 233 | - "compile_targets": A list of the labels derived from the input |
| 234 | compile_targets list that are affected by the input files. Due to the way |
| 235 | the filtering works for compile targets as described above, this list may |
| 236 | contain targets that do not appear in the input list. |
| 237 | |
| 238 | - "test_targets": A list of the labels from the input test_targets list that |
| 239 | are affected by the input files. This list will be a proper subset of the |
| 240 | input list. |
| 241 | |
| 242 | - "invalid_targets": A list of any names from the input that do not exist in |
| 243 | the build graph. If this list is non-empty, the "error" field will also be |
| 244 | set to "Invalid targets". |
| 245 | |
| 246 | - "status": A string containing one of three values: |
| 247 | |
| 248 | - "Found dependency" |
| 249 | - "No dependency" |
Sylvain Defresne | 7b0690e | 2020-06-26 15:33:01 +0200 | [diff] [blame] | 250 | - "Found dependency (all)" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 251 | |
| 252 | In the first case, the lists returned in compile_targets and test_targets |
| 253 | should be passed to ninja to build. In the second case, nothing was |
| 254 | affected and no build is necessary. In the third case, GN could not |
| 255 | determine the correct answer and returned the input as the output in order |
| 256 | to be safe. |
| 257 | |
| 258 | - "error": This will only be present if an error occurred, and will contain |
| 259 | a string describing the error. This includes cases where the input file is |
| 260 | not in the right format, or contains invalid targets. |
| 261 | |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 262 | If output_path is -, output is written to stdout. |
| 263 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 264 | The command returns 1 if it is unable to read the input file or write the |
| 265 | output file, or if there is something wrong with the build such that gen |
| 266 | would also fail, and 0 otherwise. In particular, it returns 0 even if the |
| 267 | "error" key is non-empty and a non-fatal error occurred. In other words, it |
| 268 | tries really hard to always write something to the output JSON and convey |
| 269 | errors that way rather than via return codes. |
| 270 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 271 | ### <a name="cmd_args"></a>**gn args**: (command-line tool) [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 272 | |
| 273 | ``` |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 274 | Display or configure arguments declared by the build. |
| 275 | |
| 276 | gn args <out_dir> [--list] [--short] [--args] [--overrides-only] |
| 277 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 278 | See also "gn help buildargs" for a more high-level overview of how |
| 279 | build arguments work. |
| 280 | ``` |
| 281 | |
| 282 | #### **Usage** |
| 283 | |
| 284 | ``` |
| 285 | gn args <out_dir> |
| 286 | Open the arguments for the given build directory in an editor. If the |
| 287 | given build directory doesn't exist, it will be created and an empty args |
| 288 | file will be opened in the editor. You would type something like this |
| 289 | into that file: |
| 290 | enable_doom_melon=false |
| 291 | os="android" |
| 292 | |
| 293 | To find your editor on Posix, GN will search the environment variables in |
| 294 | order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command |
| 295 | associated with .txt files. |
| 296 | |
| 297 | Note: you can edit the build args manually by editing the file "args.gn" |
| 298 | in the build directory and then running "gn gen <out_dir>". |
| 299 | |
| 300 | gn args <out_dir> --list[=<exact_arg>] [--short] [--overrides-only] [--json] |
| 301 | Lists all build arguments available in the current configuration, or, if |
| 302 | an exact_arg is specified for the list flag, just that one build |
| 303 | argument. |
| 304 | |
| 305 | The output will list the declaration location, current value for the |
| 306 | build, default value (if different than the current value), and comment |
| 307 | preceding the declaration. |
| 308 | |
| 309 | If --short is specified, only the names and current values will be |
| 310 | printed. |
| 311 | |
| 312 | If --overrides-only is specified, only the names and current values of |
| 313 | arguments that have been overridden (i.e. non-default arguments) will |
| 314 | be printed. Overrides come from the <out_dir>/args.gn file and //.gn |
| 315 | |
| 316 | If --json is specified, the output will be emitted in json format. |
| 317 | JSON schema for output: |
| 318 | [ |
| 319 | { |
| 320 | "name": variable_name, |
| 321 | "current": { |
| 322 | "value": overridden_value, |
| 323 | "file": file_name, |
| 324 | "line": line_no |
| 325 | }, |
| 326 | "default": { |
| 327 | "value": default_value, |
| 328 | "file": file_name, |
| 329 | "line": line_no |
| 330 | }, |
| 331 | "comment": comment_string |
| 332 | }, |
| 333 | ... |
| 334 | ] |
| 335 | ``` |
| 336 | |
| 337 | #### **Examples** |
| 338 | |
| 339 | ``` |
| 340 | gn args out/Debug |
| 341 | Opens an editor with the args for out/Debug. |
| 342 | |
| 343 | gn args out/Debug --list --short |
| 344 | Prints all arguments with their default values for the out/Debug |
| 345 | build. |
| 346 | |
| 347 | gn args out/Debug --list --short --overrides-only |
| 348 | Prints overridden arguments for the out/Debug build. |
| 349 | |
| 350 | gn args out/Debug --list=target_cpu |
| 351 | Prints information about the "target_cpu" argument for the " |
| 352 | "out/Debug |
| 353 | build. |
| 354 | |
| 355 | gn args --list --args="os=\"android\" enable_doom_melon=true" |
| 356 | Prints all arguments with the default values for a build with the |
| 357 | given arguments set (which may affect the values of other |
| 358 | arguments). |
| 359 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 360 | ### <a name="cmd_check"></a>**gn check <out_dir> [<label_pattern>] [\--force] [\--check-generated]** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 361 | |
| 362 | ``` |
| 363 | GN's include header checker validates that the includes for C-like source |
| 364 | files match the build dependency graph. |
| 365 | |
| 366 | "gn check" is the same thing as "gn gen" with the "--check" flag except that |
| 367 | this command does not write out any build files. It's intended to be an easy |
| 368 | way to manually trigger include file checking. |
| 369 | |
| 370 | The <label_pattern> can take exact labels or patterns that match more than |
| 371 | one (although not general regular expressions). If specified, only those |
| 372 | matching targets will be checked. See "gn help label_pattern" for details. |
| 373 | ``` |
| 374 | |
| 375 | #### **Command-specific switches** |
| 376 | |
| 377 | ``` |
Joe Armstrong | 3941394 | 2019-03-15 10:34:03 +0800 | [diff] [blame] | 378 | --check-generated |
| 379 | Generated files are normally not checked since they do not exist |
| 380 | until after a build. With this flag, those generated files that |
| 381 | can be found on disk are also checked. |
James Robinson | 49f5903 | 2020-01-08 14:05:51 -0800 | [diff] [blame] | 382 | |
| 383 | --check-system |
| 384 | Check system style includes (using <angle brackets>) in addition to |
| 385 | "double quote" includes. |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 386 | |
| 387 | --default-toolchain |
| 388 | Normally wildcard targets are matched in all toolchains. This |
| 389 | switch makes wildcard labels with no explicit toolchain reference |
| 390 | only match targets in the default toolchain. |
| 391 | |
| 392 | Non-wildcard inputs with no explicit toolchain specification will |
| 393 | always match only a target in the default toolchain if one exists. |
| 394 | |
| 395 | --force |
| 396 | Ignores specifications of "check_includes = false" and checks all |
| 397 | target's files that match the target label. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 398 | ``` |
| 399 | |
| 400 | #### **What gets checked** |
| 401 | |
| 402 | ``` |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 403 | The .gn file may specify a list of targets to be checked in the list |
Erik Staab | 5d109d7 | 2020-06-29 22:53:23 -0700 | [diff] [blame] | 404 | check_targets (see "gn help dotfile"). Alternatively, the .gn file may |
| 405 | specify a list of targets not to be checked in no_check_targets. If a label |
| 406 | pattern is specified on the command line, neither check_targets or |
| 407 | no_check_targets is used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 408 | |
| 409 | Targets can opt-out from checking with "check_includes = false" (see |
| 410 | "gn help check_includes"). |
| 411 | |
| 412 | For targets being checked: |
| 413 | |
| 414 | - GN opens all C-like source files in the targets to be checked and scans |
| 415 | the top for includes. |
| 416 | |
Joe Armstrong | 3941394 | 2019-03-15 10:34:03 +0800 | [diff] [blame] | 417 | - Generated files (that might not exist yet) are ignored unless |
| 418 | the --check-generated flag is provided. |
| 419 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 420 | - Includes with a "nogncheck" annotation are skipped (see |
| 421 | "gn help nogncheck"). |
| 422 | |
James Robinson | 49f5903 | 2020-01-08 14:05:51 -0800 | [diff] [blame] | 423 | - Includes using "quotes" are always checked. |
| 424 | If system style checking is enabled, includes using <angle brackets> |
| 425 | are also checked. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 426 | |
| 427 | - Include paths are assumed to be relative to any of the "include_dirs" for |
| 428 | the target (including the implicit current dir). |
| 429 | |
| 430 | - GN does not run the preprocessor so will not understand conditional |
| 431 | includes. |
| 432 | |
| 433 | - Only includes matching known files in the build are checked: includes |
| 434 | matching unknown paths are ignored. |
| 435 | |
| 436 | For an include to be valid: |
| 437 | |
| 438 | - The included file must be in the current target, or there must be a path |
| 439 | following only public dependencies to a target with the file in it |
| 440 | ("gn path" is a good way to diagnose problems). |
| 441 | |
| 442 | - There can be multiple targets with an included file: only one needs to be |
| 443 | valid for the include to be allowed. |
| 444 | |
| 445 | - If there are only "sources" in a target, all are considered to be public |
| 446 | and can be included by other targets with a valid public dependency path. |
| 447 | |
| 448 | - If a target lists files as "public", only those files are able to be |
| 449 | included by other targets. Anything in the sources will be considered |
| 450 | private and will not be includable regardless of dependency paths. |
| 451 | |
| 452 | - Outputs from actions are treated like public sources on that target. |
| 453 | |
| 454 | - A target can include headers from a target that depends on it if the |
| 455 | other target is annotated accordingly. See "gn help |
| 456 | allow_circular_includes_from". |
| 457 | ``` |
| 458 | |
| 459 | #### **Advice on fixing problems** |
| 460 | |
| 461 | ``` |
| 462 | If you have a third party project that is difficult to fix or doesn't care |
| 463 | about include checks it's generally best to exclude that target from checking |
| 464 | altogether via "check_includes = false". |
| 465 | |
| 466 | If you have conditional includes, make sure the build conditions and the |
| 467 | preprocessor conditions match, and annotate the line with "nogncheck" (see |
| 468 | "gn help nogncheck" for an example). |
| 469 | |
| 470 | If two targets are hopelessly intertwined, use the |
| 471 | "allow_circular_includes_from" annotation. Ideally each should have identical |
| 472 | dependencies so configs inherited from those dependencies are consistent (see |
| 473 | "gn help allow_circular_includes_from"). |
| 474 | |
| 475 | If you have a standalone header file or files that need to be shared between |
| 476 | a few targets, you can consider making a source_set listing only those |
| 477 | headers as public sources. With only header files, the source set will be a |
| 478 | no-op from a build perspective, but will give a central place to refer to |
| 479 | those headers. That source set's files will still need to pass "gn check" in |
| 480 | isolation. |
| 481 | |
| 482 | In rare cases it makes sense to list a header in more than one target if it |
| 483 | could be considered conceptually a member of both. |
| 484 | ``` |
| 485 | |
| 486 | #### **Examples** |
| 487 | |
| 488 | ``` |
| 489 | gn check out/Debug |
| 490 | Check everything. |
| 491 | |
| 492 | gn check out/Default //foo:bar |
| 493 | Check only the files in the //foo:bar target. |
| 494 | |
| 495 | gn check out/Default "//foo/* |
| 496 | Check only the files in targets in the //foo directory tree. |
| 497 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 498 | ### <a name="cmd_clean"></a>**gn clean <out_dir>...** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 499 | |
| 500 | ``` |
| 501 | Deletes the contents of the output directory except for args.gn and |
| 502 | creates a Ninja build environment sufficient to regenerate the build. |
| 503 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 504 | ### <a name="cmd_clean_stale"></a>**gn clean_stale [\--ninja-executable=...] <out_dir>...** [Back to Top](#gn-reference) |
RJ Ascani | 4e260f1 | 2020-10-19 17:41:51 -0700 | [diff] [blame] | 505 | |
| 506 | ``` |
| 507 | Removes the no longer needed output files from the build directory and prunes |
| 508 | their records from the ninja build log and dependency database. These are |
| 509 | output files that were generated from previous builds, but the current build |
| 510 | graph no longer references them. |
| 511 | |
| 512 | This command requires a ninja executable of at least version 1.10.0. The |
| 513 | executable must be provided by the --ninja-executable switch. |
| 514 | ``` |
| 515 | |
| 516 | #### **Options** |
| 517 | |
| 518 | ``` |
| 519 | --ninja-executable=<string> |
| 520 | Can be used to specify the ninja executable to use. |
| 521 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 522 | ### <a name="cmd_desc"></a>**gn desc** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 523 | |
| 524 | ``` |
Joe Armstrong | b199e54 | 2019-03-21 09:40:08 +0800 | [diff] [blame] | 525 | gn desc <out_dir> <label or pattern> [<what to show>] [--blame] |
| 526 | [--format=json] |
| 527 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 528 | Displays information about a given target or config. The build parameters |
| 529 | will be taken for the build in the given <out_dir>. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 530 | |
| 531 | The <label or pattern> can be a target label, a config label, or a label |
| 532 | pattern (see "gn help label_pattern"). A label pattern will only match |
| 533 | targets. |
| 534 | ``` |
| 535 | |
Nico Weber | 75eb3c7 | 2018-08-28 16:01:52 -0400 | [diff] [blame] | 536 | #### **Possibilities for <what to show>** |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 537 | |
| 538 | ``` |
| 539 | (If unspecified an overall summary will be displayed.) |
| 540 | |
| 541 | all_dependent_configs |
| 542 | allow_circular_includes_from |
| 543 | arflags [--blame] |
| 544 | args |
| 545 | cflags [--blame] |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 546 | cflags_c [--blame] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 547 | cflags_cc [--blame] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 548 | check_includes |
| 549 | configs [--tree] (see below) |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 550 | data_keys |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 551 | defines [--blame] |
| 552 | depfile |
| 553 | deps [--all] [--tree] (see below) |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 554 | framework_dirs |
| 555 | frameworks |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 556 | include_dirs [--blame] |
| 557 | inputs |
| 558 | ldflags [--blame] |
| 559 | lib_dirs |
| 560 | libs |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 561 | metadata |
| 562 | output_conversion |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 563 | outputs |
| 564 | public_configs |
| 565 | public |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 566 | rebase |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 567 | script |
| 568 | sources |
| 569 | testonly |
| 570 | visibility |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 571 | walk_keys |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 572 | weak_frameworks |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 573 | |
| 574 | runtime_deps |
| 575 | Compute all runtime deps for the given target. This is a computed list |
| 576 | and does not correspond to any GN variable, unlike most other values |
| 577 | here. |
| 578 | |
| 579 | The output is a list of file names relative to the build directory. See |
| 580 | "gn help runtime_deps" for how this is computed. This also works with |
| 581 | "--blame" to see the source of the dependency. |
| 582 | ``` |
| 583 | |
| 584 | #### **Shared flags** |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 585 | |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 586 | ``` |
| 587 | --default-toolchain |
| 588 | Normally wildcard targets are matched in all toolchains. This |
| 589 | switch makes wildcard labels with no explicit toolchain reference |
| 590 | only match targets in the default toolchain. |
| 591 | |
| 592 | Non-wildcard inputs with no explicit toolchain specification will |
| 593 | always match only a target in the default toolchain if one exists. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 594 | |
| 595 | --format=json |
| 596 | Format the output as JSON instead of text. |
| 597 | ``` |
| 598 | |
| 599 | #### **Target flags** |
| 600 | |
| 601 | ``` |
| 602 | --blame |
| 603 | Used with any value specified on a config, this will name the config that |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 604 | causes that target to get the flag. This doesn't currently work for libs, |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 605 | lib_dirs, frameworks, weak_frameworks and framework_dirs because those are |
| 606 | inherited and are more complicated to figure out the blame (patches |
| 607 | welcome). |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 608 | ``` |
| 609 | |
| 610 | #### **Configs** |
| 611 | |
| 612 | ``` |
| 613 | The "configs" section will list all configs that apply. For targets this will |
| 614 | include configs specified in the "configs" variable of the target, and also |
| 615 | configs pushed onto this target via public or "all dependent" configs. |
| 616 | |
| 617 | Configs can have child configs. Specifying --tree will show the hierarchy. |
| 618 | ``` |
| 619 | |
| 620 | #### **Printing outputs** |
| 621 | |
| 622 | ``` |
| 623 | The "outputs" section will list all outputs that apply, including the outputs |
| 624 | computed from the tool definition (eg for "executable", "static_library", ... |
| 625 | targets). |
| 626 | ``` |
| 627 | |
| 628 | #### **Printing deps** |
| 629 | |
| 630 | ``` |
| 631 | Deps will include all public, private, and data deps (TODO this could be |
| 632 | clarified and enhanced) sorted in order applying. The following may be used: |
| 633 | |
| 634 | --all |
| 635 | Collects all recursive dependencies and prints a sorted flat list. Also |
| 636 | usable with --tree (see below). |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 637 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 638 | --as=(buildfile|label|output) |
| 639 | How to print targets. |
| 640 | |
| 641 | buildfile |
| 642 | Prints the build files where the given target was declared as |
| 643 | file names. |
| 644 | label (default) |
| 645 | Prints the label of the target. |
| 646 | output |
| 647 | Prints the first output file for the target relative to the |
| 648 | root build directory. |
| 649 | |
| 650 | --testonly=(true|false) |
| 651 | Restrict outputs to targets with the testonly flag set |
| 652 | accordingly. When unspecified, the target's testonly flags are |
| 653 | ignored. |
| 654 | |
| 655 | --tree |
| 656 | Print a dependency tree. By default, duplicates will be elided with "..." |
| 657 | but when --all and -tree are used together, no eliding will be performed. |
| 658 | |
| 659 | The "deps", "public_deps", and "data_deps" will all be included in the |
| 660 | tree. |
| 661 | |
| 662 | Tree output can not be used with the filtering or output flags: --as, |
| 663 | --type, --testonly. |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 664 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 665 | --type=(action|copy|executable|group|loadable_module|shared_library| |
| 666 | source_set|static_library) |
| 667 | Restrict outputs to targets matching the given type. If |
| 668 | unspecified, no filtering will be performed. |
| 669 | ``` |
| 670 | |
| 671 | #### **Note** |
| 672 | |
| 673 | ``` |
| 674 | This command will show the full name of directories and source files, but |
| 675 | when directories and source paths are written to the build file, they will be |
| 676 | adjusted to be relative to the build directory. So the values for paths |
| 677 | displayed by this command won't match (but should mean the same thing). |
| 678 | ``` |
| 679 | |
| 680 | #### **Examples** |
| 681 | |
| 682 | ``` |
| 683 | gn desc out/Debug //base:base |
| 684 | Summarizes the given target. |
| 685 | |
| 686 | gn desc out/Foo :base_unittests deps --tree |
| 687 | Shows a dependency tree of the "base_unittests" project in |
| 688 | the current directory. |
| 689 | |
| 690 | gn desc out/Debug //base defines --blame |
| 691 | Shows defines set for the //base:base target, annotated by where |
| 692 | each one was set from. |
| 693 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 694 | ### <a name="cmd_format"></a>**gn format [\--dump-tree] (\--stdin | <list of build_files...>)** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 695 | |
| 696 | ``` |
| 697 | Formats .gn file to a standard format. |
| 698 | |
| 699 | The contents of some lists ('sources', 'deps', etc.) will be sorted to a |
| 700 | canonical order. To suppress this, you can add a comment of the form "# |
| 701 | NOSORT" immediately preceding the assignment. e.g. |
| 702 | |
| 703 | # NOSORT |
| 704 | sources = [ |
| 705 | "z.cc", |
| 706 | "a.cc", |
| 707 | ] |
| 708 | ``` |
| 709 | |
| 710 | #### **Arguments** |
| 711 | |
| 712 | ``` |
| 713 | --dry-run |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 714 | Prints the list of files that would be reformatted but does not write |
| 715 | anything to disk. This is useful for presubmit/lint-type checks. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 716 | - Exit code 0: successful format, matches on disk. |
| 717 | - Exit code 1: general failure (parse error, etc.) |
| 718 | - Exit code 2: successful format, but differs from on disk. |
| 719 | |
Julie Hockett | d69a9c3 | 2019-01-23 14:36:18 -0800 | [diff] [blame] | 720 | --dump-tree[=( text | json )] |
| 721 | Dumps the parse tree to stdout and does not update the file or print |
| 722 | formatted output. If no format is specified, text format will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 723 | |
| 724 | --stdin |
| 725 | Read input from stdin and write to stdout rather than update a file |
| 726 | in-place. |
RJ Ascani | 4e260f1 | 2020-10-19 17:41:51 -0700 | [diff] [blame] | 727 | |
| 728 | --read-tree=json |
| 729 | Reads an AST from stdin in the format output by --dump-tree=json and |
| 730 | uses that as the parse tree. (The only read-tree format currently |
| 731 | supported is json.) The given .gn file will be overwritten. This can be |
| 732 | used to programmatically transform .gn files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 733 | ``` |
| 734 | |
| 735 | #### **Examples** |
| 736 | ``` |
Julie Hockett | d69a9c3 | 2019-01-23 14:36:18 -0800 | [diff] [blame] | 737 | gn format //some/BUILD.gn //some/other/BUILD.gn //and/another/BUILD.gn |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 738 | gn format some\\BUILD.gn |
| 739 | gn format /abspath/some/BUILD.gn |
| 740 | gn format --stdin |
RJ Ascani | 4e260f1 | 2020-10-19 17:41:51 -0700 | [diff] [blame] | 741 | gn format --read-tree=json //rewritten/BUILD.gn |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 742 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 743 | ### <a name="cmd_gen"></a>**gn gen [\--check] [<ide options>] <out_dir>** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 744 | |
| 745 | ``` |
| 746 | Generates ninja files from the current tree and puts them in the given output |
| 747 | directory. |
| 748 | |
| 749 | The output directory can be a source-repo-absolute path name such as: |
| 750 | //out/foo |
| 751 | Or it can be a directory relative to the current directory such as: |
| 752 | out/foo |
| 753 | |
James Robinson | 49f5903 | 2020-01-08 14:05:51 -0800 | [diff] [blame] | 754 | "gn gen --check" is the same as running "gn check". "gn gen --check=system" is |
| 755 | the same as running "gn check --check-system". See "gn help check" for |
| 756 | documentation on that mode. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 757 | |
| 758 | See "gn help switches" for the common command-line switches. |
| 759 | ``` |
| 760 | |
RJ Ascani | 6966efb | 2020-10-19 16:50:11 -0700 | [diff] [blame] | 761 | #### **General options** |
| 762 | |
| 763 | ``` |
| 764 | --ninja-executable=<string> |
| 765 | Can be used to specify the ninja executable to use. This executable will |
| 766 | be used as an IDE option to indicate which ninja to use for building. This |
| 767 | executable will also be used as part of the gen process for triggering a |
RJ Ascani | 4e260f1 | 2020-10-19 17:41:51 -0700 | [diff] [blame] | 768 | restat on generated ninja files and for use with --clean-stale. |
| 769 | |
| 770 | --clean-stale |
| 771 | This option will cause no longer needed output files to be removed from |
| 772 | the build directory, and their records pruned from the ninja build log and |
| 773 | dependency database after the ninja build graph has been generated. This |
| 774 | option requires a ninja executable of at least version 1.10.0. It can be |
| 775 | provided by the --ninja-executable switch. Also see "gn help clean_stale". |
RJ Ascani | 6966efb | 2020-10-19 16:50:11 -0700 | [diff] [blame] | 776 | ``` |
| 777 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 778 | #### **IDE options** |
| 779 | |
| 780 | ``` |
Harley Li | 0a9affb | 2020-06-03 10:38:42 -0400 | [diff] [blame] | 781 | GN optionally generates files for IDE. Files won't be overwritten if their |
| 782 | contents don't change. Possibilities for <ide options> |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 783 | |
| 784 | --ide=<ide_name> |
| 785 | Generate files for an IDE. Currently supported values: |
| 786 | "eclipse" - Eclipse CDT settings file. |
| 787 | "vs" - Visual Studio project/solution files. |
Chunbo Hua | 4a8016d | 2025-02-20 10:31:32 +0800 | [diff] [blame] | 788 | (default Visual Studio version: 2022) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 789 | "vs2013" - Visual Studio 2013 project/solution files. |
| 790 | "vs2015" - Visual Studio 2015 project/solution files. |
| 791 | "vs2017" - Visual Studio 2017 project/solution files. |
Sylvain Defresne | d46f92f | 2019-04-03 16:14:29 +0200 | [diff] [blame] | 792 | "vs2019" - Visual Studio 2019 project/solution files. |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 793 | "vs2022" - Visual Studio 2022 project/solution files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 794 | "xcode" - Xcode workspace/solution files. |
| 795 | "qtcreator" - QtCreator project files. |
| 796 | "json" - JSON file containing target information |
| 797 | |
| 798 | --filters=<path_prefixes> |
| 799 | Semicolon-separated list of label patterns used to limit the set of |
| 800 | generated projects (see "gn help label_pattern"). Only matching targets |
| 801 | and their dependencies will be included in the solution. Only used for |
| 802 | Visual Studio, Xcode and JSON. |
| 803 | ``` |
| 804 | |
| 805 | #### **Visual Studio Flags** |
| 806 | |
| 807 | ``` |
| 808 | --sln=<file_name> |
| 809 | Override default sln file name ("all"). Solution file is written to the |
| 810 | root build directory. |
| 811 | |
| 812 | --no-deps |
| 813 | Don't include targets dependencies to the solution. Changes the way how |
| 814 | --filters option works. Only directly matching targets are included. |
| 815 | |
| 816 | --winsdk=<sdk_version> |
| 817 | Use the specified Windows 10 SDK version to generate project files. |
| 818 | As an example, "10.0.15063.0" can be specified to use Creators Update SDK |
| 819 | instead of the default one. |
| 820 | |
Shezan Baig | c0a2d23 | 2021-07-01 00:29:49 -0400 | [diff] [blame] | 821 | --ninja-executable=<string> |
| 822 | Can be used to specify the ninja executable to use when building. |
| 823 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 824 | --ninja-extra-args=<string> |
| 825 | This string is passed without any quoting to the ninja invocation |
| 826 | command-line. Can be used to configure ninja flags, like "-j". |
| 827 | ``` |
| 828 | |
| 829 | #### **Xcode Flags** |
| 830 | |
| 831 | ``` |
Sylvain Defresne | db55efd | 2020-06-16 12:23:03 +0200 | [diff] [blame] | 832 | --xcode-project=<file_name> |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 833 | Override default Xcode project file name ("all"). The project file is |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 834 | written to the root build directory. |
| 835 | |
Sylvain Defresne | 7b0690e | 2020-06-26 15:33:01 +0200 | [diff] [blame] | 836 | --xcode-build-system=<value> |
| 837 | Configure the build system to use for the Xcode project. Supported |
| 838 | values are (default to "legacy"): |
| 839 | "legacy" - Legacy Build system |
| 840 | "new" - New Build System |
| 841 | |
Sylvain Defresne | 8883070 | 2022-06-20 17:11:49 +0200 | [diff] [blame] | 842 | --xcode-configs=<config_name_list> |
| 843 | Configure the list of build configuration supported by the generated |
| 844 | project. If specified, must be a list of semicolon-separated strings. |
Ricardo Ribalda | 85bd0a6 | 2023-11-11 16:30:19 +0000 | [diff] [blame] | 845 | If omitted, a single configuration will be used in the generated |
Sylvain Defresne | 8883070 | 2022-06-20 17:11:49 +0200 | [diff] [blame] | 846 | project derived from the build directory. |
| 847 | |
| 848 | --xcode-config-build-dir=<string> |
| 849 | If present, must be a path relative to the source directory. It will |
Ricardo Ribalda | 85bd0a6 | 2023-11-11 16:30:19 +0000 | [diff] [blame] | 850 | default to $root_out_dir if omitted. The path is assumed to point to |
Sylvain Defresne | 8883070 | 2022-06-20 17:11:49 +0200 | [diff] [blame] | 851 | the directory where ninja needs to be invoked. This variable can be |
| 852 | used to build for multiple configuration / platform / environment from |
| 853 | the same generated Xcode project (assuming that the user has created a |
| 854 | gn build directory with the correct args.gn for each). |
| 855 | |
| 856 | One useful value is to use Xcode variables such as '${CONFIGURATION}' |
| 857 | or '${EFFECTIVE_PLATFORM}'. |
| 858 | |
Sylvain Defresne | ae474cc | 2022-06-21 14:44:02 +0200 | [diff] [blame] | 859 | --xcode-additional-files-patterns=<pattern_list> |
| 860 | If present, must be a list of semicolon-separated file patterns. It |
| 861 | will be used to add all files matching the pattern located in the |
| 862 | source tree to the project. It can be used to add, e.g. documentation |
| 863 | files to the project to allow easily edit them. |
| 864 | |
| 865 | --xcode-additional-files-roots=<path_list> |
| 866 | If present, must be a list of semicolon-separated paths. It will be used |
Ricardo Ribalda | 85bd0a6 | 2023-11-11 16:30:19 +0000 | [diff] [blame] | 867 | as roots when looking for additional files to add. If omitted, defaults |
Sylvain Defresne | ae474cc | 2022-06-21 14:44:02 +0200 | [diff] [blame] | 868 | to "//". |
| 869 | |
Sylvain Defresne | aff489a | 2020-03-11 18:27:43 +0100 | [diff] [blame] | 870 | --ninja-executable=<string> |
| 871 | Can be used to specify the ninja executable to use when building. |
| 872 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 873 | --ninja-extra-args=<string> |
| 874 | This string is passed without any quoting to the ninja invocation |
| 875 | command-line. Can be used to configure ninja flags, like "-j". |
| 876 | |
Sylvain Defresne | e431b2f | 2020-11-04 10:49:47 +0100 | [diff] [blame] | 877 | --ide-root-target=<target_name> |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 878 | Name of the target corresponding to "All" target in Xcode. If unset, |
| 879 | "All" invokes ninja without any target and builds everything. |
| 880 | ``` |
| 881 | |
| 882 | #### **QtCreator Flags** |
| 883 | |
| 884 | ``` |
Sylvain Defresne | e431b2f | 2020-11-04 10:49:47 +0100 | [diff] [blame] | 885 | --ide-root-target=<target_name> |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 886 | Name of the root target for which the QtCreator project will be generated |
| 887 | to contain files of it and its dependencies. If unset, the whole build |
| 888 | graph will be emitted. |
| 889 | ``` |
| 890 | |
| 891 | #### **Eclipse IDE Support** |
| 892 | |
| 893 | ``` |
| 894 | GN DOES NOT generate Eclipse CDT projects. Instead, it generates a settings |
| 895 | file which can be imported into an Eclipse CDT project. The XML file contains |
| 896 | a list of include paths and defines. Because GN does not generate a full |
| 897 | .cproject definition, it is not possible to properly define includes/defines |
| 898 | for each file individually. Instead, one set of includes/defines is generated |
| 899 | for the entire project. This works fairly well but may still result in a few |
| 900 | indexer issues here and there. |
| 901 | ``` |
| 902 | |
| 903 | #### **Generic JSON Output** |
| 904 | |
| 905 | ``` |
| 906 | Dumps target information to a JSON file and optionally invokes a |
| 907 | python script on the generated file. See the comments at the beginning |
| 908 | of json_project_writer.cc and desc_builder.cc for an overview of the JSON |
| 909 | file format. |
| 910 | |
| 911 | --json-file-name=<json_file_name> |
| 912 | Overrides default file name (project.json) of generated JSON file. |
| 913 | |
| 914 | --json-ide-script=<path_to_python_script> |
Harley Li | 0a9affb | 2020-06-03 10:38:42 -0400 | [diff] [blame] | 915 | Executes python script after the JSON file is generated or updated with |
| 916 | new content. Path can be project absolute (//), system absolute (/) or |
| 917 | relative, in which case the output directory will be base. Path to |
| 918 | generated JSON file will be first argument when invoking script. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 919 | |
| 920 | --json-ide-script-args=<argument> |
David 'Digit' Turner | 6253a39 | 2024-02-20 19:55:35 +0100 | [diff] [blame] | 921 | Optional second argument that will be passed to executed script. |
| 922 | ``` |
| 923 | |
| 924 | #### **Ninja Outputs** |
| 925 | |
| 926 | ``` |
| 927 | The --ninja-outputs-file=<FILE> option dumps a JSON file that maps GN labels |
| 928 | to their Ninja output paths. This can be later processed to build an index |
| 929 | to convert between Ninja targets and GN ones before or after the build itself. |
| 930 | It looks like: |
| 931 | |
| 932 | { |
| 933 | "label1": [ |
| 934 | "path1", |
| 935 | "path2" |
| 936 | ], |
| 937 | "label2": [ |
| 938 | "path3" |
| 939 | ] |
| 940 | } |
| 941 | |
| 942 | --ninja-outputs-script=<path_to_python_script> |
| 943 | Executes python script after the outputs file is generated or updated |
| 944 | with new content. Path can be project absolute (//), system absolute (/) or |
| 945 | relative, in which case the output directory will be base. Path to |
| 946 | generated file will be first argument when invoking script. |
| 947 | |
| 948 | --ninja-outputs-script-args=<argument> |
| 949 | Optional second argument that will be passed to executed script. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 950 | ``` |
Julie Hockett | 11e0991 | 2018-07-31 13:15:08 -0700 | [diff] [blame] | 951 | |
| 952 | #### **Compilation Database** |
| 953 | |
| 954 | ``` |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 955 | --export-rust-project |
| 956 | Produces a rust-project.json file in the root of the build directory |
| 957 | This is used for various tools in the Rust ecosystem allowing for the |
| 958 | replay of individual compilations independent of the build system. |
| 959 | This is an unstable format and likely to change without warning. |
| 960 | |
Brett Wilson | 1da84bb | 2022-09-14 15:35:29 -0700 | [diff] [blame] | 961 | --add-export-compile-commands=<label_pattern> |
| 962 | Adds an additional label pattern (see "gn help label_pattern") of a |
| 963 | target to add to the compilation database. This pattern is appended to any |
| 964 | list values specified in the export_compile_commands variable in the |
| 965 | .gn file (see "gn help dotfile"). This allows the user to add additional |
| 966 | targets to the compilation database that the project doesn't add by default. |
| 967 | |
| 968 | To add more than one value, specify this switch more than once. Each |
| 969 | invocation adds an additional label pattern. |
| 970 | |
| 971 | Example: |
| 972 | --add-export-compile-commands=//tools:my_tool |
| 973 | --add-export-compile-commands="//base/*" |
| 974 | |
Robert Sesek | 252674d | 2019-05-10 14:08:52 -0400 | [diff] [blame] | 975 | --export-compile-commands[=<target_name1,target_name2...>] |
Brett Wilson | 1da84bb | 2022-09-14 15:35:29 -0700 | [diff] [blame] | 976 | DEPRECATED https://bugs.chromium.org/p/gn/issues/detail?id=302. |
| 977 | Please use --add-export-compile-commands for per-user configuration, and |
| 978 | the "export_compile_commands" value in the project-level .gn file (see |
| 979 | "gn help dotfile") for per-project configuration. |
| 980 | |
| 981 | Overrides the value of the export_compile_commands in the .gn file (see |
| 982 | "gn help dotfile") as well as the --add-export-compile-commands switch. |
| 983 | |
| 984 | Unlike the .gn setting, this switch takes a legacy format which is a list |
| 985 | of target names that are matched in any directory. For example, "foo" will |
| 986 | match: |
| 987 | - "//path/to/src:foo" |
| 988 | - "//other/path:foo" |
| 989 | - "//foo:foo" |
Aaron Wood | 2d9fd62 | 2020-12-10 21:28:19 -0800 | [diff] [blame] | 990 | and not match: |
Brett Wilson | 1da84bb | 2022-09-14 15:35:29 -0700 | [diff] [blame] | 991 | - "//foo:bar" |
Julie Hockett | 11e0991 | 2018-07-31 13:15:08 -0700 | [diff] [blame] | 992 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 993 | ### <a name="cmd_help"></a>**gn help <anything>** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 994 | |
| 995 | ``` |
| 996 | Yo dawg, I heard you like help on your help so I put help on the help in the |
| 997 | help. |
| 998 | |
| 999 | You can also use "all" as the parameter to get all help at once. |
| 1000 | ``` |
| 1001 | |
| 1002 | #### **Switches** |
| 1003 | |
| 1004 | ``` |
| 1005 | --markdown |
| 1006 | Format output in markdown syntax. |
| 1007 | ``` |
| 1008 | |
| 1009 | #### **Example** |
| 1010 | |
| 1011 | ``` |
| 1012 | gn help --markdown all |
| 1013 | Dump all help to stdout in markdown format. |
| 1014 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1015 | ### <a name="cmd_ls"></a>**gn ls <out_dir> [<label_pattern>] [\--default-toolchain] [\--as=...]** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1016 | ``` |
| 1017 | [--type=...] [--testonly=...] |
| 1018 | |
| 1019 | Lists all targets matching the given pattern for the given build directory. |
| 1020 | By default, only targets in the default toolchain will be matched unless a |
| 1021 | toolchain is explicitly supplied. |
| 1022 | |
| 1023 | If the label pattern is unspecified, list all targets. The label pattern is |
| 1024 | not a general regular expression (see "gn help label_pattern"). If you need |
| 1025 | more complex expressions, pipe the result through grep. |
| 1026 | ``` |
| 1027 | |
| 1028 | #### **Options** |
| 1029 | |
| 1030 | ``` |
| 1031 | --as=(buildfile|label|output) |
| 1032 | How to print targets. |
| 1033 | |
| 1034 | buildfile |
| 1035 | Prints the build files where the given target was declared as |
| 1036 | file names. |
| 1037 | label (default) |
| 1038 | Prints the label of the target. |
| 1039 | output |
| 1040 | Prints the first output file for the target relative to the |
| 1041 | root build directory. |
| 1042 | |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 1043 | --default-toolchain |
| 1044 | Normally wildcard targets are matched in all toolchains. This |
| 1045 | switch makes wildcard labels with no explicit toolchain reference |
| 1046 | only match targets in the default toolchain. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1047 | |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 1048 | Non-wildcard inputs with no explicit toolchain specification will |
| 1049 | always match only a target in the default toolchain if one exists. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1050 | |
| 1051 | --testonly=(true|false) |
| 1052 | Restrict outputs to targets with the testonly flag set |
| 1053 | accordingly. When unspecified, the target's testonly flags are |
| 1054 | ignored. |
| 1055 | |
| 1056 | --type=(action|copy|executable|group|loadable_module|shared_library| |
| 1057 | source_set|static_library) |
| 1058 | Restrict outputs to targets matching the given type. If |
| 1059 | unspecified, no filtering will be performed. |
| 1060 | ``` |
| 1061 | |
| 1062 | #### **Examples** |
| 1063 | |
| 1064 | ``` |
| 1065 | gn ls out/Debug |
| 1066 | Lists all targets in the default toolchain. |
| 1067 | |
| 1068 | gn ls out/Debug "//base/*" |
| 1069 | Lists all targets in the directory base and all subdirectories. |
| 1070 | |
| 1071 | gn ls out/Debug "//base:*" |
| 1072 | Lists all targets defined in //base/BUILD.gn. |
| 1073 | |
| 1074 | gn ls out/Debug //base --as=output |
| 1075 | Lists the build output file for //base:base |
| 1076 | |
| 1077 | gn ls out/Debug --type=executable |
| 1078 | Lists all executables produced by the build. |
| 1079 | |
| 1080 | gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug |
| 1081 | Builds all targets in //base and all subdirectories. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1082 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1083 | ### <a name="cmd_meta"></a>**gn meta** [Back to Top](#gn-reference) |
Joe Armstrong | b199e54 | 2019-03-21 09:40:08 +0800 | [diff] [blame] | 1084 | |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1085 | ``` |
Joe Armstrong | b199e54 | 2019-03-21 09:40:08 +0800 | [diff] [blame] | 1086 | gn meta <out_dir> <target>* --data=<key>[,<key>*]* [--walk=<key>[,<key>*]*] |
| 1087 | [--rebase=<dest dir>] |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1088 | |
| 1089 | Lists collected metaresults of all given targets for the given data key(s), |
| 1090 | collecting metadata dependencies as specified by the given walk key(s). |
| 1091 | |
| 1092 | See `gn help generated_file` for more information on the walk. |
| 1093 | ``` |
| 1094 | |
| 1095 | #### **Arguments** |
| 1096 | |
| 1097 | ``` |
| 1098 | <target(s)> |
| 1099 | A list of target labels from which to initiate the walk. |
| 1100 | |
| 1101 | --data |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 1102 | A comma-separated list of keys from which to extract data. In each target |
| 1103 | walked, its metadata scope is checked for the presence of these keys. If |
| 1104 | present, the contents of those variable in the scope are appended to the |
| 1105 | results list. |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1106 | |
| 1107 | --walk (optional) |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 1108 | A comma-separated list of keys from which to control the walk. In each |
| 1109 | target walked, its metadata scope is checked for the presence of any of |
| 1110 | these keys. If present, the contents of those variables is checked to ensure |
| 1111 | that it is a label of a valid dependency of the target and then added to the |
| 1112 | set of targets to walk. If the empty string ("") is present in any of these |
| 1113 | keys, all deps and data_deps are added to the walk set. |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1114 | |
| 1115 | --rebase (optional) |
| 1116 | A destination directory onto which to rebase any paths found. If set, all |
| 1117 | collected metadata will be rebased onto this path. This option will throw errors |
| 1118 | if collected metadata is not a list of strings. |
| 1119 | ``` |
| 1120 | |
| 1121 | #### **Examples** |
| 1122 | |
| 1123 | ``` |
| 1124 | gn meta out/Debug "//base/foo" --data=files |
| 1125 | Lists collected metaresults for the `files` key in the //base/foo:foo |
| 1126 | target and all of its dependency tree. |
| 1127 | |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 1128 | gn meta out/Debug "//base/foo" --data=files,other |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1129 | Lists collected metaresults for the `files` and `other` keys in the |
| 1130 | //base/foo:foo target and all of its dependency tree. |
| 1131 | |
| 1132 | gn meta out/Debug "//base/foo" --data=files --walk=stop |
| 1133 | Lists collected metaresults for the `files` key in the //base/foo:foo |
| 1134 | target and all of the dependencies listed in the `stop` key (and so on). |
| 1135 | |
| 1136 | gn meta out/Debug "//base/foo" --data=files --rebase="/" |
| 1137 | Lists collected metaresults for the `files` key in the //base/foo:foo |
| 1138 | target and all of its dependency tree, rebasing the strings in the `files` |
| 1139 | key onto the source directory of the target's declaration relative to "/". |
| 1140 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1141 | ### <a name="cmd_outputs"></a>**gn outputs <out_dir> <list of target or file names...>** [Back to Top](#gn-reference) |
Brett Wilson | 7439709 | 2020-03-18 16:34:14 -0700 | [diff] [blame] | 1142 | |
| 1143 | ``` |
| 1144 | Lists the output files corresponding to the given target(s) or file name(s). |
| 1145 | There can be multiple outputs because there can be more than one output |
| 1146 | generated by a build step, and there can be more than one toolchain matched. |
| 1147 | You can also list multiple inputs which will generate a union of all the |
| 1148 | outputs from those inputs. |
| 1149 | |
| 1150 | - The input target/file names are relative to the current directory. |
| 1151 | |
| 1152 | - The output file names are relative to the root build directory. |
| 1153 | |
| 1154 | This command is useful for finding a ninja command that will build only a |
| 1155 | portion of the build. |
| 1156 | ``` |
| 1157 | |
| 1158 | #### **Target outputs** |
| 1159 | |
| 1160 | ``` |
| 1161 | If the parameter is a target name that includes a toolchain, it will match |
| 1162 | only that target in that toolchain. If no toolchain is specified, it will |
| 1163 | match all targets with that name in any toolchain. |
| 1164 | |
| 1165 | The result will be the outputs specified by that target which could be a |
| 1166 | library, executable, output of an action, a stamp file, etc. |
| 1167 | ``` |
| 1168 | |
| 1169 | #### **File outputs** |
| 1170 | |
| 1171 | ``` |
| 1172 | If the parameter is a file name it will compute the output for that compile |
| 1173 | step for all targets in all toolchains that contain that file as a source |
| 1174 | file. |
| 1175 | |
| 1176 | If the source is not compiled (e.g. a header or text file), the command will |
| 1177 | produce no output. |
| 1178 | |
| 1179 | If the source is listed as an "input" to a binary target or action will |
| 1180 | resolve to that target's outputs. |
| 1181 | ``` |
| 1182 | |
| 1183 | #### **Example** |
| 1184 | |
| 1185 | ``` |
| 1186 | gn outputs out/debug some/directory:some_target |
| 1187 | Find the outputs of a given target. |
| 1188 | |
| 1189 | gn outputs out/debug src/project/my_file.cc | xargs ninja -C out/debug |
| 1190 | Compiles just the given source file in all toolchains it's referenced in. |
| 1191 | |
| 1192 | git diff --name-only | xargs gn outputs out/x64 | xargs ninja -C out/x64 |
| 1193 | Compiles all files changed in git. |
| 1194 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1195 | ### <a name="cmd_path"></a>**gn path <out_dir> <target_one> <target_two>** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1196 | |
| 1197 | ``` |
| 1198 | Finds paths of dependencies between two targets. Each unique path will be |
| 1199 | printed in one group, and groups will be separate by newlines. The two |
| 1200 | targets can appear in either order (paths will be found going in either |
| 1201 | direction). |
| 1202 | |
| 1203 | By default, a single path will be printed. If there is a path with only |
| 1204 | public dependencies, the shortest public path will be printed. Otherwise, the |
| 1205 | shortest path using either public or private dependencies will be printed. If |
| 1206 | --with-data is specified, data deps will also be considered. If there are |
| 1207 | multiple shortest paths, an arbitrary one will be selected. |
| 1208 | ``` |
| 1209 | |
| 1210 | #### **Interesting paths** |
| 1211 | |
| 1212 | ``` |
| 1213 | In a large project, there can be 100's of millions of unique paths between a |
| 1214 | very high level and a common low-level target. To make the output more useful |
| 1215 | (and terminate in a reasonable time), GN will not revisit sub-paths |
| 1216 | previously known to lead to the target. |
| 1217 | ``` |
| 1218 | |
| 1219 | #### **Options** |
| 1220 | |
| 1221 | ``` |
| 1222 | --all |
| 1223 | Prints all "interesting" paths found rather than just the first one. |
| 1224 | Public paths will be printed first in order of increasing length, followed |
| 1225 | by non-public paths in order of increasing length. |
| 1226 | |
| 1227 | --public |
| 1228 | Considers only public paths. Can't be used with --with-data. |
| 1229 | |
| 1230 | --with-data |
| 1231 | Additionally follows data deps. Without this flag, only public and private |
| 1232 | linked deps will be followed. Can't be used with --public. |
| 1233 | ``` |
| 1234 | |
| 1235 | #### **Example** |
| 1236 | |
| 1237 | ``` |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 1238 | gn path out/Default //base //gn |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1239 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1240 | ### <a name="cmd_refs"></a>**gn refs** [Back to Top](#gn-reference) |
Joe Armstrong | b199e54 | 2019-03-21 09:40:08 +0800 | [diff] [blame] | 1241 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1242 | ``` |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 1243 | gn refs <out_dir> (<label_pattern>|<label>|<file>|@<response_file>)* [--all] |
| 1244 | [--default-toolchain] [--as=...] [--testonly=...] [--type=...] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1245 | |
| 1246 | Finds reverse dependencies (which targets reference something). The input is |
| 1247 | a list containing: |
| 1248 | |
| 1249 | - Target label: The result will be which targets depend on it. |
| 1250 | |
| 1251 | - Config label: The result will be which targets list the given config in |
| 1252 | its "configs" or "public_configs" list. |
| 1253 | |
| 1254 | - Label pattern: The result will be which targets depend on any target |
| 1255 | matching the given pattern. Patterns will not match configs. These are not |
| 1256 | general regular expressions, see "gn help label_pattern" for details. |
| 1257 | |
| 1258 | - File name: The result will be which targets list the given file in its |
| 1259 | "inputs", "sources", "public", "data", or "outputs". Any input that does |
| 1260 | not contain wildcards and does not match a target or a config will be |
| 1261 | treated as a file. |
| 1262 | |
| 1263 | - Response file: If the input starts with an "@", it will be interpreted as |
| 1264 | a path to a file containing a list of labels or file names, one per line. |
| 1265 | This allows us to handle long lists of inputs without worrying about |
| 1266 | command line limits. |
| 1267 | ``` |
| 1268 | |
| 1269 | #### **Options** |
| 1270 | |
| 1271 | ``` |
| 1272 | --all |
| 1273 | When used without --tree, will recurse and display all unique |
| 1274 | dependencies of the given targets. For example, if the input is a target, |
| 1275 | this will output all targets that depend directly or indirectly on the |
| 1276 | input. If the input is a file, this will output all targets that depend |
| 1277 | directly or indirectly on that file. |
| 1278 | |
| 1279 | When used with --tree, turns off eliding to show a complete tree. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1280 | |
| 1281 | --as=(buildfile|label|output) |
| 1282 | How to print targets. |
| 1283 | |
| 1284 | buildfile |
| 1285 | Prints the build files where the given target was declared as |
| 1286 | file names. |
| 1287 | label (default) |
| 1288 | Prints the label of the target. |
| 1289 | output |
| 1290 | Prints the first output file for the target relative to the |
| 1291 | root build directory. |
| 1292 | |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 1293 | --default-toolchain |
| 1294 | Normally wildcard targets are matched in all toolchains. This |
| 1295 | switch makes wildcard labels with no explicit toolchain reference |
| 1296 | only match targets in the default toolchain. |
| 1297 | |
| 1298 | Non-wildcard inputs with no explicit toolchain specification will |
| 1299 | always match only a target in the default toolchain if one exists. |
| 1300 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1301 | -q |
| 1302 | Quiet. If nothing matches, don't print any output. Without this option, if |
| 1303 | there are no matches there will be an informational message printed which |
| 1304 | might interfere with scripts processing the output. |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 1305 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1306 | --testonly=(true|false) |
| 1307 | Restrict outputs to targets with the testonly flag set |
| 1308 | accordingly. When unspecified, the target's testonly flags are |
| 1309 | ignored. |
| 1310 | |
| 1311 | --tree |
| 1312 | Outputs a reverse dependency tree from the given target. Duplicates will |
| 1313 | be elided. Combine with --all to see a full dependency tree. |
| 1314 | |
| 1315 | Tree output can not be used with the filtering or output flags: --as, |
| 1316 | --type, --testonly. |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 1317 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1318 | --type=(action|copy|executable|group|loadable_module|shared_library| |
| 1319 | source_set|static_library) |
| 1320 | Restrict outputs to targets matching the given type. If |
| 1321 | unspecified, no filtering will be performed. |
| 1322 | ``` |
| 1323 | |
| 1324 | #### **Examples (target input)** |
| 1325 | |
| 1326 | ``` |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 1327 | gn refs out/Debug //gn:gn |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1328 | Find all targets depending on the given exact target name. |
| 1329 | |
Lei Zhang | 415b3b1 | 2024-04-02 09:29:36 -0700 | [diff] [blame] | 1330 | gn refs out/Debug //base:i18n --as=buildfile | xargs gvim |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1331 | Edit all .gn files containing references to //base:i18n |
| 1332 | |
| 1333 | gn refs out/Debug //base --all |
| 1334 | List all targets depending directly or indirectly on //base:base. |
| 1335 | |
| 1336 | gn refs out/Debug "//base/*" |
| 1337 | List all targets depending directly on any target in //base or |
| 1338 | its subdirectories. |
| 1339 | |
| 1340 | gn refs out/Debug "//base:*" |
| 1341 | List all targets depending directly on any target in |
| 1342 | //base/BUILD.gn. |
| 1343 | |
| 1344 | gn refs out/Debug //base --tree |
| 1345 | Print a reverse dependency tree of //base:base |
| 1346 | ``` |
| 1347 | |
| 1348 | #### **Examples (file input)** |
| 1349 | |
| 1350 | ``` |
| 1351 | gn refs out/Debug //base/macros.h |
| 1352 | Print target(s) listing //base/macros.h as a source. |
| 1353 | |
| 1354 | gn refs out/Debug //base/macros.h --tree |
| 1355 | Display a reverse dependency tree to get to the given file. This |
| 1356 | will show how dependencies will reference that file. |
| 1357 | |
| 1358 | gn refs out/Debug //base/macros.h //base/at_exit.h --all |
| 1359 | Display all unique targets with some dependency path to a target |
| 1360 | containing either of the given files as a source. |
| 1361 | |
| 1362 | gn refs out/Debug //base/macros.h --testonly=true --type=executable |
| 1363 | --all --as=output |
| 1364 | Display the executable file names of all test executables |
| 1365 | potentially affected by a change to the given file. |
| 1366 | ``` |
| 1367 | ## <a name="targets"></a>Target declarations |
| 1368 | |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1369 | ### <a name="func_action"></a>**action**: Declare a target that runs a script a single time. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1370 | |
| 1371 | ``` |
| 1372 | This target type allows you to run a script a single time to produce one or |
| 1373 | more output files. If you want to run a script once for each of a set of |
| 1374 | input files, see "gn help action_foreach". |
| 1375 | ``` |
| 1376 | |
| 1377 | #### **Inputs** |
| 1378 | |
| 1379 | ``` |
| 1380 | In an action the "sources" and "inputs" are treated the same: they're both |
| 1381 | input dependencies on script execution with no special handling. If you want |
| 1382 | to pass the sources to your script, you must do so explicitly by including |
| 1383 | them in the "args". Note also that this means there is no special handling of |
| 1384 | paths since GN doesn't know which of the args are paths and not. You will |
| 1385 | want to use rebase_path() to convert paths to be relative to the |
| 1386 | root_build_dir. |
| 1387 | |
| 1388 | You can dynamically write input dependencies (for incremental rebuilds if an |
| 1389 | input file changes) by writing a depfile when the script is run (see "gn help |
| 1390 | depfile"). This is more flexible than "inputs". |
| 1391 | |
| 1392 | If the command line length is very long, you can use response files to pass |
| 1393 | args to your script. See "gn help response_file_contents". |
| 1394 | |
| 1395 | It is recommended you put inputs to your script in the "sources" variable, |
| 1396 | and stuff like other Python files required to run your script in the "inputs" |
| 1397 | variable. |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 1398 | |
Adrian Taylor | c0d4f8b | 2022-03-25 15:05:57 -0700 | [diff] [blame] | 1399 | Actions can take the configs and public_configs lists, as well as any of the |
| 1400 | configs variables (defines, include_dirs, etc.) set directly on the target. |
| 1401 | These behave exactly as they would on a binary target and can be accessed |
| 1402 | using substitution patterns in the script args (see "gn help args") to |
| 1403 | implement custom compiler-like tools. |
| 1404 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1405 | The "deps" and "public_deps" for an action will always be |
| 1406 | completed before any part of the action is run so it can depend on |
| 1407 | the output of previous steps. The "data_deps" will be built if the |
| 1408 | action is built, but may not have completed before all steps of the |
| 1409 | action are started. This can give additional parallelism in the build |
| 1410 | for runtime-only dependencies. |
| 1411 | ``` |
| 1412 | |
| 1413 | #### **Outputs** |
| 1414 | |
| 1415 | ``` |
| 1416 | You should specify files created by your script by specifying them in the |
| 1417 | "outputs". |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 1418 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1419 | The script will be executed with the given arguments with the current |
| 1420 | directory being that of the root build directory. If you pass files |
| 1421 | to your script, see "gn help rebase_path" for how to convert |
| 1422 | file names to be relative to the build directory (file names in the |
| 1423 | sources, outputs, and inputs will be all treated as relative to the |
| 1424 | current build file and converted as needed automatically). |
Brett Wilson | 1e1ea9d | 2020-02-04 15:34:05 -0800 | [diff] [blame] | 1425 | |
| 1426 | GN sets Ninja's flag 'restat = 1` for all action commands. This means |
| 1427 | that Ninja will check the timestamp of the output after the action |
| 1428 | completes. If output timestamp is unchanged, the step will be treated |
| 1429 | as if it never needed to be rebuilt, potentially eliminating some |
| 1430 | downstream steps for incremental builds. Scripts can improve build |
| 1431 | performance by taking care not to change the timstamp of the output |
| 1432 | file(s) if the contents have not changed. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1433 | ``` |
| 1434 | |
| 1435 | #### **File name handling** |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 1436 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1437 | ``` |
| 1438 | All output files must be inside the output directory of the build. |
| 1439 | You would generally use |$target_out_dir| or |$target_gen_dir| to |
| 1440 | reference the output or generated intermediate file directories, |
| 1441 | respectively. |
| 1442 | ``` |
| 1443 | |
| 1444 | #### **Variables** |
| 1445 | |
| 1446 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1447 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 1448 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 1449 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 1450 | rustenv, rustflags, swiftflags, testonly |
| 1451 | Dependent configs: all_dependent_configs, public_configs |
| 1452 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 1453 | write_runtime_deps |
| 1454 | General: check_includes, configs, data, friend, inputs, metadata, |
| 1455 | output_extension, output_name, public, sources, testonly, |
| 1456 | visibility |
| 1457 | Action variables: args, bridge_header, configs, data, depfile, |
Joseph Ryan | ad1e5ce | 2023-04-26 13:48:01 -0700 | [diff] [blame] | 1458 | framework_dirs, inputs, mnemonic, module_deps, |
| 1459 | module_name, outputs*, pool, response_file_contents, |
| 1460 | script*, sources |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1461 | * = required |
| 1462 | ``` |
| 1463 | |
| 1464 | #### **Example** |
| 1465 | |
| 1466 | ``` |
| 1467 | action("run_this_guy_once") { |
| 1468 | script = "doprocessing.py" |
| 1469 | sources = [ "my_configuration.txt" ] |
| 1470 | outputs = [ "$target_gen_dir/insightful_output.txt" ] |
| 1471 | |
| 1472 | # Our script imports this Python file so we want to rebuild if it changes. |
| 1473 | inputs = [ "helper_library.py" ] |
| 1474 | |
| 1475 | # Note that we have to manually pass the sources to our script if the |
| 1476 | # script needs them as inputs. |
| 1477 | args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] + |
| 1478 | rebase_path(sources, root_build_dir) |
| 1479 | } |
| 1480 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1481 | ### <a name="func_action_foreach"></a>**action_foreach**: Declare a target that runs a script over a set of files. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1482 | |
| 1483 | ``` |
| 1484 | This target type allows you to run a script once-per-file over a set of |
| 1485 | sources. If you want to run a script once that takes many files as input, see |
| 1486 | "gn help action". |
| 1487 | ``` |
| 1488 | |
| 1489 | #### **Inputs** |
| 1490 | |
| 1491 | ``` |
| 1492 | The script will be run once per file in the "sources" variable. The "outputs" |
| 1493 | variable should specify one or more files with a source expansion pattern in |
| 1494 | it (see "gn help source_expansion"). The output file(s) for each script |
| 1495 | invocation should be unique. Normally you use "{{source_name_part}}" in each |
| 1496 | output file. |
| 1497 | |
| 1498 | If your script takes additional data as input, such as a shared configuration |
| 1499 | file or a Python module it uses, those files should be listed in the "inputs" |
| 1500 | variable. These files are treated as dependencies of each script invocation. |
| 1501 | |
| 1502 | If the command line length is very long, you can use response files to pass |
| 1503 | args to your script. See "gn help response_file_contents". |
| 1504 | |
| 1505 | You can dynamically write input dependencies (for incremental rebuilds if an |
| 1506 | input file changes) by writing a depfile when the script is run (see "gn help |
| 1507 | depfile"). This is more flexible than "inputs". |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 1508 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1509 | The "deps" and "public_deps" for an action will always be |
| 1510 | completed before any part of the action is run so it can depend on |
| 1511 | the output of previous steps. The "data_deps" will be built if the |
| 1512 | action is built, but may not have completed before all steps of the |
| 1513 | action are started. This can give additional parallelism in the build |
| 1514 | for runtime-only dependencies. |
| 1515 | ``` |
| 1516 | |
| 1517 | #### **Outputs** |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 1518 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1519 | ``` |
| 1520 | The script will be executed with the given arguments with the current |
| 1521 | directory being that of the root build directory. If you pass files |
| 1522 | to your script, see "gn help rebase_path" for how to convert |
| 1523 | file names to be relative to the build directory (file names in the |
| 1524 | sources, outputs, and inputs will be all treated as relative to the |
| 1525 | current build file and converted as needed automatically). |
Brett Wilson | 1e1ea9d | 2020-02-04 15:34:05 -0800 | [diff] [blame] | 1526 | |
| 1527 | GN sets Ninja's flag 'restat = 1` for all action commands. This means |
| 1528 | that Ninja will check the timestamp of the output after the action |
| 1529 | completes. If output timestamp is unchanged, the step will be treated |
| 1530 | as if it never needed to be rebuilt, potentially eliminating some |
| 1531 | downstream steps for incremental builds. Scripts can improve build |
| 1532 | performance by taking care not to change the timstamp of the output |
| 1533 | file(s) if the contents have not changed. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1534 | ``` |
| 1535 | |
| 1536 | #### **File name handling** |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 1537 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1538 | ``` |
| 1539 | All output files must be inside the output directory of the build. |
| 1540 | You would generally use |$target_out_dir| or |$target_gen_dir| to |
| 1541 | reference the output or generated intermediate file directories, |
| 1542 | respectively. |
| 1543 | ``` |
| 1544 | |
| 1545 | #### **Variables** |
| 1546 | |
| 1547 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1548 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 1549 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 1550 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 1551 | rustenv, rustflags, swiftflags, testonly |
| 1552 | Dependent configs: all_dependent_configs, public_configs |
| 1553 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 1554 | write_runtime_deps |
| 1555 | General: check_includes, configs, data, friend, inputs, metadata, |
| 1556 | output_extension, output_name, public, sources, testonly, |
| 1557 | visibility |
| 1558 | Action variables: args, bridge_header, configs, data, depfile, |
Joseph Ryan | ad1e5ce | 2023-04-26 13:48:01 -0700 | [diff] [blame] | 1559 | framework_dirs, inputs, mnemonic, module_deps, |
| 1560 | module_name, outputs*, pool, response_file_contents, |
| 1561 | script*, sources |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1562 | * = required |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1563 | ``` |
| 1564 | |
| 1565 | #### **Example** |
| 1566 | |
| 1567 | ``` |
| 1568 | # Runs the script over each IDL file. The IDL script will generate both a .cc |
| 1569 | # and a .h file for each input. |
| 1570 | action_foreach("my_idl") { |
| 1571 | script = "idl_processor.py" |
| 1572 | sources = [ "foo.idl", "bar.idl" ] |
| 1573 | |
Joseph Ryan | ad1e5ce | 2023-04-26 13:48:01 -0700 | [diff] [blame] | 1574 | # Causes ninja to output "IDL <label>" rather than the default |
| 1575 | # "ACTION <label>" when building this action. |
| 1576 | mnemonic = "IDL" |
| 1577 | |
Joe Armstrong | 73e8907 | 2019-05-10 11:23:52 +0800 | [diff] [blame] | 1578 | # Our script reads this file each time, so we need to list it as a |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1579 | # dependency so we can rebuild if it changes. |
| 1580 | inputs = [ "my_configuration.txt" ] |
| 1581 | |
| 1582 | # Transformation from source file name to output file names. |
| 1583 | outputs = [ "$target_gen_dir/{{source_name_part}}.h", |
| 1584 | "$target_gen_dir/{{source_name_part}}.cc" ] |
| 1585 | |
| 1586 | # Note that since "args" is opaque to GN, if you specify paths here, you |
| 1587 | # will need to convert it to be relative to the build directory using |
| 1588 | # rebase_path(). |
| 1589 | args = [ |
| 1590 | "{{source}}", |
| 1591 | "-o", |
Hector Dearman | 0d67e27 | 2020-12-22 01:37:16 +0000 | [diff] [blame] | 1592 | rebase_path(target_gen_dir, root_build_dir) + |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1593 | "/{{source_name_part}}.h" ] |
| 1594 | } |
| 1595 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1596 | ### <a name="func_bundle_data"></a>**bundle_data**: [iOS/macOS] Declare a target without output. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1597 | |
| 1598 | ``` |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 1599 | This target type allows one to declare data that is required at runtime. It is |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1600 | used to inform "create_bundle" targets of the files to copy into generated |
| 1601 | bundle, see "gn help create_bundle" for help. |
| 1602 | |
| 1603 | The target must define a list of files as "sources" and a single "outputs". |
| 1604 | If there are multiple files, source expansions must be used to express the |
| 1605 | output. The output must reference a file inside of {{bundle_root_dir}}. |
| 1606 | |
| 1607 | This target can be used on all platforms though it is designed only to |
| 1608 | generate iOS/macOS bundle. In cross-platform projects, it is advised to put it |
| 1609 | behind iOS/macOS conditionals. |
| 1610 | |
Sylvain Defresne | 06cdcc8 | 2024-03-21 13:48:59 +0100 | [diff] [blame] | 1611 | If any source files in a bundle_data target match `*/*.xcassets/*` then they |
| 1612 | will be considered part of an assets catalog, and instead of being copied to |
| 1613 | the final bundle the assets catalog itself will be added to the inputs of the |
| 1614 | assets catalog compilation step. See "compile_xcassets" tool. |
| 1615 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1616 | See "gn help create_bundle" for more information. |
| 1617 | ``` |
| 1618 | |
| 1619 | #### **Variables** |
| 1620 | |
| 1621 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1622 | Dependent configs: all_dependent_configs, public_configs |
| 1623 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 1624 | write_runtime_deps |
| 1625 | General: check_includes, configs, data, friend, inputs, metadata, |
| 1626 | output_extension, output_name, public, sources, testonly, |
| 1627 | visibility |
| 1628 | Bundle-specific: sources*, outputs* |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1629 | * = required |
| 1630 | ``` |
| 1631 | |
| 1632 | #### **Examples** |
| 1633 | |
| 1634 | ``` |
| 1635 | bundle_data("icudata") { |
| 1636 | sources = [ "sources/data/in/icudtl.dat" ] |
| 1637 | outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ] |
| 1638 | } |
| 1639 | |
| 1640 | bundle_data("base_unittests_bundle_data]") { |
| 1641 | sources = [ "test/data" ] |
| 1642 | outputs = [ |
| 1643 | "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" + |
| 1644 | "{{source_file_part}}" |
| 1645 | ] |
| 1646 | } |
| 1647 | |
| 1648 | bundle_data("material_typography_bundle_data") { |
| 1649 | sources = [ |
| 1650 | "src/MaterialTypography.bundle/Roboto-Bold.ttf", |
| 1651 | "src/MaterialTypography.bundle/Roboto-Italic.ttf", |
| 1652 | "src/MaterialTypography.bundle/Roboto-Regular.ttf", |
| 1653 | "src/MaterialTypography.bundle/Roboto-Thin.ttf", |
| 1654 | ] |
| 1655 | outputs = [ |
| 1656 | "{{bundle_resources_dir}}/MaterialTypography.bundle/" |
| 1657 | "{{source_file_part}}" |
| 1658 | ] |
| 1659 | } |
| 1660 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1661 | ### <a name="func_copy"></a>**copy**: Declare a target that copies files. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1662 | |
| 1663 | #### **File name handling** |
| 1664 | |
| 1665 | ``` |
| 1666 | All output files must be inside the output directory of the build. You would |
| 1667 | generally use |$target_out_dir| or |$target_gen_dir| to reference the output |
| 1668 | or generated intermediate file directories, respectively. |
| 1669 | |
| 1670 | Both "sources" and "outputs" must be specified. Sources can include as many |
| 1671 | files as you want, but there can only be one item in the outputs list (plural |
| 1672 | is used for the name for consistency with other target types). |
| 1673 | |
| 1674 | If there is more than one source file, your output name should specify a |
| 1675 | mapping from each source file to an output file name using source expansion |
| 1676 | (see "gn help source_expansion"). The placeholders will look like |
| 1677 | "{{source_name_part}}", for example. |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1678 | |
| 1679 | If you want to copy the output of a previous build step, the target that |
| 1680 | generates the file to copy must be reachable from the deps or public_deps of |
| 1681 | the copy target. |
| 1682 | ``` |
| 1683 | |
| 1684 | #### **Variables** |
| 1685 | |
| 1686 | ``` |
| 1687 | Dependent configs: all_dependent_configs, public_configs |
| 1688 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 1689 | write_runtime_deps |
| 1690 | General: check_includes, configs, data, friend, inputs, metadata, |
| 1691 | output_extension, output_name, public, sources, testonly, |
| 1692 | visibility |
| 1693 | Copy variables: sources*, outputs* |
| 1694 | * = required |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1695 | ``` |
| 1696 | |
| 1697 | #### **Examples** |
| 1698 | |
| 1699 | ``` |
| 1700 | # Write a rule that copies a checked-in DLL to the output directory. |
| 1701 | copy("mydll") { |
| 1702 | sources = [ "mydll.dll" ] |
| 1703 | outputs = [ "$target_out_dir/mydll.dll" ] |
| 1704 | } |
| 1705 | |
| 1706 | # Write a rule to copy several files to the target generated files directory. |
| 1707 | copy("myfiles") { |
| 1708 | sources = [ "data1.dat", "data2.dat", "data3.dat" ] |
| 1709 | |
| 1710 | # Use source expansion to generate output files with the corresponding file |
| 1711 | # names in the gen dir. This will just copy each file. |
| 1712 | outputs = [ "$target_gen_dir/{{source_file_part}}" ] |
| 1713 | } |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1714 | |
| 1715 | # Copy the output of a generated executable. |
| 1716 | copy("package_melon") { |
| 1717 | # This example uses get_label_info() to compute the output directory of the |
| 1718 | # dependency. This allows the copy rule to work regardless of the toolchain. |
| 1719 | # |
| 1720 | # In some cases (particularly actions defined previously in the same file) |
| 1721 | # you can use get_target_outputs() to get the input file which can eliminate |
| 1722 | # the assumptions about the output file name of the dependency. |
| 1723 | |
| 1724 | input_dir = get_label_info("//src/tools/melon", "root_out_dir"); |
| 1725 | sources = [ "$input_dir/melon" ] |
| 1726 | |
| 1727 | outputs = [ "$target_gen_dir/{{source_file_part}}" ] |
| 1728 | |
| 1729 | # Depend on the target to build the file before copying. |
| 1730 | deps = [ "//src/tools/melon" ] |
| 1731 | } |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1732 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1733 | ### <a name="func_create_bundle"></a>**create_bundle**: [ios/macOS] Build an iOS or macOS bundle. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1734 | |
| 1735 | ``` |
| 1736 | This target generates an iOS or macOS bundle (which is a directory with a |
| 1737 | well-know structure). This target does not define any sources, instead they |
| 1738 | are computed from all "bundle_data" target this one depends on transitively |
| 1739 | (the recursion stops at "create_bundle" targets). |
| 1740 | |
Sylvain Defresne | d46f92f | 2019-04-03 16:14:29 +0200 | [diff] [blame] | 1741 | The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in |
| 1742 | "bundle_data" outputs. The properties are optional but must be defined if any |
| 1743 | of the "bundle_data" target use them. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1744 | |
| 1745 | This target can be used on all platforms though it is designed only to |
| 1746 | generate iOS or macOS bundle. In cross-platform projects, it is advised to put |
| 1747 | it behind iOS/macOS conditionals. |
| 1748 | |
| 1749 | If a create_bundle is specified as a data_deps for another target, the bundle |
| 1750 | is considered a leaf, and its public and private dependencies will not |
| 1751 | contribute to any data or data_deps. Required runtime dependencies should be |
| 1752 | placed in the bundle. A create_bundle can declare its own explicit data and |
| 1753 | data_deps, however. |
| 1754 | ``` |
| 1755 | |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 1756 | #### **Post-processing** |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1757 | |
| 1758 | ``` |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 1759 | Some bundle needs to be post-processed as part of the build (e.g. on iOS all |
| 1760 | application needs to be code signed to run on a device). The post processing |
| 1761 | step can be configured via the post_processing_script variable. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1762 | |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 1763 | If set, `post_processing_script` is the path of a script that invoked after |
| 1764 | all files have been moved into the bundle. The script must not change any file |
| 1765 | in the bundle, but may add new files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1766 | |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 1767 | If `post_processing_script` is defined, then `post_processing_outputs` must |
| 1768 | be defined and non-empty to inform when the script needs to be re-run. The |
| 1769 | `post_processing_args` will be passed as is to the script (so path have to be |
| 1770 | rebased) and additional inputs may be listed via `post_processing_sources`. |
| 1771 | ``` |
| 1772 | |
| 1773 | #### **Migration** |
| 1774 | |
| 1775 | ``` |
| 1776 | The post-processing step used to be limited to code-signing. The properties |
| 1777 | used to be named `code_signing_$name` instead of `post_processing_$name`. The |
| 1778 | old names are still accepted as alias to facilitate migration but a warning |
| 1779 | will be emitted and the alias eventually be removed. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1780 | ``` |
| 1781 | |
| 1782 | #### **Variables** |
| 1783 | |
| 1784 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1785 | Dependent configs: all_dependent_configs, public_configs |
| 1786 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 1787 | write_runtime_deps |
| 1788 | General: check_includes, configs, data, friend, inputs, metadata, |
| 1789 | output_extension, output_name, public, sources, testonly, |
| 1790 | visibility |
| 1791 | Bundle vars: bundle_root_dir, bundle_contents_dir, bundle_resources_dir, |
| 1792 | bundle_executable_dir, bundle_deps_filter, product_type, |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 1793 | post_processing_args, post_processing_script, |
| 1794 | post_processing_sources, post_processing_outputs, |
| 1795 | xcode_extra_attributes, xcode_test_application_name, |
| 1796 | partial_info_plist |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1797 | ``` |
| 1798 | |
| 1799 | #### **Example** |
| 1800 | |
| 1801 | ``` |
| 1802 | # Defines a template to create an application. On most platform, this is just |
| 1803 | # an alias for an "executable" target, but on iOS/macOS, it builds an |
| 1804 | # application bundle. |
| 1805 | template("app") { |
| 1806 | if (!is_ios && !is_mac) { |
| 1807 | executable(target_name) { |
| 1808 | forward_variables_from(invoker, "*") |
| 1809 | } |
| 1810 | } else { |
| 1811 | app_name = target_name |
| 1812 | gen_path = target_gen_dir |
| 1813 | |
| 1814 | action("${app_name}_generate_info_plist") { |
| 1815 | script = [ "//build/ios/ios_gen_plist.py" ] |
| 1816 | sources = [ "templates/Info.plist" ] |
| 1817 | outputs = [ "$gen_path/Info.plist" ] |
| 1818 | args = rebase_path(sources, root_build_dir) + |
| 1819 | rebase_path(outputs, root_build_dir) |
| 1820 | } |
| 1821 | |
| 1822 | bundle_data("${app_name}_bundle_info_plist") { |
Sylvain Defresne | dcab2f9 | 2019-04-03 16:34:05 +0200 | [diff] [blame] | 1823 | public_deps = [ ":${app_name}_generate_info_plist" ] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1824 | sources = [ "$gen_path/Info.plist" ] |
| 1825 | outputs = [ "{{bundle_contents_dir}}/Info.plist" ] |
| 1826 | } |
| 1827 | |
| 1828 | executable("${app_name}_generate_executable") { |
| 1829 | forward_variables_from(invoker, "*", [ |
| 1830 | "output_name", |
| 1831 | "visibility", |
| 1832 | ]) |
| 1833 | output_name = |
| 1834 | rebase_path("$gen_path/$app_name", root_build_dir) |
| 1835 | } |
| 1836 | |
| 1837 | code_signing = |
| 1838 | defined(invoker.code_signing) && invoker.code_signing |
| 1839 | |
Sylvain Defresne | dcab2f9 | 2019-04-03 16:34:05 +0200 | [diff] [blame] | 1840 | if (!is_ios || !code_signing) { |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1841 | bundle_data("${app_name}_bundle_executable") { |
Sylvain Defresne | dcab2f9 | 2019-04-03 16:34:05 +0200 | [diff] [blame] | 1842 | public_deps = [ ":${app_name}_generate_executable" ] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1843 | sources = [ "$gen_path/$app_name" ] |
| 1844 | outputs = [ "{{bundle_executable_dir}}/$app_name" ] |
| 1845 | } |
| 1846 | } |
| 1847 | |
Sylvain Defresne | dcab2f9 | 2019-04-03 16:34:05 +0200 | [diff] [blame] | 1848 | create_bundle("$app_name.app") { |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1849 | product_type = "com.apple.product-type.application" |
| 1850 | |
| 1851 | if (is_ios) { |
Sylvain Defresne | dcab2f9 | 2019-04-03 16:34:05 +0200 | [diff] [blame] | 1852 | bundle_root_dir = "$root_build_dir/$target_name" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1853 | bundle_contents_dir = bundle_root_dir |
| 1854 | bundle_resources_dir = bundle_contents_dir |
| 1855 | bundle_executable_dir = bundle_contents_dir |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1856 | |
Sylvain Defresne | 7b0690e | 2020-06-26 15:33:01 +0200 | [diff] [blame] | 1857 | xcode_extra_attributes = { |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1858 | ONLY_ACTIVE_ARCH = "YES" |
| 1859 | DEBUG_INFORMATION_FORMAT = "dwarf" |
| 1860 | } |
| 1861 | } else { |
Sylvain Defresne | dcab2f9 | 2019-04-03 16:34:05 +0200 | [diff] [blame] | 1862 | bundle_root_dir = "$root_build_dir/$target_name" |
| 1863 | bundle_contents_dir = "$bundle_root_dir/Contents" |
| 1864 | bundle_resources_dir = "$bundle_contents_dir/Resources" |
| 1865 | bundle_executable_dir = "$bundle_contents_dir/MacOS" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1866 | } |
| 1867 | deps = [ ":${app_name}_bundle_info_plist" ] |
| 1868 | if (is_ios && code_signing) { |
| 1869 | deps += [ ":${app_name}_generate_executable" ] |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 1870 | post_processing_script = "//build/config/ios/codesign.py" |
| 1871 | post_processing_sources = [ |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1872 | invoker.entitlements_path, |
| 1873 | "$target_gen_dir/$app_name", |
| 1874 | ] |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 1875 | post_processing_outputs = [ |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1876 | "$bundle_root_dir/$app_name", |
| 1877 | "$bundle_root_dir/_CodeSignature/CodeResources", |
| 1878 | "$bundle_root_dir/embedded.mobileprovision", |
| 1879 | "$target_gen_dir/$app_name.xcent", |
| 1880 | ] |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 1881 | post_processing_args = [ |
| 1882 | "-i=" + ios_post_processing_identity, |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1883 | "-b=" + rebase_path( |
| 1884 | "$target_gen_dir/$app_name", root_build_dir), |
| 1885 | "-e=" + rebase_path( |
| 1886 | invoker.entitlements_path, root_build_dir), |
| 1887 | "-e=" + rebase_path( |
| 1888 | "$target_gen_dir/$app_name.xcent", root_build_dir), |
| 1889 | rebase_path(bundle_root_dir, root_build_dir), |
| 1890 | ] |
| 1891 | } else { |
| 1892 | deps += [ ":${app_name}_bundle_executable" ] |
| 1893 | } |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1898 | ### <a name="func_executable"></a>**executable**: Declare an executable target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1899 | |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 1900 | #### **Language and compilation** |
| 1901 | |
| 1902 | ``` |
| 1903 | The tools and commands used to create this target type will be |
| 1904 | determined by the source files in its sources. Targets containing |
| 1905 | multiple compiler-incompatible languages are not allowed (e.g. a |
| 1906 | target containing both C and C++ sources is acceptable, but a |
| 1907 | target containing C and Rust sources is not). |
| 1908 | ``` |
| 1909 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1910 | #### **Variables** |
| 1911 | |
| 1912 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1913 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 1914 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 1915 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 1916 | rustenv, rustflags, swiftflags, testonly |
| 1917 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 1918 | write_runtime_deps |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 1919 | Dependent configs: all_dependent_configs, public_configs |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1920 | General: check_includes, configs, data, friend, inputs, metadata, |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1921 | output_extension, output_name, public, sources, testonly, |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1922 | visibility |
Petr Hosek | add1bb1 | 2019-09-11 14:39:38 -0700 | [diff] [blame] | 1923 | Rust variables: aliased_deps, crate_root, crate_name |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1924 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 1925 | ### <a name="func_generated_file"></a>**generated_file**: Declare a generated_file target. [Back to Top](#gn-reference) |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1926 | |
| 1927 | ``` |
| 1928 | Writes data value(s) to disk on resolution. This target type mirrors some |
| 1929 | functionality of the write_file() function, but also provides the ability to |
| 1930 | collect metadata from its dependencies on resolution rather than writing out |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 1931 | at parse time. |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1932 | |
| 1933 | The `outputs` variable is required to be a list with a single element, |
| 1934 | specifying the intended location of the output file. |
| 1935 | |
| 1936 | The `output_conversion` variable specified the format to write the |
Sylvain Defresne | 72d5a6e | 2021-06-15 17:01:32 +0200 | [diff] [blame] | 1937 | value. See `gn help io_conversion`. |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1938 | |
Tomasz Śniatowski | 46b572c | 2021-09-03 21:47:17 +0200 | [diff] [blame] | 1939 | One of `contents` or `data_keys` must be specified; use of `contents` will |
| 1940 | write the contents of that value to file, while use of `data_keys` will |
| 1941 | trigger a metadata collection walk based on the dependencies of the target and |
| 1942 | the optional values of the `rebase` and `walk_keys` variables. See |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1943 | `gn help metadata`. |
| 1944 | |
| 1945 | Collected metadata, if specified, will be returned in postorder of |
| 1946 | dependencies. See the example for details. |
| 1947 | ``` |
| 1948 | |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1949 | #### **Variables** |
| 1950 | |
| 1951 | ``` |
| 1952 | Dependent configs: all_dependent_configs, public_configs |
| 1953 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 1954 | write_runtime_deps |
| 1955 | General: check_includes, configs, data, friend, inputs, metadata, |
| 1956 | output_extension, output_name, public, sources, testonly, |
| 1957 | visibility |
Brett Wilson | 225e90c | 2024-08-09 10:17:48 -0700 | [diff] [blame] | 1958 | Generated file: contents, data_keys, rebase, walk_keys, output_conversion, |
| 1959 | outputs |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 1960 | ``` |
| 1961 | |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1962 | #### **Example (metadata collection)** |
| 1963 | |
| 1964 | ``` |
| 1965 | Given the following targets defined in //base/BUILD.gn, where A depends on B |
| 1966 | and B depends on C and D: |
| 1967 | |
| 1968 | group("a") { |
| 1969 | metadata = { |
| 1970 | doom_melon = [ "enable" ] |
| 1971 | my_files = [ "foo.cpp" ] |
| 1972 | |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 1973 | # Note: this is functionally equivalent to not defining `my_barrier` |
| 1974 | # at all in this target's metadata. |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1975 | my_barrier = [ "" ] |
| 1976 | } |
| 1977 | |
| 1978 | deps = [ ":b" ] |
| 1979 | } |
| 1980 | |
Julie Hockett | d69a9c3 | 2019-01-23 14:36:18 -0800 | [diff] [blame] | 1981 | group("b") { |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 1982 | metadata = { |
| 1983 | my_files = [ "bar.cpp" ] |
| 1984 | my_barrier = [ ":c" ] |
| 1985 | } |
| 1986 | |
| 1987 | deps = [ ":c", ":d" ] |
| 1988 | } |
| 1989 | |
| 1990 | group("c") { |
| 1991 | metadata = { |
| 1992 | doom_melon = [ "disable" ] |
| 1993 | my_files = [ "baz.cpp" ] |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | group("d") { |
| 1998 | metadata = { |
| 1999 | my_files = [ "missing.cpp" ] |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | If the following generated_file target is defined: |
| 2004 | |
| 2005 | generated_file("my_files_metadata") { |
| 2006 | outputs = [ "$root_build_dir/my_files.json" ] |
| 2007 | data_keys = [ "my_files" ] |
| 2008 | |
| 2009 | deps = [ "//base:a" ] |
| 2010 | } |
| 2011 | |
| 2012 | The following will be written to "$root_build_dir/my_files.json" (less the |
| 2013 | comments): |
| 2014 | [ |
| 2015 | "baz.cpp", // from //base:c via //base:b |
| 2016 | "missing.cpp" // from //base:d via //base:b |
| 2017 | "bar.cpp", // from //base:b via //base:a |
| 2018 | "foo.cpp", // from //base:a |
| 2019 | ] |
| 2020 | |
| 2021 | Alternatively, as an example of using walk_keys, if the following |
| 2022 | generated_file target is defined: |
| 2023 | |
| 2024 | generated_file("my_files_metadata") { |
| 2025 | outputs = [ "$root_build_dir/my_files.json" ] |
| 2026 | data_keys = [ "my_files" ] |
| 2027 | walk_keys = [ "my_barrier" ] |
| 2028 | |
| 2029 | deps = [ "//base:a" ] |
| 2030 | } |
| 2031 | |
| 2032 | The following will be written to "$root_build_dir/my_files.json" (again less |
| 2033 | the comments): |
| 2034 | [ |
| 2035 | "baz.cpp", // from //base:c via //base:b |
| 2036 | "bar.cpp", // from //base:b via //base:a |
| 2037 | "foo.cpp", // from //base:a |
| 2038 | ] |
| 2039 | |
| 2040 | If `rebase` is used in the following generated_file target: |
| 2041 | |
| 2042 | generated_file("my_files_metadata") { |
| 2043 | outputs = [ "$root_build_dir/my_files.json" ] |
| 2044 | data_keys = [ "my_files" ] |
| 2045 | walk_keys = [ "my_barrier" ] |
| 2046 | rebase = root_build_dir |
| 2047 | |
| 2048 | deps = [ "//base:a" ] |
| 2049 | } |
| 2050 | |
| 2051 | The following will be written to "$root_build_dir/my_files.json" (again less |
| 2052 | the comments) (assuming root_build_dir = "//out"): |
| 2053 | [ |
| 2054 | "../base/baz.cpp", // from //base:c via //base:b |
| 2055 | "../base/bar.cpp", // from //base:b via //base:a |
| 2056 | "../base/foo.cpp", // from //base:a |
| 2057 | ] |
| 2058 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2059 | ### <a name="func_group"></a>**group**: Declare a named group of targets. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2060 | |
| 2061 | ``` |
| 2062 | This target type allows you to create meta-targets that just collect a set of |
| 2063 | dependencies into one named target. Groups can additionally specify configs |
| 2064 | that apply to their dependents. |
| 2065 | ``` |
| 2066 | |
| 2067 | #### **Variables** |
| 2068 | |
| 2069 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2070 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 2071 | write_runtime_deps |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2072 | Dependent configs: all_dependent_configs, public_configs |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2073 | General: check_includes, configs, data, friend, inputs, metadata, |
| 2074 | output_extension, output_name, public, sources, testonly, |
| 2075 | visibility |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2076 | ``` |
| 2077 | |
| 2078 | #### **Example** |
| 2079 | |
| 2080 | ``` |
| 2081 | group("all") { |
| 2082 | deps = [ |
| 2083 | "//project:runner", |
| 2084 | "//project:unit_tests", |
| 2085 | ] |
| 2086 | } |
| 2087 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2088 | ### <a name="func_loadable_module"></a>**loadable_module**: Declare a loadable module target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2089 | |
| 2090 | ``` |
| 2091 | This target type allows you to create an object file that is (and can only |
| 2092 | be) loaded and unloaded at runtime. |
| 2093 | |
| 2094 | A loadable module will be specified on the linker line for targets listing |
| 2095 | the loadable module in its "deps". If you don't want this (if you don't need |
| 2096 | to dynamically load the library at runtime), then you should use a |
| 2097 | "shared_library" target type instead. |
| 2098 | ``` |
| 2099 | |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2100 | #### **Language and compilation** |
| 2101 | |
| 2102 | ``` |
| 2103 | The tools and commands used to create this target type will be |
| 2104 | determined by the source files in its sources. Targets containing |
| 2105 | multiple compiler-incompatible languages are not allowed (e.g. a |
| 2106 | target containing both C and C++ sources is acceptable, but a |
| 2107 | target containing C and Rust sources is not). |
| 2108 | ``` |
| 2109 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2110 | #### **Variables** |
| 2111 | |
| 2112 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2113 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 2114 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 2115 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 2116 | rustenv, rustflags, swiftflags, testonly |
| 2117 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 2118 | write_runtime_deps |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2119 | Dependent configs: all_dependent_configs, public_configs |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 2120 | General: check_includes, configs, data, friend, inputs, metadata, |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2121 | output_extension, output_name, public, sources, testonly, |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 2122 | visibility |
Petr Hosek | add1bb1 | 2019-09-11 14:39:38 -0700 | [diff] [blame] | 2123 | Rust variables: aliased_deps, crate_root, crate_name, crate_type |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2124 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2125 | ### <a name="func_rust_library"></a>**rust_library**: Declare a Rust library target. [Back to Top](#gn-reference) |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2126 | |
| 2127 | ``` |
| 2128 | A Rust library is an archive containing additional rust-c provided metadata. |
| 2129 | These are the files produced by the rustc compiler with the `.rlib` |
| 2130 | extension, and are the intermediate step for most Rust-based binaries. |
| 2131 | ``` |
| 2132 | |
| 2133 | #### **Language and compilation** |
| 2134 | |
| 2135 | ``` |
| 2136 | The tools and commands used to create this target type will be |
| 2137 | determined by the source files in its sources. Targets containing |
| 2138 | multiple compiler-incompatible languages are not allowed (e.g. a |
| 2139 | target containing both C and C++ sources is acceptable, but a |
| 2140 | target containing C and Rust sources is not). |
| 2141 | ``` |
| 2142 | |
| 2143 | #### **Variables** |
| 2144 | |
| 2145 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2146 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 2147 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 2148 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 2149 | rustenv, rustflags, swiftflags, testonly |
| 2150 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 2151 | write_runtime_deps |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2152 | Dependent configs: all_dependent_configs, public_configs |
| 2153 | General: check_includes, configs, data, friend, inputs, metadata, |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2154 | output_extension, output_name, public, sources, testonly, |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2155 | visibility |
Petr Hosek | add1bb1 | 2019-09-11 14:39:38 -0700 | [diff] [blame] | 2156 | Rust variables: aliased_deps, crate_root, crate_name |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2157 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2158 | ### <a name="func_rust_proc_macro"></a>**rust_proc_macro**: Declare a Rust procedural macro target. [Back to Top](#gn-reference) |
Petr Hosek | fe36c7c | 2019-11-26 14:38:08 -0800 | [diff] [blame] | 2159 | |
| 2160 | ``` |
| 2161 | A Rust procedural macro allows creating syntax extensions as execution of a |
| 2162 | function. They are compiled as dynamic libraries and used by the compiler at |
| 2163 | runtime. |
| 2164 | |
| 2165 | Their use is the same as of other Rust libraries, but their build has some |
| 2166 | additional restrictions in terms of supported flags. |
| 2167 | ``` |
| 2168 | |
| 2169 | #### **Language and compilation** |
| 2170 | |
| 2171 | ``` |
| 2172 | The tools and commands used to create this target type will be |
| 2173 | determined by the source files in its sources. Targets containing |
| 2174 | multiple compiler-incompatible languages are not allowed (e.g. a |
| 2175 | target containing both C and C++ sources is acceptable, but a |
| 2176 | target containing C and Rust sources is not). |
| 2177 | ``` |
| 2178 | |
| 2179 | #### **Variables** |
| 2180 | |
| 2181 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2182 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 2183 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 2184 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 2185 | rustenv, rustflags, swiftflags, testonly |
| 2186 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 2187 | write_runtime_deps |
Petr Hosek | fe36c7c | 2019-11-26 14:38:08 -0800 | [diff] [blame] | 2188 | Dependent configs: all_dependent_configs, public_configs |
| 2189 | General: check_includes, configs, data, friend, inputs, metadata, |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2190 | output_extension, output_name, public, sources, testonly, |
Petr Hosek | fe36c7c | 2019-11-26 14:38:08 -0800 | [diff] [blame] | 2191 | visibility |
| 2192 | Rust variables: aliased_deps, crate_root, crate_name |
| 2193 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2194 | ### <a name="func_shared_library"></a>**shared_library**: Declare a shared library target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2195 | |
| 2196 | ``` |
| 2197 | A shared library will be specified on the linker line for targets listing the |
| 2198 | shared library in its "deps". If you don't want this (say you dynamically |
| 2199 | load the library at runtime), then you should depend on the shared library |
| 2200 | via "data_deps" or, on Darwin platforms, use a "loadable_module" target type |
| 2201 | instead. |
| 2202 | ``` |
| 2203 | |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2204 | #### **Language and compilation** |
| 2205 | |
| 2206 | ``` |
| 2207 | The tools and commands used to create this target type will be |
| 2208 | determined by the source files in its sources. Targets containing |
| 2209 | multiple compiler-incompatible languages are not allowed (e.g. a |
| 2210 | target containing both C and C++ sources is acceptable, but a |
| 2211 | target containing C and Rust sources is not). |
| 2212 | ``` |
| 2213 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2214 | #### **Variables** |
| 2215 | |
| 2216 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2217 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 2218 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 2219 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 2220 | rustenv, rustflags, swiftflags, testonly |
| 2221 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 2222 | write_runtime_deps |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2223 | Dependent configs: all_dependent_configs, public_configs |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 2224 | General: check_includes, configs, data, friend, inputs, metadata, |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2225 | output_extension, output_name, public, sources, testonly, |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 2226 | visibility |
Petr Hosek | add1bb1 | 2019-09-11 14:39:38 -0700 | [diff] [blame] | 2227 | Rust variables: aliased_deps, crate_root, crate_name, crate_type |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2228 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2229 | ### <a name="func_source_set"></a>**source_set**: Declare a source set target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2230 | |
| 2231 | ``` |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 2232 | Only C-language source sets are supported at the moment. |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2233 | ``` |
| 2234 | |
| 2235 | #### **C-language source_sets** |
| 2236 | |
| 2237 | ``` |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2238 | A source set is a collection of sources that get compiled, but are not linked |
| 2239 | to produce any kind of library. Instead, the resulting object files are |
| 2240 | implicitly added to the linker line of all targets that depend on the source |
| 2241 | set. |
| 2242 | |
| 2243 | In most cases, a source set will behave like a static library, except no |
| 2244 | actual library file will be produced. This will make the build go a little |
| 2245 | faster by skipping creation of a large static library, while maintaining the |
| 2246 | organizational benefits of focused build targets. |
| 2247 | |
| 2248 | The main difference between a source set and a static library is around |
| 2249 | handling of exported symbols. Most linkers assume declaring a function |
| 2250 | exported means exported from the static library. The linker can then do dead |
| 2251 | code elimination to delete code not reachable from exported functions. |
| 2252 | |
| 2253 | A source set will not do this code elimination since there is no link step. |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 2254 | This allows you to link many source sets into a shared library and have the |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2255 | "exported symbol" notation indicate "export from the final shared library and |
| 2256 | not from the intermediate targets." There is no way to express this concept |
| 2257 | when linking multiple static libraries into a shared library. |
| 2258 | ``` |
| 2259 | |
| 2260 | #### **Variables** |
| 2261 | |
| 2262 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2263 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 2264 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 2265 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 2266 | rustenv, rustflags, swiftflags, testonly |
| 2267 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 2268 | write_runtime_deps |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2269 | Dependent configs: all_dependent_configs, public_configs |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 2270 | General: check_includes, configs, data, friend, inputs, metadata, |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2271 | output_extension, output_name, public, sources, testonly, |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 2272 | visibility |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2273 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2274 | ### <a name="func_static_library"></a>**static_library**: Declare a static library target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2275 | |
| 2276 | ``` |
| 2277 | Make a ".a" / ".lib" file. |
| 2278 | |
| 2279 | If you only need the static library for intermediate results in the build, |
| 2280 | you should consider a source_set instead since it will skip the (potentially |
| 2281 | slow) step of creating the intermediate library file. |
| 2282 | ``` |
| 2283 | |
| 2284 | #### **Variables** |
| 2285 | |
| 2286 | ``` |
| 2287 | complete_static_lib |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2288 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 2289 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 2290 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 2291 | rustenv, rustflags, swiftflags, testonly |
| 2292 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 2293 | write_runtime_deps |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2294 | Dependent configs: all_dependent_configs, public_configs |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 2295 | General: check_includes, configs, data, friend, inputs, metadata, |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2296 | output_extension, output_name, public, sources, testonly, |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 2297 | visibility |
Petr Hosek | add1bb1 | 2019-09-11 14:39:38 -0700 | [diff] [blame] | 2298 | Rust variables: aliased_deps, crate_root, crate_name |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2299 | |
| 2300 | The tools and commands used to create this target type will be |
| 2301 | determined by the source files in its sources. Targets containing |
| 2302 | multiple compiler-incompatible languages are not allowed (e.g. a |
| 2303 | target containing both C and C++ sources is acceptable, but a |
| 2304 | target containing C and Rust sources is not). |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2305 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2306 | ### <a name="func_target"></a>**target**: Declare a target with the given programmatic type. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2307 | |
| 2308 | ``` |
| 2309 | target(target_type_string, target_name_string) { ... } |
| 2310 | |
| 2311 | The target() function is a way to invoke a built-in target or template with a |
| 2312 | type determined at runtime. This is useful for cases where the type of a |
| 2313 | target might not be known statically. |
| 2314 | |
| 2315 | Only templates and built-in target functions are supported for the |
| 2316 | target_type_string parameter. Arbitrary functions, configs, and toolchains |
| 2317 | are not supported. |
| 2318 | |
| 2319 | The call: |
| 2320 | target("source_set", "doom_melon") { |
| 2321 | Is equivalent to: |
| 2322 | source_set("doom_melon") { |
| 2323 | ``` |
| 2324 | |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2325 | #### **Common target variables** |
| 2326 | |
| 2327 | ``` |
| 2328 | Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps, |
| 2329 | write_runtime_deps |
| 2330 | Dependent configs: all_dependent_configs, public_configs |
| 2331 | General: check_includes, configs, data, friend, inputs, metadata, |
| 2332 | output_extension, output_name, public, sources, testonly, |
| 2333 | visibility |
| 2334 | |
| 2335 | Targets will also have variables specific to that type, see "gn help <type>" |
| 2336 | for more. |
| 2337 | ``` |
| 2338 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2339 | #### **Example** |
| 2340 | |
| 2341 | ``` |
| 2342 | if (foo_build_as_shared) { |
| 2343 | my_type = "shared_library" |
| 2344 | } else { |
| 2345 | my_type = "source_set" |
| 2346 | } |
| 2347 | |
| 2348 | target(my_type, "foo") { |
| 2349 | ... |
| 2350 | } |
| 2351 | ``` |
| 2352 | ## <a name="functions"></a>Buildfile functions |
| 2353 | |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2354 | ### <a name="func_assert"></a>**assert**: Assert an expression is true at generation time. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2355 | |
| 2356 | ``` |
| 2357 | assert(<condition> [, <error string>]) |
| 2358 | |
| 2359 | If the condition is false, the build will fail with an error. If the |
| 2360 | optional second argument is provided, that string will be printed |
| 2361 | with the error message. |
| 2362 | ``` |
| 2363 | |
| 2364 | #### **Examples** |
| 2365 | |
| 2366 | ``` |
| 2367 | assert(is_win) |
| 2368 | assert(defined(sources), "Sources must be defined"); |
| 2369 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2370 | ### <a name="func_config"></a>**config**: Defines a configuration object. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2371 | |
| 2372 | ``` |
| 2373 | Configuration objects can be applied to targets and specify sets of compiler |
| 2374 | flags, includes, defines, etc. They provide a way to conveniently group sets |
| 2375 | of this configuration information. |
| 2376 | |
| 2377 | A config is referenced by its label just like a target. |
| 2378 | |
| 2379 | The values in a config are additive only. If you want to remove a flag you |
| 2380 | need to remove the corresponding config that sets it. The final set of flags, |
| 2381 | defines, etc. for a target is generated in this order: |
| 2382 | |
Nico Weber | 693f9fb | 2021-10-08 15:33:57 -0400 | [diff] [blame] | 2383 | 1. The values specified directly on the target (rather than using a config). |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2384 | 2. The configs specified in the target's "configs" list, in order. |
| 2385 | 3. Public_configs from a breadth-first traversal of the dependency tree in |
| 2386 | the order that the targets appear in "deps". |
| 2387 | 4. All dependent configs from a breadth-first traversal of the dependency |
| 2388 | tree in the order that the targets appear in "deps". |
| 2389 | ``` |
| 2390 | |
Joe Armstrong | 3941394 | 2019-03-15 10:34:03 +0800 | [diff] [blame] | 2391 | #### **More background** |
| 2392 | |
| 2393 | ``` |
| 2394 | Configs solve a problem where the build system needs to have a higher-level |
| 2395 | understanding of various compiler settings. For example, some compiler flags |
| 2396 | have to appear in a certain order relative to each other, some settings like |
| 2397 | defines and flags logically go together, and the build system needs to |
| 2398 | de-duplicate flags even though raw command-line parameters can't always be |
| 2399 | operated on in that way. |
| 2400 | |
| 2401 | The config gives a name to a group of settings that can then be reasoned |
| 2402 | about by GN. GN can know that configs with the same label are the same thing |
| 2403 | so can be de-duplicated. It allows related settings to be grouped so they |
| 2404 | are added or removed as a unit. And it allows targets to refer to settings |
| 2405 | with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than |
| 2406 | having to hard-coding every compiler's flags each time they are referred to. |
| 2407 | ``` |
| 2408 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2409 | #### **Variables valid in a config definition** |
Joe Armstrong | 3941394 | 2019-03-15 10:34:03 +0800 | [diff] [blame] | 2410 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2411 | ``` |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 2412 | Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc, |
| 2413 | cflags_objcc, defines, include_dirs, inputs, ldflags, |
| 2414 | lib_dirs, libs, precompiled_header, precompiled_source, |
| 2415 | rustenv, rustflags, swiftflags, testonly |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2416 | Nested configs: configs |
Tomasz Śniatowski | 46b572c | 2021-09-03 21:47:17 +0200 | [diff] [blame] | 2417 | General: visibility |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2418 | ``` |
| 2419 | |
| 2420 | #### **Variables on a target used to apply configs** |
| 2421 | |
| 2422 | ``` |
| 2423 | all_dependent_configs, configs, public_configs |
| 2424 | ``` |
| 2425 | |
| 2426 | #### **Example** |
| 2427 | |
| 2428 | ``` |
| 2429 | config("myconfig") { |
Joe Armstrong | f5ad807 | 2019-05-08 08:42:56 +0800 | [diff] [blame] | 2430 | include_dirs = [ "include/common" ] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2431 | defines = [ "ENABLE_DOOM_MELON" ] |
| 2432 | } |
| 2433 | |
| 2434 | executable("mything") { |
| 2435 | configs = [ ":myconfig" ] |
| 2436 | } |
| 2437 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2438 | ### <a name="func_declare_args"></a>**declare_args**: Declare build arguments. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2439 | |
| 2440 | ``` |
| 2441 | Introduces the given arguments into the current scope. If they are not |
| 2442 | specified on the command line or in a toolchain's arguments, the default |
| 2443 | values given in the declare_args block will be used. However, these defaults |
| 2444 | will not override command-line values. |
| 2445 | |
| 2446 | See also "gn help buildargs" for an overview. |
| 2447 | |
| 2448 | The precise behavior of declare args is: |
| 2449 | |
| 2450 | 1. The declare_args() block executes. Any variable defined in the enclosing |
| 2451 | scope is available for reading, but any variable defined earlier in |
| 2452 | the current scope is not (since the overrides haven't been applied yet). |
| 2453 | |
| 2454 | 2. At the end of executing the block, any variables set within that scope |
Petr Hosek | 7617b59 | 2019-10-02 09:52:21 -0700 | [diff] [blame] | 2455 | are saved, with the values specified in the block used as the "default value" |
| 2456 | for that argument. Once saved, these variables are available for override |
| 2457 | via args.gn. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2458 | |
| 2459 | 3. User-defined overrides are applied. Anything set in "gn args" now |
| 2460 | overrides any default values. The resulting set of variables is promoted |
| 2461 | to be readable from the following code in the file. |
| 2462 | |
| 2463 | This has some ramifications that may not be obvious: |
| 2464 | |
| 2465 | - You should not perform difficult work inside a declare_args block since |
| 2466 | this only sets a default value that may be discarded. In particular, |
| 2467 | don't use the result of exec_script() to set the default value. If you |
| 2468 | want to have a script-defined default, set some default "undefined" value |
| 2469 | like [], "", or -1, and after the declare_args block, call exec_script if |
| 2470 | the value is unset by the user. |
| 2471 | |
| 2472 | - Because you cannot read the value of a variable defined in the same |
| 2473 | block, if you need to make the default value of one arg depend |
| 2474 | on the possibly-overridden value of another, write two separate |
| 2475 | declare_args() blocks: |
| 2476 | |
| 2477 | declare_args() { |
| 2478 | enable_foo = true |
| 2479 | } |
| 2480 | declare_args() { |
| 2481 | # Bar defaults to same user-overridden state as foo. |
| 2482 | enable_bar = enable_foo |
| 2483 | } |
| 2484 | ``` |
| 2485 | |
| 2486 | #### **Example** |
| 2487 | |
| 2488 | ``` |
| 2489 | declare_args() { |
| 2490 | enable_teleporter = true |
| 2491 | enable_doom_melon = false |
| 2492 | } |
| 2493 | |
| 2494 | If you want to override the (default disabled) Doom Melon: |
| 2495 | gn --args="enable_doom_melon=true enable_teleporter=true" |
| 2496 | This also sets the teleporter, but it's already defaulted to on so it will |
| 2497 | have no effect. |
| 2498 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2499 | ### <a name="func_defined"></a>**defined**: Returns whether an identifier is defined. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2500 | |
| 2501 | ``` |
| 2502 | Returns true if the given argument is defined. This is most useful in |
| 2503 | templates to assert that the caller set things up properly. |
| 2504 | |
| 2505 | You can pass an identifier: |
| 2506 | defined(foo) |
| 2507 | which will return true or false depending on whether foo is defined in the |
| 2508 | current scope. |
| 2509 | |
| 2510 | You can also check a named scope: |
| 2511 | defined(foo.bar) |
| 2512 | which will return true or false depending on whether bar is defined in the |
| 2513 | named scope foo. It will throw an error if foo is not defined or is not a |
| 2514 | scope. |
Joseph Ryan | ad1e5ce | 2023-04-26 13:48:01 -0700 | [diff] [blame] | 2515 | |
| 2516 | You can also check a named scope using a subscript string expression: |
| 2517 | defined(foo[bar + "_name"]) |
Takuto Ikuta | 26aa46c | 2023-05-09 16:28:45 +0900 | [diff] [blame] | 2518 | which will return true or false depending on whether the subscript |
Joseph Ryan | ad1e5ce | 2023-04-26 13:48:01 -0700 | [diff] [blame] | 2519 | expression expands to the name of a member of the scope foo. It will |
| 2520 | throw an error if foo is not defined or is not a scope, or if the |
| 2521 | expression does not expand to a string, or if it is an empty string. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2522 | ``` |
| 2523 | |
| 2524 | #### **Example** |
| 2525 | |
| 2526 | ``` |
| 2527 | template("mytemplate") { |
| 2528 | # To help users call this template properly... |
| 2529 | assert(defined(invoker.sources), "Sources must be defined") |
| 2530 | |
| 2531 | # If we want to accept an optional "values" argument, we don't |
| 2532 | # want to dereference something that may not be defined. |
| 2533 | if (defined(invoker.values)) { |
| 2534 | values = invoker.values |
| 2535 | } else { |
| 2536 | values = "some default value" |
| 2537 | } |
| 2538 | } |
| 2539 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2540 | ### <a name="func_exec_script"></a>**exec_script**: Synchronously run a script and return the output. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2541 | |
| 2542 | ``` |
| 2543 | exec_script(filename, |
| 2544 | arguments = [], |
| 2545 | input_conversion = "", |
| 2546 | file_dependencies = []) |
| 2547 | |
| 2548 | Runs the given script, returning the stdout of the script. The build |
| 2549 | generation will fail if the script does not exist or returns a nonzero exit |
| 2550 | code. |
| 2551 | |
| 2552 | The current directory when executing the script will be the root build |
| 2553 | directory. If you are passing file names, you will want to use the |
| 2554 | rebase_path() function to make file names relative to this path (see "gn help |
| 2555 | rebase_path"). |
Gary Miguel | ce7fa36 | 2018-09-17 12:48:17 -0700 | [diff] [blame] | 2556 | |
| 2557 | The default script interpreter is Python ("python" on POSIX, "python.exe" or |
| 2558 | "python.bat" on Windows). This can be configured by the script_executable |
| 2559 | variable, see "gn help dotfile". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2560 | ``` |
| 2561 | |
| 2562 | #### **Arguments**: |
| 2563 | |
| 2564 | ``` |
| 2565 | filename: |
Gary Miguel | ce7fa36 | 2018-09-17 12:48:17 -0700 | [diff] [blame] | 2566 | File name of script to execute. Non-absolute names will be treated as |
| 2567 | relative to the current build file. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2568 | |
| 2569 | arguments: |
| 2570 | A list of strings to be passed to the script as arguments. May be |
| 2571 | unspecified or the empty list which means no arguments. |
| 2572 | |
| 2573 | input_conversion: |
Sylvain Defresne | 72d5a6e | 2021-06-15 17:01:32 +0200 | [diff] [blame] | 2574 | Controls how the file is read and parsed. See `gn help io_conversion`. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2575 | |
| 2576 | If unspecified, defaults to the empty string which causes the script |
| 2577 | result to be discarded. exec script will return None. |
| 2578 | |
| 2579 | dependencies: |
| 2580 | (Optional) A list of files that this script reads or otherwise depends |
| 2581 | on. These dependencies will be added to the build result such that if any |
| 2582 | of them change, the build will be regenerated and the script will be |
| 2583 | re-run. |
| 2584 | |
| 2585 | The script itself will be an implicit dependency so you do not need to |
| 2586 | list it. |
| 2587 | ``` |
| 2588 | |
| 2589 | #### **Example** |
| 2590 | |
| 2591 | ``` |
| 2592 | all_lines = exec_script( |
| 2593 | "myscript.py", [some_input], "list lines", |
| 2594 | [ rebase_path("data_file.txt", root_build_dir) ]) |
| 2595 | |
| 2596 | # This example just calls the script with no arguments and discards the |
| 2597 | # result. |
| 2598 | exec_script("//foo/bar/myscript.py") |
| 2599 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2600 | ### <a name="func_filter_exclude"></a>**filter_exclude**: Remove values that match a set of patterns. [Back to Top](#gn-reference) |
Sylvain Defresne | aff489a | 2020-03-11 18:27:43 +0100 | [diff] [blame] | 2601 | |
| 2602 | ``` |
| 2603 | filter_exclude(values, exclude_patterns) |
| 2604 | |
| 2605 | The argument values must be a list of strings. |
| 2606 | |
| 2607 | The argument exclude_patterns must be a list of file patterns (see |
| 2608 | "gn help file_pattern"). Any elements in values matching at least one |
| 2609 | of those patterns will be excluded. |
| 2610 | ``` |
| 2611 | |
| 2612 | #### **Examples** |
| 2613 | ``` |
| 2614 | values = [ "foo.cc", "foo.h", "foo.proto" ] |
| 2615 | result = filter_exclude(values, [ "*.proto" ]) |
| 2616 | # result will be [ "foo.cc", "foo.h" ] |
| 2617 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2618 | ### <a name="func_filter_include"></a>**filter_include**: Remove values that do not match a set of patterns. [Back to Top](#gn-reference) |
Sylvain Defresne | aff489a | 2020-03-11 18:27:43 +0100 | [diff] [blame] | 2619 | |
| 2620 | ``` |
| 2621 | filter_include(values, include_patterns) |
| 2622 | |
| 2623 | The argument values must be a list of strings. |
| 2624 | |
| 2625 | The argument include_patterns must be a list of file patterns (see |
| 2626 | "gn help file_pattern"). Only elements from values matching at least |
| 2627 | one of the pattern will be included. |
| 2628 | ``` |
| 2629 | |
| 2630 | #### **Examples** |
| 2631 | ``` |
| 2632 | values = [ "foo.cc", "foo.h", "foo.proto" ] |
| 2633 | result = filter_include(values, [ "*.proto" ]) |
| 2634 | # result will be [ "foo.proto" ] |
| 2635 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2636 | ### <a name="func_filter_labels_exclude"></a>**filter_labels_exclude**: Remove labels that match a set of patterns. [Back to Top](#gn-reference) |
Aaron Wood | e05c0aa | 2024-02-14 13:40:53 -0800 | [diff] [blame] | 2637 | |
| 2638 | ``` |
| 2639 | filter_labels_exclude(labels, exclude_patterns) |
| 2640 | |
| 2641 | The argument labels must be a list of strings. |
| 2642 | |
| 2643 | The argument exclude_patterns must be a list of label patterns (see |
| 2644 | "gn help label_pattern"). Only elements from labels matching at least |
| 2645 | one of the patterns will be excluded. |
| 2646 | ``` |
| 2647 | |
| 2648 | #### **Examples** |
| 2649 | ``` |
| 2650 | labels = [ "//foo:baz", "//foo/bar:baz", "//bar:baz" ] |
| 2651 | result = filter_labels_exclude(labels, [ "//foo:*" ]) |
| 2652 | # result will be [ "//foo/bar:baz", "//bar:baz" ] |
| 2653 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2654 | ### <a name="func_filter_labels_include"></a>**filter_labels_include**: Remove labels that do not match a set of patterns. [Back to Top](#gn-reference) |
Aaron Wood | e05c0aa | 2024-02-14 13:40:53 -0800 | [diff] [blame] | 2655 | |
| 2656 | ``` |
| 2657 | filter_labels_include(labels, include_patterns) |
| 2658 | |
| 2659 | The argument labels must be a list of strings. |
| 2660 | |
| 2661 | The argument include_patterns must be a list of label patterns (see |
| 2662 | "gn help label_pattern"). Only elements from labels matching at least |
| 2663 | one of the patterns will be included. |
| 2664 | ``` |
| 2665 | |
| 2666 | #### **Examples** |
| 2667 | ``` |
| 2668 | labels = [ "//foo:baz", "//foo/bar:baz", "//bar:baz" ] |
| 2669 | result = filter_labels_include(labels, [ "//foo:*" ]) |
| 2670 | # result will be [ "//foo:baz" ] |
| 2671 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2672 | ### <a name="func_foreach"></a>**foreach**: Iterate over a list. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2673 | |
| 2674 | ``` |
| 2675 | foreach(<loop_var>, <list>) { |
| 2676 | <loop contents> |
| 2677 | } |
| 2678 | |
| 2679 | Executes the loop contents block over each item in the list, assigning the |
| 2680 | loop_var to each item in sequence. The <loop_var> will be a copy so assigning |
| 2681 | to it will not mutate the list. The loop will iterate over a copy of <list> |
| 2682 | so mutating it inside the loop will not affect iteration. |
| 2683 | |
| 2684 | The block does not introduce a new scope, so that variable assignments inside |
| 2685 | the loop will be visible once the loop terminates. |
| 2686 | |
| 2687 | The loop variable will temporarily shadow any existing variables with the |
| 2688 | same name for the duration of the loop. After the loop terminates the loop |
| 2689 | variable will no longer be in scope, and the previous value (if any) will be |
| 2690 | restored. |
| 2691 | ``` |
| 2692 | |
| 2693 | #### **Example** |
| 2694 | |
| 2695 | ``` |
| 2696 | mylist = [ "a", "b", "c" ] |
| 2697 | foreach(i, mylist) { |
| 2698 | print(i) |
| 2699 | } |
| 2700 | |
| 2701 | Prints: |
| 2702 | a |
| 2703 | b |
| 2704 | c |
| 2705 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2706 | ### <a name="func_forward_variables_from"></a>**forward_variables_from**: Copies variables from a different scope. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2707 | |
| 2708 | ``` |
| 2709 | forward_variables_from(from_scope, variable_list_or_star, |
| 2710 | variable_to_not_forward_list = []) |
| 2711 | |
| 2712 | Copies the given variables from the given scope to the local scope if they |
| 2713 | exist. This is normally used in the context of templates to use the values of |
| 2714 | variables defined in the template invocation to a template-defined target. |
| 2715 | |
| 2716 | The variables in the given variable_list will be copied if they exist in the |
| 2717 | given scope or any enclosing scope. If they do not exist, nothing will happen |
| 2718 | and they be left undefined in the current scope. |
| 2719 | |
| 2720 | As a special case, if the variable_list is a string with the value of "*", |
| 2721 | all variables from the given scope will be copied. "*" only copies variables |
| 2722 | set directly on the from_scope, not enclosing ones. Otherwise it would |
| 2723 | duplicate all global variables. |
| 2724 | |
| 2725 | When an explicit list of variables is supplied, if the variable exists in the |
| 2726 | current (destination) scope already, an error will be thrown. If "*" is |
| 2727 | specified, variables in the current scope will be clobbered (the latter is |
| 2728 | important because most targets have an implicit configs list, which means it |
| 2729 | wouldn't work at all if it didn't clobber). |
| 2730 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2731 | If variables_to_not_forward_list is non-empty, then it must contains a list |
| 2732 | of variable names that will not be forwarded. This is mostly useful when |
| 2733 | variable_list_or_star has a value of "*". |
| 2734 | ``` |
| 2735 | |
| 2736 | #### **Examples** |
| 2737 | |
| 2738 | ``` |
Andrew Grieve | 57a964c | 2018-09-14 10:50:56 -0400 | [diff] [blame] | 2739 | # forward_variables_from(invoker, ["foo"]) |
| 2740 | # is equivalent to: |
| 2741 | assert(!defined(foo)) |
| 2742 | if (defined(invoker.foo)) { |
| 2743 | foo = invoker.foo |
| 2744 | } |
| 2745 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2746 | # This is a common action template. It would invoke a script with some given |
| 2747 | # parameters, and wants to use the various types of deps and the visibility |
| 2748 | # from the invoker if it's defined. It also injects an additional dependency |
| 2749 | # to all targets. |
| 2750 | template("my_test") { |
| 2751 | action(target_name) { |
| 2752 | forward_variables_from(invoker, [ "data_deps", "deps", |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 2753 | "public_deps", "visibility"]) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2754 | # Add our test code to the dependencies. |
| 2755 | # "deps" may or may not be defined at this point. |
| 2756 | if (defined(deps)) { |
| 2757 | deps += [ "//tools/doom_melon" ] |
| 2758 | } else { |
| 2759 | deps = [ "//tools/doom_melon" ] |
| 2760 | } |
| 2761 | } |
| 2762 | } |
| 2763 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 2764 | # This is a template around a target whose type depends on a global variable. |
| 2765 | # It forwards all values from the invoker. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2766 | template("my_wrapper") { |
| 2767 | target(my_wrapper_target_type, target_name) { |
| 2768 | forward_variables_from(invoker, "*") |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | # A template that wraps another. It adds behavior based on one |
| 2773 | # variable, and forwards all others to the nested target. |
| 2774 | template("my_ios_test_app") { |
| 2775 | ios_test_app(target_name) { |
| 2776 | forward_variables_from(invoker, "*", ["test_bundle_name"]) |
| 2777 | if (!defined(extra_substitutions)) { |
| 2778 | extra_substitutions = [] |
| 2779 | } |
| 2780 | extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ] |
| 2781 | } |
| 2782 | } |
| 2783 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2784 | ### <a name="func_get_label_info"></a>**get_label_info**: Get an attribute from a target's label. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2785 | |
| 2786 | ``` |
| 2787 | get_label_info(target_label, what) |
| 2788 | |
| 2789 | Given the label of a target, returns some attribute of that target. The |
| 2790 | target need not have been previously defined in the same file, since none of |
| 2791 | the attributes depend on the actual target definition, only the label itself. |
| 2792 | |
| 2793 | See also "gn help get_target_outputs". |
| 2794 | ``` |
| 2795 | |
| 2796 | #### **Possible values for the "what" parameter** |
| 2797 | |
| 2798 | ``` |
| 2799 | "name" |
| 2800 | The short name of the target. This will match the value of the |
| 2801 | "target_name" variable inside that target's declaration. For the label |
| 2802 | "//foo/bar:baz" this will return "baz". |
| 2803 | |
| 2804 | "dir" |
| 2805 | The directory containing the target's definition, with no slash at the |
| 2806 | end. For the label "//foo/bar:baz" this will return "//foo/bar". |
| 2807 | |
| 2808 | "target_gen_dir" |
| 2809 | The generated file directory for the target. This will match the value of |
| 2810 | the "target_gen_dir" variable when inside that target's declaration. |
| 2811 | |
| 2812 | "root_gen_dir" |
| 2813 | The root of the generated file tree for the target. This will match the |
| 2814 | value of the "root_gen_dir" variable when inside that target's |
| 2815 | declaration. |
| 2816 | |
| 2817 | "target_out_dir |
| 2818 | The output directory for the target. This will match the value of the |
| 2819 | "target_out_dir" variable when inside that target's declaration. |
| 2820 | |
| 2821 | "root_out_dir" |
| 2822 | The root of the output file tree for the target. This will match the |
| 2823 | value of the "root_out_dir" variable when inside that target's |
| 2824 | declaration. |
| 2825 | |
| 2826 | "label_no_toolchain" |
| 2827 | The fully qualified version of this label, not including the toolchain. |
| 2828 | For the input ":bar" it might return "//foo:bar". |
| 2829 | |
| 2830 | "label_with_toolchain" |
| 2831 | The fully qualified version of this label, including the toolchain. For |
| 2832 | the input ":bar" it might return "//foo:bar(//toolchain:x64)". |
| 2833 | |
| 2834 | "toolchain" |
| 2835 | The label of the toolchain. This will match the value of the |
| 2836 | "current_toolchain" variable when inside that target's declaration. |
| 2837 | ``` |
| 2838 | |
| 2839 | #### **Examples** |
| 2840 | |
| 2841 | ``` |
| 2842 | get_label_info(":foo", "name") |
| 2843 | # Returns string "foo". |
| 2844 | |
| 2845 | get_label_info("//foo/bar:baz", "target_gen_dir") |
| 2846 | # Returns string "//out/Debug/gen/foo/bar". |
| 2847 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2848 | ### <a name="func_get_path_info"></a>**get_path_info**: Extract parts of a file or directory name. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2849 | |
| 2850 | ``` |
| 2851 | get_path_info(input, what) |
| 2852 | |
| 2853 | The first argument is either a string representing a file or directory name, |
| 2854 | or a list of such strings. If the input is a list the return value will be a |
| 2855 | list containing the result of applying the rule to each item in the input. |
| 2856 | ``` |
| 2857 | |
| 2858 | #### **Possible values for the "what" parameter** |
| 2859 | |
| 2860 | ``` |
| 2861 | "file" |
| 2862 | The substring after the last slash in the path, including the name and |
| 2863 | extension. If the input ends in a slash, the empty string will be |
| 2864 | returned. |
| 2865 | "foo/bar.txt" => "bar.txt" |
| 2866 | "bar.txt" => "bar.txt" |
| 2867 | "foo/" => "" |
| 2868 | "" => "" |
| 2869 | |
| 2870 | "name" |
| 2871 | The substring of the file name not including the extension. |
| 2872 | "foo/bar.txt" => "bar" |
| 2873 | "foo/bar" => "bar" |
| 2874 | "foo/" => "" |
| 2875 | |
| 2876 | "extension" |
| 2877 | The substring following the last period following the last slash, or the |
| 2878 | empty string if not found. The period is not included. |
| 2879 | "foo/bar.txt" => "txt" |
| 2880 | "foo/bar" => "" |
| 2881 | |
| 2882 | "dir" |
| 2883 | The directory portion of the name, not including the slash. |
| 2884 | "foo/bar.txt" => "foo" |
| 2885 | "//foo/bar" => "//foo" |
| 2886 | "foo" => "." |
| 2887 | |
| 2888 | The result will never end in a slash, so if the resulting is empty, the |
| 2889 | system ("/") or source ("//") roots, a "." will be appended such that it |
| 2890 | is always legal to append a slash and a filename and get a valid path. |
| 2891 | |
| 2892 | "out_dir" |
| 2893 | The output file directory corresponding to the path of the given file, |
| 2894 | not including a trailing slash. |
| 2895 | "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar" |
| 2896 | |
| 2897 | "gen_dir" |
| 2898 | The generated file directory corresponding to the path of the given file, |
| 2899 | not including a trailing slash. |
| 2900 | "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar" |
| 2901 | |
| 2902 | "abspath" |
| 2903 | The full absolute path name to the file or directory. It will be resolved |
| 2904 | relative to the current directory, and then the source- absolute version |
| 2905 | will be returned. If the input is system- absolute, the same input will |
| 2906 | be returned. |
| 2907 | "foo/bar.txt" => "//mydir/foo/bar.txt" |
| 2908 | "foo/" => "//mydir/foo/" |
| 2909 | "//foo/bar" => "//foo/bar" (already absolute) |
| 2910 | "/usr/include" => "/usr/include" (already absolute) |
| 2911 | |
| 2912 | If you want to make the path relative to another directory, or to be |
| 2913 | system-absolute, see rebase_path(). |
| 2914 | ``` |
| 2915 | |
| 2916 | #### **Examples** |
| 2917 | ``` |
| 2918 | sources = [ "foo.cc", "foo.h" ] |
| 2919 | result = get_path_info(source, "abspath") |
| 2920 | # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ] |
| 2921 | |
| 2922 | result = get_path_info("//foo/bar/baz.cc", "dir") |
| 2923 | # result will be "//foo/bar" |
| 2924 | |
| 2925 | # Extract the source-absolute directory name, |
Joe Armstrong | 3941394 | 2019-03-15 10:34:03 +0800 | [diff] [blame] | 2926 | result = get_path_info(get_path_info(path, "dir"), "abspath") |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2927 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2928 | ### <a name="func_get_target_outputs"></a>**get_target_outputs**: [file list] Get the list of outputs from a target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2929 | |
| 2930 | ``` |
| 2931 | get_target_outputs(target_label) |
| 2932 | |
| 2933 | Returns a list of output files for the named target. The named target must |
| 2934 | have been previously defined in the current file before this function is |
| 2935 | called (it can't reference targets in other files because there isn't a |
| 2936 | defined execution order, and it obviously can't reference targets that are |
| 2937 | defined after the function call). |
| 2938 | |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2939 | Only copy, generated_file, and action targets are supported. The outputs from |
| 2940 | binary targets will depend on the toolchain definition which won't |
| 2941 | necessarily have been loaded by the time a given line of code has run, and |
| 2942 | source sets and groups have no useful output file. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2943 | ``` |
| 2944 | |
| 2945 | #### **Return value** |
| 2946 | |
| 2947 | ``` |
| 2948 | The names in the resulting list will be absolute file paths (normally like |
| 2949 | "//out/Debug/bar.exe", depending on the build directory). |
| 2950 | |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 2951 | action, copy, and generated_file targets: this will just return the files |
| 2952 | specified in the "outputs" variable of the target. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2953 | |
| 2954 | action_foreach targets: this will return the result of applying the output |
| 2955 | template to the sources (see "gn help source_expansion"). This will be the |
| 2956 | same result (though with guaranteed absolute file paths), as |
| 2957 | process_file_template will return for those inputs (see "gn help |
| 2958 | process_file_template"). |
| 2959 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2960 | source sets and groups: this will return a list containing the path of the |
Takuto Ikuta | 50ecf4c | 2024-08-19 18:43:24 +0900 | [diff] [blame] | 2961 | phony target that Ninja completes once all outputs are generated. This |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2962 | probably isn't very useful. |
| 2963 | ``` |
| 2964 | |
| 2965 | #### **Example** |
| 2966 | |
| 2967 | ``` |
| 2968 | # Say this action generates a bunch of C source files. |
| 2969 | action_foreach("my_action") { |
| 2970 | sources = [ ... ] |
| 2971 | outputs = [ ... ] |
| 2972 | } |
| 2973 | |
| 2974 | # Compile the resulting source files into a source set. |
| 2975 | source_set("my_lib") { |
| 2976 | sources = get_target_outputs(":my_action") |
| 2977 | } |
| 2978 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2979 | ### <a name="func_getenv"></a>**getenv**: Get an environment variable. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 2980 | |
| 2981 | ``` |
| 2982 | value = getenv(env_var_name) |
| 2983 | |
| 2984 | Returns the value of the given environment variable. If the value is not |
| 2985 | found, it will try to look up the variable with the "opposite" case (based on |
| 2986 | the case of the first letter of the variable), but is otherwise |
| 2987 | case-sensitive. |
| 2988 | |
| 2989 | If the environment variable is not found, the empty string will be returned. |
| 2990 | Note: it might be nice to extend this if we had the concept of "none" in the |
| 2991 | language to indicate lookup failure. |
| 2992 | ``` |
| 2993 | |
| 2994 | #### **Example** |
| 2995 | |
| 2996 | ``` |
| 2997 | home_dir = getenv("HOME") |
| 2998 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 2999 | ### <a name="func_import"></a>**import**: Import a file into the current scope. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3000 | |
| 3001 | ``` |
| 3002 | The import command loads the rules and variables resulting from executing the |
| 3003 | given file into the current scope. |
| 3004 | |
| 3005 | By convention, imported files are named with a .gni extension. |
| 3006 | |
| 3007 | An import is different than a C++ "include". The imported file is executed in |
| 3008 | a standalone environment from the caller of the import command. The results |
| 3009 | of this execution are cached for other files that import the same .gni file. |
| 3010 | |
| 3011 | Note that you can not import a BUILD.gn file that's otherwise used in the |
| 3012 | build. Files must either be imported or implicitly loaded as a result of deps |
| 3013 | rules, but not both. |
| 3014 | |
| 3015 | The imported file's scope will be merged with the scope at the point import |
| 3016 | was called. If there is a conflict (both the current scope and the imported |
| 3017 | file define some variable or rule with the same name but different value), a |
| 3018 | runtime error will be thrown. Therefore, it's good practice to minimize the |
| 3019 | stuff that an imported file defines. |
| 3020 | |
| 3021 | Variables and templates beginning with an underscore '_' are considered |
| 3022 | private and will not be imported. Imported files can use such variables for |
| 3023 | internal computation without affecting other files. |
| 3024 | ``` |
| 3025 | |
| 3026 | #### **Examples** |
| 3027 | |
| 3028 | ``` |
| 3029 | import("//build/rules/idl_compilation_rule.gni") |
| 3030 | |
| 3031 | # Looks in the current directory. |
| 3032 | import("my_vars.gni") |
| 3033 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3034 | ### <a name="func_label_matches"></a>**label_matches**: Returns true if the label matches any of a set of patterns. [Back to Top](#gn-reference) |
Aaron Wood | e05c0aa | 2024-02-14 13:40:53 -0800 | [diff] [blame] | 3035 | |
| 3036 | ``` |
| 3037 | label_matches(target_label, patterns) |
| 3038 | |
| 3039 | The argument patterns must be a list of label patterns (see |
| 3040 | "gn help label_pattern"). If the target_label matches any of the patterns, |
| 3041 | the function returns the value true. |
| 3042 | ``` |
| 3043 | |
| 3044 | #### **Examples** |
| 3045 | ``` |
| 3046 | result = label_matches("//baz:bar", [ "//foo/bar/*", "//baz:*" ]) |
| 3047 | # result will be true |
| 3048 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3049 | ### <a name="func_not_needed"></a>**not_needed**: Mark variables from scope as not needed. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3050 | |
| 3051 | ``` |
| 3052 | not_needed(variable_list_or_star, variable_to_ignore_list = []) |
| 3053 | not_needed(from_scope, variable_list_or_star, |
| 3054 | variable_to_ignore_list = []) |
| 3055 | |
| 3056 | Mark the variables in the current or given scope as not needed, which means |
| 3057 | you will not get an error about unused variables for these. The |
| 3058 | variable_to_ignore_list allows excluding variables from "all matches" if |
| 3059 | variable_list_or_star is "*". |
| 3060 | ``` |
| 3061 | |
| 3062 | #### **Example** |
| 3063 | |
| 3064 | ``` |
| 3065 | not_needed("*", [ "config" ]) |
| 3066 | not_needed([ "data_deps", "deps" ]) |
| 3067 | not_needed(invoker, "*", [ "config" ]) |
| 3068 | not_needed(invoker, [ "data_deps", "deps" ]) |
| 3069 | ``` |
Takuto Ikuta | dae6a44 | 2025-03-05 17:31:58 +0900 | [diff] [blame^] | 3070 | ### <a name="func_path_exists"></a>**path_exists**: Returns whether the given path exists. [Back to Top](#gn-reference) |
Andrew Grieve | f98b6d7 | 2025-02-25 08:54:51 -0800 | [diff] [blame] | 3071 | |
| 3072 | ``` |
| 3073 | path_exists(path) |
Andrew Grieve | f98b6d7 | 2025-02-25 08:54:51 -0800 | [diff] [blame] | 3074 | ``` |
| 3075 | |
Takuto Ikuta | dae6a44 | 2025-03-05 17:31:58 +0900 | [diff] [blame^] | 3076 | #### **Examples**: |
Andrew Grieve | f98b6d7 | 2025-02-25 08:54:51 -0800 | [diff] [blame] | 3077 | ``` |
| 3078 | path_exists("//") # true |
| 3079 | path_exists("BUILD.gn") # true |
| 3080 | path_exists("/abs-non-existent") # false |
| 3081 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3082 | ### <a name="func_pool"></a>**pool**: Defines a pool object. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3083 | |
| 3084 | ``` |
| 3085 | Pool objects can be applied to a tool to limit the parallelism of the |
| 3086 | build. This object has a single property "depth" corresponding to |
| 3087 | the number of tasks that may run simultaneously. |
| 3088 | |
| 3089 | As the file containing the pool definition may be executed in the |
| 3090 | context of more than one toolchain it is recommended to specify an |
| 3091 | explicit toolchain when defining and referencing a pool. |
| 3092 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 3093 | A pool named "console" defined in the root build file represents Ninja's |
| 3094 | console pool. Targets using this pool will have access to the console's |
| 3095 | stdin and stdout, and output will not be buffered. This special pool must |
| 3096 | have a depth of 1. Pools not defined in the root must not be named "console". |
| 3097 | The console pool can only be defined for the default toolchain. |
| 3098 | Refer to the Ninja documentation on the console pool for more info. |
| 3099 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3100 | A pool is referenced by its label just like a target. |
| 3101 | ``` |
| 3102 | |
| 3103 | #### **Variables** |
| 3104 | |
| 3105 | ``` |
| 3106 | depth* |
| 3107 | * = required |
| 3108 | ``` |
| 3109 | |
| 3110 | #### **Example** |
| 3111 | |
| 3112 | ``` |
| 3113 | if (current_toolchain == default_toolchain) { |
| 3114 | pool("link_pool") { |
| 3115 | depth = 1 |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | toolchain("toolchain") { |
| 3120 | tool("link") { |
| 3121 | command = "..." |
Petr Hosek | add1bb1 | 2019-09-11 14:39:38 -0700 | [diff] [blame] | 3122 | pool = ":link_pool($default_toolchain)" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3126 | ### <a name="func_print"></a>**print**: Prints to the console. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3127 | |
| 3128 | ``` |
| 3129 | Prints all arguments to the console separated by spaces. A newline is |
| 3130 | automatically appended to the end. |
| 3131 | |
| 3132 | This function is intended for debugging. Note that build files are run in |
| 3133 | parallel so you may get interleaved prints. A buildfile may also be executed |
| 3134 | more than once in parallel in the context of different toolchains so the |
| 3135 | prints from one file may be duplicated or |
| 3136 | interleaved with itself. |
| 3137 | ``` |
| 3138 | |
| 3139 | #### **Examples** |
| 3140 | |
| 3141 | ``` |
| 3142 | print("Hello world") |
| 3143 | |
| 3144 | print(sources, deps) |
| 3145 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3146 | ### <a name="func_print_stack_trace"></a>**print_stack_trace**: Prints a stack trace. [Back to Top](#gn-reference) |
Aaron Wood | 24062bb | 2022-04-25 20:37:48 -0700 | [diff] [blame] | 3147 | |
| 3148 | ``` |
| 3149 | Prints the current file location, and all template invocations that led up to |
| 3150 | this location, to the console. |
| 3151 | ``` |
| 3152 | |
| 3153 | #### **Examples** |
| 3154 | |
| 3155 | ``` |
| 3156 | template("foo"){ |
| 3157 | print_stack_trace() |
| 3158 | } |
| 3159 | template("bar"){ |
| 3160 | foo(target_name + ".foo") { |
| 3161 | baz = invoker.baz |
| 3162 | } |
| 3163 | } |
| 3164 | bar("lala") { |
| 3165 | baz = 42 |
| 3166 | } |
| 3167 | |
| 3168 | will print out the following: |
| 3169 | |
| 3170 | print_stack_trace() initiated at //build.gn:2 |
| 3171 | bar("lala") //BUILD.gn:9 |
| 3172 | foo("lala.foo") //BUILD.gn:5 |
| 3173 | print_stack_trace() //BUILD.gn:2 |
| 3174 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3175 | ### <a name="func_process_file_template"></a>**process_file_template**: Do template expansion over a list of files. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3176 | |
| 3177 | ``` |
| 3178 | process_file_template(source_list, template) |
| 3179 | |
| 3180 | process_file_template applies a template list to a source file list, |
| 3181 | returning the result of applying each template to each source. This is |
| 3182 | typically used for computing output file names from input files. |
| 3183 | |
| 3184 | In most cases, get_target_outputs() will give the same result with shorter, |
| 3185 | more maintainable code. This function should only be used when that function |
| 3186 | can't be used (like there's no target or the target is defined in another |
| 3187 | build file). |
| 3188 | ``` |
| 3189 | |
| 3190 | #### **Arguments** |
| 3191 | |
| 3192 | ``` |
| 3193 | The source_list is a list of file names. |
| 3194 | |
| 3195 | The template can be a string or a list. If it is a list, multiple output |
| 3196 | strings are generated for each input. |
| 3197 | |
| 3198 | The template should contain source expansions to which each name in the |
| 3199 | source list is applied. See "gn help source_expansion". |
| 3200 | ``` |
| 3201 | |
| 3202 | #### **Example** |
| 3203 | |
| 3204 | ``` |
| 3205 | sources = [ |
| 3206 | "foo.idl", |
| 3207 | "bar.idl", |
| 3208 | ] |
| 3209 | myoutputs = process_file_template( |
| 3210 | sources, |
| 3211 | [ "$target_gen_dir/{{source_name_part}}.cc", |
| 3212 | "$target_gen_dir/{{source_name_part}}.h" ]) |
| 3213 | |
| 3214 | The result in this case will be: |
| 3215 | [ "//out/Debug/foo.cc" |
| 3216 | "//out/Debug/foo.h" |
| 3217 | "//out/Debug/bar.cc" |
| 3218 | "//out/Debug/bar.h" ] |
| 3219 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3220 | ### <a name="func_read_file"></a>**read_file**: Read a file into a variable. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3221 | |
| 3222 | ``` |
| 3223 | read_file(filename, input_conversion) |
| 3224 | |
| 3225 | Whitespace will be trimmed from the end of the file. Throws an error if the |
| 3226 | file can not be opened. |
| 3227 | ``` |
| 3228 | |
| 3229 | #### **Arguments** |
| 3230 | |
| 3231 | ``` |
| 3232 | filename |
| 3233 | Filename to read, relative to the build file. |
| 3234 | |
| 3235 | input_conversion |
Sylvain Defresne | 72d5a6e | 2021-06-15 17:01:32 +0200 | [diff] [blame] | 3236 | Controls how the file is read and parsed. See `gn help io_conversion`. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3237 | ``` |
| 3238 | |
| 3239 | #### **Example** |
| 3240 | |
| 3241 | ``` |
| 3242 | lines = read_file("foo.txt", "list lines") |
| 3243 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3244 | ### <a name="func_rebase_path"></a>**rebase_path**: Rebase a file or directory to another location. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3245 | |
| 3246 | ``` |
| 3247 | converted = rebase_path(input, |
| 3248 | new_base = "", |
| 3249 | current_base = ".") |
| 3250 | |
| 3251 | Takes a string argument representing a file name, or a list of such strings |
| 3252 | and converts it/them to be relative to a different base directory. |
| 3253 | |
| 3254 | When invoking the compiler or scripts, GN will automatically convert sources |
| 3255 | and include directories to be relative to the build directory. However, if |
| 3256 | you're passing files directly in the "args" array or doing other manual |
| 3257 | manipulations where GN doesn't know something is a file name, you will need |
| 3258 | to convert paths to be relative to what your tool is expecting. |
| 3259 | |
| 3260 | The common case is to use this to convert paths relative to the current |
| 3261 | directory to be relative to the build directory (which will be the current |
| 3262 | directory when executing scripts). |
| 3263 | |
| 3264 | If you want to convert a file path to be source-absolute (that is, beginning |
| 3265 | with a double slash like "//foo/bar"), you should use the get_path_info() |
| 3266 | function. This function won't work because it will always make relative |
| 3267 | paths, and it needs to support making paths relative to the source root, so |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 3268 | it can't also generate source-absolute paths without more special-cases. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3269 | ``` |
| 3270 | |
| 3271 | #### **Arguments** |
| 3272 | |
| 3273 | ``` |
| 3274 | input |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 3275 | A string or list of strings representing file or directory names. These |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3276 | can be relative paths ("foo/bar.txt"), system absolute paths |
| 3277 | ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt"). |
| 3278 | |
| 3279 | new_base |
| 3280 | The directory to convert the paths to be relative to. This can be an |
| 3281 | absolute path or a relative path (which will be treated as being relative |
| 3282 | to the current BUILD-file's directory). |
| 3283 | |
| 3284 | As a special case, if new_base is the empty string (the default), all |
| 3285 | paths will be converted to system-absolute native style paths with system |
| 3286 | path separators. This is useful for invoking external programs. |
| 3287 | |
| 3288 | current_base |
| 3289 | Directory representing the base for relative paths in the input. If this |
| 3290 | is not an absolute path, it will be treated as being relative to the |
| 3291 | current build file. Use "." (the default) to convert paths from the |
| 3292 | current BUILD-file's directory. |
| 3293 | ``` |
| 3294 | |
| 3295 | #### **Return value** |
| 3296 | |
| 3297 | ``` |
| 3298 | The return value will be the same type as the input value (either a string or |
| 3299 | a list of strings). All relative and source-absolute file names will be |
| 3300 | converted to be relative to the requested output System-absolute paths will |
| 3301 | be unchanged. |
| 3302 | |
| 3303 | Whether an output path will end in a slash will match whether the |
| 3304 | corresponding input path ends in a slash. It will return "." or "./" |
| 3305 | (depending on whether the input ends in a slash) to avoid returning empty |
| 3306 | strings. This means if you want a root path ("//" or "/") not ending in a |
| 3307 | slash, you can add a dot ("//."). |
| 3308 | ``` |
| 3309 | |
| 3310 | #### **Example** |
| 3311 | |
| 3312 | ``` |
| 3313 | # Convert a file in the current directory to be relative to the build |
| 3314 | # directory (the current dir when executing compilers and scripts). |
| 3315 | foo = rebase_path("myfile.txt", root_build_dir) |
| 3316 | # might produce "../../project/myfile.txt". |
| 3317 | |
| 3318 | # Convert a file to be system absolute: |
| 3319 | foo = rebase_path("myfile.txt") |
| 3320 | # Might produce "D:\\source\\project\\myfile.txt" on Windows or |
| 3321 | # "/home/you/source/project/myfile.txt" on Linux. |
| 3322 | |
| 3323 | # Typical usage for converting to the build directory for a script. |
| 3324 | action("myscript") { |
| 3325 | # Don't convert sources, GN will automatically convert these to be relative |
| 3326 | # to the build directory when it constructs the command line for your |
| 3327 | # script. |
| 3328 | sources = [ "foo.txt", "bar.txt" ] |
| 3329 | |
| 3330 | # Extra file args passed manually need to be explicitly converted |
| 3331 | # to be relative to the build directory: |
| 3332 | args = [ |
| 3333 | "--data", |
| 3334 | rebase_path("//mything/data/input.dat", root_build_dir), |
| 3335 | "--rel", |
| 3336 | rebase_path("relative_path.txt", root_build_dir) |
| 3337 | ] + rebase_path(sources, root_build_dir) |
| 3338 | } |
| 3339 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3340 | ### <a name="func_set_default_toolchain"></a>**set_default_toolchain**: Sets the default toolchain name. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3341 | |
| 3342 | ``` |
| 3343 | set_default_toolchain(toolchain_label) |
| 3344 | |
| 3345 | The given label should identify a toolchain definition (see "gn help |
| 3346 | toolchain"). This toolchain will be used for all targets unless otherwise |
| 3347 | specified. |
| 3348 | |
| 3349 | This function is only valid to call during the processing of the build |
| 3350 | configuration file. Since the build configuration file is processed |
| 3351 | separately for each toolchain, this function will be a no-op when called |
| 3352 | under any non-default toolchains. |
| 3353 | |
| 3354 | For example, the default toolchain should be appropriate for the current |
| 3355 | environment. If the current environment is 32-bit and somebody references a |
| 3356 | target with a 64-bit toolchain, we wouldn't want processing of the build |
| 3357 | config file for the 64-bit toolchain to reset the default toolchain to |
| 3358 | 64-bit, we want to keep it 32-bits. |
| 3359 | ``` |
| 3360 | |
| 3361 | #### **Argument** |
| 3362 | |
| 3363 | ``` |
| 3364 | toolchain_label |
| 3365 | Toolchain name. |
| 3366 | ``` |
| 3367 | |
| 3368 | #### **Example** |
| 3369 | |
| 3370 | ``` |
| 3371 | # Set default toolchain only has an effect when run in the context of the |
| 3372 | # default toolchain. Pick the right one according to the current CPU |
| 3373 | # architecture. |
| 3374 | if (target_cpu == "x64") { |
| 3375 | set_default_toolchain("//toolchains:64") |
| 3376 | } else if (target_cpu == "x86") { |
| 3377 | set_default_toolchain("//toolchains:32") |
| 3378 | } |
| 3379 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3380 | ### <a name="func_set_defaults"></a>**set_defaults**: Set default values for a target type. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3381 | |
| 3382 | ``` |
| 3383 | set_defaults(<target_type_name>) { <values...> } |
| 3384 | |
| 3385 | Sets the default values for a given target type. Whenever target_type_name is |
| 3386 | seen in the future, the values specified in set_default's block will be |
| 3387 | copied into the current scope. |
| 3388 | |
| 3389 | When the target type is used, the variable copying is very strict. If a |
| 3390 | variable with that name is already in scope, the build will fail with an |
| 3391 | error. |
| 3392 | |
| 3393 | set_defaults can be used for built-in target types ("executable", |
| 3394 | "shared_library", etc.) and custom ones defined via the "template" command. |
| 3395 | It can be called more than once and the most recent call in any scope will |
| 3396 | apply, but there is no way to refer to the previous defaults and modify them |
| 3397 | (each call to set_defaults must supply a complete list of all defaults it |
| 3398 | wants). If you want to share defaults, store them in a separate variable. |
| 3399 | ``` |
| 3400 | |
| 3401 | #### **Example** |
| 3402 | |
| 3403 | ``` |
| 3404 | set_defaults("static_library") { |
| 3405 | configs = [ "//tools/mything:settings" ] |
| 3406 | } |
| 3407 | |
Nico Weber | e49cb72 | 2018-08-28 13:10:29 -0400 | [diff] [blame] | 3408 | static_library("mylib") { |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3409 | # The configs will be auto-populated as above. You can remove it if |
| 3410 | # you don't want the default for a particular default: |
| 3411 | configs -= [ "//tools/mything:settings" ] |
| 3412 | } |
| 3413 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3414 | ### <a name="func_split_list"></a>**split_list**: Splits a list into N different sub-lists. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3415 | |
| 3416 | ``` |
| 3417 | result = split_list(input, n) |
| 3418 | |
| 3419 | Given a list and a number N, splits the list into N sub-lists of |
| 3420 | approximately equal size. The return value is a list of the sub-lists. The |
| 3421 | result will always be a list of size N. If N is greater than the number of |
| 3422 | elements in the input, it will be padded with empty lists. |
| 3423 | |
| 3424 | The expected use is to divide source files into smaller uniform chunks. |
| 3425 | ``` |
| 3426 | |
| 3427 | #### **Example** |
| 3428 | |
| 3429 | ``` |
| 3430 | The code: |
| 3431 | mylist = [1, 2, 3, 4, 5, 6] |
| 3432 | print(split_list(mylist, 3)) |
| 3433 | |
| 3434 | Will print: |
| 3435 | [[1, 2], [3, 4], [5, 6] |
| 3436 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3437 | ### <a name="func_string_join"></a>**string_join**: Concatenates a list of strings with a separator. [Back to Top](#gn-reference) |
Keir Mierle | 6ae6330 | 2019-11-08 23:02:18 -0800 | [diff] [blame] | 3438 | |
| 3439 | ``` |
| 3440 | result = string_join(separator, strings) |
| 3441 | |
| 3442 | Concatenate a list of strings with intervening occurrences of separator. |
| 3443 | ``` |
| 3444 | |
| 3445 | #### **Examples** |
| 3446 | |
| 3447 | ``` |
| 3448 | string_join("", ["a", "b", "c"]) --> "abc" |
| 3449 | string_join("|", ["a", "b", "c"]) --> "a|b|c" |
| 3450 | string_join(", ", ["a", "b", "c"]) --> "a, b, c" |
| 3451 | string_join("s", ["", ""]) --> "s" |
| 3452 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3453 | ### <a name="func_string_replace"></a>**string_replace**: Replaces substring in the given string. [Back to Top](#gn-reference) |
Petr Hosek | c0744ed | 2018-08-04 16:13:59 -0700 | [diff] [blame] | 3454 | |
| 3455 | ``` |
| 3456 | result = string_replace(str, old, new[, max]) |
| 3457 | |
| 3458 | Returns a copy of the string str in which the occurrences of old have been |
| 3459 | replaced with new, optionally restricting the number of replacements. The |
| 3460 | replacement is performed sequentially, so if new contains old, it won't be |
| 3461 | replaced. |
| 3462 | ``` |
| 3463 | |
| 3464 | #### **Example** |
| 3465 | |
| 3466 | ``` |
| 3467 | The code: |
| 3468 | mystr = "Hello, world!" |
| 3469 | print(string_replace(mystr, "world", "GN")) |
| 3470 | |
| 3471 | Will print: |
| 3472 | Hello, GN! |
| 3473 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3474 | ### <a name="func_string_split"></a>**string_split**: Split string into a list of strings. [Back to Top](#gn-reference) |
Keir Mierle | 6ae6330 | 2019-11-08 23:02:18 -0800 | [diff] [blame] | 3475 | |
| 3476 | ``` |
| 3477 | result = string_split(str[, sep]) |
| 3478 | |
| 3479 | Split string into all substrings separated by separator and returns a list |
| 3480 | of the substrings between those separators. |
| 3481 | |
| 3482 | If the separator argument is omitted, the split is by any whitespace, and |
| 3483 | any leading/trailing whitespace is ignored; similar to Python's str.split(). |
| 3484 | ``` |
| 3485 | |
| 3486 | #### **Examples without a separator (split on whitespace)**: |
| 3487 | |
| 3488 | ``` |
| 3489 | string_split("") --> [] |
| 3490 | string_split("a") --> ["a"] |
| 3491 | string_split(" aa bb") --> ["aa", "bb"] |
| 3492 | ``` |
| 3493 | |
| 3494 | #### **Examples with a separator (split on separators)**: |
| 3495 | |
| 3496 | ``` |
| 3497 | string_split("", "|") --> [""] |
| 3498 | string_split(" a b ", " ") --> ["", "", "a", "b", "", ""] |
| 3499 | string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"] |
| 3500 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3501 | ### <a name="func_template"></a>**template**: Define a template rule. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3502 | |
| 3503 | ``` |
| 3504 | A template defines a custom name that acts like a function. It provides a way |
| 3505 | to add to the built-in target types. |
| 3506 | |
| 3507 | The template() function is used to declare a template. To invoke the |
| 3508 | template, just use the name of the template like any other target type. |
| 3509 | |
| 3510 | Often you will want to declare your template in a special file that other |
| 3511 | files will import (see "gn help import") so your template rule can be shared |
| 3512 | across build files. |
| 3513 | ``` |
| 3514 | |
| 3515 | #### **Variables and templates**: |
| 3516 | |
| 3517 | ``` |
| 3518 | When you call template() it creates a closure around all variables currently |
| 3519 | in scope with the code in the template block. When the template is invoked, |
| 3520 | the closure will be executed. |
| 3521 | |
| 3522 | When the template is invoked, the code in the caller is executed and passed |
| 3523 | to the template code as an implicit "invoker" variable. The template uses |
| 3524 | this to read state out of the invoking code. |
| 3525 | |
| 3526 | One thing explicitly excluded from the closure is the "current directory" |
| 3527 | against which relative file names are resolved. The current directory will be |
| 3528 | that of the invoking code, since typically that code specifies the file |
| 3529 | names. This means all files internal to the template should use absolute |
| 3530 | names. |
| 3531 | |
| 3532 | A template will typically forward some or all variables from the invoking |
| 3533 | scope to a target that it defines. Often, such variables might be optional. |
| 3534 | Use the pattern: |
| 3535 | |
| 3536 | if (defined(invoker.deps)) { |
| 3537 | deps = invoker.deps |
| 3538 | } |
| 3539 | |
| 3540 | The function forward_variables_from() provides a shortcut to forward one or |
| 3541 | more or possibly all variables in this manner: |
| 3542 | |
| 3543 | forward_variables_from(invoker, ["deps", "public_deps"]) |
| 3544 | ``` |
| 3545 | |
| 3546 | #### **Target naming** |
| 3547 | |
| 3548 | ``` |
| 3549 | Your template should almost always define a built-in target with the name the |
| 3550 | template invoker specified. For example, if you have an IDL template and |
| 3551 | somebody does: |
| 3552 | idl("foo") {... |
| 3553 | you will normally want this to expand to something defining a source_set or |
| 3554 | static_library named "foo" (among other things you may need). This way, when |
| 3555 | another target specifies a dependency on "foo", the static_library or |
| 3556 | source_set will be linked. |
| 3557 | |
| 3558 | It is also important that any other targets your template expands to have |
| 3559 | unique names, or you will get collisions. |
| 3560 | |
| 3561 | Access the invoking name in your template via the implicit "target_name" |
| 3562 | variable. This should also be the basis for how other targets that a template |
| 3563 | expands to ensure uniqueness. |
| 3564 | |
| 3565 | A typical example would be a template that defines an action to generate some |
| 3566 | source files, and a source_set to compile that source. Your template would |
| 3567 | name the source_set "target_name" because that's what you want external |
| 3568 | targets to depend on to link your code. And you would name the action |
| 3569 | something like "${target_name}_action" to make it unique. The source set |
| 3570 | would have a dependency on the action to make it run. |
| 3571 | ``` |
| 3572 | |
| 3573 | #### **Overriding builtin targets** |
| 3574 | |
| 3575 | ``` |
| 3576 | You can use template to redefine a built-in target in which case your template |
| 3577 | takes a precedence over the built-in one. All uses of the target from within |
| 3578 | the template definition will refer to the built-in target which makes it |
| 3579 | possible to extend the behavior of the built-in target: |
| 3580 | |
| 3581 | template("shared_library") { |
| 3582 | shared_library(shlib) { |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 3583 | forward_variables_from(invoker, "*") |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3584 | ... |
| 3585 | } |
| 3586 | } |
| 3587 | ``` |
| 3588 | |
| 3589 | #### **Example of defining a template** |
| 3590 | |
| 3591 | ``` |
| 3592 | template("my_idl") { |
| 3593 | # Be nice and help callers debug problems by checking that the variables |
| 3594 | # the template requires are defined. This gives a nice message rather than |
| 3595 | # giving the user an error about an undefined variable in the file defining |
| 3596 | # the template |
| 3597 | # |
| 3598 | # You can also use defined() to give default values to variables |
| 3599 | # unspecified by the invoker. |
| 3600 | assert(defined(invoker.sources), |
| 3601 | "Need sources in $target_name listing the idl files.") |
| 3602 | |
| 3603 | # Name of the intermediate target that does the code gen. This must |
| 3604 | # incorporate the target name so it's unique across template |
| 3605 | # instantiations. |
| 3606 | code_gen_target_name = target_name + "_code_gen" |
| 3607 | |
| 3608 | # Intermediate target to convert IDL to C source. Note that the name is |
| 3609 | # based on the name the invoker of the template specified. This way, each |
| 3610 | # time the template is invoked we get a unique intermediate action name |
| 3611 | # (since all target names are in the global scope). |
| 3612 | action_foreach(code_gen_target_name) { |
| 3613 | # Access the scope defined by the invoker via the implicit "invoker" |
| 3614 | # variable. |
| 3615 | sources = invoker.sources |
| 3616 | |
| 3617 | # Note that we need an absolute path for our script file name. The |
| 3618 | # current directory when executing this code will be that of the invoker |
| 3619 | # (this is why we can use the "sources" directly above without having to |
| 3620 | # rebase all of the paths). But if we need to reference a script relative |
| 3621 | # to the template file, we'll need to use an absolute path instead. |
| 3622 | script = "//tools/idl/idl_code_generator.py" |
| 3623 | |
| 3624 | # Tell GN how to expand output names given the sources. |
| 3625 | # See "gn help source_expansion" for more. |
| 3626 | outputs = [ "$target_gen_dir/{{source_name_part}}.cc", |
| 3627 | "$target_gen_dir/{{source_name_part}}.h" ] |
| 3628 | } |
| 3629 | |
| 3630 | # Name the source set the same as the template invocation so instancing |
| 3631 | # this template produces something that other targets can link to in their |
| 3632 | # deps. |
| 3633 | source_set(target_name) { |
| 3634 | # Generates the list of sources, we get these from the action_foreach |
| 3635 | # above. |
| 3636 | sources = get_target_outputs(":$code_gen_target_name") |
| 3637 | |
| 3638 | # This target depends on the files produced by the above code gen target. |
| 3639 | deps = [ ":$code_gen_target_name" ] |
| 3640 | } |
| 3641 | } |
| 3642 | ``` |
| 3643 | |
| 3644 | #### **Example of invoking the resulting template** |
| 3645 | |
| 3646 | ``` |
| 3647 | # This calls the template code above, defining target_name to be |
| 3648 | # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly |
| 3649 | # brackets. |
| 3650 | my_idl("foo_idl_files") { |
| 3651 | # Goes into the template as "invoker.sources". |
| 3652 | sources = [ "foo.idl", "bar.idl" ] |
| 3653 | } |
| 3654 | |
| 3655 | # Here is a target that depends on our template. |
| 3656 | executable("my_exe") { |
| 3657 | # Depend on the name we gave the template call above. Internally, this will |
| 3658 | # produce a dependency from executable to the source_set inside the |
| 3659 | # template (since it has this name), which will in turn depend on the code |
| 3660 | # gen action. |
| 3661 | deps = [ ":foo_idl_files" ] |
| 3662 | } |
| 3663 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 3664 | ### <a name="func_tool"></a>**tool**: Specify arguments to a toolchain tool. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3665 | |
| 3666 | #### **Usage** |
| 3667 | |
| 3668 | ``` |
| 3669 | tool(<tool type>) { |
| 3670 | <tool variables...> |
| 3671 | } |
| 3672 | ``` |
| 3673 | |
| 3674 | #### **Tool types** |
| 3675 | |
| 3676 | ``` |
| 3677 | Compiler tools: |
| 3678 | "cc": C compiler |
| 3679 | "cxx": C++ compiler |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 3680 | "cxx_module": C++ compiler used for Clang .modulemap files |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3681 | "objc": Objective C compiler |
| 3682 | "objcxx": Objective C++ compiler |
| 3683 | "rc": Resource compiler (Windows .rc files) |
| 3684 | "asm": Assembler |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 3685 | "swift": Swift compiler driver |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3686 | |
| 3687 | Linker tools: |
| 3688 | "alink": Linker for static libraries (archives) |
| 3689 | "solink": Linker for shared libraries |
| 3690 | "link": Linker for executables |
| 3691 | |
| 3692 | Other tools: |
| 3693 | "stamp": Tool for creating stamp files |
| 3694 | "copy": Tool to copy files. |
| 3695 | "action": Defaults for actions |
| 3696 | |
| 3697 | Platform specific tools: |
| 3698 | "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle. |
| 3699 | "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs. |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 3700 | |
| 3701 | Rust tools: |
Petr Hosek | 7617b59 | 2019-10-02 09:52:21 -0700 | [diff] [blame] | 3702 | "rust_bin": Tool for compiling Rust binaries |
| 3703 | "rust_cdylib": Tool for compiling C-compatible dynamic libraries. |
| 3704 | "rust_dylib": Tool for compiling Rust dynamic libraries. |
| 3705 | "rust_macro": Tool for compiling Rust procedural macros. |
| 3706 | "rust_rlib": Tool for compiling Rust libraries. |
| 3707 | "rust_staticlib": Tool for compiling Rust static libraries. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3708 | ``` |
| 3709 | |
| 3710 | #### **Tool variables** |
| 3711 | |
| 3712 | ``` |
| 3713 | command [string with substitutions] |
| 3714 | Valid for: all tools except "action" (required) |
| 3715 | |
| 3716 | The command to run. |
| 3717 | |
Joe Armstrong | 23d22bc | 2019-06-28 15:31:03 +0800 | [diff] [blame] | 3718 | command_launcher [string] |
| 3719 | Valid for: all tools except "action" (optional) |
| 3720 | |
| 3721 | The prefix with which to launch the command (e.g. the path to a Goma or |
| 3722 | CCache compiler launcher). |
| 3723 | |
| 3724 | Note that this prefix will not be included in the compilation database or |
| 3725 | IDE files generated from the build. |
| 3726 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3727 | default_output_dir [string with substitutions] |
| 3728 | Valid for: linker tools |
| 3729 | |
| 3730 | Default directory name for the output file relative to the |
| 3731 | root_build_dir. It can contain other substitution patterns. This will |
| 3732 | be the default value for the {{output_dir}} expansion (discussed below) |
| 3733 | but will be overridden by the "output_dir" variable in a target, if one |
| 3734 | is specified. |
| 3735 | |
| 3736 | GN doesn't do anything with this string other than pass it along, |
| 3737 | potentially with target-specific overrides. It is the tool's job to use |
| 3738 | the expansion so that the files will be in the right place. |
| 3739 | |
| 3740 | default_output_extension [string] |
| 3741 | Valid for: linker tools |
| 3742 | |
| 3743 | Extension for the main output of a linkable tool. It includes the |
| 3744 | leading dot. This will be the default value for the |
| 3745 | {{output_extension}} expansion (discussed below) but will be overridden |
| 3746 | by by the "output extension" variable in a target, if one is specified. |
| 3747 | Empty string means no extension. |
| 3748 | |
| 3749 | GN doesn't actually do anything with this extension other than pass it |
| 3750 | along, potentially with target-specific overrides. One would typically |
| 3751 | use the {{output_extension}} value in the "outputs" to read this value. |
| 3752 | |
| 3753 | Example: default_output_extension = ".exe" |
| 3754 | |
| 3755 | depfile [string with substitutions] |
| 3756 | Valid for: compiler tools (optional) |
| 3757 | |
| 3758 | If the tool can write ".d" files, this specifies the name of the |
| 3759 | resulting file. These files are used to list header file dependencies |
| 3760 | (or other implicit input dependencies) that are discovered at build |
| 3761 | time. See also "depsformat". |
| 3762 | |
| 3763 | Example: depfile = "{{output}}.d" |
| 3764 | |
| 3765 | depsformat [string] |
| 3766 | Valid for: compiler tools (when depfile is specified) |
| 3767 | |
| 3768 | Format for the deps outputs. This is either "gcc" or "msvc". See the |
| 3769 | ninja documentation for "deps" for more information. |
| 3770 | |
| 3771 | Example: depsformat = "gcc" |
| 3772 | |
| 3773 | description [string with substitutions, optional] |
| 3774 | Valid for: all tools |
| 3775 | |
| 3776 | What to print when the command is run. |
| 3777 | |
| 3778 | Example: description = "Compiling {{source}}" |
| 3779 | |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 3780 | exe_output_extension [string, optional, rust tools only] |
| 3781 | rlib_output_extension [string, optional, rust tools only] |
| 3782 | dylib_output_extension [string, optional, rust tools only] |
| 3783 | cdylib_output_extension [string, optional, rust tools only] |
Petr Hosek | fe36c7c | 2019-11-26 14:38:08 -0800 | [diff] [blame] | 3784 | rust_proc_macro_output_extension [string, optional, rust tools only] |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 3785 | Valid for: Rust tools |
| 3786 | |
| 3787 | These specify the default tool output for each of the crate types. |
| 3788 | The default is empty for executables, shared, and static libraries and |
| 3789 | ".rlib" for rlibs. Note that the Rust compiler complains with an error |
| 3790 | if external crates do not take the form `lib<name>.rlib` or |
| 3791 | `lib<name>.<shared_extension>`, where `<shared_extension>` is `.so`, |
| 3792 | `.dylib`, or `.dll` as appropriate for the platform. |
| 3793 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3794 | lib_switch [string, optional, link tools only] |
| 3795 | lib_dir_switch [string, optional, link tools only] |
| 3796 | Valid for: Linker tools except "alink" |
| 3797 | |
| 3798 | These strings will be prepended to the libraries and library search |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 3799 | directories, respectively, because linkers differ on how to specify |
| 3800 | them. |
| 3801 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3802 | If you specified: |
| 3803 | lib_switch = "-l" |
| 3804 | lib_dir_switch = "-L" |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 3805 | then the "{{libs}}" expansion for |
| 3806 | [ "freetype", "expat" ] |
| 3807 | would be |
| 3808 | "-lfreetype -lexpat". |
| 3809 | |
| 3810 | framework_switch [string, optional, link tools only] |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 3811 | weak_framework_switch [string, optional, link tools only] |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 3812 | framework_dir_switch [string, optional, link tools only] |
| 3813 | Valid for: Linker tools |
| 3814 | |
| 3815 | These strings will be prepended to the frameworks and framework search |
| 3816 | path directories, respectively, because linkers differ on how to specify |
| 3817 | them. |
| 3818 | |
| 3819 | If you specified: |
| 3820 | framework_switch = "-framework " |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 3821 | weak_framework_switch = "-weak_framework " |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 3822 | framework_dir_switch = "-F" |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 3823 | and: |
| 3824 | framework_dirs = [ "$root_out_dir" ] |
| 3825 | frameworks = [ "UIKit.framework", "Foo.framework" ] |
| 3826 | weak_frameworks = [ "MediaPlayer.framework" ] |
| 3827 | would be: |
| 3828 | "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3829 | |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 3830 | swiftmodule_switch [string, optional, link tools only] |
| 3831 | Valid for: Linker tools except "alink" |
| 3832 | |
| 3833 | The string will be prependend to the path to the .swiftmodule files |
| 3834 | that are embedded in the linker output. |
| 3835 | |
| 3836 | If you specified: |
| 3837 | swiftmodule_swift = "-Wl,-add_ast_path," |
| 3838 | then the "{{swiftmodules}}" expansion for |
| 3839 | [ "obj/foo/Foo.swiftmodule" ] |
| 3840 | would be |
| 3841 | "-Wl,-add_ast_path,obj/foo/Foo.swiftmodule" |
| 3842 | |
danakj | 4fb095d | 2024-05-30 11:40:51 -0400 | [diff] [blame] | 3843 | rust_swiftmodule_switch [string, optional, link tools only] |
| 3844 | Valid for: Linker tools except "alink" |
| 3845 | |
| 3846 | Like swiftmodule_switch, but for targets built/linked with the Rust |
| 3847 | compiler. The string will be prependend to the path to the |
| 3848 | .swiftmodule files that are embedded in the linker output. |
| 3849 | |
| 3850 | If you specified: |
| 3851 | rust_swiftmodule_swift = "-Clink-arg=-Wl,-add_ast_path," |
| 3852 | then the "{{swiftmodules}}" expansion for |
| 3853 | [ "obj/foo/Foo.swiftmodule" ] |
| 3854 | would be |
| 3855 | "-Clink-arg=-Wl,-add_ast_path,obj/foo/Foo.swiftmodule" |
| 3856 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3857 | outputs [list of strings with substitutions] |
| 3858 | Valid for: Linker and compiler tools (required) |
| 3859 | |
| 3860 | An array of names for the output files the tool produces. These are |
| 3861 | relative to the build output directory. There must always be at least |
| 3862 | one output file. There can be more than one output (a linker might |
| 3863 | produce a library and an import library, for example). |
| 3864 | |
| 3865 | This array just declares to GN what files the tool will produce. It is |
| 3866 | your responsibility to specify the tool command that actually produces |
| 3867 | these files. |
| 3868 | |
| 3869 | If you specify more than one output for shared library links, you |
| 3870 | should consider setting link_output, depend_output, and |
| 3871 | runtime_outputs. |
| 3872 | |
| 3873 | Example for a compiler tool that produces .obj files: |
| 3874 | outputs = [ |
| 3875 | "{{source_out_dir}}/{{source_name_part}}.obj" |
| 3876 | ] |
| 3877 | |
| 3878 | Example for a linker tool that produces a .dll and a .lib. The use of |
| 3879 | {{target_output_name}}, {{output_extension}} and {{output_dir}} allows |
| 3880 | the target to override these values. |
| 3881 | outputs = [ |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 3882 | "{{output_dir}}/{{target_output_name}}{{output_extension}}", |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3883 | "{{output_dir}}/{{target_output_name}}.lib", |
| 3884 | ] |
| 3885 | |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 3886 | partial_outputs [list of strings with substitutions] |
| 3887 | Valid for: "swift" only |
| 3888 | |
| 3889 | An array of names for the partial outputs the tool produces. These |
| 3890 | are relative to the build output directory. The expansion will be |
| 3891 | evaluated for each file listed in the "sources" of the target. |
| 3892 | |
| 3893 | This is used to deal with whole module optimization, allowing to |
| 3894 | list one object file per source file when whole module optimization |
| 3895 | is disabled. |
| 3896 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3897 | pool [label, optional] |
| 3898 | Valid for: all tools (optional) |
| 3899 | |
| 3900 | Label of the pool to use for the tool. Pools are used to limit the |
| 3901 | number of tasks that can execute concurrently during the build. |
| 3902 | |
| 3903 | See also "gn help pool". |
| 3904 | |
| 3905 | link_output [string with substitutions] |
| 3906 | depend_output [string with substitutions] |
David 'Digit' Turner | e3d088c | 2024-08-12 16:36:30 +0200 | [diff] [blame] | 3907 | Valid for: "solink", "rust_dylib" or "rust_cdylib" only (optional) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3908 | |
David 'Digit' Turner | e3d088c | 2024-08-12 16:36:30 +0200 | [diff] [blame] | 3909 | These two files specify which of the outputs from the tool should |
| 3910 | be used for linking and dependency tracking. These should match entries |
| 3911 | in the "outputs". If unspecified, the first item in the "outputs" array |
| 3912 | will be used for all. See "Separate linking and dependencies for shared |
| 3913 | libraries" below for more. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3914 | |
| 3915 | On Windows, where the tools produce a .dll shared library and a .lib |
| 3916 | import library, you will want the first two to be the import library |
| 3917 | and the third one to be the .dll file. On Linux, if you're not doing |
| 3918 | the separate linking/dependency optimization, all of these should be |
| 3919 | the .so output. |
| 3920 | |
| 3921 | output_prefix [string] |
| 3922 | Valid for: Linker tools (optional) |
| 3923 | |
| 3924 | Prefix to use for the output name. Defaults to empty. This prefix will |
| 3925 | be prepended to the name of the target (or the output_name if one is |
| 3926 | manually specified for it) if the prefix is not already there. The |
| 3927 | result will show up in the {{output_name}} substitution pattern. |
| 3928 | |
| 3929 | Individual targets can opt-out of the output prefix by setting: |
| 3930 | output_prefix_override = true |
| 3931 | (see "gn help output_prefix_override"). |
| 3932 | |
| 3933 | This is typically used to prepend "lib" to libraries on |
| 3934 | Posix systems: |
| 3935 | output_prefix = "lib" |
| 3936 | |
| 3937 | precompiled_header_type [string] |
| 3938 | Valid for: "cc", "cxx", "objc", "objcxx" |
| 3939 | |
| 3940 | Type of precompiled headers. If undefined or the empty string, |
| 3941 | precompiled headers will not be used for this tool. Otherwise use "gcc" |
| 3942 | or "msvc". |
| 3943 | |
| 3944 | For precompiled headers to be used for a given target, the target (or a |
| 3945 | config applied to it) must also specify a "precompiled_header" and, for |
| 3946 | "msvc"-style headers, a "precompiled_source" value. If the type is |
| 3947 | "gcc", then both "precompiled_header" and "precompiled_source" must |
| 3948 | resolve to the same file, despite the different formats required for |
| 3949 | each." |
| 3950 | |
| 3951 | See "gn help precompiled_header" for more. |
| 3952 | |
| 3953 | restat [boolean] |
| 3954 | Valid for: all tools (optional, defaults to false) |
| 3955 | |
| 3956 | Requests that Ninja check the file timestamp after this tool has run to |
| 3957 | determine if anything changed. Set this if your tool has the ability to |
| 3958 | skip writing output if the output file has not changed. |
| 3959 | |
| 3960 | Normally, Ninja will assume that when a tool runs the output be new and |
Takuto Ikuta | 60a28b6 | 2024-09-05 17:45:42 +0900 | [diff] [blame] | 3961 | downstream dependents must be rebuild. When this is set to true, Ninja |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3962 | can skip rebuilding downstream dependents for input changes that don't |
| 3963 | actually affect the output. |
| 3964 | |
| 3965 | Example: |
| 3966 | restat = true |
| 3967 | |
| 3968 | rspfile [string with substitutions] |
| 3969 | Valid for: all tools except "action" (optional) |
| 3970 | |
| 3971 | Name of the response file. If empty, no response file will be |
| 3972 | used. See "rspfile_content". |
| 3973 | |
| 3974 | rspfile_content [string with substitutions] |
| 3975 | Valid for: all tools except "action" (required when "rspfile" is used) |
| 3976 | |
| 3977 | The contents to be written to the response file. This may include all |
| 3978 | or part of the command to send to the tool which allows you to get |
| 3979 | around OS command-line length limits. |
| 3980 | |
| 3981 | This example adds the inputs and libraries to a response file, but |
| 3982 | passes the linker flags directly on the command line: |
| 3983 | tool("link") { |
| 3984 | command = "link -o {{output}} {{ldflags}} @{{output}}.rsp" |
| 3985 | rspfile = "{{output}}.rsp" |
Adrian Taylor | ac6cc95 | 2020-04-26 20:49:18 -0700 | [diff] [blame] | 3986 | rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | runtime_outputs [string list with substitutions] |
| 3990 | Valid for: linker tools |
| 3991 | |
| 3992 | If specified, this list is the subset of the outputs that should be |
| 3993 | added to runtime deps (see "gn help runtime_deps"). By default (if |
| 3994 | runtime_outputs is empty or unspecified), it will be the link_output. |
Adrian Taylor | 4aa9bdf | 2021-11-15 19:00:06 -0800 | [diff] [blame] | 3995 | |
| 3996 | rust_sysroot |
| 3997 | Valid for: Rust tools |
| 3998 | |
| 3999 | A path relative to root_out_dir. This is not used in the build |
| 4000 | process, but may be used when generating metadata for rust-analyzer. |
| 4001 | (See --export-rust-project). It enables such metadata to include |
| 4002 | information about the Rust standard library. |
Adrian Taylor | e3978de | 2023-05-26 12:07:53 +0100 | [diff] [blame] | 4003 | |
| 4004 | dynamic_link_switch |
| 4005 | Valid for: Rust tools which link |
| 4006 | |
| 4007 | A switch to be optionally inserted into linker command lines |
| 4008 | to indicate that subsequent items may be dynamically linked. |
| 4009 | For ld-like linkers, -Clink-arg=-Bdynamic may be a good choice. |
| 4010 | This switch is inserted by gn into rustc command lines before |
| 4011 | listing any non-Rust dependencies. This may be necessary because |
| 4012 | sometimes rustc puts the linker into a mode where it would otherwise |
| 4013 | link against static libraries by default. This flag will be |
| 4014 | inserted into the {{rustdeps}} variable at the appropriate place; |
| 4015 | {{ldflags}} can't be used for the same purpose because the flags |
| 4016 | may not be inserted at the desired place in the command line. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4017 | ``` |
| 4018 | |
| 4019 | #### **Expansions for tool variables** |
| 4020 | |
| 4021 | ``` |
| 4022 | All paths are relative to the root build directory, which is the current |
| 4023 | directory for running all tools. These expansions are available to all tools: |
| 4024 | |
| 4025 | {{label}} |
| 4026 | The label of the current target. This is typically used in the |
| 4027 | "description" field for link tools. The toolchain will be omitted from |
| 4028 | the label for targets in the default toolchain, and will be included |
| 4029 | for targets in other toolchains. |
| 4030 | |
| 4031 | {{label_name}} |
| 4032 | The short name of the label of the target. This is the part after the |
| 4033 | colon. For "//foo/bar:baz" this will be "baz". Unlike |
| 4034 | {{target_output_name}}, this is not affected by the "output_prefix" in |
| 4035 | the tool or the "output_name" set on the target. |
| 4036 | |
Scott Graham | 501b49a | 2020-08-05 13:03:37 -0700 | [diff] [blame] | 4037 | {{label_no_toolchain}} |
| 4038 | The label of the current target, never including the toolchain |
| 4039 | (otherwise, this is identical to {{label}}). This is used as the module |
| 4040 | name when using .modulemap files. |
| 4041 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4042 | {{output}} |
| 4043 | The relative path and name of the output(s) of the current build step. |
| 4044 | If there is more than one output, this will expand to a list of all of |
| 4045 | them. Example: "out/base/my_file.o" |
| 4046 | |
| 4047 | {{target_gen_dir}} |
| 4048 | {{target_out_dir}} |
| 4049 | The directory of the generated file and output directories, |
| 4050 | respectively, for the current target. There is no trailing slash. See |
| 4051 | also {{output_dir}} for linker tools. Example: "out/base/test" |
| 4052 | |
| 4053 | {{target_output_name}} |
| 4054 | The short name of the current target with no path information, or the |
| 4055 | value of the "output_name" variable if one is specified in the target. |
| 4056 | This will include the "output_prefix" if any. See also {{label_name}}. |
| 4057 | |
| 4058 | Example: "libfoo" for the target named "foo" and an output prefix for |
| 4059 | the linker tool of "lib". |
| 4060 | |
| 4061 | Compiler tools have the notion of a single input and a single output, along |
| 4062 | with a set of compiler-specific flags. The following expansions are |
| 4063 | available: |
| 4064 | |
| 4065 | {{asmflags}} |
| 4066 | {{cflags}} |
| 4067 | {{cflags_c}} |
| 4068 | {{cflags_cc}} |
| 4069 | {{cflags_objc}} |
| 4070 | {{cflags_objcc}} |
| 4071 | {{defines}} |
| 4072 | {{include_dirs}} |
| 4073 | Strings correspond that to the processed flags/defines/include |
| 4074 | directories specified for the target. |
| 4075 | Example: "--enable-foo --enable-bar" |
| 4076 | |
| 4077 | Defines will be prefixed by "-D" and include directories will be |
| 4078 | prefixed by "-I" (these work with Posix tools as well as Microsoft |
| 4079 | ones). |
| 4080 | |
Scott Graham | 484245b | 2020-08-11 10:58:50 -0700 | [diff] [blame] | 4081 | {{module_deps}} |
| 4082 | {{module_deps_no_self}} |
| 4083 | Strings that correspond to the flags necessary to depend upon the Clang |
| 4084 | modules referenced by the current target. The "_no_self" version doesn't |
| 4085 | include the module for the current target, and can be used to compile |
| 4086 | the pcm itself. |
| 4087 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4088 | {{source}} |
| 4089 | The relative path and name of the current input file. |
| 4090 | Example: "../../base/my_file.cc" |
| 4091 | |
| 4092 | {{source_file_part}} |
| 4093 | The file part of the source including the extension (with no directory |
| 4094 | information). |
| 4095 | Example: "foo.cc" |
| 4096 | |
| 4097 | {{source_name_part}} |
| 4098 | The filename part of the source file with no directory or extension. |
| 4099 | Example: "foo" |
| 4100 | |
| 4101 | {{source_gen_dir}} |
| 4102 | {{source_out_dir}} |
| 4103 | The directory in the generated file and output directories, |
| 4104 | respectively, for the current input file. If the source file is in the |
| 4105 | same directory as the target is declared in, they will will be the same |
| 4106 | as the "target" versions above. Example: "gen/base/test" |
| 4107 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 4108 | Linker tools have multiple inputs and (potentially) multiple outputs. The |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4109 | static library tool ("alink") is not considered a linker tool. The following |
| 4110 | expansions are available: |
| 4111 | |
| 4112 | {{inputs}} |
| 4113 | {{inputs_newline}} |
| 4114 | Expands to the inputs to the link step. This will be a list of object |
| 4115 | files and static libraries. |
| 4116 | Example: "obj/foo.o obj/bar.o obj/somelibrary.a" |
| 4117 | |
| 4118 | The "_newline" version will separate the input files with newlines |
| 4119 | instead of spaces. This is useful in response files: some linkers can |
| 4120 | take a "-filelist" flag which expects newline separated files, and some |
| 4121 | Microsoft tools have a fixed-sized buffer for parsing each line of a |
| 4122 | response file. |
| 4123 | |
| 4124 | {{ldflags}} |
| 4125 | Expands to the processed set of ldflags and library search paths |
| 4126 | specified for the target. |
| 4127 | Example: "-m64 -fPIC -pthread -L/usr/local/mylib" |
| 4128 | |
| 4129 | {{libs}} |
| 4130 | Expands to the list of system libraries to link to. Each will be |
| 4131 | prefixed by the "lib_switch". |
| 4132 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4133 | Example: "-lfoo -lbar" |
| 4134 | |
| 4135 | {{output_dir}} |
| 4136 | The value of the "output_dir" variable in the target, or the the value |
| 4137 | of the "default_output_dir" value in the tool if the target does not |
| 4138 | override the output directory. This will be relative to the |
| 4139 | root_build_dir and will not end in a slash. Will be "." for output to |
| 4140 | the root_build_dir. |
| 4141 | |
| 4142 | This is subtly different than {{target_out_dir}} which is defined by GN |
| 4143 | based on the target's path and not overridable. {{output_dir}} is for |
| 4144 | the final output, {{target_out_dir}} is generally for object files and |
| 4145 | other outputs. |
| 4146 | |
| 4147 | Usually {{output_dir}} would be defined in terms of either |
| 4148 | {{target_out_dir}} or {{root_out_dir}} |
| 4149 | |
| 4150 | {{output_extension}} |
| 4151 | The value of the "output_extension" variable in the target, or the |
| 4152 | value of the "default_output_extension" value in the tool if the target |
| 4153 | does not specify an output extension. |
| 4154 | Example: ".so" |
| 4155 | |
| 4156 | {{solibs}} |
| 4157 | Extra libraries from shared library dependencies not specified in the |
| 4158 | {{inputs}}. This is the list of link_output files from shared libraries |
David 'Digit' Turner | e3d088c | 2024-08-12 16:36:30 +0200 | [diff] [blame] | 4159 | (if the solink, rust_dylib and rust_cdylib tools specify a "link_output" |
| 4160 | variable separate from the "depend_output"). |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4161 | |
| 4162 | These should generally be treated the same as libs by your tool. |
| 4163 | |
| 4164 | Example: "libfoo.so libbar.so" |
| 4165 | |
Adrian Taylor | ac6cc95 | 2020-04-26 20:49:18 -0700 | [diff] [blame] | 4166 | {{rlibs}} |
| 4167 | Any Rust .rlibs which need to be linked into a final C++ target. |
| 4168 | These should be treated as {{inputs}} except that sometimes |
| 4169 | they might have different linker directives applied. |
| 4170 | |
| 4171 | Example: "obj/foo/libfoo.rlib" |
| 4172 | |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 4173 | {{frameworks}} |
| 4174 | Shared libraries packaged as framework bundle. This is principally |
| 4175 | used on Apple's platforms (macOS and iOS). All name must be ending |
| 4176 | with ".framework" suffix; the suffix will be stripped when expanding |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 4177 | {{frameworks}} and each item will be preceded by "-framework" or |
| 4178 | "-weak_framework". |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 4179 | |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 4180 | {{swiftmodules}} |
| 4181 | Swift .swiftmodule files that needs to be embedded into the binary. |
| 4182 | This is necessary to correctly link with object generated by the |
| 4183 | Swift compiler (the .swiftmodule file cannot be embedded in object |
| 4184 | files directly). Those will be prefixed with "swiftmodule_switch" |
| 4185 | value. |
| 4186 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4187 | The static library ("alink") tool allows {{arflags}} plus the common tool |
| 4188 | substitutions. |
| 4189 | |
| 4190 | The copy tool allows the common compiler/linker substitutions, plus |
| 4191 | {{source}} which is the source of the copy. The stamp tool allows only the |
| 4192 | common tool substitutions. |
| 4193 | |
| 4194 | The copy_bundle_data and compile_xcassets tools only allows the common tool |
| 4195 | substitutions. Both tools are required to create iOS/macOS bundles and need |
| 4196 | only be defined on those platforms. |
| 4197 | |
| 4198 | The copy_bundle_data tool will be called with one source and needs to copy |
| 4199 | (optionally optimizing the data representation) to its output. It may be |
| 4200 | called with a directory as input and it needs to be recursively copied. |
| 4201 | |
| 4202 | The compile_xcassets tool will be called with one or more source (each an |
| 4203 | asset catalog) that needs to be compiled to a single output. The following |
| 4204 | substitutions are available: |
| 4205 | |
| 4206 | {{inputs}} |
| 4207 | Expands to the list of .xcassets to use as input to compile the asset |
| 4208 | catalog. |
| 4209 | |
| 4210 | {{bundle_product_type}} |
| 4211 | Expands to the product_type of the bundle that will contain the |
| 4212 | compiled asset catalog. Usually corresponds to the product_type |
| 4213 | property of the corresponding create_bundle target. |
| 4214 | |
| 4215 | {{bundle_partial_info_plist}} |
| 4216 | Expands to the path to the partial Info.plist generated by the |
| 4217 | assets catalog compiler. Usually based on the target_name of |
| 4218 | the create_bundle target. |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 4219 | |
Harley Li | 0a9affb | 2020-06-03 10:38:42 -0400 | [diff] [blame] | 4220 | {{xcasset_compiler_flags}} |
| 4221 | Expands to the list of flags specified in corresponding |
| 4222 | create_bundle target. |
| 4223 | |
Sylvain Defresne | 06cdcc8 | 2024-03-21 13:48:59 +0100 | [diff] [blame] | 4224 | The inputs for compile_xcassets tool will be found from the bundle_data |
| 4225 | dependencies by looking for any file matching "*/*.xcassets/*" pattern. |
| 4226 | The "$assets.xcassets" directory will be added as input to the tool. |
| 4227 | |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 4228 | The Swift tool has multiple input and outputs. It must have exactly one |
| 4229 | output of .swiftmodule type, but can have one or more object file outputs, |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 4230 | in addition to other type of outputs. The following expansions are available: |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 4231 | |
| 4232 | {{module_name}} |
| 4233 | Expands to the string representing the module name of target under |
| 4234 | compilation (see "module_name" variable). |
| 4235 | |
| 4236 | {{module_dirs}} |
| 4237 | Expands to the list of -I<path> for the target Swift module search |
| 4238 | path computed from target dependencies. |
| 4239 | |
| 4240 | {{swiftflags}} |
| 4241 | Expands to the list of strings representing Swift compiler flags. |
| 4242 | |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 4243 | Rust tools have the notion of a single input and a single output, along |
| 4244 | with a set of compiler-specific flags. The following expansions are |
| 4245 | available: |
| 4246 | |
| 4247 | {{crate_name}} |
| 4248 | Expands to the string representing the crate name of target under |
| 4249 | compilation. |
| 4250 | |
| 4251 | {{crate_type}} |
| 4252 | Expands to the string representing the type of crate for the target |
| 4253 | under compilation. |
| 4254 | |
| 4255 | {{externs}} |
| 4256 | Expands to the list of --extern flags needed to include addition Rust |
| 4257 | libraries in this target. Includes any specified renamed dependencies. |
| 4258 | |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 4259 | {{rustdeps}} |
Takuto Ikuta | dae6a44 | 2025-03-05 17:31:58 +0900 | [diff] [blame^] | 4260 | Expands to the list of -Ldependency=<path> and -Clink-arg=<path> strings |
| 4261 | needed to compile this target. |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 4262 | |
| 4263 | {{rustenv}} |
| 4264 | Expands to the list of environment variables. |
| 4265 | |
| 4266 | {{rustflags}} |
| 4267 | Expands to the list of strings representing Rust compiler flags. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4268 | ``` |
| 4269 | |
| 4270 | #### **Separate linking and dependencies for shared libraries** |
| 4271 | |
| 4272 | ``` |
| 4273 | Shared libraries are special in that not all changes to them require that |
| 4274 | dependent targets be re-linked. If the shared library is changed but no |
| 4275 | imports or exports are different, dependent code needn't be relinked, which |
| 4276 | can speed up the build. |
| 4277 | |
| 4278 | If your link step can output a list of exports from a shared library and |
| 4279 | writes the file only if the new one is different, the timestamp of this file |
| 4280 | can be used for triggering re-links, while the actual shared library would be |
| 4281 | used for linking. |
| 4282 | |
| 4283 | You will need to specify |
| 4284 | restat = true |
| 4285 | in the linker tool to make this work, so Ninja will detect if the timestamp |
| 4286 | of the dependency file has changed after linking (otherwise it will always |
| 4287 | assume that running a command updates the output): |
| 4288 | |
| 4289 | tool("solink") { |
| 4290 | command = "..." |
| 4291 | outputs = [ |
| 4292 | "{{output_dir}}/{{target_output_name}}{{output_extension}}", |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 4293 | "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC", |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4294 | ] |
| 4295 | link_output = |
| 4296 | "{{output_dir}}/{{target_output_name}}{{output_extension}}" |
| 4297 | depend_output = |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 4298 | "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4299 | restat = true |
| 4300 | } |
| 4301 | ``` |
| 4302 | |
| 4303 | #### **Example** |
| 4304 | |
| 4305 | ``` |
| 4306 | toolchain("my_toolchain") { |
| 4307 | # Put these at the top to apply to all tools below. |
| 4308 | lib_switch = "-l" |
| 4309 | lib_dir_switch = "-L" |
| 4310 | |
| 4311 | tool("cc") { |
| 4312 | command = "gcc {{source}} -o {{output}}" |
| 4313 | outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ] |
| 4314 | description = "GCC {{source}}" |
| 4315 | } |
| 4316 | tool("cxx") { |
| 4317 | command = "g++ {{source}} -o {{output}}" |
| 4318 | outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ] |
| 4319 | description = "G++ {{source}}" |
| 4320 | } |
| 4321 | }; |
| 4322 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4323 | ### <a name="func_toolchain"></a>**toolchain**: Defines a toolchain. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4324 | |
| 4325 | ``` |
| 4326 | A toolchain is a set of commands and build flags used to compile the source |
| 4327 | code. The toolchain() function defines these commands. |
| 4328 | ``` |
| 4329 | |
| 4330 | #### **Toolchain overview** |
| 4331 | |
| 4332 | ``` |
| 4333 | You can have more than one toolchain in use at once in a build and a target |
| 4334 | can exist simultaneously in multiple toolchains. A build file is executed |
| 4335 | once for each toolchain it is referenced in so the GN code can vary all |
| 4336 | parameters of each target (or which targets exist) on a per-toolchain basis. |
| 4337 | |
| 4338 | When you have a simple build with only one toolchain, the build config file |
| 4339 | is loaded only once at the beginning of the build. It must call |
| 4340 | set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the |
| 4341 | label of the toolchain definition to use. The "toolchain_args" section of the |
| 4342 | toolchain definition is ignored. |
| 4343 | |
| 4344 | When a target has a dependency on a target using different toolchain (see "gn |
| 4345 | help labels" for how to specify this), GN will start a build using that |
| 4346 | secondary toolchain to resolve the target. GN will load the build config file |
| 4347 | with the build arguments overridden as specified in the toolchain_args. |
| 4348 | Because the default toolchain is already known, calls to |
| 4349 | set_default_toolchain() are ignored. |
| 4350 | |
| 4351 | To load a file in an alternate toolchain, GN does the following: |
| 4352 | |
| 4353 | 1. Loads the file with the toolchain definition in it (as determined by the |
| 4354 | toolchain label). |
| 4355 | 2. Re-runs the master build configuration file, applying the arguments |
| 4356 | specified by the toolchain_args section of the toolchain definition. |
| 4357 | 3. Loads the destination build file in the context of the configuration file |
| 4358 | in the previous step. |
| 4359 | |
| 4360 | The toolchain configuration is two-way. In the default toolchain (i.e. the |
| 4361 | main build target) the configuration flows from the build config file to the |
| 4362 | toolchain. The build config file looks at the state of the build (OS type, |
| 4363 | CPU architecture, etc.) and decides which toolchain to use (via |
| 4364 | set_default_toolchain()). In secondary toolchains, the configuration flows |
| 4365 | from the toolchain to the build config file: the "toolchain_args" in the |
| 4366 | toolchain definition specifies the arguments to re-invoke the build. |
| 4367 | ``` |
| 4368 | |
| 4369 | #### **Functions and variables** |
| 4370 | |
| 4371 | ``` |
| 4372 | tool() |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 4373 | The tool() function call specifies the commands to run for a given step. See |
| 4374 | "gn help tool". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4375 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 4376 | toolchain_args [scope] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4377 | Overrides for build arguments to pass to the toolchain when invoking it. |
| 4378 | This is a variable of type "scope" where the variable names correspond to |
| 4379 | variables in declare_args() blocks. |
| 4380 | |
| 4381 | When you specify a target using an alternate toolchain, the master build |
| 4382 | configuration file is re-interpreted in the context of that toolchain. |
| 4383 | toolchain_args allows you to control the arguments passed into this |
| 4384 | alternate invocation of the build. |
| 4385 | |
| 4386 | Any default system arguments or arguments passed in via "gn args" will also |
| 4387 | be passed to the alternate invocation unless explicitly overridden by |
| 4388 | toolchain_args. |
| 4389 | |
| 4390 | The toolchain_args will be ignored when the toolchain being defined is the |
| 4391 | default. In this case, it's expected you want the default argument values. |
| 4392 | |
| 4393 | See also "gn help buildargs" for an overview of these arguments. |
| 4394 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 4395 | propagates_configs [boolean, default=false] |
| 4396 | Determines whether public_configs and all_dependent_configs in this |
| 4397 | toolchain propagate to targets in other toolchains. |
| 4398 | |
| 4399 | When false (the default), this toolchain will not propagate any configs to |
| 4400 | targets in other toolchains that depend on it targets inside this |
| 4401 | toolchain. This matches the most common usage of toolchains where they |
| 4402 | represent different architectures or compilers and the settings that apply |
| 4403 | to one won't necessarily apply to others. |
| 4404 | |
| 4405 | When true, configs (public and all-dependent) will cross the boundary out |
| 4406 | of this toolchain as if the toolchain boundary wasn't there. This only |
| 4407 | affects one direction of dependencies: a toolchain can't control whether |
| 4408 | it accepts such configs, only whether it pushes them. The build is |
| 4409 | responsible for ensuring that any external targets depending on targets in |
| 4410 | this toolchain are compatible with the compiler flags, etc. that may be |
| 4411 | propagated. |
| 4412 | |
| 4413 | deps [string list] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4414 | Dependencies of this toolchain. These dependencies will be resolved before |
| 4415 | any target in the toolchain is compiled. To avoid circular dependencies |
| 4416 | these must be targets defined in another toolchain. |
| 4417 | |
| 4418 | This is expressed as a list of targets, and generally these targets will |
| 4419 | always specify a toolchain: |
| 4420 | deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ] |
| 4421 | |
| 4422 | This concept is somewhat inefficient to express in Ninja (it requires a lot |
| 4423 | of duplicate of rules) so should only be used when absolutely necessary. |
| 4424 | ``` |
| 4425 | |
| 4426 | #### **Example of defining a toolchain** |
| 4427 | |
| 4428 | ``` |
| 4429 | toolchain("32") { |
| 4430 | tool("cc") { |
| 4431 | command = "gcc {{source}}" |
| 4432 | ... |
| 4433 | } |
| 4434 | |
| 4435 | toolchain_args = { |
| 4436 | use_doom_melon = true # Doom melon always required for 32-bit builds. |
| 4437 | current_cpu = "x86" |
| 4438 | } |
| 4439 | } |
| 4440 | |
| 4441 | toolchain("64") { |
| 4442 | tool("cc") { |
| 4443 | command = "gcc {{source}}" |
| 4444 | ... |
| 4445 | } |
| 4446 | |
| 4447 | toolchain_args = { |
| 4448 | # use_doom_melon is not overridden here, it will take the default. |
| 4449 | current_cpu = "x64" |
| 4450 | } |
| 4451 | } |
| 4452 | ``` |
| 4453 | |
| 4454 | #### **Example of cross-toolchain dependencies** |
| 4455 | |
| 4456 | ``` |
| 4457 | If a 64-bit target wants to depend on a 32-bit binary, it would specify a |
| 4458 | dependency using data_deps (data deps are like deps that are only needed at |
| 4459 | runtime and aren't linked, since you can't link a 32-bit and a 64-bit |
| 4460 | library). |
| 4461 | |
| 4462 | executable("my_program") { |
| 4463 | ... |
| 4464 | if (target_cpu == "x64") { |
| 4465 | # The 64-bit build needs this 32-bit helper. |
| 4466 | data_deps = [ ":helper(//toolchains:32)" ] |
| 4467 | } |
| 4468 | } |
| 4469 | |
| 4470 | if (target_cpu == "x86") { |
| 4471 | # Our helper library is only compiled in 32-bits. |
| 4472 | shared_library("helper") { |
| 4473 | ... |
| 4474 | } |
| 4475 | } |
| 4476 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4477 | ### <a name="func_write_file"></a>**write_file**: Write a file to disk. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4478 | |
| 4479 | ``` |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 4480 | write_file(filename, data, output_conversion = "") |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4481 | |
| 4482 | If data is a list, the list will be written one-item-per-line with no quoting |
| 4483 | or brackets. |
| 4484 | |
| 4485 | If the file exists and the contents are identical to that being written, the |
| 4486 | file will not be updated. This will prevent unnecessary rebuilds of targets |
| 4487 | that depend on this file. |
| 4488 | |
| 4489 | One use for write_file is to write a list of inputs to an script that might |
| 4490 | be too long for the command line. However, it is preferable to use response |
| 4491 | files for this purpose. See "gn help response_file_contents". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4492 | ``` |
| 4493 | |
| 4494 | #### **Arguments** |
| 4495 | |
| 4496 | ``` |
| 4497 | filename |
| 4498 | Filename to write. This must be within the output directory. |
| 4499 | |
| 4500 | data |
| 4501 | The list or string to write. |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 4502 | |
| 4503 | output_conversion |
Sylvain Defresne | 72d5a6e | 2021-06-15 17:01:32 +0200 | [diff] [blame] | 4504 | Controls how the output is written. See `gn help io_conversion`. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4505 | ``` |
| 4506 | ## <a name="predefined_variables"></a>Built-in predefined variables |
| 4507 | |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4508 | ### <a name="var_current_cpu"></a>**current_cpu**: The processor architecture of the current toolchain. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4509 | |
| 4510 | ``` |
| 4511 | The build configuration usually sets this value based on the value of |
| 4512 | "host_cpu" (see "gn help host_cpu") and then threads this through the |
| 4513 | toolchain definitions to ensure that it always reflects the appropriate |
| 4514 | value. |
| 4515 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 4516 | This value is not used internally by GN for any purpose. It is set to the |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4517 | empty string ("") by default but is declared so that it can be overridden on |
| 4518 | the command line if so desired. |
| 4519 | |
| 4520 | See "gn help target_cpu" for a list of common values returned. |
| 4521 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4522 | ### <a name="var_current_os"></a>**current_os**: The operating system of the current toolchain. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4523 | |
| 4524 | ``` |
| 4525 | The build configuration usually sets this value based on the value of |
| 4526 | "target_os" (see "gn help target_os"), and then threads this through the |
| 4527 | toolchain definitions to ensure that it always reflects the appropriate |
| 4528 | value. |
| 4529 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 4530 | This value is not used internally by GN for any purpose. It is set to the |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4531 | empty string ("") by default but is declared so that it can be overridden on |
| 4532 | the command line if so desired. |
| 4533 | |
| 4534 | See "gn help target_os" for a list of common values returned. |
| 4535 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4536 | ### <a name="var_current_toolchain"></a>**current_toolchain**: Label of the current toolchain. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4537 | |
| 4538 | ``` |
| 4539 | A fully-qualified label representing the current toolchain. You can use this |
| 4540 | to make toolchain-related decisions in the build. See also |
| 4541 | "default_toolchain". |
| 4542 | ``` |
| 4543 | |
| 4544 | #### **Example** |
| 4545 | |
| 4546 | ``` |
| 4547 | if (current_toolchain == "//build:64_bit_toolchain") { |
| 4548 | executable("output_thats_64_bit_only") { |
| 4549 | ... |
| 4550 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4551 | ### <a name="var_default_toolchain"></a>**default_toolchain**: [string] Label of the default toolchain. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4552 | |
| 4553 | ``` |
| 4554 | A fully-qualified label representing the default toolchain, which may not |
| 4555 | necessarily be the current one (see "current_toolchain"). |
| 4556 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4557 | ### <a name="var_gn_version"></a>**gn_version**: [number] The version of gn. [Back to Top](#gn-reference) |
James Robinson | 61377e3 | 2020-02-13 15:20:07 -0800 | [diff] [blame] | 4558 | |
| 4559 | ``` |
| 4560 | Corresponds to the number printed by `gn --version`. |
| 4561 | ``` |
| 4562 | |
| 4563 | #### **Example** |
| 4564 | |
| 4565 | ``` |
| 4566 | assert(gn_version >= 1700, "need GN version 1700 for the frobulate feature") |
| 4567 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4568 | ### <a name="var_host_cpu"></a>**host_cpu**: The processor architecture that GN is running on. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4569 | |
| 4570 | ``` |
| 4571 | This is value is exposed so that cross-compile toolchains can access the host |
| 4572 | architecture when needed. |
| 4573 | |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 4574 | The value should generally be considered read-only, but it can be overridden |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4575 | in order to handle unusual cases where there might be multiple plausible |
| 4576 | values for the host architecture (e.g., if you can do either 32-bit or 64-bit |
| 4577 | builds). The value is not used internally by GN for any purpose. |
| 4578 | ``` |
| 4579 | |
| 4580 | #### **Some possible values** |
| 4581 | |
| 4582 | ``` |
| 4583 | - "x64" |
| 4584 | - "x86" |
| 4585 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4586 | ### <a name="var_host_os"></a>**host_os**: [string] The operating system that GN is running on. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4587 | |
| 4588 | ``` |
| 4589 | This value is exposed so that cross-compiles can access the host build |
| 4590 | system's settings. |
| 4591 | |
| 4592 | This value should generally be treated as read-only. It, however, is not used |
| 4593 | internally by GN for any purpose. |
| 4594 | ``` |
| 4595 | |
| 4596 | #### **Some possible values** |
| 4597 | |
| 4598 | ``` |
| 4599 | - "linux" |
| 4600 | - "mac" |
| 4601 | - "win" |
| 4602 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4603 | ### <a name="var_invoker"></a>**invoker**: [string] The invoking scope inside a template. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4604 | |
| 4605 | ``` |
| 4606 | Inside a template invocation, this variable refers to the scope of the |
| 4607 | invoker of the template. Outside of template invocations, this variable is |
| 4608 | undefined. |
| 4609 | |
| 4610 | All of the variables defined inside the template invocation are accessible as |
| 4611 | members of the "invoker" scope. This is the way that templates read values |
| 4612 | set by the callers. |
| 4613 | |
| 4614 | This is often used with "defined" to see if a value is set on the invoking |
| 4615 | scope. |
| 4616 | |
| 4617 | See "gn help template" for more examples. |
| 4618 | ``` |
| 4619 | |
| 4620 | #### **Example** |
| 4621 | |
| 4622 | ``` |
| 4623 | template("my_template") { |
| 4624 | print(invoker.sources) # Prints [ "a.cc", "b.cc" ] |
| 4625 | print(defined(invoker.foo)) # Prints false. |
| 4626 | print(defined(invoker.bar)) # Prints true. |
| 4627 | } |
| 4628 | |
| 4629 | my_template("doom_melon") { |
| 4630 | sources = [ "a.cc", "b.cc" ] |
| 4631 | bar = 123 |
| 4632 | } |
| 4633 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4634 | ### <a name="var_python_path"></a>**python_path**: Absolute path of Python. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4635 | |
| 4636 | ``` |
| 4637 | Normally used in toolchain definitions if running some command requires |
| 4638 | Python. You will normally not need this when invoking scripts since GN |
| 4639 | automatically finds it for you. |
| 4640 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4641 | ### <a name="var_root_build_dir"></a>**root_build_dir**: [string] Directory where build commands are run. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4642 | |
| 4643 | ``` |
| 4644 | This is the root build output directory which will be the current directory |
| 4645 | when executing all compilers and scripts. |
| 4646 | |
| 4647 | Most often this is used with rebase_path (see "gn help rebase_path") to |
| 4648 | convert arguments to be relative to a script's current directory. |
| 4649 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4650 | ### <a name="var_root_gen_dir"></a>**root_gen_dir**: Directory for the toolchain's generated files. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4651 | |
| 4652 | ``` |
| 4653 | Absolute path to the root of the generated output directory tree for the |
| 4654 | current toolchain. An example would be "//out/Debug/gen" for the default |
| 4655 | toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain. |
| 4656 | |
| 4657 | This is primarily useful for setting up include paths for generated files. If |
| 4658 | you are passing this to a script, you will want to pass it through |
| 4659 | rebase_path() (see "gn help rebase_path") to convert it to be relative to the |
| 4660 | build directory. |
| 4661 | |
| 4662 | See also "target_gen_dir" which is usually a better location for generated |
| 4663 | files. It will be inside the root generated dir. |
| 4664 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4665 | ### <a name="var_root_out_dir"></a>**root_out_dir**: [string] Root directory for toolchain output files. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4666 | |
| 4667 | ``` |
| 4668 | Absolute path to the root of the output directory tree for the current |
| 4669 | toolchain. It will not have a trailing slash. |
| 4670 | |
| 4671 | For the default toolchain this will be the same as the root_build_dir. An |
| 4672 | example would be "//out/Debug" for the default toolchain, or |
| 4673 | "//out/Debug/arm" for the "arm" toolchain. |
| 4674 | |
| 4675 | This is primarily useful for setting up script calls. If you are passing this |
| 4676 | to a script, you will want to pass it through rebase_path() (see "gn help |
| 4677 | rebase_path") to convert it to be relative to the build directory. |
| 4678 | |
| 4679 | See also "target_out_dir" which is usually a better location for output |
| 4680 | files. It will be inside the root output dir. |
| 4681 | ``` |
| 4682 | |
| 4683 | #### **Example** |
| 4684 | |
| 4685 | ``` |
| 4686 | action("myscript") { |
| 4687 | # Pass the output dir to the script. |
| 4688 | args = [ "-o", rebase_path(root_out_dir, root_build_dir) ] |
| 4689 | } |
| 4690 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4691 | ### <a name="var_target_cpu"></a>**target_cpu**: The desired cpu architecture for the build. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4692 | |
| 4693 | ``` |
| 4694 | This value should be used to indicate the desired architecture for the |
| 4695 | primary objects of the build. It will match the cpu architecture of the |
| 4696 | default toolchain, but not necessarily the current toolchain. |
| 4697 | |
| 4698 | In many cases, this is the same as "host_cpu", but in the case of |
| 4699 | cross-compiles, this can be set to something different. This value is |
| 4700 | different from "current_cpu" in that it does not change based on the current |
| 4701 | toolchain. When writing rules, "current_cpu" should be used rather than |
| 4702 | "target_cpu" most of the time. |
| 4703 | |
| 4704 | This value is not used internally by GN for any purpose, so it may be set to |
| 4705 | whatever value is needed for the build. GN defaults this value to the empty |
| 4706 | string ("") and the configuration files should set it to an appropriate value |
| 4707 | (e.g., setting it to the value of "host_cpu") if it is not overridden on the |
| 4708 | command line or in the args.gn file. |
| 4709 | ``` |
| 4710 | |
| 4711 | #### **Possible values** |
| 4712 | |
| 4713 | ``` |
| 4714 | - "x86" |
| 4715 | - "x64" |
| 4716 | - "arm" |
| 4717 | - "arm64" |
| 4718 | - "mipsel" |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 4719 | - "mips64el" |
| 4720 | - "s390x" |
| 4721 | - "ppc64" |
| 4722 | - "riscv32" |
| 4723 | - "riscv64" |
| 4724 | - "e2k" |
| 4725 | - "loong64" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4726 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4727 | ### <a name="var_target_gen_dir"></a>**target_gen_dir**: Directory for a target's generated files. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4728 | |
| 4729 | ``` |
| 4730 | Absolute path to the target's generated file directory. This will be the |
| 4731 | "root_gen_dir" followed by the relative path to the current build file. If |
| 4732 | your file is in "//tools/doom_melon" then target_gen_dir would be |
| 4733 | "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash. |
| 4734 | |
| 4735 | This is primarily useful for setting up include paths for generated files. If |
| 4736 | you are passing this to a script, you will want to pass it through |
| 4737 | rebase_path() (see "gn help rebase_path") to convert it to be relative to the |
| 4738 | build directory. |
| 4739 | |
| 4740 | See also "gn help root_gen_dir". |
| 4741 | ``` |
| 4742 | |
| 4743 | #### **Example** |
| 4744 | |
| 4745 | ``` |
| 4746 | action("myscript") { |
| 4747 | # Pass the generated output dir to the script. |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 4748 | args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4749 | } |
| 4750 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4751 | ### <a name="var_target_name"></a>**target_name**: [string] The name of the current target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4752 | |
| 4753 | ``` |
| 4754 | Inside a target or template invocation, this variable refers to the name |
| 4755 | given to the target or template invocation. Outside of these, this variable |
| 4756 | is undefined. |
| 4757 | |
| 4758 | This is most often used in template definitions to name targets defined in |
| 4759 | the template based on the name of the invocation. This is necessary both to |
| 4760 | ensure generated targets have unique names and to generate a target with the |
| 4761 | exact name of the invocation that other targets can depend on. |
| 4762 | |
| 4763 | Be aware that this value will always reflect the innermost scope. So when |
| 4764 | defining a target inside a template, target_name will refer to the target |
| 4765 | rather than the template invocation. To get the name of the template |
| 4766 | invocation in this case, you should save target_name to a temporary variable |
| 4767 | outside of any target definitions. |
| 4768 | |
| 4769 | See "gn help template" for more examples. |
| 4770 | ``` |
| 4771 | |
| 4772 | #### **Example** |
| 4773 | |
| 4774 | ``` |
| 4775 | executable("doom_melon") { |
| 4776 | print(target_name) # Prints "doom_melon". |
| 4777 | } |
| 4778 | |
| 4779 | template("my_template") { |
| 4780 | print(target_name) # Prints "space_ray" when invoked below. |
| 4781 | |
| 4782 | executable(target_name + "_impl") { |
| 4783 | print(target_name) # Prints "space_ray_impl". |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | my_template("space_ray") { |
| 4788 | } |
| 4789 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4790 | ### <a name="var_target_os"></a>**target_os**: The desired operating system for the build. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4791 | |
| 4792 | ``` |
| 4793 | This value should be used to indicate the desired operating system for the |
| 4794 | primary object(s) of the build. It will match the OS of the default |
| 4795 | toolchain. |
| 4796 | |
| 4797 | In many cases, this is the same as "host_os", but in the case of |
| 4798 | cross-compiles, it may be different. This variable differs from "current_os" |
| 4799 | in that it can be referenced from inside any toolchain and will always return |
| 4800 | the initial value. |
| 4801 | |
| 4802 | This should be set to the most specific value possible. So, "android" or |
| 4803 | "chromeos" should be used instead of "linux" where applicable, even though |
| 4804 | Android and ChromeOS are both Linux variants. This can mean that one needs to |
| 4805 | write |
| 4806 | |
| 4807 | if (target_os == "android" || target_os == "linux") { |
| 4808 | # ... |
| 4809 | } |
| 4810 | |
| 4811 | and so forth. |
| 4812 | |
| 4813 | This value is not used internally by GN for any purpose, so it may be set to |
| 4814 | whatever value is needed for the build. GN defaults this value to the empty |
| 4815 | string ("") and the configuration files should set it to an appropriate value |
| 4816 | (e.g., setting it to the value of "host_os") if it is not set via the command |
| 4817 | line or in the args.gn file. |
| 4818 | ``` |
| 4819 | |
| 4820 | #### **Possible values** |
| 4821 | |
| 4822 | ``` |
| 4823 | - "android" |
| 4824 | - "chromeos" |
| 4825 | - "ios" |
| 4826 | - "linux" |
| 4827 | - "nacl" |
| 4828 | - "mac" |
| 4829 | - "win" |
| 4830 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4831 | ### <a name="var_target_out_dir"></a>**target_out_dir**: [string] Directory for target output files. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4832 | |
| 4833 | ``` |
| 4834 | Absolute path to the target's generated file directory. If your current |
| 4835 | target is in "//tools/doom_melon" then this value might be |
| 4836 | "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash. |
| 4837 | |
| 4838 | This is primarily useful for setting up arguments for calling scripts. If you |
| 4839 | are passing this to a script, you will want to pass it through rebase_path() |
| 4840 | (see "gn help rebase_path") to convert it to be relative to the build |
| 4841 | directory. |
| 4842 | |
| 4843 | See also "gn help root_out_dir". |
| 4844 | ``` |
| 4845 | |
| 4846 | #### **Example** |
| 4847 | |
| 4848 | ``` |
| 4849 | action("myscript") { |
| 4850 | # Pass the output dir to the script. |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 4851 | args = [ "-o", rebase_path(target_out_dir, root_build_dir) ] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4852 | } |
| 4853 | ``` |
| 4854 | ## <a name="target_variables"></a>Variables you set in targets |
| 4855 | |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4856 | ### <a name="var_aliased_deps"></a>**aliased_deps**: [scope] Set of crate-dependency pairs. [Back to Top](#gn-reference) |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 4857 | |
| 4858 | ``` |
| 4859 | Valid for `rust_library` targets and `executable`, `static_library`, and |
| 4860 | `shared_library` targets that contain Rust sources. |
| 4861 | |
| 4862 | A scope, each key indicating the renamed crate and the corresponding value |
| 4863 | specifying the label of the dependency producing the relevant binary. |
| 4864 | |
| 4865 | All dependencies listed in this field *must* be listed as deps of the target. |
| 4866 | |
| 4867 | executable("foo") { |
| 4868 | sources = [ "main.rs" ] |
| 4869 | deps = [ "//bar" ] |
| 4870 | } |
| 4871 | |
| 4872 | This target would compile the `foo` crate with the following `extern` flag: |
| 4873 | `rustc ...command... --extern bar=<build_out_dir>/obj/bar` |
| 4874 | |
| 4875 | executable("foo") { |
| 4876 | sources = [ "main.rs" ] |
| 4877 | deps = [ ":bar" ] |
| 4878 | aliased_deps = { |
| 4879 | bar_renamed = ":bar" |
| 4880 | } |
| 4881 | } |
| 4882 | |
| 4883 | With the addition of `aliased_deps`, above target would instead compile with: |
| 4884 | `rustc ...command... --extern bar_renamed=<build_out_dir>/obj/bar` |
| 4885 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4886 | ### <a name="var_all_dependent_configs"></a>**all_dependent_configs**: Configs to be forced on dependents. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4887 | |
| 4888 | ``` |
| 4889 | A list of config labels. |
| 4890 | |
| 4891 | All targets depending on this one, and recursively, all targets depending on |
| 4892 | those, will have the configs listed in this variable added to them. These |
| 4893 | configs will also apply to the current target. |
| 4894 | |
| 4895 | This addition happens in a second phase once a target and all of its |
| 4896 | dependencies have been resolved. Therefore, a target will not see these |
| 4897 | force-added configs in their "configs" variable while the script is running, |
Brett Wilson | 20806f7 | 2024-09-09 10:04:39 -0700 | [diff] [blame] | 4898 | and they can not be removed. |
| 4899 | |
| 4900 | Use of all_dependent_configs should be avoided when possible. |
| 4901 | |
| 4902 | If your target has include_dirs and defines needed by targets that depend on |
| 4903 | it, the correct solution is to add those settings to public_configs and those |
| 4904 | targets choose whether to forward them up the dependency tree by whether they |
| 4905 | depend on your target via public_deps or not. |
| 4906 | |
| 4907 | There are two recommended uses of this feature: |
| 4908 | |
| 4909 | 1. Legacy cases that can't easily be updated to use the proper public deps |
| 4910 | and configs. |
| 4911 | 2. Additional linker flag that need to be set on the final linked target |
| 4912 | regardless of whether the dependency path is public or private. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4913 | |
| 4914 | See also "public_configs". |
| 4915 | ``` |
| 4916 | |
| 4917 | #### **Ordering of flags and values** |
| 4918 | |
| 4919 | ``` |
| 4920 | 1. Those set on the current target (not in a config). |
| 4921 | 2. Those set on the "configs" on the target in order that the |
| 4922 | configs appear in the list. |
| 4923 | 3. Those set on the "all_dependent_configs" on the target in order |
| 4924 | that the configs appear in the list. |
| 4925 | 4. Those set on the "public_configs" on the target in order that |
| 4926 | those configs appear in the list. |
| 4927 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 4928 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 4929 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4930 | 6. public_configs pulled from dependencies, in the order of the |
| 4931 | "deps" list. If a dependency is public, they will be applied |
| 4932 | recursively. |
| 4933 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 4934 | ### <a name="var_allow_circular_includes_from"></a>**allow_circular_includes_from**: Permit includes from deps. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 4935 | |
| 4936 | ``` |
| 4937 | A list of target labels. Must be a subset of the target's "deps". These |
| 4938 | targets will be permitted to include headers from the current target despite |
| 4939 | the dependency going in the opposite direction. |
| 4940 | |
| 4941 | When you use this, both targets must be included in a final binary for it to |
| 4942 | link. To keep linker errors from happening, it is good practice to have all |
| 4943 | external dependencies depend only on one of the two targets, and to set the |
| 4944 | visibility on the other to enforce this. Thus the targets will always be |
| 4945 | linked together in any output. |
| 4946 | ``` |
| 4947 | |
| 4948 | #### **Details** |
| 4949 | |
| 4950 | ``` |
| 4951 | Normally, for a file in target A to include a file from target B, A must list |
| 4952 | B as a dependency. This invariant is enforced by the "gn check" command (and |
| 4953 | the --check flag to "gn gen" -- see "gn help check"). |
| 4954 | |
| 4955 | Sometimes, two targets might be the same unit for linking purposes (two |
| 4956 | source sets or static libraries that would always be linked together in a |
| 4957 | final executable or shared library) and they each include headers from the |
| 4958 | other: you want A to be able to include B's headers, and B to include A's |
| 4959 | headers. This is not an ideal situation but is sometimes unavoidable. |
| 4960 | |
| 4961 | This list, if specified, lists which of the dependencies of the current |
| 4962 | target can include header files from the current target. That is, if A |
| 4963 | depends on B, B can only include headers from A if it is in A's |
| 4964 | allow_circular_includes_from list. Normally includes must follow the |
| 4965 | direction of dependencies, this flag allows them to go in the opposite |
| 4966 | direction. |
| 4967 | ``` |
| 4968 | |
| 4969 | #### **Danger** |
| 4970 | |
| 4971 | ``` |
| 4972 | In the above example, A's headers are likely to include headers from A's |
| 4973 | dependencies. Those dependencies may have public_configs that apply flags, |
| 4974 | defines, and include paths that make those headers work properly. |
| 4975 | |
| 4976 | With allow_circular_includes_from, B can include A's headers, and |
| 4977 | transitively from A's dependencies, without having the dependencies that |
| 4978 | would bring in the public_configs those headers need. The result may be |
| 4979 | errors or inconsistent builds. |
| 4980 | |
| 4981 | So when you use allow_circular_includes_from, make sure that any compiler |
| 4982 | settings, flags, and include directories are the same between both targets |
| 4983 | (consider putting such things in a shared config they can both reference). |
| 4984 | Make sure the dependencies are also the same (you might consider a group to |
| 4985 | collect such dependencies they both depend on). |
| 4986 | ``` |
| 4987 | |
| 4988 | #### **Example** |
| 4989 | |
| 4990 | ``` |
| 4991 | source_set("a") { |
| 4992 | deps = [ ":b", ":a_b_shared_deps" ] |
| 4993 | allow_circular_includes_from = [ ":b" ] |
| 4994 | ... |
| 4995 | } |
| 4996 | |
| 4997 | source_set("b") { |
| 4998 | deps = [ ":a_b_shared_deps" ] |
| 4999 | # Sources here can include headers from a despite lack of deps. |
| 5000 | ... |
| 5001 | } |
| 5002 | |
| 5003 | group("a_b_shared_deps") { |
| 5004 | public_deps = [ ":c" ] |
| 5005 | } |
| 5006 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5007 | ### <a name="var_arflags"></a>**arflags**: Arguments passed to static_library archiver. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5008 | |
| 5009 | ``` |
| 5010 | A list of flags passed to the archive/lib command that creates static |
| 5011 | libraries. |
| 5012 | |
| 5013 | arflags are NOT pushed to dependents, so applying arflags to source sets or |
| 5014 | any other target type will be a no-op. As with ldflags, you could put the |
| 5015 | arflags in a config and set that as a public or "all dependent" config, but |
| 5016 | that will likely not be what you want. If you have a chain of static |
| 5017 | libraries dependent on each other, this can cause the flags to propagate up |
| 5018 | to other static libraries. Due to the nature of how arflags are typically |
| 5019 | used, you will normally want to apply them directly on static_library targets |
| 5020 | themselves. |
| 5021 | ``` |
| 5022 | |
| 5023 | #### **Ordering of flags and values** |
| 5024 | |
| 5025 | ``` |
| 5026 | 1. Those set on the current target (not in a config). |
| 5027 | 2. Those set on the "configs" on the target in order that the |
| 5028 | configs appear in the list. |
| 5029 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5030 | that the configs appear in the list. |
| 5031 | 4. Those set on the "public_configs" on the target in order that |
| 5032 | those configs appear in the list. |
| 5033 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5034 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5035 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5036 | 6. public_configs pulled from dependencies, in the order of the |
| 5037 | "deps" list. If a dependency is public, they will be applied |
| 5038 | recursively. |
| 5039 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5040 | ### <a name="var_args"></a>**args**: (target variable) Arguments passed to an action. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5041 | |
| 5042 | ``` |
| 5043 | For action and action_foreach targets, args is the list of arguments to pass |
| 5044 | to the script. Typically you would use source expansion (see "gn help |
| 5045 | source_expansion") to insert the source file names. |
| 5046 | |
Adrian Taylor | c0d4f8b | 2022-03-25 15:05:57 -0700 | [diff] [blame] | 5047 | Args can also expand the substitution patterns corresponding to config |
| 5048 | variables in the same way that compiler tools (see "gn help tool") do. These |
| 5049 | allow actions that run compiler or compiler-like tools to access the results |
| 5050 | of propagating configs through the build graph. For example: |
| 5051 | |
| 5052 | args = [ "{{defines}}", "{{include_dirs}}", "{{rustenv}}", "--input-file", |
| 5053 | "{{source}}" ] |
| 5054 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5055 | See also "gn help action" and "gn help action_foreach". |
| 5056 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5057 | ### <a name="var_asmflags"></a>**asmflags**: Flags passed to the assembler. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5058 | |
| 5059 | ``` |
| 5060 | A list of strings. |
| 5061 | |
| 5062 | "asmflags" are passed to any invocation of a tool that takes an .asm or .S |
| 5063 | file as input. |
| 5064 | ``` |
| 5065 | |
| 5066 | #### **Ordering of flags and values** |
| 5067 | |
| 5068 | ``` |
| 5069 | 1. Those set on the current target (not in a config). |
| 5070 | 2. Those set on the "configs" on the target in order that the |
| 5071 | configs appear in the list. |
| 5072 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5073 | that the configs appear in the list. |
| 5074 | 4. Those set on the "public_configs" on the target in order that |
| 5075 | those configs appear in the list. |
| 5076 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5077 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5078 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5079 | 6. public_configs pulled from dependencies, in the order of the |
| 5080 | "deps" list. If a dependency is public, they will be applied |
| 5081 | recursively. |
| 5082 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5083 | ### <a name="var_assert_no_deps"></a>**assert_no_deps**: Ensure no deps on these targets. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5084 | |
| 5085 | ``` |
| 5086 | A list of label patterns. |
| 5087 | |
| 5088 | This list is a list of patterns that must not match any of the transitive |
| 5089 | dependencies of the target. These include all public, private, and data |
| 5090 | dependencies, and cross shared library boundaries. This allows you to express |
| 5091 | that undesirable code isn't accidentally added to downstream dependencies in |
| 5092 | a way that might otherwise be difficult to notice. |
| 5093 | |
| 5094 | Checking does not cross executable boundaries. If a target depends on an |
| 5095 | executable, it's assumed that the executable is a tool that is producing part |
| 5096 | of the build rather than something that is linked and distributed. This |
| 5097 | allows assert_no_deps to express what is distributed in the final target |
| 5098 | rather than depend on the internal build steps (which may include |
| 5099 | non-distributable code). |
| 5100 | |
| 5101 | See "gn help label_pattern" for the format of the entries in the list. These |
| 5102 | patterns allow blacklisting individual targets or whole directory |
| 5103 | hierarchies. |
| 5104 | |
| 5105 | Sometimes it is desirable to enforce that many targets have no dependencies |
| 5106 | on a target or set of targets. One efficient way to express this is to create |
| 5107 | a group with the assert_no_deps rule on it, and make that group depend on all |
| 5108 | targets you want to apply that assertion to. |
| 5109 | ``` |
| 5110 | |
| 5111 | #### **Example** |
| 5112 | |
| 5113 | ``` |
| 5114 | executable("doom_melon") { |
| 5115 | deps = [ "//foo:bar" ] |
| 5116 | ... |
| 5117 | assert_no_deps = [ |
| 5118 | "//evil/*", # Don't link any code from the evil directory. |
| 5119 | "//foo:test_support", # This target is also disallowed. |
| 5120 | ] |
| 5121 | } |
| 5122 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5123 | ### <a name="var_bridge_header"></a>**bridge_header**: [string] Path to C/Objective-C compatibility header. [Back to Top](#gn-reference) |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 5124 | |
| 5125 | ``` |
| 5126 | Valid for binary targets that contain Swift sources. |
| 5127 | |
| 5128 | Path to an header that includes C/Objective-C functions and types that |
| 5129 | needs to be made available to the Swift module. |
| 5130 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5131 | ### <a name="var_bundle_contents_dir"></a>**bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5132 | ``` |
| 5133 | create_bundle. |
| 5134 | |
| 5135 | A string corresponding to a path in $root_build_dir. |
| 5136 | |
| 5137 | This string is used by the "create_bundle" target to expand the |
| 5138 | {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must |
| 5139 | correspond to a path under "bundle_root_dir". |
| 5140 | |
| 5141 | See "gn help bundle_root_dir" for examples. |
| 5142 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5143 | ### <a name="var_bundle_deps_filter"></a>**bundle_deps_filter**: [label list] A list of labels that are filtered out. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5144 | |
| 5145 | ``` |
| 5146 | A list of target labels. |
| 5147 | |
| 5148 | This list contains target label patterns that should be filtered out when |
| 5149 | creating the bundle. Any target matching one of those label will be removed |
| 5150 | from the dependencies of the create_bundle target. |
| 5151 | |
| 5152 | This is mostly useful when creating application extension bundle as the |
| 5153 | application extension has access to runtime resources from the application |
| 5154 | bundle and thus do not require a second copy. |
| 5155 | |
| 5156 | See "gn help create_bundle" for more information. |
| 5157 | ``` |
| 5158 | |
| 5159 | #### **Example** |
| 5160 | |
| 5161 | ``` |
| 5162 | create_bundle("today_extension") { |
| 5163 | deps = [ |
| 5164 | "//base" |
| 5165 | ] |
| 5166 | bundle_root_dir = "$root_out_dir/today_extension.appex" |
| 5167 | bundle_deps_filter = [ |
| 5168 | # The extension uses //base but does not use any function calling into |
| 5169 | # third_party/icu and thus does not need the icudtl.dat file. |
| 5170 | "//third_party/icu:icudata", |
| 5171 | ] |
| 5172 | } |
| 5173 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5174 | ### <a name="var_bundle_executable_dir"></a>**bundle_executable_dir** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5175 | |
Joe Armstrong | b199e54 | 2019-03-21 09:40:08 +0800 | [diff] [blame] | 5176 | ``` |
Sylvain Defresne | d46f92f | 2019-04-03 16:14:29 +0200 | [diff] [blame] | 5177 | bundle_executable_dir: Expansion of {{bundle_executable_dir}} in |
| 5178 | create_bundle. |
| 5179 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5180 | A string corresponding to a path in $root_build_dir. |
| 5181 | |
| 5182 | This string is used by the "create_bundle" target to expand the |
| 5183 | {{bundle_executable_dir}} of the "bundle_data" target it depends on. This |
| 5184 | must correspond to a path under "bundle_root_dir". |
| 5185 | |
| 5186 | See "gn help bundle_root_dir" for examples. |
| 5187 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5188 | ### <a name="var_bundle_resources_dir"></a>**bundle_resources_dir** [Back to Top](#gn-reference) |
Joe Armstrong | b199e54 | 2019-03-21 09:40:08 +0800 | [diff] [blame] | 5189 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5190 | ``` |
Sylvain Defresne | d46f92f | 2019-04-03 16:14:29 +0200 | [diff] [blame] | 5191 | bundle_resources_dir: Expansion of {{bundle_resources_dir}} in |
| 5192 | create_bundle. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5193 | |
| 5194 | A string corresponding to a path in $root_build_dir. |
| 5195 | |
| 5196 | This string is used by the "create_bundle" target to expand the |
| 5197 | {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must |
| 5198 | correspond to a path under "bundle_root_dir". |
| 5199 | |
| 5200 | See "gn help bundle_root_dir" for examples. |
| 5201 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5202 | ### <a name="var_bundle_root_dir"></a>**bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5203 | |
| 5204 | ``` |
| 5205 | A string corresponding to a path in root_build_dir. |
| 5206 | |
| 5207 | This string is used by the "create_bundle" target to expand the |
| 5208 | {{bundle_root_dir}} of the "bundle_data" target it depends on. This must |
| 5209 | correspond to a path under root_build_dir. |
| 5210 | ``` |
| 5211 | |
| 5212 | #### **Example** |
| 5213 | |
| 5214 | ``` |
| 5215 | bundle_data("info_plist") { |
| 5216 | sources = [ "Info.plist" ] |
| 5217 | outputs = [ "{{bundle_contents_dir}}/Info.plist" ] |
| 5218 | } |
| 5219 | |
| 5220 | create_bundle("doom_melon.app") { |
| 5221 | deps = [ ":info_plist" ] |
| 5222 | bundle_root_dir = "${root_build_dir}/doom_melon.app" |
| 5223 | bundle_contents_dir = "${bundle_root_dir}/Contents" |
| 5224 | bundle_resources_dir = "${bundle_contents_dir}/Resources" |
| 5225 | bundle_executable_dir = "${bundle_contents_dir}/MacOS" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5226 | } |
| 5227 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5228 | ### <a name="var_cflags"></a>**cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5229 | |
| 5230 | ``` |
| 5231 | A list of strings. |
| 5232 | |
| 5233 | "cflags" are passed to all invocations of the C, C++, Objective C, and |
| 5234 | Objective C++ compilers. |
| 5235 | |
| 5236 | To target one of these variants individually, use "cflags_c", "cflags_cc", |
| 5237 | "cflags_objc", and "cflags_objcc", respectively. These variant-specific |
| 5238 | versions of cflags* will be appended on the compiler command line after |
| 5239 | "cflags". |
| 5240 | |
Max Ihlenfeldt | 5110a7f | 2024-01-22 11:56:47 +0100 | [diff] [blame] | 5241 | See also "asmflags" for flags for assembly-language files, "swiftflags" for |
| 5242 | swift files, and "rustflags" for Rust files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5243 | ``` |
| 5244 | |
| 5245 | #### **Ordering of flags and values** |
| 5246 | |
| 5247 | ``` |
| 5248 | 1. Those set on the current target (not in a config). |
| 5249 | 2. Those set on the "configs" on the target in order that the |
| 5250 | configs appear in the list. |
| 5251 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5252 | that the configs appear in the list. |
| 5253 | 4. Those set on the "public_configs" on the target in order that |
| 5254 | those configs appear in the list. |
| 5255 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5256 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5257 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5258 | 6. public_configs pulled from dependencies, in the order of the |
| 5259 | "deps" list. If a dependency is public, they will be applied |
| 5260 | recursively. |
| 5261 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5262 | ### <a name="var_cflags_c"></a>**cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5263 | |
| 5264 | ``` |
| 5265 | A list of strings. |
| 5266 | |
| 5267 | "cflags" are passed to all invocations of the C, C++, Objective C, and |
| 5268 | Objective C++ compilers. |
| 5269 | |
| 5270 | To target one of these variants individually, use "cflags_c", "cflags_cc", |
| 5271 | "cflags_objc", and "cflags_objcc", respectively. These variant-specific |
| 5272 | versions of cflags* will be appended on the compiler command line after |
| 5273 | "cflags". |
| 5274 | |
Max Ihlenfeldt | 5110a7f | 2024-01-22 11:56:47 +0100 | [diff] [blame] | 5275 | See also "asmflags" for flags for assembly-language files, "swiftflags" for |
| 5276 | swift files, and "rustflags" for Rust files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5277 | ``` |
| 5278 | |
| 5279 | #### **Ordering of flags and values** |
| 5280 | |
| 5281 | ``` |
| 5282 | 1. Those set on the current target (not in a config). |
| 5283 | 2. Those set on the "configs" on the target in order that the |
| 5284 | configs appear in the list. |
| 5285 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5286 | that the configs appear in the list. |
| 5287 | 4. Those set on the "public_configs" on the target in order that |
| 5288 | those configs appear in the list. |
| 5289 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5290 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5291 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5292 | 6. public_configs pulled from dependencies, in the order of the |
| 5293 | "deps" list. If a dependency is public, they will be applied |
| 5294 | recursively. |
| 5295 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5296 | ### <a name="var_cflags_cc"></a>**cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5297 | |
| 5298 | ``` |
| 5299 | A list of strings. |
| 5300 | |
| 5301 | "cflags" are passed to all invocations of the C, C++, Objective C, and |
| 5302 | Objective C++ compilers. |
| 5303 | |
| 5304 | To target one of these variants individually, use "cflags_c", "cflags_cc", |
| 5305 | "cflags_objc", and "cflags_objcc", respectively. These variant-specific |
| 5306 | versions of cflags* will be appended on the compiler command line after |
| 5307 | "cflags". |
| 5308 | |
Max Ihlenfeldt | 5110a7f | 2024-01-22 11:56:47 +0100 | [diff] [blame] | 5309 | See also "asmflags" for flags for assembly-language files, "swiftflags" for |
| 5310 | swift files, and "rustflags" for Rust files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5311 | ``` |
| 5312 | |
| 5313 | #### **Ordering of flags and values** |
| 5314 | |
| 5315 | ``` |
| 5316 | 1. Those set on the current target (not in a config). |
| 5317 | 2. Those set on the "configs" on the target in order that the |
| 5318 | configs appear in the list. |
| 5319 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5320 | that the configs appear in the list. |
| 5321 | 4. Those set on the "public_configs" on the target in order that |
| 5322 | those configs appear in the list. |
| 5323 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5324 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5325 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5326 | 6. public_configs pulled from dependencies, in the order of the |
| 5327 | "deps" list. If a dependency is public, they will be applied |
| 5328 | recursively. |
| 5329 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5330 | ### <a name="var_cflags_objc"></a>**cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5331 | |
| 5332 | ``` |
| 5333 | A list of strings. |
| 5334 | |
| 5335 | "cflags" are passed to all invocations of the C, C++, Objective C, and |
| 5336 | Objective C++ compilers. |
| 5337 | |
| 5338 | To target one of these variants individually, use "cflags_c", "cflags_cc", |
| 5339 | "cflags_objc", and "cflags_objcc", respectively. These variant-specific |
| 5340 | versions of cflags* will be appended on the compiler command line after |
| 5341 | "cflags". |
| 5342 | |
Max Ihlenfeldt | 5110a7f | 2024-01-22 11:56:47 +0100 | [diff] [blame] | 5343 | See also "asmflags" for flags for assembly-language files, "swiftflags" for |
| 5344 | swift files, and "rustflags" for Rust files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5345 | ``` |
| 5346 | |
| 5347 | #### **Ordering of flags and values** |
| 5348 | |
| 5349 | ``` |
| 5350 | 1. Those set on the current target (not in a config). |
| 5351 | 2. Those set on the "configs" on the target in order that the |
| 5352 | configs appear in the list. |
| 5353 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5354 | that the configs appear in the list. |
| 5355 | 4. Those set on the "public_configs" on the target in order that |
| 5356 | those configs appear in the list. |
| 5357 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5358 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5359 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5360 | 6. public_configs pulled from dependencies, in the order of the |
| 5361 | "deps" list. If a dependency is public, they will be applied |
| 5362 | recursively. |
| 5363 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5364 | ### <a name="var_cflags_objcc"></a>**cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5365 | |
| 5366 | ``` |
| 5367 | A list of strings. |
| 5368 | |
| 5369 | "cflags" are passed to all invocations of the C, C++, Objective C, and |
| 5370 | Objective C++ compilers. |
| 5371 | |
| 5372 | To target one of these variants individually, use "cflags_c", "cflags_cc", |
| 5373 | "cflags_objc", and "cflags_objcc", respectively. These variant-specific |
| 5374 | versions of cflags* will be appended on the compiler command line after |
| 5375 | "cflags". |
| 5376 | |
Max Ihlenfeldt | 5110a7f | 2024-01-22 11:56:47 +0100 | [diff] [blame] | 5377 | See also "asmflags" for flags for assembly-language files, "swiftflags" for |
| 5378 | swift files, and "rustflags" for Rust files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5379 | ``` |
| 5380 | |
| 5381 | #### **Ordering of flags and values** |
| 5382 | |
| 5383 | ``` |
| 5384 | 1. Those set on the current target (not in a config). |
| 5385 | 2. Those set on the "configs" on the target in order that the |
| 5386 | configs appear in the list. |
| 5387 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5388 | that the configs appear in the list. |
| 5389 | 4. Those set on the "public_configs" on the target in order that |
| 5390 | those configs appear in the list. |
| 5391 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5392 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5393 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5394 | 6. public_configs pulled from dependencies, in the order of the |
| 5395 | "deps" list. If a dependency is public, they will be applied |
| 5396 | recursively. |
| 5397 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5398 | ### <a name="var_check_includes"></a>**check_includes**: [boolean] Controls whether a target's files are checked. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5399 | |
| 5400 | ``` |
| 5401 | When true (the default), the "gn check" command (as well as "gn gen" with the |
| 5402 | --check flag) will check this target's sources and headers for proper |
| 5403 | dependencies. |
| 5404 | |
| 5405 | When false, the files in this target will be skipped by default. This does |
| 5406 | not affect other targets that depend on the current target, it just skips |
| 5407 | checking the includes of the current target's files. |
| 5408 | |
| 5409 | If there are a few conditionally included headers that trip up checking, you |
| 5410 | can exclude headers individually by annotating them with "nogncheck" (see "gn |
| 5411 | help nogncheck"). |
| 5412 | |
| 5413 | The topic "gn help check" has general information on how checking works and |
| 5414 | advice on how to pass a check in problematic cases. |
| 5415 | ``` |
| 5416 | |
| 5417 | #### **Example** |
| 5418 | |
| 5419 | ``` |
| 5420 | source_set("busted_includes") { |
| 5421 | # This target's includes are messed up, exclude it from checking. |
| 5422 | check_includes = false |
| 5423 | ... |
| 5424 | } |
| 5425 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5426 | ### <a name="var_code_signing_args"></a>**code_signing_args**: [string list] [deprecated] Args for the post-processing script. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5427 | |
| 5428 | ``` |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 5429 | For create_bundle targets, post_processing_args is the list of arguments to |
| 5430 | pass to the post-processing script. Typically you would use source expansion |
| 5431 | (see "gn help source_expansion") to insert the source file names. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5432 | |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 5433 | Deprecated: this is an old name for the "post_processing_args" property of |
| 5434 | the "create_bundle" target. It is still supported to avoid breaking existing |
| 5435 | build rules, but a warning will be emitted when it is used. |
| 5436 | |
| 5437 | See also "gn help create_bundle" and "gn help post_processing_args". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5438 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5439 | ### <a name="var_code_signing_outputs"></a>**code_signing_outputs**: [file list] [deprecated] Outputs of the post-processing step. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5440 | |
| 5441 | ``` |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 5442 | Outputs from the post-processing step of a create_bundle target. Must refer to |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5443 | files in the build directory. |
| 5444 | |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 5445 | Deprecated: this is an old name for the "post_processing_outputs" property of |
| 5446 | the "create_bundle" target. It is still supported to avoid breaking existing |
| 5447 | build rules, but a warning will be emitted when it is used. |
| 5448 | |
| 5449 | See also "gn help create_bundle" and "gn help post_processing_args". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5450 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5451 | ### <a name="var_code_signing_script"></a>**code_signing_script**: [file name] [deprecated] Script for the post-processing step." [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5452 | |
| 5453 | ``` |
| 5454 | An absolute or buildfile-relative file name of a Python script to run for a |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 5455 | create_bundle target to perform the post-processing step. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5456 | |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 5457 | Deprecated: this is an old name for the "post_processing_script" property of |
| 5458 | the "create_bundle" target. It is still supported to avoid breaking existing |
| 5459 | build rules, but a warning will be emitted when it is used. |
| 5460 | |
| 5461 | See also "gn help create_bundle" and "gn help post_processing_args". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5462 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5463 | ### <a name="var_code_signing_sources"></a>**code_signing_sources**: [file list] [deprecated] Sources for the post-processing step. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5464 | |
| 5465 | ``` |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 5466 | A list of files used as input for the post-processing step of a create_bundle |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5467 | target. Non-absolute paths will be resolved relative to the current build |
| 5468 | file. |
| 5469 | |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 5470 | Deprecated: this is an old name for the "post_processing_sources" property of |
| 5471 | the "create_bundle" target. It is still supported to avoid breaking existing |
| 5472 | build rules, but a warning will be emitted when it is used. |
| 5473 | |
| 5474 | See also "gn help create_bundle" and "gn help post_processing_args". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5475 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5476 | ### <a name="var_complete_static_lib"></a>**complete_static_lib**: [boolean] Links all deps into a static library. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5477 | |
| 5478 | ``` |
| 5479 | A static library normally doesn't include code from dependencies, but instead |
| 5480 | forwards the static libraries and source sets in its deps up the dependency |
| 5481 | chain until a linkable target (an executable or shared library) is reached. |
| 5482 | The final linkable target only links each static library once, even if it |
| 5483 | appears more than once in its dependency graph. |
| 5484 | |
| 5485 | In some cases the static library might be the final desired output. For |
| 5486 | example, you may be producing a static library for distribution to third |
| 5487 | parties. In this case, the static library should include code for all |
| 5488 | dependencies in one complete package. However, complete static libraries |
| 5489 | themselves are never linked into other complete static libraries. All |
| 5490 | complete static libraries are for distribution and linking them in would |
| 5491 | cause code duplication in this case. If the static library is not for |
| 5492 | distribution, it should not be complete. |
| 5493 | |
| 5494 | GN treats non-complete static libraries as source sets when they are linked |
| 5495 | into complete static libraries. This is done because some tools like AR do |
| 5496 | not handle dependent static libraries properly. This makes it easier to write |
| 5497 | "alink" rules. |
| 5498 | |
| 5499 | In rare cases it makes sense to list a header in more than one target if it |
| 5500 | could be considered conceptually a member of both. libraries. |
| 5501 | ``` |
| 5502 | |
| 5503 | #### **Example** |
| 5504 | |
| 5505 | ``` |
| 5506 | static_library("foo") { |
| 5507 | complete_static_lib = true |
| 5508 | deps = [ "bar" ] |
| 5509 | } |
| 5510 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5511 | ### <a name="var_configs"></a>**configs**: Configs applying to this target or config. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5512 | |
| 5513 | ``` |
| 5514 | A list of config labels. |
| 5515 | ``` |
| 5516 | |
| 5517 | #### **Configs on a target** |
| 5518 | |
| 5519 | ``` |
| 5520 | When used on a target, the include_dirs, defines, etc. in each config are |
| 5521 | appended in the order they appear to the compile command for each file in the |
| 5522 | target. They will appear after the include_dirs, defines, etc. that the |
| 5523 | target sets directly. |
| 5524 | |
| 5525 | Since configs apply after the values set on a target, directly setting a |
| 5526 | compiler flag will prepend it to the command line. If you want to append a |
| 5527 | flag instead, you can put that flag in a one-off config and append that |
| 5528 | config to the target's configs list. |
| 5529 | |
| 5530 | The build configuration script will generally set up the default configs |
| 5531 | applying to a given target type (see "set_defaults"). When a target is being |
| 5532 | defined, it can add to or remove from this list. |
| 5533 | ``` |
| 5534 | |
| 5535 | #### **Configs on a config** |
| 5536 | |
| 5537 | ``` |
| 5538 | It is possible to create composite configs by specifying configs on a config. |
| 5539 | One might do this to forward values, or to factor out blocks of settings from |
| 5540 | very large configs into more manageable named chunks. |
| 5541 | |
| 5542 | In this case, the composite config is expanded to be the concatenation of its |
| 5543 | own values, and in order, the values from its sub-configs *before* anything |
| 5544 | else happens. This has some ramifications: |
| 5545 | |
| 5546 | - A target has no visibility into a config's sub-configs. Target code only |
| 5547 | sees the name of the composite config. It can't remove sub-configs or opt |
| 5548 | in to only parts of it. The composite config may not even be defined |
| 5549 | before the target is. |
| 5550 | |
| 5551 | - You can get duplication of values if a config is listed twice, say, on a |
| 5552 | target and in a sub-config that also applies. In other cases, the configs |
| 5553 | applying to a target are de-duped. It's expected that if a config is |
| 5554 | listed as a sub-config that it is only used in that context. (Note that |
| 5555 | it's possible to fix this and de-dupe, but it's not normally relevant and |
| 5556 | complicates the implementation.) |
| 5557 | ``` |
| 5558 | |
| 5559 | #### **Ordering of flags and values** |
| 5560 | |
| 5561 | ``` |
| 5562 | 1. Those set on the current target (not in a config). |
| 5563 | 2. Those set on the "configs" on the target in order that the |
| 5564 | configs appear in the list. |
| 5565 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5566 | that the configs appear in the list. |
| 5567 | 4. Those set on the "public_configs" on the target in order that |
| 5568 | those configs appear in the list. |
| 5569 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5570 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5571 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5572 | 6. public_configs pulled from dependencies, in the order of the |
| 5573 | "deps" list. If a dependency is public, they will be applied |
| 5574 | recursively. |
| 5575 | ``` |
| 5576 | |
| 5577 | #### **Example** |
| 5578 | |
| 5579 | ``` |
| 5580 | # Configs on a target. |
| 5581 | source_set("foo") { |
| 5582 | # Don't use the default RTTI config that BUILDCONFIG applied to us. |
| 5583 | configs -= [ "//build:no_rtti" ] |
| 5584 | |
| 5585 | # Add some of our own settings. |
| 5586 | configs += [ ":mysettings" ] |
| 5587 | } |
| 5588 | |
| 5589 | # Create a default_optimization config that forwards to one of a set of more |
| 5590 | # specialized configs depending on build flags. This pattern is useful |
| 5591 | # because it allows a target to opt in to either a default set, or a more |
| 5592 | # specific set, while avoid duplicating the settings in two places. |
| 5593 | config("super_optimization") { |
| 5594 | cflags = [ ... ] |
| 5595 | } |
| 5596 | config("default_optimization") { |
| 5597 | if (optimize_everything) { |
| 5598 | configs = [ ":super_optimization" ] |
| 5599 | } else { |
| 5600 | configs = [ ":no_optimization" ] |
| 5601 | } |
| 5602 | } |
| 5603 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5604 | ### <a name="var_contents"></a>**contents**: Contents to write to file. [Back to Top](#gn-reference) |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 5605 | |
| 5606 | ``` |
| 5607 | The contents of the file for a generated_file target. |
| 5608 | See "gn help generated_file". |
| 5609 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5610 | ### <a name="var_crate_name"></a>**crate_name**: [string] The name for the compiled crate. [Back to Top](#gn-reference) |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 5611 | |
| 5612 | ``` |
| 5613 | Valid for `rust_library` targets and `executable`, `static_library`, |
| 5614 | `shared_library`, and `source_set` targets that contain Rust sources. |
| 5615 | |
| 5616 | If crate_name is not set, then this rule will use the target name. |
| 5617 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5618 | ### <a name="var_crate_root"></a>**crate_root**: [string] The root source file for a binary or library. [Back to Top](#gn-reference) |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 5619 | |
| 5620 | ``` |
| 5621 | Valid for `rust_library` targets and `executable`, `static_library`, |
| 5622 | `shared_library`, and `source_set` targets that contain Rust sources. |
| 5623 | |
| 5624 | This file is usually the `main.rs` or `lib.rs` for binaries and libraries, |
| 5625 | respectively. |
| 5626 | |
| 5627 | If crate_root is not set, then this rule will look for a lib.rs file (or |
| 5628 | main.rs for executable) or a single file in sources, if sources contains |
| 5629 | only one file. |
| 5630 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5631 | ### <a name="var_crate_type"></a>**crate_type**: [string] The type of linkage to use on a shared_library. [Back to Top](#gn-reference) |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 5632 | |
| 5633 | ``` |
| 5634 | Valid for `rust_library` targets and `executable`, `static_library`, |
| 5635 | `shared_library`, and `source_set` targets that contain Rust sources. |
| 5636 | |
| 5637 | Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib". |
| 5638 | This field sets the `crate-type` attribute for the `rustc` tool on static |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5639 | libraries, as well as the appropriate output extension in the |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 5640 | `rust_output_extension` attribute. Since outputs must be explicit, the `lib` |
| 5641 | crate type (where the Rust compiler produces what it thinks is the |
| 5642 | appropriate library type) is not supported. |
| 5643 | |
| 5644 | It should be noted that the "dylib" crate type in Rust is unstable in the set |
| 5645 | of symbols it exposes, and most usages today are potentially wrong and will |
| 5646 | be broken in the future. |
| 5647 | |
| 5648 | Static libraries, rust libraries, and executables have this field set |
| 5649 | automatically. |
| 5650 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5651 | ### <a name="var_data"></a>**data**: Runtime data file dependencies. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5652 | |
| 5653 | ``` |
| 5654 | Lists files or directories required to run the given target. These are |
| 5655 | typically data files or directories of data files. The paths are interpreted |
| 5656 | as being relative to the current build file. Since these are runtime |
| 5657 | dependencies, they do not affect which targets are built or when. To declare |
| 5658 | input files to a script, use "inputs". |
| 5659 | |
| 5660 | Appearing in the "data" section does not imply any special handling such as |
| 5661 | copying them to the output directory. This is just used for declaring runtime |
| 5662 | dependencies. Runtime dependencies can be queried using the "runtime_deps" |
| 5663 | category of "gn desc" or written during build generation via |
| 5664 | "--runtime-deps-list-file". |
| 5665 | |
| 5666 | GN doesn't require data files to exist at build-time. So actions that produce |
| 5667 | files that are in turn runtime dependencies can list those generated files |
| 5668 | both in the "outputs" list as well as the "data" list. |
| 5669 | |
| 5670 | By convention, directories are listed with a trailing slash: |
| 5671 | data = [ "test/data/" ] |
| 5672 | However, no verification is done on these so GN doesn't enforce this. The |
| 5673 | paths are just rebased and passed along when requested. |
| 5674 | |
| 5675 | Note: On iOS and macOS, create_bundle targets will not be recursed into when |
| 5676 | gathering data. See "gn help create_bundle" for details. |
| 5677 | |
| 5678 | See "gn help runtime_deps" for how these are used. |
| 5679 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5680 | ### <a name="var_data_deps"></a>**data_deps**: Non-linked dependencies. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5681 | |
| 5682 | ``` |
| 5683 | A list of target labels. |
| 5684 | |
| 5685 | Specifies dependencies of a target that are not actually linked into the |
| 5686 | current target. Such dependencies will be built and will be available at |
| 5687 | runtime. |
| 5688 | |
| 5689 | This is normally used for things like plugins or helper programs that a |
| 5690 | target needs at runtime. |
| 5691 | |
| 5692 | Note: On iOS and macOS, create_bundle targets will not be recursed into when |
| 5693 | gathering data_deps. See "gn help create_bundle" for details. |
| 5694 | |
| 5695 | See also "gn help deps" and "gn help data". |
| 5696 | ``` |
| 5697 | |
| 5698 | #### **Example** |
| 5699 | |
| 5700 | ``` |
| 5701 | executable("foo") { |
| 5702 | deps = [ "//base" ] |
| 5703 | data_deps = [ "//plugins:my_runtime_plugin" ] |
| 5704 | } |
| 5705 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5706 | ### <a name="var_data_keys"></a>**data_keys**: Keys from which to collect metadata. [Back to Top](#gn-reference) |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 5707 | |
| 5708 | ``` |
| 5709 | These keys are used to identify metadata to collect. If a walked target |
| 5710 | defines this key in its metadata, its value will be appended to the resulting |
| 5711 | collection. |
| 5712 | |
| 5713 | See "gn help generated_file". |
| 5714 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5715 | ### <a name="var_defines"></a>**defines**: C preprocessor defines. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5716 | |
| 5717 | ``` |
| 5718 | A list of strings |
| 5719 | |
| 5720 | These strings will be passed to the C/C++ compiler as #defines. The strings |
| 5721 | may or may not include an "=" to assign a value. |
| 5722 | ``` |
| 5723 | |
| 5724 | #### **Ordering of flags and values** |
| 5725 | |
| 5726 | ``` |
| 5727 | 1. Those set on the current target (not in a config). |
| 5728 | 2. Those set on the "configs" on the target in order that the |
| 5729 | configs appear in the list. |
| 5730 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5731 | that the configs appear in the list. |
| 5732 | 4. Those set on the "public_configs" on the target in order that |
| 5733 | those configs appear in the list. |
| 5734 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5735 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5736 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5737 | 6. public_configs pulled from dependencies, in the order of the |
| 5738 | "deps" list. If a dependency is public, they will be applied |
| 5739 | recursively. |
| 5740 | ``` |
| 5741 | |
| 5742 | #### **Example** |
| 5743 | |
| 5744 | ``` |
| 5745 | defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ] |
| 5746 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5747 | ### <a name="var_depfile"></a>**depfile**: [string] File name for input dependencies for actions. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5748 | |
| 5749 | ``` |
| 5750 | If nonempty, this string specifies that the current action or action_foreach |
| 5751 | target will generate the given ".d" file containing the dependencies of the |
| 5752 | input. Empty or unset means that the script doesn't generate the files. |
| 5753 | |
| 5754 | A depfile should be used only when a target depends on files that are not |
| 5755 | already specified by a target's inputs and sources. Likewise, depfiles should |
| 5756 | specify only those dependencies not already included in sources or inputs. |
| 5757 | |
| 5758 | The .d file should go in the target output directory. If you have more than |
| 5759 | one source file that the script is being run over, you can use the output |
| 5760 | file expansions described in "gn help action_foreach" to name the .d file |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 5761 | according to the input. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5762 | |
| 5763 | The format is that of a Makefile and all paths must be relative to the root |
| 5764 | build directory. Only one output may be listed and it must match the first |
| 5765 | output of the action. |
| 5766 | |
| 5767 | Although depfiles are created by an action, they should not be listed in the |
| 5768 | action's "outputs" unless another target will use the file as an input. |
| 5769 | ``` |
| 5770 | |
| 5771 | #### **Example** |
| 5772 | |
| 5773 | ``` |
| 5774 | action_foreach("myscript_target") { |
| 5775 | script = "myscript.py" |
| 5776 | sources = [ ... ] |
| 5777 | |
| 5778 | # Locate the depfile in the output directory named like the |
| 5779 | # inputs but with a ".d" appended. |
Hector Dearman | 84c8431 | 2023-01-29 11:50:02 +0000 | [diff] [blame] | 5780 | depfile = "$target_gen_dir/{{source_name_part}}.d" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5781 | |
| 5782 | # Say our script uses "-o <d file>" to indicate the depfile. |
Hector Dearman | 84c8431 | 2023-01-29 11:50:02 +0000 | [diff] [blame] | 5783 | args = [ "{{source}}", "-o", rebase_path(depfile, root_build_dir)] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5784 | } |
| 5785 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5786 | ### <a name="var_deps"></a>**deps**: Private linked dependencies. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5787 | |
| 5788 | ``` |
| 5789 | A list of target labels. |
| 5790 | |
| 5791 | Specifies private dependencies of a target. Private dependencies are |
| 5792 | propagated up the dependency tree and linked to dependent targets, but do not |
| 5793 | grant the ability to include headers from the dependency. Public configs are |
| 5794 | not forwarded. |
| 5795 | ``` |
| 5796 | |
| 5797 | #### **Details of dependency propagation** |
| 5798 | |
| 5799 | ``` |
| 5800 | Source sets, shared libraries, and non-complete static libraries will be |
| 5801 | propagated up the dependency tree across groups, non-complete static |
| 5802 | libraries and source sets. |
| 5803 | |
| 5804 | Executables, shared libraries, and complete static libraries will link all |
| 5805 | propagated targets and stop propagation. Actions and copy steps also stop |
| 5806 | propagation, allowing them to take a library as an input but not force |
| 5807 | dependents to link to it. |
| 5808 | |
| 5809 | Propagation of all_dependent_configs and public_configs happens independently |
| 5810 | of target type. all_dependent_configs are always propagated across all types |
| 5811 | of targets, and public_configs are always propagated across public deps of |
| 5812 | all types of targets. |
| 5813 | |
danakj | 2a92efd | 2023-07-06 14:16:38 -0400 | [diff] [blame] | 5814 | For Rust targets, deps ensures that Rust code can refer to the dependency |
| 5815 | target. If the dependency is a C/C++ target, the path to that target will |
| 5816 | be made available to Rust for `#[link]` directives. |
| 5817 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5818 | Data dependencies are propagated differently. See "gn help data_deps" and |
| 5819 | "gn help runtime_deps". |
| 5820 | |
| 5821 | See also "public_deps". |
| 5822 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5823 | ### <a name="var_externs"></a>**externs**: [scope] Set of Rust crate-dependency pairs. [Back to Top](#gn-reference) |
Petr Hosek | a141386 | 2020-01-03 12:54:33 -0800 | [diff] [blame] | 5824 | |
| 5825 | ``` |
| 5826 | A list, each value being a scope indicating a pair of crate name and the path |
| 5827 | to the Rust library. |
| 5828 | |
| 5829 | These libraries will be passed as `--extern crate_name=path` to compiler |
| 5830 | invocation containing the current target. |
| 5831 | ``` |
| 5832 | |
| 5833 | #### **Examples** |
| 5834 | |
| 5835 | ``` |
| 5836 | executable("foo") { |
| 5837 | sources = [ "main.rs" ] |
| 5838 | externs = [{ |
| 5839 | crate_name = "bar", |
| 5840 | path = "path/to/bar.rlib" |
| 5841 | }] |
| 5842 | } |
| 5843 | |
| 5844 | This target would compile the `foo` crate with the following `extern` flag: |
| 5845 | `--extern bar=path/to/bar.rlib`. |
| 5846 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5847 | ### <a name="var_framework_dirs"></a>**framework_dirs**: [directory list] Additional framework search directories. [Back to Top](#gn-reference) |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 5848 | |
| 5849 | ``` |
| 5850 | A list of source directories. |
| 5851 | |
| 5852 | The directories in this list will be added to the framework search path for |
| 5853 | the files in the affected target. |
| 5854 | ``` |
| 5855 | |
| 5856 | #### **Ordering of flags and values** |
| 5857 | |
| 5858 | ``` |
| 5859 | 1. Those set on the current target (not in a config). |
| 5860 | 2. Those set on the "configs" on the target in order that the |
| 5861 | configs appear in the list. |
| 5862 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5863 | that the configs appear in the list. |
| 5864 | 4. Those set on the "public_configs" on the target in order that |
| 5865 | those configs appear in the list. |
| 5866 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5867 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5868 | more than once, only the first occurrence will be used. |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 5869 | 6. public_configs pulled from dependencies, in the order of the |
| 5870 | "deps" list. If a dependency is public, they will be applied |
| 5871 | recursively. |
| 5872 | ``` |
| 5873 | |
| 5874 | #### **Example** |
| 5875 | |
| 5876 | ``` |
| 5877 | framework_dirs = [ "src/include", "//third_party/foo" ] |
| 5878 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5879 | ### <a name="var_frameworks"></a>**frameworks**: [name list] Name of frameworks that must be linked. [Back to Top](#gn-reference) |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 5880 | |
| 5881 | ``` |
| 5882 | A list of framework names. |
| 5883 | |
| 5884 | The frameworks named in that list will be linked with any dynamic link |
| 5885 | type target. |
| 5886 | ``` |
| 5887 | |
| 5888 | #### **Ordering of flags and values** |
| 5889 | |
| 5890 | ``` |
| 5891 | 1. Those set on the current target (not in a config). |
| 5892 | 2. Those set on the "configs" on the target in order that the |
| 5893 | configs appear in the list. |
| 5894 | 3. Those set on the "all_dependent_configs" on the target in order |
| 5895 | that the configs appear in the list. |
| 5896 | 4. Those set on the "public_configs" on the target in order that |
| 5897 | those configs appear in the list. |
| 5898 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 5899 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 5900 | more than once, only the first occurrence will be used. |
Sylvain Defresne | a09ec16 | 2020-01-16 12:36:40 +0100 | [diff] [blame] | 5901 | 6. public_configs pulled from dependencies, in the order of the |
| 5902 | "deps" list. If a dependency is public, they will be applied |
| 5903 | recursively. |
| 5904 | ``` |
| 5905 | |
| 5906 | #### **Example** |
| 5907 | |
| 5908 | ``` |
| 5909 | frameworks = [ "Foundation.framework", "Foo.framework" ] |
| 5910 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5911 | ### <a name="var_friend"></a>**friend**: Allow targets to include private headers. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5912 | |
| 5913 | ``` |
| 5914 | A list of label patterns (see "gn help label_pattern") that allow dependent |
| 5915 | targets to include private headers. Applies to all binary targets. |
| 5916 | |
| 5917 | Normally if a target lists headers in the "public" list (see "gn help |
| 5918 | public"), other headers are implicitly marked as private. Private headers |
| 5919 | can not be included by other targets, even with a public dependency path. |
| 5920 | The "gn check" function performs this validation. |
| 5921 | |
| 5922 | A friend declaration allows one or more targets to include private headers. |
| 5923 | This is useful for things like unit tests that are closely associated with a |
| 5924 | target and require internal knowledge without opening up all headers to be |
| 5925 | included by all dependents. |
| 5926 | |
| 5927 | A friend target does not allow that target to include headers when no |
| 5928 | dependency exists. A public dependency path must still exist between two |
| 5929 | targets to include any headers from a destination target. The friend |
| 5930 | annotation merely allows the use of headers that would otherwise be |
| 5931 | prohibited because they are private. |
| 5932 | |
| 5933 | The friend annotation is matched only against the target containing the file |
| 5934 | with the include directive. Friend annotations are not propagated across |
| 5935 | public or private dependencies. Friend annotations do not affect visibility. |
| 5936 | ``` |
| 5937 | |
| 5938 | #### **Example** |
| 5939 | |
| 5940 | ``` |
| 5941 | static_library("lib") { |
| 5942 | # This target can include our private headers. |
| 5943 | friend = [ ":unit_tests" ] |
| 5944 | |
| 5945 | public = [ |
| 5946 | "public_api.h", # Normal public API for dependent targets. |
| 5947 | ] |
| 5948 | |
| 5949 | # Private API and sources. |
| 5950 | sources = [ |
| 5951 | "a_source_file.cc", |
| 5952 | |
| 5953 | # Normal targets that depend on this one won't be able to include this |
| 5954 | # because this target defines a list of "public" headers. Without the |
| 5955 | # "public" list, all headers are implicitly public. |
| 5956 | "private_api.h", |
| 5957 | ] |
| 5958 | } |
| 5959 | |
| 5960 | executable("unit_tests") { |
| 5961 | sources = [ |
| 5962 | # This can include "private_api.h" from the :lib target because it |
| 5963 | # depends on that target and because of the friend annotation. |
| 5964 | "my_test.cc", |
| 5965 | ] |
| 5966 | |
| 5967 | deps = [ |
| 5968 | ":lib", # Required for the include to be allowed. |
| 5969 | ] |
| 5970 | } |
| 5971 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5972 | ### <a name="var_gen_deps"></a>**gen_deps**: Declares targets that should generate when this one does. [Back to Top](#gn-reference) |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 5973 | |
| 5974 | ``` |
| 5975 | A list of target labels. |
| 5976 | |
| 5977 | Not all GN targets that get evaluated are actually turned into ninja targets |
| 5978 | (see "gn help execution"). If this target is generated, then any targets in |
Ricardo Ribalda | 85bd0a6 | 2023-11-11 16:30:19 +0000 | [diff] [blame] | 5979 | the "gen_deps" list will also be generated, regardless of the usual criteria. |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 5980 | |
| 5981 | Since "gen_deps" are not build time dependencies, there can be cycles between |
| 5982 | "deps" and "gen_deps" or within "gen_deps" itself. |
| 5983 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 5984 | ### <a name="var_include_dirs"></a>**include_dirs**: Additional include directories. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 5985 | |
| 5986 | ``` |
| 5987 | A list of source directories. |
| 5988 | |
| 5989 | The directories in this list will be added to the include path for the files |
| 5990 | in the affected target. |
| 5991 | ``` |
| 5992 | |
| 5993 | #### **Ordering of flags and values** |
| 5994 | |
| 5995 | ``` |
| 5996 | 1. Those set on the current target (not in a config). |
| 5997 | 2. Those set on the "configs" on the target in order that the |
| 5998 | configs appear in the list. |
| 5999 | 3. Those set on the "all_dependent_configs" on the target in order |
| 6000 | that the configs appear in the list. |
| 6001 | 4. Those set on the "public_configs" on the target in order that |
| 6002 | those configs appear in the list. |
| 6003 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 6004 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 6005 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6006 | 6. public_configs pulled from dependencies, in the order of the |
| 6007 | "deps" list. If a dependency is public, they will be applied |
| 6008 | recursively. |
| 6009 | ``` |
| 6010 | |
| 6011 | #### **Example** |
| 6012 | |
| 6013 | ``` |
| 6014 | include_dirs = [ "src/include", "//third_party/foo" ] |
| 6015 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6016 | ### <a name="var_inputs"></a>**inputs**: Additional compile-time dependencies. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6017 | |
| 6018 | ``` |
| 6019 | Inputs are compile-time dependencies of the current target. This means that |
| 6020 | all inputs must be available before compiling any of the sources or executing |
| 6021 | any actions. |
| 6022 | |
| 6023 | Inputs are typically only used for action and action_foreach targets. |
| 6024 | ``` |
| 6025 | |
| 6026 | #### **Inputs for actions** |
| 6027 | |
| 6028 | ``` |
| 6029 | For action and action_foreach targets, inputs should be the inputs to script |
| 6030 | that don't vary. These should be all .py files that the script uses via |
| 6031 | imports (the main script itself will be an implicit dependency of the action |
| 6032 | so need not be listed). |
| 6033 | |
| 6034 | For action targets, inputs and sources are treated the same, but from a style |
| 6035 | perspective, it's recommended to follow the same rule as action_foreach and |
| 6036 | put helper files in the inputs, and the data used by the script (if any) in |
| 6037 | sources. |
| 6038 | |
| 6039 | Note that another way to declare input dependencies from an action is to have |
| 6040 | the action write a depfile (see "gn help depfile"). This allows the script to |
| 6041 | dynamically write input dependencies, that might not be known until actually |
| 6042 | executing the script. This is more efficient than doing processing while |
| 6043 | running GN to determine the inputs, and is easier to keep in-sync than |
| 6044 | hardcoding the list. |
| 6045 | ``` |
| 6046 | |
| 6047 | #### **Script input gotchas** |
| 6048 | |
| 6049 | ``` |
| 6050 | It may be tempting to write a script that enumerates all files in a directory |
| 6051 | as inputs. Don't do this! Even if you specify all the files in the inputs or |
| 6052 | sources in the GN target (or worse, enumerate the files in an exec_script |
| 6053 | call when running GN, which will be slow), the dependencies will be broken. |
| 6054 | |
| 6055 | The problem happens if a file is ever removed because the inputs are not |
| 6056 | listed on the command line to the script. Because the script hasn't changed |
| 6057 | and all inputs are up to date, the script will not re-run and you will get a |
| 6058 | stale build. Instead, either list all inputs on the command line to the |
| 6059 | script, or if there are many, create a separate list file that the script |
| 6060 | reads. As long as this file is listed in the inputs, the build will detect |
| 6061 | when it has changed in any way and the action will re-run. |
| 6062 | ``` |
| 6063 | |
| 6064 | #### **Inputs for binary targets** |
| 6065 | |
| 6066 | ``` |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6067 | Any input dependencies will be resolved before compiling any sources or |
| 6068 | linking the target. Normally, all actions that a target depends on will be run |
| 6069 | before any files in a target are compiled. So if you depend on generated |
| 6070 | headers, you do not typically need to list them in the inputs section. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6071 | |
| 6072 | Inputs for binary targets will be treated as implicit dependencies, meaning |
| 6073 | that changes in any of the inputs will force all sources in the target to be |
| 6074 | recompiled. If an input only applies to a subset of source files, you may |
| 6075 | want to split those into a separate target to avoid unnecessary recompiles. |
| 6076 | ``` |
| 6077 | |
| 6078 | #### **Example** |
| 6079 | |
| 6080 | ``` |
| 6081 | action("myscript") { |
| 6082 | script = "domything.py" |
| 6083 | inputs = [ "input.data" ] |
| 6084 | } |
| 6085 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6086 | ### <a name="var_ldflags"></a>**ldflags**: Flags passed to the linker. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6087 | |
| 6088 | ``` |
| 6089 | A list of strings. |
| 6090 | |
| 6091 | These flags are passed on the command-line to the linker and generally |
| 6092 | specify various linking options. Most targets will not need these and will |
| 6093 | use "libs" and "lib_dirs" instead. |
| 6094 | |
| 6095 | ldflags are NOT pushed to dependents, so applying ldflags to source sets or |
| 6096 | static libraries will be a no-op. If you want to apply ldflags to dependent |
| 6097 | targets, put them in a config and set it in the all_dependent_configs or |
| 6098 | public_configs. |
| 6099 | ``` |
| 6100 | |
| 6101 | #### **Ordering of flags and values** |
| 6102 | |
| 6103 | ``` |
| 6104 | 1. Those set on the current target (not in a config). |
| 6105 | 2. Those set on the "configs" on the target in order that the |
| 6106 | configs appear in the list. |
| 6107 | 3. Those set on the "all_dependent_configs" on the target in order |
| 6108 | that the configs appear in the list. |
| 6109 | 4. Those set on the "public_configs" on the target in order that |
| 6110 | those configs appear in the list. |
| 6111 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 6112 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 6113 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6114 | 6. public_configs pulled from dependencies, in the order of the |
| 6115 | "deps" list. If a dependency is public, they will be applied |
| 6116 | recursively. |
| 6117 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6118 | ### <a name="var_lib_dirs"></a>**lib_dirs**: Additional library directories. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6119 | |
| 6120 | ``` |
| 6121 | A list of directories. |
| 6122 | |
| 6123 | Specifies additional directories passed to the linker for searching for the |
| 6124 | required libraries. If an item is not an absolute path, it will be treated as |
| 6125 | being relative to the current build file. |
| 6126 | |
| 6127 | libs and lib_dirs work differently than other flags in two respects. |
Joe Armstrong | 23d22bc | 2019-06-28 15:31:03 +0800 | [diff] [blame] | 6128 | First, they are inherited across static library boundaries until a |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6129 | shared library or executable target is reached. Second, they are |
| 6130 | uniquified so each one is only passed once (the first instance of it |
| 6131 | will be the one used). |
| 6132 | ``` |
| 6133 | |
| 6134 | #### **Ordering of flags and values** |
| 6135 | |
| 6136 | ``` |
| 6137 | 1. Those set on the current target (not in a config). |
| 6138 | 2. Those set on the "configs" on the target in order that the |
| 6139 | configs appear in the list. |
| 6140 | 3. Those set on the "all_dependent_configs" on the target in order |
| 6141 | that the configs appear in the list. |
| 6142 | 4. Those set on the "public_configs" on the target in order that |
| 6143 | those configs appear in the list. |
| 6144 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 6145 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 6146 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6147 | 6. public_configs pulled from dependencies, in the order of the |
| 6148 | "deps" list. If a dependency is public, they will be applied |
| 6149 | recursively. |
| 6150 | |
| 6151 | For "libs" and "lib_dirs" only, the values propagated from |
| 6152 | dependencies (as described above) are applied last assuming they |
| 6153 | are not already in the list. |
| 6154 | ``` |
| 6155 | |
| 6156 | #### **Example** |
| 6157 | |
| 6158 | ``` |
| 6159 | lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ] |
| 6160 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6161 | ### <a name="var_libs"></a>**libs**: Additional libraries to link. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6162 | |
| 6163 | ``` |
| 6164 | A list of library names or library paths. |
| 6165 | |
| 6166 | These libraries will be linked into the final binary (executable or shared |
| 6167 | library) containing the current target. |
| 6168 | |
| 6169 | libs and lib_dirs work differently than other flags in two respects. |
Joe Armstrong | 23d22bc | 2019-06-28 15:31:03 +0800 | [diff] [blame] | 6170 | First, they are inherited across static library boundaries until a |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6171 | shared library or executable target is reached. Second, they are |
| 6172 | uniquified so each one is only passed once (the first instance of it |
| 6173 | will be the one used). |
| 6174 | ``` |
| 6175 | |
| 6176 | #### **Types of libs** |
| 6177 | |
| 6178 | ``` |
| 6179 | There are several different things that can be expressed in libs: |
| 6180 | |
| 6181 | File paths |
| 6182 | Values containing '/' will be treated as references to files in the |
| 6183 | checkout. They will be rebased to be relative to the build directory and |
| 6184 | specified in the "libs" for linker tools. This facility should be used |
| 6185 | for libraries that are checked in to the version control. For libraries |
| 6186 | that are generated by the build, use normal GN deps to link them. |
| 6187 | |
| 6188 | System libraries |
| 6189 | Values not containing '/' will be treated as system library names. These |
| 6190 | will be passed unmodified to the linker and prefixed with the |
| 6191 | "lib_switch" attribute of the linker tool. Generally you would set the |
| 6192 | "lib_dirs" so the given library is found. Your BUILD.gn file should not |
| 6193 | specify the switch (like "-l"): this will be encoded in the "lib_switch" |
| 6194 | of the tool. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6195 | ``` |
| 6196 | |
| 6197 | #### **Ordering of flags and values** |
| 6198 | |
| 6199 | ``` |
| 6200 | 1. Those set on the current target (not in a config). |
| 6201 | 2. Those set on the "configs" on the target in order that the |
| 6202 | configs appear in the list. |
| 6203 | 3. Those set on the "all_dependent_configs" on the target in order |
| 6204 | that the configs appear in the list. |
| 6205 | 4. Those set on the "public_configs" on the target in order that |
| 6206 | those configs appear in the list. |
| 6207 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 6208 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 6209 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6210 | 6. public_configs pulled from dependencies, in the order of the |
| 6211 | "deps" list. If a dependency is public, they will be applied |
| 6212 | recursively. |
| 6213 | |
| 6214 | For "libs" and "lib_dirs" only, the values propagated from |
| 6215 | dependencies (as described above) are applied last assuming they |
| 6216 | are not already in the list. |
| 6217 | ``` |
| 6218 | |
| 6219 | #### **Examples** |
| 6220 | |
| 6221 | ``` |
| 6222 | On Windows: |
| 6223 | libs = [ "ctl3d.lib" ] |
| 6224 | |
| 6225 | On Linux: |
| 6226 | libs = [ "ld" ] |
| 6227 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6228 | ### <a name="var_metadata"></a>**metadata**: Metadata of this target. [Back to Top](#gn-reference) |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6229 | |
| 6230 | ``` |
| 6231 | Metadata is a collection of keys and values relating to a particular target. |
Julie Hockett | d69a9c3 | 2019-01-23 14:36:18 -0800 | [diff] [blame] | 6232 | Values must be lists, allowing for sane and predictable collection behavior. |
| 6233 | Generally, these keys will include three types of lists: lists of ordinary |
| 6234 | strings, lists of filenames intended to be rebased according to their |
| 6235 | particular source directory, and lists of target labels intended to be used |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 6236 | as barriers to the walk. Verification of these categories occurs at walk time, |
Julie Hockett | d69a9c3 | 2019-01-23 14:36:18 -0800 | [diff] [blame] | 6237 | not creation time (since it is not clear until the walk which values are |
| 6238 | intended for which purpose). |
| 6239 | ``` |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6240 | |
Julie Hockett | d69a9c3 | 2019-01-23 14:36:18 -0800 | [diff] [blame] | 6241 | #### **Example** |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6242 | |
Julie Hockett | d69a9c3 | 2019-01-23 14:36:18 -0800 | [diff] [blame] | 6243 | ``` |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6244 | group("doom_melon") { |
| 6245 | metadata = { |
| 6246 | # These keys are not built in to GN but are interpreted when consuming |
| 6247 | # metadata. |
| 6248 | my_barrier = [] |
| 6249 | my_files = [ "a.txt", "b.txt" ] |
| 6250 | } |
| 6251 | } |
| 6252 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6253 | ### <a name="var_mnemonic"></a>**mnemonic**: [string] Prefix displayed when ninja runs this action. [Back to Top](#gn-reference) |
Joseph Ryan | ad1e5ce | 2023-04-26 13:48:01 -0700 | [diff] [blame] | 6254 | |
| 6255 | ``` |
| 6256 | Tools in GN can set their ninja "description" which is displayed when |
| 6257 | building a target. These are commonly set with the format "CXX $output" |
| 6258 | or "LINK $label". By default, all GN actions will have the description |
| 6259 | "ACTION $label". Setting a mnemonic will override the "ACTION" prefix |
| 6260 | with another string, but the label will still be unconditionally displayed. |
| 6261 | |
| 6262 | Whitespace is not allowed within a mnemonic. |
| 6263 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6264 | ### <a name="var_module_name"></a>**module_name**: [string] The name for the compiled module. [Back to Top](#gn-reference) |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 6265 | |
| 6266 | ``` |
| 6267 | Valid for binary targets that contain Swift sources. |
| 6268 | |
| 6269 | If module_name is not set, then this rule will use the target name. |
| 6270 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6271 | ### <a name="var_output_conversion"></a>**output_conversion**: Data format for generated_file targets. [Back to Top](#gn-reference) |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6272 | |
| 6273 | ``` |
| 6274 | Controls how the "contents" of a generated_file target is formatted. |
Sylvain Defresne | 72d5a6e | 2021-06-15 17:01:32 +0200 | [diff] [blame] | 6275 | See `gn help io_conversion`. |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6276 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6277 | ### <a name="var_output_dir"></a>**output_dir**: [directory] Directory to put output file in. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6278 | |
| 6279 | ``` |
| 6280 | For library and executable targets, overrides the directory for the final |
| 6281 | output. This must be in the root_build_dir or a child thereof. |
| 6282 | |
| 6283 | This should generally be in the root_out_dir or a subdirectory thereof (the |
| 6284 | root_out_dir will be the same as the root_build_dir for the default |
| 6285 | toolchain, and will be a subdirectory for other toolchains). Not putting the |
| 6286 | output in a subdirectory of root_out_dir can result in collisions between |
| 6287 | different toolchains, so you will need to take steps to ensure that your |
| 6288 | target is only present in one toolchain. |
| 6289 | |
| 6290 | Normally the toolchain specifies the output directory for libraries and |
| 6291 | executables (see "gn help tool"). You will have to consult that for the |
| 6292 | default location. The default location will be used if output_dir is |
| 6293 | undefined or empty. |
| 6294 | ``` |
| 6295 | |
| 6296 | #### **Example** |
| 6297 | |
| 6298 | ``` |
| 6299 | shared_library("doom_melon") { |
| 6300 | output_dir = "$root_out_dir/plugin_libs" |
| 6301 | ... |
| 6302 | } |
| 6303 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6304 | ### <a name="var_output_extension"></a>**output_extension**: Value to use for the output's file extension. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6305 | |
| 6306 | ``` |
| 6307 | Normally the file extension for a target is based on the target type and the |
| 6308 | operating system, but in rare cases you will need to override the name (for |
| 6309 | example to use "libfreetype.so.6" instead of libfreetype.so on Linux). |
| 6310 | |
| 6311 | This value should not include a leading dot. If undefined, the default |
| 6312 | specified on the tool will be used. If set to the empty string, no output |
| 6313 | extension will be used. |
| 6314 | |
| 6315 | The output_extension will be used to set the "{{output_extension}}" expansion |
| 6316 | which the linker tool will generally use to specify the output file name. See |
| 6317 | "gn help tool". |
| 6318 | ``` |
| 6319 | |
| 6320 | #### **Example** |
| 6321 | |
| 6322 | ``` |
| 6323 | shared_library("freetype") { |
| 6324 | if (is_linux) { |
| 6325 | # Call the output "libfreetype.so.6" |
| 6326 | output_extension = "so.6" |
| 6327 | } |
| 6328 | ... |
| 6329 | } |
| 6330 | |
| 6331 | # On Windows, generate a "mysettings.cpl" control panel applet. Control panel |
| 6332 | # applets are actually special shared libraries. |
| 6333 | if (is_win) { |
| 6334 | shared_library("mysettings") { |
| 6335 | output_extension = "cpl" |
| 6336 | ... |
| 6337 | } |
| 6338 | } |
| 6339 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6340 | ### <a name="var_output_name"></a>**output_name**: Define a name for the output file other than the default. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6341 | |
| 6342 | ``` |
| 6343 | Normally the output name of a target will be based on the target name, so the |
| 6344 | target "//foo/bar:bar_unittests" will generate an output file such as |
| 6345 | "bar_unittests.exe" (using Windows as an example). |
| 6346 | |
| 6347 | Sometimes you will want an alternate name to avoid collisions or if the |
| 6348 | internal name isn't appropriate for public distribution. |
| 6349 | |
| 6350 | The output name should have no extension or prefixes, these will be added |
| 6351 | using the default system rules. For example, on Linux an output name of "foo" |
| 6352 | will produce a shared library "libfoo.so". There is no way to override the |
| 6353 | output prefix of a linker tool on a per- target basis. If you need more |
| 6354 | flexibility, create a copy target to produce the file you want. |
| 6355 | |
| 6356 | This variable is valid for all binary output target types. |
| 6357 | ``` |
| 6358 | |
| 6359 | #### **Example** |
| 6360 | |
| 6361 | ``` |
| 6362 | static_library("doom_melon") { |
| 6363 | output_name = "fluffy_bunny" |
| 6364 | } |
| 6365 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6366 | ### <a name="var_output_prefix_override"></a>**output_prefix_override**: Don't use prefix for output name. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6367 | |
| 6368 | ``` |
| 6369 | A boolean that overrides the output prefix for a target. Defaults to false. |
| 6370 | |
| 6371 | Some systems use prefixes for the names of the final target output file. The |
| 6372 | normal example is "libfoo.so" on Linux for a target named "foo". |
| 6373 | |
| 6374 | The output prefix for a given target type is specified on the linker tool |
| 6375 | (see "gn help tool"). Sometimes this prefix is undesired. |
| 6376 | |
| 6377 | See also "gn help output_extension". |
| 6378 | ``` |
| 6379 | |
| 6380 | #### **Example** |
| 6381 | |
| 6382 | ``` |
| 6383 | shared_library("doom_melon") { |
| 6384 | # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag |
| 6385 | # will produce "doom_melon.so". |
| 6386 | output_prefix_override = true |
| 6387 | ... |
| 6388 | } |
| 6389 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6390 | ### <a name="var_outputs"></a>**outputs**: Output files for actions and copy targets. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6391 | |
| 6392 | ``` |
| 6393 | Outputs is valid for "copy", "action", and "action_foreach" target types and |
| 6394 | indicates the resulting files. Outputs must always refer to files in the |
| 6395 | build directory. |
| 6396 | |
| 6397 | copy |
| 6398 | Copy targets should have exactly one entry in the outputs list. If there is |
| 6399 | exactly one source, this can be a literal file name or a source expansion. |
| 6400 | If there is more than one source, this must contain a source expansion to |
| 6401 | map a single input name to a single output name. See "gn help copy". |
| 6402 | |
| 6403 | action_foreach |
| 6404 | Action_foreach targets must always use source expansions to map input files |
| 6405 | to output files. There can be more than one output, which means that each |
| 6406 | invocation of the script will produce a set of files (presumably based on |
| 6407 | the name of the input file). See "gn help action_foreach". |
| 6408 | |
| 6409 | action |
| 6410 | Action targets (excluding action_foreach) must list literal output file(s) |
| 6411 | with no source expansions. See "gn help action". |
| 6412 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6413 | ### <a name="var_partial_info_plist"></a>**partial_info_plist**: [filename] Path plist from asset catalog compiler. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6414 | |
| 6415 | ``` |
| 6416 | Valid for create_bundle target, corresponds to the path for the partial |
| 6417 | Info.plist created by the asset catalog compiler that needs to be merged |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 6418 | with the application Info.plist (usually done by the post-processing script). |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6419 | |
| 6420 | The file will be generated regardless of whether the asset compiler has |
| 6421 | been invoked or not. See "gn help create_bundle". |
| 6422 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6423 | ### <a name="var_pool"></a>**pool**: Label of the pool used by binary targets actions. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6424 | |
| 6425 | ``` |
Petr Hosek | 7a6231e | 2022-10-22 23:14:18 +0000 | [diff] [blame] | 6426 | A fully-qualified label representing the pool that will be used for binary |
| 6427 | targets and actions. Pools are defined using the pool() {...} declaration. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6428 | ``` |
| 6429 | |
| 6430 | #### **Example** |
| 6431 | |
| 6432 | ``` |
Petr Hosek | 7a6231e | 2022-10-22 23:14:18 +0000 | [diff] [blame] | 6433 | executable("binary") { |
| 6434 | pool = "//build:custom_pool" |
| 6435 | ... |
| 6436 | } |
| 6437 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6438 | action("action") { |
| 6439 | pool = "//build:custom_pool" |
| 6440 | ... |
| 6441 | } |
| 6442 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6443 | ### <a name="var_post_processing_args"></a>**post_processing_args**: [string list] Args for the post-processing script. [Back to Top](#gn-reference) |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 6444 | |
| 6445 | ``` |
| 6446 | For create_bundle targets, post_processing_args is the list of arguments to |
| 6447 | pass to the post-processing script. Typically you would use source expansion |
| 6448 | (see "gn help source_expansion") to insert the source file names. |
| 6449 | |
| 6450 | See also "gn help create_bundle". |
| 6451 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6452 | ### <a name="var_post_processing_outputs"></a>**post_processing_outputs**: [file list] Outputs of the post-processing step. [Back to Top](#gn-reference) |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 6453 | |
| 6454 | ``` |
| 6455 | Outputs from the post-processing step of a create_bundle target. Must refer to |
| 6456 | files in the build directory. |
| 6457 | |
| 6458 | See also "gn help create_bundle". |
| 6459 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6460 | ### <a name="var_post_processing_script"></a>**post_processing_script**: [file name] Script for the post-processing step." [Back to Top](#gn-reference) |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 6461 | |
| 6462 | ``` |
| 6463 | An absolute or buildfile-relative file name of a Python script to run for a |
| 6464 | create_bundle target to perform the post-processing step. |
| 6465 | |
| 6466 | See also "gn help create_bundle". |
| 6467 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6468 | ### <a name="var_post_processing_sources"></a>**post_processing_sources**: [file list] Sources for the post-processing step. [Back to Top](#gn-reference) |
Sylvain Defresne | f07499a | 2024-04-04 13:06:37 +0200 | [diff] [blame] | 6469 | |
| 6470 | ``` |
| 6471 | A list of files used as input for the post-processing step of a create_bundle |
| 6472 | target. Non-absolute paths will be resolved relative to the current build |
| 6473 | file. |
| 6474 | |
| 6475 | See also "gn help create_bundle". |
| 6476 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6477 | ### <a name="var_precompiled_header"></a>**precompiled_header**: [string] Header file to precompile. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6478 | |
| 6479 | ``` |
| 6480 | Precompiled headers will be used when a target specifies this value, or a |
| 6481 | config applying to this target specifies this value. In addition, the tool |
| 6482 | corresponding to the source files must also specify precompiled headers (see |
| 6483 | "gn help tool"). The tool will also specify what type of precompiled headers |
| 6484 | to use, by setting precompiled_header_type to either "gcc" or "msvc". |
| 6485 | |
| 6486 | The precompiled header/source variables can be specified on a target or a |
| 6487 | config, but must be the same for all configs applying to a given target since |
| 6488 | a target can only have one precompiled header. |
| 6489 | |
| 6490 | If you use both C and C++ sources, the precompiled header and source file |
| 6491 | will be compiled once per language. You will want to make sure to wrap C++ |
| 6492 | includes in __cplusplus #ifdefs so the file will compile in C mode. |
| 6493 | ``` |
| 6494 | |
| 6495 | #### **GCC precompiled headers** |
| 6496 | |
| 6497 | ``` |
| 6498 | When using GCC-style precompiled headers, "precompiled_source" contains the |
| 6499 | path of a .h file that is precompiled and then included by all source files |
| 6500 | in targets that set "precompiled_source". |
| 6501 | |
| 6502 | The value of "precompiled_header" is not used with GCC-style precompiled |
| 6503 | headers. |
| 6504 | ``` |
| 6505 | |
| 6506 | #### **MSVC precompiled headers** |
| 6507 | |
| 6508 | ``` |
| 6509 | When using MSVC-style precompiled headers, the "precompiled_header" value is |
| 6510 | a string corresponding to the header. This is NOT a path to a file that GN |
| 6511 | recognises, but rather the exact string that appears in quotes after |
| 6512 | an #include line in source code. The compiler will match this string against |
| 6513 | includes or forced includes (/FI). |
| 6514 | |
| 6515 | MSVC also requires a source file to compile the header with. This must be |
| 6516 | specified by the "precompiled_source" value. In contrast to the header value, |
| 6517 | this IS a GN-style file name, and tells GN which source file to compile to |
| 6518 | make the .pch file used for subsequent compiles. |
| 6519 | |
| 6520 | For example, if the toolchain specifies MSVC headers: |
| 6521 | |
| 6522 | toolchain("vc_x64") { |
| 6523 | ... |
| 6524 | tool("cxx") { |
| 6525 | precompiled_header_type = "msvc" |
| 6526 | ... |
| 6527 | |
| 6528 | You might make a config like this: |
| 6529 | |
| 6530 | config("use_precompiled_headers") { |
| 6531 | precompiled_header = "build/precompile.h" |
| 6532 | precompiled_source = "//build/precompile.cc" |
| 6533 | |
| 6534 | # Either your source files should #include "build/precompile.h" |
| 6535 | # first, or you can do this to force-include the header. |
| 6536 | cflags = [ "/FI$precompiled_header" ] |
| 6537 | } |
| 6538 | |
| 6539 | And then define a target that uses the config: |
| 6540 | |
| 6541 | executable("doom_melon") { |
| 6542 | configs += [ ":use_precompiled_headers" ] |
| 6543 | ... |
| 6544 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6545 | ### <a name="var_precompiled_header_type"></a>**precompiled_header_type**: [string] "gcc" or "msvc". [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6546 | |
| 6547 | ``` |
| 6548 | See "gn help precompiled_header". |
| 6549 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6550 | ### <a name="var_precompiled_source"></a>**precompiled_source**: [file name] Source file to precompile. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6551 | |
| 6552 | ``` |
| 6553 | The source file that goes along with the precompiled_header when using |
| 6554 | "msvc"-style precompiled headers. It will be implicitly added to the sources |
| 6555 | of the target. See "gn help precompiled_header". |
| 6556 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6557 | ### <a name="var_product_type"></a>**product_type**: [string] Product type for the bundle. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6558 | |
| 6559 | ``` |
Sylvain Defresne | e5aee7b | 2023-06-08 10:08:10 -0400 | [diff] [blame] | 6560 | Valid for "create_bundle" and "bundle_data" targets. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6561 | |
Sylvain Defresne | e5aee7b | 2023-06-08 10:08:10 -0400 | [diff] [blame] | 6562 | Correspond to the type of the bundle. Used by transparent "create_bundle" |
| 6563 | target to control whether a "bundle_data" needs to be propagated or not. |
| 6564 | |
| 6565 | When generating Xcode project, the product_type is propagated and only |
| 6566 | "create_bundle" with a non-empty product_type will have a corresponding |
| 6567 | target in the project. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6568 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6569 | ### <a name="var_public"></a>**public**: Declare public header files for a target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6570 | |
| 6571 | ``` |
| 6572 | A list of files that other targets can include. These permissions are checked |
| 6573 | via the "check" command (see "gn help check"). |
| 6574 | |
| 6575 | If no public files are declared, other targets (assuming they have visibility |
| 6576 | to depend on this target) can include any file in the sources list. If this |
| 6577 | variable is defined on a target, dependent targets may only include files on |
Dirk Pranke | ed1abc1 | 2025-01-10 17:41:03 -0800 | [diff] [blame] | 6578 | this allowlist unless that target is marked as a friend (see "gn help |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6579 | friend"). |
| 6580 | |
| 6581 | Header file permissions are also subject to visibility. A target must be |
| 6582 | visible to another target to include any files from it at all and the public |
| 6583 | headers indicate which subset of those files are permitted. See "gn help |
| 6584 | visibility" for more. |
| 6585 | |
| 6586 | Public files are inherited through the dependency tree. So if there is a |
| 6587 | dependency A -> B -> C, then A can include C's public headers. However, the |
| 6588 | same is NOT true of visibility, so unless A is in C's visibility list, the |
| 6589 | include will be rejected. |
| 6590 | |
| 6591 | GN only knows about files declared in the "sources" and "public" sections of |
| 6592 | targets. If a file is included that is not known to the build, it will be |
| 6593 | allowed. |
| 6594 | |
| 6595 | It is common for test targets to need to include private headers for their |
| 6596 | associated code. In this case, list the test target in the "friend" list of |
| 6597 | the target that owns the private header to allow the inclusion. See |
| 6598 | "gn help friend" for more. |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6599 | |
| 6600 | When a binary target has no explicit or implicit public headers (a "public" |
| 6601 | list is defined but is empty), GN assumes that the target can not propagate |
| 6602 | any compile-time dependencies up the dependency tree. In this case, the build |
| 6603 | can be parallelized more efficiently. |
| 6604 | Say there are dependencies: |
| 6605 | A (shared library) -> B (shared library) -> C (action). |
| 6606 | Normally C must complete before any source files in A can compile (because |
| 6607 | there might be generated includes). But when B explicitly declares no public |
| 6608 | headers, C can execute in parallel with A's compile steps. C must still be |
| 6609 | complete before any dependents link. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6610 | ``` |
| 6611 | |
| 6612 | #### **Examples** |
| 6613 | |
| 6614 | ``` |
| 6615 | These exact files are public: |
| 6616 | public = [ "foo.h", "bar.h" ] |
| 6617 | |
| 6618 | No files are public (no targets may include headers from this one): |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6619 | # This allows starting compilation in dependent targets earlier. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6620 | public = [] |
| 6621 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6622 | ### <a name="var_public_configs"></a>**public_configs**: Configs to be applied on dependents. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6623 | |
| 6624 | ``` |
| 6625 | A list of config labels. |
| 6626 | |
| 6627 | Targets directly depending on this one will have the configs listed in this |
| 6628 | variable added to them. These configs will also apply to the current target. |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6629 | Generally, public configs are used to apply defines and include directories |
| 6630 | necessary to compile this target's header files. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6631 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6632 | See also "gn help all_dependent_configs". |
| 6633 | ``` |
| 6634 | |
| 6635 | #### **Propagation of public configs** |
| 6636 | |
| 6637 | ``` |
| 6638 | Public configs are applied to all targets that depend directly on this one. |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 6639 | These dependent targets can further push this target's public configs |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6640 | higher in the dependency tree by depending on it via public_deps (see "gn |
| 6641 | help public_deps"). |
| 6642 | |
| 6643 | static_library("toplevel") { |
| 6644 | # This target will get "my_config" applied to it. However, since this |
| 6645 | # target uses "deps" and not "public_deps", targets that depend on this |
| 6646 | # one won't get it. |
| 6647 | deps = [ ":intermediate" ] |
| 6648 | } |
| 6649 | |
| 6650 | static_library("intermediate") { |
| 6651 | # Depending on "lower" in any way will apply "my_config" to this target. |
| 6652 | # Additionall, since this target depends on "lower" via public_deps, |
| 6653 | # targets that depend on this one will also get "my_config". |
| 6654 | public_deps = [ ":lower" ] |
| 6655 | } |
| 6656 | |
| 6657 | static_library("lower") { |
| 6658 | # This will get applied to all targets that depend on this one. |
| 6659 | public_configs = [ ":my_config" ] |
| 6660 | } |
| 6661 | |
| 6662 | Public config propagation happens in a second phase once a target and all of |
| 6663 | its dependencies have been resolved. Therefore, a target will not see these |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6664 | force-added configs in their "configs" variable while the script is running, |
| 6665 | and they can not be removed. As a result, this capability should generally |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6666 | only be used to add defines and include directories rather than setting |
| 6667 | complicated flags that some targets may not want. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6668 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6669 | Public configs may or may not be propagated across toolchain boundaries |
| 6670 | depending on the value of the propagates_configs flag (see "gn help |
| 6671 | toolchain") on the toolchain of the target declaring the public_config. |
| 6672 | ``` |
| 6673 | |
| 6674 | #### **Avoiding applying public configs to this target** |
| 6675 | |
| 6676 | ``` |
| 6677 | If you want the config to apply to targets that depend on this one, but NOT |
| 6678 | this one, define an extra layer of indirection using a group: |
| 6679 | |
| 6680 | # External targets depend on this group. |
| 6681 | group("my_target") { |
| 6682 | # Config to apply to all targets that depend on this one. |
| 6683 | public_configs = [ ":external_settings" ] |
| 6684 | deps = [ ":internal_target" ] |
| 6685 | } |
| 6686 | |
| 6687 | # Internal target to actually compile the sources. |
| 6688 | static_library("internal_target") { |
| 6689 | # Force all external targets to depend on the group instead of directly |
| 6690 | # on this so the "external_settings" config will get applied. |
| 6691 | visibility = [ ":my_target" ] |
| 6692 | ... |
| 6693 | } |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6694 | ``` |
| 6695 | |
| 6696 | #### **Ordering of flags and values** |
| 6697 | |
| 6698 | ``` |
| 6699 | 1. Those set on the current target (not in a config). |
| 6700 | 2. Those set on the "configs" on the target in order that the |
| 6701 | configs appear in the list. |
| 6702 | 3. Those set on the "all_dependent_configs" on the target in order |
| 6703 | that the configs appear in the list. |
| 6704 | 4. Those set on the "public_configs" on the target in order that |
| 6705 | those configs appear in the list. |
| 6706 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 6707 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 6708 | more than once, only the first occurrence will be used. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6709 | 6. public_configs pulled from dependencies, in the order of the |
| 6710 | "deps" list. If a dependency is public, they will be applied |
| 6711 | recursively. |
| 6712 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6713 | ### <a name="var_public_deps"></a>**public_deps**: Declare public dependencies. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6714 | |
| 6715 | ``` |
| 6716 | Public dependencies are like private dependencies (see "gn help deps") but |
| 6717 | additionally express that the current target exposes the listed deps as part |
| 6718 | of its public API. |
| 6719 | |
| 6720 | This has several ramifications: |
| 6721 | |
| 6722 | - public_configs that are part of the dependency are forwarded to direct |
| 6723 | dependents. |
| 6724 | |
| 6725 | - Public headers in the dependency are usable by dependents (includes do |
| 6726 | not require a direct dependency or visibility). |
| 6727 | |
| 6728 | - If the current target is a shared library, other shared libraries that it |
| 6729 | publicly depends on (directly or indirectly) are propagated up the |
| 6730 | dependency tree to dependents for linking. |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 6731 | |
| 6732 | See also "gn help public_configs". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6733 | ``` |
| 6734 | |
| 6735 | #### **Discussion** |
| 6736 | |
| 6737 | ``` |
| 6738 | Say you have three targets: A -> B -> C. C's visibility may allow B to depend |
| 6739 | on it but not A. Normally, this would prevent A from including any headers |
| 6740 | from C, and C's public_configs would apply only to B. |
| 6741 | |
| 6742 | If B lists C in its public_deps instead of regular deps, A will now inherit |
| 6743 | C's public_configs and the ability to include C's public headers. |
| 6744 | |
| 6745 | Generally if you are writing a target B and you include C's headers as part |
| 6746 | of B's public headers, or targets depending on B should consider B and C to |
| 6747 | be part of a unit, you should use public_deps instead of deps. |
| 6748 | ``` |
| 6749 | |
| 6750 | #### **Example** |
| 6751 | |
| 6752 | ``` |
| 6753 | # This target can include files from "c" but not from |
| 6754 | # "super_secret_implementation_details". |
| 6755 | executable("a") { |
| 6756 | deps = [ ":b" ] |
| 6757 | } |
| 6758 | |
| 6759 | shared_library("b") { |
| 6760 | deps = [ ":super_secret_implementation_details" ] |
| 6761 | public_deps = [ ":c" ] |
| 6762 | } |
| 6763 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6764 | ### <a name="var_rebase"></a>**rebase**: Rebase collected metadata as files. [Back to Top](#gn-reference) |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6765 | |
| 6766 | ``` |
| 6767 | A boolean that triggers a rebase of collected metadata strings based on their |
| 6768 | declared file. Defaults to false. |
| 6769 | |
| 6770 | Metadata generally declares files as strings relative to the local build file. |
| 6771 | However, this data is often used in other contexts, and so setting this flag |
| 6772 | will force the metadata collection to be rebased according to the local build |
| 6773 | file's location and thus allow the filename to be used anywhere. |
| 6774 | |
| 6775 | Setting this flag will raise an error if any target's specified metadata is |
| 6776 | not a string value. |
| 6777 | |
| 6778 | See also "gn help generated_file". |
| 6779 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6780 | ### <a name="var_response_file_contents"></a>**response_file_contents**: Contents of a response file for actions. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6781 | |
| 6782 | ``` |
| 6783 | Sometimes the arguments passed to a script can be too long for the system's |
| 6784 | command-line capabilities. This is especially the case on Windows where the |
| 6785 | maximum command-line length is less than 8K. A response file allows you to |
| 6786 | pass an unlimited amount of data to a script in a temporary file for an |
| 6787 | action or action_foreach target. |
| 6788 | |
| 6789 | If the response_file_contents variable is defined and non-empty, the list |
| 6790 | will be treated as script args (including possibly substitution patterns) |
| 6791 | that will be written to a temporary file at build time. The name of the |
| 6792 | temporary file will be substituted for "{{response_file_name}}" in the script |
| 6793 | args. |
| 6794 | |
| 6795 | The response file contents will always be quoted and escaped according to |
| 6796 | Unix shell rules. To parse the response file, the Python script should use |
| 6797 | "shlex.split(file_contents)". |
| 6798 | ``` |
| 6799 | |
| 6800 | #### **Example** |
| 6801 | |
| 6802 | ``` |
| 6803 | action("process_lots_of_files") { |
| 6804 | script = "process.py", |
| 6805 | inputs = [ ... huge list of files ... ] |
| 6806 | |
| 6807 | # Write all the inputs to a response file for the script. Also, |
| 6808 | # make the paths relative to the script working directory. |
| 6809 | response_file_contents = rebase_path(inputs, root_build_dir) |
| 6810 | |
| 6811 | # The script expects the name of the response file in --file-list. |
| 6812 | args = [ |
| 6813 | "--enable-foo", |
| 6814 | "--file-list={{response_file_name}}", |
| 6815 | ] |
| 6816 | } |
| 6817 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6818 | ### <a name="var_rustflags"></a>**rustflags**: Flags passed to the Rust compiler. [Back to Top](#gn-reference) |
Max Ihlenfeldt | 5110a7f | 2024-01-22 11:56:47 +0100 | [diff] [blame] | 6819 | |
| 6820 | ``` |
| 6821 | A list of strings. |
| 6822 | |
| 6823 | "rustflags" are passed to all invocations of the Rust compiler. |
| 6824 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6825 | ### <a name="var_script"></a>**script**: Script file for actions. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6826 | |
| 6827 | ``` |
| 6828 | An absolute or buildfile-relative file name of a Python script to run for a |
| 6829 | action and action_foreach targets (see "gn help action" and "gn help |
| 6830 | action_foreach"). |
| 6831 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6832 | ### <a name="var_sources"></a>**sources**: Source files for a target [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6833 | |
| 6834 | ``` |
| 6835 | A list of files. Non-absolute paths will be resolved relative to the current |
| 6836 | build file. |
| 6837 | ``` |
| 6838 | |
| 6839 | #### **Sources for binary targets** |
| 6840 | |
| 6841 | ``` |
| 6842 | For binary targets (source sets, executables, and libraries), the known file |
| 6843 | types will be compiled with the associated tools. Unknown file types and |
| 6844 | headers will be skipped. However, you should still list all C/C+ header files |
| 6845 | so GN knows about the existence of those files for the purposes of include |
| 6846 | checking. |
| 6847 | |
| 6848 | As a special case, a file ending in ".def" will be treated as a Windows |
| 6849 | module definition file. It will be appended to the link line with a |
| 6850 | preceding "/DEF:" string. There must be at most one .def file in a target |
| 6851 | and they do not cross dependency boundaries (so specifying a .def file in a |
| 6852 | static library or source set will have no effect on the executable or shared |
| 6853 | library they're linked into). |
Julie Hockett | ce1fa07 | 2019-05-07 17:44:37 -0700 | [diff] [blame] | 6854 | |
| 6855 | For Rust targets that do not specify a crate_root, then the crate_root will |
| 6856 | look for a lib.rs file (or main.rs for executable) or a single file in |
| 6857 | sources, if sources contains only one file. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6858 | ``` |
| 6859 | |
| 6860 | #### **Sources for non-binary targets** |
| 6861 | |
| 6862 | ``` |
| 6863 | action_foreach |
| 6864 | The sources are the set of files that the script will be executed over. The |
| 6865 | script will run once per file. |
| 6866 | |
| 6867 | action |
| 6868 | The sources will be treated the same as inputs. See "gn help inputs" for |
| 6869 | more information and usage advice. |
| 6870 | |
| 6871 | copy |
| 6872 | The source are the source files to copy. |
| 6873 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6874 | ### <a name="var_swiftflags"></a>**swiftflags**: Flags passed to the swift compiler. [Back to Top](#gn-reference) |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 6875 | |
| 6876 | ``` |
| 6877 | A list of strings. |
| 6878 | |
| 6879 | "swiftflags" are passed to any invocation of a tool that takes an .swift |
| 6880 | file as input. |
| 6881 | ``` |
| 6882 | |
| 6883 | #### **Ordering of flags and values** |
| 6884 | |
| 6885 | ``` |
| 6886 | 1. Those set on the current target (not in a config). |
| 6887 | 2. Those set on the "configs" on the target in order that the |
| 6888 | configs appear in the list. |
| 6889 | 3. Those set on the "all_dependent_configs" on the target in order |
| 6890 | that the configs appear in the list. |
| 6891 | 4. Those set on the "public_configs" on the target in order that |
| 6892 | those configs appear in the list. |
| 6893 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 6894 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 6895 | more than once, only the first occurrence will be used. |
Sylvain Defresne | 89e6425 | 2020-08-07 13:01:06 +0200 | [diff] [blame] | 6896 | 6. public_configs pulled from dependencies, in the order of the |
| 6897 | "deps" list. If a dependency is public, they will be applied |
| 6898 | recursively. |
| 6899 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6900 | ### <a name="var_testonly"></a>**testonly**: Declares a target must only be used for testing. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6901 | |
| 6902 | ``` |
| 6903 | Boolean. Defaults to false. |
| 6904 | |
| 6905 | When a target is marked "testonly = true", it must only be depended on by |
| 6906 | other test-only targets. Otherwise, GN will issue an error that the |
| 6907 | depenedency is not allowed. |
| 6908 | |
| 6909 | This feature is intended to prevent accidentally shipping test code in a |
| 6910 | final product. |
| 6911 | ``` |
| 6912 | |
| 6913 | #### **Example** |
| 6914 | |
| 6915 | ``` |
| 6916 | source_set("test_support") { |
| 6917 | testonly = true |
| 6918 | ... |
| 6919 | } |
| 6920 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6921 | ### <a name="var_transparent"></a>**transparent**: [bool] True if the bundle is transparent. [Back to Top](#gn-reference) |
Sylvain Defresne | e5aee7b | 2023-06-08 10:08:10 -0400 | [diff] [blame] | 6922 | |
| 6923 | ``` |
| 6924 | A boolean. |
| 6925 | |
| 6926 | Valid for "create_bundle" target. If true, the "create_bundle" target will |
| 6927 | not package the "bundle_data" deps but will forward them to all targets that |
| 6928 | depends on it (unless the "bundle_data" target sets "product_type" to the |
| 6929 | same value as the "create_bundle" target). |
| 6930 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6931 | ### <a name="var_visibility"></a>**visibility**: A list of labels that can depend on a target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6932 | |
| 6933 | ``` |
| 6934 | A list of labels and label patterns that define which targets can depend on |
| 6935 | the current one. These permissions are checked via the "check" command (see |
| 6936 | "gn help check"). |
| 6937 | |
| 6938 | If visibility is not defined, it defaults to public ("*"). |
| 6939 | |
| 6940 | If visibility is defined, only the targets with labels that match it can |
| 6941 | depend on the current target. The empty list means no targets can depend on |
| 6942 | the current target. |
| 6943 | |
| 6944 | Tip: Often you will want the same visibility for all targets in a BUILD file. |
| 6945 | In this case you can just put the definition at the top, outside of any |
| 6946 | target, and the targets will inherit that scope and see the definition. |
| 6947 | ``` |
| 6948 | |
| 6949 | #### **Patterns** |
| 6950 | |
| 6951 | ``` |
| 6952 | See "gn help label_pattern" for more details on what types of patterns are |
| 6953 | supported. If a toolchain is specified, only targets in that toolchain will |
| 6954 | be matched. If a toolchain is not specified on a pattern, targets in all |
| 6955 | toolchains will be matched. |
| 6956 | ``` |
| 6957 | |
| 6958 | #### **Examples** |
| 6959 | |
| 6960 | ``` |
| 6961 | Only targets in the current buildfile ("private"): |
| 6962 | visibility = [ ":*" ] |
| 6963 | |
| 6964 | No targets (used for targets that should be leaf nodes): |
| 6965 | visibility = [] |
| 6966 | |
| 6967 | Any target ("public", the default): |
| 6968 | visibility = [ "*" ] |
| 6969 | |
| 6970 | All targets in the current directory and any subdirectory: |
| 6971 | visibility = [ "./*" ] |
| 6972 | |
| 6973 | Any target in "//bar/BUILD.gn": |
| 6974 | visibility = [ "//bar:*" ] |
| 6975 | |
| 6976 | Any target in "//bar/" or any subdirectory thereof: |
| 6977 | visibility = [ "//bar/*" ] |
| 6978 | |
| 6979 | Just these specific targets: |
| 6980 | visibility = [ ":mything", "//foo:something_else" ] |
| 6981 | |
| 6982 | Any target in the current directory and any subdirectory thereof, plus |
Petr Hosek | 7a6231e | 2022-10-22 23:14:18 +0000 | [diff] [blame] | 6983 | any targets in "//bar/" and any subdirectory thereof. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 6984 | visibility = [ "./*", "//bar/*" ] |
| 6985 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 6986 | ### <a name="var_walk_keys"></a>**walk_keys**: Key(s) for managing the metadata collection walk. [Back to Top](#gn-reference) |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6987 | |
| 6988 | ``` |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 6989 | Defaults to [""]. |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6990 | |
| 6991 | These keys are used to control the next step in a collection walk, acting as |
| 6992 | barriers. If a specified key is defined in a target's metadata, the walk will |
| 6993 | use the targets listed in that value to determine which targets are walked. |
| 6994 | |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 6995 | If no walk_keys are specified for a generated_file target (i.e. "[""]"), the |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 6996 | walk will touch all deps and data_deps of the specified target recursively. |
| 6997 | |
| 6998 | See "gn help generated_file". |
| 6999 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7000 | ### <a name="var_weak_frameworks"></a>**weak_frameworks**: [name list] Name of frameworks that must be weak linked. [Back to Top](#gn-reference) |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 7001 | |
| 7002 | ``` |
| 7003 | A list of framework names. |
| 7004 | |
| 7005 | The frameworks named in that list will be weak linked with any dynamic link |
| 7006 | type target. Weak linking instructs the dynamic loader to attempt to load |
| 7007 | the framework, but if it is not able to do so, it leaves any imported symbols |
| 7008 | unresolved. This is typically used when a framework is present in a new |
| 7009 | version of an SDK but not on older versions of the OS that the software runs |
| 7010 | on. |
| 7011 | ``` |
| 7012 | |
| 7013 | #### **Ordering of flags and values** |
| 7014 | |
| 7015 | ``` |
| 7016 | 1. Those set on the current target (not in a config). |
| 7017 | 2. Those set on the "configs" on the target in order that the |
| 7018 | configs appear in the list. |
| 7019 | 3. Those set on the "all_dependent_configs" on the target in order |
| 7020 | that the configs appear in the list. |
| 7021 | 4. Those set on the "public_configs" on the target in order that |
| 7022 | those configs appear in the list. |
| 7023 | 5. all_dependent_configs pulled from dependencies, in the order of |
| 7024 | the "deps" list. This is done recursively. If a config appears |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 7025 | more than once, only the first occurrence will be used. |
Robert Sesek | d0a6f07 | 2020-05-15 11:21:22 -0400 | [diff] [blame] | 7026 | 6. public_configs pulled from dependencies, in the order of the |
| 7027 | "deps" list. If a dependency is public, they will be applied |
| 7028 | recursively. |
| 7029 | ``` |
| 7030 | |
| 7031 | #### **Example** |
| 7032 | |
| 7033 | ``` |
| 7034 | weak_frameworks = [ "OnlyOnNewerOSes.framework" ] |
| 7035 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7036 | ### <a name="var_write_runtime_deps"></a>**write_runtime_deps**: Writes the target's runtime_deps to the given path. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7037 | |
| 7038 | ``` |
| 7039 | Does not synchronously write the file, but rather schedules it to be written |
| 7040 | at the end of generation. |
| 7041 | |
| 7042 | If the file exists and the contents are identical to that being written, the |
| 7043 | file will not be updated. This will prevent unnecessary rebuilds of targets |
| 7044 | that depend on this file. |
| 7045 | |
| 7046 | Path must be within the output directory. |
| 7047 | |
| 7048 | See "gn help runtime_deps" for how the runtime dependencies are computed. |
| 7049 | |
| 7050 | The format of this file will list one file per line with no escaping. The |
| 7051 | files will be relative to the root_build_dir. The first line of the file will |
| 7052 | be the main output file of the target itself. The file contents will be the |
| 7053 | same as requesting the runtime deps be written on the command line (see "gn |
| 7054 | help --runtime-deps-list-file"). |
| 7055 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7056 | ### <a name="var_xcasset_compiler_flags"></a>**xcasset_compiler_flags**: Flags passed to xcassets compiler. [Back to Top](#gn-reference) |
Harley Li | 0a9affb | 2020-06-03 10:38:42 -0400 | [diff] [blame] | 7057 | |
| 7058 | ``` |
| 7059 | A list of strings. |
| 7060 | |
| 7061 | Valid for create_bundle target. Those flags are directly passed to |
| 7062 | xcassets compiler, corresponding to {{xcasset_compiler_flags}} substitution |
| 7063 | in compile_xcassets tool. |
| 7064 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7065 | ### <a name="var_xcode_extra_attributes"></a>**xcode_extra_attributes**: [scope] Extra attributes for Xcode projects. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7066 | |
| 7067 | ``` |
| 7068 | The value defined in this scope will be copied to the EXTRA_ATTRIBUTES |
| 7069 | property of the generated Xcode project. They are only meaningful when |
| 7070 | generating with --ide=xcode. |
| 7071 | |
| 7072 | See "gn help create_bundle" for more information. |
| 7073 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7074 | ### <a name="var_xcode_test_application_name"></a>**xcode_test_application_name**: Name for Xcode test target. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7075 | |
| 7076 | ``` |
| 7077 | Each unit and ui test target must have a test application target, and this |
| 7078 | value is used to specify the relationship. Only meaningful to Xcode (used as |
| 7079 | part of the Xcode project generation). |
| 7080 | |
| 7081 | See "gn help create_bundle" for more information. |
| 7082 | ``` |
| 7083 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 7084 | #### **Example** |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7085 | |
| 7086 | ``` |
| 7087 | create_bundle("chrome_xctest") { |
| 7088 | test_application_name = "chrome" |
| 7089 | ... |
| 7090 | } |
| 7091 | ``` |
| 7092 | ## <a name="other"></a>Other help topics |
| 7093 | |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7094 | ### <a name="buildargs"></a>**Build Arguments Overview** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7095 | |
| 7096 | ``` |
| 7097 | Build arguments are variables passed in from outside of the build that build |
| 7098 | files can query to determine how the build works. |
| 7099 | ``` |
| 7100 | |
| 7101 | #### **How build arguments are set** |
| 7102 | |
| 7103 | ``` |
| 7104 | First, system default arguments are set based on the current system. The |
| 7105 | built-in arguments are: |
| 7106 | - host_cpu |
| 7107 | - host_os |
| 7108 | - current_cpu |
| 7109 | - current_os |
| 7110 | - target_cpu |
| 7111 | - target_os |
| 7112 | |
| 7113 | Next, project-specific overrides are applied. These are specified inside |
| 7114 | the default_args variable of //.gn. See "gn help dotfile" for more. |
| 7115 | |
| 7116 | If specified, arguments from the --args command line flag are used. If that |
| 7117 | flag is not specified, args from previous builds in the build directory will |
| 7118 | be used (this is in the file args.gn in the build directory). |
| 7119 | |
| 7120 | Last, for targets being compiled with a non-default toolchain, the toolchain |
| 7121 | overrides are applied. These are specified in the toolchain_args section of a |
| 7122 | toolchain definition. The use-case for this is that a toolchain may be |
| 7123 | building code for a different platform, and that it may want to always |
| 7124 | specify Posix, for example. See "gn help toolchain" for more. |
| 7125 | |
| 7126 | If you specify an override for a build argument that never appears in a |
| 7127 | "declare_args" call, a nonfatal error will be displayed. |
| 7128 | ``` |
| 7129 | |
| 7130 | #### **Examples** |
| 7131 | |
| 7132 | ``` |
| 7133 | gn args out/FooBar |
| 7134 | Create the directory out/FooBar and open an editor. You would type |
| 7135 | something like this into that file: |
| 7136 | enable_doom_melon=false |
| 7137 | os="android" |
| 7138 | |
| 7139 | gn gen out/FooBar --args="enable_doom_melon=true os=\"android\"" |
| 7140 | This will overwrite the build directory with the given arguments. (Note |
| 7141 | that the quotes inside the args command will usually need to be escaped |
| 7142 | for your shell to pass through strings values.) |
| 7143 | ``` |
| 7144 | |
| 7145 | #### **How build arguments are used** |
| 7146 | |
| 7147 | ``` |
| 7148 | If you want to use an argument, you use declare_args() and specify default |
| 7149 | values. These default values will apply if none of the steps listed in the |
| 7150 | "How build arguments are set" section above apply to the given argument, but |
| 7151 | the defaults will not override any of these. |
| 7152 | |
| 7153 | Often, the root build config file will declare global arguments that will be |
| 7154 | passed to all buildfiles. Individual build files can also specify arguments |
| 7155 | that apply only to those files. It is also useful to specify build args in an |
| 7156 | "import"-ed file if you want such arguments to apply to multiple buildfiles. |
| 7157 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7158 | ### <a name="dotfile"></a>**.gn file** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7159 | |
| 7160 | ``` |
| 7161 | When gn starts, it will search the current directory and parent directories |
| 7162 | for a file called ".gn". This indicates the source root. You can override |
| 7163 | this detection by using the --root command-line argument |
| 7164 | |
| 7165 | The .gn file in the source root will be executed. The syntax is the same as a |
| 7166 | buildfile, but with very limited build setup-specific meaning. |
| 7167 | |
| 7168 | If you specify --root, by default GN will look for the file .gn in that |
| 7169 | directory. If you want to specify a different file, you can additionally pass |
| 7170 | --dotfile: |
| 7171 | |
| 7172 | gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn |
| 7173 | ``` |
| 7174 | |
| 7175 | #### **Variables** |
| 7176 | |
| 7177 | ``` |
| 7178 | arg_file_template [optional] |
| 7179 | Path to a file containing the text that should be used as the default |
| 7180 | args.gn content when you run `gn args`. |
| 7181 | |
| 7182 | buildconfig [required] |
| 7183 | Path to the build config file. This file will be used to set up the |
| 7184 | build file execution environment for each toolchain. |
| 7185 | |
| 7186 | check_targets [optional] |
| 7187 | A list of labels and label patterns that should be checked when running |
Erik Staab | 5d109d7 | 2020-06-29 22:53:23 -0700 | [diff] [blame] | 7188 | "gn check" or "gn gen --check". If neither check_targets or |
| 7189 | no_check_targets (see below) is specified, all targets will be checked. |
| 7190 | It is an error to specify both check_targets and no_check_targets. If it |
| 7191 | is the empty list, no targets will be checked. To bypass this list, |
| 7192 | request an explicit check of targets, like "//*". |
| 7193 | |
| 7194 | The format of this list is identical to that of "visibility" so see "gn |
| 7195 | help visibility" for examples. |
| 7196 | |
| 7197 | no_check_targets [optional] |
| 7198 | A list of labels and label patterns that should *not* be checked when |
| 7199 | running "gn check" or "gn gen --check". All other targets will be checked. |
| 7200 | If neither check_targets (see above) or no_check_targets is specified, all |
| 7201 | targets will be checked. It is an error to specify both check_targets and |
| 7202 | no_check_targets. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7203 | |
| 7204 | The format of this list is identical to that of "visibility" so see "gn |
| 7205 | help visibility" for examples. |
| 7206 | |
James Robinson | 49f5903 | 2020-01-08 14:05:51 -0800 | [diff] [blame] | 7207 | check_system_includes [optional] |
| 7208 | Boolean to control whether system style includes are checked by default |
| 7209 | when running "gn check" or "gn gen --check". System style includes are |
| 7210 | includes that use angle brackets <> instead of double quotes "". If this |
| 7211 | setting is omitted or set to false, these includes will be ignored by |
| 7212 | default. They can be checked explicitly by running |
| 7213 | "gn check --check-system" or "gn gen --check=system" |
| 7214 | |
Dirk Pranke | ed1abc1 | 2025-01-10 17:41:03 -0800 | [diff] [blame] | 7215 | exec_script_allowlist [optional] |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7216 | A list of .gn/.gni files (not labels) that have permission to call the |
| 7217 | exec_script function. If this list is defined, calls to exec_script will |
| 7218 | be checked against this list and GN will fail if the current file isn't |
| 7219 | in the list. |
| 7220 | |
| 7221 | This is to allow the use of exec_script to be restricted since is easy to |
| 7222 | use inappropriately. Wildcards are not supported. Files in the |
| 7223 | secondary_source tree (if defined) should be referenced by ignoring the |
| 7224 | secondary tree and naming them as if they are in the main tree. |
| 7225 | |
| 7226 | If unspecified, the ability to call exec_script is unrestricted. |
| 7227 | |
| 7228 | Example: |
Dirk Pranke | ed1abc1 | 2025-01-10 17:41:03 -0800 | [diff] [blame] | 7229 | exec_script_allowlist = [ |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7230 | "//base/BUILD.gn", |
| 7231 | "//build/my_config.gni", |
| 7232 | ] |
| 7233 | |
Dirk Pranke | ed1abc1 | 2025-01-10 17:41:03 -0800 | [diff] [blame] | 7234 | exec_script_whitelist [optional] |
| 7235 | A synonym for "exec_script_allowlist" that exists for backwards |
| 7236 | compatibility. New code should use "exec_script_allowlist" instead. |
| 7237 | If both values are set, only the value in "exec_script_allowlist" will |
| 7238 | have any effect (so don't set both!). |
| 7239 | |
Brett Wilson | 1da84bb | 2022-09-14 15:35:29 -0700 | [diff] [blame] | 7240 | export_compile_commands [optional] |
| 7241 | A list of label patterns for which to generate a Clang compilation |
| 7242 | database (see "gn help label_pattern" for the string format). |
| 7243 | |
| 7244 | When specified, GN will generate a compile_commands.json file in the root |
| 7245 | of the build directory containing information on how to compile each |
| 7246 | source file reachable from any label matching any pattern in the list. |
| 7247 | This is used for Clang-based tooling and some editor integration. See |
| 7248 | https://clang.llvm.org/docs/JSONCompilationDatabase.html |
| 7249 | |
| 7250 | The switch --add-export-compile-commands to "gn gen" (see "gn help gen") |
| 7251 | appends to this value which provides a per-user way to customize it. |
| 7252 | |
| 7253 | The deprecated switch --export-compile-commands to "gn gen" (see "gn help |
| 7254 | gen") adds to the export target list using a different format. |
| 7255 | |
| 7256 | Example: |
| 7257 | export_compile_commands = [ |
| 7258 | "//base/*", |
| 7259 | "//tools:doom_melon", |
| 7260 | ] |
| 7261 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7262 | root [optional] |
| 7263 | Label of the root build target. The GN build will start by loading the |
| 7264 | build file containing this target name. This defaults to "//:" which will |
Harley Li | 0a9affb | 2020-06-03 10:38:42 -0400 | [diff] [blame] | 7265 | cause the file //BUILD.gn to be loaded. Note that build_file_extension |
| 7266 | applies to the default case as well. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7267 | |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 7268 | The command-line switch --root-target will override this value (see "gn |
| 7269 | help --root-target"). |
| 7270 | |
David 'Digit' Turner | b8562a4 | 2023-10-23 18:05:37 +0200 | [diff] [blame] | 7271 | root_patterns [optional] |
| 7272 | A list of label pattern strings. When not defined or empty, the GN build |
| 7273 | graph will contain all targets from any BUILD.gn evaluated in the default |
| 7274 | toolchain context, and their transitive dependencies. |
| 7275 | |
| 7276 | When set to a non empty list, only the targets in the default toolchain |
| 7277 | matching these patterns, and their transitive dependencies, will be defined |
| 7278 | instead. |
| 7279 | |
| 7280 | The command-line switch --root-pattern will override this value (see |
| 7281 | "gn help --root-pattern") |
| 7282 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7283 | script_executable [optional] |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 7284 | By default, GN runs the scripts used in action targets and exec_script |
| 7285 | calls using the Python interpreter found in PATH. This value specifies the |
| 7286 | Python executable or other interpreter to use instead. |
James Robinson | 61377e3 | 2020-02-13 15:20:07 -0800 | [diff] [blame] | 7287 | |
Brett Wilson | d0f8bc9 | 2022-04-22 14:01:40 -0700 | [diff] [blame] | 7288 | If set to the empty string, the scripts will be executed directly. |
| 7289 | |
| 7290 | The command-line switch --script-executable will override this value (see |
| 7291 | "gn help --script-executable") |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7292 | |
| 7293 | secondary_source [optional] |
| 7294 | Label of an alternate directory tree to find input files. When searching |
| 7295 | for a BUILD.gn file (or the build config file discussed above), the file |
| 7296 | will first be looked for in the source root. If it's not found, the |
| 7297 | secondary source root will be checked (which would contain a parallel |
| 7298 | directory hierarchy). |
| 7299 | |
| 7300 | This behavior is intended to be used when BUILD.gn files can't be checked |
| 7301 | in to certain source directories for whatever reason. |
| 7302 | |
| 7303 | The secondary source root must be inside the main source tree. |
| 7304 | |
| 7305 | default_args [optional] |
| 7306 | Scope containing the default overrides for declared arguments. These |
| 7307 | overrides take precedence over the default values specified in the |
Ricardo Ribalda | 09c9e5e | 2021-01-25 19:07:20 +0100 | [diff] [blame] | 7308 | declare_args() block, but can be overridden using --args or the |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7309 | args.gn file. |
| 7310 | |
| 7311 | This is intended to be used when subprojects declare arguments with |
| 7312 | default values that need to be changed for whatever reason. |
Harley Li | 0a9affb | 2020-06-03 10:38:42 -0400 | [diff] [blame] | 7313 | |
| 7314 | build_file_extension [optional] |
| 7315 | If set to a non-empty string, this is added to the name of all build files |
| 7316 | to load. |
| 7317 | GN will look for build files named "BUILD.$build_file_extension.gn". |
| 7318 | This is intended to be used during migrations or other situations where |
| 7319 | there are two independent GN builds in the same directories. |
Petr Hosek | 317fdc1 | 2020-05-28 10:23:42 -0700 | [diff] [blame] | 7320 | |
| 7321 | ninja_required_version [optional] |
| 7322 | When set specifies the minimum required version of Ninja. The default |
| 7323 | required version is 1.7.2. Specifying a higher version might enable the |
| 7324 | use of some of newer features that can make the build more efficient. |
Takuto Ikuta | 60a28b6 | 2024-09-05 17:45:42 +0900 | [diff] [blame] | 7325 | |
| 7326 | no_stamp_files [optional] |
| 7327 | A boolean flag that can be set to generate Ninja files that use phony |
| 7328 | rules instead of stamp files whenever possible. This results in smaller |
| 7329 | Ninja build plans, but requires at least Ninja 1.11. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7330 | ``` |
| 7331 | |
| 7332 | #### **Example .gn file contents** |
| 7333 | |
| 7334 | ``` |
| 7335 | buildconfig = "//build/config/BUILDCONFIG.gn" |
| 7336 | |
| 7337 | check_targets = [ |
| 7338 | "//doom_melon/*", # Check everything in this subtree. |
| 7339 | "//tools:mind_controlling_ant", # Check this specific target. |
| 7340 | ] |
| 7341 | |
| 7342 | root = "//:root" |
| 7343 | |
| 7344 | secondary_source = "//build/config/temporary_buildfiles/" |
| 7345 | |
| 7346 | default_args = { |
| 7347 | # Default to release builds for this project. |
| 7348 | is_debug = false |
| 7349 | is_component_build = false |
| 7350 | } |
| 7351 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7352 | ### <a name="execution"></a>**Build graph and execution overview** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7353 | |
| 7354 | #### **Overall build flow** |
| 7355 | |
| 7356 | ``` |
| 7357 | 1. Look for ".gn" file (see "gn help dotfile") in the current directory and |
| 7358 | walk up the directory tree until one is found. Set this directory to be |
| 7359 | the "source root" and interpret this file to find the name of the build |
| 7360 | config file. |
| 7361 | |
| 7362 | 2. Execute the build config file identified by .gn to set up the global |
| 7363 | variables and default toolchain name. Any arguments, variables, defaults, |
| 7364 | etc. set up in this file will be visible to all files in the build. |
| 7365 | |
| 7366 | 3. Load the //BUILD.gn (in the source root directory). |
| 7367 | |
| 7368 | 4. Recursively evaluate rules and load BUILD.gn in other directories as |
| 7369 | necessary to resolve dependencies. If a BUILD file isn't found in the |
| 7370 | specified location, GN will look in the corresponding location inside |
| 7371 | the secondary_source defined in the dotfile (see "gn help dotfile"). |
| 7372 | |
| 7373 | 5. When a target's dependencies are resolved, write out the `.ninja` |
| 7374 | file to disk. |
| 7375 | |
| 7376 | 6. When all targets are resolved, write out the root build.ninja file. |
Harley Li | 0a9affb | 2020-06-03 10:38:42 -0400 | [diff] [blame] | 7377 | |
| 7378 | Note that the BUILD.gn file name may be modulated by .gn arguments such as |
| 7379 | build_file_extension. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7380 | ``` |
| 7381 | |
| 7382 | #### **Executing target definitions and templates** |
| 7383 | |
| 7384 | ``` |
| 7385 | Build files are loaded in parallel. This means it is impossible to |
| 7386 | interrogate a target from GN code for any information not derivable from its |
| 7387 | label (see "gn help label"). The exception is the get_target_outputs() |
| 7388 | function which requires the target being interrogated to have been defined |
| 7389 | previously in the same file. |
| 7390 | |
| 7391 | Targets are declared by their type and given a name: |
| 7392 | |
| 7393 | static_library("my_static_library") { |
| 7394 | ... target parameter definitions ... |
| 7395 | } |
| 7396 | |
| 7397 | There is also a generic "target" function for programmatically defined types |
| 7398 | (see "gn help target"). You can define new types using templates (see "gn |
| 7399 | help template"). A template defines some custom code that expands to one or |
| 7400 | more other targets. |
| 7401 | |
| 7402 | Before executing the code inside the target's { }, the target defaults are |
| 7403 | applied (see "gn help set_defaults"). It will inject implicit variable |
| 7404 | definitions that can be overridden by the target code as necessary. Typically |
| 7405 | this mechanism is used to inject a default set of configs that define the |
| 7406 | global compiler and linker flags. |
| 7407 | ``` |
| 7408 | |
| 7409 | #### **Which targets are built** |
| 7410 | |
| 7411 | ``` |
| 7412 | All targets encountered in the default toolchain (see "gn help toolchain") |
| 7413 | will have build rules generated for them, even if no other targets reference |
| 7414 | them. Their dependencies must resolve and they will be added to the implicit |
| 7415 | "all" rule (see "gn help ninja_rules"). |
| 7416 | |
| 7417 | Targets in non-default toolchains will only be generated when they are |
| 7418 | required (directly or transitively) to build a target in the default |
| 7419 | toolchain. |
| 7420 | |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 7421 | Some targets might be associated but without a formal build dependency (for |
| 7422 | example, related tools or optional variants). A target that is marked as |
| 7423 | "generated" can propagate its generated state to an associated target using |
| 7424 | "gen_deps". This will make the referenced dependency have Ninja rules |
| 7425 | generated in the same cases the source target has but without a build-time |
| 7426 | dependency and even in non-default toolchains. |
| 7427 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7428 | See also "gn help ninja_rules". |
| 7429 | ``` |
| 7430 | |
| 7431 | #### **Dependencies** |
| 7432 | |
| 7433 | ``` |
| 7434 | The only difference between "public_deps" and "deps" except for pushing |
| 7435 | configs around the build tree and allowing includes for the purposes of "gn |
| 7436 | check". |
| 7437 | |
| 7438 | A target's "data_deps" are guaranteed to be built whenever the target is |
| 7439 | built, but the ordering is not defined. The meaning of this is dependencies |
| 7440 | required at runtime. Currently data deps will be complete before the target |
| 7441 | is linked, but this is not semantically guaranteed and this is undesirable |
| 7442 | from a build performance perspective. Since we hope to change this in the |
| 7443 | future, do not rely on this behavior. |
| 7444 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7445 | ### <a name="grammar"></a>**Language and grammar for GN build files** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7446 | |
| 7447 | #### **Tokens** |
| 7448 | |
| 7449 | ``` |
| 7450 | GN build files are read as sequences of tokens. While splitting the file |
| 7451 | into tokens, the next token is the longest sequence of characters that form a |
| 7452 | valid token. |
| 7453 | ``` |
| 7454 | |
| 7455 | #### **White space and comments** |
| 7456 | |
| 7457 | ``` |
| 7458 | White space is comprised of spaces (U+0020), horizontal tabs (U+0009), |
| 7459 | carriage returns (U+000D), and newlines (U+000A). |
| 7460 | |
| 7461 | Comments start at the character "#" and stop at the next newline. |
| 7462 | |
| 7463 | White space and comments are ignored except that they may separate tokens |
| 7464 | that would otherwise combine into a single token. |
| 7465 | ``` |
| 7466 | |
| 7467 | #### **Identifiers** |
| 7468 | |
| 7469 | ``` |
| 7470 | Identifiers name variables and functions. |
| 7471 | |
| 7472 | identifier = letter { letter | digit } . |
| 7473 | letter = "A" ... "Z" | "a" ... "z" | "_" . |
| 7474 | digit = "0" ... "9" . |
| 7475 | ``` |
| 7476 | |
| 7477 | #### **Keywords** |
| 7478 | |
| 7479 | ``` |
| 7480 | The following keywords are reserved and may not be used as identifiers: |
| 7481 | |
| 7482 | else false if true |
| 7483 | ``` |
| 7484 | |
| 7485 | #### **Integer literals** |
| 7486 | |
| 7487 | ``` |
| 7488 | An integer literal represents a decimal integer value. |
| 7489 | |
| 7490 | integer = [ "-" ] digit { digit } . |
| 7491 | |
| 7492 | Leading zeros and negative zero are disallowed. |
| 7493 | ``` |
| 7494 | |
| 7495 | #### **String literals** |
| 7496 | |
| 7497 | ``` |
| 7498 | A string literal represents a string value consisting of the quoted |
| 7499 | characters with possible escape sequences and variable expansions. |
| 7500 | |
| 7501 | string = `"` { char | escape | expansion } `"` . |
| 7502 | escape = `\` ( "$" | `"` | char ) . |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 7503 | BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" . |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7504 | Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f] |
| 7505 | expansion = "$" ( identifier | BracketExpansion | Hex ) . |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 7506 | char = /* any character except "$", `"`, or newline */ . |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7507 | |
| 7508 | After a backslash, certain sequences represent special characters: |
| 7509 | |
| 7510 | \" U+0022 quotation mark |
| 7511 | \$ U+0024 dollar sign |
| 7512 | \\ U+005C backslash |
| 7513 | |
| 7514 | All other backslashes represent themselves. |
| 7515 | |
| 7516 | To insert an arbitrary byte value, use $0xFF. For example, to insert a |
| 7517 | newline character: "Line one$0x0ALine two". |
| 7518 | |
| 7519 | An expansion will evaluate the variable following the '$' and insert a |
| 7520 | stringified version of it into the result. For example, to concat two path |
| 7521 | components with a slash separating them: |
| 7522 | "$var_one/$var_two" |
| 7523 | Use the "${var_one}" format to be explicitly deliniate the variable for |
| 7524 | otherwise-ambiguous cases. |
| 7525 | ``` |
| 7526 | |
| 7527 | #### **Punctuation** |
| 7528 | |
| 7529 | ``` |
| 7530 | The following character sequences represent punctuation: |
| 7531 | |
| 7532 | + += == != ( ) |
| 7533 | - -= < <= [ ] |
| 7534 | ! = > >= { } |
| 7535 | && || . , |
| 7536 | ``` |
| 7537 | |
| 7538 | #### **Grammar** |
| 7539 | |
| 7540 | ``` |
| 7541 | The input tokens form a syntax tree following a context-free grammar: |
| 7542 | |
| 7543 | File = StatementList . |
| 7544 | |
| 7545 | Statement = Assignment | Call | Condition . |
| 7546 | LValue = identifier | ArrayAccess | ScopeAccess . |
| 7547 | Assignment = LValue AssignOp Expr . |
| 7548 | Call = identifier "(" [ ExprList ] ")" [ Block ] . |
| 7549 | Condition = "if" "(" Expr ")" Block |
| 7550 | [ "else" ( Condition | Block ) ] . |
| 7551 | Block = "{" StatementList "}" . |
| 7552 | StatementList = { Statement } . |
| 7553 | |
| 7554 | ArrayAccess = identifier "[" Expr "]" . |
| 7555 | ScopeAccess = identifier "." identifier . |
| 7556 | Expr = UnaryExpr | Expr BinaryOp Expr . |
| 7557 | UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr . |
| 7558 | PrimaryExpr = identifier | integer | string | Call |
| 7559 | | ArrayAccess | ScopeAccess | Block |
| 7560 | | "(" Expr ")" |
| 7561 | | "[" [ ExprList [ "," ] ] "]" . |
| 7562 | ExprList = Expr { "," Expr } . |
| 7563 | |
| 7564 | AssignOp = "=" | "+=" | "-=" . |
| 7565 | UnaryOp = "!" . |
| 7566 | BinaryOp = "+" | "-" // highest priority |
| 7567 | | "<" | "<=" | ">" | ">=" |
| 7568 | | "==" | "!=" |
| 7569 | | "&&" |
| 7570 | | "||" . // lowest priority |
| 7571 | |
| 7572 | All binary operators are left-associative. |
| 7573 | ``` |
| 7574 | |
| 7575 | #### **Types** |
| 7576 | |
| 7577 | ``` |
| 7578 | The GN language is dynamically typed. The following types are used: |
| 7579 | |
| 7580 | - Boolean: Uses the keywords "true" and "false". There is no implicit |
| 7581 | conversion between booleans and integers. |
| 7582 | |
| 7583 | - Integers: All numbers in GN are signed 64-bit integers. |
| 7584 | |
| 7585 | - Strings: Strings are 8-bit with no enforced encoding. When a string is |
| 7586 | used to interact with other systems with particular encodings (like the |
| 7587 | Windows and Mac filesystems) it is assumed to be UTF-8. See "String |
| 7588 | literals" above for more. |
| 7589 | |
| 7590 | - Lists: Lists are arbitrary-length ordered lists of values. See "Lists" |
| 7591 | below for more. |
| 7592 | |
| 7593 | - Scopes: Scopes are like dictionaries that use variable names for keys. See |
| 7594 | "Scopes" below for more. |
| 7595 | ``` |
| 7596 | |
| 7597 | #### **Lists** |
| 7598 | |
| 7599 | ``` |
| 7600 | Lists are created with [] and using commas to separate items: |
| 7601 | |
| 7602 | mylist = [ 0, 1, 2, "some string" ] |
| 7603 | |
| 7604 | A comma after the last item is optional. Lists are dereferenced using 0-based |
| 7605 | indexing: |
| 7606 | |
| 7607 | mylist[0] += 1 |
| 7608 | var = mylist[2] |
| 7609 | |
| 7610 | Lists can be concatenated using the '+' and '+=' operators. Bare values can |
| 7611 | not be concatenated with lists, to add a single item, it must be put into a |
| 7612 | list of length one. |
| 7613 | |
| 7614 | Items can be removed from lists using the '-' and '-=' operators. This will |
| 7615 | remove all occurrences of every item in the right-hand list from the |
| 7616 | left-hand list. It is an error to remove an item not in the list. This is to |
| 7617 | prevent common typos and to detect dead code that is removing things that no |
| 7618 | longer apply. |
| 7619 | |
| 7620 | It is an error to use '=' to replace a nonempty list with another nonempty |
| 7621 | list. This is to prevent accidentally overwriting data when in most cases |
| 7622 | '+=' was intended. To overwrite a list on purpose, first assign it to the |
| 7623 | empty list: |
| 7624 | |
| 7625 | mylist = [] |
| 7626 | mylist = otherlist |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7627 | ``` |
| 7628 | |
| 7629 | #### **Scopes** |
| 7630 | |
| 7631 | ``` |
| 7632 | All execution happens in the context of a scope which holds the current state |
| 7633 | (like variables). With the exception of loops and conditions, '{' introduces |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 7634 | a new scope. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7635 | |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 7636 | Most scopes have a parent reference to the old scope. Variable reads |
| 7637 | recursively search all parent scopes until the variable is found or there are |
| 7638 | no more scopes. Variable writes always go into the current scope. This means |
| 7639 | that after the closing '}' (again excepting loops and conditions), all local |
| 7640 | variables will be restored to the previous values. This also means that "foo |
| 7641 | = foo" can do useful work by copying a variable into the current scope that |
| 7642 | was defined in a containing scope. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7643 | |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 7644 | Scopes can be assigned to variables. Examples of such scopes are the |
| 7645 | implicitly-created "invoker" when invoking a template (which refers to the |
| 7646 | variables set by the invoking code), scopes created by functions like |
| 7647 | exec_script, and scopes explicitly created like |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7648 | |
| 7649 | empty_scope = {} |
| 7650 | myvalues = { |
| 7651 | foo = 21 |
| 7652 | bar = "something" |
| 7653 | } |
| 7654 | |
Tyler Mandry | 4a64809 | 2022-02-15 19:47:09 +0000 | [diff] [blame] | 7655 | In the case of explicitly created scopes and scopes created by functions like |
| 7656 | exec_script, there is no reference to the parent scope. Such scopes are fully |
| 7657 | self-contained and do not "inherit" values from their defining scope. |
| 7658 | |
| 7659 | Inside an explicit scope definition can be any GN code including conditionals |
| 7660 | and function calls. After the close of the scope, it will contain all |
| 7661 | variables explicitly set by the code contained inside it. After this, the |
| 7662 | values can be read, modified, or added to: |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7663 | |
| 7664 | myvalues.foo += 2 |
| 7665 | empty_scope.new_thing = [ 1, 2, 3 ] |
Nate Fischer | 8ed01d3 | 2019-01-08 17:32:01 -0800 | [diff] [blame] | 7666 | |
| 7667 | Scope equality is defined as single-level scopes identical within the current |
| 7668 | scope. That is, all values in the first scope must be present and identical |
| 7669 | within the second, and vice versa. Note that this means inherited scopes are |
| 7670 | always unequal by definition. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7671 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7672 | ### <a name="io_conversion"></a>**Input and output conversion** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7673 | |
| 7674 | ``` |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 7675 | Input and output conversions are arguments to file and process functions |
| 7676 | that specify how to convert data to or from external formats. The possible |
| 7677 | values for parameters specifying conversions are: |
| 7678 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7679 | "" (the default) |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7680 | input: Discard the result and return None. |
| 7681 | |
| 7682 | output: If value is a list, then "list lines"; otherwise "value". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7683 | |
| 7684 | "list lines" |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7685 | input: |
| 7686 | Return the file contents as a list, with a string for each line. The |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 7687 | newlines will not be present in the result. The last line may or may |
| 7688 | not end in a newline. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7689 | |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7690 | After splitting, each individual line will be trimmed of whitespace on |
| 7691 | both ends. |
| 7692 | |
| 7693 | output: |
| 7694 | Renders the value contents as a list, with a string for each line. The |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 7695 | newlines will not be present in the result. The last line will end in |
| 7696 | with a newline. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7697 | |
| 7698 | "scope" |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7699 | input: |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 7700 | Execute the block as GN code and return a scope with the resulting |
| 7701 | values in it. If the input was: |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7702 | a = [ "hello.cc", "world.cc" ] |
| 7703 | b = 26 |
| 7704 | and you read the result into a variable named "val", then you could |
| 7705 | access contents the "." operator on "val": |
| 7706 | sources = val.a |
| 7707 | some_count = val.b |
| 7708 | |
| 7709 | output: |
| 7710 | Renders the value contents as a GN code block, reversing the input |
| 7711 | result above. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7712 | |
| 7713 | "string" |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7714 | input: Return the file contents into a single string. |
| 7715 | |
| 7716 | output: |
| 7717 | Render the value contents into a single string. The output is: |
| 7718 | a string renders with quotes, e.g. "str" |
| 7719 | an integer renders as a stringified integer, e.g. "6" |
| 7720 | a boolean renders as the associated string, e.g. "true" |
| 7721 | a list renders as a representation of its contents, e.g. "[\"str\", 6]" |
| 7722 | a scope renders as a GN code block of its values. If the Value was: |
| 7723 | Value val; |
| 7724 | val.a = [ "hello.cc", "world.cc" ]; |
| 7725 | val.b = 26 |
| 7726 | the resulting output would be: |
| 7727 | "{ |
| 7728 | a = [ \"hello.cc\", \"world.cc\" ] |
| 7729 | b = 26 |
| 7730 | }" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7731 | |
| 7732 | "value" |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7733 | input: |
| 7734 | Parse the input as if it was a literal rvalue in a buildfile. Examples of |
| 7735 | typical program output using this mode: |
| 7736 | [ "foo", "bar" ] (result will be a list) |
| 7737 | or |
| 7738 | "foo bar" (result will be a string) |
| 7739 | or |
| 7740 | 5 (result will be an integer) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7741 | |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7742 | Note that if the input is empty, the result will be a null value which |
| 7743 | will produce an error if assigned to a variable. |
| 7744 | |
| 7745 | output: |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 7746 | Render the value contents as a literal rvalue. Strings render with |
| 7747 | escaped quotes. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7748 | |
| 7749 | "json" |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7750 | input: Parse the input as a JSON and convert it to equivalent GN rvalue. |
| 7751 | |
| 7752 | output: Convert the Value to equivalent JSON value. |
| 7753 | |
| 7754 | The data type mapping is: |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7755 | a string in JSON maps to string in GN |
| 7756 | an integer in JSON maps to integer in GN |
| 7757 | a float in JSON is unsupported and will result in an error |
| 7758 | an object in JSON maps to scope in GN |
| 7759 | an array in JSON maps to list in GN |
| 7760 | a boolean in JSON maps to boolean in GN |
| 7761 | a null in JSON is unsupported and will result in an error |
| 7762 | |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7763 | Nota that the input dictionary keys have to be valid GN identifiers |
| 7764 | otherwise they will produce an error. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7765 | |
Julie Hockett | 0917129 | 2018-07-31 14:35:10 -0700 | [diff] [blame] | 7766 | "trim ..." (input only) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7767 | Prefixing any of the other transformations with the word "trim" will |
| 7768 | result in whitespace being trimmed from the beginning and end of the |
| 7769 | result before processing. |
| 7770 | |
| 7771 | Examples: "trim string" or "trim list lines" |
| 7772 | |
| 7773 | Note that "trim value" is useless because the value parser skips |
| 7774 | whitespace anyway. |
| 7775 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7776 | ### <a name="file_pattern"></a>**File patterns** [Back to Top](#gn-reference) |
Sylvain Defresne | aff489a | 2020-03-11 18:27:43 +0100 | [diff] [blame] | 7777 | |
| 7778 | ``` |
| 7779 | File patterns are VERY limited regular expressions. They must match the |
| 7780 | entire input string to be counted as a match. In regular expression parlance, |
| 7781 | there is an implicit "^...$" surrounding your input. If you want to match a |
| 7782 | substring, you need to use wildcards at the beginning and end. |
| 7783 | |
| 7784 | There are only two special tokens understood by the pattern matcher. |
| 7785 | Everything else is a literal. |
| 7786 | |
| 7787 | - "*" Matches zero or more of any character. It does not depend on the |
| 7788 | preceding character (in regular expression parlance it is equivalent to |
| 7789 | ".*"). |
| 7790 | |
| 7791 | - "\b" Matches a path boundary. This will match the beginning or end of a |
| 7792 | string, or a slash. |
| 7793 | ``` |
| 7794 | |
| 7795 | #### **Pattern examples** |
| 7796 | |
| 7797 | ``` |
| 7798 | "*asdf*" |
| 7799 | Matches a string containing "asdf" anywhere. |
| 7800 | |
| 7801 | "asdf" |
| 7802 | Matches only the exact string "asdf". |
| 7803 | |
| 7804 | "*.cc" |
| 7805 | Matches strings ending in the literal ".cc". |
| 7806 | |
| 7807 | "\bwin/*" |
| 7808 | Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo". |
| 7809 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7810 | ### <a name="label_pattern"></a>**Label patterns** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7811 | |
| 7812 | ``` |
| 7813 | A label pattern is a way of expressing one or more labels in a portion of the |
| 7814 | source tree. They are not general regular expressions. |
| 7815 | |
| 7816 | They can take the following forms only: |
| 7817 | |
| 7818 | - Explicit (no wildcard): |
| 7819 | "//foo/bar:baz" |
| 7820 | ":baz" |
| 7821 | |
| 7822 | - Wildcard target names: |
| 7823 | "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file) |
| 7824 | ":*" (all targets in the current build file) |
| 7825 | |
| 7826 | - Wildcard directory names ("*" is only supported at the end) |
| 7827 | "*" (all targets) |
| 7828 | "//foo/bar/*" (all targets in any subdir of //foo/bar) |
| 7829 | "./*" (all targets in the current build file or sub dirs) |
| 7830 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 7831 | Any of the above forms can additionally take an explicit toolchain |
| 7832 | in parenthesis at the end of the label pattern. In this case, the |
| 7833 | toolchain must be fully qualified (no wildcards are supported in the |
| 7834 | toolchain name). |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7835 | |
Julie Hockett | e2a2940 | 2018-07-31 10:11:42 -0700 | [diff] [blame] | 7836 | "//foo:bar(//build/toolchain:mac)" |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7837 | An explicit target in an explicit toolchain. |
| 7838 | |
| 7839 | ":*(//build/toolchain/linux:32bit)" |
| 7840 | All targets in the current build file using the 32-bit Linux toolchain. |
| 7841 | |
| 7842 | "//foo/*(//build/toolchain:win)" |
| 7843 | All targets in //foo and any subdirectory using the Windows |
| 7844 | toolchain. |
| 7845 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7846 | ### <a name="labels"></a>**About labels** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 7847 | |
| 7848 | ``` |
| 7849 | Everything that can participate in the dependency graph (targets, configs, |
| 7850 | and toolchains) are identified by labels. A common label looks like: |
| 7851 | |
| 7852 | //base/test:test_support |
| 7853 | |
| 7854 | This consists of a source-root-absolute path, a colon, and a name. This means |
| 7855 | to look for the thing named "test_support" in "base/test/BUILD.gn". |
| 7856 | |
| 7857 | You can also specify system absolute paths if necessary. Typically such |
| 7858 | paths would be specified via a build arg so the developer can specify where |
| 7859 | the component is on their system. |
| 7860 | |
| 7861 | /usr/local/foo:bar (Posix) |
| 7862 | /C:/Program Files/MyLibs:bar (Windows) |
| 7863 | ``` |
| 7864 | |
| 7865 | #### **Toolchains** |
| 7866 | |
| 7867 | ``` |
| 7868 | A canonical label includes the label of the toolchain being used. Normally, |
| 7869 | the toolchain label is implicitly inherited from the current execution |
| 7870 | context, but you can override this to specify cross-toolchain dependencies: |
| 7871 | |
| 7872 | //base/test:test_support(//build/toolchain/win:msvc) |
| 7873 | |
| 7874 | Here GN will look for the toolchain definition called "msvc" in the file |
| 7875 | "//build/toolchain/win" to know how to compile this target. |
| 7876 | ``` |
| 7877 | |
| 7878 | #### **Relative labels** |
| 7879 | |
| 7880 | ``` |
| 7881 | If you want to refer to something in the same buildfile, you can omit |
| 7882 | the path name and just start with a colon. This format is recommended for |
| 7883 | all same-file references. |
| 7884 | |
| 7885 | :base |
| 7886 | |
| 7887 | Labels can be specified as being relative to the current directory. |
| 7888 | Stylistically, we prefer to use absolute paths for all non-file-local |
| 7889 | references unless a build file needs to be run in different contexts (like a |
| 7890 | project needs to be both standalone and pulled into other projects in |
| 7891 | difference places in the directory hierarchy). |
| 7892 | |
| 7893 | source/plugin:myplugin |
| 7894 | ../net:url_request |
| 7895 | ``` |
| 7896 | |
| 7897 | #### **Implicit names** |
| 7898 | |
| 7899 | ``` |
| 7900 | If a name is unspecified, it will inherit the directory name. Stylistically, |
| 7901 | we prefer to omit the colon and name when possible: |
| 7902 | |
| 7903 | //net -> //net:net |
| 7904 | //tools/gn -> //tools/gn:gn |
| 7905 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 7906 | ### <a name="metadata_collection"></a>**Metadata Collection** [Back to Top](#gn-reference) |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7907 | |
| 7908 | ``` |
| 7909 | Metadata is information attached to targets throughout the dependency tree. GN |
| 7910 | allows for the collection of this data into files written during the generation |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 7911 | step, enabling users to expose and aggregate this data based on the dependency |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7912 | tree. |
| 7913 | ``` |
| 7914 | |
| 7915 | #### **generated_file targets** |
| 7916 | |
| 7917 | ``` |
| 7918 | Similar to the write_file() function, the generated_file target type |
| 7919 | creates a file in the specified location with the specified content. The |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 7920 | primary difference between write_file() and this target type is that the |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7921 | write_file function does the file write at parse time, while the |
| 7922 | generated_file target type writes at target resolution time. See |
| 7923 | "gn help generated_file" for more detail. |
| 7924 | |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 7925 | When written at target resolution time, generated_file enables GN to |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7926 | collect and write aggregated metadata from dependents. |
| 7927 | |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 7928 | A generated_file target can declare either 'contents' to write statically |
| 7929 | known contents to a file or 'data_keys' to aggregate metadata and write the |
| 7930 | result to a file. It can also specify 'walk_keys' (to restrict the metadata |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7931 | collection), 'output_conversion', and 'rebase'. |
| 7932 | ``` |
| 7933 | |
| 7934 | #### **Collection and Aggregation** |
| 7935 | |
| 7936 | ``` |
| 7937 | Targets can declare a 'metadata' variable containing a scope, and this |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 7938 | metadata may be collected and written out to a file specified by |
| 7939 | generated_file aggregation targets. The 'metadata' scope must contain |
| 7940 | only list values since the aggregation step collects a list of these values. |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7941 | |
| 7942 | During the target resolution, generated_file targets will walk their |
| 7943 | dependencies recursively, collecting metadata based on the specified |
| 7944 | 'data_keys'. 'data_keys' is specified as a list of strings, used by the walk |
| 7945 | to identify which variables in dependencies' 'metadata' scopes to collect. |
| 7946 | |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 7947 | The walk begins with the listed dependencies of the 'generated_file' target. |
| 7948 | The 'metadata' scope for each dependency is inspected for matching elements |
| 7949 | of the 'generated_file' target's 'data_keys' list. If a match is found, the |
| 7950 | data from the dependent's matching key list is appended to the aggregate walk |
| 7951 | list. Note that this means that if more than one walk key is specified, the |
| 7952 | data in all of them will be aggregated into one list. From there, the walk |
| 7953 | will then recurse into the dependencies of each target it encounters, |
| 7954 | collecting the specified metadata for each. |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7955 | |
| 7956 | For example: |
| 7957 | |
| 7958 | group("a") { |
| 7959 | metadata = { |
| 7960 | doom_melon = [ "enable" ] |
| 7961 | my_files = [ "foo.cpp" ] |
| 7962 | my_extra_files = [ "bar.cpp" ] |
| 7963 | } |
| 7964 | |
| 7965 | deps = [ ":b" ] |
| 7966 | } |
| 7967 | |
| 7968 | group("b") { |
| 7969 | metadata = { |
| 7970 | my_files = [ "baz.cpp" ] |
| 7971 | } |
| 7972 | } |
| 7973 | |
| 7974 | generated_file("metadata") { |
| 7975 | outputs = [ "$root_build_dir/my_files.json" ] |
| 7976 | data_keys = [ "my_files", "my_extra_files" ] |
| 7977 | |
| 7978 | deps = [ ":a" ] |
| 7979 | } |
| 7980 | |
| 7981 | The above will produce the following file data: |
| 7982 | |
| 7983 | foo.cpp |
| 7984 | bar.cpp |
| 7985 | baz.cpp |
| 7986 | |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 7987 | The dependency walk can be limited by using the 'walk_keys'. This is a list of |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7988 | labels that should be included in the walk. All labels specified here should |
| 7989 | also be in one of the deps lists. These keys act as barriers, where the walk |
Keir Mierle | 45611e3 | 2019-11-12 11:18:00 -0800 | [diff] [blame] | 7990 | will only recurse into the targets listed. An empty list in all specified |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7991 | barriers will end that portion of the walk. |
| 7992 | |
| 7993 | group("a") { |
| 7994 | metadata = { |
| 7995 | my_files = [ "foo.cpp" ] |
Sylvain Defresne | 3028c6a | 2020-07-20 11:28:59 +0200 | [diff] [blame] | 7996 | my_files_barrier = [ ":b" ] |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 7997 | } |
| 7998 | |
| 7999 | deps = [ ":b", ":c" ] |
| 8000 | } |
| 8001 | |
| 8002 | group("b") { |
| 8003 | metadata = { |
| 8004 | my_files = [ "bar.cpp" ] |
| 8005 | } |
| 8006 | } |
| 8007 | |
| 8008 | group("c") { |
| 8009 | metadata = { |
| 8010 | my_files = [ "doom_melon.cpp" ] |
| 8011 | } |
| 8012 | } |
| 8013 | |
| 8014 | generated_file("metadata") { |
| 8015 | outputs = [ "$root_build_dir/my_files.json" ] |
Sylvain Defresne | 3028c6a | 2020-07-20 11:28:59 +0200 | [diff] [blame] | 8016 | data_keys = [ "my_files" ] |
| 8017 | walk_keys = [ "my_files_barrier" ] |
Julie Hockett | 152c514 | 2019-07-12 09:53:43 -0600 | [diff] [blame] | 8018 | |
| 8019 | deps = [ ":a" ] |
| 8020 | } |
| 8021 | |
| 8022 | The above will produce the following file data (note that `doom_melon.cpp` is |
| 8023 | not included): |
| 8024 | |
| 8025 | foo.cpp |
| 8026 | bar.cpp |
| 8027 | |
| 8028 | A common example of this sort of barrier is in builds that have host tools |
| 8029 | built as part of the tree, but do not want the metadata from those host tools |
| 8030 | to be collected with the target-side code. |
| 8031 | ``` |
| 8032 | |
| 8033 | #### **Common Uses** |
| 8034 | |
| 8035 | ``` |
| 8036 | Metadata can be used to collect information about the different targets in the |
| 8037 | build, and so a common use is to provide post-build tooling with a set of data |
| 8038 | necessary to do aggregation tasks. For example, if each test target specifies |
| 8039 | the output location of its binary to run in a metadata field, that can be |
| 8040 | collected into a single file listing the locations of all tests in the |
| 8041 | dependency tree. A local build tool (or continuous integration infrastructure) |
| 8042 | can then use that file to know which tests exist, and where, and run them |
| 8043 | accordingly. |
| 8044 | |
| 8045 | Another use is in image creation, where a post-build image tool needs to know |
| 8046 | various pieces of information about the components it should include in order |
| 8047 | to put together the correct image. |
| 8048 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 8049 | ### <a name="ninja_rules"></a>**Ninja build rules** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8050 | |
| 8051 | #### **The "all" and "default" rules** |
| 8052 | |
| 8053 | ``` |
| 8054 | All generated targets (see "gn help execution") will be added to an implicit |
| 8055 | build rule called "all" so "ninja all" will always compile everything. The |
| 8056 | default rule will be used by Ninja if no specific target is specified (just |
| 8057 | typing "ninja"). If there is a target named "default" in the root build file, |
| 8058 | it will be the default build rule, otherwise the implicit "all" rule will be |
| 8059 | used. |
| 8060 | ``` |
| 8061 | |
| 8062 | #### **Phony rules** |
| 8063 | |
| 8064 | ``` |
| 8065 | GN generates Ninja "phony" rules for targets in the default toolchain. The |
| 8066 | phony rules can collide with each other and with the names of generated files |
| 8067 | so are generated with the following priority: |
| 8068 | |
| 8069 | 1. Actual files generated by the build always take precedence. |
| 8070 | |
| 8071 | 2. Targets in the toplevel //BUILD.gn file. |
| 8072 | |
| 8073 | 3. Targets in toplevel directories matching the names of the directories. |
| 8074 | So "ninja foo" can be used to compile "//foo:foo". This only applies to |
| 8075 | the first level of directories since usually these are the most |
| 8076 | important (so this won't apply to "//foo/bar:bar"). |
| 8077 | |
| 8078 | 4. The short names of executables if there is only one executable with that |
| 8079 | short name. Use "ninja doom_melon" to compile the |
| 8080 | "//tools/fruit:doom_melon" executable. |
| 8081 | |
Sylvain Defresne | 5e19d2f | 2022-12-12 18:20:36 +0100 | [diff] [blame] | 8082 | Note that for Apple platforms, create_bundle targets with a product_type |
| 8083 | of "com.apple.product-type.application" are considered as executable |
| 8084 | for this rule (as they define application bundles). |
| 8085 | |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8086 | 5. The short names of all targets if there is only one target with that |
| 8087 | short name. |
| 8088 | |
| 8089 | 6. Full label name with no leading slashes. So you can use |
| 8090 | "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon". |
| 8091 | |
| 8092 | 7. Labels with an implicit name part (when the short names match the |
| 8093 | directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar". |
| 8094 | |
| 8095 | These "phony" rules are provided only for running Ninja since this matches |
| 8096 | people's historical expectations for building. For consistency with the rest |
| 8097 | of the program, GN introspection commands accept explicit labels. |
| 8098 | |
| 8099 | To explicitly compile a target in a non-default toolchain, you must give |
| 8100 | Ninja the exact name of the output file relative to the build directory. |
| 8101 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 8102 | ### <a name="nogncheck"></a>**nogncheck**: Skip an include line from checking. [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8103 | |
| 8104 | ``` |
| 8105 | GN's header checker helps validate that the includes match the build |
| 8106 | dependency graph. Sometimes an include might be conditional or otherwise |
| 8107 | problematic, but you want to specifically allow it. In this case, it can be |
Dirk Pranke | ed1abc1 | 2025-01-10 17:41:03 -0800 | [diff] [blame] | 8108 | allowlisted. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8109 | |
| 8110 | Include lines containing the substring "nogncheck" will be excluded from |
| 8111 | header checking. The most common case is a conditional include: |
| 8112 | |
| 8113 | #if defined(ENABLE_DOOM_MELON) |
| 8114 | #include "tools/doom_melon/doom_melon.h" // nogncheck |
| 8115 | #endif |
| 8116 | |
| 8117 | If the build file has a conditional dependency on the corresponding target |
| 8118 | that matches the conditional include, everything will always link correctly: |
| 8119 | |
| 8120 | source_set("mytarget") { |
| 8121 | ... |
| 8122 | if (enable_doom_melon) { |
| 8123 | defines = [ "ENABLE_DOOM_MELON" ] |
| 8124 | deps += [ "//tools/doom_melon" ] |
| 8125 | } |
| 8126 | |
| 8127 | But GN's header checker does not understand preprocessor directives, won't |
| 8128 | know it matches the build dependencies, and will flag this include as |
| 8129 | incorrect when the condition is false. |
| 8130 | ``` |
| 8131 | |
| 8132 | #### **More information** |
| 8133 | |
| 8134 | ``` |
| 8135 | The topic "gn help check" has general information on how checking works and |
| 8136 | advice on fixing problems. Targets can also opt-out of checking, see |
| 8137 | "gn help check_includes". |
| 8138 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 8139 | ### <a name="runtime_deps"></a>**Runtime dependencies** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8140 | |
| 8141 | ``` |
| 8142 | Runtime dependencies of a target are exposed via the "runtime_deps" category |
| 8143 | of "gn desc" (see "gn help desc") or they can be written at build generation |
| 8144 | time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help |
| 8145 | --runtime-deps-list-file"). |
| 8146 | |
| 8147 | To a first approximation, the runtime dependencies of a target are the set of |
| 8148 | "data" files, data directories, and the shared libraries from all transitive |
| 8149 | dependencies. Executables, shared libraries, and loadable modules are |
| 8150 | considered runtime dependencies of themselves. |
| 8151 | ``` |
| 8152 | |
| 8153 | #### **Executables** |
| 8154 | |
| 8155 | ``` |
| 8156 | Executable targets and those executable targets' transitive dependencies are |
| 8157 | not considered unless that executable is listed in "data_deps". Otherwise, GN |
| 8158 | assumes that the executable (and everything it requires) is a build-time |
| 8159 | dependency only. |
| 8160 | ``` |
| 8161 | |
| 8162 | #### **Actions and copies** |
| 8163 | |
| 8164 | ``` |
| 8165 | Action and copy targets that are listed as "data_deps" will have all of their |
| 8166 | outputs and data files considered as runtime dependencies. Action and copy |
| 8167 | targets that are "deps" or "public_deps" will have only their data files |
| 8168 | considered as runtime dependencies. These targets can list an output file in |
| 8169 | both the "outputs" and "data" lists to force an output file as a runtime |
| 8170 | dependency in all cases. |
| 8171 | |
| 8172 | The different rules for deps and data_deps are to express build-time (deps) |
| 8173 | vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as |
| 8174 | data dependencies, there would be a lot of extra stuff, and if GN counted all |
| 8175 | run-time dependencies as regular deps, the build's parallelism would be |
| 8176 | unnecessarily constrained. |
| 8177 | |
| 8178 | This rule can sometimes lead to unintuitive results. For example, given the |
| 8179 | three targets: |
| 8180 | A --[data_deps]--> B --[deps]--> ACTION |
| 8181 | GN would say that A does not have runtime deps on the result of the ACTION, |
| 8182 | which is often correct. But the purpose of the B target might be to collect |
| 8183 | many actions into one logic unit, and the "data"-ness of A's dependency is |
| 8184 | lost. Solutions: |
| 8185 | |
| 8186 | - List the outputs of the action in its data section (if the results of |
| 8187 | that action are always runtime files). |
| 8188 | - Have B list the action in data_deps (if the outputs of the actions are |
| 8189 | always runtime files). |
| 8190 | - Have B list the action in both deps and data deps (if the outputs might be |
| 8191 | used in both contexts and you don't care about unnecessary entries in the |
| 8192 | list of files required at runtime). |
| 8193 | - Split B into run-time and build-time versions with the appropriate "deps" |
| 8194 | for each. |
| 8195 | ``` |
| 8196 | |
| 8197 | #### **Static libraries and source sets** |
| 8198 | |
| 8199 | ``` |
| 8200 | The results of static_library or source_set targets are not considered |
| 8201 | runtime dependencies since these are assumed to be intermediate targets only. |
| 8202 | If you need to list a static library as a runtime dependency, you can |
| 8203 | manually compute the .a/.lib file name for the current platform and list it |
| 8204 | in the "data" list of a target (possibly on the static library target |
| 8205 | itself). |
| 8206 | ``` |
| 8207 | |
| 8208 | #### **Multiple outputs** |
| 8209 | |
| 8210 | ``` |
| 8211 | Linker tools can specify which of their outputs should be considered when |
| 8212 | computing the runtime deps by setting runtime_outputs. If this is unset on |
| 8213 | the tool, the default will be the first output only. |
| 8214 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 8215 | ### <a name="source_expansion"></a>**How Source Expansion Works** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8216 | |
| 8217 | ``` |
| 8218 | Source expansion is used for the action_foreach and copy target types to map |
| 8219 | source file names to output file names or arguments. |
| 8220 | |
| 8221 | To perform source expansion in the outputs, GN maps every entry in the |
| 8222 | sources to every entry in the outputs list, producing the cross product of |
| 8223 | all combinations, expanding placeholders (see below). |
| 8224 | |
| 8225 | Source expansion in the args works similarly, but performing the placeholder |
| 8226 | substitution produces a different set of arguments for each invocation of the |
| 8227 | script. |
| 8228 | |
| 8229 | If no placeholders are found, the outputs or args list will be treated as a |
| 8230 | static list of literal file names that do not depend on the sources. |
| 8231 | |
| 8232 | See "gn help copy" and "gn help action_foreach" for more on how this is |
| 8233 | applied. |
| 8234 | ``` |
| 8235 | |
| 8236 | #### **Placeholders** |
| 8237 | |
| 8238 | ``` |
| 8239 | This section discusses only placeholders for actions. There are other |
| 8240 | placeholders used in the definition of tools. See "gn help tool" for those. |
| 8241 | |
| 8242 | {{source}} |
| 8243 | The name of the source file including directory (*). This will generally |
| 8244 | be used for specifying inputs to a script in the "args" variable. |
| 8245 | "//foo/bar/baz.txt" => "../../foo/bar/baz.txt" |
| 8246 | |
| 8247 | {{source_file_part}} |
| 8248 | The file part of the source including the extension. |
| 8249 | "//foo/bar/baz.txt" => "baz.txt" |
| 8250 | |
| 8251 | {{source_name_part}} |
| 8252 | The filename part of the source file with no directory or extension. This |
| 8253 | will generally be used for specifying a transformation from a source file |
| 8254 | to a destination file with the same name but different extension. |
| 8255 | "//foo/bar/baz.txt" => "baz" |
| 8256 | |
| 8257 | {{source_dir}} |
| 8258 | The directory (*) containing the source file with no trailing slash. |
| 8259 | "//foo/bar/baz.txt" => "../../foo/bar" |
| 8260 | |
| 8261 | {{source_root_relative_dir}} |
| 8262 | The path to the source file's directory relative to the source root, with |
| 8263 | no leading "//" or trailing slashes. If the path is system-absolute, |
| 8264 | (beginning in a single slash) this will just return the path with no |
| 8265 | trailing slash. This value will always be the same, regardless of whether |
| 8266 | it appears in the "outputs" or "args" section. |
| 8267 | "//foo/bar/baz.txt" => "foo/bar" |
| 8268 | |
| 8269 | {{source_gen_dir}} |
| 8270 | The generated file directory (*) corresponding to the source file's path. |
| 8271 | This will be different than the target's generated file directory if the |
| 8272 | source file is in a different directory than the BUILD.gn file. |
| 8273 | "//foo/bar/baz.txt" => "gen/foo/bar" |
| 8274 | |
| 8275 | {{source_out_dir}} |
| 8276 | The object file directory (*) corresponding to the source file's path, |
| 8277 | relative to the build directory. this us be different than the target's |
| 8278 | out directory if the source file is in a different directory than the |
| 8279 | build.gn file. |
| 8280 | "//foo/bar/baz.txt" => "obj/foo/bar" |
| 8281 | |
| 8282 | {{source_target_relative}} |
| 8283 | The path to the source file relative to the target's directory. This will |
| 8284 | generally be used for replicating the source directory layout in the |
Robert Sesek | 252674d | 2019-05-10 14:08:52 -0400 | [diff] [blame] | 8285 | output directory. This can only be used in actions and bundle_data |
| 8286 | targets. It is an error to use in process_file_template where there is no |
| 8287 | "target". |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8288 | "//foo/bar/baz.txt" => "baz.txt" |
| 8289 | ``` |
| 8290 | |
| 8291 | #### **(*) Note on directories** |
| 8292 | |
| 8293 | ``` |
| 8294 | Paths containing directories (except the source_root_relative_dir) will be |
| 8295 | different depending on what context the expansion is evaluated in. Generally |
| 8296 | it should "just work" but it means you can't concatenate strings containing |
| 8297 | these values with reasonable results. |
| 8298 | |
| 8299 | Details: source expansions can be used in the "outputs" variable, the "args" |
| 8300 | variable, and in calls to "process_file_template". The "args" are passed to a |
| 8301 | script which is run from the build directory, so these directories will |
| 8302 | relative to the build directory for the script to find. In the other cases, |
| 8303 | the directories will be source- absolute (begin with a "//") because the |
| 8304 | results of those expansions will be handled by GN internally. |
| 8305 | ``` |
| 8306 | |
| 8307 | #### **Examples** |
| 8308 | |
| 8309 | ``` |
| 8310 | Non-varying outputs: |
| 8311 | action("hardcoded_outputs") { |
| 8312 | sources = [ "input1.idl", "input2.idl" ] |
| 8313 | outputs = [ "$target_out_dir/output1.dat", |
| 8314 | "$target_out_dir/output2.dat" ] |
| 8315 | } |
| 8316 | The outputs in this case will be the two literal files given. |
| 8317 | |
| 8318 | Varying outputs: |
| 8319 | action_foreach("varying_outputs") { |
| 8320 | sources = [ "input1.idl", "input2.idl" ] |
| 8321 | outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h", |
| 8322 | "{{source_gen_dir}}/{{source_name_part}}.cc" ] |
| 8323 | } |
| 8324 | Performing source expansion will result in the following output names: |
| 8325 | //out/Debug/obj/mydirectory/input1.h |
| 8326 | //out/Debug/obj/mydirectory/input1.cc |
| 8327 | //out/Debug/obj/mydirectory/input2.h |
| 8328 | //out/Debug/obj/mydirectory/input2.cc |
| 8329 | ``` |
[email protected] | 449f3e4 | 2024-08-01 21:43:27 +0800 | [diff] [blame] | 8330 | ### <a name="switch_list"></a>**Available global switches** [Back to Top](#gn-reference) |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8331 | |
| 8332 | ``` |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 8333 | Do "gn help --the_switch_you_want_help_on" for more. Individual commands may |
| 8334 | take command-specific switches not listed here. See the help on your specific |
| 8335 | command for more. |
| 8336 | ``` |
| 8337 | ``` |
| 8338 | * --args: Specifies build arguments overrides. |
| 8339 | * --color: Force colored output. |
| 8340 | * --dotfile: Override the name of the ".gn" file. |
| 8341 | * --fail-on-unused-args: Treat unused build args as fatal errors. |
| 8342 | * --markdown: Write help output in the Markdown format. |
RJ Ascani | 6966efb | 2020-10-19 16:50:11 -0700 | [diff] [blame] | 8343 | * --ninja-executable: Set the Ninja executable. |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 8344 | * --nocolor: Force non-colored output. |
| 8345 | * -q: Quiet mode. Don't print output on success. |
| 8346 | * --root: Explicitly specify source root. |
Nico Weber | ad9eab2 | 2020-11-15 22:20:28 -0500 | [diff] [blame] | 8347 | * --root-target: Override the root target. |
Brett Wilson | b5a2ac6 | 2018-12-21 21:20:05 -0800 | [diff] [blame] | 8348 | * --runtime-deps-list-file: Save runtime dependencies for targets in file. |
| 8349 | * --script-executable: Set the executable used to execute scripts. |
| 8350 | * --threads: Specify number of worker threads. |
| 8351 | * --time: Outputs a summary of how long everything took. |
| 8352 | * --tracelog: Writes a Chrome-compatible trace log to the given file. |
| 8353 | * -v: Verbose logging. |
| 8354 | * --version: Prints the GN version number and exits. |
Brett Wilson | 796ed47 | 2018-07-16 15:11:09 -0700 | [diff] [blame] | 8355 | ``` |
| 8356 | |