blob: 0dac783ed4a21bbe75e0f991f5478443c74d2cc5 [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)
12 * [desc: Show lots of insightful information about a target or config.](#cmd_desc)
Julie Hockettd69a9c32019-01-23 14:36:18 -080013 * [format: Format .gn files.](#cmd_format)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080014 * [gen: Generate ninja files.](#cmd_gen)
15 * [help: Does what you think.](#cmd_help)
16 * [ls: List matching targets.](#cmd_ls)
17 * [meta: List target metadata collection results.](#cmd_meta)
Brett Wilson74397092020-03-18 16:34:14 -070018 * [outputs: Which files a source/target make.](#cmd_outputs)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080019 * [path: Find paths between two targets.](#cmd_path)
20 * [refs: Find stuff referencing a target or file.](#cmd_refs)
Brett Wilson796ed472018-07-16 15:11:09 -070021* [Target declarations](#targets)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080022 * [action: Declare a target that runs a script a single time.](#func_action)
23 * [action_foreach: Declare a target that runs a script over a set of files.](#func_action_foreach)
24 * [bundle_data: [iOS/macOS] Declare a target without output.](#func_bundle_data)
25 * [copy: Declare a target that copies files.](#func_copy)
26 * [create_bundle: [iOS/macOS] Build an iOS or macOS bundle.](#func_create_bundle)
27 * [executable: Declare an executable target.](#func_executable)
28 * [generated_file: Declare a generated_file target.](#func_generated_file)
29 * [group: Declare a named group of targets.](#func_group)
30 * [loadable_module: Declare a loadable module target.](#func_loadable_module)
Julie Hockettce1fa072019-05-07 17:44:37 -070031 * [rust_library: Declare a Rust library target.](#func_rust_library)
Petr Hosekfe36c7c2019-11-26 14:38:08 -080032 * [rust_proc_macro: Declare a Rust procedural macro target.](#func_rust_proc_macro)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080033 * [shared_library: Declare a shared library target.](#func_shared_library)
34 * [source_set: Declare a source set target.](#func_source_set)
35 * [static_library: Declare a static library target.](#func_static_library)
36 * [target: Declare an target with the given programmatic type.](#func_target)
Brett Wilson796ed472018-07-16 15:11:09 -070037* [Buildfile functions](#functions)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080038 * [assert: Assert an expression is true at generation time.](#func_assert)
39 * [config: Defines a configuration object.](#func_config)
40 * [declare_args: Declare build arguments.](#func_declare_args)
41 * [defined: Returns whether an identifier is defined.](#func_defined)
42 * [exec_script: Synchronously run a script and return the output.](#func_exec_script)
Sylvain Defresneaff489a2020-03-11 18:27:43 +010043 * [filter_exclude: Remove values that match a set of patterns.](#func_filter_exclude)
44 * [filter_include: Remove values that do not match a set of patterns.](#func_filter_include)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080045 * [foreach: Iterate over a list.](#func_foreach)
46 * [forward_variables_from: Copies variables from a different scope.](#func_forward_variables_from)
47 * [get_label_info: Get an attribute from a target's label.](#func_get_label_info)
48 * [get_path_info: Extract parts of a file or directory name.](#func_get_path_info)
49 * [get_target_outputs: [file list] Get the list of outputs from a target.](#func_get_target_outputs)
50 * [getenv: Get an environment variable.](#func_getenv)
51 * [import: Import a file into the current scope.](#func_import)
52 * [not_needed: Mark variables from scope as not needed.](#func_not_needed)
53 * [pool: Defines a pool object.](#func_pool)
54 * [print: Prints to the console.](#func_print)
55 * [process_file_template: Do template expansion over a list of files.](#func_process_file_template)
56 * [read_file: Read a file into a variable.](#func_read_file)
57 * [rebase_path: Rebase a file or directory to another location.](#func_rebase_path)
58 * [set_default_toolchain: Sets the default toolchain name.](#func_set_default_toolchain)
59 * [set_defaults: Set default values for a target type.](#func_set_defaults)
60 * [set_sources_assignment_filter: Set a pattern to filter source files.](#func_set_sources_assignment_filter)
61 * [split_list: Splits a list into N different sub-lists.](#func_split_list)
Keir Mierle6ae63302019-11-08 23:02:18 -080062 * [string_join: Concatenates a list of strings with a separator.](#func_string_join)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080063 * [string_replace: Replaces substring in the given string.](#func_string_replace)
Keir Mierle6ae63302019-11-08 23:02:18 -080064 * [string_split: Split string into a list of strings.](#func_string_split)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080065 * [template: Define a template rule.](#func_template)
66 * [tool: Specify arguments to a toolchain tool.](#func_tool)
67 * [toolchain: Defines a toolchain.](#func_toolchain)
68 * [write_file: Write a file to disk.](#func_write_file)
Brett Wilson796ed472018-07-16 15:11:09 -070069* [Built-in predefined variables](#predefined_variables)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080070 * [current_cpu: [string] The processor architecture of the current toolchain.](#var_current_cpu)
71 * [current_os: [string] The operating system of the current toolchain.](#var_current_os)
72 * [current_toolchain: [string] Label of the current toolchain.](#var_current_toolchain)
73 * [default_toolchain: [string] Label of the default toolchain.](#var_default_toolchain)
James Robinson61377e32020-02-13 15:20:07 -080074 * [gn_version: [number] The version of gn.](#var_gn_version)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080075 * [host_cpu: [string] The processor architecture that GN is running on.](#var_host_cpu)
76 * [host_os: [string] The operating system that GN is running on.](#var_host_os)
77 * [invoker: [string] The invoking scope inside a template.](#var_invoker)
78 * [python_path: [string] Absolute path of Python.](#var_python_path)
79 * [root_build_dir: [string] Directory where build commands are run.](#var_root_build_dir)
80 * [root_gen_dir: [string] Directory for the toolchain's generated files.](#var_root_gen_dir)
81 * [root_out_dir: [string] Root directory for toolchain output files.](#var_root_out_dir)
82 * [target_cpu: [string] The desired cpu architecture for the build.](#var_target_cpu)
83 * [target_gen_dir: [string] Directory for a target's generated files.](#var_target_gen_dir)
84 * [target_name: [string] The name of the current target.](#var_target_name)
85 * [target_os: [string] The desired operating system for the build.](#var_target_os)
86 * [target_out_dir: [string] Directory for target output files.](#var_target_out_dir)
Brett Wilson796ed472018-07-16 15:11:09 -070087* [Variables you set in targets](#target_variables)
Julie Hockettce1fa072019-05-07 17:44:37 -070088 * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080089 * [all_dependent_configs: [label list] Configs to be forced on dependents.](#var_all_dependent_configs)
90 * [allow_circular_includes_from: [label list] Permit includes from deps.](#var_allow_circular_includes_from)
91 * [arflags: [string list] Arguments passed to static_library archiver.](#var_arflags)
92 * [args: [string list] Arguments passed to an action.](#var_args)
93 * [asmflags: [string list] Flags passed to the assembler.](#var_asmflags)
94 * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#var_assert_no_deps)
95 * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#var_bundle_contents_dir)
96 * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#var_bundle_deps_filter)
97 * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#var_bundle_executable_dir)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080098 * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#var_bundle_resources_dir)
99 * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#var_bundle_root_dir)
100 * [cflags: [string list] Flags passed to all C compiler variants.](#var_cflags)
101 * [cflags_c: [string list] Flags passed to the C compiler.](#var_cflags_c)
102 * [cflags_cc: [string list] Flags passed to the C++ compiler.](#var_cflags_cc)
103 * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#var_cflags_objc)
104 * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#var_cflags_objcc)
105 * [check_includes: [boolean] Controls whether a target's files are checked.](#var_check_includes)
106 * [code_signing_args: [string list] Arguments passed to code signing script.](#var_code_signing_args)
107 * [code_signing_outputs: [file list] Output files for code signing step.](#var_code_signing_outputs)
108 * [code_signing_script: [file name] Script for code signing.](#var_code_signing_script)
109 * [code_signing_sources: [file list] Sources for code signing step.](#var_code_signing_sources)
110 * [complete_static_lib: [boolean] Links all deps into a static library.](#var_complete_static_lib)
111 * [configs: [label list] Configs applying to this target or config.](#var_configs)
112 * [contents: Contents to write to file.](#var_contents)
Julie Hockettce1fa072019-05-07 17:44:37 -0700113 * [crate_name: [string] The name for the compiled crate.](#var_crate_name)
114 * [crate_root: [string] The root source file for a binary or library.](#var_crate_root)
115 * [crate_type: [string] The type of linkage to use on a shared_library.](#var_crate_type)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800116 * [data: [file list] Runtime data file dependencies.](#var_data)
117 * [data_deps: [label list] Non-linked dependencies.](#var_data_deps)
118 * [data_keys: [string list] Keys from which to collect metadata.](#var_data_keys)
119 * [defines: [string list] C preprocessor defines.](#var_defines)
120 * [depfile: [string] File name for input dependencies for actions.](#var_depfile)
121 * [deps: [label list] Private linked dependencies.](#var_deps)
Petr Hoseka1413862020-01-03 12:54:33 -0800122 * [externs: [scope] Set of Rust crate-dependency pairs.](#var_externs)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100123 * [framework_dirs: [directory list] Additional framework search directories.](#var_framework_dirs)
124 * [frameworks: [name list] Name of frameworks that must be linked.](#var_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800125 * [friend: [label pattern list] Allow targets to include private headers.](#var_friend)
126 * [include_dirs: [directory list] Additional include directories.](#var_include_dirs)
127 * [inputs: [file list] Additional compile-time dependencies.](#var_inputs)
128 * [ldflags: [string list] Flags passed to the linker.](#var_ldflags)
129 * [lib_dirs: [directory list] Additional library directories.](#var_lib_dirs)
130 * [libs: [string list] Additional libraries to link.](#var_libs)
131 * [metadata: [scope] Metadata of this target.](#var_metadata)
132 * [output_conversion: Data format for generated_file targets.](#var_output_conversion)
133 * [output_dir: [directory] Directory to put output file in.](#var_output_dir)
134 * [output_extension: [string] Value to use for the output's file extension.](#var_output_extension)
135 * [output_name: [string] Name for the output file other than the default.](#var_output_name)
136 * [output_prefix_override: [boolean] Don't use prefix for output name.](#var_output_prefix_override)
137 * [outputs: [file list] Output files for actions and copy targets.](#var_outputs)
138 * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#var_partial_info_plist)
139 * [pool: [string] Label of the pool used by the action.](#var_pool)
140 * [precompiled_header: [string] Header file to precompile.](#var_precompiled_header)
141 * [precompiled_header_type: [string] "gcc" or "msvc".](#var_precompiled_header_type)
142 * [precompiled_source: [file name] Source file to precompile.](#var_precompiled_source)
143 * [product_type: [string] Product type for Xcode projects.](#var_product_type)
144 * [public: [file list] Declare public header files for a target.](#var_public)
145 * [public_configs: [label list] Configs applied to dependents.](#var_public_configs)
146 * [public_deps: [label list] Declare public dependencies.](#var_public_deps)
147 * [rebase: [boolean] Rebase collected metadata as files.](#var_rebase)
148 * [response_file_contents: [string list] Contents of .rsp file for actions.](#var_response_file_contents)
149 * [script: [file name] Script file for actions.](#var_script)
150 * [sources: [file list] Source files for a target.](#var_sources)
151 * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly)
152 * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility)
153 * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys)
Robert Sesekd0a6f072020-05-15 11:21:22 -0400154 * [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800155 * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps)
Harley Li0a9affb2020-06-03 10:38:42 -0400156 * [xcasset_compiler_flags: [string list] Flags passed to xcassets compiler](#var_xcasset_compiler_flags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800157 * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes)
158 * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name)
Brett Wilson796ed472018-07-16 15:11:09 -0700159* [Other help topics](#other)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800160 * all: Print all the help at once
Brett Wilson796ed472018-07-16 15:11:09 -0700161 * [buildargs: How build arguments work.](#buildargs)
162 * [dotfile: Info about the toplevel .gn file.](#dotfile)
163 * [execution: Build graph and execution overview.](#execution)
164 * [grammar: Language and grammar for GN build files.](#grammar)
Julie Hockett81ee1962019-05-13 11:24:42 -0700165 * [input_conversion: Processing input from exec_script and read_file.](#io_conversion)
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100166 * [file_pattern: Matching more than one file.](#file_pattern)
Brett Wilson796ed472018-07-16 15:11:09 -0700167 * [label_pattern: Matching more than one label.](#label_pattern)
168 * [labels: About labels.](#labels)
Julie Hockett152c5142019-07-12 09:53:43 -0600169 * [metadata_collection: About metadata and its collection.](#metadata_collection)
Brett Wilson796ed472018-07-16 15:11:09 -0700170 * [ninja_rules: How Ninja build rules are named.](#ninja_rules)
171 * [nogncheck: Annotating includes for checking.](#nogncheck)
Julie Hockett81ee1962019-05-13 11:24:42 -0700172 * [output_conversion: Specifies how to transform a value to output.](#io_conversion)
Brett Wilson796ed472018-07-16 15:11:09 -0700173 * [runtime_deps: How runtime dependency computation works.](#runtime_deps)
174 * [source_expansion: Map sources to outputs for scripts.](#source_expansion)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800175 * [switches: Show available command-line switches.](#switch_list)
Brett Wilson796ed472018-07-16 15:11:09 -0700176
177## Commands
178
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800179### **gn analyze <out_dir> <input_path> <output_path>**
Brett Wilson796ed472018-07-16 15:11:09 -0700180
181```
182 Analyze which targets are affected by a list of files.
183
184 This command takes three arguments:
185
186 out_dir is the path to the build directory.
187
188 input_path is a path to a file containing a JSON object with three fields:
189
190 - "files": A list of the filenames to check.
191
192 - "test_targets": A list of the labels for targets that are needed to run
193 the tests we wish to run.
194
195 - "additional_compile_targets": A list of the labels for targets that we
196 wish to rebuild, but aren't necessarily needed for testing. The important
197 difference between this field and "test_targets" is that if an item in
198 the additional_compile_targets list refers to a group, then any
199 dependencies of that group will be returned if they are out of date, but
200 the group itself does not need to be. If the dependencies themselves are
201 groups, the same filtering is repeated. This filtering can be used to
202 avoid rebuilding dependencies of a group that are unaffected by the input
203 files. The list may also contain the string "all" to refer to a
204 pseudo-group that contains every root target in the build graph.
205
206 This filtering behavior is also known as "pruning" the list of compile
207 targets.
208
Keir Mierle45611e32019-11-12 11:18:00 -0800209 If input_path is -, input is read from stdin.
210
Brett Wilson796ed472018-07-16 15:11:09 -0700211 output_path is a path indicating where the results of the command are to be
212 written. The results will be a file containing a JSON object with one or more
213 of following fields:
214
215 - "compile_targets": A list of the labels derived from the input
216 compile_targets list that are affected by the input files. Due to the way
217 the filtering works for compile targets as described above, this list may
218 contain targets that do not appear in the input list.
219
220 - "test_targets": A list of the labels from the input test_targets list that
221 are affected by the input files. This list will be a proper subset of the
222 input list.
223
224 - "invalid_targets": A list of any names from the input that do not exist in
225 the build graph. If this list is non-empty, the "error" field will also be
226 set to "Invalid targets".
227
228 - "status": A string containing one of three values:
229
230 - "Found dependency"
231 - "No dependency"
232 - "Found dependency (all) "
233
234 In the first case, the lists returned in compile_targets and test_targets
235 should be passed to ninja to build. In the second case, nothing was
236 affected and no build is necessary. In the third case, GN could not
237 determine the correct answer and returned the input as the output in order
238 to be safe.
239
240 - "error": This will only be present if an error occurred, and will contain
241 a string describing the error. This includes cases where the input file is
242 not in the right format, or contains invalid targets.
243
Keir Mierle45611e32019-11-12 11:18:00 -0800244 If output_path is -, output is written to stdout.
245
Brett Wilson796ed472018-07-16 15:11:09 -0700246 The command returns 1 if it is unable to read the input file or write the
247 output file, or if there is something wrong with the build such that gen
248 would also fail, and 0 otherwise. In particular, it returns 0 even if the
249 "error" key is non-empty and a non-fatal error occurred. In other words, it
250 tries really hard to always write something to the output JSON and convey
251 errors that way rather than via return codes.
252```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800253### **gn args**: (command-line tool)
Brett Wilson796ed472018-07-16 15:11:09 -0700254
255```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800256 Display or configure arguments declared by the build.
257
258 gn args [--list] [--short] [--args] [--overrides-only]
259
Brett Wilson796ed472018-07-16 15:11:09 -0700260 See also "gn help buildargs" for a more high-level overview of how
261 build arguments work.
262```
263
264#### **Usage**
265
266```
267 gn args
268 Open the arguments for the given build directory in an editor. If the
269 given build directory doesn't exist, it will be created and an empty args
270 file will be opened in the editor. You would type something like this
271 into that file:
272 enable_doom_melon=false
273 os="android"
274
275 To find your editor on Posix, GN will search the environment variables in
276 order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command
277 associated with .txt files.
278
279 Note: you can edit the build args manually by editing the file "args.gn"
280 in the build directory and then running "gn gen ".
281
282 gn args --list[=] [--short] [--overrides-only] [--json]
283 Lists all build arguments available in the current configuration, or, if
284 an exact_arg is specified for the list flag, just that one build
285 argument.
286
287 The output will list the declaration location, current value for the
288 build, default value (if different than the current value), and comment
289 preceding the declaration.
290
291 If --short is specified, only the names and current values will be
292 printed.
293
294 If --overrides-only is specified, only the names and current values of
295 arguments that have been overridden (i.e. non-default arguments) will
296 be printed. Overrides come from the /args.gn file and //.gn
297
298 If --json is specified, the output will be emitted in json format.
299 JSON schema for output:
300 [
301 {
302 "name": variable_name,
303 "current": {
304 "value": overridden_value,
305 "file": file_name,
306 "line": line_no
307 },
308 "default": {
309 "value": default_value,
310 "file": file_name,
311 "line": line_no
312 },
313 "comment": comment_string
314 },
315 ...
316 ]
317```
318
319#### **Examples**
320
321```
322 gn args out/Debug
323 Opens an editor with the args for out/Debug.
324
325 gn args out/Debug --list --short
326 Prints all arguments with their default values for the out/Debug
327 build.
328
329 gn args out/Debug --list --short --overrides-only
330 Prints overridden arguments for the out/Debug build.
331
332 gn args out/Debug --list=target_cpu
333 Prints information about the "target_cpu" argument for the "
334 "out/Debug
335 build.
336
337 gn args --list --args="os=\"android\" enable_doom_melon=true"
338 Prints all arguments with the default values for a build with the
339 given arguments set (which may affect the values of other
340 arguments).
341```
Joe Armstrong39413942019-03-15 10:34:03 +0800342### **gn check <out_dir> [<label_pattern>] [\--force] [\--check-generated]**
Brett Wilson796ed472018-07-16 15:11:09 -0700343
344```
345 GN's include header checker validates that the includes for C-like source
346 files match the build dependency graph.
347
348 "gn check" is the same thing as "gn gen" with the "--check" flag except that
349 this command does not write out any build files. It's intended to be an easy
350 way to manually trigger include file checking.
351
352 The can take exact labels or patterns that match more than
353 one (although not general regular expressions). If specified, only those
354 matching targets will be checked. See "gn help label_pattern" for details.
355```
356
357#### **Command-specific switches**
358
359```
Joe Armstrong39413942019-03-15 10:34:03 +0800360 --check-generated
361 Generated files are normally not checked since they do not exist
362 until after a build. With this flag, those generated files that
363 can be found on disk are also checked.
James Robinson49f59032020-01-08 14:05:51 -0800364
365 --check-system
366 Check system style includes (using ) in addition to
367 "double quote" includes.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400368
369 --default-toolchain
370 Normally wildcard targets are matched in all toolchains. This
371 switch makes wildcard labels with no explicit toolchain reference
372 only match targets in the default toolchain.
373
374 Non-wildcard inputs with no explicit toolchain specification will
375 always match only a target in the default toolchain if one exists.
376
377 --force
378 Ignores specifications of "check_includes = false" and checks all
379 target's files that match the target label.
Brett Wilson796ed472018-07-16 15:11:09 -0700380```
381
382#### **What gets checked**
383
384```
Nate Fischer8ed01d32019-01-08 17:32:01 -0800385 The .gn file may specify a list of targets to be checked in the list
386 check_targets (see "gn help dotfile"). If a label pattern is specified
387 on the command line, check_targets is not used.
Brett Wilson796ed472018-07-16 15:11:09 -0700388
389 Targets can opt-out from checking with "check_includes = false" (see
390 "gn help check_includes").
391
392 For targets being checked:
393
394 - GN opens all C-like source files in the targets to be checked and scans
395 the top for includes.
396
Joe Armstrong39413942019-03-15 10:34:03 +0800397 - Generated files (that might not exist yet) are ignored unless
398 the --check-generated flag is provided.
399
Brett Wilson796ed472018-07-16 15:11:09 -0700400 - Includes with a "nogncheck" annotation are skipped (see
401 "gn help nogncheck").
402
James Robinson49f59032020-01-08 14:05:51 -0800403 - Includes using "quotes" are always checked.
404 If system style checking is enabled, includes using
405 are also checked.
Brett Wilson796ed472018-07-16 15:11:09 -0700406
407 - Include paths are assumed to be relative to any of the "include_dirs" for
408 the target (including the implicit current dir).
409
410 - GN does not run the preprocessor so will not understand conditional
411 includes.
412
413 - Only includes matching known files in the build are checked: includes
414 matching unknown paths are ignored.
415
416 For an include to be valid:
417
418 - The included file must be in the current target, or there must be a path
419 following only public dependencies to a target with the file in it
420 ("gn path" is a good way to diagnose problems).
421
422 - There can be multiple targets with an included file: only one needs to be
423 valid for the include to be allowed.
424
425 - If there are only "sources" in a target, all are considered to be public
426 and can be included by other targets with a valid public dependency path.
427
428 - If a target lists files as "public", only those files are able to be
429 included by other targets. Anything in the sources will be considered
430 private and will not be includable regardless of dependency paths.
431
432 - Outputs from actions are treated like public sources on that target.
433
434 - A target can include headers from a target that depends on it if the
435 other target is annotated accordingly. See "gn help
436 allow_circular_includes_from".
437```
438
439#### **Advice on fixing problems**
440
441```
442 If you have a third party project that is difficult to fix or doesn't care
443 about include checks it's generally best to exclude that target from checking
444 altogether via "check_includes = false".
445
446 If you have conditional includes, make sure the build conditions and the
447 preprocessor conditions match, and annotate the line with "nogncheck" (see
448 "gn help nogncheck" for an example).
449
450 If two targets are hopelessly intertwined, use the
451 "allow_circular_includes_from" annotation. Ideally each should have identical
452 dependencies so configs inherited from those dependencies are consistent (see
453 "gn help allow_circular_includes_from").
454
455 If you have a standalone header file or files that need to be shared between
456 a few targets, you can consider making a source_set listing only those
457 headers as public sources. With only header files, the source set will be a
458 no-op from a build perspective, but will give a central place to refer to
459 those headers. That source set's files will still need to pass "gn check" in
460 isolation.
461
462 In rare cases it makes sense to list a header in more than one target if it
463 could be considered conceptually a member of both.
464```
465
466#### **Examples**
467
468```
469 gn check out/Debug
470 Check everything.
471
472 gn check out/Default //foo:bar
473 Check only the files in the //foo:bar target.
474
475 gn check out/Default "//foo/*
476 Check only the files in targets in the //foo directory tree.
477```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400478### **gn clean <out_dir>...**
Brett Wilson796ed472018-07-16 15:11:09 -0700479
480```
481 Deletes the contents of the output directory except for args.gn and
482 creates a Ninja build environment sufficient to regenerate the build.
483```
Joe Armstrongb199e542019-03-21 09:40:08 +0800484### **gn desc**
Brett Wilson796ed472018-07-16 15:11:09 -0700485
486```
Joe Armstrongb199e542019-03-21 09:40:08 +0800487 gn desc
488 [--format=json]
489
Julie Hockette2a29402018-07-31 10:11:42 -0700490 Displays information about a given target or config. The build parameters
491 will be taken for the build in the given .
Brett Wilson796ed472018-07-16 15:11:09 -0700492
493 The
494 pattern (see "gn help label_pattern"). A label pattern will only match
495 targets.
496```
497
Nico Weber75eb3c72018-08-28 16:01:52 -0400498#### **Possibilities for <what to show>**
Brett Wilson796ed472018-07-16 15:11:09 -0700499
500```
501 (If unspecified an overall summary will be displayed.)
502
503 all_dependent_configs
504 allow_circular_includes_from
505 arflags [--blame]
506 args
507 cflags [--blame]
Julie Hockette2a29402018-07-31 10:11:42 -0700508 cflags_c [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700509 cflags_cc [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700510 check_includes
511 configs [--tree] (see below)
Nate Fischer8ed01d32019-01-08 17:32:01 -0800512 data_keys
Brett Wilson796ed472018-07-16 15:11:09 -0700513 defines [--blame]
514 depfile
515 deps [--all] [--tree] (see below)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100516 framework_dirs
517 frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700518 include_dirs [--blame]
519 inputs
520 ldflags [--blame]
521 lib_dirs
522 libs
Nate Fischer8ed01d32019-01-08 17:32:01 -0800523 metadata
524 output_conversion
Brett Wilson796ed472018-07-16 15:11:09 -0700525 outputs
526 public_configs
527 public
Nate Fischer8ed01d32019-01-08 17:32:01 -0800528 rebase
Brett Wilson796ed472018-07-16 15:11:09 -0700529 script
530 sources
531 testonly
532 visibility
Nate Fischer8ed01d32019-01-08 17:32:01 -0800533 walk_keys
Robert Sesekd0a6f072020-05-15 11:21:22 -0400534 weak_frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700535
536 runtime_deps
537 Compute all runtime deps for the given target. This is a computed list
538 and does not correspond to any GN variable, unlike most other values
539 here.
540
541 The output is a list of file names relative to the build directory. See
542 "gn help runtime_deps" for how this is computed. This also works with
543 "--blame" to see the source of the dependency.
544```
545
546#### **Shared flags**
Brett Wilson796ed472018-07-16 15:11:09 -0700547
Robert Sesekd0a6f072020-05-15 11:21:22 -0400548```
549 --default-toolchain
550 Normally wildcard targets are matched in all toolchains. This
551 switch makes wildcard labels with no explicit toolchain reference
552 only match targets in the default toolchain.
553
554 Non-wildcard inputs with no explicit toolchain specification will
555 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -0700556
557 --format=json
558 Format the output as JSON instead of text.
559```
560
561#### **Target flags**
562
563```
564 --blame
565 Used with any value specified on a config, this will name the config that
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100566 causes that target to get the flag. This doesn't currently work for libs,
Robert Sesekd0a6f072020-05-15 11:21:22 -0400567 lib_dirs, frameworks, weak_frameworks and framework_dirs because those are
568 inherited and are more complicated to figure out the blame (patches
569 welcome).
Brett Wilson796ed472018-07-16 15:11:09 -0700570```
571
572#### **Configs**
573
574```
575 The "configs" section will list all configs that apply. For targets this will
576 include configs specified in the "configs" variable of the target, and also
577 configs pushed onto this target via public or "all dependent" configs.
578
579 Configs can have child configs. Specifying --tree will show the hierarchy.
580```
581
582#### **Printing outputs**
583
584```
585 The "outputs" section will list all outputs that apply, including the outputs
586 computed from the tool definition (eg for "executable", "static_library", ...
587 targets).
588```
589
590#### **Printing deps**
591
592```
593 Deps will include all public, private, and data deps (TODO this could be
594 clarified and enhanced) sorted in order applying. The following may be used:
595
596 --all
597 Collects all recursive dependencies and prints a sorted flat list. Also
598 usable with --tree (see below).
Robert Sesekd0a6f072020-05-15 11:21:22 -0400599
Brett Wilson796ed472018-07-16 15:11:09 -0700600 --as=(buildfile|label|output)
601 How to print targets.
602
603 buildfile
604 Prints the build files where the given target was declared as
605 file names.
606 label (default)
607 Prints the label of the target.
608 output
609 Prints the first output file for the target relative to the
610 root build directory.
611
612 --testonly=(true|false)
613 Restrict outputs to targets with the testonly flag set
614 accordingly. When unspecified, the target's testonly flags are
615 ignored.
616
617 --tree
618 Print a dependency tree. By default, duplicates will be elided with "..."
619 but when --all and -tree are used together, no eliding will be performed.
620
621 The "deps", "public_deps", and "data_deps" will all be included in the
622 tree.
623
624 Tree output can not be used with the filtering or output flags: --as,
625 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400626
Brett Wilson796ed472018-07-16 15:11:09 -0700627 --type=(action|copy|executable|group|loadable_module|shared_library|
628 source_set|static_library)
629 Restrict outputs to targets matching the given type. If
630 unspecified, no filtering will be performed.
631```
632
633#### **Note**
634
635```
636 This command will show the full name of directories and source files, but
637 when directories and source paths are written to the build file, they will be
638 adjusted to be relative to the build directory. So the values for paths
639 displayed by this command won't match (but should mean the same thing).
640```
641
642#### **Examples**
643
644```
645 gn desc out/Debug //base:base
646 Summarizes the given target.
647
648 gn desc out/Foo :base_unittests deps --tree
649 Shows a dependency tree of the "base_unittests" project in
650 the current directory.
651
652 gn desc out/Debug //base defines --blame
653 Shows defines set for the //base:base target, annotated by where
654 each one was set from.
655```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800656### **gn format [\--dump-tree] (\--stdin | <list of build_files...>)**
Brett Wilson796ed472018-07-16 15:11:09 -0700657
658```
659 Formats .gn file to a standard format.
660
661 The contents of some lists ('sources', 'deps', etc.) will be sorted to a
662 canonical order. To suppress this, you can add a comment of the form "#
663 NOSORT" immediately preceding the assignment. e.g.
664
665 # NOSORT
666 sources = [
667 "z.cc",
668 "a.cc",
669 ]
670```
671
672#### **Arguments**
673
674```
675 --dry-run
676 Does not change or output anything, but sets the process exit code based
677 on whether output would be different than what's on disk. This is useful
678 for presubmit/lint-type checks.
679 - Exit code 0: successful format, matches on disk.
680 - Exit code 1: general failure (parse error, etc.)
681 - Exit code 2: successful format, but differs from on disk.
682
Julie Hockettd69a9c32019-01-23 14:36:18 -0800683 --dump-tree[=( text | json )]
684 Dumps the parse tree to stdout and does not update the file or print
685 formatted output. If no format is specified, text format will be used.
Brett Wilson796ed472018-07-16 15:11:09 -0700686
687 --stdin
688 Read input from stdin and write to stdout rather than update a file
689 in-place.
690```
691
692#### **Examples**
693```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800694 gn format //some/BUILD.gn //some/other/BUILD.gn //and/another/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700695 gn format some\\BUILD.gn
696 gn format /abspath/some/BUILD.gn
697 gn format --stdin
698```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800699### **gn gen [\--check] [<ide options>] <out_dir>**
Brett Wilson796ed472018-07-16 15:11:09 -0700700
701```
702 Generates ninja files from the current tree and puts them in the given output
703 directory.
704
705 The output directory can be a source-repo-absolute path name such as:
706 //out/foo
707 Or it can be a directory relative to the current directory such as:
708 out/foo
709
James Robinson49f59032020-01-08 14:05:51 -0800710 "gn gen --check" is the same as running "gn check". "gn gen --check=system" is
711 the same as running "gn check --check-system". See "gn help check" for
712 documentation on that mode.
Brett Wilson796ed472018-07-16 15:11:09 -0700713
714 See "gn help switches" for the common command-line switches.
715```
716
717#### **IDE options**
718
719```
Harley Li0a9affb2020-06-03 10:38:42 -0400720 GN optionally generates files for IDE. Files won't be overwritten if their
721 contents don't change. Possibilities for
Brett Wilson796ed472018-07-16 15:11:09 -0700722
723 --ide=
724 Generate files for an IDE. Currently supported values:
725 "eclipse" - Eclipse CDT settings file.
726 "vs" - Visual Studio project/solution files.
Aaron Gablea5bcbd72019-12-30 15:08:52 -0800727 (default Visual Studio version: 2019)
Brett Wilson796ed472018-07-16 15:11:09 -0700728 "vs2013" - Visual Studio 2013 project/solution files.
729 "vs2015" - Visual Studio 2015 project/solution files.
730 "vs2017" - Visual Studio 2017 project/solution files.
Sylvain Defresned46f92f2019-04-03 16:14:29 +0200731 "vs2019" - Visual Studio 2019 project/solution files.
Brett Wilson796ed472018-07-16 15:11:09 -0700732 "xcode" - Xcode workspace/solution files.
733 "qtcreator" - QtCreator project files.
734 "json" - JSON file containing target information
735
736 --filters=
737 Semicolon-separated list of label patterns used to limit the set of
738 generated projects (see "gn help label_pattern"). Only matching targets
739 and their dependencies will be included in the solution. Only used for
740 Visual Studio, Xcode and JSON.
741```
742
743#### **Visual Studio Flags**
744
745```
746 --sln=
747 Override default sln file name ("all"). Solution file is written to the
748 root build directory.
749
750 --no-deps
751 Don't include targets dependencies to the solution. Changes the way how
752 --filters option works. Only directly matching targets are included.
753
754 --winsdk=
755 Use the specified Windows 10 SDK version to generate project files.
756 As an example, "10.0.15063.0" can be specified to use Creators Update SDK
757 instead of the default one.
758
759 --ninja-extra-args=
760 This string is passed without any quoting to the ninja invocation
761 command-line. Can be used to configure ninja flags, like "-j".
762```
763
764#### **Xcode Flags**
765
766```
767 --workspace=
768 Override defaut workspace file name ("all"). The workspace file is
769 written to the root build directory.
770
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100771 --ninja-executable=
772 Can be used to specify the ninja executable to use when building.
773
Brett Wilson796ed472018-07-16 15:11:09 -0700774 --ninja-extra-args=
775 This string is passed without any quoting to the ninja invocation
776 command-line. Can be used to configure ninja flags, like "-j".
777
778 --root-target=
779 Name of the target corresponding to "All" target in Xcode. If unset,
780 "All" invokes ninja without any target and builds everything.
781```
782
783#### **QtCreator Flags**
784
785```
786 --root-target=
787 Name of the root target for which the QtCreator project will be generated
788 to contain files of it and its dependencies. If unset, the whole build
789 graph will be emitted.
790```
791
792#### **Eclipse IDE Support**
793
794```
795 GN DOES NOT generate Eclipse CDT projects. Instead, it generates a settings
796 file which can be imported into an Eclipse CDT project. The XML file contains
797 a list of include paths and defines. Because GN does not generate a full
798 .cproject definition, it is not possible to properly define includes/defines
799 for each file individually. Instead, one set of includes/defines is generated
800 for the entire project. This works fairly well but may still result in a few
801 indexer issues here and there.
802```
803
804#### **Generic JSON Output**
805
806```
807 Dumps target information to a JSON file and optionally invokes a
808 python script on the generated file. See the comments at the beginning
809 of json_project_writer.cc and desc_builder.cc for an overview of the JSON
810 file format.
811
812 --json-file-name=
813 Overrides default file name (project.json) of generated JSON file.
814
815 --json-ide-script=
Harley Li0a9affb2020-06-03 10:38:42 -0400816 Executes python script after the JSON file is generated or updated with
817 new content. Path can be project absolute (//), system absolute (/) or
818 relative, in which case the output directory will be base. Path to
819 generated JSON file will be first argument when invoking script.
Brett Wilson796ed472018-07-16 15:11:09 -0700820
821 --json-ide-script-args=
822 Optional second argument that will passed to executed script.
823```
Julie Hockett11e09912018-07-31 13:15:08 -0700824
825#### **Compilation Database**
826
827```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400828 --export-rust-project
829 Produces a rust-project.json file in the root of the build directory
830 This is used for various tools in the Rust ecosystem allowing for the
831 replay of individual compilations independent of the build system.
832 This is an unstable format and likely to change without warning.
833
Robert Sesek252674d2019-05-10 14:08:52 -0400834 --export-compile-commands[=]
Julie Hockett11e09912018-07-31 13:15:08 -0700835 Produces a compile_commands.json file in the root of the build directory
836 containing an array of “command objects”, where each command object
Robert Sesek252674d2019-05-10 14:08:52 -0400837 specifies one way a translation unit is compiled in the project. If a list
838 of target_name is supplied, only targets that are reachable from the list
839 of target_name will be used for “command objects” generation, otherwise
840 all available targets will be used. This is used for various Clang-based
841 tooling, allowing for the replay of individual compilations independent
842 of the build system.
Julie Hockett11e09912018-07-31 13:15:08 -0700843```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800844### **gn help <anything>**
Brett Wilson796ed472018-07-16 15:11:09 -0700845
846```
847 Yo dawg, I heard you like help on your help so I put help on the help in the
848 help.
849
850 You can also use "all" as the parameter to get all help at once.
851```
852
853#### **Switches**
854
855```
856 --markdown
857 Format output in markdown syntax.
858```
859
860#### **Example**
861
862```
863 gn help --markdown all
864 Dump all help to stdout in markdown format.
865```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400866### **gn ls <out_dir> [<label_pattern>] [\--default-toolchain] [\--as=...]**
Brett Wilson796ed472018-07-16 15:11:09 -0700867```
868 [--type=...] [--testonly=...]
869
870 Lists all targets matching the given pattern for the given build directory.
871 By default, only targets in the default toolchain will be matched unless a
872 toolchain is explicitly supplied.
873
874 If the label pattern is unspecified, list all targets. The label pattern is
875 not a general regular expression (see "gn help label_pattern"). If you need
876 more complex expressions, pipe the result through grep.
877```
878
879#### **Options**
880
881```
882 --as=(buildfile|label|output)
883 How to print targets.
884
885 buildfile
886 Prints the build files where the given target was declared as
887 file names.
888 label (default)
889 Prints the label of the target.
890 output
891 Prints the first output file for the target relative to the
892 root build directory.
893
Robert Sesekd0a6f072020-05-15 11:21:22 -0400894 --default-toolchain
895 Normally wildcard targets are matched in all toolchains. This
896 switch makes wildcard labels with no explicit toolchain reference
897 only match targets in the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -0700898
Robert Sesekd0a6f072020-05-15 11:21:22 -0400899 Non-wildcard inputs with no explicit toolchain specification will
900 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -0700901
902 --testonly=(true|false)
903 Restrict outputs to targets with the testonly flag set
904 accordingly. When unspecified, the target's testonly flags are
905 ignored.
906
907 --type=(action|copy|executable|group|loadable_module|shared_library|
908 source_set|static_library)
909 Restrict outputs to targets matching the given type. If
910 unspecified, no filtering will be performed.
911```
912
913#### **Examples**
914
915```
916 gn ls out/Debug
917 Lists all targets in the default toolchain.
918
919 gn ls out/Debug "//base/*"
920 Lists all targets in the directory base and all subdirectories.
921
922 gn ls out/Debug "//base:*"
923 Lists all targets defined in //base/BUILD.gn.
924
925 gn ls out/Debug //base --as=output
926 Lists the build output file for //base:base
927
928 gn ls out/Debug --type=executable
929 Lists all executables produced by the build.
930
931 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
932 Builds all targets in //base and all subdirectories.
Brett Wilson796ed472018-07-16 15:11:09 -0700933```
Joe Armstrongb199e542019-03-21 09:40:08 +0800934### **gn meta**
935
Nate Fischer8ed01d32019-01-08 17:32:01 -0800936```
Joe Armstrongb199e542019-03-21 09:40:08 +0800937 gn meta * --data=[,*]* [--walk=[,*]*]
938 [--rebase=]
Nate Fischer8ed01d32019-01-08 17:32:01 -0800939
940 Lists collected metaresults of all given targets for the given data key(s),
941 collecting metadata dependencies as specified by the given walk key(s).
942
943 See `gn help generated_file` for more information on the walk.
944```
945
946#### **Arguments**
947
948```
949
950 A list of target labels from which to initiate the walk.
951
952 --data
953 A list of keys from which to extract data. In each target walked, its metadata
954 scope is checked for the presence of these keys. If present, the contents of
955 those variable in the scope are appended to the results list.
956
957 --walk (optional)
958 A list of keys from which to control the walk. In each target walked, its
959 metadata scope is checked for the presence of any of these keys. If present,
960 the contents of those variables is checked to ensure that it is a label of
961 a valid dependency of the target and then added to the set of targets to walk.
962 If the empty string ("") is present in any of these keys, all deps and data_deps
963 are added to the walk set.
964
965 --rebase (optional)
966 A destination directory onto which to rebase any paths found. If set, all
967 collected metadata will be rebased onto this path. This option will throw errors
968 if collected metadata is not a list of strings.
969```
970
971#### **Examples**
972
973```
974 gn meta out/Debug "//base/foo" --data=files
975 Lists collected metaresults for the `files` key in the //base/foo:foo
976 target and all of its dependency tree.
977
978 gn meta out/Debug "//base/foo" --data=files --data=other
979 Lists collected metaresults for the `files` and `other` keys in the
980 //base/foo:foo target and all of its dependency tree.
981
982 gn meta out/Debug "//base/foo" --data=files --walk=stop
983 Lists collected metaresults for the `files` key in the //base/foo:foo
984 target and all of the dependencies listed in the `stop` key (and so on).
985
986 gn meta out/Debug "//base/foo" --data=files --rebase="/"
987 Lists collected metaresults for the `files` key in the //base/foo:foo
988 target and all of its dependency tree, rebasing the strings in the `files`
989 key onto the source directory of the target's declaration relative to "/".
990```
Brett Wilson74397092020-03-18 16:34:14 -0700991### **gn outputs <out_dir> <list of target or file names...> ***
992
993```
994 Lists the output files corresponding to the given target(s) or file name(s).
995 There can be multiple outputs because there can be more than one output
996 generated by a build step, and there can be more than one toolchain matched.
997 You can also list multiple inputs which will generate a union of all the
998 outputs from those inputs.
999
1000 - The input target/file names are relative to the current directory.
1001
1002 - The output file names are relative to the root build directory.
1003
1004 This command is useful for finding a ninja command that will build only a
1005 portion of the build.
1006```
1007
1008#### **Target outputs**
1009
1010```
1011 If the parameter is a target name that includes a toolchain, it will match
1012 only that target in that toolchain. If no toolchain is specified, it will
1013 match all targets with that name in any toolchain.
1014
1015 The result will be the outputs specified by that target which could be a
1016 library, executable, output of an action, a stamp file, etc.
1017```
1018
1019#### **File outputs**
1020
1021```
1022 If the parameter is a file name it will compute the output for that compile
1023 step for all targets in all toolchains that contain that file as a source
1024 file.
1025
1026 If the source is not compiled (e.g. a header or text file), the command will
1027 produce no output.
1028
1029 If the source is listed as an "input" to a binary target or action will
1030 resolve to that target's outputs.
1031```
1032
1033#### **Example**
1034
1035```
1036 gn outputs out/debug some/directory:some_target
1037 Find the outputs of a given target.
1038
1039 gn outputs out/debug src/project/my_file.cc | xargs ninja -C out/debug
1040 Compiles just the given source file in all toolchains it's referenced in.
1041
1042 git diff --name-only | xargs gn outputs out/x64 | xargs ninja -C out/x64
1043 Compiles all files changed in git.
1044```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001045### **gn path <out_dir> <target_one> <target_two>**
Brett Wilson796ed472018-07-16 15:11:09 -07001046
1047```
1048 Finds paths of dependencies between two targets. Each unique path will be
1049 printed in one group, and groups will be separate by newlines. The two
1050 targets can appear in either order (paths will be found going in either
1051 direction).
1052
1053 By default, a single path will be printed. If there is a path with only
1054 public dependencies, the shortest public path will be printed. Otherwise, the
1055 shortest path using either public or private dependencies will be printed. If
1056 --with-data is specified, data deps will also be considered. If there are
1057 multiple shortest paths, an arbitrary one will be selected.
1058```
1059
1060#### **Interesting paths**
1061
1062```
1063 In a large project, there can be 100's of millions of unique paths between a
1064 very high level and a common low-level target. To make the output more useful
1065 (and terminate in a reasonable time), GN will not revisit sub-paths
1066 previously known to lead to the target.
1067```
1068
1069#### **Options**
1070
1071```
1072 --all
1073 Prints all "interesting" paths found rather than just the first one.
1074 Public paths will be printed first in order of increasing length, followed
1075 by non-public paths in order of increasing length.
1076
1077 --public
1078 Considers only public paths. Can't be used with --with-data.
1079
1080 --with-data
1081 Additionally follows data deps. Without this flag, only public and private
1082 linked deps will be followed. Can't be used with --public.
1083```
1084
1085#### **Example**
1086
1087```
Keir Mierle45611e32019-11-12 11:18:00 -08001088 gn path out/Default //base //gn
Brett Wilson796ed472018-07-16 15:11:09 -07001089```
Joe Armstrongb199e542019-03-21 09:40:08 +08001090### **gn refs**
1091
Brett Wilson796ed472018-07-16 15:11:09 -07001092```
Joe Armstrongb199e542019-03-21 09:40:08 +08001093 gn refs (|
Robert Sesekd0a6f072020-05-15 11:21:22 -04001094 [--all] [--default-toolchain] [--as=...] [--testonly=...] [--type=...]
Brett Wilson796ed472018-07-16 15:11:09 -07001095
1096 Finds reverse dependencies (which targets reference something). The input is
1097 a list containing:
1098
1099 - Target label: The result will be which targets depend on it.
1100
1101 - Config label: The result will be which targets list the given config in
1102 its "configs" or "public_configs" list.
1103
1104 - Label pattern: The result will be which targets depend on any target
1105 matching the given pattern. Patterns will not match configs. These are not
1106 general regular expressions, see "gn help label_pattern" for details.
1107
1108 - File name: The result will be which targets list the given file in its
1109 "inputs", "sources", "public", "data", or "outputs". Any input that does
1110 not contain wildcards and does not match a target or a config will be
1111 treated as a file.
1112
1113 - Response file: If the input starts with an "@", it will be interpreted as
1114 a path to a file containing a list of labels or file names, one per line.
1115 This allows us to handle long lists of inputs without worrying about
1116 command line limits.
1117```
1118
1119#### **Options**
1120
1121```
1122 --all
1123 When used without --tree, will recurse and display all unique
1124 dependencies of the given targets. For example, if the input is a target,
1125 this will output all targets that depend directly or indirectly on the
1126 input. If the input is a file, this will output all targets that depend
1127 directly or indirectly on that file.
1128
1129 When used with --tree, turns off eliding to show a complete tree.
Brett Wilson796ed472018-07-16 15:11:09 -07001130
1131 --as=(buildfile|label|output)
1132 How to print targets.
1133
1134 buildfile
1135 Prints the build files where the given target was declared as
1136 file names.
1137 label (default)
1138 Prints the label of the target.
1139 output
1140 Prints the first output file for the target relative to the
1141 root build directory.
1142
Robert Sesekd0a6f072020-05-15 11:21:22 -04001143 --default-toolchain
1144 Normally wildcard targets are matched in all toolchains. This
1145 switch makes wildcard labels with no explicit toolchain reference
1146 only match targets in the default toolchain.
1147
1148 Non-wildcard inputs with no explicit toolchain specification will
1149 always match only a target in the default toolchain if one exists.
1150
Brett Wilson796ed472018-07-16 15:11:09 -07001151 -q
1152 Quiet. If nothing matches, don't print any output. Without this option, if
1153 there are no matches there will be an informational message printed which
1154 might interfere with scripts processing the output.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001155
Brett Wilson796ed472018-07-16 15:11:09 -07001156 --testonly=(true|false)
1157 Restrict outputs to targets with the testonly flag set
1158 accordingly. When unspecified, the target's testonly flags are
1159 ignored.
1160
1161 --tree
1162 Outputs a reverse dependency tree from the given target. Duplicates will
1163 be elided. Combine with --all to see a full dependency tree.
1164
1165 Tree output can not be used with the filtering or output flags: --as,
1166 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001167
Brett Wilson796ed472018-07-16 15:11:09 -07001168 --type=(action|copy|executable|group|loadable_module|shared_library|
1169 source_set|static_library)
1170 Restrict outputs to targets matching the given type. If
1171 unspecified, no filtering will be performed.
1172```
1173
1174#### **Examples (target input)**
1175
1176```
Keir Mierle45611e32019-11-12 11:18:00 -08001177 gn refs out/Debug //gn:gn
Brett Wilson796ed472018-07-16 15:11:09 -07001178 Find all targets depending on the given exact target name.
1179
1180 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
1181 Edit all .gn files containing references to //base:i18n
1182
1183 gn refs out/Debug //base --all
1184 List all targets depending directly or indirectly on //base:base.
1185
1186 gn refs out/Debug "//base/*"
1187 List all targets depending directly on any target in //base or
1188 its subdirectories.
1189
1190 gn refs out/Debug "//base:*"
1191 List all targets depending directly on any target in
1192 //base/BUILD.gn.
1193
1194 gn refs out/Debug //base --tree
1195 Print a reverse dependency tree of //base:base
1196```
1197
1198#### **Examples (file input)**
1199
1200```
1201 gn refs out/Debug //base/macros.h
1202 Print target(s) listing //base/macros.h as a source.
1203
1204 gn refs out/Debug //base/macros.h --tree
1205 Display a reverse dependency tree to get to the given file. This
1206 will show how dependencies will reference that file.
1207
1208 gn refs out/Debug //base/macros.h //base/at_exit.h --all
1209 Display all unique targets with some dependency path to a target
1210 containing either of the given files as a source.
1211
1212 gn refs out/Debug //base/macros.h --testonly=true --type=executable
1213 --all --as=output
1214 Display the executable file names of all test executables
1215 potentially affected by a change to the given file.
1216```
1217## Target declarations
1218
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001219### **action**: Declare a target that runs a script a single time.
Brett Wilson796ed472018-07-16 15:11:09 -07001220
1221```
1222 This target type allows you to run a script a single time to produce one or
1223 more output files. If you want to run a script once for each of a set of
1224 input files, see "gn help action_foreach".
1225```
1226
1227#### **Inputs**
1228
1229```
1230 In an action the "sources" and "inputs" are treated the same: they're both
1231 input dependencies on script execution with no special handling. If you want
1232 to pass the sources to your script, you must do so explicitly by including
1233 them in the "args". Note also that this means there is no special handling of
1234 paths since GN doesn't know which of the args are paths and not. You will
1235 want to use rebase_path() to convert paths to be relative to the
1236 root_build_dir.
1237
1238 You can dynamically write input dependencies (for incremental rebuilds if an
1239 input file changes) by writing a depfile when the script is run (see "gn help
1240 depfile"). This is more flexible than "inputs".
1241
1242 If the command line length is very long, you can use response files to pass
1243 args to your script. See "gn help response_file_contents".
1244
1245 It is recommended you put inputs to your script in the "sources" variable,
1246 and stuff like other Python files required to run your script in the "inputs"
1247 variable.
Julie Hockette2a29402018-07-31 10:11:42 -07001248
Brett Wilson796ed472018-07-16 15:11:09 -07001249 The "deps" and "public_deps" for an action will always be
1250 completed before any part of the action is run so it can depend on
1251 the output of previous steps. The "data_deps" will be built if the
1252 action is built, but may not have completed before all steps of the
1253 action are started. This can give additional parallelism in the build
1254 for runtime-only dependencies.
1255```
1256
1257#### **Outputs**
1258
1259```
1260 You should specify files created by your script by specifying them in the
1261 "outputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001262
Brett Wilson796ed472018-07-16 15:11:09 -07001263 The script will be executed with the given arguments with the current
1264 directory being that of the root build directory. If you pass files
1265 to your script, see "gn help rebase_path" for how to convert
1266 file names to be relative to the build directory (file names in the
1267 sources, outputs, and inputs will be all treated as relative to the
1268 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001269
1270 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1271 that Ninja will check the timestamp of the output after the action
1272 completes. If output timestamp is unchanged, the step will be treated
1273 as if it never needed to be rebuilt, potentially eliminating some
1274 downstream steps for incremental builds. Scripts can improve build
1275 performance by taking care not to change the timstamp of the output
1276 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001277```
1278
1279#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001280
Brett Wilson796ed472018-07-16 15:11:09 -07001281```
1282 All output files must be inside the output directory of the build.
1283 You would generally use |$target_out_dir| or |$target_gen_dir| to
1284 reference the output or generated intermediate file directories,
1285 respectively.
1286```
1287
1288#### **Variables**
1289
1290```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001291 args, data, data_deps, depfile, deps, inputs, metadata, outputs*, pool,
Brett Wilson796ed472018-07-16 15:11:09 -07001292 response_file_contents, script*, sources
1293 * = required
1294```
1295
1296#### **Example**
1297
1298```
1299 action("run_this_guy_once") {
1300 script = "doprocessing.py"
1301 sources = [ "my_configuration.txt" ]
1302 outputs = [ "$target_gen_dir/insightful_output.txt" ]
1303
1304 # Our script imports this Python file so we want to rebuild if it changes.
1305 inputs = [ "helper_library.py" ]
1306
1307 # Note that we have to manually pass the sources to our script if the
1308 # script needs them as inputs.
1309 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
1310 rebase_path(sources, root_build_dir)
1311 }
1312```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001313### **action_foreach**: Declare a target that runs a script over a set of files.
Brett Wilson796ed472018-07-16 15:11:09 -07001314
1315```
1316 This target type allows you to run a script once-per-file over a set of
1317 sources. If you want to run a script once that takes many files as input, see
1318 "gn help action".
1319```
1320
1321#### **Inputs**
1322
1323```
1324 The script will be run once per file in the "sources" variable. The "outputs"
1325 variable should specify one or more files with a source expansion pattern in
1326 it (see "gn help source_expansion"). The output file(s) for each script
1327 invocation should be unique. Normally you use "{{source_name_part}}" in each
1328 output file.
1329
1330 If your script takes additional data as input, such as a shared configuration
1331 file or a Python module it uses, those files should be listed in the "inputs"
1332 variable. These files are treated as dependencies of each script invocation.
1333
1334 If the command line length is very long, you can use response files to pass
1335 args to your script. See "gn help response_file_contents".
1336
1337 You can dynamically write input dependencies (for incremental rebuilds if an
1338 input file changes) by writing a depfile when the script is run (see "gn help
1339 depfile"). This is more flexible than "inputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001340
Brett Wilson796ed472018-07-16 15:11:09 -07001341 The "deps" and "public_deps" for an action will always be
1342 completed before any part of the action is run so it can depend on
1343 the output of previous steps. The "data_deps" will be built if the
1344 action is built, but may not have completed before all steps of the
1345 action are started. This can give additional parallelism in the build
1346 for runtime-only dependencies.
1347```
1348
1349#### **Outputs**
Julie Hockette2a29402018-07-31 10:11:42 -07001350
Brett Wilson796ed472018-07-16 15:11:09 -07001351```
1352 The script will be executed with the given arguments with the current
1353 directory being that of the root build directory. If you pass files
1354 to your script, see "gn help rebase_path" for how to convert
1355 file names to be relative to the build directory (file names in the
1356 sources, outputs, and inputs will be all treated as relative to the
1357 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001358
1359 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1360 that Ninja will check the timestamp of the output after the action
1361 completes. If output timestamp is unchanged, the step will be treated
1362 as if it never needed to be rebuilt, potentially eliminating some
1363 downstream steps for incremental builds. Scripts can improve build
1364 performance by taking care not to change the timstamp of the output
1365 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001366```
1367
1368#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001369
Brett Wilson796ed472018-07-16 15:11:09 -07001370```
1371 All output files must be inside the output directory of the build.
1372 You would generally use |$target_out_dir| or |$target_gen_dir| to
1373 reference the output or generated intermediate file directories,
1374 respectively.
1375```
1376
1377#### **Variables**
1378
1379```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001380 args, data, data_deps, depfile, deps, inputs, metadata, outputs*, pool,
Brett Wilson796ed472018-07-16 15:11:09 -07001381 response_file_contents, script*, sources*
1382 * = required
1383```
1384
1385#### **Example**
1386
1387```
1388 # Runs the script over each IDL file. The IDL script will generate both a .cc
1389 # and a .h file for each input.
1390 action_foreach("my_idl") {
1391 script = "idl_processor.py"
1392 sources = [ "foo.idl", "bar.idl" ]
1393
Joe Armstrong73e89072019-05-10 11:23:52 +08001394 # Our script reads this file each time, so we need to list it as a
Brett Wilson796ed472018-07-16 15:11:09 -07001395 # dependency so we can rebuild if it changes.
1396 inputs = [ "my_configuration.txt" ]
1397
1398 # Transformation from source file name to output file names.
1399 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
1400 "$target_gen_dir/{{source_name_part}}.cc" ]
1401
1402 # Note that since "args" is opaque to GN, if you specify paths here, you
1403 # will need to convert it to be relative to the build directory using
1404 # rebase_path().
1405 args = [
1406 "{{source}}",
1407 "-o",
1408 rebase_path(relative_target_gen_dir, root_build_dir) +
1409 "/{{source_name_part}}.h" ]
1410 }
1411```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001412### **bundle_data**: [iOS/macOS] Declare a target without output.
Brett Wilson796ed472018-07-16 15:11:09 -07001413
1414```
1415 This target type allows to declare data that is required at runtime. It is
1416 used to inform "create_bundle" targets of the files to copy into generated
1417 bundle, see "gn help create_bundle" for help.
1418
1419 The target must define a list of files as "sources" and a single "outputs".
1420 If there are multiple files, source expansions must be used to express the
1421 output. The output must reference a file inside of {{bundle_root_dir}}.
1422
1423 This target can be used on all platforms though it is designed only to
1424 generate iOS/macOS bundle. In cross-platform projects, it is advised to put it
1425 behind iOS/macOS conditionals.
1426
1427 See "gn help create_bundle" for more information.
1428```
1429
1430#### **Variables**
1431
1432```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001433 sources*, outputs*, deps, data_deps, metadata, public_deps, visibility
Brett Wilson796ed472018-07-16 15:11:09 -07001434 * = required
1435```
1436
1437#### **Examples**
1438
1439```
1440 bundle_data("icudata") {
1441 sources = [ "sources/data/in/icudtl.dat" ]
1442 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
1443 }
1444
1445 bundle_data("base_unittests_bundle_data]") {
1446 sources = [ "test/data" ]
1447 outputs = [
1448 "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" +
1449 "{{source_file_part}}"
1450 ]
1451 }
1452
1453 bundle_data("material_typography_bundle_data") {
1454 sources = [
1455 "src/MaterialTypography.bundle/Roboto-Bold.ttf",
1456 "src/MaterialTypography.bundle/Roboto-Italic.ttf",
1457 "src/MaterialTypography.bundle/Roboto-Regular.ttf",
1458 "src/MaterialTypography.bundle/Roboto-Thin.ttf",
1459 ]
1460 outputs = [
1461 "{{bundle_resources_dir}}/MaterialTypography.bundle/"
1462 "{{source_file_part}}"
1463 ]
1464 }
1465```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001466### **copy**: Declare a target that copies files.
Brett Wilson796ed472018-07-16 15:11:09 -07001467
1468#### **File name handling**
1469
1470```
1471 All output files must be inside the output directory of the build. You would
1472 generally use |$target_out_dir| or |$target_gen_dir| to reference the output
1473 or generated intermediate file directories, respectively.
1474
1475 Both "sources" and "outputs" must be specified. Sources can include as many
1476 files as you want, but there can only be one item in the outputs list (plural
1477 is used for the name for consistency with other target types).
1478
1479 If there is more than one source file, your output name should specify a
1480 mapping from each source file to an output file name using source expansion
1481 (see "gn help source_expansion"). The placeholders will look like
1482 "{{source_name_part}}", for example.
1483```
1484
1485#### **Examples**
1486
1487```
1488 # Write a rule that copies a checked-in DLL to the output directory.
1489 copy("mydll") {
1490 sources = [ "mydll.dll" ]
1491 outputs = [ "$target_out_dir/mydll.dll" ]
1492 }
1493
1494 # Write a rule to copy several files to the target generated files directory.
1495 copy("myfiles") {
1496 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1497
1498 # Use source expansion to generate output files with the corresponding file
1499 # names in the gen dir. This will just copy each file.
1500 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1501 }
1502```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001503### **create_bundle**: [ios/macOS] Build an iOS or macOS bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07001504
1505```
1506 This target generates an iOS or macOS bundle (which is a directory with a
1507 well-know structure). This target does not define any sources, instead they
1508 are computed from all "bundle_data" target this one depends on transitively
1509 (the recursion stops at "create_bundle" targets).
1510
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001511 The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in
1512 "bundle_data" outputs. The properties are optional but must be defined if any
1513 of the "bundle_data" target use them.
Brett Wilson796ed472018-07-16 15:11:09 -07001514
1515 This target can be used on all platforms though it is designed only to
1516 generate iOS or macOS bundle. In cross-platform projects, it is advised to put
1517 it behind iOS/macOS conditionals.
1518
1519 If a create_bundle is specified as a data_deps for another target, the bundle
1520 is considered a leaf, and its public and private dependencies will not
1521 contribute to any data or data_deps. Required runtime dependencies should be
1522 placed in the bundle. A create_bundle can declare its own explicit data and
1523 data_deps, however.
1524```
1525
1526#### **Code signing**
1527
1528```
1529 Some bundle needs to be code signed as part of the build (on iOS all
1530 application needs to be code signed to run on a device). The code signature
1531 can be configured via the code_signing_script variable.
1532
1533 If set, code_signing_script is the path of a script that invoked after all
1534 files have been moved into the bundle. The script must not change any file in
1535 the bundle, but may add new files.
1536
1537 If code_signing_script is defined, then code_signing_outputs must also be
1538 defined and non-empty to inform when the script needs to be re-run. The
1539 code_signing_args will be passed as is to the script (so path have to be
1540 rebased) and additional inputs may be listed with the variable
1541 code_signing_sources.
1542```
1543
1544#### **Variables**
1545
1546```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001547 bundle_root_dir, bundle_contents_dir, bundle_resources_dir,
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001548 bundle_executable_dir, bundle_deps_filter, deps, data_deps, public_deps,
1549 visibility, product_type, code_signing_args, code_signing_script,
1550 code_signing_sources, code_signing_outputs, xcode_extra_attributes,
1551 xcode_test_application_name, partial_info_plist, metadata
Brett Wilson796ed472018-07-16 15:11:09 -07001552```
1553
1554#### **Example**
1555
1556```
1557 # Defines a template to create an application. On most platform, this is just
1558 # an alias for an "executable" target, but on iOS/macOS, it builds an
1559 # application bundle.
1560 template("app") {
1561 if (!is_ios && !is_mac) {
1562 executable(target_name) {
1563 forward_variables_from(invoker, "*")
1564 }
1565 } else {
1566 app_name = target_name
1567 gen_path = target_gen_dir
1568
1569 action("${app_name}_generate_info_plist") {
1570 script = [ "//build/ios/ios_gen_plist.py" ]
1571 sources = [ "templates/Info.plist" ]
1572 outputs = [ "$gen_path/Info.plist" ]
1573 args = rebase_path(sources, root_build_dir) +
1574 rebase_path(outputs, root_build_dir)
1575 }
1576
1577 bundle_data("${app_name}_bundle_info_plist") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001578 public_deps = [ ":${app_name}_generate_info_plist" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001579 sources = [ "$gen_path/Info.plist" ]
1580 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
1581 }
1582
1583 executable("${app_name}_generate_executable") {
1584 forward_variables_from(invoker, "*", [
1585 "output_name",
1586 "visibility",
1587 ])
1588 output_name =
1589 rebase_path("$gen_path/$app_name", root_build_dir)
1590 }
1591
1592 code_signing =
1593 defined(invoker.code_signing) && invoker.code_signing
1594
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001595 if (!is_ios || !code_signing) {
Brett Wilson796ed472018-07-16 15:11:09 -07001596 bundle_data("${app_name}_bundle_executable") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001597 public_deps = [ ":${app_name}_generate_executable" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001598 sources = [ "$gen_path/$app_name" ]
1599 outputs = [ "{{bundle_executable_dir}}/$app_name" ]
1600 }
1601 }
1602
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001603 create_bundle("$app_name.app") {
Brett Wilson796ed472018-07-16 15:11:09 -07001604 product_type = "com.apple.product-type.application"
1605
1606 if (is_ios) {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001607 bundle_root_dir = "$root_build_dir/$target_name"
Brett Wilson796ed472018-07-16 15:11:09 -07001608 bundle_contents_dir = bundle_root_dir
1609 bundle_resources_dir = bundle_contents_dir
1610 bundle_executable_dir = bundle_contents_dir
Brett Wilson796ed472018-07-16 15:11:09 -07001611
1612 extra_attributes = {
1613 ONLY_ACTIVE_ARCH = "YES"
1614 DEBUG_INFORMATION_FORMAT = "dwarf"
1615 }
1616 } else {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001617 bundle_root_dir = "$root_build_dir/$target_name"
1618 bundle_contents_dir = "$bundle_root_dir/Contents"
1619 bundle_resources_dir = "$bundle_contents_dir/Resources"
1620 bundle_executable_dir = "$bundle_contents_dir/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07001621 }
1622 deps = [ ":${app_name}_bundle_info_plist" ]
1623 if (is_ios && code_signing) {
1624 deps += [ ":${app_name}_generate_executable" ]
1625 code_signing_script = "//build/config/ios/codesign.py"
1626 code_signing_sources = [
1627 invoker.entitlements_path,
1628 "$target_gen_dir/$app_name",
1629 ]
1630 code_signing_outputs = [
1631 "$bundle_root_dir/$app_name",
1632 "$bundle_root_dir/_CodeSignature/CodeResources",
1633 "$bundle_root_dir/embedded.mobileprovision",
1634 "$target_gen_dir/$app_name.xcent",
1635 ]
1636 code_signing_args = [
1637 "-i=" + ios_code_signing_identity,
1638 "-b=" + rebase_path(
1639 "$target_gen_dir/$app_name", root_build_dir),
1640 "-e=" + rebase_path(
1641 invoker.entitlements_path, root_build_dir),
1642 "-e=" + rebase_path(
1643 "$target_gen_dir/$app_name.xcent", root_build_dir),
1644 rebase_path(bundle_root_dir, root_build_dir),
1645 ]
1646 } else {
1647 deps += [ ":${app_name}_bundle_executable" ]
1648 }
1649 }
1650 }
1651 }
1652```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001653### **executable**: Declare an executable target.
Brett Wilson796ed472018-07-16 15:11:09 -07001654
Julie Hockettce1fa072019-05-07 17:44:37 -07001655#### **Language and compilation**
1656
1657```
1658 The tools and commands used to create this target type will be
1659 determined by the source files in its sources. Targets containing
1660 multiple compiler-incompatible languages are not allowed (e.g. a
1661 target containing both C and C++ sources is acceptable, but a
1662 target containing C and Rust sources is not).
1663```
1664
Brett Wilson796ed472018-07-16 15:11:09 -07001665#### **Variables**
1666
1667```
1668 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1669 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001670 libs, precompiled_header, precompiled_source, rustflags,
1671 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07001672 Deps: data_deps, deps, public_deps
1673 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001674 General: check_includes, configs, data, friend, inputs, metadata,
1675 output_name, output_extension, public, sources, testonly,
1676 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001677 Rust variables: aliased_deps, crate_root, crate_name
Nate Fischer8ed01d32019-01-08 17:32:01 -08001678```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001679### **generated_file**: Declare a generated_file target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001680
1681```
1682 Writes data value(s) to disk on resolution. This target type mirrors some
1683 functionality of the write_file() function, but also provides the ability to
1684 collect metadata from its dependencies on resolution rather than writing out
Keir Mierle45611e32019-11-12 11:18:00 -08001685 at parse time.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001686
1687 The `outputs` variable is required to be a list with a single element,
1688 specifying the intended location of the output file.
1689
1690 The `output_conversion` variable specified the format to write the
1691 value. See `gn help output_conversion`.
1692
1693 One of `contents` or `data_keys` must be specified; use of `data` will write
1694 the contents of that value to file, while use of `data_keys` will trigger a
1695 metadata collection walk based on the dependencies of the target and the
1696 optional values of the `rebase` and `walk_keys` variables. See
1697 `gn help metadata`.
1698
1699 Collected metadata, if specified, will be returned in postorder of
1700 dependencies. See the example for details.
1701```
1702
1703#### **Example (metadata collection)**
1704
1705```
1706 Given the following targets defined in //base/BUILD.gn, where A depends on B
1707 and B depends on C and D:
1708
1709 group("a") {
1710 metadata = {
1711 doom_melon = [ "enable" ]
1712 my_files = [ "foo.cpp" ]
1713
Keir Mierle45611e32019-11-12 11:18:00 -08001714 # Note: this is functionally equivalent to not defining `my_barrier`
1715 # at all in this target's metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001716 my_barrier = [ "" ]
1717 }
1718
1719 deps = [ ":b" ]
1720 }
1721
Julie Hockettd69a9c32019-01-23 14:36:18 -08001722 group("b") {
Nate Fischer8ed01d32019-01-08 17:32:01 -08001723 metadata = {
1724 my_files = [ "bar.cpp" ]
1725 my_barrier = [ ":c" ]
1726 }
1727
1728 deps = [ ":c", ":d" ]
1729 }
1730
1731 group("c") {
1732 metadata = {
1733 doom_melon = [ "disable" ]
1734 my_files = [ "baz.cpp" ]
1735 }
1736 }
1737
1738 group("d") {
1739 metadata = {
1740 my_files = [ "missing.cpp" ]
1741 }
1742 }
1743
1744 If the following generated_file target is defined:
1745
1746 generated_file("my_files_metadata") {
1747 outputs = [ "$root_build_dir/my_files.json" ]
1748 data_keys = [ "my_files" ]
1749
1750 deps = [ "//base:a" ]
1751 }
1752
1753 The following will be written to "$root_build_dir/my_files.json" (less the
1754 comments):
1755 [
1756 "baz.cpp", // from //base:c via //base:b
1757 "missing.cpp" // from //base:d via //base:b
1758 "bar.cpp", // from //base:b via //base:a
1759 "foo.cpp", // from //base:a
1760 ]
1761
1762 Alternatively, as an example of using walk_keys, if the following
1763 generated_file target is defined:
1764
1765 generated_file("my_files_metadata") {
1766 outputs = [ "$root_build_dir/my_files.json" ]
1767 data_keys = [ "my_files" ]
1768 walk_keys = [ "my_barrier" ]
1769
1770 deps = [ "//base:a" ]
1771 }
1772
1773 The following will be written to "$root_build_dir/my_files.json" (again less
1774 the comments):
1775 [
1776 "baz.cpp", // from //base:c via //base:b
1777 "bar.cpp", // from //base:b via //base:a
1778 "foo.cpp", // from //base:a
1779 ]
1780
1781 If `rebase` is used in the following generated_file target:
1782
1783 generated_file("my_files_metadata") {
1784 outputs = [ "$root_build_dir/my_files.json" ]
1785 data_keys = [ "my_files" ]
1786 walk_keys = [ "my_barrier" ]
1787 rebase = root_build_dir
1788
1789 deps = [ "//base:a" ]
1790 }
1791
1792 The following will be written to "$root_build_dir/my_files.json" (again less
1793 the comments) (assuming root_build_dir = "//out"):
1794 [
1795 "../base/baz.cpp", // from //base:c via //base:b
1796 "../base/bar.cpp", // from //base:b via //base:a
1797 "../base/foo.cpp", // from //base:a
1798 ]
1799```
1800
1801#### **Variables**
1802
1803```
Julie Hockettd69a9c32019-01-23 14:36:18 -08001804 contents
Nate Fischer8ed01d32019-01-08 17:32:01 -08001805 data_keys
1806 rebase
1807 walk_keys
1808 output_conversion
1809 Deps: data_deps, deps, public_deps
1810 Dependent configs: all_dependent_configs, public_configs
Brett Wilson796ed472018-07-16 15:11:09 -07001811```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001812### **group**: Declare a named group of targets.
Brett Wilson796ed472018-07-16 15:11:09 -07001813
1814```
1815 This target type allows you to create meta-targets that just collect a set of
1816 dependencies into one named target. Groups can additionally specify configs
1817 that apply to their dependents.
1818```
1819
1820#### **Variables**
1821
1822```
1823 Deps: data_deps, deps, public_deps
1824 Dependent configs: all_dependent_configs, public_configs
1825```
1826
1827#### **Example**
1828
1829```
1830 group("all") {
1831 deps = [
1832 "//project:runner",
1833 "//project:unit_tests",
1834 ]
1835 }
1836```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001837### **loadable_module**: Declare a loadable module target.
Brett Wilson796ed472018-07-16 15:11:09 -07001838
1839```
1840 This target type allows you to create an object file that is (and can only
1841 be) loaded and unloaded at runtime.
1842
1843 A loadable module will be specified on the linker line for targets listing
1844 the loadable module in its "deps". If you don't want this (if you don't need
1845 to dynamically load the library at runtime), then you should use a
1846 "shared_library" target type instead.
1847```
1848
Julie Hockettce1fa072019-05-07 17:44:37 -07001849#### **Language and compilation**
1850
1851```
1852 The tools and commands used to create this target type will be
1853 determined by the source files in its sources. Targets containing
1854 multiple compiler-incompatible languages are not allowed (e.g. a
1855 target containing both C and C++ sources is acceptable, but a
1856 target containing C and Rust sources is not).
1857```
1858
Brett Wilson796ed472018-07-16 15:11:09 -07001859#### **Variables**
1860
1861```
1862 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1863 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001864 libs, precompiled_header, precompiled_source, rustflags,
1865 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07001866 Deps: data_deps, deps, public_deps
1867 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001868 General: check_includes, configs, data, friend, inputs, metadata,
1869 output_name, output_extension, public, sources, testonly,
1870 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001871 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Julie Hockettce1fa072019-05-07 17:44:37 -07001872```
1873### **rust_library**: Declare a Rust library target.
1874
1875```
1876 A Rust library is an archive containing additional rust-c provided metadata.
1877 These are the files produced by the rustc compiler with the `.rlib`
1878 extension, and are the intermediate step for most Rust-based binaries.
1879```
1880
1881#### **Language and compilation**
1882
1883```
1884 The tools and commands used to create this target type will be
1885 determined by the source files in its sources. Targets containing
1886 multiple compiler-incompatible languages are not allowed (e.g. a
1887 target containing both C and C++ sources is acceptable, but a
1888 target containing C and Rust sources is not).
1889```
1890
1891#### **Variables**
1892
1893```
1894 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1895 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
1896 libs, precompiled_header, precompiled_source, rustflags,
1897 rustenv
1898 Deps: data_deps, deps, public_deps
1899 Dependent configs: all_dependent_configs, public_configs
1900 General: check_includes, configs, data, friend, inputs, metadata,
1901 output_name, output_extension, public, sources, testonly,
1902 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001903 Rust variables: aliased_deps, crate_root, crate_name
Brett Wilson796ed472018-07-16 15:11:09 -07001904```
Petr Hosekfe36c7c2019-11-26 14:38:08 -08001905### **rust_proc_macro**: Declare a Rust procedural macro target.
1906
1907```
1908 A Rust procedural macro allows creating syntax extensions as execution of a
1909 function. They are compiled as dynamic libraries and used by the compiler at
1910 runtime.
1911
1912 Their use is the same as of other Rust libraries, but their build has some
1913 additional restrictions in terms of supported flags.
1914```
1915
1916#### **Language and compilation**
1917
1918```
1919 The tools and commands used to create this target type will be
1920 determined by the source files in its sources. Targets containing
1921 multiple compiler-incompatible languages are not allowed (e.g. a
1922 target containing both C and C++ sources is acceptable, but a
1923 target containing C and Rust sources is not).
1924```
1925
1926#### **Variables**
1927
1928```
1929 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1930 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
1931 libs, precompiled_header, precompiled_source, rustflags,
1932 rustenv
1933 Deps: data_deps, deps, public_deps
1934 Dependent configs: all_dependent_configs, public_configs
1935 General: check_includes, configs, data, friend, inputs, metadata,
1936 output_name, output_extension, public, sources, testonly,
1937 visibility
1938 Rust variables: aliased_deps, crate_root, crate_name
1939```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001940### **shared_library**: Declare a shared library target.
Brett Wilson796ed472018-07-16 15:11:09 -07001941
1942```
1943 A shared library will be specified on the linker line for targets listing the
1944 shared library in its "deps". If you don't want this (say you dynamically
1945 load the library at runtime), then you should depend on the shared library
1946 via "data_deps" or, on Darwin platforms, use a "loadable_module" target type
1947 instead.
1948```
1949
Julie Hockettce1fa072019-05-07 17:44:37 -07001950#### **Language and compilation**
1951
1952```
1953 The tools and commands used to create this target type will be
1954 determined by the source files in its sources. Targets containing
1955 multiple compiler-incompatible languages are not allowed (e.g. a
1956 target containing both C and C++ sources is acceptable, but a
1957 target containing C and Rust sources is not).
1958```
1959
Brett Wilson796ed472018-07-16 15:11:09 -07001960#### **Variables**
1961
1962```
1963 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1964 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001965 libs, precompiled_header, precompiled_source, rustflags,
1966 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07001967 Deps: data_deps, deps, public_deps
1968 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001969 General: check_includes, configs, data, friend, inputs, metadata,
1970 output_name, output_extension, public, sources, testonly,
1971 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001972 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Brett Wilson796ed472018-07-16 15:11:09 -07001973```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001974### **source_set**: Declare a source set target.
Brett Wilson796ed472018-07-16 15:11:09 -07001975
1976```
Robert Sesekd0a6f072020-05-15 11:21:22 -04001977 Only C-language source sets are supported at the moment.
Julie Hockettce1fa072019-05-07 17:44:37 -07001978```
1979
1980#### **C-language source_sets**
1981
1982```
Brett Wilson796ed472018-07-16 15:11:09 -07001983 A source set is a collection of sources that get compiled, but are not linked
1984 to produce any kind of library. Instead, the resulting object files are
1985 implicitly added to the linker line of all targets that depend on the source
1986 set.
1987
1988 In most cases, a source set will behave like a static library, except no
1989 actual library file will be produced. This will make the build go a little
1990 faster by skipping creation of a large static library, while maintaining the
1991 organizational benefits of focused build targets.
1992
1993 The main difference between a source set and a static library is around
1994 handling of exported symbols. Most linkers assume declaring a function
1995 exported means exported from the static library. The linker can then do dead
1996 code elimination to delete code not reachable from exported functions.
1997
1998 A source set will not do this code elimination since there is no link step.
Julie Hockette2a29402018-07-31 10:11:42 -07001999 This allows you to link many source sets into a shared library and have the
Brett Wilson796ed472018-07-16 15:11:09 -07002000 "exported symbol" notation indicate "export from the final shared library and
2001 not from the intermediate targets." There is no way to express this concept
2002 when linking multiple static libraries into a shared library.
2003```
2004
2005#### **Variables**
2006
2007```
2008 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2009 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002010 libs, precompiled_header, precompiled_source, rustflags,
2011 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07002012 Deps: data_deps, deps, public_deps
2013 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002014 General: check_includes, configs, data, friend, inputs, metadata,
2015 output_name, output_extension, public, sources, testonly,
2016 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002017```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002018### **static_library**: Declare a static library target.
Brett Wilson796ed472018-07-16 15:11:09 -07002019
2020```
2021 Make a ".a" / ".lib" file.
2022
2023 If you only need the static library for intermediate results in the build,
2024 you should consider a source_set instead since it will skip the (potentially
2025 slow) step of creating the intermediate library file.
2026```
2027
2028#### **Variables**
2029
2030```
2031 complete_static_lib
2032 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2033 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002034 libs, precompiled_header, precompiled_source, rustflags,
2035 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07002036 Deps: data_deps, deps, public_deps
2037 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002038 General: check_includes, configs, data, friend, inputs, metadata,
2039 output_name, output_extension, public, sources, testonly,
2040 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002041 Rust variables: aliased_deps, crate_root, crate_name
Julie Hockettce1fa072019-05-07 17:44:37 -07002042
2043 The tools and commands used to create this target type will be
2044 determined by the source files in its sources. Targets containing
2045 multiple compiler-incompatible languages are not allowed (e.g. a
2046 target containing both C and C++ sources is acceptable, but a
2047 target containing C and Rust sources is not).
Brett Wilson796ed472018-07-16 15:11:09 -07002048```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002049### **target**: Declare an target with the given programmatic type.
Brett Wilson796ed472018-07-16 15:11:09 -07002050
2051```
2052 target(target_type_string, target_name_string) { ... }
2053
2054 The target() function is a way to invoke a built-in target or template with a
2055 type determined at runtime. This is useful for cases where the type of a
2056 target might not be known statically.
2057
2058 Only templates and built-in target functions are supported for the
2059 target_type_string parameter. Arbitrary functions, configs, and toolchains
2060 are not supported.
2061
2062 The call:
2063 target("source_set", "doom_melon") {
2064 Is equivalent to:
2065 source_set("doom_melon") {
2066```
2067
2068#### **Example**
2069
2070```
2071 if (foo_build_as_shared) {
2072 my_type = "shared_library"
2073 } else {
2074 my_type = "source_set"
2075 }
2076
2077 target(my_type, "foo") {
2078 ...
2079 }
2080```
2081## Buildfile functions
2082
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002083### **assert**: Assert an expression is true at generation time.
Brett Wilson796ed472018-07-16 15:11:09 -07002084
2085```
2086 assert( [, ])
2087
2088 If the condition is false, the build will fail with an error. If the
2089 optional second argument is provided, that string will be printed
2090 with the error message.
2091```
2092
2093#### **Examples**
2094
2095```
2096 assert(is_win)
2097 assert(defined(sources), "Sources must be defined");
2098```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002099### **config**: Defines a configuration object.
Brett Wilson796ed472018-07-16 15:11:09 -07002100
2101```
2102 Configuration objects can be applied to targets and specify sets of compiler
2103 flags, includes, defines, etc. They provide a way to conveniently group sets
2104 of this configuration information.
2105
2106 A config is referenced by its label just like a target.
2107
2108 The values in a config are additive only. If you want to remove a flag you
2109 need to remove the corresponding config that sets it. The final set of flags,
2110 defines, etc. for a target is generated in this order:
2111
2112 1. The values specified directly on the target (rather than using a config.
2113 2. The configs specified in the target's "configs" list, in order.
2114 3. Public_configs from a breadth-first traversal of the dependency tree in
2115 the order that the targets appear in "deps".
2116 4. All dependent configs from a breadth-first traversal of the dependency
2117 tree in the order that the targets appear in "deps".
2118```
2119
Joe Armstrong39413942019-03-15 10:34:03 +08002120#### **More background**
2121
2122```
2123 Configs solve a problem where the build system needs to have a higher-level
2124 understanding of various compiler settings. For example, some compiler flags
2125 have to appear in a certain order relative to each other, some settings like
2126 defines and flags logically go together, and the build system needs to
2127 de-duplicate flags even though raw command-line parameters can't always be
2128 operated on in that way.
2129
2130 The config gives a name to a group of settings that can then be reasoned
2131 about by GN. GN can know that configs with the same label are the same thing
2132 so can be de-duplicated. It allows related settings to be grouped so they
2133 are added or removed as a unit. And it allows targets to refer to settings
2134 with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than
2135 having to hard-coding every compiler's flags each time they are referred to.
2136```
2137
Brett Wilson796ed472018-07-16 15:11:09 -07002138#### **Variables valid in a config definition**
Joe Armstrong39413942019-03-15 10:34:03 +08002139
Brett Wilson796ed472018-07-16 15:11:09 -07002140```
2141 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2142 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002143 libs, precompiled_header, precompiled_source, rustflags,
2144 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07002145 Nested configs: configs
2146```
2147
2148#### **Variables on a target used to apply configs**
2149
2150```
2151 all_dependent_configs, configs, public_configs
2152```
2153
2154#### **Example**
2155
2156```
2157 config("myconfig") {
Joe Armstrongf5ad8072019-05-08 08:42:56 +08002158 include_dirs = [ "include/common" ]
Brett Wilson796ed472018-07-16 15:11:09 -07002159 defines = [ "ENABLE_DOOM_MELON" ]
2160 }
2161
2162 executable("mything") {
2163 configs = [ ":myconfig" ]
2164 }
2165```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002166### **declare_args**: Declare build arguments.
Brett Wilson796ed472018-07-16 15:11:09 -07002167
2168```
2169 Introduces the given arguments into the current scope. If they are not
2170 specified on the command line or in a toolchain's arguments, the default
2171 values given in the declare_args block will be used. However, these defaults
2172 will not override command-line values.
2173
2174 See also "gn help buildargs" for an overview.
2175
2176 The precise behavior of declare args is:
2177
2178 1. The declare_args() block executes. Any variable defined in the enclosing
2179 scope is available for reading, but any variable defined earlier in
2180 the current scope is not (since the overrides haven't been applied yet).
2181
2182 2. At the end of executing the block, any variables set within that scope
Petr Hosek7617b592019-10-02 09:52:21 -07002183 are saved, with the values specified in the block used as the "default value"
2184 for that argument. Once saved, these variables are available for override
2185 via args.gn.
Brett Wilson796ed472018-07-16 15:11:09 -07002186
2187 3. User-defined overrides are applied. Anything set in "gn args" now
2188 overrides any default values. The resulting set of variables is promoted
2189 to be readable from the following code in the file.
2190
2191 This has some ramifications that may not be obvious:
2192
2193 - You should not perform difficult work inside a declare_args block since
2194 this only sets a default value that may be discarded. In particular,
2195 don't use the result of exec_script() to set the default value. If you
2196 want to have a script-defined default, set some default "undefined" value
2197 like [], "", or -1, and after the declare_args block, call exec_script if
2198 the value is unset by the user.
2199
2200 - Because you cannot read the value of a variable defined in the same
2201 block, if you need to make the default value of one arg depend
2202 on the possibly-overridden value of another, write two separate
2203 declare_args() blocks:
2204
2205 declare_args() {
2206 enable_foo = true
2207 }
2208 declare_args() {
2209 # Bar defaults to same user-overridden state as foo.
2210 enable_bar = enable_foo
2211 }
2212```
2213
2214#### **Example**
2215
2216```
2217 declare_args() {
2218 enable_teleporter = true
2219 enable_doom_melon = false
2220 }
2221
2222 If you want to override the (default disabled) Doom Melon:
2223 gn --args="enable_doom_melon=true enable_teleporter=true"
2224 This also sets the teleporter, but it's already defaulted to on so it will
2225 have no effect.
2226```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002227### **defined**: Returns whether an identifier is defined.
Brett Wilson796ed472018-07-16 15:11:09 -07002228
2229```
2230 Returns true if the given argument is defined. This is most useful in
2231 templates to assert that the caller set things up properly.
2232
2233 You can pass an identifier:
2234 defined(foo)
2235 which will return true or false depending on whether foo is defined in the
2236 current scope.
2237
2238 You can also check a named scope:
2239 defined(foo.bar)
2240 which will return true or false depending on whether bar is defined in the
2241 named scope foo. It will throw an error if foo is not defined or is not a
2242 scope.
2243```
2244
2245#### **Example**
2246
2247```
2248 template("mytemplate") {
2249 # To help users call this template properly...
2250 assert(defined(invoker.sources), "Sources must be defined")
2251
2252 # If we want to accept an optional "values" argument, we don't
2253 # want to dereference something that may not be defined.
2254 if (defined(invoker.values)) {
2255 values = invoker.values
2256 } else {
2257 values = "some default value"
2258 }
2259 }
2260```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002261### **exec_script**: Synchronously run a script and return the output.
Brett Wilson796ed472018-07-16 15:11:09 -07002262
2263```
2264 exec_script(filename,
2265 arguments = [],
2266 input_conversion = "",
2267 file_dependencies = [])
2268
2269 Runs the given script, returning the stdout of the script. The build
2270 generation will fail if the script does not exist or returns a nonzero exit
2271 code.
2272
2273 The current directory when executing the script will be the root build
2274 directory. If you are passing file names, you will want to use the
2275 rebase_path() function to make file names relative to this path (see "gn help
2276 rebase_path").
Gary Miguelce7fa362018-09-17 12:48:17 -07002277
2278 The default script interpreter is Python ("python" on POSIX, "python.exe" or
2279 "python.bat" on Windows). This can be configured by the script_executable
2280 variable, see "gn help dotfile".
Brett Wilson796ed472018-07-16 15:11:09 -07002281```
2282
2283#### **Arguments**:
2284
2285```
2286 filename:
Gary Miguelce7fa362018-09-17 12:48:17 -07002287 File name of script to execute. Non-absolute names will be treated as
2288 relative to the current build file.
Brett Wilson796ed472018-07-16 15:11:09 -07002289
2290 arguments:
2291 A list of strings to be passed to the script as arguments. May be
2292 unspecified or the empty list which means no arguments.
2293
2294 input_conversion:
Julie Hockett81ee1962019-05-13 11:24:42 -07002295 Controls how the file is read and parsed. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07002296
2297 If unspecified, defaults to the empty string which causes the script
2298 result to be discarded. exec script will return None.
2299
2300 dependencies:
2301 (Optional) A list of files that this script reads or otherwise depends
2302 on. These dependencies will be added to the build result such that if any
2303 of them change, the build will be regenerated and the script will be
2304 re-run.
2305
2306 The script itself will be an implicit dependency so you do not need to
2307 list it.
2308```
2309
2310#### **Example**
2311
2312```
2313 all_lines = exec_script(
2314 "myscript.py", [some_input], "list lines",
2315 [ rebase_path("data_file.txt", root_build_dir) ])
2316
2317 # This example just calls the script with no arguments and discards the
2318 # result.
2319 exec_script("//foo/bar/myscript.py")
2320```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01002321### **filter_exclude**: Remove values that match a set of patterns.
2322
2323```
2324 filter_exclude(values, exclude_patterns)
2325
2326 The argument values must be a list of strings.
2327
2328 The argument exclude_patterns must be a list of file patterns (see
2329 "gn help file_pattern"). Any elements in values matching at least one
2330 of those patterns will be excluded.
2331```
2332
2333#### **Examples**
2334```
2335 values = [ "foo.cc", "foo.h", "foo.proto" ]
2336 result = filter_exclude(values, [ "*.proto" ])
2337 # result will be [ "foo.cc", "foo.h" ]
2338```
2339### **filter_include**: Remove values that do not match a set of patterns.
2340
2341```
2342 filter_include(values, include_patterns)
2343
2344 The argument values must be a list of strings.
2345
2346 The argument include_patterns must be a list of file patterns (see
2347 "gn help file_pattern"). Only elements from values matching at least
2348 one of the pattern will be included.
2349```
2350
2351#### **Examples**
2352```
2353 values = [ "foo.cc", "foo.h", "foo.proto" ]
2354 result = filter_include(values, [ "*.proto" ])
2355 # result will be [ "foo.proto" ]
2356```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002357### **foreach**: Iterate over a list.
Brett Wilson796ed472018-07-16 15:11:09 -07002358
2359```
2360 foreach(, ) {
2361
2362 }
2363
2364 Executes the loop contents block over each item in the list, assigning the
2365 loop_var to each item in sequence. The will be a copy so assigning
2366 to it will not mutate the list. The loop will iterate over a copy of
2367 so mutating it inside the loop will not affect iteration.
2368
2369 The block does not introduce a new scope, so that variable assignments inside
2370 the loop will be visible once the loop terminates.
2371
2372 The loop variable will temporarily shadow any existing variables with the
2373 same name for the duration of the loop. After the loop terminates the loop
2374 variable will no longer be in scope, and the previous value (if any) will be
2375 restored.
2376```
2377
2378#### **Example**
2379
2380```
2381 mylist = [ "a", "b", "c" ]
2382 foreach(i, mylist) {
2383 print(i)
2384 }
2385
2386 Prints:
2387 a
2388 b
2389 c
2390```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002391### **forward_variables_from**: Copies variables from a different scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002392
2393```
2394 forward_variables_from(from_scope, variable_list_or_star,
2395 variable_to_not_forward_list = [])
2396
2397 Copies the given variables from the given scope to the local scope if they
2398 exist. This is normally used in the context of templates to use the values of
2399 variables defined in the template invocation to a template-defined target.
2400
2401 The variables in the given variable_list will be copied if they exist in the
2402 given scope or any enclosing scope. If they do not exist, nothing will happen
2403 and they be left undefined in the current scope.
2404
2405 As a special case, if the variable_list is a string with the value of "*",
2406 all variables from the given scope will be copied. "*" only copies variables
2407 set directly on the from_scope, not enclosing ones. Otherwise it would
2408 duplicate all global variables.
2409
2410 When an explicit list of variables is supplied, if the variable exists in the
2411 current (destination) scope already, an error will be thrown. If "*" is
2412 specified, variables in the current scope will be clobbered (the latter is
2413 important because most targets have an implicit configs list, which means it
2414 wouldn't work at all if it didn't clobber).
2415
2416 The sources assignment filter (see "gn help set_sources_assignment_filter")
2417 is never applied by this function. It's assumed than any desired filtering
2418 was already done when sources was set on the from_scope.
2419
2420 If variables_to_not_forward_list is non-empty, then it must contains a list
2421 of variable names that will not be forwarded. This is mostly useful when
2422 variable_list_or_star has a value of "*".
2423```
2424
2425#### **Examples**
2426
2427```
Andrew Grieve57a964c2018-09-14 10:50:56 -04002428 # forward_variables_from(invoker, ["foo"])
2429 # is equivalent to:
2430 assert(!defined(foo))
2431 if (defined(invoker.foo)) {
2432 foo = invoker.foo
2433 }
2434
Brett Wilson796ed472018-07-16 15:11:09 -07002435 # This is a common action template. It would invoke a script with some given
2436 # parameters, and wants to use the various types of deps and the visibility
2437 # from the invoker if it's defined. It also injects an additional dependency
2438 # to all targets.
2439 template("my_test") {
2440 action(target_name) {
2441 forward_variables_from(invoker, [ "data_deps", "deps",
Julie Hockette2a29402018-07-31 10:11:42 -07002442 "public_deps", "visibility"])
Brett Wilson796ed472018-07-16 15:11:09 -07002443 # Add our test code to the dependencies.
2444 # "deps" may or may not be defined at this point.
2445 if (defined(deps)) {
2446 deps += [ "//tools/doom_melon" ]
2447 } else {
2448 deps = [ "//tools/doom_melon" ]
2449 }
2450 }
2451 }
2452
Julie Hockette2a29402018-07-31 10:11:42 -07002453 # This is a template around a target whose type depends on a global variable.
2454 # It forwards all values from the invoker.
Brett Wilson796ed472018-07-16 15:11:09 -07002455 template("my_wrapper") {
2456 target(my_wrapper_target_type, target_name) {
2457 forward_variables_from(invoker, "*")
2458 }
2459 }
2460
2461 # A template that wraps another. It adds behavior based on one
2462 # variable, and forwards all others to the nested target.
2463 template("my_ios_test_app") {
2464 ios_test_app(target_name) {
2465 forward_variables_from(invoker, "*", ["test_bundle_name"])
2466 if (!defined(extra_substitutions)) {
2467 extra_substitutions = []
2468 }
2469 extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ]
2470 }
2471 }
2472```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002473### **get_label_info**: Get an attribute from a target's label.
Brett Wilson796ed472018-07-16 15:11:09 -07002474
2475```
2476 get_label_info(target_label, what)
2477
2478 Given the label of a target, returns some attribute of that target. The
2479 target need not have been previously defined in the same file, since none of
2480 the attributes depend on the actual target definition, only the label itself.
2481
2482 See also "gn help get_target_outputs".
2483```
2484
2485#### **Possible values for the "what" parameter**
2486
2487```
2488 "name"
2489 The short name of the target. This will match the value of the
2490 "target_name" variable inside that target's declaration. For the label
2491 "//foo/bar:baz" this will return "baz".
2492
2493 "dir"
2494 The directory containing the target's definition, with no slash at the
2495 end. For the label "//foo/bar:baz" this will return "//foo/bar".
2496
2497 "target_gen_dir"
2498 The generated file directory for the target. This will match the value of
2499 the "target_gen_dir" variable when inside that target's declaration.
2500
2501 "root_gen_dir"
2502 The root of the generated file tree for the target. This will match the
2503 value of the "root_gen_dir" variable when inside that target's
2504 declaration.
2505
2506 "target_out_dir
2507 The output directory for the target. This will match the value of the
2508 "target_out_dir" variable when inside that target's declaration.
2509
2510 "root_out_dir"
2511 The root of the output file tree for the target. This will match the
2512 value of the "root_out_dir" variable when inside that target's
2513 declaration.
2514
2515 "label_no_toolchain"
2516 The fully qualified version of this label, not including the toolchain.
2517 For the input ":bar" it might return "//foo:bar".
2518
2519 "label_with_toolchain"
2520 The fully qualified version of this label, including the toolchain. For
2521 the input ":bar" it might return "//foo:bar(//toolchain:x64)".
2522
2523 "toolchain"
2524 The label of the toolchain. This will match the value of the
2525 "current_toolchain" variable when inside that target's declaration.
2526```
2527
2528#### **Examples**
2529
2530```
2531 get_label_info(":foo", "name")
2532 # Returns string "foo".
2533
2534 get_label_info("//foo/bar:baz", "target_gen_dir")
2535 # Returns string "//out/Debug/gen/foo/bar".
2536```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002537### **get_path_info**: Extract parts of a file or directory name.
Brett Wilson796ed472018-07-16 15:11:09 -07002538
2539```
2540 get_path_info(input, what)
2541
2542 The first argument is either a string representing a file or directory name,
2543 or a list of such strings. If the input is a list the return value will be a
2544 list containing the result of applying the rule to each item in the input.
2545```
2546
2547#### **Possible values for the "what" parameter**
2548
2549```
2550 "file"
2551 The substring after the last slash in the path, including the name and
2552 extension. If the input ends in a slash, the empty string will be
2553 returned.
2554 "foo/bar.txt" => "bar.txt"
2555 "bar.txt" => "bar.txt"
2556 "foo/" => ""
2557 "" => ""
2558
2559 "name"
2560 The substring of the file name not including the extension.
2561 "foo/bar.txt" => "bar"
2562 "foo/bar" => "bar"
2563 "foo/" => ""
2564
2565 "extension"
2566 The substring following the last period following the last slash, or the
2567 empty string if not found. The period is not included.
2568 "foo/bar.txt" => "txt"
2569 "foo/bar" => ""
2570
2571 "dir"
2572 The directory portion of the name, not including the slash.
2573 "foo/bar.txt" => "foo"
2574 "//foo/bar" => "//foo"
2575 "foo" => "."
2576
2577 The result will never end in a slash, so if the resulting is empty, the
2578 system ("/") or source ("//") roots, a "." will be appended such that it
2579 is always legal to append a slash and a filename and get a valid path.
2580
2581 "out_dir"
2582 The output file directory corresponding to the path of the given file,
2583 not including a trailing slash.
2584 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
2585
2586 "gen_dir"
2587 The generated file directory corresponding to the path of the given file,
2588 not including a trailing slash.
2589 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
2590
2591 "abspath"
2592 The full absolute path name to the file or directory. It will be resolved
2593 relative to the current directory, and then the source- absolute version
2594 will be returned. If the input is system- absolute, the same input will
2595 be returned.
2596 "foo/bar.txt" => "//mydir/foo/bar.txt"
2597 "foo/" => "//mydir/foo/"
2598 "//foo/bar" => "//foo/bar" (already absolute)
2599 "/usr/include" => "/usr/include" (already absolute)
2600
2601 If you want to make the path relative to another directory, or to be
2602 system-absolute, see rebase_path().
2603```
2604
2605#### **Examples**
2606```
2607 sources = [ "foo.cc", "foo.h" ]
2608 result = get_path_info(source, "abspath")
2609 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
2610
2611 result = get_path_info("//foo/bar/baz.cc", "dir")
2612 # result will be "//foo/bar"
2613
2614 # Extract the source-absolute directory name,
Joe Armstrong39413942019-03-15 10:34:03 +08002615 result = get_path_info(get_path_info(path, "dir"), "abspath")
Brett Wilson796ed472018-07-16 15:11:09 -07002616```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002617### **get_target_outputs**: [file list] Get the list of outputs from a target.
Brett Wilson796ed472018-07-16 15:11:09 -07002618
2619```
2620 get_target_outputs(target_label)
2621
2622 Returns a list of output files for the named target. The named target must
2623 have been previously defined in the current file before this function is
2624 called (it can't reference targets in other files because there isn't a
2625 defined execution order, and it obviously can't reference targets that are
2626 defined after the function call).
2627
Julie Hockettce1fa072019-05-07 17:44:37 -07002628 Only copy, generated_file, and action targets are supported. The outputs from
2629 binary targets will depend on the toolchain definition which won't
2630 necessarily have been loaded by the time a given line of code has run, and
2631 source sets and groups have no useful output file.
Brett Wilson796ed472018-07-16 15:11:09 -07002632```
2633
2634#### **Return value**
2635
2636```
2637 The names in the resulting list will be absolute file paths (normally like
2638 "//out/Debug/bar.exe", depending on the build directory).
2639
Julie Hockettce1fa072019-05-07 17:44:37 -07002640 action, copy, and generated_file targets: this will just return the files
2641 specified in the "outputs" variable of the target.
Brett Wilson796ed472018-07-16 15:11:09 -07002642
2643 action_foreach targets: this will return the result of applying the output
2644 template to the sources (see "gn help source_expansion"). This will be the
2645 same result (though with guaranteed absolute file paths), as
2646 process_file_template will return for those inputs (see "gn help
2647 process_file_template").
2648
Brett Wilson796ed472018-07-16 15:11:09 -07002649 source sets and groups: this will return a list containing the path of the
2650 "stamp" file that Ninja will produce once all outputs are generated. This
2651 probably isn't very useful.
2652```
2653
2654#### **Example**
2655
2656```
2657 # Say this action generates a bunch of C source files.
2658 action_foreach("my_action") {
2659 sources = [ ... ]
2660 outputs = [ ... ]
2661 }
2662
2663 # Compile the resulting source files into a source set.
2664 source_set("my_lib") {
2665 sources = get_target_outputs(":my_action")
2666 }
2667```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002668### **getenv**: Get an environment variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002669
2670```
2671 value = getenv(env_var_name)
2672
2673 Returns the value of the given environment variable. If the value is not
2674 found, it will try to look up the variable with the "opposite" case (based on
2675 the case of the first letter of the variable), but is otherwise
2676 case-sensitive.
2677
2678 If the environment variable is not found, the empty string will be returned.
2679 Note: it might be nice to extend this if we had the concept of "none" in the
2680 language to indicate lookup failure.
2681```
2682
2683#### **Example**
2684
2685```
2686 home_dir = getenv("HOME")
2687```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002688### **import**: Import a file into the current scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002689
2690```
2691 The import command loads the rules and variables resulting from executing the
2692 given file into the current scope.
2693
2694 By convention, imported files are named with a .gni extension.
2695
2696 An import is different than a C++ "include". The imported file is executed in
2697 a standalone environment from the caller of the import command. The results
2698 of this execution are cached for other files that import the same .gni file.
2699
2700 Note that you can not import a BUILD.gn file that's otherwise used in the
2701 build. Files must either be imported or implicitly loaded as a result of deps
2702 rules, but not both.
2703
2704 The imported file's scope will be merged with the scope at the point import
2705 was called. If there is a conflict (both the current scope and the imported
2706 file define some variable or rule with the same name but different value), a
2707 runtime error will be thrown. Therefore, it's good practice to minimize the
2708 stuff that an imported file defines.
2709
2710 Variables and templates beginning with an underscore '_' are considered
2711 private and will not be imported. Imported files can use such variables for
2712 internal computation without affecting other files.
2713```
2714
2715#### **Examples**
2716
2717```
2718 import("//build/rules/idl_compilation_rule.gni")
2719
2720 # Looks in the current directory.
2721 import("my_vars.gni")
2722```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002723### **not_needed**: Mark variables from scope as not needed.
Brett Wilson796ed472018-07-16 15:11:09 -07002724
2725```
2726 not_needed(variable_list_or_star, variable_to_ignore_list = [])
2727 not_needed(from_scope, variable_list_or_star,
2728 variable_to_ignore_list = [])
2729
2730 Mark the variables in the current or given scope as not needed, which means
2731 you will not get an error about unused variables for these. The
2732 variable_to_ignore_list allows excluding variables from "all matches" if
2733 variable_list_or_star is "*".
2734```
2735
2736#### **Example**
2737
2738```
2739 not_needed("*", [ "config" ])
2740 not_needed([ "data_deps", "deps" ])
2741 not_needed(invoker, "*", [ "config" ])
2742 not_needed(invoker, [ "data_deps", "deps" ])
2743```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002744### **pool**: Defines a pool object.
Brett Wilson796ed472018-07-16 15:11:09 -07002745
2746```
2747 Pool objects can be applied to a tool to limit the parallelism of the
2748 build. This object has a single property "depth" corresponding to
2749 the number of tasks that may run simultaneously.
2750
2751 As the file containing the pool definition may be executed in the
2752 context of more than one toolchain it is recommended to specify an
2753 explicit toolchain when defining and referencing a pool.
2754
Julie Hockette2a29402018-07-31 10:11:42 -07002755 A pool named "console" defined in the root build file represents Ninja's
2756 console pool. Targets using this pool will have access to the console's
2757 stdin and stdout, and output will not be buffered. This special pool must
2758 have a depth of 1. Pools not defined in the root must not be named "console".
2759 The console pool can only be defined for the default toolchain.
2760 Refer to the Ninja documentation on the console pool for more info.
2761
Brett Wilson796ed472018-07-16 15:11:09 -07002762 A pool is referenced by its label just like a target.
2763```
2764
2765#### **Variables**
2766
2767```
2768 depth*
2769 * = required
2770```
2771
2772#### **Example**
2773
2774```
2775 if (current_toolchain == default_toolchain) {
2776 pool("link_pool") {
2777 depth = 1
2778 }
2779 }
2780
2781 toolchain("toolchain") {
2782 tool("link") {
2783 command = "..."
Petr Hosekadd1bb12019-09-11 14:39:38 -07002784 pool = ":link_pool($default_toolchain)"
Brett Wilson796ed472018-07-16 15:11:09 -07002785 }
2786 }
2787```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002788### **print**: Prints to the console.
Brett Wilson796ed472018-07-16 15:11:09 -07002789
2790```
2791 Prints all arguments to the console separated by spaces. A newline is
2792 automatically appended to the end.
2793
2794 This function is intended for debugging. Note that build files are run in
2795 parallel so you may get interleaved prints. A buildfile may also be executed
2796 more than once in parallel in the context of different toolchains so the
2797 prints from one file may be duplicated or
2798 interleaved with itself.
2799```
2800
2801#### **Examples**
2802
2803```
2804 print("Hello world")
2805
2806 print(sources, deps)
2807```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002808### **process_file_template**: Do template expansion over a list of files.
Brett Wilson796ed472018-07-16 15:11:09 -07002809
2810```
2811 process_file_template(source_list, template)
2812
2813 process_file_template applies a template list to a source file list,
2814 returning the result of applying each template to each source. This is
2815 typically used for computing output file names from input files.
2816
2817 In most cases, get_target_outputs() will give the same result with shorter,
2818 more maintainable code. This function should only be used when that function
2819 can't be used (like there's no target or the target is defined in another
2820 build file).
2821```
2822
2823#### **Arguments**
2824
2825```
2826 The source_list is a list of file names.
2827
2828 The template can be a string or a list. If it is a list, multiple output
2829 strings are generated for each input.
2830
2831 The template should contain source expansions to which each name in the
2832 source list is applied. See "gn help source_expansion".
2833```
2834
2835#### **Example**
2836
2837```
2838 sources = [
2839 "foo.idl",
2840 "bar.idl",
2841 ]
2842 myoutputs = process_file_template(
2843 sources,
2844 [ "$target_gen_dir/{{source_name_part}}.cc",
2845 "$target_gen_dir/{{source_name_part}}.h" ])
2846
2847 The result in this case will be:
2848 [ "//out/Debug/foo.cc"
2849 "//out/Debug/foo.h"
2850 "//out/Debug/bar.cc"
2851 "//out/Debug/bar.h" ]
2852```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002853### **read_file**: Read a file into a variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002854
2855```
2856 read_file(filename, input_conversion)
2857
2858 Whitespace will be trimmed from the end of the file. Throws an error if the
2859 file can not be opened.
2860```
2861
2862#### **Arguments**
2863
2864```
2865 filename
2866 Filename to read, relative to the build file.
2867
2868 input_conversion
Julie Hockett81ee1962019-05-13 11:24:42 -07002869 Controls how the file is read and parsed. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07002870```
2871
2872#### **Example**
2873
2874```
2875 lines = read_file("foo.txt", "list lines")
2876```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002877### **rebase_path**: Rebase a file or directory to another location.
Brett Wilson796ed472018-07-16 15:11:09 -07002878
2879```
2880 converted = rebase_path(input,
2881 new_base = "",
2882 current_base = ".")
2883
2884 Takes a string argument representing a file name, or a list of such strings
2885 and converts it/them to be relative to a different base directory.
2886
2887 When invoking the compiler or scripts, GN will automatically convert sources
2888 and include directories to be relative to the build directory. However, if
2889 you're passing files directly in the "args" array or doing other manual
2890 manipulations where GN doesn't know something is a file name, you will need
2891 to convert paths to be relative to what your tool is expecting.
2892
2893 The common case is to use this to convert paths relative to the current
2894 directory to be relative to the build directory (which will be the current
2895 directory when executing scripts).
2896
2897 If you want to convert a file path to be source-absolute (that is, beginning
2898 with a double slash like "//foo/bar"), you should use the get_path_info()
2899 function. This function won't work because it will always make relative
2900 paths, and it needs to support making paths relative to the source root, so
Robert Sesekd0a6f072020-05-15 11:21:22 -04002901 it can't also generate source-absolute paths without more special-cases.
Brett Wilson796ed472018-07-16 15:11:09 -07002902```
2903
2904#### **Arguments**
2905
2906```
2907 input
Robert Sesekd0a6f072020-05-15 11:21:22 -04002908 A string or list of strings representing file or directory names. These
Brett Wilson796ed472018-07-16 15:11:09 -07002909 can be relative paths ("foo/bar.txt"), system absolute paths
2910 ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt").
2911
2912 new_base
2913 The directory to convert the paths to be relative to. This can be an
2914 absolute path or a relative path (which will be treated as being relative
2915 to the current BUILD-file's directory).
2916
2917 As a special case, if new_base is the empty string (the default), all
2918 paths will be converted to system-absolute native style paths with system
2919 path separators. This is useful for invoking external programs.
2920
2921 current_base
2922 Directory representing the base for relative paths in the input. If this
2923 is not an absolute path, it will be treated as being relative to the
2924 current build file. Use "." (the default) to convert paths from the
2925 current BUILD-file's directory.
2926```
2927
2928#### **Return value**
2929
2930```
2931 The return value will be the same type as the input value (either a string or
2932 a list of strings). All relative and source-absolute file names will be
2933 converted to be relative to the requested output System-absolute paths will
2934 be unchanged.
2935
2936 Whether an output path will end in a slash will match whether the
2937 corresponding input path ends in a slash. It will return "." or "./"
2938 (depending on whether the input ends in a slash) to avoid returning empty
2939 strings. This means if you want a root path ("//" or "/") not ending in a
2940 slash, you can add a dot ("//.").
2941```
2942
2943#### **Example**
2944
2945```
2946 # Convert a file in the current directory to be relative to the build
2947 # directory (the current dir when executing compilers and scripts).
2948 foo = rebase_path("myfile.txt", root_build_dir)
2949 # might produce "../../project/myfile.txt".
2950
2951 # Convert a file to be system absolute:
2952 foo = rebase_path("myfile.txt")
2953 # Might produce "D:\\source\\project\\myfile.txt" on Windows or
2954 # "/home/you/source/project/myfile.txt" on Linux.
2955
2956 # Typical usage for converting to the build directory for a script.
2957 action("myscript") {
2958 # Don't convert sources, GN will automatically convert these to be relative
2959 # to the build directory when it constructs the command line for your
2960 # script.
2961 sources = [ "foo.txt", "bar.txt" ]
2962
2963 # Extra file args passed manually need to be explicitly converted
2964 # to be relative to the build directory:
2965 args = [
2966 "--data",
2967 rebase_path("//mything/data/input.dat", root_build_dir),
2968 "--rel",
2969 rebase_path("relative_path.txt", root_build_dir)
2970 ] + rebase_path(sources, root_build_dir)
2971 }
2972```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002973### **set_default_toolchain**: Sets the default toolchain name.
Brett Wilson796ed472018-07-16 15:11:09 -07002974
2975```
2976 set_default_toolchain(toolchain_label)
2977
2978 The given label should identify a toolchain definition (see "gn help
2979 toolchain"). This toolchain will be used for all targets unless otherwise
2980 specified.
2981
2982 This function is only valid to call during the processing of the build
2983 configuration file. Since the build configuration file is processed
2984 separately for each toolchain, this function will be a no-op when called
2985 under any non-default toolchains.
2986
2987 For example, the default toolchain should be appropriate for the current
2988 environment. If the current environment is 32-bit and somebody references a
2989 target with a 64-bit toolchain, we wouldn't want processing of the build
2990 config file for the 64-bit toolchain to reset the default toolchain to
2991 64-bit, we want to keep it 32-bits.
2992```
2993
2994#### **Argument**
2995
2996```
2997 toolchain_label
2998 Toolchain name.
2999```
3000
3001#### **Example**
3002
3003```
3004 # Set default toolchain only has an effect when run in the context of the
3005 # default toolchain. Pick the right one according to the current CPU
3006 # architecture.
3007 if (target_cpu == "x64") {
3008 set_default_toolchain("//toolchains:64")
3009 } else if (target_cpu == "x86") {
3010 set_default_toolchain("//toolchains:32")
3011 }
3012```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003013### **set_defaults**: Set default values for a target type.
Brett Wilson796ed472018-07-16 15:11:09 -07003014
3015```
3016 set_defaults() { }
3017
3018 Sets the default values for a given target type. Whenever target_type_name is
3019 seen in the future, the values specified in set_default's block will be
3020 copied into the current scope.
3021
3022 When the target type is used, the variable copying is very strict. If a
3023 variable with that name is already in scope, the build will fail with an
3024 error.
3025
3026 set_defaults can be used for built-in target types ("executable",
3027 "shared_library", etc.) and custom ones defined via the "template" command.
3028 It can be called more than once and the most recent call in any scope will
3029 apply, but there is no way to refer to the previous defaults and modify them
3030 (each call to set_defaults must supply a complete list of all defaults it
3031 wants). If you want to share defaults, store them in a separate variable.
3032```
3033
3034#### **Example**
3035
3036```
3037 set_defaults("static_library") {
3038 configs = [ "//tools/mything:settings" ]
3039 }
3040
Nico Webere49cb722018-08-28 13:10:29 -04003041 static_library("mylib") {
Brett Wilson796ed472018-07-16 15:11:09 -07003042 # The configs will be auto-populated as above. You can remove it if
3043 # you don't want the default for a particular default:
3044 configs -= [ "//tools/mything:settings" ]
3045 }
3046```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003047### **set_sources_assignment_filter**: Set a pattern to filter source files.
Brett Wilson796ed472018-07-16 15:11:09 -07003048
3049```
3050 The sources assignment filter is a list of patterns that remove files from
3051 the list implicitly whenever the "sources" variable is assigned to. This will
3052 do nothing for non-lists.
3053
3054 This is intended to be used to globally filter out files with
3055 platform-specific naming schemes when they don't apply, for example you may
3056 want to filter out all "*_win.cc" files on non-Windows platforms.
3057
3058 Typically this will be called once in the master build config script to set
3059 up the filter for the current platform. Subsequent calls will overwrite the
3060 previous values.
3061
3062 If you want to bypass the filter and add a file even if it might be filtered
3063 out, call set_sources_assignment_filter([]) to clear the list of filters.
Sylvain Defresneaff489a2020-03-11 18:27:43 +01003064 This will apply until the current scope exits.
Brett Wilson796ed472018-07-16 15:11:09 -07003065
Sylvain Defresneaff489a2020-03-11 18:27:43 +01003066 See "gn help file_pattern" for more information on file pattern.
Brett Wilson796ed472018-07-16 15:11:09 -07003067```
3068
3069#### **Sources assignment example**
3070
3071```
3072 # Filter out all _win files.
3073 set_sources_assignment_filter([ "*_win.cc", "*_win.h" ])
3074 sources = [ "a.cc", "b_win.cc" ]
3075 print(sources)
3076 # Will print [ "a.cc" ]. b_win one was filtered out.
3077```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003078### **split_list**: Splits a list into N different sub-lists.
Brett Wilson796ed472018-07-16 15:11:09 -07003079
3080```
3081 result = split_list(input, n)
3082
3083 Given a list and a number N, splits the list into N sub-lists of
3084 approximately equal size. The return value is a list of the sub-lists. The
3085 result will always be a list of size N. If N is greater than the number of
3086 elements in the input, it will be padded with empty lists.
3087
3088 The expected use is to divide source files into smaller uniform chunks.
3089```
3090
3091#### **Example**
3092
3093```
3094 The code:
3095 mylist = [1, 2, 3, 4, 5, 6]
3096 print(split_list(mylist, 3))
3097
3098 Will print:
3099 [[1, 2], [3, 4], [5, 6]
3100```
Keir Mierle6ae63302019-11-08 23:02:18 -08003101### **string_join**: Concatenates a list of strings with a separator.
3102
3103```
3104 result = string_join(separator, strings)
3105
3106 Concatenate a list of strings with intervening occurrences of separator.
3107```
3108
3109#### **Examples**
3110
3111```
3112 string_join("", ["a", "b", "c"]) --> "abc"
3113 string_join("|", ["a", "b", "c"]) --> "a|b|c"
3114 string_join(", ", ["a", "b", "c"]) --> "a, b, c"
3115 string_join("s", ["", ""]) --> "s"
3116```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003117### **string_replace**: Replaces substring in the given string.
Petr Hosekc0744ed2018-08-04 16:13:59 -07003118
3119```
3120 result = string_replace(str, old, new[, max])
3121
3122 Returns a copy of the string str in which the occurrences of old have been
3123 replaced with new, optionally restricting the number of replacements. The
3124 replacement is performed sequentially, so if new contains old, it won't be
3125 replaced.
3126```
3127
3128#### **Example**
3129
3130```
3131 The code:
3132 mystr = "Hello, world!"
3133 print(string_replace(mystr, "world", "GN"))
3134
3135 Will print:
3136 Hello, GN!
3137```
Keir Mierle6ae63302019-11-08 23:02:18 -08003138### **string_split**: Split string into a list of strings.
3139
3140```
3141 result = string_split(str[, sep])
3142
3143 Split string into all substrings separated by separator and returns a list
3144 of the substrings between those separators.
3145
3146 If the separator argument is omitted, the split is by any whitespace, and
3147 any leading/trailing whitespace is ignored; similar to Python's str.split().
3148```
3149
3150#### **Examples without a separator (split on whitespace)**:
3151
3152```
3153 string_split("") --> []
3154 string_split("a") --> ["a"]
3155 string_split(" aa bb") --> ["aa", "bb"]
3156```
3157
3158#### **Examples with a separator (split on separators)**:
3159
3160```
3161 string_split("", "|") --> [""]
3162 string_split(" a b ", " ") --> ["", "", "a", "b", "", ""]
3163 string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"]
3164```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003165### **template**: Define a template rule.
Brett Wilson796ed472018-07-16 15:11:09 -07003166
3167```
3168 A template defines a custom name that acts like a function. It provides a way
3169 to add to the built-in target types.
3170
3171 The template() function is used to declare a template. To invoke the
3172 template, just use the name of the template like any other target type.
3173
3174 Often you will want to declare your template in a special file that other
3175 files will import (see "gn help import") so your template rule can be shared
3176 across build files.
3177```
3178
3179#### **Variables and templates**:
3180
3181```
3182 When you call template() it creates a closure around all variables currently
3183 in scope with the code in the template block. When the template is invoked,
3184 the closure will be executed.
3185
3186 When the template is invoked, the code in the caller is executed and passed
3187 to the template code as an implicit "invoker" variable. The template uses
3188 this to read state out of the invoking code.
3189
3190 One thing explicitly excluded from the closure is the "current directory"
3191 against which relative file names are resolved. The current directory will be
3192 that of the invoking code, since typically that code specifies the file
3193 names. This means all files internal to the template should use absolute
3194 names.
3195
3196 A template will typically forward some or all variables from the invoking
3197 scope to a target that it defines. Often, such variables might be optional.
3198 Use the pattern:
3199
3200 if (defined(invoker.deps)) {
3201 deps = invoker.deps
3202 }
3203
3204 The function forward_variables_from() provides a shortcut to forward one or
3205 more or possibly all variables in this manner:
3206
3207 forward_variables_from(invoker, ["deps", "public_deps"])
3208```
3209
3210#### **Target naming**
3211
3212```
3213 Your template should almost always define a built-in target with the name the
3214 template invoker specified. For example, if you have an IDL template and
3215 somebody does:
3216 idl("foo") {...
3217 you will normally want this to expand to something defining a source_set or
3218 static_library named "foo" (among other things you may need). This way, when
3219 another target specifies a dependency on "foo", the static_library or
3220 source_set will be linked.
3221
3222 It is also important that any other targets your template expands to have
3223 unique names, or you will get collisions.
3224
3225 Access the invoking name in your template via the implicit "target_name"
3226 variable. This should also be the basis for how other targets that a template
3227 expands to ensure uniqueness.
3228
3229 A typical example would be a template that defines an action to generate some
3230 source files, and a source_set to compile that source. Your template would
3231 name the source_set "target_name" because that's what you want external
3232 targets to depend on to link your code. And you would name the action
3233 something like "${target_name}_action" to make it unique. The source set
3234 would have a dependency on the action to make it run.
3235```
3236
3237#### **Overriding builtin targets**
3238
3239```
3240 You can use template to redefine a built-in target in which case your template
3241 takes a precedence over the built-in one. All uses of the target from within
3242 the template definition will refer to the built-in target which makes it
3243 possible to extend the behavior of the built-in target:
3244
3245 template("shared_library") {
3246 shared_library(shlib) {
Julie Hockette2a29402018-07-31 10:11:42 -07003247 forward_variables_from(invoker, "*")
Brett Wilson796ed472018-07-16 15:11:09 -07003248 ...
3249 }
3250 }
3251```
3252
3253#### **Example of defining a template**
3254
3255```
3256 template("my_idl") {
3257 # Be nice and help callers debug problems by checking that the variables
3258 # the template requires are defined. This gives a nice message rather than
3259 # giving the user an error about an undefined variable in the file defining
3260 # the template
3261 #
3262 # You can also use defined() to give default values to variables
3263 # unspecified by the invoker.
3264 assert(defined(invoker.sources),
3265 "Need sources in $target_name listing the idl files.")
3266
3267 # Name of the intermediate target that does the code gen. This must
3268 # incorporate the target name so it's unique across template
3269 # instantiations.
3270 code_gen_target_name = target_name + "_code_gen"
3271
3272 # Intermediate target to convert IDL to C source. Note that the name is
3273 # based on the name the invoker of the template specified. This way, each
3274 # time the template is invoked we get a unique intermediate action name
3275 # (since all target names are in the global scope).
3276 action_foreach(code_gen_target_name) {
3277 # Access the scope defined by the invoker via the implicit "invoker"
3278 # variable.
3279 sources = invoker.sources
3280
3281 # Note that we need an absolute path for our script file name. The
3282 # current directory when executing this code will be that of the invoker
3283 # (this is why we can use the "sources" directly above without having to
3284 # rebase all of the paths). But if we need to reference a script relative
3285 # to the template file, we'll need to use an absolute path instead.
3286 script = "//tools/idl/idl_code_generator.py"
3287
3288 # Tell GN how to expand output names given the sources.
3289 # See "gn help source_expansion" for more.
3290 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
3291 "$target_gen_dir/{{source_name_part}}.h" ]
3292 }
3293
3294 # Name the source set the same as the template invocation so instancing
3295 # this template produces something that other targets can link to in their
3296 # deps.
3297 source_set(target_name) {
3298 # Generates the list of sources, we get these from the action_foreach
3299 # above.
3300 sources = get_target_outputs(":$code_gen_target_name")
3301
3302 # This target depends on the files produced by the above code gen target.
3303 deps = [ ":$code_gen_target_name" ]
3304 }
3305 }
3306```
3307
3308#### **Example of invoking the resulting template**
3309
3310```
3311 # This calls the template code above, defining target_name to be
3312 # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly
3313 # brackets.
3314 my_idl("foo_idl_files") {
3315 # Goes into the template as "invoker.sources".
3316 sources = [ "foo.idl", "bar.idl" ]
3317 }
3318
3319 # Here is a target that depends on our template.
3320 executable("my_exe") {
3321 # Depend on the name we gave the template call above. Internally, this will
3322 # produce a dependency from executable to the source_set inside the
3323 # template (since it has this name), which will in turn depend on the code
3324 # gen action.
3325 deps = [ ":foo_idl_files" ]
3326 }
3327```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003328### **tool**: Specify arguments to a toolchain tool.
Brett Wilson796ed472018-07-16 15:11:09 -07003329
3330#### **Usage**
3331
3332```
3333 tool() {
3334
3335 }
3336```
3337
3338#### **Tool types**
3339
3340```
3341 Compiler tools:
3342 "cc": C compiler
3343 "cxx": C++ compiler
3344 "objc": Objective C compiler
3345 "objcxx": Objective C++ compiler
3346 "rc": Resource compiler (Windows .rc files)
3347 "asm": Assembler
3348
3349 Linker tools:
3350 "alink": Linker for static libraries (archives)
3351 "solink": Linker for shared libraries
3352 "link": Linker for executables
3353
3354 Other tools:
3355 "stamp": Tool for creating stamp files
3356 "copy": Tool to copy files.
3357 "action": Defaults for actions
3358
3359 Platform specific tools:
3360 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
3361 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
Julie Hockettce1fa072019-05-07 17:44:37 -07003362
3363 Rust tools:
Petr Hosek7617b592019-10-02 09:52:21 -07003364 "rust_bin": Tool for compiling Rust binaries
3365 "rust_cdylib": Tool for compiling C-compatible dynamic libraries.
3366 "rust_dylib": Tool for compiling Rust dynamic libraries.
3367 "rust_macro": Tool for compiling Rust procedural macros.
3368 "rust_rlib": Tool for compiling Rust libraries.
3369 "rust_staticlib": Tool for compiling Rust static libraries.
Brett Wilson796ed472018-07-16 15:11:09 -07003370```
3371
3372#### **Tool variables**
3373
3374```
3375 command [string with substitutions]
3376 Valid for: all tools except "action" (required)
3377
3378 The command to run.
3379
Joe Armstrong23d22bc2019-06-28 15:31:03 +08003380 command_launcher [string]
3381 Valid for: all tools except "action" (optional)
3382
3383 The prefix with which to launch the command (e.g. the path to a Goma or
3384 CCache compiler launcher).
3385
3386 Note that this prefix will not be included in the compilation database or
3387 IDE files generated from the build.
3388
Brett Wilson796ed472018-07-16 15:11:09 -07003389 default_output_dir [string with substitutions]
3390 Valid for: linker tools
3391
3392 Default directory name for the output file relative to the
3393 root_build_dir. It can contain other substitution patterns. This will
3394 be the default value for the {{output_dir}} expansion (discussed below)
3395 but will be overridden by the "output_dir" variable in a target, if one
3396 is specified.
3397
3398 GN doesn't do anything with this string other than pass it along,
3399 potentially with target-specific overrides. It is the tool's job to use
3400 the expansion so that the files will be in the right place.
3401
3402 default_output_extension [string]
3403 Valid for: linker tools
3404
3405 Extension for the main output of a linkable tool. It includes the
3406 leading dot. This will be the default value for the
3407 {{output_extension}} expansion (discussed below) but will be overridden
3408 by by the "output extension" variable in a target, if one is specified.
3409 Empty string means no extension.
3410
3411 GN doesn't actually do anything with this extension other than pass it
3412 along, potentially with target-specific overrides. One would typically
3413 use the {{output_extension}} value in the "outputs" to read this value.
3414
3415 Example: default_output_extension = ".exe"
3416
3417 depfile [string with substitutions]
3418 Valid for: compiler tools (optional)
3419
3420 If the tool can write ".d" files, this specifies the name of the
3421 resulting file. These files are used to list header file dependencies
3422 (or other implicit input dependencies) that are discovered at build
3423 time. See also "depsformat".
3424
3425 Example: depfile = "{{output}}.d"
3426
3427 depsformat [string]
3428 Valid for: compiler tools (when depfile is specified)
3429
3430 Format for the deps outputs. This is either "gcc" or "msvc". See the
3431 ninja documentation for "deps" for more information.
3432
3433 Example: depsformat = "gcc"
3434
3435 description [string with substitutions, optional]
3436 Valid for: all tools
3437
3438 What to print when the command is run.
3439
3440 Example: description = "Compiling {{source}}"
3441
Julie Hockettce1fa072019-05-07 17:44:37 -07003442 exe_output_extension [string, optional, rust tools only]
3443 rlib_output_extension [string, optional, rust tools only]
3444 dylib_output_extension [string, optional, rust tools only]
3445 cdylib_output_extension [string, optional, rust tools only]
Petr Hosekfe36c7c2019-11-26 14:38:08 -08003446 rust_proc_macro_output_extension [string, optional, rust tools only]
Julie Hockettce1fa072019-05-07 17:44:37 -07003447 Valid for: Rust tools
3448
3449 These specify the default tool output for each of the crate types.
3450 The default is empty for executables, shared, and static libraries and
3451 ".rlib" for rlibs. Note that the Rust compiler complains with an error
3452 if external crates do not take the form `lib.rlib` or
3453 `lib.`, where `` is `.so`,
3454 `.dylib`, or `.dll` as appropriate for the platform.
3455
Brett Wilson796ed472018-07-16 15:11:09 -07003456 lib_switch [string, optional, link tools only]
3457 lib_dir_switch [string, optional, link tools only]
3458 Valid for: Linker tools except "alink"
3459
3460 These strings will be prepended to the libraries and library search
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003461 directories, respectively, because linkers differ on how to specify
3462 them.
3463
Brett Wilson796ed472018-07-16 15:11:09 -07003464 If you specified:
3465 lib_switch = "-l"
3466 lib_dir_switch = "-L"
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003467 then the "{{libs}}" expansion for
3468 [ "freetype", "expat" ]
3469 would be
3470 "-lfreetype -lexpat".
3471
3472 framework_switch [string, optional, link tools only]
Robert Sesekd0a6f072020-05-15 11:21:22 -04003473 weak_framework_switch [string, optional, link tools only]
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003474 framework_dir_switch [string, optional, link tools only]
3475 Valid for: Linker tools
3476
3477 These strings will be prepended to the frameworks and framework search
3478 path directories, respectively, because linkers differ on how to specify
3479 them.
3480
3481 If you specified:
3482 framework_switch = "-framework "
Robert Sesekd0a6f072020-05-15 11:21:22 -04003483 weak_framework_switch = "-weak_framework "
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003484 framework_dir_switch = "-F"
Robert Sesekd0a6f072020-05-15 11:21:22 -04003485 and:
3486 framework_dirs = [ "$root_out_dir" ]
3487 frameworks = [ "UIKit.framework", "Foo.framework" ]
3488 weak_frameworks = [ "MediaPlayer.framework" ]
3489 would be:
3490 "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer"
Brett Wilson796ed472018-07-16 15:11:09 -07003491
3492 outputs [list of strings with substitutions]
3493 Valid for: Linker and compiler tools (required)
3494
3495 An array of names for the output files the tool produces. These are
3496 relative to the build output directory. There must always be at least
3497 one output file. There can be more than one output (a linker might
3498 produce a library and an import library, for example).
3499
3500 This array just declares to GN what files the tool will produce. It is
3501 your responsibility to specify the tool command that actually produces
3502 these files.
3503
3504 If you specify more than one output for shared library links, you
3505 should consider setting link_output, depend_output, and
3506 runtime_outputs.
3507
3508 Example for a compiler tool that produces .obj files:
3509 outputs = [
3510 "{{source_out_dir}}/{{source_name_part}}.obj"
3511 ]
3512
3513 Example for a linker tool that produces a .dll and a .lib. The use of
3514 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
3515 the target to override these values.
3516 outputs = [
3517 "{{output_dir}}/{{target_output_name}}"
3518 "{{output_extension}}",
3519 "{{output_dir}}/{{target_output_name}}.lib",
3520 ]
3521
3522 pool [label, optional]
3523 Valid for: all tools (optional)
3524
3525 Label of the pool to use for the tool. Pools are used to limit the
3526 number of tasks that can execute concurrently during the build.
3527
3528 See also "gn help pool".
3529
3530 link_output [string with substitutions]
3531 depend_output [string with substitutions]
3532 Valid for: "solink" only (optional)
3533
3534 These two files specify which of the outputs from the solink tool
3535 should be used for linking and dependency tracking. These should match
3536 entries in the "outputs". If unspecified, the first item in the
3537 "outputs" array will be used for all. See "Separate linking and
3538 dependencies for shared libraries" below for more.
3539
3540 On Windows, where the tools produce a .dll shared library and a .lib
3541 import library, you will want the first two to be the import library
3542 and the third one to be the .dll file. On Linux, if you're not doing
3543 the separate linking/dependency optimization, all of these should be
3544 the .so output.
3545
3546 output_prefix [string]
3547 Valid for: Linker tools (optional)
3548
3549 Prefix to use for the output name. Defaults to empty. This prefix will
3550 be prepended to the name of the target (or the output_name if one is
3551 manually specified for it) if the prefix is not already there. The
3552 result will show up in the {{output_name}} substitution pattern.
3553
3554 Individual targets can opt-out of the output prefix by setting:
3555 output_prefix_override = true
3556 (see "gn help output_prefix_override").
3557
3558 This is typically used to prepend "lib" to libraries on
3559 Posix systems:
3560 output_prefix = "lib"
3561
3562 precompiled_header_type [string]
3563 Valid for: "cc", "cxx", "objc", "objcxx"
3564
3565 Type of precompiled headers. If undefined or the empty string,
3566 precompiled headers will not be used for this tool. Otherwise use "gcc"
3567 or "msvc".
3568
3569 For precompiled headers to be used for a given target, the target (or a
3570 config applied to it) must also specify a "precompiled_header" and, for
3571 "msvc"-style headers, a "precompiled_source" value. If the type is
3572 "gcc", then both "precompiled_header" and "precompiled_source" must
3573 resolve to the same file, despite the different formats required for
3574 each."
3575
3576 See "gn help precompiled_header" for more.
3577
3578 restat [boolean]
3579 Valid for: all tools (optional, defaults to false)
3580
3581 Requests that Ninja check the file timestamp after this tool has run to
3582 determine if anything changed. Set this if your tool has the ability to
3583 skip writing output if the output file has not changed.
3584
3585 Normally, Ninja will assume that when a tool runs the output be new and
3586 downstream dependents must be rebuild. When this is set to trye, Ninja
3587 can skip rebuilding downstream dependents for input changes that don't
3588 actually affect the output.
3589
3590 Example:
3591 restat = true
3592
3593 rspfile [string with substitutions]
3594 Valid for: all tools except "action" (optional)
3595
3596 Name of the response file. If empty, no response file will be
3597 used. See "rspfile_content".
3598
3599 rspfile_content [string with substitutions]
3600 Valid for: all tools except "action" (required when "rspfile" is used)
3601
3602 The contents to be written to the response file. This may include all
3603 or part of the command to send to the tool which allows you to get
3604 around OS command-line length limits.
3605
3606 This example adds the inputs and libraries to a response file, but
3607 passes the linker flags directly on the command line:
3608 tool("link") {
3609 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
3610 rspfile = "{{output}}.rsp"
Adrian Taylorac6cc952020-04-26 20:49:18 -07003611 rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}"
Brett Wilson796ed472018-07-16 15:11:09 -07003612 }
3613
3614 runtime_outputs [string list with substitutions]
3615 Valid for: linker tools
3616
3617 If specified, this list is the subset of the outputs that should be
3618 added to runtime deps (see "gn help runtime_deps"). By default (if
3619 runtime_outputs is empty or unspecified), it will be the link_output.
3620```
3621
3622#### **Expansions for tool variables**
3623
3624```
3625 All paths are relative to the root build directory, which is the current
3626 directory for running all tools. These expansions are available to all tools:
3627
3628 {{label}}
3629 The label of the current target. This is typically used in the
3630 "description" field for link tools. The toolchain will be omitted from
3631 the label for targets in the default toolchain, and will be included
3632 for targets in other toolchains.
3633
3634 {{label_name}}
3635 The short name of the label of the target. This is the part after the
3636 colon. For "//foo/bar:baz" this will be "baz". Unlike
3637 {{target_output_name}}, this is not affected by the "output_prefix" in
3638 the tool or the "output_name" set on the target.
3639
3640 {{output}}
3641 The relative path and name of the output(s) of the current build step.
3642 If there is more than one output, this will expand to a list of all of
3643 them. Example: "out/base/my_file.o"
3644
3645 {{target_gen_dir}}
3646 {{target_out_dir}}
3647 The directory of the generated file and output directories,
3648 respectively, for the current target. There is no trailing slash. See
3649 also {{output_dir}} for linker tools. Example: "out/base/test"
3650
3651 {{target_output_name}}
3652 The short name of the current target with no path information, or the
3653 value of the "output_name" variable if one is specified in the target.
3654 This will include the "output_prefix" if any. See also {{label_name}}.
3655
3656 Example: "libfoo" for the target named "foo" and an output prefix for
3657 the linker tool of "lib".
3658
3659 Compiler tools have the notion of a single input and a single output, along
3660 with a set of compiler-specific flags. The following expansions are
3661 available:
3662
3663 {{asmflags}}
3664 {{cflags}}
3665 {{cflags_c}}
3666 {{cflags_cc}}
3667 {{cflags_objc}}
3668 {{cflags_objcc}}
3669 {{defines}}
3670 {{include_dirs}}
3671 Strings correspond that to the processed flags/defines/include
3672 directories specified for the target.
3673 Example: "--enable-foo --enable-bar"
3674
3675 Defines will be prefixed by "-D" and include directories will be
3676 prefixed by "-I" (these work with Posix tools as well as Microsoft
3677 ones).
3678
3679 {{source}}
3680 The relative path and name of the current input file.
3681 Example: "../../base/my_file.cc"
3682
3683 {{source_file_part}}
3684 The file part of the source including the extension (with no directory
3685 information).
3686 Example: "foo.cc"
3687
3688 {{source_name_part}}
3689 The filename part of the source file with no directory or extension.
3690 Example: "foo"
3691
3692 {{source_gen_dir}}
3693 {{source_out_dir}}
3694 The directory in the generated file and output directories,
3695 respectively, for the current input file. If the source file is in the
3696 same directory as the target is declared in, they will will be the same
3697 as the "target" versions above. Example: "gen/base/test"
3698
Julie Hockette2a29402018-07-31 10:11:42 -07003699 Linker tools have multiple inputs and (potentially) multiple outputs. The
Brett Wilson796ed472018-07-16 15:11:09 -07003700 static library tool ("alink") is not considered a linker tool. The following
3701 expansions are available:
3702
3703 {{inputs}}
3704 {{inputs_newline}}
3705 Expands to the inputs to the link step. This will be a list of object
3706 files and static libraries.
3707 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
3708
3709 The "_newline" version will separate the input files with newlines
3710 instead of spaces. This is useful in response files: some linkers can
3711 take a "-filelist" flag which expects newline separated files, and some
3712 Microsoft tools have a fixed-sized buffer for parsing each line of a
3713 response file.
3714
3715 {{ldflags}}
3716 Expands to the processed set of ldflags and library search paths
3717 specified for the target.
3718 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
3719
3720 {{libs}}
3721 Expands to the list of system libraries to link to. Each will be
3722 prefixed by the "lib_switch".
3723
3724 As a special case to support Mac, libraries with names ending in
3725 ".framework" will be added to the {{libs}} with "-framework" preceding
3726 it, and the lib prefix will be ignored.
3727
3728 Example: "-lfoo -lbar"
3729
3730 {{output_dir}}
3731 The value of the "output_dir" variable in the target, or the the value
3732 of the "default_output_dir" value in the tool if the target does not
3733 override the output directory. This will be relative to the
3734 root_build_dir and will not end in a slash. Will be "." for output to
3735 the root_build_dir.
3736
3737 This is subtly different than {{target_out_dir}} which is defined by GN
3738 based on the target's path and not overridable. {{output_dir}} is for
3739 the final output, {{target_out_dir}} is generally for object files and
3740 other outputs.
3741
3742 Usually {{output_dir}} would be defined in terms of either
3743 {{target_out_dir}} or {{root_out_dir}}
3744
3745 {{output_extension}}
3746 The value of the "output_extension" variable in the target, or the
3747 value of the "default_output_extension" value in the tool if the target
3748 does not specify an output extension.
3749 Example: ".so"
3750
3751 {{solibs}}
3752 Extra libraries from shared library dependencies not specified in the
3753 {{inputs}}. This is the list of link_output files from shared libraries
3754 (if the solink tool specifies a "link_output" variable separate from
3755 the "depend_output").
3756
3757 These should generally be treated the same as libs by your tool.
3758
3759 Example: "libfoo.so libbar.so"
3760
Adrian Taylorac6cc952020-04-26 20:49:18 -07003761 {{rlibs}}
3762 Any Rust .rlibs which need to be linked into a final C++ target.
3763 These should be treated as {{inputs}} except that sometimes
3764 they might have different linker directives applied.
3765
3766 Example: "obj/foo/libfoo.rlib"
3767
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003768 {{frameworks}}
3769 Shared libraries packaged as framework bundle. This is principally
3770 used on Apple's platforms (macOS and iOS). All name must be ending
3771 with ".framework" suffix; the suffix will be stripped when expanding
Robert Sesekd0a6f072020-05-15 11:21:22 -04003772 {{frameworks}} and each item will be preceded by "-framework" or
3773 "-weak_framework".
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003774
Brett Wilson796ed472018-07-16 15:11:09 -07003775 The static library ("alink") tool allows {{arflags}} plus the common tool
3776 substitutions.
3777
3778 The copy tool allows the common compiler/linker substitutions, plus
3779 {{source}} which is the source of the copy. The stamp tool allows only the
3780 common tool substitutions.
3781
3782 The copy_bundle_data and compile_xcassets tools only allows the common tool
3783 substitutions. Both tools are required to create iOS/macOS bundles and need
3784 only be defined on those platforms.
3785
3786 The copy_bundle_data tool will be called with one source and needs to copy
3787 (optionally optimizing the data representation) to its output. It may be
3788 called with a directory as input and it needs to be recursively copied.
3789
3790 The compile_xcassets tool will be called with one or more source (each an
3791 asset catalog) that needs to be compiled to a single output. The following
3792 substitutions are available:
3793
3794 {{inputs}}
3795 Expands to the list of .xcassets to use as input to compile the asset
3796 catalog.
3797
3798 {{bundle_product_type}}
3799 Expands to the product_type of the bundle that will contain the
3800 compiled asset catalog. Usually corresponds to the product_type
3801 property of the corresponding create_bundle target.
3802
3803 {{bundle_partial_info_plist}}
3804 Expands to the path to the partial Info.plist generated by the
3805 assets catalog compiler. Usually based on the target_name of
3806 the create_bundle target.
Julie Hockettce1fa072019-05-07 17:44:37 -07003807
Harley Li0a9affb2020-06-03 10:38:42 -04003808 {{xcasset_compiler_flags}}
3809 Expands to the list of flags specified in corresponding
3810 create_bundle target.
3811
Julie Hockettce1fa072019-05-07 17:44:37 -07003812 Rust tools have the notion of a single input and a single output, along
3813 with a set of compiler-specific flags. The following expansions are
3814 available:
3815
3816 {{crate_name}}
3817 Expands to the string representing the crate name of target under
3818 compilation.
3819
3820 {{crate_type}}
3821 Expands to the string representing the type of crate for the target
3822 under compilation.
3823
3824 {{externs}}
3825 Expands to the list of --extern flags needed to include addition Rust
3826 libraries in this target. Includes any specified renamed dependencies.
3827
Julie Hockettce1fa072019-05-07 17:44:37 -07003828 {{rustdeps}}
3829 Expands to the list of -Ldependency= strings needed to compile
3830 this target.
3831
3832 {{rustenv}}
3833 Expands to the list of environment variables.
3834
3835 {{rustflags}}
3836 Expands to the list of strings representing Rust compiler flags.
Brett Wilson796ed472018-07-16 15:11:09 -07003837```
3838
3839#### **Separate linking and dependencies for shared libraries**
3840
3841```
3842 Shared libraries are special in that not all changes to them require that
3843 dependent targets be re-linked. If the shared library is changed but no
3844 imports or exports are different, dependent code needn't be relinked, which
3845 can speed up the build.
3846
3847 If your link step can output a list of exports from a shared library and
3848 writes the file only if the new one is different, the timestamp of this file
3849 can be used for triggering re-links, while the actual shared library would be
3850 used for linking.
3851
3852 You will need to specify
3853 restat = true
3854 in the linker tool to make this work, so Ninja will detect if the timestamp
3855 of the dependency file has changed after linking (otherwise it will always
3856 assume that running a command updates the output):
3857
3858 tool("solink") {
3859 command = "..."
3860 outputs = [
3861 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
3862 "{{output_dir}}/{{target_output_name}}"
3863 "{{output_extension}}.TOC",
3864 ]
3865 link_output =
3866 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
3867 depend_output =
3868 "{{output_dir}}/{{target_output_name}}"
3869 "{{output_extension}}.TOC"
3870 restat = true
3871 }
3872```
3873
3874#### **Example**
3875
3876```
3877 toolchain("my_toolchain") {
3878 # Put these at the top to apply to all tools below.
3879 lib_switch = "-l"
3880 lib_dir_switch = "-L"
3881
3882 tool("cc") {
3883 command = "gcc {{source}} -o {{output}}"
3884 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
3885 description = "GCC {{source}}"
3886 }
3887 tool("cxx") {
3888 command = "g++ {{source}} -o {{output}}"
3889 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
3890 description = "G++ {{source}}"
3891 }
3892 };
3893```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003894### **toolchain**: Defines a toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07003895
3896```
3897 A toolchain is a set of commands and build flags used to compile the source
3898 code. The toolchain() function defines these commands.
3899```
3900
3901#### **Toolchain overview**
3902
3903```
3904 You can have more than one toolchain in use at once in a build and a target
3905 can exist simultaneously in multiple toolchains. A build file is executed
3906 once for each toolchain it is referenced in so the GN code can vary all
3907 parameters of each target (or which targets exist) on a per-toolchain basis.
3908
3909 When you have a simple build with only one toolchain, the build config file
3910 is loaded only once at the beginning of the build. It must call
3911 set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the
3912 label of the toolchain definition to use. The "toolchain_args" section of the
3913 toolchain definition is ignored.
3914
3915 When a target has a dependency on a target using different toolchain (see "gn
3916 help labels" for how to specify this), GN will start a build using that
3917 secondary toolchain to resolve the target. GN will load the build config file
3918 with the build arguments overridden as specified in the toolchain_args.
3919 Because the default toolchain is already known, calls to
3920 set_default_toolchain() are ignored.
3921
3922 To load a file in an alternate toolchain, GN does the following:
3923
3924 1. Loads the file with the toolchain definition in it (as determined by the
3925 toolchain label).
3926 2. Re-runs the master build configuration file, applying the arguments
3927 specified by the toolchain_args section of the toolchain definition.
3928 3. Loads the destination build file in the context of the configuration file
3929 in the previous step.
3930
3931 The toolchain configuration is two-way. In the default toolchain (i.e. the
3932 main build target) the configuration flows from the build config file to the
3933 toolchain. The build config file looks at the state of the build (OS type,
3934 CPU architecture, etc.) and decides which toolchain to use (via
3935 set_default_toolchain()). In secondary toolchains, the configuration flows
3936 from the toolchain to the build config file: the "toolchain_args" in the
3937 toolchain definition specifies the arguments to re-invoke the build.
3938```
3939
3940#### **Functions and variables**
3941
3942```
3943 tool()
Julie Hockette2a29402018-07-31 10:11:42 -07003944 The tool() function call specifies the commands to run for a given step. See
3945 "gn help tool".
Brett Wilson796ed472018-07-16 15:11:09 -07003946
Julie Hockette2a29402018-07-31 10:11:42 -07003947 toolchain_args [scope]
Brett Wilson796ed472018-07-16 15:11:09 -07003948 Overrides for build arguments to pass to the toolchain when invoking it.
3949 This is a variable of type "scope" where the variable names correspond to
3950 variables in declare_args() blocks.
3951
3952 When you specify a target using an alternate toolchain, the master build
3953 configuration file is re-interpreted in the context of that toolchain.
3954 toolchain_args allows you to control the arguments passed into this
3955 alternate invocation of the build.
3956
3957 Any default system arguments or arguments passed in via "gn args" will also
3958 be passed to the alternate invocation unless explicitly overridden by
3959 toolchain_args.
3960
3961 The toolchain_args will be ignored when the toolchain being defined is the
3962 default. In this case, it's expected you want the default argument values.
3963
3964 See also "gn help buildargs" for an overview of these arguments.
3965
Julie Hockette2a29402018-07-31 10:11:42 -07003966 propagates_configs [boolean, default=false]
3967 Determines whether public_configs and all_dependent_configs in this
3968 toolchain propagate to targets in other toolchains.
3969
3970 When false (the default), this toolchain will not propagate any configs to
3971 targets in other toolchains that depend on it targets inside this
3972 toolchain. This matches the most common usage of toolchains where they
3973 represent different architectures or compilers and the settings that apply
3974 to one won't necessarily apply to others.
3975
3976 When true, configs (public and all-dependent) will cross the boundary out
3977 of this toolchain as if the toolchain boundary wasn't there. This only
3978 affects one direction of dependencies: a toolchain can't control whether
3979 it accepts such configs, only whether it pushes them. The build is
3980 responsible for ensuring that any external targets depending on targets in
3981 this toolchain are compatible with the compiler flags, etc. that may be
3982 propagated.
3983
3984 deps [string list]
Brett Wilson796ed472018-07-16 15:11:09 -07003985 Dependencies of this toolchain. These dependencies will be resolved before
3986 any target in the toolchain is compiled. To avoid circular dependencies
3987 these must be targets defined in another toolchain.
3988
3989 This is expressed as a list of targets, and generally these targets will
3990 always specify a toolchain:
3991 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
3992
3993 This concept is somewhat inefficient to express in Ninja (it requires a lot
3994 of duplicate of rules) so should only be used when absolutely necessary.
3995```
3996
3997#### **Example of defining a toolchain**
3998
3999```
4000 toolchain("32") {
4001 tool("cc") {
4002 command = "gcc {{source}}"
4003 ...
4004 }
4005
4006 toolchain_args = {
4007 use_doom_melon = true # Doom melon always required for 32-bit builds.
4008 current_cpu = "x86"
4009 }
4010 }
4011
4012 toolchain("64") {
4013 tool("cc") {
4014 command = "gcc {{source}}"
4015 ...
4016 }
4017
4018 toolchain_args = {
4019 # use_doom_melon is not overridden here, it will take the default.
4020 current_cpu = "x64"
4021 }
4022 }
4023```
4024
4025#### **Example of cross-toolchain dependencies**
4026
4027```
4028 If a 64-bit target wants to depend on a 32-bit binary, it would specify a
4029 dependency using data_deps (data deps are like deps that are only needed at
4030 runtime and aren't linked, since you can't link a 32-bit and a 64-bit
4031 library).
4032
4033 executable("my_program") {
4034 ...
4035 if (target_cpu == "x64") {
4036 # The 64-bit build needs this 32-bit helper.
4037 data_deps = [ ":helper(//toolchains:32)" ]
4038 }
4039 }
4040
4041 if (target_cpu == "x86") {
4042 # Our helper library is only compiled in 32-bits.
4043 shared_library("helper") {
4044 ...
4045 }
4046 }
4047```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004048### **write_file**: Write a file to disk.
Brett Wilson796ed472018-07-16 15:11:09 -07004049
4050```
Julie Hockett09171292018-07-31 14:35:10 -07004051 write_file(filename, data, output_conversion = "")
Brett Wilson796ed472018-07-16 15:11:09 -07004052
4053 If data is a list, the list will be written one-item-per-line with no quoting
4054 or brackets.
4055
4056 If the file exists and the contents are identical to that being written, the
4057 file will not be updated. This will prevent unnecessary rebuilds of targets
4058 that depend on this file.
4059
4060 One use for write_file is to write a list of inputs to an script that might
4061 be too long for the command line. However, it is preferable to use response
4062 files for this purpose. See "gn help response_file_contents".
Brett Wilson796ed472018-07-16 15:11:09 -07004063```
4064
4065#### **Arguments**
4066
4067```
4068 filename
4069 Filename to write. This must be within the output directory.
4070
4071 data
4072 The list or string to write.
Julie Hockett09171292018-07-31 14:35:10 -07004073
4074 output_conversion
Julie Hockett81ee1962019-05-13 11:24:42 -07004075 Controls how the output is written. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07004076```
4077## Built-in predefined variables
4078
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004079### **current_cpu**: The processor architecture of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004080
4081```
4082 The build configuration usually sets this value based on the value of
4083 "host_cpu" (see "gn help host_cpu") and then threads this through the
4084 toolchain definitions to ensure that it always reflects the appropriate
4085 value.
4086
Julie Hockette2a29402018-07-31 10:11:42 -07004087 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004088 empty string ("") by default but is declared so that it can be overridden on
4089 the command line if so desired.
4090
4091 See "gn help target_cpu" for a list of common values returned.
4092```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004093### **current_os**: The operating system of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004094
4095```
4096 The build configuration usually sets this value based on the value of
4097 "target_os" (see "gn help target_os"), and then threads this through the
4098 toolchain definitions to ensure that it always reflects the appropriate
4099 value.
4100
Julie Hockette2a29402018-07-31 10:11:42 -07004101 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004102 empty string ("") by default but is declared so that it can be overridden on
4103 the command line if so desired.
4104
4105 See "gn help target_os" for a list of common values returned.
4106```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004107### **current_toolchain**: Label of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004108
4109```
4110 A fully-qualified label representing the current toolchain. You can use this
4111 to make toolchain-related decisions in the build. See also
4112 "default_toolchain".
4113```
4114
4115#### **Example**
4116
4117```
4118 if (current_toolchain == "//build:64_bit_toolchain") {
4119 executable("output_thats_64_bit_only") {
4120 ...
4121```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004122### **default_toolchain**: [string] Label of the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004123
4124```
4125 A fully-qualified label representing the default toolchain, which may not
4126 necessarily be the current one (see "current_toolchain").
4127```
James Robinson61377e32020-02-13 15:20:07 -08004128### **gn_version**: [number] The version of gn.
4129
4130```
4131 Corresponds to the number printed by `gn --version`.
4132```
4133
4134#### **Example**
4135
4136```
4137 assert(gn_version >= 1700, "need GN version 1700 for the frobulate feature")
4138```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004139### **host_cpu**: The processor architecture that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004140
4141```
4142 This is value is exposed so that cross-compile toolchains can access the host
4143 architecture when needed.
4144
4145 The value should generally be considered read-only, but it can be overriden
4146 in order to handle unusual cases where there might be multiple plausible
4147 values for the host architecture (e.g., if you can do either 32-bit or 64-bit
4148 builds). The value is not used internally by GN for any purpose.
4149```
4150
4151#### **Some possible values**
4152
4153```
4154 - "x64"
4155 - "x86"
4156```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004157### **host_os**: [string] The operating system that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004158
4159```
4160 This value is exposed so that cross-compiles can access the host build
4161 system's settings.
4162
4163 This value should generally be treated as read-only. It, however, is not used
4164 internally by GN for any purpose.
4165```
4166
4167#### **Some possible values**
4168
4169```
4170 - "linux"
4171 - "mac"
4172 - "win"
4173```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004174### **invoker**: [string] The invoking scope inside a template.
Brett Wilson796ed472018-07-16 15:11:09 -07004175
4176```
4177 Inside a template invocation, this variable refers to the scope of the
4178 invoker of the template. Outside of template invocations, this variable is
4179 undefined.
4180
4181 All of the variables defined inside the template invocation are accessible as
4182 members of the "invoker" scope. This is the way that templates read values
4183 set by the callers.
4184
4185 This is often used with "defined" to see if a value is set on the invoking
4186 scope.
4187
4188 See "gn help template" for more examples.
4189```
4190
4191#### **Example**
4192
4193```
4194 template("my_template") {
4195 print(invoker.sources) # Prints [ "a.cc", "b.cc" ]
4196 print(defined(invoker.foo)) # Prints false.
4197 print(defined(invoker.bar)) # Prints true.
4198 }
4199
4200 my_template("doom_melon") {
4201 sources = [ "a.cc", "b.cc" ]
4202 bar = 123
4203 }
4204```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004205### **python_path**: Absolute path of Python.
Brett Wilson796ed472018-07-16 15:11:09 -07004206
4207```
4208 Normally used in toolchain definitions if running some command requires
4209 Python. You will normally not need this when invoking scripts since GN
4210 automatically finds it for you.
4211```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004212### **root_build_dir**: [string] Directory where build commands are run.
Brett Wilson796ed472018-07-16 15:11:09 -07004213
4214```
4215 This is the root build output directory which will be the current directory
4216 when executing all compilers and scripts.
4217
4218 Most often this is used with rebase_path (see "gn help rebase_path") to
4219 convert arguments to be relative to a script's current directory.
4220```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004221### **root_gen_dir**: Directory for the toolchain's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004222
4223```
4224 Absolute path to the root of the generated output directory tree for the
4225 current toolchain. An example would be "//out/Debug/gen" for the default
4226 toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain.
4227
4228 This is primarily useful for setting up include paths for generated files. If
4229 you are passing this to a script, you will want to pass it through
4230 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4231 build directory.
4232
4233 See also "target_gen_dir" which is usually a better location for generated
4234 files. It will be inside the root generated dir.
4235```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004236### **root_out_dir**: [string] Root directory for toolchain output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004237
4238```
4239 Absolute path to the root of the output directory tree for the current
4240 toolchain. It will not have a trailing slash.
4241
4242 For the default toolchain this will be the same as the root_build_dir. An
4243 example would be "//out/Debug" for the default toolchain, or
4244 "//out/Debug/arm" for the "arm" toolchain.
4245
4246 This is primarily useful for setting up script calls. If you are passing this
4247 to a script, you will want to pass it through rebase_path() (see "gn help
4248 rebase_path") to convert it to be relative to the build directory.
4249
4250 See also "target_out_dir" which is usually a better location for output
4251 files. It will be inside the root output dir.
4252```
4253
4254#### **Example**
4255
4256```
4257 action("myscript") {
4258 # Pass the output dir to the script.
4259 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
4260 }
4261```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004262### **target_cpu**: The desired cpu architecture for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004263
4264```
4265 This value should be used to indicate the desired architecture for the
4266 primary objects of the build. It will match the cpu architecture of the
4267 default toolchain, but not necessarily the current toolchain.
4268
4269 In many cases, this is the same as "host_cpu", but in the case of
4270 cross-compiles, this can be set to something different. This value is
4271 different from "current_cpu" in that it does not change based on the current
4272 toolchain. When writing rules, "current_cpu" should be used rather than
4273 "target_cpu" most of the time.
4274
4275 This value is not used internally by GN for any purpose, so it may be set to
4276 whatever value is needed for the build. GN defaults this value to the empty
4277 string ("") and the configuration files should set it to an appropriate value
4278 (e.g., setting it to the value of "host_cpu") if it is not overridden on the
4279 command line or in the args.gn file.
4280```
4281
4282#### **Possible values**
4283
4284```
4285 - "x86"
4286 - "x64"
4287 - "arm"
4288 - "arm64"
4289 - "mipsel"
4290```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004291### **target_gen_dir**: Directory for a target's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004292
4293```
4294 Absolute path to the target's generated file directory. This will be the
4295 "root_gen_dir" followed by the relative path to the current build file. If
4296 your file is in "//tools/doom_melon" then target_gen_dir would be
4297 "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash.
4298
4299 This is primarily useful for setting up include paths for generated files. If
4300 you are passing this to a script, you will want to pass it through
4301 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4302 build directory.
4303
4304 See also "gn help root_gen_dir".
4305```
4306
4307#### **Example**
4308
4309```
4310 action("myscript") {
4311 # Pass the generated output dir to the script.
4312 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]"
4313 }
4314```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004315### **target_name**: [string] The name of the current target.
Brett Wilson796ed472018-07-16 15:11:09 -07004316
4317```
4318 Inside a target or template invocation, this variable refers to the name
4319 given to the target or template invocation. Outside of these, this variable
4320 is undefined.
4321
4322 This is most often used in template definitions to name targets defined in
4323 the template based on the name of the invocation. This is necessary both to
4324 ensure generated targets have unique names and to generate a target with the
4325 exact name of the invocation that other targets can depend on.
4326
4327 Be aware that this value will always reflect the innermost scope. So when
4328 defining a target inside a template, target_name will refer to the target
4329 rather than the template invocation. To get the name of the template
4330 invocation in this case, you should save target_name to a temporary variable
4331 outside of any target definitions.
4332
4333 See "gn help template" for more examples.
4334```
4335
4336#### **Example**
4337
4338```
4339 executable("doom_melon") {
4340 print(target_name) # Prints "doom_melon".
4341 }
4342
4343 template("my_template") {
4344 print(target_name) # Prints "space_ray" when invoked below.
4345
4346 executable(target_name + "_impl") {
4347 print(target_name) # Prints "space_ray_impl".
4348 }
4349 }
4350
4351 my_template("space_ray") {
4352 }
4353```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004354### **target_os**: The desired operating system for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004355
4356```
4357 This value should be used to indicate the desired operating system for the
4358 primary object(s) of the build. It will match the OS of the default
4359 toolchain.
4360
4361 In many cases, this is the same as "host_os", but in the case of
4362 cross-compiles, it may be different. This variable differs from "current_os"
4363 in that it can be referenced from inside any toolchain and will always return
4364 the initial value.
4365
4366 This should be set to the most specific value possible. So, "android" or
4367 "chromeos" should be used instead of "linux" where applicable, even though
4368 Android and ChromeOS are both Linux variants. This can mean that one needs to
4369 write
4370
4371 if (target_os == "android" || target_os == "linux") {
4372 # ...
4373 }
4374
4375 and so forth.
4376
4377 This value is not used internally by GN for any purpose, so it may be set to
4378 whatever value is needed for the build. GN defaults this value to the empty
4379 string ("") and the configuration files should set it to an appropriate value
4380 (e.g., setting it to the value of "host_os") if it is not set via the command
4381 line or in the args.gn file.
4382```
4383
4384#### **Possible values**
4385
4386```
4387 - "android"
4388 - "chromeos"
4389 - "ios"
4390 - "linux"
4391 - "nacl"
4392 - "mac"
4393 - "win"
4394```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004395### **target_out_dir**: [string] Directory for target output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004396
4397```
4398 Absolute path to the target's generated file directory. If your current
4399 target is in "//tools/doom_melon" then this value might be
4400 "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash.
4401
4402 This is primarily useful for setting up arguments for calling scripts. If you
4403 are passing this to a script, you will want to pass it through rebase_path()
4404 (see "gn help rebase_path") to convert it to be relative to the build
4405 directory.
4406
4407 See also "gn help root_out_dir".
4408```
4409
4410#### **Example**
4411
4412```
4413 action("myscript") {
4414 # Pass the output dir to the script.
4415 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]"
Brett Wilson796ed472018-07-16 15:11:09 -07004416 }
4417```
4418## Variables you set in targets
4419
Julie Hockettce1fa072019-05-07 17:44:37 -07004420### **aliased_deps**: [scope] Set of crate-dependency pairs.
4421
4422```
4423 Valid for `rust_library` targets and `executable`, `static_library`, and
4424 `shared_library` targets that contain Rust sources.
4425
4426 A scope, each key indicating the renamed crate and the corresponding value
4427 specifying the label of the dependency producing the relevant binary.
4428
4429 All dependencies listed in this field *must* be listed as deps of the target.
4430
4431 executable("foo") {
4432 sources = [ "main.rs" ]
4433 deps = [ "//bar" ]
4434 }
4435
4436 This target would compile the `foo` crate with the following `extern` flag:
4437 `rustc ...command... --extern bar=/obj/bar`
4438
4439 executable("foo") {
4440 sources = [ "main.rs" ]
4441 deps = [ ":bar" ]
4442 aliased_deps = {
4443 bar_renamed = ":bar"
4444 }
4445 }
4446
4447 With the addition of `aliased_deps`, above target would instead compile with:
4448 `rustc ...command... --extern bar_renamed=/obj/bar`
4449```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004450### **all_dependent_configs**: Configs to be forced on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07004451
4452```
4453 A list of config labels.
4454
4455 All targets depending on this one, and recursively, all targets depending on
4456 those, will have the configs listed in this variable added to them. These
4457 configs will also apply to the current target.
4458
4459 This addition happens in a second phase once a target and all of its
4460 dependencies have been resolved. Therefore, a target will not see these
4461 force-added configs in their "configs" variable while the script is running,
4462 and they can not be removed. As a result, this capability should generally
4463 only be used to add defines and include directories necessary to compile a
4464 target's headers.
4465
4466 See also "public_configs".
4467```
4468
4469#### **Ordering of flags and values**
4470
4471```
4472 1. Those set on the current target (not in a config).
4473 2. Those set on the "configs" on the target in order that the
4474 configs appear in the list.
4475 3. Those set on the "all_dependent_configs" on the target in order
4476 that the configs appear in the list.
4477 4. Those set on the "public_configs" on the target in order that
4478 those configs appear in the list.
4479 5. all_dependent_configs pulled from dependencies, in the order of
4480 the "deps" list. This is done recursively. If a config appears
4481 more than once, only the first occurence will be used.
4482 6. public_configs pulled from dependencies, in the order of the
4483 "deps" list. If a dependency is public, they will be applied
4484 recursively.
4485```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004486### **allow_circular_includes_from**: Permit includes from deps.
Brett Wilson796ed472018-07-16 15:11:09 -07004487
4488```
4489 A list of target labels. Must be a subset of the target's "deps". These
4490 targets will be permitted to include headers from the current target despite
4491 the dependency going in the opposite direction.
4492
4493 When you use this, both targets must be included in a final binary for it to
4494 link. To keep linker errors from happening, it is good practice to have all
4495 external dependencies depend only on one of the two targets, and to set the
4496 visibility on the other to enforce this. Thus the targets will always be
4497 linked together in any output.
4498```
4499
4500#### **Details**
4501
4502```
4503 Normally, for a file in target A to include a file from target B, A must list
4504 B as a dependency. This invariant is enforced by the "gn check" command (and
4505 the --check flag to "gn gen" -- see "gn help check").
4506
4507 Sometimes, two targets might be the same unit for linking purposes (two
4508 source sets or static libraries that would always be linked together in a
4509 final executable or shared library) and they each include headers from the
4510 other: you want A to be able to include B's headers, and B to include A's
4511 headers. This is not an ideal situation but is sometimes unavoidable.
4512
4513 This list, if specified, lists which of the dependencies of the current
4514 target can include header files from the current target. That is, if A
4515 depends on B, B can only include headers from A if it is in A's
4516 allow_circular_includes_from list. Normally includes must follow the
4517 direction of dependencies, this flag allows them to go in the opposite
4518 direction.
4519```
4520
4521#### **Danger**
4522
4523```
4524 In the above example, A's headers are likely to include headers from A's
4525 dependencies. Those dependencies may have public_configs that apply flags,
4526 defines, and include paths that make those headers work properly.
4527
4528 With allow_circular_includes_from, B can include A's headers, and
4529 transitively from A's dependencies, without having the dependencies that
4530 would bring in the public_configs those headers need. The result may be
4531 errors or inconsistent builds.
4532
4533 So when you use allow_circular_includes_from, make sure that any compiler
4534 settings, flags, and include directories are the same between both targets
4535 (consider putting such things in a shared config they can both reference).
4536 Make sure the dependencies are also the same (you might consider a group to
4537 collect such dependencies they both depend on).
4538```
4539
4540#### **Example**
4541
4542```
4543 source_set("a") {
4544 deps = [ ":b", ":a_b_shared_deps" ]
4545 allow_circular_includes_from = [ ":b" ]
4546 ...
4547 }
4548
4549 source_set("b") {
4550 deps = [ ":a_b_shared_deps" ]
4551 # Sources here can include headers from a despite lack of deps.
4552 ...
4553 }
4554
4555 group("a_b_shared_deps") {
4556 public_deps = [ ":c" ]
4557 }
4558```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004559### **arflags**: Arguments passed to static_library archiver.
Brett Wilson796ed472018-07-16 15:11:09 -07004560
4561```
4562 A list of flags passed to the archive/lib command that creates static
4563 libraries.
4564
4565 arflags are NOT pushed to dependents, so applying arflags to source sets or
4566 any other target type will be a no-op. As with ldflags, you could put the
4567 arflags in a config and set that as a public or "all dependent" config, but
4568 that will likely not be what you want. If you have a chain of static
4569 libraries dependent on each other, this can cause the flags to propagate up
4570 to other static libraries. Due to the nature of how arflags are typically
4571 used, you will normally want to apply them directly on static_library targets
4572 themselves.
4573```
4574
4575#### **Ordering of flags and values**
4576
4577```
4578 1. Those set on the current target (not in a config).
4579 2. Those set on the "configs" on the target in order that the
4580 configs appear in the list.
4581 3. Those set on the "all_dependent_configs" on the target in order
4582 that the configs appear in the list.
4583 4. Those set on the "public_configs" on the target in order that
4584 those configs appear in the list.
4585 5. all_dependent_configs pulled from dependencies, in the order of
4586 the "deps" list. This is done recursively. If a config appears
4587 more than once, only the first occurence will be used.
4588 6. public_configs pulled from dependencies, in the order of the
4589 "deps" list. If a dependency is public, they will be applied
4590 recursively.
4591```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004592### **args**: (target variable) Arguments passed to an action.
Brett Wilson796ed472018-07-16 15:11:09 -07004593
4594```
4595 For action and action_foreach targets, args is the list of arguments to pass
4596 to the script. Typically you would use source expansion (see "gn help
4597 source_expansion") to insert the source file names.
4598
4599 See also "gn help action" and "gn help action_foreach".
4600```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004601### **asmflags**: Flags passed to the assembler.
Brett Wilson796ed472018-07-16 15:11:09 -07004602
4603```
4604 A list of strings.
4605
4606 "asmflags" are passed to any invocation of a tool that takes an .asm or .S
4607 file as input.
4608```
4609
4610#### **Ordering of flags and values**
4611
4612```
4613 1. Those set on the current target (not in a config).
4614 2. Those set on the "configs" on the target in order that the
4615 configs appear in the list.
4616 3. Those set on the "all_dependent_configs" on the target in order
4617 that the configs appear in the list.
4618 4. Those set on the "public_configs" on the target in order that
4619 those configs appear in the list.
4620 5. all_dependent_configs pulled from dependencies, in the order of
4621 the "deps" list. This is done recursively. If a config appears
4622 more than once, only the first occurence will be used.
4623 6. public_configs pulled from dependencies, in the order of the
4624 "deps" list. If a dependency is public, they will be applied
4625 recursively.
4626```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004627### **assert_no_deps**: Ensure no deps on these targets.
Brett Wilson796ed472018-07-16 15:11:09 -07004628
4629```
4630 A list of label patterns.
4631
4632 This list is a list of patterns that must not match any of the transitive
4633 dependencies of the target. These include all public, private, and data
4634 dependencies, and cross shared library boundaries. This allows you to express
4635 that undesirable code isn't accidentally added to downstream dependencies in
4636 a way that might otherwise be difficult to notice.
4637
4638 Checking does not cross executable boundaries. If a target depends on an
4639 executable, it's assumed that the executable is a tool that is producing part
4640 of the build rather than something that is linked and distributed. This
4641 allows assert_no_deps to express what is distributed in the final target
4642 rather than depend on the internal build steps (which may include
4643 non-distributable code).
4644
4645 See "gn help label_pattern" for the format of the entries in the list. These
4646 patterns allow blacklisting individual targets or whole directory
4647 hierarchies.
4648
4649 Sometimes it is desirable to enforce that many targets have no dependencies
4650 on a target or set of targets. One efficient way to express this is to create
4651 a group with the assert_no_deps rule on it, and make that group depend on all
4652 targets you want to apply that assertion to.
4653```
4654
4655#### **Example**
4656
4657```
4658 executable("doom_melon") {
4659 deps = [ "//foo:bar" ]
4660 ...
4661 assert_no_deps = [
4662 "//evil/*", # Don't link any code from the evil directory.
4663 "//foo:test_support", # This target is also disallowed.
4664 ]
4665 }
4666```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004667### **bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in
Brett Wilson796ed472018-07-16 15:11:09 -07004668```
4669 create_bundle.
4670
4671 A string corresponding to a path in $root_build_dir.
4672
4673 This string is used by the "create_bundle" target to expand the
4674 {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must
4675 correspond to a path under "bundle_root_dir".
4676
4677 See "gn help bundle_root_dir" for examples.
4678```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004679### **bundle_deps_filter**: [label list] A list of labels that are filtered out.
Brett Wilson796ed472018-07-16 15:11:09 -07004680
4681```
4682 A list of target labels.
4683
4684 This list contains target label patterns that should be filtered out when
4685 creating the bundle. Any target matching one of those label will be removed
4686 from the dependencies of the create_bundle target.
4687
4688 This is mostly useful when creating application extension bundle as the
4689 application extension has access to runtime resources from the application
4690 bundle and thus do not require a second copy.
4691
4692 See "gn help create_bundle" for more information.
4693```
4694
4695#### **Example**
4696
4697```
4698 create_bundle("today_extension") {
4699 deps = [
4700 "//base"
4701 ]
4702 bundle_root_dir = "$root_out_dir/today_extension.appex"
4703 bundle_deps_filter = [
4704 # The extension uses //base but does not use any function calling into
4705 # third_party/icu and thus does not need the icudtl.dat file.
4706 "//third_party/icu:icudata",
4707 ]
4708 }
4709```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004710### **bundle_executable_dir**
Brett Wilson796ed472018-07-16 15:11:09 -07004711
Joe Armstrongb199e542019-03-21 09:40:08 +08004712```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004713 bundle_executable_dir: Expansion of {{bundle_executable_dir}} in
4714 create_bundle.
4715
Brett Wilson796ed472018-07-16 15:11:09 -07004716 A string corresponding to a path in $root_build_dir.
4717
4718 This string is used by the "create_bundle" target to expand the
4719 {{bundle_executable_dir}} of the "bundle_data" target it depends on. This
4720 must correspond to a path under "bundle_root_dir".
4721
4722 See "gn help bundle_root_dir" for examples.
4723```
Joe Armstrongb199e542019-03-21 09:40:08 +08004724### **bundle_resources_dir**
4725
Brett Wilson796ed472018-07-16 15:11:09 -07004726```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004727 bundle_resources_dir: Expansion of {{bundle_resources_dir}} in
4728 create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07004729
4730 A string corresponding to a path in $root_build_dir.
4731
4732 This string is used by the "create_bundle" target to expand the
4733 {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must
4734 correspond to a path under "bundle_root_dir".
4735
4736 See "gn help bundle_root_dir" for examples.
4737```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004738### **bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07004739
4740```
4741 A string corresponding to a path in root_build_dir.
4742
4743 This string is used by the "create_bundle" target to expand the
4744 {{bundle_root_dir}} of the "bundle_data" target it depends on. This must
4745 correspond to a path under root_build_dir.
4746```
4747
4748#### **Example**
4749
4750```
4751 bundle_data("info_plist") {
4752 sources = [ "Info.plist" ]
4753 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
4754 }
4755
4756 create_bundle("doom_melon.app") {
4757 deps = [ ":info_plist" ]
4758 bundle_root_dir = "${root_build_dir}/doom_melon.app"
4759 bundle_contents_dir = "${bundle_root_dir}/Contents"
4760 bundle_resources_dir = "${bundle_contents_dir}/Resources"
4761 bundle_executable_dir = "${bundle_contents_dir}/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07004762 }
4763```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004764### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004765
4766```
4767 A list of strings.
4768
4769 "cflags" are passed to all invocations of the C, C++, Objective C, and
4770 Objective C++ compilers.
4771
4772 To target one of these variants individually, use "cflags_c", "cflags_cc",
4773 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4774 versions of cflags* will be appended on the compiler command line after
4775 "cflags".
4776
4777 See also "asmflags" for flags for assembly-language files.
4778```
4779
4780#### **Ordering of flags and values**
4781
4782```
4783 1. Those set on the current target (not in a config).
4784 2. Those set on the "configs" on the target in order that the
4785 configs appear in the list.
4786 3. Those set on the "all_dependent_configs" on the target in order
4787 that the configs appear in the list.
4788 4. Those set on the "public_configs" on the target in order that
4789 those configs appear in the list.
4790 5. all_dependent_configs pulled from dependencies, in the order of
4791 the "deps" list. This is done recursively. If a config appears
4792 more than once, only the first occurence will be used.
4793 6. public_configs pulled from dependencies, in the order of the
4794 "deps" list. If a dependency is public, they will be applied
4795 recursively.
4796```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004797### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004798
4799```
4800 A list of strings.
4801
4802 "cflags" are passed to all invocations of the C, C++, Objective C, and
4803 Objective C++ compilers.
4804
4805 To target one of these variants individually, use "cflags_c", "cflags_cc",
4806 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4807 versions of cflags* will be appended on the compiler command line after
4808 "cflags".
4809
4810 See also "asmflags" for flags for assembly-language files.
4811```
4812
4813#### **Ordering of flags and values**
4814
4815```
4816 1. Those set on the current target (not in a config).
4817 2. Those set on the "configs" on the target in order that the
4818 configs appear in the list.
4819 3. Those set on the "all_dependent_configs" on the target in order
4820 that the configs appear in the list.
4821 4. Those set on the "public_configs" on the target in order that
4822 those configs appear in the list.
4823 5. all_dependent_configs pulled from dependencies, in the order of
4824 the "deps" list. This is done recursively. If a config appears
4825 more than once, only the first occurence will be used.
4826 6. public_configs pulled from dependencies, in the order of the
4827 "deps" list. If a dependency is public, they will be applied
4828 recursively.
4829```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004830### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004831
4832```
4833 A list of strings.
4834
4835 "cflags" are passed to all invocations of the C, C++, Objective C, and
4836 Objective C++ compilers.
4837
4838 To target one of these variants individually, use "cflags_c", "cflags_cc",
4839 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4840 versions of cflags* will be appended on the compiler command line after
4841 "cflags".
4842
4843 See also "asmflags" for flags for assembly-language files.
4844```
4845
4846#### **Ordering of flags and values**
4847
4848```
4849 1. Those set on the current target (not in a config).
4850 2. Those set on the "configs" on the target in order that the
4851 configs appear in the list.
4852 3. Those set on the "all_dependent_configs" on the target in order
4853 that the configs appear in the list.
4854 4. Those set on the "public_configs" on the target in order that
4855 those configs appear in the list.
4856 5. all_dependent_configs pulled from dependencies, in the order of
4857 the "deps" list. This is done recursively. If a config appears
4858 more than once, only the first occurence will be used.
4859 6. public_configs pulled from dependencies, in the order of the
4860 "deps" list. If a dependency is public, they will be applied
4861 recursively.
4862```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004863### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004864
4865```
4866 A list of strings.
4867
4868 "cflags" are passed to all invocations of the C, C++, Objective C, and
4869 Objective C++ compilers.
4870
4871 To target one of these variants individually, use "cflags_c", "cflags_cc",
4872 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4873 versions of cflags* will be appended on the compiler command line after
4874 "cflags".
4875
4876 See also "asmflags" for flags for assembly-language files.
4877```
4878
4879#### **Ordering of flags and values**
4880
4881```
4882 1. Those set on the current target (not in a config).
4883 2. Those set on the "configs" on the target in order that the
4884 configs appear in the list.
4885 3. Those set on the "all_dependent_configs" on the target in order
4886 that the configs appear in the list.
4887 4. Those set on the "public_configs" on the target in order that
4888 those configs appear in the list.
4889 5. all_dependent_configs pulled from dependencies, in the order of
4890 the "deps" list. This is done recursively. If a config appears
4891 more than once, only the first occurence will be used.
4892 6. public_configs pulled from dependencies, in the order of the
4893 "deps" list. If a dependency is public, they will be applied
4894 recursively.
4895```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004896### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004897
4898```
4899 A list of strings.
4900
4901 "cflags" are passed to all invocations of the C, C++, Objective C, and
4902 Objective C++ compilers.
4903
4904 To target one of these variants individually, use "cflags_c", "cflags_cc",
4905 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4906 versions of cflags* will be appended on the compiler command line after
4907 "cflags".
4908
4909 See also "asmflags" for flags for assembly-language files.
4910```
4911
4912#### **Ordering of flags and values**
4913
4914```
4915 1. Those set on the current target (not in a config).
4916 2. Those set on the "configs" on the target in order that the
4917 configs appear in the list.
4918 3. Those set on the "all_dependent_configs" on the target in order
4919 that the configs appear in the list.
4920 4. Those set on the "public_configs" on the target in order that
4921 those configs appear in the list.
4922 5. all_dependent_configs pulled from dependencies, in the order of
4923 the "deps" list. This is done recursively. If a config appears
4924 more than once, only the first occurence will be used.
4925 6. public_configs pulled from dependencies, in the order of the
4926 "deps" list. If a dependency is public, they will be applied
4927 recursively.
4928```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004929### **check_includes**: [boolean] Controls whether a target's files are checked.
Brett Wilson796ed472018-07-16 15:11:09 -07004930
4931```
4932 When true (the default), the "gn check" command (as well as "gn gen" with the
4933 --check flag) will check this target's sources and headers for proper
4934 dependencies.
4935
4936 When false, the files in this target will be skipped by default. This does
4937 not affect other targets that depend on the current target, it just skips
4938 checking the includes of the current target's files.
4939
4940 If there are a few conditionally included headers that trip up checking, you
4941 can exclude headers individually by annotating them with "nogncheck" (see "gn
4942 help nogncheck").
4943
4944 The topic "gn help check" has general information on how checking works and
4945 advice on how to pass a check in problematic cases.
4946```
4947
4948#### **Example**
4949
4950```
4951 source_set("busted_includes") {
4952 # This target's includes are messed up, exclude it from checking.
4953 check_includes = false
4954 ...
4955 }
4956```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004957### **code_signing_args**: [string list] Arguments passed to code signing script.
Brett Wilson796ed472018-07-16 15:11:09 -07004958
4959```
4960 For create_bundle targets, code_signing_args is the list of arguments to pass
4961 to the code signing script. Typically you would use source expansion (see "gn
4962 help source_expansion") to insert the source file names.
4963
4964 See also "gn help create_bundle".
4965```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004966### **code_signing_outputs**: [file list] Output files for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07004967
4968```
4969 Outputs from the code signing step of a create_bundle target. Must refer to
4970 files in the build directory.
4971
4972 See also "gn help create_bundle".
4973```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004974### **code_signing_script**: [file name] Script for code signing."
Brett Wilson796ed472018-07-16 15:11:09 -07004975
4976```
4977 An absolute or buildfile-relative file name of a Python script to run for a
4978 create_bundle target to perform code signing step.
4979
4980 See also "gn help create_bundle".
4981```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004982### **code_signing_sources**: [file list] Sources for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07004983
4984```
4985 A list of files used as input for code signing script step of a create_bundle
4986 target. Non-absolute paths will be resolved relative to the current build
4987 file.
4988
4989 See also "gn help create_bundle".
4990```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004991### **complete_static_lib**: [boolean] Links all deps into a static library.
Brett Wilson796ed472018-07-16 15:11:09 -07004992
4993```
4994 A static library normally doesn't include code from dependencies, but instead
4995 forwards the static libraries and source sets in its deps up the dependency
4996 chain until a linkable target (an executable or shared library) is reached.
4997 The final linkable target only links each static library once, even if it
4998 appears more than once in its dependency graph.
4999
5000 In some cases the static library might be the final desired output. For
5001 example, you may be producing a static library for distribution to third
5002 parties. In this case, the static library should include code for all
5003 dependencies in one complete package. However, complete static libraries
5004 themselves are never linked into other complete static libraries. All
5005 complete static libraries are for distribution and linking them in would
5006 cause code duplication in this case. If the static library is not for
5007 distribution, it should not be complete.
5008
5009 GN treats non-complete static libraries as source sets when they are linked
5010 into complete static libraries. This is done because some tools like AR do
5011 not handle dependent static libraries properly. This makes it easier to write
5012 "alink" rules.
5013
5014 In rare cases it makes sense to list a header in more than one target if it
5015 could be considered conceptually a member of both. libraries.
5016```
5017
5018#### **Example**
5019
5020```
5021 static_library("foo") {
5022 complete_static_lib = true
5023 deps = [ "bar" ]
5024 }
5025```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005026### **configs**: Configs applying to this target or config.
Brett Wilson796ed472018-07-16 15:11:09 -07005027
5028```
5029 A list of config labels.
5030```
5031
5032#### **Configs on a target**
5033
5034```
5035 When used on a target, the include_dirs, defines, etc. in each config are
5036 appended in the order they appear to the compile command for each file in the
5037 target. They will appear after the include_dirs, defines, etc. that the
5038 target sets directly.
5039
5040 Since configs apply after the values set on a target, directly setting a
5041 compiler flag will prepend it to the command line. If you want to append a
5042 flag instead, you can put that flag in a one-off config and append that
5043 config to the target's configs list.
5044
5045 The build configuration script will generally set up the default configs
5046 applying to a given target type (see "set_defaults"). When a target is being
5047 defined, it can add to or remove from this list.
5048```
5049
5050#### **Configs on a config**
5051
5052```
5053 It is possible to create composite configs by specifying configs on a config.
5054 One might do this to forward values, or to factor out blocks of settings from
5055 very large configs into more manageable named chunks.
5056
5057 In this case, the composite config is expanded to be the concatenation of its
5058 own values, and in order, the values from its sub-configs *before* anything
5059 else happens. This has some ramifications:
5060
5061 - A target has no visibility into a config's sub-configs. Target code only
5062 sees the name of the composite config. It can't remove sub-configs or opt
5063 in to only parts of it. The composite config may not even be defined
5064 before the target is.
5065
5066 - You can get duplication of values if a config is listed twice, say, on a
5067 target and in a sub-config that also applies. In other cases, the configs
5068 applying to a target are de-duped. It's expected that if a config is
5069 listed as a sub-config that it is only used in that context. (Note that
5070 it's possible to fix this and de-dupe, but it's not normally relevant and
5071 complicates the implementation.)
5072```
5073
5074#### **Ordering of flags and values**
5075
5076```
5077 1. Those set on the current target (not in a config).
5078 2. Those set on the "configs" on the target in order that the
5079 configs appear in the list.
5080 3. Those set on the "all_dependent_configs" on the target in order
5081 that the configs appear in the list.
5082 4. Those set on the "public_configs" on the target in order that
5083 those configs appear in the list.
5084 5. all_dependent_configs pulled from dependencies, in the order of
5085 the "deps" list. This is done recursively. If a config appears
5086 more than once, only the first occurence will be used.
5087 6. public_configs pulled from dependencies, in the order of the
5088 "deps" list. If a dependency is public, they will be applied
5089 recursively.
5090```
5091
5092#### **Example**
5093
5094```
5095 # Configs on a target.
5096 source_set("foo") {
5097 # Don't use the default RTTI config that BUILDCONFIG applied to us.
5098 configs -= [ "//build:no_rtti" ]
5099
5100 # Add some of our own settings.
5101 configs += [ ":mysettings" ]
5102 }
5103
5104 # Create a default_optimization config that forwards to one of a set of more
5105 # specialized configs depending on build flags. This pattern is useful
5106 # because it allows a target to opt in to either a default set, or a more
5107 # specific set, while avoid duplicating the settings in two places.
5108 config("super_optimization") {
5109 cflags = [ ... ]
5110 }
5111 config("default_optimization") {
5112 if (optimize_everything) {
5113 configs = [ ":super_optimization" ]
5114 } else {
5115 configs = [ ":no_optimization" ]
5116 }
5117 }
5118```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005119### **contents**: Contents to write to file.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005120
5121```
5122 The contents of the file for a generated_file target.
5123 See "gn help generated_file".
5124```
Julie Hockettce1fa072019-05-07 17:44:37 -07005125### **crate_name**: [string] The name for the compiled crate.
5126
5127```
5128 Valid for `rust_library` targets and `executable`, `static_library`,
5129 `shared_library`, and `source_set` targets that contain Rust sources.
5130
5131 If crate_name is not set, then this rule will use the target name.
5132```
5133### **crate_root**: [string] The root source file for a binary or library.
5134
5135```
5136 Valid for `rust_library` targets and `executable`, `static_library`,
5137 `shared_library`, and `source_set` targets that contain Rust sources.
5138
5139 This file is usually the `main.rs` or `lib.rs` for binaries and libraries,
5140 respectively.
5141
5142 If crate_root is not set, then this rule will look for a lib.rs file (or
5143 main.rs for executable) or a single file in sources, if sources contains
5144 only one file.
5145```
5146### **crate_type**: [string] The type of linkage to use on a shared_library.
5147
5148```
5149 Valid for `rust_library` targets and `executable`, `static_library`,
5150 `shared_library`, and `source_set` targets that contain Rust sources.
5151
5152 Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib".
5153 This field sets the `crate-type` attribute for the `rustc` tool on static
5154 libraries, as well as the appropiate output extension in the
5155 `rust_output_extension` attribute. Since outputs must be explicit, the `lib`
5156 crate type (where the Rust compiler produces what it thinks is the
5157 appropriate library type) is not supported.
5158
5159 It should be noted that the "dylib" crate type in Rust is unstable in the set
5160 of symbols it exposes, and most usages today are potentially wrong and will
5161 be broken in the future.
5162
5163 Static libraries, rust libraries, and executables have this field set
5164 automatically.
5165```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005166### **data**: Runtime data file dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005167
5168```
5169 Lists files or directories required to run the given target. These are
5170 typically data files or directories of data files. The paths are interpreted
5171 as being relative to the current build file. Since these are runtime
5172 dependencies, they do not affect which targets are built or when. To declare
5173 input files to a script, use "inputs".
5174
5175 Appearing in the "data" section does not imply any special handling such as
5176 copying them to the output directory. This is just used for declaring runtime
5177 dependencies. Runtime dependencies can be queried using the "runtime_deps"
5178 category of "gn desc" or written during build generation via
5179 "--runtime-deps-list-file".
5180
5181 GN doesn't require data files to exist at build-time. So actions that produce
5182 files that are in turn runtime dependencies can list those generated files
5183 both in the "outputs" list as well as the "data" list.
5184
5185 By convention, directories are listed with a trailing slash:
5186 data = [ "test/data/" ]
5187 However, no verification is done on these so GN doesn't enforce this. The
5188 paths are just rebased and passed along when requested.
5189
5190 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5191 gathering data. See "gn help create_bundle" for details.
5192
5193 See "gn help runtime_deps" for how these are used.
5194```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005195### **data_deps**: Non-linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005196
5197```
5198 A list of target labels.
5199
5200 Specifies dependencies of a target that are not actually linked into the
5201 current target. Such dependencies will be built and will be available at
5202 runtime.
5203
5204 This is normally used for things like plugins or helper programs that a
5205 target needs at runtime.
5206
5207 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5208 gathering data_deps. See "gn help create_bundle" for details.
5209
5210 See also "gn help deps" and "gn help data".
5211```
5212
5213#### **Example**
5214
5215```
5216 executable("foo") {
5217 deps = [ "//base" ]
5218 data_deps = [ "//plugins:my_runtime_plugin" ]
5219 }
5220```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005221### **data_keys**: Keys from which to collect metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005222
5223```
5224 These keys are used to identify metadata to collect. If a walked target
5225 defines this key in its metadata, its value will be appended to the resulting
5226 collection.
5227
5228 See "gn help generated_file".
5229```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005230### **defines**: C preprocessor defines.
Brett Wilson796ed472018-07-16 15:11:09 -07005231
5232```
5233 A list of strings
5234
5235 These strings will be passed to the C/C++ compiler as #defines. The strings
5236 may or may not include an "=" to assign a value.
5237```
5238
5239#### **Ordering of flags and values**
5240
5241```
5242 1. Those set on the current target (not in a config).
5243 2. Those set on the "configs" on the target in order that the
5244 configs appear in the list.
5245 3. Those set on the "all_dependent_configs" on the target in order
5246 that the configs appear in the list.
5247 4. Those set on the "public_configs" on the target in order that
5248 those configs appear in the list.
5249 5. all_dependent_configs pulled from dependencies, in the order of
5250 the "deps" list. This is done recursively. If a config appears
5251 more than once, only the first occurence will be used.
5252 6. public_configs pulled from dependencies, in the order of the
5253 "deps" list. If a dependency is public, they will be applied
5254 recursively.
5255```
5256
5257#### **Example**
5258
5259```
5260 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
5261```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005262### **depfile**: [string] File name for input dependencies for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07005263
5264```
5265 If nonempty, this string specifies that the current action or action_foreach
5266 target will generate the given ".d" file containing the dependencies of the
5267 input. Empty or unset means that the script doesn't generate the files.
5268
5269 A depfile should be used only when a target depends on files that are not
5270 already specified by a target's inputs and sources. Likewise, depfiles should
5271 specify only those dependencies not already included in sources or inputs.
5272
5273 The .d file should go in the target output directory. If you have more than
5274 one source file that the script is being run over, you can use the output
5275 file expansions described in "gn help action_foreach" to name the .d file
5276 according to the input."
5277
5278 The format is that of a Makefile and all paths must be relative to the root
5279 build directory. Only one output may be listed and it must match the first
5280 output of the action.
5281
5282 Although depfiles are created by an action, they should not be listed in the
5283 action's "outputs" unless another target will use the file as an input.
5284```
5285
5286#### **Example**
5287
5288```
5289 action_foreach("myscript_target") {
5290 script = "myscript.py"
5291 sources = [ ... ]
5292
5293 # Locate the depfile in the output directory named like the
5294 # inputs but with a ".d" appended.
5295 depfile = "$relative_target_output_dir/{{source_name}}.d"
5296
5297 # Say our script uses "-o " to indicate the depfile.
5298 args = [ "{{source}}", "-o", depfile ]
5299 }
5300```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005301### **deps**: Private linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005302
5303```
5304 A list of target labels.
5305
5306 Specifies private dependencies of a target. Private dependencies are
5307 propagated up the dependency tree and linked to dependent targets, but do not
5308 grant the ability to include headers from the dependency. Public configs are
5309 not forwarded.
5310```
5311
5312#### **Details of dependency propagation**
5313
5314```
5315 Source sets, shared libraries, and non-complete static libraries will be
5316 propagated up the dependency tree across groups, non-complete static
5317 libraries and source sets.
5318
5319 Executables, shared libraries, and complete static libraries will link all
5320 propagated targets and stop propagation. Actions and copy steps also stop
5321 propagation, allowing them to take a library as an input but not force
5322 dependents to link to it.
5323
5324 Propagation of all_dependent_configs and public_configs happens independently
5325 of target type. all_dependent_configs are always propagated across all types
5326 of targets, and public_configs are always propagated across public deps of
5327 all types of targets.
5328
5329 Data dependencies are propagated differently. See "gn help data_deps" and
5330 "gn help runtime_deps".
5331
5332 See also "public_deps".
5333```
Petr Hoseka1413862020-01-03 12:54:33 -08005334### **externs**: [scope] Set of Rust crate-dependency pairs.
5335
5336```
5337 A list, each value being a scope indicating a pair of crate name and the path
5338 to the Rust library.
5339
5340 These libraries will be passed as `--extern crate_name=path` to compiler
5341 invocation containing the current target.
5342```
5343
5344#### **Examples**
5345
5346```
5347 executable("foo") {
5348 sources = [ "main.rs" ]
5349 externs = [{
5350 crate_name = "bar",
5351 path = "path/to/bar.rlib"
5352 }]
5353 }
5354
5355 This target would compile the `foo` crate with the following `extern` flag:
5356 `--extern bar=path/to/bar.rlib`.
5357```
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005358### **framework_dirs**: [directory list] Additional framework search directories.
5359
5360```
5361 A list of source directories.
5362
5363 The directories in this list will be added to the framework search path for
5364 the files in the affected target.
5365```
5366
5367#### **Ordering of flags and values**
5368
5369```
5370 1. Those set on the current target (not in a config).
5371 2. Those set on the "configs" on the target in order that the
5372 configs appear in the list.
5373 3. Those set on the "all_dependent_configs" on the target in order
5374 that the configs appear in the list.
5375 4. Those set on the "public_configs" on the target in order that
5376 those configs appear in the list.
5377 5. all_dependent_configs pulled from dependencies, in the order of
5378 the "deps" list. This is done recursively. If a config appears
5379 more than once, only the first occurence will be used.
5380 6. public_configs pulled from dependencies, in the order of the
5381 "deps" list. If a dependency is public, they will be applied
5382 recursively.
5383```
5384
5385#### **Example**
5386
5387```
5388 framework_dirs = [ "src/include", "//third_party/foo" ]
5389```
5390### **frameworks**: [name list] Name of frameworks that must be linked.
5391
5392```
5393 A list of framework names.
5394
5395 The frameworks named in that list will be linked with any dynamic link
5396 type target.
5397```
5398
5399#### **Ordering of flags and values**
5400
5401```
5402 1. Those set on the current target (not in a config).
5403 2. Those set on the "configs" on the target in order that the
5404 configs appear in the list.
5405 3. Those set on the "all_dependent_configs" on the target in order
5406 that the configs appear in the list.
5407 4. Those set on the "public_configs" on the target in order that
5408 those configs appear in the list.
5409 5. all_dependent_configs pulled from dependencies, in the order of
5410 the "deps" list. This is done recursively. If a config appears
5411 more than once, only the first occurence will be used.
5412 6. public_configs pulled from dependencies, in the order of the
5413 "deps" list. If a dependency is public, they will be applied
5414 recursively.
5415```
5416
5417#### **Example**
5418
5419```
5420 frameworks = [ "Foundation.framework", "Foo.framework" ]
5421```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005422### **friend**: Allow targets to include private headers.
Brett Wilson796ed472018-07-16 15:11:09 -07005423
5424```
5425 A list of label patterns (see "gn help label_pattern") that allow dependent
5426 targets to include private headers. Applies to all binary targets.
5427
5428 Normally if a target lists headers in the "public" list (see "gn help
5429 public"), other headers are implicitly marked as private. Private headers
5430 can not be included by other targets, even with a public dependency path.
5431 The "gn check" function performs this validation.
5432
5433 A friend declaration allows one or more targets to include private headers.
5434 This is useful for things like unit tests that are closely associated with a
5435 target and require internal knowledge without opening up all headers to be
5436 included by all dependents.
5437
5438 A friend target does not allow that target to include headers when no
5439 dependency exists. A public dependency path must still exist between two
5440 targets to include any headers from a destination target. The friend
5441 annotation merely allows the use of headers that would otherwise be
5442 prohibited because they are private.
5443
5444 The friend annotation is matched only against the target containing the file
5445 with the include directive. Friend annotations are not propagated across
5446 public or private dependencies. Friend annotations do not affect visibility.
5447```
5448
5449#### **Example**
5450
5451```
5452 static_library("lib") {
5453 # This target can include our private headers.
5454 friend = [ ":unit_tests" ]
5455
5456 public = [
5457 "public_api.h", # Normal public API for dependent targets.
5458 ]
5459
5460 # Private API and sources.
5461 sources = [
5462 "a_source_file.cc",
5463
5464 # Normal targets that depend on this one won't be able to include this
5465 # because this target defines a list of "public" headers. Without the
5466 # "public" list, all headers are implicitly public.
5467 "private_api.h",
5468 ]
5469 }
5470
5471 executable("unit_tests") {
5472 sources = [
5473 # This can include "private_api.h" from the :lib target because it
5474 # depends on that target and because of the friend annotation.
5475 "my_test.cc",
5476 ]
5477
5478 deps = [
5479 ":lib", # Required for the include to be allowed.
5480 ]
5481 }
5482```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005483### **include_dirs**: Additional include directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005484
5485```
5486 A list of source directories.
5487
5488 The directories in this list will be added to the include path for the files
5489 in the affected target.
5490```
5491
5492#### **Ordering of flags and values**
5493
5494```
5495 1. Those set on the current target (not in a config).
5496 2. Those set on the "configs" on the target in order that the
5497 configs appear in the list.
5498 3. Those set on the "all_dependent_configs" on the target in order
5499 that the configs appear in the list.
5500 4. Those set on the "public_configs" on the target in order that
5501 those configs appear in the list.
5502 5. all_dependent_configs pulled from dependencies, in the order of
5503 the "deps" list. This is done recursively. If a config appears
5504 more than once, only the first occurence will be used.
5505 6. public_configs pulled from dependencies, in the order of the
5506 "deps" list. If a dependency is public, they will be applied
5507 recursively.
5508```
5509
5510#### **Example**
5511
5512```
5513 include_dirs = [ "src/include", "//third_party/foo" ]
5514```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005515### **inputs**: Additional compile-time dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005516
5517```
5518 Inputs are compile-time dependencies of the current target. This means that
5519 all inputs must be available before compiling any of the sources or executing
5520 any actions.
5521
5522 Inputs are typically only used for action and action_foreach targets.
5523```
5524
5525#### **Inputs for actions**
5526
5527```
5528 For action and action_foreach targets, inputs should be the inputs to script
5529 that don't vary. These should be all .py files that the script uses via
5530 imports (the main script itself will be an implicit dependency of the action
5531 so need not be listed).
5532
5533 For action targets, inputs and sources are treated the same, but from a style
5534 perspective, it's recommended to follow the same rule as action_foreach and
5535 put helper files in the inputs, and the data used by the script (if any) in
5536 sources.
5537
5538 Note that another way to declare input dependencies from an action is to have
5539 the action write a depfile (see "gn help depfile"). This allows the script to
5540 dynamically write input dependencies, that might not be known until actually
5541 executing the script. This is more efficient than doing processing while
5542 running GN to determine the inputs, and is easier to keep in-sync than
5543 hardcoding the list.
5544```
5545
5546#### **Script input gotchas**
5547
5548```
5549 It may be tempting to write a script that enumerates all files in a directory
5550 as inputs. Don't do this! Even if you specify all the files in the inputs or
5551 sources in the GN target (or worse, enumerate the files in an exec_script
5552 call when running GN, which will be slow), the dependencies will be broken.
5553
5554 The problem happens if a file is ever removed because the inputs are not
5555 listed on the command line to the script. Because the script hasn't changed
5556 and all inputs are up to date, the script will not re-run and you will get a
5557 stale build. Instead, either list all inputs on the command line to the
5558 script, or if there are many, create a separate list file that the script
5559 reads. As long as this file is listed in the inputs, the build will detect
5560 when it has changed in any way and the action will re-run.
5561```
5562
5563#### **Inputs for binary targets**
5564
5565```
Julie Hockette2a29402018-07-31 10:11:42 -07005566 Any input dependencies will be resolved before compiling any sources or
5567 linking the target. Normally, all actions that a target depends on will be run
5568 before any files in a target are compiled. So if you depend on generated
5569 headers, you do not typically need to list them in the inputs section.
Brett Wilson796ed472018-07-16 15:11:09 -07005570
5571 Inputs for binary targets will be treated as implicit dependencies, meaning
5572 that changes in any of the inputs will force all sources in the target to be
5573 recompiled. If an input only applies to a subset of source files, you may
5574 want to split those into a separate target to avoid unnecessary recompiles.
5575```
5576
5577#### **Example**
5578
5579```
5580 action("myscript") {
5581 script = "domything.py"
5582 inputs = [ "input.data" ]
5583 }
5584```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005585### **ldflags**: Flags passed to the linker.
Brett Wilson796ed472018-07-16 15:11:09 -07005586
5587```
5588 A list of strings.
5589
5590 These flags are passed on the command-line to the linker and generally
5591 specify various linking options. Most targets will not need these and will
5592 use "libs" and "lib_dirs" instead.
5593
5594 ldflags are NOT pushed to dependents, so applying ldflags to source sets or
5595 static libraries will be a no-op. If you want to apply ldflags to dependent
5596 targets, put them in a config and set it in the all_dependent_configs or
5597 public_configs.
5598```
5599
5600#### **Ordering of flags and values**
5601
5602```
5603 1. Those set on the current target (not in a config).
5604 2. Those set on the "configs" on the target in order that the
5605 configs appear in the list.
5606 3. Those set on the "all_dependent_configs" on the target in order
5607 that the configs appear in the list.
5608 4. Those set on the "public_configs" on the target in order that
5609 those configs appear in the list.
5610 5. all_dependent_configs pulled from dependencies, in the order of
5611 the "deps" list. This is done recursively. If a config appears
5612 more than once, only the first occurence will be used.
5613 6. public_configs pulled from dependencies, in the order of the
5614 "deps" list. If a dependency is public, they will be applied
5615 recursively.
5616```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005617### **lib_dirs**: Additional library directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005618
5619```
5620 A list of directories.
5621
5622 Specifies additional directories passed to the linker for searching for the
5623 required libraries. If an item is not an absolute path, it will be treated as
5624 being relative to the current build file.
5625
5626 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005627 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005628 shared library or executable target is reached. Second, they are
5629 uniquified so each one is only passed once (the first instance of it
5630 will be the one used).
5631```
5632
5633#### **Ordering of flags and values**
5634
5635```
5636 1. Those set on the current target (not in a config).
5637 2. Those set on the "configs" on the target in order that the
5638 configs appear in the list.
5639 3. Those set on the "all_dependent_configs" on the target in order
5640 that the configs appear in the list.
5641 4. Those set on the "public_configs" on the target in order that
5642 those configs appear in the list.
5643 5. all_dependent_configs pulled from dependencies, in the order of
5644 the "deps" list. This is done recursively. If a config appears
5645 more than once, only the first occurence will be used.
5646 6. public_configs pulled from dependencies, in the order of the
5647 "deps" list. If a dependency is public, they will be applied
5648 recursively.
5649
5650 For "libs" and "lib_dirs" only, the values propagated from
5651 dependencies (as described above) are applied last assuming they
5652 are not already in the list.
5653```
5654
5655#### **Example**
5656
5657```
5658 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
5659```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005660### **libs**: Additional libraries to link.
Brett Wilson796ed472018-07-16 15:11:09 -07005661
5662```
5663 A list of library names or library paths.
5664
5665 These libraries will be linked into the final binary (executable or shared
5666 library) containing the current target.
5667
5668 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005669 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005670 shared library or executable target is reached. Second, they are
5671 uniquified so each one is only passed once (the first instance of it
5672 will be the one used).
5673```
5674
5675#### **Types of libs**
5676
5677```
5678 There are several different things that can be expressed in libs:
5679
5680 File paths
5681 Values containing '/' will be treated as references to files in the
5682 checkout. They will be rebased to be relative to the build directory and
5683 specified in the "libs" for linker tools. This facility should be used
5684 for libraries that are checked in to the version control. For libraries
5685 that are generated by the build, use normal GN deps to link them.
5686
5687 System libraries
5688 Values not containing '/' will be treated as system library names. These
5689 will be passed unmodified to the linker and prefixed with the
5690 "lib_switch" attribute of the linker tool. Generally you would set the
5691 "lib_dirs" so the given library is found. Your BUILD.gn file should not
5692 specify the switch (like "-l"): this will be encoded in the "lib_switch"
5693 of the tool.
5694
5695 Apple frameworks
5696 System libraries ending in ".framework" will be special-cased: the switch
5697 "-framework" will be prepended instead of the lib_switch, and the
5698 ".framework" suffix will be trimmed. This is to support the way Mac links
5699 framework dependencies.
5700```
5701
5702#### **Ordering of flags and values**
5703
5704```
5705 1. Those set on the current target (not in a config).
5706 2. Those set on the "configs" on the target in order that the
5707 configs appear in the list.
5708 3. Those set on the "all_dependent_configs" on the target in order
5709 that the configs appear in the list.
5710 4. Those set on the "public_configs" on the target in order that
5711 those configs appear in the list.
5712 5. all_dependent_configs pulled from dependencies, in the order of
5713 the "deps" list. This is done recursively. If a config appears
5714 more than once, only the first occurence will be used.
5715 6. public_configs pulled from dependencies, in the order of the
5716 "deps" list. If a dependency is public, they will be applied
5717 recursively.
5718
5719 For "libs" and "lib_dirs" only, the values propagated from
5720 dependencies (as described above) are applied last assuming they
5721 are not already in the list.
5722```
5723
5724#### **Examples**
5725
5726```
5727 On Windows:
5728 libs = [ "ctl3d.lib" ]
5729
5730 On Linux:
5731 libs = [ "ld" ]
5732```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005733### **metadata**: Metadata of this target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005734
5735```
5736 Metadata is a collection of keys and values relating to a particular target.
Julie Hockettd69a9c32019-01-23 14:36:18 -08005737 Values must be lists, allowing for sane and predictable collection behavior.
5738 Generally, these keys will include three types of lists: lists of ordinary
5739 strings, lists of filenames intended to be rebased according to their
5740 particular source directory, and lists of target labels intended to be used
5741 as barriers to the walk. Verfication of these categories occurs at walk time,
5742 not creation time (since it is not clear until the walk which values are
5743 intended for which purpose).
5744```
Nate Fischer8ed01d32019-01-08 17:32:01 -08005745
Julie Hockettd69a9c32019-01-23 14:36:18 -08005746#### **Example**
Nate Fischer8ed01d32019-01-08 17:32:01 -08005747
Julie Hockettd69a9c32019-01-23 14:36:18 -08005748```
Nate Fischer8ed01d32019-01-08 17:32:01 -08005749 group("doom_melon") {
5750 metadata = {
5751 # These keys are not built in to GN but are interpreted when consuming
5752 # metadata.
5753 my_barrier = []
5754 my_files = [ "a.txt", "b.txt" ]
5755 }
5756 }
5757```
Wesley Moyd874bb92020-01-21 16:01:04 -08005758### **output_conversion**: Data format for generated_file targets.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005759
5760```
5761 Controls how the "contents" of a generated_file target is formatted.
Julie Hockett81ee1962019-05-13 11:24:42 -07005762 See "gn help io_conversion".
Nate Fischer8ed01d32019-01-08 17:32:01 -08005763```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005764### **output_dir**: [directory] Directory to put output file in.
Brett Wilson796ed472018-07-16 15:11:09 -07005765
5766```
5767 For library and executable targets, overrides the directory for the final
5768 output. This must be in the root_build_dir or a child thereof.
5769
5770 This should generally be in the root_out_dir or a subdirectory thereof (the
5771 root_out_dir will be the same as the root_build_dir for the default
5772 toolchain, and will be a subdirectory for other toolchains). Not putting the
5773 output in a subdirectory of root_out_dir can result in collisions between
5774 different toolchains, so you will need to take steps to ensure that your
5775 target is only present in one toolchain.
5776
5777 Normally the toolchain specifies the output directory for libraries and
5778 executables (see "gn help tool"). You will have to consult that for the
5779 default location. The default location will be used if output_dir is
5780 undefined or empty.
5781```
5782
5783#### **Example**
5784
5785```
5786 shared_library("doom_melon") {
5787 output_dir = "$root_out_dir/plugin_libs"
5788 ...
5789 }
5790```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005791### **output_extension**: Value to use for the output's file extension.
Brett Wilson796ed472018-07-16 15:11:09 -07005792
5793```
5794 Normally the file extension for a target is based on the target type and the
5795 operating system, but in rare cases you will need to override the name (for
5796 example to use "libfreetype.so.6" instead of libfreetype.so on Linux).
5797
5798 This value should not include a leading dot. If undefined, the default
5799 specified on the tool will be used. If set to the empty string, no output
5800 extension will be used.
5801
5802 The output_extension will be used to set the "{{output_extension}}" expansion
5803 which the linker tool will generally use to specify the output file name. See
5804 "gn help tool".
5805```
5806
5807#### **Example**
5808
5809```
5810 shared_library("freetype") {
5811 if (is_linux) {
5812 # Call the output "libfreetype.so.6"
5813 output_extension = "so.6"
5814 }
5815 ...
5816 }
5817
5818 # On Windows, generate a "mysettings.cpl" control panel applet. Control panel
5819 # applets are actually special shared libraries.
5820 if (is_win) {
5821 shared_library("mysettings") {
5822 output_extension = "cpl"
5823 ...
5824 }
5825 }
5826```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005827### **output_name**: Define a name for the output file other than the default.
Brett Wilson796ed472018-07-16 15:11:09 -07005828
5829```
5830 Normally the output name of a target will be based on the target name, so the
5831 target "//foo/bar:bar_unittests" will generate an output file such as
5832 "bar_unittests.exe" (using Windows as an example).
5833
5834 Sometimes you will want an alternate name to avoid collisions or if the
5835 internal name isn't appropriate for public distribution.
5836
5837 The output name should have no extension or prefixes, these will be added
5838 using the default system rules. For example, on Linux an output name of "foo"
5839 will produce a shared library "libfoo.so". There is no way to override the
5840 output prefix of a linker tool on a per- target basis. If you need more
5841 flexibility, create a copy target to produce the file you want.
5842
5843 This variable is valid for all binary output target types.
5844```
5845
5846#### **Example**
5847
5848```
5849 static_library("doom_melon") {
5850 output_name = "fluffy_bunny"
5851 }
5852```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005853### **output_prefix_override**: Don't use prefix for output name.
Brett Wilson796ed472018-07-16 15:11:09 -07005854
5855```
5856 A boolean that overrides the output prefix for a target. Defaults to false.
5857
5858 Some systems use prefixes for the names of the final target output file. The
5859 normal example is "libfoo.so" on Linux for a target named "foo".
5860
5861 The output prefix for a given target type is specified on the linker tool
5862 (see "gn help tool"). Sometimes this prefix is undesired.
5863
5864 See also "gn help output_extension".
5865```
5866
5867#### **Example**
5868
5869```
5870 shared_library("doom_melon") {
5871 # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag
5872 # will produce "doom_melon.so".
5873 output_prefix_override = true
5874 ...
5875 }
5876```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005877### **outputs**: Output files for actions and copy targets.
Brett Wilson796ed472018-07-16 15:11:09 -07005878
5879```
5880 Outputs is valid for "copy", "action", and "action_foreach" target types and
5881 indicates the resulting files. Outputs must always refer to files in the
5882 build directory.
5883
5884 copy
5885 Copy targets should have exactly one entry in the outputs list. If there is
5886 exactly one source, this can be a literal file name or a source expansion.
5887 If there is more than one source, this must contain a source expansion to
5888 map a single input name to a single output name. See "gn help copy".
5889
5890 action_foreach
5891 Action_foreach targets must always use source expansions to map input files
5892 to output files. There can be more than one output, which means that each
5893 invocation of the script will produce a set of files (presumably based on
5894 the name of the input file). See "gn help action_foreach".
5895
5896 action
5897 Action targets (excluding action_foreach) must list literal output file(s)
5898 with no source expansions. See "gn help action".
5899```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005900### **partial_info_plist**: [filename] Path plist from asset catalog compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005901
5902```
5903 Valid for create_bundle target, corresponds to the path for the partial
5904 Info.plist created by the asset catalog compiler that needs to be merged
5905 with the application Info.plist (usually done by the code signing script).
5906
5907 The file will be generated regardless of whether the asset compiler has
5908 been invoked or not. See "gn help create_bundle".
5909```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005910### **pool**: Label of the pool used by the action.
Brett Wilson796ed472018-07-16 15:11:09 -07005911
5912```
5913 A fully-qualified label representing the pool that will be used for the
5914 action. Pools are defined using the pool() {...} declaration.
5915```
5916
5917#### **Example**
5918
5919```
5920 action("action") {
5921 pool = "//build:custom_pool"
5922 ...
5923 }
5924```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005925### **precompiled_header**: [string] Header file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07005926
5927```
5928 Precompiled headers will be used when a target specifies this value, or a
5929 config applying to this target specifies this value. In addition, the tool
5930 corresponding to the source files must also specify precompiled headers (see
5931 "gn help tool"). The tool will also specify what type of precompiled headers
5932 to use, by setting precompiled_header_type to either "gcc" or "msvc".
5933
5934 The precompiled header/source variables can be specified on a target or a
5935 config, but must be the same for all configs applying to a given target since
5936 a target can only have one precompiled header.
5937
5938 If you use both C and C++ sources, the precompiled header and source file
5939 will be compiled once per language. You will want to make sure to wrap C++
5940 includes in __cplusplus #ifdefs so the file will compile in C mode.
5941```
5942
5943#### **GCC precompiled headers**
5944
5945```
5946 When using GCC-style precompiled headers, "precompiled_source" contains the
5947 path of a .h file that is precompiled and then included by all source files
5948 in targets that set "precompiled_source".
5949
5950 The value of "precompiled_header" is not used with GCC-style precompiled
5951 headers.
5952```
5953
5954#### **MSVC precompiled headers**
5955
5956```
5957 When using MSVC-style precompiled headers, the "precompiled_header" value is
5958 a string corresponding to the header. This is NOT a path to a file that GN
5959 recognises, but rather the exact string that appears in quotes after
5960 an #include line in source code. The compiler will match this string against
5961 includes or forced includes (/FI).
5962
5963 MSVC also requires a source file to compile the header with. This must be
5964 specified by the "precompiled_source" value. In contrast to the header value,
5965 this IS a GN-style file name, and tells GN which source file to compile to
5966 make the .pch file used for subsequent compiles.
5967
5968 For example, if the toolchain specifies MSVC headers:
5969
5970 toolchain("vc_x64") {
5971 ...
5972 tool("cxx") {
5973 precompiled_header_type = "msvc"
5974 ...
5975
5976 You might make a config like this:
5977
5978 config("use_precompiled_headers") {
5979 precompiled_header = "build/precompile.h"
5980 precompiled_source = "//build/precompile.cc"
5981
5982 # Either your source files should #include "build/precompile.h"
5983 # first, or you can do this to force-include the header.
5984 cflags = [ "/FI$precompiled_header" ]
5985 }
5986
5987 And then define a target that uses the config:
5988
5989 executable("doom_melon") {
5990 configs += [ ":use_precompiled_headers" ]
5991 ...
5992```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005993### **precompiled_header_type**: [string] "gcc" or "msvc".
Brett Wilson796ed472018-07-16 15:11:09 -07005994
5995```
5996 See "gn help precompiled_header".
5997```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005998### **precompiled_source**: [file name] Source file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07005999
6000```
6001 The source file that goes along with the precompiled_header when using
6002 "msvc"-style precompiled headers. It will be implicitly added to the sources
6003 of the target. See "gn help precompiled_header".
6004```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006005### **product_type**: Product type for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006006
6007```
6008 Correspond to the type of the product of a create_bundle target. Only
6009 meaningful to Xcode (used as part of the Xcode project generation).
6010
6011 When generating Xcode project files, only create_bundle target with a
6012 non-empty product_type will have a corresponding target in Xcode project.
6013```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006014### **public**: Declare public header files for a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006015
6016```
6017 A list of files that other targets can include. These permissions are checked
6018 via the "check" command (see "gn help check").
6019
6020 If no public files are declared, other targets (assuming they have visibility
6021 to depend on this target) can include any file in the sources list. If this
6022 variable is defined on a target, dependent targets may only include files on
6023 this whitelist unless that target is marked as a friend (see "gn help
6024 friend").
6025
6026 Header file permissions are also subject to visibility. A target must be
6027 visible to another target to include any files from it at all and the public
6028 headers indicate which subset of those files are permitted. See "gn help
6029 visibility" for more.
6030
6031 Public files are inherited through the dependency tree. So if there is a
6032 dependency A -> B -> C, then A can include C's public headers. However, the
6033 same is NOT true of visibility, so unless A is in C's visibility list, the
6034 include will be rejected.
6035
6036 GN only knows about files declared in the "sources" and "public" sections of
6037 targets. If a file is included that is not known to the build, it will be
6038 allowed.
6039
6040 It is common for test targets to need to include private headers for their
6041 associated code. In this case, list the test target in the "friend" list of
6042 the target that owns the private header to allow the inclusion. See
6043 "gn help friend" for more.
Julie Hockette2a29402018-07-31 10:11:42 -07006044
6045 When a binary target has no explicit or implicit public headers (a "public"
6046 list is defined but is empty), GN assumes that the target can not propagate
6047 any compile-time dependencies up the dependency tree. In this case, the build
6048 can be parallelized more efficiently.
6049 Say there are dependencies:
6050 A (shared library) -> B (shared library) -> C (action).
6051 Normally C must complete before any source files in A can compile (because
6052 there might be generated includes). But when B explicitly declares no public
6053 headers, C can execute in parallel with A's compile steps. C must still be
6054 complete before any dependents link.
Brett Wilson796ed472018-07-16 15:11:09 -07006055```
6056
6057#### **Examples**
6058
6059```
6060 These exact files are public:
6061 public = [ "foo.h", "bar.h" ]
6062
6063 No files are public (no targets may include headers from this one):
Julie Hockette2a29402018-07-31 10:11:42 -07006064 # This allows starting compilation in dependent targets earlier.
Brett Wilson796ed472018-07-16 15:11:09 -07006065 public = []
6066```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006067### **public_configs**: Configs to be applied on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07006068
6069```
6070 A list of config labels.
6071
6072 Targets directly depending on this one will have the configs listed in this
6073 variable added to them. These configs will also apply to the current target.
Julie Hockette2a29402018-07-31 10:11:42 -07006074 Generally, public configs are used to apply defines and include directories
6075 necessary to compile this target's header files.
Brett Wilson796ed472018-07-16 15:11:09 -07006076
Julie Hockette2a29402018-07-31 10:11:42 -07006077 See also "gn help all_dependent_configs".
6078```
6079
6080#### **Propagation of public configs**
6081
6082```
6083 Public configs are applied to all targets that depend directly on this one.
6084 These dependant targets can further push this target's public configs
6085 higher in the dependency tree by depending on it via public_deps (see "gn
6086 help public_deps").
6087
6088 static_library("toplevel") {
6089 # This target will get "my_config" applied to it. However, since this
6090 # target uses "deps" and not "public_deps", targets that depend on this
6091 # one won't get it.
6092 deps = [ ":intermediate" ]
6093 }
6094
6095 static_library("intermediate") {
6096 # Depending on "lower" in any way will apply "my_config" to this target.
6097 # Additionall, since this target depends on "lower" via public_deps,
6098 # targets that depend on this one will also get "my_config".
6099 public_deps = [ ":lower" ]
6100 }
6101
6102 static_library("lower") {
6103 # This will get applied to all targets that depend on this one.
6104 public_configs = [ ":my_config" ]
6105 }
6106
6107 Public config propagation happens in a second phase once a target and all of
6108 its dependencies have been resolved. Therefore, a target will not see these
Brett Wilson796ed472018-07-16 15:11:09 -07006109 force-added configs in their "configs" variable while the script is running,
6110 and they can not be removed. As a result, this capability should generally
Julie Hockette2a29402018-07-31 10:11:42 -07006111 only be used to add defines and include directories rather than setting
6112 complicated flags that some targets may not want.
Brett Wilson796ed472018-07-16 15:11:09 -07006113
Julie Hockette2a29402018-07-31 10:11:42 -07006114 Public configs may or may not be propagated across toolchain boundaries
6115 depending on the value of the propagates_configs flag (see "gn help
6116 toolchain") on the toolchain of the target declaring the public_config.
6117```
6118
6119#### **Avoiding applying public configs to this target**
6120
6121```
6122 If you want the config to apply to targets that depend on this one, but NOT
6123 this one, define an extra layer of indirection using a group:
6124
6125 # External targets depend on this group.
6126 group("my_target") {
6127 # Config to apply to all targets that depend on this one.
6128 public_configs = [ ":external_settings" ]
6129 deps = [ ":internal_target" ]
6130 }
6131
6132 # Internal target to actually compile the sources.
6133 static_library("internal_target") {
6134 # Force all external targets to depend on the group instead of directly
6135 # on this so the "external_settings" config will get applied.
6136 visibility = [ ":my_target" ]
6137 ...
6138 }
Brett Wilson796ed472018-07-16 15:11:09 -07006139```
6140
6141#### **Ordering of flags and values**
6142
6143```
6144 1. Those set on the current target (not in a config).
6145 2. Those set on the "configs" on the target in order that the
6146 configs appear in the list.
6147 3. Those set on the "all_dependent_configs" on the target in order
6148 that the configs appear in the list.
6149 4. Those set on the "public_configs" on the target in order that
6150 those configs appear in the list.
6151 5. all_dependent_configs pulled from dependencies, in the order of
6152 the "deps" list. This is done recursively. If a config appears
6153 more than once, only the first occurence will be used.
6154 6. public_configs pulled from dependencies, in the order of the
6155 "deps" list. If a dependency is public, they will be applied
6156 recursively.
6157```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006158### **public_deps**: Declare public dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07006159
6160```
6161 Public dependencies are like private dependencies (see "gn help deps") but
6162 additionally express that the current target exposes the listed deps as part
6163 of its public API.
6164
6165 This has several ramifications:
6166
6167 - public_configs that are part of the dependency are forwarded to direct
6168 dependents.
6169
6170 - Public headers in the dependency are usable by dependents (includes do
6171 not require a direct dependency or visibility).
6172
6173 - If the current target is a shared library, other shared libraries that it
6174 publicly depends on (directly or indirectly) are propagated up the
6175 dependency tree to dependents for linking.
Julie Hockette2a29402018-07-31 10:11:42 -07006176
6177 See also "gn help public_configs".
Brett Wilson796ed472018-07-16 15:11:09 -07006178```
6179
6180#### **Discussion**
6181
6182```
6183 Say you have three targets: A -> B -> C. C's visibility may allow B to depend
6184 on it but not A. Normally, this would prevent A from including any headers
6185 from C, and C's public_configs would apply only to B.
6186
6187 If B lists C in its public_deps instead of regular deps, A will now inherit
6188 C's public_configs and the ability to include C's public headers.
6189
6190 Generally if you are writing a target B and you include C's headers as part
6191 of B's public headers, or targets depending on B should consider B and C to
6192 be part of a unit, you should use public_deps instead of deps.
6193```
6194
6195#### **Example**
6196
6197```
6198 # This target can include files from "c" but not from
6199 # "super_secret_implementation_details".
6200 executable("a") {
6201 deps = [ ":b" ]
6202 }
6203
6204 shared_library("b") {
6205 deps = [ ":super_secret_implementation_details" ]
6206 public_deps = [ ":c" ]
6207 }
6208```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006209### **rebase**: Rebase collected metadata as files.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006210
6211```
6212 A boolean that triggers a rebase of collected metadata strings based on their
6213 declared file. Defaults to false.
6214
6215 Metadata generally declares files as strings relative to the local build file.
6216 However, this data is often used in other contexts, and so setting this flag
6217 will force the metadata collection to be rebased according to the local build
6218 file's location and thus allow the filename to be used anywhere.
6219
6220 Setting this flag will raise an error if any target's specified metadata is
6221 not a string value.
6222
6223 See also "gn help generated_file".
6224```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006225### **response_file_contents**: Contents of a response file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006226
6227```
6228 Sometimes the arguments passed to a script can be too long for the system's
6229 command-line capabilities. This is especially the case on Windows where the
6230 maximum command-line length is less than 8K. A response file allows you to
6231 pass an unlimited amount of data to a script in a temporary file for an
6232 action or action_foreach target.
6233
6234 If the response_file_contents variable is defined and non-empty, the list
6235 will be treated as script args (including possibly substitution patterns)
6236 that will be written to a temporary file at build time. The name of the
6237 temporary file will be substituted for "{{response_file_name}}" in the script
6238 args.
6239
6240 The response file contents will always be quoted and escaped according to
6241 Unix shell rules. To parse the response file, the Python script should use
6242 "shlex.split(file_contents)".
6243```
6244
6245#### **Example**
6246
6247```
6248 action("process_lots_of_files") {
6249 script = "process.py",
6250 inputs = [ ... huge list of files ... ]
6251
6252 # Write all the inputs to a response file for the script. Also,
6253 # make the paths relative to the script working directory.
6254 response_file_contents = rebase_path(inputs, root_build_dir)
6255
6256 # The script expects the name of the response file in --file-list.
6257 args = [
6258 "--enable-foo",
6259 "--file-list={{response_file_name}}",
6260 ]
6261 }
6262```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006263### **script**: Script file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006264
6265```
6266 An absolute or buildfile-relative file name of a Python script to run for a
6267 action and action_foreach targets (see "gn help action" and "gn help
6268 action_foreach").
6269```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006270### **sources**: Source files for a target
Brett Wilson796ed472018-07-16 15:11:09 -07006271
6272```
6273 A list of files. Non-absolute paths will be resolved relative to the current
6274 build file.
6275```
6276
6277#### **Sources for binary targets**
6278
6279```
6280 For binary targets (source sets, executables, and libraries), the known file
6281 types will be compiled with the associated tools. Unknown file types and
6282 headers will be skipped. However, you should still list all C/C+ header files
6283 so GN knows about the existence of those files for the purposes of include
6284 checking.
6285
6286 As a special case, a file ending in ".def" will be treated as a Windows
6287 module definition file. It will be appended to the link line with a
6288 preceding "/DEF:" string. There must be at most one .def file in a target
6289 and they do not cross dependency boundaries (so specifying a .def file in a
6290 static library or source set will have no effect on the executable or shared
6291 library they're linked into).
Julie Hockettce1fa072019-05-07 17:44:37 -07006292
6293 For Rust targets that do not specify a crate_root, then the crate_root will
6294 look for a lib.rs file (or main.rs for executable) or a single file in
6295 sources, if sources contains only one file.
Brett Wilson796ed472018-07-16 15:11:09 -07006296```
6297
6298#### **Sources for non-binary targets**
6299
6300```
6301 action_foreach
6302 The sources are the set of files that the script will be executed over. The
6303 script will run once per file.
6304
6305 action
6306 The sources will be treated the same as inputs. See "gn help inputs" for
6307 more information and usage advice.
6308
6309 copy
6310 The source are the source files to copy.
6311```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006312### **testonly**: Declares a target must only be used for testing.
Brett Wilson796ed472018-07-16 15:11:09 -07006313
6314```
6315 Boolean. Defaults to false.
6316
6317 When a target is marked "testonly = true", it must only be depended on by
6318 other test-only targets. Otherwise, GN will issue an error that the
6319 depenedency is not allowed.
6320
6321 This feature is intended to prevent accidentally shipping test code in a
6322 final product.
6323```
6324
6325#### **Example**
6326
6327```
6328 source_set("test_support") {
6329 testonly = true
6330 ...
6331 }
6332```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006333### **visibility**: A list of labels that can depend on a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006334
6335```
6336 A list of labels and label patterns that define which targets can depend on
6337 the current one. These permissions are checked via the "check" command (see
6338 "gn help check").
6339
6340 If visibility is not defined, it defaults to public ("*").
6341
6342 If visibility is defined, only the targets with labels that match it can
6343 depend on the current target. The empty list means no targets can depend on
6344 the current target.
6345
6346 Tip: Often you will want the same visibility for all targets in a BUILD file.
6347 In this case you can just put the definition at the top, outside of any
6348 target, and the targets will inherit that scope and see the definition.
6349```
6350
6351#### **Patterns**
6352
6353```
6354 See "gn help label_pattern" for more details on what types of patterns are
6355 supported. If a toolchain is specified, only targets in that toolchain will
6356 be matched. If a toolchain is not specified on a pattern, targets in all
6357 toolchains will be matched.
6358```
6359
6360#### **Examples**
6361
6362```
6363 Only targets in the current buildfile ("private"):
6364 visibility = [ ":*" ]
6365
6366 No targets (used for targets that should be leaf nodes):
6367 visibility = []
6368
6369 Any target ("public", the default):
6370 visibility = [ "*" ]
6371
6372 All targets in the current directory and any subdirectory:
6373 visibility = [ "./*" ]
6374
6375 Any target in "//bar/BUILD.gn":
6376 visibility = [ "//bar:*" ]
6377
6378 Any target in "//bar/" or any subdirectory thereof:
6379 visibility = [ "//bar/*" ]
6380
6381 Just these specific targets:
6382 visibility = [ ":mything", "//foo:something_else" ]
6383
6384 Any target in the current directory and any subdirectory thereof, plus
6385 any targets in "//bar/" and any subdirectory thereof.
6386 visibility = [ "./*", "//bar/*" ]
6387```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006388### **walk_keys**: Key(s) for managing the metadata collection walk.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006389
6390```
Julie Hockett152c5142019-07-12 09:53:43 -06006391 Defaults to [""].
Nate Fischer8ed01d32019-01-08 17:32:01 -08006392
6393 These keys are used to control the next step in a collection walk, acting as
6394 barriers. If a specified key is defined in a target's metadata, the walk will
6395 use the targets listed in that value to determine which targets are walked.
6396
Julie Hockett152c5142019-07-12 09:53:43 -06006397 If no walk_keys are specified for a generated_file target (i.e. "[""]"), the
Nate Fischer8ed01d32019-01-08 17:32:01 -08006398 walk will touch all deps and data_deps of the specified target recursively.
6399
6400 See "gn help generated_file".
6401```
Robert Sesekd0a6f072020-05-15 11:21:22 -04006402### **weak_frameworks**: [name list] Name of frameworks that must be weak linked.
6403
6404```
6405 A list of framework names.
6406
6407 The frameworks named in that list will be weak linked with any dynamic link
6408 type target. Weak linking instructs the dynamic loader to attempt to load
6409 the framework, but if it is not able to do so, it leaves any imported symbols
6410 unresolved. This is typically used when a framework is present in a new
6411 version of an SDK but not on older versions of the OS that the software runs
6412 on.
6413```
6414
6415#### **Ordering of flags and values**
6416
6417```
6418 1. Those set on the current target (not in a config).
6419 2. Those set on the "configs" on the target in order that the
6420 configs appear in the list.
6421 3. Those set on the "all_dependent_configs" on the target in order
6422 that the configs appear in the list.
6423 4. Those set on the "public_configs" on the target in order that
6424 those configs appear in the list.
6425 5. all_dependent_configs pulled from dependencies, in the order of
6426 the "deps" list. This is done recursively. If a config appears
6427 more than once, only the first occurence will be used.
6428 6. public_configs pulled from dependencies, in the order of the
6429 "deps" list. If a dependency is public, they will be applied
6430 recursively.
6431```
6432
6433#### **Example**
6434
6435```
6436 weak_frameworks = [ "OnlyOnNewerOSes.framework" ]
6437```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006438### **write_runtime_deps**: Writes the target's runtime_deps to the given path.
Brett Wilson796ed472018-07-16 15:11:09 -07006439
6440```
6441 Does not synchronously write the file, but rather schedules it to be written
6442 at the end of generation.
6443
6444 If the file exists and the contents are identical to that being written, the
6445 file will not be updated. This will prevent unnecessary rebuilds of targets
6446 that depend on this file.
6447
6448 Path must be within the output directory.
6449
6450 See "gn help runtime_deps" for how the runtime dependencies are computed.
6451
6452 The format of this file will list one file per line with no escaping. The
6453 files will be relative to the root_build_dir. The first line of the file will
6454 be the main output file of the target itself. The file contents will be the
6455 same as requesting the runtime deps be written on the command line (see "gn
6456 help --runtime-deps-list-file").
6457```
Harley Li0a9affb2020-06-03 10:38:42 -04006458### **xcasset_compiler_flags**: Flags passed to xcassets compiler.
6459
6460```
6461 A list of strings.
6462
6463 Valid for create_bundle target. Those flags are directly passed to
6464 xcassets compiler, corresponding to {{xcasset_compiler_flags}} substitution
6465 in compile_xcassets tool.
6466```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006467### **xcode_extra_attributes**: [scope] Extra attributes for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006468
6469```
6470 The value defined in this scope will be copied to the EXTRA_ATTRIBUTES
6471 property of the generated Xcode project. They are only meaningful when
6472 generating with --ide=xcode.
6473
6474 See "gn help create_bundle" for more information.
6475```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006476### **xcode_test_application_name**: Name for Xcode test target.
Brett Wilson796ed472018-07-16 15:11:09 -07006477
6478```
6479 Each unit and ui test target must have a test application target, and this
6480 value is used to specify the relationship. Only meaningful to Xcode (used as
6481 part of the Xcode project generation).
6482
6483 See "gn help create_bundle" for more information.
6484```
6485
Julie Hockette2a29402018-07-31 10:11:42 -07006486#### **Example**
Brett Wilson796ed472018-07-16 15:11:09 -07006487
6488```
6489 create_bundle("chrome_xctest") {
6490 test_application_name = "chrome"
6491 ...
6492 }
6493```
6494## Other help topics
6495
6496### **Build Arguments Overview**
6497
6498```
6499 Build arguments are variables passed in from outside of the build that build
6500 files can query to determine how the build works.
6501```
6502
6503#### **How build arguments are set**
6504
6505```
6506 First, system default arguments are set based on the current system. The
6507 built-in arguments are:
6508 - host_cpu
6509 - host_os
6510 - current_cpu
6511 - current_os
6512 - target_cpu
6513 - target_os
6514
6515 Next, project-specific overrides are applied. These are specified inside
6516 the default_args variable of //.gn. See "gn help dotfile" for more.
6517
6518 If specified, arguments from the --args command line flag are used. If that
6519 flag is not specified, args from previous builds in the build directory will
6520 be used (this is in the file args.gn in the build directory).
6521
6522 Last, for targets being compiled with a non-default toolchain, the toolchain
6523 overrides are applied. These are specified in the toolchain_args section of a
6524 toolchain definition. The use-case for this is that a toolchain may be
6525 building code for a different platform, and that it may want to always
6526 specify Posix, for example. See "gn help toolchain" for more.
6527
6528 If you specify an override for a build argument that never appears in a
6529 "declare_args" call, a nonfatal error will be displayed.
6530```
6531
6532#### **Examples**
6533
6534```
6535 gn args out/FooBar
6536 Create the directory out/FooBar and open an editor. You would type
6537 something like this into that file:
6538 enable_doom_melon=false
6539 os="android"
6540
6541 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
6542 This will overwrite the build directory with the given arguments. (Note
6543 that the quotes inside the args command will usually need to be escaped
6544 for your shell to pass through strings values.)
6545```
6546
6547#### **How build arguments are used**
6548
6549```
6550 If you want to use an argument, you use declare_args() and specify default
6551 values. These default values will apply if none of the steps listed in the
6552 "How build arguments are set" section above apply to the given argument, but
6553 the defaults will not override any of these.
6554
6555 Often, the root build config file will declare global arguments that will be
6556 passed to all buildfiles. Individual build files can also specify arguments
6557 that apply only to those files. It is also useful to specify build args in an
6558 "import"-ed file if you want such arguments to apply to multiple buildfiles.
6559```
6560### **.gn file**
6561
6562```
6563 When gn starts, it will search the current directory and parent directories
6564 for a file called ".gn". This indicates the source root. You can override
6565 this detection by using the --root command-line argument
6566
6567 The .gn file in the source root will be executed. The syntax is the same as a
6568 buildfile, but with very limited build setup-specific meaning.
6569
6570 If you specify --root, by default GN will look for the file .gn in that
6571 directory. If you want to specify a different file, you can additionally pass
6572 --dotfile:
6573
6574 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
6575```
6576
6577#### **Variables**
6578
6579```
6580 arg_file_template [optional]
6581 Path to a file containing the text that should be used as the default
6582 args.gn content when you run `gn args`.
6583
6584 buildconfig [required]
6585 Path to the build config file. This file will be used to set up the
6586 build file execution environment for each toolchain.
6587
6588 check_targets [optional]
6589 A list of labels and label patterns that should be checked when running
6590 "gn check" or "gn gen --check". If unspecified, all targets will be
Nate Fischer8ed01d32019-01-08 17:32:01 -08006591 checked. If it is the empty list, no targets will be checked. To
6592 bypass this list, request an explicit check of targets, like "//*".
Brett Wilson796ed472018-07-16 15:11:09 -07006593
6594 The format of this list is identical to that of "visibility" so see "gn
6595 help visibility" for examples.
6596
James Robinson49f59032020-01-08 14:05:51 -08006597 check_system_includes [optional]
6598 Boolean to control whether system style includes are checked by default
6599 when running "gn check" or "gn gen --check". System style includes are
6600 includes that use angle brackets <> instead of double quotes "". If this
6601 setting is omitted or set to false, these includes will be ignored by
6602 default. They can be checked explicitly by running
6603 "gn check --check-system" or "gn gen --check=system"
6604
Brett Wilson796ed472018-07-16 15:11:09 -07006605 exec_script_whitelist [optional]
6606 A list of .gn/.gni files (not labels) that have permission to call the
6607 exec_script function. If this list is defined, calls to exec_script will
6608 be checked against this list and GN will fail if the current file isn't
6609 in the list.
6610
6611 This is to allow the use of exec_script to be restricted since is easy to
6612 use inappropriately. Wildcards are not supported. Files in the
6613 secondary_source tree (if defined) should be referenced by ignoring the
6614 secondary tree and naming them as if they are in the main tree.
6615
6616 If unspecified, the ability to call exec_script is unrestricted.
6617
6618 Example:
6619 exec_script_whitelist = [
6620 "//base/BUILD.gn",
6621 "//build/my_config.gni",
6622 ]
6623
6624 root [optional]
6625 Label of the root build target. The GN build will start by loading the
6626 build file containing this target name. This defaults to "//:" which will
Harley Li0a9affb2020-06-03 10:38:42 -04006627 cause the file //BUILD.gn to be loaded. Note that build_file_extension
6628 applies to the default case as well.
Brett Wilson796ed472018-07-16 15:11:09 -07006629
6630 script_executable [optional]
James Robinson61377e32020-02-13 15:20:07 -08006631 Path to specific Python executable or other interpreter to use in
6632 action targets and exec_script calls. By default GN searches the
6633 PATH for Python to execute these scripts.
6634
6635 If set to the empty string, the path specified in action targets
6636 and exec_script calls will be executed directly.
Brett Wilson796ed472018-07-16 15:11:09 -07006637
6638 secondary_source [optional]
6639 Label of an alternate directory tree to find input files. When searching
6640 for a BUILD.gn file (or the build config file discussed above), the file
6641 will first be looked for in the source root. If it's not found, the
6642 secondary source root will be checked (which would contain a parallel
6643 directory hierarchy).
6644
6645 This behavior is intended to be used when BUILD.gn files can't be checked
6646 in to certain source directories for whatever reason.
6647
6648 The secondary source root must be inside the main source tree.
6649
6650 default_args [optional]
6651 Scope containing the default overrides for declared arguments. These
6652 overrides take precedence over the default values specified in the
6653 declare_args() block, but can be overriden using --args or the
6654 args.gn file.
6655
6656 This is intended to be used when subprojects declare arguments with
6657 default values that need to be changed for whatever reason.
Harley Li0a9affb2020-06-03 10:38:42 -04006658
6659 build_file_extension [optional]
6660 If set to a non-empty string, this is added to the name of all build files
6661 to load.
6662 GN will look for build files named "BUILD.$build_file_extension.gn".
6663 This is intended to be used during migrations or other situations where
6664 there are two independent GN builds in the same directories.
Brett Wilson796ed472018-07-16 15:11:09 -07006665```
6666
6667#### **Example .gn file contents**
6668
6669```
6670 buildconfig = "//build/config/BUILDCONFIG.gn"
6671
6672 check_targets = [
6673 "//doom_melon/*", # Check everything in this subtree.
6674 "//tools:mind_controlling_ant", # Check this specific target.
6675 ]
6676
6677 root = "//:root"
6678
6679 secondary_source = "//build/config/temporary_buildfiles/"
6680
6681 default_args = {
6682 # Default to release builds for this project.
6683 is_debug = false
6684 is_component_build = false
6685 }
6686```
6687### **Build graph and execution overview**
6688
6689#### **Overall build flow**
6690
6691```
6692 1. Look for ".gn" file (see "gn help dotfile") in the current directory and
6693 walk up the directory tree until one is found. Set this directory to be
6694 the "source root" and interpret this file to find the name of the build
6695 config file.
6696
6697 2. Execute the build config file identified by .gn to set up the global
6698 variables and default toolchain name. Any arguments, variables, defaults,
6699 etc. set up in this file will be visible to all files in the build.
6700
6701 3. Load the //BUILD.gn (in the source root directory).
6702
6703 4. Recursively evaluate rules and load BUILD.gn in other directories as
6704 necessary to resolve dependencies. If a BUILD file isn't found in the
6705 specified location, GN will look in the corresponding location inside
6706 the secondary_source defined in the dotfile (see "gn help dotfile").
6707
6708 5. When a target's dependencies are resolved, write out the `.ninja`
6709 file to disk.
6710
6711 6. When all targets are resolved, write out the root build.ninja file.
Harley Li0a9affb2020-06-03 10:38:42 -04006712
6713 Note that the BUILD.gn file name may be modulated by .gn arguments such as
6714 build_file_extension.
Brett Wilson796ed472018-07-16 15:11:09 -07006715```
6716
6717#### **Executing target definitions and templates**
6718
6719```
6720 Build files are loaded in parallel. This means it is impossible to
6721 interrogate a target from GN code for any information not derivable from its
6722 label (see "gn help label"). The exception is the get_target_outputs()
6723 function which requires the target being interrogated to have been defined
6724 previously in the same file.
6725
6726 Targets are declared by their type and given a name:
6727
6728 static_library("my_static_library") {
6729 ... target parameter definitions ...
6730 }
6731
6732 There is also a generic "target" function for programmatically defined types
6733 (see "gn help target"). You can define new types using templates (see "gn
6734 help template"). A template defines some custom code that expands to one or
6735 more other targets.
6736
6737 Before executing the code inside the target's { }, the target defaults are
6738 applied (see "gn help set_defaults"). It will inject implicit variable
6739 definitions that can be overridden by the target code as necessary. Typically
6740 this mechanism is used to inject a default set of configs that define the
6741 global compiler and linker flags.
6742```
6743
6744#### **Which targets are built**
6745
6746```
6747 All targets encountered in the default toolchain (see "gn help toolchain")
6748 will have build rules generated for them, even if no other targets reference
6749 them. Their dependencies must resolve and they will be added to the implicit
6750 "all" rule (see "gn help ninja_rules").
6751
6752 Targets in non-default toolchains will only be generated when they are
6753 required (directly or transitively) to build a target in the default
6754 toolchain.
6755
6756 See also "gn help ninja_rules".
6757```
6758
6759#### **Dependencies**
6760
6761```
6762 The only difference between "public_deps" and "deps" except for pushing
6763 configs around the build tree and allowing includes for the purposes of "gn
6764 check".
6765
6766 A target's "data_deps" are guaranteed to be built whenever the target is
6767 built, but the ordering is not defined. The meaning of this is dependencies
6768 required at runtime. Currently data deps will be complete before the target
6769 is linked, but this is not semantically guaranteed and this is undesirable
6770 from a build performance perspective. Since we hope to change this in the
6771 future, do not rely on this behavior.
6772```
6773### **Language and grammar for GN build files**
6774
6775#### **Tokens**
6776
6777```
6778 GN build files are read as sequences of tokens. While splitting the file
6779 into tokens, the next token is the longest sequence of characters that form a
6780 valid token.
6781```
6782
6783#### **White space and comments**
6784
6785```
6786 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
6787 carriage returns (U+000D), and newlines (U+000A).
6788
6789 Comments start at the character "#" and stop at the next newline.
6790
6791 White space and comments are ignored except that they may separate tokens
6792 that would otherwise combine into a single token.
6793```
6794
6795#### **Identifiers**
6796
6797```
6798 Identifiers name variables and functions.
6799
6800 identifier = letter { letter | digit } .
6801 letter = "A" ... "Z" | "a" ... "z" | "_" .
6802 digit = "0" ... "9" .
6803```
6804
6805#### **Keywords**
6806
6807```
6808 The following keywords are reserved and may not be used as identifiers:
6809
6810 else false if true
6811```
6812
6813#### **Integer literals**
6814
6815```
6816 An integer literal represents a decimal integer value.
6817
6818 integer = [ "-" ] digit { digit } .
6819
6820 Leading zeros and negative zero are disallowed.
6821```
6822
6823#### **String literals**
6824
6825```
6826 A string literal represents a string value consisting of the quoted
6827 characters with possible escape sequences and variable expansions.
6828
6829 string = `"` { char | escape | expansion } `"` .
6830 escape = `\` ( "$" | `"` | char ) .
6831 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess "
6832 ") "}" .
6833 Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f]
6834 expansion = "$" ( identifier | BracketExpansion | Hex ) .
6835 char = /* any character except "$", `"`, or newline "
6836 "*/ .
6837
6838 After a backslash, certain sequences represent special characters:
6839
6840 \" U+0022 quotation mark
6841 \$ U+0024 dollar sign
6842 \\ U+005C backslash
6843
6844 All other backslashes represent themselves.
6845
6846 To insert an arbitrary byte value, use $0xFF. For example, to insert a
6847 newline character: "Line one$0x0ALine two".
6848
6849 An expansion will evaluate the variable following the '$' and insert a
6850 stringified version of it into the result. For example, to concat two path
6851 components with a slash separating them:
6852 "$var_one/$var_two"
6853 Use the "${var_one}" format to be explicitly deliniate the variable for
6854 otherwise-ambiguous cases.
6855```
6856
6857#### **Punctuation**
6858
6859```
6860 The following character sequences represent punctuation:
6861
6862 + += == != ( )
6863 - -= < <= [ ]
6864 ! = > >= { }
6865 && || . ,
6866```
6867
6868#### **Grammar**
6869
6870```
6871 The input tokens form a syntax tree following a context-free grammar:
6872
6873 File = StatementList .
6874
6875 Statement = Assignment | Call | Condition .
6876 LValue = identifier | ArrayAccess | ScopeAccess .
6877 Assignment = LValue AssignOp Expr .
6878 Call = identifier "(" [ ExprList ] ")" [ Block ] .
6879 Condition = "if" "(" Expr ")" Block
6880 [ "else" ( Condition | Block ) ] .
6881 Block = "{" StatementList "}" .
6882 StatementList = { Statement } .
6883
6884 ArrayAccess = identifier "[" Expr "]" .
6885 ScopeAccess = identifier "." identifier .
6886 Expr = UnaryExpr | Expr BinaryOp Expr .
6887 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
6888 PrimaryExpr = identifier | integer | string | Call
6889 | ArrayAccess | ScopeAccess | Block
6890 | "(" Expr ")"
6891 | "[" [ ExprList [ "," ] ] "]" .
6892 ExprList = Expr { "," Expr } .
6893
6894 AssignOp = "=" | "+=" | "-=" .
6895 UnaryOp = "!" .
6896 BinaryOp = "+" | "-" // highest priority
6897 | "<" | "<=" | ">" | ">="
6898 | "==" | "!="
6899 | "&&"
6900 | "||" . // lowest priority
6901
6902 All binary operators are left-associative.
6903```
6904
6905#### **Types**
6906
6907```
6908 The GN language is dynamically typed. The following types are used:
6909
6910 - Boolean: Uses the keywords "true" and "false". There is no implicit
6911 conversion between booleans and integers.
6912
6913 - Integers: All numbers in GN are signed 64-bit integers.
6914
6915 - Strings: Strings are 8-bit with no enforced encoding. When a string is
6916 used to interact with other systems with particular encodings (like the
6917 Windows and Mac filesystems) it is assumed to be UTF-8. See "String
6918 literals" above for more.
6919
6920 - Lists: Lists are arbitrary-length ordered lists of values. See "Lists"
6921 below for more.
6922
6923 - Scopes: Scopes are like dictionaries that use variable names for keys. See
6924 "Scopes" below for more.
6925```
6926
6927#### **Lists**
6928
6929```
6930 Lists are created with [] and using commas to separate items:
6931
6932 mylist = [ 0, 1, 2, "some string" ]
6933
6934 A comma after the last item is optional. Lists are dereferenced using 0-based
6935 indexing:
6936
6937 mylist[0] += 1
6938 var = mylist[2]
6939
6940 Lists can be concatenated using the '+' and '+=' operators. Bare values can
6941 not be concatenated with lists, to add a single item, it must be put into a
6942 list of length one.
6943
6944 Items can be removed from lists using the '-' and '-=' operators. This will
6945 remove all occurrences of every item in the right-hand list from the
6946 left-hand list. It is an error to remove an item not in the list. This is to
6947 prevent common typos and to detect dead code that is removing things that no
6948 longer apply.
6949
6950 It is an error to use '=' to replace a nonempty list with another nonempty
6951 list. This is to prevent accidentally overwriting data when in most cases
6952 '+=' was intended. To overwrite a list on purpose, first assign it to the
6953 empty list:
6954
6955 mylist = []
6956 mylist = otherlist
6957
6958 When assigning to a list named 'sources' using '=' or '+=', list items may be
6959 automatically filtered out. See "gn help set_sources_assignment_filter" for
6960 more.
6961```
6962
6963#### **Scopes**
6964
6965```
6966 All execution happens in the context of a scope which holds the current state
6967 (like variables). With the exception of loops and conditions, '{' introduces
6968 a new scope that has a parent reference to the old scope.
6969
6970 Variable reads recursively search all nested scopes until the variable is
6971 found or there are no more scopes. Variable writes always go into the current
6972 scope. This means that after the closing '}' (again excepting loops and
6973 conditions), all local variables will be restored to the previous values.
6974 This also means that "foo = foo" can do useful work by copying a variable
6975 into the current scope that was defined in a containing scope.
6976
6977 Scopes can also be assigned to variables. Such scopes can be created by
6978 functions like exec_script, when invoking a template (the template code
6979 refers to the variables set by the invoking code by the implicitly-created
6980 "invoker" scope), or explicitly like:
6981
6982 empty_scope = {}
6983 myvalues = {
6984 foo = 21
6985 bar = "something"
6986 }
6987
6988 Inside such a scope definition can be any GN code including conditionals and
6989 function calls. After the close of the scope, it will contain all variables
6990 explicitly set by the code contained inside it. After this, the values can be
6991 read, modified, or added to:
6992
6993 myvalues.foo += 2
6994 empty_scope.new_thing = [ 1, 2, 3 ]
Nate Fischer8ed01d32019-01-08 17:32:01 -08006995
6996 Scope equality is defined as single-level scopes identical within the current
6997 scope. That is, all values in the first scope must be present and identical
6998 within the second, and vice versa. Note that this means inherited scopes are
6999 always unequal by definition.
Brett Wilson796ed472018-07-16 15:11:09 -07007000```
Julie Hockettd69a9c32019-01-23 14:36:18 -08007001### **Input and output conversion**
Brett Wilson796ed472018-07-16 15:11:09 -07007002
7003```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007004 Input and output conversions are arguments to file and process functions
7005 that specify how to convert data to or from external formats. The possible
7006 values for parameters specifying conversions are:
7007
Brett Wilson796ed472018-07-16 15:11:09 -07007008 "" (the default)
Julie Hockett09171292018-07-31 14:35:10 -07007009 input: Discard the result and return None.
7010
7011 output: If value is a list, then "list lines"; otherwise "value".
Brett Wilson796ed472018-07-16 15:11:09 -07007012
7013 "list lines"
Julie Hockett09171292018-07-31 14:35:10 -07007014 input:
7015 Return the file contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007016 newlines will not be present in the result. The last line may or may
7017 not end in a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007018
Julie Hockett09171292018-07-31 14:35:10 -07007019 After splitting, each individual line will be trimmed of whitespace on
7020 both ends.
7021
7022 output:
7023 Renders the value contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007024 newlines will not be present in the result. The last line will end in
7025 with a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007026
7027 "scope"
Julie Hockett09171292018-07-31 14:35:10 -07007028 input:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007029 Execute the block as GN code and return a scope with the resulting
7030 values in it. If the input was:
Julie Hockett09171292018-07-31 14:35:10 -07007031 a = [ "hello.cc", "world.cc" ]
7032 b = 26
7033 and you read the result into a variable named "val", then you could
7034 access contents the "." operator on "val":
7035 sources = val.a
7036 some_count = val.b
7037
7038 output:
7039 Renders the value contents as a GN code block, reversing the input
7040 result above.
Brett Wilson796ed472018-07-16 15:11:09 -07007041
7042 "string"
Julie Hockett09171292018-07-31 14:35:10 -07007043 input: Return the file contents into a single string.
7044
7045 output:
7046 Render the value contents into a single string. The output is:
7047 a string renders with quotes, e.g. "str"
7048 an integer renders as a stringified integer, e.g. "6"
7049 a boolean renders as the associated string, e.g. "true"
7050 a list renders as a representation of its contents, e.g. "[\"str\", 6]"
7051 a scope renders as a GN code block of its values. If the Value was:
7052 Value val;
7053 val.a = [ "hello.cc", "world.cc" ];
7054 val.b = 26
7055 the resulting output would be:
7056 "{
7057 a = [ \"hello.cc\", \"world.cc\" ]
7058 b = 26
7059 }"
Brett Wilson796ed472018-07-16 15:11:09 -07007060
7061 "value"
Julie Hockett09171292018-07-31 14:35:10 -07007062 input:
7063 Parse the input as if it was a literal rvalue in a buildfile. Examples of
7064 typical program output using this mode:
7065 [ "foo", "bar" ] (result will be a list)
7066 or
7067 "foo bar" (result will be a string)
7068 or
7069 5 (result will be an integer)
Brett Wilson796ed472018-07-16 15:11:09 -07007070
Julie Hockett09171292018-07-31 14:35:10 -07007071 Note that if the input is empty, the result will be a null value which
7072 will produce an error if assigned to a variable.
7073
7074 output:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007075 Render the value contents as a literal rvalue. Strings render with
7076 escaped quotes.
Brett Wilson796ed472018-07-16 15:11:09 -07007077
7078 "json"
Julie Hockett09171292018-07-31 14:35:10 -07007079 input: Parse the input as a JSON and convert it to equivalent GN rvalue.
7080
7081 output: Convert the Value to equivalent JSON value.
7082
7083 The data type mapping is:
Brett Wilson796ed472018-07-16 15:11:09 -07007084 a string in JSON maps to string in GN
7085 an integer in JSON maps to integer in GN
7086 a float in JSON is unsupported and will result in an error
7087 an object in JSON maps to scope in GN
7088 an array in JSON maps to list in GN
7089 a boolean in JSON maps to boolean in GN
7090 a null in JSON is unsupported and will result in an error
7091
Julie Hockett09171292018-07-31 14:35:10 -07007092 Nota that the input dictionary keys have to be valid GN identifiers
7093 otherwise they will produce an error.
Brett Wilson796ed472018-07-16 15:11:09 -07007094
Julie Hockett09171292018-07-31 14:35:10 -07007095 "trim ..." (input only)
Brett Wilson796ed472018-07-16 15:11:09 -07007096 Prefixing any of the other transformations with the word "trim" will
7097 result in whitespace being trimmed from the beginning and end of the
7098 result before processing.
7099
7100 Examples: "trim string" or "trim list lines"
7101
7102 Note that "trim value" is useless because the value parser skips
7103 whitespace anyway.
7104```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01007105### **File patterns**
7106
7107```
7108 File patterns are VERY limited regular expressions. They must match the
7109 entire input string to be counted as a match. In regular expression parlance,
7110 there is an implicit "^...$" surrounding your input. If you want to match a
7111 substring, you need to use wildcards at the beginning and end.
7112
7113 There are only two special tokens understood by the pattern matcher.
7114 Everything else is a literal.
7115
7116 - "*" Matches zero or more of any character. It does not depend on the
7117 preceding character (in regular expression parlance it is equivalent to
7118 ".*").
7119
7120 - "\b" Matches a path boundary. This will match the beginning or end of a
7121 string, or a slash.
7122```
7123
7124#### **Pattern examples**
7125
7126```
7127 "*asdf*"
7128 Matches a string containing "asdf" anywhere.
7129
7130 "asdf"
7131 Matches only the exact string "asdf".
7132
7133 "*.cc"
7134 Matches strings ending in the literal ".cc".
7135
7136 "\bwin/*"
7137 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
7138```
Brett Wilson796ed472018-07-16 15:11:09 -07007139### **Label patterns**
7140
7141```
7142 A label pattern is a way of expressing one or more labels in a portion of the
7143 source tree. They are not general regular expressions.
7144
7145 They can take the following forms only:
7146
7147 - Explicit (no wildcard):
7148 "//foo/bar:baz"
7149 ":baz"
7150
7151 - Wildcard target names:
7152 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
7153 ":*" (all targets in the current build file)
7154
7155 - Wildcard directory names ("*" is only supported at the end)
7156 "*" (all targets)
7157 "//foo/bar/*" (all targets in any subdir of //foo/bar)
7158 "./*" (all targets in the current build file or sub dirs)
7159
Julie Hockette2a29402018-07-31 10:11:42 -07007160 Any of the above forms can additionally take an explicit toolchain
7161 in parenthesis at the end of the label pattern. In this case, the
7162 toolchain must be fully qualified (no wildcards are supported in the
7163 toolchain name).
Brett Wilson796ed472018-07-16 15:11:09 -07007164
Julie Hockette2a29402018-07-31 10:11:42 -07007165 "//foo:bar(//build/toolchain:mac)"
Brett Wilson796ed472018-07-16 15:11:09 -07007166 An explicit target in an explicit toolchain.
7167
7168 ":*(//build/toolchain/linux:32bit)"
7169 All targets in the current build file using the 32-bit Linux toolchain.
7170
7171 "//foo/*(//build/toolchain:win)"
7172 All targets in //foo and any subdirectory using the Windows
7173 toolchain.
7174```
7175### **About labels**
7176
7177```
7178 Everything that can participate in the dependency graph (targets, configs,
7179 and toolchains) are identified by labels. A common label looks like:
7180
7181 //base/test:test_support
7182
7183 This consists of a source-root-absolute path, a colon, and a name. This means
7184 to look for the thing named "test_support" in "base/test/BUILD.gn".
7185
7186 You can also specify system absolute paths if necessary. Typically such
7187 paths would be specified via a build arg so the developer can specify where
7188 the component is on their system.
7189
7190 /usr/local/foo:bar (Posix)
7191 /C:/Program Files/MyLibs:bar (Windows)
7192```
7193
7194#### **Toolchains**
7195
7196```
7197 A canonical label includes the label of the toolchain being used. Normally,
7198 the toolchain label is implicitly inherited from the current execution
7199 context, but you can override this to specify cross-toolchain dependencies:
7200
7201 //base/test:test_support(//build/toolchain/win:msvc)
7202
7203 Here GN will look for the toolchain definition called "msvc" in the file
7204 "//build/toolchain/win" to know how to compile this target.
7205```
7206
7207#### **Relative labels**
7208
7209```
7210 If you want to refer to something in the same buildfile, you can omit
7211 the path name and just start with a colon. This format is recommended for
7212 all same-file references.
7213
7214 :base
7215
7216 Labels can be specified as being relative to the current directory.
7217 Stylistically, we prefer to use absolute paths for all non-file-local
7218 references unless a build file needs to be run in different contexts (like a
7219 project needs to be both standalone and pulled into other projects in
7220 difference places in the directory hierarchy).
7221
7222 source/plugin:myplugin
7223 ../net:url_request
7224```
7225
7226#### **Implicit names**
7227
7228```
7229 If a name is unspecified, it will inherit the directory name. Stylistically,
7230 we prefer to omit the colon and name when possible:
7231
7232 //net -> //net:net
7233 //tools/gn -> //tools/gn:gn
7234```
Julie Hockett152c5142019-07-12 09:53:43 -06007235### **Metadata Collection**
7236
7237```
7238 Metadata is information attached to targets throughout the dependency tree. GN
7239 allows for the collection of this data into files written during the generation
Keir Mierle45611e32019-11-12 11:18:00 -08007240 step, enabling users to expose and aggregate this data based on the dependency
Julie Hockett152c5142019-07-12 09:53:43 -06007241 tree.
7242```
7243
7244#### **generated_file targets**
7245
7246```
7247 Similar to the write_file() function, the generated_file target type
7248 creates a file in the specified location with the specified content. The
Keir Mierle45611e32019-11-12 11:18:00 -08007249 primary difference between write_file() and this target type is that the
Julie Hockett152c5142019-07-12 09:53:43 -06007250 write_file function does the file write at parse time, while the
7251 generated_file target type writes at target resolution time. See
7252 "gn help generated_file" for more detail.
7253
Keir Mierle45611e32019-11-12 11:18:00 -08007254 When written at target resolution time, generated_file enables GN to
Julie Hockett152c5142019-07-12 09:53:43 -06007255 collect and write aggregated metadata from dependents.
7256
Keir Mierle45611e32019-11-12 11:18:00 -08007257 A generated_file target can declare either 'contents' to write statically
7258 known contents to a file or 'data_keys' to aggregate metadata and write the
7259 result to a file. It can also specify 'walk_keys' (to restrict the metadata
Julie Hockett152c5142019-07-12 09:53:43 -06007260 collection), 'output_conversion', and 'rebase'.
7261```
7262
7263#### **Collection and Aggregation**
7264
7265```
7266 Targets can declare a 'metadata' variable containing a scope, and this
Keir Mierle45611e32019-11-12 11:18:00 -08007267 metadata may be collected and written out to a file specified by
7268 generated_file aggregation targets. The 'metadata' scope must contain
7269 only list values since the aggregation step collects a list of these values.
Julie Hockett152c5142019-07-12 09:53:43 -06007270
7271 During the target resolution, generated_file targets will walk their
7272 dependencies recursively, collecting metadata based on the specified
7273 'data_keys'. 'data_keys' is specified as a list of strings, used by the walk
7274 to identify which variables in dependencies' 'metadata' scopes to collect.
7275
Keir Mierle45611e32019-11-12 11:18:00 -08007276 The walk begins with the listed dependencies of the 'generated_file' target.
7277 The 'metadata' scope for each dependency is inspected for matching elements
7278 of the 'generated_file' target's 'data_keys' list. If a match is found, the
7279 data from the dependent's matching key list is appended to the aggregate walk
7280 list. Note that this means that if more than one walk key is specified, the
7281 data in all of them will be aggregated into one list. From there, the walk
7282 will then recurse into the dependencies of each target it encounters,
7283 collecting the specified metadata for each.
Julie Hockett152c5142019-07-12 09:53:43 -06007284
7285 For example:
7286
7287 group("a") {
7288 metadata = {
7289 doom_melon = [ "enable" ]
7290 my_files = [ "foo.cpp" ]
7291 my_extra_files = [ "bar.cpp" ]
7292 }
7293
7294 deps = [ ":b" ]
7295 }
7296
7297 group("b") {
7298 metadata = {
7299 my_files = [ "baz.cpp" ]
7300 }
7301 }
7302
7303 generated_file("metadata") {
7304 outputs = [ "$root_build_dir/my_files.json" ]
7305 data_keys = [ "my_files", "my_extra_files" ]
7306
7307 deps = [ ":a" ]
7308 }
7309
7310 The above will produce the following file data:
7311
7312 foo.cpp
7313 bar.cpp
7314 baz.cpp
7315
Keir Mierle45611e32019-11-12 11:18:00 -08007316 The dependency walk can be limited by using the 'walk_keys'. This is a list of
Julie Hockett152c5142019-07-12 09:53:43 -06007317 labels that should be included in the walk. All labels specified here should
7318 also be in one of the deps lists. These keys act as barriers, where the walk
Keir Mierle45611e32019-11-12 11:18:00 -08007319 will only recurse into the targets listed. An empty list in all specified
Julie Hockett152c5142019-07-12 09:53:43 -06007320 barriers will end that portion of the walk.
7321
7322 group("a") {
7323 metadata = {
7324 my_files = [ "foo.cpp" ]
7325 my_files_barrier [ ":b" ]
7326 }
7327
7328 deps = [ ":b", ":c" ]
7329 }
7330
7331 group("b") {
7332 metadata = {
7333 my_files = [ "bar.cpp" ]
7334 }
7335 }
7336
7337 group("c") {
7338 metadata = {
7339 my_files = [ "doom_melon.cpp" ]
7340 }
7341 }
7342
7343 generated_file("metadata") {
7344 outputs = [ "$root_build_dir/my_files.json" ]
7345 data_keys = [ "my_files", "my_extra_files" ]
7346
7347 deps = [ ":a" ]
7348 }
7349
7350 The above will produce the following file data (note that `doom_melon.cpp` is
7351 not included):
7352
7353 foo.cpp
7354 bar.cpp
7355
7356 A common example of this sort of barrier is in builds that have host tools
7357 built as part of the tree, but do not want the metadata from those host tools
7358 to be collected with the target-side code.
7359```
7360
7361#### **Common Uses**
7362
7363```
7364 Metadata can be used to collect information about the different targets in the
7365 build, and so a common use is to provide post-build tooling with a set of data
7366 necessary to do aggregation tasks. For example, if each test target specifies
7367 the output location of its binary to run in a metadata field, that can be
7368 collected into a single file listing the locations of all tests in the
7369 dependency tree. A local build tool (or continuous integration infrastructure)
7370 can then use that file to know which tests exist, and where, and run them
7371 accordingly.
7372
7373 Another use is in image creation, where a post-build image tool needs to know
7374 various pieces of information about the components it should include in order
7375 to put together the correct image.
7376```
Brett Wilson796ed472018-07-16 15:11:09 -07007377### **Ninja build rules**
7378
7379#### **The "all" and "default" rules**
7380
7381```
7382 All generated targets (see "gn help execution") will be added to an implicit
7383 build rule called "all" so "ninja all" will always compile everything. The
7384 default rule will be used by Ninja if no specific target is specified (just
7385 typing "ninja"). If there is a target named "default" in the root build file,
7386 it will be the default build rule, otherwise the implicit "all" rule will be
7387 used.
7388```
7389
7390#### **Phony rules**
7391
7392```
7393 GN generates Ninja "phony" rules for targets in the default toolchain. The
7394 phony rules can collide with each other and with the names of generated files
7395 so are generated with the following priority:
7396
7397 1. Actual files generated by the build always take precedence.
7398
7399 2. Targets in the toplevel //BUILD.gn file.
7400
7401 3. Targets in toplevel directories matching the names of the directories.
7402 So "ninja foo" can be used to compile "//foo:foo". This only applies to
7403 the first level of directories since usually these are the most
7404 important (so this won't apply to "//foo/bar:bar").
7405
7406 4. The short names of executables if there is only one executable with that
7407 short name. Use "ninja doom_melon" to compile the
7408 "//tools/fruit:doom_melon" executable.
7409
7410 5. The short names of all targets if there is only one target with that
7411 short name.
7412
7413 6. Full label name with no leading slashes. So you can use
7414 "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon".
7415
7416 7. Labels with an implicit name part (when the short names match the
7417 directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar".
7418
7419 These "phony" rules are provided only for running Ninja since this matches
7420 people's historical expectations for building. For consistency with the rest
7421 of the program, GN introspection commands accept explicit labels.
7422
7423 To explicitly compile a target in a non-default toolchain, you must give
7424 Ninja the exact name of the output file relative to the build directory.
7425```
7426### **nogncheck**: Skip an include line from checking.
7427
7428```
7429 GN's header checker helps validate that the includes match the build
7430 dependency graph. Sometimes an include might be conditional or otherwise
7431 problematic, but you want to specifically allow it. In this case, it can be
7432 whitelisted.
7433
7434 Include lines containing the substring "nogncheck" will be excluded from
7435 header checking. The most common case is a conditional include:
7436
7437 #if defined(ENABLE_DOOM_MELON)
7438 #include "tools/doom_melon/doom_melon.h" // nogncheck
7439 #endif
7440
7441 If the build file has a conditional dependency on the corresponding target
7442 that matches the conditional include, everything will always link correctly:
7443
7444 source_set("mytarget") {
7445 ...
7446 if (enable_doom_melon) {
7447 defines = [ "ENABLE_DOOM_MELON" ]
7448 deps += [ "//tools/doom_melon" ]
7449 }
7450
7451 But GN's header checker does not understand preprocessor directives, won't
7452 know it matches the build dependencies, and will flag this include as
7453 incorrect when the condition is false.
7454```
7455
7456#### **More information**
7457
7458```
7459 The topic "gn help check" has general information on how checking works and
7460 advice on fixing problems. Targets can also opt-out of checking, see
7461 "gn help check_includes".
7462```
Brett Wilson796ed472018-07-16 15:11:09 -07007463### **Runtime dependencies**
7464
7465```
7466 Runtime dependencies of a target are exposed via the "runtime_deps" category
7467 of "gn desc" (see "gn help desc") or they can be written at build generation
7468 time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help
7469 --runtime-deps-list-file").
7470
7471 To a first approximation, the runtime dependencies of a target are the set of
7472 "data" files, data directories, and the shared libraries from all transitive
7473 dependencies. Executables, shared libraries, and loadable modules are
7474 considered runtime dependencies of themselves.
7475```
7476
7477#### **Executables**
7478
7479```
7480 Executable targets and those executable targets' transitive dependencies are
7481 not considered unless that executable is listed in "data_deps". Otherwise, GN
7482 assumes that the executable (and everything it requires) is a build-time
7483 dependency only.
7484```
7485
7486#### **Actions and copies**
7487
7488```
7489 Action and copy targets that are listed as "data_deps" will have all of their
7490 outputs and data files considered as runtime dependencies. Action and copy
7491 targets that are "deps" or "public_deps" will have only their data files
7492 considered as runtime dependencies. These targets can list an output file in
7493 both the "outputs" and "data" lists to force an output file as a runtime
7494 dependency in all cases.
7495
7496 The different rules for deps and data_deps are to express build-time (deps)
7497 vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as
7498 data dependencies, there would be a lot of extra stuff, and if GN counted all
7499 run-time dependencies as regular deps, the build's parallelism would be
7500 unnecessarily constrained.
7501
7502 This rule can sometimes lead to unintuitive results. For example, given the
7503 three targets:
7504 A --[data_deps]--> B --[deps]--> ACTION
7505 GN would say that A does not have runtime deps on the result of the ACTION,
7506 which is often correct. But the purpose of the B target might be to collect
7507 many actions into one logic unit, and the "data"-ness of A's dependency is
7508 lost. Solutions:
7509
7510 - List the outputs of the action in its data section (if the results of
7511 that action are always runtime files).
7512 - Have B list the action in data_deps (if the outputs of the actions are
7513 always runtime files).
7514 - Have B list the action in both deps and data deps (if the outputs might be
7515 used in both contexts and you don't care about unnecessary entries in the
7516 list of files required at runtime).
7517 - Split B into run-time and build-time versions with the appropriate "deps"
7518 for each.
7519```
7520
7521#### **Static libraries and source sets**
7522
7523```
7524 The results of static_library or source_set targets are not considered
7525 runtime dependencies since these are assumed to be intermediate targets only.
7526 If you need to list a static library as a runtime dependency, you can
7527 manually compute the .a/.lib file name for the current platform and list it
7528 in the "data" list of a target (possibly on the static library target
7529 itself).
7530```
7531
7532#### **Multiple outputs**
7533
7534```
7535 Linker tools can specify which of their outputs should be considered when
7536 computing the runtime deps by setting runtime_outputs. If this is unset on
7537 the tool, the default will be the first output only.
7538```
7539### **How Source Expansion Works**
7540
7541```
7542 Source expansion is used for the action_foreach and copy target types to map
7543 source file names to output file names or arguments.
7544
7545 To perform source expansion in the outputs, GN maps every entry in the
7546 sources to every entry in the outputs list, producing the cross product of
7547 all combinations, expanding placeholders (see below).
7548
7549 Source expansion in the args works similarly, but performing the placeholder
7550 substitution produces a different set of arguments for each invocation of the
7551 script.
7552
7553 If no placeholders are found, the outputs or args list will be treated as a
7554 static list of literal file names that do not depend on the sources.
7555
7556 See "gn help copy" and "gn help action_foreach" for more on how this is
7557 applied.
7558```
7559
7560#### **Placeholders**
7561
7562```
7563 This section discusses only placeholders for actions. There are other
7564 placeholders used in the definition of tools. See "gn help tool" for those.
7565
7566 {{source}}
7567 The name of the source file including directory (*). This will generally
7568 be used for specifying inputs to a script in the "args" variable.
7569 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
7570
7571 {{source_file_part}}
7572 The file part of the source including the extension.
7573 "//foo/bar/baz.txt" => "baz.txt"
7574
7575 {{source_name_part}}
7576 The filename part of the source file with no directory or extension. This
7577 will generally be used for specifying a transformation from a source file
7578 to a destination file with the same name but different extension.
7579 "//foo/bar/baz.txt" => "baz"
7580
7581 {{source_dir}}
7582 The directory (*) containing the source file with no trailing slash.
7583 "//foo/bar/baz.txt" => "../../foo/bar"
7584
7585 {{source_root_relative_dir}}
7586 The path to the source file's directory relative to the source root, with
7587 no leading "//" or trailing slashes. If the path is system-absolute,
7588 (beginning in a single slash) this will just return the path with no
7589 trailing slash. This value will always be the same, regardless of whether
7590 it appears in the "outputs" or "args" section.
7591 "//foo/bar/baz.txt" => "foo/bar"
7592
7593 {{source_gen_dir}}
7594 The generated file directory (*) corresponding to the source file's path.
7595 This will be different than the target's generated file directory if the
7596 source file is in a different directory than the BUILD.gn file.
7597 "//foo/bar/baz.txt" => "gen/foo/bar"
7598
7599 {{source_out_dir}}
7600 The object file directory (*) corresponding to the source file's path,
7601 relative to the build directory. this us be different than the target's
7602 out directory if the source file is in a different directory than the
7603 build.gn file.
7604 "//foo/bar/baz.txt" => "obj/foo/bar"
7605
7606 {{source_target_relative}}
7607 The path to the source file relative to the target's directory. This will
7608 generally be used for replicating the source directory layout in the
Robert Sesek252674d2019-05-10 14:08:52 -04007609 output directory. This can only be used in actions and bundle_data
7610 targets. It is an error to use in process_file_template where there is no
7611 "target".
Brett Wilson796ed472018-07-16 15:11:09 -07007612 "//foo/bar/baz.txt" => "baz.txt"
7613```
7614
7615#### **(*) Note on directories**
7616
7617```
7618 Paths containing directories (except the source_root_relative_dir) will be
7619 different depending on what context the expansion is evaluated in. Generally
7620 it should "just work" but it means you can't concatenate strings containing
7621 these values with reasonable results.
7622
7623 Details: source expansions can be used in the "outputs" variable, the "args"
7624 variable, and in calls to "process_file_template". The "args" are passed to a
7625 script which is run from the build directory, so these directories will
7626 relative to the build directory for the script to find. In the other cases,
7627 the directories will be source- absolute (begin with a "//") because the
7628 results of those expansions will be handled by GN internally.
7629```
7630
7631#### **Examples**
7632
7633```
7634 Non-varying outputs:
7635 action("hardcoded_outputs") {
7636 sources = [ "input1.idl", "input2.idl" ]
7637 outputs = [ "$target_out_dir/output1.dat",
7638 "$target_out_dir/output2.dat" ]
7639 }
7640 The outputs in this case will be the two literal files given.
7641
7642 Varying outputs:
7643 action_foreach("varying_outputs") {
7644 sources = [ "input1.idl", "input2.idl" ]
7645 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
7646 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
7647 }
7648 Performing source expansion will result in the following output names:
7649 //out/Debug/obj/mydirectory/input1.h
7650 //out/Debug/obj/mydirectory/input1.cc
7651 //out/Debug/obj/mydirectory/input2.h
7652 //out/Debug/obj/mydirectory/input2.cc
7653```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007654### **Available global switches**
Brett Wilson796ed472018-07-16 15:11:09 -07007655
7656```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007657 Do "gn help --the_switch_you_want_help_on" for more. Individual commands may
7658 take command-specific switches not listed here. See the help on your specific
7659 command for more.
7660```
7661```
7662 * --args: Specifies build arguments overrides.
7663 * --color: Force colored output.
7664 * --dotfile: Override the name of the ".gn" file.
7665 * --fail-on-unused-args: Treat unused build args as fatal errors.
7666 * --markdown: Write help output in the Markdown format.
7667 * --nocolor: Force non-colored output.
7668 * -q: Quiet mode. Don't print output on success.
7669 * --root: Explicitly specify source root.
7670 * --runtime-deps-list-file: Save runtime dependencies for targets in file.
7671 * --script-executable: Set the executable used to execute scripts.
7672 * --threads: Specify number of worker threads.
7673 * --time: Outputs a summary of how long everything took.
7674 * --tracelog: Writes a Chrome-compatible trace log to the given file.
7675 * -v: Verbose logging.
7676 * --version: Prints the GN version number and exits.
Brett Wilson796ed472018-07-16 15:11:09 -07007677```
7678