blob: 93bc0af7f417f59b1a6aa09eb1e1695dc5a6a2bb [file] [log] [blame] [view]
Brett Wilson796ed472018-07-16 15:11:09 -07001# GN Reference
2
3*This page is automatically generated from* `gn help --markdown all`.
4
5## Contents
6
7* [Commands](#commands)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008 * [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 Ascani4e260f12020-10-19 17:41:51 -070012 * [clean_stale: Cleans the stale output files from the output directory.](#cmd_clean_stale)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080013 * [desc: Show lots of insightful information about a target or config.](#cmd_desc)
Julie Hockettd69a9c32019-01-23 14:36:18 -080014 * [format: Format .gn files.](#cmd_format)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080015 * [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 Wilson74397092020-03-18 16:34:14 -070019 * [outputs: Which files a source/target make.](#cmd_outputs)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080020 * [path: Find paths between two targets.](#cmd_path)
21 * [refs: Find stuff referencing a target or file.](#cmd_refs)
Brett Wilson796ed472018-07-16 15:11:09 -070022* [Target declarations](#targets)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080023 * [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 Hockettce1fa072019-05-07 17:44:37 -070032 * [rust_library: Declare a Rust library target.](#func_rust_library)
Petr Hosekfe36c7c2019-11-26 14:38:08 -080033 * [rust_proc_macro: Declare a Rust procedural macro target.](#func_rust_proc_macro)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080034 * [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 Sartoriecaaf4b2022-07-01 05:57:08 +000037 * [target: Declare a target with the given programmatic type.](#func_target)
Brett Wilson796ed472018-07-16 15:11:09 -070038* [Buildfile functions](#functions)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080039 * [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 Defresneaff489a2020-03-11 18:27:43 +010044 * [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 Woode05c0aa2024-02-14 13:40:53 -080046 * [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 Wilsonb5a2ac62018-12-21 21:20:05 -080048 * [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 Woode05c0aa2024-02-14 13:40:53 -080055 * [label_matches: Returns whether a label matches any of a list of patterns.](#func_label_matches)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080056 * [not_needed: Mark variables from scope as not needed.](#func_not_needed)
Andrew Grievef98b6d72025-02-25 08:54:51 -080057 * [path_exists: Returns whether the given path exists.](#func_path_exists)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080058 * [pool: Defines a pool object.](#func_pool)
59 * [print: Prints to the console.](#func_print)
Aaron Wood24062bb2022-04-25 20:37:48 -070060 * [print_stack_trace: Prints a stack trace.](#func_print_stack_trace)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080061 * [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 Wilsonb5a2ac62018-12-21 21:20:05 -080066 * [split_list: Splits a list into N different sub-lists.](#func_split_list)
Keir Mierle6ae63302019-11-08 23:02:18 -080067 * [string_join: Concatenates a list of strings with a separator.](#func_string_join)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080068 * [string_replace: Replaces substring in the given string.](#func_string_replace)
Keir Mierle6ae63302019-11-08 23:02:18 -080069 * [string_split: Split string into a list of strings.](#func_string_split)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080070 * [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 Wilson796ed472018-07-16 15:11:09 -070074* [Built-in predefined variables](#predefined_variables)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080075 * [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 Robinson61377e32020-02-13 15:20:07 -080079 * [gn_version: [number] The version of gn.](#var_gn_version)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080080 * [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 Wilson796ed472018-07-16 15:11:09 -070092* [Variables you set in targets](#target_variables)
Julie Hockettce1fa072019-05-07 17:44:37 -070093 * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080094 * [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 Defresne89e64252020-08-07 13:01:06 +0200100 * [bridge_header: [string] Path to C/Objective-C compatibility header.](#var_bridge_header)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800101 * [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 Wilsonb5a2ac62018-12-21 21:20:05 -0800104 * [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 Defresnef07499a2024-04-04 13:06:37 +0200112 * [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 Wilsonb5a2ac62018-12-21 21:20:05 -0800116 * [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 Hockettce1fa072019-05-07 17:44:37 -0700119 * [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 Wilsonb5a2ac62018-12-21 21:20:05 -0800122 * [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 Hoseka1413862020-01-03 12:54:33 -0800128 * [externs: [scope] Set of Rust crate-dependency pairs.](#var_externs)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100129 * [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 Wilsonb5a2ac62018-12-21 21:20:05 -0800131 * [friend: [label pattern list] Allow targets to include private headers.](#var_friend)
Tyler Mandry4a648092022-02-15 19:47:09 +0000132 * [gen_deps: [label list] Declares targets that should generate when this one does.](#var_gen_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800133 * [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 Ryanad1e5ce2023-04-26 13:48:01 -0700139 * [mnemonic: [string] Prefix displayed when ninja runs this action.](#var_mnemonic)
Sylvain Defresne89e64252020-08-07 13:01:06 +0200140 * [module_name: [string] The name for the compiled module.](#var_module_name)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800141 * [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 Hosek7a6231e2022-10-22 23:14:18 +0000148 * [pool: [string] Label of the pool used by binary targets and actions.](#var_pool)
Sylvain Defresnef07499a2024-04-04 13:06:37 +0200149 * [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 Wilsonb5a2ac62018-12-21 21:20:05 -0800153 * [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 Defresnee5aee7b2023-06-08 10:08:10 -0400156 * [product_type: [string] Product type for the bundle.](#var_product_type)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800157 * [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 Ihlenfeldt5110a7f2024-01-22 11:56:47 +0100162 * [rustflags: [string list] Flags passed to the Rust compiler.](#var_rustflags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800163 * [script: [file name] Script file for actions.](#var_script)
164 * [sources: [file list] Source files for a target.](#var_sources)
Sylvain Defresne89e64252020-08-07 13:01:06 +0200165 * [swiftflags: [string list] Flags passed to the swift compiler.](#var_swiftflags)
Julie Jeongeun Kimebc8f162025-05-19 11:45:26 +0900166 * [target_xcode_platform: [string] The desired platform for the build.](#var_target_xcode_platform)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800167 * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly)
Sylvain Defresnee5aee7b2023-06-08 10:08:10 -0400168 * [transparent: [bool] True if the bundle is transparent.](#var_transparent)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800169 * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility)
170 * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys)
Robert Sesekd0a6f072020-05-15 11:21:22 -0400171 * [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800172 * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps)
Harley Li0a9affb2020-06-03 10:38:42 -0400173 * [xcasset_compiler_flags: [string list] Flags passed to xcassets compiler](#var_xcasset_compiler_flags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800174 * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes)
175 * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name)
Brett Wilson796ed472018-07-16 15:11:09 -0700176* [Other help topics](#other)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800177 * all: Print all the help at once
Brett Wilson796ed472018-07-16 15:11:09 -0700178 * [buildargs: How build arguments work.](#buildargs)
179 * [dotfile: Info about the toplevel .gn file.](#dotfile)
180 * [execution: Build graph and execution overview.](#execution)
181 * [grammar: Language and grammar for GN build files.](#grammar)
Julie Hockett81ee1962019-05-13 11:24:42 -0700182 * [input_conversion: Processing input from exec_script and read_file.](#io_conversion)
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100183 * [file_pattern: Matching more than one file.](#file_pattern)
Brett Wilson796ed472018-07-16 15:11:09 -0700184 * [label_pattern: Matching more than one label.](#label_pattern)
185 * [labels: About labels.](#labels)
Julie Hockett152c5142019-07-12 09:53:43 -0600186 * [metadata_collection: About metadata and its collection.](#metadata_collection)
Brett Wilson796ed472018-07-16 15:11:09 -0700187 * [ninja_rules: How Ninja build rules are named.](#ninja_rules)
188 * [nogncheck: Annotating includes for checking.](#nogncheck)
Julie Hockett81ee1962019-05-13 11:24:42 -0700189 * [output_conversion: Specifies how to transform a value to output.](#io_conversion)
Brett Wilson796ed472018-07-16 15:11:09 -0700190 * [runtime_deps: How runtime dependency computation works.](#runtime_deps)
191 * [source_expansion: Map sources to outputs for scripts.](#source_expansion)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800192 * [switches: Show available command-line switches.](#switch_list)
Brett Wilson796ed472018-07-16 15:11:09 -0700193
194## Commands
195
[email protected]449f3e42024-08-01 21:43:27 +0800196### **gn analyze <out_dir> <input_path> <output_path>** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -0700197
198```
199 Analyze which targets are affected by a list of files.
200
201 This command takes three arguments:
202
203 out_dir is the path to the build directory.
204
205 input_path is a path to a file containing a JSON object with three fields:
206
207 - "files": A list of the filenames to check.
208
209 - "test_targets": A list of the labels for targets that are needed to run
210 the tests we wish to run.
211
Nico Weberdba01722021-04-06 08:56:46 -0400212 - "additional_compile_targets" (optional): A list of the labels for targets
213 that we wish to rebuild, but aren't necessarily needed for testing. The
214 important difference between this field and "test_targets" is that if an
215 item in the additional_compile_targets list refers to a group, then any
Brett Wilson796ed472018-07-16 15:11:09 -0700216 dependencies of that group will be returned if they are out of date, but
217 the group itself does not need to be. If the dependencies themselves are
218 groups, the same filtering is repeated. This filtering can be used to
219 avoid rebuilding dependencies of a group that are unaffected by the input
220 files. The list may also contain the string "all" to refer to a
221 pseudo-group that contains every root target in the build graph.
222
223 This filtering behavior is also known as "pruning" the list of compile
224 targets.
225
Nico Weberdba01722021-04-06 08:56:46 -0400226 If "additional_compile_targets" is absent, it defaults to the empty list.
227
Keir Mierle45611e32019-11-12 11:18:00 -0800228 If input_path is -, input is read from stdin.
229
Brett Wilson796ed472018-07-16 15:11:09 -0700230 output_path is a path indicating where the results of the command are to be
231 written. The results will be a file containing a JSON object with one or more
232 of following fields:
233
234 - "compile_targets": A list of the labels derived from the input
235 compile_targets list that are affected by the input files. Due to the way
236 the filtering works for compile targets as described above, this list may
237 contain targets that do not appear in the input list.
238
239 - "test_targets": A list of the labels from the input test_targets list that
240 are affected by the input files. This list will be a proper subset of the
241 input list.
242
243 - "invalid_targets": A list of any names from the input that do not exist in
244 the build graph. If this list is non-empty, the "error" field will also be
245 set to "Invalid targets".
246
247 - "status": A string containing one of three values:
248
249 - "Found dependency"
250 - "No dependency"
Sylvain Defresne7b0690e2020-06-26 15:33:01 +0200251 - "Found dependency (all)"
Brett Wilson796ed472018-07-16 15:11:09 -0700252
253 In the first case, the lists returned in compile_targets and test_targets
254 should be passed to ninja to build. In the second case, nothing was
255 affected and no build is necessary. In the third case, GN could not
256 determine the correct answer and returned the input as the output in order
257 to be safe.
258
259 - "error": This will only be present if an error occurred, and will contain
260 a string describing the error. This includes cases where the input file is
261 not in the right format, or contains invalid targets.
262
Keir Mierle45611e32019-11-12 11:18:00 -0800263 If output_path is -, output is written to stdout.
264
Brett Wilson796ed472018-07-16 15:11:09 -0700265 The command returns 1 if it is unable to read the input file or write the
266 output file, or if there is something wrong with the build such that gen
267 would also fail, and 0 otherwise. In particular, it returns 0 even if the
268 "error" key is non-empty and a non-fatal error occurred. In other words, it
269 tries really hard to always write something to the output JSON and convey
270 errors that way rather than via return codes.
271```
[email protected]449f3e42024-08-01 21:43:27 +0800272### **gn args**: (command-line tool) [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -0700273
274```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800275 Display or configure arguments declared by the build.
276
277 gn args [--list] [--short] [--args] [--overrides-only]
278
Brett Wilson796ed472018-07-16 15:11:09 -0700279 See also "gn help buildargs" for a more high-level overview of how
280 build arguments work.
281```
282
283#### **Usage**
284
285```
286 gn args
287 Open the arguments for the given build directory in an editor. If the
288 given build directory doesn't exist, it will be created and an empty args
289 file will be opened in the editor. You would type something like this
290 into that file:
291 enable_doom_melon=false
292 os="android"
293
294 To find your editor on Posix, GN will search the environment variables in
295 order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command
296 associated with .txt files.
297
298 Note: you can edit the build args manually by editing the file "args.gn"
299 in the build directory and then running "gn gen ".
300
301 gn args --list[=] [--short] [--overrides-only] [--json]
302 Lists all build arguments available in the current configuration, or, if
303 an exact_arg is specified for the list flag, just that one build
304 argument.
305
306 The output will list the declaration location, current value for the
307 build, default value (if different than the current value), and comment
308 preceding the declaration.
309
310 If --short is specified, only the names and current values will be
311 printed.
312
313 If --overrides-only is specified, only the names and current values of
314 arguments that have been overridden (i.e. non-default arguments) will
315 be printed. Overrides come from the /args.gn file and //.gn
316
317 If --json is specified, the output will be emitted in json format.
318 JSON schema for output:
319 [
320 {
321 "name": variable_name,
322 "current": {
323 "value": overridden_value,
324 "file": file_name,
325 "line": line_no
326 },
327 "default": {
328 "value": default_value,
329 "file": file_name,
330 "line": line_no
331 },
332 "comment": comment_string
333 },
334 ...
335 ]
336```
337
338#### **Examples**
339
340```
341 gn args out/Debug
342 Opens an editor with the args for out/Debug.
343
344 gn args out/Debug --list --short
345 Prints all arguments with their default values for the out/Debug
346 build.
347
348 gn args out/Debug --list --short --overrides-only
349 Prints overridden arguments for the out/Debug build.
350
351 gn args out/Debug --list=target_cpu
352 Prints information about the "target_cpu" argument for the "
353 "out/Debug
354 build.
355
356 gn args --list --args="os=\"android\" enable_doom_melon=true"
357 Prints all arguments with the default values for a build with the
358 given arguments set (which may affect the values of other
359 arguments).
360```
[email protected]449f3e42024-08-01 21:43:27 +0800361### **gn check <out_dir> [<label_pattern>] [\--force] [\--check-generated]** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -0700362
363```
364 GN's include header checker validates that the includes for C-like source
365 files match the build dependency graph.
366
367 "gn check" is the same thing as "gn gen" with the "--check" flag except that
368 this command does not write out any build files. It's intended to be an easy
369 way to manually trigger include file checking.
370
371 The can take exact labels or patterns that match more than
372 one (although not general regular expressions). If specified, only those
373 matching targets will be checked. See "gn help label_pattern" for details.
374```
375
376#### **Command-specific switches**
377
378```
Joe Armstrong39413942019-03-15 10:34:03 +0800379 --check-generated
380 Generated files are normally not checked since they do not exist
381 until after a build. With this flag, those generated files that
382 can be found on disk are also checked.
James Robinson49f59032020-01-08 14:05:51 -0800383
384 --check-system
385 Check system style includes (using ) in addition to
386 "double quote" includes.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400387
388 --default-toolchain
389 Normally wildcard targets are matched in all toolchains. This
390 switch makes wildcard labels with no explicit toolchain reference
391 only match targets in the default toolchain.
392
393 Non-wildcard inputs with no explicit toolchain specification will
394 always match only a target in the default toolchain if one exists.
395
396 --force
397 Ignores specifications of "check_includes = false" and checks all
398 target's files that match the target label.
Brett Wilson796ed472018-07-16 15:11:09 -0700399```
400
401#### **What gets checked**
402
403```
Nate Fischer8ed01d32019-01-08 17:32:01 -0800404 The .gn file may specify a list of targets to be checked in the list
Erik Staab5d109d72020-06-29 22:53:23 -0700405 check_targets (see "gn help dotfile"). Alternatively, the .gn file may
406 specify a list of targets not to be checked in no_check_targets. If a label
407 pattern is specified on the command line, neither check_targets or
408 no_check_targets is used.
Brett Wilson796ed472018-07-16 15:11:09 -0700409
410 Targets can opt-out from checking with "check_includes = false" (see
411 "gn help check_includes").
412
413 For targets being checked:
414
415 - GN opens all C-like source files in the targets to be checked and scans
416 the top for includes.
417
Joe Armstrong39413942019-03-15 10:34:03 +0800418 - Generated files (that might not exist yet) are ignored unless
419 the --check-generated flag is provided.
420
Brett Wilson796ed472018-07-16 15:11:09 -0700421 - Includes with a "nogncheck" annotation are skipped (see
422 "gn help nogncheck").
423
James Robinson49f59032020-01-08 14:05:51 -0800424 - Includes using "quotes" are always checked.
425 If system style checking is enabled, includes using
426 are also checked.
Brett Wilson796ed472018-07-16 15:11:09 -0700427
428 - Include paths are assumed to be relative to any of the "include_dirs" for
429 the target (including the implicit current dir).
430
431 - GN does not run the preprocessor so will not understand conditional
432 includes.
433
434 - Only includes matching known files in the build are checked: includes
435 matching unknown paths are ignored.
436
437 For an include to be valid:
438
439 - The included file must be in the current target, or there must be a path
440 following only public dependencies to a target with the file in it
441 ("gn path" is a good way to diagnose problems).
442
443 - There can be multiple targets with an included file: only one needs to be
444 valid for the include to be allowed.
445
446 - If there are only "sources" in a target, all are considered to be public
447 and can be included by other targets with a valid public dependency path.
448
449 - If a target lists files as "public", only those files are able to be
450 included by other targets. Anything in the sources will be considered
451 private and will not be includable regardless of dependency paths.
452
453 - Outputs from actions are treated like public sources on that target.
454
455 - A target can include headers from a target that depends on it if the
456 other target is annotated accordingly. See "gn help
457 allow_circular_includes_from".
458```
459
460#### **Advice on fixing problems**
461
462```
463 If you have a third party project that is difficult to fix or doesn't care
464 about include checks it's generally best to exclude that target from checking
465 altogether via "check_includes = false".
466
467 If you have conditional includes, make sure the build conditions and the
468 preprocessor conditions match, and annotate the line with "nogncheck" (see
469 "gn help nogncheck" for an example).
470
471 If two targets are hopelessly intertwined, use the
472 "allow_circular_includes_from" annotation. Ideally each should have identical
473 dependencies so configs inherited from those dependencies are consistent (see
474 "gn help allow_circular_includes_from").
475
476 If you have a standalone header file or files that need to be shared between
477 a few targets, you can consider making a source_set listing only those
478 headers as public sources. With only header files, the source set will be a
479 no-op from a build perspective, but will give a central place to refer to
480 those headers. That source set's files will still need to pass "gn check" in
481 isolation.
482
483 In rare cases it makes sense to list a header in more than one target if it
484 could be considered conceptually a member of both.
485```
486
487#### **Examples**
488
489```
490 gn check out/Debug
491 Check everything.
492
493 gn check out/Default //foo:bar
494 Check only the files in the //foo:bar target.
495
496 gn check out/Default "//foo/*
497 Check only the files in targets in the //foo directory tree.
498```
[email protected]449f3e42024-08-01 21:43:27 +0800499### **gn clean <out_dir>...** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -0700500
501```
502 Deletes the contents of the output directory except for args.gn and
503 creates a Ninja build environment sufficient to regenerate the build.
504```
[email protected]449f3e42024-08-01 21:43:27 +0800505### **gn clean_stale [\--ninja-executable=...] <out_dir>...** [Back to Top](#gn-reference)
RJ Ascani4e260f12020-10-19 17:41:51 -0700506
507```
508 Removes the no longer needed output files from the build directory and prunes
509 their records from the ninja build log and dependency database. These are
510 output files that were generated from previous builds, but the current build
511 graph no longer references them.
512
513 This command requires a ninja executable of at least version 1.10.0. The
514 executable must be provided by the --ninja-executable switch.
515```
516
517#### **Options**
518
519```
520 --ninja-executable=
521 Can be used to specify the ninja executable to use.
522```
[email protected]449f3e42024-08-01 21:43:27 +0800523### **gn desc** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -0700524
525```
Joe Armstrongb199e542019-03-21 09:40:08 +0800526 gn desc
527 [--format=json]
528
Julie Hockette2a29402018-07-31 10:11:42 -0700529 Displays information about a given target or config. The build parameters
530 will be taken for the build in the given .
Brett Wilson796ed472018-07-16 15:11:09 -0700531
532 The
533 pattern (see "gn help label_pattern"). A label pattern will only match
534 targets.
535```
536
Nico Weber75eb3c72018-08-28 16:01:52 -0400537#### **Possibilities for <what to show>**
Brett Wilson796ed472018-07-16 15:11:09 -0700538
539```
540 (If unspecified an overall summary will be displayed.)
541
542 all_dependent_configs
543 allow_circular_includes_from
544 arflags [--blame]
545 args
546 cflags [--blame]
Julie Hockette2a29402018-07-31 10:11:42 -0700547 cflags_c [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700548 cflags_cc [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700549 check_includes
550 configs [--tree] (see below)
Nate Fischer8ed01d32019-01-08 17:32:01 -0800551 data_keys
Brett Wilson796ed472018-07-16 15:11:09 -0700552 defines [--blame]
553 depfile
554 deps [--all] [--tree] (see below)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100555 framework_dirs
556 frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700557 include_dirs [--blame]
558 inputs
559 ldflags [--blame]
560 lib_dirs
561 libs
Nate Fischer8ed01d32019-01-08 17:32:01 -0800562 metadata
563 output_conversion
Brett Wilson796ed472018-07-16 15:11:09 -0700564 outputs
565 public_configs
566 public
Nate Fischer8ed01d32019-01-08 17:32:01 -0800567 rebase
Brett Wilson796ed472018-07-16 15:11:09 -0700568 script
569 sources
570 testonly
571 visibility
Nate Fischer8ed01d32019-01-08 17:32:01 -0800572 walk_keys
Robert Sesekd0a6f072020-05-15 11:21:22 -0400573 weak_frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700574
575 runtime_deps
576 Compute all runtime deps for the given target. This is a computed list
577 and does not correspond to any GN variable, unlike most other values
578 here.
579
580 The output is a list of file names relative to the build directory. See
581 "gn help runtime_deps" for how this is computed. This also works with
582 "--blame" to see the source of the dependency.
583```
584
585#### **Shared flags**
Brett Wilson796ed472018-07-16 15:11:09 -0700586
Robert Sesekd0a6f072020-05-15 11:21:22 -0400587```
588 --default-toolchain
589 Normally wildcard targets are matched in all toolchains. This
590 switch makes wildcard labels with no explicit toolchain reference
591 only match targets in the default toolchain.
592
593 Non-wildcard inputs with no explicit toolchain specification will
594 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -0700595
596 --format=json
597 Format the output as JSON instead of text.
598```
599
600#### **Target flags**
601
602```
603 --blame
604 Used with any value specified on a config, this will name the config that
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100605 causes that target to get the flag. This doesn't currently work for libs,
Robert Sesekd0a6f072020-05-15 11:21:22 -0400606 lib_dirs, frameworks, weak_frameworks and framework_dirs because those are
607 inherited and are more complicated to figure out the blame (patches
608 welcome).
Brett Wilson796ed472018-07-16 15:11:09 -0700609```
610
611#### **Configs**
612
613```
614 The "configs" section will list all configs that apply. For targets this will
615 include configs specified in the "configs" variable of the target, and also
616 configs pushed onto this target via public or "all dependent" configs.
617
618 Configs can have child configs. Specifying --tree will show the hierarchy.
619```
620
621#### **Printing outputs**
622
623```
624 The "outputs" section will list all outputs that apply, including the outputs
625 computed from the tool definition (eg for "executable", "static_library", ...
626 targets).
627```
628
629#### **Printing deps**
630
631```
632 Deps will include all public, private, and data deps (TODO this could be
633 clarified and enhanced) sorted in order applying. The following may be used:
634
635 --all
636 Collects all recursive dependencies and prints a sorted flat list. Also
637 usable with --tree (see below).
Robert Sesekd0a6f072020-05-15 11:21:22 -0400638
Brett Wilson796ed472018-07-16 15:11:09 -0700639 --as=(buildfile|label|output)
640 How to print targets.
641
642 buildfile
643 Prints the build files where the given target was declared as
644 file names.
645 label (default)
646 Prints the label of the target.
647 output
648 Prints the first output file for the target relative to the
649 root build directory.
650
651 --testonly=(true|false)
652 Restrict outputs to targets with the testonly flag set
653 accordingly. When unspecified, the target's testonly flags are
654 ignored.
655
656 --tree
657 Print a dependency tree. By default, duplicates will be elided with "..."
658 but when --all and -tree are used together, no eliding will be performed.
659
660 The "deps", "public_deps", and "data_deps" will all be included in the
661 tree.
662
663 Tree output can not be used with the filtering or output flags: --as,
664 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400665
Brett Wilson796ed472018-07-16 15:11:09 -0700666 --type=(action|copy|executable|group|loadable_module|shared_library|
667 source_set|static_library)
668 Restrict outputs to targets matching the given type. If
669 unspecified, no filtering will be performed.
670```
671
672#### **Note**
673
674```
675 This command will show the full name of directories and source files, but
676 when directories and source paths are written to the build file, they will be
677 adjusted to be relative to the build directory. So the values for paths
678 displayed by this command won't match (but should mean the same thing).
679```
680
681#### **Examples**
682
683```
684 gn desc out/Debug //base:base
685 Summarizes the given target.
686
687 gn desc out/Foo :base_unittests deps --tree
688 Shows a dependency tree of the "base_unittests" project in
689 the current directory.
690
691 gn desc out/Debug //base defines --blame
692 Shows defines set for the //base:base target, annotated by where
693 each one was set from.
694```
[email protected]449f3e42024-08-01 21:43:27 +0800695### **gn format [\--dump-tree] (\--stdin | <list of build_files...>)** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -0700696
697```
698 Formats .gn file to a standard format.
699
700 The contents of some lists ('sources', 'deps', etc.) will be sorted to a
701 canonical order. To suppress this, you can add a comment of the form "#
702 NOSORT" immediately preceding the assignment. e.g.
703
704 # NOSORT
705 sources = [
706 "z.cc",
707 "a.cc",
708 ]
709```
710
711#### **Arguments**
712
713```
714 --dry-run
Nico Weberad9eab22020-11-15 22:20:28 -0500715 Prints the list of files that would be reformatted but does not write
716 anything to disk. This is useful for presubmit/lint-type checks.
Brett Wilson796ed472018-07-16 15:11:09 -0700717 - Exit code 0: successful format, matches on disk.
718 - Exit code 1: general failure (parse error, etc.)
719 - Exit code 2: successful format, but differs from on disk.
720
Julie Hockettd69a9c32019-01-23 14:36:18 -0800721 --dump-tree[=( text | json )]
722 Dumps the parse tree to stdout and does not update the file or print
723 formatted output. If no format is specified, text format will be used.
Brett Wilson796ed472018-07-16 15:11:09 -0700724
725 --stdin
726 Read input from stdin and write to stdout rather than update a file
727 in-place.
RJ Ascani4e260f12020-10-19 17:41:51 -0700728
729 --read-tree=json
730 Reads an AST from stdin in the format output by --dump-tree=json and
731 uses that as the parse tree. (The only read-tree format currently
732 supported is json.) The given .gn file will be overwritten. This can be
733 used to programmatically transform .gn files.
Brett Wilson796ed472018-07-16 15:11:09 -0700734```
735
736#### **Examples**
737```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800738 gn format //some/BUILD.gn //some/other/BUILD.gn //and/another/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700739 gn format some\\BUILD.gn
740 gn format /abspath/some/BUILD.gn
741 gn format --stdin
RJ Ascani4e260f12020-10-19 17:41:51 -0700742 gn format --read-tree=json //rewritten/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700743```
[email protected]449f3e42024-08-01 21:43:27 +0800744### **gn gen [\--check] [<ide options>] <out_dir>** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -0700745
746```
747 Generates ninja files from the current tree and puts them in the given output
748 directory.
749
750 The output directory can be a source-repo-absolute path name such as:
751 //out/foo
752 Or it can be a directory relative to the current directory such as:
753 out/foo
754
James Robinson49f59032020-01-08 14:05:51 -0800755 "gn gen --check" is the same as running "gn check". "gn gen --check=system" is
756 the same as running "gn check --check-system". See "gn help check" for
757 documentation on that mode.
Brett Wilson796ed472018-07-16 15:11:09 -0700758
759 See "gn help switches" for the common command-line switches.
760```
761
RJ Ascani6966efb2020-10-19 16:50:11 -0700762#### **General options**
763
764```
765 --ninja-executable=
766 Can be used to specify the ninja executable to use. This executable will
767 be used as an IDE option to indicate which ninja to use for building. This
768 executable will also be used as part of the gen process for triggering a
RJ Ascani4e260f12020-10-19 17:41:51 -0700769 restat on generated ninja files and for use with --clean-stale.
770
771 --clean-stale
772 This option will cause no longer needed output files to be removed from
773 the build directory, and their records pruned from the ninja build log and
774 dependency database after the ninja build graph has been generated. This
775 option requires a ninja executable of at least version 1.10.0. It can be
776 provided by the --ninja-executable switch. Also see "gn help clean_stale".
RJ Ascani6966efb2020-10-19 16:50:11 -0700777```
778
Brett Wilson796ed472018-07-16 15:11:09 -0700779#### **IDE options**
780
781```
Harley Li0a9affb2020-06-03 10:38:42 -0400782 GN optionally generates files for IDE. Files won't be overwritten if their
783 contents don't change. Possibilities for
Brett Wilson796ed472018-07-16 15:11:09 -0700784
785 --ide=
786 Generate files for an IDE. Currently supported values:
787 "eclipse" - Eclipse CDT settings file.
788 "vs" - Visual Studio project/solution files.
Chunbo Hua4a8016d2025-02-20 10:31:32 +0800789 (default Visual Studio version: 2022)
Brett Wilson796ed472018-07-16 15:11:09 -0700790 "vs2013" - Visual Studio 2013 project/solution files.
791 "vs2015" - Visual Studio 2015 project/solution files.
792 "vs2017" - Visual Studio 2017 project/solution files.
Sylvain Defresned46f92f2019-04-03 16:14:29 +0200793 "vs2019" - Visual Studio 2019 project/solution files.
Tyler Mandry4a648092022-02-15 19:47:09 +0000794 "vs2022" - Visual Studio 2022 project/solution files.
Brett Wilson796ed472018-07-16 15:11:09 -0700795 "xcode" - Xcode workspace/solution files.
796 "qtcreator" - QtCreator project files.
797 "json" - JSON file containing target information
798
799 --filters=
800 Semicolon-separated list of label patterns used to limit the set of
801 generated projects (see "gn help label_pattern"). Only matching targets
802 and their dependencies will be included in the solution. Only used for
803 Visual Studio, Xcode and JSON.
804```
805
806#### **Visual Studio Flags**
807
808```
809 --sln=
810 Override default sln file name ("all"). Solution file is written to the
811 root build directory.
812
813 --no-deps
814 Don't include targets dependencies to the solution. Changes the way how
815 --filters option works. Only directly matching targets are included.
816
817 --winsdk=
818 Use the specified Windows 10 SDK version to generate project files.
819 As an example, "10.0.15063.0" can be specified to use Creators Update SDK
820 instead of the default one.
821
Shezan Baigc0a2d232021-07-01 00:29:49 -0400822 --ninja-executable=
823 Can be used to specify the ninja executable to use when building.
824
Brett Wilson796ed472018-07-16 15:11:09 -0700825 --ninja-extra-args=
826 This string is passed without any quoting to the ninja invocation
827 command-line. Can be used to configure ninja flags, like "-j".
828```
829
830#### **Xcode Flags**
831
832```
Sylvain Defresnedb55efd2020-06-16 12:23:03 +0200833 --xcode-project=
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +0100834 Override default Xcode project file name ("all"). The project file is
Brett Wilson796ed472018-07-16 15:11:09 -0700835 written to the root build directory.
836
Sylvain Defresne7b0690e2020-06-26 15:33:01 +0200837 --xcode-build-system=
838 Configure the build system to use for the Xcode project. Supported
839 values are (default to "legacy"):
840 "legacy" - Legacy Build system
841 "new" - New Build System
842
Sylvain Defresne88830702022-06-20 17:11:49 +0200843 --xcode-configs=
844 Configure the list of build configuration supported by the generated
845 project. If specified, must be a list of semicolon-separated strings.
Ricardo Ribalda85bd0a62023-11-11 16:30:19 +0000846 If omitted, a single configuration will be used in the generated
Sylvain Defresne88830702022-06-20 17:11:49 +0200847 project derived from the build directory.
848
849 --xcode-config-build-dir=
850 If present, must be a path relative to the source directory. It will
Ricardo Ribalda85bd0a62023-11-11 16:30:19 +0000851 default to $root_out_dir if omitted. The path is assumed to point to
Sylvain Defresne88830702022-06-20 17:11:49 +0200852 the directory where ninja needs to be invoked. This variable can be
853 used to build for multiple configuration / platform / environment from
854 the same generated Xcode project (assuming that the user has created a
855 gn build directory with the correct args.gn for each).
856
857 One useful value is to use Xcode variables such as '${CONFIGURATION}'
858 or '${EFFECTIVE_PLATFORM}'.
859
Sylvain Defresneae474cc2022-06-21 14:44:02 +0200860 --xcode-additional-files-patterns=
861 If present, must be a list of semicolon-separated file patterns. It
862 will be used to add all files matching the pattern located in the
863 source tree to the project. It can be used to add, e.g. documentation
864 files to the project to allow easily edit them.
865
866 --xcode-additional-files-roots=
867 If present, must be a list of semicolon-separated paths. It will be used
Ricardo Ribalda85bd0a62023-11-11 16:30:19 +0000868 as roots when looking for additional files to add. If omitted, defaults
Sylvain Defresneae474cc2022-06-21 14:44:02 +0200869 to "//".
870
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100871 --ninja-executable=
872 Can be used to specify the ninja executable to use when building.
873
Brett Wilson796ed472018-07-16 15:11:09 -0700874 --ninja-extra-args=
875 This string is passed without any quoting to the ninja invocation
876 command-line. Can be used to configure ninja flags, like "-j".
877
Sylvain Defresnee431b2f2020-11-04 10:49:47 +0100878 --ide-root-target=
Brett Wilson796ed472018-07-16 15:11:09 -0700879 Name of the target corresponding to "All" target in Xcode. If unset,
880 "All" invokes ninja without any target and builds everything.
881```
882
883#### **QtCreator Flags**
884
885```
Sylvain Defresnee431b2f2020-11-04 10:49:47 +0100886 --ide-root-target=
Brett Wilson796ed472018-07-16 15:11:09 -0700887 Name of the root target for which the QtCreator project will be generated
888 to contain files of it and its dependencies. If unset, the whole build
889 graph will be emitted.
890```
891
892#### **Eclipse IDE Support**
893
894```
895 GN DOES NOT generate Eclipse CDT projects. Instead, it generates a settings
896 file which can be imported into an Eclipse CDT project. The XML file contains
897 a list of include paths and defines. Because GN does not generate a full
898 .cproject definition, it is not possible to properly define includes/defines
899 for each file individually. Instead, one set of includes/defines is generated
900 for the entire project. This works fairly well but may still result in a few
901 indexer issues here and there.
902```
903
904#### **Generic JSON Output**
905
906```
907 Dumps target information to a JSON file and optionally invokes a
908 python script on the generated file. See the comments at the beginning
909 of json_project_writer.cc and desc_builder.cc for an overview of the JSON
910 file format.
911
912 --json-file-name=
913 Overrides default file name (project.json) of generated JSON file.
914
915 --json-ide-script=
Harley Li0a9affb2020-06-03 10:38:42 -0400916 Executes python script after the JSON file is generated or updated with
917 new content. Path can be project absolute (//), system absolute (/) or
918 relative, in which case the output directory will be base. Path to
919 generated JSON file will be first argument when invoking script.
Brett Wilson796ed472018-07-16 15:11:09 -0700920
921 --json-ide-script-args=
David 'Digit' Turner6253a392024-02-20 19:55:35 +0100922 Optional second argument that will be passed to executed script.
nespie1cad8f672025-04-20 17:44:49 +0300923
924 --filter-with-data
925 Additionally follows data deps when filtering. Without this flag, only
926 public and private linked deps will be followed. Only used with --filters.
David 'Digit' Turner6253a392024-02-20 19:55:35 +0100927```
928
929#### **Ninja Outputs**
930
931```
932 The --ninja-outputs-file= option dumps a JSON file that maps GN labels
933 to their Ninja output paths. This can be later processed to build an index
934 to convert between Ninja targets and GN ones before or after the build itself.
935 It looks like:
936
937 {
938 "label1": [
939 "path1",
940 "path2"
941 ],
942 "label2": [
943 "path3"
944 ]
945 }
946
947 --ninja-outputs-script=
948 Executes python script after the outputs file is generated or updated
949 with new content. Path can be project absolute (//), system absolute (/) or
950 relative, in which case the output directory will be base. Path to
951 generated file will be first argument when invoking script.
952
953 --ninja-outputs-script-args=
954 Optional second argument that will be passed to executed script.
Brett Wilson796ed472018-07-16 15:11:09 -0700955```
Julie Hockett11e09912018-07-31 13:15:08 -0700956
957#### **Compilation Database**
958
959```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400960 --export-rust-project
961 Produces a rust-project.json file in the root of the build directory
962 This is used for various tools in the Rust ecosystem allowing for the
963 replay of individual compilations independent of the build system.
964 This is an unstable format and likely to change without warning.
965
Brett Wilson1da84bb2022-09-14 15:35:29 -0700966 --add-export-compile-commands=
967 Adds an additional label pattern (see "gn help label_pattern") of a
968 target to add to the compilation database. This pattern is appended to any
969 list values specified in the export_compile_commands variable in the
970 .gn file (see "gn help dotfile"). This allows the user to add additional
971 targets to the compilation database that the project doesn't add by default.
972
973 To add more than one value, specify this switch more than once. Each
974 invocation adds an additional label pattern.
975
976 Example:
977 --add-export-compile-commands=//tools:my_tool
978 --add-export-compile-commands="//base/*"
979
Robert Sesek252674d2019-05-10 14:08:52 -0400980 --export-compile-commands[=]
Brett Wilson1da84bb2022-09-14 15:35:29 -0700981 DEPRECATED https://bugs.chromium.org/p/gn/issues/detail?id=302.
982 Please use --add-export-compile-commands for per-user configuration, and
983 the "export_compile_commands" value in the project-level .gn file (see
984 "gn help dotfile") for per-project configuration.
985
986 Overrides the value of the export_compile_commands in the .gn file (see
987 "gn help dotfile") as well as the --add-export-compile-commands switch.
988
989 Unlike the .gn setting, this switch takes a legacy format which is a list
990 of target names that are matched in any directory. For example, "foo" will
991 match:
992 - "//path/to/src:foo"
993 - "//other/path:foo"
994 - "//foo:foo"
Aaron Wood2d9fd622020-12-10 21:28:19 -0800995 and not match:
Brett Wilson1da84bb2022-09-14 15:35:29 -0700996 - "//foo:bar"
Julie Hockett11e09912018-07-31 13:15:08 -0700997```
[email protected]449f3e42024-08-01 21:43:27 +0800998### **gn help <anything>** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -0700999
1000```
1001 Yo dawg, I heard you like help on your help so I put help on the help in the
1002 help.
1003
1004 You can also use "all" as the parameter to get all help at once.
1005```
1006
1007#### **Switches**
1008
1009```
1010 --markdown
1011 Format output in markdown syntax.
1012```
1013
1014#### **Example**
1015
1016```
1017 gn help --markdown all
1018 Dump all help to stdout in markdown format.
1019```
[email protected]449f3e42024-08-01 21:43:27 +08001020### **gn ls <out_dir> [<label_pattern>] [\--default-toolchain] [\--as=...]** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07001021```
1022 [--type=...] [--testonly=...]
1023
1024 Lists all targets matching the given pattern for the given build directory.
1025 By default, only targets in the default toolchain will be matched unless a
1026 toolchain is explicitly supplied.
1027
1028 If the label pattern is unspecified, list all targets. The label pattern is
1029 not a general regular expression (see "gn help label_pattern"). If you need
1030 more complex expressions, pipe the result through grep.
1031```
1032
1033#### **Options**
1034
1035```
1036 --as=(buildfile|label|output)
1037 How to print targets.
1038
1039 buildfile
1040 Prints the build files where the given target was declared as
1041 file names.
1042 label (default)
1043 Prints the label of the target.
1044 output
1045 Prints the first output file for the target relative to the
1046 root build directory.
1047
Robert Sesekd0a6f072020-05-15 11:21:22 -04001048 --default-toolchain
1049 Normally wildcard targets are matched in all toolchains. This
1050 switch makes wildcard labels with no explicit toolchain reference
1051 only match targets in the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07001052
Robert Sesekd0a6f072020-05-15 11:21:22 -04001053 Non-wildcard inputs with no explicit toolchain specification will
1054 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -07001055
1056 --testonly=(true|false)
1057 Restrict outputs to targets with the testonly flag set
1058 accordingly. When unspecified, the target's testonly flags are
1059 ignored.
1060
1061 --type=(action|copy|executable|group|loadable_module|shared_library|
1062 source_set|static_library)
1063 Restrict outputs to targets matching the given type. If
1064 unspecified, no filtering will be performed.
1065```
1066
1067#### **Examples**
1068
1069```
1070 gn ls out/Debug
1071 Lists all targets in the default toolchain.
1072
1073 gn ls out/Debug "//base/*"
1074 Lists all targets in the directory base and all subdirectories.
1075
1076 gn ls out/Debug "//base:*"
1077 Lists all targets defined in //base/BUILD.gn.
1078
1079 gn ls out/Debug //base --as=output
1080 Lists the build output file for //base:base
1081
1082 gn ls out/Debug --type=executable
1083 Lists all executables produced by the build.
1084
1085 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
1086 Builds all targets in //base and all subdirectories.
Brett Wilson796ed472018-07-16 15:11:09 -07001087```
[email protected]449f3e42024-08-01 21:43:27 +08001088### **gn meta** [Back to Top](#gn-reference)
Joe Armstrongb199e542019-03-21 09:40:08 +08001089
Nate Fischer8ed01d32019-01-08 17:32:01 -08001090```
Joe Armstrongb199e542019-03-21 09:40:08 +08001091 gn meta * --data=[,*]* [--walk=[,*]*]
1092 [--rebase=]
Nate Fischer8ed01d32019-01-08 17:32:01 -08001093
1094 Lists collected metaresults of all given targets for the given data key(s),
1095 collecting metadata dependencies as specified by the given walk key(s).
1096
1097 See `gn help generated_file` for more information on the walk.
1098```
1099
1100#### **Arguments**
1101
1102```
1103
1104 A list of target labels from which to initiate the walk.
1105
1106 --data
Tyler Mandry4a648092022-02-15 19:47:09 +00001107 A comma-separated list of keys from which to extract data. In each target
1108 walked, its metadata scope is checked for the presence of these keys. If
1109 present, the contents of those variable in the scope are appended to the
1110 results list.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001111
1112 --walk (optional)
Tyler Mandry4a648092022-02-15 19:47:09 +00001113 A comma-separated list of keys from which to control the walk. In each
1114 target walked, its metadata scope is checked for the presence of any of
1115 these keys. If present, the contents of those variables is checked to ensure
1116 that it is a label of a valid dependency of the target and then added to the
1117 set of targets to walk. If the empty string ("") is present in any of these
1118 keys, all deps and data_deps are added to the walk set.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001119
1120 --rebase (optional)
1121 A destination directory onto which to rebase any paths found. If set, all
1122 collected metadata will be rebased onto this path. This option will throw errors
1123 if collected metadata is not a list of strings.
1124```
1125
1126#### **Examples**
1127
1128```
1129 gn meta out/Debug "//base/foo" --data=files
1130 Lists collected metaresults for the `files` key in the //base/foo:foo
1131 target and all of its dependency tree.
1132
Tyler Mandry4a648092022-02-15 19:47:09 +00001133 gn meta out/Debug "//base/foo" --data=files,other
Nate Fischer8ed01d32019-01-08 17:32:01 -08001134 Lists collected metaresults for the `files` and `other` keys in the
1135 //base/foo:foo target and all of its dependency tree.
1136
1137 gn meta out/Debug "//base/foo" --data=files --walk=stop
1138 Lists collected metaresults for the `files` key in the //base/foo:foo
1139 target and all of the dependencies listed in the `stop` key (and so on).
1140
1141 gn meta out/Debug "//base/foo" --data=files --rebase="/"
1142 Lists collected metaresults for the `files` key in the //base/foo:foo
1143 target and all of its dependency tree, rebasing the strings in the `files`
1144 key onto the source directory of the target's declaration relative to "/".
1145```
[email protected]449f3e42024-08-01 21:43:27 +08001146### **gn outputs <out_dir> <list of target or file names...>** [Back to Top](#gn-reference)
Brett Wilson74397092020-03-18 16:34:14 -07001147
1148```
1149 Lists the output files corresponding to the given target(s) or file name(s).
1150 There can be multiple outputs because there can be more than one output
1151 generated by a build step, and there can be more than one toolchain matched.
1152 You can also list multiple inputs which will generate a union of all the
1153 outputs from those inputs.
1154
1155 - The input target/file names are relative to the current directory.
1156
1157 - The output file names are relative to the root build directory.
1158
1159 This command is useful for finding a ninja command that will build only a
1160 portion of the build.
1161```
1162
1163#### **Target outputs**
1164
1165```
1166 If the parameter is a target name that includes a toolchain, it will match
1167 only that target in that toolchain. If no toolchain is specified, it will
1168 match all targets with that name in any toolchain.
1169
1170 The result will be the outputs specified by that target which could be a
1171 library, executable, output of an action, a stamp file, etc.
1172```
1173
1174#### **File outputs**
1175
1176```
1177 If the parameter is a file name it will compute the output for that compile
1178 step for all targets in all toolchains that contain that file as a source
1179 file.
1180
1181 If the source is not compiled (e.g. a header or text file), the command will
1182 produce no output.
1183
1184 If the source is listed as an "input" to a binary target or action will
1185 resolve to that target's outputs.
1186```
1187
1188#### **Example**
1189
1190```
1191 gn outputs out/debug some/directory:some_target
1192 Find the outputs of a given target.
1193
1194 gn outputs out/debug src/project/my_file.cc | xargs ninja -C out/debug
1195 Compiles just the given source file in all toolchains it's referenced in.
1196
1197 git diff --name-only | xargs gn outputs out/x64 | xargs ninja -C out/x64
1198 Compiles all files changed in git.
1199```
[email protected]449f3e42024-08-01 21:43:27 +08001200### **gn path <out_dir> <target_one> <target_two>** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07001201
1202```
1203 Finds paths of dependencies between two targets. Each unique path will be
1204 printed in one group, and groups will be separate by newlines. The two
1205 targets can appear in either order (paths will be found going in either
1206 direction).
1207
1208 By default, a single path will be printed. If there is a path with only
1209 public dependencies, the shortest public path will be printed. Otherwise, the
1210 shortest path using either public or private dependencies will be printed. If
1211 --with-data is specified, data deps will also be considered. If there are
1212 multiple shortest paths, an arbitrary one will be selected.
1213```
1214
1215#### **Interesting paths**
1216
1217```
1218 In a large project, there can be 100's of millions of unique paths between a
1219 very high level and a common low-level target. To make the output more useful
1220 (and terminate in a reasonable time), GN will not revisit sub-paths
1221 previously known to lead to the target.
1222```
1223
1224#### **Options**
1225
1226```
1227 --all
1228 Prints all "interesting" paths found rather than just the first one.
1229 Public paths will be printed first in order of increasing length, followed
1230 by non-public paths in order of increasing length.
1231
1232 --public
1233 Considers only public paths. Can't be used with --with-data.
1234
1235 --with-data
1236 Additionally follows data deps. Without this flag, only public and private
1237 linked deps will be followed. Can't be used with --public.
1238```
1239
1240#### **Example**
1241
1242```
Keir Mierle45611e32019-11-12 11:18:00 -08001243 gn path out/Default //base //gn
Brett Wilson796ed472018-07-16 15:11:09 -07001244```
[email protected]449f3e42024-08-01 21:43:27 +08001245### **gn refs** [Back to Top](#gn-reference)
Joe Armstrongb199e542019-03-21 09:40:08 +08001246
Brett Wilson796ed472018-07-16 15:11:09 -07001247```
Nico Weberad9eab22020-11-15 22:20:28 -05001248 gn refs (|
1249 [--default-toolchain] [--as=...] [--testonly=...] [--type=...]
Brett Wilson796ed472018-07-16 15:11:09 -07001250
1251 Finds reverse dependencies (which targets reference something). The input is
1252 a list containing:
1253
1254 - Target label: The result will be which targets depend on it.
1255
1256 - Config label: The result will be which targets list the given config in
1257 its "configs" or "public_configs" list.
1258
1259 - Label pattern: The result will be which targets depend on any target
1260 matching the given pattern. Patterns will not match configs. These are not
1261 general regular expressions, see "gn help label_pattern" for details.
1262
1263 - File name: The result will be which targets list the given file in its
1264 "inputs", "sources", "public", "data", or "outputs". Any input that does
1265 not contain wildcards and does not match a target or a config will be
1266 treated as a file.
1267
1268 - Response file: If the input starts with an "@", it will be interpreted as
1269 a path to a file containing a list of labels or file names, one per line.
1270 This allows us to handle long lists of inputs without worrying about
1271 command line limits.
1272```
1273
1274#### **Options**
1275
1276```
1277 --all
1278 When used without --tree, will recurse and display all unique
1279 dependencies of the given targets. For example, if the input is a target,
1280 this will output all targets that depend directly or indirectly on the
1281 input. If the input is a file, this will output all targets that depend
1282 directly or indirectly on that file.
1283
1284 When used with --tree, turns off eliding to show a complete tree.
Brett Wilson796ed472018-07-16 15:11:09 -07001285
1286 --as=(buildfile|label|output)
1287 How to print targets.
1288
1289 buildfile
1290 Prints the build files where the given target was declared as
1291 file names.
1292 label (default)
1293 Prints the label of the target.
1294 output
1295 Prints the first output file for the target relative to the
1296 root build directory.
1297
Robert Sesekd0a6f072020-05-15 11:21:22 -04001298 --default-toolchain
1299 Normally wildcard targets are matched in all toolchains. This
1300 switch makes wildcard labels with no explicit toolchain reference
1301 only match targets in the default toolchain.
1302
1303 Non-wildcard inputs with no explicit toolchain specification will
1304 always match only a target in the default toolchain if one exists.
1305
Brett Wilson796ed472018-07-16 15:11:09 -07001306 -q
1307 Quiet. If nothing matches, don't print any output. Without this option, if
1308 there are no matches there will be an informational message printed which
1309 might interfere with scripts processing the output.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001310
Brett Wilson796ed472018-07-16 15:11:09 -07001311 --testonly=(true|false)
1312 Restrict outputs to targets with the testonly flag set
1313 accordingly. When unspecified, the target's testonly flags are
1314 ignored.
1315
1316 --tree
1317 Outputs a reverse dependency tree from the given target. Duplicates will
1318 be elided. Combine with --all to see a full dependency tree.
1319
1320 Tree output can not be used with the filtering or output flags: --as,
1321 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001322
Brett Wilson796ed472018-07-16 15:11:09 -07001323 --type=(action|copy|executable|group|loadable_module|shared_library|
1324 source_set|static_library)
1325 Restrict outputs to targets matching the given type. If
1326 unspecified, no filtering will be performed.
1327```
1328
1329#### **Examples (target input)**
1330
1331```
Keir Mierle45611e32019-11-12 11:18:00 -08001332 gn refs out/Debug //gn:gn
Brett Wilson796ed472018-07-16 15:11:09 -07001333 Find all targets depending on the given exact target name.
1334
Lei Zhang415b3b12024-04-02 09:29:36 -07001335 gn refs out/Debug //base:i18n --as=buildfile | xargs gvim
Brett Wilson796ed472018-07-16 15:11:09 -07001336 Edit all .gn files containing references to //base:i18n
1337
1338 gn refs out/Debug //base --all
1339 List all targets depending directly or indirectly on //base:base.
1340
1341 gn refs out/Debug "//base/*"
1342 List all targets depending directly on any target in //base or
1343 its subdirectories.
1344
1345 gn refs out/Debug "//base:*"
1346 List all targets depending directly on any target in
1347 //base/BUILD.gn.
1348
1349 gn refs out/Debug //base --tree
1350 Print a reverse dependency tree of //base:base
1351```
1352
1353#### **Examples (file input)**
1354
1355```
1356 gn refs out/Debug //base/macros.h
1357 Print target(s) listing //base/macros.h as a source.
1358
1359 gn refs out/Debug //base/macros.h --tree
1360 Display a reverse dependency tree to get to the given file. This
1361 will show how dependencies will reference that file.
1362
1363 gn refs out/Debug //base/macros.h //base/at_exit.h --all
1364 Display all unique targets with some dependency path to a target
1365 containing either of the given files as a source.
1366
1367 gn refs out/Debug //base/macros.h --testonly=true --type=executable
1368 --all --as=output
1369 Display the executable file names of all test executables
1370 potentially affected by a change to the given file.
1371```
1372## Target declarations
1373
[email protected]449f3e42024-08-01 21:43:27 +08001374### **action**: Declare a target that runs a script a single time. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07001375
1376```
1377 This target type allows you to run a script a single time to produce one or
1378 more output files. If you want to run a script once for each of a set of
1379 input files, see "gn help action_foreach".
1380```
1381
1382#### **Inputs**
1383
1384```
1385 In an action the "sources" and "inputs" are treated the same: they're both
1386 input dependencies on script execution with no special handling. If you want
1387 to pass the sources to your script, you must do so explicitly by including
1388 them in the "args". Note also that this means there is no special handling of
1389 paths since GN doesn't know which of the args are paths and not. You will
1390 want to use rebase_path() to convert paths to be relative to the
1391 root_build_dir.
1392
1393 You can dynamically write input dependencies (for incremental rebuilds if an
1394 input file changes) by writing a depfile when the script is run (see "gn help
1395 depfile"). This is more flexible than "inputs".
1396
1397 If the command line length is very long, you can use response files to pass
1398 args to your script. See "gn help response_file_contents".
1399
1400 It is recommended you put inputs to your script in the "sources" variable,
1401 and stuff like other Python files required to run your script in the "inputs"
1402 variable.
Julie Hockette2a29402018-07-31 10:11:42 -07001403
Adrian Taylorc0d4f8b2022-03-25 15:05:57 -07001404 Actions can take the configs and public_configs lists, as well as any of the
1405 configs variables (defines, include_dirs, etc.) set directly on the target.
1406 These behave exactly as they would on a binary target and can be accessed
1407 using substitution patterns in the script args (see "gn help args") to
1408 implement custom compiler-like tools.
1409
Brett Wilson796ed472018-07-16 15:11:09 -07001410 The "deps" and "public_deps" for an action will always be
1411 completed before any part of the action is run so it can depend on
1412 the output of previous steps. The "data_deps" will be built if the
1413 action is built, but may not have completed before all steps of the
1414 action are started. This can give additional parallelism in the build
1415 for runtime-only dependencies.
1416```
1417
1418#### **Outputs**
1419
1420```
1421 You should specify files created by your script by specifying them in the
1422 "outputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001423
Brett Wilson796ed472018-07-16 15:11:09 -07001424 The script will be executed with the given arguments with the current
1425 directory being that of the root build directory. If you pass files
1426 to your script, see "gn help rebase_path" for how to convert
1427 file names to be relative to the build directory (file names in the
1428 sources, outputs, and inputs will be all treated as relative to the
1429 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001430
1431 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1432 that Ninja will check the timestamp of the output after the action
1433 completes. If output timestamp is unchanged, the step will be treated
1434 as if it never needed to be rebuilt, potentially eliminating some
1435 downstream steps for incremental builds. Scripts can improve build
1436 performance by taking care not to change the timstamp of the output
1437 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001438```
1439
1440#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001441
Brett Wilson796ed472018-07-16 15:11:09 -07001442```
1443 All output files must be inside the output directory of the build.
1444 You would generally use |$target_out_dir| or |$target_gen_dir| to
1445 reference the output or generated intermediate file directories,
1446 respectively.
1447```
1448
1449#### **Variables**
1450
1451```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001452 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1453 cflags_objcc, defines, include_dirs, inputs, ldflags,
1454 lib_dirs, libs, precompiled_header, precompiled_source,
1455 rustenv, rustflags, swiftflags, testonly
1456 Dependent configs: all_dependent_configs, public_configs
1457 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1458 write_runtime_deps
1459 General: check_includes, configs, data, friend, inputs, metadata,
1460 output_extension, output_name, public, sources, testonly,
1461 visibility
1462 Action variables: args, bridge_header, configs, data, depfile,
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07001463 framework_dirs, inputs, mnemonic, module_deps,
1464 module_name, outputs*, pool, response_file_contents,
1465 script*, sources
Brett Wilson796ed472018-07-16 15:11:09 -07001466 * = required
1467```
1468
1469#### **Example**
1470
1471```
1472 action("run_this_guy_once") {
1473 script = "doprocessing.py"
1474 sources = [ "my_configuration.txt" ]
1475 outputs = [ "$target_gen_dir/insightful_output.txt" ]
1476
1477 # Our script imports this Python file so we want to rebuild if it changes.
1478 inputs = [ "helper_library.py" ]
1479
1480 # Note that we have to manually pass the sources to our script if the
1481 # script needs them as inputs.
1482 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
1483 rebase_path(sources, root_build_dir)
1484 }
1485```
[email protected]449f3e42024-08-01 21:43:27 +08001486### **action_foreach**: Declare a target that runs a script over a set of files. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07001487
1488```
1489 This target type allows you to run a script once-per-file over a set of
1490 sources. If you want to run a script once that takes many files as input, see
1491 "gn help action".
1492```
1493
1494#### **Inputs**
1495
1496```
1497 The script will be run once per file in the "sources" variable. The "outputs"
1498 variable should specify one or more files with a source expansion pattern in
1499 it (see "gn help source_expansion"). The output file(s) for each script
1500 invocation should be unique. Normally you use "{{source_name_part}}" in each
1501 output file.
1502
1503 If your script takes additional data as input, such as a shared configuration
1504 file or a Python module it uses, those files should be listed in the "inputs"
1505 variable. These files are treated as dependencies of each script invocation.
1506
1507 If the command line length is very long, you can use response files to pass
1508 args to your script. See "gn help response_file_contents".
1509
1510 You can dynamically write input dependencies (for incremental rebuilds if an
1511 input file changes) by writing a depfile when the script is run (see "gn help
1512 depfile"). This is more flexible than "inputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001513
Brett Wilson796ed472018-07-16 15:11:09 -07001514 The "deps" and "public_deps" for an action will always be
1515 completed before any part of the action is run so it can depend on
1516 the output of previous steps. The "data_deps" will be built if the
1517 action is built, but may not have completed before all steps of the
1518 action are started. This can give additional parallelism in the build
1519 for runtime-only dependencies.
1520```
1521
1522#### **Outputs**
Julie Hockette2a29402018-07-31 10:11:42 -07001523
Brett Wilson796ed472018-07-16 15:11:09 -07001524```
1525 The script will be executed with the given arguments with the current
1526 directory being that of the root build directory. If you pass files
1527 to your script, see "gn help rebase_path" for how to convert
1528 file names to be relative to the build directory (file names in the
1529 sources, outputs, and inputs will be all treated as relative to the
1530 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001531
1532 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1533 that Ninja will check the timestamp of the output after the action
1534 completes. If output timestamp is unchanged, the step will be treated
1535 as if it never needed to be rebuilt, potentially eliminating some
1536 downstream steps for incremental builds. Scripts can improve build
1537 performance by taking care not to change the timstamp of the output
1538 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001539```
1540
1541#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001542
Brett Wilson796ed472018-07-16 15:11:09 -07001543```
1544 All output files must be inside the output directory of the build.
1545 You would generally use |$target_out_dir| or |$target_gen_dir| to
1546 reference the output or generated intermediate file directories,
1547 respectively.
1548```
1549
1550#### **Variables**
1551
1552```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001553 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1554 cflags_objcc, defines, include_dirs, inputs, ldflags,
1555 lib_dirs, libs, precompiled_header, precompiled_source,
1556 rustenv, rustflags, swiftflags, testonly
1557 Dependent configs: all_dependent_configs, public_configs
1558 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1559 write_runtime_deps
1560 General: check_includes, configs, data, friend, inputs, metadata,
1561 output_extension, output_name, public, sources, testonly,
1562 visibility
1563 Action variables: args, bridge_header, configs, data, depfile,
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07001564 framework_dirs, inputs, mnemonic, module_deps,
1565 module_name, outputs*, pool, response_file_contents,
1566 script*, sources
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001567 * = required
Brett Wilson796ed472018-07-16 15:11:09 -07001568```
1569
1570#### **Example**
1571
1572```
1573 # Runs the script over each IDL file. The IDL script will generate both a .cc
1574 # and a .h file for each input.
1575 action_foreach("my_idl") {
1576 script = "idl_processor.py"
1577 sources = [ "foo.idl", "bar.idl" ]
1578
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07001579 # Causes ninja to output "IDL
1580 # "ACTION
1581 mnemonic = "IDL"
1582
Joe Armstrong73e89072019-05-10 11:23:52 +08001583 # Our script reads this file each time, so we need to list it as a
Brett Wilson796ed472018-07-16 15:11:09 -07001584 # dependency so we can rebuild if it changes.
1585 inputs = [ "my_configuration.txt" ]
1586
1587 # Transformation from source file name to output file names.
1588 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
1589 "$target_gen_dir/{{source_name_part}}.cc" ]
1590
1591 # Note that since "args" is opaque to GN, if you specify paths here, you
1592 # will need to convert it to be relative to the build directory using
1593 # rebase_path().
1594 args = [
1595 "{{source}}",
1596 "-o",
Hector Dearman0d67e272020-12-22 01:37:16 +00001597 rebase_path(target_gen_dir, root_build_dir) +
Brett Wilson796ed472018-07-16 15:11:09 -07001598 "/{{source_name_part}}.h" ]
1599 }
1600```
[email protected]449f3e42024-08-01 21:43:27 +08001601### **bundle_data**: [iOS/macOS] Declare a target without output. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07001602
1603```
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01001604 This target type allows one to declare data that is required at runtime. It is
Brett Wilson796ed472018-07-16 15:11:09 -07001605 used to inform "create_bundle" targets of the files to copy into generated
1606 bundle, see "gn help create_bundle" for help.
1607
1608 The target must define a list of files as "sources" and a single "outputs".
1609 If there are multiple files, source expansions must be used to express the
1610 output. The output must reference a file inside of {{bundle_root_dir}}.
1611
1612 This target can be used on all platforms though it is designed only to
1613 generate iOS/macOS bundle. In cross-platform projects, it is advised to put it
1614 behind iOS/macOS conditionals.
1615
Sylvain Defresne06cdcc82024-03-21 13:48:59 +01001616 If any source files in a bundle_data target match `*/*.xcassets/*` then they
1617 will be considered part of an assets catalog, and instead of being copied to
1618 the final bundle the assets catalog itself will be added to the inputs of the
1619 assets catalog compilation step. See "compile_xcassets" tool.
1620
Brett Wilson796ed472018-07-16 15:11:09 -07001621 See "gn help create_bundle" for more information.
1622```
1623
1624#### **Variables**
1625
1626```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001627 Dependent configs: all_dependent_configs, public_configs
1628 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1629 write_runtime_deps
1630 General: check_includes, configs, data, friend, inputs, metadata,
1631 output_extension, output_name, public, sources, testonly,
1632 visibility
1633 Bundle-specific: sources*, outputs*
Brett Wilson796ed472018-07-16 15:11:09 -07001634 * = required
1635```
1636
1637#### **Examples**
1638
1639```
1640 bundle_data("icudata") {
1641 sources = [ "sources/data/in/icudtl.dat" ]
1642 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
1643 }
1644
1645 bundle_data("base_unittests_bundle_data]") {
1646 sources = [ "test/data" ]
1647 outputs = [
1648 "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" +
1649 "{{source_file_part}}"
1650 ]
1651 }
1652
1653 bundle_data("material_typography_bundle_data") {
1654 sources = [
1655 "src/MaterialTypography.bundle/Roboto-Bold.ttf",
1656 "src/MaterialTypography.bundle/Roboto-Italic.ttf",
1657 "src/MaterialTypography.bundle/Roboto-Regular.ttf",
1658 "src/MaterialTypography.bundle/Roboto-Thin.ttf",
1659 ]
1660 outputs = [
1661 "{{bundle_resources_dir}}/MaterialTypography.bundle/"
1662 "{{source_file_part}}"
1663 ]
1664 }
1665```
[email protected]449f3e42024-08-01 21:43:27 +08001666### **copy**: Declare a target that copies files. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07001667
1668#### **File name handling**
1669
1670```
1671 All output files must be inside the output directory of the build. You would
1672 generally use |$target_out_dir| or |$target_gen_dir| to reference the output
1673 or generated intermediate file directories, respectively.
1674
1675 Both "sources" and "outputs" must be specified. Sources can include as many
1676 files as you want, but there can only be one item in the outputs list (plural
1677 is used for the name for consistency with other target types).
1678
1679 If there is more than one source file, your output name should specify a
1680 mapping from each source file to an output file name using source expansion
1681 (see "gn help source_expansion"). The placeholders will look like
1682 "{{source_name_part}}", for example.
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001683
1684 If you want to copy the output of a previous build step, the target that
1685 generates the file to copy must be reachable from the deps or public_deps of
1686 the copy target.
1687```
1688
1689#### **Variables**
1690
1691```
1692 Dependent configs: all_dependent_configs, public_configs
1693 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1694 write_runtime_deps
1695 General: check_includes, configs, data, friend, inputs, metadata,
1696 output_extension, output_name, public, sources, testonly,
1697 visibility
1698 Copy variables: sources*, outputs*
1699 * = required
Brett Wilson796ed472018-07-16 15:11:09 -07001700```
1701
1702#### **Examples**
1703
1704```
1705 # Write a rule that copies a checked-in DLL to the output directory.
1706 copy("mydll") {
1707 sources = [ "mydll.dll" ]
1708 outputs = [ "$target_out_dir/mydll.dll" ]
1709 }
1710
1711 # Write a rule to copy several files to the target generated files directory.
1712 copy("myfiles") {
1713 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1714
1715 # Use source expansion to generate output files with the corresponding file
1716 # names in the gen dir. This will just copy each file.
1717 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1718 }
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001719
1720 # Copy the output of a generated executable.
1721 copy("package_melon") {
1722 # This example uses get_label_info() to compute the output directory of the
1723 # dependency. This allows the copy rule to work regardless of the toolchain.
1724 #
1725 # In some cases (particularly actions defined previously in the same file)
1726 # you can use get_target_outputs() to get the input file which can eliminate
1727 # the assumptions about the output file name of the dependency.
1728
1729 input_dir = get_label_info("//src/tools/melon", "root_out_dir");
1730 sources = [ "$input_dir/melon" ]
1731
1732 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1733
1734 # Depend on the target to build the file before copying.
1735 deps = [ "//src/tools/melon" ]
1736 }
Brett Wilson796ed472018-07-16 15:11:09 -07001737```
[email protected]449f3e42024-08-01 21:43:27 +08001738### **create_bundle**: [ios/macOS] Build an iOS or macOS bundle. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07001739
1740```
1741 This target generates an iOS or macOS bundle (which is a directory with a
1742 well-know structure). This target does not define any sources, instead they
1743 are computed from all "bundle_data" target this one depends on transitively
1744 (the recursion stops at "create_bundle" targets).
1745
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001746 The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in
1747 "bundle_data" outputs. The properties are optional but must be defined if any
1748 of the "bundle_data" target use them.
Brett Wilson796ed472018-07-16 15:11:09 -07001749
1750 This target can be used on all platforms though it is designed only to
1751 generate iOS or macOS bundle. In cross-platform projects, it is advised to put
1752 it behind iOS/macOS conditionals.
1753
1754 If a create_bundle is specified as a data_deps for another target, the bundle
1755 is considered a leaf, and its public and private dependencies will not
1756 contribute to any data or data_deps. Required runtime dependencies should be
1757 placed in the bundle. A create_bundle can declare its own explicit data and
1758 data_deps, however.
1759```
1760
Sylvain Defresnef07499a2024-04-04 13:06:37 +02001761#### **Post-processing**
Brett Wilson796ed472018-07-16 15:11:09 -07001762
1763```
Sylvain Defresnef07499a2024-04-04 13:06:37 +02001764 Some bundle needs to be post-processed as part of the build (e.g. on iOS all
1765 application needs to be code signed to run on a device). The post processing
1766 step can be configured via the post_processing_script variable.
Brett Wilson796ed472018-07-16 15:11:09 -07001767
Sylvain Defresnef07499a2024-04-04 13:06:37 +02001768 If set, `post_processing_script` is the path of a script that invoked after
1769 all files have been moved into the bundle. The script must not change any file
1770 in the bundle, but may add new files.
Brett Wilson796ed472018-07-16 15:11:09 -07001771
Sylvain Defresnef07499a2024-04-04 13:06:37 +02001772 If `post_processing_script` is defined, then `post_processing_outputs` must
1773 be defined and non-empty to inform when the script needs to be re-run. The
1774 `post_processing_args` will be passed as is to the script (so path have to be
1775 rebased) and additional inputs may be listed via `post_processing_sources`.
1776```
1777
1778#### **Migration**
1779
1780```
1781 The post-processing step used to be limited to code-signing. The properties
1782 used to be named `code_signing_$name` instead of `post_processing_$name`. The
1783 old names are still accepted as alias to facilitate migration but a warning
1784 will be emitted and the alias eventually be removed.
Brett Wilson796ed472018-07-16 15:11:09 -07001785```
1786
1787#### **Variables**
1788
1789```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001790 Dependent configs: all_dependent_configs, public_configs
1791 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1792 write_runtime_deps
1793 General: check_includes, configs, data, friend, inputs, metadata,
1794 output_extension, output_name, public, sources, testonly,
1795 visibility
1796 Bundle vars: bundle_root_dir, bundle_contents_dir, bundle_resources_dir,
1797 bundle_executable_dir, bundle_deps_filter, product_type,
Sylvain Defresnef07499a2024-04-04 13:06:37 +02001798 post_processing_args, post_processing_script,
1799 post_processing_sources, post_processing_outputs,
1800 xcode_extra_attributes, xcode_test_application_name,
1801 partial_info_plist
Brett Wilson796ed472018-07-16 15:11:09 -07001802```
1803
1804#### **Example**
1805
1806```
1807 # Defines a template to create an application. On most platform, this is just
1808 # an alias for an "executable" target, but on iOS/macOS, it builds an
1809 # application bundle.
1810 template("app") {
1811 if (!is_ios && !is_mac) {
1812 executable(target_name) {
1813 forward_variables_from(invoker, "*")
1814 }
1815 } else {
1816 app_name = target_name
1817 gen_path = target_gen_dir
1818
1819 action("${app_name}_generate_info_plist") {
1820 script = [ "//build/ios/ios_gen_plist.py" ]
1821 sources = [ "templates/Info.plist" ]
1822 outputs = [ "$gen_path/Info.plist" ]
1823 args = rebase_path(sources, root_build_dir) +
1824 rebase_path(outputs, root_build_dir)
1825 }
1826
1827 bundle_data("${app_name}_bundle_info_plist") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001828 public_deps = [ ":${app_name}_generate_info_plist" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001829 sources = [ "$gen_path/Info.plist" ]
1830 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
1831 }
1832
1833 executable("${app_name}_generate_executable") {
1834 forward_variables_from(invoker, "*", [
1835 "output_name",
1836 "visibility",
1837 ])
1838 output_name =
1839 rebase_path("$gen_path/$app_name", root_build_dir)
1840 }
1841
1842 code_signing =
1843 defined(invoker.code_signing) && invoker.code_signing
1844
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001845 if (!is_ios || !code_signing) {
Brett Wilson796ed472018-07-16 15:11:09 -07001846 bundle_data("${app_name}_bundle_executable") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001847 public_deps = [ ":${app_name}_generate_executable" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001848 sources = [ "$gen_path/$app_name" ]
1849 outputs = [ "{{bundle_executable_dir}}/$app_name" ]
1850 }
1851 }
1852
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001853 create_bundle("$app_name.app") {
Brett Wilson796ed472018-07-16 15:11:09 -07001854 product_type = "com.apple.product-type.application"
1855
1856 if (is_ios) {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001857 bundle_root_dir = "$root_build_dir/$target_name"
Brett Wilson796ed472018-07-16 15:11:09 -07001858 bundle_contents_dir = bundle_root_dir
1859 bundle_resources_dir = bundle_contents_dir
1860 bundle_executable_dir = bundle_contents_dir
Brett Wilson796ed472018-07-16 15:11:09 -07001861
Sylvain Defresne7b0690e2020-06-26 15:33:01 +02001862 xcode_extra_attributes = {
Brett Wilson796ed472018-07-16 15:11:09 -07001863 ONLY_ACTIVE_ARCH = "YES"
1864 DEBUG_INFORMATION_FORMAT = "dwarf"
1865 }
1866 } else {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001867 bundle_root_dir = "$root_build_dir/$target_name"
1868 bundle_contents_dir = "$bundle_root_dir/Contents"
1869 bundle_resources_dir = "$bundle_contents_dir/Resources"
1870 bundle_executable_dir = "$bundle_contents_dir/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07001871 }
1872 deps = [ ":${app_name}_bundle_info_plist" ]
1873 if (is_ios && code_signing) {
1874 deps += [ ":${app_name}_generate_executable" ]
Sylvain Defresnef07499a2024-04-04 13:06:37 +02001875 post_processing_script = "//build/config/ios/codesign.py"
1876 post_processing_sources = [
Brett Wilson796ed472018-07-16 15:11:09 -07001877 invoker.entitlements_path,
1878 "$target_gen_dir/$app_name",
1879 ]
Sylvain Defresnef07499a2024-04-04 13:06:37 +02001880 post_processing_outputs = [
Brett Wilson796ed472018-07-16 15:11:09 -07001881 "$bundle_root_dir/$app_name",
1882 "$bundle_root_dir/_CodeSignature/CodeResources",
1883 "$bundle_root_dir/embedded.mobileprovision",
1884 "$target_gen_dir/$app_name.xcent",
1885 ]
Sylvain Defresnef07499a2024-04-04 13:06:37 +02001886 post_processing_args = [
1887 "-i=" + ios_post_processing_identity,
Brett Wilson796ed472018-07-16 15:11:09 -07001888 "-b=" + rebase_path(
1889 "$target_gen_dir/$app_name", root_build_dir),
1890 "-e=" + rebase_path(
1891 invoker.entitlements_path, root_build_dir),
1892 "-e=" + rebase_path(
1893 "$target_gen_dir/$app_name.xcent", root_build_dir),
1894 rebase_path(bundle_root_dir, root_build_dir),
1895 ]
1896 } else {
1897 deps += [ ":${app_name}_bundle_executable" ]
1898 }
1899 }
1900 }
1901 }
1902```
[email protected]449f3e42024-08-01 21:43:27 +08001903### **executable**: Declare an executable target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07001904
Julie Hockettce1fa072019-05-07 17:44:37 -07001905#### **Language and compilation**
1906
1907```
1908 The tools and commands used to create this target type will be
1909 determined by the source files in its sources. Targets containing
1910 multiple compiler-incompatible languages are not allowed (e.g. a
1911 target containing both C and C++ sources is acceptable, but a
1912 target containing C and Rust sources is not).
1913```
1914
Brett Wilson796ed472018-07-16 15:11:09 -07001915#### **Variables**
1916
1917```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001918 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1919 cflags_objcc, defines, include_dirs, inputs, ldflags,
1920 lib_dirs, libs, precompiled_header, precompiled_source,
1921 rustenv, rustflags, swiftflags, testonly
1922 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1923 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07001924 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001925 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001926 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08001927 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001928 Rust variables: aliased_deps, crate_root, crate_name
Nate Fischer8ed01d32019-01-08 17:32:01 -08001929```
[email protected]449f3e42024-08-01 21:43:27 +08001930### **generated_file**: Declare a generated_file target. [Back to Top](#gn-reference)
Nate Fischer8ed01d32019-01-08 17:32:01 -08001931
1932```
1933 Writes data value(s) to disk on resolution. This target type mirrors some
1934 functionality of the write_file() function, but also provides the ability to
1935 collect metadata from its dependencies on resolution rather than writing out
Keir Mierle45611e32019-11-12 11:18:00 -08001936 at parse time.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001937
1938 The `outputs` variable is required to be a list with a single element,
1939 specifying the intended location of the output file.
1940
1941 The `output_conversion` variable specified the format to write the
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02001942 value. See `gn help io_conversion`.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001943
Tomasz Åšniatowski46b572c2021-09-03 21:47:17 +02001944 One of `contents` or `data_keys` must be specified; use of `contents` will
1945 write the contents of that value to file, while use of `data_keys` will
1946 trigger a metadata collection walk based on the dependencies of the target and
1947 the optional values of the `rebase` and `walk_keys` variables. See
Nate Fischer8ed01d32019-01-08 17:32:01 -08001948 `gn help metadata`.
1949
1950 Collected metadata, if specified, will be returned in postorder of
1951 dependencies. See the example for details.
1952```
1953
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001954#### **Variables**
1955
1956```
1957 Dependent configs: all_dependent_configs, public_configs
1958 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1959 write_runtime_deps
1960 General: check_includes, configs, data, friend, inputs, metadata,
1961 output_extension, output_name, public, sources, testonly,
1962 visibility
Brett Wilson225e90c2024-08-09 10:17:48 -07001963 Generated file: contents, data_keys, rebase, walk_keys, output_conversion,
1964 outputs
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001965```
1966
Nate Fischer8ed01d32019-01-08 17:32:01 -08001967#### **Example (metadata collection)**
1968
1969```
1970 Given the following targets defined in //base/BUILD.gn, where A depends on B
1971 and B depends on C and D:
1972
1973 group("a") {
1974 metadata = {
1975 doom_melon = [ "enable" ]
1976 my_files = [ "foo.cpp" ]
1977
Keir Mierle45611e32019-11-12 11:18:00 -08001978 # Note: this is functionally equivalent to not defining `my_barrier`
1979 # at all in this target's metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001980 my_barrier = [ "" ]
1981 }
1982
1983 deps = [ ":b" ]
1984 }
1985
Julie Hockettd69a9c32019-01-23 14:36:18 -08001986 group("b") {
Nate Fischer8ed01d32019-01-08 17:32:01 -08001987 metadata = {
1988 my_files = [ "bar.cpp" ]
1989 my_barrier = [ ":c" ]
1990 }
1991
1992 deps = [ ":c", ":d" ]
1993 }
1994
1995 group("c") {
1996 metadata = {
1997 doom_melon = [ "disable" ]
1998 my_files = [ "baz.cpp" ]
1999 }
2000 }
2001
2002 group("d") {
2003 metadata = {
2004 my_files = [ "missing.cpp" ]
2005 }
2006 }
2007
2008 If the following generated_file target is defined:
2009
2010 generated_file("my_files_metadata") {
2011 outputs = [ "$root_build_dir/my_files.json" ]
2012 data_keys = [ "my_files" ]
2013
2014 deps = [ "//base:a" ]
2015 }
2016
2017 The following will be written to "$root_build_dir/my_files.json" (less the
2018 comments):
2019 [
2020 "baz.cpp", // from //base:c via //base:b
2021 "missing.cpp" // from //base:d via //base:b
2022 "bar.cpp", // from //base:b via //base:a
2023 "foo.cpp", // from //base:a
2024 ]
2025
2026 Alternatively, as an example of using walk_keys, if the following
2027 generated_file target is defined:
2028
2029 generated_file("my_files_metadata") {
2030 outputs = [ "$root_build_dir/my_files.json" ]
2031 data_keys = [ "my_files" ]
2032 walk_keys = [ "my_barrier" ]
2033
2034 deps = [ "//base:a" ]
2035 }
2036
2037 The following will be written to "$root_build_dir/my_files.json" (again less
2038 the comments):
2039 [
2040 "baz.cpp", // from //base:c via //base:b
2041 "bar.cpp", // from //base:b via //base:a
2042 "foo.cpp", // from //base:a
2043 ]
2044
2045 If `rebase` is used in the following generated_file target:
2046
2047 generated_file("my_files_metadata") {
2048 outputs = [ "$root_build_dir/my_files.json" ]
2049 data_keys = [ "my_files" ]
2050 walk_keys = [ "my_barrier" ]
2051 rebase = root_build_dir
2052
2053 deps = [ "//base:a" ]
2054 }
2055
2056 The following will be written to "$root_build_dir/my_files.json" (again less
2057 the comments) (assuming root_build_dir = "//out"):
2058 [
2059 "../base/baz.cpp", // from //base:c via //base:b
2060 "../base/bar.cpp", // from //base:b via //base:a
2061 "../base/foo.cpp", // from //base:a
2062 ]
2063```
[email protected]449f3e42024-08-01 21:43:27 +08002064### **group**: Declare a named group of targets. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002065
2066```
2067 This target type allows you to create meta-targets that just collect a set of
2068 dependencies into one named target. Groups can additionally specify configs
2069 that apply to their dependents.
2070```
2071
2072#### **Variables**
2073
2074```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002075 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2076 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002077 Dependent configs: all_dependent_configs, public_configs
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002078 General: check_includes, configs, data, friend, inputs, metadata,
2079 output_extension, output_name, public, sources, testonly,
2080 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002081```
2082
2083#### **Example**
2084
2085```
2086 group("all") {
2087 deps = [
2088 "//project:runner",
2089 "//project:unit_tests",
2090 ]
2091 }
2092```
[email protected]449f3e42024-08-01 21:43:27 +08002093### **loadable_module**: Declare a loadable module target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002094
2095```
2096 This target type allows you to create an object file that is (and can only
2097 be) loaded and unloaded at runtime.
2098
2099 A loadable module will be specified on the linker line for targets listing
2100 the loadable module in its "deps". If you don't want this (if you don't need
2101 to dynamically load the library at runtime), then you should use a
2102 "shared_library" target type instead.
2103```
2104
Julie Hockettce1fa072019-05-07 17:44:37 -07002105#### **Language and compilation**
2106
2107```
2108 The tools and commands used to create this target type will be
2109 determined by the source files in its sources. Targets containing
2110 multiple compiler-incompatible languages are not allowed (e.g. a
2111 target containing both C and C++ sources is acceptable, but a
2112 target containing C and Rust sources is not).
2113```
2114
Brett Wilson796ed472018-07-16 15:11:09 -07002115#### **Variables**
2116
2117```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002118 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2119 cflags_objcc, defines, include_dirs, inputs, ldflags,
2120 lib_dirs, libs, precompiled_header, precompiled_source,
2121 rustenv, rustflags, swiftflags, testonly
2122 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2123 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002124 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002125 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002126 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002127 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002128 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Julie Hockettce1fa072019-05-07 17:44:37 -07002129```
[email protected]449f3e42024-08-01 21:43:27 +08002130### **rust_library**: Declare a Rust library target. [Back to Top](#gn-reference)
Julie Hockettce1fa072019-05-07 17:44:37 -07002131
2132```
2133 A Rust library is an archive containing additional rust-c provided metadata.
2134 These are the files produced by the rustc compiler with the `.rlib`
2135 extension, and are the intermediate step for most Rust-based binaries.
2136```
2137
2138#### **Language and compilation**
2139
2140```
2141 The tools and commands used to create this target type will be
2142 determined by the source files in its sources. Targets containing
2143 multiple compiler-incompatible languages are not allowed (e.g. a
2144 target containing both C and C++ sources is acceptable, but a
2145 target containing C and Rust sources is not).
2146```
2147
2148#### **Variables**
2149
2150```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002151 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2152 cflags_objcc, defines, include_dirs, inputs, ldflags,
2153 lib_dirs, libs, precompiled_header, precompiled_source,
2154 rustenv, rustflags, swiftflags, testonly
2155 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2156 write_runtime_deps
Julie Hockettce1fa072019-05-07 17:44:37 -07002157 Dependent configs: all_dependent_configs, public_configs
2158 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002159 output_extension, output_name, public, sources, testonly,
Julie Hockettce1fa072019-05-07 17:44:37 -07002160 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002161 Rust variables: aliased_deps, crate_root, crate_name
Brett Wilson796ed472018-07-16 15:11:09 -07002162```
[email protected]449f3e42024-08-01 21:43:27 +08002163### **rust_proc_macro**: Declare a Rust procedural macro target. [Back to Top](#gn-reference)
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002164
2165```
2166 A Rust procedural macro allows creating syntax extensions as execution of a
2167 function. They are compiled as dynamic libraries and used by the compiler at
2168 runtime.
2169
2170 Their use is the same as of other Rust libraries, but their build has some
2171 additional restrictions in terms of supported flags.
2172```
2173
2174#### **Language and compilation**
2175
2176```
2177 The tools and commands used to create this target type will be
2178 determined by the source files in its sources. Targets containing
2179 multiple compiler-incompatible languages are not allowed (e.g. a
2180 target containing both C and C++ sources is acceptable, but a
2181 target containing C and Rust sources is not).
2182```
2183
2184#### **Variables**
2185
2186```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002187 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2188 cflags_objcc, defines, include_dirs, inputs, ldflags,
2189 lib_dirs, libs, precompiled_header, precompiled_source,
2190 rustenv, rustflags, swiftflags, testonly
2191 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2192 write_runtime_deps
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002193 Dependent configs: all_dependent_configs, public_configs
2194 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002195 output_extension, output_name, public, sources, testonly,
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002196 visibility
2197 Rust variables: aliased_deps, crate_root, crate_name
2198```
[email protected]449f3e42024-08-01 21:43:27 +08002199### **shared_library**: Declare a shared library target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002200
2201```
2202 A shared library will be specified on the linker line for targets listing the
2203 shared library in its "deps". If you don't want this (say you dynamically
2204 load the library at runtime), then you should depend on the shared library
2205 via "data_deps" or, on Darwin platforms, use a "loadable_module" target type
2206 instead.
2207```
2208
Julie Hockettce1fa072019-05-07 17:44:37 -07002209#### **Language and compilation**
2210
2211```
2212 The tools and commands used to create this target type will be
2213 determined by the source files in its sources. Targets containing
2214 multiple compiler-incompatible languages are not allowed (e.g. a
2215 target containing both C and C++ sources is acceptable, but a
2216 target containing C and Rust sources is not).
2217```
2218
Brett Wilson796ed472018-07-16 15:11:09 -07002219#### **Variables**
2220
2221```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002222 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2223 cflags_objcc, defines, include_dirs, inputs, ldflags,
2224 lib_dirs, libs, precompiled_header, precompiled_source,
2225 rustenv, rustflags, swiftflags, testonly
2226 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2227 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002228 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002229 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002230 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002231 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002232 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Brett Wilson796ed472018-07-16 15:11:09 -07002233```
[email protected]449f3e42024-08-01 21:43:27 +08002234### **source_set**: Declare a source set target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002235
2236```
Robert Sesekd0a6f072020-05-15 11:21:22 -04002237 Only C-language source sets are supported at the moment.
Julie Hockettce1fa072019-05-07 17:44:37 -07002238```
2239
2240#### **C-language source_sets**
2241
2242```
Brett Wilson796ed472018-07-16 15:11:09 -07002243 A source set is a collection of sources that get compiled, but are not linked
2244 to produce any kind of library. Instead, the resulting object files are
2245 implicitly added to the linker line of all targets that depend on the source
2246 set.
2247
2248 In most cases, a source set will behave like a static library, except no
2249 actual library file will be produced. This will make the build go a little
2250 faster by skipping creation of a large static library, while maintaining the
2251 organizational benefits of focused build targets.
2252
2253 The main difference between a source set and a static library is around
2254 handling of exported symbols. Most linkers assume declaring a function
2255 exported means exported from the static library. The linker can then do dead
2256 code elimination to delete code not reachable from exported functions.
2257
2258 A source set will not do this code elimination since there is no link step.
Julie Hockette2a29402018-07-31 10:11:42 -07002259 This allows you to link many source sets into a shared library and have the
Brett Wilson796ed472018-07-16 15:11:09 -07002260 "exported symbol" notation indicate "export from the final shared library and
2261 not from the intermediate targets." There is no way to express this concept
2262 when linking multiple static libraries into a shared library.
2263```
2264
2265#### **Variables**
2266
2267```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002268 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2269 cflags_objcc, defines, include_dirs, inputs, ldflags,
2270 lib_dirs, libs, precompiled_header, precompiled_source,
2271 rustenv, rustflags, swiftflags, testonly
2272 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2273 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002274 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002275 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002276 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002277 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002278```
[email protected]449f3e42024-08-01 21:43:27 +08002279### **static_library**: Declare a static library target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002280
2281```
2282 Make a ".a" / ".lib" file.
2283
2284 If you only need the static library for intermediate results in the build,
2285 you should consider a source_set instead since it will skip the (potentially
2286 slow) step of creating the intermediate library file.
2287```
2288
2289#### **Variables**
2290
2291```
2292 complete_static_lib
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002293 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2294 cflags_objcc, defines, include_dirs, inputs, ldflags,
2295 lib_dirs, libs, precompiled_header, precompiled_source,
2296 rustenv, rustflags, swiftflags, testonly
2297 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2298 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002299 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002300 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002301 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002302 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002303 Rust variables: aliased_deps, crate_root, crate_name
Julie Hockettce1fa072019-05-07 17:44:37 -07002304
2305 The tools and commands used to create this target type will be
2306 determined by the source files in its sources. Targets containing
2307 multiple compiler-incompatible languages are not allowed (e.g. a
2308 target containing both C and C++ sources is acceptable, but a
2309 target containing C and Rust sources is not).
Brett Wilson796ed472018-07-16 15:11:09 -07002310```
[email protected]449f3e42024-08-01 21:43:27 +08002311### **target**: Declare a target with the given programmatic type. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002312
2313```
2314 target(target_type_string, target_name_string) { ... }
2315
2316 The target() function is a way to invoke a built-in target or template with a
2317 type determined at runtime. This is useful for cases where the type of a
2318 target might not be known statically.
2319
2320 Only templates and built-in target functions are supported for the
2321 target_type_string parameter. Arbitrary functions, configs, and toolchains
2322 are not supported.
2323
2324 The call:
2325 target("source_set", "doom_melon") {
2326 Is equivalent to:
2327 source_set("doom_melon") {
2328```
2329
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002330#### **Common target variables**
2331
2332```
2333 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2334 write_runtime_deps
2335 Dependent configs: all_dependent_configs, public_configs
2336 General: check_includes, configs, data, friend, inputs, metadata,
2337 output_extension, output_name, public, sources, testonly,
2338 visibility
2339
2340 Targets will also have variables specific to that type, see "gn help "
2341 for more.
2342```
2343
Brett Wilson796ed472018-07-16 15:11:09 -07002344#### **Example**
2345
2346```
2347 if (foo_build_as_shared) {
2348 my_type = "shared_library"
2349 } else {
2350 my_type = "source_set"
2351 }
2352
2353 target(my_type, "foo") {
2354 ...
2355 }
2356```
2357## Buildfile functions
2358
[email protected]449f3e42024-08-01 21:43:27 +08002359### **assert**: Assert an expression is true at generation time. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002360
2361```
2362 assert( [, ])
2363
2364 If the condition is false, the build will fail with an error. If the
2365 optional second argument is provided, that string will be printed
2366 with the error message.
2367```
2368
2369#### **Examples**
2370
2371```
2372 assert(is_win)
2373 assert(defined(sources), "Sources must be defined");
2374```
[email protected]449f3e42024-08-01 21:43:27 +08002375### **config**: Defines a configuration object. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002376
2377```
2378 Configuration objects can be applied to targets and specify sets of compiler
2379 flags, includes, defines, etc. They provide a way to conveniently group sets
2380 of this configuration information.
2381
2382 A config is referenced by its label just like a target.
2383
2384 The values in a config are additive only. If you want to remove a flag you
2385 need to remove the corresponding config that sets it. The final set of flags,
2386 defines, etc. for a target is generated in this order:
2387
Nico Weber693f9fb2021-10-08 15:33:57 -04002388 1. The values specified directly on the target (rather than using a config).
Brett Wilson796ed472018-07-16 15:11:09 -07002389 2. The configs specified in the target's "configs" list, in order.
2390 3. Public_configs from a breadth-first traversal of the dependency tree in
2391 the order that the targets appear in "deps".
2392 4. All dependent configs from a breadth-first traversal of the dependency
2393 tree in the order that the targets appear in "deps".
2394```
2395
Joe Armstrong39413942019-03-15 10:34:03 +08002396#### **More background**
2397
2398```
2399 Configs solve a problem where the build system needs to have a higher-level
2400 understanding of various compiler settings. For example, some compiler flags
2401 have to appear in a certain order relative to each other, some settings like
2402 defines and flags logically go together, and the build system needs to
2403 de-duplicate flags even though raw command-line parameters can't always be
2404 operated on in that way.
2405
2406 The config gives a name to a group of settings that can then be reasoned
2407 about by GN. GN can know that configs with the same label are the same thing
2408 so can be de-duplicated. It allows related settings to be grouped so they
2409 are added or removed as a unit. And it allows targets to refer to settings
2410 with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than
2411 having to hard-coding every compiler's flags each time they are referred to.
2412```
2413
Brett Wilson796ed472018-07-16 15:11:09 -07002414#### **Variables valid in a config definition**
Joe Armstrong39413942019-03-15 10:34:03 +08002415
Brett Wilson796ed472018-07-16 15:11:09 -07002416```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002417 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2418 cflags_objcc, defines, include_dirs, inputs, ldflags,
2419 lib_dirs, libs, precompiled_header, precompiled_source,
2420 rustenv, rustflags, swiftflags, testonly
Brett Wilson796ed472018-07-16 15:11:09 -07002421 Nested configs: configs
Tomasz Åšniatowski46b572c2021-09-03 21:47:17 +02002422 General: visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002423```
2424
2425#### **Variables on a target used to apply configs**
2426
2427```
2428 all_dependent_configs, configs, public_configs
2429```
2430
2431#### **Example**
2432
2433```
2434 config("myconfig") {
Joe Armstrongf5ad8072019-05-08 08:42:56 +08002435 include_dirs = [ "include/common" ]
Brett Wilson796ed472018-07-16 15:11:09 -07002436 defines = [ "ENABLE_DOOM_MELON" ]
2437 }
2438
2439 executable("mything") {
2440 configs = [ ":myconfig" ]
2441 }
2442```
[email protected]449f3e42024-08-01 21:43:27 +08002443### **declare_args**: Declare build arguments. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002444
2445```
2446 Introduces the given arguments into the current scope. If they are not
2447 specified on the command line or in a toolchain's arguments, the default
2448 values given in the declare_args block will be used. However, these defaults
2449 will not override command-line values.
2450
2451 See also "gn help buildargs" for an overview.
2452
2453 The precise behavior of declare args is:
2454
2455 1. The declare_args() block executes. Any variable defined in the enclosing
2456 scope is available for reading, but any variable defined earlier in
2457 the current scope is not (since the overrides haven't been applied yet).
2458
2459 2. At the end of executing the block, any variables set within that scope
Petr Hosek7617b592019-10-02 09:52:21 -07002460 are saved, with the values specified in the block used as the "default value"
2461 for that argument. Once saved, these variables are available for override
2462 via args.gn.
Brett Wilson796ed472018-07-16 15:11:09 -07002463
2464 3. User-defined overrides are applied. Anything set in "gn args" now
2465 overrides any default values. The resulting set of variables is promoted
2466 to be readable from the following code in the file.
2467
2468 This has some ramifications that may not be obvious:
2469
2470 - You should not perform difficult work inside a declare_args block since
2471 this only sets a default value that may be discarded. In particular,
2472 don't use the result of exec_script() to set the default value. If you
2473 want to have a script-defined default, set some default "undefined" value
2474 like [], "", or -1, and after the declare_args block, call exec_script if
2475 the value is unset by the user.
2476
2477 - Because you cannot read the value of a variable defined in the same
2478 block, if you need to make the default value of one arg depend
2479 on the possibly-overridden value of another, write two separate
2480 declare_args() blocks:
2481
2482 declare_args() {
2483 enable_foo = true
2484 }
2485 declare_args() {
2486 # Bar defaults to same user-overridden state as foo.
2487 enable_bar = enable_foo
2488 }
2489```
2490
2491#### **Example**
2492
2493```
2494 declare_args() {
2495 enable_teleporter = true
2496 enable_doom_melon = false
2497 }
2498
2499 If you want to override the (default disabled) Doom Melon:
2500 gn --args="enable_doom_melon=true enable_teleporter=true"
2501 This also sets the teleporter, but it's already defaulted to on so it will
2502 have no effect.
2503```
[email protected]449f3e42024-08-01 21:43:27 +08002504### **defined**: Returns whether an identifier is defined. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002505
2506```
2507 Returns true if the given argument is defined. This is most useful in
2508 templates to assert that the caller set things up properly.
2509
2510 You can pass an identifier:
2511 defined(foo)
2512 which will return true or false depending on whether foo is defined in the
2513 current scope.
2514
2515 You can also check a named scope:
2516 defined(foo.bar)
2517 which will return true or false depending on whether bar is defined in the
2518 named scope foo. It will throw an error if foo is not defined or is not a
2519 scope.
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07002520
2521 You can also check a named scope using a subscript string expression:
2522 defined(foo[bar + "_name"])
Takuto Ikuta26aa46c2023-05-09 16:28:45 +09002523 which will return true or false depending on whether the subscript
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07002524 expression expands to the name of a member of the scope foo. It will
2525 throw an error if foo is not defined or is not a scope, or if the
2526 expression does not expand to a string, or if it is an empty string.
Brett Wilson796ed472018-07-16 15:11:09 -07002527```
2528
2529#### **Example**
2530
2531```
2532 template("mytemplate") {
2533 # To help users call this template properly...
2534 assert(defined(invoker.sources), "Sources must be defined")
2535
2536 # If we want to accept an optional "values" argument, we don't
2537 # want to dereference something that may not be defined.
2538 if (defined(invoker.values)) {
2539 values = invoker.values
2540 } else {
2541 values = "some default value"
2542 }
2543 }
2544```
[email protected]449f3e42024-08-01 21:43:27 +08002545### **exec_script**: Synchronously run a script and return the output. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002546
2547```
2548 exec_script(filename,
2549 arguments = [],
2550 input_conversion = "",
2551 file_dependencies = [])
2552
2553 Runs the given script, returning the stdout of the script. The build
2554 generation will fail if the script does not exist or returns a nonzero exit
2555 code.
2556
2557 The current directory when executing the script will be the root build
2558 directory. If you are passing file names, you will want to use the
2559 rebase_path() function to make file names relative to this path (see "gn help
2560 rebase_path").
Gary Miguelce7fa362018-09-17 12:48:17 -07002561
2562 The default script interpreter is Python ("python" on POSIX, "python.exe" or
2563 "python.bat" on Windows). This can be configured by the script_executable
2564 variable, see "gn help dotfile".
Brett Wilson796ed472018-07-16 15:11:09 -07002565```
2566
2567#### **Arguments**:
2568
2569```
2570 filename:
Gary Miguelce7fa362018-09-17 12:48:17 -07002571 File name of script to execute. Non-absolute names will be treated as
2572 relative to the current build file.
Brett Wilson796ed472018-07-16 15:11:09 -07002573
2574 arguments:
2575 A list of strings to be passed to the script as arguments. May be
2576 unspecified or the empty list which means no arguments.
2577
2578 input_conversion:
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02002579 Controls how the file is read and parsed. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07002580
2581 If unspecified, defaults to the empty string which causes the script
2582 result to be discarded. exec script will return None.
2583
2584 dependencies:
2585 (Optional) A list of files that this script reads or otherwise depends
2586 on. These dependencies will be added to the build result such that if any
2587 of them change, the build will be regenerated and the script will be
2588 re-run.
2589
2590 The script itself will be an implicit dependency so you do not need to
2591 list it.
2592```
2593
2594#### **Example**
2595
2596```
2597 all_lines = exec_script(
2598 "myscript.py", [some_input], "list lines",
2599 [ rebase_path("data_file.txt", root_build_dir) ])
2600
2601 # This example just calls the script with no arguments and discards the
2602 # result.
2603 exec_script("//foo/bar/myscript.py")
2604```
[email protected]449f3e42024-08-01 21:43:27 +08002605### **filter_exclude**: Remove values that match a set of patterns. [Back to Top](#gn-reference)
Sylvain Defresneaff489a2020-03-11 18:27:43 +01002606
2607```
2608 filter_exclude(values, exclude_patterns)
2609
2610 The argument values must be a list of strings.
2611
2612 The argument exclude_patterns must be a list of file patterns (see
2613 "gn help file_pattern"). Any elements in values matching at least one
2614 of those patterns will be excluded.
2615```
2616
2617#### **Examples**
2618```
2619 values = [ "foo.cc", "foo.h", "foo.proto" ]
2620 result = filter_exclude(values, [ "*.proto" ])
2621 # result will be [ "foo.cc", "foo.h" ]
2622```
[email protected]449f3e42024-08-01 21:43:27 +08002623### **filter_include**: Remove values that do not match a set of patterns. [Back to Top](#gn-reference)
Sylvain Defresneaff489a2020-03-11 18:27:43 +01002624
2625```
2626 filter_include(values, include_patterns)
2627
2628 The argument values must be a list of strings.
2629
2630 The argument include_patterns must be a list of file patterns (see
2631 "gn help file_pattern"). Only elements from values matching at least
2632 one of the pattern will be included.
2633```
2634
2635#### **Examples**
2636```
2637 values = [ "foo.cc", "foo.h", "foo.proto" ]
2638 result = filter_include(values, [ "*.proto" ])
2639 # result will be [ "foo.proto" ]
2640```
[email protected]449f3e42024-08-01 21:43:27 +08002641### **filter_labels_exclude**: Remove labels that match a set of patterns. [Back to Top](#gn-reference)
Aaron Woode05c0aa2024-02-14 13:40:53 -08002642
2643```
2644 filter_labels_exclude(labels, exclude_patterns)
2645
2646 The argument labels must be a list of strings.
2647
2648 The argument exclude_patterns must be a list of label patterns (see
2649 "gn help label_pattern"). Only elements from labels matching at least
2650 one of the patterns will be excluded.
2651```
2652
2653#### **Examples**
2654```
2655 labels = [ "//foo:baz", "//foo/bar:baz", "//bar:baz" ]
2656 result = filter_labels_exclude(labels, [ "//foo:*" ])
2657 # result will be [ "//foo/bar:baz", "//bar:baz" ]
2658```
[email protected]449f3e42024-08-01 21:43:27 +08002659### **filter_labels_include**: Remove labels that do not match a set of patterns. [Back to Top](#gn-reference)
Aaron Woode05c0aa2024-02-14 13:40:53 -08002660
2661```
2662 filter_labels_include(labels, include_patterns)
2663
2664 The argument labels must be a list of strings.
2665
2666 The argument include_patterns must be a list of label patterns (see
2667 "gn help label_pattern"). Only elements from labels matching at least
2668 one of the patterns will be included.
2669```
2670
2671#### **Examples**
2672```
2673 labels = [ "//foo:baz", "//foo/bar:baz", "//bar:baz" ]
2674 result = filter_labels_include(labels, [ "//foo:*" ])
2675 # result will be [ "//foo:baz" ]
2676```
[email protected]449f3e42024-08-01 21:43:27 +08002677### **foreach**: Iterate over a list. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002678
2679```
2680 foreach(, ) {
2681
2682 }
2683
2684 Executes the loop contents block over each item in the list, assigning the
2685 loop_var to each item in sequence. The will be a copy so assigning
2686 to it will not mutate the list. The loop will iterate over a copy of
2687 so mutating it inside the loop will not affect iteration.
2688
2689 The block does not introduce a new scope, so that variable assignments inside
2690 the loop will be visible once the loop terminates.
2691
2692 The loop variable will temporarily shadow any existing variables with the
2693 same name for the duration of the loop. After the loop terminates the loop
2694 variable will no longer be in scope, and the previous value (if any) will be
2695 restored.
2696```
2697
2698#### **Example**
2699
2700```
2701 mylist = [ "a", "b", "c" ]
2702 foreach(i, mylist) {
2703 print(i)
2704 }
2705
2706 Prints:
2707 a
2708 b
2709 c
2710```
[email protected]449f3e42024-08-01 21:43:27 +08002711### **forward_variables_from**: Copies variables from a different scope. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002712
2713```
2714 forward_variables_from(from_scope, variable_list_or_star,
2715 variable_to_not_forward_list = [])
2716
2717 Copies the given variables from the given scope to the local scope if they
2718 exist. This is normally used in the context of templates to use the values of
2719 variables defined in the template invocation to a template-defined target.
2720
2721 The variables in the given variable_list will be copied if they exist in the
2722 given scope or any enclosing scope. If they do not exist, nothing will happen
2723 and they be left undefined in the current scope.
2724
2725 As a special case, if the variable_list is a string with the value of "*",
2726 all variables from the given scope will be copied. "*" only copies variables
2727 set directly on the from_scope, not enclosing ones. Otherwise it would
2728 duplicate all global variables.
2729
2730 When an explicit list of variables is supplied, if the variable exists in the
2731 current (destination) scope already, an error will be thrown. If "*" is
2732 specified, variables in the current scope will be clobbered (the latter is
2733 important because most targets have an implicit configs list, which means it
2734 wouldn't work at all if it didn't clobber).
2735
Brett Wilson796ed472018-07-16 15:11:09 -07002736 If variables_to_not_forward_list is non-empty, then it must contains a list
2737 of variable names that will not be forwarded. This is mostly useful when
2738 variable_list_or_star has a value of "*".
2739```
2740
2741#### **Examples**
2742
2743```
Andrew Grieve57a964c2018-09-14 10:50:56 -04002744 # forward_variables_from(invoker, ["foo"])
2745 # is equivalent to:
2746 assert(!defined(foo))
2747 if (defined(invoker.foo)) {
2748 foo = invoker.foo
2749 }
2750
Brett Wilson796ed472018-07-16 15:11:09 -07002751 # This is a common action template. It would invoke a script with some given
2752 # parameters, and wants to use the various types of deps and the visibility
2753 # from the invoker if it's defined. It also injects an additional dependency
2754 # to all targets.
2755 template("my_test") {
2756 action(target_name) {
2757 forward_variables_from(invoker, [ "data_deps", "deps",
Julie Hockette2a29402018-07-31 10:11:42 -07002758 "public_deps", "visibility"])
Brett Wilson796ed472018-07-16 15:11:09 -07002759 # Add our test code to the dependencies.
2760 # "deps" may or may not be defined at this point.
2761 if (defined(deps)) {
2762 deps += [ "//tools/doom_melon" ]
2763 } else {
2764 deps = [ "//tools/doom_melon" ]
2765 }
2766 }
2767 }
2768
Julie Hockette2a29402018-07-31 10:11:42 -07002769 # This is a template around a target whose type depends on a global variable.
2770 # It forwards all values from the invoker.
Brett Wilson796ed472018-07-16 15:11:09 -07002771 template("my_wrapper") {
2772 target(my_wrapper_target_type, target_name) {
2773 forward_variables_from(invoker, "*")
2774 }
2775 }
2776
2777 # A template that wraps another. It adds behavior based on one
2778 # variable, and forwards all others to the nested target.
2779 template("my_ios_test_app") {
2780 ios_test_app(target_name) {
2781 forward_variables_from(invoker, "*", ["test_bundle_name"])
2782 if (!defined(extra_substitutions)) {
2783 extra_substitutions = []
2784 }
2785 extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ]
2786 }
2787 }
2788```
[email protected]449f3e42024-08-01 21:43:27 +08002789### **get_label_info**: Get an attribute from a target's label. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002790
2791```
2792 get_label_info(target_label, what)
2793
2794 Given the label of a target, returns some attribute of that target. The
2795 target need not have been previously defined in the same file, since none of
2796 the attributes depend on the actual target definition, only the label itself.
2797
2798 See also "gn help get_target_outputs".
2799```
2800
2801#### **Possible values for the "what" parameter**
2802
2803```
2804 "name"
2805 The short name of the target. This will match the value of the
2806 "target_name" variable inside that target's declaration. For the label
2807 "//foo/bar:baz" this will return "baz".
2808
2809 "dir"
2810 The directory containing the target's definition, with no slash at the
2811 end. For the label "//foo/bar:baz" this will return "//foo/bar".
2812
2813 "target_gen_dir"
2814 The generated file directory for the target. This will match the value of
2815 the "target_gen_dir" variable when inside that target's declaration.
2816
2817 "root_gen_dir"
2818 The root of the generated file tree for the target. This will match the
2819 value of the "root_gen_dir" variable when inside that target's
2820 declaration.
2821
2822 "target_out_dir
2823 The output directory for the target. This will match the value of the
2824 "target_out_dir" variable when inside that target's declaration.
2825
2826 "root_out_dir"
2827 The root of the output file tree for the target. This will match the
2828 value of the "root_out_dir" variable when inside that target's
2829 declaration.
2830
2831 "label_no_toolchain"
2832 The fully qualified version of this label, not including the toolchain.
2833 For the input ":bar" it might return "//foo:bar".
2834
2835 "label_with_toolchain"
2836 The fully qualified version of this label, including the toolchain. For
2837 the input ":bar" it might return "//foo:bar(//toolchain:x64)".
2838
2839 "toolchain"
2840 The label of the toolchain. This will match the value of the
2841 "current_toolchain" variable when inside that target's declaration.
2842```
2843
2844#### **Examples**
2845
2846```
2847 get_label_info(":foo", "name")
2848 # Returns string "foo".
2849
2850 get_label_info("//foo/bar:baz", "target_gen_dir")
2851 # Returns string "//out/Debug/gen/foo/bar".
2852```
[email protected]449f3e42024-08-01 21:43:27 +08002853### **get_path_info**: Extract parts of a file or directory name. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002854
2855```
2856 get_path_info(input, what)
2857
2858 The first argument is either a string representing a file or directory name,
2859 or a list of such strings. If the input is a list the return value will be a
2860 list containing the result of applying the rule to each item in the input.
2861```
2862
2863#### **Possible values for the "what" parameter**
2864
2865```
2866 "file"
2867 The substring after the last slash in the path, including the name and
2868 extension. If the input ends in a slash, the empty string will be
2869 returned.
2870 "foo/bar.txt" => "bar.txt"
2871 "bar.txt" => "bar.txt"
2872 "foo/" => ""
2873 "" => ""
2874
2875 "name"
2876 The substring of the file name not including the extension.
2877 "foo/bar.txt" => "bar"
2878 "foo/bar" => "bar"
2879 "foo/" => ""
2880
2881 "extension"
2882 The substring following the last period following the last slash, or the
2883 empty string if not found. The period is not included.
2884 "foo/bar.txt" => "txt"
2885 "foo/bar" => ""
2886
2887 "dir"
2888 The directory portion of the name, not including the slash.
2889 "foo/bar.txt" => "foo"
2890 "//foo/bar" => "//foo"
2891 "foo" => "."
2892
2893 The result will never end in a slash, so if the resulting is empty, the
2894 system ("/") or source ("//") roots, a "." will be appended such that it
2895 is always legal to append a slash and a filename and get a valid path.
2896
2897 "out_dir"
2898 The output file directory corresponding to the path of the given file,
2899 not including a trailing slash.
2900 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
2901
2902 "gen_dir"
2903 The generated file directory corresponding to the path of the given file,
2904 not including a trailing slash.
2905 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
2906
2907 "abspath"
2908 The full absolute path name to the file or directory. It will be resolved
2909 relative to the current directory, and then the source- absolute version
2910 will be returned. If the input is system- absolute, the same input will
2911 be returned.
2912 "foo/bar.txt" => "//mydir/foo/bar.txt"
2913 "foo/" => "//mydir/foo/"
2914 "//foo/bar" => "//foo/bar" (already absolute)
2915 "/usr/include" => "/usr/include" (already absolute)
2916
2917 If you want to make the path relative to another directory, or to be
2918 system-absolute, see rebase_path().
2919```
2920
2921#### **Examples**
2922```
2923 sources = [ "foo.cc", "foo.h" ]
2924 result = get_path_info(source, "abspath")
2925 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
2926
2927 result = get_path_info("//foo/bar/baz.cc", "dir")
2928 # result will be "//foo/bar"
2929
2930 # Extract the source-absolute directory name,
Joe Armstrong39413942019-03-15 10:34:03 +08002931 result = get_path_info(get_path_info(path, "dir"), "abspath")
Brett Wilson796ed472018-07-16 15:11:09 -07002932```
[email protected]449f3e42024-08-01 21:43:27 +08002933### **get_target_outputs**: [file list] Get the list of outputs from a target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002934
2935```
2936 get_target_outputs(target_label)
2937
2938 Returns a list of output files for the named target. The named target must
2939 have been previously defined in the current file before this function is
2940 called (it can't reference targets in other files because there isn't a
2941 defined execution order, and it obviously can't reference targets that are
2942 defined after the function call).
2943
Julie Hockettce1fa072019-05-07 17:44:37 -07002944 Only copy, generated_file, and action targets are supported. The outputs from
2945 binary targets will depend on the toolchain definition which won't
2946 necessarily have been loaded by the time a given line of code has run, and
2947 source sets and groups have no useful output file.
Brett Wilson796ed472018-07-16 15:11:09 -07002948```
2949
2950#### **Return value**
2951
2952```
2953 The names in the resulting list will be absolute file paths (normally like
2954 "//out/Debug/bar.exe", depending on the build directory).
2955
Julie Hockettce1fa072019-05-07 17:44:37 -07002956 action, copy, and generated_file targets: this will just return the files
2957 specified in the "outputs" variable of the target.
Brett Wilson796ed472018-07-16 15:11:09 -07002958
2959 action_foreach targets: this will return the result of applying the output
2960 template to the sources (see "gn help source_expansion"). This will be the
2961 same result (though with guaranteed absolute file paths), as
2962 process_file_template will return for those inputs (see "gn help
2963 process_file_template").
2964
Brett Wilson796ed472018-07-16 15:11:09 -07002965 source sets and groups: this will return a list containing the path of the
Takuto Ikuta50ecf4c2024-08-19 18:43:24 +09002966 phony target that Ninja completes once all outputs are generated. This
Brett Wilson796ed472018-07-16 15:11:09 -07002967 probably isn't very useful.
2968```
2969
2970#### **Example**
2971
2972```
2973 # Say this action generates a bunch of C source files.
2974 action_foreach("my_action") {
2975 sources = [ ... ]
2976 outputs = [ ... ]
2977 }
2978
2979 # Compile the resulting source files into a source set.
2980 source_set("my_lib") {
2981 sources = get_target_outputs(":my_action")
2982 }
2983```
[email protected]449f3e42024-08-01 21:43:27 +08002984### **getenv**: Get an environment variable. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07002985
2986```
2987 value = getenv(env_var_name)
2988
2989 Returns the value of the given environment variable. If the value is not
2990 found, it will try to look up the variable with the "opposite" case (based on
2991 the case of the first letter of the variable), but is otherwise
2992 case-sensitive.
2993
2994 If the environment variable is not found, the empty string will be returned.
2995 Note: it might be nice to extend this if we had the concept of "none" in the
2996 language to indicate lookup failure.
2997```
2998
2999#### **Example**
3000
3001```
3002 home_dir = getenv("HOME")
3003```
[email protected]449f3e42024-08-01 21:43:27 +08003004### **import**: Import a file into the current scope. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003005
3006```
3007 The import command loads the rules and variables resulting from executing the
3008 given file into the current scope.
3009
3010 By convention, imported files are named with a .gni extension.
3011
3012 An import is different than a C++ "include". The imported file is executed in
3013 a standalone environment from the caller of the import command. The results
3014 of this execution are cached for other files that import the same .gni file.
3015
3016 Note that you can not import a BUILD.gn file that's otherwise used in the
3017 build. Files must either be imported or implicitly loaded as a result of deps
3018 rules, but not both.
3019
3020 The imported file's scope will be merged with the scope at the point import
3021 was called. If there is a conflict (both the current scope and the imported
3022 file define some variable or rule with the same name but different value), a
3023 runtime error will be thrown. Therefore, it's good practice to minimize the
3024 stuff that an imported file defines.
3025
3026 Variables and templates beginning with an underscore '_' are considered
3027 private and will not be imported. Imported files can use such variables for
3028 internal computation without affecting other files.
3029```
3030
3031#### **Examples**
3032
3033```
3034 import("//build/rules/idl_compilation_rule.gni")
3035
3036 # Looks in the current directory.
3037 import("my_vars.gni")
3038```
[email protected]449f3e42024-08-01 21:43:27 +08003039### **label_matches**: Returns true if the label matches any of a set of patterns. [Back to Top](#gn-reference)
Aaron Woode05c0aa2024-02-14 13:40:53 -08003040
3041```
3042 label_matches(target_label, patterns)
3043
3044 The argument patterns must be a list of label patterns (see
3045 "gn help label_pattern"). If the target_label matches any of the patterns,
3046 the function returns the value true.
3047```
3048
3049#### **Examples**
3050```
3051 result = label_matches("//baz:bar", [ "//foo/bar/*", "//baz:*" ])
3052 # result will be true
3053```
[email protected]449f3e42024-08-01 21:43:27 +08003054### **not_needed**: Mark variables from scope as not needed. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003055
3056```
3057 not_needed(variable_list_or_star, variable_to_ignore_list = [])
3058 not_needed(from_scope, variable_list_or_star,
3059 variable_to_ignore_list = [])
3060
3061 Mark the variables in the current or given scope as not needed, which means
3062 you will not get an error about unused variables for these. The
3063 variable_to_ignore_list allows excluding variables from "all matches" if
3064 variable_list_or_star is "*".
3065```
3066
3067#### **Example**
3068
3069```
3070 not_needed("*", [ "config" ])
3071 not_needed([ "data_deps", "deps" ])
3072 not_needed(invoker, "*", [ "config" ])
3073 not_needed(invoker, [ "data_deps", "deps" ])
3074```
Takuto Ikutadae6a442025-03-05 17:31:58 +09003075### **path_exists**: Returns whether the given path exists. [Back to Top](#gn-reference)
Andrew Grievef98b6d72025-02-25 08:54:51 -08003076
3077```
3078 path_exists(path)
Andrew Grievef98b6d72025-02-25 08:54:51 -08003079```
3080
Takuto Ikutadae6a442025-03-05 17:31:58 +09003081#### **Examples**:
Andrew Grievef98b6d72025-02-25 08:54:51 -08003082```
3083 path_exists("//") # true
3084 path_exists("BUILD.gn") # true
3085 path_exists("/abs-non-existent") # false
3086```
[email protected]449f3e42024-08-01 21:43:27 +08003087### **pool**: Defines a pool object. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003088
3089```
3090 Pool objects can be applied to a tool to limit the parallelism of the
3091 build. This object has a single property "depth" corresponding to
3092 the number of tasks that may run simultaneously.
3093
3094 As the file containing the pool definition may be executed in the
3095 context of more than one toolchain it is recommended to specify an
3096 explicit toolchain when defining and referencing a pool.
3097
Julie Hockette2a29402018-07-31 10:11:42 -07003098 A pool named "console" defined in the root build file represents Ninja's
3099 console pool. Targets using this pool will have access to the console's
3100 stdin and stdout, and output will not be buffered. This special pool must
3101 have a depth of 1. Pools not defined in the root must not be named "console".
3102 The console pool can only be defined for the default toolchain.
3103 Refer to the Ninja documentation on the console pool for more info.
3104
Brett Wilson796ed472018-07-16 15:11:09 -07003105 A pool is referenced by its label just like a target.
3106```
3107
3108#### **Variables**
3109
3110```
3111 depth*
3112 * = required
3113```
3114
3115#### **Example**
3116
3117```
3118 if (current_toolchain == default_toolchain) {
3119 pool("link_pool") {
3120 depth = 1
3121 }
3122 }
3123
3124 toolchain("toolchain") {
3125 tool("link") {
3126 command = "..."
Petr Hosekadd1bb12019-09-11 14:39:38 -07003127 pool = ":link_pool($default_toolchain)"
Brett Wilson796ed472018-07-16 15:11:09 -07003128 }
3129 }
3130```
[email protected]449f3e42024-08-01 21:43:27 +08003131### **print**: Prints to the console. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003132
3133```
3134 Prints all arguments to the console separated by spaces. A newline is
3135 automatically appended to the end.
3136
3137 This function is intended for debugging. Note that build files are run in
3138 parallel so you may get interleaved prints. A buildfile may also be executed
3139 more than once in parallel in the context of different toolchains so the
3140 prints from one file may be duplicated or
3141 interleaved with itself.
3142```
3143
3144#### **Examples**
3145
3146```
3147 print("Hello world")
3148
3149 print(sources, deps)
3150```
[email protected]449f3e42024-08-01 21:43:27 +08003151### **print_stack_trace**: Prints a stack trace. [Back to Top](#gn-reference)
Aaron Wood24062bb2022-04-25 20:37:48 -07003152
3153```
3154 Prints the current file location, and all template invocations that led up to
3155 this location, to the console.
3156```
3157
3158#### **Examples**
3159
3160```
3161 template("foo"){
3162 print_stack_trace()
3163 }
3164 template("bar"){
3165 foo(target_name + ".foo") {
3166 baz = invoker.baz
3167 }
3168 }
3169 bar("lala") {
3170 baz = 42
3171 }
3172
3173 will print out the following:
3174
3175 print_stack_trace() initiated at //build.gn:2
3176 bar("lala") //BUILD.gn:9
3177 foo("lala.foo") //BUILD.gn:5
3178 print_stack_trace() //BUILD.gn:2
3179```
[email protected]449f3e42024-08-01 21:43:27 +08003180### **process_file_template**: Do template expansion over a list of files. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003181
3182```
3183 process_file_template(source_list, template)
3184
3185 process_file_template applies a template list to a source file list,
3186 returning the result of applying each template to each source. This is
3187 typically used for computing output file names from input files.
3188
3189 In most cases, get_target_outputs() will give the same result with shorter,
3190 more maintainable code. This function should only be used when that function
3191 can't be used (like there's no target or the target is defined in another
3192 build file).
3193```
3194
3195#### **Arguments**
3196
3197```
3198 The source_list is a list of file names.
3199
3200 The template can be a string or a list. If it is a list, multiple output
3201 strings are generated for each input.
3202
3203 The template should contain source expansions to which each name in the
3204 source list is applied. See "gn help source_expansion".
3205```
3206
3207#### **Example**
3208
3209```
3210 sources = [
3211 "foo.idl",
3212 "bar.idl",
3213 ]
3214 myoutputs = process_file_template(
3215 sources,
3216 [ "$target_gen_dir/{{source_name_part}}.cc",
3217 "$target_gen_dir/{{source_name_part}}.h" ])
3218
3219 The result in this case will be:
3220 [ "//out/Debug/foo.cc"
3221 "//out/Debug/foo.h"
3222 "//out/Debug/bar.cc"
3223 "//out/Debug/bar.h" ]
3224```
[email protected]449f3e42024-08-01 21:43:27 +08003225### **read_file**: Read a file into a variable. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003226
3227```
3228 read_file(filename, input_conversion)
3229
3230 Whitespace will be trimmed from the end of the file. Throws an error if the
3231 file can not be opened.
3232```
3233
3234#### **Arguments**
3235
3236```
3237 filename
3238 Filename to read, relative to the build file.
3239
3240 input_conversion
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02003241 Controls how the file is read and parsed. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07003242```
3243
3244#### **Example**
3245
3246```
3247 lines = read_file("foo.txt", "list lines")
3248```
[email protected]449f3e42024-08-01 21:43:27 +08003249### **rebase_path**: Rebase a file or directory to another location. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003250
3251```
3252 converted = rebase_path(input,
3253 new_base = "",
3254 current_base = ".")
3255
3256 Takes a string argument representing a file name, or a list of such strings
3257 and converts it/them to be relative to a different base directory.
3258
3259 When invoking the compiler or scripts, GN will automatically convert sources
3260 and include directories to be relative to the build directory. However, if
3261 you're passing files directly in the "args" array or doing other manual
3262 manipulations where GN doesn't know something is a file name, you will need
3263 to convert paths to be relative to what your tool is expecting.
3264
3265 The common case is to use this to convert paths relative to the current
3266 directory to be relative to the build directory (which will be the current
3267 directory when executing scripts).
3268
3269 If you want to convert a file path to be source-absolute (that is, beginning
3270 with a double slash like "//foo/bar"), you should use the get_path_info()
3271 function. This function won't work because it will always make relative
3272 paths, and it needs to support making paths relative to the source root, so
Robert Sesekd0a6f072020-05-15 11:21:22 -04003273 it can't also generate source-absolute paths without more special-cases.
Brett Wilson796ed472018-07-16 15:11:09 -07003274```
3275
3276#### **Arguments**
3277
3278```
3279 input
Robert Sesekd0a6f072020-05-15 11:21:22 -04003280 A string or list of strings representing file or directory names. These
Brett Wilson796ed472018-07-16 15:11:09 -07003281 can be relative paths ("foo/bar.txt"), system absolute paths
3282 ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt").
3283
3284 new_base
3285 The directory to convert the paths to be relative to. This can be an
3286 absolute path or a relative path (which will be treated as being relative
3287 to the current BUILD-file's directory).
3288
3289 As a special case, if new_base is the empty string (the default), all
3290 paths will be converted to system-absolute native style paths with system
3291 path separators. This is useful for invoking external programs.
3292
3293 current_base
3294 Directory representing the base for relative paths in the input. If this
3295 is not an absolute path, it will be treated as being relative to the
3296 current build file. Use "." (the default) to convert paths from the
3297 current BUILD-file's directory.
3298```
3299
3300#### **Return value**
3301
3302```
3303 The return value will be the same type as the input value (either a string or
3304 a list of strings). All relative and source-absolute file names will be
3305 converted to be relative to the requested output System-absolute paths will
3306 be unchanged.
3307
3308 Whether an output path will end in a slash will match whether the
3309 corresponding input path ends in a slash. It will return "." or "./"
3310 (depending on whether the input ends in a slash) to avoid returning empty
3311 strings. This means if you want a root path ("//" or "/") not ending in a
3312 slash, you can add a dot ("//.").
3313```
3314
3315#### **Example**
3316
3317```
3318 # Convert a file in the current directory to be relative to the build
3319 # directory (the current dir when executing compilers and scripts).
3320 foo = rebase_path("myfile.txt", root_build_dir)
3321 # might produce "../../project/myfile.txt".
3322
3323 # Convert a file to be system absolute:
3324 foo = rebase_path("myfile.txt")
3325 # Might produce "D:\\source\\project\\myfile.txt" on Windows or
3326 # "/home/you/source/project/myfile.txt" on Linux.
3327
3328 # Typical usage for converting to the build directory for a script.
3329 action("myscript") {
3330 # Don't convert sources, GN will automatically convert these to be relative
3331 # to the build directory when it constructs the command line for your
3332 # script.
3333 sources = [ "foo.txt", "bar.txt" ]
3334
3335 # Extra file args passed manually need to be explicitly converted
3336 # to be relative to the build directory:
3337 args = [
3338 "--data",
3339 rebase_path("//mything/data/input.dat", root_build_dir),
3340 "--rel",
3341 rebase_path("relative_path.txt", root_build_dir)
3342 ] + rebase_path(sources, root_build_dir)
3343 }
3344```
[email protected]449f3e42024-08-01 21:43:27 +08003345### **set_default_toolchain**: Sets the default toolchain name. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003346
3347```
3348 set_default_toolchain(toolchain_label)
3349
3350 The given label should identify a toolchain definition (see "gn help
3351 toolchain"). This toolchain will be used for all targets unless otherwise
3352 specified.
3353
3354 This function is only valid to call during the processing of the build
3355 configuration file. Since the build configuration file is processed
3356 separately for each toolchain, this function will be a no-op when called
3357 under any non-default toolchains.
3358
3359 For example, the default toolchain should be appropriate for the current
3360 environment. If the current environment is 32-bit and somebody references a
3361 target with a 64-bit toolchain, we wouldn't want processing of the build
3362 config file for the 64-bit toolchain to reset the default toolchain to
3363 64-bit, we want to keep it 32-bits.
3364```
3365
3366#### **Argument**
3367
3368```
3369 toolchain_label
3370 Toolchain name.
3371```
3372
3373#### **Example**
3374
3375```
3376 # Set default toolchain only has an effect when run in the context of the
3377 # default toolchain. Pick the right one according to the current CPU
3378 # architecture.
3379 if (target_cpu == "x64") {
3380 set_default_toolchain("//toolchains:64")
3381 } else if (target_cpu == "x86") {
3382 set_default_toolchain("//toolchains:32")
3383 }
3384```
[email protected]449f3e42024-08-01 21:43:27 +08003385### **set_defaults**: Set default values for a target type. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003386
3387```
3388 set_defaults() { }
3389
3390 Sets the default values for a given target type. Whenever target_type_name is
3391 seen in the future, the values specified in set_default's block will be
3392 copied into the current scope.
3393
3394 When the target type is used, the variable copying is very strict. If a
3395 variable with that name is already in scope, the build will fail with an
3396 error.
3397
3398 set_defaults can be used for built-in target types ("executable",
3399 "shared_library", etc.) and custom ones defined via the "template" command.
3400 It can be called more than once and the most recent call in any scope will
3401 apply, but there is no way to refer to the previous defaults and modify them
3402 (each call to set_defaults must supply a complete list of all defaults it
3403 wants). If you want to share defaults, store them in a separate variable.
3404```
3405
3406#### **Example**
3407
3408```
3409 set_defaults("static_library") {
3410 configs = [ "//tools/mything:settings" ]
3411 }
3412
Nico Webere49cb722018-08-28 13:10:29 -04003413 static_library("mylib") {
Brett Wilson796ed472018-07-16 15:11:09 -07003414 # The configs will be auto-populated as above. You can remove it if
3415 # you don't want the default for a particular default:
3416 configs -= [ "//tools/mything:settings" ]
3417 }
3418```
[email protected]449f3e42024-08-01 21:43:27 +08003419### **split_list**: Splits a list into N different sub-lists. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003420
3421```
3422 result = split_list(input, n)
3423
3424 Given a list and a number N, splits the list into N sub-lists of
3425 approximately equal size. The return value is a list of the sub-lists. The
3426 result will always be a list of size N. If N is greater than the number of
3427 elements in the input, it will be padded with empty lists.
3428
3429 The expected use is to divide source files into smaller uniform chunks.
3430```
3431
3432#### **Example**
3433
3434```
3435 The code:
3436 mylist = [1, 2, 3, 4, 5, 6]
3437 print(split_list(mylist, 3))
3438
3439 Will print:
3440 [[1, 2], [3, 4], [5, 6]
3441```
[email protected]449f3e42024-08-01 21:43:27 +08003442### **string_join**: Concatenates a list of strings with a separator. [Back to Top](#gn-reference)
Keir Mierle6ae63302019-11-08 23:02:18 -08003443
3444```
3445 result = string_join(separator, strings)
3446
3447 Concatenate a list of strings with intervening occurrences of separator.
3448```
3449
3450#### **Examples**
3451
3452```
3453 string_join("", ["a", "b", "c"]) --> "abc"
3454 string_join("|", ["a", "b", "c"]) --> "a|b|c"
3455 string_join(", ", ["a", "b", "c"]) --> "a, b, c"
3456 string_join("s", ["", ""]) --> "s"
3457```
[email protected]449f3e42024-08-01 21:43:27 +08003458### **string_replace**: Replaces substring in the given string. [Back to Top](#gn-reference)
Petr Hosekc0744ed2018-08-04 16:13:59 -07003459
3460```
3461 result = string_replace(str, old, new[, max])
3462
3463 Returns a copy of the string str in which the occurrences of old have been
3464 replaced with new, optionally restricting the number of replacements. The
3465 replacement is performed sequentially, so if new contains old, it won't be
3466 replaced.
3467```
3468
3469#### **Example**
3470
3471```
3472 The code:
3473 mystr = "Hello, world!"
3474 print(string_replace(mystr, "world", "GN"))
3475
3476 Will print:
3477 Hello, GN!
3478```
[email protected]449f3e42024-08-01 21:43:27 +08003479### **string_split**: Split string into a list of strings. [Back to Top](#gn-reference)
Keir Mierle6ae63302019-11-08 23:02:18 -08003480
3481```
3482 result = string_split(str[, sep])
3483
3484 Split string into all substrings separated by separator and returns a list
3485 of the substrings between those separators.
3486
3487 If the separator argument is omitted, the split is by any whitespace, and
3488 any leading/trailing whitespace is ignored; similar to Python's str.split().
3489```
3490
3491#### **Examples without a separator (split on whitespace)**:
3492
3493```
3494 string_split("") --> []
3495 string_split("a") --> ["a"]
3496 string_split(" aa bb") --> ["aa", "bb"]
3497```
3498
3499#### **Examples with a separator (split on separators)**:
3500
3501```
3502 string_split("", "|") --> [""]
3503 string_split(" a b ", " ") --> ["", "", "a", "b", "", ""]
3504 string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"]
3505```
[email protected]449f3e42024-08-01 21:43:27 +08003506### **template**: Define a template rule. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003507
3508```
3509 A template defines a custom name that acts like a function. It provides a way
3510 to add to the built-in target types.
3511
3512 The template() function is used to declare a template. To invoke the
3513 template, just use the name of the template like any other target type.
3514
3515 Often you will want to declare your template in a special file that other
3516 files will import (see "gn help import") so your template rule can be shared
3517 across build files.
3518```
3519
3520#### **Variables and templates**:
3521
3522```
3523 When you call template() it creates a closure around all variables currently
3524 in scope with the code in the template block. When the template is invoked,
3525 the closure will be executed.
3526
3527 When the template is invoked, the code in the caller is executed and passed
3528 to the template code as an implicit "invoker" variable. The template uses
3529 this to read state out of the invoking code.
3530
3531 One thing explicitly excluded from the closure is the "current directory"
3532 against which relative file names are resolved. The current directory will be
3533 that of the invoking code, since typically that code specifies the file
3534 names. This means all files internal to the template should use absolute
3535 names.
3536
3537 A template will typically forward some or all variables from the invoking
3538 scope to a target that it defines. Often, such variables might be optional.
3539 Use the pattern:
3540
3541 if (defined(invoker.deps)) {
3542 deps = invoker.deps
3543 }
3544
3545 The function forward_variables_from() provides a shortcut to forward one or
3546 more or possibly all variables in this manner:
3547
3548 forward_variables_from(invoker, ["deps", "public_deps"])
3549```
3550
3551#### **Target naming**
3552
3553```
3554 Your template should almost always define a built-in target with the name the
3555 template invoker specified. For example, if you have an IDL template and
3556 somebody does:
3557 idl("foo") {...
3558 you will normally want this to expand to something defining a source_set or
3559 static_library named "foo" (among other things you may need). This way, when
3560 another target specifies a dependency on "foo", the static_library or
3561 source_set will be linked.
3562
3563 It is also important that any other targets your template expands to have
3564 unique names, or you will get collisions.
3565
3566 Access the invoking name in your template via the implicit "target_name"
3567 variable. This should also be the basis for how other targets that a template
3568 expands to ensure uniqueness.
3569
3570 A typical example would be a template that defines an action to generate some
3571 source files, and a source_set to compile that source. Your template would
3572 name the source_set "target_name" because that's what you want external
3573 targets to depend on to link your code. And you would name the action
3574 something like "${target_name}_action" to make it unique. The source set
3575 would have a dependency on the action to make it run.
3576```
3577
3578#### **Overriding builtin targets**
3579
3580```
3581 You can use template to redefine a built-in target in which case your template
3582 takes a precedence over the built-in one. All uses of the target from within
3583 the template definition will refer to the built-in target which makes it
3584 possible to extend the behavior of the built-in target:
3585
3586 template("shared_library") {
3587 shared_library(shlib) {
Julie Hockette2a29402018-07-31 10:11:42 -07003588 forward_variables_from(invoker, "*")
Brett Wilson796ed472018-07-16 15:11:09 -07003589 ...
3590 }
3591 }
3592```
3593
3594#### **Example of defining a template**
3595
3596```
3597 template("my_idl") {
3598 # Be nice and help callers debug problems by checking that the variables
3599 # the template requires are defined. This gives a nice message rather than
3600 # giving the user an error about an undefined variable in the file defining
3601 # the template
3602 #
3603 # You can also use defined() to give default values to variables
3604 # unspecified by the invoker.
3605 assert(defined(invoker.sources),
3606 "Need sources in $target_name listing the idl files.")
3607
3608 # Name of the intermediate target that does the code gen. This must
3609 # incorporate the target name so it's unique across template
3610 # instantiations.
3611 code_gen_target_name = target_name + "_code_gen"
3612
3613 # Intermediate target to convert IDL to C source. Note that the name is
3614 # based on the name the invoker of the template specified. This way, each
3615 # time the template is invoked we get a unique intermediate action name
3616 # (since all target names are in the global scope).
3617 action_foreach(code_gen_target_name) {
3618 # Access the scope defined by the invoker via the implicit "invoker"
3619 # variable.
3620 sources = invoker.sources
3621
3622 # Note that we need an absolute path for our script file name. The
3623 # current directory when executing this code will be that of the invoker
3624 # (this is why we can use the "sources" directly above without having to
3625 # rebase all of the paths). But if we need to reference a script relative
3626 # to the template file, we'll need to use an absolute path instead.
3627 script = "//tools/idl/idl_code_generator.py"
3628
3629 # Tell GN how to expand output names given the sources.
3630 # See "gn help source_expansion" for more.
3631 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
3632 "$target_gen_dir/{{source_name_part}}.h" ]
3633 }
3634
3635 # Name the source set the same as the template invocation so instancing
3636 # this template produces something that other targets can link to in their
3637 # deps.
3638 source_set(target_name) {
3639 # Generates the list of sources, we get these from the action_foreach
3640 # above.
3641 sources = get_target_outputs(":$code_gen_target_name")
3642
3643 # This target depends on the files produced by the above code gen target.
3644 deps = [ ":$code_gen_target_name" ]
3645 }
3646 }
3647```
3648
3649#### **Example of invoking the resulting template**
3650
3651```
3652 # This calls the template code above, defining target_name to be
3653 # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly
3654 # brackets.
3655 my_idl("foo_idl_files") {
3656 # Goes into the template as "invoker.sources".
3657 sources = [ "foo.idl", "bar.idl" ]
3658 }
3659
3660 # Here is a target that depends on our template.
3661 executable("my_exe") {
3662 # Depend on the name we gave the template call above. Internally, this will
3663 # produce a dependency from executable to the source_set inside the
3664 # template (since it has this name), which will in turn depend on the code
3665 # gen action.
3666 deps = [ ":foo_idl_files" ]
3667 }
3668```
[email protected]449f3e42024-08-01 21:43:27 +08003669### **tool**: Specify arguments to a toolchain tool. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07003670
3671#### **Usage**
3672
3673```
3674 tool() {
3675
3676 }
3677```
3678
3679#### **Tool types**
3680
3681```
3682 Compiler tools:
3683 "cc": C compiler
3684 "cxx": C++ compiler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003685 "cxx_module": C++ compiler used for Clang .modulemap files
Brett Wilson796ed472018-07-16 15:11:09 -07003686 "objc": Objective C compiler
3687 "objcxx": Objective C++ compiler
3688 "rc": Resource compiler (Windows .rc files)
3689 "asm": Assembler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003690 "swift": Swift compiler driver
Brett Wilson796ed472018-07-16 15:11:09 -07003691
3692 Linker tools:
3693 "alink": Linker for static libraries (archives)
3694 "solink": Linker for shared libraries
3695 "link": Linker for executables
3696
3697 Other tools:
3698 "stamp": Tool for creating stamp files
3699 "copy": Tool to copy files.
3700 "action": Defaults for actions
3701
3702 Platform specific tools:
3703 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
3704 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
Julie Hockettce1fa072019-05-07 17:44:37 -07003705
3706 Rust tools:
Petr Hosek7617b592019-10-02 09:52:21 -07003707 "rust_bin": Tool for compiling Rust binaries
3708 "rust_cdylib": Tool for compiling C-compatible dynamic libraries.
3709 "rust_dylib": Tool for compiling Rust dynamic libraries.
3710 "rust_macro": Tool for compiling Rust procedural macros.
3711 "rust_rlib": Tool for compiling Rust libraries.
3712 "rust_staticlib": Tool for compiling Rust static libraries.
Brett Wilson796ed472018-07-16 15:11:09 -07003713```
3714
3715#### **Tool variables**
3716
3717```
3718 command [string with substitutions]
3719 Valid for: all tools except "action" (required)
3720
3721 The command to run.
3722
Joe Armstrong23d22bc2019-06-28 15:31:03 +08003723 command_launcher [string]
3724 Valid for: all tools except "action" (optional)
3725
3726 The prefix with which to launch the command (e.g. the path to a Goma or
3727 CCache compiler launcher).
3728
3729 Note that this prefix will not be included in the compilation database or
3730 IDE files generated from the build.
3731
Brett Wilson796ed472018-07-16 15:11:09 -07003732 default_output_dir [string with substitutions]
3733 Valid for: linker tools
3734
3735 Default directory name for the output file relative to the
3736 root_build_dir. It can contain other substitution patterns. This will
3737 be the default value for the {{output_dir}} expansion (discussed below)
3738 but will be overridden by the "output_dir" variable in a target, if one
3739 is specified.
3740
3741 GN doesn't do anything with this string other than pass it along,
3742 potentially with target-specific overrides. It is the tool's job to use
3743 the expansion so that the files will be in the right place.
3744
3745 default_output_extension [string]
3746 Valid for: linker tools
3747
3748 Extension for the main output of a linkable tool. It includes the
3749 leading dot. This will be the default value for the
3750 {{output_extension}} expansion (discussed below) but will be overridden
3751 by by the "output extension" variable in a target, if one is specified.
3752 Empty string means no extension.
3753
3754 GN doesn't actually do anything with this extension other than pass it
3755 along, potentially with target-specific overrides. One would typically
3756 use the {{output_extension}} value in the "outputs" to read this value.
3757
3758 Example: default_output_extension = ".exe"
3759
3760 depfile [string with substitutions]
3761 Valid for: compiler tools (optional)
3762
3763 If the tool can write ".d" files, this specifies the name of the
3764 resulting file. These files are used to list header file dependencies
3765 (or other implicit input dependencies) that are discovered at build
3766 time. See also "depsformat".
3767
3768 Example: depfile = "{{output}}.d"
3769
3770 depsformat [string]
3771 Valid for: compiler tools (when depfile is specified)
3772
3773 Format for the deps outputs. This is either "gcc" or "msvc". See the
3774 ninja documentation for "deps" for more information.
3775
3776 Example: depsformat = "gcc"
3777
3778 description [string with substitutions, optional]
3779 Valid for: all tools
3780
3781 What to print when the command is run.
3782
3783 Example: description = "Compiling {{source}}"
3784
Julie Hockettce1fa072019-05-07 17:44:37 -07003785 exe_output_extension [string, optional, rust tools only]
3786 rlib_output_extension [string, optional, rust tools only]
3787 dylib_output_extension [string, optional, rust tools only]
3788 cdylib_output_extension [string, optional, rust tools only]
Petr Hosekfe36c7c2019-11-26 14:38:08 -08003789 rust_proc_macro_output_extension [string, optional, rust tools only]
Julie Hockettce1fa072019-05-07 17:44:37 -07003790 Valid for: Rust tools
3791
3792 These specify the default tool output for each of the crate types.
3793 The default is empty for executables, shared, and static libraries and
3794 ".rlib" for rlibs. Note that the Rust compiler complains with an error
3795 if external crates do not take the form `lib.rlib` or
3796 `lib.`, where `` is `.so`,
3797 `.dylib`, or `.dll` as appropriate for the platform.
3798
Brett Wilson796ed472018-07-16 15:11:09 -07003799 lib_switch [string, optional, link tools only]
3800 lib_dir_switch [string, optional, link tools only]
3801 Valid for: Linker tools except "alink"
3802
3803 These strings will be prepended to the libraries and library search
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003804 directories, respectively, because linkers differ on how to specify
3805 them.
3806
Brett Wilson796ed472018-07-16 15:11:09 -07003807 If you specified:
3808 lib_switch = "-l"
3809 lib_dir_switch = "-L"
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003810 then the "{{libs}}" expansion for
3811 [ "freetype", "expat" ]
3812 would be
3813 "-lfreetype -lexpat".
3814
3815 framework_switch [string, optional, link tools only]
Robert Sesekd0a6f072020-05-15 11:21:22 -04003816 weak_framework_switch [string, optional, link tools only]
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003817 framework_dir_switch [string, optional, link tools only]
3818 Valid for: Linker tools
3819
3820 These strings will be prepended to the frameworks and framework search
3821 path directories, respectively, because linkers differ on how to specify
3822 them.
3823
3824 If you specified:
3825 framework_switch = "-framework "
Robert Sesekd0a6f072020-05-15 11:21:22 -04003826 weak_framework_switch = "-weak_framework "
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003827 framework_dir_switch = "-F"
Robert Sesekd0a6f072020-05-15 11:21:22 -04003828 and:
3829 framework_dirs = [ "$root_out_dir" ]
3830 frameworks = [ "UIKit.framework", "Foo.framework" ]
3831 weak_frameworks = [ "MediaPlayer.framework" ]
3832 would be:
3833 "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer"
Brett Wilson796ed472018-07-16 15:11:09 -07003834
Sylvain Defresne89e64252020-08-07 13:01:06 +02003835 swiftmodule_switch [string, optional, link tools only]
3836 Valid for: Linker tools except "alink"
3837
3838 The string will be prependend to the path to the .swiftmodule files
3839 that are embedded in the linker output.
3840
3841 If you specified:
3842 swiftmodule_swift = "-Wl,-add_ast_path,"
3843 then the "{{swiftmodules}}" expansion for
3844 [ "obj/foo/Foo.swiftmodule" ]
3845 would be
3846 "-Wl,-add_ast_path,obj/foo/Foo.swiftmodule"
3847
danakj4fb095d2024-05-30 11:40:51 -04003848 rust_swiftmodule_switch [string, optional, link tools only]
3849 Valid for: Linker tools except "alink"
3850
3851 Like swiftmodule_switch, but for targets built/linked with the Rust
3852 compiler. The string will be prependend to the path to the
3853 .swiftmodule files that are embedded in the linker output.
3854
3855 If you specified:
3856 rust_swiftmodule_swift = "-Clink-arg=-Wl,-add_ast_path,"
3857 then the "{{swiftmodules}}" expansion for
3858 [ "obj/foo/Foo.swiftmodule" ]
3859 would be
3860 "-Clink-arg=-Wl,-add_ast_path,obj/foo/Foo.swiftmodule"
3861
Brett Wilson796ed472018-07-16 15:11:09 -07003862 outputs [list of strings with substitutions]
3863 Valid for: Linker and compiler tools (required)
3864
3865 An array of names for the output files the tool produces. These are
3866 relative to the build output directory. There must always be at least
3867 one output file. There can be more than one output (a linker might
3868 produce a library and an import library, for example).
3869
3870 This array just declares to GN what files the tool will produce. It is
3871 your responsibility to specify the tool command that actually produces
3872 these files.
3873
3874 If you specify more than one output for shared library links, you
3875 should consider setting link_output, depend_output, and
3876 runtime_outputs.
3877
3878 Example for a compiler tool that produces .obj files:
3879 outputs = [
3880 "{{source_out_dir}}/{{source_name_part}}.obj"
3881 ]
3882
3883 Example for a linker tool that produces a .dll and a .lib. The use of
3884 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
3885 the target to override these values.
3886 outputs = [
Nico Weberad9eab22020-11-15 22:20:28 -05003887 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Brett Wilson796ed472018-07-16 15:11:09 -07003888 "{{output_dir}}/{{target_output_name}}.lib",
3889 ]
3890
Sylvain Defresne89e64252020-08-07 13:01:06 +02003891 partial_outputs [list of strings with substitutions]
3892 Valid for: "swift" only
3893
3894 An array of names for the partial outputs the tool produces. These
3895 are relative to the build output directory. The expansion will be
3896 evaluated for each file listed in the "sources" of the target.
3897
3898 This is used to deal with whole module optimization, allowing to
3899 list one object file per source file when whole module optimization
3900 is disabled.
3901
Brett Wilson796ed472018-07-16 15:11:09 -07003902 pool [label, optional]
3903 Valid for: all tools (optional)
3904
3905 Label of the pool to use for the tool. Pools are used to limit the
3906 number of tasks that can execute concurrently during the build.
3907
3908 See also "gn help pool".
3909
3910 link_output [string with substitutions]
3911 depend_output [string with substitutions]
David 'Digit' Turnere3d088c2024-08-12 16:36:30 +02003912 Valid for: "solink", "rust_dylib" or "rust_cdylib" only (optional)
Brett Wilson796ed472018-07-16 15:11:09 -07003913
David 'Digit' Turnere3d088c2024-08-12 16:36:30 +02003914 These two files specify which of the outputs from the tool should
3915 be used for linking and dependency tracking. These should match entries
3916 in the "outputs". If unspecified, the first item in the "outputs" array
3917 will be used for all. See "Separate linking and dependencies for shared
3918 libraries" below for more.
Brett Wilson796ed472018-07-16 15:11:09 -07003919
3920 On Windows, where the tools produce a .dll shared library and a .lib
3921 import library, you will want the first two to be the import library
3922 and the third one to be the .dll file. On Linux, if you're not doing
3923 the separate linking/dependency optimization, all of these should be
3924 the .so output.
3925
3926 output_prefix [string]
3927 Valid for: Linker tools (optional)
3928
3929 Prefix to use for the output name. Defaults to empty. This prefix will
3930 be prepended to the name of the target (or the output_name if one is
3931 manually specified for it) if the prefix is not already there. The
3932 result will show up in the {{output_name}} substitution pattern.
3933
3934 Individual targets can opt-out of the output prefix by setting:
3935 output_prefix_override = true
3936 (see "gn help output_prefix_override").
3937
3938 This is typically used to prepend "lib" to libraries on
3939 Posix systems:
3940 output_prefix = "lib"
3941
3942 precompiled_header_type [string]
3943 Valid for: "cc", "cxx", "objc", "objcxx"
3944
3945 Type of precompiled headers. If undefined or the empty string,
3946 precompiled headers will not be used for this tool. Otherwise use "gcc"
3947 or "msvc".
3948
3949 For precompiled headers to be used for a given target, the target (or a
3950 config applied to it) must also specify a "precompiled_header" and, for
3951 "msvc"-style headers, a "precompiled_source" value. If the type is
3952 "gcc", then both "precompiled_header" and "precompiled_source" must
3953 resolve to the same file, despite the different formats required for
3954 each."
3955
3956 See "gn help precompiled_header" for more.
3957
3958 restat [boolean]
3959 Valid for: all tools (optional, defaults to false)
3960
3961 Requests that Ninja check the file timestamp after this tool has run to
3962 determine if anything changed. Set this if your tool has the ability to
3963 skip writing output if the output file has not changed.
3964
3965 Normally, Ninja will assume that when a tool runs the output be new and
Takuto Ikuta60a28b62024-09-05 17:45:42 +09003966 downstream dependents must be rebuild. When this is set to true, Ninja
Brett Wilson796ed472018-07-16 15:11:09 -07003967 can skip rebuilding downstream dependents for input changes that don't
3968 actually affect the output.
3969
3970 Example:
3971 restat = true
3972
3973 rspfile [string with substitutions]
3974 Valid for: all tools except "action" (optional)
3975
3976 Name of the response file. If empty, no response file will be
3977 used. See "rspfile_content".
3978
3979 rspfile_content [string with substitutions]
3980 Valid for: all tools except "action" (required when "rspfile" is used)
3981
3982 The contents to be written to the response file. This may include all
3983 or part of the command to send to the tool which allows you to get
3984 around OS command-line length limits.
3985
3986 This example adds the inputs and libraries to a response file, but
3987 passes the linker flags directly on the command line:
3988 tool("link") {
3989 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
3990 rspfile = "{{output}}.rsp"
Adrian Taylorac6cc952020-04-26 20:49:18 -07003991 rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}"
Brett Wilson796ed472018-07-16 15:11:09 -07003992 }
3993
3994 runtime_outputs [string list with substitutions]
3995 Valid for: linker tools
3996
3997 If specified, this list is the subset of the outputs that should be
3998 added to runtime deps (see "gn help runtime_deps"). By default (if
3999 runtime_outputs is empty or unspecified), it will be the link_output.
Adrian Taylor4aa9bdf2021-11-15 19:00:06 -08004000
4001 rust_sysroot
4002 Valid for: Rust tools
4003
4004 A path relative to root_out_dir. This is not used in the build
4005 process, but may be used when generating metadata for rust-analyzer.
4006 (See --export-rust-project). It enables such metadata to include
4007 information about the Rust standard library.
Adrian Taylore3978de2023-05-26 12:07:53 +01004008
4009 dynamic_link_switch
4010 Valid for: Rust tools which link
4011
4012 A switch to be optionally inserted into linker command lines
4013 to indicate that subsequent items may be dynamically linked.
4014 For ld-like linkers, -Clink-arg=-Bdynamic may be a good choice.
4015 This switch is inserted by gn into rustc command lines before
4016 listing any non-Rust dependencies. This may be necessary because
4017 sometimes rustc puts the linker into a mode where it would otherwise
4018 link against static libraries by default. This flag will be
4019 inserted into the {{rustdeps}} variable at the appropriate place;
4020 {{ldflags}} can't be used for the same purpose because the flags
4021 may not be inserted at the desired place in the command line.
Brett Wilson796ed472018-07-16 15:11:09 -07004022```
4023
4024#### **Expansions for tool variables**
4025
4026```
4027 All paths are relative to the root build directory, which is the current
4028 directory for running all tools. These expansions are available to all tools:
4029
4030 {{label}}
4031 The label of the current target. This is typically used in the
4032 "description" field for link tools. The toolchain will be omitted from
4033 the label for targets in the default toolchain, and will be included
4034 for targets in other toolchains.
4035
4036 {{label_name}}
4037 The short name of the label of the target. This is the part after the
4038 colon. For "//foo/bar:baz" this will be "baz". Unlike
4039 {{target_output_name}}, this is not affected by the "output_prefix" in
4040 the tool or the "output_name" set on the target.
4041
Scott Graham501b49a2020-08-05 13:03:37 -07004042 {{label_no_toolchain}}
4043 The label of the current target, never including the toolchain
4044 (otherwise, this is identical to {{label}}). This is used as the module
4045 name when using .modulemap files.
4046
Brett Wilson796ed472018-07-16 15:11:09 -07004047 {{output}}
4048 The relative path and name of the output(s) of the current build step.
4049 If there is more than one output, this will expand to a list of all of
4050 them. Example: "out/base/my_file.o"
4051
4052 {{target_gen_dir}}
4053 {{target_out_dir}}
4054 The directory of the generated file and output directories,
4055 respectively, for the current target. There is no trailing slash. See
4056 also {{output_dir}} for linker tools. Example: "out/base/test"
4057
4058 {{target_output_name}}
4059 The short name of the current target with no path information, or the
4060 value of the "output_name" variable if one is specified in the target.
4061 This will include the "output_prefix" if any. See also {{label_name}}.
4062
4063 Example: "libfoo" for the target named "foo" and an output prefix for
4064 the linker tool of "lib".
4065
4066 Compiler tools have the notion of a single input and a single output, along
4067 with a set of compiler-specific flags. The following expansions are
4068 available:
4069
4070 {{asmflags}}
4071 {{cflags}}
4072 {{cflags_c}}
4073 {{cflags_cc}}
4074 {{cflags_objc}}
4075 {{cflags_objcc}}
4076 {{defines}}
4077 {{include_dirs}}
4078 Strings correspond that to the processed flags/defines/include
4079 directories specified for the target.
4080 Example: "--enable-foo --enable-bar"
4081
4082 Defines will be prefixed by "-D" and include directories will be
4083 prefixed by "-I" (these work with Posix tools as well as Microsoft
4084 ones).
4085
Scott Graham484245b2020-08-11 10:58:50 -07004086 {{module_deps}}
4087 {{module_deps_no_self}}
4088 Strings that correspond to the flags necessary to depend upon the Clang
4089 modules referenced by the current target. The "_no_self" version doesn't
4090 include the module for the current target, and can be used to compile
4091 the pcm itself.
4092
Brett Wilson796ed472018-07-16 15:11:09 -07004093 {{source}}
4094 The relative path and name of the current input file.
4095 Example: "../../base/my_file.cc"
4096
4097 {{source_file_part}}
4098 The file part of the source including the extension (with no directory
4099 information).
4100 Example: "foo.cc"
4101
4102 {{source_name_part}}
4103 The filename part of the source file with no directory or extension.
4104 Example: "foo"
4105
4106 {{source_gen_dir}}
4107 {{source_out_dir}}
4108 The directory in the generated file and output directories,
4109 respectively, for the current input file. If the source file is in the
4110 same directory as the target is declared in, they will will be the same
4111 as the "target" versions above. Example: "gen/base/test"
4112
Julie Hockette2a29402018-07-31 10:11:42 -07004113 Linker tools have multiple inputs and (potentially) multiple outputs. The
Brett Wilson796ed472018-07-16 15:11:09 -07004114 static library tool ("alink") is not considered a linker tool. The following
4115 expansions are available:
4116
4117 {{inputs}}
4118 {{inputs_newline}}
4119 Expands to the inputs to the link step. This will be a list of object
4120 files and static libraries.
4121 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
4122
4123 The "_newline" version will separate the input files with newlines
4124 instead of spaces. This is useful in response files: some linkers can
4125 take a "-filelist" flag which expects newline separated files, and some
4126 Microsoft tools have a fixed-sized buffer for parsing each line of a
4127 response file.
4128
4129 {{ldflags}}
4130 Expands to the processed set of ldflags and library search paths
4131 specified for the target.
4132 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
4133
4134 {{libs}}
4135 Expands to the list of system libraries to link to. Each will be
4136 prefixed by the "lib_switch".
4137
Brett Wilson796ed472018-07-16 15:11:09 -07004138 Example: "-lfoo -lbar"
4139
4140 {{output_dir}}
4141 The value of the "output_dir" variable in the target, or the the value
4142 of the "default_output_dir" value in the tool if the target does not
4143 override the output directory. This will be relative to the
4144 root_build_dir and will not end in a slash. Will be "." for output to
4145 the root_build_dir.
4146
4147 This is subtly different than {{target_out_dir}} which is defined by GN
4148 based on the target's path and not overridable. {{output_dir}} is for
4149 the final output, {{target_out_dir}} is generally for object files and
4150 other outputs.
4151
4152 Usually {{output_dir}} would be defined in terms of either
4153 {{target_out_dir}} or {{root_out_dir}}
4154
4155 {{output_extension}}
4156 The value of the "output_extension" variable in the target, or the
4157 value of the "default_output_extension" value in the tool if the target
4158 does not specify an output extension.
4159 Example: ".so"
4160
4161 {{solibs}}
4162 Extra libraries from shared library dependencies not specified in the
4163 {{inputs}}. This is the list of link_output files from shared libraries
David 'Digit' Turnere3d088c2024-08-12 16:36:30 +02004164 (if the solink, rust_dylib and rust_cdylib tools specify a "link_output"
4165 variable separate from the "depend_output").
Brett Wilson796ed472018-07-16 15:11:09 -07004166
4167 These should generally be treated the same as libs by your tool.
4168
4169 Example: "libfoo.so libbar.so"
4170
Adrian Taylorac6cc952020-04-26 20:49:18 -07004171 {{rlibs}}
4172 Any Rust .rlibs which need to be linked into a final C++ target.
4173 These should be treated as {{inputs}} except that sometimes
4174 they might have different linker directives applied.
4175
4176 Example: "obj/foo/libfoo.rlib"
4177
Sylvain Defresnea09ec162020-01-16 12:36:40 +01004178 {{frameworks}}
4179 Shared libraries packaged as framework bundle. This is principally
4180 used on Apple's platforms (macOS and iOS). All name must be ending
4181 with ".framework" suffix; the suffix will be stripped when expanding
Robert Sesekd0a6f072020-05-15 11:21:22 -04004182 {{frameworks}} and each item will be preceded by "-framework" or
4183 "-weak_framework".
Sylvain Defresnea09ec162020-01-16 12:36:40 +01004184
Sylvain Defresne89e64252020-08-07 13:01:06 +02004185 {{swiftmodules}}
4186 Swift .swiftmodule files that needs to be embedded into the binary.
4187 This is necessary to correctly link with object generated by the
4188 Swift compiler (the .swiftmodule file cannot be embedded in object
4189 files directly). Those will be prefixed with "swiftmodule_switch"
4190 value.
4191
Brett Wilson796ed472018-07-16 15:11:09 -07004192 The static library ("alink") tool allows {{arflags}} plus the common tool
4193 substitutions.
4194
4195 The copy tool allows the common compiler/linker substitutions, plus
4196 {{source}} which is the source of the copy. The stamp tool allows only the
4197 common tool substitutions.
4198
4199 The copy_bundle_data and compile_xcassets tools only allows the common tool
4200 substitutions. Both tools are required to create iOS/macOS bundles and need
4201 only be defined on those platforms.
4202
4203 The copy_bundle_data tool will be called with one source and needs to copy
4204 (optionally optimizing the data representation) to its output. It may be
4205 called with a directory as input and it needs to be recursively copied.
4206
4207 The compile_xcassets tool will be called with one or more source (each an
4208 asset catalog) that needs to be compiled to a single output. The following
4209 substitutions are available:
4210
4211 {{inputs}}
4212 Expands to the list of .xcassets to use as input to compile the asset
4213 catalog.
4214
4215 {{bundle_product_type}}
4216 Expands to the product_type of the bundle that will contain the
4217 compiled asset catalog. Usually corresponds to the product_type
4218 property of the corresponding create_bundle target.
4219
4220 {{bundle_partial_info_plist}}
4221 Expands to the path to the partial Info.plist generated by the
4222 assets catalog compiler. Usually based on the target_name of
4223 the create_bundle target.
Julie Hockettce1fa072019-05-07 17:44:37 -07004224
Harley Li0a9affb2020-06-03 10:38:42 -04004225 {{xcasset_compiler_flags}}
4226 Expands to the list of flags specified in corresponding
4227 create_bundle target.
4228
Sylvain Defresne06cdcc82024-03-21 13:48:59 +01004229 The inputs for compile_xcassets tool will be found from the bundle_data
4230 dependencies by looking for any file matching "*/*.xcassets/*" pattern.
4231 The "$assets.xcassets" directory will be added as input to the tool.
4232
Sylvain Defresne89e64252020-08-07 13:01:06 +02004233 The Swift tool has multiple input and outputs. It must have exactly one
4234 output of .swiftmodule type, but can have one or more object file outputs,
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004235 in addition to other type of outputs. The following expansions are available:
Sylvain Defresne89e64252020-08-07 13:01:06 +02004236
4237 {{module_name}}
4238 Expands to the string representing the module name of target under
4239 compilation (see "module_name" variable).
4240
4241 {{module_dirs}}
4242 Expands to the list of -I for the target Swift module search
4243 path computed from target dependencies.
4244
4245 {{swiftflags}}
4246 Expands to the list of strings representing Swift compiler flags.
4247
Julie Hockettce1fa072019-05-07 17:44:37 -07004248 Rust tools have the notion of a single input and a single output, along
4249 with a set of compiler-specific flags. The following expansions are
4250 available:
4251
4252 {{crate_name}}
4253 Expands to the string representing the crate name of target under
4254 compilation.
4255
4256 {{crate_type}}
4257 Expands to the string representing the type of crate for the target
4258 under compilation.
4259
4260 {{externs}}
4261 Expands to the list of --extern flags needed to include addition Rust
4262 libraries in this target. Includes any specified renamed dependencies.
4263
Julie Hockettce1fa072019-05-07 17:44:37 -07004264 {{rustdeps}}
Takuto Ikutadae6a442025-03-05 17:31:58 +09004265 Expands to the list of -Ldependency= and -Clink-arg= strings
4266 needed to compile this target.
Julie Hockettce1fa072019-05-07 17:44:37 -07004267
4268 {{rustenv}}
4269 Expands to the list of environment variables.
4270
4271 {{rustflags}}
4272 Expands to the list of strings representing Rust compiler flags.
Brett Wilson796ed472018-07-16 15:11:09 -07004273```
4274
4275#### **Separate linking and dependencies for shared libraries**
4276
4277```
4278 Shared libraries are special in that not all changes to them require that
4279 dependent targets be re-linked. If the shared library is changed but no
4280 imports or exports are different, dependent code needn't be relinked, which
4281 can speed up the build.
4282
4283 If your link step can output a list of exports from a shared library and
4284 writes the file only if the new one is different, the timestamp of this file
4285 can be used for triggering re-links, while the actual shared library would be
4286 used for linking.
4287
4288 You will need to specify
4289 restat = true
4290 in the linker tool to make this work, so Ninja will detect if the timestamp
4291 of the dependency file has changed after linking (otherwise it will always
4292 assume that running a command updates the output):
4293
4294 tool("solink") {
4295 command = "..."
4296 outputs = [
4297 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Nico Weberad9eab22020-11-15 22:20:28 -05004298 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC",
Brett Wilson796ed472018-07-16 15:11:09 -07004299 ]
4300 link_output =
4301 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
4302 depend_output =
Nico Weberad9eab22020-11-15 22:20:28 -05004303 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC"
Brett Wilson796ed472018-07-16 15:11:09 -07004304 restat = true
4305 }
4306```
4307
4308#### **Example**
4309
4310```
4311 toolchain("my_toolchain") {
4312 # Put these at the top to apply to all tools below.
4313 lib_switch = "-l"
4314 lib_dir_switch = "-L"
4315
4316 tool("cc") {
4317 command = "gcc {{source}} -o {{output}}"
4318 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
4319 description = "GCC {{source}}"
4320 }
4321 tool("cxx") {
4322 command = "g++ {{source}} -o {{output}}"
4323 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
4324 description = "G++ {{source}}"
4325 }
4326 };
4327```
[email protected]449f3e42024-08-01 21:43:27 +08004328### **toolchain**: Defines a toolchain. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004329
4330```
4331 A toolchain is a set of commands and build flags used to compile the source
4332 code. The toolchain() function defines these commands.
4333```
4334
4335#### **Toolchain overview**
4336
4337```
4338 You can have more than one toolchain in use at once in a build and a target
4339 can exist simultaneously in multiple toolchains. A build file is executed
4340 once for each toolchain it is referenced in so the GN code can vary all
4341 parameters of each target (or which targets exist) on a per-toolchain basis.
4342
4343 When you have a simple build with only one toolchain, the build config file
4344 is loaded only once at the beginning of the build. It must call
4345 set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the
4346 label of the toolchain definition to use. The "toolchain_args" section of the
4347 toolchain definition is ignored.
4348
4349 When a target has a dependency on a target using different toolchain (see "gn
4350 help labels" for how to specify this), GN will start a build using that
4351 secondary toolchain to resolve the target. GN will load the build config file
4352 with the build arguments overridden as specified in the toolchain_args.
4353 Because the default toolchain is already known, calls to
4354 set_default_toolchain() are ignored.
4355
4356 To load a file in an alternate toolchain, GN does the following:
4357
4358 1. Loads the file with the toolchain definition in it (as determined by the
4359 toolchain label).
4360 2. Re-runs the master build configuration file, applying the arguments
4361 specified by the toolchain_args section of the toolchain definition.
4362 3. Loads the destination build file in the context of the configuration file
4363 in the previous step.
4364
4365 The toolchain configuration is two-way. In the default toolchain (i.e. the
4366 main build target) the configuration flows from the build config file to the
4367 toolchain. The build config file looks at the state of the build (OS type,
4368 CPU architecture, etc.) and decides which toolchain to use (via
4369 set_default_toolchain()). In secondary toolchains, the configuration flows
4370 from the toolchain to the build config file: the "toolchain_args" in the
4371 toolchain definition specifies the arguments to re-invoke the build.
4372```
4373
4374#### **Functions and variables**
4375
4376```
4377 tool()
Julie Hockette2a29402018-07-31 10:11:42 -07004378 The tool() function call specifies the commands to run for a given step. See
4379 "gn help tool".
Brett Wilson796ed472018-07-16 15:11:09 -07004380
Julie Hockette2a29402018-07-31 10:11:42 -07004381 toolchain_args [scope]
Brett Wilson796ed472018-07-16 15:11:09 -07004382 Overrides for build arguments to pass to the toolchain when invoking it.
4383 This is a variable of type "scope" where the variable names correspond to
4384 variables in declare_args() blocks.
4385
4386 When you specify a target using an alternate toolchain, the master build
4387 configuration file is re-interpreted in the context of that toolchain.
4388 toolchain_args allows you to control the arguments passed into this
4389 alternate invocation of the build.
4390
4391 Any default system arguments or arguments passed in via "gn args" will also
4392 be passed to the alternate invocation unless explicitly overridden by
4393 toolchain_args.
4394
4395 The toolchain_args will be ignored when the toolchain being defined is the
4396 default. In this case, it's expected you want the default argument values.
4397
4398 See also "gn help buildargs" for an overview of these arguments.
4399
Julie Hockette2a29402018-07-31 10:11:42 -07004400 propagates_configs [boolean, default=false]
4401 Determines whether public_configs and all_dependent_configs in this
4402 toolchain propagate to targets in other toolchains.
4403
4404 When false (the default), this toolchain will not propagate any configs to
4405 targets in other toolchains that depend on it targets inside this
4406 toolchain. This matches the most common usage of toolchains where they
4407 represent different architectures or compilers and the settings that apply
4408 to one won't necessarily apply to others.
4409
4410 When true, configs (public and all-dependent) will cross the boundary out
4411 of this toolchain as if the toolchain boundary wasn't there. This only
4412 affects one direction of dependencies: a toolchain can't control whether
4413 it accepts such configs, only whether it pushes them. The build is
4414 responsible for ensuring that any external targets depending on targets in
4415 this toolchain are compatible with the compiler flags, etc. that may be
4416 propagated.
4417
4418 deps [string list]
Brett Wilson796ed472018-07-16 15:11:09 -07004419 Dependencies of this toolchain. These dependencies will be resolved before
4420 any target in the toolchain is compiled. To avoid circular dependencies
4421 these must be targets defined in another toolchain.
4422
4423 This is expressed as a list of targets, and generally these targets will
4424 always specify a toolchain:
4425 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
4426
4427 This concept is somewhat inefficient to express in Ninja (it requires a lot
4428 of duplicate of rules) so should only be used when absolutely necessary.
4429```
4430
4431#### **Example of defining a toolchain**
4432
4433```
4434 toolchain("32") {
4435 tool("cc") {
4436 command = "gcc {{source}}"
4437 ...
4438 }
4439
4440 toolchain_args = {
4441 use_doom_melon = true # Doom melon always required for 32-bit builds.
4442 current_cpu = "x86"
4443 }
4444 }
4445
4446 toolchain("64") {
4447 tool("cc") {
4448 command = "gcc {{source}}"
4449 ...
4450 }
4451
4452 toolchain_args = {
4453 # use_doom_melon is not overridden here, it will take the default.
4454 current_cpu = "x64"
4455 }
4456 }
4457```
4458
4459#### **Example of cross-toolchain dependencies**
4460
4461```
4462 If a 64-bit target wants to depend on a 32-bit binary, it would specify a
4463 dependency using data_deps (data deps are like deps that are only needed at
4464 runtime and aren't linked, since you can't link a 32-bit and a 64-bit
4465 library).
4466
4467 executable("my_program") {
4468 ...
4469 if (target_cpu == "x64") {
4470 # The 64-bit build needs this 32-bit helper.
4471 data_deps = [ ":helper(//toolchains:32)" ]
4472 }
4473 }
4474
4475 if (target_cpu == "x86") {
4476 # Our helper library is only compiled in 32-bits.
4477 shared_library("helper") {
4478 ...
4479 }
4480 }
4481```
[email protected]449f3e42024-08-01 21:43:27 +08004482### **write_file**: Write a file to disk. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004483
4484```
Julie Hockett09171292018-07-31 14:35:10 -07004485 write_file(filename, data, output_conversion = "")
Brett Wilson796ed472018-07-16 15:11:09 -07004486
4487 If data is a list, the list will be written one-item-per-line with no quoting
4488 or brackets.
4489
4490 If the file exists and the contents are identical to that being written, the
4491 file will not be updated. This will prevent unnecessary rebuilds of targets
4492 that depend on this file.
4493
4494 One use for write_file is to write a list of inputs to an script that might
4495 be too long for the command line. However, it is preferable to use response
4496 files for this purpose. See "gn help response_file_contents".
Brett Wilson796ed472018-07-16 15:11:09 -07004497```
4498
4499#### **Arguments**
4500
4501```
4502 filename
4503 Filename to write. This must be within the output directory.
4504
4505 data
4506 The list or string to write.
Julie Hockett09171292018-07-31 14:35:10 -07004507
4508 output_conversion
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02004509 Controls how the output is written. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07004510```
4511## Built-in predefined variables
4512
[email protected]449f3e42024-08-01 21:43:27 +08004513### **current_cpu**: The processor architecture of the current toolchain. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004514
4515```
4516 The build configuration usually sets this value based on the value of
4517 "host_cpu" (see "gn help host_cpu") and then threads this through the
4518 toolchain definitions to ensure that it always reflects the appropriate
4519 value.
4520
Julie Hockette2a29402018-07-31 10:11:42 -07004521 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004522 empty string ("") by default but is declared so that it can be overridden on
4523 the command line if so desired.
4524
4525 See "gn help target_cpu" for a list of common values returned.
4526```
[email protected]449f3e42024-08-01 21:43:27 +08004527### **current_os**: The operating system of the current toolchain. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004528
4529```
4530 The build configuration usually sets this value based on the value of
4531 "target_os" (see "gn help target_os"), and then threads this through the
4532 toolchain definitions to ensure that it always reflects the appropriate
4533 value.
4534
Julie Hockette2a29402018-07-31 10:11:42 -07004535 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004536 empty string ("") by default but is declared so that it can be overridden on
4537 the command line if so desired.
4538
4539 See "gn help target_os" for a list of common values returned.
4540```
[email protected]449f3e42024-08-01 21:43:27 +08004541### **current_toolchain**: Label of the current toolchain. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004542
4543```
4544 A fully-qualified label representing the current toolchain. You can use this
4545 to make toolchain-related decisions in the build. See also
4546 "default_toolchain".
4547```
4548
4549#### **Example**
4550
4551```
4552 if (current_toolchain == "//build:64_bit_toolchain") {
4553 executable("output_thats_64_bit_only") {
4554 ...
4555```
[email protected]449f3e42024-08-01 21:43:27 +08004556### **default_toolchain**: [string] Label of the default toolchain. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004557
4558```
4559 A fully-qualified label representing the default toolchain, which may not
4560 necessarily be the current one (see "current_toolchain").
4561```
[email protected]449f3e42024-08-01 21:43:27 +08004562### **gn_version**: [number] The version of gn. [Back to Top](#gn-reference)
James Robinson61377e32020-02-13 15:20:07 -08004563
4564```
Dirk Pranke7f109842025-04-09 16:48:38 -07004565 Corresponds to the number printed by `gn --version`. This variable is
4566 only variable available in the dotfile (all the rest are missing
4567 because the dotfile has to be parsed before args.gn or anything else
4568 is processed).
James Robinson61377e32020-02-13 15:20:07 -08004569```
4570
4571#### **Example**
4572
4573```
4574 assert(gn_version >= 1700, "need GN version 1700 for the frobulate feature")
4575```
[email protected]449f3e42024-08-01 21:43:27 +08004576### **host_cpu**: The processor architecture that GN is running on. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004577
4578```
4579 This is value is exposed so that cross-compile toolchains can access the host
4580 architecture when needed.
4581
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004582 The value should generally be considered read-only, but it can be overridden
Brett Wilson796ed472018-07-16 15:11:09 -07004583 in order to handle unusual cases where there might be multiple plausible
4584 values for the host architecture (e.g., if you can do either 32-bit or 64-bit
4585 builds). The value is not used internally by GN for any purpose.
4586```
4587
4588#### **Some possible values**
4589
4590```
4591 - "x64"
4592 - "x86"
4593```
[email protected]449f3e42024-08-01 21:43:27 +08004594### **host_os**: [string] The operating system that GN is running on. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004595
4596```
4597 This value is exposed so that cross-compiles can access the host build
4598 system's settings.
4599
4600 This value should generally be treated as read-only. It, however, is not used
4601 internally by GN for any purpose.
4602```
4603
4604#### **Some possible values**
4605
4606```
4607 - "linux"
4608 - "mac"
4609 - "win"
4610```
[email protected]449f3e42024-08-01 21:43:27 +08004611### **invoker**: [string] The invoking scope inside a template. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004612
4613```
4614 Inside a template invocation, this variable refers to the scope of the
4615 invoker of the template. Outside of template invocations, this variable is
4616 undefined.
4617
4618 All of the variables defined inside the template invocation are accessible as
4619 members of the "invoker" scope. This is the way that templates read values
4620 set by the callers.
4621
4622 This is often used with "defined" to see if a value is set on the invoking
4623 scope.
4624
4625 See "gn help template" for more examples.
4626```
4627
4628#### **Example**
4629
4630```
4631 template("my_template") {
4632 print(invoker.sources) # Prints [ "a.cc", "b.cc" ]
4633 print(defined(invoker.foo)) # Prints false.
4634 print(defined(invoker.bar)) # Prints true.
4635 }
4636
4637 my_template("doom_melon") {
4638 sources = [ "a.cc", "b.cc" ]
4639 bar = 123
4640 }
4641```
[email protected]449f3e42024-08-01 21:43:27 +08004642### **python_path**: Absolute path of Python. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004643
4644```
4645 Normally used in toolchain definitions if running some command requires
4646 Python. You will normally not need this when invoking scripts since GN
4647 automatically finds it for you.
4648```
[email protected]449f3e42024-08-01 21:43:27 +08004649### **root_build_dir**: [string] Directory where build commands are run. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004650
4651```
4652 This is the root build output directory which will be the current directory
4653 when executing all compilers and scripts.
4654
4655 Most often this is used with rebase_path (see "gn help rebase_path") to
4656 convert arguments to be relative to a script's current directory.
4657```
[email protected]449f3e42024-08-01 21:43:27 +08004658### **root_gen_dir**: Directory for the toolchain's generated files. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004659
4660```
4661 Absolute path to the root of the generated output directory tree for the
4662 current toolchain. An example would be "//out/Debug/gen" for the default
4663 toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain.
4664
4665 This is primarily useful for setting up include paths for generated files. If
4666 you are passing this to a script, you will want to pass it through
4667 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4668 build directory.
4669
4670 See also "target_gen_dir" which is usually a better location for generated
4671 files. It will be inside the root generated dir.
4672```
[email protected]449f3e42024-08-01 21:43:27 +08004673### **root_out_dir**: [string] Root directory for toolchain output files. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004674
4675```
4676 Absolute path to the root of the output directory tree for the current
4677 toolchain. It will not have a trailing slash.
4678
4679 For the default toolchain this will be the same as the root_build_dir. An
4680 example would be "//out/Debug" for the default toolchain, or
4681 "//out/Debug/arm" for the "arm" toolchain.
4682
4683 This is primarily useful for setting up script calls. If you are passing this
4684 to a script, you will want to pass it through rebase_path() (see "gn help
4685 rebase_path") to convert it to be relative to the build directory.
4686
4687 See also "target_out_dir" which is usually a better location for output
4688 files. It will be inside the root output dir.
4689```
4690
4691#### **Example**
4692
4693```
4694 action("myscript") {
4695 # Pass the output dir to the script.
4696 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
4697 }
4698```
[email protected]449f3e42024-08-01 21:43:27 +08004699### **target_cpu**: The desired cpu architecture for the build. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004700
4701```
4702 This value should be used to indicate the desired architecture for the
4703 primary objects of the build. It will match the cpu architecture of the
4704 default toolchain, but not necessarily the current toolchain.
4705
4706 In many cases, this is the same as "host_cpu", but in the case of
4707 cross-compiles, this can be set to something different. This value is
4708 different from "current_cpu" in that it does not change based on the current
4709 toolchain. When writing rules, "current_cpu" should be used rather than
4710 "target_cpu" most of the time.
4711
4712 This value is not used internally by GN for any purpose, so it may be set to
4713 whatever value is needed for the build. GN defaults this value to the empty
4714 string ("") and the configuration files should set it to an appropriate value
4715 (e.g., setting it to the value of "host_cpu") if it is not overridden on the
4716 command line or in the args.gn file.
4717```
4718
4719#### **Possible values**
4720
4721```
4722 - "x86"
4723 - "x64"
4724 - "arm"
4725 - "arm64"
4726 - "mipsel"
Tyler Mandry4a648092022-02-15 19:47:09 +00004727 - "mips64el"
4728 - "s390x"
4729 - "ppc64"
4730 - "riscv32"
4731 - "riscv64"
4732 - "e2k"
4733 - "loong64"
Brett Wilson796ed472018-07-16 15:11:09 -07004734```
[email protected]449f3e42024-08-01 21:43:27 +08004735### **target_gen_dir**: Directory for a target's generated files. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004736
4737```
4738 Absolute path to the target's generated file directory. This will be the
4739 "root_gen_dir" followed by the relative path to the current build file. If
4740 your file is in "//tools/doom_melon" then target_gen_dir would be
4741 "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash.
4742
4743 This is primarily useful for setting up include paths for generated files. If
4744 you are passing this to a script, you will want to pass it through
4745 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4746 build directory.
4747
4748 See also "gn help root_gen_dir".
4749```
4750
4751#### **Example**
4752
4753```
4754 action("myscript") {
4755 # Pass the generated output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004756 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004757 }
4758```
[email protected]449f3e42024-08-01 21:43:27 +08004759### **target_name**: [string] The name of the current target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004760
4761```
4762 Inside a target or template invocation, this variable refers to the name
4763 given to the target or template invocation. Outside of these, this variable
4764 is undefined.
4765
4766 This is most often used in template definitions to name targets defined in
4767 the template based on the name of the invocation. This is necessary both to
4768 ensure generated targets have unique names and to generate a target with the
4769 exact name of the invocation that other targets can depend on.
4770
4771 Be aware that this value will always reflect the innermost scope. So when
4772 defining a target inside a template, target_name will refer to the target
4773 rather than the template invocation. To get the name of the template
4774 invocation in this case, you should save target_name to a temporary variable
4775 outside of any target definitions.
4776
4777 See "gn help template" for more examples.
4778```
4779
4780#### **Example**
4781
4782```
4783 executable("doom_melon") {
4784 print(target_name) # Prints "doom_melon".
4785 }
4786
4787 template("my_template") {
4788 print(target_name) # Prints "space_ray" when invoked below.
4789
4790 executable(target_name + "_impl") {
4791 print(target_name) # Prints "space_ray_impl".
4792 }
4793 }
4794
4795 my_template("space_ray") {
4796 }
4797```
[email protected]449f3e42024-08-01 21:43:27 +08004798### **target_os**: The desired operating system for the build. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004799
4800```
4801 This value should be used to indicate the desired operating system for the
4802 primary object(s) of the build. It will match the OS of the default
4803 toolchain.
4804
4805 In many cases, this is the same as "host_os", but in the case of
4806 cross-compiles, it may be different. This variable differs from "current_os"
4807 in that it can be referenced from inside any toolchain and will always return
4808 the initial value.
4809
4810 This should be set to the most specific value possible. So, "android" or
4811 "chromeos" should be used instead of "linux" where applicable, even though
4812 Android and ChromeOS are both Linux variants. This can mean that one needs to
4813 write
4814
4815 if (target_os == "android" || target_os == "linux") {
4816 # ...
4817 }
4818
4819 and so forth.
4820
4821 This value is not used internally by GN for any purpose, so it may be set to
4822 whatever value is needed for the build. GN defaults this value to the empty
4823 string ("") and the configuration files should set it to an appropriate value
4824 (e.g., setting it to the value of "host_os") if it is not set via the command
4825 line or in the args.gn file.
4826```
4827
4828#### **Possible values**
4829
4830```
4831 - "android"
4832 - "chromeos"
4833 - "ios"
4834 - "linux"
4835 - "nacl"
4836 - "mac"
4837 - "win"
4838```
[email protected]449f3e42024-08-01 21:43:27 +08004839### **target_out_dir**: [string] Directory for target output files. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004840
4841```
4842 Absolute path to the target's generated file directory. If your current
4843 target is in "//tools/doom_melon" then this value might be
4844 "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash.
4845
4846 This is primarily useful for setting up arguments for calling scripts. If you
4847 are passing this to a script, you will want to pass it through rebase_path()
4848 (see "gn help rebase_path") to convert it to be relative to the build
4849 directory.
4850
4851 See also "gn help root_out_dir".
4852```
4853
4854#### **Example**
4855
4856```
4857 action("myscript") {
4858 # Pass the output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004859 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004860 }
4861```
4862## Variables you set in targets
4863
[email protected]449f3e42024-08-01 21:43:27 +08004864### **aliased_deps**: [scope] Set of crate-dependency pairs. [Back to Top](#gn-reference)
Julie Hockettce1fa072019-05-07 17:44:37 -07004865
4866```
4867 Valid for `rust_library` targets and `executable`, `static_library`, and
4868 `shared_library` targets that contain Rust sources.
4869
4870 A scope, each key indicating the renamed crate and the corresponding value
4871 specifying the label of the dependency producing the relevant binary.
4872
4873 All dependencies listed in this field *must* be listed as deps of the target.
4874
4875 executable("foo") {
4876 sources = [ "main.rs" ]
4877 deps = [ "//bar" ]
4878 }
4879
4880 This target would compile the `foo` crate with the following `extern` flag:
4881 `rustc ...command... --extern bar=/obj/bar`
4882
4883 executable("foo") {
4884 sources = [ "main.rs" ]
4885 deps = [ ":bar" ]
4886 aliased_deps = {
4887 bar_renamed = ":bar"
4888 }
4889 }
4890
4891 With the addition of `aliased_deps`, above target would instead compile with:
4892 `rustc ...command... --extern bar_renamed=/obj/bar`
4893```
[email protected]449f3e42024-08-01 21:43:27 +08004894### **all_dependent_configs**: Configs to be forced on dependents. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004895
4896```
4897 A list of config labels.
4898
4899 All targets depending on this one, and recursively, all targets depending on
4900 those, will have the configs listed in this variable added to them. These
4901 configs will also apply to the current target.
4902
4903 This addition happens in a second phase once a target and all of its
4904 dependencies have been resolved. Therefore, a target will not see these
4905 force-added configs in their "configs" variable while the script is running,
Brett Wilson20806f72024-09-09 10:04:39 -07004906 and they can not be removed.
4907
4908 Use of all_dependent_configs should be avoided when possible.
4909
4910 If your target has include_dirs and defines needed by targets that depend on
4911 it, the correct solution is to add those settings to public_configs and those
4912 targets choose whether to forward them up the dependency tree by whether they
4913 depend on your target via public_deps or not.
4914
4915 There are two recommended uses of this feature:
4916
4917 1. Legacy cases that can't easily be updated to use the proper public deps
4918 and configs.
4919 2. Additional linker flag that need to be set on the final linked target
4920 regardless of whether the dependency path is public or private.
Brett Wilson796ed472018-07-16 15:11:09 -07004921
4922 See also "public_configs".
4923```
4924
4925#### **Ordering of flags and values**
4926
4927```
4928 1. Those set on the current target (not in a config).
4929 2. Those set on the "configs" on the target in order that the
4930 configs appear in the list.
4931 3. Those set on the "all_dependent_configs" on the target in order
4932 that the configs appear in the list.
4933 4. Those set on the "public_configs" on the target in order that
4934 those configs appear in the list.
4935 5. all_dependent_configs pulled from dependencies, in the order of
4936 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004937 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07004938 6. public_configs pulled from dependencies, in the order of the
4939 "deps" list. If a dependency is public, they will be applied
4940 recursively.
4941```
[email protected]449f3e42024-08-01 21:43:27 +08004942### **allow_circular_includes_from**: Permit includes from deps. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07004943
4944```
4945 A list of target labels. Must be a subset of the target's "deps". These
4946 targets will be permitted to include headers from the current target despite
4947 the dependency going in the opposite direction.
4948
4949 When you use this, both targets must be included in a final binary for it to
4950 link. To keep linker errors from happening, it is good practice to have all
4951 external dependencies depend only on one of the two targets, and to set the
4952 visibility on the other to enforce this. Thus the targets will always be
4953 linked together in any output.
4954```
4955
4956#### **Details**
4957
4958```
4959 Normally, for a file in target A to include a file from target B, A must list
4960 B as a dependency. This invariant is enforced by the "gn check" command (and
4961 the --check flag to "gn gen" -- see "gn help check").
4962
4963 Sometimes, two targets might be the same unit for linking purposes (two
4964 source sets or static libraries that would always be linked together in a
4965 final executable or shared library) and they each include headers from the
4966 other: you want A to be able to include B's headers, and B to include A's
4967 headers. This is not an ideal situation but is sometimes unavoidable.
4968
4969 This list, if specified, lists which of the dependencies of the current
4970 target can include header files from the current target. That is, if A
4971 depends on B, B can only include headers from A if it is in A's
4972 allow_circular_includes_from list. Normally includes must follow the
4973 direction of dependencies, this flag allows them to go in the opposite
4974 direction.
4975```
4976
4977#### **Danger**
4978
4979```
4980 In the above example, A's headers are likely to include headers from A's
4981 dependencies. Those dependencies may have public_configs that apply flags,
4982 defines, and include paths that make those headers work properly.
4983
4984 With allow_circular_includes_from, B can include A's headers, and
4985 transitively from A's dependencies, without having the dependencies that
4986 would bring in the public_configs those headers need. The result may be
4987 errors or inconsistent builds.
4988
4989 So when you use allow_circular_includes_from, make sure that any compiler
4990 settings, flags, and include directories are the same between both targets
4991 (consider putting such things in a shared config they can both reference).
4992 Make sure the dependencies are also the same (you might consider a group to
4993 collect such dependencies they both depend on).
4994```
4995
4996#### **Example**
4997
4998```
4999 source_set("a") {
5000 deps = [ ":b", ":a_b_shared_deps" ]
5001 allow_circular_includes_from = [ ":b" ]
5002 ...
5003 }
5004
5005 source_set("b") {
5006 deps = [ ":a_b_shared_deps" ]
5007 # Sources here can include headers from a despite lack of deps.
5008 ...
5009 }
5010
5011 group("a_b_shared_deps") {
5012 public_deps = [ ":c" ]
5013 }
5014```
[email protected]449f3e42024-08-01 21:43:27 +08005015### **arflags**: Arguments passed to static_library archiver. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005016
5017```
5018 A list of flags passed to the archive/lib command that creates static
5019 libraries.
5020
5021 arflags are NOT pushed to dependents, so applying arflags to source sets or
5022 any other target type will be a no-op. As with ldflags, you could put the
5023 arflags in a config and set that as a public or "all dependent" config, but
5024 that will likely not be what you want. If you have a chain of static
5025 libraries dependent on each other, this can cause the flags to propagate up
5026 to other static libraries. Due to the nature of how arflags are typically
5027 used, you will normally want to apply them directly on static_library targets
5028 themselves.
5029```
5030
5031#### **Ordering of flags and values**
5032
5033```
5034 1. Those set on the current target (not in a config).
5035 2. Those set on the "configs" on the target in order that the
5036 configs appear in the list.
5037 3. Those set on the "all_dependent_configs" on the target in order
5038 that the configs appear in the list.
5039 4. Those set on the "public_configs" on the target in order that
5040 those configs appear in the list.
5041 5. all_dependent_configs pulled from dependencies, in the order of
5042 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005043 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005044 6. public_configs pulled from dependencies, in the order of the
5045 "deps" list. If a dependency is public, they will be applied
5046 recursively.
5047```
[email protected]449f3e42024-08-01 21:43:27 +08005048### **args**: (target variable) Arguments passed to an action. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005049
5050```
5051 For action and action_foreach targets, args is the list of arguments to pass
5052 to the script. Typically you would use source expansion (see "gn help
5053 source_expansion") to insert the source file names.
5054
Adrian Taylorc0d4f8b2022-03-25 15:05:57 -07005055 Args can also expand the substitution patterns corresponding to config
5056 variables in the same way that compiler tools (see "gn help tool") do. These
5057 allow actions that run compiler or compiler-like tools to access the results
5058 of propagating configs through the build graph. For example:
5059
5060 args = [ "{{defines}}", "{{include_dirs}}", "{{rustenv}}", "--input-file",
5061 "{{source}}" ]
5062
Brett Wilson796ed472018-07-16 15:11:09 -07005063 See also "gn help action" and "gn help action_foreach".
5064```
[email protected]449f3e42024-08-01 21:43:27 +08005065### **asmflags**: Flags passed to the assembler. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005066
5067```
5068 A list of strings.
5069
5070 "asmflags" are passed to any invocation of a tool that takes an .asm or .S
5071 file as input.
5072```
5073
5074#### **Ordering of flags and values**
5075
5076```
5077 1. Those set on the current target (not in a config).
5078 2. Those set on the "configs" on the target in order that the
5079 configs appear in the list.
5080 3. Those set on the "all_dependent_configs" on the target in order
5081 that the configs appear in the list.
5082 4. Those set on the "public_configs" on the target in order that
5083 those configs appear in the list.
5084 5. all_dependent_configs pulled from dependencies, in the order of
5085 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005086 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005087 6. public_configs pulled from dependencies, in the order of the
5088 "deps" list. If a dependency is public, they will be applied
5089 recursively.
5090```
[email protected]449f3e42024-08-01 21:43:27 +08005091### **assert_no_deps**: Ensure no deps on these targets. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005092
5093```
5094 A list of label patterns.
5095
5096 This list is a list of patterns that must not match any of the transitive
5097 dependencies of the target. These include all public, private, and data
5098 dependencies, and cross shared library boundaries. This allows you to express
5099 that undesirable code isn't accidentally added to downstream dependencies in
5100 a way that might otherwise be difficult to notice.
5101
5102 Checking does not cross executable boundaries. If a target depends on an
5103 executable, it's assumed that the executable is a tool that is producing part
5104 of the build rather than something that is linked and distributed. This
5105 allows assert_no_deps to express what is distributed in the final target
5106 rather than depend on the internal build steps (which may include
5107 non-distributable code).
5108
5109 See "gn help label_pattern" for the format of the entries in the list. These
5110 patterns allow blacklisting individual targets or whole directory
5111 hierarchies.
5112
5113 Sometimes it is desirable to enforce that many targets have no dependencies
5114 on a target or set of targets. One efficient way to express this is to create
5115 a group with the assert_no_deps rule on it, and make that group depend on all
5116 targets you want to apply that assertion to.
5117```
5118
5119#### **Example**
5120
5121```
5122 executable("doom_melon") {
5123 deps = [ "//foo:bar" ]
5124 ...
5125 assert_no_deps = [
5126 "//evil/*", # Don't link any code from the evil directory.
5127 "//foo:test_support", # This target is also disallowed.
5128 ]
5129 }
5130```
[email protected]449f3e42024-08-01 21:43:27 +08005131### **bridge_header**: [string] Path to C/Objective-C compatibility header. [Back to Top](#gn-reference)
Sylvain Defresne89e64252020-08-07 13:01:06 +02005132
5133```
5134 Valid for binary targets that contain Swift sources.
5135
5136 Path to an header that includes C/Objective-C functions and types that
5137 needs to be made available to the Swift module.
5138```
[email protected]449f3e42024-08-01 21:43:27 +08005139### **bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005140```
5141 create_bundle.
5142
5143 A string corresponding to a path in $root_build_dir.
5144
5145 This string is used by the "create_bundle" target to expand the
5146 {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must
5147 correspond to a path under "bundle_root_dir".
5148
5149 See "gn help bundle_root_dir" for examples.
5150```
[email protected]449f3e42024-08-01 21:43:27 +08005151### **bundle_deps_filter**: [label list] A list of labels that are filtered out. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005152
5153```
5154 A list of target labels.
5155
5156 This list contains target label patterns that should be filtered out when
5157 creating the bundle. Any target matching one of those label will be removed
5158 from the dependencies of the create_bundle target.
5159
5160 This is mostly useful when creating application extension bundle as the
5161 application extension has access to runtime resources from the application
5162 bundle and thus do not require a second copy.
5163
5164 See "gn help create_bundle" for more information.
5165```
5166
5167#### **Example**
5168
5169```
5170 create_bundle("today_extension") {
5171 deps = [
5172 "//base"
5173 ]
5174 bundle_root_dir = "$root_out_dir/today_extension.appex"
5175 bundle_deps_filter = [
5176 # The extension uses //base but does not use any function calling into
5177 # third_party/icu and thus does not need the icudtl.dat file.
5178 "//third_party/icu:icudata",
5179 ]
5180 }
5181```
[email protected]449f3e42024-08-01 21:43:27 +08005182### **bundle_executable_dir** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005183
Joe Armstrongb199e542019-03-21 09:40:08 +08005184```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02005185 bundle_executable_dir: Expansion of {{bundle_executable_dir}} in
5186 create_bundle.
5187
Brett Wilson796ed472018-07-16 15:11:09 -07005188 A string corresponding to a path in $root_build_dir.
5189
5190 This string is used by the "create_bundle" target to expand the
5191 {{bundle_executable_dir}} of the "bundle_data" target it depends on. This
5192 must correspond to a path under "bundle_root_dir".
5193
5194 See "gn help bundle_root_dir" for examples.
5195```
[email protected]449f3e42024-08-01 21:43:27 +08005196### **bundle_resources_dir** [Back to Top](#gn-reference)
Joe Armstrongb199e542019-03-21 09:40:08 +08005197
Brett Wilson796ed472018-07-16 15:11:09 -07005198```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02005199 bundle_resources_dir: Expansion of {{bundle_resources_dir}} in
5200 create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07005201
5202 A string corresponding to a path in $root_build_dir.
5203
5204 This string is used by the "create_bundle" target to expand the
5205 {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must
5206 correspond to a path under "bundle_root_dir".
5207
5208 See "gn help bundle_root_dir" for examples.
5209```
[email protected]449f3e42024-08-01 21:43:27 +08005210### **bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005211
5212```
5213 A string corresponding to a path in root_build_dir.
5214
5215 This string is used by the "create_bundle" target to expand the
5216 {{bundle_root_dir}} of the "bundle_data" target it depends on. This must
5217 correspond to a path under root_build_dir.
5218```
5219
5220#### **Example**
5221
5222```
5223 bundle_data("info_plist") {
5224 sources = [ "Info.plist" ]
5225 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
5226 }
5227
5228 create_bundle("doom_melon.app") {
5229 deps = [ ":info_plist" ]
5230 bundle_root_dir = "${root_build_dir}/doom_melon.app"
5231 bundle_contents_dir = "${bundle_root_dir}/Contents"
5232 bundle_resources_dir = "${bundle_contents_dir}/Resources"
5233 bundle_executable_dir = "${bundle_contents_dir}/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07005234 }
5235```
[email protected]449f3e42024-08-01 21:43:27 +08005236### **cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005237
5238```
5239 A list of strings.
5240
5241 "cflags" are passed to all invocations of the C, C++, Objective C, and
5242 Objective C++ compilers.
5243
5244 To target one of these variants individually, use "cflags_c", "cflags_cc",
5245 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5246 versions of cflags* will be appended on the compiler command line after
5247 "cflags".
5248
Max Ihlenfeldt5110a7f2024-01-22 11:56:47 +01005249 See also "asmflags" for flags for assembly-language files, "swiftflags" for
5250 swift files, and "rustflags" for Rust files.
Brett Wilson796ed472018-07-16 15:11:09 -07005251```
5252
5253#### **Ordering of flags and values**
5254
5255```
5256 1. Those set on the current target (not in a config).
5257 2. Those set on the "configs" on the target in order that the
5258 configs appear in the list.
5259 3. Those set on the "all_dependent_configs" on the target in order
5260 that the configs appear in the list.
5261 4. Those set on the "public_configs" on the target in order that
5262 those configs appear in the list.
5263 5. all_dependent_configs pulled from dependencies, in the order of
5264 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005265 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005266 6. public_configs pulled from dependencies, in the order of the
5267 "deps" list. If a dependency is public, they will be applied
5268 recursively.
5269```
[email protected]449f3e42024-08-01 21:43:27 +08005270### **cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005271
5272```
5273 A list of strings.
5274
5275 "cflags" are passed to all invocations of the C, C++, Objective C, and
5276 Objective C++ compilers.
5277
5278 To target one of these variants individually, use "cflags_c", "cflags_cc",
5279 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5280 versions of cflags* will be appended on the compiler command line after
5281 "cflags".
5282
Max Ihlenfeldt5110a7f2024-01-22 11:56:47 +01005283 See also "asmflags" for flags for assembly-language files, "swiftflags" for
5284 swift files, and "rustflags" for Rust files.
Brett Wilson796ed472018-07-16 15:11:09 -07005285```
5286
5287#### **Ordering of flags and values**
5288
5289```
5290 1. Those set on the current target (not in a config).
5291 2. Those set on the "configs" on the target in order that the
5292 configs appear in the list.
5293 3. Those set on the "all_dependent_configs" on the target in order
5294 that the configs appear in the list.
5295 4. Those set on the "public_configs" on the target in order that
5296 those configs appear in the list.
5297 5. all_dependent_configs pulled from dependencies, in the order of
5298 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005299 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005300 6. public_configs pulled from dependencies, in the order of the
5301 "deps" list. If a dependency is public, they will be applied
5302 recursively.
5303```
[email protected]449f3e42024-08-01 21:43:27 +08005304### **cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005305
5306```
5307 A list of strings.
5308
5309 "cflags" are passed to all invocations of the C, C++, Objective C, and
5310 Objective C++ compilers.
5311
5312 To target one of these variants individually, use "cflags_c", "cflags_cc",
5313 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5314 versions of cflags* will be appended on the compiler command line after
5315 "cflags".
5316
Max Ihlenfeldt5110a7f2024-01-22 11:56:47 +01005317 See also "asmflags" for flags for assembly-language files, "swiftflags" for
5318 swift files, and "rustflags" for Rust files.
Brett Wilson796ed472018-07-16 15:11:09 -07005319```
5320
5321#### **Ordering of flags and values**
5322
5323```
5324 1. Those set on the current target (not in a config).
5325 2. Those set on the "configs" on the target in order that the
5326 configs appear in the list.
5327 3. Those set on the "all_dependent_configs" on the target in order
5328 that the configs appear in the list.
5329 4. Those set on the "public_configs" on the target in order that
5330 those configs appear in the list.
5331 5. all_dependent_configs pulled from dependencies, in the order of
5332 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005333 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005334 6. public_configs pulled from dependencies, in the order of the
5335 "deps" list. If a dependency is public, they will be applied
5336 recursively.
5337```
[email protected]449f3e42024-08-01 21:43:27 +08005338### **cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005339
5340```
5341 A list of strings.
5342
5343 "cflags" are passed to all invocations of the C, C++, Objective C, and
5344 Objective C++ compilers.
5345
5346 To target one of these variants individually, use "cflags_c", "cflags_cc",
5347 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5348 versions of cflags* will be appended on the compiler command line after
5349 "cflags".
5350
Max Ihlenfeldt5110a7f2024-01-22 11:56:47 +01005351 See also "asmflags" for flags for assembly-language files, "swiftflags" for
5352 swift files, and "rustflags" for Rust files.
Brett Wilson796ed472018-07-16 15:11:09 -07005353```
5354
5355#### **Ordering of flags and values**
5356
5357```
5358 1. Those set on the current target (not in a config).
5359 2. Those set on the "configs" on the target in order that the
5360 configs appear in the list.
5361 3. Those set on the "all_dependent_configs" on the target in order
5362 that the configs appear in the list.
5363 4. Those set on the "public_configs" on the target in order that
5364 those configs appear in the list.
5365 5. all_dependent_configs pulled from dependencies, in the order of
5366 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005367 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005368 6. public_configs pulled from dependencies, in the order of the
5369 "deps" list. If a dependency is public, they will be applied
5370 recursively.
5371```
[email protected]449f3e42024-08-01 21:43:27 +08005372### **cflags***: Flags passed to the C compiler. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005373
5374```
5375 A list of strings.
5376
5377 "cflags" are passed to all invocations of the C, C++, Objective C, and
5378 Objective C++ compilers.
5379
5380 To target one of these variants individually, use "cflags_c", "cflags_cc",
5381 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5382 versions of cflags* will be appended on the compiler command line after
5383 "cflags".
5384
Max Ihlenfeldt5110a7f2024-01-22 11:56:47 +01005385 See also "asmflags" for flags for assembly-language files, "swiftflags" for
5386 swift files, and "rustflags" for Rust files.
Brett Wilson796ed472018-07-16 15:11:09 -07005387```
5388
5389#### **Ordering of flags and values**
5390
5391```
5392 1. Those set on the current target (not in a config).
5393 2. Those set on the "configs" on the target in order that the
5394 configs appear in the list.
5395 3. Those set on the "all_dependent_configs" on the target in order
5396 that the configs appear in the list.
5397 4. Those set on the "public_configs" on the target in order that
5398 those configs appear in the list.
5399 5. all_dependent_configs pulled from dependencies, in the order of
5400 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005401 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005402 6. public_configs pulled from dependencies, in the order of the
5403 "deps" list. If a dependency is public, they will be applied
5404 recursively.
5405```
[email protected]449f3e42024-08-01 21:43:27 +08005406### **check_includes**: [boolean] Controls whether a target's files are checked. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005407
5408```
5409 When true (the default), the "gn check" command (as well as "gn gen" with the
5410 --check flag) will check this target's sources and headers for proper
5411 dependencies.
5412
5413 When false, the files in this target will be skipped by default. This does
5414 not affect other targets that depend on the current target, it just skips
5415 checking the includes of the current target's files.
5416
5417 If there are a few conditionally included headers that trip up checking, you
5418 can exclude headers individually by annotating them with "nogncheck" (see "gn
5419 help nogncheck").
5420
5421 The topic "gn help check" has general information on how checking works and
5422 advice on how to pass a check in problematic cases.
5423```
5424
5425#### **Example**
5426
5427```
5428 source_set("busted_includes") {
5429 # This target's includes are messed up, exclude it from checking.
5430 check_includes = false
5431 ...
5432 }
5433```
[email protected]449f3e42024-08-01 21:43:27 +08005434### **code_signing_args**: [string list] [deprecated] Args for the post-processing script. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005435
5436```
Sylvain Defresnef07499a2024-04-04 13:06:37 +02005437 For create_bundle targets, post_processing_args is the list of arguments to
5438 pass to the post-processing script. Typically you would use source expansion
5439 (see "gn help source_expansion") to insert the source file names.
Brett Wilson796ed472018-07-16 15:11:09 -07005440
Sylvain Defresnef07499a2024-04-04 13:06:37 +02005441 Deprecated: this is an old name for the "post_processing_args" property of
5442 the "create_bundle" target. It is still supported to avoid breaking existing
5443 build rules, but a warning will be emitted when it is used.
5444
5445 See also "gn help create_bundle" and "gn help post_processing_args".
Brett Wilson796ed472018-07-16 15:11:09 -07005446```
[email protected]449f3e42024-08-01 21:43:27 +08005447### **code_signing_outputs**: [file list] [deprecated] Outputs of the post-processing step. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005448
5449```
Sylvain Defresnef07499a2024-04-04 13:06:37 +02005450 Outputs from the post-processing step of a create_bundle target. Must refer to
Brett Wilson796ed472018-07-16 15:11:09 -07005451 files in the build directory.
5452
Sylvain Defresnef07499a2024-04-04 13:06:37 +02005453 Deprecated: this is an old name for the "post_processing_outputs" property of
5454 the "create_bundle" target. It is still supported to avoid breaking existing
5455 build rules, but a warning will be emitted when it is used.
5456
5457 See also "gn help create_bundle" and "gn help post_processing_args".
Brett Wilson796ed472018-07-16 15:11:09 -07005458```
[email protected]449f3e42024-08-01 21:43:27 +08005459### **code_signing_script**: [file name] [deprecated] Script for the post-processing step." [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005460
5461```
5462 An absolute or buildfile-relative file name of a Python script to run for a
Sylvain Defresnef07499a2024-04-04 13:06:37 +02005463 create_bundle target to perform the post-processing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005464
Sylvain Defresnef07499a2024-04-04 13:06:37 +02005465 Deprecated: this is an old name for the "post_processing_script" property of
5466 the "create_bundle" target. It is still supported to avoid breaking existing
5467 build rules, but a warning will be emitted when it is used.
5468
5469 See also "gn help create_bundle" and "gn help post_processing_args".
Brett Wilson796ed472018-07-16 15:11:09 -07005470```
[email protected]449f3e42024-08-01 21:43:27 +08005471### **code_signing_sources**: [file list] [deprecated] Sources for the post-processing step. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005472
5473```
Sylvain Defresnef07499a2024-04-04 13:06:37 +02005474 A list of files used as input for the post-processing step of a create_bundle
Brett Wilson796ed472018-07-16 15:11:09 -07005475 target. Non-absolute paths will be resolved relative to the current build
5476 file.
5477
Sylvain Defresnef07499a2024-04-04 13:06:37 +02005478 Deprecated: this is an old name for the "post_processing_sources" property of
5479 the "create_bundle" target. It is still supported to avoid breaking existing
5480 build rules, but a warning will be emitted when it is used.
5481
5482 See also "gn help create_bundle" and "gn help post_processing_args".
Brett Wilson796ed472018-07-16 15:11:09 -07005483```
[email protected]449f3e42024-08-01 21:43:27 +08005484### **complete_static_lib**: [boolean] Links all deps into a static library. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005485
5486```
5487 A static library normally doesn't include code from dependencies, but instead
5488 forwards the static libraries and source sets in its deps up the dependency
5489 chain until a linkable target (an executable or shared library) is reached.
5490 The final linkable target only links each static library once, even if it
5491 appears more than once in its dependency graph.
5492
5493 In some cases the static library might be the final desired output. For
5494 example, you may be producing a static library for distribution to third
5495 parties. In this case, the static library should include code for all
5496 dependencies in one complete package. However, complete static libraries
5497 themselves are never linked into other complete static libraries. All
5498 complete static libraries are for distribution and linking them in would
5499 cause code duplication in this case. If the static library is not for
5500 distribution, it should not be complete.
5501
5502 GN treats non-complete static libraries as source sets when they are linked
5503 into complete static libraries. This is done because some tools like AR do
5504 not handle dependent static libraries properly. This makes it easier to write
5505 "alink" rules.
5506
5507 In rare cases it makes sense to list a header in more than one target if it
5508 could be considered conceptually a member of both. libraries.
5509```
5510
5511#### **Example**
5512
5513```
5514 static_library("foo") {
5515 complete_static_lib = true
5516 deps = [ "bar" ]
5517 }
5518```
[email protected]449f3e42024-08-01 21:43:27 +08005519### **configs**: Configs applying to this target or config. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005520
5521```
5522 A list of config labels.
5523```
5524
5525#### **Configs on a target**
5526
5527```
5528 When used on a target, the include_dirs, defines, etc. in each config are
5529 appended in the order they appear to the compile command for each file in the
5530 target. They will appear after the include_dirs, defines, etc. that the
5531 target sets directly.
5532
5533 Since configs apply after the values set on a target, directly setting a
5534 compiler flag will prepend it to the command line. If you want to append a
5535 flag instead, you can put that flag in a one-off config and append that
5536 config to the target's configs list.
5537
5538 The build configuration script will generally set up the default configs
5539 applying to a given target type (see "set_defaults"). When a target is being
5540 defined, it can add to or remove from this list.
5541```
5542
5543#### **Configs on a config**
5544
5545```
5546 It is possible to create composite configs by specifying configs on a config.
5547 One might do this to forward values, or to factor out blocks of settings from
5548 very large configs into more manageable named chunks.
5549
5550 In this case, the composite config is expanded to be the concatenation of its
5551 own values, and in order, the values from its sub-configs *before* anything
5552 else happens. This has some ramifications:
5553
5554 - A target has no visibility into a config's sub-configs. Target code only
5555 sees the name of the composite config. It can't remove sub-configs or opt
5556 in to only parts of it. The composite config may not even be defined
5557 before the target is.
5558
5559 - You can get duplication of values if a config is listed twice, say, on a
5560 target and in a sub-config that also applies. In other cases, the configs
5561 applying to a target are de-duped. It's expected that if a config is
5562 listed as a sub-config that it is only used in that context. (Note that
5563 it's possible to fix this and de-dupe, but it's not normally relevant and
5564 complicates the implementation.)
5565```
5566
5567#### **Ordering of flags and values**
5568
5569```
5570 1. Those set on the current target (not in a config).
5571 2. Those set on the "configs" on the target in order that the
5572 configs appear in the list.
5573 3. Those set on the "all_dependent_configs" on the target in order
5574 that the configs appear in the list.
5575 4. Those set on the "public_configs" on the target in order that
5576 those configs appear in the list.
5577 5. all_dependent_configs pulled from dependencies, in the order of
5578 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005579 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005580 6. public_configs pulled from dependencies, in the order of the
5581 "deps" list. If a dependency is public, they will be applied
5582 recursively.
5583```
5584
5585#### **Example**
5586
5587```
5588 # Configs on a target.
5589 source_set("foo") {
5590 # Don't use the default RTTI config that BUILDCONFIG applied to us.
5591 configs -= [ "//build:no_rtti" ]
5592
5593 # Add some of our own settings.
5594 configs += [ ":mysettings" ]
5595 }
5596
5597 # Create a default_optimization config that forwards to one of a set of more
5598 # specialized configs depending on build flags. This pattern is useful
5599 # because it allows a target to opt in to either a default set, or a more
5600 # specific set, while avoid duplicating the settings in two places.
5601 config("super_optimization") {
5602 cflags = [ ... ]
5603 }
5604 config("default_optimization") {
5605 if (optimize_everything) {
5606 configs = [ ":super_optimization" ]
5607 } else {
5608 configs = [ ":no_optimization" ]
5609 }
5610 }
5611```
[email protected]449f3e42024-08-01 21:43:27 +08005612### **contents**: Contents to write to file. [Back to Top](#gn-reference)
Nate Fischer8ed01d32019-01-08 17:32:01 -08005613
5614```
5615 The contents of the file for a generated_file target.
5616 See "gn help generated_file".
5617```
[email protected]449f3e42024-08-01 21:43:27 +08005618### **crate_name**: [string] The name for the compiled crate. [Back to Top](#gn-reference)
Julie Hockettce1fa072019-05-07 17:44:37 -07005619
5620```
5621 Valid for `rust_library` targets and `executable`, `static_library`,
5622 `shared_library`, and `source_set` targets that contain Rust sources.
5623
5624 If crate_name is not set, then this rule will use the target name.
5625```
[email protected]449f3e42024-08-01 21:43:27 +08005626### **crate_root**: [string] The root source file for a binary or library. [Back to Top](#gn-reference)
Julie Hockettce1fa072019-05-07 17:44:37 -07005627
5628```
5629 Valid for `rust_library` targets and `executable`, `static_library`,
5630 `shared_library`, and `source_set` targets that contain Rust sources.
5631
5632 This file is usually the `main.rs` or `lib.rs` for binaries and libraries,
5633 respectively.
5634
5635 If crate_root is not set, then this rule will look for a lib.rs file (or
5636 main.rs for executable) or a single file in sources, if sources contains
5637 only one file.
5638```
[email protected]449f3e42024-08-01 21:43:27 +08005639### **crate_type**: [string] The type of linkage to use on a shared_library. [Back to Top](#gn-reference)
Julie Hockettce1fa072019-05-07 17:44:37 -07005640
5641```
5642 Valid for `rust_library` targets and `executable`, `static_library`,
5643 `shared_library`, and `source_set` targets that contain Rust sources.
5644
5645 Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib".
5646 This field sets the `crate-type` attribute for the `rustc` tool on static
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005647 libraries, as well as the appropriate output extension in the
Julie Hockettce1fa072019-05-07 17:44:37 -07005648 `rust_output_extension` attribute. Since outputs must be explicit, the `lib`
5649 crate type (where the Rust compiler produces what it thinks is the
5650 appropriate library type) is not supported.
5651
5652 It should be noted that the "dylib" crate type in Rust is unstable in the set
5653 of symbols it exposes, and most usages today are potentially wrong and will
5654 be broken in the future.
5655
5656 Static libraries, rust libraries, and executables have this field set
5657 automatically.
5658```
[email protected]449f3e42024-08-01 21:43:27 +08005659### **data**: Runtime data file dependencies. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005660
5661```
5662 Lists files or directories required to run the given target. These are
5663 typically data files or directories of data files. The paths are interpreted
5664 as being relative to the current build file. Since these are runtime
5665 dependencies, they do not affect which targets are built or when. To declare
5666 input files to a script, use "inputs".
5667
5668 Appearing in the "data" section does not imply any special handling such as
5669 copying them to the output directory. This is just used for declaring runtime
5670 dependencies. Runtime dependencies can be queried using the "runtime_deps"
5671 category of "gn desc" or written during build generation via
5672 "--runtime-deps-list-file".
5673
5674 GN doesn't require data files to exist at build-time. So actions that produce
5675 files that are in turn runtime dependencies can list those generated files
5676 both in the "outputs" list as well as the "data" list.
5677
5678 By convention, directories are listed with a trailing slash:
5679 data = [ "test/data/" ]
5680 However, no verification is done on these so GN doesn't enforce this. The
5681 paths are just rebased and passed along when requested.
5682
5683 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5684 gathering data. See "gn help create_bundle" for details.
5685
5686 See "gn help runtime_deps" for how these are used.
5687```
[email protected]449f3e42024-08-01 21:43:27 +08005688### **data_deps**: Non-linked dependencies. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005689
5690```
5691 A list of target labels.
5692
5693 Specifies dependencies of a target that are not actually linked into the
5694 current target. Such dependencies will be built and will be available at
5695 runtime.
5696
5697 This is normally used for things like plugins or helper programs that a
5698 target needs at runtime.
5699
5700 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5701 gathering data_deps. See "gn help create_bundle" for details.
5702
5703 See also "gn help deps" and "gn help data".
5704```
5705
5706#### **Example**
5707
5708```
5709 executable("foo") {
5710 deps = [ "//base" ]
5711 data_deps = [ "//plugins:my_runtime_plugin" ]
5712 }
5713```
[email protected]449f3e42024-08-01 21:43:27 +08005714### **data_keys**: Keys from which to collect metadata. [Back to Top](#gn-reference)
Nate Fischer8ed01d32019-01-08 17:32:01 -08005715
5716```
5717 These keys are used to identify metadata to collect. If a walked target
5718 defines this key in its metadata, its value will be appended to the resulting
5719 collection.
5720
5721 See "gn help generated_file".
5722```
[email protected]449f3e42024-08-01 21:43:27 +08005723### **defines**: C preprocessor defines. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005724
5725```
5726 A list of strings
5727
5728 These strings will be passed to the C/C++ compiler as #defines. The strings
5729 may or may not include an "=" to assign a value.
5730```
5731
5732#### **Ordering of flags and values**
5733
5734```
5735 1. Those set on the current target (not in a config).
5736 2. Those set on the "configs" on the target in order that the
5737 configs appear in the list.
5738 3. Those set on the "all_dependent_configs" on the target in order
5739 that the configs appear in the list.
5740 4. Those set on the "public_configs" on the target in order that
5741 those configs appear in the list.
5742 5. all_dependent_configs pulled from dependencies, in the order of
5743 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005744 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005745 6. public_configs pulled from dependencies, in the order of the
5746 "deps" list. If a dependency is public, they will be applied
5747 recursively.
5748```
5749
5750#### **Example**
5751
5752```
5753 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
5754```
[email protected]449f3e42024-08-01 21:43:27 +08005755### **depfile**: [string] File name for input dependencies for actions. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005756
5757```
5758 If nonempty, this string specifies that the current action or action_foreach
5759 target will generate the given ".d" file containing the dependencies of the
5760 input. Empty or unset means that the script doesn't generate the files.
5761
5762 A depfile should be used only when a target depends on files that are not
5763 already specified by a target's inputs and sources. Likewise, depfiles should
5764 specify only those dependencies not already included in sources or inputs.
5765
5766 The .d file should go in the target output directory. If you have more than
5767 one source file that the script is being run over, you can use the output
5768 file expansions described in "gn help action_foreach" to name the .d file
Nico Weberad9eab22020-11-15 22:20:28 -05005769 according to the input.
Brett Wilson796ed472018-07-16 15:11:09 -07005770
5771 The format is that of a Makefile and all paths must be relative to the root
5772 build directory. Only one output may be listed and it must match the first
5773 output of the action.
5774
5775 Although depfiles are created by an action, they should not be listed in the
5776 action's "outputs" unless another target will use the file as an input.
5777```
5778
5779#### **Example**
5780
5781```
5782 action_foreach("myscript_target") {
5783 script = "myscript.py"
5784 sources = [ ... ]
5785
5786 # Locate the depfile in the output directory named like the
5787 # inputs but with a ".d" appended.
Hector Dearman84c84312023-01-29 11:50:02 +00005788 depfile = "$target_gen_dir/{{source_name_part}}.d"
Brett Wilson796ed472018-07-16 15:11:09 -07005789
5790 # Say our script uses "-o " to indicate the depfile.
Hector Dearman84c84312023-01-29 11:50:02 +00005791 args = [ "{{source}}", "-o", rebase_path(depfile, root_build_dir)]
Brett Wilson796ed472018-07-16 15:11:09 -07005792 }
5793```
[email protected]449f3e42024-08-01 21:43:27 +08005794### **deps**: Private linked dependencies. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005795
5796```
5797 A list of target labels.
5798
5799 Specifies private dependencies of a target. Private dependencies are
5800 propagated up the dependency tree and linked to dependent targets, but do not
5801 grant the ability to include headers from the dependency. Public configs are
5802 not forwarded.
5803```
5804
5805#### **Details of dependency propagation**
5806
5807```
5808 Source sets, shared libraries, and non-complete static libraries will be
5809 propagated up the dependency tree across groups, non-complete static
5810 libraries and source sets.
5811
5812 Executables, shared libraries, and complete static libraries will link all
5813 propagated targets and stop propagation. Actions and copy steps also stop
5814 propagation, allowing them to take a library as an input but not force
5815 dependents to link to it.
5816
5817 Propagation of all_dependent_configs and public_configs happens independently
5818 of target type. all_dependent_configs are always propagated across all types
5819 of targets, and public_configs are always propagated across public deps of
5820 all types of targets.
5821
danakj2a92efd2023-07-06 14:16:38 -04005822 For Rust targets, deps ensures that Rust code can refer to the dependency
5823 target. If the dependency is a C/C++ target, the path to that target will
5824 be made available to Rust for `#[link]` directives.
5825
Brett Wilson796ed472018-07-16 15:11:09 -07005826 Data dependencies are propagated differently. See "gn help data_deps" and
5827 "gn help runtime_deps".
5828
5829 See also "public_deps".
5830```
[email protected]449f3e42024-08-01 21:43:27 +08005831### **externs**: [scope] Set of Rust crate-dependency pairs. [Back to Top](#gn-reference)
Petr Hoseka1413862020-01-03 12:54:33 -08005832
5833```
5834 A list, each value being a scope indicating a pair of crate name and the path
5835 to the Rust library.
5836
5837 These libraries will be passed as `--extern crate_name=path` to compiler
5838 invocation containing the current target.
5839```
5840
5841#### **Examples**
5842
5843```
5844 executable("foo") {
5845 sources = [ "main.rs" ]
5846 externs = [{
5847 crate_name = "bar",
5848 path = "path/to/bar.rlib"
5849 }]
5850 }
5851
5852 This target would compile the `foo` crate with the following `extern` flag:
5853 `--extern bar=path/to/bar.rlib`.
5854```
[email protected]449f3e42024-08-01 21:43:27 +08005855### **framework_dirs**: [directory list] Additional framework search directories. [Back to Top](#gn-reference)
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005856
5857```
5858 A list of source directories.
5859
5860 The directories in this list will be added to the framework search path for
5861 the files in the affected target.
5862```
5863
5864#### **Ordering of flags and values**
5865
5866```
5867 1. Those set on the current target (not in a config).
5868 2. Those set on the "configs" on the target in order that the
5869 configs appear in the list.
5870 3. Those set on the "all_dependent_configs" on the target in order
5871 that the configs appear in the list.
5872 4. Those set on the "public_configs" on the target in order that
5873 those configs appear in the list.
5874 5. all_dependent_configs pulled from dependencies, in the order of
5875 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005876 more than once, only the first occurrence will be used.
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005877 6. public_configs pulled from dependencies, in the order of the
5878 "deps" list. If a dependency is public, they will be applied
5879 recursively.
5880```
5881
5882#### **Example**
5883
5884```
5885 framework_dirs = [ "src/include", "//third_party/foo" ]
5886```
[email protected]449f3e42024-08-01 21:43:27 +08005887### **frameworks**: [name list] Name of frameworks that must be linked. [Back to Top](#gn-reference)
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005888
5889```
5890 A list of framework names.
5891
5892 The frameworks named in that list will be linked with any dynamic link
5893 type target.
5894```
5895
5896#### **Ordering of flags and values**
5897
5898```
5899 1. Those set on the current target (not in a config).
5900 2. Those set on the "configs" on the target in order that the
5901 configs appear in the list.
5902 3. Those set on the "all_dependent_configs" on the target in order
5903 that the configs appear in the list.
5904 4. Those set on the "public_configs" on the target in order that
5905 those configs appear in the list.
5906 5. all_dependent_configs pulled from dependencies, in the order of
5907 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005908 more than once, only the first occurrence will be used.
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005909 6. public_configs pulled from dependencies, in the order of the
5910 "deps" list. If a dependency is public, they will be applied
5911 recursively.
5912```
5913
5914#### **Example**
5915
5916```
5917 frameworks = [ "Foundation.framework", "Foo.framework" ]
5918```
[email protected]449f3e42024-08-01 21:43:27 +08005919### **friend**: Allow targets to include private headers. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005920
5921```
5922 A list of label patterns (see "gn help label_pattern") that allow dependent
5923 targets to include private headers. Applies to all binary targets.
5924
5925 Normally if a target lists headers in the "public" list (see "gn help
5926 public"), other headers are implicitly marked as private. Private headers
5927 can not be included by other targets, even with a public dependency path.
5928 The "gn check" function performs this validation.
5929
5930 A friend declaration allows one or more targets to include private headers.
5931 This is useful for things like unit tests that are closely associated with a
5932 target and require internal knowledge without opening up all headers to be
5933 included by all dependents.
5934
5935 A friend target does not allow that target to include headers when no
5936 dependency exists. A public dependency path must still exist between two
5937 targets to include any headers from a destination target. The friend
5938 annotation merely allows the use of headers that would otherwise be
5939 prohibited because they are private.
5940
5941 The friend annotation is matched only against the target containing the file
5942 with the include directive. Friend annotations are not propagated across
5943 public or private dependencies. Friend annotations do not affect visibility.
5944```
5945
5946#### **Example**
5947
5948```
5949 static_library("lib") {
5950 # This target can include our private headers.
5951 friend = [ ":unit_tests" ]
5952
5953 public = [
5954 "public_api.h", # Normal public API for dependent targets.
5955 ]
5956
5957 # Private API and sources.
5958 sources = [
5959 "a_source_file.cc",
5960
5961 # Normal targets that depend on this one won't be able to include this
5962 # because this target defines a list of "public" headers. Without the
5963 # "public" list, all headers are implicitly public.
5964 "private_api.h",
5965 ]
5966 }
5967
5968 executable("unit_tests") {
5969 sources = [
5970 # This can include "private_api.h" from the :lib target because it
5971 # depends on that target and because of the friend annotation.
5972 "my_test.cc",
5973 ]
5974
5975 deps = [
5976 ":lib", # Required for the include to be allowed.
5977 ]
5978 }
5979```
[email protected]449f3e42024-08-01 21:43:27 +08005980### **gen_deps**: Declares targets that should generate when this one does. [Back to Top](#gn-reference)
Tyler Mandry4a648092022-02-15 19:47:09 +00005981
5982```
5983 A list of target labels.
5984
5985 Not all GN targets that get evaluated are actually turned into ninja targets
5986 (see "gn help execution"). If this target is generated, then any targets in
Ricardo Ribalda85bd0a62023-11-11 16:30:19 +00005987 the "gen_deps" list will also be generated, regardless of the usual criteria.
Tyler Mandry4a648092022-02-15 19:47:09 +00005988
5989 Since "gen_deps" are not build time dependencies, there can be cycles between
5990 "deps" and "gen_deps" or within "gen_deps" itself.
5991```
[email protected]449f3e42024-08-01 21:43:27 +08005992### **include_dirs**: Additional include directories. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07005993
5994```
5995 A list of source directories.
5996
5997 The directories in this list will be added to the include path for the files
5998 in the affected target.
5999```
6000
6001#### **Ordering of flags and values**
6002
6003```
6004 1. Those set on the current target (not in a config).
6005 2. Those set on the "configs" on the target in order that the
6006 configs appear in the list.
6007 3. Those set on the "all_dependent_configs" on the target in order
6008 that the configs appear in the list.
6009 4. Those set on the "public_configs" on the target in order that
6010 those configs appear in the list.
6011 5. all_dependent_configs pulled from dependencies, in the order of
6012 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006013 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006014 6. public_configs pulled from dependencies, in the order of the
6015 "deps" list. If a dependency is public, they will be applied
6016 recursively.
6017```
6018
6019#### **Example**
6020
6021```
6022 include_dirs = [ "src/include", "//third_party/foo" ]
6023```
[email protected]449f3e42024-08-01 21:43:27 +08006024### **inputs**: Additional compile-time dependencies. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006025
6026```
6027 Inputs are compile-time dependencies of the current target. This means that
6028 all inputs must be available before compiling any of the sources or executing
6029 any actions.
6030
6031 Inputs are typically only used for action and action_foreach targets.
6032```
6033
6034#### **Inputs for actions**
6035
6036```
6037 For action and action_foreach targets, inputs should be the inputs to script
6038 that don't vary. These should be all .py files that the script uses via
6039 imports (the main script itself will be an implicit dependency of the action
6040 so need not be listed).
6041
6042 For action targets, inputs and sources are treated the same, but from a style
6043 perspective, it's recommended to follow the same rule as action_foreach and
6044 put helper files in the inputs, and the data used by the script (if any) in
6045 sources.
6046
6047 Note that another way to declare input dependencies from an action is to have
6048 the action write a depfile (see "gn help depfile"). This allows the script to
6049 dynamically write input dependencies, that might not be known until actually
6050 executing the script. This is more efficient than doing processing while
6051 running GN to determine the inputs, and is easier to keep in-sync than
6052 hardcoding the list.
6053```
6054
6055#### **Script input gotchas**
6056
6057```
6058 It may be tempting to write a script that enumerates all files in a directory
6059 as inputs. Don't do this! Even if you specify all the files in the inputs or
6060 sources in the GN target (or worse, enumerate the files in an exec_script
6061 call when running GN, which will be slow), the dependencies will be broken.
6062
6063 The problem happens if a file is ever removed because the inputs are not
6064 listed on the command line to the script. Because the script hasn't changed
6065 and all inputs are up to date, the script will not re-run and you will get a
6066 stale build. Instead, either list all inputs on the command line to the
6067 script, or if there are many, create a separate list file that the script
6068 reads. As long as this file is listed in the inputs, the build will detect
6069 when it has changed in any way and the action will re-run.
6070```
6071
6072#### **Inputs for binary targets**
6073
6074```
Julie Hockette2a29402018-07-31 10:11:42 -07006075 Any input dependencies will be resolved before compiling any sources or
6076 linking the target. Normally, all actions that a target depends on will be run
6077 before any files in a target are compiled. So if you depend on generated
6078 headers, you do not typically need to list them in the inputs section.
Brett Wilson796ed472018-07-16 15:11:09 -07006079
6080 Inputs for binary targets will be treated as implicit dependencies, meaning
6081 that changes in any of the inputs will force all sources in the target to be
6082 recompiled. If an input only applies to a subset of source files, you may
6083 want to split those into a separate target to avoid unnecessary recompiles.
6084```
6085
6086#### **Example**
6087
6088```
6089 action("myscript") {
6090 script = "domything.py"
6091 inputs = [ "input.data" ]
6092 }
6093```
[email protected]449f3e42024-08-01 21:43:27 +08006094### **ldflags**: Flags passed to the linker. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006095
6096```
6097 A list of strings.
6098
6099 These flags are passed on the command-line to the linker and generally
6100 specify various linking options. Most targets will not need these and will
6101 use "libs" and "lib_dirs" instead.
6102
6103 ldflags are NOT pushed to dependents, so applying ldflags to source sets or
6104 static libraries will be a no-op. If you want to apply ldflags to dependent
6105 targets, put them in a config and set it in the all_dependent_configs or
6106 public_configs.
6107```
6108
6109#### **Ordering of flags and values**
6110
6111```
6112 1. Those set on the current target (not in a config).
6113 2. Those set on the "configs" on the target in order that the
6114 configs appear in the list.
6115 3. Those set on the "all_dependent_configs" on the target in order
6116 that the configs appear in the list.
6117 4. Those set on the "public_configs" on the target in order that
6118 those configs appear in the list.
6119 5. all_dependent_configs pulled from dependencies, in the order of
6120 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006121 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006122 6. public_configs pulled from dependencies, in the order of the
6123 "deps" list. If a dependency is public, they will be applied
6124 recursively.
6125```
[email protected]449f3e42024-08-01 21:43:27 +08006126### **lib_dirs**: Additional library directories. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006127
6128```
6129 A list of directories.
6130
6131 Specifies additional directories passed to the linker for searching for the
6132 required libraries. If an item is not an absolute path, it will be treated as
6133 being relative to the current build file.
6134
6135 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08006136 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07006137 shared library or executable target is reached. Second, they are
6138 uniquified so each one is only passed once (the first instance of it
6139 will be the one used).
6140```
6141
6142#### **Ordering of flags and values**
6143
6144```
6145 1. Those set on the current target (not in a config).
6146 2. Those set on the "configs" on the target in order that the
6147 configs appear in the list.
6148 3. Those set on the "all_dependent_configs" on the target in order
6149 that the configs appear in the list.
6150 4. Those set on the "public_configs" on the target in order that
6151 those configs appear in the list.
6152 5. all_dependent_configs pulled from dependencies, in the order of
6153 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006154 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006155 6. public_configs pulled from dependencies, in the order of the
6156 "deps" list. If a dependency is public, they will be applied
6157 recursively.
6158
6159 For "libs" and "lib_dirs" only, the values propagated from
6160 dependencies (as described above) are applied last assuming they
6161 are not already in the list.
6162```
6163
6164#### **Example**
6165
6166```
6167 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
6168```
[email protected]449f3e42024-08-01 21:43:27 +08006169### **libs**: Additional libraries to link. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006170
6171```
6172 A list of library names or library paths.
6173
6174 These libraries will be linked into the final binary (executable or shared
6175 library) containing the current target.
6176
6177 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08006178 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07006179 shared library or executable target is reached. Second, they are
6180 uniquified so each one is only passed once (the first instance of it
6181 will be the one used).
6182```
6183
6184#### **Types of libs**
6185
6186```
6187 There are several different things that can be expressed in libs:
6188
6189 File paths
6190 Values containing '/' will be treated as references to files in the
6191 checkout. They will be rebased to be relative to the build directory and
6192 specified in the "libs" for linker tools. This facility should be used
6193 for libraries that are checked in to the version control. For libraries
6194 that are generated by the build, use normal GN deps to link them.
6195
6196 System libraries
6197 Values not containing '/' will be treated as system library names. These
6198 will be passed unmodified to the linker and prefixed with the
6199 "lib_switch" attribute of the linker tool. Generally you would set the
6200 "lib_dirs" so the given library is found. Your BUILD.gn file should not
6201 specify the switch (like "-l"): this will be encoded in the "lib_switch"
6202 of the tool.
Brett Wilson796ed472018-07-16 15:11:09 -07006203```
6204
6205#### **Ordering of flags and values**
6206
6207```
6208 1. Those set on the current target (not in a config).
6209 2. Those set on the "configs" on the target in order that the
6210 configs appear in the list.
6211 3. Those set on the "all_dependent_configs" on the target in order
6212 that the configs appear in the list.
6213 4. Those set on the "public_configs" on the target in order that
6214 those configs appear in the list.
6215 5. all_dependent_configs pulled from dependencies, in the order of
6216 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006217 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006218 6. public_configs pulled from dependencies, in the order of the
6219 "deps" list. If a dependency is public, they will be applied
6220 recursively.
6221
6222 For "libs" and "lib_dirs" only, the values propagated from
6223 dependencies (as described above) are applied last assuming they
6224 are not already in the list.
6225```
6226
6227#### **Examples**
6228
6229```
6230 On Windows:
6231 libs = [ "ctl3d.lib" ]
6232
6233 On Linux:
6234 libs = [ "ld" ]
6235```
[email protected]449f3e42024-08-01 21:43:27 +08006236### **metadata**: Metadata of this target. [Back to Top](#gn-reference)
Nate Fischer8ed01d32019-01-08 17:32:01 -08006237
6238```
6239 Metadata is a collection of keys and values relating to a particular target.
Julie Hockettd69a9c32019-01-23 14:36:18 -08006240 Values must be lists, allowing for sane and predictable collection behavior.
6241 Generally, these keys will include three types of lists: lists of ordinary
6242 strings, lists of filenames intended to be rebased according to their
6243 particular source directory, and lists of target labels intended to be used
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006244 as barriers to the walk. Verification of these categories occurs at walk time,
Julie Hockettd69a9c32019-01-23 14:36:18 -08006245 not creation time (since it is not clear until the walk which values are
6246 intended for which purpose).
6247```
Nate Fischer8ed01d32019-01-08 17:32:01 -08006248
Julie Hockettd69a9c32019-01-23 14:36:18 -08006249#### **Example**
Nate Fischer8ed01d32019-01-08 17:32:01 -08006250
Julie Hockettd69a9c32019-01-23 14:36:18 -08006251```
Nate Fischer8ed01d32019-01-08 17:32:01 -08006252 group("doom_melon") {
6253 metadata = {
6254 # These keys are not built in to GN but are interpreted when consuming
6255 # metadata.
6256 my_barrier = []
6257 my_files = [ "a.txt", "b.txt" ]
6258 }
6259 }
6260```
[email protected]449f3e42024-08-01 21:43:27 +08006261### **mnemonic**: [string] Prefix displayed when ninja runs this action. [Back to Top](#gn-reference)
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07006262
6263```
6264 Tools in GN can set their ninja "description" which is displayed when
6265 building a target. These are commonly set with the format "CXX $output"
6266 or "LINK $label". By default, all GN actions will have the description
6267 "ACTION $label". Setting a mnemonic will override the "ACTION" prefix
6268 with another string, but the label will still be unconditionally displayed.
6269
6270 Whitespace is not allowed within a mnemonic.
6271```
[email protected]449f3e42024-08-01 21:43:27 +08006272### **module_name**: [string] The name for the compiled module. [Back to Top](#gn-reference)
Sylvain Defresne89e64252020-08-07 13:01:06 +02006273
6274```
6275 Valid for binary targets that contain Swift sources.
6276
6277 If module_name is not set, then this rule will use the target name.
6278```
[email protected]449f3e42024-08-01 21:43:27 +08006279### **output_conversion**: Data format for generated_file targets. [Back to Top](#gn-reference)
Nate Fischer8ed01d32019-01-08 17:32:01 -08006280
6281```
6282 Controls how the "contents" of a generated_file target is formatted.
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02006283 See `gn help io_conversion`.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006284```
[email protected]449f3e42024-08-01 21:43:27 +08006285### **output_dir**: [directory] Directory to put output file in. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006286
6287```
6288 For library and executable targets, overrides the directory for the final
6289 output. This must be in the root_build_dir or a child thereof.
6290
6291 This should generally be in the root_out_dir or a subdirectory thereof (the
6292 root_out_dir will be the same as the root_build_dir for the default
6293 toolchain, and will be a subdirectory for other toolchains). Not putting the
6294 output in a subdirectory of root_out_dir can result in collisions between
6295 different toolchains, so you will need to take steps to ensure that your
6296 target is only present in one toolchain.
6297
6298 Normally the toolchain specifies the output directory for libraries and
6299 executables (see "gn help tool"). You will have to consult that for the
6300 default location. The default location will be used if output_dir is
6301 undefined or empty.
6302```
6303
6304#### **Example**
6305
6306```
6307 shared_library("doom_melon") {
6308 output_dir = "$root_out_dir/plugin_libs"
6309 ...
6310 }
6311```
[email protected]449f3e42024-08-01 21:43:27 +08006312### **output_extension**: Value to use for the output's file extension. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006313
6314```
6315 Normally the file extension for a target is based on the target type and the
6316 operating system, but in rare cases you will need to override the name (for
6317 example to use "libfreetype.so.6" instead of libfreetype.so on Linux).
6318
6319 This value should not include a leading dot. If undefined, the default
6320 specified on the tool will be used. If set to the empty string, no output
6321 extension will be used.
6322
6323 The output_extension will be used to set the "{{output_extension}}" expansion
6324 which the linker tool will generally use to specify the output file name. See
6325 "gn help tool".
6326```
6327
6328#### **Example**
6329
6330```
6331 shared_library("freetype") {
6332 if (is_linux) {
6333 # Call the output "libfreetype.so.6"
6334 output_extension = "so.6"
6335 }
6336 ...
6337 }
6338
6339 # On Windows, generate a "mysettings.cpl" control panel applet. Control panel
6340 # applets are actually special shared libraries.
6341 if (is_win) {
6342 shared_library("mysettings") {
6343 output_extension = "cpl"
6344 ...
6345 }
6346 }
6347```
[email protected]449f3e42024-08-01 21:43:27 +08006348### **output_name**: Define a name for the output file other than the default. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006349
6350```
6351 Normally the output name of a target will be based on the target name, so the
6352 target "//foo/bar:bar_unittests" will generate an output file such as
6353 "bar_unittests.exe" (using Windows as an example).
6354
6355 Sometimes you will want an alternate name to avoid collisions or if the
6356 internal name isn't appropriate for public distribution.
6357
6358 The output name should have no extension or prefixes, these will be added
6359 using the default system rules. For example, on Linux an output name of "foo"
6360 will produce a shared library "libfoo.so". There is no way to override the
6361 output prefix of a linker tool on a per- target basis. If you need more
6362 flexibility, create a copy target to produce the file you want.
6363
6364 This variable is valid for all binary output target types.
6365```
6366
6367#### **Example**
6368
6369```
6370 static_library("doom_melon") {
6371 output_name = "fluffy_bunny"
6372 }
6373```
[email protected]449f3e42024-08-01 21:43:27 +08006374### **output_prefix_override**: Don't use prefix for output name. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006375
6376```
6377 A boolean that overrides the output prefix for a target. Defaults to false.
6378
6379 Some systems use prefixes for the names of the final target output file. The
6380 normal example is "libfoo.so" on Linux for a target named "foo".
6381
6382 The output prefix for a given target type is specified on the linker tool
6383 (see "gn help tool"). Sometimes this prefix is undesired.
6384
6385 See also "gn help output_extension".
6386```
6387
6388#### **Example**
6389
6390```
6391 shared_library("doom_melon") {
6392 # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag
6393 # will produce "doom_melon.so".
6394 output_prefix_override = true
6395 ...
6396 }
6397```
[email protected]449f3e42024-08-01 21:43:27 +08006398### **outputs**: Output files for actions and copy targets. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006399
6400```
6401 Outputs is valid for "copy", "action", and "action_foreach" target types and
6402 indicates the resulting files. Outputs must always refer to files in the
6403 build directory.
6404
6405 copy
6406 Copy targets should have exactly one entry in the outputs list. If there is
6407 exactly one source, this can be a literal file name or a source expansion.
6408 If there is more than one source, this must contain a source expansion to
6409 map a single input name to a single output name. See "gn help copy".
6410
6411 action_foreach
6412 Action_foreach targets must always use source expansions to map input files
6413 to output files. There can be more than one output, which means that each
6414 invocation of the script will produce a set of files (presumably based on
6415 the name of the input file). See "gn help action_foreach".
6416
6417 action
6418 Action targets (excluding action_foreach) must list literal output file(s)
6419 with no source expansions. See "gn help action".
6420```
[email protected]449f3e42024-08-01 21:43:27 +08006421### **partial_info_plist**: [filename] Path plist from asset catalog compiler. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006422
6423```
6424 Valid for create_bundle target, corresponds to the path for the partial
6425 Info.plist created by the asset catalog compiler that needs to be merged
Sylvain Defresnef07499a2024-04-04 13:06:37 +02006426 with the application Info.plist (usually done by the post-processing script).
Brett Wilson796ed472018-07-16 15:11:09 -07006427
6428 The file will be generated regardless of whether the asset compiler has
6429 been invoked or not. See "gn help create_bundle".
6430```
[email protected]449f3e42024-08-01 21:43:27 +08006431### **pool**: Label of the pool used by binary targets actions. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006432
6433```
Petr Hosek7a6231e2022-10-22 23:14:18 +00006434 A fully-qualified label representing the pool that will be used for binary
6435 targets and actions. Pools are defined using the pool() {...} declaration.
Brett Wilson796ed472018-07-16 15:11:09 -07006436```
6437
6438#### **Example**
6439
6440```
Petr Hosek7a6231e2022-10-22 23:14:18 +00006441 executable("binary") {
6442 pool = "//build:custom_pool"
6443 ...
6444 }
6445
Brett Wilson796ed472018-07-16 15:11:09 -07006446 action("action") {
6447 pool = "//build:custom_pool"
6448 ...
6449 }
6450```
[email protected]449f3e42024-08-01 21:43:27 +08006451### **post_processing_args**: [string list] Args for the post-processing script. [Back to Top](#gn-reference)
Sylvain Defresnef07499a2024-04-04 13:06:37 +02006452
6453```
6454 For create_bundle targets, post_processing_args is the list of arguments to
6455 pass to the post-processing script. Typically you would use source expansion
6456 (see "gn help source_expansion") to insert the source file names.
6457
6458 See also "gn help create_bundle".
6459```
[email protected]449f3e42024-08-01 21:43:27 +08006460### **post_processing_outputs**: [file list] Outputs of the post-processing step. [Back to Top](#gn-reference)
Sylvain Defresnef07499a2024-04-04 13:06:37 +02006461
6462```
6463 Outputs from the post-processing step of a create_bundle target. Must refer to
6464 files in the build directory.
6465
6466 See also "gn help create_bundle".
6467```
[email protected]449f3e42024-08-01 21:43:27 +08006468### **post_processing_script**: [file name] Script for the post-processing step." [Back to Top](#gn-reference)
Sylvain Defresnef07499a2024-04-04 13:06:37 +02006469
6470```
6471 An absolute or buildfile-relative file name of a Python script to run for a
6472 create_bundle target to perform the post-processing step.
6473
6474 See also "gn help create_bundle".
6475```
[email protected]449f3e42024-08-01 21:43:27 +08006476### **post_processing_sources**: [file list] Sources for the post-processing step. [Back to Top](#gn-reference)
Sylvain Defresnef07499a2024-04-04 13:06:37 +02006477
6478```
6479 A list of files used as input for the post-processing step of a create_bundle
6480 target. Non-absolute paths will be resolved relative to the current build
6481 file.
6482
6483 See also "gn help create_bundle".
6484```
[email protected]449f3e42024-08-01 21:43:27 +08006485### **precompiled_header**: [string] Header file to precompile. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006486
6487```
6488 Precompiled headers will be used when a target specifies this value, or a
6489 config applying to this target specifies this value. In addition, the tool
6490 corresponding to the source files must also specify precompiled headers (see
6491 "gn help tool"). The tool will also specify what type of precompiled headers
6492 to use, by setting precompiled_header_type to either "gcc" or "msvc".
6493
6494 The precompiled header/source variables can be specified on a target or a
6495 config, but must be the same for all configs applying to a given target since
6496 a target can only have one precompiled header.
6497
6498 If you use both C and C++ sources, the precompiled header and source file
6499 will be compiled once per language. You will want to make sure to wrap C++
6500 includes in __cplusplus #ifdefs so the file will compile in C mode.
6501```
6502
6503#### **GCC precompiled headers**
6504
6505```
6506 When using GCC-style precompiled headers, "precompiled_source" contains the
6507 path of a .h file that is precompiled and then included by all source files
6508 in targets that set "precompiled_source".
6509
6510 The value of "precompiled_header" is not used with GCC-style precompiled
6511 headers.
6512```
6513
6514#### **MSVC precompiled headers**
6515
6516```
6517 When using MSVC-style precompiled headers, the "precompiled_header" value is
6518 a string corresponding to the header. This is NOT a path to a file that GN
6519 recognises, but rather the exact string that appears in quotes after
6520 an #include line in source code. The compiler will match this string against
6521 includes or forced includes (/FI).
6522
6523 MSVC also requires a source file to compile the header with. This must be
6524 specified by the "precompiled_source" value. In contrast to the header value,
6525 this IS a GN-style file name, and tells GN which source file to compile to
6526 make the .pch file used for subsequent compiles.
6527
6528 For example, if the toolchain specifies MSVC headers:
6529
6530 toolchain("vc_x64") {
6531 ...
6532 tool("cxx") {
6533 precompiled_header_type = "msvc"
6534 ...
6535
6536 You might make a config like this:
6537
6538 config("use_precompiled_headers") {
6539 precompiled_header = "build/precompile.h"
6540 precompiled_source = "//build/precompile.cc"
6541
6542 # Either your source files should #include "build/precompile.h"
6543 # first, or you can do this to force-include the header.
6544 cflags = [ "/FI$precompiled_header" ]
6545 }
6546
6547 And then define a target that uses the config:
6548
6549 executable("doom_melon") {
6550 configs += [ ":use_precompiled_headers" ]
6551 ...
6552```
[email protected]449f3e42024-08-01 21:43:27 +08006553### **precompiled_header_type**: [string] "gcc" or "msvc". [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006554
6555```
6556 See "gn help precompiled_header".
6557```
[email protected]449f3e42024-08-01 21:43:27 +08006558### **precompiled_source**: [file name] Source file to precompile. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006559
6560```
6561 The source file that goes along with the precompiled_header when using
6562 "msvc"-style precompiled headers. It will be implicitly added to the sources
6563 of the target. See "gn help precompiled_header".
6564```
[email protected]449f3e42024-08-01 21:43:27 +08006565### **product_type**: [string] Product type for the bundle. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006566
6567```
Sylvain Defresnee5aee7b2023-06-08 10:08:10 -04006568 Valid for "create_bundle" and "bundle_data" targets.
Brett Wilson796ed472018-07-16 15:11:09 -07006569
Sylvain Defresnee5aee7b2023-06-08 10:08:10 -04006570 Correspond to the type of the bundle. Used by transparent "create_bundle"
6571 target to control whether a "bundle_data" needs to be propagated or not.
6572
6573 When generating Xcode project, the product_type is propagated and only
6574 "create_bundle" with a non-empty product_type will have a corresponding
6575 target in the project.
Brett Wilson796ed472018-07-16 15:11:09 -07006576```
[email protected]449f3e42024-08-01 21:43:27 +08006577### **public**: Declare public header files for a target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006578
6579```
6580 A list of files that other targets can include. These permissions are checked
6581 via the "check" command (see "gn help check").
6582
6583 If no public files are declared, other targets (assuming they have visibility
6584 to depend on this target) can include any file in the sources list. If this
6585 variable is defined on a target, dependent targets may only include files on
Dirk Prankeed1abc12025-01-10 17:41:03 -08006586 this allowlist unless that target is marked as a friend (see "gn help
Brett Wilson796ed472018-07-16 15:11:09 -07006587 friend").
6588
6589 Header file permissions are also subject to visibility. A target must be
6590 visible to another target to include any files from it at all and the public
6591 headers indicate which subset of those files are permitted. See "gn help
6592 visibility" for more.
6593
6594 Public files are inherited through the dependency tree. So if there is a
6595 dependency A -> B -> C, then A can include C's public headers. However, the
6596 same is NOT true of visibility, so unless A is in C's visibility list, the
6597 include will be rejected.
6598
6599 GN only knows about files declared in the "sources" and "public" sections of
6600 targets. If a file is included that is not known to the build, it will be
6601 allowed.
6602
6603 It is common for test targets to need to include private headers for their
6604 associated code. In this case, list the test target in the "friend" list of
6605 the target that owns the private header to allow the inclusion. See
6606 "gn help friend" for more.
Julie Hockette2a29402018-07-31 10:11:42 -07006607
6608 When a binary target has no explicit or implicit public headers (a "public"
6609 list is defined but is empty), GN assumes that the target can not propagate
6610 any compile-time dependencies up the dependency tree. In this case, the build
6611 can be parallelized more efficiently.
6612 Say there are dependencies:
6613 A (shared library) -> B (shared library) -> C (action).
6614 Normally C must complete before any source files in A can compile (because
6615 there might be generated includes). But when B explicitly declares no public
6616 headers, C can execute in parallel with A's compile steps. C must still be
6617 complete before any dependents link.
Brett Wilson796ed472018-07-16 15:11:09 -07006618```
6619
6620#### **Examples**
6621
6622```
6623 These exact files are public:
6624 public = [ "foo.h", "bar.h" ]
6625
6626 No files are public (no targets may include headers from this one):
Julie Hockette2a29402018-07-31 10:11:42 -07006627 # This allows starting compilation in dependent targets earlier.
Brett Wilson796ed472018-07-16 15:11:09 -07006628 public = []
6629```
[email protected]449f3e42024-08-01 21:43:27 +08006630### **public_configs**: Configs to be applied on dependents. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006631
6632```
6633 A list of config labels.
6634
6635 Targets directly depending on this one will have the configs listed in this
6636 variable added to them. These configs will also apply to the current target.
Julie Hockette2a29402018-07-31 10:11:42 -07006637 Generally, public configs are used to apply defines and include directories
6638 necessary to compile this target's header files.
Brett Wilson796ed472018-07-16 15:11:09 -07006639
Julie Hockette2a29402018-07-31 10:11:42 -07006640 See also "gn help all_dependent_configs".
6641```
6642
6643#### **Propagation of public configs**
6644
6645```
6646 Public configs are applied to all targets that depend directly on this one.
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006647 These dependent targets can further push this target's public configs
Julie Hockette2a29402018-07-31 10:11:42 -07006648 higher in the dependency tree by depending on it via public_deps (see "gn
6649 help public_deps").
6650
6651 static_library("toplevel") {
6652 # This target will get "my_config" applied to it. However, since this
6653 # target uses "deps" and not "public_deps", targets that depend on this
6654 # one won't get it.
6655 deps = [ ":intermediate" ]
6656 }
6657
6658 static_library("intermediate") {
6659 # Depending on "lower" in any way will apply "my_config" to this target.
6660 # Additionall, since this target depends on "lower" via public_deps,
6661 # targets that depend on this one will also get "my_config".
6662 public_deps = [ ":lower" ]
6663 }
6664
6665 static_library("lower") {
6666 # This will get applied to all targets that depend on this one.
6667 public_configs = [ ":my_config" ]
6668 }
6669
6670 Public config propagation happens in a second phase once a target and all of
6671 its dependencies have been resolved. Therefore, a target will not see these
Brett Wilson796ed472018-07-16 15:11:09 -07006672 force-added configs in their "configs" variable while the script is running,
6673 and they can not be removed. As a result, this capability should generally
Julie Hockette2a29402018-07-31 10:11:42 -07006674 only be used to add defines and include directories rather than setting
6675 complicated flags that some targets may not want.
Brett Wilson796ed472018-07-16 15:11:09 -07006676
Julie Hockette2a29402018-07-31 10:11:42 -07006677 Public configs may or may not be propagated across toolchain boundaries
6678 depending on the value of the propagates_configs flag (see "gn help
6679 toolchain") on the toolchain of the target declaring the public_config.
6680```
6681
6682#### **Avoiding applying public configs to this target**
6683
6684```
6685 If you want the config to apply to targets that depend on this one, but NOT
6686 this one, define an extra layer of indirection using a group:
6687
6688 # External targets depend on this group.
6689 group("my_target") {
6690 # Config to apply to all targets that depend on this one.
6691 public_configs = [ ":external_settings" ]
6692 deps = [ ":internal_target" ]
6693 }
6694
6695 # Internal target to actually compile the sources.
6696 static_library("internal_target") {
6697 # Force all external targets to depend on the group instead of directly
6698 # on this so the "external_settings" config will get applied.
6699 visibility = [ ":my_target" ]
6700 ...
6701 }
Brett Wilson796ed472018-07-16 15:11:09 -07006702```
6703
6704#### **Ordering of flags and values**
6705
6706```
6707 1. Those set on the current target (not in a config).
6708 2. Those set on the "configs" on the target in order that the
6709 configs appear in the list.
6710 3. Those set on the "all_dependent_configs" on the target in order
6711 that the configs appear in the list.
6712 4. Those set on the "public_configs" on the target in order that
6713 those configs appear in the list.
6714 5. all_dependent_configs pulled from dependencies, in the order of
6715 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006716 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006717 6. public_configs pulled from dependencies, in the order of the
6718 "deps" list. If a dependency is public, they will be applied
6719 recursively.
6720```
[email protected]449f3e42024-08-01 21:43:27 +08006721### **public_deps**: Declare public dependencies. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006722
6723```
6724 Public dependencies are like private dependencies (see "gn help deps") but
6725 additionally express that the current target exposes the listed deps as part
6726 of its public API.
6727
6728 This has several ramifications:
6729
6730 - public_configs that are part of the dependency are forwarded to direct
6731 dependents.
6732
6733 - Public headers in the dependency are usable by dependents (includes do
6734 not require a direct dependency or visibility).
6735
6736 - If the current target is a shared library, other shared libraries that it
6737 publicly depends on (directly or indirectly) are propagated up the
6738 dependency tree to dependents for linking.
Julie Hockette2a29402018-07-31 10:11:42 -07006739
6740 See also "gn help public_configs".
Brett Wilson796ed472018-07-16 15:11:09 -07006741```
6742
6743#### **Discussion**
6744
6745```
6746 Say you have three targets: A -> B -> C. C's visibility may allow B to depend
6747 on it but not A. Normally, this would prevent A from including any headers
6748 from C, and C's public_configs would apply only to B.
6749
6750 If B lists C in its public_deps instead of regular deps, A will now inherit
6751 C's public_configs and the ability to include C's public headers.
6752
6753 Generally if you are writing a target B and you include C's headers as part
6754 of B's public headers, or targets depending on B should consider B and C to
6755 be part of a unit, you should use public_deps instead of deps.
6756```
6757
6758#### **Example**
6759
6760```
6761 # This target can include files from "c" but not from
6762 # "super_secret_implementation_details".
6763 executable("a") {
6764 deps = [ ":b" ]
6765 }
6766
6767 shared_library("b") {
6768 deps = [ ":super_secret_implementation_details" ]
6769 public_deps = [ ":c" ]
6770 }
6771```
[email protected]449f3e42024-08-01 21:43:27 +08006772### **rebase**: Rebase collected metadata as files. [Back to Top](#gn-reference)
Nate Fischer8ed01d32019-01-08 17:32:01 -08006773
6774```
6775 A boolean that triggers a rebase of collected metadata strings based on their
6776 declared file. Defaults to false.
6777
6778 Metadata generally declares files as strings relative to the local build file.
6779 However, this data is often used in other contexts, and so setting this flag
6780 will force the metadata collection to be rebased according to the local build
6781 file's location and thus allow the filename to be used anywhere.
6782
6783 Setting this flag will raise an error if any target's specified metadata is
6784 not a string value.
6785
6786 See also "gn help generated_file".
6787```
[email protected]449f3e42024-08-01 21:43:27 +08006788### **response_file_contents**: Contents of a response file for actions. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006789
6790```
6791 Sometimes the arguments passed to a script can be too long for the system's
6792 command-line capabilities. This is especially the case on Windows where the
6793 maximum command-line length is less than 8K. A response file allows you to
6794 pass an unlimited amount of data to a script in a temporary file for an
6795 action or action_foreach target.
6796
6797 If the response_file_contents variable is defined and non-empty, the list
6798 will be treated as script args (including possibly substitution patterns)
6799 that will be written to a temporary file at build time. The name of the
6800 temporary file will be substituted for "{{response_file_name}}" in the script
6801 args.
6802
6803 The response file contents will always be quoted and escaped according to
6804 Unix shell rules. To parse the response file, the Python script should use
6805 "shlex.split(file_contents)".
6806```
6807
6808#### **Example**
6809
6810```
6811 action("process_lots_of_files") {
6812 script = "process.py",
6813 inputs = [ ... huge list of files ... ]
6814
6815 # Write all the inputs to a response file for the script. Also,
6816 # make the paths relative to the script working directory.
6817 response_file_contents = rebase_path(inputs, root_build_dir)
6818
6819 # The script expects the name of the response file in --file-list.
6820 args = [
6821 "--enable-foo",
6822 "--file-list={{response_file_name}}",
6823 ]
6824 }
6825```
[email protected]449f3e42024-08-01 21:43:27 +08006826### **rustflags**: Flags passed to the Rust compiler. [Back to Top](#gn-reference)
Max Ihlenfeldt5110a7f2024-01-22 11:56:47 +01006827
6828```
6829 A list of strings.
6830
6831 "rustflags" are passed to all invocations of the Rust compiler.
6832```
[email protected]449f3e42024-08-01 21:43:27 +08006833### **script**: Script file for actions. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006834
6835```
6836 An absolute or buildfile-relative file name of a Python script to run for a
6837 action and action_foreach targets (see "gn help action" and "gn help
6838 action_foreach").
6839```
[email protected]449f3e42024-08-01 21:43:27 +08006840### **sources**: Source files for a target [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006841
6842```
6843 A list of files. Non-absolute paths will be resolved relative to the current
6844 build file.
6845```
6846
6847#### **Sources for binary targets**
6848
6849```
6850 For binary targets (source sets, executables, and libraries), the known file
6851 types will be compiled with the associated tools. Unknown file types and
6852 headers will be skipped. However, you should still list all C/C+ header files
6853 so GN knows about the existence of those files for the purposes of include
6854 checking.
6855
6856 As a special case, a file ending in ".def" will be treated as a Windows
6857 module definition file. It will be appended to the link line with a
6858 preceding "/DEF:" string. There must be at most one .def file in a target
6859 and they do not cross dependency boundaries (so specifying a .def file in a
6860 static library or source set will have no effect on the executable or shared
6861 library they're linked into).
Julie Hockettce1fa072019-05-07 17:44:37 -07006862
6863 For Rust targets that do not specify a crate_root, then the crate_root will
6864 look for a lib.rs file (or main.rs for executable) or a single file in
6865 sources, if sources contains only one file.
Brett Wilson796ed472018-07-16 15:11:09 -07006866```
6867
6868#### **Sources for non-binary targets**
6869
6870```
6871 action_foreach
6872 The sources are the set of files that the script will be executed over. The
6873 script will run once per file.
6874
6875 action
6876 The sources will be treated the same as inputs. See "gn help inputs" for
6877 more information and usage advice.
6878
6879 copy
6880 The source are the source files to copy.
6881```
[email protected]449f3e42024-08-01 21:43:27 +08006882### **swiftflags**: Flags passed to the swift compiler. [Back to Top](#gn-reference)
Sylvain Defresne89e64252020-08-07 13:01:06 +02006883
6884```
6885 A list of strings.
6886
6887 "swiftflags" are passed to any invocation of a tool that takes an .swift
6888 file as input.
6889```
6890
6891#### **Ordering of flags and values**
6892
6893```
6894 1. Those set on the current target (not in a config).
6895 2. Those set on the "configs" on the target in order that the
6896 configs appear in the list.
6897 3. Those set on the "all_dependent_configs" on the target in order
6898 that the configs appear in the list.
6899 4. Those set on the "public_configs" on the target in order that
6900 those configs appear in the list.
6901 5. all_dependent_configs pulled from dependencies, in the order of
6902 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006903 more than once, only the first occurrence will be used.
Sylvain Defresne89e64252020-08-07 13:01:06 +02006904 6. public_configs pulled from dependencies, in the order of the
6905 "deps" list. If a dependency is public, they will be applied
6906 recursively.
6907```
Julie Jeongeun Kimebc8f162025-05-19 11:45:26 +09006908### **target_xcode_platform**: The desired platform for the build. [Back to Top](#gn-reference)
6909
6910```
6911 This value should be used to indicate the kind of iOS or iOS-based platform
6912 that is being the desired platform for the primary object(s) of the build.
6913
6914 This should be set to the most specific value possible. So, "iphoneos" or
6915 "tvos" should be used instead of "ios" where applicable, even though
6916 iPhoneOS and tvOS are both iOS variants.
6917
6918 GN defaults this value to "iphoneos" and the configuration files should set
6919 it to an appropriate value if it is not set via the command line or in the
6920 args.gn file.
6921
6922 This value configures the base SDK and the targeted device families of the
6923 generated Xcode project. only meaningful when generating with --ide=xcode.
6924
6925 Possible values
6926
6927 - "iphoneos"
6928 - "tvos"
6929```
[email protected]449f3e42024-08-01 21:43:27 +08006930### **testonly**: Declares a target must only be used for testing. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006931
6932```
6933 Boolean. Defaults to false.
6934
6935 When a target is marked "testonly = true", it must only be depended on by
6936 other test-only targets. Otherwise, GN will issue an error that the
6937 depenedency is not allowed.
6938
6939 This feature is intended to prevent accidentally shipping test code in a
6940 final product.
6941```
6942
6943#### **Example**
6944
6945```
6946 source_set("test_support") {
6947 testonly = true
6948 ...
6949 }
6950```
[email protected]449f3e42024-08-01 21:43:27 +08006951### **transparent**: [bool] True if the bundle is transparent. [Back to Top](#gn-reference)
Sylvain Defresnee5aee7b2023-06-08 10:08:10 -04006952
6953```
6954 A boolean.
6955
6956 Valid for "create_bundle" target. If true, the "create_bundle" target will
6957 not package the "bundle_data" deps but will forward them to all targets that
6958 depends on it (unless the "bundle_data" target sets "product_type" to the
6959 same value as the "create_bundle" target).
6960```
[email protected]449f3e42024-08-01 21:43:27 +08006961### **visibility**: A list of labels that can depend on a target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07006962
6963```
6964 A list of labels and label patterns that define which targets can depend on
6965 the current one. These permissions are checked via the "check" command (see
6966 "gn help check").
6967
6968 If visibility is not defined, it defaults to public ("*").
6969
6970 If visibility is defined, only the targets with labels that match it can
6971 depend on the current target. The empty list means no targets can depend on
6972 the current target.
6973
6974 Tip: Often you will want the same visibility for all targets in a BUILD file.
6975 In this case you can just put the definition at the top, outside of any
6976 target, and the targets will inherit that scope and see the definition.
6977```
6978
6979#### **Patterns**
6980
6981```
6982 See "gn help label_pattern" for more details on what types of patterns are
6983 supported. If a toolchain is specified, only targets in that toolchain will
6984 be matched. If a toolchain is not specified on a pattern, targets in all
6985 toolchains will be matched.
6986```
6987
6988#### **Examples**
6989
6990```
6991 Only targets in the current buildfile ("private"):
6992 visibility = [ ":*" ]
6993
6994 No targets (used for targets that should be leaf nodes):
6995 visibility = []
6996
6997 Any target ("public", the default):
6998 visibility = [ "*" ]
6999
7000 All targets in the current directory and any subdirectory:
7001 visibility = [ "./*" ]
7002
7003 Any target in "//bar/BUILD.gn":
7004 visibility = [ "//bar:*" ]
7005
7006 Any target in "//bar/" or any subdirectory thereof:
7007 visibility = [ "//bar/*" ]
7008
7009 Just these specific targets:
7010 visibility = [ ":mything", "//foo:something_else" ]
7011
7012 Any target in the current directory and any subdirectory thereof, plus
Petr Hosek7a6231e2022-10-22 23:14:18 +00007013 any targets in "//bar/" and any subdirectory thereof.
Brett Wilson796ed472018-07-16 15:11:09 -07007014 visibility = [ "./*", "//bar/*" ]
7015```
[email protected]449f3e42024-08-01 21:43:27 +08007016### **walk_keys**: Key(s) for managing the metadata collection walk. [Back to Top](#gn-reference)
Nate Fischer8ed01d32019-01-08 17:32:01 -08007017
7018```
Julie Hockett152c5142019-07-12 09:53:43 -06007019 Defaults to [""].
Nate Fischer8ed01d32019-01-08 17:32:01 -08007020
7021 These keys are used to control the next step in a collection walk, acting as
7022 barriers. If a specified key is defined in a target's metadata, the walk will
7023 use the targets listed in that value to determine which targets are walked.
7024
Julie Hockett152c5142019-07-12 09:53:43 -06007025 If no walk_keys are specified for a generated_file target (i.e. "[""]"), the
Nate Fischer8ed01d32019-01-08 17:32:01 -08007026 walk will touch all deps and data_deps of the specified target recursively.
7027
7028 See "gn help generated_file".
7029```
[email protected]449f3e42024-08-01 21:43:27 +08007030### **weak_frameworks**: [name list] Name of frameworks that must be weak linked. [Back to Top](#gn-reference)
Robert Sesekd0a6f072020-05-15 11:21:22 -04007031
7032```
7033 A list of framework names.
7034
7035 The frameworks named in that list will be weak linked with any dynamic link
7036 type target. Weak linking instructs the dynamic loader to attempt to load
7037 the framework, but if it is not able to do so, it leaves any imported symbols
7038 unresolved. This is typically used when a framework is present in a new
7039 version of an SDK but not on older versions of the OS that the software runs
7040 on.
7041```
7042
7043#### **Ordering of flags and values**
7044
7045```
7046 1. Those set on the current target (not in a config).
7047 2. Those set on the "configs" on the target in order that the
7048 configs appear in the list.
7049 3. Those set on the "all_dependent_configs" on the target in order
7050 that the configs appear in the list.
7051 4. Those set on the "public_configs" on the target in order that
7052 those configs appear in the list.
7053 5. all_dependent_configs pulled from dependencies, in the order of
7054 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01007055 more than once, only the first occurrence will be used.
Robert Sesekd0a6f072020-05-15 11:21:22 -04007056 6. public_configs pulled from dependencies, in the order of the
7057 "deps" list. If a dependency is public, they will be applied
7058 recursively.
7059```
7060
7061#### **Example**
7062
7063```
7064 weak_frameworks = [ "OnlyOnNewerOSes.framework" ]
7065```
[email protected]449f3e42024-08-01 21:43:27 +08007066### **write_runtime_deps**: Writes the target's runtime_deps to the given path. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007067
7068```
7069 Does not synchronously write the file, but rather schedules it to be written
7070 at the end of generation.
7071
7072 If the file exists and the contents are identical to that being written, the
7073 file will not be updated. This will prevent unnecessary rebuilds of targets
7074 that depend on this file.
7075
7076 Path must be within the output directory.
7077
7078 See "gn help runtime_deps" for how the runtime dependencies are computed.
7079
7080 The format of this file will list one file per line with no escaping. The
7081 files will be relative to the root_build_dir. The first line of the file will
7082 be the main output file of the target itself. The file contents will be the
7083 same as requesting the runtime deps be written on the command line (see "gn
7084 help --runtime-deps-list-file").
7085```
[email protected]449f3e42024-08-01 21:43:27 +08007086### **xcasset_compiler_flags**: Flags passed to xcassets compiler. [Back to Top](#gn-reference)
Harley Li0a9affb2020-06-03 10:38:42 -04007087
7088```
7089 A list of strings.
7090
7091 Valid for create_bundle target. Those flags are directly passed to
7092 xcassets compiler, corresponding to {{xcasset_compiler_flags}} substitution
7093 in compile_xcassets tool.
7094```
[email protected]449f3e42024-08-01 21:43:27 +08007095### **xcode_extra_attributes**: [scope] Extra attributes for Xcode projects. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007096
7097```
7098 The value defined in this scope will be copied to the EXTRA_ATTRIBUTES
7099 property of the generated Xcode project. They are only meaningful when
7100 generating with --ide=xcode.
7101
7102 See "gn help create_bundle" for more information.
7103```
[email protected]449f3e42024-08-01 21:43:27 +08007104### **xcode_test_application_name**: Name for Xcode test target. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007105
7106```
7107 Each unit and ui test target must have a test application target, and this
7108 value is used to specify the relationship. Only meaningful to Xcode (used as
7109 part of the Xcode project generation).
7110
7111 See "gn help create_bundle" for more information.
7112```
7113
Julie Hockette2a29402018-07-31 10:11:42 -07007114#### **Example**
Brett Wilson796ed472018-07-16 15:11:09 -07007115
7116```
7117 create_bundle("chrome_xctest") {
7118 test_application_name = "chrome"
7119 ...
7120 }
7121```
7122## Other help topics
7123
[email protected]449f3e42024-08-01 21:43:27 +08007124### **Build Arguments Overview** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007125
7126```
7127 Build arguments are variables passed in from outside of the build that build
7128 files can query to determine how the build works.
7129```
7130
7131#### **How build arguments are set**
7132
7133```
7134 First, system default arguments are set based on the current system. The
7135 built-in arguments are:
Dirk Pranke7f109842025-04-09 16:48:38 -07007136 - gn_version
Brett Wilson796ed472018-07-16 15:11:09 -07007137 - host_cpu
7138 - host_os
7139 - current_cpu
7140 - current_os
7141 - target_cpu
7142 - target_os
7143
7144 Next, project-specific overrides are applied. These are specified inside
Dirk Pranke7f109842025-04-09 16:48:38 -07007145 the default_args variable of //.gn. See "gn help dotfile" for more. Note
7146 that during processing of the dotfile itself, only `gn_version` is defined.
Brett Wilson796ed472018-07-16 15:11:09 -07007147
7148 If specified, arguments from the --args command line flag are used. If that
7149 flag is not specified, args from previous builds in the build directory will
7150 be used (this is in the file args.gn in the build directory).
7151
7152 Last, for targets being compiled with a non-default toolchain, the toolchain
7153 overrides are applied. These are specified in the toolchain_args section of a
7154 toolchain definition. The use-case for this is that a toolchain may be
7155 building code for a different platform, and that it may want to always
7156 specify Posix, for example. See "gn help toolchain" for more.
7157
7158 If you specify an override for a build argument that never appears in a
7159 "declare_args" call, a nonfatal error will be displayed.
7160```
7161
7162#### **Examples**
7163
7164```
7165 gn args out/FooBar
7166 Create the directory out/FooBar and open an editor. You would type
7167 something like this into that file:
7168 enable_doom_melon=false
7169 os="android"
7170
7171 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
7172 This will overwrite the build directory with the given arguments. (Note
7173 that the quotes inside the args command will usually need to be escaped
7174 for your shell to pass through strings values.)
7175```
7176
7177#### **How build arguments are used**
7178
7179```
7180 If you want to use an argument, you use declare_args() and specify default
7181 values. These default values will apply if none of the steps listed in the
7182 "How build arguments are set" section above apply to the given argument, but
7183 the defaults will not override any of these.
7184
7185 Often, the root build config file will declare global arguments that will be
7186 passed to all buildfiles. Individual build files can also specify arguments
7187 that apply only to those files. It is also useful to specify build args in an
7188 "import"-ed file if you want such arguments to apply to multiple buildfiles.
7189```
[email protected]449f3e42024-08-01 21:43:27 +08007190### **.gn file** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007191
7192```
7193 When gn starts, it will search the current directory and parent directories
7194 for a file called ".gn". This indicates the source root. You can override
7195 this detection by using the --root command-line argument
7196
7197 The .gn file in the source root will be executed. The syntax is the same as a
7198 buildfile, but with very limited build setup-specific meaning.
7199
7200 If you specify --root, by default GN will look for the file .gn in that
7201 directory. If you want to specify a different file, you can additionally pass
7202 --dotfile:
7203
7204 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
Dirk Pranke7f109842025-04-09 16:48:38 -07007205
7206 The system variable `gn_version` is available in the dotfile, but none of
7207 the other variables are, because the dotfile is processed before args.gn
7208 or anything else is processed.
Brett Wilson796ed472018-07-16 15:11:09 -07007209```
7210
7211#### **Variables**
7212
7213```
7214 arg_file_template [optional]
7215 Path to a file containing the text that should be used as the default
7216 args.gn content when you run `gn args`.
7217
7218 buildconfig [required]
7219 Path to the build config file. This file will be used to set up the
7220 build file execution environment for each toolchain.
7221
7222 check_targets [optional]
7223 A list of labels and label patterns that should be checked when running
Erik Staab5d109d72020-06-29 22:53:23 -07007224 "gn check" or "gn gen --check". If neither check_targets or
7225 no_check_targets (see below) is specified, all targets will be checked.
7226 It is an error to specify both check_targets and no_check_targets. If it
7227 is the empty list, no targets will be checked. To bypass this list,
7228 request an explicit check of targets, like "//*".
7229
7230 The format of this list is identical to that of "visibility" so see "gn
7231 help visibility" for examples.
7232
7233 no_check_targets [optional]
7234 A list of labels and label patterns that should *not* be checked when
7235 running "gn check" or "gn gen --check". All other targets will be checked.
7236 If neither check_targets (see above) or no_check_targets is specified, all
7237 targets will be checked. It is an error to specify both check_targets and
7238 no_check_targets.
Brett Wilson796ed472018-07-16 15:11:09 -07007239
7240 The format of this list is identical to that of "visibility" so see "gn
7241 help visibility" for examples.
7242
James Robinson49f59032020-01-08 14:05:51 -08007243 check_system_includes [optional]
7244 Boolean to control whether system style includes are checked by default
7245 when running "gn check" or "gn gen --check". System style includes are
7246 includes that use angle brackets <> instead of double quotes "". If this
7247 setting is omitted or set to false, these includes will be ignored by
7248 default. They can be checked explicitly by running
7249 "gn check --check-system" or "gn gen --check=system"
7250
Dirk Prankeed1abc12025-01-10 17:41:03 -08007251 exec_script_allowlist [optional]
Brett Wilson796ed472018-07-16 15:11:09 -07007252 A list of .gn/.gni files (not labels) that have permission to call the
7253 exec_script function. If this list is defined, calls to exec_script will
7254 be checked against this list and GN will fail if the current file isn't
7255 in the list.
7256
7257 This is to allow the use of exec_script to be restricted since is easy to
7258 use inappropriately. Wildcards are not supported. Files in the
7259 secondary_source tree (if defined) should be referenced by ignoring the
7260 secondary tree and naming them as if they are in the main tree.
7261
7262 If unspecified, the ability to call exec_script is unrestricted.
7263
7264 Example:
Dirk Prankeed1abc12025-01-10 17:41:03 -08007265 exec_script_allowlist = [
Brett Wilson796ed472018-07-16 15:11:09 -07007266 "//base/BUILD.gn",
7267 "//build/my_config.gni",
7268 ]
7269
Dirk Prankeed1abc12025-01-10 17:41:03 -08007270 exec_script_whitelist [optional]
7271 A synonym for "exec_script_allowlist" that exists for backwards
7272 compatibility. New code should use "exec_script_allowlist" instead.
7273 If both values are set, only the value in "exec_script_allowlist" will
7274 have any effect (so don't set both!).
7275
Brett Wilson1da84bb2022-09-14 15:35:29 -07007276 export_compile_commands [optional]
7277 A list of label patterns for which to generate a Clang compilation
7278 database (see "gn help label_pattern" for the string format).
7279
7280 When specified, GN will generate a compile_commands.json file in the root
7281 of the build directory containing information on how to compile each
7282 source file reachable from any label matching any pattern in the list.
7283 This is used for Clang-based tooling and some editor integration. See
7284 https://clang.llvm.org/docs/JSONCompilationDatabase.html
7285
7286 The switch --add-export-compile-commands to "gn gen" (see "gn help gen")
7287 appends to this value which provides a per-user way to customize it.
7288
7289 The deprecated switch --export-compile-commands to "gn gen" (see "gn help
7290 gen") adds to the export target list using a different format.
7291
7292 Example:
7293 export_compile_commands = [
7294 "//base/*",
7295 "//tools:doom_melon",
7296 ]
7297
Brett Wilson796ed472018-07-16 15:11:09 -07007298 root [optional]
7299 Label of the root build target. The GN build will start by loading the
7300 build file containing this target name. This defaults to "//:" which will
Harley Li0a9affb2020-06-03 10:38:42 -04007301 cause the file //BUILD.gn to be loaded. Note that build_file_extension
7302 applies to the default case as well.
Brett Wilson796ed472018-07-16 15:11:09 -07007303
Nico Weberad9eab22020-11-15 22:20:28 -05007304 The command-line switch --root-target will override this value (see "gn
7305 help --root-target").
7306
David 'Digit' Turnerb8562a42023-10-23 18:05:37 +02007307 root_patterns [optional]
7308 A list of label pattern strings. When not defined or empty, the GN build
7309 graph will contain all targets from any BUILD.gn evaluated in the default
7310 toolchain context, and their transitive dependencies.
7311
7312 When set to a non empty list, only the targets in the default toolchain
7313 matching these patterns, and their transitive dependencies, will be defined
7314 instead.
7315
7316 The command-line switch --root-pattern will override this value (see
7317 "gn help --root-pattern")
7318
Brett Wilson796ed472018-07-16 15:11:09 -07007319 script_executable [optional]
Brett Wilsond0f8bc92022-04-22 14:01:40 -07007320 By default, GN runs the scripts used in action targets and exec_script
7321 calls using the Python interpreter found in PATH. This value specifies the
7322 Python executable or other interpreter to use instead.
James Robinson61377e32020-02-13 15:20:07 -08007323
Brett Wilsond0f8bc92022-04-22 14:01:40 -07007324 If set to the empty string, the scripts will be executed directly.
7325
7326 The command-line switch --script-executable will override this value (see
7327 "gn help --script-executable")
Brett Wilson796ed472018-07-16 15:11:09 -07007328
7329 secondary_source [optional]
7330 Label of an alternate directory tree to find input files. When searching
7331 for a BUILD.gn file (or the build config file discussed above), the file
7332 will first be looked for in the source root. If it's not found, the
7333 secondary source root will be checked (which would contain a parallel
7334 directory hierarchy).
7335
7336 This behavior is intended to be used when BUILD.gn files can't be checked
7337 in to certain source directories for whatever reason.
7338
7339 The secondary source root must be inside the main source tree.
7340
7341 default_args [optional]
7342 Scope containing the default overrides for declared arguments. These
7343 overrides take precedence over the default values specified in the
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01007344 declare_args() block, but can be overridden using --args or the
Brett Wilson796ed472018-07-16 15:11:09 -07007345 args.gn file.
7346
7347 This is intended to be used when subprojects declare arguments with
7348 default values that need to be changed for whatever reason.
Harley Li0a9affb2020-06-03 10:38:42 -04007349
7350 build_file_extension [optional]
7351 If set to a non-empty string, this is added to the name of all build files
7352 to load.
7353 GN will look for build files named "BUILD.$build_file_extension.gn".
7354 This is intended to be used during migrations or other situations where
7355 there are two independent GN builds in the same directories.
Petr Hosek317fdc12020-05-28 10:23:42 -07007356
7357 ninja_required_version [optional]
7358 When set specifies the minimum required version of Ninja. The default
7359 required version is 1.7.2. Specifying a higher version might enable the
7360 use of some of newer features that can make the build more efficient.
Takuto Ikuta60a28b62024-09-05 17:45:42 +09007361
7362 no_stamp_files [optional]
7363 A boolean flag that can be set to generate Ninja files that use phony
7364 rules instead of stamp files whenever possible. This results in smaller
7365 Ninja build plans, but requires at least Ninja 1.11.
Brett Wilson796ed472018-07-16 15:11:09 -07007366```
7367
7368#### **Example .gn file contents**
7369
7370```
7371 buildconfig = "//build/config/BUILDCONFIG.gn"
7372
7373 check_targets = [
7374 "//doom_melon/*", # Check everything in this subtree.
7375 "//tools:mind_controlling_ant", # Check this specific target.
7376 ]
7377
7378 root = "//:root"
7379
7380 secondary_source = "//build/config/temporary_buildfiles/"
7381
7382 default_args = {
7383 # Default to release builds for this project.
7384 is_debug = false
7385 is_component_build = false
7386 }
7387```
[email protected]449f3e42024-08-01 21:43:27 +08007388### **Build graph and execution overview** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007389
7390#### **Overall build flow**
7391
7392```
7393 1. Look for ".gn" file (see "gn help dotfile") in the current directory and
7394 walk up the directory tree until one is found. Set this directory to be
7395 the "source root" and interpret this file to find the name of the build
7396 config file.
7397
7398 2. Execute the build config file identified by .gn to set up the global
7399 variables and default toolchain name. Any arguments, variables, defaults,
7400 etc. set up in this file will be visible to all files in the build.
Dirk Pranke12a6f482025-04-09 16:52:34 -07007401 Any values set in the `default_args` scope will be merged into
7402 subsequent `declare_args()` scopes and override the default values.
Brett Wilson796ed472018-07-16 15:11:09 -07007403
Dirk Pranke12a6f482025-04-09 16:52:34 -07007404 3. Process the --args command line option or load the arguments from
7405 the args.gn file in the build directory. These values will be merged
7406 into any subsequent declare_args() scope (after the `default_args`
7407 are merged in) to override the default values. See `help buildargs`
7408 for more on how args are handled.
7409
7410 4. Load the BUILDCONFIG.gn file and create a dedicated scope for it.
7411
7412 5. Load the //BUILD.gn (in the source root directory). The BUILD.gn
Dirk Prankec0a46c52025-04-08 18:09:33 -07007413 file is executed in a scope whose parent scope is the BUILDCONFIG.gn
7414 file, i.e., only the definitions in the BUILDCONFIG.gn file exist.
Brett Wilson796ed472018-07-16 15:11:09 -07007415
Dirk Pranke12a6f482025-04-09 16:52:34 -07007416 5. If the BUILD.gn file imports other files, each of those other
Dirk Prankec0a46c52025-04-08 18:09:33 -07007417 files is executed in a separate scope whose parent is the BUILDCONFIG.gn
7418 file, i.e., no definitions from the importing BUILD.gn file are
7419 available. When the imported file has been fully processed, its scope
7420 is merged into the BUILD.gn file's scope. If there is a conflict
7421 (both the BUILD.gn file and the imported file define some variable
7422 or rule with the same name but different values), a runtime error
7423 will be thrown. See "gn help import" for more on this.
7424
Dirk Pranke12a6f482025-04-09 16:52:34 -07007425 6. Recursively evaluate rules and load BUILD.gn in other directories as
Brett Wilson796ed472018-07-16 15:11:09 -07007426 necessary to resolve dependencies. If a BUILD file isn't found in the
7427 specified location, GN will look in the corresponding location inside
7428 the secondary_source defined in the dotfile (see "gn help dotfile").
Dirk Pranke6d326e92025-04-09 12:49:26 -07007429 Each BUILD.gn file will again be executed in a new scope whose only
7430 parent is BUILDCONFIG.gn's scope.
Brett Wilson796ed472018-07-16 15:11:09 -07007431
Dirk Pranke12a6f482025-04-09 16:52:34 -07007432 7. If a target is referenced using an alternate toolchain, then
7433
7434 1. The toolchain file is loaded in a scope whose parent is the
7435 BUILDCONFIG.gn file.
7436 2. The BUILDCONFIG.gn file is re-loaded and re-parsed into a new
7437 scope, with any `toolchain_args` merged into the defaults. See
7438 `help buildargs` for more on how args are handled.
7439 3. The BUILD.gn containing the target is then parsed as in step 5,
7440 only we use the scope from step 7.2 instead of the default
7441 BUILDCONFIG.gn scope.
7442
7443 8. When a target's dependencies are resolved, write out the `.ninja`
Brett Wilson796ed472018-07-16 15:11:09 -07007444 file to disk.
7445
Dirk Pranke12a6f482025-04-09 16:52:34 -07007446 9. When all targets are resolved, write out the root build.ninja file.
Harley Li0a9affb2020-06-03 10:38:42 -04007447
7448 Note that the BUILD.gn file name may be modulated by .gn arguments such as
7449 build_file_extension.
Brett Wilson796ed472018-07-16 15:11:09 -07007450```
7451
7452#### **Executing target definitions and templates**
7453
7454```
7455 Build files are loaded in parallel. This means it is impossible to
7456 interrogate a target from GN code for any information not derivable from its
7457 label (see "gn help label"). The exception is the get_target_outputs()
7458 function which requires the target being interrogated to have been defined
7459 previously in the same file.
7460
7461 Targets are declared by their type and given a name:
7462
7463 static_library("my_static_library") {
7464 ... target parameter definitions ...
7465 }
7466
7467 There is also a generic "target" function for programmatically defined types
Dirk Prankec0a46c52025-04-08 18:09:33 -07007468 (see "gn help target").
7469
7470 You can define new types using templates (see "gn help template"). A template
7471 defines some custom code that expands to one or more other targets. When a
7472 template is invoked, it is executed in the scope of the file that defined the
7473 template (as described above). To access values from the caller's scope, you
7474 must use the `invoker` variable (see "gn help template" for more on the
7475 invoker).
Brett Wilson796ed472018-07-16 15:11:09 -07007476
7477 Before executing the code inside the target's { }, the target defaults are
7478 applied (see "gn help set_defaults"). It will inject implicit variable
7479 definitions that can be overridden by the target code as necessary. Typically
7480 this mechanism is used to inject a default set of configs that define the
7481 global compiler and linker flags.
7482```
7483
7484#### **Which targets are built**
7485
7486```
7487 All targets encountered in the default toolchain (see "gn help toolchain")
7488 will have build rules generated for them, even if no other targets reference
7489 them. Their dependencies must resolve and they will be added to the implicit
7490 "all" rule (see "gn help ninja_rules").
7491
7492 Targets in non-default toolchains will only be generated when they are
7493 required (directly or transitively) to build a target in the default
7494 toolchain.
7495
Tyler Mandry4a648092022-02-15 19:47:09 +00007496 Some targets might be associated but without a formal build dependency (for
7497 example, related tools or optional variants). A target that is marked as
7498 "generated" can propagate its generated state to an associated target using
7499 "gen_deps". This will make the referenced dependency have Ninja rules
7500 generated in the same cases the source target has but without a build-time
7501 dependency and even in non-default toolchains.
7502
Brett Wilson796ed472018-07-16 15:11:09 -07007503 See also "gn help ninja_rules".
7504```
7505
7506#### **Dependencies**
7507
7508```
7509 The only difference between "public_deps" and "deps" except for pushing
7510 configs around the build tree and allowing includes for the purposes of "gn
7511 check".
7512
7513 A target's "data_deps" are guaranteed to be built whenever the target is
7514 built, but the ordering is not defined. The meaning of this is dependencies
7515 required at runtime. Currently data deps will be complete before the target
7516 is linked, but this is not semantically guaranteed and this is undesirable
7517 from a build performance perspective. Since we hope to change this in the
7518 future, do not rely on this behavior.
7519```
[email protected]449f3e42024-08-01 21:43:27 +08007520### **Language and grammar for GN build files** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007521
7522#### **Tokens**
7523
7524```
7525 GN build files are read as sequences of tokens. While splitting the file
7526 into tokens, the next token is the longest sequence of characters that form a
7527 valid token.
7528```
7529
7530#### **White space and comments**
7531
7532```
7533 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
7534 carriage returns (U+000D), and newlines (U+000A).
7535
7536 Comments start at the character "#" and stop at the next newline.
7537
7538 White space and comments are ignored except that they may separate tokens
7539 that would otherwise combine into a single token.
7540```
7541
7542#### **Identifiers**
7543
7544```
7545 Identifiers name variables and functions.
7546
7547 identifier = letter { letter | digit } .
7548 letter = "A" ... "Z" | "a" ... "z" | "_" .
7549 digit = "0" ... "9" .
7550```
7551
7552#### **Keywords**
7553
7554```
7555 The following keywords are reserved and may not be used as identifiers:
7556
7557 else false if true
7558```
7559
7560#### **Integer literals**
7561
7562```
7563 An integer literal represents a decimal integer value.
7564
7565 integer = [ "-" ] digit { digit } .
7566
7567 Leading zeros and negative zero are disallowed.
7568```
7569
7570#### **String literals**
7571
7572```
7573 A string literal represents a string value consisting of the quoted
7574 characters with possible escape sequences and variable expansions.
7575
7576 string = `"` { char | escape | expansion } `"` .
7577 escape = `\` ( "$" | `"` | char ) .
Nico Weberad9eab22020-11-15 22:20:28 -05007578 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" .
Brett Wilson796ed472018-07-16 15:11:09 -07007579 Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f]
7580 expansion = "$" ( identifier | BracketExpansion | Hex ) .
Nico Weberad9eab22020-11-15 22:20:28 -05007581 char = /* any character except "$", `"`, or newline */ .
Brett Wilson796ed472018-07-16 15:11:09 -07007582
7583 After a backslash, certain sequences represent special characters:
7584
7585 \" U+0022 quotation mark
7586 \$ U+0024 dollar sign
7587 \\ U+005C backslash
7588
7589 All other backslashes represent themselves.
7590
7591 To insert an arbitrary byte value, use $0xFF. For example, to insert a
7592 newline character: "Line one$0x0ALine two".
7593
7594 An expansion will evaluate the variable following the '$' and insert a
7595 stringified version of it into the result. For example, to concat two path
7596 components with a slash separating them:
7597 "$var_one/$var_two"
7598 Use the "${var_one}" format to be explicitly deliniate the variable for
7599 otherwise-ambiguous cases.
7600```
7601
7602#### **Punctuation**
7603
7604```
7605 The following character sequences represent punctuation:
7606
7607 + += == != ( )
7608 - -= < <= [ ]
7609 ! = > >= { }
7610 && || . ,
7611```
7612
7613#### **Grammar**
7614
7615```
7616 The input tokens form a syntax tree following a context-free grammar:
7617
7618 File = StatementList .
7619
7620 Statement = Assignment | Call | Condition .
7621 LValue = identifier | ArrayAccess | ScopeAccess .
7622 Assignment = LValue AssignOp Expr .
7623 Call = identifier "(" [ ExprList ] ")" [ Block ] .
7624 Condition = "if" "(" Expr ")" Block
7625 [ "else" ( Condition | Block ) ] .
7626 Block = "{" StatementList "}" .
7627 StatementList = { Statement } .
7628
7629 ArrayAccess = identifier "[" Expr "]" .
7630 ScopeAccess = identifier "." identifier .
7631 Expr = UnaryExpr | Expr BinaryOp Expr .
7632 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
7633 PrimaryExpr = identifier | integer | string | Call
7634 | ArrayAccess | ScopeAccess | Block
7635 | "(" Expr ")"
7636 | "[" [ ExprList [ "," ] ] "]" .
7637 ExprList = Expr { "," Expr } .
7638
7639 AssignOp = "=" | "+=" | "-=" .
7640 UnaryOp = "!" .
7641 BinaryOp = "+" | "-" // highest priority
7642 | "<" | "<=" | ">" | ">="
7643 | "==" | "!="
7644 | "&&"
7645 | "||" . // lowest priority
7646
7647 All binary operators are left-associative.
7648```
7649
7650#### **Types**
7651
7652```
7653 The GN language is dynamically typed. The following types are used:
7654
7655 - Boolean: Uses the keywords "true" and "false". There is no implicit
7656 conversion between booleans and integers.
7657
7658 - Integers: All numbers in GN are signed 64-bit integers.
7659
7660 - Strings: Strings are 8-bit with no enforced encoding. When a string is
7661 used to interact with other systems with particular encodings (like the
7662 Windows and Mac filesystems) it is assumed to be UTF-8. See "String
7663 literals" above for more.
7664
7665 - Lists: Lists are arbitrary-length ordered lists of values. See "Lists"
7666 below for more.
7667
7668 - Scopes: Scopes are like dictionaries that use variable names for keys. See
7669 "Scopes" below for more.
7670```
7671
7672#### **Lists**
7673
7674```
7675 Lists are created with [] and using commas to separate items:
7676
7677 mylist = [ 0, 1, 2, "some string" ]
7678
7679 A comma after the last item is optional. Lists are dereferenced using 0-based
7680 indexing:
7681
7682 mylist[0] += 1
7683 var = mylist[2]
7684
7685 Lists can be concatenated using the '+' and '+=' operators. Bare values can
7686 not be concatenated with lists, to add a single item, it must be put into a
7687 list of length one.
7688
7689 Items can be removed from lists using the '-' and '-=' operators. This will
7690 remove all occurrences of every item in the right-hand list from the
7691 left-hand list. It is an error to remove an item not in the list. This is to
7692 prevent common typos and to detect dead code that is removing things that no
7693 longer apply.
7694
7695 It is an error to use '=' to replace a nonempty list with another nonempty
7696 list. This is to prevent accidentally overwriting data when in most cases
7697 '+=' was intended. To overwrite a list on purpose, first assign it to the
7698 empty list:
7699
7700 mylist = []
7701 mylist = otherlist
Brett Wilson796ed472018-07-16 15:11:09 -07007702```
7703
7704#### **Scopes**
7705
7706```
7707 All execution happens in the context of a scope which holds the current state
7708 (like variables). With the exception of loops and conditions, '{' introduces
Tyler Mandry4a648092022-02-15 19:47:09 +00007709 a new scope.
Brett Wilson796ed472018-07-16 15:11:09 -07007710
Tyler Mandry4a648092022-02-15 19:47:09 +00007711 Most scopes have a parent reference to the old scope. Variable reads
7712 recursively search all parent scopes until the variable is found or there are
7713 no more scopes. Variable writes always go into the current scope. This means
7714 that after the closing '}' (again excepting loops and conditions), all local
7715 variables will be restored to the previous values. This also means that "foo
7716 = foo" can do useful work by copying a variable into the current scope that
7717 was defined in a containing scope.
Brett Wilson796ed472018-07-16 15:11:09 -07007718
Tyler Mandry4a648092022-02-15 19:47:09 +00007719 Scopes can be assigned to variables. Examples of such scopes are the
7720 implicitly-created "invoker" when invoking a template (which refers to the
7721 variables set by the invoking code), scopes created by functions like
7722 exec_script, and scopes explicitly created like
Brett Wilson796ed472018-07-16 15:11:09 -07007723
7724 empty_scope = {}
7725 myvalues = {
7726 foo = 21
7727 bar = "something"
7728 }
7729
Tyler Mandry4a648092022-02-15 19:47:09 +00007730 In the case of explicitly created scopes and scopes created by functions like
7731 exec_script, there is no reference to the parent scope. Such scopes are fully
7732 self-contained and do not "inherit" values from their defining scope.
7733
7734 Inside an explicit scope definition can be any GN code including conditionals
7735 and function calls. After the close of the scope, it will contain all
7736 variables explicitly set by the code contained inside it. After this, the
7737 values can be read, modified, or added to:
Brett Wilson796ed472018-07-16 15:11:09 -07007738
7739 myvalues.foo += 2
7740 empty_scope.new_thing = [ 1, 2, 3 ]
Nate Fischer8ed01d32019-01-08 17:32:01 -08007741
7742 Scope equality is defined as single-level scopes identical within the current
7743 scope. That is, all values in the first scope must be present and identical
7744 within the second, and vice versa. Note that this means inherited scopes are
7745 always unequal by definition.
Brett Wilson796ed472018-07-16 15:11:09 -07007746```
[email protected]449f3e42024-08-01 21:43:27 +08007747### **Input and output conversion** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007748
7749```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007750 Input and output conversions are arguments to file and process functions
7751 that specify how to convert data to or from external formats. The possible
7752 values for parameters specifying conversions are:
7753
Brett Wilson796ed472018-07-16 15:11:09 -07007754 "" (the default)
Julie Hockett09171292018-07-31 14:35:10 -07007755 input: Discard the result and return None.
7756
7757 output: If value is a list, then "list lines"; otherwise "value".
Brett Wilson796ed472018-07-16 15:11:09 -07007758
7759 "list lines"
Julie Hockett09171292018-07-31 14:35:10 -07007760 input:
7761 Return the file contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007762 newlines will not be present in the result. The last line may or may
7763 not end in a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007764
Julie Hockett09171292018-07-31 14:35:10 -07007765 After splitting, each individual line will be trimmed of whitespace on
7766 both ends.
7767
7768 output:
7769 Renders the value contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007770 newlines will not be present in the result. The last line will end in
7771 with a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007772
7773 "scope"
Julie Hockett09171292018-07-31 14:35:10 -07007774 input:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007775 Execute the block as GN code and return a scope with the resulting
7776 values in it. If the input was:
Julie Hockett09171292018-07-31 14:35:10 -07007777 a = [ "hello.cc", "world.cc" ]
7778 b = 26
7779 and you read the result into a variable named "val", then you could
7780 access contents the "." operator on "val":
7781 sources = val.a
7782 some_count = val.b
7783
7784 output:
7785 Renders the value contents as a GN code block, reversing the input
7786 result above.
Brett Wilson796ed472018-07-16 15:11:09 -07007787
7788 "string"
Julie Hockett09171292018-07-31 14:35:10 -07007789 input: Return the file contents into a single string.
7790
7791 output:
7792 Render the value contents into a single string. The output is:
7793 a string renders with quotes, e.g. "str"
7794 an integer renders as a stringified integer, e.g. "6"
7795 a boolean renders as the associated string, e.g. "true"
7796 a list renders as a representation of its contents, e.g. "[\"str\", 6]"
7797 a scope renders as a GN code block of its values. If the Value was:
7798 Value val;
7799 val.a = [ "hello.cc", "world.cc" ];
7800 val.b = 26
7801 the resulting output would be:
7802 "{
7803 a = [ \"hello.cc\", \"world.cc\" ]
7804 b = 26
7805 }"
Brett Wilson796ed472018-07-16 15:11:09 -07007806
7807 "value"
Julie Hockett09171292018-07-31 14:35:10 -07007808 input:
7809 Parse the input as if it was a literal rvalue in a buildfile. Examples of
7810 typical program output using this mode:
7811 [ "foo", "bar" ] (result will be a list)
7812 or
7813 "foo bar" (result will be a string)
7814 or
7815 5 (result will be an integer)
Brett Wilson796ed472018-07-16 15:11:09 -07007816
Julie Hockett09171292018-07-31 14:35:10 -07007817 Note that if the input is empty, the result will be a null value which
7818 will produce an error if assigned to a variable.
7819
7820 output:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007821 Render the value contents as a literal rvalue. Strings render with
7822 escaped quotes.
Brett Wilson796ed472018-07-16 15:11:09 -07007823
7824 "json"
Julie Hockett09171292018-07-31 14:35:10 -07007825 input: Parse the input as a JSON and convert it to equivalent GN rvalue.
7826
7827 output: Convert the Value to equivalent JSON value.
7828
7829 The data type mapping is:
Brett Wilson796ed472018-07-16 15:11:09 -07007830 a string in JSON maps to string in GN
7831 an integer in JSON maps to integer in GN
7832 a float in JSON is unsupported and will result in an error
7833 an object in JSON maps to scope in GN
7834 an array in JSON maps to list in GN
7835 a boolean in JSON maps to boolean in GN
7836 a null in JSON is unsupported and will result in an error
7837
Julie Hockett09171292018-07-31 14:35:10 -07007838 Nota that the input dictionary keys have to be valid GN identifiers
7839 otherwise they will produce an error.
Brett Wilson796ed472018-07-16 15:11:09 -07007840
Julie Hockett09171292018-07-31 14:35:10 -07007841 "trim ..." (input only)
Brett Wilson796ed472018-07-16 15:11:09 -07007842 Prefixing any of the other transformations with the word "trim" will
7843 result in whitespace being trimmed from the beginning and end of the
7844 result before processing.
7845
7846 Examples: "trim string" or "trim list lines"
7847
7848 Note that "trim value" is useless because the value parser skips
7849 whitespace anyway.
7850```
[email protected]449f3e42024-08-01 21:43:27 +08007851### **File patterns** [Back to Top](#gn-reference)
Sylvain Defresneaff489a2020-03-11 18:27:43 +01007852
7853```
7854 File patterns are VERY limited regular expressions. They must match the
7855 entire input string to be counted as a match. In regular expression parlance,
7856 there is an implicit "^...$" surrounding your input. If you want to match a
7857 substring, you need to use wildcards at the beginning and end.
7858
7859 There are only two special tokens understood by the pattern matcher.
7860 Everything else is a literal.
7861
7862 - "*" Matches zero or more of any character. It does not depend on the
7863 preceding character (in regular expression parlance it is equivalent to
7864 ".*").
7865
7866 - "\b" Matches a path boundary. This will match the beginning or end of a
7867 string, or a slash.
7868```
7869
7870#### **Pattern examples**
7871
7872```
7873 "*asdf*"
7874 Matches a string containing "asdf" anywhere.
7875
7876 "asdf"
7877 Matches only the exact string "asdf".
7878
7879 "*.cc"
7880 Matches strings ending in the literal ".cc".
7881
7882 "\bwin/*"
7883 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
7884```
[email protected]449f3e42024-08-01 21:43:27 +08007885### **Label patterns** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007886
7887```
7888 A label pattern is a way of expressing one or more labels in a portion of the
7889 source tree. They are not general regular expressions.
7890
7891 They can take the following forms only:
7892
7893 - Explicit (no wildcard):
7894 "//foo/bar:baz"
7895 ":baz"
7896
7897 - Wildcard target names:
7898 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
7899 ":*" (all targets in the current build file)
7900
7901 - Wildcard directory names ("*" is only supported at the end)
7902 "*" (all targets)
7903 "//foo/bar/*" (all targets in any subdir of //foo/bar)
7904 "./*" (all targets in the current build file or sub dirs)
7905
Julie Hockette2a29402018-07-31 10:11:42 -07007906 Any of the above forms can additionally take an explicit toolchain
7907 in parenthesis at the end of the label pattern. In this case, the
7908 toolchain must be fully qualified (no wildcards are supported in the
7909 toolchain name).
Brett Wilson796ed472018-07-16 15:11:09 -07007910
Julie Hockette2a29402018-07-31 10:11:42 -07007911 "//foo:bar(//build/toolchain:mac)"
Brett Wilson796ed472018-07-16 15:11:09 -07007912 An explicit target in an explicit toolchain.
7913
7914 ":*(//build/toolchain/linux:32bit)"
7915 All targets in the current build file using the 32-bit Linux toolchain.
7916
7917 "//foo/*(//build/toolchain:win)"
7918 All targets in //foo and any subdirectory using the Windows
7919 toolchain.
7920```
[email protected]449f3e42024-08-01 21:43:27 +08007921### **About labels** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07007922
7923```
7924 Everything that can participate in the dependency graph (targets, configs,
7925 and toolchains) are identified by labels. A common label looks like:
7926
7927 //base/test:test_support
7928
7929 This consists of a source-root-absolute path, a colon, and a name. This means
7930 to look for the thing named "test_support" in "base/test/BUILD.gn".
7931
7932 You can also specify system absolute paths if necessary. Typically such
7933 paths would be specified via a build arg so the developer can specify where
7934 the component is on their system.
7935
7936 /usr/local/foo:bar (Posix)
7937 /C:/Program Files/MyLibs:bar (Windows)
7938```
7939
7940#### **Toolchains**
7941
7942```
7943 A canonical label includes the label of the toolchain being used. Normally,
7944 the toolchain label is implicitly inherited from the current execution
7945 context, but you can override this to specify cross-toolchain dependencies:
7946
7947 //base/test:test_support(//build/toolchain/win:msvc)
7948
7949 Here GN will look for the toolchain definition called "msvc" in the file
7950 "//build/toolchain/win" to know how to compile this target.
7951```
7952
7953#### **Relative labels**
7954
7955```
7956 If you want to refer to something in the same buildfile, you can omit
7957 the path name and just start with a colon. This format is recommended for
7958 all same-file references.
7959
7960 :base
7961
7962 Labels can be specified as being relative to the current directory.
7963 Stylistically, we prefer to use absolute paths for all non-file-local
7964 references unless a build file needs to be run in different contexts (like a
7965 project needs to be both standalone and pulled into other projects in
7966 difference places in the directory hierarchy).
7967
7968 source/plugin:myplugin
7969 ../net:url_request
7970```
7971
7972#### **Implicit names**
7973
7974```
7975 If a name is unspecified, it will inherit the directory name. Stylistically,
7976 we prefer to omit the colon and name when possible:
7977
7978 //net -> //net:net
7979 //tools/gn -> //tools/gn:gn
7980```
[email protected]449f3e42024-08-01 21:43:27 +08007981### **Metadata Collection** [Back to Top](#gn-reference)
Julie Hockett152c5142019-07-12 09:53:43 -06007982
7983```
7984 Metadata is information attached to targets throughout the dependency tree. GN
7985 allows for the collection of this data into files written during the generation
Keir Mierle45611e32019-11-12 11:18:00 -08007986 step, enabling users to expose and aggregate this data based on the dependency
Julie Hockett152c5142019-07-12 09:53:43 -06007987 tree.
7988```
7989
7990#### **generated_file targets**
7991
7992```
7993 Similar to the write_file() function, the generated_file target type
7994 creates a file in the specified location with the specified content. The
Keir Mierle45611e32019-11-12 11:18:00 -08007995 primary difference between write_file() and this target type is that the
Julie Hockett152c5142019-07-12 09:53:43 -06007996 write_file function does the file write at parse time, while the
7997 generated_file target type writes at target resolution time. See
7998 "gn help generated_file" for more detail.
7999
Keir Mierle45611e32019-11-12 11:18:00 -08008000 When written at target resolution time, generated_file enables GN to
Julie Hockett152c5142019-07-12 09:53:43 -06008001 collect and write aggregated metadata from dependents.
8002
Keir Mierle45611e32019-11-12 11:18:00 -08008003 A generated_file target can declare either 'contents' to write statically
8004 known contents to a file or 'data_keys' to aggregate metadata and write the
8005 result to a file. It can also specify 'walk_keys' (to restrict the metadata
Julie Hockett152c5142019-07-12 09:53:43 -06008006 collection), 'output_conversion', and 'rebase'.
8007```
8008
8009#### **Collection and Aggregation**
8010
8011```
8012 Targets can declare a 'metadata' variable containing a scope, and this
Keir Mierle45611e32019-11-12 11:18:00 -08008013 metadata may be collected and written out to a file specified by
8014 generated_file aggregation targets. The 'metadata' scope must contain
8015 only list values since the aggregation step collects a list of these values.
Julie Hockett152c5142019-07-12 09:53:43 -06008016
8017 During the target resolution, generated_file targets will walk their
8018 dependencies recursively, collecting metadata based on the specified
8019 'data_keys'. 'data_keys' is specified as a list of strings, used by the walk
8020 to identify which variables in dependencies' 'metadata' scopes to collect.
8021
Keir Mierle45611e32019-11-12 11:18:00 -08008022 The walk begins with the listed dependencies of the 'generated_file' target.
8023 The 'metadata' scope for each dependency is inspected for matching elements
8024 of the 'generated_file' target's 'data_keys' list. If a match is found, the
8025 data from the dependent's matching key list is appended to the aggregate walk
8026 list. Note that this means that if more than one walk key is specified, the
8027 data in all of them will be aggregated into one list. From there, the walk
8028 will then recurse into the dependencies of each target it encounters,
8029 collecting the specified metadata for each.
Julie Hockett152c5142019-07-12 09:53:43 -06008030
8031 For example:
8032
8033 group("a") {
8034 metadata = {
8035 doom_melon = [ "enable" ]
8036 my_files = [ "foo.cpp" ]
8037 my_extra_files = [ "bar.cpp" ]
8038 }
8039
8040 deps = [ ":b" ]
8041 }
8042
8043 group("b") {
8044 metadata = {
8045 my_files = [ "baz.cpp" ]
8046 }
8047 }
8048
8049 generated_file("metadata") {
8050 outputs = [ "$root_build_dir/my_files.json" ]
8051 data_keys = [ "my_files", "my_extra_files" ]
8052
8053 deps = [ ":a" ]
8054 }
8055
8056 The above will produce the following file data:
8057
8058 foo.cpp
8059 bar.cpp
8060 baz.cpp
8061
Keir Mierle45611e32019-11-12 11:18:00 -08008062 The dependency walk can be limited by using the 'walk_keys'. This is a list of
Julie Hockett152c5142019-07-12 09:53:43 -06008063 labels that should be included in the walk. All labels specified here should
8064 also be in one of the deps lists. These keys act as barriers, where the walk
Keir Mierle45611e32019-11-12 11:18:00 -08008065 will only recurse into the targets listed. An empty list in all specified
Julie Hockett152c5142019-07-12 09:53:43 -06008066 barriers will end that portion of the walk.
8067
8068 group("a") {
8069 metadata = {
8070 my_files = [ "foo.cpp" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02008071 my_files_barrier = [ ":b" ]
Julie Hockett152c5142019-07-12 09:53:43 -06008072 }
8073
8074 deps = [ ":b", ":c" ]
8075 }
8076
8077 group("b") {
8078 metadata = {
8079 my_files = [ "bar.cpp" ]
8080 }
8081 }
8082
8083 group("c") {
8084 metadata = {
8085 my_files = [ "doom_melon.cpp" ]
8086 }
8087 }
8088
8089 generated_file("metadata") {
8090 outputs = [ "$root_build_dir/my_files.json" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02008091 data_keys = [ "my_files" ]
8092 walk_keys = [ "my_files_barrier" ]
Julie Hockett152c5142019-07-12 09:53:43 -06008093
8094 deps = [ ":a" ]
8095 }
8096
8097 The above will produce the following file data (note that `doom_melon.cpp` is
8098 not included):
8099
8100 foo.cpp
8101 bar.cpp
8102
8103 A common example of this sort of barrier is in builds that have host tools
8104 built as part of the tree, but do not want the metadata from those host tools
8105 to be collected with the target-side code.
8106```
8107
8108#### **Common Uses**
8109
8110```
8111 Metadata can be used to collect information about the different targets in the
8112 build, and so a common use is to provide post-build tooling with a set of data
8113 necessary to do aggregation tasks. For example, if each test target specifies
8114 the output location of its binary to run in a metadata field, that can be
8115 collected into a single file listing the locations of all tests in the
8116 dependency tree. A local build tool (or continuous integration infrastructure)
8117 can then use that file to know which tests exist, and where, and run them
8118 accordingly.
8119
8120 Another use is in image creation, where a post-build image tool needs to know
8121 various pieces of information about the components it should include in order
8122 to put together the correct image.
8123```
[email protected]449f3e42024-08-01 21:43:27 +08008124### **Ninja build rules** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07008125
8126#### **The "all" and "default" rules**
8127
8128```
8129 All generated targets (see "gn help execution") will be added to an implicit
8130 build rule called "all" so "ninja all" will always compile everything. The
8131 default rule will be used by Ninja if no specific target is specified (just
8132 typing "ninja"). If there is a target named "default" in the root build file,
8133 it will be the default build rule, otherwise the implicit "all" rule will be
8134 used.
8135```
8136
8137#### **Phony rules**
8138
8139```
8140 GN generates Ninja "phony" rules for targets in the default toolchain. The
8141 phony rules can collide with each other and with the names of generated files
8142 so are generated with the following priority:
8143
8144 1. Actual files generated by the build always take precedence.
8145
8146 2. Targets in the toplevel //BUILD.gn file.
8147
8148 3. Targets in toplevel directories matching the names of the directories.
8149 So "ninja foo" can be used to compile "//foo:foo". This only applies to
8150 the first level of directories since usually these are the most
8151 important (so this won't apply to "//foo/bar:bar").
8152
8153 4. The short names of executables if there is only one executable with that
8154 short name. Use "ninja doom_melon" to compile the
8155 "//tools/fruit:doom_melon" executable.
8156
Sylvain Defresne5e19d2f2022-12-12 18:20:36 +01008157 Note that for Apple platforms, create_bundle targets with a product_type
8158 of "com.apple.product-type.application" are considered as executable
8159 for this rule (as they define application bundles).
8160
Brett Wilson796ed472018-07-16 15:11:09 -07008161 5. The short names of all targets if there is only one target with that
8162 short name.
8163
8164 6. Full label name with no leading slashes. So you can use
8165 "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon".
8166
8167 7. Labels with an implicit name part (when the short names match the
8168 directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar".
8169
8170 These "phony" rules are provided only for running Ninja since this matches
8171 people's historical expectations for building. For consistency with the rest
8172 of the program, GN introspection commands accept explicit labels.
8173
8174 To explicitly compile a target in a non-default toolchain, you must give
8175 Ninja the exact name of the output file relative to the build directory.
8176```
[email protected]449f3e42024-08-01 21:43:27 +08008177### **nogncheck**: Skip an include line from checking. [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07008178
8179```
8180 GN's header checker helps validate that the includes match the build
8181 dependency graph. Sometimes an include might be conditional or otherwise
8182 problematic, but you want to specifically allow it. In this case, it can be
Dirk Prankeed1abc12025-01-10 17:41:03 -08008183 allowlisted.
Brett Wilson796ed472018-07-16 15:11:09 -07008184
8185 Include lines containing the substring "nogncheck" will be excluded from
8186 header checking. The most common case is a conditional include:
8187
8188 #if defined(ENABLE_DOOM_MELON)
8189 #include "tools/doom_melon/doom_melon.h" // nogncheck
8190 #endif
8191
8192 If the build file has a conditional dependency on the corresponding target
8193 that matches the conditional include, everything will always link correctly:
8194
8195 source_set("mytarget") {
8196 ...
8197 if (enable_doom_melon) {
8198 defines = [ "ENABLE_DOOM_MELON" ]
8199 deps += [ "//tools/doom_melon" ]
8200 }
8201
8202 But GN's header checker does not understand preprocessor directives, won't
8203 know it matches the build dependencies, and will flag this include as
8204 incorrect when the condition is false.
8205```
8206
8207#### **More information**
8208
8209```
8210 The topic "gn help check" has general information on how checking works and
8211 advice on fixing problems. Targets can also opt-out of checking, see
8212 "gn help check_includes".
8213```
[email protected]449f3e42024-08-01 21:43:27 +08008214### **Runtime dependencies** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07008215
8216```
8217 Runtime dependencies of a target are exposed via the "runtime_deps" category
8218 of "gn desc" (see "gn help desc") or they can be written at build generation
8219 time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help
8220 --runtime-deps-list-file").
8221
8222 To a first approximation, the runtime dependencies of a target are the set of
8223 "data" files, data directories, and the shared libraries from all transitive
8224 dependencies. Executables, shared libraries, and loadable modules are
8225 considered runtime dependencies of themselves.
8226```
8227
8228#### **Executables**
8229
8230```
8231 Executable targets and those executable targets' transitive dependencies are
8232 not considered unless that executable is listed in "data_deps". Otherwise, GN
8233 assumes that the executable (and everything it requires) is a build-time
8234 dependency only.
8235```
8236
8237#### **Actions and copies**
8238
8239```
8240 Action and copy targets that are listed as "data_deps" will have all of their
8241 outputs and data files considered as runtime dependencies. Action and copy
8242 targets that are "deps" or "public_deps" will have only their data files
8243 considered as runtime dependencies. These targets can list an output file in
8244 both the "outputs" and "data" lists to force an output file as a runtime
8245 dependency in all cases.
8246
8247 The different rules for deps and data_deps are to express build-time (deps)
8248 vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as
8249 data dependencies, there would be a lot of extra stuff, and if GN counted all
8250 run-time dependencies as regular deps, the build's parallelism would be
8251 unnecessarily constrained.
8252
8253 This rule can sometimes lead to unintuitive results. For example, given the
8254 three targets:
8255 A --[data_deps]--> B --[deps]--> ACTION
8256 GN would say that A does not have runtime deps on the result of the ACTION,
8257 which is often correct. But the purpose of the B target might be to collect
8258 many actions into one logic unit, and the "data"-ness of A's dependency is
8259 lost. Solutions:
8260
8261 - List the outputs of the action in its data section (if the results of
8262 that action are always runtime files).
8263 - Have B list the action in data_deps (if the outputs of the actions are
8264 always runtime files).
8265 - Have B list the action in both deps and data deps (if the outputs might be
8266 used in both contexts and you don't care about unnecessary entries in the
8267 list of files required at runtime).
8268 - Split B into run-time and build-time versions with the appropriate "deps"
8269 for each.
8270```
8271
8272#### **Static libraries and source sets**
8273
8274```
8275 The results of static_library or source_set targets are not considered
8276 runtime dependencies since these are assumed to be intermediate targets only.
8277 If you need to list a static library as a runtime dependency, you can
8278 manually compute the .a/.lib file name for the current platform and list it
8279 in the "data" list of a target (possibly on the static library target
8280 itself).
8281```
8282
8283#### **Multiple outputs**
8284
8285```
8286 Linker tools can specify which of their outputs should be considered when
8287 computing the runtime deps by setting runtime_outputs. If this is unset on
8288 the tool, the default will be the first output only.
8289```
[email protected]449f3e42024-08-01 21:43:27 +08008290### **How Source Expansion Works** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07008291
8292```
8293 Source expansion is used for the action_foreach and copy target types to map
8294 source file names to output file names or arguments.
8295
8296 To perform source expansion in the outputs, GN maps every entry in the
8297 sources to every entry in the outputs list, producing the cross product of
8298 all combinations, expanding placeholders (see below).
8299
8300 Source expansion in the args works similarly, but performing the placeholder
8301 substitution produces a different set of arguments for each invocation of the
8302 script.
8303
8304 If no placeholders are found, the outputs or args list will be treated as a
8305 static list of literal file names that do not depend on the sources.
8306
8307 See "gn help copy" and "gn help action_foreach" for more on how this is
8308 applied.
8309```
8310
8311#### **Placeholders**
8312
8313```
8314 This section discusses only placeholders for actions. There are other
8315 placeholders used in the definition of tools. See "gn help tool" for those.
8316
8317 {{source}}
8318 The name of the source file including directory (*). This will generally
8319 be used for specifying inputs to a script in the "args" variable.
8320 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
8321
8322 {{source_file_part}}
8323 The file part of the source including the extension.
8324 "//foo/bar/baz.txt" => "baz.txt"
8325
8326 {{source_name_part}}
8327 The filename part of the source file with no directory or extension. This
8328 will generally be used for specifying a transformation from a source file
8329 to a destination file with the same name but different extension.
8330 "//foo/bar/baz.txt" => "baz"
8331
8332 {{source_dir}}
8333 The directory (*) containing the source file with no trailing slash.
8334 "//foo/bar/baz.txt" => "../../foo/bar"
8335
8336 {{source_root_relative_dir}}
8337 The path to the source file's directory relative to the source root, with
8338 no leading "//" or trailing slashes. If the path is system-absolute,
8339 (beginning in a single slash) this will just return the path with no
8340 trailing slash. This value will always be the same, regardless of whether
8341 it appears in the "outputs" or "args" section.
8342 "//foo/bar/baz.txt" => "foo/bar"
8343
8344 {{source_gen_dir}}
8345 The generated file directory (*) corresponding to the source file's path.
8346 This will be different than the target's generated file directory if the
8347 source file is in a different directory than the BUILD.gn file.
8348 "//foo/bar/baz.txt" => "gen/foo/bar"
8349
8350 {{source_out_dir}}
8351 The object file directory (*) corresponding to the source file's path,
8352 relative to the build directory. this us be different than the target's
8353 out directory if the source file is in a different directory than the
8354 build.gn file.
8355 "//foo/bar/baz.txt" => "obj/foo/bar"
8356
8357 {{source_target_relative}}
8358 The path to the source file relative to the target's directory. This will
8359 generally be used for replicating the source directory layout in the
Robert Sesek252674d2019-05-10 14:08:52 -04008360 output directory. This can only be used in actions and bundle_data
8361 targets. It is an error to use in process_file_template where there is no
8362 "target".
Brett Wilson796ed472018-07-16 15:11:09 -07008363 "//foo/bar/baz.txt" => "baz.txt"
8364```
8365
8366#### **(*) Note on directories**
8367
8368```
8369 Paths containing directories (except the source_root_relative_dir) will be
8370 different depending on what context the expansion is evaluated in. Generally
8371 it should "just work" but it means you can't concatenate strings containing
8372 these values with reasonable results.
8373
8374 Details: source expansions can be used in the "outputs" variable, the "args"
8375 variable, and in calls to "process_file_template". The "args" are passed to a
8376 script which is run from the build directory, so these directories will
8377 relative to the build directory for the script to find. In the other cases,
8378 the directories will be source- absolute (begin with a "//") because the
8379 results of those expansions will be handled by GN internally.
8380```
8381
8382#### **Examples**
8383
8384```
8385 Non-varying outputs:
8386 action("hardcoded_outputs") {
8387 sources = [ "input1.idl", "input2.idl" ]
8388 outputs = [ "$target_out_dir/output1.dat",
8389 "$target_out_dir/output2.dat" ]
8390 }
8391 The outputs in this case will be the two literal files given.
8392
8393 Varying outputs:
8394 action_foreach("varying_outputs") {
8395 sources = [ "input1.idl", "input2.idl" ]
8396 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
8397 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
8398 }
8399 Performing source expansion will result in the following output names:
8400 //out/Debug/obj/mydirectory/input1.h
8401 //out/Debug/obj/mydirectory/input1.cc
8402 //out/Debug/obj/mydirectory/input2.h
8403 //out/Debug/obj/mydirectory/input2.cc
8404```
[email protected]449f3e42024-08-01 21:43:27 +08008405### **Available global switches** [Back to Top](#gn-reference)
Brett Wilson796ed472018-07-16 15:11:09 -07008406
8407```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008408 Do "gn help --the_switch_you_want_help_on" for more. Individual commands may
8409 take command-specific switches not listed here. See the help on your specific
8410 command for more.
8411```
8412```
8413 * --args: Specifies build arguments overrides.
8414 * --color: Force colored output.
8415 * --dotfile: Override the name of the ".gn" file.
8416 * --fail-on-unused-args: Treat unused build args as fatal errors.
8417 * --markdown: Write help output in the Markdown format.
RJ Ascani6966efb2020-10-19 16:50:11 -07008418 * --ninja-executable: Set the Ninja executable.
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008419 * --nocolor: Force non-colored output.
8420 * -q: Quiet mode. Don't print output on success.
8421 * --root: Explicitly specify source root.
David 'Digit' Turner85cc21e2025-04-25 17:28:31 +02008422 * --root-pattern: Add root pattern override.
Nico Weberad9eab22020-11-15 22:20:28 -05008423 * --root-target: Override the root target.
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008424 * --runtime-deps-list-file: Save runtime dependencies for targets in file.
8425 * --script-executable: Set the executable used to execute scripts.
8426 * --threads: Specify number of worker threads.
8427 * --time: Outputs a summary of how long everything took.
8428 * --tracelog: Writes a Chrome-compatible trace log to the given file.
8429 * -v: Verbose logging.
8430 * --version: Prints the GN version number and exits.
Brett Wilson796ed472018-07-16 15:11:09 -07008431```
8432