blob: 093c711a6e0dd17ce032f04f05f9ebb02207ee20 [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)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080046 * [foreach: Iterate over a list.](#func_foreach)
47 * [forward_variables_from: Copies variables from a different scope.](#func_forward_variables_from)
48 * [get_label_info: Get an attribute from a target's label.](#func_get_label_info)
49 * [get_path_info: Extract parts of a file or directory name.](#func_get_path_info)
50 * [get_target_outputs: [file list] Get the list of outputs from a target.](#func_get_target_outputs)
51 * [getenv: Get an environment variable.](#func_getenv)
52 * [import: Import a file into the current scope.](#func_import)
53 * [not_needed: Mark variables from scope as not needed.](#func_not_needed)
54 * [pool: Defines a pool object.](#func_pool)
55 * [print: Prints to the console.](#func_print)
Aaron Wood24062bb2022-04-25 20:37:48 -070056 * [print_stack_trace: Prints a stack trace.](#func_print_stack_trace)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080057 * [process_file_template: Do template expansion over a list of files.](#func_process_file_template)
58 * [read_file: Read a file into a variable.](#func_read_file)
59 * [rebase_path: Rebase a file or directory to another location.](#func_rebase_path)
60 * [set_default_toolchain: Sets the default toolchain name.](#func_set_default_toolchain)
61 * [set_defaults: Set default values for a target type.](#func_set_defaults)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080062 * [split_list: Splits a list into N different sub-lists.](#func_split_list)
Keir Mierle6ae63302019-11-08 23:02:18 -080063 * [string_join: Concatenates a list of strings with a separator.](#func_string_join)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080064 * [string_replace: Replaces substring in the given string.](#func_string_replace)
Keir Mierle6ae63302019-11-08 23:02:18 -080065 * [string_split: Split string into a list of strings.](#func_string_split)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080066 * [template: Define a template rule.](#func_template)
67 * [tool: Specify arguments to a toolchain tool.](#func_tool)
68 * [toolchain: Defines a toolchain.](#func_toolchain)
69 * [write_file: Write a file to disk.](#func_write_file)
Brett Wilson796ed472018-07-16 15:11:09 -070070* [Built-in predefined variables](#predefined_variables)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080071 * [current_cpu: [string] The processor architecture of the current toolchain.](#var_current_cpu)
72 * [current_os: [string] The operating system of the current toolchain.](#var_current_os)
73 * [current_toolchain: [string] Label of the current toolchain.](#var_current_toolchain)
74 * [default_toolchain: [string] Label of the default toolchain.](#var_default_toolchain)
James Robinson61377e32020-02-13 15:20:07 -080075 * [gn_version: [number] The version of gn.](#var_gn_version)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080076 * [host_cpu: [string] The processor architecture that GN is running on.](#var_host_cpu)
77 * [host_os: [string] The operating system that GN is running on.](#var_host_os)
78 * [invoker: [string] The invoking scope inside a template.](#var_invoker)
79 * [python_path: [string] Absolute path of Python.](#var_python_path)
80 * [root_build_dir: [string] Directory where build commands are run.](#var_root_build_dir)
81 * [root_gen_dir: [string] Directory for the toolchain's generated files.](#var_root_gen_dir)
82 * [root_out_dir: [string] Root directory for toolchain output files.](#var_root_out_dir)
83 * [target_cpu: [string] The desired cpu architecture for the build.](#var_target_cpu)
84 * [target_gen_dir: [string] Directory for a target's generated files.](#var_target_gen_dir)
85 * [target_name: [string] The name of the current target.](#var_target_name)
86 * [target_os: [string] The desired operating system for the build.](#var_target_os)
87 * [target_out_dir: [string] Directory for target output files.](#var_target_out_dir)
Brett Wilson796ed472018-07-16 15:11:09 -070088* [Variables you set in targets](#target_variables)
Julie Hockettce1fa072019-05-07 17:44:37 -070089 * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080090 * [all_dependent_configs: [label list] Configs to be forced on dependents.](#var_all_dependent_configs)
91 * [allow_circular_includes_from: [label list] Permit includes from deps.](#var_allow_circular_includes_from)
92 * [arflags: [string list] Arguments passed to static_library archiver.](#var_arflags)
93 * [args: [string list] Arguments passed to an action.](#var_args)
94 * [asmflags: [string list] Flags passed to the assembler.](#var_asmflags)
95 * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#var_assert_no_deps)
Sylvain Defresne89e64252020-08-07 13:01:06 +020096 * [bridge_header: [string] Path to C/Objective-C compatibility header.](#var_bridge_header)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080097 * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#var_bundle_contents_dir)
98 * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#var_bundle_deps_filter)
99 * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#var_bundle_executable_dir)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800100 * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#var_bundle_resources_dir)
101 * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#var_bundle_root_dir)
102 * [cflags: [string list] Flags passed to all C compiler variants.](#var_cflags)
103 * [cflags_c: [string list] Flags passed to the C compiler.](#var_cflags_c)
104 * [cflags_cc: [string list] Flags passed to the C++ compiler.](#var_cflags_cc)
105 * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#var_cflags_objc)
106 * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#var_cflags_objcc)
107 * [check_includes: [boolean] Controls whether a target's files are checked.](#var_check_includes)
108 * [code_signing_args: [string list] Arguments passed to code signing script.](#var_code_signing_args)
109 * [code_signing_outputs: [file list] Output files for code signing step.](#var_code_signing_outputs)
110 * [code_signing_script: [file name] Script for code signing.](#var_code_signing_script)
111 * [code_signing_sources: [file list] Sources for code signing step.](#var_code_signing_sources)
112 * [complete_static_lib: [boolean] Links all deps into a static library.](#var_complete_static_lib)
113 * [configs: [label list] Configs applying to this target or config.](#var_configs)
114 * [contents: Contents to write to file.](#var_contents)
Julie Hockettce1fa072019-05-07 17:44:37 -0700115 * [crate_name: [string] The name for the compiled crate.](#var_crate_name)
116 * [crate_root: [string] The root source file for a binary or library.](#var_crate_root)
117 * [crate_type: [string] The type of linkage to use on a shared_library.](#var_crate_type)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800118 * [data: [file list] Runtime data file dependencies.](#var_data)
119 * [data_deps: [label list] Non-linked dependencies.](#var_data_deps)
120 * [data_keys: [string list] Keys from which to collect metadata.](#var_data_keys)
121 * [defines: [string list] C preprocessor defines.](#var_defines)
122 * [depfile: [string] File name for input dependencies for actions.](#var_depfile)
123 * [deps: [label list] Private linked dependencies.](#var_deps)
Petr Hoseka1413862020-01-03 12:54:33 -0800124 * [externs: [scope] Set of Rust crate-dependency pairs.](#var_externs)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100125 * [framework_dirs: [directory list] Additional framework search directories.](#var_framework_dirs)
126 * [frameworks: [name list] Name of frameworks that must be linked.](#var_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800127 * [friend: [label pattern list] Allow targets to include private headers.](#var_friend)
Tyler Mandry4a648092022-02-15 19:47:09 +0000128 * [gen_deps: [label list] Declares targets that should generate when this one does.](#var_gen_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800129 * [include_dirs: [directory list] Additional include directories.](#var_include_dirs)
130 * [inputs: [file list] Additional compile-time dependencies.](#var_inputs)
131 * [ldflags: [string list] Flags passed to the linker.](#var_ldflags)
132 * [lib_dirs: [directory list] Additional library directories.](#var_lib_dirs)
133 * [libs: [string list] Additional libraries to link.](#var_libs)
134 * [metadata: [scope] Metadata of this target.](#var_metadata)
Joseph Ryanad1e5ce2023-04-26 13:48:01 -0700135 * [mnemonic: [string] Prefix displayed when ninja runs this action.](#var_mnemonic)
Sylvain Defresne89e64252020-08-07 13:01:06 +0200136 * [module_name: [string] The name for the compiled module.](#var_module_name)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800137 * [output_conversion: Data format for generated_file targets.](#var_output_conversion)
138 * [output_dir: [directory] Directory to put output file in.](#var_output_dir)
139 * [output_extension: [string] Value to use for the output's file extension.](#var_output_extension)
140 * [output_name: [string] Name for the output file other than the default.](#var_output_name)
141 * [output_prefix_override: [boolean] Don't use prefix for output name.](#var_output_prefix_override)
142 * [outputs: [file list] Output files for actions and copy targets.](#var_outputs)
143 * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#var_partial_info_plist)
Petr Hosek7a6231e2022-10-22 23:14:18 +0000144 * [pool: [string] Label of the pool used by binary targets and actions.](#var_pool)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800145 * [precompiled_header: [string] Header file to precompile.](#var_precompiled_header)
146 * [precompiled_header_type: [string] "gcc" or "msvc".](#var_precompiled_header_type)
147 * [precompiled_source: [file name] Source file to precompile.](#var_precompiled_source)
148 * [product_type: [string] Product type for Xcode projects.](#var_product_type)
149 * [public: [file list] Declare public header files for a target.](#var_public)
150 * [public_configs: [label list] Configs applied to dependents.](#var_public_configs)
151 * [public_deps: [label list] Declare public dependencies.](#var_public_deps)
152 * [rebase: [boolean] Rebase collected metadata as files.](#var_rebase)
153 * [response_file_contents: [string list] Contents of .rsp file for actions.](#var_response_file_contents)
154 * [script: [file name] Script file for actions.](#var_script)
155 * [sources: [file list] Source files for a target.](#var_sources)
Sylvain Defresne89e64252020-08-07 13:01:06 +0200156 * [swiftflags: [string list] Flags passed to the swift compiler.](#var_swiftflags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800157 * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly)
158 * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility)
159 * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys)
Robert Sesekd0a6f072020-05-15 11:21:22 -0400160 * [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800161 * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps)
Harley Li0a9affb2020-06-03 10:38:42 -0400162 * [xcasset_compiler_flags: [string list] Flags passed to xcassets compiler](#var_xcasset_compiler_flags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800163 * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes)
164 * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name)
Brett Wilson796ed472018-07-16 15:11:09 -0700165* [Other help topics](#other)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800166 * all: Print all the help at once
Brett Wilson796ed472018-07-16 15:11:09 -0700167 * [buildargs: How build arguments work.](#buildargs)
168 * [dotfile: Info about the toplevel .gn file.](#dotfile)
169 * [execution: Build graph and execution overview.](#execution)
170 * [grammar: Language and grammar for GN build files.](#grammar)
Julie Hockett81ee1962019-05-13 11:24:42 -0700171 * [input_conversion: Processing input from exec_script and read_file.](#io_conversion)
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100172 * [file_pattern: Matching more than one file.](#file_pattern)
Brett Wilson796ed472018-07-16 15:11:09 -0700173 * [label_pattern: Matching more than one label.](#label_pattern)
174 * [labels: About labels.](#labels)
Julie Hockett152c5142019-07-12 09:53:43 -0600175 * [metadata_collection: About metadata and its collection.](#metadata_collection)
Brett Wilson796ed472018-07-16 15:11:09 -0700176 * [ninja_rules: How Ninja build rules are named.](#ninja_rules)
177 * [nogncheck: Annotating includes for checking.](#nogncheck)
Julie Hockett81ee1962019-05-13 11:24:42 -0700178 * [output_conversion: Specifies how to transform a value to output.](#io_conversion)
Brett Wilson796ed472018-07-16 15:11:09 -0700179 * [runtime_deps: How runtime dependency computation works.](#runtime_deps)
180 * [source_expansion: Map sources to outputs for scripts.](#source_expansion)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800181 * [switches: Show available command-line switches.](#switch_list)
Brett Wilson796ed472018-07-16 15:11:09 -0700182
183## <a name="commands"></a>Commands
184
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800185### <a name="cmd_analyze"></a>**gn analyze &lt;out_dir&gt; &lt;input_path&gt; &lt;output_path&gt;**
Brett Wilson796ed472018-07-16 15:11:09 -0700186
187```
188 Analyze which targets are affected by a list of files.
189
190 This command takes three arguments:
191
192 out_dir is the path to the build directory.
193
194 input_path is a path to a file containing a JSON object with three fields:
195
196 - "files": A list of the filenames to check.
197
198 - "test_targets": A list of the labels for targets that are needed to run
199 the tests we wish to run.
200
Nico Weberdba01722021-04-06 08:56:46 -0400201 - "additional_compile_targets" (optional): A list of the labels for targets
202 that we wish to rebuild, but aren't necessarily needed for testing. The
203 important difference between this field and "test_targets" is that if an
204 item in the additional_compile_targets list refers to a group, then any
Brett Wilson796ed472018-07-16 15:11:09 -0700205 dependencies of that group will be returned if they are out of date, but
206 the group itself does not need to be. If the dependencies themselves are
207 groups, the same filtering is repeated. This filtering can be used to
208 avoid rebuilding dependencies of a group that are unaffected by the input
209 files. The list may also contain the string "all" to refer to a
210 pseudo-group that contains every root target in the build graph.
211
212 This filtering behavior is also known as "pruning" the list of compile
213 targets.
214
Nico Weberdba01722021-04-06 08:56:46 -0400215 If "additional_compile_targets" is absent, it defaults to the empty list.
216
Keir Mierle45611e32019-11-12 11:18:00 -0800217 If input_path is -, input is read from stdin.
218
Brett Wilson796ed472018-07-16 15:11:09 -0700219 output_path is a path indicating where the results of the command are to be
220 written. The results will be a file containing a JSON object with one or more
221 of following fields:
222
223 - "compile_targets": A list of the labels derived from the input
224 compile_targets list that are affected by the input files. Due to the way
225 the filtering works for compile targets as described above, this list may
226 contain targets that do not appear in the input list.
227
228 - "test_targets": A list of the labels from the input test_targets list that
229 are affected by the input files. This list will be a proper subset of the
230 input list.
231
232 - "invalid_targets": A list of any names from the input that do not exist in
233 the build graph. If this list is non-empty, the "error" field will also be
234 set to "Invalid targets".
235
236 - "status": A string containing one of three values:
237
238 - "Found dependency"
239 - "No dependency"
Sylvain Defresne7b0690e2020-06-26 15:33:01 +0200240 - "Found dependency (all)"
Brett Wilson796ed472018-07-16 15:11:09 -0700241
242 In the first case, the lists returned in compile_targets and test_targets
243 should be passed to ninja to build. In the second case, nothing was
244 affected and no build is necessary. In the third case, GN could not
245 determine the correct answer and returned the input as the output in order
246 to be safe.
247
248 - "error": This will only be present if an error occurred, and will contain
249 a string describing the error. This includes cases where the input file is
250 not in the right format, or contains invalid targets.
251
Keir Mierle45611e32019-11-12 11:18:00 -0800252 If output_path is -, output is written to stdout.
253
Brett Wilson796ed472018-07-16 15:11:09 -0700254 The command returns 1 if it is unable to read the input file or write the
255 output file, or if there is something wrong with the build such that gen
256 would also fail, and 0 otherwise. In particular, it returns 0 even if the
257 "error" key is non-empty and a non-fatal error occurred. In other words, it
258 tries really hard to always write something to the output JSON and convey
259 errors that way rather than via return codes.
260```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800261### <a name="cmd_args"></a>**gn args**: (command-line tool)
Brett Wilson796ed472018-07-16 15:11:09 -0700262
263```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800264 Display or configure arguments declared by the build.
265
266 gn args <out_dir> [--list] [--short] [--args] [--overrides-only]
267
Brett Wilson796ed472018-07-16 15:11:09 -0700268 See also "gn help buildargs" for a more high-level overview of how
269 build arguments work.
270```
271
272#### **Usage**
273
274```
275 gn args <out_dir>
276 Open the arguments for the given build directory in an editor. If the
277 given build directory doesn't exist, it will be created and an empty args
278 file will be opened in the editor. You would type something like this
279 into that file:
280 enable_doom_melon=false
281 os="android"
282
283 To find your editor on Posix, GN will search the environment variables in
284 order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command
285 associated with .txt files.
286
287 Note: you can edit the build args manually by editing the file "args.gn"
288 in the build directory and then running "gn gen <out_dir>".
289
290 gn args <out_dir> --list[=<exact_arg>] [--short] [--overrides-only] [--json]
291 Lists all build arguments available in the current configuration, or, if
292 an exact_arg is specified for the list flag, just that one build
293 argument.
294
295 The output will list the declaration location, current value for the
296 build, default value (if different than the current value), and comment
297 preceding the declaration.
298
299 If --short is specified, only the names and current values will be
300 printed.
301
302 If --overrides-only is specified, only the names and current values of
303 arguments that have been overridden (i.e. non-default arguments) will
304 be printed. Overrides come from the <out_dir>/args.gn file and //.gn
305
306 If --json is specified, the output will be emitted in json format.
307 JSON schema for output:
308 [
309 {
310 "name": variable_name,
311 "current": {
312 "value": overridden_value,
313 "file": file_name,
314 "line": line_no
315 },
316 "default": {
317 "value": default_value,
318 "file": file_name,
319 "line": line_no
320 },
321 "comment": comment_string
322 },
323 ...
324 ]
325```
326
327#### **Examples**
328
329```
330 gn args out/Debug
331 Opens an editor with the args for out/Debug.
332
333 gn args out/Debug --list --short
334 Prints all arguments with their default values for the out/Debug
335 build.
336
337 gn args out/Debug --list --short --overrides-only
338 Prints overridden arguments for the out/Debug build.
339
340 gn args out/Debug --list=target_cpu
341 Prints information about the "target_cpu" argument for the "
342 "out/Debug
343 build.
344
345 gn args --list --args="os=\"android\" enable_doom_melon=true"
346 Prints all arguments with the default values for a build with the
347 given arguments set (which may affect the values of other
348 arguments).
349```
Joe Armstrong39413942019-03-15 10:34:03 +0800350### <a name="cmd_check"></a>**gn check &lt;out_dir&gt; [&lt;label_pattern&gt;] [\--force] [\--check-generated]**
Brett Wilson796ed472018-07-16 15:11:09 -0700351
352```
353 GN's include header checker validates that the includes for C-like source
354 files match the build dependency graph.
355
356 "gn check" is the same thing as "gn gen" with the "--check" flag except that
357 this command does not write out any build files. It's intended to be an easy
358 way to manually trigger include file checking.
359
360 The <label_pattern> can take exact labels or patterns that match more than
361 one (although not general regular expressions). If specified, only those
362 matching targets will be checked. See "gn help label_pattern" for details.
363```
364
365#### **Command-specific switches**
366
367```
Joe Armstrong39413942019-03-15 10:34:03 +0800368 --check-generated
369 Generated files are normally not checked since they do not exist
370 until after a build. With this flag, those generated files that
371 can be found on disk are also checked.
James Robinson49f59032020-01-08 14:05:51 -0800372
373 --check-system
374 Check system style includes (using <angle brackets>) in addition to
375 "double quote" includes.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400376
377 --default-toolchain
378 Normally wildcard targets are matched in all toolchains. This
379 switch makes wildcard labels with no explicit toolchain reference
380 only match targets in the default toolchain.
381
382 Non-wildcard inputs with no explicit toolchain specification will
383 always match only a target in the default toolchain if one exists.
384
385 --force
386 Ignores specifications of "check_includes = false" and checks all
387 target's files that match the target label.
Brett Wilson796ed472018-07-16 15:11:09 -0700388```
389
390#### **What gets checked**
391
392```
Nate Fischer8ed01d32019-01-08 17:32:01 -0800393 The .gn file may specify a list of targets to be checked in the list
Erik Staab5d109d72020-06-29 22:53:23 -0700394 check_targets (see "gn help dotfile"). Alternatively, the .gn file may
395 specify a list of targets not to be checked in no_check_targets. If a label
396 pattern is specified on the command line, neither check_targets or
397 no_check_targets is used.
Brett Wilson796ed472018-07-16 15:11:09 -0700398
399 Targets can opt-out from checking with "check_includes = false" (see
400 "gn help check_includes").
401
402 For targets being checked:
403
404 - GN opens all C-like source files in the targets to be checked and scans
405 the top for includes.
406
Joe Armstrong39413942019-03-15 10:34:03 +0800407 - Generated files (that might not exist yet) are ignored unless
408 the --check-generated flag is provided.
409
Brett Wilson796ed472018-07-16 15:11:09 -0700410 - Includes with a "nogncheck" annotation are skipped (see
411 "gn help nogncheck").
412
James Robinson49f59032020-01-08 14:05:51 -0800413 - Includes using "quotes" are always checked.
414 If system style checking is enabled, includes using <angle brackets>
415 are also checked.
Brett Wilson796ed472018-07-16 15:11:09 -0700416
417 - Include paths are assumed to be relative to any of the "include_dirs" for
418 the target (including the implicit current dir).
419
420 - GN does not run the preprocessor so will not understand conditional
421 includes.
422
423 - Only includes matching known files in the build are checked: includes
424 matching unknown paths are ignored.
425
426 For an include to be valid:
427
428 - The included file must be in the current target, or there must be a path
429 following only public dependencies to a target with the file in it
430 ("gn path" is a good way to diagnose problems).
431
432 - There can be multiple targets with an included file: only one needs to be
433 valid for the include to be allowed.
434
435 - If there are only "sources" in a target, all are considered to be public
436 and can be included by other targets with a valid public dependency path.
437
438 - If a target lists files as "public", only those files are able to be
439 included by other targets. Anything in the sources will be considered
440 private and will not be includable regardless of dependency paths.
441
442 - Outputs from actions are treated like public sources on that target.
443
444 - A target can include headers from a target that depends on it if the
445 other target is annotated accordingly. See "gn help
446 allow_circular_includes_from".
447```
448
449#### **Advice on fixing problems**
450
451```
452 If you have a third party project that is difficult to fix or doesn't care
453 about include checks it's generally best to exclude that target from checking
454 altogether via "check_includes = false".
455
456 If you have conditional includes, make sure the build conditions and the
457 preprocessor conditions match, and annotate the line with "nogncheck" (see
458 "gn help nogncheck" for an example).
459
460 If two targets are hopelessly intertwined, use the
461 "allow_circular_includes_from" annotation. Ideally each should have identical
462 dependencies so configs inherited from those dependencies are consistent (see
463 "gn help allow_circular_includes_from").
464
465 If you have a standalone header file or files that need to be shared between
466 a few targets, you can consider making a source_set listing only those
467 headers as public sources. With only header files, the source set will be a
468 no-op from a build perspective, but will give a central place to refer to
469 those headers. That source set's files will still need to pass "gn check" in
470 isolation.
471
472 In rare cases it makes sense to list a header in more than one target if it
473 could be considered conceptually a member of both.
474```
475
476#### **Examples**
477
478```
479 gn check out/Debug
480 Check everything.
481
482 gn check out/Default //foo:bar
483 Check only the files in the //foo:bar target.
484
485 gn check out/Default "//foo/*
486 Check only the files in targets in the //foo directory tree.
487```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400488### <a name="cmd_clean"></a>**gn clean &lt;out_dir&gt;...**
Brett Wilson796ed472018-07-16 15:11:09 -0700489
490```
491 Deletes the contents of the output directory except for args.gn and
492 creates a Ninja build environment sufficient to regenerate the build.
493```
RJ Ascani4e260f12020-10-19 17:41:51 -0700494### <a name="cmd_clean_stale"></a>**gn clean_stale [\--ninja-executable=...] &lt;out_dir&gt;...**
495
496```
497 Removes the no longer needed output files from the build directory and prunes
498 their records from the ninja build log and dependency database. These are
499 output files that were generated from previous builds, but the current build
500 graph no longer references them.
501
502 This command requires a ninja executable of at least version 1.10.0. The
503 executable must be provided by the --ninja-executable switch.
504```
505
506#### **Options**
507
508```
509 --ninja-executable=<string>
510 Can be used to specify the ninja executable to use.
511```
Joe Armstrongb199e542019-03-21 09:40:08 +0800512### <a name="cmd_desc"></a>**gn desc**
Brett Wilson796ed472018-07-16 15:11:09 -0700513
514```
Joe Armstrongb199e542019-03-21 09:40:08 +0800515 gn desc <out_dir> <label or pattern> [<what to show>] [--blame]
516 [--format=json]
517
Julie Hockette2a29402018-07-31 10:11:42 -0700518 Displays information about a given target or config. The build parameters
519 will be taken for the build in the given <out_dir>.
Brett Wilson796ed472018-07-16 15:11:09 -0700520
521 The <label or pattern> can be a target label, a config label, or a label
522 pattern (see "gn help label_pattern"). A label pattern will only match
523 targets.
524```
525
Nico Weber75eb3c72018-08-28 16:01:52 -0400526#### **Possibilities for &lt;what to show&gt;**
Brett Wilson796ed472018-07-16 15:11:09 -0700527
528```
529 (If unspecified an overall summary will be displayed.)
530
531 all_dependent_configs
532 allow_circular_includes_from
533 arflags [--blame]
534 args
535 cflags [--blame]
Julie Hockette2a29402018-07-31 10:11:42 -0700536 cflags_c [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700537 cflags_cc [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700538 check_includes
539 configs [--tree] (see below)
Nate Fischer8ed01d32019-01-08 17:32:01 -0800540 data_keys
Brett Wilson796ed472018-07-16 15:11:09 -0700541 defines [--blame]
542 depfile
543 deps [--all] [--tree] (see below)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100544 framework_dirs
545 frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700546 include_dirs [--blame]
547 inputs
548 ldflags [--blame]
549 lib_dirs
550 libs
Nate Fischer8ed01d32019-01-08 17:32:01 -0800551 metadata
552 output_conversion
Brett Wilson796ed472018-07-16 15:11:09 -0700553 outputs
554 public_configs
555 public
Nate Fischer8ed01d32019-01-08 17:32:01 -0800556 rebase
Brett Wilson796ed472018-07-16 15:11:09 -0700557 script
558 sources
559 testonly
560 visibility
Nate Fischer8ed01d32019-01-08 17:32:01 -0800561 walk_keys
Robert Sesekd0a6f072020-05-15 11:21:22 -0400562 weak_frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700563
564 runtime_deps
565 Compute all runtime deps for the given target. This is a computed list
566 and does not correspond to any GN variable, unlike most other values
567 here.
568
569 The output is a list of file names relative to the build directory. See
570 "gn help runtime_deps" for how this is computed. This also works with
571 "--blame" to see the source of the dependency.
572```
573
574#### **Shared flags**
Brett Wilson796ed472018-07-16 15:11:09 -0700575
Robert Sesekd0a6f072020-05-15 11:21:22 -0400576```
577 --default-toolchain
578 Normally wildcard targets are matched in all toolchains. This
579 switch makes wildcard labels with no explicit toolchain reference
580 only match targets in the default toolchain.
581
582 Non-wildcard inputs with no explicit toolchain specification will
583 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -0700584
585 --format=json
586 Format the output as JSON instead of text.
587```
588
589#### **Target flags**
590
591```
592 --blame
593 Used with any value specified on a config, this will name the config that
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100594 causes that target to get the flag. This doesn't currently work for libs,
Robert Sesekd0a6f072020-05-15 11:21:22 -0400595 lib_dirs, frameworks, weak_frameworks and framework_dirs because those are
596 inherited and are more complicated to figure out the blame (patches
597 welcome).
Brett Wilson796ed472018-07-16 15:11:09 -0700598```
599
600#### **Configs**
601
602```
603 The "configs" section will list all configs that apply. For targets this will
604 include configs specified in the "configs" variable of the target, and also
605 configs pushed onto this target via public or "all dependent" configs.
606
607 Configs can have child configs. Specifying --tree will show the hierarchy.
608```
609
610#### **Printing outputs**
611
612```
613 The "outputs" section will list all outputs that apply, including the outputs
614 computed from the tool definition (eg for "executable", "static_library", ...
615 targets).
616```
617
618#### **Printing deps**
619
620```
621 Deps will include all public, private, and data deps (TODO this could be
622 clarified and enhanced) sorted in order applying. The following may be used:
623
624 --all
625 Collects all recursive dependencies and prints a sorted flat list. Also
626 usable with --tree (see below).
Robert Sesekd0a6f072020-05-15 11:21:22 -0400627
Brett Wilson796ed472018-07-16 15:11:09 -0700628 --as=(buildfile|label|output)
629 How to print targets.
630
631 buildfile
632 Prints the build files where the given target was declared as
633 file names.
634 label (default)
635 Prints the label of the target.
636 output
637 Prints the first output file for the target relative to the
638 root build directory.
639
640 --testonly=(true|false)
641 Restrict outputs to targets with the testonly flag set
642 accordingly. When unspecified, the target's testonly flags are
643 ignored.
644
645 --tree
646 Print a dependency tree. By default, duplicates will be elided with "..."
647 but when --all and -tree are used together, no eliding will be performed.
648
649 The "deps", "public_deps", and "data_deps" will all be included in the
650 tree.
651
652 Tree output can not be used with the filtering or output flags: --as,
653 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400654
Brett Wilson796ed472018-07-16 15:11:09 -0700655 --type=(action|copy|executable|group|loadable_module|shared_library|
656 source_set|static_library)
657 Restrict outputs to targets matching the given type. If
658 unspecified, no filtering will be performed.
659```
660
661#### **Note**
662
663```
664 This command will show the full name of directories and source files, but
665 when directories and source paths are written to the build file, they will be
666 adjusted to be relative to the build directory. So the values for paths
667 displayed by this command won't match (but should mean the same thing).
668```
669
670#### **Examples**
671
672```
673 gn desc out/Debug //base:base
674 Summarizes the given target.
675
676 gn desc out/Foo :base_unittests deps --tree
677 Shows a dependency tree of the "base_unittests" project in
678 the current directory.
679
680 gn desc out/Debug //base defines --blame
681 Shows defines set for the //base:base target, annotated by where
682 each one was set from.
683```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800684### <a name="cmd_format"></a>**gn format [\--dump-tree] (\--stdin | &lt;list of build_files...&gt;)**
Brett Wilson796ed472018-07-16 15:11:09 -0700685
686```
687 Formats .gn file to a standard format.
688
689 The contents of some lists ('sources', 'deps', etc.) will be sorted to a
690 canonical order. To suppress this, you can add a comment of the form "#
691 NOSORT" immediately preceding the assignment. e.g.
692
693 # NOSORT
694 sources = [
695 "z.cc",
696 "a.cc",
697 ]
698```
699
700#### **Arguments**
701
702```
703 --dry-run
Nico Weberad9eab22020-11-15 22:20:28 -0500704 Prints the list of files that would be reformatted but does not write
705 anything to disk. This is useful for presubmit/lint-type checks.
Brett Wilson796ed472018-07-16 15:11:09 -0700706 - Exit code 0: successful format, matches on disk.
707 - Exit code 1: general failure (parse error, etc.)
708 - Exit code 2: successful format, but differs from on disk.
709
Julie Hockettd69a9c32019-01-23 14:36:18 -0800710 --dump-tree[=( text | json )]
711 Dumps the parse tree to stdout and does not update the file or print
712 formatted output. If no format is specified, text format will be used.
Brett Wilson796ed472018-07-16 15:11:09 -0700713
714 --stdin
715 Read input from stdin and write to stdout rather than update a file
716 in-place.
RJ Ascani4e260f12020-10-19 17:41:51 -0700717
718 --read-tree=json
719 Reads an AST from stdin in the format output by --dump-tree=json and
720 uses that as the parse tree. (The only read-tree format currently
721 supported is json.) The given .gn file will be overwritten. This can be
722 used to programmatically transform .gn files.
Brett Wilson796ed472018-07-16 15:11:09 -0700723```
724
725#### **Examples**
726```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800727 gn format //some/BUILD.gn //some/other/BUILD.gn //and/another/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700728 gn format some\\BUILD.gn
729 gn format /abspath/some/BUILD.gn
730 gn format --stdin
RJ Ascani4e260f12020-10-19 17:41:51 -0700731 gn format --read-tree=json //rewritten/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700732```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800733### <a name="cmd_gen"></a>**gn gen [\--check] [&lt;ide options&gt;] &lt;out_dir&gt;**
Brett Wilson796ed472018-07-16 15:11:09 -0700734
735```
736 Generates ninja files from the current tree and puts them in the given output
737 directory.
738
739 The output directory can be a source-repo-absolute path name such as:
740 //out/foo
741 Or it can be a directory relative to the current directory such as:
742 out/foo
743
James Robinson49f59032020-01-08 14:05:51 -0800744 "gn gen --check" is the same as running "gn check". "gn gen --check=system" is
745 the same as running "gn check --check-system". See "gn help check" for
746 documentation on that mode.
Brett Wilson796ed472018-07-16 15:11:09 -0700747
748 See "gn help switches" for the common command-line switches.
749```
750
RJ Ascani6966efb2020-10-19 16:50:11 -0700751#### **General options**
752
753```
754 --ninja-executable=<string>
755 Can be used to specify the ninja executable to use. This executable will
756 be used as an IDE option to indicate which ninja to use for building. This
757 executable will also be used as part of the gen process for triggering a
RJ Ascani4e260f12020-10-19 17:41:51 -0700758 restat on generated ninja files and for use with --clean-stale.
759
760 --clean-stale
761 This option will cause no longer needed output files to be removed from
762 the build directory, and their records pruned from the ninja build log and
763 dependency database after the ninja build graph has been generated. This
764 option requires a ninja executable of at least version 1.10.0. It can be
765 provided by the --ninja-executable switch. Also see "gn help clean_stale".
RJ Ascani6966efb2020-10-19 16:50:11 -0700766```
767
Brett Wilson796ed472018-07-16 15:11:09 -0700768#### **IDE options**
769
770```
Harley Li0a9affb2020-06-03 10:38:42 -0400771 GN optionally generates files for IDE. Files won't be overwritten if their
772 contents don't change. Possibilities for <ide options>
Brett Wilson796ed472018-07-16 15:11:09 -0700773
774 --ide=<ide_name>
775 Generate files for an IDE. Currently supported values:
776 "eclipse" - Eclipse CDT settings file.
777 "vs" - Visual Studio project/solution files.
Aaron Gablea5bcbd72019-12-30 15:08:52 -0800778 (default Visual Studio version: 2019)
Brett Wilson796ed472018-07-16 15:11:09 -0700779 "vs2013" - Visual Studio 2013 project/solution files.
780 "vs2015" - Visual Studio 2015 project/solution files.
781 "vs2017" - Visual Studio 2017 project/solution files.
Sylvain Defresned46f92f2019-04-03 16:14:29 +0200782 "vs2019" - Visual Studio 2019 project/solution files.
Tyler Mandry4a648092022-02-15 19:47:09 +0000783 "vs2022" - Visual Studio 2022 project/solution files.
Brett Wilson796ed472018-07-16 15:11:09 -0700784 "xcode" - Xcode workspace/solution files.
785 "qtcreator" - QtCreator project files.
786 "json" - JSON file containing target information
787
788 --filters=<path_prefixes>
789 Semicolon-separated list of label patterns used to limit the set of
790 generated projects (see "gn help label_pattern"). Only matching targets
791 and their dependencies will be included in the solution. Only used for
792 Visual Studio, Xcode and JSON.
793```
794
795#### **Visual Studio Flags**
796
797```
798 --sln=<file_name>
799 Override default sln file name ("all"). Solution file is written to the
800 root build directory.
801
802 --no-deps
803 Don't include targets dependencies to the solution. Changes the way how
804 --filters option works. Only directly matching targets are included.
805
806 --winsdk=<sdk_version>
807 Use the specified Windows 10 SDK version to generate project files.
808 As an example, "10.0.15063.0" can be specified to use Creators Update SDK
809 instead of the default one.
810
Shezan Baigc0a2d232021-07-01 00:29:49 -0400811 --ninja-executable=<string>
812 Can be used to specify the ninja executable to use when building.
813
Brett Wilson796ed472018-07-16 15:11:09 -0700814 --ninja-extra-args=<string>
815 This string is passed without any quoting to the ninja invocation
816 command-line. Can be used to configure ninja flags, like "-j".
817```
818
819#### **Xcode Flags**
820
821```
Sylvain Defresnedb55efd2020-06-16 12:23:03 +0200822 --xcode-project=<file_name>
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +0100823 Override default Xcode project file name ("all"). The project file is
Brett Wilson796ed472018-07-16 15:11:09 -0700824 written to the root build directory.
825
Sylvain Defresne7b0690e2020-06-26 15:33:01 +0200826 --xcode-build-system=<value>
827 Configure the build system to use for the Xcode project. Supported
828 values are (default to "legacy"):
829 "legacy" - Legacy Build system
830 "new" - New Build System
831
Sylvain Defresne88830702022-06-20 17:11:49 +0200832 --xcode-configs=<config_name_list>
833 Configure the list of build configuration supported by the generated
834 project. If specified, must be a list of semicolon-separated strings.
835 If ommitted, a single configuration will be used in the generated
836 project derived from the build directory.
837
838 --xcode-config-build-dir=<string>
839 If present, must be a path relative to the source directory. It will
840 default to $root_out_dir if ommitted. The path is assumed to point to
841 the directory where ninja needs to be invoked. This variable can be
842 used to build for multiple configuration / platform / environment from
843 the same generated Xcode project (assuming that the user has created a
844 gn build directory with the correct args.gn for each).
845
846 One useful value is to use Xcode variables such as '${CONFIGURATION}'
847 or '${EFFECTIVE_PLATFORM}'.
848
Sylvain Defresneae474cc2022-06-21 14:44:02 +0200849 --xcode-additional-files-patterns=<pattern_list>
850 If present, must be a list of semicolon-separated file patterns. It
851 will be used to add all files matching the pattern located in the
852 source tree to the project. It can be used to add, e.g. documentation
853 files to the project to allow easily edit them.
854
855 --xcode-additional-files-roots=<path_list>
856 If present, must be a list of semicolon-separated paths. It will be used
857 as roots when looking for additional files to add. If ommitted, defaults
858 to "//".
859
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100860 --ninja-executable=<string>
861 Can be used to specify the ninja executable to use when building.
862
Brett Wilson796ed472018-07-16 15:11:09 -0700863 --ninja-extra-args=<string>
864 This string is passed without any quoting to the ninja invocation
865 command-line. Can be used to configure ninja flags, like "-j".
866
Sylvain Defresnee431b2f2020-11-04 10:49:47 +0100867 --ide-root-target=<target_name>
Brett Wilson796ed472018-07-16 15:11:09 -0700868 Name of the target corresponding to "All" target in Xcode. If unset,
869 "All" invokes ninja without any target and builds everything.
870```
871
872#### **QtCreator Flags**
873
874```
Sylvain Defresnee431b2f2020-11-04 10:49:47 +0100875 --ide-root-target=<target_name>
Brett Wilson796ed472018-07-16 15:11:09 -0700876 Name of the root target for which the QtCreator project will be generated
877 to contain files of it and its dependencies. If unset, the whole build
878 graph will be emitted.
879```
880
881#### **Eclipse IDE Support**
882
883```
884 GN DOES NOT generate Eclipse CDT projects. Instead, it generates a settings
885 file which can be imported into an Eclipse CDT project. The XML file contains
886 a list of include paths and defines. Because GN does not generate a full
887 .cproject definition, it is not possible to properly define includes/defines
888 for each file individually. Instead, one set of includes/defines is generated
889 for the entire project. This works fairly well but may still result in a few
890 indexer issues here and there.
891```
892
893#### **Generic JSON Output**
894
895```
896 Dumps target information to a JSON file and optionally invokes a
897 python script on the generated file. See the comments at the beginning
898 of json_project_writer.cc and desc_builder.cc for an overview of the JSON
899 file format.
900
901 --json-file-name=<json_file_name>
902 Overrides default file name (project.json) of generated JSON file.
903
904 --json-ide-script=<path_to_python_script>
Harley Li0a9affb2020-06-03 10:38:42 -0400905 Executes python script after the JSON file is generated or updated with
906 new content. Path can be project absolute (//), system absolute (/) or
907 relative, in which case the output directory will be base. Path to
908 generated JSON file will be first argument when invoking script.
Brett Wilson796ed472018-07-16 15:11:09 -0700909
910 --json-ide-script-args=<argument>
911 Optional second argument that will passed to executed script.
912```
Julie Hockett11e09912018-07-31 13:15:08 -0700913
914#### **Compilation Database**
915
916```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400917 --export-rust-project
918 Produces a rust-project.json file in the root of the build directory
919 This is used for various tools in the Rust ecosystem allowing for the
920 replay of individual compilations independent of the build system.
921 This is an unstable format and likely to change without warning.
922
Brett Wilson1da84bb2022-09-14 15:35:29 -0700923 --add-export-compile-commands=<label_pattern>
924 Adds an additional label pattern (see "gn help label_pattern") of a
925 target to add to the compilation database. This pattern is appended to any
926 list values specified in the export_compile_commands variable in the
927 .gn file (see "gn help dotfile"). This allows the user to add additional
928 targets to the compilation database that the project doesn't add by default.
929
930 To add more than one value, specify this switch more than once. Each
931 invocation adds an additional label pattern.
932
933 Example:
934 --add-export-compile-commands=//tools:my_tool
935 --add-export-compile-commands="//base/*"
936
Robert Sesek252674d2019-05-10 14:08:52 -0400937 --export-compile-commands[=<target_name1,target_name2...>]
Brett Wilson1da84bb2022-09-14 15:35:29 -0700938 DEPRECATED https://bugs.chromium.org/p/gn/issues/detail?id=302.
939 Please use --add-export-compile-commands for per-user configuration, and
940 the "export_compile_commands" value in the project-level .gn file (see
941 "gn help dotfile") for per-project configuration.
942
943 Overrides the value of the export_compile_commands in the .gn file (see
944 "gn help dotfile") as well as the --add-export-compile-commands switch.
945
946 Unlike the .gn setting, this switch takes a legacy format which is a list
947 of target names that are matched in any directory. For example, "foo" will
948 match:
949 - "//path/to/src:foo"
950 - "//other/path:foo"
951 - "//foo:foo"
Aaron Wood2d9fd622020-12-10 21:28:19 -0800952 and not match:
Brett Wilson1da84bb2022-09-14 15:35:29 -0700953 - "//foo:bar"
Julie Hockett11e09912018-07-31 13:15:08 -0700954```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800955### <a name="cmd_help"></a>**gn help &lt;anything&gt;**
Brett Wilson796ed472018-07-16 15:11:09 -0700956
957```
958 Yo dawg, I heard you like help on your help so I put help on the help in the
959 help.
960
961 You can also use "all" as the parameter to get all help at once.
962```
963
964#### **Switches**
965
966```
967 --markdown
968 Format output in markdown syntax.
969```
970
971#### **Example**
972
973```
974 gn help --markdown all
975 Dump all help to stdout in markdown format.
976```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400977### <a name="cmd_ls"></a>**gn ls &lt;out_dir&gt; [&lt;label_pattern&gt;] [\--default-toolchain] [\--as=...]**
Brett Wilson796ed472018-07-16 15:11:09 -0700978```
979 [--type=...] [--testonly=...]
980
981 Lists all targets matching the given pattern for the given build directory.
982 By default, only targets in the default toolchain will be matched unless a
983 toolchain is explicitly supplied.
984
985 If the label pattern is unspecified, list all targets. The label pattern is
986 not a general regular expression (see "gn help label_pattern"). If you need
987 more complex expressions, pipe the result through grep.
988```
989
990#### **Options**
991
992```
993 --as=(buildfile|label|output)
994 How to print targets.
995
996 buildfile
997 Prints the build files where the given target was declared as
998 file names.
999 label (default)
1000 Prints the label of the target.
1001 output
1002 Prints the first output file for the target relative to the
1003 root build directory.
1004
Robert Sesekd0a6f072020-05-15 11:21:22 -04001005 --default-toolchain
1006 Normally wildcard targets are matched in all toolchains. This
1007 switch makes wildcard labels with no explicit toolchain reference
1008 only match targets in the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07001009
Robert Sesekd0a6f072020-05-15 11:21:22 -04001010 Non-wildcard inputs with no explicit toolchain specification will
1011 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -07001012
1013 --testonly=(true|false)
1014 Restrict outputs to targets with the testonly flag set
1015 accordingly. When unspecified, the target's testonly flags are
1016 ignored.
1017
1018 --type=(action|copy|executable|group|loadable_module|shared_library|
1019 source_set|static_library)
1020 Restrict outputs to targets matching the given type. If
1021 unspecified, no filtering will be performed.
1022```
1023
1024#### **Examples**
1025
1026```
1027 gn ls out/Debug
1028 Lists all targets in the default toolchain.
1029
1030 gn ls out/Debug "//base/*"
1031 Lists all targets in the directory base and all subdirectories.
1032
1033 gn ls out/Debug "//base:*"
1034 Lists all targets defined in //base/BUILD.gn.
1035
1036 gn ls out/Debug //base --as=output
1037 Lists the build output file for //base:base
1038
1039 gn ls out/Debug --type=executable
1040 Lists all executables produced by the build.
1041
1042 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
1043 Builds all targets in //base and all subdirectories.
Brett Wilson796ed472018-07-16 15:11:09 -07001044```
Joe Armstrongb199e542019-03-21 09:40:08 +08001045### <a name="cmd_meta"></a>**gn meta**
1046
Nate Fischer8ed01d32019-01-08 17:32:01 -08001047```
Joe Armstrongb199e542019-03-21 09:40:08 +08001048 gn meta <out_dir> <target>* --data=<key>[,<key>*]* [--walk=<key>[,<key>*]*]
1049 [--rebase=<dest dir>]
Nate Fischer8ed01d32019-01-08 17:32:01 -08001050
1051 Lists collected metaresults of all given targets for the given data key(s),
1052 collecting metadata dependencies as specified by the given walk key(s).
1053
1054 See `gn help generated_file` for more information on the walk.
1055```
1056
1057#### **Arguments**
1058
1059```
1060 <target(s)>
1061 A list of target labels from which to initiate the walk.
1062
1063 --data
Tyler Mandry4a648092022-02-15 19:47:09 +00001064 A comma-separated list of keys from which to extract data. In each target
1065 walked, its metadata scope is checked for the presence of these keys. If
1066 present, the contents of those variable in the scope are appended to the
1067 results list.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001068
1069 --walk (optional)
Tyler Mandry4a648092022-02-15 19:47:09 +00001070 A comma-separated list of keys from which to control the walk. In each
1071 target walked, its metadata scope is checked for the presence of any of
1072 these keys. If present, the contents of those variables is checked to ensure
1073 that it is a label of a valid dependency of the target and then added to the
1074 set of targets to walk. If the empty string ("") is present in any of these
1075 keys, all deps and data_deps are added to the walk set.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001076
1077 --rebase (optional)
1078 A destination directory onto which to rebase any paths found. If set, all
1079 collected metadata will be rebased onto this path. This option will throw errors
1080 if collected metadata is not a list of strings.
1081```
1082
1083#### **Examples**
1084
1085```
1086 gn meta out/Debug "//base/foo" --data=files
1087 Lists collected metaresults for the `files` key in the //base/foo:foo
1088 target and all of its dependency tree.
1089
Tyler Mandry4a648092022-02-15 19:47:09 +00001090 gn meta out/Debug "//base/foo" --data=files,other
Nate Fischer8ed01d32019-01-08 17:32:01 -08001091 Lists collected metaresults for the `files` and `other` keys in the
1092 //base/foo:foo target and all of its dependency tree.
1093
1094 gn meta out/Debug "//base/foo" --data=files --walk=stop
1095 Lists collected metaresults for the `files` key in the //base/foo:foo
1096 target and all of the dependencies listed in the `stop` key (and so on).
1097
1098 gn meta out/Debug "//base/foo" --data=files --rebase="/"
1099 Lists collected metaresults for the `files` key in the //base/foo:foo
1100 target and all of its dependency tree, rebasing the strings in the `files`
1101 key onto the source directory of the target's declaration relative to "/".
1102```
Hector Dearman5233fa12020-09-11 12:18:21 +01001103### <a name="cmd_outputs"></a>**gn outputs &lt;out_dir&gt; &lt;list of target or file names...&gt;**
Brett Wilson74397092020-03-18 16:34:14 -07001104
1105```
1106 Lists the output files corresponding to the given target(s) or file name(s).
1107 There can be multiple outputs because there can be more than one output
1108 generated by a build step, and there can be more than one toolchain matched.
1109 You can also list multiple inputs which will generate a union of all the
1110 outputs from those inputs.
1111
1112 - The input target/file names are relative to the current directory.
1113
1114 - The output file names are relative to the root build directory.
1115
1116 This command is useful for finding a ninja command that will build only a
1117 portion of the build.
1118```
1119
1120#### **Target outputs**
1121
1122```
1123 If the parameter is a target name that includes a toolchain, it will match
1124 only that target in that toolchain. If no toolchain is specified, it will
1125 match all targets with that name in any toolchain.
1126
1127 The result will be the outputs specified by that target which could be a
1128 library, executable, output of an action, a stamp file, etc.
1129```
1130
1131#### **File outputs**
1132
1133```
1134 If the parameter is a file name it will compute the output for that compile
1135 step for all targets in all toolchains that contain that file as a source
1136 file.
1137
1138 If the source is not compiled (e.g. a header or text file), the command will
1139 produce no output.
1140
1141 If the source is listed as an "input" to a binary target or action will
1142 resolve to that target's outputs.
1143```
1144
1145#### **Example**
1146
1147```
1148 gn outputs out/debug some/directory:some_target
1149 Find the outputs of a given target.
1150
1151 gn outputs out/debug src/project/my_file.cc | xargs ninja -C out/debug
1152 Compiles just the given source file in all toolchains it's referenced in.
1153
1154 git diff --name-only | xargs gn outputs out/x64 | xargs ninja -C out/x64
1155 Compiles all files changed in git.
1156```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001157### <a name="cmd_path"></a>**gn path &lt;out_dir&gt; &lt;target_one&gt; &lt;target_two&gt;**
Brett Wilson796ed472018-07-16 15:11:09 -07001158
1159```
1160 Finds paths of dependencies between two targets. Each unique path will be
1161 printed in one group, and groups will be separate by newlines. The two
1162 targets can appear in either order (paths will be found going in either
1163 direction).
1164
1165 By default, a single path will be printed. If there is a path with only
1166 public dependencies, the shortest public path will be printed. Otherwise, the
1167 shortest path using either public or private dependencies will be printed. If
1168 --with-data is specified, data deps will also be considered. If there are
1169 multiple shortest paths, an arbitrary one will be selected.
1170```
1171
1172#### **Interesting paths**
1173
1174```
1175 In a large project, there can be 100's of millions of unique paths between a
1176 very high level and a common low-level target. To make the output more useful
1177 (and terminate in a reasonable time), GN will not revisit sub-paths
1178 previously known to lead to the target.
1179```
1180
1181#### **Options**
1182
1183```
1184 --all
1185 Prints all "interesting" paths found rather than just the first one.
1186 Public paths will be printed first in order of increasing length, followed
1187 by non-public paths in order of increasing length.
1188
1189 --public
1190 Considers only public paths. Can't be used with --with-data.
1191
1192 --with-data
1193 Additionally follows data deps. Without this flag, only public and private
1194 linked deps will be followed. Can't be used with --public.
1195```
1196
1197#### **Example**
1198
1199```
Keir Mierle45611e32019-11-12 11:18:00 -08001200 gn path out/Default //base //gn
Brett Wilson796ed472018-07-16 15:11:09 -07001201```
Joe Armstrongb199e542019-03-21 09:40:08 +08001202### <a name="cmd_refs"></a>**gn refs**
1203
Brett Wilson796ed472018-07-16 15:11:09 -07001204```
Nico Weberad9eab22020-11-15 22:20:28 -05001205 gn refs <out_dir> (<label_pattern>|<label>|<file>|@<response_file>)* [--all]
1206 [--default-toolchain] [--as=...] [--testonly=...] [--type=...]
Brett Wilson796ed472018-07-16 15:11:09 -07001207
1208 Finds reverse dependencies (which targets reference something). The input is
1209 a list containing:
1210
1211 - Target label: The result will be which targets depend on it.
1212
1213 - Config label: The result will be which targets list the given config in
1214 its "configs" or "public_configs" list.
1215
1216 - Label pattern: The result will be which targets depend on any target
1217 matching the given pattern. Patterns will not match configs. These are not
1218 general regular expressions, see "gn help label_pattern" for details.
1219
1220 - File name: The result will be which targets list the given file in its
1221 "inputs", "sources", "public", "data", or "outputs". Any input that does
1222 not contain wildcards and does not match a target or a config will be
1223 treated as a file.
1224
1225 - Response file: If the input starts with an "@", it will be interpreted as
1226 a path to a file containing a list of labels or file names, one per line.
1227 This allows us to handle long lists of inputs without worrying about
1228 command line limits.
1229```
1230
1231#### **Options**
1232
1233```
1234 --all
1235 When used without --tree, will recurse and display all unique
1236 dependencies of the given targets. For example, if the input is a target,
1237 this will output all targets that depend directly or indirectly on the
1238 input. If the input is a file, this will output all targets that depend
1239 directly or indirectly on that file.
1240
1241 When used with --tree, turns off eliding to show a complete tree.
Brett Wilson796ed472018-07-16 15:11:09 -07001242
1243 --as=(buildfile|label|output)
1244 How to print targets.
1245
1246 buildfile
1247 Prints the build files where the given target was declared as
1248 file names.
1249 label (default)
1250 Prints the label of the target.
1251 output
1252 Prints the first output file for the target relative to the
1253 root build directory.
1254
Robert Sesekd0a6f072020-05-15 11:21:22 -04001255 --default-toolchain
1256 Normally wildcard targets are matched in all toolchains. This
1257 switch makes wildcard labels with no explicit toolchain reference
1258 only match targets in the default toolchain.
1259
1260 Non-wildcard inputs with no explicit toolchain specification will
1261 always match only a target in the default toolchain if one exists.
1262
Brett Wilson796ed472018-07-16 15:11:09 -07001263 -q
1264 Quiet. If nothing matches, don't print any output. Without this option, if
1265 there are no matches there will be an informational message printed which
1266 might interfere with scripts processing the output.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001267
Brett Wilson796ed472018-07-16 15:11:09 -07001268 --testonly=(true|false)
1269 Restrict outputs to targets with the testonly flag set
1270 accordingly. When unspecified, the target's testonly flags are
1271 ignored.
1272
1273 --tree
1274 Outputs a reverse dependency tree from the given target. Duplicates will
1275 be elided. Combine with --all to see a full dependency tree.
1276
1277 Tree output can not be used with the filtering or output flags: --as,
1278 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001279
Brett Wilson796ed472018-07-16 15:11:09 -07001280 --type=(action|copy|executable|group|loadable_module|shared_library|
1281 source_set|static_library)
1282 Restrict outputs to targets matching the given type. If
1283 unspecified, no filtering will be performed.
1284```
1285
1286#### **Examples (target input)**
1287
1288```
Keir Mierle45611e32019-11-12 11:18:00 -08001289 gn refs out/Debug //gn:gn
Brett Wilson796ed472018-07-16 15:11:09 -07001290 Find all targets depending on the given exact target name.
1291
1292 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
1293 Edit all .gn files containing references to //base:i18n
1294
1295 gn refs out/Debug //base --all
1296 List all targets depending directly or indirectly on //base:base.
1297
1298 gn refs out/Debug "//base/*"
1299 List all targets depending directly on any target in //base or
1300 its subdirectories.
1301
1302 gn refs out/Debug "//base:*"
1303 List all targets depending directly on any target in
1304 //base/BUILD.gn.
1305
1306 gn refs out/Debug //base --tree
1307 Print a reverse dependency tree of //base:base
1308```
1309
1310#### **Examples (file input)**
1311
1312```
1313 gn refs out/Debug //base/macros.h
1314 Print target(s) listing //base/macros.h as a source.
1315
1316 gn refs out/Debug //base/macros.h --tree
1317 Display a reverse dependency tree to get to the given file. This
1318 will show how dependencies will reference that file.
1319
1320 gn refs out/Debug //base/macros.h //base/at_exit.h --all
1321 Display all unique targets with some dependency path to a target
1322 containing either of the given files as a source.
1323
1324 gn refs out/Debug //base/macros.h --testonly=true --type=executable
1325 --all --as=output
1326 Display the executable file names of all test executables
1327 potentially affected by a change to the given file.
1328```
1329## <a name="targets"></a>Target declarations
1330
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001331### <a name="func_action"></a>**action**: Declare a target that runs a script a single time.
Brett Wilson796ed472018-07-16 15:11:09 -07001332
1333```
1334 This target type allows you to run a script a single time to produce one or
1335 more output files. If you want to run a script once for each of a set of
1336 input files, see "gn help action_foreach".
1337```
1338
1339#### **Inputs**
1340
1341```
1342 In an action the "sources" and "inputs" are treated the same: they're both
1343 input dependencies on script execution with no special handling. If you want
1344 to pass the sources to your script, you must do so explicitly by including
1345 them in the "args". Note also that this means there is no special handling of
1346 paths since GN doesn't know which of the args are paths and not. You will
1347 want to use rebase_path() to convert paths to be relative to the
1348 root_build_dir.
1349
1350 You can dynamically write input dependencies (for incremental rebuilds if an
1351 input file changes) by writing a depfile when the script is run (see "gn help
1352 depfile"). This is more flexible than "inputs".
1353
1354 If the command line length is very long, you can use response files to pass
1355 args to your script. See "gn help response_file_contents".
1356
1357 It is recommended you put inputs to your script in the "sources" variable,
1358 and stuff like other Python files required to run your script in the "inputs"
1359 variable.
Julie Hockette2a29402018-07-31 10:11:42 -07001360
Adrian Taylorc0d4f8b2022-03-25 15:05:57 -07001361 Actions can take the configs and public_configs lists, as well as any of the
1362 configs variables (defines, include_dirs, etc.) set directly on the target.
1363 These behave exactly as they would on a binary target and can be accessed
1364 using substitution patterns in the script args (see "gn help args") to
1365 implement custom compiler-like tools.
1366
Brett Wilson796ed472018-07-16 15:11:09 -07001367 The "deps" and "public_deps" for an action will always be
1368 completed before any part of the action is run so it can depend on
1369 the output of previous steps. The "data_deps" will be built if the
1370 action is built, but may not have completed before all steps of the
1371 action are started. This can give additional parallelism in the build
1372 for runtime-only dependencies.
1373```
1374
1375#### **Outputs**
1376
1377```
1378 You should specify files created by your script by specifying them in the
1379 "outputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001380
Brett Wilson796ed472018-07-16 15:11:09 -07001381 The script will be executed with the given arguments with the current
1382 directory being that of the root build directory. If you pass files
1383 to your script, see "gn help rebase_path" for how to convert
1384 file names to be relative to the build directory (file names in the
1385 sources, outputs, and inputs will be all treated as relative to the
1386 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001387
1388 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1389 that Ninja will check the timestamp of the output after the action
1390 completes. If output timestamp is unchanged, the step will be treated
1391 as if it never needed to be rebuilt, potentially eliminating some
1392 downstream steps for incremental builds. Scripts can improve build
1393 performance by taking care not to change the timstamp of the output
1394 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001395```
1396
1397#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001398
Brett Wilson796ed472018-07-16 15:11:09 -07001399```
1400 All output files must be inside the output directory of the build.
1401 You would generally use |$target_out_dir| or |$target_gen_dir| to
1402 reference the output or generated intermediate file directories,
1403 respectively.
1404```
1405
1406#### **Variables**
1407
1408```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001409 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1410 cflags_objcc, defines, include_dirs, inputs, ldflags,
1411 lib_dirs, libs, precompiled_header, precompiled_source,
1412 rustenv, rustflags, swiftflags, testonly
1413 Dependent configs: all_dependent_configs, public_configs
1414 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1415 write_runtime_deps
1416 General: check_includes, configs, data, friend, inputs, metadata,
1417 output_extension, output_name, public, sources, testonly,
1418 visibility
1419 Action variables: args, bridge_header, configs, data, depfile,
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07001420 framework_dirs, inputs, mnemonic, module_deps,
1421 module_name, outputs*, pool, response_file_contents,
1422 script*, sources
Brett Wilson796ed472018-07-16 15:11:09 -07001423 * = required
1424```
1425
1426#### **Example**
1427
1428```
1429 action("run_this_guy_once") {
1430 script = "doprocessing.py"
1431 sources = [ "my_configuration.txt" ]
1432 outputs = [ "$target_gen_dir/insightful_output.txt" ]
1433
1434 # Our script imports this Python file so we want to rebuild if it changes.
1435 inputs = [ "helper_library.py" ]
1436
1437 # Note that we have to manually pass the sources to our script if the
1438 # script needs them as inputs.
1439 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
1440 rebase_path(sources, root_build_dir)
1441 }
1442```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001443### <a name="func_action_foreach"></a>**action_foreach**: Declare a target that runs a script over a set of files.
Brett Wilson796ed472018-07-16 15:11:09 -07001444
1445```
1446 This target type allows you to run a script once-per-file over a set of
1447 sources. If you want to run a script once that takes many files as input, see
1448 "gn help action".
1449```
1450
1451#### **Inputs**
1452
1453```
1454 The script will be run once per file in the "sources" variable. The "outputs"
1455 variable should specify one or more files with a source expansion pattern in
1456 it (see "gn help source_expansion"). The output file(s) for each script
1457 invocation should be unique. Normally you use "{{source_name_part}}" in each
1458 output file.
1459
1460 If your script takes additional data as input, such as a shared configuration
1461 file or a Python module it uses, those files should be listed in the "inputs"
1462 variable. These files are treated as dependencies of each script invocation.
1463
1464 If the command line length is very long, you can use response files to pass
1465 args to your script. See "gn help response_file_contents".
1466
1467 You can dynamically write input dependencies (for incremental rebuilds if an
1468 input file changes) by writing a depfile when the script is run (see "gn help
1469 depfile"). This is more flexible than "inputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001470
Brett Wilson796ed472018-07-16 15:11:09 -07001471 The "deps" and "public_deps" for an action will always be
1472 completed before any part of the action is run so it can depend on
1473 the output of previous steps. The "data_deps" will be built if the
1474 action is built, but may not have completed before all steps of the
1475 action are started. This can give additional parallelism in the build
1476 for runtime-only dependencies.
1477```
1478
1479#### **Outputs**
Julie Hockette2a29402018-07-31 10:11:42 -07001480
Brett Wilson796ed472018-07-16 15:11:09 -07001481```
1482 The script will be executed with the given arguments with the current
1483 directory being that of the root build directory. If you pass files
1484 to your script, see "gn help rebase_path" for how to convert
1485 file names to be relative to the build directory (file names in the
1486 sources, outputs, and inputs will be all treated as relative to the
1487 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001488
1489 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1490 that Ninja will check the timestamp of the output after the action
1491 completes. If output timestamp is unchanged, the step will be treated
1492 as if it never needed to be rebuilt, potentially eliminating some
1493 downstream steps for incremental builds. Scripts can improve build
1494 performance by taking care not to change the timstamp of the output
1495 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001496```
1497
1498#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001499
Brett Wilson796ed472018-07-16 15:11:09 -07001500```
1501 All output files must be inside the output directory of the build.
1502 You would generally use |$target_out_dir| or |$target_gen_dir| to
1503 reference the output or generated intermediate file directories,
1504 respectively.
1505```
1506
1507#### **Variables**
1508
1509```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001510 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1511 cflags_objcc, defines, include_dirs, inputs, ldflags,
1512 lib_dirs, libs, precompiled_header, precompiled_source,
1513 rustenv, rustflags, swiftflags, testonly
1514 Dependent configs: all_dependent_configs, public_configs
1515 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1516 write_runtime_deps
1517 General: check_includes, configs, data, friend, inputs, metadata,
1518 output_extension, output_name, public, sources, testonly,
1519 visibility
1520 Action variables: args, bridge_header, configs, data, depfile,
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07001521 framework_dirs, inputs, mnemonic, module_deps,
1522 module_name, outputs*, pool, response_file_contents,
1523 script*, sources
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001524 * = required
Brett Wilson796ed472018-07-16 15:11:09 -07001525```
1526
1527#### **Example**
1528
1529```
1530 # Runs the script over each IDL file. The IDL script will generate both a .cc
1531 # and a .h file for each input.
1532 action_foreach("my_idl") {
1533 script = "idl_processor.py"
1534 sources = [ "foo.idl", "bar.idl" ]
1535
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07001536 # Causes ninja to output "IDL <label>" rather than the default
1537 # "ACTION <label>" when building this action.
1538 mnemonic = "IDL"
1539
Joe Armstrong73e89072019-05-10 11:23:52 +08001540 # Our script reads this file each time, so we need to list it as a
Brett Wilson796ed472018-07-16 15:11:09 -07001541 # dependency so we can rebuild if it changes.
1542 inputs = [ "my_configuration.txt" ]
1543
1544 # Transformation from source file name to output file names.
1545 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
1546 "$target_gen_dir/{{source_name_part}}.cc" ]
1547
1548 # Note that since "args" is opaque to GN, if you specify paths here, you
1549 # will need to convert it to be relative to the build directory using
1550 # rebase_path().
1551 args = [
1552 "{{source}}",
1553 "-o",
Hector Dearman0d67e272020-12-22 01:37:16 +00001554 rebase_path(target_gen_dir, root_build_dir) +
Brett Wilson796ed472018-07-16 15:11:09 -07001555 "/{{source_name_part}}.h" ]
1556 }
1557```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001558### <a name="func_bundle_data"></a>**bundle_data**: [iOS/macOS] Declare a target without output.
Brett Wilson796ed472018-07-16 15:11:09 -07001559
1560```
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01001561 This target type allows one to declare data that is required at runtime. It is
Brett Wilson796ed472018-07-16 15:11:09 -07001562 used to inform "create_bundle" targets of the files to copy into generated
1563 bundle, see "gn help create_bundle" for help.
1564
1565 The target must define a list of files as "sources" and a single "outputs".
1566 If there are multiple files, source expansions must be used to express the
1567 output. The output must reference a file inside of {{bundle_root_dir}}.
1568
1569 This target can be used on all platforms though it is designed only to
1570 generate iOS/macOS bundle. In cross-platform projects, it is advised to put it
1571 behind iOS/macOS conditionals.
1572
1573 See "gn help create_bundle" for more information.
1574```
1575
1576#### **Variables**
1577
1578```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001579 Dependent configs: all_dependent_configs, public_configs
1580 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1581 write_runtime_deps
1582 General: check_includes, configs, data, friend, inputs, metadata,
1583 output_extension, output_name, public, sources, testonly,
1584 visibility
1585 Bundle-specific: sources*, outputs*
Brett Wilson796ed472018-07-16 15:11:09 -07001586 * = required
1587```
1588
1589#### **Examples**
1590
1591```
1592 bundle_data("icudata") {
1593 sources = [ "sources/data/in/icudtl.dat" ]
1594 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
1595 }
1596
1597 bundle_data("base_unittests_bundle_data]") {
1598 sources = [ "test/data" ]
1599 outputs = [
1600 "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" +
1601 "{{source_file_part}}"
1602 ]
1603 }
1604
1605 bundle_data("material_typography_bundle_data") {
1606 sources = [
1607 "src/MaterialTypography.bundle/Roboto-Bold.ttf",
1608 "src/MaterialTypography.bundle/Roboto-Italic.ttf",
1609 "src/MaterialTypography.bundle/Roboto-Regular.ttf",
1610 "src/MaterialTypography.bundle/Roboto-Thin.ttf",
1611 ]
1612 outputs = [
1613 "{{bundle_resources_dir}}/MaterialTypography.bundle/"
1614 "{{source_file_part}}"
1615 ]
1616 }
1617```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001618### <a name="func_copy"></a>**copy**: Declare a target that copies files.
Brett Wilson796ed472018-07-16 15:11:09 -07001619
1620#### **File name handling**
1621
1622```
1623 All output files must be inside the output directory of the build. You would
1624 generally use |$target_out_dir| or |$target_gen_dir| to reference the output
1625 or generated intermediate file directories, respectively.
1626
1627 Both "sources" and "outputs" must be specified. Sources can include as many
1628 files as you want, but there can only be one item in the outputs list (plural
1629 is used for the name for consistency with other target types).
1630
1631 If there is more than one source file, your output name should specify a
1632 mapping from each source file to an output file name using source expansion
1633 (see "gn help source_expansion"). The placeholders will look like
1634 "{{source_name_part}}", for example.
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001635
1636 If you want to copy the output of a previous build step, the target that
1637 generates the file to copy must be reachable from the deps or public_deps of
1638 the copy target.
1639```
1640
1641#### **Variables**
1642
1643```
1644 Dependent configs: all_dependent_configs, public_configs
1645 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1646 write_runtime_deps
1647 General: check_includes, configs, data, friend, inputs, metadata,
1648 output_extension, output_name, public, sources, testonly,
1649 visibility
1650 Copy variables: sources*, outputs*
1651 * = required
Brett Wilson796ed472018-07-16 15:11:09 -07001652```
1653
1654#### **Examples**
1655
1656```
1657 # Write a rule that copies a checked-in DLL to the output directory.
1658 copy("mydll") {
1659 sources = [ "mydll.dll" ]
1660 outputs = [ "$target_out_dir/mydll.dll" ]
1661 }
1662
1663 # Write a rule to copy several files to the target generated files directory.
1664 copy("myfiles") {
1665 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1666
1667 # Use source expansion to generate output files with the corresponding file
1668 # names in the gen dir. This will just copy each file.
1669 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1670 }
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001671
1672 # Copy the output of a generated executable.
1673 copy("package_melon") {
1674 # This example uses get_label_info() to compute the output directory of the
1675 # dependency. This allows the copy rule to work regardless of the toolchain.
1676 #
1677 # In some cases (particularly actions defined previously in the same file)
1678 # you can use get_target_outputs() to get the input file which can eliminate
1679 # the assumptions about the output file name of the dependency.
1680
1681 input_dir = get_label_info("//src/tools/melon", "root_out_dir");
1682 sources = [ "$input_dir/melon" ]
1683
1684 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1685
1686 # Depend on the target to build the file before copying.
1687 deps = [ "//src/tools/melon" ]
1688 }
Brett Wilson796ed472018-07-16 15:11:09 -07001689```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001690### <a name="func_create_bundle"></a>**create_bundle**: [ios/macOS] Build an iOS or macOS bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07001691
1692```
1693 This target generates an iOS or macOS bundle (which is a directory with a
1694 well-know structure). This target does not define any sources, instead they
1695 are computed from all "bundle_data" target this one depends on transitively
1696 (the recursion stops at "create_bundle" targets).
1697
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001698 The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in
1699 "bundle_data" outputs. The properties are optional but must be defined if any
1700 of the "bundle_data" target use them.
Brett Wilson796ed472018-07-16 15:11:09 -07001701
1702 This target can be used on all platforms though it is designed only to
1703 generate iOS or macOS bundle. In cross-platform projects, it is advised to put
1704 it behind iOS/macOS conditionals.
1705
1706 If a create_bundle is specified as a data_deps for another target, the bundle
1707 is considered a leaf, and its public and private dependencies will not
1708 contribute to any data or data_deps. Required runtime dependencies should be
1709 placed in the bundle. A create_bundle can declare its own explicit data and
1710 data_deps, however.
1711```
1712
1713#### **Code signing**
1714
1715```
1716 Some bundle needs to be code signed as part of the build (on iOS all
1717 application needs to be code signed to run on a device). The code signature
1718 can be configured via the code_signing_script variable.
1719
1720 If set, code_signing_script is the path of a script that invoked after all
1721 files have been moved into the bundle. The script must not change any file in
1722 the bundle, but may add new files.
1723
1724 If code_signing_script is defined, then code_signing_outputs must also be
1725 defined and non-empty to inform when the script needs to be re-run. The
1726 code_signing_args will be passed as is to the script (so path have to be
1727 rebased) and additional inputs may be listed with the variable
1728 code_signing_sources.
1729```
1730
1731#### **Variables**
1732
1733```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001734 Dependent configs: all_dependent_configs, public_configs
1735 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1736 write_runtime_deps
1737 General: check_includes, configs, data, friend, inputs, metadata,
1738 output_extension, output_name, public, sources, testonly,
1739 visibility
1740 Bundle vars: bundle_root_dir, bundle_contents_dir, bundle_resources_dir,
1741 bundle_executable_dir, bundle_deps_filter, product_type,
1742 code_signing_args, code_signing_script, code_signing_sources,
1743 code_signing_outputs, xcode_extra_attributes,
1744 xcode_test_application_name, partial_info_plist
Brett Wilson796ed472018-07-16 15:11:09 -07001745```
1746
1747#### **Example**
1748
1749```
1750 # Defines a template to create an application. On most platform, this is just
1751 # an alias for an "executable" target, but on iOS/macOS, it builds an
1752 # application bundle.
1753 template("app") {
1754 if (!is_ios && !is_mac) {
1755 executable(target_name) {
1756 forward_variables_from(invoker, "*")
1757 }
1758 } else {
1759 app_name = target_name
1760 gen_path = target_gen_dir
1761
1762 action("${app_name}_generate_info_plist") {
1763 script = [ "//build/ios/ios_gen_plist.py" ]
1764 sources = [ "templates/Info.plist" ]
1765 outputs = [ "$gen_path/Info.plist" ]
1766 args = rebase_path(sources, root_build_dir) +
1767 rebase_path(outputs, root_build_dir)
1768 }
1769
1770 bundle_data("${app_name}_bundle_info_plist") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001771 public_deps = [ ":${app_name}_generate_info_plist" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001772 sources = [ "$gen_path/Info.plist" ]
1773 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
1774 }
1775
1776 executable("${app_name}_generate_executable") {
1777 forward_variables_from(invoker, "*", [
1778 "output_name",
1779 "visibility",
1780 ])
1781 output_name =
1782 rebase_path("$gen_path/$app_name", root_build_dir)
1783 }
1784
1785 code_signing =
1786 defined(invoker.code_signing) && invoker.code_signing
1787
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001788 if (!is_ios || !code_signing) {
Brett Wilson796ed472018-07-16 15:11:09 -07001789 bundle_data("${app_name}_bundle_executable") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001790 public_deps = [ ":${app_name}_generate_executable" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001791 sources = [ "$gen_path/$app_name" ]
1792 outputs = [ "{{bundle_executable_dir}}/$app_name" ]
1793 }
1794 }
1795
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001796 create_bundle("$app_name.app") {
Brett Wilson796ed472018-07-16 15:11:09 -07001797 product_type = "com.apple.product-type.application"
1798
1799 if (is_ios) {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001800 bundle_root_dir = "$root_build_dir/$target_name"
Brett Wilson796ed472018-07-16 15:11:09 -07001801 bundle_contents_dir = bundle_root_dir
1802 bundle_resources_dir = bundle_contents_dir
1803 bundle_executable_dir = bundle_contents_dir
Brett Wilson796ed472018-07-16 15:11:09 -07001804
Sylvain Defresne7b0690e2020-06-26 15:33:01 +02001805 xcode_extra_attributes = {
Brett Wilson796ed472018-07-16 15:11:09 -07001806 ONLY_ACTIVE_ARCH = "YES"
1807 DEBUG_INFORMATION_FORMAT = "dwarf"
1808 }
1809 } else {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001810 bundle_root_dir = "$root_build_dir/$target_name"
1811 bundle_contents_dir = "$bundle_root_dir/Contents"
1812 bundle_resources_dir = "$bundle_contents_dir/Resources"
1813 bundle_executable_dir = "$bundle_contents_dir/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07001814 }
1815 deps = [ ":${app_name}_bundle_info_plist" ]
1816 if (is_ios && code_signing) {
1817 deps += [ ":${app_name}_generate_executable" ]
1818 code_signing_script = "//build/config/ios/codesign.py"
1819 code_signing_sources = [
1820 invoker.entitlements_path,
1821 "$target_gen_dir/$app_name",
1822 ]
1823 code_signing_outputs = [
1824 "$bundle_root_dir/$app_name",
1825 "$bundle_root_dir/_CodeSignature/CodeResources",
1826 "$bundle_root_dir/embedded.mobileprovision",
1827 "$target_gen_dir/$app_name.xcent",
1828 ]
1829 code_signing_args = [
1830 "-i=" + ios_code_signing_identity,
1831 "-b=" + rebase_path(
1832 "$target_gen_dir/$app_name", root_build_dir),
1833 "-e=" + rebase_path(
1834 invoker.entitlements_path, root_build_dir),
1835 "-e=" + rebase_path(
1836 "$target_gen_dir/$app_name.xcent", root_build_dir),
1837 rebase_path(bundle_root_dir, root_build_dir),
1838 ]
1839 } else {
1840 deps += [ ":${app_name}_bundle_executable" ]
1841 }
1842 }
1843 }
1844 }
1845```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001846### <a name="func_executable"></a>**executable**: Declare an executable target.
Brett Wilson796ed472018-07-16 15:11:09 -07001847
Julie Hockettce1fa072019-05-07 17:44:37 -07001848#### **Language and compilation**
1849
1850```
1851 The tools and commands used to create this target type will be
1852 determined by the source files in its sources. Targets containing
1853 multiple compiler-incompatible languages are not allowed (e.g. a
1854 target containing both C and C++ sources is acceptable, but a
1855 target containing C and Rust sources is not).
1856```
1857
Brett Wilson796ed472018-07-16 15:11:09 -07001858#### **Variables**
1859
1860```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001861 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1862 cflags_objcc, defines, include_dirs, inputs, ldflags,
1863 lib_dirs, libs, precompiled_header, precompiled_source,
1864 rustenv, rustflags, swiftflags, testonly
1865 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1866 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07001867 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001868 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001869 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08001870 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001871 Rust variables: aliased_deps, crate_root, crate_name
Nate Fischer8ed01d32019-01-08 17:32:01 -08001872```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001873### <a name="func_generated_file"></a>**generated_file**: Declare a generated_file target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001874
1875```
1876 Writes data value(s) to disk on resolution. This target type mirrors some
1877 functionality of the write_file() function, but also provides the ability to
1878 collect metadata from its dependencies on resolution rather than writing out
Keir Mierle45611e32019-11-12 11:18:00 -08001879 at parse time.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001880
1881 The `outputs` variable is required to be a list with a single element,
1882 specifying the intended location of the output file.
1883
1884 The `output_conversion` variable specified the format to write the
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02001885 value. See `gn help io_conversion`.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001886
Tomasz Śniatowski46b572c2021-09-03 21:47:17 +02001887 One of `contents` or `data_keys` must be specified; use of `contents` will
1888 write the contents of that value to file, while use of `data_keys` will
1889 trigger a metadata collection walk based on the dependencies of the target and
1890 the optional values of the `rebase` and `walk_keys` variables. See
Nate Fischer8ed01d32019-01-08 17:32:01 -08001891 `gn help metadata`.
1892
1893 Collected metadata, if specified, will be returned in postorder of
1894 dependencies. See the example for details.
1895```
1896
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001897#### **Variables**
1898
1899```
1900 Dependent configs: all_dependent_configs, public_configs
1901 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1902 write_runtime_deps
1903 General: check_includes, configs, data, friend, inputs, metadata,
1904 output_extension, output_name, public, sources, testonly,
1905 visibility
1906 Generated file: contents, data_keys, rebase, walk_keys, output_conversion
1907```
1908
Nate Fischer8ed01d32019-01-08 17:32:01 -08001909#### **Example (metadata collection)**
1910
1911```
1912 Given the following targets defined in //base/BUILD.gn, where A depends on B
1913 and B depends on C and D:
1914
1915 group("a") {
1916 metadata = {
1917 doom_melon = [ "enable" ]
1918 my_files = [ "foo.cpp" ]
1919
Keir Mierle45611e32019-11-12 11:18:00 -08001920 # Note: this is functionally equivalent to not defining `my_barrier`
1921 # at all in this target's metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001922 my_barrier = [ "" ]
1923 }
1924
1925 deps = [ ":b" ]
1926 }
1927
Julie Hockettd69a9c32019-01-23 14:36:18 -08001928 group("b") {
Nate Fischer8ed01d32019-01-08 17:32:01 -08001929 metadata = {
1930 my_files = [ "bar.cpp" ]
1931 my_barrier = [ ":c" ]
1932 }
1933
1934 deps = [ ":c", ":d" ]
1935 }
1936
1937 group("c") {
1938 metadata = {
1939 doom_melon = [ "disable" ]
1940 my_files = [ "baz.cpp" ]
1941 }
1942 }
1943
1944 group("d") {
1945 metadata = {
1946 my_files = [ "missing.cpp" ]
1947 }
1948 }
1949
1950 If the following generated_file target is defined:
1951
1952 generated_file("my_files_metadata") {
1953 outputs = [ "$root_build_dir/my_files.json" ]
1954 data_keys = [ "my_files" ]
1955
1956 deps = [ "//base:a" ]
1957 }
1958
1959 The following will be written to "$root_build_dir/my_files.json" (less the
1960 comments):
1961 [
1962 "baz.cpp", // from //base:c via //base:b
1963 "missing.cpp" // from //base:d via //base:b
1964 "bar.cpp", // from //base:b via //base:a
1965 "foo.cpp", // from //base:a
1966 ]
1967
1968 Alternatively, as an example of using walk_keys, if the following
1969 generated_file target is defined:
1970
1971 generated_file("my_files_metadata") {
1972 outputs = [ "$root_build_dir/my_files.json" ]
1973 data_keys = [ "my_files" ]
1974 walk_keys = [ "my_barrier" ]
1975
1976 deps = [ "//base:a" ]
1977 }
1978
1979 The following will be written to "$root_build_dir/my_files.json" (again less
1980 the comments):
1981 [
1982 "baz.cpp", // from //base:c via //base:b
1983 "bar.cpp", // from //base:b via //base:a
1984 "foo.cpp", // from //base:a
1985 ]
1986
1987 If `rebase` is used in the following generated_file target:
1988
1989 generated_file("my_files_metadata") {
1990 outputs = [ "$root_build_dir/my_files.json" ]
1991 data_keys = [ "my_files" ]
1992 walk_keys = [ "my_barrier" ]
1993 rebase = root_build_dir
1994
1995 deps = [ "//base:a" ]
1996 }
1997
1998 The following will be written to "$root_build_dir/my_files.json" (again less
1999 the comments) (assuming root_build_dir = "//out"):
2000 [
2001 "../base/baz.cpp", // from //base:c via //base:b
2002 "../base/bar.cpp", // from //base:b via //base:a
2003 "../base/foo.cpp", // from //base:a
2004 ]
2005```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002006### <a name="func_group"></a>**group**: Declare a named group of targets.
Brett Wilson796ed472018-07-16 15:11:09 -07002007
2008```
2009 This target type allows you to create meta-targets that just collect a set of
2010 dependencies into one named target. Groups can additionally specify configs
2011 that apply to their dependents.
2012```
2013
2014#### **Variables**
2015
2016```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002017 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2018 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002019 Dependent configs: all_dependent_configs, public_configs
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002020 General: check_includes, configs, data, friend, inputs, metadata,
2021 output_extension, output_name, public, sources, testonly,
2022 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002023```
2024
2025#### **Example**
2026
2027```
2028 group("all") {
2029 deps = [
2030 "//project:runner",
2031 "//project:unit_tests",
2032 ]
2033 }
2034```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002035### <a name="func_loadable_module"></a>**loadable_module**: Declare a loadable module target.
Brett Wilson796ed472018-07-16 15:11:09 -07002036
2037```
2038 This target type allows you to create an object file that is (and can only
2039 be) loaded and unloaded at runtime.
2040
2041 A loadable module will be specified on the linker line for targets listing
2042 the loadable module in its "deps". If you don't want this (if you don't need
2043 to dynamically load the library at runtime), then you should use a
2044 "shared_library" target type instead.
2045```
2046
Julie Hockettce1fa072019-05-07 17:44:37 -07002047#### **Language and compilation**
2048
2049```
2050 The tools and commands used to create this target type will be
2051 determined by the source files in its sources. Targets containing
2052 multiple compiler-incompatible languages are not allowed (e.g. a
2053 target containing both C and C++ sources is acceptable, but a
2054 target containing C and Rust sources is not).
2055```
2056
Brett Wilson796ed472018-07-16 15:11:09 -07002057#### **Variables**
2058
2059```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002060 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2061 cflags_objcc, defines, include_dirs, inputs, ldflags,
2062 lib_dirs, libs, precompiled_header, precompiled_source,
2063 rustenv, rustflags, swiftflags, testonly
2064 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2065 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002066 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002067 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002068 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002069 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002070 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Julie Hockettce1fa072019-05-07 17:44:37 -07002071```
2072### <a name="func_rust_library"></a>**rust_library**: Declare a Rust library target.
2073
2074```
2075 A Rust library is an archive containing additional rust-c provided metadata.
2076 These are the files produced by the rustc compiler with the `.rlib`
2077 extension, and are the intermediate step for most Rust-based binaries.
2078```
2079
2080#### **Language and compilation**
2081
2082```
2083 The tools and commands used to create this target type will be
2084 determined by the source files in its sources. Targets containing
2085 multiple compiler-incompatible languages are not allowed (e.g. a
2086 target containing both C and C++ sources is acceptable, but a
2087 target containing C and Rust sources is not).
2088```
2089
2090#### **Variables**
2091
2092```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002093 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2094 cflags_objcc, defines, include_dirs, inputs, ldflags,
2095 lib_dirs, libs, precompiled_header, precompiled_source,
2096 rustenv, rustflags, swiftflags, testonly
2097 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2098 write_runtime_deps
Julie Hockettce1fa072019-05-07 17:44:37 -07002099 Dependent configs: all_dependent_configs, public_configs
2100 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002101 output_extension, output_name, public, sources, testonly,
Julie Hockettce1fa072019-05-07 17:44:37 -07002102 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002103 Rust variables: aliased_deps, crate_root, crate_name
Brett Wilson796ed472018-07-16 15:11:09 -07002104```
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002105### <a name="func_rust_proc_macro"></a>**rust_proc_macro**: Declare a Rust procedural macro target.
2106
2107```
2108 A Rust procedural macro allows creating syntax extensions as execution of a
2109 function. They are compiled as dynamic libraries and used by the compiler at
2110 runtime.
2111
2112 Their use is the same as of other Rust libraries, but their build has some
2113 additional restrictions in terms of supported flags.
2114```
2115
2116#### **Language and compilation**
2117
2118```
2119 The tools and commands used to create this target type will be
2120 determined by the source files in its sources. Targets containing
2121 multiple compiler-incompatible languages are not allowed (e.g. a
2122 target containing both C and C++ sources is acceptable, but a
2123 target containing C and Rust sources is not).
2124```
2125
2126#### **Variables**
2127
2128```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002129 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2130 cflags_objcc, defines, include_dirs, inputs, ldflags,
2131 lib_dirs, libs, precompiled_header, precompiled_source,
2132 rustenv, rustflags, swiftflags, testonly
2133 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2134 write_runtime_deps
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002135 Dependent configs: all_dependent_configs, public_configs
2136 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002137 output_extension, output_name, public, sources, testonly,
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002138 visibility
2139 Rust variables: aliased_deps, crate_root, crate_name
2140```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002141### <a name="func_shared_library"></a>**shared_library**: Declare a shared library target.
Brett Wilson796ed472018-07-16 15:11:09 -07002142
2143```
2144 A shared library will be specified on the linker line for targets listing the
2145 shared library in its "deps". If you don't want this (say you dynamically
2146 load the library at runtime), then you should depend on the shared library
2147 via "data_deps" or, on Darwin platforms, use a "loadable_module" target type
2148 instead.
2149```
2150
Julie Hockettce1fa072019-05-07 17:44:37 -07002151#### **Language and compilation**
2152
2153```
2154 The tools and commands used to create this target type will be
2155 determined by the source files in its sources. Targets containing
2156 multiple compiler-incompatible languages are not allowed (e.g. a
2157 target containing both C and C++ sources is acceptable, but a
2158 target containing C and Rust sources is not).
2159```
2160
Brett Wilson796ed472018-07-16 15:11:09 -07002161#### **Variables**
2162
2163```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002164 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2165 cflags_objcc, defines, include_dirs, inputs, ldflags,
2166 lib_dirs, libs, precompiled_header, precompiled_source,
2167 rustenv, rustflags, swiftflags, testonly
2168 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2169 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002170 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002171 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002172 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002173 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002174 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Brett Wilson796ed472018-07-16 15:11:09 -07002175```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002176### <a name="func_source_set"></a>**source_set**: Declare a source set target.
Brett Wilson796ed472018-07-16 15:11:09 -07002177
2178```
Robert Sesekd0a6f072020-05-15 11:21:22 -04002179 Only C-language source sets are supported at the moment.
Julie Hockettce1fa072019-05-07 17:44:37 -07002180```
2181
2182#### **C-language source_sets**
2183
2184```
Brett Wilson796ed472018-07-16 15:11:09 -07002185 A source set is a collection of sources that get compiled, but are not linked
2186 to produce any kind of library. Instead, the resulting object files are
2187 implicitly added to the linker line of all targets that depend on the source
2188 set.
2189
2190 In most cases, a source set will behave like a static library, except no
2191 actual library file will be produced. This will make the build go a little
2192 faster by skipping creation of a large static library, while maintaining the
2193 organizational benefits of focused build targets.
2194
2195 The main difference between a source set and a static library is around
2196 handling of exported symbols. Most linkers assume declaring a function
2197 exported means exported from the static library. The linker can then do dead
2198 code elimination to delete code not reachable from exported functions.
2199
2200 A source set will not do this code elimination since there is no link step.
Julie Hockette2a29402018-07-31 10:11:42 -07002201 This allows you to link many source sets into a shared library and have the
Brett Wilson796ed472018-07-16 15:11:09 -07002202 "exported symbol" notation indicate "export from the final shared library and
2203 not from the intermediate targets." There is no way to express this concept
2204 when linking multiple static libraries into a shared library.
2205```
2206
2207#### **Variables**
2208
2209```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002210 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2211 cflags_objcc, defines, include_dirs, inputs, ldflags,
2212 lib_dirs, libs, precompiled_header, precompiled_source,
2213 rustenv, rustflags, swiftflags, testonly
2214 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2215 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002216 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002217 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002218 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002219 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002220```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002221### <a name="func_static_library"></a>**static_library**: Declare a static library target.
Brett Wilson796ed472018-07-16 15:11:09 -07002222
2223```
2224 Make a ".a" / ".lib" file.
2225
2226 If you only need the static library for intermediate results in the build,
2227 you should consider a source_set instead since it will skip the (potentially
2228 slow) step of creating the intermediate library file.
2229```
2230
2231#### **Variables**
2232
2233```
2234 complete_static_lib
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002235 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2236 cflags_objcc, defines, include_dirs, inputs, ldflags,
2237 lib_dirs, libs, precompiled_header, precompiled_source,
2238 rustenv, rustflags, swiftflags, testonly
2239 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2240 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002241 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002242 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002243 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002244 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002245 Rust variables: aliased_deps, crate_root, crate_name
Julie Hockettce1fa072019-05-07 17:44:37 -07002246
2247 The tools and commands used to create this target type will be
2248 determined by the source files in its sources. Targets containing
2249 multiple compiler-incompatible languages are not allowed (e.g. a
2250 target containing both C and C++ sources is acceptable, but a
2251 target containing C and Rust sources is not).
Brett Wilson796ed472018-07-16 15:11:09 -07002252```
Antonio Sartoriecaaf4b2022-07-01 05:57:08 +00002253### <a name="func_target"></a>**target**: Declare a target with the given programmatic type.
Brett Wilson796ed472018-07-16 15:11:09 -07002254
2255```
2256 target(target_type_string, target_name_string) { ... }
2257
2258 The target() function is a way to invoke a built-in target or template with a
2259 type determined at runtime. This is useful for cases where the type of a
2260 target might not be known statically.
2261
2262 Only templates and built-in target functions are supported for the
2263 target_type_string parameter. Arbitrary functions, configs, and toolchains
2264 are not supported.
2265
2266 The call:
2267 target("source_set", "doom_melon") {
2268 Is equivalent to:
2269 source_set("doom_melon") {
2270```
2271
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002272#### **Common target variables**
2273
2274```
2275 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2276 write_runtime_deps
2277 Dependent configs: all_dependent_configs, public_configs
2278 General: check_includes, configs, data, friend, inputs, metadata,
2279 output_extension, output_name, public, sources, testonly,
2280 visibility
2281
2282 Targets will also have variables specific to that type, see "gn help <type>"
2283 for more.
2284```
2285
Brett Wilson796ed472018-07-16 15:11:09 -07002286#### **Example**
2287
2288```
2289 if (foo_build_as_shared) {
2290 my_type = "shared_library"
2291 } else {
2292 my_type = "source_set"
2293 }
2294
2295 target(my_type, "foo") {
2296 ...
2297 }
2298```
2299## <a name="functions"></a>Buildfile functions
2300
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002301### <a name="func_assert"></a>**assert**: Assert an expression is true at generation time.
Brett Wilson796ed472018-07-16 15:11:09 -07002302
2303```
2304 assert(<condition> [, <error string>])
2305
2306 If the condition is false, the build will fail with an error. If the
2307 optional second argument is provided, that string will be printed
2308 with the error message.
2309```
2310
2311#### **Examples**
2312
2313```
2314 assert(is_win)
2315 assert(defined(sources), "Sources must be defined");
2316```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002317### <a name="func_config"></a>**config**: Defines a configuration object.
Brett Wilson796ed472018-07-16 15:11:09 -07002318
2319```
2320 Configuration objects can be applied to targets and specify sets of compiler
2321 flags, includes, defines, etc. They provide a way to conveniently group sets
2322 of this configuration information.
2323
2324 A config is referenced by its label just like a target.
2325
2326 The values in a config are additive only. If you want to remove a flag you
2327 need to remove the corresponding config that sets it. The final set of flags,
2328 defines, etc. for a target is generated in this order:
2329
Nico Weber693f9fb2021-10-08 15:33:57 -04002330 1. The values specified directly on the target (rather than using a config).
Brett Wilson796ed472018-07-16 15:11:09 -07002331 2. The configs specified in the target's "configs" list, in order.
2332 3. Public_configs from a breadth-first traversal of the dependency tree in
2333 the order that the targets appear in "deps".
2334 4. All dependent configs from a breadth-first traversal of the dependency
2335 tree in the order that the targets appear in "deps".
2336```
2337
Joe Armstrong39413942019-03-15 10:34:03 +08002338#### **More background**
2339
2340```
2341 Configs solve a problem where the build system needs to have a higher-level
2342 understanding of various compiler settings. For example, some compiler flags
2343 have to appear in a certain order relative to each other, some settings like
2344 defines and flags logically go together, and the build system needs to
2345 de-duplicate flags even though raw command-line parameters can't always be
2346 operated on in that way.
2347
2348 The config gives a name to a group of settings that can then be reasoned
2349 about by GN. GN can know that configs with the same label are the same thing
2350 so can be de-duplicated. It allows related settings to be grouped so they
2351 are added or removed as a unit. And it allows targets to refer to settings
2352 with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than
2353 having to hard-coding every compiler's flags each time they are referred to.
2354```
2355
Brett Wilson796ed472018-07-16 15:11:09 -07002356#### **Variables valid in a config definition**
Joe Armstrong39413942019-03-15 10:34:03 +08002357
Brett Wilson796ed472018-07-16 15:11:09 -07002358```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002359 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2360 cflags_objcc, defines, include_dirs, inputs, ldflags,
2361 lib_dirs, libs, precompiled_header, precompiled_source,
2362 rustenv, rustflags, swiftflags, testonly
Brett Wilson796ed472018-07-16 15:11:09 -07002363 Nested configs: configs
Tomasz Śniatowski46b572c2021-09-03 21:47:17 +02002364 General: visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002365```
2366
2367#### **Variables on a target used to apply configs**
2368
2369```
2370 all_dependent_configs, configs, public_configs
2371```
2372
2373#### **Example**
2374
2375```
2376 config("myconfig") {
Joe Armstrongf5ad8072019-05-08 08:42:56 +08002377 include_dirs = [ "include/common" ]
Brett Wilson796ed472018-07-16 15:11:09 -07002378 defines = [ "ENABLE_DOOM_MELON" ]
2379 }
2380
2381 executable("mything") {
2382 configs = [ ":myconfig" ]
2383 }
2384```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002385### <a name="func_declare_args"></a>**declare_args**: Declare build arguments.
Brett Wilson796ed472018-07-16 15:11:09 -07002386
2387```
2388 Introduces the given arguments into the current scope. If they are not
2389 specified on the command line or in a toolchain's arguments, the default
2390 values given in the declare_args block will be used. However, these defaults
2391 will not override command-line values.
2392
2393 See also "gn help buildargs" for an overview.
2394
2395 The precise behavior of declare args is:
2396
2397 1. The declare_args() block executes. Any variable defined in the enclosing
2398 scope is available for reading, but any variable defined earlier in
2399 the current scope is not (since the overrides haven't been applied yet).
2400
2401 2. At the end of executing the block, any variables set within that scope
Petr Hosek7617b592019-10-02 09:52:21 -07002402 are saved, with the values specified in the block used as the "default value"
2403 for that argument. Once saved, these variables are available for override
2404 via args.gn.
Brett Wilson796ed472018-07-16 15:11:09 -07002405
2406 3. User-defined overrides are applied. Anything set in "gn args" now
2407 overrides any default values. The resulting set of variables is promoted
2408 to be readable from the following code in the file.
2409
2410 This has some ramifications that may not be obvious:
2411
2412 - You should not perform difficult work inside a declare_args block since
2413 this only sets a default value that may be discarded. In particular,
2414 don't use the result of exec_script() to set the default value. If you
2415 want to have a script-defined default, set some default "undefined" value
2416 like [], "", or -1, and after the declare_args block, call exec_script if
2417 the value is unset by the user.
2418
2419 - Because you cannot read the value of a variable defined in the same
2420 block, if you need to make the default value of one arg depend
2421 on the possibly-overridden value of another, write two separate
2422 declare_args() blocks:
2423
2424 declare_args() {
2425 enable_foo = true
2426 }
2427 declare_args() {
2428 # Bar defaults to same user-overridden state as foo.
2429 enable_bar = enable_foo
2430 }
2431```
2432
2433#### **Example**
2434
2435```
2436 declare_args() {
2437 enable_teleporter = true
2438 enable_doom_melon = false
2439 }
2440
2441 If you want to override the (default disabled) Doom Melon:
2442 gn --args="enable_doom_melon=true enable_teleporter=true"
2443 This also sets the teleporter, but it's already defaulted to on so it will
2444 have no effect.
2445```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002446### <a name="func_defined"></a>**defined**: Returns whether an identifier is defined.
Brett Wilson796ed472018-07-16 15:11:09 -07002447
2448```
2449 Returns true if the given argument is defined. This is most useful in
2450 templates to assert that the caller set things up properly.
2451
2452 You can pass an identifier:
2453 defined(foo)
2454 which will return true or false depending on whether foo is defined in the
2455 current scope.
2456
2457 You can also check a named scope:
2458 defined(foo.bar)
2459 which will return true or false depending on whether bar is defined in the
2460 named scope foo. It will throw an error if foo is not defined or is not a
2461 scope.
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07002462
2463 You can also check a named scope using a subscript string expression:
2464 defined(foo[bar + "_name"])
2465 Which will return true or false depending on whether the subscript
2466 expression expands to the name of a member of the scope foo. It will
2467 throw an error if foo is not defined or is not a scope, or if the
2468 expression does not expand to a string, or if it is an empty string.
Brett Wilson796ed472018-07-16 15:11:09 -07002469```
2470
2471#### **Example**
2472
2473```
2474 template("mytemplate") {
2475 # To help users call this template properly...
2476 assert(defined(invoker.sources), "Sources must be defined")
2477
2478 # If we want to accept an optional "values" argument, we don't
2479 # want to dereference something that may not be defined.
2480 if (defined(invoker.values)) {
2481 values = invoker.values
2482 } else {
2483 values = "some default value"
2484 }
2485 }
2486```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002487### <a name="func_exec_script"></a>**exec_script**: Synchronously run a script and return the output.
Brett Wilson796ed472018-07-16 15:11:09 -07002488
2489```
2490 exec_script(filename,
2491 arguments = [],
2492 input_conversion = "",
2493 file_dependencies = [])
2494
2495 Runs the given script, returning the stdout of the script. The build
2496 generation will fail if the script does not exist or returns a nonzero exit
2497 code.
2498
2499 The current directory when executing the script will be the root build
2500 directory. If you are passing file names, you will want to use the
2501 rebase_path() function to make file names relative to this path (see "gn help
2502 rebase_path").
Gary Miguelce7fa362018-09-17 12:48:17 -07002503
2504 The default script interpreter is Python ("python" on POSIX, "python.exe" or
2505 "python.bat" on Windows). This can be configured by the script_executable
2506 variable, see "gn help dotfile".
Brett Wilson796ed472018-07-16 15:11:09 -07002507```
2508
2509#### **Arguments**:
2510
2511```
2512 filename:
Gary Miguelce7fa362018-09-17 12:48:17 -07002513 File name of script to execute. Non-absolute names will be treated as
2514 relative to the current build file.
Brett Wilson796ed472018-07-16 15:11:09 -07002515
2516 arguments:
2517 A list of strings to be passed to the script as arguments. May be
2518 unspecified or the empty list which means no arguments.
2519
2520 input_conversion:
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02002521 Controls how the file is read and parsed. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07002522
2523 If unspecified, defaults to the empty string which causes the script
2524 result to be discarded. exec script will return None.
2525
2526 dependencies:
2527 (Optional) A list of files that this script reads or otherwise depends
2528 on. These dependencies will be added to the build result such that if any
2529 of them change, the build will be regenerated and the script will be
2530 re-run.
2531
2532 The script itself will be an implicit dependency so you do not need to
2533 list it.
2534```
2535
2536#### **Example**
2537
2538```
2539 all_lines = exec_script(
2540 "myscript.py", [some_input], "list lines",
2541 [ rebase_path("data_file.txt", root_build_dir) ])
2542
2543 # This example just calls the script with no arguments and discards the
2544 # result.
2545 exec_script("//foo/bar/myscript.py")
2546```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01002547### <a name="func_filter_exclude"></a>**filter_exclude**: Remove values that match a set of patterns.
2548
2549```
2550 filter_exclude(values, exclude_patterns)
2551
2552 The argument values must be a list of strings.
2553
2554 The argument exclude_patterns must be a list of file patterns (see
2555 "gn help file_pattern"). Any elements in values matching at least one
2556 of those patterns will be excluded.
2557```
2558
2559#### **Examples**
2560```
2561 values = [ "foo.cc", "foo.h", "foo.proto" ]
2562 result = filter_exclude(values, [ "*.proto" ])
2563 # result will be [ "foo.cc", "foo.h" ]
2564```
2565### <a name="func_filter_include"></a>**filter_include**: Remove values that do not match a set of patterns.
2566
2567```
2568 filter_include(values, include_patterns)
2569
2570 The argument values must be a list of strings.
2571
2572 The argument include_patterns must be a list of file patterns (see
2573 "gn help file_pattern"). Only elements from values matching at least
2574 one of the pattern will be included.
2575```
2576
2577#### **Examples**
2578```
2579 values = [ "foo.cc", "foo.h", "foo.proto" ]
2580 result = filter_include(values, [ "*.proto" ])
2581 # result will be [ "foo.proto" ]
2582```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002583### <a name="func_foreach"></a>**foreach**: Iterate over a list.
Brett Wilson796ed472018-07-16 15:11:09 -07002584
2585```
2586 foreach(<loop_var>, <list>) {
2587 <loop contents>
2588 }
2589
2590 Executes the loop contents block over each item in the list, assigning the
2591 loop_var to each item in sequence. The <loop_var> will be a copy so assigning
2592 to it will not mutate the list. The loop will iterate over a copy of <list>
2593 so mutating it inside the loop will not affect iteration.
2594
2595 The block does not introduce a new scope, so that variable assignments inside
2596 the loop will be visible once the loop terminates.
2597
2598 The loop variable will temporarily shadow any existing variables with the
2599 same name for the duration of the loop. After the loop terminates the loop
2600 variable will no longer be in scope, and the previous value (if any) will be
2601 restored.
2602```
2603
2604#### **Example**
2605
2606```
2607 mylist = [ "a", "b", "c" ]
2608 foreach(i, mylist) {
2609 print(i)
2610 }
2611
2612 Prints:
2613 a
2614 b
2615 c
2616```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002617### <a name="func_forward_variables_from"></a>**forward_variables_from**: Copies variables from a different scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002618
2619```
2620 forward_variables_from(from_scope, variable_list_or_star,
2621 variable_to_not_forward_list = [])
2622
2623 Copies the given variables from the given scope to the local scope if they
2624 exist. This is normally used in the context of templates to use the values of
2625 variables defined in the template invocation to a template-defined target.
2626
2627 The variables in the given variable_list will be copied if they exist in the
2628 given scope or any enclosing scope. If they do not exist, nothing will happen
2629 and they be left undefined in the current scope.
2630
2631 As a special case, if the variable_list is a string with the value of "*",
2632 all variables from the given scope will be copied. "*" only copies variables
2633 set directly on the from_scope, not enclosing ones. Otherwise it would
2634 duplicate all global variables.
2635
2636 When an explicit list of variables is supplied, if the variable exists in the
2637 current (destination) scope already, an error will be thrown. If "*" is
2638 specified, variables in the current scope will be clobbered (the latter is
2639 important because most targets have an implicit configs list, which means it
2640 wouldn't work at all if it didn't clobber).
2641
Brett Wilson796ed472018-07-16 15:11:09 -07002642 If variables_to_not_forward_list is non-empty, then it must contains a list
2643 of variable names that will not be forwarded. This is mostly useful when
2644 variable_list_or_star has a value of "*".
2645```
2646
2647#### **Examples**
2648
2649```
Andrew Grieve57a964c2018-09-14 10:50:56 -04002650 # forward_variables_from(invoker, ["foo"])
2651 # is equivalent to:
2652 assert(!defined(foo))
2653 if (defined(invoker.foo)) {
2654 foo = invoker.foo
2655 }
2656
Brett Wilson796ed472018-07-16 15:11:09 -07002657 # This is a common action template. It would invoke a script with some given
2658 # parameters, and wants to use the various types of deps and the visibility
2659 # from the invoker if it's defined. It also injects an additional dependency
2660 # to all targets.
2661 template("my_test") {
2662 action(target_name) {
2663 forward_variables_from(invoker, [ "data_deps", "deps",
Julie Hockette2a29402018-07-31 10:11:42 -07002664 "public_deps", "visibility"])
Brett Wilson796ed472018-07-16 15:11:09 -07002665 # Add our test code to the dependencies.
2666 # "deps" may or may not be defined at this point.
2667 if (defined(deps)) {
2668 deps += [ "//tools/doom_melon" ]
2669 } else {
2670 deps = [ "//tools/doom_melon" ]
2671 }
2672 }
2673 }
2674
Julie Hockette2a29402018-07-31 10:11:42 -07002675 # This is a template around a target whose type depends on a global variable.
2676 # It forwards all values from the invoker.
Brett Wilson796ed472018-07-16 15:11:09 -07002677 template("my_wrapper") {
2678 target(my_wrapper_target_type, target_name) {
2679 forward_variables_from(invoker, "*")
2680 }
2681 }
2682
2683 # A template that wraps another. It adds behavior based on one
2684 # variable, and forwards all others to the nested target.
2685 template("my_ios_test_app") {
2686 ios_test_app(target_name) {
2687 forward_variables_from(invoker, "*", ["test_bundle_name"])
2688 if (!defined(extra_substitutions)) {
2689 extra_substitutions = []
2690 }
2691 extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ]
2692 }
2693 }
2694```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002695### <a name="func_get_label_info"></a>**get_label_info**: Get an attribute from a target's label.
Brett Wilson796ed472018-07-16 15:11:09 -07002696
2697```
2698 get_label_info(target_label, what)
2699
2700 Given the label of a target, returns some attribute of that target. The
2701 target need not have been previously defined in the same file, since none of
2702 the attributes depend on the actual target definition, only the label itself.
2703
2704 See also "gn help get_target_outputs".
2705```
2706
2707#### **Possible values for the "what" parameter**
2708
2709```
2710 "name"
2711 The short name of the target. This will match the value of the
2712 "target_name" variable inside that target's declaration. For the label
2713 "//foo/bar:baz" this will return "baz".
2714
2715 "dir"
2716 The directory containing the target's definition, with no slash at the
2717 end. For the label "//foo/bar:baz" this will return "//foo/bar".
2718
2719 "target_gen_dir"
2720 The generated file directory for the target. This will match the value of
2721 the "target_gen_dir" variable when inside that target's declaration.
2722
2723 "root_gen_dir"
2724 The root of the generated file tree for the target. This will match the
2725 value of the "root_gen_dir" variable when inside that target's
2726 declaration.
2727
2728 "target_out_dir
2729 The output directory for the target. This will match the value of the
2730 "target_out_dir" variable when inside that target's declaration.
2731
2732 "root_out_dir"
2733 The root of the output file tree for the target. This will match the
2734 value of the "root_out_dir" variable when inside that target's
2735 declaration.
2736
2737 "label_no_toolchain"
2738 The fully qualified version of this label, not including the toolchain.
2739 For the input ":bar" it might return "//foo:bar".
2740
2741 "label_with_toolchain"
2742 The fully qualified version of this label, including the toolchain. For
2743 the input ":bar" it might return "//foo:bar(//toolchain:x64)".
2744
2745 "toolchain"
2746 The label of the toolchain. This will match the value of the
2747 "current_toolchain" variable when inside that target's declaration.
2748```
2749
2750#### **Examples**
2751
2752```
2753 get_label_info(":foo", "name")
2754 # Returns string "foo".
2755
2756 get_label_info("//foo/bar:baz", "target_gen_dir")
2757 # Returns string "//out/Debug/gen/foo/bar".
2758```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002759### <a name="func_get_path_info"></a>**get_path_info**: Extract parts of a file or directory name.
Brett Wilson796ed472018-07-16 15:11:09 -07002760
2761```
2762 get_path_info(input, what)
2763
2764 The first argument is either a string representing a file or directory name,
2765 or a list of such strings. If the input is a list the return value will be a
2766 list containing the result of applying the rule to each item in the input.
2767```
2768
2769#### **Possible values for the "what" parameter**
2770
2771```
2772 "file"
2773 The substring after the last slash in the path, including the name and
2774 extension. If the input ends in a slash, the empty string will be
2775 returned.
2776 "foo/bar.txt" => "bar.txt"
2777 "bar.txt" => "bar.txt"
2778 "foo/" => ""
2779 "" => ""
2780
2781 "name"
2782 The substring of the file name not including the extension.
2783 "foo/bar.txt" => "bar"
2784 "foo/bar" => "bar"
2785 "foo/" => ""
2786
2787 "extension"
2788 The substring following the last period following the last slash, or the
2789 empty string if not found. The period is not included.
2790 "foo/bar.txt" => "txt"
2791 "foo/bar" => ""
2792
2793 "dir"
2794 The directory portion of the name, not including the slash.
2795 "foo/bar.txt" => "foo"
2796 "//foo/bar" => "//foo"
2797 "foo" => "."
2798
2799 The result will never end in a slash, so if the resulting is empty, the
2800 system ("/") or source ("//") roots, a "." will be appended such that it
2801 is always legal to append a slash and a filename and get a valid path.
2802
2803 "out_dir"
2804 The output file directory corresponding to the path of the given file,
2805 not including a trailing slash.
2806 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
2807
2808 "gen_dir"
2809 The generated file directory corresponding to the path of the given file,
2810 not including a trailing slash.
2811 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
2812
2813 "abspath"
2814 The full absolute path name to the file or directory. It will be resolved
2815 relative to the current directory, and then the source- absolute version
2816 will be returned. If the input is system- absolute, the same input will
2817 be returned.
2818 "foo/bar.txt" => "//mydir/foo/bar.txt"
2819 "foo/" => "//mydir/foo/"
2820 "//foo/bar" => "//foo/bar" (already absolute)
2821 "/usr/include" => "/usr/include" (already absolute)
2822
2823 If you want to make the path relative to another directory, or to be
2824 system-absolute, see rebase_path().
2825```
2826
2827#### **Examples**
2828```
2829 sources = [ "foo.cc", "foo.h" ]
2830 result = get_path_info(source, "abspath")
2831 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
2832
2833 result = get_path_info("//foo/bar/baz.cc", "dir")
2834 # result will be "//foo/bar"
2835
2836 # Extract the source-absolute directory name,
Joe Armstrong39413942019-03-15 10:34:03 +08002837 result = get_path_info(get_path_info(path, "dir"), "abspath")
Brett Wilson796ed472018-07-16 15:11:09 -07002838```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002839### <a name="func_get_target_outputs"></a>**get_target_outputs**: [file list] Get the list of outputs from a target.
Brett Wilson796ed472018-07-16 15:11:09 -07002840
2841```
2842 get_target_outputs(target_label)
2843
2844 Returns a list of output files for the named target. The named target must
2845 have been previously defined in the current file before this function is
2846 called (it can't reference targets in other files because there isn't a
2847 defined execution order, and it obviously can't reference targets that are
2848 defined after the function call).
2849
Julie Hockettce1fa072019-05-07 17:44:37 -07002850 Only copy, generated_file, and action targets are supported. The outputs from
2851 binary targets will depend on the toolchain definition which won't
2852 necessarily have been loaded by the time a given line of code has run, and
2853 source sets and groups have no useful output file.
Brett Wilson796ed472018-07-16 15:11:09 -07002854```
2855
2856#### **Return value**
2857
2858```
2859 The names in the resulting list will be absolute file paths (normally like
2860 "//out/Debug/bar.exe", depending on the build directory).
2861
Julie Hockettce1fa072019-05-07 17:44:37 -07002862 action, copy, and generated_file targets: this will just return the files
2863 specified in the "outputs" variable of the target.
Brett Wilson796ed472018-07-16 15:11:09 -07002864
2865 action_foreach targets: this will return the result of applying the output
2866 template to the sources (see "gn help source_expansion"). This will be the
2867 same result (though with guaranteed absolute file paths), as
2868 process_file_template will return for those inputs (see "gn help
2869 process_file_template").
2870
Brett Wilson796ed472018-07-16 15:11:09 -07002871 source sets and groups: this will return a list containing the path of the
RJ Ascani843b5002020-11-04 14:49:23 -08002872 "stamp" file that Ninja will produce once all outputs are generated. This
Brett Wilson796ed472018-07-16 15:11:09 -07002873 probably isn't very useful.
2874```
2875
2876#### **Example**
2877
2878```
2879 # Say this action generates a bunch of C source files.
2880 action_foreach("my_action") {
2881 sources = [ ... ]
2882 outputs = [ ... ]
2883 }
2884
2885 # Compile the resulting source files into a source set.
2886 source_set("my_lib") {
2887 sources = get_target_outputs(":my_action")
2888 }
2889```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002890### <a name="func_getenv"></a>**getenv**: Get an environment variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002891
2892```
2893 value = getenv(env_var_name)
2894
2895 Returns the value of the given environment variable. If the value is not
2896 found, it will try to look up the variable with the "opposite" case (based on
2897 the case of the first letter of the variable), but is otherwise
2898 case-sensitive.
2899
2900 If the environment variable is not found, the empty string will be returned.
2901 Note: it might be nice to extend this if we had the concept of "none" in the
2902 language to indicate lookup failure.
2903```
2904
2905#### **Example**
2906
2907```
2908 home_dir = getenv("HOME")
2909```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002910### <a name="func_import"></a>**import**: Import a file into the current scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002911
2912```
2913 The import command loads the rules and variables resulting from executing the
2914 given file into the current scope.
2915
2916 By convention, imported files are named with a .gni extension.
2917
2918 An import is different than a C++ "include". The imported file is executed in
2919 a standalone environment from the caller of the import command. The results
2920 of this execution are cached for other files that import the same .gni file.
2921
2922 Note that you can not import a BUILD.gn file that's otherwise used in the
2923 build. Files must either be imported or implicitly loaded as a result of deps
2924 rules, but not both.
2925
2926 The imported file's scope will be merged with the scope at the point import
2927 was called. If there is a conflict (both the current scope and the imported
2928 file define some variable or rule with the same name but different value), a
2929 runtime error will be thrown. Therefore, it's good practice to minimize the
2930 stuff that an imported file defines.
2931
2932 Variables and templates beginning with an underscore '_' are considered
2933 private and will not be imported. Imported files can use such variables for
2934 internal computation without affecting other files.
2935```
2936
2937#### **Examples**
2938
2939```
2940 import("//build/rules/idl_compilation_rule.gni")
2941
2942 # Looks in the current directory.
2943 import("my_vars.gni")
2944```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002945### <a name="func_not_needed"></a>**not_needed**: Mark variables from scope as not needed.
Brett Wilson796ed472018-07-16 15:11:09 -07002946
2947```
2948 not_needed(variable_list_or_star, variable_to_ignore_list = [])
2949 not_needed(from_scope, variable_list_or_star,
2950 variable_to_ignore_list = [])
2951
2952 Mark the variables in the current or given scope as not needed, which means
2953 you will not get an error about unused variables for these. The
2954 variable_to_ignore_list allows excluding variables from "all matches" if
2955 variable_list_or_star is "*".
2956```
2957
2958#### **Example**
2959
2960```
2961 not_needed("*", [ "config" ])
2962 not_needed([ "data_deps", "deps" ])
2963 not_needed(invoker, "*", [ "config" ])
2964 not_needed(invoker, [ "data_deps", "deps" ])
2965```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002966### <a name="func_pool"></a>**pool**: Defines a pool object.
Brett Wilson796ed472018-07-16 15:11:09 -07002967
2968```
2969 Pool objects can be applied to a tool to limit the parallelism of the
2970 build. This object has a single property "depth" corresponding to
2971 the number of tasks that may run simultaneously.
2972
2973 As the file containing the pool definition may be executed in the
2974 context of more than one toolchain it is recommended to specify an
2975 explicit toolchain when defining and referencing a pool.
2976
Julie Hockette2a29402018-07-31 10:11:42 -07002977 A pool named "console" defined in the root build file represents Ninja's
2978 console pool. Targets using this pool will have access to the console's
2979 stdin and stdout, and output will not be buffered. This special pool must
2980 have a depth of 1. Pools not defined in the root must not be named "console".
2981 The console pool can only be defined for the default toolchain.
2982 Refer to the Ninja documentation on the console pool for more info.
2983
Brett Wilson796ed472018-07-16 15:11:09 -07002984 A pool is referenced by its label just like a target.
2985```
2986
2987#### **Variables**
2988
2989```
2990 depth*
2991 * = required
2992```
2993
2994#### **Example**
2995
2996```
2997 if (current_toolchain == default_toolchain) {
2998 pool("link_pool") {
2999 depth = 1
3000 }
3001 }
3002
3003 toolchain("toolchain") {
3004 tool("link") {
3005 command = "..."
Petr Hosekadd1bb12019-09-11 14:39:38 -07003006 pool = ":link_pool($default_toolchain)"
Brett Wilson796ed472018-07-16 15:11:09 -07003007 }
3008 }
3009```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003010### <a name="func_print"></a>**print**: Prints to the console.
Brett Wilson796ed472018-07-16 15:11:09 -07003011
3012```
3013 Prints all arguments to the console separated by spaces. A newline is
3014 automatically appended to the end.
3015
3016 This function is intended for debugging. Note that build files are run in
3017 parallel so you may get interleaved prints. A buildfile may also be executed
3018 more than once in parallel in the context of different toolchains so the
3019 prints from one file may be duplicated or
3020 interleaved with itself.
3021```
3022
3023#### **Examples**
3024
3025```
3026 print("Hello world")
3027
3028 print(sources, deps)
3029```
Aaron Wood24062bb2022-04-25 20:37:48 -07003030### <a name="func_print_stack_trace"></a>**print_stack_trace**: Prints a stack trace.
3031
3032```
3033 Prints the current file location, and all template invocations that led up to
3034 this location, to the console.
3035```
3036
3037#### **Examples**
3038
3039```
3040 template("foo"){
3041 print_stack_trace()
3042 }
3043 template("bar"){
3044 foo(target_name + ".foo") {
3045 baz = invoker.baz
3046 }
3047 }
3048 bar("lala") {
3049 baz = 42
3050 }
3051
3052 will print out the following:
3053
3054 print_stack_trace() initiated at //build.gn:2
3055 bar("lala") //BUILD.gn:9
3056 foo("lala.foo") //BUILD.gn:5
3057 print_stack_trace() //BUILD.gn:2
3058```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003059### <a name="func_process_file_template"></a>**process_file_template**: Do template expansion over a list of files.
Brett Wilson796ed472018-07-16 15:11:09 -07003060
3061```
3062 process_file_template(source_list, template)
3063
3064 process_file_template applies a template list to a source file list,
3065 returning the result of applying each template to each source. This is
3066 typically used for computing output file names from input files.
3067
3068 In most cases, get_target_outputs() will give the same result with shorter,
3069 more maintainable code. This function should only be used when that function
3070 can't be used (like there's no target or the target is defined in another
3071 build file).
3072```
3073
3074#### **Arguments**
3075
3076```
3077 The source_list is a list of file names.
3078
3079 The template can be a string or a list. If it is a list, multiple output
3080 strings are generated for each input.
3081
3082 The template should contain source expansions to which each name in the
3083 source list is applied. See "gn help source_expansion".
3084```
3085
3086#### **Example**
3087
3088```
3089 sources = [
3090 "foo.idl",
3091 "bar.idl",
3092 ]
3093 myoutputs = process_file_template(
3094 sources,
3095 [ "$target_gen_dir/{{source_name_part}}.cc",
3096 "$target_gen_dir/{{source_name_part}}.h" ])
3097
3098 The result in this case will be:
3099 [ "//out/Debug/foo.cc"
3100 "//out/Debug/foo.h"
3101 "//out/Debug/bar.cc"
3102 "//out/Debug/bar.h" ]
3103```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003104### <a name="func_read_file"></a>**read_file**: Read a file into a variable.
Brett Wilson796ed472018-07-16 15:11:09 -07003105
3106```
3107 read_file(filename, input_conversion)
3108
3109 Whitespace will be trimmed from the end of the file. Throws an error if the
3110 file can not be opened.
3111```
3112
3113#### **Arguments**
3114
3115```
3116 filename
3117 Filename to read, relative to the build file.
3118
3119 input_conversion
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02003120 Controls how the file is read and parsed. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07003121```
3122
3123#### **Example**
3124
3125```
3126 lines = read_file("foo.txt", "list lines")
3127```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003128### <a name="func_rebase_path"></a>**rebase_path**: Rebase a file or directory to another location.
Brett Wilson796ed472018-07-16 15:11:09 -07003129
3130```
3131 converted = rebase_path(input,
3132 new_base = "",
3133 current_base = ".")
3134
3135 Takes a string argument representing a file name, or a list of such strings
3136 and converts it/them to be relative to a different base directory.
3137
3138 When invoking the compiler or scripts, GN will automatically convert sources
3139 and include directories to be relative to the build directory. However, if
3140 you're passing files directly in the "args" array or doing other manual
3141 manipulations where GN doesn't know something is a file name, you will need
3142 to convert paths to be relative to what your tool is expecting.
3143
3144 The common case is to use this to convert paths relative to the current
3145 directory to be relative to the build directory (which will be the current
3146 directory when executing scripts).
3147
3148 If you want to convert a file path to be source-absolute (that is, beginning
3149 with a double slash like "//foo/bar"), you should use the get_path_info()
3150 function. This function won't work because it will always make relative
3151 paths, and it needs to support making paths relative to the source root, so
Robert Sesekd0a6f072020-05-15 11:21:22 -04003152 it can't also generate source-absolute paths without more special-cases.
Brett Wilson796ed472018-07-16 15:11:09 -07003153```
3154
3155#### **Arguments**
3156
3157```
3158 input
Robert Sesekd0a6f072020-05-15 11:21:22 -04003159 A string or list of strings representing file or directory names. These
Brett Wilson796ed472018-07-16 15:11:09 -07003160 can be relative paths ("foo/bar.txt"), system absolute paths
3161 ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt").
3162
3163 new_base
3164 The directory to convert the paths to be relative to. This can be an
3165 absolute path or a relative path (which will be treated as being relative
3166 to the current BUILD-file's directory).
3167
3168 As a special case, if new_base is the empty string (the default), all
3169 paths will be converted to system-absolute native style paths with system
3170 path separators. This is useful for invoking external programs.
3171
3172 current_base
3173 Directory representing the base for relative paths in the input. If this
3174 is not an absolute path, it will be treated as being relative to the
3175 current build file. Use "." (the default) to convert paths from the
3176 current BUILD-file's directory.
3177```
3178
3179#### **Return value**
3180
3181```
3182 The return value will be the same type as the input value (either a string or
3183 a list of strings). All relative and source-absolute file names will be
3184 converted to be relative to the requested output System-absolute paths will
3185 be unchanged.
3186
3187 Whether an output path will end in a slash will match whether the
3188 corresponding input path ends in a slash. It will return "." or "./"
3189 (depending on whether the input ends in a slash) to avoid returning empty
3190 strings. This means if you want a root path ("//" or "/") not ending in a
3191 slash, you can add a dot ("//.").
3192```
3193
3194#### **Example**
3195
3196```
3197 # Convert a file in the current directory to be relative to the build
3198 # directory (the current dir when executing compilers and scripts).
3199 foo = rebase_path("myfile.txt", root_build_dir)
3200 # might produce "../../project/myfile.txt".
3201
3202 # Convert a file to be system absolute:
3203 foo = rebase_path("myfile.txt")
3204 # Might produce "D:\\source\\project\\myfile.txt" on Windows or
3205 # "/home/you/source/project/myfile.txt" on Linux.
3206
3207 # Typical usage for converting to the build directory for a script.
3208 action("myscript") {
3209 # Don't convert sources, GN will automatically convert these to be relative
3210 # to the build directory when it constructs the command line for your
3211 # script.
3212 sources = [ "foo.txt", "bar.txt" ]
3213
3214 # Extra file args passed manually need to be explicitly converted
3215 # to be relative to the build directory:
3216 args = [
3217 "--data",
3218 rebase_path("//mything/data/input.dat", root_build_dir),
3219 "--rel",
3220 rebase_path("relative_path.txt", root_build_dir)
3221 ] + rebase_path(sources, root_build_dir)
3222 }
3223```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003224### <a name="func_set_default_toolchain"></a>**set_default_toolchain**: Sets the default toolchain name.
Brett Wilson796ed472018-07-16 15:11:09 -07003225
3226```
3227 set_default_toolchain(toolchain_label)
3228
3229 The given label should identify a toolchain definition (see "gn help
3230 toolchain"). This toolchain will be used for all targets unless otherwise
3231 specified.
3232
3233 This function is only valid to call during the processing of the build
3234 configuration file. Since the build configuration file is processed
3235 separately for each toolchain, this function will be a no-op when called
3236 under any non-default toolchains.
3237
3238 For example, the default toolchain should be appropriate for the current
3239 environment. If the current environment is 32-bit and somebody references a
3240 target with a 64-bit toolchain, we wouldn't want processing of the build
3241 config file for the 64-bit toolchain to reset the default toolchain to
3242 64-bit, we want to keep it 32-bits.
3243```
3244
3245#### **Argument**
3246
3247```
3248 toolchain_label
3249 Toolchain name.
3250```
3251
3252#### **Example**
3253
3254```
3255 # Set default toolchain only has an effect when run in the context of the
3256 # default toolchain. Pick the right one according to the current CPU
3257 # architecture.
3258 if (target_cpu == "x64") {
3259 set_default_toolchain("//toolchains:64")
3260 } else if (target_cpu == "x86") {
3261 set_default_toolchain("//toolchains:32")
3262 }
3263```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003264### <a name="func_set_defaults"></a>**set_defaults**: Set default values for a target type.
Brett Wilson796ed472018-07-16 15:11:09 -07003265
3266```
3267 set_defaults(<target_type_name>) { <values...> }
3268
3269 Sets the default values for a given target type. Whenever target_type_name is
3270 seen in the future, the values specified in set_default's block will be
3271 copied into the current scope.
3272
3273 When the target type is used, the variable copying is very strict. If a
3274 variable with that name is already in scope, the build will fail with an
3275 error.
3276
3277 set_defaults can be used for built-in target types ("executable",
3278 "shared_library", etc.) and custom ones defined via the "template" command.
3279 It can be called more than once and the most recent call in any scope will
3280 apply, but there is no way to refer to the previous defaults and modify them
3281 (each call to set_defaults must supply a complete list of all defaults it
3282 wants). If you want to share defaults, store them in a separate variable.
3283```
3284
3285#### **Example**
3286
3287```
3288 set_defaults("static_library") {
3289 configs = [ "//tools/mything:settings" ]
3290 }
3291
Nico Webere49cb722018-08-28 13:10:29 -04003292 static_library("mylib") {
Brett Wilson796ed472018-07-16 15:11:09 -07003293 # The configs will be auto-populated as above. You can remove it if
3294 # you don't want the default for a particular default:
3295 configs -= [ "//tools/mything:settings" ]
3296 }
3297```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003298### <a name="func_split_list"></a>**split_list**: Splits a list into N different sub-lists.
Brett Wilson796ed472018-07-16 15:11:09 -07003299
3300```
3301 result = split_list(input, n)
3302
3303 Given a list and a number N, splits the list into N sub-lists of
3304 approximately equal size. The return value is a list of the sub-lists. The
3305 result will always be a list of size N. If N is greater than the number of
3306 elements in the input, it will be padded with empty lists.
3307
3308 The expected use is to divide source files into smaller uniform chunks.
3309```
3310
3311#### **Example**
3312
3313```
3314 The code:
3315 mylist = [1, 2, 3, 4, 5, 6]
3316 print(split_list(mylist, 3))
3317
3318 Will print:
3319 [[1, 2], [3, 4], [5, 6]
3320```
Keir Mierle6ae63302019-11-08 23:02:18 -08003321### <a name="func_string_join"></a>**string_join**: Concatenates a list of strings with a separator.
3322
3323```
3324 result = string_join(separator, strings)
3325
3326 Concatenate a list of strings with intervening occurrences of separator.
3327```
3328
3329#### **Examples**
3330
3331```
3332 string_join("", ["a", "b", "c"]) --> "abc"
3333 string_join("|", ["a", "b", "c"]) --> "a|b|c"
3334 string_join(", ", ["a", "b", "c"]) --> "a, b, c"
3335 string_join("s", ["", ""]) --> "s"
3336```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003337### <a name="func_string_replace"></a>**string_replace**: Replaces substring in the given string.
Petr Hosekc0744ed2018-08-04 16:13:59 -07003338
3339```
3340 result = string_replace(str, old, new[, max])
3341
3342 Returns a copy of the string str in which the occurrences of old have been
3343 replaced with new, optionally restricting the number of replacements. The
3344 replacement is performed sequentially, so if new contains old, it won't be
3345 replaced.
3346```
3347
3348#### **Example**
3349
3350```
3351 The code:
3352 mystr = "Hello, world!"
3353 print(string_replace(mystr, "world", "GN"))
3354
3355 Will print:
3356 Hello, GN!
3357```
Keir Mierle6ae63302019-11-08 23:02:18 -08003358### <a name="func_string_split"></a>**string_split**: Split string into a list of strings.
3359
3360```
3361 result = string_split(str[, sep])
3362
3363 Split string into all substrings separated by separator and returns a list
3364 of the substrings between those separators.
3365
3366 If the separator argument is omitted, the split is by any whitespace, and
3367 any leading/trailing whitespace is ignored; similar to Python's str.split().
3368```
3369
3370#### **Examples without a separator (split on whitespace)**:
3371
3372```
3373 string_split("") --> []
3374 string_split("a") --> ["a"]
3375 string_split(" aa bb") --> ["aa", "bb"]
3376```
3377
3378#### **Examples with a separator (split on separators)**:
3379
3380```
3381 string_split("", "|") --> [""]
3382 string_split(" a b ", " ") --> ["", "", "a", "b", "", ""]
3383 string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"]
3384```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003385### <a name="func_template"></a>**template**: Define a template rule.
Brett Wilson796ed472018-07-16 15:11:09 -07003386
3387```
3388 A template defines a custom name that acts like a function. It provides a way
3389 to add to the built-in target types.
3390
3391 The template() function is used to declare a template. To invoke the
3392 template, just use the name of the template like any other target type.
3393
3394 Often you will want to declare your template in a special file that other
3395 files will import (see "gn help import") so your template rule can be shared
3396 across build files.
3397```
3398
3399#### **Variables and templates**:
3400
3401```
3402 When you call template() it creates a closure around all variables currently
3403 in scope with the code in the template block. When the template is invoked,
3404 the closure will be executed.
3405
3406 When the template is invoked, the code in the caller is executed and passed
3407 to the template code as an implicit "invoker" variable. The template uses
3408 this to read state out of the invoking code.
3409
3410 One thing explicitly excluded from the closure is the "current directory"
3411 against which relative file names are resolved. The current directory will be
3412 that of the invoking code, since typically that code specifies the file
3413 names. This means all files internal to the template should use absolute
3414 names.
3415
3416 A template will typically forward some or all variables from the invoking
3417 scope to a target that it defines. Often, such variables might be optional.
3418 Use the pattern:
3419
3420 if (defined(invoker.deps)) {
3421 deps = invoker.deps
3422 }
3423
3424 The function forward_variables_from() provides a shortcut to forward one or
3425 more or possibly all variables in this manner:
3426
3427 forward_variables_from(invoker, ["deps", "public_deps"])
3428```
3429
3430#### **Target naming**
3431
3432```
3433 Your template should almost always define a built-in target with the name the
3434 template invoker specified. For example, if you have an IDL template and
3435 somebody does:
3436 idl("foo") {...
3437 you will normally want this to expand to something defining a source_set or
3438 static_library named "foo" (among other things you may need). This way, when
3439 another target specifies a dependency on "foo", the static_library or
3440 source_set will be linked.
3441
3442 It is also important that any other targets your template expands to have
3443 unique names, or you will get collisions.
3444
3445 Access the invoking name in your template via the implicit "target_name"
3446 variable. This should also be the basis for how other targets that a template
3447 expands to ensure uniqueness.
3448
3449 A typical example would be a template that defines an action to generate some
3450 source files, and a source_set to compile that source. Your template would
3451 name the source_set "target_name" because that's what you want external
3452 targets to depend on to link your code. And you would name the action
3453 something like "${target_name}_action" to make it unique. The source set
3454 would have a dependency on the action to make it run.
3455```
3456
3457#### **Overriding builtin targets**
3458
3459```
3460 You can use template to redefine a built-in target in which case your template
3461 takes a precedence over the built-in one. All uses of the target from within
3462 the template definition will refer to the built-in target which makes it
3463 possible to extend the behavior of the built-in target:
3464
3465 template("shared_library") {
3466 shared_library(shlib) {
Julie Hockette2a29402018-07-31 10:11:42 -07003467 forward_variables_from(invoker, "*")
Brett Wilson796ed472018-07-16 15:11:09 -07003468 ...
3469 }
3470 }
3471```
3472
3473#### **Example of defining a template**
3474
3475```
3476 template("my_idl") {
3477 # Be nice and help callers debug problems by checking that the variables
3478 # the template requires are defined. This gives a nice message rather than
3479 # giving the user an error about an undefined variable in the file defining
3480 # the template
3481 #
3482 # You can also use defined() to give default values to variables
3483 # unspecified by the invoker.
3484 assert(defined(invoker.sources),
3485 "Need sources in $target_name listing the idl files.")
3486
3487 # Name of the intermediate target that does the code gen. This must
3488 # incorporate the target name so it's unique across template
3489 # instantiations.
3490 code_gen_target_name = target_name + "_code_gen"
3491
3492 # Intermediate target to convert IDL to C source. Note that the name is
3493 # based on the name the invoker of the template specified. This way, each
3494 # time the template is invoked we get a unique intermediate action name
3495 # (since all target names are in the global scope).
3496 action_foreach(code_gen_target_name) {
3497 # Access the scope defined by the invoker via the implicit "invoker"
3498 # variable.
3499 sources = invoker.sources
3500
3501 # Note that we need an absolute path for our script file name. The
3502 # current directory when executing this code will be that of the invoker
3503 # (this is why we can use the "sources" directly above without having to
3504 # rebase all of the paths). But if we need to reference a script relative
3505 # to the template file, we'll need to use an absolute path instead.
3506 script = "//tools/idl/idl_code_generator.py"
3507
3508 # Tell GN how to expand output names given the sources.
3509 # See "gn help source_expansion" for more.
3510 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
3511 "$target_gen_dir/{{source_name_part}}.h" ]
3512 }
3513
3514 # Name the source set the same as the template invocation so instancing
3515 # this template produces something that other targets can link to in their
3516 # deps.
3517 source_set(target_name) {
3518 # Generates the list of sources, we get these from the action_foreach
3519 # above.
3520 sources = get_target_outputs(":$code_gen_target_name")
3521
3522 # This target depends on the files produced by the above code gen target.
3523 deps = [ ":$code_gen_target_name" ]
3524 }
3525 }
3526```
3527
3528#### **Example of invoking the resulting template**
3529
3530```
3531 # This calls the template code above, defining target_name to be
3532 # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly
3533 # brackets.
3534 my_idl("foo_idl_files") {
3535 # Goes into the template as "invoker.sources".
3536 sources = [ "foo.idl", "bar.idl" ]
3537 }
3538
3539 # Here is a target that depends on our template.
3540 executable("my_exe") {
3541 # Depend on the name we gave the template call above. Internally, this will
3542 # produce a dependency from executable to the source_set inside the
3543 # template (since it has this name), which will in turn depend on the code
3544 # gen action.
3545 deps = [ ":foo_idl_files" ]
3546 }
3547```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003548### <a name="func_tool"></a>**tool**: Specify arguments to a toolchain tool.
Brett Wilson796ed472018-07-16 15:11:09 -07003549
3550#### **Usage**
3551
3552```
3553 tool(<tool type>) {
3554 <tool variables...>
3555 }
3556```
3557
3558#### **Tool types**
3559
3560```
3561 Compiler tools:
3562 "cc": C compiler
3563 "cxx": C++ compiler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003564 "cxx_module": C++ compiler used for Clang .modulemap files
Brett Wilson796ed472018-07-16 15:11:09 -07003565 "objc": Objective C compiler
3566 "objcxx": Objective C++ compiler
3567 "rc": Resource compiler (Windows .rc files)
3568 "asm": Assembler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003569 "swift": Swift compiler driver
Brett Wilson796ed472018-07-16 15:11:09 -07003570
3571 Linker tools:
3572 "alink": Linker for static libraries (archives)
3573 "solink": Linker for shared libraries
3574 "link": Linker for executables
3575
3576 Other tools:
3577 "stamp": Tool for creating stamp files
3578 "copy": Tool to copy files.
3579 "action": Defaults for actions
3580
3581 Platform specific tools:
3582 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
3583 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
Julie Hockettce1fa072019-05-07 17:44:37 -07003584
3585 Rust tools:
Petr Hosek7617b592019-10-02 09:52:21 -07003586 "rust_bin": Tool for compiling Rust binaries
3587 "rust_cdylib": Tool for compiling C-compatible dynamic libraries.
3588 "rust_dylib": Tool for compiling Rust dynamic libraries.
3589 "rust_macro": Tool for compiling Rust procedural macros.
3590 "rust_rlib": Tool for compiling Rust libraries.
3591 "rust_staticlib": Tool for compiling Rust static libraries.
Brett Wilson796ed472018-07-16 15:11:09 -07003592```
3593
3594#### **Tool variables**
3595
3596```
3597 command [string with substitutions]
3598 Valid for: all tools except "action" (required)
3599
3600 The command to run.
3601
Joe Armstrong23d22bc2019-06-28 15:31:03 +08003602 command_launcher [string]
3603 Valid for: all tools except "action" (optional)
3604
3605 The prefix with which to launch the command (e.g. the path to a Goma or
3606 CCache compiler launcher).
3607
3608 Note that this prefix will not be included in the compilation database or
3609 IDE files generated from the build.
3610
Brett Wilson796ed472018-07-16 15:11:09 -07003611 default_output_dir [string with substitutions]
3612 Valid for: linker tools
3613
3614 Default directory name for the output file relative to the
3615 root_build_dir. It can contain other substitution patterns. This will
3616 be the default value for the {{output_dir}} expansion (discussed below)
3617 but will be overridden by the "output_dir" variable in a target, if one
3618 is specified.
3619
3620 GN doesn't do anything with this string other than pass it along,
3621 potentially with target-specific overrides. It is the tool's job to use
3622 the expansion so that the files will be in the right place.
3623
3624 default_output_extension [string]
3625 Valid for: linker tools
3626
3627 Extension for the main output of a linkable tool. It includes the
3628 leading dot. This will be the default value for the
3629 {{output_extension}} expansion (discussed below) but will be overridden
3630 by by the "output extension" variable in a target, if one is specified.
3631 Empty string means no extension.
3632
3633 GN doesn't actually do anything with this extension other than pass it
3634 along, potentially with target-specific overrides. One would typically
3635 use the {{output_extension}} value in the "outputs" to read this value.
3636
3637 Example: default_output_extension = ".exe"
3638
3639 depfile [string with substitutions]
3640 Valid for: compiler tools (optional)
3641
3642 If the tool can write ".d" files, this specifies the name of the
3643 resulting file. These files are used to list header file dependencies
3644 (or other implicit input dependencies) that are discovered at build
3645 time. See also "depsformat".
3646
3647 Example: depfile = "{{output}}.d"
3648
3649 depsformat [string]
3650 Valid for: compiler tools (when depfile is specified)
3651
3652 Format for the deps outputs. This is either "gcc" or "msvc". See the
3653 ninja documentation for "deps" for more information.
3654
3655 Example: depsformat = "gcc"
3656
3657 description [string with substitutions, optional]
3658 Valid for: all tools
3659
3660 What to print when the command is run.
3661
3662 Example: description = "Compiling {{source}}"
3663
Julie Hockettce1fa072019-05-07 17:44:37 -07003664 exe_output_extension [string, optional, rust tools only]
3665 rlib_output_extension [string, optional, rust tools only]
3666 dylib_output_extension [string, optional, rust tools only]
3667 cdylib_output_extension [string, optional, rust tools only]
Petr Hosekfe36c7c2019-11-26 14:38:08 -08003668 rust_proc_macro_output_extension [string, optional, rust tools only]
Julie Hockettce1fa072019-05-07 17:44:37 -07003669 Valid for: Rust tools
3670
3671 These specify the default tool output for each of the crate types.
3672 The default is empty for executables, shared, and static libraries and
3673 ".rlib" for rlibs. Note that the Rust compiler complains with an error
3674 if external crates do not take the form `lib<name>.rlib` or
3675 `lib<name>.<shared_extension>`, where `<shared_extension>` is `.so`,
3676 `.dylib`, or `.dll` as appropriate for the platform.
3677
Brett Wilson796ed472018-07-16 15:11:09 -07003678 lib_switch [string, optional, link tools only]
3679 lib_dir_switch [string, optional, link tools only]
3680 Valid for: Linker tools except "alink"
3681
3682 These strings will be prepended to the libraries and library search
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003683 directories, respectively, because linkers differ on how to specify
3684 them.
3685
Brett Wilson796ed472018-07-16 15:11:09 -07003686 If you specified:
3687 lib_switch = "-l"
3688 lib_dir_switch = "-L"
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003689 then the "{{libs}}" expansion for
3690 [ "freetype", "expat" ]
3691 would be
3692 "-lfreetype -lexpat".
3693
3694 framework_switch [string, optional, link tools only]
Robert Sesekd0a6f072020-05-15 11:21:22 -04003695 weak_framework_switch [string, optional, link tools only]
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003696 framework_dir_switch [string, optional, link tools only]
3697 Valid for: Linker tools
3698
3699 These strings will be prepended to the frameworks and framework search
3700 path directories, respectively, because linkers differ on how to specify
3701 them.
3702
3703 If you specified:
3704 framework_switch = "-framework "
Robert Sesekd0a6f072020-05-15 11:21:22 -04003705 weak_framework_switch = "-weak_framework "
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003706 framework_dir_switch = "-F"
Robert Sesekd0a6f072020-05-15 11:21:22 -04003707 and:
3708 framework_dirs = [ "$root_out_dir" ]
3709 frameworks = [ "UIKit.framework", "Foo.framework" ]
3710 weak_frameworks = [ "MediaPlayer.framework" ]
3711 would be:
3712 "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer"
Brett Wilson796ed472018-07-16 15:11:09 -07003713
Sylvain Defresne89e64252020-08-07 13:01:06 +02003714 swiftmodule_switch [string, optional, link tools only]
3715 Valid for: Linker tools except "alink"
3716
3717 The string will be prependend to the path to the .swiftmodule files
3718 that are embedded in the linker output.
3719
3720 If you specified:
3721 swiftmodule_swift = "-Wl,-add_ast_path,"
3722 then the "{{swiftmodules}}" expansion for
3723 [ "obj/foo/Foo.swiftmodule" ]
3724 would be
3725 "-Wl,-add_ast_path,obj/foo/Foo.swiftmodule"
3726
Brett Wilson796ed472018-07-16 15:11:09 -07003727 outputs [list of strings with substitutions]
3728 Valid for: Linker and compiler tools (required)
3729
3730 An array of names for the output files the tool produces. These are
3731 relative to the build output directory. There must always be at least
3732 one output file. There can be more than one output (a linker might
3733 produce a library and an import library, for example).
3734
3735 This array just declares to GN what files the tool will produce. It is
3736 your responsibility to specify the tool command that actually produces
3737 these files.
3738
3739 If you specify more than one output for shared library links, you
3740 should consider setting link_output, depend_output, and
3741 runtime_outputs.
3742
3743 Example for a compiler tool that produces .obj files:
3744 outputs = [
3745 "{{source_out_dir}}/{{source_name_part}}.obj"
3746 ]
3747
3748 Example for a linker tool that produces a .dll and a .lib. The use of
3749 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
3750 the target to override these values.
3751 outputs = [
Nico Weberad9eab22020-11-15 22:20:28 -05003752 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Brett Wilson796ed472018-07-16 15:11:09 -07003753 "{{output_dir}}/{{target_output_name}}.lib",
3754 ]
3755
Sylvain Defresne89e64252020-08-07 13:01:06 +02003756 partial_outputs [list of strings with substitutions]
3757 Valid for: "swift" only
3758
3759 An array of names for the partial outputs the tool produces. These
3760 are relative to the build output directory. The expansion will be
3761 evaluated for each file listed in the "sources" of the target.
3762
3763 This is used to deal with whole module optimization, allowing to
3764 list one object file per source file when whole module optimization
3765 is disabled.
3766
Brett Wilson796ed472018-07-16 15:11:09 -07003767 pool [label, optional]
3768 Valid for: all tools (optional)
3769
3770 Label of the pool to use for the tool. Pools are used to limit the
3771 number of tasks that can execute concurrently during the build.
3772
3773 See also "gn help pool".
3774
3775 link_output [string with substitutions]
3776 depend_output [string with substitutions]
3777 Valid for: "solink" only (optional)
3778
3779 These two files specify which of the outputs from the solink tool
3780 should be used for linking and dependency tracking. These should match
3781 entries in the "outputs". If unspecified, the first item in the
3782 "outputs" array will be used for all. See "Separate linking and
3783 dependencies for shared libraries" below for more.
3784
3785 On Windows, where the tools produce a .dll shared library and a .lib
3786 import library, you will want the first two to be the import library
3787 and the third one to be the .dll file. On Linux, if you're not doing
3788 the separate linking/dependency optimization, all of these should be
3789 the .so output.
3790
3791 output_prefix [string]
3792 Valid for: Linker tools (optional)
3793
3794 Prefix to use for the output name. Defaults to empty. This prefix will
3795 be prepended to the name of the target (or the output_name if one is
3796 manually specified for it) if the prefix is not already there. The
3797 result will show up in the {{output_name}} substitution pattern.
3798
3799 Individual targets can opt-out of the output prefix by setting:
3800 output_prefix_override = true
3801 (see "gn help output_prefix_override").
3802
3803 This is typically used to prepend "lib" to libraries on
3804 Posix systems:
3805 output_prefix = "lib"
3806
3807 precompiled_header_type [string]
3808 Valid for: "cc", "cxx", "objc", "objcxx"
3809
3810 Type of precompiled headers. If undefined or the empty string,
3811 precompiled headers will not be used for this tool. Otherwise use "gcc"
3812 or "msvc".
3813
3814 For precompiled headers to be used for a given target, the target (or a
3815 config applied to it) must also specify a "precompiled_header" and, for
3816 "msvc"-style headers, a "precompiled_source" value. If the type is
3817 "gcc", then both "precompiled_header" and "precompiled_source" must
3818 resolve to the same file, despite the different formats required for
3819 each."
3820
3821 See "gn help precompiled_header" for more.
3822
3823 restat [boolean]
3824 Valid for: all tools (optional, defaults to false)
3825
3826 Requests that Ninja check the file timestamp after this tool has run to
3827 determine if anything changed. Set this if your tool has the ability to
3828 skip writing output if the output file has not changed.
3829
3830 Normally, Ninja will assume that when a tool runs the output be new and
3831 downstream dependents must be rebuild. When this is set to trye, Ninja
3832 can skip rebuilding downstream dependents for input changes that don't
3833 actually affect the output.
3834
3835 Example:
3836 restat = true
3837
3838 rspfile [string with substitutions]
3839 Valid for: all tools except "action" (optional)
3840
3841 Name of the response file. If empty, no response file will be
3842 used. See "rspfile_content".
3843
3844 rspfile_content [string with substitutions]
3845 Valid for: all tools except "action" (required when "rspfile" is used)
3846
3847 The contents to be written to the response file. This may include all
3848 or part of the command to send to the tool which allows you to get
3849 around OS command-line length limits.
3850
3851 This example adds the inputs and libraries to a response file, but
3852 passes the linker flags directly on the command line:
3853 tool("link") {
3854 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
3855 rspfile = "{{output}}.rsp"
Adrian Taylorac6cc952020-04-26 20:49:18 -07003856 rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}"
Brett Wilson796ed472018-07-16 15:11:09 -07003857 }
3858
3859 runtime_outputs [string list with substitutions]
3860 Valid for: linker tools
3861
3862 If specified, this list is the subset of the outputs that should be
3863 added to runtime deps (see "gn help runtime_deps"). By default (if
3864 runtime_outputs is empty or unspecified), it will be the link_output.
Adrian Taylor4aa9bdf2021-11-15 19:00:06 -08003865
3866 rust_sysroot
3867 Valid for: Rust tools
3868
3869 A path relative to root_out_dir. This is not used in the build
3870 process, but may be used when generating metadata for rust-analyzer.
3871 (See --export-rust-project). It enables such metadata to include
3872 information about the Rust standard library.
Brett Wilson796ed472018-07-16 15:11:09 -07003873```
3874
3875#### **Expansions for tool variables**
3876
3877```
3878 All paths are relative to the root build directory, which is the current
3879 directory for running all tools. These expansions are available to all tools:
3880
3881 {{label}}
3882 The label of the current target. This is typically used in the
3883 "description" field for link tools. The toolchain will be omitted from
3884 the label for targets in the default toolchain, and will be included
3885 for targets in other toolchains.
3886
3887 {{label_name}}
3888 The short name of the label of the target. This is the part after the
3889 colon. For "//foo/bar:baz" this will be "baz". Unlike
3890 {{target_output_name}}, this is not affected by the "output_prefix" in
3891 the tool or the "output_name" set on the target.
3892
Scott Graham501b49a2020-08-05 13:03:37 -07003893 {{label_no_toolchain}}
3894 The label of the current target, never including the toolchain
3895 (otherwise, this is identical to {{label}}). This is used as the module
3896 name when using .modulemap files.
3897
Brett Wilson796ed472018-07-16 15:11:09 -07003898 {{output}}
3899 The relative path and name of the output(s) of the current build step.
3900 If there is more than one output, this will expand to a list of all of
3901 them. Example: "out/base/my_file.o"
3902
3903 {{target_gen_dir}}
3904 {{target_out_dir}}
3905 The directory of the generated file and output directories,
3906 respectively, for the current target. There is no trailing slash. See
3907 also {{output_dir}} for linker tools. Example: "out/base/test"
3908
3909 {{target_output_name}}
3910 The short name of the current target with no path information, or the
3911 value of the "output_name" variable if one is specified in the target.
3912 This will include the "output_prefix" if any. See also {{label_name}}.
3913
3914 Example: "libfoo" for the target named "foo" and an output prefix for
3915 the linker tool of "lib".
3916
3917 Compiler tools have the notion of a single input and a single output, along
3918 with a set of compiler-specific flags. The following expansions are
3919 available:
3920
3921 {{asmflags}}
3922 {{cflags}}
3923 {{cflags_c}}
3924 {{cflags_cc}}
3925 {{cflags_objc}}
3926 {{cflags_objcc}}
3927 {{defines}}
3928 {{include_dirs}}
3929 Strings correspond that to the processed flags/defines/include
3930 directories specified for the target.
3931 Example: "--enable-foo --enable-bar"
3932
3933 Defines will be prefixed by "-D" and include directories will be
3934 prefixed by "-I" (these work with Posix tools as well as Microsoft
3935 ones).
3936
Scott Graham484245b2020-08-11 10:58:50 -07003937 {{module_deps}}
3938 {{module_deps_no_self}}
3939 Strings that correspond to the flags necessary to depend upon the Clang
3940 modules referenced by the current target. The "_no_self" version doesn't
3941 include the module for the current target, and can be used to compile
3942 the pcm itself.
3943
Brett Wilson796ed472018-07-16 15:11:09 -07003944 {{source}}
3945 The relative path and name of the current input file.
3946 Example: "../../base/my_file.cc"
3947
3948 {{source_file_part}}
3949 The file part of the source including the extension (with no directory
3950 information).
3951 Example: "foo.cc"
3952
3953 {{source_name_part}}
3954 The filename part of the source file with no directory or extension.
3955 Example: "foo"
3956
3957 {{source_gen_dir}}
3958 {{source_out_dir}}
3959 The directory in the generated file and output directories,
3960 respectively, for the current input file. If the source file is in the
3961 same directory as the target is declared in, they will will be the same
3962 as the "target" versions above. Example: "gen/base/test"
3963
Julie Hockette2a29402018-07-31 10:11:42 -07003964 Linker tools have multiple inputs and (potentially) multiple outputs. The
Brett Wilson796ed472018-07-16 15:11:09 -07003965 static library tool ("alink") is not considered a linker tool. The following
3966 expansions are available:
3967
3968 {{inputs}}
3969 {{inputs_newline}}
3970 Expands to the inputs to the link step. This will be a list of object
3971 files and static libraries.
3972 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
3973
3974 The "_newline" version will separate the input files with newlines
3975 instead of spaces. This is useful in response files: some linkers can
3976 take a "-filelist" flag which expects newline separated files, and some
3977 Microsoft tools have a fixed-sized buffer for parsing each line of a
3978 response file.
3979
3980 {{ldflags}}
3981 Expands to the processed set of ldflags and library search paths
3982 specified for the target.
3983 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
3984
3985 {{libs}}
3986 Expands to the list of system libraries to link to. Each will be
3987 prefixed by the "lib_switch".
3988
Brett Wilson796ed472018-07-16 15:11:09 -07003989 Example: "-lfoo -lbar"
3990
3991 {{output_dir}}
3992 The value of the "output_dir" variable in the target, or the the value
3993 of the "default_output_dir" value in the tool if the target does not
3994 override the output directory. This will be relative to the
3995 root_build_dir and will not end in a slash. Will be "." for output to
3996 the root_build_dir.
3997
3998 This is subtly different than {{target_out_dir}} which is defined by GN
3999 based on the target's path and not overridable. {{output_dir}} is for
4000 the final output, {{target_out_dir}} is generally for object files and
4001 other outputs.
4002
4003 Usually {{output_dir}} would be defined in terms of either
4004 {{target_out_dir}} or {{root_out_dir}}
4005
4006 {{output_extension}}
4007 The value of the "output_extension" variable in the target, or the
4008 value of the "default_output_extension" value in the tool if the target
4009 does not specify an output extension.
4010 Example: ".so"
4011
4012 {{solibs}}
4013 Extra libraries from shared library dependencies not specified in the
4014 {{inputs}}. This is the list of link_output files from shared libraries
4015 (if the solink tool specifies a "link_output" variable separate from
4016 the "depend_output").
4017
4018 These should generally be treated the same as libs by your tool.
4019
4020 Example: "libfoo.so libbar.so"
4021
Adrian Taylorac6cc952020-04-26 20:49:18 -07004022 {{rlibs}}
4023 Any Rust .rlibs which need to be linked into a final C++ target.
4024 These should be treated as {{inputs}} except that sometimes
4025 they might have different linker directives applied.
4026
4027 Example: "obj/foo/libfoo.rlib"
4028
Sylvain Defresnea09ec162020-01-16 12:36:40 +01004029 {{frameworks}}
4030 Shared libraries packaged as framework bundle. This is principally
4031 used on Apple's platforms (macOS and iOS). All name must be ending
4032 with ".framework" suffix; the suffix will be stripped when expanding
Robert Sesekd0a6f072020-05-15 11:21:22 -04004033 {{frameworks}} and each item will be preceded by "-framework" or
4034 "-weak_framework".
Sylvain Defresnea09ec162020-01-16 12:36:40 +01004035
Sylvain Defresne89e64252020-08-07 13:01:06 +02004036 {{swiftmodules}}
4037 Swift .swiftmodule files that needs to be embedded into the binary.
4038 This is necessary to correctly link with object generated by the
4039 Swift compiler (the .swiftmodule file cannot be embedded in object
4040 files directly). Those will be prefixed with "swiftmodule_switch"
4041 value.
4042
Brett Wilson796ed472018-07-16 15:11:09 -07004043 The static library ("alink") tool allows {{arflags}} plus the common tool
4044 substitutions.
4045
4046 The copy tool allows the common compiler/linker substitutions, plus
4047 {{source}} which is the source of the copy. The stamp tool allows only the
4048 common tool substitutions.
4049
4050 The copy_bundle_data and compile_xcassets tools only allows the common tool
4051 substitutions. Both tools are required to create iOS/macOS bundles and need
4052 only be defined on those platforms.
4053
4054 The copy_bundle_data tool will be called with one source and needs to copy
4055 (optionally optimizing the data representation) to its output. It may be
4056 called with a directory as input and it needs to be recursively copied.
4057
4058 The compile_xcassets tool will be called with one or more source (each an
4059 asset catalog) that needs to be compiled to a single output. The following
4060 substitutions are available:
4061
4062 {{inputs}}
4063 Expands to the list of .xcassets to use as input to compile the asset
4064 catalog.
4065
4066 {{bundle_product_type}}
4067 Expands to the product_type of the bundle that will contain the
4068 compiled asset catalog. Usually corresponds to the product_type
4069 property of the corresponding create_bundle target.
4070
4071 {{bundle_partial_info_plist}}
4072 Expands to the path to the partial Info.plist generated by the
4073 assets catalog compiler. Usually based on the target_name of
4074 the create_bundle target.
Julie Hockettce1fa072019-05-07 17:44:37 -07004075
Harley Li0a9affb2020-06-03 10:38:42 -04004076 {{xcasset_compiler_flags}}
4077 Expands to the list of flags specified in corresponding
4078 create_bundle target.
4079
Sylvain Defresne89e64252020-08-07 13:01:06 +02004080 The Swift tool has multiple input and outputs. It must have exactly one
4081 output of .swiftmodule type, but can have one or more object file outputs,
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004082 in addition to other type of outputs. The following expansions are available:
Sylvain Defresne89e64252020-08-07 13:01:06 +02004083
4084 {{module_name}}
4085 Expands to the string representing the module name of target under
4086 compilation (see "module_name" variable).
4087
4088 {{module_dirs}}
4089 Expands to the list of -I<path> for the target Swift module search
4090 path computed from target dependencies.
4091
4092 {{swiftflags}}
4093 Expands to the list of strings representing Swift compiler flags.
4094
Julie Hockettce1fa072019-05-07 17:44:37 -07004095 Rust tools have the notion of a single input and a single output, along
4096 with a set of compiler-specific flags. The following expansions are
4097 available:
4098
4099 {{crate_name}}
4100 Expands to the string representing the crate name of target under
4101 compilation.
4102
4103 {{crate_type}}
4104 Expands to the string representing the type of crate for the target
4105 under compilation.
4106
4107 {{externs}}
4108 Expands to the list of --extern flags needed to include addition Rust
4109 libraries in this target. Includes any specified renamed dependencies.
4110
Julie Hockettce1fa072019-05-07 17:44:37 -07004111 {{rustdeps}}
4112 Expands to the list of -Ldependency=<path> strings needed to compile
4113 this target.
4114
4115 {{rustenv}}
4116 Expands to the list of environment variables.
4117
4118 {{rustflags}}
4119 Expands to the list of strings representing Rust compiler flags.
Brett Wilson796ed472018-07-16 15:11:09 -07004120```
4121
4122#### **Separate linking and dependencies for shared libraries**
4123
4124```
4125 Shared libraries are special in that not all changes to them require that
4126 dependent targets be re-linked. If the shared library is changed but no
4127 imports or exports are different, dependent code needn't be relinked, which
4128 can speed up the build.
4129
4130 If your link step can output a list of exports from a shared library and
4131 writes the file only if the new one is different, the timestamp of this file
4132 can be used for triggering re-links, while the actual shared library would be
4133 used for linking.
4134
4135 You will need to specify
4136 restat = true
4137 in the linker tool to make this work, so Ninja will detect if the timestamp
4138 of the dependency file has changed after linking (otherwise it will always
4139 assume that running a command updates the output):
4140
4141 tool("solink") {
4142 command = "..."
4143 outputs = [
4144 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Nico Weberad9eab22020-11-15 22:20:28 -05004145 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC",
Brett Wilson796ed472018-07-16 15:11:09 -07004146 ]
4147 link_output =
4148 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
4149 depend_output =
Nico Weberad9eab22020-11-15 22:20:28 -05004150 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC"
Brett Wilson796ed472018-07-16 15:11:09 -07004151 restat = true
4152 }
4153```
4154
4155#### **Example**
4156
4157```
4158 toolchain("my_toolchain") {
4159 # Put these at the top to apply to all tools below.
4160 lib_switch = "-l"
4161 lib_dir_switch = "-L"
4162
4163 tool("cc") {
4164 command = "gcc {{source}} -o {{output}}"
4165 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
4166 description = "GCC {{source}}"
4167 }
4168 tool("cxx") {
4169 command = "g++ {{source}} -o {{output}}"
4170 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
4171 description = "G++ {{source}}"
4172 }
4173 };
4174```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004175### <a name="func_toolchain"></a>**toolchain**: Defines a toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004176
4177```
4178 A toolchain is a set of commands and build flags used to compile the source
4179 code. The toolchain() function defines these commands.
4180```
4181
4182#### **Toolchain overview**
4183
4184```
4185 You can have more than one toolchain in use at once in a build and a target
4186 can exist simultaneously in multiple toolchains. A build file is executed
4187 once for each toolchain it is referenced in so the GN code can vary all
4188 parameters of each target (or which targets exist) on a per-toolchain basis.
4189
4190 When you have a simple build with only one toolchain, the build config file
4191 is loaded only once at the beginning of the build. It must call
4192 set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the
4193 label of the toolchain definition to use. The "toolchain_args" section of the
4194 toolchain definition is ignored.
4195
4196 When a target has a dependency on a target using different toolchain (see "gn
4197 help labels" for how to specify this), GN will start a build using that
4198 secondary toolchain to resolve the target. GN will load the build config file
4199 with the build arguments overridden as specified in the toolchain_args.
4200 Because the default toolchain is already known, calls to
4201 set_default_toolchain() are ignored.
4202
4203 To load a file in an alternate toolchain, GN does the following:
4204
4205 1. Loads the file with the toolchain definition in it (as determined by the
4206 toolchain label).
4207 2. Re-runs the master build configuration file, applying the arguments
4208 specified by the toolchain_args section of the toolchain definition.
4209 3. Loads the destination build file in the context of the configuration file
4210 in the previous step.
4211
4212 The toolchain configuration is two-way. In the default toolchain (i.e. the
4213 main build target) the configuration flows from the build config file to the
4214 toolchain. The build config file looks at the state of the build (OS type,
4215 CPU architecture, etc.) and decides which toolchain to use (via
4216 set_default_toolchain()). In secondary toolchains, the configuration flows
4217 from the toolchain to the build config file: the "toolchain_args" in the
4218 toolchain definition specifies the arguments to re-invoke the build.
4219```
4220
4221#### **Functions and variables**
4222
4223```
4224 tool()
Julie Hockette2a29402018-07-31 10:11:42 -07004225 The tool() function call specifies the commands to run for a given step. See
4226 "gn help tool".
Brett Wilson796ed472018-07-16 15:11:09 -07004227
Julie Hockette2a29402018-07-31 10:11:42 -07004228 toolchain_args [scope]
Brett Wilson796ed472018-07-16 15:11:09 -07004229 Overrides for build arguments to pass to the toolchain when invoking it.
4230 This is a variable of type "scope" where the variable names correspond to
4231 variables in declare_args() blocks.
4232
4233 When you specify a target using an alternate toolchain, the master build
4234 configuration file is re-interpreted in the context of that toolchain.
4235 toolchain_args allows you to control the arguments passed into this
4236 alternate invocation of the build.
4237
4238 Any default system arguments or arguments passed in via "gn args" will also
4239 be passed to the alternate invocation unless explicitly overridden by
4240 toolchain_args.
4241
4242 The toolchain_args will be ignored when the toolchain being defined is the
4243 default. In this case, it's expected you want the default argument values.
4244
4245 See also "gn help buildargs" for an overview of these arguments.
4246
Julie Hockette2a29402018-07-31 10:11:42 -07004247 propagates_configs [boolean, default=false]
4248 Determines whether public_configs and all_dependent_configs in this
4249 toolchain propagate to targets in other toolchains.
4250
4251 When false (the default), this toolchain will not propagate any configs to
4252 targets in other toolchains that depend on it targets inside this
4253 toolchain. This matches the most common usage of toolchains where they
4254 represent different architectures or compilers and the settings that apply
4255 to one won't necessarily apply to others.
4256
4257 When true, configs (public and all-dependent) will cross the boundary out
4258 of this toolchain as if the toolchain boundary wasn't there. This only
4259 affects one direction of dependencies: a toolchain can't control whether
4260 it accepts such configs, only whether it pushes them. The build is
4261 responsible for ensuring that any external targets depending on targets in
4262 this toolchain are compatible with the compiler flags, etc. that may be
4263 propagated.
4264
4265 deps [string list]
Brett Wilson796ed472018-07-16 15:11:09 -07004266 Dependencies of this toolchain. These dependencies will be resolved before
4267 any target in the toolchain is compiled. To avoid circular dependencies
4268 these must be targets defined in another toolchain.
4269
4270 This is expressed as a list of targets, and generally these targets will
4271 always specify a toolchain:
4272 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
4273
4274 This concept is somewhat inefficient to express in Ninja (it requires a lot
4275 of duplicate of rules) so should only be used when absolutely necessary.
4276```
4277
4278#### **Example of defining a toolchain**
4279
4280```
4281 toolchain("32") {
4282 tool("cc") {
4283 command = "gcc {{source}}"
4284 ...
4285 }
4286
4287 toolchain_args = {
4288 use_doom_melon = true # Doom melon always required for 32-bit builds.
4289 current_cpu = "x86"
4290 }
4291 }
4292
4293 toolchain("64") {
4294 tool("cc") {
4295 command = "gcc {{source}}"
4296 ...
4297 }
4298
4299 toolchain_args = {
4300 # use_doom_melon is not overridden here, it will take the default.
4301 current_cpu = "x64"
4302 }
4303 }
4304```
4305
4306#### **Example of cross-toolchain dependencies**
4307
4308```
4309 If a 64-bit target wants to depend on a 32-bit binary, it would specify a
4310 dependency using data_deps (data deps are like deps that are only needed at
4311 runtime and aren't linked, since you can't link a 32-bit and a 64-bit
4312 library).
4313
4314 executable("my_program") {
4315 ...
4316 if (target_cpu == "x64") {
4317 # The 64-bit build needs this 32-bit helper.
4318 data_deps = [ ":helper(//toolchains:32)" ]
4319 }
4320 }
4321
4322 if (target_cpu == "x86") {
4323 # Our helper library is only compiled in 32-bits.
4324 shared_library("helper") {
4325 ...
4326 }
4327 }
4328```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004329### <a name="func_write_file"></a>**write_file**: Write a file to disk.
Brett Wilson796ed472018-07-16 15:11:09 -07004330
4331```
Julie Hockett09171292018-07-31 14:35:10 -07004332 write_file(filename, data, output_conversion = "")
Brett Wilson796ed472018-07-16 15:11:09 -07004333
4334 If data is a list, the list will be written one-item-per-line with no quoting
4335 or brackets.
4336
4337 If the file exists and the contents are identical to that being written, the
4338 file will not be updated. This will prevent unnecessary rebuilds of targets
4339 that depend on this file.
4340
4341 One use for write_file is to write a list of inputs to an script that might
4342 be too long for the command line. However, it is preferable to use response
4343 files for this purpose. See "gn help response_file_contents".
Brett Wilson796ed472018-07-16 15:11:09 -07004344```
4345
4346#### **Arguments**
4347
4348```
4349 filename
4350 Filename to write. This must be within the output directory.
4351
4352 data
4353 The list or string to write.
Julie Hockett09171292018-07-31 14:35:10 -07004354
4355 output_conversion
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02004356 Controls how the output is written. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07004357```
4358## <a name="predefined_variables"></a>Built-in predefined variables
4359
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004360### <a name="var_current_cpu"></a>**current_cpu**: The processor architecture of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004361
4362```
4363 The build configuration usually sets this value based on the value of
4364 "host_cpu" (see "gn help host_cpu") and then threads this through the
4365 toolchain definitions to ensure that it always reflects the appropriate
4366 value.
4367
Julie Hockette2a29402018-07-31 10:11:42 -07004368 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004369 empty string ("") by default but is declared so that it can be overridden on
4370 the command line if so desired.
4371
4372 See "gn help target_cpu" for a list of common values returned.
4373```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004374### <a name="var_current_os"></a>**current_os**: The operating system of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004375
4376```
4377 The build configuration usually sets this value based on the value of
4378 "target_os" (see "gn help target_os"), and then threads this through the
4379 toolchain definitions to ensure that it always reflects the appropriate
4380 value.
4381
Julie Hockette2a29402018-07-31 10:11:42 -07004382 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004383 empty string ("") by default but is declared so that it can be overridden on
4384 the command line if so desired.
4385
4386 See "gn help target_os" for a list of common values returned.
4387```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004388### <a name="var_current_toolchain"></a>**current_toolchain**: Label of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004389
4390```
4391 A fully-qualified label representing the current toolchain. You can use this
4392 to make toolchain-related decisions in the build. See also
4393 "default_toolchain".
4394```
4395
4396#### **Example**
4397
4398```
4399 if (current_toolchain == "//build:64_bit_toolchain") {
4400 executable("output_thats_64_bit_only") {
4401 ...
4402```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004403### <a name="var_default_toolchain"></a>**default_toolchain**: [string] Label of the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004404
4405```
4406 A fully-qualified label representing the default toolchain, which may not
4407 necessarily be the current one (see "current_toolchain").
4408```
James Robinson61377e32020-02-13 15:20:07 -08004409### <a name="var_gn_version"></a>**gn_version**: [number] The version of gn.
4410
4411```
4412 Corresponds to the number printed by `gn --version`.
4413```
4414
4415#### **Example**
4416
4417```
4418 assert(gn_version >= 1700, "need GN version 1700 for the frobulate feature")
4419```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004420### <a name="var_host_cpu"></a>**host_cpu**: The processor architecture that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004421
4422```
4423 This is value is exposed so that cross-compile toolchains can access the host
4424 architecture when needed.
4425
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004426 The value should generally be considered read-only, but it can be overridden
Brett Wilson796ed472018-07-16 15:11:09 -07004427 in order to handle unusual cases where there might be multiple plausible
4428 values for the host architecture (e.g., if you can do either 32-bit or 64-bit
4429 builds). The value is not used internally by GN for any purpose.
4430```
4431
4432#### **Some possible values**
4433
4434```
4435 - "x64"
4436 - "x86"
4437```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004438### <a name="var_host_os"></a>**host_os**: [string] The operating system that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004439
4440```
4441 This value is exposed so that cross-compiles can access the host build
4442 system's settings.
4443
4444 This value should generally be treated as read-only. It, however, is not used
4445 internally by GN for any purpose.
4446```
4447
4448#### **Some possible values**
4449
4450```
4451 - "linux"
4452 - "mac"
4453 - "win"
4454```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004455### <a name="var_invoker"></a>**invoker**: [string] The invoking scope inside a template.
Brett Wilson796ed472018-07-16 15:11:09 -07004456
4457```
4458 Inside a template invocation, this variable refers to the scope of the
4459 invoker of the template. Outside of template invocations, this variable is
4460 undefined.
4461
4462 All of the variables defined inside the template invocation are accessible as
4463 members of the "invoker" scope. This is the way that templates read values
4464 set by the callers.
4465
4466 This is often used with "defined" to see if a value is set on the invoking
4467 scope.
4468
4469 See "gn help template" for more examples.
4470```
4471
4472#### **Example**
4473
4474```
4475 template("my_template") {
4476 print(invoker.sources) # Prints [ "a.cc", "b.cc" ]
4477 print(defined(invoker.foo)) # Prints false.
4478 print(defined(invoker.bar)) # Prints true.
4479 }
4480
4481 my_template("doom_melon") {
4482 sources = [ "a.cc", "b.cc" ]
4483 bar = 123
4484 }
4485```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004486### <a name="var_python_path"></a>**python_path**: Absolute path of Python.
Brett Wilson796ed472018-07-16 15:11:09 -07004487
4488```
4489 Normally used in toolchain definitions if running some command requires
4490 Python. You will normally not need this when invoking scripts since GN
4491 automatically finds it for you.
4492```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004493### <a name="var_root_build_dir"></a>**root_build_dir**: [string] Directory where build commands are run.
Brett Wilson796ed472018-07-16 15:11:09 -07004494
4495```
4496 This is the root build output directory which will be the current directory
4497 when executing all compilers and scripts.
4498
4499 Most often this is used with rebase_path (see "gn help rebase_path") to
4500 convert arguments to be relative to a script's current directory.
4501```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004502### <a name="var_root_gen_dir"></a>**root_gen_dir**: Directory for the toolchain's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004503
4504```
4505 Absolute path to the root of the generated output directory tree for the
4506 current toolchain. An example would be "//out/Debug/gen" for the default
4507 toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain.
4508
4509 This is primarily useful for setting up include paths for generated files. If
4510 you are passing this to a script, you will want to pass it through
4511 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4512 build directory.
4513
4514 See also "target_gen_dir" which is usually a better location for generated
4515 files. It will be inside the root generated dir.
4516```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004517### <a name="var_root_out_dir"></a>**root_out_dir**: [string] Root directory for toolchain output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004518
4519```
4520 Absolute path to the root of the output directory tree for the current
4521 toolchain. It will not have a trailing slash.
4522
4523 For the default toolchain this will be the same as the root_build_dir. An
4524 example would be "//out/Debug" for the default toolchain, or
4525 "//out/Debug/arm" for the "arm" toolchain.
4526
4527 This is primarily useful for setting up script calls. If you are passing this
4528 to a script, you will want to pass it through rebase_path() (see "gn help
4529 rebase_path") to convert it to be relative to the build directory.
4530
4531 See also "target_out_dir" which is usually a better location for output
4532 files. It will be inside the root output dir.
4533```
4534
4535#### **Example**
4536
4537```
4538 action("myscript") {
4539 # Pass the output dir to the script.
4540 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
4541 }
4542```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004543### <a name="var_target_cpu"></a>**target_cpu**: The desired cpu architecture for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004544
4545```
4546 This value should be used to indicate the desired architecture for the
4547 primary objects of the build. It will match the cpu architecture of the
4548 default toolchain, but not necessarily the current toolchain.
4549
4550 In many cases, this is the same as "host_cpu", but in the case of
4551 cross-compiles, this can be set to something different. This value is
4552 different from "current_cpu" in that it does not change based on the current
4553 toolchain. When writing rules, "current_cpu" should be used rather than
4554 "target_cpu" most of the time.
4555
4556 This value is not used internally by GN for any purpose, so it may be set to
4557 whatever value is needed for the build. GN defaults this value to the empty
4558 string ("") and the configuration files should set it to an appropriate value
4559 (e.g., setting it to the value of "host_cpu") if it is not overridden on the
4560 command line or in the args.gn file.
4561```
4562
4563#### **Possible values**
4564
4565```
4566 - "x86"
4567 - "x64"
4568 - "arm"
4569 - "arm64"
4570 - "mipsel"
Tyler Mandry4a648092022-02-15 19:47:09 +00004571 - "mips64el"
4572 - "s390x"
4573 - "ppc64"
4574 - "riscv32"
4575 - "riscv64"
4576 - "e2k"
4577 - "loong64"
Brett Wilson796ed472018-07-16 15:11:09 -07004578```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004579### <a name="var_target_gen_dir"></a>**target_gen_dir**: Directory for a target's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004580
4581```
4582 Absolute path to the target's generated file directory. This will be the
4583 "root_gen_dir" followed by the relative path to the current build file. If
4584 your file is in "//tools/doom_melon" then target_gen_dir would be
4585 "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash.
4586
4587 This is primarily useful for setting up include paths for generated files. If
4588 you are passing this to a script, you will want to pass it through
4589 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4590 build directory.
4591
4592 See also "gn help root_gen_dir".
4593```
4594
4595#### **Example**
4596
4597```
4598 action("myscript") {
4599 # Pass the generated output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004600 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004601 }
4602```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004603### <a name="var_target_name"></a>**target_name**: [string] The name of the current target.
Brett Wilson796ed472018-07-16 15:11:09 -07004604
4605```
4606 Inside a target or template invocation, this variable refers to the name
4607 given to the target or template invocation. Outside of these, this variable
4608 is undefined.
4609
4610 This is most often used in template definitions to name targets defined in
4611 the template based on the name of the invocation. This is necessary both to
4612 ensure generated targets have unique names and to generate a target with the
4613 exact name of the invocation that other targets can depend on.
4614
4615 Be aware that this value will always reflect the innermost scope. So when
4616 defining a target inside a template, target_name will refer to the target
4617 rather than the template invocation. To get the name of the template
4618 invocation in this case, you should save target_name to a temporary variable
4619 outside of any target definitions.
4620
4621 See "gn help template" for more examples.
4622```
4623
4624#### **Example**
4625
4626```
4627 executable("doom_melon") {
4628 print(target_name) # Prints "doom_melon".
4629 }
4630
4631 template("my_template") {
4632 print(target_name) # Prints "space_ray" when invoked below.
4633
4634 executable(target_name + "_impl") {
4635 print(target_name) # Prints "space_ray_impl".
4636 }
4637 }
4638
4639 my_template("space_ray") {
4640 }
4641```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004642### <a name="var_target_os"></a>**target_os**: The desired operating system for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004643
4644```
4645 This value should be used to indicate the desired operating system for the
4646 primary object(s) of the build. It will match the OS of the default
4647 toolchain.
4648
4649 In many cases, this is the same as "host_os", but in the case of
4650 cross-compiles, it may be different. This variable differs from "current_os"
4651 in that it can be referenced from inside any toolchain and will always return
4652 the initial value.
4653
4654 This should be set to the most specific value possible. So, "android" or
4655 "chromeos" should be used instead of "linux" where applicable, even though
4656 Android and ChromeOS are both Linux variants. This can mean that one needs to
4657 write
4658
4659 if (target_os == "android" || target_os == "linux") {
4660 # ...
4661 }
4662
4663 and so forth.
4664
4665 This value is not used internally by GN for any purpose, so it may be set to
4666 whatever value is needed for the build. GN defaults this value to the empty
4667 string ("") and the configuration files should set it to an appropriate value
4668 (e.g., setting it to the value of "host_os") if it is not set via the command
4669 line or in the args.gn file.
4670```
4671
4672#### **Possible values**
4673
4674```
4675 - "android"
4676 - "chromeos"
4677 - "ios"
4678 - "linux"
4679 - "nacl"
4680 - "mac"
4681 - "win"
4682```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004683### <a name="var_target_out_dir"></a>**target_out_dir**: [string] Directory for target output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004684
4685```
4686 Absolute path to the target's generated file directory. If your current
4687 target is in "//tools/doom_melon" then this value might be
4688 "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash.
4689
4690 This is primarily useful for setting up arguments for calling scripts. If you
4691 are passing this to a script, you will want to pass it through rebase_path()
4692 (see "gn help rebase_path") to convert it to be relative to the build
4693 directory.
4694
4695 See also "gn help root_out_dir".
4696```
4697
4698#### **Example**
4699
4700```
4701 action("myscript") {
4702 # Pass the output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004703 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004704 }
4705```
4706## <a name="target_variables"></a>Variables you set in targets
4707
Julie Hockettce1fa072019-05-07 17:44:37 -07004708### <a name="var_aliased_deps"></a>**aliased_deps**: [scope] Set of crate-dependency pairs.
4709
4710```
4711 Valid for `rust_library` targets and `executable`, `static_library`, and
4712 `shared_library` targets that contain Rust sources.
4713
4714 A scope, each key indicating the renamed crate and the corresponding value
4715 specifying the label of the dependency producing the relevant binary.
4716
4717 All dependencies listed in this field *must* be listed as deps of the target.
4718
4719 executable("foo") {
4720 sources = [ "main.rs" ]
4721 deps = [ "//bar" ]
4722 }
4723
4724 This target would compile the `foo` crate with the following `extern` flag:
4725 `rustc ...command... --extern bar=<build_out_dir>/obj/bar`
4726
4727 executable("foo") {
4728 sources = [ "main.rs" ]
4729 deps = [ ":bar" ]
4730 aliased_deps = {
4731 bar_renamed = ":bar"
4732 }
4733 }
4734
4735 With the addition of `aliased_deps`, above target would instead compile with:
4736 `rustc ...command... --extern bar_renamed=<build_out_dir>/obj/bar`
4737```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004738### <a name="var_all_dependent_configs"></a>**all_dependent_configs**: Configs to be forced on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07004739
4740```
4741 A list of config labels.
4742
4743 All targets depending on this one, and recursively, all targets depending on
4744 those, will have the configs listed in this variable added to them. These
4745 configs will also apply to the current target.
4746
4747 This addition happens in a second phase once a target and all of its
4748 dependencies have been resolved. Therefore, a target will not see these
4749 force-added configs in their "configs" variable while the script is running,
4750 and they can not be removed. As a result, this capability should generally
4751 only be used to add defines and include directories necessary to compile a
4752 target's headers.
4753
4754 See also "public_configs".
4755```
4756
4757#### **Ordering of flags and values**
4758
4759```
4760 1. Those set on the current target (not in a config).
4761 2. Those set on the "configs" on the target in order that the
4762 configs appear in the list.
4763 3. Those set on the "all_dependent_configs" on the target in order
4764 that the configs appear in the list.
4765 4. Those set on the "public_configs" on the target in order that
4766 those configs appear in the list.
4767 5. all_dependent_configs pulled from dependencies, in the order of
4768 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004769 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07004770 6. public_configs pulled from dependencies, in the order of the
4771 "deps" list. If a dependency is public, they will be applied
4772 recursively.
4773```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004774### <a name="var_allow_circular_includes_from"></a>**allow_circular_includes_from**: Permit includes from deps.
Brett Wilson796ed472018-07-16 15:11:09 -07004775
4776```
4777 A list of target labels. Must be a subset of the target's "deps". These
4778 targets will be permitted to include headers from the current target despite
4779 the dependency going in the opposite direction.
4780
4781 When you use this, both targets must be included in a final binary for it to
4782 link. To keep linker errors from happening, it is good practice to have all
4783 external dependencies depend only on one of the two targets, and to set the
4784 visibility on the other to enforce this. Thus the targets will always be
4785 linked together in any output.
4786```
4787
4788#### **Details**
4789
4790```
4791 Normally, for a file in target A to include a file from target B, A must list
4792 B as a dependency. This invariant is enforced by the "gn check" command (and
4793 the --check flag to "gn gen" -- see "gn help check").
4794
4795 Sometimes, two targets might be the same unit for linking purposes (two
4796 source sets or static libraries that would always be linked together in a
4797 final executable or shared library) and they each include headers from the
4798 other: you want A to be able to include B's headers, and B to include A's
4799 headers. This is not an ideal situation but is sometimes unavoidable.
4800
4801 This list, if specified, lists which of the dependencies of the current
4802 target can include header files from the current target. That is, if A
4803 depends on B, B can only include headers from A if it is in A's
4804 allow_circular_includes_from list. Normally includes must follow the
4805 direction of dependencies, this flag allows them to go in the opposite
4806 direction.
4807```
4808
4809#### **Danger**
4810
4811```
4812 In the above example, A's headers are likely to include headers from A's
4813 dependencies. Those dependencies may have public_configs that apply flags,
4814 defines, and include paths that make those headers work properly.
4815
4816 With allow_circular_includes_from, B can include A's headers, and
4817 transitively from A's dependencies, without having the dependencies that
4818 would bring in the public_configs those headers need. The result may be
4819 errors or inconsistent builds.
4820
4821 So when you use allow_circular_includes_from, make sure that any compiler
4822 settings, flags, and include directories are the same between both targets
4823 (consider putting such things in a shared config they can both reference).
4824 Make sure the dependencies are also the same (you might consider a group to
4825 collect such dependencies they both depend on).
4826```
4827
4828#### **Example**
4829
4830```
4831 source_set("a") {
4832 deps = [ ":b", ":a_b_shared_deps" ]
4833 allow_circular_includes_from = [ ":b" ]
4834 ...
4835 }
4836
4837 source_set("b") {
4838 deps = [ ":a_b_shared_deps" ]
4839 # Sources here can include headers from a despite lack of deps.
4840 ...
4841 }
4842
4843 group("a_b_shared_deps") {
4844 public_deps = [ ":c" ]
4845 }
4846```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004847### <a name="var_arflags"></a>**arflags**: Arguments passed to static_library archiver.
Brett Wilson796ed472018-07-16 15:11:09 -07004848
4849```
4850 A list of flags passed to the archive/lib command that creates static
4851 libraries.
4852
4853 arflags are NOT pushed to dependents, so applying arflags to source sets or
4854 any other target type will be a no-op. As with ldflags, you could put the
4855 arflags in a config and set that as a public or "all dependent" config, but
4856 that will likely not be what you want. If you have a chain of static
4857 libraries dependent on each other, this can cause the flags to propagate up
4858 to other static libraries. Due to the nature of how arflags are typically
4859 used, you will normally want to apply them directly on static_library targets
4860 themselves.
4861```
4862
4863#### **Ordering of flags and values**
4864
4865```
4866 1. Those set on the current target (not in a config).
4867 2. Those set on the "configs" on the target in order that the
4868 configs appear in the list.
4869 3. Those set on the "all_dependent_configs" on the target in order
4870 that the configs appear in the list.
4871 4. Those set on the "public_configs" on the target in order that
4872 those configs appear in the list.
4873 5. all_dependent_configs pulled from dependencies, in the order of
4874 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004875 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07004876 6. public_configs pulled from dependencies, in the order of the
4877 "deps" list. If a dependency is public, they will be applied
4878 recursively.
4879```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004880### <a name="var_args"></a>**args**: (target variable) Arguments passed to an action.
Brett Wilson796ed472018-07-16 15:11:09 -07004881
4882```
4883 For action and action_foreach targets, args is the list of arguments to pass
4884 to the script. Typically you would use source expansion (see "gn help
4885 source_expansion") to insert the source file names.
4886
Adrian Taylorc0d4f8b2022-03-25 15:05:57 -07004887 Args can also expand the substitution patterns corresponding to config
4888 variables in the same way that compiler tools (see "gn help tool") do. These
4889 allow actions that run compiler or compiler-like tools to access the results
4890 of propagating configs through the build graph. For example:
4891
4892 args = [ "{{defines}}", "{{include_dirs}}", "{{rustenv}}", "--input-file",
4893 "{{source}}" ]
4894
Brett Wilson796ed472018-07-16 15:11:09 -07004895 See also "gn help action" and "gn help action_foreach".
4896```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004897### <a name="var_asmflags"></a>**asmflags**: Flags passed to the assembler.
Brett Wilson796ed472018-07-16 15:11:09 -07004898
4899```
4900 A list of strings.
4901
4902 "asmflags" are passed to any invocation of a tool that takes an .asm or .S
4903 file as input.
4904```
4905
4906#### **Ordering of flags and values**
4907
4908```
4909 1. Those set on the current target (not in a config).
4910 2. Those set on the "configs" on the target in order that the
4911 configs appear in the list.
4912 3. Those set on the "all_dependent_configs" on the target in order
4913 that the configs appear in the list.
4914 4. Those set on the "public_configs" on the target in order that
4915 those configs appear in the list.
4916 5. all_dependent_configs pulled from dependencies, in the order of
4917 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004918 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07004919 6. public_configs pulled from dependencies, in the order of the
4920 "deps" list. If a dependency is public, they will be applied
4921 recursively.
4922```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004923### <a name="var_assert_no_deps"></a>**assert_no_deps**: Ensure no deps on these targets.
Brett Wilson796ed472018-07-16 15:11:09 -07004924
4925```
4926 A list of label patterns.
4927
4928 This list is a list of patterns that must not match any of the transitive
4929 dependencies of the target. These include all public, private, and data
4930 dependencies, and cross shared library boundaries. This allows you to express
4931 that undesirable code isn't accidentally added to downstream dependencies in
4932 a way that might otherwise be difficult to notice.
4933
4934 Checking does not cross executable boundaries. If a target depends on an
4935 executable, it's assumed that the executable is a tool that is producing part
4936 of the build rather than something that is linked and distributed. This
4937 allows assert_no_deps to express what is distributed in the final target
4938 rather than depend on the internal build steps (which may include
4939 non-distributable code).
4940
4941 See "gn help label_pattern" for the format of the entries in the list. These
4942 patterns allow blacklisting individual targets or whole directory
4943 hierarchies.
4944
4945 Sometimes it is desirable to enforce that many targets have no dependencies
4946 on a target or set of targets. One efficient way to express this is to create
4947 a group with the assert_no_deps rule on it, and make that group depend on all
4948 targets you want to apply that assertion to.
4949```
4950
4951#### **Example**
4952
4953```
4954 executable("doom_melon") {
4955 deps = [ "//foo:bar" ]
4956 ...
4957 assert_no_deps = [
4958 "//evil/*", # Don't link any code from the evil directory.
4959 "//foo:test_support", # This target is also disallowed.
4960 ]
4961 }
4962```
Sylvain Defresne89e64252020-08-07 13:01:06 +02004963### <a name="var_bridge_header"></a>**bridge_header**: [string] Path to C/Objective-C compatibility header.
4964
4965```
4966 Valid for binary targets that contain Swift sources.
4967
4968 Path to an header that includes C/Objective-C functions and types that
4969 needs to be made available to the Swift module.
4970```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004971### <a name="var_bundle_contents_dir"></a>**bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in
Brett Wilson796ed472018-07-16 15:11:09 -07004972```
4973 create_bundle.
4974
4975 A string corresponding to a path in $root_build_dir.
4976
4977 This string is used by the "create_bundle" target to expand the
4978 {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must
4979 correspond to a path under "bundle_root_dir".
4980
4981 See "gn help bundle_root_dir" for examples.
4982```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004983### <a name="var_bundle_deps_filter"></a>**bundle_deps_filter**: [label list] A list of labels that are filtered out.
Brett Wilson796ed472018-07-16 15:11:09 -07004984
4985```
4986 A list of target labels.
4987
4988 This list contains target label patterns that should be filtered out when
4989 creating the bundle. Any target matching one of those label will be removed
4990 from the dependencies of the create_bundle target.
4991
4992 This is mostly useful when creating application extension bundle as the
4993 application extension has access to runtime resources from the application
4994 bundle and thus do not require a second copy.
4995
4996 See "gn help create_bundle" for more information.
4997```
4998
4999#### **Example**
5000
5001```
5002 create_bundle("today_extension") {
5003 deps = [
5004 "//base"
5005 ]
5006 bundle_root_dir = "$root_out_dir/today_extension.appex"
5007 bundle_deps_filter = [
5008 # The extension uses //base but does not use any function calling into
5009 # third_party/icu and thus does not need the icudtl.dat file.
5010 "//third_party/icu:icudata",
5011 ]
5012 }
5013```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02005014### <a name="var_bundle_executable_dir"></a>**bundle_executable_dir**
Brett Wilson796ed472018-07-16 15:11:09 -07005015
Joe Armstrongb199e542019-03-21 09:40:08 +08005016```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02005017 bundle_executable_dir: Expansion of {{bundle_executable_dir}} in
5018 create_bundle.
5019
Brett Wilson796ed472018-07-16 15:11:09 -07005020 A string corresponding to a path in $root_build_dir.
5021
5022 This string is used by the "create_bundle" target to expand the
5023 {{bundle_executable_dir}} of the "bundle_data" target it depends on. This
5024 must correspond to a path under "bundle_root_dir".
5025
5026 See "gn help bundle_root_dir" for examples.
5027```
Joe Armstrongb199e542019-03-21 09:40:08 +08005028### <a name="var_bundle_resources_dir"></a>**bundle_resources_dir**
5029
Brett Wilson796ed472018-07-16 15:11:09 -07005030```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02005031 bundle_resources_dir: Expansion of {{bundle_resources_dir}} in
5032 create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07005033
5034 A string corresponding to a path in $root_build_dir.
5035
5036 This string is used by the "create_bundle" target to expand the
5037 {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must
5038 correspond to a path under "bundle_root_dir".
5039
5040 See "gn help bundle_root_dir" for examples.
5041```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005042### <a name="var_bundle_root_dir"></a>**bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07005043
5044```
5045 A string corresponding to a path in root_build_dir.
5046
5047 This string is used by the "create_bundle" target to expand the
5048 {{bundle_root_dir}} of the "bundle_data" target it depends on. This must
5049 correspond to a path under root_build_dir.
5050```
5051
5052#### **Example**
5053
5054```
5055 bundle_data("info_plist") {
5056 sources = [ "Info.plist" ]
5057 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
5058 }
5059
5060 create_bundle("doom_melon.app") {
5061 deps = [ ":info_plist" ]
5062 bundle_root_dir = "${root_build_dir}/doom_melon.app"
5063 bundle_contents_dir = "${bundle_root_dir}/Contents"
5064 bundle_resources_dir = "${bundle_contents_dir}/Resources"
5065 bundle_executable_dir = "${bundle_contents_dir}/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07005066 }
5067```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005068### <a name="var_cflags"></a>**cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005069
5070```
5071 A list of strings.
5072
5073 "cflags" are passed to all invocations of the C, C++, Objective C, and
5074 Objective C++ compilers.
5075
5076 To target one of these variants individually, use "cflags_c", "cflags_cc",
5077 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5078 versions of cflags* will be appended on the compiler command line after
5079 "cflags".
5080
Sylvain Defresne89e64252020-08-07 13:01:06 +02005081 See also "asmflags" for flags for assembly-language files and "swiftflags"
5082 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005083```
5084
5085#### **Ordering of flags and values**
5086
5087```
5088 1. Those set on the current target (not in a config).
5089 2. Those set on the "configs" on the target in order that the
5090 configs appear in the list.
5091 3. Those set on the "all_dependent_configs" on the target in order
5092 that the configs appear in the list.
5093 4. Those set on the "public_configs" on the target in order that
5094 those configs appear in the list.
5095 5. all_dependent_configs pulled from dependencies, in the order of
5096 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005097 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005098 6. public_configs pulled from dependencies, in the order of the
5099 "deps" list. If a dependency is public, they will be applied
5100 recursively.
5101```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005102### <a name="var_cflags_c"></a>**cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005103
5104```
5105 A list of strings.
5106
5107 "cflags" are passed to all invocations of the C, C++, Objective C, and
5108 Objective C++ compilers.
5109
5110 To target one of these variants individually, use "cflags_c", "cflags_cc",
5111 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5112 versions of cflags* will be appended on the compiler command line after
5113 "cflags".
5114
Sylvain Defresne89e64252020-08-07 13:01:06 +02005115 See also "asmflags" for flags for assembly-language files and "swiftflags"
5116 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005117```
5118
5119#### **Ordering of flags and values**
5120
5121```
5122 1. Those set on the current target (not in a config).
5123 2. Those set on the "configs" on the target in order that the
5124 configs appear in the list.
5125 3. Those set on the "all_dependent_configs" on the target in order
5126 that the configs appear in the list.
5127 4. Those set on the "public_configs" on the target in order that
5128 those configs appear in the list.
5129 5. all_dependent_configs pulled from dependencies, in the order of
5130 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005131 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005132 6. public_configs pulled from dependencies, in the order of the
5133 "deps" list. If a dependency is public, they will be applied
5134 recursively.
5135```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005136### <a name="var_cflags_cc"></a>**cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005137
5138```
5139 A list of strings.
5140
5141 "cflags" are passed to all invocations of the C, C++, Objective C, and
5142 Objective C++ compilers.
5143
5144 To target one of these variants individually, use "cflags_c", "cflags_cc",
5145 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5146 versions of cflags* will be appended on the compiler command line after
5147 "cflags".
5148
Sylvain Defresne89e64252020-08-07 13:01:06 +02005149 See also "asmflags" for flags for assembly-language files and "swiftflags"
5150 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005151```
5152
5153#### **Ordering of flags and values**
5154
5155```
5156 1. Those set on the current target (not in a config).
5157 2. Those set on the "configs" on the target in order that the
5158 configs appear in the list.
5159 3. Those set on the "all_dependent_configs" on the target in order
5160 that the configs appear in the list.
5161 4. Those set on the "public_configs" on the target in order that
5162 those configs appear in the list.
5163 5. all_dependent_configs pulled from dependencies, in the order of
5164 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005165 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005166 6. public_configs pulled from dependencies, in the order of the
5167 "deps" list. If a dependency is public, they will be applied
5168 recursively.
5169```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005170### <a name="var_cflags_objc"></a>**cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005171
5172```
5173 A list of strings.
5174
5175 "cflags" are passed to all invocations of the C, C++, Objective C, and
5176 Objective C++ compilers.
5177
5178 To target one of these variants individually, use "cflags_c", "cflags_cc",
5179 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5180 versions of cflags* will be appended on the compiler command line after
5181 "cflags".
5182
Sylvain Defresne89e64252020-08-07 13:01:06 +02005183 See also "asmflags" for flags for assembly-language files and "swiftflags"
5184 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005185```
5186
5187#### **Ordering of flags and values**
5188
5189```
5190 1. Those set on the current target (not in a config).
5191 2. Those set on the "configs" on the target in order that the
5192 configs appear in the list.
5193 3. Those set on the "all_dependent_configs" on the target in order
5194 that the configs appear in the list.
5195 4. Those set on the "public_configs" on the target in order that
5196 those configs appear in the list.
5197 5. all_dependent_configs pulled from dependencies, in the order of
5198 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005199 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005200 6. public_configs pulled from dependencies, in the order of the
5201 "deps" list. If a dependency is public, they will be applied
5202 recursively.
5203```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005204### <a name="var_cflags_objcc"></a>**cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005205
5206```
5207 A list of strings.
5208
5209 "cflags" are passed to all invocations of the C, C++, Objective C, and
5210 Objective C++ compilers.
5211
5212 To target one of these variants individually, use "cflags_c", "cflags_cc",
5213 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5214 versions of cflags* will be appended on the compiler command line after
5215 "cflags".
5216
Sylvain Defresne89e64252020-08-07 13:01:06 +02005217 See also "asmflags" for flags for assembly-language files and "swiftflags"
5218 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005219```
5220
5221#### **Ordering of flags and values**
5222
5223```
5224 1. Those set on the current target (not in a config).
5225 2. Those set on the "configs" on the target in order that the
5226 configs appear in the list.
5227 3. Those set on the "all_dependent_configs" on the target in order
5228 that the configs appear in the list.
5229 4. Those set on the "public_configs" on the target in order that
5230 those configs appear in the list.
5231 5. all_dependent_configs pulled from dependencies, in the order of
5232 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005233 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005234 6. public_configs pulled from dependencies, in the order of the
5235 "deps" list. If a dependency is public, they will be applied
5236 recursively.
5237```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005238### <a name="var_check_includes"></a>**check_includes**: [boolean] Controls whether a target's files are checked.
Brett Wilson796ed472018-07-16 15:11:09 -07005239
5240```
5241 When true (the default), the "gn check" command (as well as "gn gen" with the
5242 --check flag) will check this target's sources and headers for proper
5243 dependencies.
5244
5245 When false, the files in this target will be skipped by default. This does
5246 not affect other targets that depend on the current target, it just skips
5247 checking the includes of the current target's files.
5248
5249 If there are a few conditionally included headers that trip up checking, you
5250 can exclude headers individually by annotating them with "nogncheck" (see "gn
5251 help nogncheck").
5252
5253 The topic "gn help check" has general information on how checking works and
5254 advice on how to pass a check in problematic cases.
5255```
5256
5257#### **Example**
5258
5259```
5260 source_set("busted_includes") {
5261 # This target's includes are messed up, exclude it from checking.
5262 check_includes = false
5263 ...
5264 }
5265```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005266### <a name="var_code_signing_args"></a>**code_signing_args**: [string list] Arguments passed to code signing script.
Brett Wilson796ed472018-07-16 15:11:09 -07005267
5268```
5269 For create_bundle targets, code_signing_args is the list of arguments to pass
5270 to the code signing script. Typically you would use source expansion (see "gn
5271 help source_expansion") to insert the source file names.
5272
5273 See also "gn help create_bundle".
5274```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005275### <a name="var_code_signing_outputs"></a>**code_signing_outputs**: [file list] Output files for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005276
5277```
5278 Outputs from the code signing step of a create_bundle target. Must refer to
5279 files in the build directory.
5280
5281 See also "gn help create_bundle".
5282```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005283### <a name="var_code_signing_script"></a>**code_signing_script**: [file name] Script for code signing."
Brett Wilson796ed472018-07-16 15:11:09 -07005284
5285```
5286 An absolute or buildfile-relative file name of a Python script to run for a
5287 create_bundle target to perform code signing step.
5288
5289 See also "gn help create_bundle".
5290```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005291### <a name="var_code_signing_sources"></a>**code_signing_sources**: [file list] Sources for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005292
5293```
5294 A list of files used as input for code signing script step of a create_bundle
5295 target. Non-absolute paths will be resolved relative to the current build
5296 file.
5297
5298 See also "gn help create_bundle".
5299```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005300### <a name="var_complete_static_lib"></a>**complete_static_lib**: [boolean] Links all deps into a static library.
Brett Wilson796ed472018-07-16 15:11:09 -07005301
5302```
5303 A static library normally doesn't include code from dependencies, but instead
5304 forwards the static libraries and source sets in its deps up the dependency
5305 chain until a linkable target (an executable or shared library) is reached.
5306 The final linkable target only links each static library once, even if it
5307 appears more than once in its dependency graph.
5308
5309 In some cases the static library might be the final desired output. For
5310 example, you may be producing a static library for distribution to third
5311 parties. In this case, the static library should include code for all
5312 dependencies in one complete package. However, complete static libraries
5313 themselves are never linked into other complete static libraries. All
5314 complete static libraries are for distribution and linking them in would
5315 cause code duplication in this case. If the static library is not for
5316 distribution, it should not be complete.
5317
5318 GN treats non-complete static libraries as source sets when they are linked
5319 into complete static libraries. This is done because some tools like AR do
5320 not handle dependent static libraries properly. This makes it easier to write
5321 "alink" rules.
5322
5323 In rare cases it makes sense to list a header in more than one target if it
5324 could be considered conceptually a member of both. libraries.
5325```
5326
5327#### **Example**
5328
5329```
5330 static_library("foo") {
5331 complete_static_lib = true
5332 deps = [ "bar" ]
5333 }
5334```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005335### <a name="var_configs"></a>**configs**: Configs applying to this target or config.
Brett Wilson796ed472018-07-16 15:11:09 -07005336
5337```
5338 A list of config labels.
5339```
5340
5341#### **Configs on a target**
5342
5343```
5344 When used on a target, the include_dirs, defines, etc. in each config are
5345 appended in the order they appear to the compile command for each file in the
5346 target. They will appear after the include_dirs, defines, etc. that the
5347 target sets directly.
5348
5349 Since configs apply after the values set on a target, directly setting a
5350 compiler flag will prepend it to the command line. If you want to append a
5351 flag instead, you can put that flag in a one-off config and append that
5352 config to the target's configs list.
5353
5354 The build configuration script will generally set up the default configs
5355 applying to a given target type (see "set_defaults"). When a target is being
5356 defined, it can add to or remove from this list.
5357```
5358
5359#### **Configs on a config**
5360
5361```
5362 It is possible to create composite configs by specifying configs on a config.
5363 One might do this to forward values, or to factor out blocks of settings from
5364 very large configs into more manageable named chunks.
5365
5366 In this case, the composite config is expanded to be the concatenation of its
5367 own values, and in order, the values from its sub-configs *before* anything
5368 else happens. This has some ramifications:
5369
5370 - A target has no visibility into a config's sub-configs. Target code only
5371 sees the name of the composite config. It can't remove sub-configs or opt
5372 in to only parts of it. The composite config may not even be defined
5373 before the target is.
5374
5375 - You can get duplication of values if a config is listed twice, say, on a
5376 target and in a sub-config that also applies. In other cases, the configs
5377 applying to a target are de-duped. It's expected that if a config is
5378 listed as a sub-config that it is only used in that context. (Note that
5379 it's possible to fix this and de-dupe, but it's not normally relevant and
5380 complicates the implementation.)
5381```
5382
5383#### **Ordering of flags and values**
5384
5385```
5386 1. Those set on the current target (not in a config).
5387 2. Those set on the "configs" on the target in order that the
5388 configs appear in the list.
5389 3. Those set on the "all_dependent_configs" on the target in order
5390 that the configs appear in the list.
5391 4. Those set on the "public_configs" on the target in order that
5392 those configs appear in the list.
5393 5. all_dependent_configs pulled from dependencies, in the order of
5394 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005395 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005396 6. public_configs pulled from dependencies, in the order of the
5397 "deps" list. If a dependency is public, they will be applied
5398 recursively.
5399```
5400
5401#### **Example**
5402
5403```
5404 # Configs on a target.
5405 source_set("foo") {
5406 # Don't use the default RTTI config that BUILDCONFIG applied to us.
5407 configs -= [ "//build:no_rtti" ]
5408
5409 # Add some of our own settings.
5410 configs += [ ":mysettings" ]
5411 }
5412
5413 # Create a default_optimization config that forwards to one of a set of more
5414 # specialized configs depending on build flags. This pattern is useful
5415 # because it allows a target to opt in to either a default set, or a more
5416 # specific set, while avoid duplicating the settings in two places.
5417 config("super_optimization") {
5418 cflags = [ ... ]
5419 }
5420 config("default_optimization") {
5421 if (optimize_everything) {
5422 configs = [ ":super_optimization" ]
5423 } else {
5424 configs = [ ":no_optimization" ]
5425 }
5426 }
5427```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005428### <a name="var_contents"></a>**contents**: Contents to write to file.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005429
5430```
5431 The contents of the file for a generated_file target.
5432 See "gn help generated_file".
5433```
Julie Hockettce1fa072019-05-07 17:44:37 -07005434### <a name="var_crate_name"></a>**crate_name**: [string] The name for the compiled crate.
5435
5436```
5437 Valid for `rust_library` targets and `executable`, `static_library`,
5438 `shared_library`, and `source_set` targets that contain Rust sources.
5439
5440 If crate_name is not set, then this rule will use the target name.
5441```
5442### <a name="var_crate_root"></a>**crate_root**: [string] The root source file for a binary or library.
5443
5444```
5445 Valid for `rust_library` targets and `executable`, `static_library`,
5446 `shared_library`, and `source_set` targets that contain Rust sources.
5447
5448 This file is usually the `main.rs` or `lib.rs` for binaries and libraries,
5449 respectively.
5450
5451 If crate_root is not set, then this rule will look for a lib.rs file (or
5452 main.rs for executable) or a single file in sources, if sources contains
5453 only one file.
5454```
5455### <a name="var_crate_type"></a>**crate_type**: [string] The type of linkage to use on a shared_library.
5456
5457```
5458 Valid for `rust_library` targets and `executable`, `static_library`,
5459 `shared_library`, and `source_set` targets that contain Rust sources.
5460
5461 Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib".
5462 This field sets the `crate-type` attribute for the `rustc` tool on static
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005463 libraries, as well as the appropriate output extension in the
Julie Hockettce1fa072019-05-07 17:44:37 -07005464 `rust_output_extension` attribute. Since outputs must be explicit, the `lib`
5465 crate type (where the Rust compiler produces what it thinks is the
5466 appropriate library type) is not supported.
5467
5468 It should be noted that the "dylib" crate type in Rust is unstable in the set
5469 of symbols it exposes, and most usages today are potentially wrong and will
5470 be broken in the future.
5471
5472 Static libraries, rust libraries, and executables have this field set
5473 automatically.
5474```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005475### <a name="var_data"></a>**data**: Runtime data file dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005476
5477```
5478 Lists files or directories required to run the given target. These are
5479 typically data files or directories of data files. The paths are interpreted
5480 as being relative to the current build file. Since these are runtime
5481 dependencies, they do not affect which targets are built or when. To declare
5482 input files to a script, use "inputs".
5483
5484 Appearing in the "data" section does not imply any special handling such as
5485 copying them to the output directory. This is just used for declaring runtime
5486 dependencies. Runtime dependencies can be queried using the "runtime_deps"
5487 category of "gn desc" or written during build generation via
5488 "--runtime-deps-list-file".
5489
5490 GN doesn't require data files to exist at build-time. So actions that produce
5491 files that are in turn runtime dependencies can list those generated files
5492 both in the "outputs" list as well as the "data" list.
5493
5494 By convention, directories are listed with a trailing slash:
5495 data = [ "test/data/" ]
5496 However, no verification is done on these so GN doesn't enforce this. The
5497 paths are just rebased and passed along when requested.
5498
5499 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5500 gathering data. See "gn help create_bundle" for details.
5501
5502 See "gn help runtime_deps" for how these are used.
5503```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005504### <a name="var_data_deps"></a>**data_deps**: Non-linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005505
5506```
5507 A list of target labels.
5508
5509 Specifies dependencies of a target that are not actually linked into the
5510 current target. Such dependencies will be built and will be available at
5511 runtime.
5512
5513 This is normally used for things like plugins or helper programs that a
5514 target needs at runtime.
5515
5516 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5517 gathering data_deps. See "gn help create_bundle" for details.
5518
5519 See also "gn help deps" and "gn help data".
5520```
5521
5522#### **Example**
5523
5524```
5525 executable("foo") {
5526 deps = [ "//base" ]
5527 data_deps = [ "//plugins:my_runtime_plugin" ]
5528 }
5529```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005530### <a name="var_data_keys"></a>**data_keys**: Keys from which to collect metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005531
5532```
5533 These keys are used to identify metadata to collect. If a walked target
5534 defines this key in its metadata, its value will be appended to the resulting
5535 collection.
5536
5537 See "gn help generated_file".
5538```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005539### <a name="var_defines"></a>**defines**: C preprocessor defines.
Brett Wilson796ed472018-07-16 15:11:09 -07005540
5541```
5542 A list of strings
5543
5544 These strings will be passed to the C/C++ compiler as #defines. The strings
5545 may or may not include an "=" to assign a value.
5546```
5547
5548#### **Ordering of flags and values**
5549
5550```
5551 1. Those set on the current target (not in a config).
5552 2. Those set on the "configs" on the target in order that the
5553 configs appear in the list.
5554 3. Those set on the "all_dependent_configs" on the target in order
5555 that the configs appear in the list.
5556 4. Those set on the "public_configs" on the target in order that
5557 those configs appear in the list.
5558 5. all_dependent_configs pulled from dependencies, in the order of
5559 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005560 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005561 6. public_configs pulled from dependencies, in the order of the
5562 "deps" list. If a dependency is public, they will be applied
5563 recursively.
5564```
5565
5566#### **Example**
5567
5568```
5569 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
5570```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005571### <a name="var_depfile"></a>**depfile**: [string] File name for input dependencies for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07005572
5573```
5574 If nonempty, this string specifies that the current action or action_foreach
5575 target will generate the given ".d" file containing the dependencies of the
5576 input. Empty or unset means that the script doesn't generate the files.
5577
5578 A depfile should be used only when a target depends on files that are not
5579 already specified by a target's inputs and sources. Likewise, depfiles should
5580 specify only those dependencies not already included in sources or inputs.
5581
5582 The .d file should go in the target output directory. If you have more than
5583 one source file that the script is being run over, you can use the output
5584 file expansions described in "gn help action_foreach" to name the .d file
Nico Weberad9eab22020-11-15 22:20:28 -05005585 according to the input.
Brett Wilson796ed472018-07-16 15:11:09 -07005586
5587 The format is that of a Makefile and all paths must be relative to the root
5588 build directory. Only one output may be listed and it must match the first
5589 output of the action.
5590
5591 Although depfiles are created by an action, they should not be listed in the
5592 action's "outputs" unless another target will use the file as an input.
5593```
5594
5595#### **Example**
5596
5597```
5598 action_foreach("myscript_target") {
5599 script = "myscript.py"
5600 sources = [ ... ]
5601
5602 # Locate the depfile in the output directory named like the
5603 # inputs but with a ".d" appended.
Hector Dearman84c84312023-01-29 11:50:02 +00005604 depfile = "$target_gen_dir/{{source_name_part}}.d"
Brett Wilson796ed472018-07-16 15:11:09 -07005605
5606 # Say our script uses "-o <d file>" to indicate the depfile.
Hector Dearman84c84312023-01-29 11:50:02 +00005607 args = [ "{{source}}", "-o", rebase_path(depfile, root_build_dir)]
Brett Wilson796ed472018-07-16 15:11:09 -07005608 }
5609```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005610### <a name="var_deps"></a>**deps**: Private linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005611
5612```
5613 A list of target labels.
5614
5615 Specifies private dependencies of a target. Private dependencies are
5616 propagated up the dependency tree and linked to dependent targets, but do not
5617 grant the ability to include headers from the dependency. Public configs are
5618 not forwarded.
5619```
5620
5621#### **Details of dependency propagation**
5622
5623```
5624 Source sets, shared libraries, and non-complete static libraries will be
5625 propagated up the dependency tree across groups, non-complete static
5626 libraries and source sets.
5627
5628 Executables, shared libraries, and complete static libraries will link all
5629 propagated targets and stop propagation. Actions and copy steps also stop
5630 propagation, allowing them to take a library as an input but not force
5631 dependents to link to it.
5632
5633 Propagation of all_dependent_configs and public_configs happens independently
5634 of target type. all_dependent_configs are always propagated across all types
5635 of targets, and public_configs are always propagated across public deps of
5636 all types of targets.
5637
5638 Data dependencies are propagated differently. See "gn help data_deps" and
5639 "gn help runtime_deps".
5640
5641 See also "public_deps".
5642```
Petr Hoseka1413862020-01-03 12:54:33 -08005643### <a name="var_externs"></a>**externs**: [scope] Set of Rust crate-dependency pairs.
5644
5645```
5646 A list, each value being a scope indicating a pair of crate name and the path
5647 to the Rust library.
5648
5649 These libraries will be passed as `--extern crate_name=path` to compiler
5650 invocation containing the current target.
5651```
5652
5653#### **Examples**
5654
5655```
5656 executable("foo") {
5657 sources = [ "main.rs" ]
5658 externs = [{
5659 crate_name = "bar",
5660 path = "path/to/bar.rlib"
5661 }]
5662 }
5663
5664 This target would compile the `foo` crate with the following `extern` flag:
5665 `--extern bar=path/to/bar.rlib`.
5666```
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005667### <a name="var_framework_dirs"></a>**framework_dirs**: [directory list] Additional framework search directories.
5668
5669```
5670 A list of source directories.
5671
5672 The directories in this list will be added to the framework search path for
5673 the files in the affected target.
5674```
5675
5676#### **Ordering of flags and values**
5677
5678```
5679 1. Those set on the current target (not in a config).
5680 2. Those set on the "configs" on the target in order that the
5681 configs appear in the list.
5682 3. Those set on the "all_dependent_configs" on the target in order
5683 that the configs appear in the list.
5684 4. Those set on the "public_configs" on the target in order that
5685 those configs appear in the list.
5686 5. all_dependent_configs pulled from dependencies, in the order of
5687 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005688 more than once, only the first occurrence will be used.
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005689 6. public_configs pulled from dependencies, in the order of the
5690 "deps" list. If a dependency is public, they will be applied
5691 recursively.
5692```
5693
5694#### **Example**
5695
5696```
5697 framework_dirs = [ "src/include", "//third_party/foo" ]
5698```
5699### <a name="var_frameworks"></a>**frameworks**: [name list] Name of frameworks that must be linked.
5700
5701```
5702 A list of framework names.
5703
5704 The frameworks named in that list will be linked with any dynamic link
5705 type target.
5706```
5707
5708#### **Ordering of flags and values**
5709
5710```
5711 1. Those set on the current target (not in a config).
5712 2. Those set on the "configs" on the target in order that the
5713 configs appear in the list.
5714 3. Those set on the "all_dependent_configs" on the target in order
5715 that the configs appear in the list.
5716 4. Those set on the "public_configs" on the target in order that
5717 those configs appear in the list.
5718 5. all_dependent_configs pulled from dependencies, in the order of
5719 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005720 more than once, only the first occurrence will be used.
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005721 6. public_configs pulled from dependencies, in the order of the
5722 "deps" list. If a dependency is public, they will be applied
5723 recursively.
5724```
5725
5726#### **Example**
5727
5728```
5729 frameworks = [ "Foundation.framework", "Foo.framework" ]
5730```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005731### <a name="var_friend"></a>**friend**: Allow targets to include private headers.
Brett Wilson796ed472018-07-16 15:11:09 -07005732
5733```
5734 A list of label patterns (see "gn help label_pattern") that allow dependent
5735 targets to include private headers. Applies to all binary targets.
5736
5737 Normally if a target lists headers in the "public" list (see "gn help
5738 public"), other headers are implicitly marked as private. Private headers
5739 can not be included by other targets, even with a public dependency path.
5740 The "gn check" function performs this validation.
5741
5742 A friend declaration allows one or more targets to include private headers.
5743 This is useful for things like unit tests that are closely associated with a
5744 target and require internal knowledge without opening up all headers to be
5745 included by all dependents.
5746
5747 A friend target does not allow that target to include headers when no
5748 dependency exists. A public dependency path must still exist between two
5749 targets to include any headers from a destination target. The friend
5750 annotation merely allows the use of headers that would otherwise be
5751 prohibited because they are private.
5752
5753 The friend annotation is matched only against the target containing the file
5754 with the include directive. Friend annotations are not propagated across
5755 public or private dependencies. Friend annotations do not affect visibility.
5756```
5757
5758#### **Example**
5759
5760```
5761 static_library("lib") {
5762 # This target can include our private headers.
5763 friend = [ ":unit_tests" ]
5764
5765 public = [
5766 "public_api.h", # Normal public API for dependent targets.
5767 ]
5768
5769 # Private API and sources.
5770 sources = [
5771 "a_source_file.cc",
5772
5773 # Normal targets that depend on this one won't be able to include this
5774 # because this target defines a list of "public" headers. Without the
5775 # "public" list, all headers are implicitly public.
5776 "private_api.h",
5777 ]
5778 }
5779
5780 executable("unit_tests") {
5781 sources = [
5782 # This can include "private_api.h" from the :lib target because it
5783 # depends on that target and because of the friend annotation.
5784 "my_test.cc",
5785 ]
5786
5787 deps = [
5788 ":lib", # Required for the include to be allowed.
5789 ]
5790 }
5791```
Tyler Mandry4a648092022-02-15 19:47:09 +00005792### <a name="var_gen_deps"></a>**gen_deps**: Declares targets that should generate when this one does.
5793
5794```
5795 A list of target labels.
5796
5797 Not all GN targets that get evaluated are actually turned into ninja targets
5798 (see "gn help execution"). If this target is generated, then any targets in
5799 the "gen_deps" list will also be generated, regardless of the usual critera.
5800
5801 Since "gen_deps" are not build time dependencies, there can be cycles between
5802 "deps" and "gen_deps" or within "gen_deps" itself.
5803```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005804### <a name="var_include_dirs"></a>**include_dirs**: Additional include directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005805
5806```
5807 A list of source directories.
5808
5809 The directories in this list will be added to the include path for the files
5810 in the affected target.
5811```
5812
5813#### **Ordering of flags and values**
5814
5815```
5816 1. Those set on the current target (not in a config).
5817 2. Those set on the "configs" on the target in order that the
5818 configs appear in the list.
5819 3. Those set on the "all_dependent_configs" on the target in order
5820 that the configs appear in the list.
5821 4. Those set on the "public_configs" on the target in order that
5822 those configs appear in the list.
5823 5. all_dependent_configs pulled from dependencies, in the order of
5824 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005825 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005826 6. public_configs pulled from dependencies, in the order of the
5827 "deps" list. If a dependency is public, they will be applied
5828 recursively.
5829```
5830
5831#### **Example**
5832
5833```
5834 include_dirs = [ "src/include", "//third_party/foo" ]
5835```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005836### <a name="var_inputs"></a>**inputs**: Additional compile-time dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005837
5838```
5839 Inputs are compile-time dependencies of the current target. This means that
5840 all inputs must be available before compiling any of the sources or executing
5841 any actions.
5842
5843 Inputs are typically only used for action and action_foreach targets.
5844```
5845
5846#### **Inputs for actions**
5847
5848```
5849 For action and action_foreach targets, inputs should be the inputs to script
5850 that don't vary. These should be all .py files that the script uses via
5851 imports (the main script itself will be an implicit dependency of the action
5852 so need not be listed).
5853
5854 For action targets, inputs and sources are treated the same, but from a style
5855 perspective, it's recommended to follow the same rule as action_foreach and
5856 put helper files in the inputs, and the data used by the script (if any) in
5857 sources.
5858
5859 Note that another way to declare input dependencies from an action is to have
5860 the action write a depfile (see "gn help depfile"). This allows the script to
5861 dynamically write input dependencies, that might not be known until actually
5862 executing the script. This is more efficient than doing processing while
5863 running GN to determine the inputs, and is easier to keep in-sync than
5864 hardcoding the list.
5865```
5866
5867#### **Script input gotchas**
5868
5869```
5870 It may be tempting to write a script that enumerates all files in a directory
5871 as inputs. Don't do this! Even if you specify all the files in the inputs or
5872 sources in the GN target (or worse, enumerate the files in an exec_script
5873 call when running GN, which will be slow), the dependencies will be broken.
5874
5875 The problem happens if a file is ever removed because the inputs are not
5876 listed on the command line to the script. Because the script hasn't changed
5877 and all inputs are up to date, the script will not re-run and you will get a
5878 stale build. Instead, either list all inputs on the command line to the
5879 script, or if there are many, create a separate list file that the script
5880 reads. As long as this file is listed in the inputs, the build will detect
5881 when it has changed in any way and the action will re-run.
5882```
5883
5884#### **Inputs for binary targets**
5885
5886```
Julie Hockette2a29402018-07-31 10:11:42 -07005887 Any input dependencies will be resolved before compiling any sources or
5888 linking the target. Normally, all actions that a target depends on will be run
5889 before any files in a target are compiled. So if you depend on generated
5890 headers, you do not typically need to list them in the inputs section.
Brett Wilson796ed472018-07-16 15:11:09 -07005891
5892 Inputs for binary targets will be treated as implicit dependencies, meaning
5893 that changes in any of the inputs will force all sources in the target to be
5894 recompiled. If an input only applies to a subset of source files, you may
5895 want to split those into a separate target to avoid unnecessary recompiles.
5896```
5897
5898#### **Example**
5899
5900```
5901 action("myscript") {
5902 script = "domything.py"
5903 inputs = [ "input.data" ]
5904 }
5905```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005906### <a name="var_ldflags"></a>**ldflags**: Flags passed to the linker.
Brett Wilson796ed472018-07-16 15:11:09 -07005907
5908```
5909 A list of strings.
5910
5911 These flags are passed on the command-line to the linker and generally
5912 specify various linking options. Most targets will not need these and will
5913 use "libs" and "lib_dirs" instead.
5914
5915 ldflags are NOT pushed to dependents, so applying ldflags to source sets or
5916 static libraries will be a no-op. If you want to apply ldflags to dependent
5917 targets, put them in a config and set it in the all_dependent_configs or
5918 public_configs.
5919```
5920
5921#### **Ordering of flags and values**
5922
5923```
5924 1. Those set on the current target (not in a config).
5925 2. Those set on the "configs" on the target in order that the
5926 configs appear in the list.
5927 3. Those set on the "all_dependent_configs" on the target in order
5928 that the configs appear in the list.
5929 4. Those set on the "public_configs" on the target in order that
5930 those configs appear in the list.
5931 5. all_dependent_configs pulled from dependencies, in the order of
5932 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005933 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005934 6. public_configs pulled from dependencies, in the order of the
5935 "deps" list. If a dependency is public, they will be applied
5936 recursively.
5937```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005938### <a name="var_lib_dirs"></a>**lib_dirs**: Additional library directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005939
5940```
5941 A list of directories.
5942
5943 Specifies additional directories passed to the linker for searching for the
5944 required libraries. If an item is not an absolute path, it will be treated as
5945 being relative to the current build file.
5946
5947 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005948 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005949 shared library or executable target is reached. Second, they are
5950 uniquified so each one is only passed once (the first instance of it
5951 will be the one used).
5952```
5953
5954#### **Ordering of flags and values**
5955
5956```
5957 1. Those set on the current target (not in a config).
5958 2. Those set on the "configs" on the target in order that the
5959 configs appear in the list.
5960 3. Those set on the "all_dependent_configs" on the target in order
5961 that the configs appear in the list.
5962 4. Those set on the "public_configs" on the target in order that
5963 those configs appear in the list.
5964 5. all_dependent_configs pulled from dependencies, in the order of
5965 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005966 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005967 6. public_configs pulled from dependencies, in the order of the
5968 "deps" list. If a dependency is public, they will be applied
5969 recursively.
5970
5971 For "libs" and "lib_dirs" only, the values propagated from
5972 dependencies (as described above) are applied last assuming they
5973 are not already in the list.
5974```
5975
5976#### **Example**
5977
5978```
5979 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
5980```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005981### <a name="var_libs"></a>**libs**: Additional libraries to link.
Brett Wilson796ed472018-07-16 15:11:09 -07005982
5983```
5984 A list of library names or library paths.
5985
5986 These libraries will be linked into the final binary (executable or shared
5987 library) containing the current target.
5988
5989 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005990 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005991 shared library or executable target is reached. Second, they are
5992 uniquified so each one is only passed once (the first instance of it
5993 will be the one used).
5994```
5995
5996#### **Types of libs**
5997
5998```
5999 There are several different things that can be expressed in libs:
6000
6001 File paths
6002 Values containing '/' will be treated as references to files in the
6003 checkout. They will be rebased to be relative to the build directory and
6004 specified in the "libs" for linker tools. This facility should be used
6005 for libraries that are checked in to the version control. For libraries
6006 that are generated by the build, use normal GN deps to link them.
6007
6008 System libraries
6009 Values not containing '/' will be treated as system library names. These
6010 will be passed unmodified to the linker and prefixed with the
6011 "lib_switch" attribute of the linker tool. Generally you would set the
6012 "lib_dirs" so the given library is found. Your BUILD.gn file should not
6013 specify the switch (like "-l"): this will be encoded in the "lib_switch"
6014 of the tool.
Brett Wilson796ed472018-07-16 15:11:09 -07006015```
6016
6017#### **Ordering of flags and values**
6018
6019```
6020 1. Those set on the current target (not in a config).
6021 2. Those set on the "configs" on the target in order that the
6022 configs appear in the list.
6023 3. Those set on the "all_dependent_configs" on the target in order
6024 that the configs appear in the list.
6025 4. Those set on the "public_configs" on the target in order that
6026 those configs appear in the list.
6027 5. all_dependent_configs pulled from dependencies, in the order of
6028 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006029 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006030 6. public_configs pulled from dependencies, in the order of the
6031 "deps" list. If a dependency is public, they will be applied
6032 recursively.
6033
6034 For "libs" and "lib_dirs" only, the values propagated from
6035 dependencies (as described above) are applied last assuming they
6036 are not already in the list.
6037```
6038
6039#### **Examples**
6040
6041```
6042 On Windows:
6043 libs = [ "ctl3d.lib" ]
6044
6045 On Linux:
6046 libs = [ "ld" ]
6047```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006048### <a name="var_metadata"></a>**metadata**: Metadata of this target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006049
6050```
6051 Metadata is a collection of keys and values relating to a particular target.
Julie Hockettd69a9c32019-01-23 14:36:18 -08006052 Values must be lists, allowing for sane and predictable collection behavior.
6053 Generally, these keys will include three types of lists: lists of ordinary
6054 strings, lists of filenames intended to be rebased according to their
6055 particular source directory, and lists of target labels intended to be used
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006056 as barriers to the walk. Verification of these categories occurs at walk time,
Julie Hockettd69a9c32019-01-23 14:36:18 -08006057 not creation time (since it is not clear until the walk which values are
6058 intended for which purpose).
6059```
Nate Fischer8ed01d32019-01-08 17:32:01 -08006060
Julie Hockettd69a9c32019-01-23 14:36:18 -08006061#### **Example**
Nate Fischer8ed01d32019-01-08 17:32:01 -08006062
Julie Hockettd69a9c32019-01-23 14:36:18 -08006063```
Nate Fischer8ed01d32019-01-08 17:32:01 -08006064 group("doom_melon") {
6065 metadata = {
6066 # These keys are not built in to GN but are interpreted when consuming
6067 # metadata.
6068 my_barrier = []
6069 my_files = [ "a.txt", "b.txt" ]
6070 }
6071 }
6072```
Joseph Ryanad1e5ce2023-04-26 13:48:01 -07006073### <a name="var_mnemonic"></a>**mnemonic**: [string] Prefix displayed when ninja runs this action.
6074
6075```
6076 Tools in GN can set their ninja "description" which is displayed when
6077 building a target. These are commonly set with the format "CXX $output"
6078 or "LINK $label". By default, all GN actions will have the description
6079 "ACTION $label". Setting a mnemonic will override the "ACTION" prefix
6080 with another string, but the label will still be unconditionally displayed.
6081
6082 Whitespace is not allowed within a mnemonic.
6083```
Sylvain Defresne89e64252020-08-07 13:01:06 +02006084### <a name="var_module_name"></a>**module_name**: [string] The name for the compiled module.
6085
6086```
6087 Valid for binary targets that contain Swift sources.
6088
6089 If module_name is not set, then this rule will use the target name.
6090```
Wesley Moyd874bb92020-01-21 16:01:04 -08006091### <a name="var_output_conversion"></a>**output_conversion**: Data format for generated_file targets.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006092
6093```
6094 Controls how the "contents" of a generated_file target is formatted.
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02006095 See `gn help io_conversion`.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006096```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006097### <a name="var_output_dir"></a>**output_dir**: [directory] Directory to put output file in.
Brett Wilson796ed472018-07-16 15:11:09 -07006098
6099```
6100 For library and executable targets, overrides the directory for the final
6101 output. This must be in the root_build_dir or a child thereof.
6102
6103 This should generally be in the root_out_dir or a subdirectory thereof (the
6104 root_out_dir will be the same as the root_build_dir for the default
6105 toolchain, and will be a subdirectory for other toolchains). Not putting the
6106 output in a subdirectory of root_out_dir can result in collisions between
6107 different toolchains, so you will need to take steps to ensure that your
6108 target is only present in one toolchain.
6109
6110 Normally the toolchain specifies the output directory for libraries and
6111 executables (see "gn help tool"). You will have to consult that for the
6112 default location. The default location will be used if output_dir is
6113 undefined or empty.
6114```
6115
6116#### **Example**
6117
6118```
6119 shared_library("doom_melon") {
6120 output_dir = "$root_out_dir/plugin_libs"
6121 ...
6122 }
6123```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006124### <a name="var_output_extension"></a>**output_extension**: Value to use for the output's file extension.
Brett Wilson796ed472018-07-16 15:11:09 -07006125
6126```
6127 Normally the file extension for a target is based on the target type and the
6128 operating system, but in rare cases you will need to override the name (for
6129 example to use "libfreetype.so.6" instead of libfreetype.so on Linux).
6130
6131 This value should not include a leading dot. If undefined, the default
6132 specified on the tool will be used. If set to the empty string, no output
6133 extension will be used.
6134
6135 The output_extension will be used to set the "{{output_extension}}" expansion
6136 which the linker tool will generally use to specify the output file name. See
6137 "gn help tool".
6138```
6139
6140#### **Example**
6141
6142```
6143 shared_library("freetype") {
6144 if (is_linux) {
6145 # Call the output "libfreetype.so.6"
6146 output_extension = "so.6"
6147 }
6148 ...
6149 }
6150
6151 # On Windows, generate a "mysettings.cpl" control panel applet. Control panel
6152 # applets are actually special shared libraries.
6153 if (is_win) {
6154 shared_library("mysettings") {
6155 output_extension = "cpl"
6156 ...
6157 }
6158 }
6159```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006160### <a name="var_output_name"></a>**output_name**: Define a name for the output file other than the default.
Brett Wilson796ed472018-07-16 15:11:09 -07006161
6162```
6163 Normally the output name of a target will be based on the target name, so the
6164 target "//foo/bar:bar_unittests" will generate an output file such as
6165 "bar_unittests.exe" (using Windows as an example).
6166
6167 Sometimes you will want an alternate name to avoid collisions or if the
6168 internal name isn't appropriate for public distribution.
6169
6170 The output name should have no extension or prefixes, these will be added
6171 using the default system rules. For example, on Linux an output name of "foo"
6172 will produce a shared library "libfoo.so". There is no way to override the
6173 output prefix of a linker tool on a per- target basis. If you need more
6174 flexibility, create a copy target to produce the file you want.
6175
6176 This variable is valid for all binary output target types.
6177```
6178
6179#### **Example**
6180
6181```
6182 static_library("doom_melon") {
6183 output_name = "fluffy_bunny"
6184 }
6185```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006186### <a name="var_output_prefix_override"></a>**output_prefix_override**: Don't use prefix for output name.
Brett Wilson796ed472018-07-16 15:11:09 -07006187
6188```
6189 A boolean that overrides the output prefix for a target. Defaults to false.
6190
6191 Some systems use prefixes for the names of the final target output file. The
6192 normal example is "libfoo.so" on Linux for a target named "foo".
6193
6194 The output prefix for a given target type is specified on the linker tool
6195 (see "gn help tool"). Sometimes this prefix is undesired.
6196
6197 See also "gn help output_extension".
6198```
6199
6200#### **Example**
6201
6202```
6203 shared_library("doom_melon") {
6204 # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag
6205 # will produce "doom_melon.so".
6206 output_prefix_override = true
6207 ...
6208 }
6209```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006210### <a name="var_outputs"></a>**outputs**: Output files for actions and copy targets.
Brett Wilson796ed472018-07-16 15:11:09 -07006211
6212```
6213 Outputs is valid for "copy", "action", and "action_foreach" target types and
6214 indicates the resulting files. Outputs must always refer to files in the
6215 build directory.
6216
6217 copy
6218 Copy targets should have exactly one entry in the outputs list. If there is
6219 exactly one source, this can be a literal file name or a source expansion.
6220 If there is more than one source, this must contain a source expansion to
6221 map a single input name to a single output name. See "gn help copy".
6222
6223 action_foreach
6224 Action_foreach targets must always use source expansions to map input files
6225 to output files. There can be more than one output, which means that each
6226 invocation of the script will produce a set of files (presumably based on
6227 the name of the input file). See "gn help action_foreach".
6228
6229 action
6230 Action targets (excluding action_foreach) must list literal output file(s)
6231 with no source expansions. See "gn help action".
6232```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006233### <a name="var_partial_info_plist"></a>**partial_info_plist**: [filename] Path plist from asset catalog compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07006234
6235```
6236 Valid for create_bundle target, corresponds to the path for the partial
6237 Info.plist created by the asset catalog compiler that needs to be merged
6238 with the application Info.plist (usually done by the code signing script).
6239
6240 The file will be generated regardless of whether the asset compiler has
6241 been invoked or not. See "gn help create_bundle".
6242```
Petr Hosek7a6231e2022-10-22 23:14:18 +00006243### <a name="var_pool"></a>**pool**: Label of the pool used by binary targets actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006244
6245```
Petr Hosek7a6231e2022-10-22 23:14:18 +00006246 A fully-qualified label representing the pool that will be used for binary
6247 targets and actions. Pools are defined using the pool() {...} declaration.
Brett Wilson796ed472018-07-16 15:11:09 -07006248```
6249
6250#### **Example**
6251
6252```
Petr Hosek7a6231e2022-10-22 23:14:18 +00006253 executable("binary") {
6254 pool = "//build:custom_pool"
6255 ...
6256 }
6257
Brett Wilson796ed472018-07-16 15:11:09 -07006258 action("action") {
6259 pool = "//build:custom_pool"
6260 ...
6261 }
6262```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006263### <a name="var_precompiled_header"></a>**precompiled_header**: [string] Header file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07006264
6265```
6266 Precompiled headers will be used when a target specifies this value, or a
6267 config applying to this target specifies this value. In addition, the tool
6268 corresponding to the source files must also specify precompiled headers (see
6269 "gn help tool"). The tool will also specify what type of precompiled headers
6270 to use, by setting precompiled_header_type to either "gcc" or "msvc".
6271
6272 The precompiled header/source variables can be specified on a target or a
6273 config, but must be the same for all configs applying to a given target since
6274 a target can only have one precompiled header.
6275
6276 If you use both C and C++ sources, the precompiled header and source file
6277 will be compiled once per language. You will want to make sure to wrap C++
6278 includes in __cplusplus #ifdefs so the file will compile in C mode.
6279```
6280
6281#### **GCC precompiled headers**
6282
6283```
6284 When using GCC-style precompiled headers, "precompiled_source" contains the
6285 path of a .h file that is precompiled and then included by all source files
6286 in targets that set "precompiled_source".
6287
6288 The value of "precompiled_header" is not used with GCC-style precompiled
6289 headers.
6290```
6291
6292#### **MSVC precompiled headers**
6293
6294```
6295 When using MSVC-style precompiled headers, the "precompiled_header" value is
6296 a string corresponding to the header. This is NOT a path to a file that GN
6297 recognises, but rather the exact string that appears in quotes after
6298 an #include line in source code. The compiler will match this string against
6299 includes or forced includes (/FI).
6300
6301 MSVC also requires a source file to compile the header with. This must be
6302 specified by the "precompiled_source" value. In contrast to the header value,
6303 this IS a GN-style file name, and tells GN which source file to compile to
6304 make the .pch file used for subsequent compiles.
6305
6306 For example, if the toolchain specifies MSVC headers:
6307
6308 toolchain("vc_x64") {
6309 ...
6310 tool("cxx") {
6311 precompiled_header_type = "msvc"
6312 ...
6313
6314 You might make a config like this:
6315
6316 config("use_precompiled_headers") {
6317 precompiled_header = "build/precompile.h"
6318 precompiled_source = "//build/precompile.cc"
6319
6320 # Either your source files should #include "build/precompile.h"
6321 # first, or you can do this to force-include the header.
6322 cflags = [ "/FI$precompiled_header" ]
6323 }
6324
6325 And then define a target that uses the config:
6326
6327 executable("doom_melon") {
6328 configs += [ ":use_precompiled_headers" ]
6329 ...
6330```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006331### <a name="var_precompiled_header_type"></a>**precompiled_header_type**: [string] "gcc" or "msvc".
Brett Wilson796ed472018-07-16 15:11:09 -07006332
6333```
6334 See "gn help precompiled_header".
6335```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006336### <a name="var_precompiled_source"></a>**precompiled_source**: [file name] Source file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07006337
6338```
6339 The source file that goes along with the precompiled_header when using
6340 "msvc"-style precompiled headers. It will be implicitly added to the sources
6341 of the target. See "gn help precompiled_header".
6342```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006343### <a name="var_product_type"></a>**product_type**: Product type for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006344
6345```
6346 Correspond to the type of the product of a create_bundle target. Only
6347 meaningful to Xcode (used as part of the Xcode project generation).
6348
6349 When generating Xcode project files, only create_bundle target with a
6350 non-empty product_type will have a corresponding target in Xcode project.
6351```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006352### <a name="var_public"></a>**public**: Declare public header files for a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006353
6354```
6355 A list of files that other targets can include. These permissions are checked
6356 via the "check" command (see "gn help check").
6357
6358 If no public files are declared, other targets (assuming they have visibility
6359 to depend on this target) can include any file in the sources list. If this
6360 variable is defined on a target, dependent targets may only include files on
6361 this whitelist unless that target is marked as a friend (see "gn help
6362 friend").
6363
6364 Header file permissions are also subject to visibility. A target must be
6365 visible to another target to include any files from it at all and the public
6366 headers indicate which subset of those files are permitted. See "gn help
6367 visibility" for more.
6368
6369 Public files are inherited through the dependency tree. So if there is a
6370 dependency A -> B -> C, then A can include C's public headers. However, the
6371 same is NOT true of visibility, so unless A is in C's visibility list, the
6372 include will be rejected.
6373
6374 GN only knows about files declared in the "sources" and "public" sections of
6375 targets. If a file is included that is not known to the build, it will be
6376 allowed.
6377
6378 It is common for test targets to need to include private headers for their
6379 associated code. In this case, list the test target in the "friend" list of
6380 the target that owns the private header to allow the inclusion. See
6381 "gn help friend" for more.
Julie Hockette2a29402018-07-31 10:11:42 -07006382
6383 When a binary target has no explicit or implicit public headers (a "public"
6384 list is defined but is empty), GN assumes that the target can not propagate
6385 any compile-time dependencies up the dependency tree. In this case, the build
6386 can be parallelized more efficiently.
6387 Say there are dependencies:
6388 A (shared library) -> B (shared library) -> C (action).
6389 Normally C must complete before any source files in A can compile (because
6390 there might be generated includes). But when B explicitly declares no public
6391 headers, C can execute in parallel with A's compile steps. C must still be
6392 complete before any dependents link.
Brett Wilson796ed472018-07-16 15:11:09 -07006393```
6394
6395#### **Examples**
6396
6397```
6398 These exact files are public:
6399 public = [ "foo.h", "bar.h" ]
6400
6401 No files are public (no targets may include headers from this one):
Julie Hockette2a29402018-07-31 10:11:42 -07006402 # This allows starting compilation in dependent targets earlier.
Brett Wilson796ed472018-07-16 15:11:09 -07006403 public = []
6404```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006405### <a name="var_public_configs"></a>**public_configs**: Configs to be applied on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07006406
6407```
6408 A list of config labels.
6409
6410 Targets directly depending on this one will have the configs listed in this
6411 variable added to them. These configs will also apply to the current target.
Julie Hockette2a29402018-07-31 10:11:42 -07006412 Generally, public configs are used to apply defines and include directories
6413 necessary to compile this target's header files.
Brett Wilson796ed472018-07-16 15:11:09 -07006414
Julie Hockette2a29402018-07-31 10:11:42 -07006415 See also "gn help all_dependent_configs".
6416```
6417
6418#### **Propagation of public configs**
6419
6420```
6421 Public configs are applied to all targets that depend directly on this one.
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006422 These dependent targets can further push this target's public configs
Julie Hockette2a29402018-07-31 10:11:42 -07006423 higher in the dependency tree by depending on it via public_deps (see "gn
6424 help public_deps").
6425
6426 static_library("toplevel") {
6427 # This target will get "my_config" applied to it. However, since this
6428 # target uses "deps" and not "public_deps", targets that depend on this
6429 # one won't get it.
6430 deps = [ ":intermediate" ]
6431 }
6432
6433 static_library("intermediate") {
6434 # Depending on "lower" in any way will apply "my_config" to this target.
6435 # Additionall, since this target depends on "lower" via public_deps,
6436 # targets that depend on this one will also get "my_config".
6437 public_deps = [ ":lower" ]
6438 }
6439
6440 static_library("lower") {
6441 # This will get applied to all targets that depend on this one.
6442 public_configs = [ ":my_config" ]
6443 }
6444
6445 Public config propagation happens in a second phase once a target and all of
6446 its dependencies have been resolved. Therefore, a target will not see these
Brett Wilson796ed472018-07-16 15:11:09 -07006447 force-added configs in their "configs" variable while the script is running,
6448 and they can not be removed. As a result, this capability should generally
Julie Hockette2a29402018-07-31 10:11:42 -07006449 only be used to add defines and include directories rather than setting
6450 complicated flags that some targets may not want.
Brett Wilson796ed472018-07-16 15:11:09 -07006451
Julie Hockette2a29402018-07-31 10:11:42 -07006452 Public configs may or may not be propagated across toolchain boundaries
6453 depending on the value of the propagates_configs flag (see "gn help
6454 toolchain") on the toolchain of the target declaring the public_config.
6455```
6456
6457#### **Avoiding applying public configs to this target**
6458
6459```
6460 If you want the config to apply to targets that depend on this one, but NOT
6461 this one, define an extra layer of indirection using a group:
6462
6463 # External targets depend on this group.
6464 group("my_target") {
6465 # Config to apply to all targets that depend on this one.
6466 public_configs = [ ":external_settings" ]
6467 deps = [ ":internal_target" ]
6468 }
6469
6470 # Internal target to actually compile the sources.
6471 static_library("internal_target") {
6472 # Force all external targets to depend on the group instead of directly
6473 # on this so the "external_settings" config will get applied.
6474 visibility = [ ":my_target" ]
6475 ...
6476 }
Brett Wilson796ed472018-07-16 15:11:09 -07006477```
6478
6479#### **Ordering of flags and values**
6480
6481```
6482 1. Those set on the current target (not in a config).
6483 2. Those set on the "configs" on the target in order that the
6484 configs appear in the list.
6485 3. Those set on the "all_dependent_configs" on the target in order
6486 that the configs appear in the list.
6487 4. Those set on the "public_configs" on the target in order that
6488 those configs appear in the list.
6489 5. all_dependent_configs pulled from dependencies, in the order of
6490 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006491 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006492 6. public_configs pulled from dependencies, in the order of the
6493 "deps" list. If a dependency is public, they will be applied
6494 recursively.
6495```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006496### <a name="var_public_deps"></a>**public_deps**: Declare public dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07006497
6498```
6499 Public dependencies are like private dependencies (see "gn help deps") but
6500 additionally express that the current target exposes the listed deps as part
6501 of its public API.
6502
6503 This has several ramifications:
6504
6505 - public_configs that are part of the dependency are forwarded to direct
6506 dependents.
6507
6508 - Public headers in the dependency are usable by dependents (includes do
6509 not require a direct dependency or visibility).
6510
6511 - If the current target is a shared library, other shared libraries that it
6512 publicly depends on (directly or indirectly) are propagated up the
6513 dependency tree to dependents for linking.
Julie Hockette2a29402018-07-31 10:11:42 -07006514
6515 See also "gn help public_configs".
Brett Wilson796ed472018-07-16 15:11:09 -07006516```
6517
6518#### **Discussion**
6519
6520```
6521 Say you have three targets: A -> B -> C. C's visibility may allow B to depend
6522 on it but not A. Normally, this would prevent A from including any headers
6523 from C, and C's public_configs would apply only to B.
6524
6525 If B lists C in its public_deps instead of regular deps, A will now inherit
6526 C's public_configs and the ability to include C's public headers.
6527
6528 Generally if you are writing a target B and you include C's headers as part
6529 of B's public headers, or targets depending on B should consider B and C to
6530 be part of a unit, you should use public_deps instead of deps.
6531```
6532
6533#### **Example**
6534
6535```
6536 # This target can include files from "c" but not from
6537 # "super_secret_implementation_details".
6538 executable("a") {
6539 deps = [ ":b" ]
6540 }
6541
6542 shared_library("b") {
6543 deps = [ ":super_secret_implementation_details" ]
6544 public_deps = [ ":c" ]
6545 }
6546```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006547### <a name="var_rebase"></a>**rebase**: Rebase collected metadata as files.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006548
6549```
6550 A boolean that triggers a rebase of collected metadata strings based on their
6551 declared file. Defaults to false.
6552
6553 Metadata generally declares files as strings relative to the local build file.
6554 However, this data is often used in other contexts, and so setting this flag
6555 will force the metadata collection to be rebased according to the local build
6556 file's location and thus allow the filename to be used anywhere.
6557
6558 Setting this flag will raise an error if any target's specified metadata is
6559 not a string value.
6560
6561 See also "gn help generated_file".
6562```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006563### <a name="var_response_file_contents"></a>**response_file_contents**: Contents of a response file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006564
6565```
6566 Sometimes the arguments passed to a script can be too long for the system's
6567 command-line capabilities. This is especially the case on Windows where the
6568 maximum command-line length is less than 8K. A response file allows you to
6569 pass an unlimited amount of data to a script in a temporary file for an
6570 action or action_foreach target.
6571
6572 If the response_file_contents variable is defined and non-empty, the list
6573 will be treated as script args (including possibly substitution patterns)
6574 that will be written to a temporary file at build time. The name of the
6575 temporary file will be substituted for "{{response_file_name}}" in the script
6576 args.
6577
6578 The response file contents will always be quoted and escaped according to
6579 Unix shell rules. To parse the response file, the Python script should use
6580 "shlex.split(file_contents)".
6581```
6582
6583#### **Example**
6584
6585```
6586 action("process_lots_of_files") {
6587 script = "process.py",
6588 inputs = [ ... huge list of files ... ]
6589
6590 # Write all the inputs to a response file for the script. Also,
6591 # make the paths relative to the script working directory.
6592 response_file_contents = rebase_path(inputs, root_build_dir)
6593
6594 # The script expects the name of the response file in --file-list.
6595 args = [
6596 "--enable-foo",
6597 "--file-list={{response_file_name}}",
6598 ]
6599 }
6600```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006601### <a name="var_script"></a>**script**: Script file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006602
6603```
6604 An absolute or buildfile-relative file name of a Python script to run for a
6605 action and action_foreach targets (see "gn help action" and "gn help
6606 action_foreach").
6607```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006608### <a name="var_sources"></a>**sources**: Source files for a target
Brett Wilson796ed472018-07-16 15:11:09 -07006609
6610```
6611 A list of files. Non-absolute paths will be resolved relative to the current
6612 build file.
6613```
6614
6615#### **Sources for binary targets**
6616
6617```
6618 For binary targets (source sets, executables, and libraries), the known file
6619 types will be compiled with the associated tools. Unknown file types and
6620 headers will be skipped. However, you should still list all C/C+ header files
6621 so GN knows about the existence of those files for the purposes of include
6622 checking.
6623
6624 As a special case, a file ending in ".def" will be treated as a Windows
6625 module definition file. It will be appended to the link line with a
6626 preceding "/DEF:" string. There must be at most one .def file in a target
6627 and they do not cross dependency boundaries (so specifying a .def file in a
6628 static library or source set will have no effect on the executable or shared
6629 library they're linked into).
Julie Hockettce1fa072019-05-07 17:44:37 -07006630
6631 For Rust targets that do not specify a crate_root, then the crate_root will
6632 look for a lib.rs file (or main.rs for executable) or a single file in
6633 sources, if sources contains only one file.
Brett Wilson796ed472018-07-16 15:11:09 -07006634```
6635
6636#### **Sources for non-binary targets**
6637
6638```
6639 action_foreach
6640 The sources are the set of files that the script will be executed over. The
6641 script will run once per file.
6642
6643 action
6644 The sources will be treated the same as inputs. See "gn help inputs" for
6645 more information and usage advice.
6646
6647 copy
6648 The source are the source files to copy.
6649```
Sylvain Defresne89e64252020-08-07 13:01:06 +02006650### <a name="var_swiftflags"></a>**swiftflags**: Flags passed to the swift compiler.
6651
6652```
6653 A list of strings.
6654
6655 "swiftflags" are passed to any invocation of a tool that takes an .swift
6656 file as input.
6657```
6658
6659#### **Ordering of flags and values**
6660
6661```
6662 1. Those set on the current target (not in a config).
6663 2. Those set on the "configs" on the target in order that the
6664 configs appear in the list.
6665 3. Those set on the "all_dependent_configs" on the target in order
6666 that the configs appear in the list.
6667 4. Those set on the "public_configs" on the target in order that
6668 those configs appear in the list.
6669 5. all_dependent_configs pulled from dependencies, in the order of
6670 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006671 more than once, only the first occurrence will be used.
Sylvain Defresne89e64252020-08-07 13:01:06 +02006672 6. public_configs pulled from dependencies, in the order of the
6673 "deps" list. If a dependency is public, they will be applied
6674 recursively.
6675```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006676### <a name="var_testonly"></a>**testonly**: Declares a target must only be used for testing.
Brett Wilson796ed472018-07-16 15:11:09 -07006677
6678```
6679 Boolean. Defaults to false.
6680
6681 When a target is marked "testonly = true", it must only be depended on by
6682 other test-only targets. Otherwise, GN will issue an error that the
6683 depenedency is not allowed.
6684
6685 This feature is intended to prevent accidentally shipping test code in a
6686 final product.
6687```
6688
6689#### **Example**
6690
6691```
6692 source_set("test_support") {
6693 testonly = true
6694 ...
6695 }
6696```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006697### <a name="var_visibility"></a>**visibility**: A list of labels that can depend on a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006698
6699```
6700 A list of labels and label patterns that define which targets can depend on
6701 the current one. These permissions are checked via the "check" command (see
6702 "gn help check").
6703
6704 If visibility is not defined, it defaults to public ("*").
6705
6706 If visibility is defined, only the targets with labels that match it can
6707 depend on the current target. The empty list means no targets can depend on
6708 the current target.
6709
6710 Tip: Often you will want the same visibility for all targets in a BUILD file.
6711 In this case you can just put the definition at the top, outside of any
6712 target, and the targets will inherit that scope and see the definition.
6713```
6714
6715#### **Patterns**
6716
6717```
6718 See "gn help label_pattern" for more details on what types of patterns are
6719 supported. If a toolchain is specified, only targets in that toolchain will
6720 be matched. If a toolchain is not specified on a pattern, targets in all
6721 toolchains will be matched.
6722```
6723
6724#### **Examples**
6725
6726```
6727 Only targets in the current buildfile ("private"):
6728 visibility = [ ":*" ]
6729
6730 No targets (used for targets that should be leaf nodes):
6731 visibility = []
6732
6733 Any target ("public", the default):
6734 visibility = [ "*" ]
6735
6736 All targets in the current directory and any subdirectory:
6737 visibility = [ "./*" ]
6738
6739 Any target in "//bar/BUILD.gn":
6740 visibility = [ "//bar:*" ]
6741
6742 Any target in "//bar/" or any subdirectory thereof:
6743 visibility = [ "//bar/*" ]
6744
6745 Just these specific targets:
6746 visibility = [ ":mything", "//foo:something_else" ]
6747
6748 Any target in the current directory and any subdirectory thereof, plus
Petr Hosek7a6231e2022-10-22 23:14:18 +00006749 any targets in "//bar/" and any subdirectory thereof.
Brett Wilson796ed472018-07-16 15:11:09 -07006750 visibility = [ "./*", "//bar/*" ]
6751```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006752### <a name="var_walk_keys"></a>**walk_keys**: Key(s) for managing the metadata collection walk.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006753
6754```
Julie Hockett152c5142019-07-12 09:53:43 -06006755 Defaults to [""].
Nate Fischer8ed01d32019-01-08 17:32:01 -08006756
6757 These keys are used to control the next step in a collection walk, acting as
6758 barriers. If a specified key is defined in a target's metadata, the walk will
6759 use the targets listed in that value to determine which targets are walked.
6760
Julie Hockett152c5142019-07-12 09:53:43 -06006761 If no walk_keys are specified for a generated_file target (i.e. "[""]"), the
Nate Fischer8ed01d32019-01-08 17:32:01 -08006762 walk will touch all deps and data_deps of the specified target recursively.
6763
6764 See "gn help generated_file".
6765```
Robert Sesekd0a6f072020-05-15 11:21:22 -04006766### <a name="var_weak_frameworks"></a>**weak_frameworks**: [name list] Name of frameworks that must be weak linked.
6767
6768```
6769 A list of framework names.
6770
6771 The frameworks named in that list will be weak linked with any dynamic link
6772 type target. Weak linking instructs the dynamic loader to attempt to load
6773 the framework, but if it is not able to do so, it leaves any imported symbols
6774 unresolved. This is typically used when a framework is present in a new
6775 version of an SDK but not on older versions of the OS that the software runs
6776 on.
6777```
6778
6779#### **Ordering of flags and values**
6780
6781```
6782 1. Those set on the current target (not in a config).
6783 2. Those set on the "configs" on the target in order that the
6784 configs appear in the list.
6785 3. Those set on the "all_dependent_configs" on the target in order
6786 that the configs appear in the list.
6787 4. Those set on the "public_configs" on the target in order that
6788 those configs appear in the list.
6789 5. all_dependent_configs pulled from dependencies, in the order of
6790 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006791 more than once, only the first occurrence will be used.
Robert Sesekd0a6f072020-05-15 11:21:22 -04006792 6. public_configs pulled from dependencies, in the order of the
6793 "deps" list. If a dependency is public, they will be applied
6794 recursively.
6795```
6796
6797#### **Example**
6798
6799```
6800 weak_frameworks = [ "OnlyOnNewerOSes.framework" ]
6801```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006802### <a name="var_write_runtime_deps"></a>**write_runtime_deps**: Writes the target's runtime_deps to the given path.
Brett Wilson796ed472018-07-16 15:11:09 -07006803
6804```
6805 Does not synchronously write the file, but rather schedules it to be written
6806 at the end of generation.
6807
6808 If the file exists and the contents are identical to that being written, the
6809 file will not be updated. This will prevent unnecessary rebuilds of targets
6810 that depend on this file.
6811
6812 Path must be within the output directory.
6813
6814 See "gn help runtime_deps" for how the runtime dependencies are computed.
6815
6816 The format of this file will list one file per line with no escaping. The
6817 files will be relative to the root_build_dir. The first line of the file will
6818 be the main output file of the target itself. The file contents will be the
6819 same as requesting the runtime deps be written on the command line (see "gn
6820 help --runtime-deps-list-file").
6821```
Harley Li0a9affb2020-06-03 10:38:42 -04006822### <a name="var_xcasset_compiler_flags"></a>**xcasset_compiler_flags**: Flags passed to xcassets compiler.
6823
6824```
6825 A list of strings.
6826
6827 Valid for create_bundle target. Those flags are directly passed to
6828 xcassets compiler, corresponding to {{xcasset_compiler_flags}} substitution
6829 in compile_xcassets tool.
6830```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006831### <a name="var_xcode_extra_attributes"></a>**xcode_extra_attributes**: [scope] Extra attributes for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006832
6833```
6834 The value defined in this scope will be copied to the EXTRA_ATTRIBUTES
6835 property of the generated Xcode project. They are only meaningful when
6836 generating with --ide=xcode.
6837
6838 See "gn help create_bundle" for more information.
6839```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006840### <a name="var_xcode_test_application_name"></a>**xcode_test_application_name**: Name for Xcode test target.
Brett Wilson796ed472018-07-16 15:11:09 -07006841
6842```
6843 Each unit and ui test target must have a test application target, and this
6844 value is used to specify the relationship. Only meaningful to Xcode (used as
6845 part of the Xcode project generation).
6846
6847 See "gn help create_bundle" for more information.
6848```
6849
Julie Hockette2a29402018-07-31 10:11:42 -07006850#### **Example**
Brett Wilson796ed472018-07-16 15:11:09 -07006851
6852```
6853 create_bundle("chrome_xctest") {
6854 test_application_name = "chrome"
6855 ...
6856 }
6857```
6858## <a name="other"></a>Other help topics
6859
6860### <a name="buildargs"></a>**Build Arguments Overview**
6861
6862```
6863 Build arguments are variables passed in from outside of the build that build
6864 files can query to determine how the build works.
6865```
6866
6867#### **How build arguments are set**
6868
6869```
6870 First, system default arguments are set based on the current system. The
6871 built-in arguments are:
6872 - host_cpu
6873 - host_os
6874 - current_cpu
6875 - current_os
6876 - target_cpu
6877 - target_os
6878
6879 Next, project-specific overrides are applied. These are specified inside
6880 the default_args variable of //.gn. See "gn help dotfile" for more.
6881
6882 If specified, arguments from the --args command line flag are used. If that
6883 flag is not specified, args from previous builds in the build directory will
6884 be used (this is in the file args.gn in the build directory).
6885
6886 Last, for targets being compiled with a non-default toolchain, the toolchain
6887 overrides are applied. These are specified in the toolchain_args section of a
6888 toolchain definition. The use-case for this is that a toolchain may be
6889 building code for a different platform, and that it may want to always
6890 specify Posix, for example. See "gn help toolchain" for more.
6891
6892 If you specify an override for a build argument that never appears in a
6893 "declare_args" call, a nonfatal error will be displayed.
6894```
6895
6896#### **Examples**
6897
6898```
6899 gn args out/FooBar
6900 Create the directory out/FooBar and open an editor. You would type
6901 something like this into that file:
6902 enable_doom_melon=false
6903 os="android"
6904
6905 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
6906 This will overwrite the build directory with the given arguments. (Note
6907 that the quotes inside the args command will usually need to be escaped
6908 for your shell to pass through strings values.)
6909```
6910
6911#### **How build arguments are used**
6912
6913```
6914 If you want to use an argument, you use declare_args() and specify default
6915 values. These default values will apply if none of the steps listed in the
6916 "How build arguments are set" section above apply to the given argument, but
6917 the defaults will not override any of these.
6918
6919 Often, the root build config file will declare global arguments that will be
6920 passed to all buildfiles. Individual build files can also specify arguments
6921 that apply only to those files. It is also useful to specify build args in an
6922 "import"-ed file if you want such arguments to apply to multiple buildfiles.
6923```
6924### <a name="dotfile"></a>**.gn file**
6925
6926```
6927 When gn starts, it will search the current directory and parent directories
6928 for a file called ".gn". This indicates the source root. You can override
6929 this detection by using the --root command-line argument
6930
6931 The .gn file in the source root will be executed. The syntax is the same as a
6932 buildfile, but with very limited build setup-specific meaning.
6933
6934 If you specify --root, by default GN will look for the file .gn in that
6935 directory. If you want to specify a different file, you can additionally pass
6936 --dotfile:
6937
6938 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
6939```
6940
6941#### **Variables**
6942
6943```
6944 arg_file_template [optional]
6945 Path to a file containing the text that should be used as the default
6946 args.gn content when you run `gn args`.
6947
6948 buildconfig [required]
6949 Path to the build config file. This file will be used to set up the
6950 build file execution environment for each toolchain.
6951
6952 check_targets [optional]
6953 A list of labels and label patterns that should be checked when running
Erik Staab5d109d72020-06-29 22:53:23 -07006954 "gn check" or "gn gen --check". If neither check_targets or
6955 no_check_targets (see below) is specified, all targets will be checked.
6956 It is an error to specify both check_targets and no_check_targets. If it
6957 is the empty list, no targets will be checked. To bypass this list,
6958 request an explicit check of targets, like "//*".
6959
6960 The format of this list is identical to that of "visibility" so see "gn
6961 help visibility" for examples.
6962
6963 no_check_targets [optional]
6964 A list of labels and label patterns that should *not* be checked when
6965 running "gn check" or "gn gen --check". All other targets will be checked.
6966 If neither check_targets (see above) or no_check_targets is specified, all
6967 targets will be checked. It is an error to specify both check_targets and
6968 no_check_targets.
Brett Wilson796ed472018-07-16 15:11:09 -07006969
6970 The format of this list is identical to that of "visibility" so see "gn
6971 help visibility" for examples.
6972
James Robinson49f59032020-01-08 14:05:51 -08006973 check_system_includes [optional]
6974 Boolean to control whether system style includes are checked by default
6975 when running "gn check" or "gn gen --check". System style includes are
6976 includes that use angle brackets <> instead of double quotes "". If this
6977 setting is omitted or set to false, these includes will be ignored by
6978 default. They can be checked explicitly by running
6979 "gn check --check-system" or "gn gen --check=system"
6980
Brett Wilson796ed472018-07-16 15:11:09 -07006981 exec_script_whitelist [optional]
6982 A list of .gn/.gni files (not labels) that have permission to call the
6983 exec_script function. If this list is defined, calls to exec_script will
6984 be checked against this list and GN will fail if the current file isn't
6985 in the list.
6986
6987 This is to allow the use of exec_script to be restricted since is easy to
6988 use inappropriately. Wildcards are not supported. Files in the
6989 secondary_source tree (if defined) should be referenced by ignoring the
6990 secondary tree and naming them as if they are in the main tree.
6991
6992 If unspecified, the ability to call exec_script is unrestricted.
6993
6994 Example:
6995 exec_script_whitelist = [
6996 "//base/BUILD.gn",
6997 "//build/my_config.gni",
6998 ]
6999
Brett Wilson1da84bb2022-09-14 15:35:29 -07007000 export_compile_commands [optional]
7001 A list of label patterns for which to generate a Clang compilation
7002 database (see "gn help label_pattern" for the string format).
7003
7004 When specified, GN will generate a compile_commands.json file in the root
7005 of the build directory containing information on how to compile each
7006 source file reachable from any label matching any pattern in the list.
7007 This is used for Clang-based tooling and some editor integration. See
7008 https://clang.llvm.org/docs/JSONCompilationDatabase.html
7009
7010 The switch --add-export-compile-commands to "gn gen" (see "gn help gen")
7011 appends to this value which provides a per-user way to customize it.
7012
7013 The deprecated switch --export-compile-commands to "gn gen" (see "gn help
7014 gen") adds to the export target list using a different format.
7015
7016 Example:
7017 export_compile_commands = [
7018 "//base/*",
7019 "//tools:doom_melon",
7020 ]
7021
Brett Wilson796ed472018-07-16 15:11:09 -07007022 root [optional]
7023 Label of the root build target. The GN build will start by loading the
7024 build file containing this target name. This defaults to "//:" which will
Harley Li0a9affb2020-06-03 10:38:42 -04007025 cause the file //BUILD.gn to be loaded. Note that build_file_extension
7026 applies to the default case as well.
Brett Wilson796ed472018-07-16 15:11:09 -07007027
Nico Weberad9eab22020-11-15 22:20:28 -05007028 The command-line switch --root-target will override this value (see "gn
7029 help --root-target").
7030
Brett Wilson796ed472018-07-16 15:11:09 -07007031 script_executable [optional]
Brett Wilsond0f8bc92022-04-22 14:01:40 -07007032 By default, GN runs the scripts used in action targets and exec_script
7033 calls using the Python interpreter found in PATH. This value specifies the
7034 Python executable or other interpreter to use instead.
James Robinson61377e32020-02-13 15:20:07 -08007035
Brett Wilsond0f8bc92022-04-22 14:01:40 -07007036 If set to the empty string, the scripts will be executed directly.
7037
7038 The command-line switch --script-executable will override this value (see
7039 "gn help --script-executable")
Brett Wilson796ed472018-07-16 15:11:09 -07007040
7041 secondary_source [optional]
7042 Label of an alternate directory tree to find input files. When searching
7043 for a BUILD.gn file (or the build config file discussed above), the file
7044 will first be looked for in the source root. If it's not found, the
7045 secondary source root will be checked (which would contain a parallel
7046 directory hierarchy).
7047
7048 This behavior is intended to be used when BUILD.gn files can't be checked
7049 in to certain source directories for whatever reason.
7050
7051 The secondary source root must be inside the main source tree.
7052
7053 default_args [optional]
7054 Scope containing the default overrides for declared arguments. These
7055 overrides take precedence over the default values specified in the
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01007056 declare_args() block, but can be overridden using --args or the
Brett Wilson796ed472018-07-16 15:11:09 -07007057 args.gn file.
7058
7059 This is intended to be used when subprojects declare arguments with
7060 default values that need to be changed for whatever reason.
Harley Li0a9affb2020-06-03 10:38:42 -04007061
7062 build_file_extension [optional]
7063 If set to a non-empty string, this is added to the name of all build files
7064 to load.
7065 GN will look for build files named "BUILD.$build_file_extension.gn".
7066 This is intended to be used during migrations or other situations where
7067 there are two independent GN builds in the same directories.
Petr Hosek317fdc12020-05-28 10:23:42 -07007068
7069 ninja_required_version [optional]
7070 When set specifies the minimum required version of Ninja. The default
7071 required version is 1.7.2. Specifying a higher version might enable the
7072 use of some of newer features that can make the build more efficient.
Brett Wilson796ed472018-07-16 15:11:09 -07007073```
7074
7075#### **Example .gn file contents**
7076
7077```
7078 buildconfig = "//build/config/BUILDCONFIG.gn"
7079
7080 check_targets = [
7081 "//doom_melon/*", # Check everything in this subtree.
7082 "//tools:mind_controlling_ant", # Check this specific target.
7083 ]
7084
7085 root = "//:root"
7086
7087 secondary_source = "//build/config/temporary_buildfiles/"
7088
7089 default_args = {
7090 # Default to release builds for this project.
7091 is_debug = false
7092 is_component_build = false
7093 }
7094```
7095### <a name="execution"></a>**Build graph and execution overview**
7096
7097#### **Overall build flow**
7098
7099```
7100 1. Look for ".gn" file (see "gn help dotfile") in the current directory and
7101 walk up the directory tree until one is found. Set this directory to be
7102 the "source root" and interpret this file to find the name of the build
7103 config file.
7104
7105 2. Execute the build config file identified by .gn to set up the global
7106 variables and default toolchain name. Any arguments, variables, defaults,
7107 etc. set up in this file will be visible to all files in the build.
7108
7109 3. Load the //BUILD.gn (in the source root directory).
7110
7111 4. Recursively evaluate rules and load BUILD.gn in other directories as
7112 necessary to resolve dependencies. If a BUILD file isn't found in the
7113 specified location, GN will look in the corresponding location inside
7114 the secondary_source defined in the dotfile (see "gn help dotfile").
7115
7116 5. When a target's dependencies are resolved, write out the `.ninja`
7117 file to disk.
7118
7119 6. When all targets are resolved, write out the root build.ninja file.
Harley Li0a9affb2020-06-03 10:38:42 -04007120
7121 Note that the BUILD.gn file name may be modulated by .gn arguments such as
7122 build_file_extension.
Brett Wilson796ed472018-07-16 15:11:09 -07007123```
7124
7125#### **Executing target definitions and templates**
7126
7127```
7128 Build files are loaded in parallel. This means it is impossible to
7129 interrogate a target from GN code for any information not derivable from its
7130 label (see "gn help label"). The exception is the get_target_outputs()
7131 function which requires the target being interrogated to have been defined
7132 previously in the same file.
7133
7134 Targets are declared by their type and given a name:
7135
7136 static_library("my_static_library") {
7137 ... target parameter definitions ...
7138 }
7139
7140 There is also a generic "target" function for programmatically defined types
7141 (see "gn help target"). You can define new types using templates (see "gn
7142 help template"). A template defines some custom code that expands to one or
7143 more other targets.
7144
7145 Before executing the code inside the target's { }, the target defaults are
7146 applied (see "gn help set_defaults"). It will inject implicit variable
7147 definitions that can be overridden by the target code as necessary. Typically
7148 this mechanism is used to inject a default set of configs that define the
7149 global compiler and linker flags.
7150```
7151
7152#### **Which targets are built**
7153
7154```
7155 All targets encountered in the default toolchain (see "gn help toolchain")
7156 will have build rules generated for them, even if no other targets reference
7157 them. Their dependencies must resolve and they will be added to the implicit
7158 "all" rule (see "gn help ninja_rules").
7159
7160 Targets in non-default toolchains will only be generated when they are
7161 required (directly or transitively) to build a target in the default
7162 toolchain.
7163
Tyler Mandry4a648092022-02-15 19:47:09 +00007164 Some targets might be associated but without a formal build dependency (for
7165 example, related tools or optional variants). A target that is marked as
7166 "generated" can propagate its generated state to an associated target using
7167 "gen_deps". This will make the referenced dependency have Ninja rules
7168 generated in the same cases the source target has but without a build-time
7169 dependency and even in non-default toolchains.
7170
Brett Wilson796ed472018-07-16 15:11:09 -07007171 See also "gn help ninja_rules".
7172```
7173
7174#### **Dependencies**
7175
7176```
7177 The only difference between "public_deps" and "deps" except for pushing
7178 configs around the build tree and allowing includes for the purposes of "gn
7179 check".
7180
7181 A target's "data_deps" are guaranteed to be built whenever the target is
7182 built, but the ordering is not defined. The meaning of this is dependencies
7183 required at runtime. Currently data deps will be complete before the target
7184 is linked, but this is not semantically guaranteed and this is undesirable
7185 from a build performance perspective. Since we hope to change this in the
7186 future, do not rely on this behavior.
7187```
7188### <a name="grammar"></a>**Language and grammar for GN build files**
7189
7190#### **Tokens**
7191
7192```
7193 GN build files are read as sequences of tokens. While splitting the file
7194 into tokens, the next token is the longest sequence of characters that form a
7195 valid token.
7196```
7197
7198#### **White space and comments**
7199
7200```
7201 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
7202 carriage returns (U+000D), and newlines (U+000A).
7203
7204 Comments start at the character "#" and stop at the next newline.
7205
7206 White space and comments are ignored except that they may separate tokens
7207 that would otherwise combine into a single token.
7208```
7209
7210#### **Identifiers**
7211
7212```
7213 Identifiers name variables and functions.
7214
7215 identifier = letter { letter | digit } .
7216 letter = "A" ... "Z" | "a" ... "z" | "_" .
7217 digit = "0" ... "9" .
7218```
7219
7220#### **Keywords**
7221
7222```
7223 The following keywords are reserved and may not be used as identifiers:
7224
7225 else false if true
7226```
7227
7228#### **Integer literals**
7229
7230```
7231 An integer literal represents a decimal integer value.
7232
7233 integer = [ "-" ] digit { digit } .
7234
7235 Leading zeros and negative zero are disallowed.
7236```
7237
7238#### **String literals**
7239
7240```
7241 A string literal represents a string value consisting of the quoted
7242 characters with possible escape sequences and variable expansions.
7243
7244 string = `"` { char | escape | expansion } `"` .
7245 escape = `\` ( "$" | `"` | char ) .
Nico Weberad9eab22020-11-15 22:20:28 -05007246 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" .
Brett Wilson796ed472018-07-16 15:11:09 -07007247 Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f]
7248 expansion = "$" ( identifier | BracketExpansion | Hex ) .
Nico Weberad9eab22020-11-15 22:20:28 -05007249 char = /* any character except "$", `"`, or newline */ .
Brett Wilson796ed472018-07-16 15:11:09 -07007250
7251 After a backslash, certain sequences represent special characters:
7252
7253 \" U+0022 quotation mark
7254 \$ U+0024 dollar sign
7255 \\ U+005C backslash
7256
7257 All other backslashes represent themselves.
7258
7259 To insert an arbitrary byte value, use $0xFF. For example, to insert a
7260 newline character: "Line one$0x0ALine two".
7261
7262 An expansion will evaluate the variable following the '$' and insert a
7263 stringified version of it into the result. For example, to concat two path
7264 components with a slash separating them:
7265 "$var_one/$var_two"
7266 Use the "${var_one}" format to be explicitly deliniate the variable for
7267 otherwise-ambiguous cases.
7268```
7269
7270#### **Punctuation**
7271
7272```
7273 The following character sequences represent punctuation:
7274
7275 + += == != ( )
7276 - -= < <= [ ]
7277 ! = > >= { }
7278 && || . ,
7279```
7280
7281#### **Grammar**
7282
7283```
7284 The input tokens form a syntax tree following a context-free grammar:
7285
7286 File = StatementList .
7287
7288 Statement = Assignment | Call | Condition .
7289 LValue = identifier | ArrayAccess | ScopeAccess .
7290 Assignment = LValue AssignOp Expr .
7291 Call = identifier "(" [ ExprList ] ")" [ Block ] .
7292 Condition = "if" "(" Expr ")" Block
7293 [ "else" ( Condition | Block ) ] .
7294 Block = "{" StatementList "}" .
7295 StatementList = { Statement } .
7296
7297 ArrayAccess = identifier "[" Expr "]" .
7298 ScopeAccess = identifier "." identifier .
7299 Expr = UnaryExpr | Expr BinaryOp Expr .
7300 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
7301 PrimaryExpr = identifier | integer | string | Call
7302 | ArrayAccess | ScopeAccess | Block
7303 | "(" Expr ")"
7304 | "[" [ ExprList [ "," ] ] "]" .
7305 ExprList = Expr { "," Expr } .
7306
7307 AssignOp = "=" | "+=" | "-=" .
7308 UnaryOp = "!" .
7309 BinaryOp = "+" | "-" // highest priority
7310 | "<" | "<=" | ">" | ">="
7311 | "==" | "!="
7312 | "&&"
7313 | "||" . // lowest priority
7314
7315 All binary operators are left-associative.
7316```
7317
7318#### **Types**
7319
7320```
7321 The GN language is dynamically typed. The following types are used:
7322
7323 - Boolean: Uses the keywords "true" and "false". There is no implicit
7324 conversion between booleans and integers.
7325
7326 - Integers: All numbers in GN are signed 64-bit integers.
7327
7328 - Strings: Strings are 8-bit with no enforced encoding. When a string is
7329 used to interact with other systems with particular encodings (like the
7330 Windows and Mac filesystems) it is assumed to be UTF-8. See "String
7331 literals" above for more.
7332
7333 - Lists: Lists are arbitrary-length ordered lists of values. See "Lists"
7334 below for more.
7335
7336 - Scopes: Scopes are like dictionaries that use variable names for keys. See
7337 "Scopes" below for more.
7338```
7339
7340#### **Lists**
7341
7342```
7343 Lists are created with [] and using commas to separate items:
7344
7345 mylist = [ 0, 1, 2, "some string" ]
7346
7347 A comma after the last item is optional. Lists are dereferenced using 0-based
7348 indexing:
7349
7350 mylist[0] += 1
7351 var = mylist[2]
7352
7353 Lists can be concatenated using the '+' and '+=' operators. Bare values can
7354 not be concatenated with lists, to add a single item, it must be put into a
7355 list of length one.
7356
7357 Items can be removed from lists using the '-' and '-=' operators. This will
7358 remove all occurrences of every item in the right-hand list from the
7359 left-hand list. It is an error to remove an item not in the list. This is to
7360 prevent common typos and to detect dead code that is removing things that no
7361 longer apply.
7362
7363 It is an error to use '=' to replace a nonempty list with another nonempty
7364 list. This is to prevent accidentally overwriting data when in most cases
7365 '+=' was intended. To overwrite a list on purpose, first assign it to the
7366 empty list:
7367
7368 mylist = []
7369 mylist = otherlist
Brett Wilson796ed472018-07-16 15:11:09 -07007370```
7371
7372#### **Scopes**
7373
7374```
7375 All execution happens in the context of a scope which holds the current state
7376 (like variables). With the exception of loops and conditions, '{' introduces
Tyler Mandry4a648092022-02-15 19:47:09 +00007377 a new scope.
Brett Wilson796ed472018-07-16 15:11:09 -07007378
Tyler Mandry4a648092022-02-15 19:47:09 +00007379 Most scopes have a parent reference to the old scope. Variable reads
7380 recursively search all parent scopes until the variable is found or there are
7381 no more scopes. Variable writes always go into the current scope. This means
7382 that after the closing '}' (again excepting loops and conditions), all local
7383 variables will be restored to the previous values. This also means that "foo
7384 = foo" can do useful work by copying a variable into the current scope that
7385 was defined in a containing scope.
Brett Wilson796ed472018-07-16 15:11:09 -07007386
Tyler Mandry4a648092022-02-15 19:47:09 +00007387 Scopes can be assigned to variables. Examples of such scopes are the
7388 implicitly-created "invoker" when invoking a template (which refers to the
7389 variables set by the invoking code), scopes created by functions like
7390 exec_script, and scopes explicitly created like
Brett Wilson796ed472018-07-16 15:11:09 -07007391
7392 empty_scope = {}
7393 myvalues = {
7394 foo = 21
7395 bar = "something"
7396 }
7397
Tyler Mandry4a648092022-02-15 19:47:09 +00007398 In the case of explicitly created scopes and scopes created by functions like
7399 exec_script, there is no reference to the parent scope. Such scopes are fully
7400 self-contained and do not "inherit" values from their defining scope.
7401
7402 Inside an explicit scope definition can be any GN code including conditionals
7403 and function calls. After the close of the scope, it will contain all
7404 variables explicitly set by the code contained inside it. After this, the
7405 values can be read, modified, or added to:
Brett Wilson796ed472018-07-16 15:11:09 -07007406
7407 myvalues.foo += 2
7408 empty_scope.new_thing = [ 1, 2, 3 ]
Nate Fischer8ed01d32019-01-08 17:32:01 -08007409
7410 Scope equality is defined as single-level scopes identical within the current
7411 scope. That is, all values in the first scope must be present and identical
7412 within the second, and vice versa. Note that this means inherited scopes are
7413 always unequal by definition.
Brett Wilson796ed472018-07-16 15:11:09 -07007414```
Julie Hockettd69a9c32019-01-23 14:36:18 -08007415### <a name="io_conversion"></a>**Input and output conversion**
Brett Wilson796ed472018-07-16 15:11:09 -07007416
7417```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007418 Input and output conversions are arguments to file and process functions
7419 that specify how to convert data to or from external formats. The possible
7420 values for parameters specifying conversions are:
7421
Brett Wilson796ed472018-07-16 15:11:09 -07007422 "" (the default)
Julie Hockett09171292018-07-31 14:35:10 -07007423 input: Discard the result and return None.
7424
7425 output: If value is a list, then "list lines"; otherwise "value".
Brett Wilson796ed472018-07-16 15:11:09 -07007426
7427 "list lines"
Julie Hockett09171292018-07-31 14:35:10 -07007428 input:
7429 Return the file contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007430 newlines will not be present in the result. The last line may or may
7431 not end in a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007432
Julie Hockett09171292018-07-31 14:35:10 -07007433 After splitting, each individual line will be trimmed of whitespace on
7434 both ends.
7435
7436 output:
7437 Renders the value contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007438 newlines will not be present in the result. The last line will end in
7439 with a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007440
7441 "scope"
Julie Hockett09171292018-07-31 14:35:10 -07007442 input:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007443 Execute the block as GN code and return a scope with the resulting
7444 values in it. If the input was:
Julie Hockett09171292018-07-31 14:35:10 -07007445 a = [ "hello.cc", "world.cc" ]
7446 b = 26
7447 and you read the result into a variable named "val", then you could
7448 access contents the "." operator on "val":
7449 sources = val.a
7450 some_count = val.b
7451
7452 output:
7453 Renders the value contents as a GN code block, reversing the input
7454 result above.
Brett Wilson796ed472018-07-16 15:11:09 -07007455
7456 "string"
Julie Hockett09171292018-07-31 14:35:10 -07007457 input: Return the file contents into a single string.
7458
7459 output:
7460 Render the value contents into a single string. The output is:
7461 a string renders with quotes, e.g. "str"
7462 an integer renders as a stringified integer, e.g. "6"
7463 a boolean renders as the associated string, e.g. "true"
7464 a list renders as a representation of its contents, e.g. "[\"str\", 6]"
7465 a scope renders as a GN code block of its values. If the Value was:
7466 Value val;
7467 val.a = [ "hello.cc", "world.cc" ];
7468 val.b = 26
7469 the resulting output would be:
7470 "{
7471 a = [ \"hello.cc\", \"world.cc\" ]
7472 b = 26
7473 }"
Brett Wilson796ed472018-07-16 15:11:09 -07007474
7475 "value"
Julie Hockett09171292018-07-31 14:35:10 -07007476 input:
7477 Parse the input as if it was a literal rvalue in a buildfile. Examples of
7478 typical program output using this mode:
7479 [ "foo", "bar" ] (result will be a list)
7480 or
7481 "foo bar" (result will be a string)
7482 or
7483 5 (result will be an integer)
Brett Wilson796ed472018-07-16 15:11:09 -07007484
Julie Hockett09171292018-07-31 14:35:10 -07007485 Note that if the input is empty, the result will be a null value which
7486 will produce an error if assigned to a variable.
7487
7488 output:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007489 Render the value contents as a literal rvalue. Strings render with
7490 escaped quotes.
Brett Wilson796ed472018-07-16 15:11:09 -07007491
7492 "json"
Julie Hockett09171292018-07-31 14:35:10 -07007493 input: Parse the input as a JSON and convert it to equivalent GN rvalue.
7494
7495 output: Convert the Value to equivalent JSON value.
7496
7497 The data type mapping is:
Brett Wilson796ed472018-07-16 15:11:09 -07007498 a string in JSON maps to string in GN
7499 an integer in JSON maps to integer in GN
7500 a float in JSON is unsupported and will result in an error
7501 an object in JSON maps to scope in GN
7502 an array in JSON maps to list in GN
7503 a boolean in JSON maps to boolean in GN
7504 a null in JSON is unsupported and will result in an error
7505
Julie Hockett09171292018-07-31 14:35:10 -07007506 Nota that the input dictionary keys have to be valid GN identifiers
7507 otherwise they will produce an error.
Brett Wilson796ed472018-07-16 15:11:09 -07007508
Julie Hockett09171292018-07-31 14:35:10 -07007509 "trim ..." (input only)
Brett Wilson796ed472018-07-16 15:11:09 -07007510 Prefixing any of the other transformations with the word "trim" will
7511 result in whitespace being trimmed from the beginning and end of the
7512 result before processing.
7513
7514 Examples: "trim string" or "trim list lines"
7515
7516 Note that "trim value" is useless because the value parser skips
7517 whitespace anyway.
7518```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01007519### <a name="file_pattern"></a>**File patterns**
7520
7521```
7522 File patterns are VERY limited regular expressions. They must match the
7523 entire input string to be counted as a match. In regular expression parlance,
7524 there is an implicit "^...$" surrounding your input. If you want to match a
7525 substring, you need to use wildcards at the beginning and end.
7526
7527 There are only two special tokens understood by the pattern matcher.
7528 Everything else is a literal.
7529
7530 - "*" Matches zero or more of any character. It does not depend on the
7531 preceding character (in regular expression parlance it is equivalent to
7532 ".*").
7533
7534 - "\b" Matches a path boundary. This will match the beginning or end of a
7535 string, or a slash.
7536```
7537
7538#### **Pattern examples**
7539
7540```
7541 "*asdf*"
7542 Matches a string containing "asdf" anywhere.
7543
7544 "asdf"
7545 Matches only the exact string "asdf".
7546
7547 "*.cc"
7548 Matches strings ending in the literal ".cc".
7549
7550 "\bwin/*"
7551 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
7552```
Brett Wilson796ed472018-07-16 15:11:09 -07007553### <a name="label_pattern"></a>**Label patterns**
7554
7555```
7556 A label pattern is a way of expressing one or more labels in a portion of the
7557 source tree. They are not general regular expressions.
7558
7559 They can take the following forms only:
7560
7561 - Explicit (no wildcard):
7562 "//foo/bar:baz"
7563 ":baz"
7564
7565 - Wildcard target names:
7566 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
7567 ":*" (all targets in the current build file)
7568
7569 - Wildcard directory names ("*" is only supported at the end)
7570 "*" (all targets)
7571 "//foo/bar/*" (all targets in any subdir of //foo/bar)
7572 "./*" (all targets in the current build file or sub dirs)
7573
Julie Hockette2a29402018-07-31 10:11:42 -07007574 Any of the above forms can additionally take an explicit toolchain
7575 in parenthesis at the end of the label pattern. In this case, the
7576 toolchain must be fully qualified (no wildcards are supported in the
7577 toolchain name).
Brett Wilson796ed472018-07-16 15:11:09 -07007578
Julie Hockette2a29402018-07-31 10:11:42 -07007579 "//foo:bar(//build/toolchain:mac)"
Brett Wilson796ed472018-07-16 15:11:09 -07007580 An explicit target in an explicit toolchain.
7581
7582 ":*(//build/toolchain/linux:32bit)"
7583 All targets in the current build file using the 32-bit Linux toolchain.
7584
7585 "//foo/*(//build/toolchain:win)"
7586 All targets in //foo and any subdirectory using the Windows
7587 toolchain.
7588```
7589### <a name="labels"></a>**About labels**
7590
7591```
7592 Everything that can participate in the dependency graph (targets, configs,
7593 and toolchains) are identified by labels. A common label looks like:
7594
7595 //base/test:test_support
7596
7597 This consists of a source-root-absolute path, a colon, and a name. This means
7598 to look for the thing named "test_support" in "base/test/BUILD.gn".
7599
7600 You can also specify system absolute paths if necessary. Typically such
7601 paths would be specified via a build arg so the developer can specify where
7602 the component is on their system.
7603
7604 /usr/local/foo:bar (Posix)
7605 /C:/Program Files/MyLibs:bar (Windows)
7606```
7607
7608#### **Toolchains**
7609
7610```
7611 A canonical label includes the label of the toolchain being used. Normally,
7612 the toolchain label is implicitly inherited from the current execution
7613 context, but you can override this to specify cross-toolchain dependencies:
7614
7615 //base/test:test_support(//build/toolchain/win:msvc)
7616
7617 Here GN will look for the toolchain definition called "msvc" in the file
7618 "//build/toolchain/win" to know how to compile this target.
7619```
7620
7621#### **Relative labels**
7622
7623```
7624 If you want to refer to something in the same buildfile, you can omit
7625 the path name and just start with a colon. This format is recommended for
7626 all same-file references.
7627
7628 :base
7629
7630 Labels can be specified as being relative to the current directory.
7631 Stylistically, we prefer to use absolute paths for all non-file-local
7632 references unless a build file needs to be run in different contexts (like a
7633 project needs to be both standalone and pulled into other projects in
7634 difference places in the directory hierarchy).
7635
7636 source/plugin:myplugin
7637 ../net:url_request
7638```
7639
7640#### **Implicit names**
7641
7642```
7643 If a name is unspecified, it will inherit the directory name. Stylistically,
7644 we prefer to omit the colon and name when possible:
7645
7646 //net -> //net:net
7647 //tools/gn -> //tools/gn:gn
7648```
Julie Hockett152c5142019-07-12 09:53:43 -06007649### <a name="metadata_collection"></a>**Metadata Collection**
7650
7651```
7652 Metadata is information attached to targets throughout the dependency tree. GN
7653 allows for the collection of this data into files written during the generation
Keir Mierle45611e32019-11-12 11:18:00 -08007654 step, enabling users to expose and aggregate this data based on the dependency
Julie Hockett152c5142019-07-12 09:53:43 -06007655 tree.
7656```
7657
7658#### **generated_file targets**
7659
7660```
7661 Similar to the write_file() function, the generated_file target type
7662 creates a file in the specified location with the specified content. The
Keir Mierle45611e32019-11-12 11:18:00 -08007663 primary difference between write_file() and this target type is that the
Julie Hockett152c5142019-07-12 09:53:43 -06007664 write_file function does the file write at parse time, while the
7665 generated_file target type writes at target resolution time. See
7666 "gn help generated_file" for more detail.
7667
Keir Mierle45611e32019-11-12 11:18:00 -08007668 When written at target resolution time, generated_file enables GN to
Julie Hockett152c5142019-07-12 09:53:43 -06007669 collect and write aggregated metadata from dependents.
7670
Keir Mierle45611e32019-11-12 11:18:00 -08007671 A generated_file target can declare either 'contents' to write statically
7672 known contents to a file or 'data_keys' to aggregate metadata and write the
7673 result to a file. It can also specify 'walk_keys' (to restrict the metadata
Julie Hockett152c5142019-07-12 09:53:43 -06007674 collection), 'output_conversion', and 'rebase'.
7675```
7676
7677#### **Collection and Aggregation**
7678
7679```
7680 Targets can declare a 'metadata' variable containing a scope, and this
Keir Mierle45611e32019-11-12 11:18:00 -08007681 metadata may be collected and written out to a file specified by
7682 generated_file aggregation targets. The 'metadata' scope must contain
7683 only list values since the aggregation step collects a list of these values.
Julie Hockett152c5142019-07-12 09:53:43 -06007684
7685 During the target resolution, generated_file targets will walk their
7686 dependencies recursively, collecting metadata based on the specified
7687 'data_keys'. 'data_keys' is specified as a list of strings, used by the walk
7688 to identify which variables in dependencies' 'metadata' scopes to collect.
7689
Keir Mierle45611e32019-11-12 11:18:00 -08007690 The walk begins with the listed dependencies of the 'generated_file' target.
7691 The 'metadata' scope for each dependency is inspected for matching elements
7692 of the 'generated_file' target's 'data_keys' list. If a match is found, the
7693 data from the dependent's matching key list is appended to the aggregate walk
7694 list. Note that this means that if more than one walk key is specified, the
7695 data in all of them will be aggregated into one list. From there, the walk
7696 will then recurse into the dependencies of each target it encounters,
7697 collecting the specified metadata for each.
Julie Hockett152c5142019-07-12 09:53:43 -06007698
7699 For example:
7700
7701 group("a") {
7702 metadata = {
7703 doom_melon = [ "enable" ]
7704 my_files = [ "foo.cpp" ]
7705 my_extra_files = [ "bar.cpp" ]
7706 }
7707
7708 deps = [ ":b" ]
7709 }
7710
7711 group("b") {
7712 metadata = {
7713 my_files = [ "baz.cpp" ]
7714 }
7715 }
7716
7717 generated_file("metadata") {
7718 outputs = [ "$root_build_dir/my_files.json" ]
7719 data_keys = [ "my_files", "my_extra_files" ]
7720
7721 deps = [ ":a" ]
7722 }
7723
7724 The above will produce the following file data:
7725
7726 foo.cpp
7727 bar.cpp
7728 baz.cpp
7729
Keir Mierle45611e32019-11-12 11:18:00 -08007730 The dependency walk can be limited by using the 'walk_keys'. This is a list of
Julie Hockett152c5142019-07-12 09:53:43 -06007731 labels that should be included in the walk. All labels specified here should
7732 also be in one of the deps lists. These keys act as barriers, where the walk
Keir Mierle45611e32019-11-12 11:18:00 -08007733 will only recurse into the targets listed. An empty list in all specified
Julie Hockett152c5142019-07-12 09:53:43 -06007734 barriers will end that portion of the walk.
7735
7736 group("a") {
7737 metadata = {
7738 my_files = [ "foo.cpp" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02007739 my_files_barrier = [ ":b" ]
Julie Hockett152c5142019-07-12 09:53:43 -06007740 }
7741
7742 deps = [ ":b", ":c" ]
7743 }
7744
7745 group("b") {
7746 metadata = {
7747 my_files = [ "bar.cpp" ]
7748 }
7749 }
7750
7751 group("c") {
7752 metadata = {
7753 my_files = [ "doom_melon.cpp" ]
7754 }
7755 }
7756
7757 generated_file("metadata") {
7758 outputs = [ "$root_build_dir/my_files.json" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02007759 data_keys = [ "my_files" ]
7760 walk_keys = [ "my_files_barrier" ]
Julie Hockett152c5142019-07-12 09:53:43 -06007761
7762 deps = [ ":a" ]
7763 }
7764
7765 The above will produce the following file data (note that `doom_melon.cpp` is
7766 not included):
7767
7768 foo.cpp
7769 bar.cpp
7770
7771 A common example of this sort of barrier is in builds that have host tools
7772 built as part of the tree, but do not want the metadata from those host tools
7773 to be collected with the target-side code.
7774```
7775
7776#### **Common Uses**
7777
7778```
7779 Metadata can be used to collect information about the different targets in the
7780 build, and so a common use is to provide post-build tooling with a set of data
7781 necessary to do aggregation tasks. For example, if each test target specifies
7782 the output location of its binary to run in a metadata field, that can be
7783 collected into a single file listing the locations of all tests in the
7784 dependency tree. A local build tool (or continuous integration infrastructure)
7785 can then use that file to know which tests exist, and where, and run them
7786 accordingly.
7787
7788 Another use is in image creation, where a post-build image tool needs to know
7789 various pieces of information about the components it should include in order
7790 to put together the correct image.
7791```
Brett Wilson796ed472018-07-16 15:11:09 -07007792### <a name="ninja_rules"></a>**Ninja build rules**
7793
7794#### **The "all" and "default" rules**
7795
7796```
7797 All generated targets (see "gn help execution") will be added to an implicit
7798 build rule called "all" so "ninja all" will always compile everything. The
7799 default rule will be used by Ninja if no specific target is specified (just
7800 typing "ninja"). If there is a target named "default" in the root build file,
7801 it will be the default build rule, otherwise the implicit "all" rule will be
7802 used.
7803```
7804
7805#### **Phony rules**
7806
7807```
7808 GN generates Ninja "phony" rules for targets in the default toolchain. The
7809 phony rules can collide with each other and with the names of generated files
7810 so are generated with the following priority:
7811
7812 1. Actual files generated by the build always take precedence.
7813
7814 2. Targets in the toplevel //BUILD.gn file.
7815
7816 3. Targets in toplevel directories matching the names of the directories.
7817 So "ninja foo" can be used to compile "//foo:foo". This only applies to
7818 the first level of directories since usually these are the most
7819 important (so this won't apply to "//foo/bar:bar").
7820
7821 4. The short names of executables if there is only one executable with that
7822 short name. Use "ninja doom_melon" to compile the
7823 "//tools/fruit:doom_melon" executable.
7824
Sylvain Defresne5e19d2f2022-12-12 18:20:36 +01007825 Note that for Apple platforms, create_bundle targets with a product_type
7826 of "com.apple.product-type.application" are considered as executable
7827 for this rule (as they define application bundles).
7828
Brett Wilson796ed472018-07-16 15:11:09 -07007829 5. The short names of all targets if there is only one target with that
7830 short name.
7831
7832 6. Full label name with no leading slashes. So you can use
7833 "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon".
7834
7835 7. Labels with an implicit name part (when the short names match the
7836 directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar".
7837
7838 These "phony" rules are provided only for running Ninja since this matches
7839 people's historical expectations for building. For consistency with the rest
7840 of the program, GN introspection commands accept explicit labels.
7841
7842 To explicitly compile a target in a non-default toolchain, you must give
7843 Ninja the exact name of the output file relative to the build directory.
7844```
7845### <a name="nogncheck"></a>**nogncheck**: Skip an include line from checking.
7846
7847```
7848 GN's header checker helps validate that the includes match the build
7849 dependency graph. Sometimes an include might be conditional or otherwise
7850 problematic, but you want to specifically allow it. In this case, it can be
7851 whitelisted.
7852
7853 Include lines containing the substring "nogncheck" will be excluded from
7854 header checking. The most common case is a conditional include:
7855
7856 #if defined(ENABLE_DOOM_MELON)
7857 #include "tools/doom_melon/doom_melon.h" // nogncheck
7858 #endif
7859
7860 If the build file has a conditional dependency on the corresponding target
7861 that matches the conditional include, everything will always link correctly:
7862
7863 source_set("mytarget") {
7864 ...
7865 if (enable_doom_melon) {
7866 defines = [ "ENABLE_DOOM_MELON" ]
7867 deps += [ "//tools/doom_melon" ]
7868 }
7869
7870 But GN's header checker does not understand preprocessor directives, won't
7871 know it matches the build dependencies, and will flag this include as
7872 incorrect when the condition is false.
7873```
7874
7875#### **More information**
7876
7877```
7878 The topic "gn help check" has general information on how checking works and
7879 advice on fixing problems. Targets can also opt-out of checking, see
7880 "gn help check_includes".
7881```
Brett Wilson796ed472018-07-16 15:11:09 -07007882### <a name="runtime_deps"></a>**Runtime dependencies**
7883
7884```
7885 Runtime dependencies of a target are exposed via the "runtime_deps" category
7886 of "gn desc" (see "gn help desc") or they can be written at build generation
7887 time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help
7888 --runtime-deps-list-file").
7889
7890 To a first approximation, the runtime dependencies of a target are the set of
7891 "data" files, data directories, and the shared libraries from all transitive
7892 dependencies. Executables, shared libraries, and loadable modules are
7893 considered runtime dependencies of themselves.
7894```
7895
7896#### **Executables**
7897
7898```
7899 Executable targets and those executable targets' transitive dependencies are
7900 not considered unless that executable is listed in "data_deps". Otherwise, GN
7901 assumes that the executable (and everything it requires) is a build-time
7902 dependency only.
7903```
7904
7905#### **Actions and copies**
7906
7907```
7908 Action and copy targets that are listed as "data_deps" will have all of their
7909 outputs and data files considered as runtime dependencies. Action and copy
7910 targets that are "deps" or "public_deps" will have only their data files
7911 considered as runtime dependencies. These targets can list an output file in
7912 both the "outputs" and "data" lists to force an output file as a runtime
7913 dependency in all cases.
7914
7915 The different rules for deps and data_deps are to express build-time (deps)
7916 vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as
7917 data dependencies, there would be a lot of extra stuff, and if GN counted all
7918 run-time dependencies as regular deps, the build's parallelism would be
7919 unnecessarily constrained.
7920
7921 This rule can sometimes lead to unintuitive results. For example, given the
7922 three targets:
7923 A --[data_deps]--> B --[deps]--> ACTION
7924 GN would say that A does not have runtime deps on the result of the ACTION,
7925 which is often correct. But the purpose of the B target might be to collect
7926 many actions into one logic unit, and the "data"-ness of A's dependency is
7927 lost. Solutions:
7928
7929 - List the outputs of the action in its data section (if the results of
7930 that action are always runtime files).
7931 - Have B list the action in data_deps (if the outputs of the actions are
7932 always runtime files).
7933 - Have B list the action in both deps and data deps (if the outputs might be
7934 used in both contexts and you don't care about unnecessary entries in the
7935 list of files required at runtime).
7936 - Split B into run-time and build-time versions with the appropriate "deps"
7937 for each.
7938```
7939
7940#### **Static libraries and source sets**
7941
7942```
7943 The results of static_library or source_set targets are not considered
7944 runtime dependencies since these are assumed to be intermediate targets only.
7945 If you need to list a static library as a runtime dependency, you can
7946 manually compute the .a/.lib file name for the current platform and list it
7947 in the "data" list of a target (possibly on the static library target
7948 itself).
7949```
7950
7951#### **Multiple outputs**
7952
7953```
7954 Linker tools can specify which of their outputs should be considered when
7955 computing the runtime deps by setting runtime_outputs. If this is unset on
7956 the tool, the default will be the first output only.
7957```
7958### <a name="source_expansion"></a>**How Source Expansion Works**
7959
7960```
7961 Source expansion is used for the action_foreach and copy target types to map
7962 source file names to output file names or arguments.
7963
7964 To perform source expansion in the outputs, GN maps every entry in the
7965 sources to every entry in the outputs list, producing the cross product of
7966 all combinations, expanding placeholders (see below).
7967
7968 Source expansion in the args works similarly, but performing the placeholder
7969 substitution produces a different set of arguments for each invocation of the
7970 script.
7971
7972 If no placeholders are found, the outputs or args list will be treated as a
7973 static list of literal file names that do not depend on the sources.
7974
7975 See "gn help copy" and "gn help action_foreach" for more on how this is
7976 applied.
7977```
7978
7979#### **Placeholders**
7980
7981```
7982 This section discusses only placeholders for actions. There are other
7983 placeholders used in the definition of tools. See "gn help tool" for those.
7984
7985 {{source}}
7986 The name of the source file including directory (*). This will generally
7987 be used for specifying inputs to a script in the "args" variable.
7988 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
7989
7990 {{source_file_part}}
7991 The file part of the source including the extension.
7992 "//foo/bar/baz.txt" => "baz.txt"
7993
7994 {{source_name_part}}
7995 The filename part of the source file with no directory or extension. This
7996 will generally be used for specifying a transformation from a source file
7997 to a destination file with the same name but different extension.
7998 "//foo/bar/baz.txt" => "baz"
7999
8000 {{source_dir}}
8001 The directory (*) containing the source file with no trailing slash.
8002 "//foo/bar/baz.txt" => "../../foo/bar"
8003
8004 {{source_root_relative_dir}}
8005 The path to the source file's directory relative to the source root, with
8006 no leading "//" or trailing slashes. If the path is system-absolute,
8007 (beginning in a single slash) this will just return the path with no
8008 trailing slash. This value will always be the same, regardless of whether
8009 it appears in the "outputs" or "args" section.
8010 "//foo/bar/baz.txt" => "foo/bar"
8011
8012 {{source_gen_dir}}
8013 The generated file directory (*) corresponding to the source file's path.
8014 This will be different than the target's generated file directory if the
8015 source file is in a different directory than the BUILD.gn file.
8016 "//foo/bar/baz.txt" => "gen/foo/bar"
8017
8018 {{source_out_dir}}
8019 The object file directory (*) corresponding to the source file's path,
8020 relative to the build directory. this us be different than the target's
8021 out directory if the source file is in a different directory than the
8022 build.gn file.
8023 "//foo/bar/baz.txt" => "obj/foo/bar"
8024
8025 {{source_target_relative}}
8026 The path to the source file relative to the target's directory. This will
8027 generally be used for replicating the source directory layout in the
Robert Sesek252674d2019-05-10 14:08:52 -04008028 output directory. This can only be used in actions and bundle_data
8029 targets. It is an error to use in process_file_template where there is no
8030 "target".
Brett Wilson796ed472018-07-16 15:11:09 -07008031 "//foo/bar/baz.txt" => "baz.txt"
8032```
8033
8034#### **(*) Note on directories**
8035
8036```
8037 Paths containing directories (except the source_root_relative_dir) will be
8038 different depending on what context the expansion is evaluated in. Generally
8039 it should "just work" but it means you can't concatenate strings containing
8040 these values with reasonable results.
8041
8042 Details: source expansions can be used in the "outputs" variable, the "args"
8043 variable, and in calls to "process_file_template". The "args" are passed to a
8044 script which is run from the build directory, so these directories will
8045 relative to the build directory for the script to find. In the other cases,
8046 the directories will be source- absolute (begin with a "//") because the
8047 results of those expansions will be handled by GN internally.
8048```
8049
8050#### **Examples**
8051
8052```
8053 Non-varying outputs:
8054 action("hardcoded_outputs") {
8055 sources = [ "input1.idl", "input2.idl" ]
8056 outputs = [ "$target_out_dir/output1.dat",
8057 "$target_out_dir/output2.dat" ]
8058 }
8059 The outputs in this case will be the two literal files given.
8060
8061 Varying outputs:
8062 action_foreach("varying_outputs") {
8063 sources = [ "input1.idl", "input2.idl" ]
8064 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
8065 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
8066 }
8067 Performing source expansion will result in the following output names:
8068 //out/Debug/obj/mydirectory/input1.h
8069 //out/Debug/obj/mydirectory/input1.cc
8070 //out/Debug/obj/mydirectory/input2.h
8071 //out/Debug/obj/mydirectory/input2.cc
8072```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008073### <a name="switch_list"></a>**Available global switches**
Brett Wilson796ed472018-07-16 15:11:09 -07008074
8075```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008076 Do "gn help --the_switch_you_want_help_on" for more. Individual commands may
8077 take command-specific switches not listed here. See the help on your specific
8078 command for more.
8079```
8080```
8081 * --args: Specifies build arguments overrides.
8082 * --color: Force colored output.
8083 * --dotfile: Override the name of the ".gn" file.
8084 * --fail-on-unused-args: Treat unused build args as fatal errors.
8085 * --markdown: Write help output in the Markdown format.
RJ Ascani6966efb2020-10-19 16:50:11 -07008086 * --ninja-executable: Set the Ninja executable.
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008087 * --nocolor: Force non-colored output.
8088 * -q: Quiet mode. Don't print output on success.
8089 * --root: Explicitly specify source root.
Nico Weberad9eab22020-11-15 22:20:28 -05008090 * --root-target: Override the root target.
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008091 * --runtime-deps-list-file: Save runtime dependencies for targets in file.
8092 * --script-executable: Set the executable used to execute scripts.
8093 * --threads: Specify number of worker threads.
8094 * --time: Outputs a summary of how long everything took.
8095 * --tracelog: Writes a Chrome-compatible trace log to the given file.
8096 * -v: Verbose logging.
8097 * --version: Prints the GN version number and exits.
Brett Wilson796ed472018-07-16 15:11:09 -07008098```
8099