blob: 5b435087220ff3f61735b9954c9d6d4e16b41dc3 [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)
18 * [path: Find paths between two targets.](#cmd_path)
19 * [refs: Find stuff referencing a target or file.](#cmd_refs)
Brett Wilson796ed472018-07-16 15:11:09 -070020* [Target declarations](#targets)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080021 * [action: Declare a target that runs a script a single time.](#func_action)
22 * [action_foreach: Declare a target that runs a script over a set of files.](#func_action_foreach)
23 * [bundle_data: [iOS/macOS] Declare a target without output.](#func_bundle_data)
24 * [copy: Declare a target that copies files.](#func_copy)
25 * [create_bundle: [iOS/macOS] Build an iOS or macOS bundle.](#func_create_bundle)
26 * [executable: Declare an executable target.](#func_executable)
27 * [generated_file: Declare a generated_file target.](#func_generated_file)
28 * [group: Declare a named group of targets.](#func_group)
29 * [loadable_module: Declare a loadable module target.](#func_loadable_module)
Julie Hockettce1fa072019-05-07 17:44:37 -070030 * [rust_library: Declare a Rust library target.](#func_rust_library)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080031 * [shared_library: Declare a shared library target.](#func_shared_library)
32 * [source_set: Declare a source set target.](#func_source_set)
33 * [static_library: Declare a static library target.](#func_static_library)
34 * [target: Declare an target with the given programmatic type.](#func_target)
Brett Wilson796ed472018-07-16 15:11:09 -070035* [Buildfile functions](#functions)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080036 * [assert: Assert an expression is true at generation time.](#func_assert)
37 * [config: Defines a configuration object.](#func_config)
38 * [declare_args: Declare build arguments.](#func_declare_args)
39 * [defined: Returns whether an identifier is defined.](#func_defined)
40 * [exec_script: Synchronously run a script and return the output.](#func_exec_script)
41 * [foreach: Iterate over a list.](#func_foreach)
42 * [forward_variables_from: Copies variables from a different scope.](#func_forward_variables_from)
43 * [get_label_info: Get an attribute from a target's label.](#func_get_label_info)
44 * [get_path_info: Extract parts of a file or directory name.](#func_get_path_info)
45 * [get_target_outputs: [file list] Get the list of outputs from a target.](#func_get_target_outputs)
46 * [getenv: Get an environment variable.](#func_getenv)
47 * [import: Import a file into the current scope.](#func_import)
48 * [not_needed: Mark variables from scope as not needed.](#func_not_needed)
49 * [pool: Defines a pool object.](#func_pool)
50 * [print: Prints to the console.](#func_print)
51 * [process_file_template: Do template expansion over a list of files.](#func_process_file_template)
52 * [read_file: Read a file into a variable.](#func_read_file)
53 * [rebase_path: Rebase a file or directory to another location.](#func_rebase_path)
54 * [set_default_toolchain: Sets the default toolchain name.](#func_set_default_toolchain)
55 * [set_defaults: Set default values for a target type.](#func_set_defaults)
56 * [set_sources_assignment_filter: Set a pattern to filter source files.](#func_set_sources_assignment_filter)
57 * [split_list: Splits a list into N different sub-lists.](#func_split_list)
58 * [string_replace: Replaces substring in the given string.](#func_string_replace)
59 * [template: Define a template rule.](#func_template)
60 * [tool: Specify arguments to a toolchain tool.](#func_tool)
61 * [toolchain: Defines a toolchain.](#func_toolchain)
62 * [write_file: Write a file to disk.](#func_write_file)
Brett Wilson796ed472018-07-16 15:11:09 -070063* [Built-in predefined variables](#predefined_variables)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080064 * [current_cpu: [string] The processor architecture of the current toolchain.](#var_current_cpu)
65 * [current_os: [string] The operating system of the current toolchain.](#var_current_os)
66 * [current_toolchain: [string] Label of the current toolchain.](#var_current_toolchain)
67 * [default_toolchain: [string] Label of the default toolchain.](#var_default_toolchain)
68 * [host_cpu: [string] The processor architecture that GN is running on.](#var_host_cpu)
69 * [host_os: [string] The operating system that GN is running on.](#var_host_os)
70 * [invoker: [string] The invoking scope inside a template.](#var_invoker)
71 * [python_path: [string] Absolute path of Python.](#var_python_path)
72 * [root_build_dir: [string] Directory where build commands are run.](#var_root_build_dir)
73 * [root_gen_dir: [string] Directory for the toolchain's generated files.](#var_root_gen_dir)
74 * [root_out_dir: [string] Root directory for toolchain output files.](#var_root_out_dir)
75 * [target_cpu: [string] The desired cpu architecture for the build.](#var_target_cpu)
76 * [target_gen_dir: [string] Directory for a target's generated files.](#var_target_gen_dir)
77 * [target_name: [string] The name of the current target.](#var_target_name)
78 * [target_os: [string] The desired operating system for the build.](#var_target_os)
79 * [target_out_dir: [string] Directory for target output files.](#var_target_out_dir)
Brett Wilson796ed472018-07-16 15:11:09 -070080* [Variables you set in targets](#target_variables)
Julie Hockettce1fa072019-05-07 17:44:37 -070081 * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080082 * [all_dependent_configs: [label list] Configs to be forced on dependents.](#var_all_dependent_configs)
83 * [allow_circular_includes_from: [label list] Permit includes from deps.](#var_allow_circular_includes_from)
84 * [arflags: [string list] Arguments passed to static_library archiver.](#var_arflags)
85 * [args: [string list] Arguments passed to an action.](#var_args)
86 * [asmflags: [string list] Flags passed to the assembler.](#var_asmflags)
87 * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#var_assert_no_deps)
88 * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#var_bundle_contents_dir)
89 * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#var_bundle_deps_filter)
90 * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#var_bundle_executable_dir)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080091 * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#var_bundle_resources_dir)
92 * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#var_bundle_root_dir)
93 * [cflags: [string list] Flags passed to all C compiler variants.](#var_cflags)
94 * [cflags_c: [string list] Flags passed to the C compiler.](#var_cflags_c)
95 * [cflags_cc: [string list] Flags passed to the C++ compiler.](#var_cflags_cc)
96 * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#var_cflags_objc)
97 * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#var_cflags_objcc)
98 * [check_includes: [boolean] Controls whether a target's files are checked.](#var_check_includes)
99 * [code_signing_args: [string list] Arguments passed to code signing script.](#var_code_signing_args)
100 * [code_signing_outputs: [file list] Output files for code signing step.](#var_code_signing_outputs)
101 * [code_signing_script: [file name] Script for code signing.](#var_code_signing_script)
102 * [code_signing_sources: [file list] Sources for code signing step.](#var_code_signing_sources)
103 * [complete_static_lib: [boolean] Links all deps into a static library.](#var_complete_static_lib)
104 * [configs: [label list] Configs applying to this target or config.](#var_configs)
105 * [contents: Contents to write to file.](#var_contents)
Julie Hockettce1fa072019-05-07 17:44:37 -0700106 * [crate_name: [string] The name for the compiled crate.](#var_crate_name)
107 * [crate_root: [string] The root source file for a binary or library.](#var_crate_root)
108 * [crate_type: [string] The type of linkage to use on a shared_library.](#var_crate_type)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800109 * [data: [file list] Runtime data file dependencies.](#var_data)
110 * [data_deps: [label list] Non-linked dependencies.](#var_data_deps)
111 * [data_keys: [string list] Keys from which to collect metadata.](#var_data_keys)
112 * [defines: [string list] C preprocessor defines.](#var_defines)
113 * [depfile: [string] File name for input dependencies for actions.](#var_depfile)
114 * [deps: [label list] Private linked dependencies.](#var_deps)
Julie Hockettce1fa072019-05-07 17:44:37 -0700115 * [edition: [string] The rustc edition to use in compiliation.](#var_edition)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800116 * [friend: [label pattern list] Allow targets to include private headers.](#var_friend)
117 * [include_dirs: [directory list] Additional include directories.](#var_include_dirs)
118 * [inputs: [file list] Additional compile-time dependencies.](#var_inputs)
119 * [ldflags: [string list] Flags passed to the linker.](#var_ldflags)
120 * [lib_dirs: [directory list] Additional library directories.](#var_lib_dirs)
121 * [libs: [string list] Additional libraries to link.](#var_libs)
122 * [metadata: [scope] Metadata of this target.](#var_metadata)
123 * [output_conversion: Data format for generated_file targets.](#var_output_conversion)
124 * [output_dir: [directory] Directory to put output file in.](#var_output_dir)
125 * [output_extension: [string] Value to use for the output's file extension.](#var_output_extension)
126 * [output_name: [string] Name for the output file other than the default.](#var_output_name)
127 * [output_prefix_override: [boolean] Don't use prefix for output name.](#var_output_prefix_override)
128 * [outputs: [file list] Output files for actions and copy targets.](#var_outputs)
129 * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#var_partial_info_plist)
130 * [pool: [string] Label of the pool used by the action.](#var_pool)
131 * [precompiled_header: [string] Header file to precompile.](#var_precompiled_header)
132 * [precompiled_header_type: [string] "gcc" or "msvc".](#var_precompiled_header_type)
133 * [precompiled_source: [file name] Source file to precompile.](#var_precompiled_source)
134 * [product_type: [string] Product type for Xcode projects.](#var_product_type)
135 * [public: [file list] Declare public header files for a target.](#var_public)
136 * [public_configs: [label list] Configs applied to dependents.](#var_public_configs)
137 * [public_deps: [label list] Declare public dependencies.](#var_public_deps)
138 * [rebase: [boolean] Rebase collected metadata as files.](#var_rebase)
139 * [response_file_contents: [string list] Contents of .rsp file for actions.](#var_response_file_contents)
140 * [script: [file name] Script file for actions.](#var_script)
141 * [sources: [file list] Source files for a target.](#var_sources)
142 * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly)
143 * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility)
144 * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys)
145 * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps)
146 * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes)
147 * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name)
Brett Wilson796ed472018-07-16 15:11:09 -0700148* [Other help topics](#other)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800149 * all: Print all the help at once
Brett Wilson796ed472018-07-16 15:11:09 -0700150 * [buildargs: How build arguments work.](#buildargs)
151 * [dotfile: Info about the toplevel .gn file.](#dotfile)
152 * [execution: Build graph and execution overview.](#execution)
153 * [grammar: Language and grammar for GN build files.](#grammar)
Julie Hockett81ee1962019-05-13 11:24:42 -0700154 * [input_conversion: Processing input from exec_script and read_file.](#io_conversion)
Brett Wilson796ed472018-07-16 15:11:09 -0700155 * [label_pattern: Matching more than one label.](#label_pattern)
156 * [labels: About labels.](#labels)
157 * [ninja_rules: How Ninja build rules are named.](#ninja_rules)
158 * [nogncheck: Annotating includes for checking.](#nogncheck)
Julie Hockett81ee1962019-05-13 11:24:42 -0700159 * [output_conversion: Specifies how to transform a value to output.](#io_conversion)
Brett Wilson796ed472018-07-16 15:11:09 -0700160 * [runtime_deps: How runtime dependency computation works.](#runtime_deps)
161 * [source_expansion: Map sources to outputs for scripts.](#source_expansion)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800162 * [switches: Show available command-line switches.](#switch_list)
Brett Wilson796ed472018-07-16 15:11:09 -0700163
164## Commands
165
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800166### **gn analyze <out_dir> <input_path> <output_path>**
Brett Wilson796ed472018-07-16 15:11:09 -0700167
168```
169 Analyze which targets are affected by a list of files.
170
171 This command takes three arguments:
172
173 out_dir is the path to the build directory.
174
175 input_path is a path to a file containing a JSON object with three fields:
176
177 - "files": A list of the filenames to check.
178
179 - "test_targets": A list of the labels for targets that are needed to run
180 the tests we wish to run.
181
182 - "additional_compile_targets": A list of the labels for targets that we
183 wish to rebuild, but aren't necessarily needed for testing. The important
184 difference between this field and "test_targets" is that if an item in
185 the additional_compile_targets list refers to a group, then any
186 dependencies of that group will be returned if they are out of date, but
187 the group itself does not need to be. If the dependencies themselves are
188 groups, the same filtering is repeated. This filtering can be used to
189 avoid rebuilding dependencies of a group that are unaffected by the input
190 files. The list may also contain the string "all" to refer to a
191 pseudo-group that contains every root target in the build graph.
192
193 This filtering behavior is also known as "pruning" the list of compile
194 targets.
195
196 output_path is a path indicating where the results of the command are to be
197 written. The results will be a file containing a JSON object with one or more
198 of following fields:
199
200 - "compile_targets": A list of the labels derived from the input
201 compile_targets list that are affected by the input files. Due to the way
202 the filtering works for compile targets as described above, this list may
203 contain targets that do not appear in the input list.
204
205 - "test_targets": A list of the labels from the input test_targets list that
206 are affected by the input files. This list will be a proper subset of the
207 input list.
208
209 - "invalid_targets": A list of any names from the input that do not exist in
210 the build graph. If this list is non-empty, the "error" field will also be
211 set to "Invalid targets".
212
213 - "status": A string containing one of three values:
214
215 - "Found dependency"
216 - "No dependency"
217 - "Found dependency (all) "
218
219 In the first case, the lists returned in compile_targets and test_targets
220 should be passed to ninja to build. In the second case, nothing was
221 affected and no build is necessary. In the third case, GN could not
222 determine the correct answer and returned the input as the output in order
223 to be safe.
224
225 - "error": This will only be present if an error occurred, and will contain
226 a string describing the error. This includes cases where the input file is
227 not in the right format, or contains invalid targets.
228
229 The command returns 1 if it is unable to read the input file or write the
230 output file, or if there is something wrong with the build such that gen
231 would also fail, and 0 otherwise. In particular, it returns 0 even if the
232 "error" key is non-empty and a non-fatal error occurred. In other words, it
233 tries really hard to always write something to the output JSON and convey
234 errors that way rather than via return codes.
235```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800236### **gn args**: (command-line tool)
Brett Wilson796ed472018-07-16 15:11:09 -0700237
238```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800239 Display or configure arguments declared by the build.
240
241 gn args [--list] [--short] [--args] [--overrides-only]
242
Brett Wilson796ed472018-07-16 15:11:09 -0700243 See also "gn help buildargs" for a more high-level overview of how
244 build arguments work.
245```
246
247#### **Usage**
248
249```
250 gn args
251 Open the arguments for the given build directory in an editor. If the
252 given build directory doesn't exist, it will be created and an empty args
253 file will be opened in the editor. You would type something like this
254 into that file:
255 enable_doom_melon=false
256 os="android"
257
258 To find your editor on Posix, GN will search the environment variables in
259 order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command
260 associated with .txt files.
261
262 Note: you can edit the build args manually by editing the file "args.gn"
263 in the build directory and then running "gn gen ".
264
265 gn args --list[=] [--short] [--overrides-only] [--json]
266 Lists all build arguments available in the current configuration, or, if
267 an exact_arg is specified for the list flag, just that one build
268 argument.
269
270 The output will list the declaration location, current value for the
271 build, default value (if different than the current value), and comment
272 preceding the declaration.
273
274 If --short is specified, only the names and current values will be
275 printed.
276
277 If --overrides-only is specified, only the names and current values of
278 arguments that have been overridden (i.e. non-default arguments) will
279 be printed. Overrides come from the /args.gn file and //.gn
280
281 If --json is specified, the output will be emitted in json format.
282 JSON schema for output:
283 [
284 {
285 "name": variable_name,
286 "current": {
287 "value": overridden_value,
288 "file": file_name,
289 "line": line_no
290 },
291 "default": {
292 "value": default_value,
293 "file": file_name,
294 "line": line_no
295 },
296 "comment": comment_string
297 },
298 ...
299 ]
300```
301
302#### **Examples**
303
304```
305 gn args out/Debug
306 Opens an editor with the args for out/Debug.
307
308 gn args out/Debug --list --short
309 Prints all arguments with their default values for the out/Debug
310 build.
311
312 gn args out/Debug --list --short --overrides-only
313 Prints overridden arguments for the out/Debug build.
314
315 gn args out/Debug --list=target_cpu
316 Prints information about the "target_cpu" argument for the "
317 "out/Debug
318 build.
319
320 gn args --list --args="os=\"android\" enable_doom_melon=true"
321 Prints all arguments with the default values for a build with the
322 given arguments set (which may affect the values of other
323 arguments).
324```
Joe Armstrong39413942019-03-15 10:34:03 +0800325### **gn check <out_dir> [<label_pattern>] [\--force] [\--check-generated]**
Brett Wilson796ed472018-07-16 15:11:09 -0700326
327```
328 GN's include header checker validates that the includes for C-like source
329 files match the build dependency graph.
330
331 "gn check" is the same thing as "gn gen" with the "--check" flag except that
332 this command does not write out any build files. It's intended to be an easy
333 way to manually trigger include file checking.
334
335 The can take exact labels or patterns that match more than
336 one (although not general regular expressions). If specified, only those
337 matching targets will be checked. See "gn help label_pattern" for details.
338```
339
340#### **Command-specific switches**
341
342```
343 --force
344 Ignores specifications of "check_includes = false" and checks all
345 target's files that match the target label.
Joe Armstrong39413942019-03-15 10:34:03 +0800346
347 --check-generated
348 Generated files are normally not checked since they do not exist
349 until after a build. With this flag, those generated files that
350 can be found on disk are also checked.
Brett Wilson796ed472018-07-16 15:11:09 -0700351```
352
353#### **What gets checked**
354
355```
Nate Fischer8ed01d32019-01-08 17:32:01 -0800356 The .gn file may specify a list of targets to be checked in the list
357 check_targets (see "gn help dotfile"). If a label pattern is specified
358 on the command line, check_targets is not used.
Brett Wilson796ed472018-07-16 15:11:09 -0700359
360 Targets can opt-out from checking with "check_includes = false" (see
361 "gn help check_includes").
362
363 For targets being checked:
364
365 - GN opens all C-like source files in the targets to be checked and scans
366 the top for includes.
367
Joe Armstrong39413942019-03-15 10:34:03 +0800368 - Generated files (that might not exist yet) are ignored unless
369 the --check-generated flag is provided.
370
Brett Wilson796ed472018-07-16 15:11:09 -0700371 - Includes with a "nogncheck" annotation are skipped (see
372 "gn help nogncheck").
373
374 - Only includes using "quotes" are checked. are assumed to be
375 system includes.
376
377 - Include paths are assumed to be relative to any of the "include_dirs" for
378 the target (including the implicit current dir).
379
380 - GN does not run the preprocessor so will not understand conditional
381 includes.
382
383 - Only includes matching known files in the build are checked: includes
384 matching unknown paths are ignored.
385
386 For an include to be valid:
387
388 - The included file must be in the current target, or there must be a path
389 following only public dependencies to a target with the file in it
390 ("gn path" is a good way to diagnose problems).
391
392 - There can be multiple targets with an included file: only one needs to be
393 valid for the include to be allowed.
394
395 - If there are only "sources" in a target, all are considered to be public
396 and can be included by other targets with a valid public dependency path.
397
398 - If a target lists files as "public", only those files are able to be
399 included by other targets. Anything in the sources will be considered
400 private and will not be includable regardless of dependency paths.
401
402 - Outputs from actions are treated like public sources on that target.
403
404 - A target can include headers from a target that depends on it if the
405 other target is annotated accordingly. See "gn help
406 allow_circular_includes_from".
407```
408
409#### **Advice on fixing problems**
410
411```
412 If you have a third party project that is difficult to fix or doesn't care
413 about include checks it's generally best to exclude that target from checking
414 altogether via "check_includes = false".
415
416 If you have conditional includes, make sure the build conditions and the
417 preprocessor conditions match, and annotate the line with "nogncheck" (see
418 "gn help nogncheck" for an example).
419
420 If two targets are hopelessly intertwined, use the
421 "allow_circular_includes_from" annotation. Ideally each should have identical
422 dependencies so configs inherited from those dependencies are consistent (see
423 "gn help allow_circular_includes_from").
424
425 If you have a standalone header file or files that need to be shared between
426 a few targets, you can consider making a source_set listing only those
427 headers as public sources. With only header files, the source set will be a
428 no-op from a build perspective, but will give a central place to refer to
429 those headers. That source set's files will still need to pass "gn check" in
430 isolation.
431
432 In rare cases it makes sense to list a header in more than one target if it
433 could be considered conceptually a member of both.
434```
435
436#### **Examples**
437
438```
439 gn check out/Debug
440 Check everything.
441
442 gn check out/Default //foo:bar
443 Check only the files in the //foo:bar target.
444
445 gn check out/Default "//foo/*
446 Check only the files in targets in the //foo directory tree.
447```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800448### **gn clean <out_dir>**
Brett Wilson796ed472018-07-16 15:11:09 -0700449
450```
451 Deletes the contents of the output directory except for args.gn and
452 creates a Ninja build environment sufficient to regenerate the build.
453```
Joe Armstrongb199e542019-03-21 09:40:08 +0800454### **gn desc**
Brett Wilson796ed472018-07-16 15:11:09 -0700455
456```
Joe Armstrongb199e542019-03-21 09:40:08 +0800457 gn desc
458 [--format=json]
459
Julie Hockette2a29402018-07-31 10:11:42 -0700460 Displays information about a given target or config. The build parameters
461 will be taken for the build in the given .
Brett Wilson796ed472018-07-16 15:11:09 -0700462
463 The
464 pattern (see "gn help label_pattern"). A label pattern will only match
465 targets.
466```
467
Nico Weber75eb3c72018-08-28 16:01:52 -0400468#### **Possibilities for <what to show>**
Brett Wilson796ed472018-07-16 15:11:09 -0700469
470```
471 (If unspecified an overall summary will be displayed.)
472
473 all_dependent_configs
474 allow_circular_includes_from
475 arflags [--blame]
476 args
477 cflags [--blame]
Julie Hockette2a29402018-07-31 10:11:42 -0700478 cflags_c [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700479 cflags_cc [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700480 check_includes
481 configs [--tree] (see below)
Nate Fischer8ed01d32019-01-08 17:32:01 -0800482 data_keys
Brett Wilson796ed472018-07-16 15:11:09 -0700483 defines [--blame]
484 depfile
485 deps [--all] [--tree] (see below)
486 include_dirs [--blame]
487 inputs
488 ldflags [--blame]
489 lib_dirs
490 libs
Nate Fischer8ed01d32019-01-08 17:32:01 -0800491 metadata
492 output_conversion
Brett Wilson796ed472018-07-16 15:11:09 -0700493 outputs
494 public_configs
495 public
Nate Fischer8ed01d32019-01-08 17:32:01 -0800496 rebase
Brett Wilson796ed472018-07-16 15:11:09 -0700497 script
498 sources
499 testonly
500 visibility
Nate Fischer8ed01d32019-01-08 17:32:01 -0800501 walk_keys
Brett Wilson796ed472018-07-16 15:11:09 -0700502
503 runtime_deps
504 Compute all runtime deps for the given target. This is a computed list
505 and does not correspond to any GN variable, unlike most other values
506 here.
507
508 The output is a list of file names relative to the build directory. See
509 "gn help runtime_deps" for how this is computed. This also works with
510 "--blame" to see the source of the dependency.
511```
512
513#### **Shared flags**
514```
515 --all-toolchains
516 Normally only inputs in the default toolchain will be included.
517 This switch will turn on matching all toolchains.
518
519 For example, a file is in a target might be compiled twice:
520 once in the default toolchain and once in a secondary one. Without
521 this flag, only the default toolchain one will be matched by
522 wildcards. With this flag, both will be matched.
523
524 --format=json
525 Format the output as JSON instead of text.
526```
527
528#### **Target flags**
529
530```
531 --blame
532 Used with any value specified on a config, this will name the config that
Julie Hockette2a29402018-07-31 10:11:42 -0700533 causes that target to get the flag. This doesn't currently work for libs
Brett Wilson796ed472018-07-16 15:11:09 -0700534 and lib_dirs because those are inherited and are more complicated to
535 figure out the blame (patches welcome).
536```
537
538#### **Configs**
539
540```
541 The "configs" section will list all configs that apply. For targets this will
542 include configs specified in the "configs" variable of the target, and also
543 configs pushed onto this target via public or "all dependent" configs.
544
545 Configs can have child configs. Specifying --tree will show the hierarchy.
546```
547
548#### **Printing outputs**
549
550```
551 The "outputs" section will list all outputs that apply, including the outputs
552 computed from the tool definition (eg for "executable", "static_library", ...
553 targets).
554```
555
556#### **Printing deps**
557
558```
559 Deps will include all public, private, and data deps (TODO this could be
560 clarified and enhanced) sorted in order applying. The following may be used:
561
562 --all
563 Collects all recursive dependencies and prints a sorted flat list. Also
564 usable with --tree (see below).
565 --as=(buildfile|label|output)
566 How to print targets.
567
568 buildfile
569 Prints the build files where the given target was declared as
570 file names.
571 label (default)
572 Prints the label of the target.
573 output
574 Prints the first output file for the target relative to the
575 root build directory.
576
577 --testonly=(true|false)
578 Restrict outputs to targets with the testonly flag set
579 accordingly. When unspecified, the target's testonly flags are
580 ignored.
581
582 --tree
583 Print a dependency tree. By default, duplicates will be elided with "..."
584 but when --all and -tree are used together, no eliding will be performed.
585
586 The "deps", "public_deps", and "data_deps" will all be included in the
587 tree.
588
589 Tree output can not be used with the filtering or output flags: --as,
590 --type, --testonly.
591 --type=(action|copy|executable|group|loadable_module|shared_library|
592 source_set|static_library)
593 Restrict outputs to targets matching the given type. If
594 unspecified, no filtering will be performed.
595```
596
597#### **Note**
598
599```
600 This command will show the full name of directories and source files, but
601 when directories and source paths are written to the build file, they will be
602 adjusted to be relative to the build directory. So the values for paths
603 displayed by this command won't match (but should mean the same thing).
604```
605
606#### **Examples**
607
608```
609 gn desc out/Debug //base:base
610 Summarizes the given target.
611
612 gn desc out/Foo :base_unittests deps --tree
613 Shows a dependency tree of the "base_unittests" project in
614 the current directory.
615
616 gn desc out/Debug //base defines --blame
617 Shows defines set for the //base:base target, annotated by where
618 each one was set from.
619```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800620### **gn format [\--dump-tree] (\--stdin | <list of build_files...>)**
Brett Wilson796ed472018-07-16 15:11:09 -0700621
622```
623 Formats .gn file to a standard format.
624
625 The contents of some lists ('sources', 'deps', etc.) will be sorted to a
626 canonical order. To suppress this, you can add a comment of the form "#
627 NOSORT" immediately preceding the assignment. e.g.
628
629 # NOSORT
630 sources = [
631 "z.cc",
632 "a.cc",
633 ]
634```
635
636#### **Arguments**
637
638```
639 --dry-run
640 Does not change or output anything, but sets the process exit code based
641 on whether output would be different than what's on disk. This is useful
642 for presubmit/lint-type checks.
643 - Exit code 0: successful format, matches on disk.
644 - Exit code 1: general failure (parse error, etc.)
645 - Exit code 2: successful format, but differs from on disk.
646
Julie Hockettd69a9c32019-01-23 14:36:18 -0800647 --dump-tree[=( text | json )]
648 Dumps the parse tree to stdout and does not update the file or print
649 formatted output. If no format is specified, text format will be used.
Brett Wilson796ed472018-07-16 15:11:09 -0700650
651 --stdin
652 Read input from stdin and write to stdout rather than update a file
653 in-place.
654```
655
656#### **Examples**
657```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800658 gn format //some/BUILD.gn //some/other/BUILD.gn //and/another/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700659 gn format some\\BUILD.gn
660 gn format /abspath/some/BUILD.gn
661 gn format --stdin
662```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800663### **gn gen [\--check] [<ide options>] <out_dir>**
Brett Wilson796ed472018-07-16 15:11:09 -0700664
665```
666 Generates ninja files from the current tree and puts them in the given output
667 directory.
668
669 The output directory can be a source-repo-absolute path name such as:
670 //out/foo
671 Or it can be a directory relative to the current directory such as:
672 out/foo
673
674 "gn gen --check" is the same as running "gn check". See "gn help check"
675 for documentation on that mode.
676
677 See "gn help switches" for the common command-line switches.
678```
679
680#### **IDE options**
681
682```
683 GN optionally generates files for IDE. Possibilities for
684
685 --ide=
686 Generate files for an IDE. Currently supported values:
687 "eclipse" - Eclipse CDT settings file.
688 "vs" - Visual Studio project/solution files.
689 (default Visual Studio version: 2017)
690 "vs2013" - Visual Studio 2013 project/solution files.
691 "vs2015" - Visual Studio 2015 project/solution files.
692 "vs2017" - Visual Studio 2017 project/solution files.
Sylvain Defresned46f92f2019-04-03 16:14:29 +0200693 "vs2019" - Visual Studio 2019 project/solution files.
Brett Wilson796ed472018-07-16 15:11:09 -0700694 "xcode" - Xcode workspace/solution files.
695 "qtcreator" - QtCreator project files.
696 "json" - JSON file containing target information
697
698 --filters=
699 Semicolon-separated list of label patterns used to limit the set of
700 generated projects (see "gn help label_pattern"). Only matching targets
701 and their dependencies will be included in the solution. Only used for
702 Visual Studio, Xcode and JSON.
703```
704
705#### **Visual Studio Flags**
706
707```
708 --sln=
709 Override default sln file name ("all"). Solution file is written to the
710 root build directory.
711
712 --no-deps
713 Don't include targets dependencies to the solution. Changes the way how
714 --filters option works. Only directly matching targets are included.
715
716 --winsdk=
717 Use the specified Windows 10 SDK version to generate project files.
718 As an example, "10.0.15063.0" can be specified to use Creators Update SDK
719 instead of the default one.
720
721 --ninja-extra-args=
722 This string is passed without any quoting to the ninja invocation
723 command-line. Can be used to configure ninja flags, like "-j".
724```
725
726#### **Xcode Flags**
727
728```
729 --workspace=
730 Override defaut workspace file name ("all"). The workspace file is
731 written to the root build directory.
732
733 --ninja-extra-args=
734 This string is passed without any quoting to the ninja invocation
735 command-line. Can be used to configure ninja flags, like "-j".
736
737 --root-target=
738 Name of the target corresponding to "All" target in Xcode. If unset,
739 "All" invokes ninja without any target and builds everything.
740```
741
742#### **QtCreator Flags**
743
744```
745 --root-target=
746 Name of the root target for which the QtCreator project will be generated
747 to contain files of it and its dependencies. If unset, the whole build
748 graph will be emitted.
749```
750
751#### **Eclipse IDE Support**
752
753```
754 GN DOES NOT generate Eclipse CDT projects. Instead, it generates a settings
755 file which can be imported into an Eclipse CDT project. The XML file contains
756 a list of include paths and defines. Because GN does not generate a full
757 .cproject definition, it is not possible to properly define includes/defines
758 for each file individually. Instead, one set of includes/defines is generated
759 for the entire project. This works fairly well but may still result in a few
760 indexer issues here and there.
761```
762
763#### **Generic JSON Output**
764
765```
766 Dumps target information to a JSON file and optionally invokes a
767 python script on the generated file. See the comments at the beginning
768 of json_project_writer.cc and desc_builder.cc for an overview of the JSON
769 file format.
770
771 --json-file-name=
772 Overrides default file name (project.json) of generated JSON file.
773
774 --json-ide-script=
775 Executes python script after the JSON file is generated. Path can be
776 project absolute (//), system absolute (/) or relative, in which case the
777 output directory will be base. Path to generated JSON file will be first
778 argument when invoking script.
779
780 --json-ide-script-args=
781 Optional second argument that will passed to executed script.
782```
Julie Hockett11e09912018-07-31 13:15:08 -0700783
784#### **Compilation Database**
785
786```
Robert Sesek252674d2019-05-10 14:08:52 -0400787 --export-compile-commands[=]
Julie Hockett11e09912018-07-31 13:15:08 -0700788 Produces a compile_commands.json file in the root of the build directory
789 containing an array of “command objects”, where each command object
Robert Sesek252674d2019-05-10 14:08:52 -0400790 specifies one way a translation unit is compiled in the project. If a list
791 of target_name is supplied, only targets that are reachable from the list
792 of target_name will be used for “command objects” generation, otherwise
793 all available targets will be used. This is used for various Clang-based
794 tooling, allowing for the replay of individual compilations independent
795 of the build system.
Julie Hockett11e09912018-07-31 13:15:08 -0700796```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800797### **gn help <anything>**
Brett Wilson796ed472018-07-16 15:11:09 -0700798
799```
800 Yo dawg, I heard you like help on your help so I put help on the help in the
801 help.
802
803 You can also use "all" as the parameter to get all help at once.
804```
805
806#### **Switches**
807
808```
809 --markdown
810 Format output in markdown syntax.
811```
812
813#### **Example**
814
815```
816 gn help --markdown all
817 Dump all help to stdout in markdown format.
818```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800819### **gn ls <out_dir> [<label_pattern>] [\--all-toolchains] [\--as=...]**
Brett Wilson796ed472018-07-16 15:11:09 -0700820```
821 [--type=...] [--testonly=...]
822
823 Lists all targets matching the given pattern for the given build directory.
824 By default, only targets in the default toolchain will be matched unless a
825 toolchain is explicitly supplied.
826
827 If the label pattern is unspecified, list all targets. The label pattern is
828 not a general regular expression (see "gn help label_pattern"). If you need
829 more complex expressions, pipe the result through grep.
830```
831
832#### **Options**
833
834```
835 --as=(buildfile|label|output)
836 How to print targets.
837
838 buildfile
839 Prints the build files where the given target was declared as
840 file names.
841 label (default)
842 Prints the label of the target.
843 output
844 Prints the first output file for the target relative to the
845 root build directory.
846
847 --all-toolchains
848 Normally only inputs in the default toolchain will be included.
849 This switch will turn on matching all toolchains.
850
851 For example, a file is in a target might be compiled twice:
852 once in the default toolchain and once in a secondary one. Without
853 this flag, only the default toolchain one will be matched by
854 wildcards. With this flag, both will be matched.
855
856 --testonly=(true|false)
857 Restrict outputs to targets with the testonly flag set
858 accordingly. When unspecified, the target's testonly flags are
859 ignored.
860
861 --type=(action|copy|executable|group|loadable_module|shared_library|
862 source_set|static_library)
863 Restrict outputs to targets matching the given type. If
864 unspecified, no filtering will be performed.
865```
866
867#### **Examples**
868
869```
870 gn ls out/Debug
871 Lists all targets in the default toolchain.
872
873 gn ls out/Debug "//base/*"
874 Lists all targets in the directory base and all subdirectories.
875
876 gn ls out/Debug "//base:*"
877 Lists all targets defined in //base/BUILD.gn.
878
879 gn ls out/Debug //base --as=output
880 Lists the build output file for //base:base
881
882 gn ls out/Debug --type=executable
883 Lists all executables produced by the build.
884
885 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
886 Builds all targets in //base and all subdirectories.
887
888 gn ls out/Debug //base --all-toolchains
889 Lists all variants of the target //base:base (it may be referenced
890 in multiple toolchains).
891```
Joe Armstrongb199e542019-03-21 09:40:08 +0800892### **gn meta**
893
Nate Fischer8ed01d32019-01-08 17:32:01 -0800894```
Joe Armstrongb199e542019-03-21 09:40:08 +0800895 gn meta * --data=[,*]* [--walk=[,*]*]
896 [--rebase=]
Nate Fischer8ed01d32019-01-08 17:32:01 -0800897
898 Lists collected metaresults of all given targets for the given data key(s),
899 collecting metadata dependencies as specified by the given walk key(s).
900
901 See `gn help generated_file` for more information on the walk.
902```
903
904#### **Arguments**
905
906```
907
908 A list of target labels from which to initiate the walk.
909
910 --data
911 A list of keys from which to extract data. In each target walked, its metadata
912 scope is checked for the presence of these keys. If present, the contents of
913 those variable in the scope are appended to the results list.
914
915 --walk (optional)
916 A list of keys from which to control the walk. In each target walked, its
917 metadata scope is checked for the presence of any of these keys. If present,
918 the contents of those variables is checked to ensure that it is a label of
919 a valid dependency of the target and then added to the set of targets to walk.
920 If the empty string ("") is present in any of these keys, all deps and data_deps
921 are added to the walk set.
922
923 --rebase (optional)
924 A destination directory onto which to rebase any paths found. If set, all
925 collected metadata will be rebased onto this path. This option will throw errors
926 if collected metadata is not a list of strings.
927```
928
929#### **Examples**
930
931```
932 gn meta out/Debug "//base/foo" --data=files
933 Lists collected metaresults for the `files` key in the //base/foo:foo
934 target and all of its dependency tree.
935
936 gn meta out/Debug "//base/foo" --data=files --data=other
937 Lists collected metaresults for the `files` and `other` keys in the
938 //base/foo:foo target and all of its dependency tree.
939
940 gn meta out/Debug "//base/foo" --data=files --walk=stop
941 Lists collected metaresults for the `files` key in the //base/foo:foo
942 target and all of the dependencies listed in the `stop` key (and so on).
943
944 gn meta out/Debug "//base/foo" --data=files --rebase="/"
945 Lists collected metaresults for the `files` key in the //base/foo:foo
946 target and all of its dependency tree, rebasing the strings in the `files`
947 key onto the source directory of the target's declaration relative to "/".
948```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800949### **gn path <out_dir> <target_one> <target_two>**
Brett Wilson796ed472018-07-16 15:11:09 -0700950
951```
952 Finds paths of dependencies between two targets. Each unique path will be
953 printed in one group, and groups will be separate by newlines. The two
954 targets can appear in either order (paths will be found going in either
955 direction).
956
957 By default, a single path will be printed. If there is a path with only
958 public dependencies, the shortest public path will be printed. Otherwise, the
959 shortest path using either public or private dependencies will be printed. If
960 --with-data is specified, data deps will also be considered. If there are
961 multiple shortest paths, an arbitrary one will be selected.
962```
963
964#### **Interesting paths**
965
966```
967 In a large project, there can be 100's of millions of unique paths between a
968 very high level and a common low-level target. To make the output more useful
969 (and terminate in a reasonable time), GN will not revisit sub-paths
970 previously known to lead to the target.
971```
972
973#### **Options**
974
975```
976 --all
977 Prints all "interesting" paths found rather than just the first one.
978 Public paths will be printed first in order of increasing length, followed
979 by non-public paths in order of increasing length.
980
981 --public
982 Considers only public paths. Can't be used with --with-data.
983
984 --with-data
985 Additionally follows data deps. Without this flag, only public and private
986 linked deps will be followed. Can't be used with --public.
987```
988
989#### **Example**
990
991```
992 gn path out/Default //base //tools/gn
993```
Joe Armstrongb199e542019-03-21 09:40:08 +0800994### **gn refs**
995
Brett Wilson796ed472018-07-16 15:11:09 -0700996```
Joe Armstrongb199e542019-03-21 09:40:08 +0800997 gn refs (|
998 [--all] [--all-toolchains] [--as=...] [--testonly=...] [--type=...]
Brett Wilson796ed472018-07-16 15:11:09 -0700999
1000 Finds reverse dependencies (which targets reference something). The input is
1001 a list containing:
1002
1003 - Target label: The result will be which targets depend on it.
1004
1005 - Config label: The result will be which targets list the given config in
1006 its "configs" or "public_configs" list.
1007
1008 - Label pattern: The result will be which targets depend on any target
1009 matching the given pattern. Patterns will not match configs. These are not
1010 general regular expressions, see "gn help label_pattern" for details.
1011
1012 - File name: The result will be which targets list the given file in its
1013 "inputs", "sources", "public", "data", or "outputs". Any input that does
1014 not contain wildcards and does not match a target or a config will be
1015 treated as a file.
1016
1017 - Response file: If the input starts with an "@", it will be interpreted as
1018 a path to a file containing a list of labels or file names, one per line.
1019 This allows us to handle long lists of inputs without worrying about
1020 command line limits.
1021```
1022
1023#### **Options**
1024
1025```
1026 --all
1027 When used without --tree, will recurse and display all unique
1028 dependencies of the given targets. For example, if the input is a target,
1029 this will output all targets that depend directly or indirectly on the
1030 input. If the input is a file, this will output all targets that depend
1031 directly or indirectly on that file.
1032
1033 When used with --tree, turns off eliding to show a complete tree.
1034 --all-toolchains
1035 Normally only inputs in the default toolchain will be included.
1036 This switch will turn on matching all toolchains.
1037
1038 For example, a file is in a target might be compiled twice:
1039 once in the default toolchain and once in a secondary one. Without
1040 this flag, only the default toolchain one will be matched by
1041 wildcards. With this flag, both will be matched.
1042
1043 --as=(buildfile|label|output)
1044 How to print targets.
1045
1046 buildfile
1047 Prints the build files where the given target was declared as
1048 file names.
1049 label (default)
1050 Prints the label of the target.
1051 output
1052 Prints the first output file for the target relative to the
1053 root build directory.
1054
1055 -q
1056 Quiet. If nothing matches, don't print any output. Without this option, if
1057 there are no matches there will be an informational message printed which
1058 might interfere with scripts processing the output.
1059 --testonly=(true|false)
1060 Restrict outputs to targets with the testonly flag set
1061 accordingly. When unspecified, the target's testonly flags are
1062 ignored.
1063
1064 --tree
1065 Outputs a reverse dependency tree from the given target. Duplicates will
1066 be elided. Combine with --all to see a full dependency tree.
1067
1068 Tree output can not be used with the filtering or output flags: --as,
1069 --type, --testonly.
1070 --type=(action|copy|executable|group|loadable_module|shared_library|
1071 source_set|static_library)
1072 Restrict outputs to targets matching the given type. If
1073 unspecified, no filtering will be performed.
1074```
1075
1076#### **Examples (target input)**
1077
1078```
1079 gn refs out/Debug //tools/gn:gn
1080 Find all targets depending on the given exact target name.
1081
1082 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
1083 Edit all .gn files containing references to //base:i18n
1084
1085 gn refs out/Debug //base --all
1086 List all targets depending directly or indirectly on //base:base.
1087
1088 gn refs out/Debug "//base/*"
1089 List all targets depending directly on any target in //base or
1090 its subdirectories.
1091
1092 gn refs out/Debug "//base:*"
1093 List all targets depending directly on any target in
1094 //base/BUILD.gn.
1095
1096 gn refs out/Debug //base --tree
1097 Print a reverse dependency tree of //base:base
1098```
1099
1100#### **Examples (file input)**
1101
1102```
1103 gn refs out/Debug //base/macros.h
1104 Print target(s) listing //base/macros.h as a source.
1105
1106 gn refs out/Debug //base/macros.h --tree
1107 Display a reverse dependency tree to get to the given file. This
1108 will show how dependencies will reference that file.
1109
1110 gn refs out/Debug //base/macros.h //base/at_exit.h --all
1111 Display all unique targets with some dependency path to a target
1112 containing either of the given files as a source.
1113
1114 gn refs out/Debug //base/macros.h --testonly=true --type=executable
1115 --all --as=output
1116 Display the executable file names of all test executables
1117 potentially affected by a change to the given file.
1118```
1119## Target declarations
1120
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001121### **action**: Declare a target that runs a script a single time.
Brett Wilson796ed472018-07-16 15:11:09 -07001122
1123```
1124 This target type allows you to run a script a single time to produce one or
1125 more output files. If you want to run a script once for each of a set of
1126 input files, see "gn help action_foreach".
1127```
1128
1129#### **Inputs**
1130
1131```
1132 In an action the "sources" and "inputs" are treated the same: they're both
1133 input dependencies on script execution with no special handling. If you want
1134 to pass the sources to your script, you must do so explicitly by including
1135 them in the "args". Note also that this means there is no special handling of
1136 paths since GN doesn't know which of the args are paths and not. You will
1137 want to use rebase_path() to convert paths to be relative to the
1138 root_build_dir.
1139
1140 You can dynamically write input dependencies (for incremental rebuilds if an
1141 input file changes) by writing a depfile when the script is run (see "gn help
1142 depfile"). This is more flexible than "inputs".
1143
1144 If the command line length is very long, you can use response files to pass
1145 args to your script. See "gn help response_file_contents".
1146
1147 It is recommended you put inputs to your script in the "sources" variable,
1148 and stuff like other Python files required to run your script in the "inputs"
1149 variable.
Julie Hockette2a29402018-07-31 10:11:42 -07001150
Brett Wilson796ed472018-07-16 15:11:09 -07001151 The "deps" and "public_deps" for an action will always be
1152 completed before any part of the action is run so it can depend on
1153 the output of previous steps. The "data_deps" will be built if the
1154 action is built, but may not have completed before all steps of the
1155 action are started. This can give additional parallelism in the build
1156 for runtime-only dependencies.
1157```
1158
1159#### **Outputs**
1160
1161```
1162 You should specify files created by your script by specifying them in the
1163 "outputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001164
Brett Wilson796ed472018-07-16 15:11:09 -07001165 The script will be executed with the given arguments with the current
1166 directory being that of the root build directory. If you pass files
1167 to your script, see "gn help rebase_path" for how to convert
1168 file names to be relative to the build directory (file names in the
1169 sources, outputs, and inputs will be all treated as relative to the
1170 current build file and converted as needed automatically).
1171```
1172
1173#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001174
Brett Wilson796ed472018-07-16 15:11:09 -07001175```
1176 All output files must be inside the output directory of the build.
1177 You would generally use |$target_out_dir| or |$target_gen_dir| to
1178 reference the output or generated intermediate file directories,
1179 respectively.
1180```
1181
1182#### **Variables**
1183
1184```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001185 args, data, data_deps, depfile, deps, inputs, metadata, outputs*, pool,
Brett Wilson796ed472018-07-16 15:11:09 -07001186 response_file_contents, script*, sources
1187 * = required
1188```
1189
1190#### **Example**
1191
1192```
1193 action("run_this_guy_once") {
1194 script = "doprocessing.py"
1195 sources = [ "my_configuration.txt" ]
1196 outputs = [ "$target_gen_dir/insightful_output.txt" ]
1197
1198 # Our script imports this Python file so we want to rebuild if it changes.
1199 inputs = [ "helper_library.py" ]
1200
1201 # Note that we have to manually pass the sources to our script if the
1202 # script needs them as inputs.
1203 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
1204 rebase_path(sources, root_build_dir)
1205 }
1206```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001207### **action_foreach**: Declare a target that runs a script over a set of files.
Brett Wilson796ed472018-07-16 15:11:09 -07001208
1209```
1210 This target type allows you to run a script once-per-file over a set of
1211 sources. If you want to run a script once that takes many files as input, see
1212 "gn help action".
1213```
1214
1215#### **Inputs**
1216
1217```
1218 The script will be run once per file in the "sources" variable. The "outputs"
1219 variable should specify one or more files with a source expansion pattern in
1220 it (see "gn help source_expansion"). The output file(s) for each script
1221 invocation should be unique. Normally you use "{{source_name_part}}" in each
1222 output file.
1223
1224 If your script takes additional data as input, such as a shared configuration
1225 file or a Python module it uses, those files should be listed in the "inputs"
1226 variable. These files are treated as dependencies of each script invocation.
1227
1228 If the command line length is very long, you can use response files to pass
1229 args to your script. See "gn help response_file_contents".
1230
1231 You can dynamically write input dependencies (for incremental rebuilds if an
1232 input file changes) by writing a depfile when the script is run (see "gn help
1233 depfile"). This is more flexible than "inputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001234
Brett Wilson796ed472018-07-16 15:11:09 -07001235 The "deps" and "public_deps" for an action will always be
1236 completed before any part of the action is run so it can depend on
1237 the output of previous steps. The "data_deps" will be built if the
1238 action is built, but may not have completed before all steps of the
1239 action are started. This can give additional parallelism in the build
1240 for runtime-only dependencies.
1241```
1242
1243#### **Outputs**
Julie Hockette2a29402018-07-31 10:11:42 -07001244
Brett Wilson796ed472018-07-16 15:11:09 -07001245```
1246 The script will be executed with the given arguments with the current
1247 directory being that of the root build directory. If you pass files
1248 to your script, see "gn help rebase_path" for how to convert
1249 file names to be relative to the build directory (file names in the
1250 sources, outputs, and inputs will be all treated as relative to the
1251 current build file and converted as needed automatically).
1252```
1253
1254#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001255
Brett Wilson796ed472018-07-16 15:11:09 -07001256```
1257 All output files must be inside the output directory of the build.
1258 You would generally use |$target_out_dir| or |$target_gen_dir| to
1259 reference the output or generated intermediate file directories,
1260 respectively.
1261```
1262
1263#### **Variables**
1264
1265```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001266 args, data, data_deps, depfile, deps, inputs, metadata, outputs*, pool,
Brett Wilson796ed472018-07-16 15:11:09 -07001267 response_file_contents, script*, sources*
1268 * = required
1269```
1270
1271#### **Example**
1272
1273```
1274 # Runs the script over each IDL file. The IDL script will generate both a .cc
1275 # and a .h file for each input.
1276 action_foreach("my_idl") {
1277 script = "idl_processor.py"
1278 sources = [ "foo.idl", "bar.idl" ]
1279
Joe Armstrong73e89072019-05-10 11:23:52 +08001280 # Our script reads this file each time, so we need to list it as a
Brett Wilson796ed472018-07-16 15:11:09 -07001281 # dependency so we can rebuild if it changes.
1282 inputs = [ "my_configuration.txt" ]
1283
1284 # Transformation from source file name to output file names.
1285 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
1286 "$target_gen_dir/{{source_name_part}}.cc" ]
1287
1288 # Note that since "args" is opaque to GN, if you specify paths here, you
1289 # will need to convert it to be relative to the build directory using
1290 # rebase_path().
1291 args = [
1292 "{{source}}",
1293 "-o",
1294 rebase_path(relative_target_gen_dir, root_build_dir) +
1295 "/{{source_name_part}}.h" ]
1296 }
1297```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001298### **bundle_data**: [iOS/macOS] Declare a target without output.
Brett Wilson796ed472018-07-16 15:11:09 -07001299
1300```
1301 This target type allows to declare data that is required at runtime. It is
1302 used to inform "create_bundle" targets of the files to copy into generated
1303 bundle, see "gn help create_bundle" for help.
1304
1305 The target must define a list of files as "sources" and a single "outputs".
1306 If there are multiple files, source expansions must be used to express the
1307 output. The output must reference a file inside of {{bundle_root_dir}}.
1308
1309 This target can be used on all platforms though it is designed only to
1310 generate iOS/macOS bundle. In cross-platform projects, it is advised to put it
1311 behind iOS/macOS conditionals.
1312
1313 See "gn help create_bundle" for more information.
1314```
1315
1316#### **Variables**
1317
1318```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001319 sources*, outputs*, deps, data_deps, metadata, public_deps, visibility
Brett Wilson796ed472018-07-16 15:11:09 -07001320 * = required
1321```
1322
1323#### **Examples**
1324
1325```
1326 bundle_data("icudata") {
1327 sources = [ "sources/data/in/icudtl.dat" ]
1328 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
1329 }
1330
1331 bundle_data("base_unittests_bundle_data]") {
1332 sources = [ "test/data" ]
1333 outputs = [
1334 "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" +
1335 "{{source_file_part}}"
1336 ]
1337 }
1338
1339 bundle_data("material_typography_bundle_data") {
1340 sources = [
1341 "src/MaterialTypography.bundle/Roboto-Bold.ttf",
1342 "src/MaterialTypography.bundle/Roboto-Italic.ttf",
1343 "src/MaterialTypography.bundle/Roboto-Regular.ttf",
1344 "src/MaterialTypography.bundle/Roboto-Thin.ttf",
1345 ]
1346 outputs = [
1347 "{{bundle_resources_dir}}/MaterialTypography.bundle/"
1348 "{{source_file_part}}"
1349 ]
1350 }
1351```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001352### **copy**: Declare a target that copies files.
Brett Wilson796ed472018-07-16 15:11:09 -07001353
1354#### **File name handling**
1355
1356```
1357 All output files must be inside the output directory of the build. You would
1358 generally use |$target_out_dir| or |$target_gen_dir| to reference the output
1359 or generated intermediate file directories, respectively.
1360
1361 Both "sources" and "outputs" must be specified. Sources can include as many
1362 files as you want, but there can only be one item in the outputs list (plural
1363 is used for the name for consistency with other target types).
1364
1365 If there is more than one source file, your output name should specify a
1366 mapping from each source file to an output file name using source expansion
1367 (see "gn help source_expansion"). The placeholders will look like
1368 "{{source_name_part}}", for example.
1369```
1370
1371#### **Examples**
1372
1373```
1374 # Write a rule that copies a checked-in DLL to the output directory.
1375 copy("mydll") {
1376 sources = [ "mydll.dll" ]
1377 outputs = [ "$target_out_dir/mydll.dll" ]
1378 }
1379
1380 # Write a rule to copy several files to the target generated files directory.
1381 copy("myfiles") {
1382 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1383
1384 # Use source expansion to generate output files with the corresponding file
1385 # names in the gen dir. This will just copy each file.
1386 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1387 }
1388```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001389### **create_bundle**: [ios/macOS] Build an iOS or macOS bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07001390
1391```
1392 This target generates an iOS or macOS bundle (which is a directory with a
1393 well-know structure). This target does not define any sources, instead they
1394 are computed from all "bundle_data" target this one depends on transitively
1395 (the recursion stops at "create_bundle" targets).
1396
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001397 The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in
1398 "bundle_data" outputs. The properties are optional but must be defined if any
1399 of the "bundle_data" target use them.
Brett Wilson796ed472018-07-16 15:11:09 -07001400
1401 This target can be used on all platforms though it is designed only to
1402 generate iOS or macOS bundle. In cross-platform projects, it is advised to put
1403 it behind iOS/macOS conditionals.
1404
1405 If a create_bundle is specified as a data_deps for another target, the bundle
1406 is considered a leaf, and its public and private dependencies will not
1407 contribute to any data or data_deps. Required runtime dependencies should be
1408 placed in the bundle. A create_bundle can declare its own explicit data and
1409 data_deps, however.
1410```
1411
1412#### **Code signing**
1413
1414```
1415 Some bundle needs to be code signed as part of the build (on iOS all
1416 application needs to be code signed to run on a device). The code signature
1417 can be configured via the code_signing_script variable.
1418
1419 If set, code_signing_script is the path of a script that invoked after all
1420 files have been moved into the bundle. The script must not change any file in
1421 the bundle, but may add new files.
1422
1423 If code_signing_script is defined, then code_signing_outputs must also be
1424 defined and non-empty to inform when the script needs to be re-run. The
1425 code_signing_args will be passed as is to the script (so path have to be
1426 rebased) and additional inputs may be listed with the variable
1427 code_signing_sources.
1428```
1429
1430#### **Variables**
1431
1432```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001433 bundle_root_dir, bundle_contents_dir, bundle_resources_dir,
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001434 bundle_executable_dir, bundle_deps_filter, deps, data_deps, public_deps,
1435 visibility, product_type, code_signing_args, code_signing_script,
1436 code_signing_sources, code_signing_outputs, xcode_extra_attributes,
1437 xcode_test_application_name, partial_info_plist, metadata
Brett Wilson796ed472018-07-16 15:11:09 -07001438```
1439
1440#### **Example**
1441
1442```
1443 # Defines a template to create an application. On most platform, this is just
1444 # an alias for an "executable" target, but on iOS/macOS, it builds an
1445 # application bundle.
1446 template("app") {
1447 if (!is_ios && !is_mac) {
1448 executable(target_name) {
1449 forward_variables_from(invoker, "*")
1450 }
1451 } else {
1452 app_name = target_name
1453 gen_path = target_gen_dir
1454
1455 action("${app_name}_generate_info_plist") {
1456 script = [ "//build/ios/ios_gen_plist.py" ]
1457 sources = [ "templates/Info.plist" ]
1458 outputs = [ "$gen_path/Info.plist" ]
1459 args = rebase_path(sources, root_build_dir) +
1460 rebase_path(outputs, root_build_dir)
1461 }
1462
1463 bundle_data("${app_name}_bundle_info_plist") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001464 public_deps = [ ":${app_name}_generate_info_plist" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001465 sources = [ "$gen_path/Info.plist" ]
1466 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
1467 }
1468
1469 executable("${app_name}_generate_executable") {
1470 forward_variables_from(invoker, "*", [
1471 "output_name",
1472 "visibility",
1473 ])
1474 output_name =
1475 rebase_path("$gen_path/$app_name", root_build_dir)
1476 }
1477
1478 code_signing =
1479 defined(invoker.code_signing) && invoker.code_signing
1480
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001481 if (!is_ios || !code_signing) {
Brett Wilson796ed472018-07-16 15:11:09 -07001482 bundle_data("${app_name}_bundle_executable") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001483 public_deps = [ ":${app_name}_generate_executable" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001484 sources = [ "$gen_path/$app_name" ]
1485 outputs = [ "{{bundle_executable_dir}}/$app_name" ]
1486 }
1487 }
1488
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001489 create_bundle("$app_name.app") {
Brett Wilson796ed472018-07-16 15:11:09 -07001490 product_type = "com.apple.product-type.application"
1491
1492 if (is_ios) {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001493 bundle_root_dir = "$root_build_dir/$target_name"
Brett Wilson796ed472018-07-16 15:11:09 -07001494 bundle_contents_dir = bundle_root_dir
1495 bundle_resources_dir = bundle_contents_dir
1496 bundle_executable_dir = bundle_contents_dir
Brett Wilson796ed472018-07-16 15:11:09 -07001497
1498 extra_attributes = {
1499 ONLY_ACTIVE_ARCH = "YES"
1500 DEBUG_INFORMATION_FORMAT = "dwarf"
1501 }
1502 } else {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001503 bundle_root_dir = "$root_build_dir/$target_name"
1504 bundle_contents_dir = "$bundle_root_dir/Contents"
1505 bundle_resources_dir = "$bundle_contents_dir/Resources"
1506 bundle_executable_dir = "$bundle_contents_dir/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07001507 }
1508 deps = [ ":${app_name}_bundle_info_plist" ]
1509 if (is_ios && code_signing) {
1510 deps += [ ":${app_name}_generate_executable" ]
1511 code_signing_script = "//build/config/ios/codesign.py"
1512 code_signing_sources = [
1513 invoker.entitlements_path,
1514 "$target_gen_dir/$app_name",
1515 ]
1516 code_signing_outputs = [
1517 "$bundle_root_dir/$app_name",
1518 "$bundle_root_dir/_CodeSignature/CodeResources",
1519 "$bundle_root_dir/embedded.mobileprovision",
1520 "$target_gen_dir/$app_name.xcent",
1521 ]
1522 code_signing_args = [
1523 "-i=" + ios_code_signing_identity,
1524 "-b=" + rebase_path(
1525 "$target_gen_dir/$app_name", root_build_dir),
1526 "-e=" + rebase_path(
1527 invoker.entitlements_path, root_build_dir),
1528 "-e=" + rebase_path(
1529 "$target_gen_dir/$app_name.xcent", root_build_dir),
1530 rebase_path(bundle_root_dir, root_build_dir),
1531 ]
1532 } else {
1533 deps += [ ":${app_name}_bundle_executable" ]
1534 }
1535 }
1536 }
1537 }
1538```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001539### **executable**: Declare an executable target.
Brett Wilson796ed472018-07-16 15:11:09 -07001540
Julie Hockettce1fa072019-05-07 17:44:37 -07001541#### **Language and compilation**
1542
1543```
1544 The tools and commands used to create this target type will be
1545 determined by the source files in its sources. Targets containing
1546 multiple compiler-incompatible languages are not allowed (e.g. a
1547 target containing both C and C++ sources is acceptable, but a
1548 target containing C and Rust sources is not).
1549```
1550
Brett Wilson796ed472018-07-16 15:11:09 -07001551#### **Variables**
1552
1553```
1554 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1555 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001556 libs, precompiled_header, precompiled_source, rustflags,
1557 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07001558 Deps: data_deps, deps, public_deps
1559 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001560 General: check_includes, configs, data, friend, inputs, metadata,
1561 output_name, output_extension, public, sources, testonly,
1562 visibility
Julie Hockettce1fa072019-05-07 17:44:37 -07001563 Rust variables: aliased_deps, crate_root, crate_name, edition
Nate Fischer8ed01d32019-01-08 17:32:01 -08001564```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001565### **generated_file**: Declare a generated_file target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001566
1567```
1568 Writes data value(s) to disk on resolution. This target type mirrors some
1569 functionality of the write_file() function, but also provides the ability to
1570 collect metadata from its dependencies on resolution rather than writing out
1571 parse time.
1572
1573 The `outputs` variable is required to be a list with a single element,
1574 specifying the intended location of the output file.
1575
1576 The `output_conversion` variable specified the format to write the
1577 value. See `gn help output_conversion`.
1578
1579 One of `contents` or `data_keys` must be specified; use of `data` will write
1580 the contents of that value to file, while use of `data_keys` will trigger a
1581 metadata collection walk based on the dependencies of the target and the
1582 optional values of the `rebase` and `walk_keys` variables. See
1583 `gn help metadata`.
1584
1585 Collected metadata, if specified, will be returned in postorder of
1586 dependencies. See the example for details.
1587```
1588
1589#### **Example (metadata collection)**
1590
1591```
1592 Given the following targets defined in //base/BUILD.gn, where A depends on B
1593 and B depends on C and D:
1594
1595 group("a") {
1596 metadata = {
1597 doom_melon = [ "enable" ]
1598 my_files = [ "foo.cpp" ]
1599
1600 // Note: this is functionally equivalent to not defining `my_barrier`
1601 // at all in this target's metadata.
1602 my_barrier = [ "" ]
1603 }
1604
1605 deps = [ ":b" ]
1606 }
1607
Julie Hockettd69a9c32019-01-23 14:36:18 -08001608 group("b") {
Nate Fischer8ed01d32019-01-08 17:32:01 -08001609 metadata = {
1610 my_files = [ "bar.cpp" ]
1611 my_barrier = [ ":c" ]
1612 }
1613
1614 deps = [ ":c", ":d" ]
1615 }
1616
1617 group("c") {
1618 metadata = {
1619 doom_melon = [ "disable" ]
1620 my_files = [ "baz.cpp" ]
1621 }
1622 }
1623
1624 group("d") {
1625 metadata = {
1626 my_files = [ "missing.cpp" ]
1627 }
1628 }
1629
1630 If the following generated_file target is defined:
1631
1632 generated_file("my_files_metadata") {
1633 outputs = [ "$root_build_dir/my_files.json" ]
1634 data_keys = [ "my_files" ]
1635
1636 deps = [ "//base:a" ]
1637 }
1638
1639 The following will be written to "$root_build_dir/my_files.json" (less the
1640 comments):
1641 [
1642 "baz.cpp", // from //base:c via //base:b
1643 "missing.cpp" // from //base:d via //base:b
1644 "bar.cpp", // from //base:b via //base:a
1645 "foo.cpp", // from //base:a
1646 ]
1647
1648 Alternatively, as an example of using walk_keys, if the following
1649 generated_file target is defined:
1650
1651 generated_file("my_files_metadata") {
1652 outputs = [ "$root_build_dir/my_files.json" ]
1653 data_keys = [ "my_files" ]
1654 walk_keys = [ "my_barrier" ]
1655
1656 deps = [ "//base:a" ]
1657 }
1658
1659 The following will be written to "$root_build_dir/my_files.json" (again less
1660 the comments):
1661 [
1662 "baz.cpp", // from //base:c via //base:b
1663 "bar.cpp", // from //base:b via //base:a
1664 "foo.cpp", // from //base:a
1665 ]
1666
1667 If `rebase` is used in the following generated_file target:
1668
1669 generated_file("my_files_metadata") {
1670 outputs = [ "$root_build_dir/my_files.json" ]
1671 data_keys = [ "my_files" ]
1672 walk_keys = [ "my_barrier" ]
1673 rebase = root_build_dir
1674
1675 deps = [ "//base:a" ]
1676 }
1677
1678 The following will be written to "$root_build_dir/my_files.json" (again less
1679 the comments) (assuming root_build_dir = "//out"):
1680 [
1681 "../base/baz.cpp", // from //base:c via //base:b
1682 "../base/bar.cpp", // from //base:b via //base:a
1683 "../base/foo.cpp", // from //base:a
1684 ]
1685```
1686
1687#### **Variables**
1688
1689```
Julie Hockettd69a9c32019-01-23 14:36:18 -08001690 contents
Nate Fischer8ed01d32019-01-08 17:32:01 -08001691 data_keys
1692 rebase
1693 walk_keys
1694 output_conversion
1695 Deps: data_deps, deps, public_deps
1696 Dependent configs: all_dependent_configs, public_configs
Brett Wilson796ed472018-07-16 15:11:09 -07001697```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001698### **group**: Declare a named group of targets.
Brett Wilson796ed472018-07-16 15:11:09 -07001699
1700```
1701 This target type allows you to create meta-targets that just collect a set of
1702 dependencies into one named target. Groups can additionally specify configs
1703 that apply to their dependents.
1704```
1705
1706#### **Variables**
1707
1708```
1709 Deps: data_deps, deps, public_deps
1710 Dependent configs: all_dependent_configs, public_configs
1711```
1712
1713#### **Example**
1714
1715```
1716 group("all") {
1717 deps = [
1718 "//project:runner",
1719 "//project:unit_tests",
1720 ]
1721 }
1722```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001723### **loadable_module**: Declare a loadable module target.
Brett Wilson796ed472018-07-16 15:11:09 -07001724
1725```
1726 This target type allows you to create an object file that is (and can only
1727 be) loaded and unloaded at runtime.
1728
1729 A loadable module will be specified on the linker line for targets listing
1730 the loadable module in its "deps". If you don't want this (if you don't need
1731 to dynamically load the library at runtime), then you should use a
1732 "shared_library" target type instead.
1733```
1734
Julie Hockettce1fa072019-05-07 17:44:37 -07001735#### **Language and compilation**
1736
1737```
1738 The tools and commands used to create this target type will be
1739 determined by the source files in its sources. Targets containing
1740 multiple compiler-incompatible languages are not allowed (e.g. a
1741 target containing both C and C++ sources is acceptable, but a
1742 target containing C and Rust sources is not).
1743```
1744
Brett Wilson796ed472018-07-16 15:11:09 -07001745#### **Variables**
1746
1747```
1748 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1749 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001750 libs, precompiled_header, precompiled_source, rustflags,
1751 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07001752 Deps: data_deps, deps, public_deps
1753 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001754 General: check_includes, configs, data, friend, inputs, metadata,
1755 output_name, output_extension, public, sources, testonly,
1756 visibility
Julie Hockettce1fa072019-05-07 17:44:37 -07001757 Rust variables: aliased_deps, crate_root, crate_name, crate_type, edition
1758```
1759### **rust_library**: Declare a Rust library target.
1760
1761```
1762 A Rust library is an archive containing additional rust-c provided metadata.
1763 These are the files produced by the rustc compiler with the `.rlib`
1764 extension, and are the intermediate step for most Rust-based binaries.
1765```
1766
1767#### **Language and compilation**
1768
1769```
1770 The tools and commands used to create this target type will be
1771 determined by the source files in its sources. Targets containing
1772 multiple compiler-incompatible languages are not allowed (e.g. a
1773 target containing both C and C++ sources is acceptable, but a
1774 target containing C and Rust sources is not).
1775```
1776
1777#### **Variables**
1778
1779```
1780 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1781 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
1782 libs, precompiled_header, precompiled_source, rustflags,
1783 rustenv
1784 Deps: data_deps, deps, public_deps
1785 Dependent configs: all_dependent_configs, public_configs
1786 General: check_includes, configs, data, friend, inputs, metadata,
1787 output_name, output_extension, public, sources, testonly,
1788 visibility
1789 Rust variables: aliased_deps, crate_root, crate_name, edition
Brett Wilson796ed472018-07-16 15:11:09 -07001790```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001791### **shared_library**: Declare a shared library target.
Brett Wilson796ed472018-07-16 15:11:09 -07001792
1793```
1794 A shared library will be specified on the linker line for targets listing the
1795 shared library in its "deps". If you don't want this (say you dynamically
1796 load the library at runtime), then you should depend on the shared library
1797 via "data_deps" or, on Darwin platforms, use a "loadable_module" target type
1798 instead.
1799```
1800
Julie Hockettce1fa072019-05-07 17:44:37 -07001801#### **Language and compilation**
1802
1803```
1804 The tools and commands used to create this target type will be
1805 determined by the source files in its sources. Targets containing
1806 multiple compiler-incompatible languages are not allowed (e.g. a
1807 target containing both C and C++ sources is acceptable, but a
1808 target containing C and Rust sources is not).
1809```
1810
Brett Wilson796ed472018-07-16 15:11:09 -07001811#### **Variables**
1812
1813```
1814 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1815 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001816 libs, precompiled_header, precompiled_source, rustflags,
1817 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07001818 Deps: data_deps, deps, public_deps
1819 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001820 General: check_includes, configs, data, friend, inputs, metadata,
1821 output_name, output_extension, public, sources, testonly,
1822 visibility
Julie Hockettce1fa072019-05-07 17:44:37 -07001823 Rust variables: aliased_deps, crate_root, crate_name, crate_type, edition
Brett Wilson796ed472018-07-16 15:11:09 -07001824```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001825### **source_set**: Declare a source set target.
Brett Wilson796ed472018-07-16 15:11:09 -07001826
1827```
Julie Hockettce1fa072019-05-07 17:44:37 -07001828 The language of a source_set target is determined by the extensions present
1829 in its sources.
1830```
1831
1832#### **C-language source_sets**
1833
1834```
Brett Wilson796ed472018-07-16 15:11:09 -07001835 A source set is a collection of sources that get compiled, but are not linked
1836 to produce any kind of library. Instead, the resulting object files are
1837 implicitly added to the linker line of all targets that depend on the source
1838 set.
1839
1840 In most cases, a source set will behave like a static library, except no
1841 actual library file will be produced. This will make the build go a little
1842 faster by skipping creation of a large static library, while maintaining the
1843 organizational benefits of focused build targets.
1844
1845 The main difference between a source set and a static library is around
1846 handling of exported symbols. Most linkers assume declaring a function
1847 exported means exported from the static library. The linker can then do dead
1848 code elimination to delete code not reachable from exported functions.
1849
1850 A source set will not do this code elimination since there is no link step.
Julie Hockette2a29402018-07-31 10:11:42 -07001851 This allows you to link many source sets into a shared library and have the
Brett Wilson796ed472018-07-16 15:11:09 -07001852 "exported symbol" notation indicate "export from the final shared library and
1853 not from the intermediate targets." There is no way to express this concept
1854 when linking multiple static libraries into a shared library.
1855```
1856
Julie Hockettce1fa072019-05-07 17:44:37 -07001857#### **Rust-language source_sets**
1858
1859```
1860 A Rust source set is a collection of sources that get passed along to the
1861 final target that depends on it. No compilation is performed, and the source
1862 files are simply added as dependencies on the eventual rustc invocation that
1863 would produce a binary.
1864```
1865
Brett Wilson796ed472018-07-16 15:11:09 -07001866#### **Variables**
1867
1868```
1869 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1870 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001871 libs, precompiled_header, precompiled_source, rustflags,
1872 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07001873 Deps: data_deps, deps, public_deps
1874 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001875 General: check_includes, configs, data, friend, inputs, metadata,
1876 output_name, output_extension, public, sources, testonly,
1877 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07001878```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001879### **static_library**: Declare a static library target.
Brett Wilson796ed472018-07-16 15:11:09 -07001880
1881```
1882 Make a ".a" / ".lib" file.
1883
1884 If you only need the static library for intermediate results in the build,
1885 you should consider a source_set instead since it will skip the (potentially
1886 slow) step of creating the intermediate library file.
1887```
1888
1889#### **Variables**
1890
1891```
1892 complete_static_lib
1893 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1894 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001895 libs, precompiled_header, precompiled_source, rustflags,
1896 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07001897 Deps: data_deps, deps, public_deps
1898 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001899 General: check_includes, configs, data, friend, inputs, metadata,
1900 output_name, output_extension, public, sources, testonly,
1901 visibility
Julie Hockettce1fa072019-05-07 17:44:37 -07001902 Rust variables: aliased_deps, crate_root, crate_name, edition
1903
1904 The tools and commands used to create this target type will be
1905 determined by the source files in its sources. Targets containing
1906 multiple compiler-incompatible languages are not allowed (e.g. a
1907 target containing both C and C++ sources is acceptable, but a
1908 target containing C and Rust sources is not).
Brett Wilson796ed472018-07-16 15:11:09 -07001909```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001910### **target**: Declare an target with the given programmatic type.
Brett Wilson796ed472018-07-16 15:11:09 -07001911
1912```
1913 target(target_type_string, target_name_string) { ... }
1914
1915 The target() function is a way to invoke a built-in target or template with a
1916 type determined at runtime. This is useful for cases where the type of a
1917 target might not be known statically.
1918
1919 Only templates and built-in target functions are supported for the
1920 target_type_string parameter. Arbitrary functions, configs, and toolchains
1921 are not supported.
1922
1923 The call:
1924 target("source_set", "doom_melon") {
1925 Is equivalent to:
1926 source_set("doom_melon") {
1927```
1928
1929#### **Example**
1930
1931```
1932 if (foo_build_as_shared) {
1933 my_type = "shared_library"
1934 } else {
1935 my_type = "source_set"
1936 }
1937
1938 target(my_type, "foo") {
1939 ...
1940 }
1941```
1942## Buildfile functions
1943
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001944### **assert**: Assert an expression is true at generation time.
Brett Wilson796ed472018-07-16 15:11:09 -07001945
1946```
1947 assert( [, ])
1948
1949 If the condition is false, the build will fail with an error. If the
1950 optional second argument is provided, that string will be printed
1951 with the error message.
1952```
1953
1954#### **Examples**
1955
1956```
1957 assert(is_win)
1958 assert(defined(sources), "Sources must be defined");
1959```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001960### **config**: Defines a configuration object.
Brett Wilson796ed472018-07-16 15:11:09 -07001961
1962```
1963 Configuration objects can be applied to targets and specify sets of compiler
1964 flags, includes, defines, etc. They provide a way to conveniently group sets
1965 of this configuration information.
1966
1967 A config is referenced by its label just like a target.
1968
1969 The values in a config are additive only. If you want to remove a flag you
1970 need to remove the corresponding config that sets it. The final set of flags,
1971 defines, etc. for a target is generated in this order:
1972
1973 1. The values specified directly on the target (rather than using a config.
1974 2. The configs specified in the target's "configs" list, in order.
1975 3. Public_configs from a breadth-first traversal of the dependency tree in
1976 the order that the targets appear in "deps".
1977 4. All dependent configs from a breadth-first traversal of the dependency
1978 tree in the order that the targets appear in "deps".
1979```
1980
Joe Armstrong39413942019-03-15 10:34:03 +08001981#### **More background**
1982
1983```
1984 Configs solve a problem where the build system needs to have a higher-level
1985 understanding of various compiler settings. For example, some compiler flags
1986 have to appear in a certain order relative to each other, some settings like
1987 defines and flags logically go together, and the build system needs to
1988 de-duplicate flags even though raw command-line parameters can't always be
1989 operated on in that way.
1990
1991 The config gives a name to a group of settings that can then be reasoned
1992 about by GN. GN can know that configs with the same label are the same thing
1993 so can be de-duplicated. It allows related settings to be grouped so they
1994 are added or removed as a unit. And it allows targets to refer to settings
1995 with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than
1996 having to hard-coding every compiler's flags each time they are referred to.
1997```
1998
Brett Wilson796ed472018-07-16 15:11:09 -07001999#### **Variables valid in a config definition**
Joe Armstrong39413942019-03-15 10:34:03 +08002000
Brett Wilson796ed472018-07-16 15:11:09 -07002001```
2002 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2003 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002004 libs, precompiled_header, precompiled_source, rustflags,
2005 rustenv
Brett Wilson796ed472018-07-16 15:11:09 -07002006 Nested configs: configs
2007```
2008
2009#### **Variables on a target used to apply configs**
2010
2011```
2012 all_dependent_configs, configs, public_configs
2013```
2014
2015#### **Example**
2016
2017```
2018 config("myconfig") {
Joe Armstrongf5ad8072019-05-08 08:42:56 +08002019 include_dirs = [ "include/common" ]
Brett Wilson796ed472018-07-16 15:11:09 -07002020 defines = [ "ENABLE_DOOM_MELON" ]
2021 }
2022
2023 executable("mything") {
2024 configs = [ ":myconfig" ]
2025 }
2026```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002027### **declare_args**: Declare build arguments.
Brett Wilson796ed472018-07-16 15:11:09 -07002028
2029```
2030 Introduces the given arguments into the current scope. If they are not
2031 specified on the command line or in a toolchain's arguments, the default
2032 values given in the declare_args block will be used. However, these defaults
2033 will not override command-line values.
2034
2035 See also "gn help buildargs" for an overview.
2036
2037 The precise behavior of declare args is:
2038
2039 1. The declare_args() block executes. Any variable defined in the enclosing
2040 scope is available for reading, but any variable defined earlier in
2041 the current scope is not (since the overrides haven't been applied yet).
2042
2043 2. At the end of executing the block, any variables set within that scope
2044 are saved globally as build arguments, with their current values being
2045 saved as the "default value" for that argument.
2046
2047 3. User-defined overrides are applied. Anything set in "gn args" now
2048 overrides any default values. The resulting set of variables is promoted
2049 to be readable from the following code in the file.
2050
2051 This has some ramifications that may not be obvious:
2052
2053 - You should not perform difficult work inside a declare_args block since
2054 this only sets a default value that may be discarded. In particular,
2055 don't use the result of exec_script() to set the default value. If you
2056 want to have a script-defined default, set some default "undefined" value
2057 like [], "", or -1, and after the declare_args block, call exec_script if
2058 the value is unset by the user.
2059
2060 - Because you cannot read the value of a variable defined in the same
2061 block, if you need to make the default value of one arg depend
2062 on the possibly-overridden value of another, write two separate
2063 declare_args() blocks:
2064
2065 declare_args() {
2066 enable_foo = true
2067 }
2068 declare_args() {
2069 # Bar defaults to same user-overridden state as foo.
2070 enable_bar = enable_foo
2071 }
2072```
2073
2074#### **Example**
2075
2076```
2077 declare_args() {
2078 enable_teleporter = true
2079 enable_doom_melon = false
2080 }
2081
2082 If you want to override the (default disabled) Doom Melon:
2083 gn --args="enable_doom_melon=true enable_teleporter=true"
2084 This also sets the teleporter, but it's already defaulted to on so it will
2085 have no effect.
2086```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002087### **defined**: Returns whether an identifier is defined.
Brett Wilson796ed472018-07-16 15:11:09 -07002088
2089```
2090 Returns true if the given argument is defined. This is most useful in
2091 templates to assert that the caller set things up properly.
2092
2093 You can pass an identifier:
2094 defined(foo)
2095 which will return true or false depending on whether foo is defined in the
2096 current scope.
2097
2098 You can also check a named scope:
2099 defined(foo.bar)
2100 which will return true or false depending on whether bar is defined in the
2101 named scope foo. It will throw an error if foo is not defined or is not a
2102 scope.
2103```
2104
2105#### **Example**
2106
2107```
2108 template("mytemplate") {
2109 # To help users call this template properly...
2110 assert(defined(invoker.sources), "Sources must be defined")
2111
2112 # If we want to accept an optional "values" argument, we don't
2113 # want to dereference something that may not be defined.
2114 if (defined(invoker.values)) {
2115 values = invoker.values
2116 } else {
2117 values = "some default value"
2118 }
2119 }
2120```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002121### **exec_script**: Synchronously run a script and return the output.
Brett Wilson796ed472018-07-16 15:11:09 -07002122
2123```
2124 exec_script(filename,
2125 arguments = [],
2126 input_conversion = "",
2127 file_dependencies = [])
2128
2129 Runs the given script, returning the stdout of the script. The build
2130 generation will fail if the script does not exist or returns a nonzero exit
2131 code.
2132
2133 The current directory when executing the script will be the root build
2134 directory. If you are passing file names, you will want to use the
2135 rebase_path() function to make file names relative to this path (see "gn help
2136 rebase_path").
Gary Miguelce7fa362018-09-17 12:48:17 -07002137
2138 The default script interpreter is Python ("python" on POSIX, "python.exe" or
2139 "python.bat" on Windows). This can be configured by the script_executable
2140 variable, see "gn help dotfile".
Brett Wilson796ed472018-07-16 15:11:09 -07002141```
2142
2143#### **Arguments**:
2144
2145```
2146 filename:
Gary Miguelce7fa362018-09-17 12:48:17 -07002147 File name of script to execute. Non-absolute names will be treated as
2148 relative to the current build file.
Brett Wilson796ed472018-07-16 15:11:09 -07002149
2150 arguments:
2151 A list of strings to be passed to the script as arguments. May be
2152 unspecified or the empty list which means no arguments.
2153
2154 input_conversion:
Julie Hockett81ee1962019-05-13 11:24:42 -07002155 Controls how the file is read and parsed. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07002156
2157 If unspecified, defaults to the empty string which causes the script
2158 result to be discarded. exec script will return None.
2159
2160 dependencies:
2161 (Optional) A list of files that this script reads or otherwise depends
2162 on. These dependencies will be added to the build result such that if any
2163 of them change, the build will be regenerated and the script will be
2164 re-run.
2165
2166 The script itself will be an implicit dependency so you do not need to
2167 list it.
2168```
2169
2170#### **Example**
2171
2172```
2173 all_lines = exec_script(
2174 "myscript.py", [some_input], "list lines",
2175 [ rebase_path("data_file.txt", root_build_dir) ])
2176
2177 # This example just calls the script with no arguments and discards the
2178 # result.
2179 exec_script("//foo/bar/myscript.py")
2180```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002181### **foreach**: Iterate over a list.
Brett Wilson796ed472018-07-16 15:11:09 -07002182
2183```
2184 foreach(, ) {
2185
2186 }
2187
2188 Executes the loop contents block over each item in the list, assigning the
2189 loop_var to each item in sequence. The will be a copy so assigning
2190 to it will not mutate the list. The loop will iterate over a copy of
2191 so mutating it inside the loop will not affect iteration.
2192
2193 The block does not introduce a new scope, so that variable assignments inside
2194 the loop will be visible once the loop terminates.
2195
2196 The loop variable will temporarily shadow any existing variables with the
2197 same name for the duration of the loop. After the loop terminates the loop
2198 variable will no longer be in scope, and the previous value (if any) will be
2199 restored.
2200```
2201
2202#### **Example**
2203
2204```
2205 mylist = [ "a", "b", "c" ]
2206 foreach(i, mylist) {
2207 print(i)
2208 }
2209
2210 Prints:
2211 a
2212 b
2213 c
2214```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002215### **forward_variables_from**: Copies variables from a different scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002216
2217```
2218 forward_variables_from(from_scope, variable_list_or_star,
2219 variable_to_not_forward_list = [])
2220
2221 Copies the given variables from the given scope to the local scope if they
2222 exist. This is normally used in the context of templates to use the values of
2223 variables defined in the template invocation to a template-defined target.
2224
2225 The variables in the given variable_list will be copied if they exist in the
2226 given scope or any enclosing scope. If they do not exist, nothing will happen
2227 and they be left undefined in the current scope.
2228
2229 As a special case, if the variable_list is a string with the value of "*",
2230 all variables from the given scope will be copied. "*" only copies variables
2231 set directly on the from_scope, not enclosing ones. Otherwise it would
2232 duplicate all global variables.
2233
2234 When an explicit list of variables is supplied, if the variable exists in the
2235 current (destination) scope already, an error will be thrown. If "*" is
2236 specified, variables in the current scope will be clobbered (the latter is
2237 important because most targets have an implicit configs list, which means it
2238 wouldn't work at all if it didn't clobber).
2239
2240 The sources assignment filter (see "gn help set_sources_assignment_filter")
2241 is never applied by this function. It's assumed than any desired filtering
2242 was already done when sources was set on the from_scope.
2243
2244 If variables_to_not_forward_list is non-empty, then it must contains a list
2245 of variable names that will not be forwarded. This is mostly useful when
2246 variable_list_or_star has a value of "*".
2247```
2248
2249#### **Examples**
2250
2251```
Andrew Grieve57a964c2018-09-14 10:50:56 -04002252 # forward_variables_from(invoker, ["foo"])
2253 # is equivalent to:
2254 assert(!defined(foo))
2255 if (defined(invoker.foo)) {
2256 foo = invoker.foo
2257 }
2258
Brett Wilson796ed472018-07-16 15:11:09 -07002259 # This is a common action template. It would invoke a script with some given
2260 # parameters, and wants to use the various types of deps and the visibility
2261 # from the invoker if it's defined. It also injects an additional dependency
2262 # to all targets.
2263 template("my_test") {
2264 action(target_name) {
2265 forward_variables_from(invoker, [ "data_deps", "deps",
Julie Hockette2a29402018-07-31 10:11:42 -07002266 "public_deps", "visibility"])
Brett Wilson796ed472018-07-16 15:11:09 -07002267 # Add our test code to the dependencies.
2268 # "deps" may or may not be defined at this point.
2269 if (defined(deps)) {
2270 deps += [ "//tools/doom_melon" ]
2271 } else {
2272 deps = [ "//tools/doom_melon" ]
2273 }
2274 }
2275 }
2276
Julie Hockette2a29402018-07-31 10:11:42 -07002277 # This is a template around a target whose type depends on a global variable.
2278 # It forwards all values from the invoker.
Brett Wilson796ed472018-07-16 15:11:09 -07002279 template("my_wrapper") {
2280 target(my_wrapper_target_type, target_name) {
2281 forward_variables_from(invoker, "*")
2282 }
2283 }
2284
2285 # A template that wraps another. It adds behavior based on one
2286 # variable, and forwards all others to the nested target.
2287 template("my_ios_test_app") {
2288 ios_test_app(target_name) {
2289 forward_variables_from(invoker, "*", ["test_bundle_name"])
2290 if (!defined(extra_substitutions)) {
2291 extra_substitutions = []
2292 }
2293 extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ]
2294 }
2295 }
2296```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002297### **get_label_info**: Get an attribute from a target's label.
Brett Wilson796ed472018-07-16 15:11:09 -07002298
2299```
2300 get_label_info(target_label, what)
2301
2302 Given the label of a target, returns some attribute of that target. The
2303 target need not have been previously defined in the same file, since none of
2304 the attributes depend on the actual target definition, only the label itself.
2305
2306 See also "gn help get_target_outputs".
2307```
2308
2309#### **Possible values for the "what" parameter**
2310
2311```
2312 "name"
2313 The short name of the target. This will match the value of the
2314 "target_name" variable inside that target's declaration. For the label
2315 "//foo/bar:baz" this will return "baz".
2316
2317 "dir"
2318 The directory containing the target's definition, with no slash at the
2319 end. For the label "//foo/bar:baz" this will return "//foo/bar".
2320
2321 "target_gen_dir"
2322 The generated file directory for the target. This will match the value of
2323 the "target_gen_dir" variable when inside that target's declaration.
2324
2325 "root_gen_dir"
2326 The root of the generated file tree for the target. This will match the
2327 value of the "root_gen_dir" variable when inside that target's
2328 declaration.
2329
2330 "target_out_dir
2331 The output directory for the target. This will match the value of the
2332 "target_out_dir" variable when inside that target's declaration.
2333
2334 "root_out_dir"
2335 The root of the output file tree for the target. This will match the
2336 value of the "root_out_dir" variable when inside that target's
2337 declaration.
2338
2339 "label_no_toolchain"
2340 The fully qualified version of this label, not including the toolchain.
2341 For the input ":bar" it might return "//foo:bar".
2342
2343 "label_with_toolchain"
2344 The fully qualified version of this label, including the toolchain. For
2345 the input ":bar" it might return "//foo:bar(//toolchain:x64)".
2346
2347 "toolchain"
2348 The label of the toolchain. This will match the value of the
2349 "current_toolchain" variable when inside that target's declaration.
2350```
2351
2352#### **Examples**
2353
2354```
2355 get_label_info(":foo", "name")
2356 # Returns string "foo".
2357
2358 get_label_info("//foo/bar:baz", "target_gen_dir")
2359 # Returns string "//out/Debug/gen/foo/bar".
2360```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002361### **get_path_info**: Extract parts of a file or directory name.
Brett Wilson796ed472018-07-16 15:11:09 -07002362
2363```
2364 get_path_info(input, what)
2365
2366 The first argument is either a string representing a file or directory name,
2367 or a list of such strings. If the input is a list the return value will be a
2368 list containing the result of applying the rule to each item in the input.
2369```
2370
2371#### **Possible values for the "what" parameter**
2372
2373```
2374 "file"
2375 The substring after the last slash in the path, including the name and
2376 extension. If the input ends in a slash, the empty string will be
2377 returned.
2378 "foo/bar.txt" => "bar.txt"
2379 "bar.txt" => "bar.txt"
2380 "foo/" => ""
2381 "" => ""
2382
2383 "name"
2384 The substring of the file name not including the extension.
2385 "foo/bar.txt" => "bar"
2386 "foo/bar" => "bar"
2387 "foo/" => ""
2388
2389 "extension"
2390 The substring following the last period following the last slash, or the
2391 empty string if not found. The period is not included.
2392 "foo/bar.txt" => "txt"
2393 "foo/bar" => ""
2394
2395 "dir"
2396 The directory portion of the name, not including the slash.
2397 "foo/bar.txt" => "foo"
2398 "//foo/bar" => "//foo"
2399 "foo" => "."
2400
2401 The result will never end in a slash, so if the resulting is empty, the
2402 system ("/") or source ("//") roots, a "." will be appended such that it
2403 is always legal to append a slash and a filename and get a valid path.
2404
2405 "out_dir"
2406 The output file directory corresponding to the path of the given file,
2407 not including a trailing slash.
2408 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
2409
2410 "gen_dir"
2411 The generated file directory corresponding to the path of the given file,
2412 not including a trailing slash.
2413 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
2414
2415 "abspath"
2416 The full absolute path name to the file or directory. It will be resolved
2417 relative to the current directory, and then the source- absolute version
2418 will be returned. If the input is system- absolute, the same input will
2419 be returned.
2420 "foo/bar.txt" => "//mydir/foo/bar.txt"
2421 "foo/" => "//mydir/foo/"
2422 "//foo/bar" => "//foo/bar" (already absolute)
2423 "/usr/include" => "/usr/include" (already absolute)
2424
2425 If you want to make the path relative to another directory, or to be
2426 system-absolute, see rebase_path().
2427```
2428
2429#### **Examples**
2430```
2431 sources = [ "foo.cc", "foo.h" ]
2432 result = get_path_info(source, "abspath")
2433 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
2434
2435 result = get_path_info("//foo/bar/baz.cc", "dir")
2436 # result will be "//foo/bar"
2437
2438 # Extract the source-absolute directory name,
Joe Armstrong39413942019-03-15 10:34:03 +08002439 result = get_path_info(get_path_info(path, "dir"), "abspath")
Brett Wilson796ed472018-07-16 15:11:09 -07002440```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002441### **get_target_outputs**: [file list] Get the list of outputs from a target.
Brett Wilson796ed472018-07-16 15:11:09 -07002442
2443```
2444 get_target_outputs(target_label)
2445
2446 Returns a list of output files for the named target. The named target must
2447 have been previously defined in the current file before this function is
2448 called (it can't reference targets in other files because there isn't a
2449 defined execution order, and it obviously can't reference targets that are
2450 defined after the function call).
2451
Julie Hockettce1fa072019-05-07 17:44:37 -07002452 Only copy, generated_file, and action targets are supported. The outputs from
2453 binary targets will depend on the toolchain definition which won't
2454 necessarily have been loaded by the time a given line of code has run, and
2455 source sets and groups have no useful output file.
Brett Wilson796ed472018-07-16 15:11:09 -07002456```
2457
2458#### **Return value**
2459
2460```
2461 The names in the resulting list will be absolute file paths (normally like
2462 "//out/Debug/bar.exe", depending on the build directory).
2463
Julie Hockettce1fa072019-05-07 17:44:37 -07002464 action, copy, and generated_file targets: this will just return the files
2465 specified in the "outputs" variable of the target.
Brett Wilson796ed472018-07-16 15:11:09 -07002466
2467 action_foreach targets: this will return the result of applying the output
2468 template to the sources (see "gn help source_expansion"). This will be the
2469 same result (though with guaranteed absolute file paths), as
2470 process_file_template will return for those inputs (see "gn help
2471 process_file_template").
2472
2473 binary targets (executables, libraries): this will return a list of the
2474 resulting binary file(s). The "main output" (the actual binary or library)
2475 will always be the 0th element in the result. Depending on the platform and
2476 output type, there may be other output files as well (like import libraries)
2477 which will follow.
2478
2479 source sets and groups: this will return a list containing the path of the
2480 "stamp" file that Ninja will produce once all outputs are generated. This
2481 probably isn't very useful.
2482```
2483
2484#### **Example**
2485
2486```
2487 # Say this action generates a bunch of C source files.
2488 action_foreach("my_action") {
2489 sources = [ ... ]
2490 outputs = [ ... ]
2491 }
2492
2493 # Compile the resulting source files into a source set.
2494 source_set("my_lib") {
2495 sources = get_target_outputs(":my_action")
2496 }
2497```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002498### **getenv**: Get an environment variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002499
2500```
2501 value = getenv(env_var_name)
2502
2503 Returns the value of the given environment variable. If the value is not
2504 found, it will try to look up the variable with the "opposite" case (based on
2505 the case of the first letter of the variable), but is otherwise
2506 case-sensitive.
2507
2508 If the environment variable is not found, the empty string will be returned.
2509 Note: it might be nice to extend this if we had the concept of "none" in the
2510 language to indicate lookup failure.
2511```
2512
2513#### **Example**
2514
2515```
2516 home_dir = getenv("HOME")
2517```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002518### **import**: Import a file into the current scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002519
2520```
2521 The import command loads the rules and variables resulting from executing the
2522 given file into the current scope.
2523
2524 By convention, imported files are named with a .gni extension.
2525
2526 An import is different than a C++ "include". The imported file is executed in
2527 a standalone environment from the caller of the import command. The results
2528 of this execution are cached for other files that import the same .gni file.
2529
2530 Note that you can not import a BUILD.gn file that's otherwise used in the
2531 build. Files must either be imported or implicitly loaded as a result of deps
2532 rules, but not both.
2533
2534 The imported file's scope will be merged with the scope at the point import
2535 was called. If there is a conflict (both the current scope and the imported
2536 file define some variable or rule with the same name but different value), a
2537 runtime error will be thrown. Therefore, it's good practice to minimize the
2538 stuff that an imported file defines.
2539
2540 Variables and templates beginning with an underscore '_' are considered
2541 private and will not be imported. Imported files can use such variables for
2542 internal computation without affecting other files.
2543```
2544
2545#### **Examples**
2546
2547```
2548 import("//build/rules/idl_compilation_rule.gni")
2549
2550 # Looks in the current directory.
2551 import("my_vars.gni")
2552```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002553### **not_needed**: Mark variables from scope as not needed.
Brett Wilson796ed472018-07-16 15:11:09 -07002554
2555```
2556 not_needed(variable_list_or_star, variable_to_ignore_list = [])
2557 not_needed(from_scope, variable_list_or_star,
2558 variable_to_ignore_list = [])
2559
2560 Mark the variables in the current or given scope as not needed, which means
2561 you will not get an error about unused variables for these. The
2562 variable_to_ignore_list allows excluding variables from "all matches" if
2563 variable_list_or_star is "*".
2564```
2565
2566#### **Example**
2567
2568```
2569 not_needed("*", [ "config" ])
2570 not_needed([ "data_deps", "deps" ])
2571 not_needed(invoker, "*", [ "config" ])
2572 not_needed(invoker, [ "data_deps", "deps" ])
2573```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002574### **pool**: Defines a pool object.
Brett Wilson796ed472018-07-16 15:11:09 -07002575
2576```
2577 Pool objects can be applied to a tool to limit the parallelism of the
2578 build. This object has a single property "depth" corresponding to
2579 the number of tasks that may run simultaneously.
2580
2581 As the file containing the pool definition may be executed in the
2582 context of more than one toolchain it is recommended to specify an
2583 explicit toolchain when defining and referencing a pool.
2584
Julie Hockette2a29402018-07-31 10:11:42 -07002585 A pool named "console" defined in the root build file represents Ninja's
2586 console pool. Targets using this pool will have access to the console's
2587 stdin and stdout, and output will not be buffered. This special pool must
2588 have a depth of 1. Pools not defined in the root must not be named "console".
2589 The console pool can only be defined for the default toolchain.
2590 Refer to the Ninja documentation on the console pool for more info.
2591
Brett Wilson796ed472018-07-16 15:11:09 -07002592 A pool is referenced by its label just like a target.
2593```
2594
2595#### **Variables**
2596
2597```
2598 depth*
2599 * = required
2600```
2601
2602#### **Example**
2603
2604```
2605 if (current_toolchain == default_toolchain) {
2606 pool("link_pool") {
2607 depth = 1
2608 }
2609 }
2610
2611 toolchain("toolchain") {
2612 tool("link") {
2613 command = "..."
2614 pool = ":link_pool($default_toolchain)")
2615 }
2616 }
2617```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002618### **print**: Prints to the console.
Brett Wilson796ed472018-07-16 15:11:09 -07002619
2620```
2621 Prints all arguments to the console separated by spaces. A newline is
2622 automatically appended to the end.
2623
2624 This function is intended for debugging. Note that build files are run in
2625 parallel so you may get interleaved prints. A buildfile may also be executed
2626 more than once in parallel in the context of different toolchains so the
2627 prints from one file may be duplicated or
2628 interleaved with itself.
2629```
2630
2631#### **Examples**
2632
2633```
2634 print("Hello world")
2635
2636 print(sources, deps)
2637```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002638### **process_file_template**: Do template expansion over a list of files.
Brett Wilson796ed472018-07-16 15:11:09 -07002639
2640```
2641 process_file_template(source_list, template)
2642
2643 process_file_template applies a template list to a source file list,
2644 returning the result of applying each template to each source. This is
2645 typically used for computing output file names from input files.
2646
2647 In most cases, get_target_outputs() will give the same result with shorter,
2648 more maintainable code. This function should only be used when that function
2649 can't be used (like there's no target or the target is defined in another
2650 build file).
2651```
2652
2653#### **Arguments**
2654
2655```
2656 The source_list is a list of file names.
2657
2658 The template can be a string or a list. If it is a list, multiple output
2659 strings are generated for each input.
2660
2661 The template should contain source expansions to which each name in the
2662 source list is applied. See "gn help source_expansion".
2663```
2664
2665#### **Example**
2666
2667```
2668 sources = [
2669 "foo.idl",
2670 "bar.idl",
2671 ]
2672 myoutputs = process_file_template(
2673 sources,
2674 [ "$target_gen_dir/{{source_name_part}}.cc",
2675 "$target_gen_dir/{{source_name_part}}.h" ])
2676
2677 The result in this case will be:
2678 [ "//out/Debug/foo.cc"
2679 "//out/Debug/foo.h"
2680 "//out/Debug/bar.cc"
2681 "//out/Debug/bar.h" ]
2682```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002683### **read_file**: Read a file into a variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002684
2685```
2686 read_file(filename, input_conversion)
2687
2688 Whitespace will be trimmed from the end of the file. Throws an error if the
2689 file can not be opened.
2690```
2691
2692#### **Arguments**
2693
2694```
2695 filename
2696 Filename to read, relative to the build file.
2697
2698 input_conversion
Julie Hockett81ee1962019-05-13 11:24:42 -07002699 Controls how the file is read and parsed. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07002700```
2701
2702#### **Example**
2703
2704```
2705 lines = read_file("foo.txt", "list lines")
2706```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002707### **rebase_path**: Rebase a file or directory to another location.
Brett Wilson796ed472018-07-16 15:11:09 -07002708
2709```
2710 converted = rebase_path(input,
2711 new_base = "",
2712 current_base = ".")
2713
2714 Takes a string argument representing a file name, or a list of such strings
2715 and converts it/them to be relative to a different base directory.
2716
2717 When invoking the compiler or scripts, GN will automatically convert sources
2718 and include directories to be relative to the build directory. However, if
2719 you're passing files directly in the "args" array or doing other manual
2720 manipulations where GN doesn't know something is a file name, you will need
2721 to convert paths to be relative to what your tool is expecting.
2722
2723 The common case is to use this to convert paths relative to the current
2724 directory to be relative to the build directory (which will be the current
2725 directory when executing scripts).
2726
2727 If you want to convert a file path to be source-absolute (that is, beginning
2728 with a double slash like "//foo/bar"), you should use the get_path_info()
2729 function. This function won't work because it will always make relative
2730 paths, and it needs to support making paths relative to the source root, so
2731 can't also generate source-absolute paths without more special-cases.
2732```
2733
2734#### **Arguments**
2735
2736```
2737 input
2738 A string or list of strings representing file or directory names These
2739 can be relative paths ("foo/bar.txt"), system absolute paths
2740 ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt").
2741
2742 new_base
2743 The directory to convert the paths to be relative to. This can be an
2744 absolute path or a relative path (which will be treated as being relative
2745 to the current BUILD-file's directory).
2746
2747 As a special case, if new_base is the empty string (the default), all
2748 paths will be converted to system-absolute native style paths with system
2749 path separators. This is useful for invoking external programs.
2750
2751 current_base
2752 Directory representing the base for relative paths in the input. If this
2753 is not an absolute path, it will be treated as being relative to the
2754 current build file. Use "." (the default) to convert paths from the
2755 current BUILD-file's directory.
2756```
2757
2758#### **Return value**
2759
2760```
2761 The return value will be the same type as the input value (either a string or
2762 a list of strings). All relative and source-absolute file names will be
2763 converted to be relative to the requested output System-absolute paths will
2764 be unchanged.
2765
2766 Whether an output path will end in a slash will match whether the
2767 corresponding input path ends in a slash. It will return "." or "./"
2768 (depending on whether the input ends in a slash) to avoid returning empty
2769 strings. This means if you want a root path ("//" or "/") not ending in a
2770 slash, you can add a dot ("//.").
2771```
2772
2773#### **Example**
2774
2775```
2776 # Convert a file in the current directory to be relative to the build
2777 # directory (the current dir when executing compilers and scripts).
2778 foo = rebase_path("myfile.txt", root_build_dir)
2779 # might produce "../../project/myfile.txt".
2780
2781 # Convert a file to be system absolute:
2782 foo = rebase_path("myfile.txt")
2783 # Might produce "D:\\source\\project\\myfile.txt" on Windows or
2784 # "/home/you/source/project/myfile.txt" on Linux.
2785
2786 # Typical usage for converting to the build directory for a script.
2787 action("myscript") {
2788 # Don't convert sources, GN will automatically convert these to be relative
2789 # to the build directory when it constructs the command line for your
2790 # script.
2791 sources = [ "foo.txt", "bar.txt" ]
2792
2793 # Extra file args passed manually need to be explicitly converted
2794 # to be relative to the build directory:
2795 args = [
2796 "--data",
2797 rebase_path("//mything/data/input.dat", root_build_dir),
2798 "--rel",
2799 rebase_path("relative_path.txt", root_build_dir)
2800 ] + rebase_path(sources, root_build_dir)
2801 }
2802```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002803### **set_default_toolchain**: Sets the default toolchain name.
Brett Wilson796ed472018-07-16 15:11:09 -07002804
2805```
2806 set_default_toolchain(toolchain_label)
2807
2808 The given label should identify a toolchain definition (see "gn help
2809 toolchain"). This toolchain will be used for all targets unless otherwise
2810 specified.
2811
2812 This function is only valid to call during the processing of the build
2813 configuration file. Since the build configuration file is processed
2814 separately for each toolchain, this function will be a no-op when called
2815 under any non-default toolchains.
2816
2817 For example, the default toolchain should be appropriate for the current
2818 environment. If the current environment is 32-bit and somebody references a
2819 target with a 64-bit toolchain, we wouldn't want processing of the build
2820 config file for the 64-bit toolchain to reset the default toolchain to
2821 64-bit, we want to keep it 32-bits.
2822```
2823
2824#### **Argument**
2825
2826```
2827 toolchain_label
2828 Toolchain name.
2829```
2830
2831#### **Example**
2832
2833```
2834 # Set default toolchain only has an effect when run in the context of the
2835 # default toolchain. Pick the right one according to the current CPU
2836 # architecture.
2837 if (target_cpu == "x64") {
2838 set_default_toolchain("//toolchains:64")
2839 } else if (target_cpu == "x86") {
2840 set_default_toolchain("//toolchains:32")
2841 }
2842```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002843### **set_defaults**: Set default values for a target type.
Brett Wilson796ed472018-07-16 15:11:09 -07002844
2845```
2846 set_defaults() { }
2847
2848 Sets the default values for a given target type. Whenever target_type_name is
2849 seen in the future, the values specified in set_default's block will be
2850 copied into the current scope.
2851
2852 When the target type is used, the variable copying is very strict. If a
2853 variable with that name is already in scope, the build will fail with an
2854 error.
2855
2856 set_defaults can be used for built-in target types ("executable",
2857 "shared_library", etc.) and custom ones defined via the "template" command.
2858 It can be called more than once and the most recent call in any scope will
2859 apply, but there is no way to refer to the previous defaults and modify them
2860 (each call to set_defaults must supply a complete list of all defaults it
2861 wants). If you want to share defaults, store them in a separate variable.
2862```
2863
2864#### **Example**
2865
2866```
2867 set_defaults("static_library") {
2868 configs = [ "//tools/mything:settings" ]
2869 }
2870
Nico Webere49cb722018-08-28 13:10:29 -04002871 static_library("mylib") {
Brett Wilson796ed472018-07-16 15:11:09 -07002872 # The configs will be auto-populated as above. You can remove it if
2873 # you don't want the default for a particular default:
2874 configs -= [ "//tools/mything:settings" ]
2875 }
2876```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002877### **set_sources_assignment_filter**: Set a pattern to filter source files.
Brett Wilson796ed472018-07-16 15:11:09 -07002878
2879```
2880 The sources assignment filter is a list of patterns that remove files from
2881 the list implicitly whenever the "sources" variable is assigned to. This will
2882 do nothing for non-lists.
2883
2884 This is intended to be used to globally filter out files with
2885 platform-specific naming schemes when they don't apply, for example you may
2886 want to filter out all "*_win.cc" files on non-Windows platforms.
2887
2888 Typically this will be called once in the master build config script to set
2889 up the filter for the current platform. Subsequent calls will overwrite the
2890 previous values.
2891
2892 If you want to bypass the filter and add a file even if it might be filtered
2893 out, call set_sources_assignment_filter([]) to clear the list of filters.
2894 This will apply until the current scope exits
2895```
2896
2897#### **How to use patterns**
2898
2899```
2900 File patterns are VERY limited regular expressions. They must match the
2901 entire input string to be counted as a match. In regular expression parlance,
2902 there is an implicit "^...$" surrounding your input. If you want to match a
2903 substring, you need to use wildcards at the beginning and end.
2904
2905 There are only two special tokens understood by the pattern matcher.
2906 Everything else is a literal.
2907
2908 - "*" Matches zero or more of any character. It does not depend on the
2909 preceding character (in regular expression parlance it is equivalent to
2910 ".*").
2911
2912 - "\b" Matches a path boundary. This will match the beginning or end of a
2913 string, or a slash.
2914```
2915
2916#### **Pattern examples**
2917
2918```
2919 "*asdf*"
2920 Matches a string containing "asdf" anywhere.
2921
2922 "asdf"
2923 Matches only the exact string "asdf".
2924
2925 "*.cc"
2926 Matches strings ending in the literal ".cc".
2927
2928 "\bwin/*"
2929 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
2930```
2931
2932#### **Sources assignment example**
2933
2934```
2935 # Filter out all _win files.
2936 set_sources_assignment_filter([ "*_win.cc", "*_win.h" ])
2937 sources = [ "a.cc", "b_win.cc" ]
2938 print(sources)
2939 # Will print [ "a.cc" ]. b_win one was filtered out.
2940```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002941### **split_list**: Splits a list into N different sub-lists.
Brett Wilson796ed472018-07-16 15:11:09 -07002942
2943```
2944 result = split_list(input, n)
2945
2946 Given a list and a number N, splits the list into N sub-lists of
2947 approximately equal size. The return value is a list of the sub-lists. The
2948 result will always be a list of size N. If N is greater than the number of
2949 elements in the input, it will be padded with empty lists.
2950
2951 The expected use is to divide source files into smaller uniform chunks.
2952```
2953
2954#### **Example**
2955
2956```
2957 The code:
2958 mylist = [1, 2, 3, 4, 5, 6]
2959 print(split_list(mylist, 3))
2960
2961 Will print:
2962 [[1, 2], [3, 4], [5, 6]
2963```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002964### **string_replace**: Replaces substring in the given string.
Petr Hosekc0744ed2018-08-04 16:13:59 -07002965
2966```
2967 result = string_replace(str, old, new[, max])
2968
2969 Returns a copy of the string str in which the occurrences of old have been
2970 replaced with new, optionally restricting the number of replacements. The
2971 replacement is performed sequentially, so if new contains old, it won't be
2972 replaced.
2973```
2974
2975#### **Example**
2976
2977```
2978 The code:
2979 mystr = "Hello, world!"
2980 print(string_replace(mystr, "world", "GN"))
2981
2982 Will print:
2983 Hello, GN!
2984```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002985### **template**: Define a template rule.
Brett Wilson796ed472018-07-16 15:11:09 -07002986
2987```
2988 A template defines a custom name that acts like a function. It provides a way
2989 to add to the built-in target types.
2990
2991 The template() function is used to declare a template. To invoke the
2992 template, just use the name of the template like any other target type.
2993
2994 Often you will want to declare your template in a special file that other
2995 files will import (see "gn help import") so your template rule can be shared
2996 across build files.
2997```
2998
2999#### **Variables and templates**:
3000
3001```
3002 When you call template() it creates a closure around all variables currently
3003 in scope with the code in the template block. When the template is invoked,
3004 the closure will be executed.
3005
3006 When the template is invoked, the code in the caller is executed and passed
3007 to the template code as an implicit "invoker" variable. The template uses
3008 this to read state out of the invoking code.
3009
3010 One thing explicitly excluded from the closure is the "current directory"
3011 against which relative file names are resolved. The current directory will be
3012 that of the invoking code, since typically that code specifies the file
3013 names. This means all files internal to the template should use absolute
3014 names.
3015
3016 A template will typically forward some or all variables from the invoking
3017 scope to a target that it defines. Often, such variables might be optional.
3018 Use the pattern:
3019
3020 if (defined(invoker.deps)) {
3021 deps = invoker.deps
3022 }
3023
3024 The function forward_variables_from() provides a shortcut to forward one or
3025 more or possibly all variables in this manner:
3026
3027 forward_variables_from(invoker, ["deps", "public_deps"])
3028```
3029
3030#### **Target naming**
3031
3032```
3033 Your template should almost always define a built-in target with the name the
3034 template invoker specified. For example, if you have an IDL template and
3035 somebody does:
3036 idl("foo") {...
3037 you will normally want this to expand to something defining a source_set or
3038 static_library named "foo" (among other things you may need). This way, when
3039 another target specifies a dependency on "foo", the static_library or
3040 source_set will be linked.
3041
3042 It is also important that any other targets your template expands to have
3043 unique names, or you will get collisions.
3044
3045 Access the invoking name in your template via the implicit "target_name"
3046 variable. This should also be the basis for how other targets that a template
3047 expands to ensure uniqueness.
3048
3049 A typical example would be a template that defines an action to generate some
3050 source files, and a source_set to compile that source. Your template would
3051 name the source_set "target_name" because that's what you want external
3052 targets to depend on to link your code. And you would name the action
3053 something like "${target_name}_action" to make it unique. The source set
3054 would have a dependency on the action to make it run.
3055```
3056
3057#### **Overriding builtin targets**
3058
3059```
3060 You can use template to redefine a built-in target in which case your template
3061 takes a precedence over the built-in one. All uses of the target from within
3062 the template definition will refer to the built-in target which makes it
3063 possible to extend the behavior of the built-in target:
3064
3065 template("shared_library") {
3066 shared_library(shlib) {
Julie Hockette2a29402018-07-31 10:11:42 -07003067 forward_variables_from(invoker, "*")
Brett Wilson796ed472018-07-16 15:11:09 -07003068 ...
3069 }
3070 }
3071```
3072
3073#### **Example of defining a template**
3074
3075```
3076 template("my_idl") {
3077 # Be nice and help callers debug problems by checking that the variables
3078 # the template requires are defined. This gives a nice message rather than
3079 # giving the user an error about an undefined variable in the file defining
3080 # the template
3081 #
3082 # You can also use defined() to give default values to variables
3083 # unspecified by the invoker.
3084 assert(defined(invoker.sources),
3085 "Need sources in $target_name listing the idl files.")
3086
3087 # Name of the intermediate target that does the code gen. This must
3088 # incorporate the target name so it's unique across template
3089 # instantiations.
3090 code_gen_target_name = target_name + "_code_gen"
3091
3092 # Intermediate target to convert IDL to C source. Note that the name is
3093 # based on the name the invoker of the template specified. This way, each
3094 # time the template is invoked we get a unique intermediate action name
3095 # (since all target names are in the global scope).
3096 action_foreach(code_gen_target_name) {
3097 # Access the scope defined by the invoker via the implicit "invoker"
3098 # variable.
3099 sources = invoker.sources
3100
3101 # Note that we need an absolute path for our script file name. The
3102 # current directory when executing this code will be that of the invoker
3103 # (this is why we can use the "sources" directly above without having to
3104 # rebase all of the paths). But if we need to reference a script relative
3105 # to the template file, we'll need to use an absolute path instead.
3106 script = "//tools/idl/idl_code_generator.py"
3107
3108 # Tell GN how to expand output names given the sources.
3109 # See "gn help source_expansion" for more.
3110 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
3111 "$target_gen_dir/{{source_name_part}}.h" ]
3112 }
3113
3114 # Name the source set the same as the template invocation so instancing
3115 # this template produces something that other targets can link to in their
3116 # deps.
3117 source_set(target_name) {
3118 # Generates the list of sources, we get these from the action_foreach
3119 # above.
3120 sources = get_target_outputs(":$code_gen_target_name")
3121
3122 # This target depends on the files produced by the above code gen target.
3123 deps = [ ":$code_gen_target_name" ]
3124 }
3125 }
3126```
3127
3128#### **Example of invoking the resulting template**
3129
3130```
3131 # This calls the template code above, defining target_name to be
3132 # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly
3133 # brackets.
3134 my_idl("foo_idl_files") {
3135 # Goes into the template as "invoker.sources".
3136 sources = [ "foo.idl", "bar.idl" ]
3137 }
3138
3139 # Here is a target that depends on our template.
3140 executable("my_exe") {
3141 # Depend on the name we gave the template call above. Internally, this will
3142 # produce a dependency from executable to the source_set inside the
3143 # template (since it has this name), which will in turn depend on the code
3144 # gen action.
3145 deps = [ ":foo_idl_files" ]
3146 }
3147```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003148### **tool**: Specify arguments to a toolchain tool.
Brett Wilson796ed472018-07-16 15:11:09 -07003149
3150#### **Usage**
3151
3152```
3153 tool() {
3154
3155 }
3156```
3157
3158#### **Tool types**
3159
3160```
3161 Compiler tools:
3162 "cc": C compiler
3163 "cxx": C++ compiler
3164 "objc": Objective C compiler
3165 "objcxx": Objective C++ compiler
3166 "rc": Resource compiler (Windows .rc files)
3167 "asm": Assembler
3168
3169 Linker tools:
3170 "alink": Linker for static libraries (archives)
3171 "solink": Linker for shared libraries
3172 "link": Linker for executables
3173
3174 Other tools:
3175 "stamp": Tool for creating stamp files
3176 "copy": Tool to copy files.
3177 "action": Defaults for actions
3178
3179 Platform specific tools:
3180 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
3181 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
Julie Hockettce1fa072019-05-07 17:44:37 -07003182
3183 Rust tools:
3184 "rustc": Rust compiler and linker
Brett Wilson796ed472018-07-16 15:11:09 -07003185```
3186
3187#### **Tool variables**
3188
3189```
3190 command [string with substitutions]
3191 Valid for: all tools except "action" (required)
3192
3193 The command to run.
3194
3195 default_output_dir [string with substitutions]
3196 Valid for: linker tools
3197
3198 Default directory name for the output file relative to the
3199 root_build_dir. It can contain other substitution patterns. This will
3200 be the default value for the {{output_dir}} expansion (discussed below)
3201 but will be overridden by the "output_dir" variable in a target, if one
3202 is specified.
3203
3204 GN doesn't do anything with this string other than pass it along,
3205 potentially with target-specific overrides. It is the tool's job to use
3206 the expansion so that the files will be in the right place.
3207
3208 default_output_extension [string]
3209 Valid for: linker tools
3210
3211 Extension for the main output of a linkable tool. It includes the
3212 leading dot. This will be the default value for the
3213 {{output_extension}} expansion (discussed below) but will be overridden
3214 by by the "output extension" variable in a target, if one is specified.
3215 Empty string means no extension.
3216
3217 GN doesn't actually do anything with this extension other than pass it
3218 along, potentially with target-specific overrides. One would typically
3219 use the {{output_extension}} value in the "outputs" to read this value.
3220
3221 Example: default_output_extension = ".exe"
3222
3223 depfile [string with substitutions]
3224 Valid for: compiler tools (optional)
3225
3226 If the tool can write ".d" files, this specifies the name of the
3227 resulting file. These files are used to list header file dependencies
3228 (or other implicit input dependencies) that are discovered at build
3229 time. See also "depsformat".
3230
3231 Example: depfile = "{{output}}.d"
3232
3233 depsformat [string]
3234 Valid for: compiler tools (when depfile is specified)
3235
3236 Format for the deps outputs. This is either "gcc" or "msvc". See the
3237 ninja documentation for "deps" for more information.
3238
3239 Example: depsformat = "gcc"
3240
3241 description [string with substitutions, optional]
3242 Valid for: all tools
3243
3244 What to print when the command is run.
3245
3246 Example: description = "Compiling {{source}}"
3247
Julie Hockettce1fa072019-05-07 17:44:37 -07003248 exe_output_extension [string, optional, rust tools only]
3249 rlib_output_extension [string, optional, rust tools only]
3250 dylib_output_extension [string, optional, rust tools only]
3251 cdylib_output_extension [string, optional, rust tools only]
3252 proc_macro_output_extension [string, optional, rust tools only]
3253 Valid for: Rust tools
3254
3255 These specify the default tool output for each of the crate types.
3256 The default is empty for executables, shared, and static libraries and
3257 ".rlib" for rlibs. Note that the Rust compiler complains with an error
3258 if external crates do not take the form `lib.rlib` or
3259 `lib.`, where `` is `.so`,
3260 `.dylib`, or `.dll` as appropriate for the platform.
3261
Brett Wilson796ed472018-07-16 15:11:09 -07003262 lib_switch [string, optional, link tools only]
3263 lib_dir_switch [string, optional, link tools only]
3264 Valid for: Linker tools except "alink"
3265
3266 These strings will be prepended to the libraries and library search
3267 directories, respectively, because linkers differ on how specify them.
3268 If you specified:
3269 lib_switch = "-l"
3270 lib_dir_switch = "-L"
3271 then the "{{libs}}" expansion for [ "freetype", "expat"] would be
3272 "-lfreetype -lexpat".
3273
3274 outputs [list of strings with substitutions]
3275 Valid for: Linker and compiler tools (required)
3276
3277 An array of names for the output files the tool produces. These are
3278 relative to the build output directory. There must always be at least
3279 one output file. There can be more than one output (a linker might
3280 produce a library and an import library, for example).
3281
3282 This array just declares to GN what files the tool will produce. It is
3283 your responsibility to specify the tool command that actually produces
3284 these files.
3285
3286 If you specify more than one output for shared library links, you
3287 should consider setting link_output, depend_output, and
3288 runtime_outputs.
3289
3290 Example for a compiler tool that produces .obj files:
3291 outputs = [
3292 "{{source_out_dir}}/{{source_name_part}}.obj"
3293 ]
3294
3295 Example for a linker tool that produces a .dll and a .lib. The use of
3296 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
3297 the target to override these values.
3298 outputs = [
3299 "{{output_dir}}/{{target_output_name}}"
3300 "{{output_extension}}",
3301 "{{output_dir}}/{{target_output_name}}.lib",
3302 ]
3303
3304 pool [label, optional]
3305 Valid for: all tools (optional)
3306
3307 Label of the pool to use for the tool. Pools are used to limit the
3308 number of tasks that can execute concurrently during the build.
3309
3310 See also "gn help pool".
3311
3312 link_output [string with substitutions]
3313 depend_output [string with substitutions]
3314 Valid for: "solink" only (optional)
3315
3316 These two files specify which of the outputs from the solink tool
3317 should be used for linking and dependency tracking. These should match
3318 entries in the "outputs". If unspecified, the first item in the
3319 "outputs" array will be used for all. See "Separate linking and
3320 dependencies for shared libraries" below for more.
3321
3322 On Windows, where the tools produce a .dll shared library and a .lib
3323 import library, you will want the first two to be the import library
3324 and the third one to be the .dll file. On Linux, if you're not doing
3325 the separate linking/dependency optimization, all of these should be
3326 the .so output.
3327
3328 output_prefix [string]
3329 Valid for: Linker tools (optional)
3330
3331 Prefix to use for the output name. Defaults to empty. This prefix will
3332 be prepended to the name of the target (or the output_name if one is
3333 manually specified for it) if the prefix is not already there. The
3334 result will show up in the {{output_name}} substitution pattern.
3335
3336 Individual targets can opt-out of the output prefix by setting:
3337 output_prefix_override = true
3338 (see "gn help output_prefix_override").
3339
3340 This is typically used to prepend "lib" to libraries on
3341 Posix systems:
3342 output_prefix = "lib"
3343
3344 precompiled_header_type [string]
3345 Valid for: "cc", "cxx", "objc", "objcxx"
3346
3347 Type of precompiled headers. If undefined or the empty string,
3348 precompiled headers will not be used for this tool. Otherwise use "gcc"
3349 or "msvc".
3350
3351 For precompiled headers to be used for a given target, the target (or a
3352 config applied to it) must also specify a "precompiled_header" and, for
3353 "msvc"-style headers, a "precompiled_source" value. If the type is
3354 "gcc", then both "precompiled_header" and "precompiled_source" must
3355 resolve to the same file, despite the different formats required for
3356 each."
3357
3358 See "gn help precompiled_header" for more.
3359
3360 restat [boolean]
3361 Valid for: all tools (optional, defaults to false)
3362
3363 Requests that Ninja check the file timestamp after this tool has run to
3364 determine if anything changed. Set this if your tool has the ability to
3365 skip writing output if the output file has not changed.
3366
3367 Normally, Ninja will assume that when a tool runs the output be new and
3368 downstream dependents must be rebuild. When this is set to trye, Ninja
3369 can skip rebuilding downstream dependents for input changes that don't
3370 actually affect the output.
3371
3372 Example:
3373 restat = true
3374
3375 rspfile [string with substitutions]
3376 Valid for: all tools except "action" (optional)
3377
3378 Name of the response file. If empty, no response file will be
3379 used. See "rspfile_content".
3380
3381 rspfile_content [string with substitutions]
3382 Valid for: all tools except "action" (required when "rspfile" is used)
3383
3384 The contents to be written to the response file. This may include all
3385 or part of the command to send to the tool which allows you to get
3386 around OS command-line length limits.
3387
3388 This example adds the inputs and libraries to a response file, but
3389 passes the linker flags directly on the command line:
3390 tool("link") {
3391 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
3392 rspfile = "{{output}}.rsp"
3393 rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
3394 }
3395
3396 runtime_outputs [string list with substitutions]
3397 Valid for: linker tools
3398
3399 If specified, this list is the subset of the outputs that should be
3400 added to runtime deps (see "gn help runtime_deps"). By default (if
3401 runtime_outputs is empty or unspecified), it will be the link_output.
3402```
3403
3404#### **Expansions for tool variables**
3405
3406```
3407 All paths are relative to the root build directory, which is the current
3408 directory for running all tools. These expansions are available to all tools:
3409
3410 {{label}}
3411 The label of the current target. This is typically used in the
3412 "description" field for link tools. The toolchain will be omitted from
3413 the label for targets in the default toolchain, and will be included
3414 for targets in other toolchains.
3415
3416 {{label_name}}
3417 The short name of the label of the target. This is the part after the
3418 colon. For "//foo/bar:baz" this will be "baz". Unlike
3419 {{target_output_name}}, this is not affected by the "output_prefix" in
3420 the tool or the "output_name" set on the target.
3421
3422 {{output}}
3423 The relative path and name of the output(s) of the current build step.
3424 If there is more than one output, this will expand to a list of all of
3425 them. Example: "out/base/my_file.o"
3426
3427 {{target_gen_dir}}
3428 {{target_out_dir}}
3429 The directory of the generated file and output directories,
3430 respectively, for the current target. There is no trailing slash. See
3431 also {{output_dir}} for linker tools. Example: "out/base/test"
3432
3433 {{target_output_name}}
3434 The short name of the current target with no path information, or the
3435 value of the "output_name" variable if one is specified in the target.
3436 This will include the "output_prefix" if any. See also {{label_name}}.
3437
3438 Example: "libfoo" for the target named "foo" and an output prefix for
3439 the linker tool of "lib".
3440
3441 Compiler tools have the notion of a single input and a single output, along
3442 with a set of compiler-specific flags. The following expansions are
3443 available:
3444
3445 {{asmflags}}
3446 {{cflags}}
3447 {{cflags_c}}
3448 {{cflags_cc}}
3449 {{cflags_objc}}
3450 {{cflags_objcc}}
3451 {{defines}}
3452 {{include_dirs}}
3453 Strings correspond that to the processed flags/defines/include
3454 directories specified for the target.
3455 Example: "--enable-foo --enable-bar"
3456
3457 Defines will be prefixed by "-D" and include directories will be
3458 prefixed by "-I" (these work with Posix tools as well as Microsoft
3459 ones).
3460
3461 {{source}}
3462 The relative path and name of the current input file.
3463 Example: "../../base/my_file.cc"
3464
3465 {{source_file_part}}
3466 The file part of the source including the extension (with no directory
3467 information).
3468 Example: "foo.cc"
3469
3470 {{source_name_part}}
3471 The filename part of the source file with no directory or extension.
3472 Example: "foo"
3473
3474 {{source_gen_dir}}
3475 {{source_out_dir}}
3476 The directory in the generated file and output directories,
3477 respectively, for the current input file. If the source file is in the
3478 same directory as the target is declared in, they will will be the same
3479 as the "target" versions above. Example: "gen/base/test"
3480
Julie Hockette2a29402018-07-31 10:11:42 -07003481 Linker tools have multiple inputs and (potentially) multiple outputs. The
Brett Wilson796ed472018-07-16 15:11:09 -07003482 static library tool ("alink") is not considered a linker tool. The following
3483 expansions are available:
3484
3485 {{inputs}}
3486 {{inputs_newline}}
3487 Expands to the inputs to the link step. This will be a list of object
3488 files and static libraries.
3489 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
3490
3491 The "_newline" version will separate the input files with newlines
3492 instead of spaces. This is useful in response files: some linkers can
3493 take a "-filelist" flag which expects newline separated files, and some
3494 Microsoft tools have a fixed-sized buffer for parsing each line of a
3495 response file.
3496
3497 {{ldflags}}
3498 Expands to the processed set of ldflags and library search paths
3499 specified for the target.
3500 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
3501
3502 {{libs}}
3503 Expands to the list of system libraries to link to. Each will be
3504 prefixed by the "lib_switch".
3505
3506 As a special case to support Mac, libraries with names ending in
3507 ".framework" will be added to the {{libs}} with "-framework" preceding
3508 it, and the lib prefix will be ignored.
3509
3510 Example: "-lfoo -lbar"
3511
3512 {{output_dir}}
3513 The value of the "output_dir" variable in the target, or the the value
3514 of the "default_output_dir" value in the tool if the target does not
3515 override the output directory. This will be relative to the
3516 root_build_dir and will not end in a slash. Will be "." for output to
3517 the root_build_dir.
3518
3519 This is subtly different than {{target_out_dir}} which is defined by GN
3520 based on the target's path and not overridable. {{output_dir}} is for
3521 the final output, {{target_out_dir}} is generally for object files and
3522 other outputs.
3523
3524 Usually {{output_dir}} would be defined in terms of either
3525 {{target_out_dir}} or {{root_out_dir}}
3526
3527 {{output_extension}}
3528 The value of the "output_extension" variable in the target, or the
3529 value of the "default_output_extension" value in the tool if the target
3530 does not specify an output extension.
3531 Example: ".so"
3532
3533 {{solibs}}
3534 Extra libraries from shared library dependencies not specified in the
3535 {{inputs}}. This is the list of link_output files from shared libraries
3536 (if the solink tool specifies a "link_output" variable separate from
3537 the "depend_output").
3538
3539 These should generally be treated the same as libs by your tool.
3540
3541 Example: "libfoo.so libbar.so"
3542
3543 The static library ("alink") tool allows {{arflags}} plus the common tool
3544 substitutions.
3545
3546 The copy tool allows the common compiler/linker substitutions, plus
3547 {{source}} which is the source of the copy. The stamp tool allows only the
3548 common tool substitutions.
3549
3550 The copy_bundle_data and compile_xcassets tools only allows the common tool
3551 substitutions. Both tools are required to create iOS/macOS bundles and need
3552 only be defined on those platforms.
3553
3554 The copy_bundle_data tool will be called with one source and needs to copy
3555 (optionally optimizing the data representation) to its output. It may be
3556 called with a directory as input and it needs to be recursively copied.
3557
3558 The compile_xcassets tool will be called with one or more source (each an
3559 asset catalog) that needs to be compiled to a single output. The following
3560 substitutions are available:
3561
3562 {{inputs}}
3563 Expands to the list of .xcassets to use as input to compile the asset
3564 catalog.
3565
3566 {{bundle_product_type}}
3567 Expands to the product_type of the bundle that will contain the
3568 compiled asset catalog. Usually corresponds to the product_type
3569 property of the corresponding create_bundle target.
3570
3571 {{bundle_partial_info_plist}}
3572 Expands to the path to the partial Info.plist generated by the
3573 assets catalog compiler. Usually based on the target_name of
3574 the create_bundle target.
Julie Hockettce1fa072019-05-07 17:44:37 -07003575
3576 Rust tools have the notion of a single input and a single output, along
3577 with a set of compiler-specific flags. The following expansions are
3578 available:
3579
3580 {{crate_name}}
3581 Expands to the string representing the crate name of target under
3582 compilation.
3583
3584 {{crate_type}}
3585 Expands to the string representing the type of crate for the target
3586 under compilation.
3587
3588 {{externs}}
3589 Expands to the list of --extern flags needed to include addition Rust
3590 libraries in this target. Includes any specified renamed dependencies.
3591
3592 {{rustc_output_extension}}
3593 Expands to the output extension for this target's crate type.
3594
3595 {{rustc_output_prefix}}
3596 Expands to the prefix for shared and static libraries. This should
3597 generally be "lib". Empty for executable targets.
3598
3599 {{rustdeps}}
3600 Expands to the list of -Ldependency= strings needed to compile
3601 this target.
3602
3603 {{rustenv}}
3604 Expands to the list of environment variables.
3605
3606 {{rustflags}}
3607 Expands to the list of strings representing Rust compiler flags.
Brett Wilson796ed472018-07-16 15:11:09 -07003608```
3609
3610#### **Separate linking and dependencies for shared libraries**
3611
3612```
3613 Shared libraries are special in that not all changes to them require that
3614 dependent targets be re-linked. If the shared library is changed but no
3615 imports or exports are different, dependent code needn't be relinked, which
3616 can speed up the build.
3617
3618 If your link step can output a list of exports from a shared library and
3619 writes the file only if the new one is different, the timestamp of this file
3620 can be used for triggering re-links, while the actual shared library would be
3621 used for linking.
3622
3623 You will need to specify
3624 restat = true
3625 in the linker tool to make this work, so Ninja will detect if the timestamp
3626 of the dependency file has changed after linking (otherwise it will always
3627 assume that running a command updates the output):
3628
3629 tool("solink") {
3630 command = "..."
3631 outputs = [
3632 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
3633 "{{output_dir}}/{{target_output_name}}"
3634 "{{output_extension}}.TOC",
3635 ]
3636 link_output =
3637 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
3638 depend_output =
3639 "{{output_dir}}/{{target_output_name}}"
3640 "{{output_extension}}.TOC"
3641 restat = true
3642 }
3643```
3644
3645#### **Example**
3646
3647```
3648 toolchain("my_toolchain") {
3649 # Put these at the top to apply to all tools below.
3650 lib_switch = "-l"
3651 lib_dir_switch = "-L"
3652
3653 tool("cc") {
3654 command = "gcc {{source}} -o {{output}}"
3655 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
3656 description = "GCC {{source}}"
3657 }
3658 tool("cxx") {
3659 command = "g++ {{source}} -o {{output}}"
3660 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
3661 description = "G++ {{source}}"
3662 }
3663 };
3664```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003665### **toolchain**: Defines a toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07003666
3667```
3668 A toolchain is a set of commands and build flags used to compile the source
3669 code. The toolchain() function defines these commands.
3670```
3671
3672#### **Toolchain overview**
3673
3674```
3675 You can have more than one toolchain in use at once in a build and a target
3676 can exist simultaneously in multiple toolchains. A build file is executed
3677 once for each toolchain it is referenced in so the GN code can vary all
3678 parameters of each target (or which targets exist) on a per-toolchain basis.
3679
3680 When you have a simple build with only one toolchain, the build config file
3681 is loaded only once at the beginning of the build. It must call
3682 set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the
3683 label of the toolchain definition to use. The "toolchain_args" section of the
3684 toolchain definition is ignored.
3685
3686 When a target has a dependency on a target using different toolchain (see "gn
3687 help labels" for how to specify this), GN will start a build using that
3688 secondary toolchain to resolve the target. GN will load the build config file
3689 with the build arguments overridden as specified in the toolchain_args.
3690 Because the default toolchain is already known, calls to
3691 set_default_toolchain() are ignored.
3692
3693 To load a file in an alternate toolchain, GN does the following:
3694
3695 1. Loads the file with the toolchain definition in it (as determined by the
3696 toolchain label).
3697 2. Re-runs the master build configuration file, applying the arguments
3698 specified by the toolchain_args section of the toolchain definition.
3699 3. Loads the destination build file in the context of the configuration file
3700 in the previous step.
3701
3702 The toolchain configuration is two-way. In the default toolchain (i.e. the
3703 main build target) the configuration flows from the build config file to the
3704 toolchain. The build config file looks at the state of the build (OS type,
3705 CPU architecture, etc.) and decides which toolchain to use (via
3706 set_default_toolchain()). In secondary toolchains, the configuration flows
3707 from the toolchain to the build config file: the "toolchain_args" in the
3708 toolchain definition specifies the arguments to re-invoke the build.
3709```
3710
3711#### **Functions and variables**
3712
3713```
3714 tool()
Julie Hockette2a29402018-07-31 10:11:42 -07003715 The tool() function call specifies the commands to run for a given step. See
3716 "gn help tool".
Brett Wilson796ed472018-07-16 15:11:09 -07003717
Julie Hockette2a29402018-07-31 10:11:42 -07003718 toolchain_args [scope]
Brett Wilson796ed472018-07-16 15:11:09 -07003719 Overrides for build arguments to pass to the toolchain when invoking it.
3720 This is a variable of type "scope" where the variable names correspond to
3721 variables in declare_args() blocks.
3722
3723 When you specify a target using an alternate toolchain, the master build
3724 configuration file is re-interpreted in the context of that toolchain.
3725 toolchain_args allows you to control the arguments passed into this
3726 alternate invocation of the build.
3727
3728 Any default system arguments or arguments passed in via "gn args" will also
3729 be passed to the alternate invocation unless explicitly overridden by
3730 toolchain_args.
3731
3732 The toolchain_args will be ignored when the toolchain being defined is the
3733 default. In this case, it's expected you want the default argument values.
3734
3735 See also "gn help buildargs" for an overview of these arguments.
3736
Julie Hockette2a29402018-07-31 10:11:42 -07003737 propagates_configs [boolean, default=false]
3738 Determines whether public_configs and all_dependent_configs in this
3739 toolchain propagate to targets in other toolchains.
3740
3741 When false (the default), this toolchain will not propagate any configs to
3742 targets in other toolchains that depend on it targets inside this
3743 toolchain. This matches the most common usage of toolchains where they
3744 represent different architectures or compilers and the settings that apply
3745 to one won't necessarily apply to others.
3746
3747 When true, configs (public and all-dependent) will cross the boundary out
3748 of this toolchain as if the toolchain boundary wasn't there. This only
3749 affects one direction of dependencies: a toolchain can't control whether
3750 it accepts such configs, only whether it pushes them. The build is
3751 responsible for ensuring that any external targets depending on targets in
3752 this toolchain are compatible with the compiler flags, etc. that may be
3753 propagated.
3754
3755 deps [string list]
Brett Wilson796ed472018-07-16 15:11:09 -07003756 Dependencies of this toolchain. These dependencies will be resolved before
3757 any target in the toolchain is compiled. To avoid circular dependencies
3758 these must be targets defined in another toolchain.
3759
3760 This is expressed as a list of targets, and generally these targets will
3761 always specify a toolchain:
3762 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
3763
3764 This concept is somewhat inefficient to express in Ninja (it requires a lot
3765 of duplicate of rules) so should only be used when absolutely necessary.
3766```
3767
3768#### **Example of defining a toolchain**
3769
3770```
3771 toolchain("32") {
3772 tool("cc") {
3773 command = "gcc {{source}}"
3774 ...
3775 }
3776
3777 toolchain_args = {
3778 use_doom_melon = true # Doom melon always required for 32-bit builds.
3779 current_cpu = "x86"
3780 }
3781 }
3782
3783 toolchain("64") {
3784 tool("cc") {
3785 command = "gcc {{source}}"
3786 ...
3787 }
3788
3789 toolchain_args = {
3790 # use_doom_melon is not overridden here, it will take the default.
3791 current_cpu = "x64"
3792 }
3793 }
3794```
3795
3796#### **Example of cross-toolchain dependencies**
3797
3798```
3799 If a 64-bit target wants to depend on a 32-bit binary, it would specify a
3800 dependency using data_deps (data deps are like deps that are only needed at
3801 runtime and aren't linked, since you can't link a 32-bit and a 64-bit
3802 library).
3803
3804 executable("my_program") {
3805 ...
3806 if (target_cpu == "x64") {
3807 # The 64-bit build needs this 32-bit helper.
3808 data_deps = [ ":helper(//toolchains:32)" ]
3809 }
3810 }
3811
3812 if (target_cpu == "x86") {
3813 # Our helper library is only compiled in 32-bits.
3814 shared_library("helper") {
3815 ...
3816 }
3817 }
3818```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003819### **write_file**: Write a file to disk.
Brett Wilson796ed472018-07-16 15:11:09 -07003820
3821```
Julie Hockett09171292018-07-31 14:35:10 -07003822 write_file(filename, data, output_conversion = "")
Brett Wilson796ed472018-07-16 15:11:09 -07003823
3824 If data is a list, the list will be written one-item-per-line with no quoting
3825 or brackets.
3826
3827 If the file exists and the contents are identical to that being written, the
3828 file will not be updated. This will prevent unnecessary rebuilds of targets
3829 that depend on this file.
3830
3831 One use for write_file is to write a list of inputs to an script that might
3832 be too long for the command line. However, it is preferable to use response
3833 files for this purpose. See "gn help response_file_contents".
Brett Wilson796ed472018-07-16 15:11:09 -07003834```
3835
3836#### **Arguments**
3837
3838```
3839 filename
3840 Filename to write. This must be within the output directory.
3841
3842 data
3843 The list or string to write.
Julie Hockett09171292018-07-31 14:35:10 -07003844
3845 output_conversion
Julie Hockett81ee1962019-05-13 11:24:42 -07003846 Controls how the output is written. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07003847```
3848## Built-in predefined variables
3849
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003850### **current_cpu**: The processor architecture of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07003851
3852```
3853 The build configuration usually sets this value based on the value of
3854 "host_cpu" (see "gn help host_cpu") and then threads this through the
3855 toolchain definitions to ensure that it always reflects the appropriate
3856 value.
3857
Julie Hockette2a29402018-07-31 10:11:42 -07003858 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07003859 empty string ("") by default but is declared so that it can be overridden on
3860 the command line if so desired.
3861
3862 See "gn help target_cpu" for a list of common values returned.
3863```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003864### **current_os**: The operating system of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07003865
3866```
3867 The build configuration usually sets this value based on the value of
3868 "target_os" (see "gn help target_os"), and then threads this through the
3869 toolchain definitions to ensure that it always reflects the appropriate
3870 value.
3871
Julie Hockette2a29402018-07-31 10:11:42 -07003872 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07003873 empty string ("") by default but is declared so that it can be overridden on
3874 the command line if so desired.
3875
3876 See "gn help target_os" for a list of common values returned.
3877```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003878### **current_toolchain**: Label of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07003879
3880```
3881 A fully-qualified label representing the current toolchain. You can use this
3882 to make toolchain-related decisions in the build. See also
3883 "default_toolchain".
3884```
3885
3886#### **Example**
3887
3888```
3889 if (current_toolchain == "//build:64_bit_toolchain") {
3890 executable("output_thats_64_bit_only") {
3891 ...
3892```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003893### **default_toolchain**: [string] Label of the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07003894
3895```
3896 A fully-qualified label representing the default toolchain, which may not
3897 necessarily be the current one (see "current_toolchain").
3898```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003899### **host_cpu**: The processor architecture that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07003900
3901```
3902 This is value is exposed so that cross-compile toolchains can access the host
3903 architecture when needed.
3904
3905 The value should generally be considered read-only, but it can be overriden
3906 in order to handle unusual cases where there might be multiple plausible
3907 values for the host architecture (e.g., if you can do either 32-bit or 64-bit
3908 builds). The value is not used internally by GN for any purpose.
3909```
3910
3911#### **Some possible values**
3912
3913```
3914 - "x64"
3915 - "x86"
3916```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003917### **host_os**: [string] The operating system that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07003918
3919```
3920 This value is exposed so that cross-compiles can access the host build
3921 system's settings.
3922
3923 This value should generally be treated as read-only. It, however, is not used
3924 internally by GN for any purpose.
3925```
3926
3927#### **Some possible values**
3928
3929```
3930 - "linux"
3931 - "mac"
3932 - "win"
3933```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003934### **invoker**: [string] The invoking scope inside a template.
Brett Wilson796ed472018-07-16 15:11:09 -07003935
3936```
3937 Inside a template invocation, this variable refers to the scope of the
3938 invoker of the template. Outside of template invocations, this variable is
3939 undefined.
3940
3941 All of the variables defined inside the template invocation are accessible as
3942 members of the "invoker" scope. This is the way that templates read values
3943 set by the callers.
3944
3945 This is often used with "defined" to see if a value is set on the invoking
3946 scope.
3947
3948 See "gn help template" for more examples.
3949```
3950
3951#### **Example**
3952
3953```
3954 template("my_template") {
3955 print(invoker.sources) # Prints [ "a.cc", "b.cc" ]
3956 print(defined(invoker.foo)) # Prints false.
3957 print(defined(invoker.bar)) # Prints true.
3958 }
3959
3960 my_template("doom_melon") {
3961 sources = [ "a.cc", "b.cc" ]
3962 bar = 123
3963 }
3964```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003965### **python_path**: Absolute path of Python.
Brett Wilson796ed472018-07-16 15:11:09 -07003966
3967```
3968 Normally used in toolchain definitions if running some command requires
3969 Python. You will normally not need this when invoking scripts since GN
3970 automatically finds it for you.
3971```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003972### **root_build_dir**: [string] Directory where build commands are run.
Brett Wilson796ed472018-07-16 15:11:09 -07003973
3974```
3975 This is the root build output directory which will be the current directory
3976 when executing all compilers and scripts.
3977
3978 Most often this is used with rebase_path (see "gn help rebase_path") to
3979 convert arguments to be relative to a script's current directory.
3980```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003981### **root_gen_dir**: Directory for the toolchain's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07003982
3983```
3984 Absolute path to the root of the generated output directory tree for the
3985 current toolchain. An example would be "//out/Debug/gen" for the default
3986 toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain.
3987
3988 This is primarily useful for setting up include paths for generated files. If
3989 you are passing this to a script, you will want to pass it through
3990 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
3991 build directory.
3992
3993 See also "target_gen_dir" which is usually a better location for generated
3994 files. It will be inside the root generated dir.
3995```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003996### **root_out_dir**: [string] Root directory for toolchain output files.
Brett Wilson796ed472018-07-16 15:11:09 -07003997
3998```
3999 Absolute path to the root of the output directory tree for the current
4000 toolchain. It will not have a trailing slash.
4001
4002 For the default toolchain this will be the same as the root_build_dir. An
4003 example would be "//out/Debug" for the default toolchain, or
4004 "//out/Debug/arm" for the "arm" toolchain.
4005
4006 This is primarily useful for setting up script calls. If you are passing this
4007 to a script, you will want to pass it through rebase_path() (see "gn help
4008 rebase_path") to convert it to be relative to the build directory.
4009
4010 See also "target_out_dir" which is usually a better location for output
4011 files. It will be inside the root output dir.
4012```
4013
4014#### **Example**
4015
4016```
4017 action("myscript") {
4018 # Pass the output dir to the script.
4019 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
4020 }
4021```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004022### **target_cpu**: The desired cpu architecture for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004023
4024```
4025 This value should be used to indicate the desired architecture for the
4026 primary objects of the build. It will match the cpu architecture of the
4027 default toolchain, but not necessarily the current toolchain.
4028
4029 In many cases, this is the same as "host_cpu", but in the case of
4030 cross-compiles, this can be set to something different. This value is
4031 different from "current_cpu" in that it does not change based on the current
4032 toolchain. When writing rules, "current_cpu" should be used rather than
4033 "target_cpu" most of the time.
4034
4035 This value is not used internally by GN for any purpose, so it may be set to
4036 whatever value is needed for the build. GN defaults this value to the empty
4037 string ("") and the configuration files should set it to an appropriate value
4038 (e.g., setting it to the value of "host_cpu") if it is not overridden on the
4039 command line or in the args.gn file.
4040```
4041
4042#### **Possible values**
4043
4044```
4045 - "x86"
4046 - "x64"
4047 - "arm"
4048 - "arm64"
4049 - "mipsel"
4050```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004051### **target_gen_dir**: Directory for a target's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004052
4053```
4054 Absolute path to the target's generated file directory. This will be the
4055 "root_gen_dir" followed by the relative path to the current build file. If
4056 your file is in "//tools/doom_melon" then target_gen_dir would be
4057 "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash.
4058
4059 This is primarily useful for setting up include paths for generated files. If
4060 you are passing this to a script, you will want to pass it through
4061 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4062 build directory.
4063
4064 See also "gn help root_gen_dir".
4065```
4066
4067#### **Example**
4068
4069```
4070 action("myscript") {
4071 # Pass the generated output dir to the script.
4072 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]"
4073 }
4074```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004075### **target_name**: [string] The name of the current target.
Brett Wilson796ed472018-07-16 15:11:09 -07004076
4077```
4078 Inside a target or template invocation, this variable refers to the name
4079 given to the target or template invocation. Outside of these, this variable
4080 is undefined.
4081
4082 This is most often used in template definitions to name targets defined in
4083 the template based on the name of the invocation. This is necessary both to
4084 ensure generated targets have unique names and to generate a target with the
4085 exact name of the invocation that other targets can depend on.
4086
4087 Be aware that this value will always reflect the innermost scope. So when
4088 defining a target inside a template, target_name will refer to the target
4089 rather than the template invocation. To get the name of the template
4090 invocation in this case, you should save target_name to a temporary variable
4091 outside of any target definitions.
4092
4093 See "gn help template" for more examples.
4094```
4095
4096#### **Example**
4097
4098```
4099 executable("doom_melon") {
4100 print(target_name) # Prints "doom_melon".
4101 }
4102
4103 template("my_template") {
4104 print(target_name) # Prints "space_ray" when invoked below.
4105
4106 executable(target_name + "_impl") {
4107 print(target_name) # Prints "space_ray_impl".
4108 }
4109 }
4110
4111 my_template("space_ray") {
4112 }
4113```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004114### **target_os**: The desired operating system for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004115
4116```
4117 This value should be used to indicate the desired operating system for the
4118 primary object(s) of the build. It will match the OS of the default
4119 toolchain.
4120
4121 In many cases, this is the same as "host_os", but in the case of
4122 cross-compiles, it may be different. This variable differs from "current_os"
4123 in that it can be referenced from inside any toolchain and will always return
4124 the initial value.
4125
4126 This should be set to the most specific value possible. So, "android" or
4127 "chromeos" should be used instead of "linux" where applicable, even though
4128 Android and ChromeOS are both Linux variants. This can mean that one needs to
4129 write
4130
4131 if (target_os == "android" || target_os == "linux") {
4132 # ...
4133 }
4134
4135 and so forth.
4136
4137 This value is not used internally by GN for any purpose, so it may be set to
4138 whatever value is needed for the build. GN defaults this value to the empty
4139 string ("") and the configuration files should set it to an appropriate value
4140 (e.g., setting it to the value of "host_os") if it is not set via the command
4141 line or in the args.gn file.
4142```
4143
4144#### **Possible values**
4145
4146```
4147 - "android"
4148 - "chromeos"
4149 - "ios"
4150 - "linux"
4151 - "nacl"
4152 - "mac"
4153 - "win"
4154```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004155### **target_out_dir**: [string] Directory for target output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004156
4157```
4158 Absolute path to the target's generated file directory. If your current
4159 target is in "//tools/doom_melon" then this value might be
4160 "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash.
4161
4162 This is primarily useful for setting up arguments for calling scripts. If you
4163 are passing this to a script, you will want to pass it through rebase_path()
4164 (see "gn help rebase_path") to convert it to be relative to the build
4165 directory.
4166
4167 See also "gn help root_out_dir".
4168```
4169
4170#### **Example**
4171
4172```
4173 action("myscript") {
4174 # Pass the output dir to the script.
4175 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]"
Brett Wilson796ed472018-07-16 15:11:09 -07004176 }
4177```
4178## Variables you set in targets
4179
Julie Hockettce1fa072019-05-07 17:44:37 -07004180### **aliased_deps**: [scope] Set of crate-dependency pairs.
4181
4182```
4183 Valid for `rust_library` targets and `executable`, `static_library`, and
4184 `shared_library` targets that contain Rust sources.
4185
4186 A scope, each key indicating the renamed crate and the corresponding value
4187 specifying the label of the dependency producing the relevant binary.
4188
4189 All dependencies listed in this field *must* be listed as deps of the target.
4190
4191 executable("foo") {
4192 sources = [ "main.rs" ]
4193 deps = [ "//bar" ]
4194 }
4195
4196 This target would compile the `foo` crate with the following `extern` flag:
4197 `rustc ...command... --extern bar=/obj/bar`
4198
4199 executable("foo") {
4200 sources = [ "main.rs" ]
4201 deps = [ ":bar" ]
4202 aliased_deps = {
4203 bar_renamed = ":bar"
4204 }
4205 }
4206
4207 With the addition of `aliased_deps`, above target would instead compile with:
4208 `rustc ...command... --extern bar_renamed=/obj/bar`
4209```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004210### **all_dependent_configs**: Configs to be forced on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07004211
4212```
4213 A list of config labels.
4214
4215 All targets depending on this one, and recursively, all targets depending on
4216 those, will have the configs listed in this variable added to them. These
4217 configs will also apply to the current target.
4218
4219 This addition happens in a second phase once a target and all of its
4220 dependencies have been resolved. Therefore, a target will not see these
4221 force-added configs in their "configs" variable while the script is running,
4222 and they can not be removed. As a result, this capability should generally
4223 only be used to add defines and include directories necessary to compile a
4224 target's headers.
4225
4226 See also "public_configs".
4227```
4228
4229#### **Ordering of flags and values**
4230
4231```
4232 1. Those set on the current target (not in a config).
4233 2. Those set on the "configs" on the target in order that the
4234 configs appear in the list.
4235 3. Those set on the "all_dependent_configs" on the target in order
4236 that the configs appear in the list.
4237 4. Those set on the "public_configs" on the target in order that
4238 those configs appear in the list.
4239 5. all_dependent_configs pulled from dependencies, in the order of
4240 the "deps" list. This is done recursively. If a config appears
4241 more than once, only the first occurence will be used.
4242 6. public_configs pulled from dependencies, in the order of the
4243 "deps" list. If a dependency is public, they will be applied
4244 recursively.
4245```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004246### **allow_circular_includes_from**: Permit includes from deps.
Brett Wilson796ed472018-07-16 15:11:09 -07004247
4248```
4249 A list of target labels. Must be a subset of the target's "deps". These
4250 targets will be permitted to include headers from the current target despite
4251 the dependency going in the opposite direction.
4252
4253 When you use this, both targets must be included in a final binary for it to
4254 link. To keep linker errors from happening, it is good practice to have all
4255 external dependencies depend only on one of the two targets, and to set the
4256 visibility on the other to enforce this. Thus the targets will always be
4257 linked together in any output.
4258```
4259
4260#### **Details**
4261
4262```
4263 Normally, for a file in target A to include a file from target B, A must list
4264 B as a dependency. This invariant is enforced by the "gn check" command (and
4265 the --check flag to "gn gen" -- see "gn help check").
4266
4267 Sometimes, two targets might be the same unit for linking purposes (two
4268 source sets or static libraries that would always be linked together in a
4269 final executable or shared library) and they each include headers from the
4270 other: you want A to be able to include B's headers, and B to include A's
4271 headers. This is not an ideal situation but is sometimes unavoidable.
4272
4273 This list, if specified, lists which of the dependencies of the current
4274 target can include header files from the current target. That is, if A
4275 depends on B, B can only include headers from A if it is in A's
4276 allow_circular_includes_from list. Normally includes must follow the
4277 direction of dependencies, this flag allows them to go in the opposite
4278 direction.
4279```
4280
4281#### **Danger**
4282
4283```
4284 In the above example, A's headers are likely to include headers from A's
4285 dependencies. Those dependencies may have public_configs that apply flags,
4286 defines, and include paths that make those headers work properly.
4287
4288 With allow_circular_includes_from, B can include A's headers, and
4289 transitively from A's dependencies, without having the dependencies that
4290 would bring in the public_configs those headers need. The result may be
4291 errors or inconsistent builds.
4292
4293 So when you use allow_circular_includes_from, make sure that any compiler
4294 settings, flags, and include directories are the same between both targets
4295 (consider putting such things in a shared config they can both reference).
4296 Make sure the dependencies are also the same (you might consider a group to
4297 collect such dependencies they both depend on).
4298```
4299
4300#### **Example**
4301
4302```
4303 source_set("a") {
4304 deps = [ ":b", ":a_b_shared_deps" ]
4305 allow_circular_includes_from = [ ":b" ]
4306 ...
4307 }
4308
4309 source_set("b") {
4310 deps = [ ":a_b_shared_deps" ]
4311 # Sources here can include headers from a despite lack of deps.
4312 ...
4313 }
4314
4315 group("a_b_shared_deps") {
4316 public_deps = [ ":c" ]
4317 }
4318```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004319### **arflags**: Arguments passed to static_library archiver.
Brett Wilson796ed472018-07-16 15:11:09 -07004320
4321```
4322 A list of flags passed to the archive/lib command that creates static
4323 libraries.
4324
4325 arflags are NOT pushed to dependents, so applying arflags to source sets or
4326 any other target type will be a no-op. As with ldflags, you could put the
4327 arflags in a config and set that as a public or "all dependent" config, but
4328 that will likely not be what you want. If you have a chain of static
4329 libraries dependent on each other, this can cause the flags to propagate up
4330 to other static libraries. Due to the nature of how arflags are typically
4331 used, you will normally want to apply them directly on static_library targets
4332 themselves.
4333```
4334
4335#### **Ordering of flags and values**
4336
4337```
4338 1. Those set on the current target (not in a config).
4339 2. Those set on the "configs" on the target in order that the
4340 configs appear in the list.
4341 3. Those set on the "all_dependent_configs" on the target in order
4342 that the configs appear in the list.
4343 4. Those set on the "public_configs" on the target in order that
4344 those configs appear in the list.
4345 5. all_dependent_configs pulled from dependencies, in the order of
4346 the "deps" list. This is done recursively. If a config appears
4347 more than once, only the first occurence will be used.
4348 6. public_configs pulled from dependencies, in the order of the
4349 "deps" list. If a dependency is public, they will be applied
4350 recursively.
4351```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004352### **args**: (target variable) Arguments passed to an action.
Brett Wilson796ed472018-07-16 15:11:09 -07004353
4354```
4355 For action and action_foreach targets, args is the list of arguments to pass
4356 to the script. Typically you would use source expansion (see "gn help
4357 source_expansion") to insert the source file names.
4358
4359 See also "gn help action" and "gn help action_foreach".
4360```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004361### **asmflags**: Flags passed to the assembler.
Brett Wilson796ed472018-07-16 15:11:09 -07004362
4363```
4364 A list of strings.
4365
4366 "asmflags" are passed to any invocation of a tool that takes an .asm or .S
4367 file as input.
4368```
4369
4370#### **Ordering of flags and values**
4371
4372```
4373 1. Those set on the current target (not in a config).
4374 2. Those set on the "configs" on the target in order that the
4375 configs appear in the list.
4376 3. Those set on the "all_dependent_configs" on the target in order
4377 that the configs appear in the list.
4378 4. Those set on the "public_configs" on the target in order that
4379 those configs appear in the list.
4380 5. all_dependent_configs pulled from dependencies, in the order of
4381 the "deps" list. This is done recursively. If a config appears
4382 more than once, only the first occurence will be used.
4383 6. public_configs pulled from dependencies, in the order of the
4384 "deps" list. If a dependency is public, they will be applied
4385 recursively.
4386```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004387### **assert_no_deps**: Ensure no deps on these targets.
Brett Wilson796ed472018-07-16 15:11:09 -07004388
4389```
4390 A list of label patterns.
4391
4392 This list is a list of patterns that must not match any of the transitive
4393 dependencies of the target. These include all public, private, and data
4394 dependencies, and cross shared library boundaries. This allows you to express
4395 that undesirable code isn't accidentally added to downstream dependencies in
4396 a way that might otherwise be difficult to notice.
4397
4398 Checking does not cross executable boundaries. If a target depends on an
4399 executable, it's assumed that the executable is a tool that is producing part
4400 of the build rather than something that is linked and distributed. This
4401 allows assert_no_deps to express what is distributed in the final target
4402 rather than depend on the internal build steps (which may include
4403 non-distributable code).
4404
4405 See "gn help label_pattern" for the format of the entries in the list. These
4406 patterns allow blacklisting individual targets or whole directory
4407 hierarchies.
4408
4409 Sometimes it is desirable to enforce that many targets have no dependencies
4410 on a target or set of targets. One efficient way to express this is to create
4411 a group with the assert_no_deps rule on it, and make that group depend on all
4412 targets you want to apply that assertion to.
4413```
4414
4415#### **Example**
4416
4417```
4418 executable("doom_melon") {
4419 deps = [ "//foo:bar" ]
4420 ...
4421 assert_no_deps = [
4422 "//evil/*", # Don't link any code from the evil directory.
4423 "//foo:test_support", # This target is also disallowed.
4424 ]
4425 }
4426```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004427### **bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in
Brett Wilson796ed472018-07-16 15:11:09 -07004428```
4429 create_bundle.
4430
4431 A string corresponding to a path in $root_build_dir.
4432
4433 This string is used by the "create_bundle" target to expand the
4434 {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must
4435 correspond to a path under "bundle_root_dir".
4436
4437 See "gn help bundle_root_dir" for examples.
4438```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004439### **bundle_deps_filter**: [label list] A list of labels that are filtered out.
Brett Wilson796ed472018-07-16 15:11:09 -07004440
4441```
4442 A list of target labels.
4443
4444 This list contains target label patterns that should be filtered out when
4445 creating the bundle. Any target matching one of those label will be removed
4446 from the dependencies of the create_bundle target.
4447
4448 This is mostly useful when creating application extension bundle as the
4449 application extension has access to runtime resources from the application
4450 bundle and thus do not require a second copy.
4451
4452 See "gn help create_bundle" for more information.
4453```
4454
4455#### **Example**
4456
4457```
4458 create_bundle("today_extension") {
4459 deps = [
4460 "//base"
4461 ]
4462 bundle_root_dir = "$root_out_dir/today_extension.appex"
4463 bundle_deps_filter = [
4464 # The extension uses //base but does not use any function calling into
4465 # third_party/icu and thus does not need the icudtl.dat file.
4466 "//third_party/icu:icudata",
4467 ]
4468 }
4469```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004470### **bundle_executable_dir**
Brett Wilson796ed472018-07-16 15:11:09 -07004471
Joe Armstrongb199e542019-03-21 09:40:08 +08004472```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004473 bundle_executable_dir: Expansion of {{bundle_executable_dir}} in
4474 create_bundle.
4475
Brett Wilson796ed472018-07-16 15:11:09 -07004476 A string corresponding to a path in $root_build_dir.
4477
4478 This string is used by the "create_bundle" target to expand the
4479 {{bundle_executable_dir}} of the "bundle_data" target it depends on. This
4480 must correspond to a path under "bundle_root_dir".
4481
4482 See "gn help bundle_root_dir" for examples.
4483```
Joe Armstrongb199e542019-03-21 09:40:08 +08004484### **bundle_resources_dir**
4485
Brett Wilson796ed472018-07-16 15:11:09 -07004486```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004487 bundle_resources_dir: Expansion of {{bundle_resources_dir}} in
4488 create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07004489
4490 A string corresponding to a path in $root_build_dir.
4491
4492 This string is used by the "create_bundle" target to expand the
4493 {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must
4494 correspond to a path under "bundle_root_dir".
4495
4496 See "gn help bundle_root_dir" for examples.
4497```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004498### **bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07004499
4500```
4501 A string corresponding to a path in root_build_dir.
4502
4503 This string is used by the "create_bundle" target to expand the
4504 {{bundle_root_dir}} of the "bundle_data" target it depends on. This must
4505 correspond to a path under root_build_dir.
4506```
4507
4508#### **Example**
4509
4510```
4511 bundle_data("info_plist") {
4512 sources = [ "Info.plist" ]
4513 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
4514 }
4515
4516 create_bundle("doom_melon.app") {
4517 deps = [ ":info_plist" ]
4518 bundle_root_dir = "${root_build_dir}/doom_melon.app"
4519 bundle_contents_dir = "${bundle_root_dir}/Contents"
4520 bundle_resources_dir = "${bundle_contents_dir}/Resources"
4521 bundle_executable_dir = "${bundle_contents_dir}/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07004522 }
4523```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004524### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004525
4526```
4527 A list of strings.
4528
4529 "cflags" are passed to all invocations of the C, C++, Objective C, and
4530 Objective C++ compilers.
4531
4532 To target one of these variants individually, use "cflags_c", "cflags_cc",
4533 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4534 versions of cflags* will be appended on the compiler command line after
4535 "cflags".
4536
4537 See also "asmflags" for flags for assembly-language files.
4538```
4539
4540#### **Ordering of flags and values**
4541
4542```
4543 1. Those set on the current target (not in a config).
4544 2. Those set on the "configs" on the target in order that the
4545 configs appear in the list.
4546 3. Those set on the "all_dependent_configs" on the target in order
4547 that the configs appear in the list.
4548 4. Those set on the "public_configs" on the target in order that
4549 those configs appear in the list.
4550 5. all_dependent_configs pulled from dependencies, in the order of
4551 the "deps" list. This is done recursively. If a config appears
4552 more than once, only the first occurence will be used.
4553 6. public_configs pulled from dependencies, in the order of the
4554 "deps" list. If a dependency is public, they will be applied
4555 recursively.
4556```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004557### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004558
4559```
4560 A list of strings.
4561
4562 "cflags" are passed to all invocations of the C, C++, Objective C, and
4563 Objective C++ compilers.
4564
4565 To target one of these variants individually, use "cflags_c", "cflags_cc",
4566 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4567 versions of cflags* will be appended on the compiler command line after
4568 "cflags".
4569
4570 See also "asmflags" for flags for assembly-language files.
4571```
4572
4573#### **Ordering of flags and values**
4574
4575```
4576 1. Those set on the current target (not in a config).
4577 2. Those set on the "configs" on the target in order that the
4578 configs appear in the list.
4579 3. Those set on the "all_dependent_configs" on the target in order
4580 that the configs appear in the list.
4581 4. Those set on the "public_configs" on the target in order that
4582 those configs appear in the list.
4583 5. all_dependent_configs pulled from dependencies, in the order of
4584 the "deps" list. This is done recursively. If a config appears
4585 more than once, only the first occurence will be used.
4586 6. public_configs pulled from dependencies, in the order of the
4587 "deps" list. If a dependency is public, they will be applied
4588 recursively.
4589```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004590### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004591
4592```
4593 A list of strings.
4594
4595 "cflags" are passed to all invocations of the C, C++, Objective C, and
4596 Objective C++ compilers.
4597
4598 To target one of these variants individually, use "cflags_c", "cflags_cc",
4599 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4600 versions of cflags* will be appended on the compiler command line after
4601 "cflags".
4602
4603 See also "asmflags" for flags for assembly-language files.
4604```
4605
4606#### **Ordering of flags and values**
4607
4608```
4609 1. Those set on the current target (not in a config).
4610 2. Those set on the "configs" on the target in order that the
4611 configs appear in the list.
4612 3. Those set on the "all_dependent_configs" on the target in order
4613 that the configs appear in the list.
4614 4. Those set on the "public_configs" on the target in order that
4615 those configs appear in the list.
4616 5. all_dependent_configs pulled from dependencies, in the order of
4617 the "deps" list. This is done recursively. If a config appears
4618 more than once, only the first occurence will be used.
4619 6. public_configs pulled from dependencies, in the order of the
4620 "deps" list. If a dependency is public, they will be applied
4621 recursively.
4622```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004623### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004624
4625```
4626 A list of strings.
4627
4628 "cflags" are passed to all invocations of the C, C++, Objective C, and
4629 Objective C++ compilers.
4630
4631 To target one of these variants individually, use "cflags_c", "cflags_cc",
4632 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4633 versions of cflags* will be appended on the compiler command line after
4634 "cflags".
4635
4636 See also "asmflags" for flags for assembly-language files.
4637```
4638
4639#### **Ordering of flags and values**
4640
4641```
4642 1. Those set on the current target (not in a config).
4643 2. Those set on the "configs" on the target in order that the
4644 configs appear in the list.
4645 3. Those set on the "all_dependent_configs" on the target in order
4646 that the configs appear in the list.
4647 4. Those set on the "public_configs" on the target in order that
4648 those configs appear in the list.
4649 5. all_dependent_configs pulled from dependencies, in the order of
4650 the "deps" list. This is done recursively. If a config appears
4651 more than once, only the first occurence will be used.
4652 6. public_configs pulled from dependencies, in the order of the
4653 "deps" list. If a dependency is public, they will be applied
4654 recursively.
4655```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004656### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004657
4658```
4659 A list of strings.
4660
4661 "cflags" are passed to all invocations of the C, C++, Objective C, and
4662 Objective C++ compilers.
4663
4664 To target one of these variants individually, use "cflags_c", "cflags_cc",
4665 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4666 versions of cflags* will be appended on the compiler command line after
4667 "cflags".
4668
4669 See also "asmflags" for flags for assembly-language files.
4670```
4671
4672#### **Ordering of flags and values**
4673
4674```
4675 1. Those set on the current target (not in a config).
4676 2. Those set on the "configs" on the target in order that the
4677 configs appear in the list.
4678 3. Those set on the "all_dependent_configs" on the target in order
4679 that the configs appear in the list.
4680 4. Those set on the "public_configs" on the target in order that
4681 those configs appear in the list.
4682 5. all_dependent_configs pulled from dependencies, in the order of
4683 the "deps" list. This is done recursively. If a config appears
4684 more than once, only the first occurence will be used.
4685 6. public_configs pulled from dependencies, in the order of the
4686 "deps" list. If a dependency is public, they will be applied
4687 recursively.
4688```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004689### **check_includes**: [boolean] Controls whether a target's files are checked.
Brett Wilson796ed472018-07-16 15:11:09 -07004690
4691```
4692 When true (the default), the "gn check" command (as well as "gn gen" with the
4693 --check flag) will check this target's sources and headers for proper
4694 dependencies.
4695
4696 When false, the files in this target will be skipped by default. This does
4697 not affect other targets that depend on the current target, it just skips
4698 checking the includes of the current target's files.
4699
4700 If there are a few conditionally included headers that trip up checking, you
4701 can exclude headers individually by annotating them with "nogncheck" (see "gn
4702 help nogncheck").
4703
4704 The topic "gn help check" has general information on how checking works and
4705 advice on how to pass a check in problematic cases.
4706```
4707
4708#### **Example**
4709
4710```
4711 source_set("busted_includes") {
4712 # This target's includes are messed up, exclude it from checking.
4713 check_includes = false
4714 ...
4715 }
4716```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004717### **code_signing_args**: [string list] Arguments passed to code signing script.
Brett Wilson796ed472018-07-16 15:11:09 -07004718
4719```
4720 For create_bundle targets, code_signing_args is the list of arguments to pass
4721 to the code signing script. Typically you would use source expansion (see "gn
4722 help source_expansion") to insert the source file names.
4723
4724 See also "gn help create_bundle".
4725```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004726### **code_signing_outputs**: [file list] Output files for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07004727
4728```
4729 Outputs from the code signing step of a create_bundle target. Must refer to
4730 files in the build directory.
4731
4732 See also "gn help create_bundle".
4733```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004734### **code_signing_script**: [file name] Script for code signing."
Brett Wilson796ed472018-07-16 15:11:09 -07004735
4736```
4737 An absolute or buildfile-relative file name of a Python script to run for a
4738 create_bundle target to perform code signing step.
4739
4740 See also "gn help create_bundle".
4741```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004742### **code_signing_sources**: [file list] Sources for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07004743
4744```
4745 A list of files used as input for code signing script step of a create_bundle
4746 target. Non-absolute paths will be resolved relative to the current build
4747 file.
4748
4749 See also "gn help create_bundle".
4750```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004751### **complete_static_lib**: [boolean] Links all deps into a static library.
Brett Wilson796ed472018-07-16 15:11:09 -07004752
4753```
4754 A static library normally doesn't include code from dependencies, but instead
4755 forwards the static libraries and source sets in its deps up the dependency
4756 chain until a linkable target (an executable or shared library) is reached.
4757 The final linkable target only links each static library once, even if it
4758 appears more than once in its dependency graph.
4759
4760 In some cases the static library might be the final desired output. For
4761 example, you may be producing a static library for distribution to third
4762 parties. In this case, the static library should include code for all
4763 dependencies in one complete package. However, complete static libraries
4764 themselves are never linked into other complete static libraries. All
4765 complete static libraries are for distribution and linking them in would
4766 cause code duplication in this case. If the static library is not for
4767 distribution, it should not be complete.
4768
4769 GN treats non-complete static libraries as source sets when they are linked
4770 into complete static libraries. This is done because some tools like AR do
4771 not handle dependent static libraries properly. This makes it easier to write
4772 "alink" rules.
4773
4774 In rare cases it makes sense to list a header in more than one target if it
4775 could be considered conceptually a member of both. libraries.
4776```
4777
4778#### **Example**
4779
4780```
4781 static_library("foo") {
4782 complete_static_lib = true
4783 deps = [ "bar" ]
4784 }
4785```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004786### **configs**: Configs applying to this target or config.
Brett Wilson796ed472018-07-16 15:11:09 -07004787
4788```
4789 A list of config labels.
4790```
4791
4792#### **Configs on a target**
4793
4794```
4795 When used on a target, the include_dirs, defines, etc. in each config are
4796 appended in the order they appear to the compile command for each file in the
4797 target. They will appear after the include_dirs, defines, etc. that the
4798 target sets directly.
4799
4800 Since configs apply after the values set on a target, directly setting a
4801 compiler flag will prepend it to the command line. If you want to append a
4802 flag instead, you can put that flag in a one-off config and append that
4803 config to the target's configs list.
4804
4805 The build configuration script will generally set up the default configs
4806 applying to a given target type (see "set_defaults"). When a target is being
4807 defined, it can add to or remove from this list.
4808```
4809
4810#### **Configs on a config**
4811
4812```
4813 It is possible to create composite configs by specifying configs on a config.
4814 One might do this to forward values, or to factor out blocks of settings from
4815 very large configs into more manageable named chunks.
4816
4817 In this case, the composite config is expanded to be the concatenation of its
4818 own values, and in order, the values from its sub-configs *before* anything
4819 else happens. This has some ramifications:
4820
4821 - A target has no visibility into a config's sub-configs. Target code only
4822 sees the name of the composite config. It can't remove sub-configs or opt
4823 in to only parts of it. The composite config may not even be defined
4824 before the target is.
4825
4826 - You can get duplication of values if a config is listed twice, say, on a
4827 target and in a sub-config that also applies. In other cases, the configs
4828 applying to a target are de-duped. It's expected that if a config is
4829 listed as a sub-config that it is only used in that context. (Note that
4830 it's possible to fix this and de-dupe, but it's not normally relevant and
4831 complicates the implementation.)
4832```
4833
4834#### **Ordering of flags and values**
4835
4836```
4837 1. Those set on the current target (not in a config).
4838 2. Those set on the "configs" on the target in order that the
4839 configs appear in the list.
4840 3. Those set on the "all_dependent_configs" on the target in order
4841 that the configs appear in the list.
4842 4. Those set on the "public_configs" on the target in order that
4843 those configs appear in the list.
4844 5. all_dependent_configs pulled from dependencies, in the order of
4845 the "deps" list. This is done recursively. If a config appears
4846 more than once, only the first occurence will be used.
4847 6. public_configs pulled from dependencies, in the order of the
4848 "deps" list. If a dependency is public, they will be applied
4849 recursively.
4850```
4851
4852#### **Example**
4853
4854```
4855 # Configs on a target.
4856 source_set("foo") {
4857 # Don't use the default RTTI config that BUILDCONFIG applied to us.
4858 configs -= [ "//build:no_rtti" ]
4859
4860 # Add some of our own settings.
4861 configs += [ ":mysettings" ]
4862 }
4863
4864 # Create a default_optimization config that forwards to one of a set of more
4865 # specialized configs depending on build flags. This pattern is useful
4866 # because it allows a target to opt in to either a default set, or a more
4867 # specific set, while avoid duplicating the settings in two places.
4868 config("super_optimization") {
4869 cflags = [ ... ]
4870 }
4871 config("default_optimization") {
4872 if (optimize_everything) {
4873 configs = [ ":super_optimization" ]
4874 } else {
4875 configs = [ ":no_optimization" ]
4876 }
4877 }
4878```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004879### **contents**: Contents to write to file.
Nate Fischer8ed01d32019-01-08 17:32:01 -08004880
4881```
4882 The contents of the file for a generated_file target.
4883 See "gn help generated_file".
4884```
Julie Hockettce1fa072019-05-07 17:44:37 -07004885### **crate_name**: [string] The name for the compiled crate.
4886
4887```
4888 Valid for `rust_library` targets and `executable`, `static_library`,
4889 `shared_library`, and `source_set` targets that contain Rust sources.
4890
4891 If crate_name is not set, then this rule will use the target name.
4892```
4893### **crate_root**: [string] The root source file for a binary or library.
4894
4895```
4896 Valid for `rust_library` targets and `executable`, `static_library`,
4897 `shared_library`, and `source_set` targets that contain Rust sources.
4898
4899 This file is usually the `main.rs` or `lib.rs` for binaries and libraries,
4900 respectively.
4901
4902 If crate_root is not set, then this rule will look for a lib.rs file (or
4903 main.rs for executable) or a single file in sources, if sources contains
4904 only one file.
4905```
4906### **crate_type**: [string] The type of linkage to use on a shared_library.
4907
4908```
4909 Valid for `rust_library` targets and `executable`, `static_library`,
4910 `shared_library`, and `source_set` targets that contain Rust sources.
4911
4912 Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib".
4913 This field sets the `crate-type` attribute for the `rustc` tool on static
4914 libraries, as well as the appropiate output extension in the
4915 `rust_output_extension` attribute. Since outputs must be explicit, the `lib`
4916 crate type (where the Rust compiler produces what it thinks is the
4917 appropriate library type) is not supported.
4918
4919 It should be noted that the "dylib" crate type in Rust is unstable in the set
4920 of symbols it exposes, and most usages today are potentially wrong and will
4921 be broken in the future.
4922
4923 Static libraries, rust libraries, and executables have this field set
4924 automatically.
4925```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004926### **data**: Runtime data file dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07004927
4928```
4929 Lists files or directories required to run the given target. These are
4930 typically data files or directories of data files. The paths are interpreted
4931 as being relative to the current build file. Since these are runtime
4932 dependencies, they do not affect which targets are built or when. To declare
4933 input files to a script, use "inputs".
4934
4935 Appearing in the "data" section does not imply any special handling such as
4936 copying them to the output directory. This is just used for declaring runtime
4937 dependencies. Runtime dependencies can be queried using the "runtime_deps"
4938 category of "gn desc" or written during build generation via
4939 "--runtime-deps-list-file".
4940
4941 GN doesn't require data files to exist at build-time. So actions that produce
4942 files that are in turn runtime dependencies can list those generated files
4943 both in the "outputs" list as well as the "data" list.
4944
4945 By convention, directories are listed with a trailing slash:
4946 data = [ "test/data/" ]
4947 However, no verification is done on these so GN doesn't enforce this. The
4948 paths are just rebased and passed along when requested.
4949
4950 Note: On iOS and macOS, create_bundle targets will not be recursed into when
4951 gathering data. See "gn help create_bundle" for details.
4952
4953 See "gn help runtime_deps" for how these are used.
4954```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004955### **data_deps**: Non-linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07004956
4957```
4958 A list of target labels.
4959
4960 Specifies dependencies of a target that are not actually linked into the
4961 current target. Such dependencies will be built and will be available at
4962 runtime.
4963
4964 This is normally used for things like plugins or helper programs that a
4965 target needs at runtime.
4966
4967 Note: On iOS and macOS, create_bundle targets will not be recursed into when
4968 gathering data_deps. See "gn help create_bundle" for details.
4969
4970 See also "gn help deps" and "gn help data".
4971```
4972
4973#### **Example**
4974
4975```
4976 executable("foo") {
4977 deps = [ "//base" ]
4978 data_deps = [ "//plugins:my_runtime_plugin" ]
4979 }
4980```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004981### **data_keys**: Keys from which to collect metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08004982
4983```
4984 These keys are used to identify metadata to collect. If a walked target
4985 defines this key in its metadata, its value will be appended to the resulting
4986 collection.
4987
4988 See "gn help generated_file".
4989```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004990### **defines**: C preprocessor defines.
Brett Wilson796ed472018-07-16 15:11:09 -07004991
4992```
4993 A list of strings
4994
4995 These strings will be passed to the C/C++ compiler as #defines. The strings
4996 may or may not include an "=" to assign a value.
4997```
4998
4999#### **Ordering of flags and values**
5000
5001```
5002 1. Those set on the current target (not in a config).
5003 2. Those set on the "configs" on the target in order that the
5004 configs appear in the list.
5005 3. Those set on the "all_dependent_configs" on the target in order
5006 that the configs appear in the list.
5007 4. Those set on the "public_configs" on the target in order that
5008 those configs appear in the list.
5009 5. all_dependent_configs pulled from dependencies, in the order of
5010 the "deps" list. This is done recursively. If a config appears
5011 more than once, only the first occurence will be used.
5012 6. public_configs pulled from dependencies, in the order of the
5013 "deps" list. If a dependency is public, they will be applied
5014 recursively.
5015```
5016
5017#### **Example**
5018
5019```
5020 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
5021```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005022### **depfile**: [string] File name for input dependencies for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07005023
5024```
5025 If nonempty, this string specifies that the current action or action_foreach
5026 target will generate the given ".d" file containing the dependencies of the
5027 input. Empty or unset means that the script doesn't generate the files.
5028
5029 A depfile should be used only when a target depends on files that are not
5030 already specified by a target's inputs and sources. Likewise, depfiles should
5031 specify only those dependencies not already included in sources or inputs.
5032
5033 The .d file should go in the target output directory. If you have more than
5034 one source file that the script is being run over, you can use the output
5035 file expansions described in "gn help action_foreach" to name the .d file
5036 according to the input."
5037
5038 The format is that of a Makefile and all paths must be relative to the root
5039 build directory. Only one output may be listed and it must match the first
5040 output of the action.
5041
5042 Although depfiles are created by an action, they should not be listed in the
5043 action's "outputs" unless another target will use the file as an input.
5044```
5045
5046#### **Example**
5047
5048```
5049 action_foreach("myscript_target") {
5050 script = "myscript.py"
5051 sources = [ ... ]
5052
5053 # Locate the depfile in the output directory named like the
5054 # inputs but with a ".d" appended.
5055 depfile = "$relative_target_output_dir/{{source_name}}.d"
5056
5057 # Say our script uses "-o " to indicate the depfile.
5058 args = [ "{{source}}", "-o", depfile ]
5059 }
5060```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005061### **deps**: Private linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005062
5063```
5064 A list of target labels.
5065
5066 Specifies private dependencies of a target. Private dependencies are
5067 propagated up the dependency tree and linked to dependent targets, but do not
5068 grant the ability to include headers from the dependency. Public configs are
5069 not forwarded.
5070```
5071
5072#### **Details of dependency propagation**
5073
5074```
5075 Source sets, shared libraries, and non-complete static libraries will be
5076 propagated up the dependency tree across groups, non-complete static
5077 libraries and source sets.
5078
5079 Executables, shared libraries, and complete static libraries will link all
5080 propagated targets and stop propagation. Actions and copy steps also stop
5081 propagation, allowing them to take a library as an input but not force
5082 dependents to link to it.
5083
5084 Propagation of all_dependent_configs and public_configs happens independently
5085 of target type. all_dependent_configs are always propagated across all types
5086 of targets, and public_configs are always propagated across public deps of
5087 all types of targets.
5088
5089 Data dependencies are propagated differently. See "gn help data_deps" and
5090 "gn help runtime_deps".
5091
5092 See also "public_deps".
5093```
Julie Hockettce1fa072019-05-07 17:44:37 -07005094### **edition**: [string] The rustc edition to use in compiliation.
5095
5096```
5097 Valid for `rust_library` targets and `executable`, `static_library`,
5098 `shared_library`, and `source_set` targets that contain Rust sources.
5099
5100 This indicates the compiler edition to use in compilition. Should be a value
5101 like "2015" or "2018", indiicating the appropriate value to pass to the
5102 `--edition=<>` flag in rustc.
5103```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005104### **friend**: Allow targets to include private headers.
Brett Wilson796ed472018-07-16 15:11:09 -07005105
5106```
5107 A list of label patterns (see "gn help label_pattern") that allow dependent
5108 targets to include private headers. Applies to all binary targets.
5109
5110 Normally if a target lists headers in the "public" list (see "gn help
5111 public"), other headers are implicitly marked as private. Private headers
5112 can not be included by other targets, even with a public dependency path.
5113 The "gn check" function performs this validation.
5114
5115 A friend declaration allows one or more targets to include private headers.
5116 This is useful for things like unit tests that are closely associated with a
5117 target and require internal knowledge without opening up all headers to be
5118 included by all dependents.
5119
5120 A friend target does not allow that target to include headers when no
5121 dependency exists. A public dependency path must still exist between two
5122 targets to include any headers from a destination target. The friend
5123 annotation merely allows the use of headers that would otherwise be
5124 prohibited because they are private.
5125
5126 The friend annotation is matched only against the target containing the file
5127 with the include directive. Friend annotations are not propagated across
5128 public or private dependencies. Friend annotations do not affect visibility.
5129```
5130
5131#### **Example**
5132
5133```
5134 static_library("lib") {
5135 # This target can include our private headers.
5136 friend = [ ":unit_tests" ]
5137
5138 public = [
5139 "public_api.h", # Normal public API for dependent targets.
5140 ]
5141
5142 # Private API and sources.
5143 sources = [
5144 "a_source_file.cc",
5145
5146 # Normal targets that depend on this one won't be able to include this
5147 # because this target defines a list of "public" headers. Without the
5148 # "public" list, all headers are implicitly public.
5149 "private_api.h",
5150 ]
5151 }
5152
5153 executable("unit_tests") {
5154 sources = [
5155 # This can include "private_api.h" from the :lib target because it
5156 # depends on that target and because of the friend annotation.
5157 "my_test.cc",
5158 ]
5159
5160 deps = [
5161 ":lib", # Required for the include to be allowed.
5162 ]
5163 }
5164```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005165### **include_dirs**: Additional include directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005166
5167```
5168 A list of source directories.
5169
5170 The directories in this list will be added to the include path for the files
5171 in the affected target.
5172```
5173
5174#### **Ordering of flags and values**
5175
5176```
5177 1. Those set on the current target (not in a config).
5178 2. Those set on the "configs" on the target in order that the
5179 configs appear in the list.
5180 3. Those set on the "all_dependent_configs" on the target in order
5181 that the configs appear in the list.
5182 4. Those set on the "public_configs" on the target in order that
5183 those configs appear in the list.
5184 5. all_dependent_configs pulled from dependencies, in the order of
5185 the "deps" list. This is done recursively. If a config appears
5186 more than once, only the first occurence will be used.
5187 6. public_configs pulled from dependencies, in the order of the
5188 "deps" list. If a dependency is public, they will be applied
5189 recursively.
5190```
5191
5192#### **Example**
5193
5194```
5195 include_dirs = [ "src/include", "//third_party/foo" ]
5196```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005197### **inputs**: Additional compile-time dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005198
5199```
5200 Inputs are compile-time dependencies of the current target. This means that
5201 all inputs must be available before compiling any of the sources or executing
5202 any actions.
5203
5204 Inputs are typically only used for action and action_foreach targets.
5205```
5206
5207#### **Inputs for actions**
5208
5209```
5210 For action and action_foreach targets, inputs should be the inputs to script
5211 that don't vary. These should be all .py files that the script uses via
5212 imports (the main script itself will be an implicit dependency of the action
5213 so need not be listed).
5214
5215 For action targets, inputs and sources are treated the same, but from a style
5216 perspective, it's recommended to follow the same rule as action_foreach and
5217 put helper files in the inputs, and the data used by the script (if any) in
5218 sources.
5219
5220 Note that another way to declare input dependencies from an action is to have
5221 the action write a depfile (see "gn help depfile"). This allows the script to
5222 dynamically write input dependencies, that might not be known until actually
5223 executing the script. This is more efficient than doing processing while
5224 running GN to determine the inputs, and is easier to keep in-sync than
5225 hardcoding the list.
5226```
5227
5228#### **Script input gotchas**
5229
5230```
5231 It may be tempting to write a script that enumerates all files in a directory
5232 as inputs. Don't do this! Even if you specify all the files in the inputs or
5233 sources in the GN target (or worse, enumerate the files in an exec_script
5234 call when running GN, which will be slow), the dependencies will be broken.
5235
5236 The problem happens if a file is ever removed because the inputs are not
5237 listed on the command line to the script. Because the script hasn't changed
5238 and all inputs are up to date, the script will not re-run and you will get a
5239 stale build. Instead, either list all inputs on the command line to the
5240 script, or if there are many, create a separate list file that the script
5241 reads. As long as this file is listed in the inputs, the build will detect
5242 when it has changed in any way and the action will re-run.
5243```
5244
5245#### **Inputs for binary targets**
5246
5247```
Julie Hockette2a29402018-07-31 10:11:42 -07005248 Any input dependencies will be resolved before compiling any sources or
5249 linking the target. Normally, all actions that a target depends on will be run
5250 before any files in a target are compiled. So if you depend on generated
5251 headers, you do not typically need to list them in the inputs section.
Brett Wilson796ed472018-07-16 15:11:09 -07005252
5253 Inputs for binary targets will be treated as implicit dependencies, meaning
5254 that changes in any of the inputs will force all sources in the target to be
5255 recompiled. If an input only applies to a subset of source files, you may
5256 want to split those into a separate target to avoid unnecessary recompiles.
5257```
5258
5259#### **Example**
5260
5261```
5262 action("myscript") {
5263 script = "domything.py"
5264 inputs = [ "input.data" ]
5265 }
5266```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005267### **ldflags**: Flags passed to the linker.
Brett Wilson796ed472018-07-16 15:11:09 -07005268
5269```
5270 A list of strings.
5271
5272 These flags are passed on the command-line to the linker and generally
5273 specify various linking options. Most targets will not need these and will
5274 use "libs" and "lib_dirs" instead.
5275
5276 ldflags are NOT pushed to dependents, so applying ldflags to source sets or
5277 static libraries will be a no-op. If you want to apply ldflags to dependent
5278 targets, put them in a config and set it in the all_dependent_configs or
5279 public_configs.
5280```
5281
5282#### **Ordering of flags and values**
5283
5284```
5285 1. Those set on the current target (not in a config).
5286 2. Those set on the "configs" on the target in order that the
5287 configs appear in the list.
5288 3. Those set on the "all_dependent_configs" on the target in order
5289 that the configs appear in the list.
5290 4. Those set on the "public_configs" on the target in order that
5291 those configs appear in the list.
5292 5. all_dependent_configs pulled from dependencies, in the order of
5293 the "deps" list. This is done recursively. If a config appears
5294 more than once, only the first occurence will be used.
5295 6. public_configs pulled from dependencies, in the order of the
5296 "deps" list. If a dependency is public, they will be applied
5297 recursively.
5298```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005299### **lib_dirs**: Additional library directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005300
5301```
5302 A list of directories.
5303
5304 Specifies additional directories passed to the linker for searching for the
5305 required libraries. If an item is not an absolute path, it will be treated as
5306 being relative to the current build file.
5307
5308 libs and lib_dirs work differently than other flags in two respects.
5309 First, then are inherited across static library boundaries until a
5310 shared library or executable target is reached. Second, they are
5311 uniquified so each one is only passed once (the first instance of it
5312 will be the one used).
5313```
5314
5315#### **Ordering of flags and values**
5316
5317```
5318 1. Those set on the current target (not in a config).
5319 2. Those set on the "configs" on the target in order that the
5320 configs appear in the list.
5321 3. Those set on the "all_dependent_configs" on the target in order
5322 that the configs appear in the list.
5323 4. Those set on the "public_configs" on the target in order that
5324 those configs appear in the list.
5325 5. all_dependent_configs pulled from dependencies, in the order of
5326 the "deps" list. This is done recursively. If a config appears
5327 more than once, only the first occurence will be used.
5328 6. public_configs pulled from dependencies, in the order of the
5329 "deps" list. If a dependency is public, they will be applied
5330 recursively.
5331
5332 For "libs" and "lib_dirs" only, the values propagated from
5333 dependencies (as described above) are applied last assuming they
5334 are not already in the list.
5335```
5336
5337#### **Example**
5338
5339```
5340 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
5341```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005342### **libs**: Additional libraries to link.
Brett Wilson796ed472018-07-16 15:11:09 -07005343
5344```
5345 A list of library names or library paths.
5346
5347 These libraries will be linked into the final binary (executable or shared
5348 library) containing the current target.
5349
5350 libs and lib_dirs work differently than other flags in two respects.
5351 First, then are inherited across static library boundaries until a
5352 shared library or executable target is reached. Second, they are
5353 uniquified so each one is only passed once (the first instance of it
5354 will be the one used).
5355```
5356
5357#### **Types of libs**
5358
5359```
5360 There are several different things that can be expressed in libs:
5361
5362 File paths
5363 Values containing '/' will be treated as references to files in the
5364 checkout. They will be rebased to be relative to the build directory and
5365 specified in the "libs" for linker tools. This facility should be used
5366 for libraries that are checked in to the version control. For libraries
5367 that are generated by the build, use normal GN deps to link them.
5368
5369 System libraries
5370 Values not containing '/' will be treated as system library names. These
5371 will be passed unmodified to the linker and prefixed with the
5372 "lib_switch" attribute of the linker tool. Generally you would set the
5373 "lib_dirs" so the given library is found. Your BUILD.gn file should not
5374 specify the switch (like "-l"): this will be encoded in the "lib_switch"
5375 of the tool.
5376
5377 Apple frameworks
5378 System libraries ending in ".framework" will be special-cased: the switch
5379 "-framework" will be prepended instead of the lib_switch, and the
5380 ".framework" suffix will be trimmed. This is to support the way Mac links
5381 framework dependencies.
5382```
5383
5384#### **Ordering of flags and values**
5385
5386```
5387 1. Those set on the current target (not in a config).
5388 2. Those set on the "configs" on the target in order that the
5389 configs appear in the list.
5390 3. Those set on the "all_dependent_configs" on the target in order
5391 that the configs appear in the list.
5392 4. Those set on the "public_configs" on the target in order that
5393 those configs appear in the list.
5394 5. all_dependent_configs pulled from dependencies, in the order of
5395 the "deps" list. This is done recursively. If a config appears
5396 more than once, only the first occurence will be used.
5397 6. public_configs pulled from dependencies, in the order of the
5398 "deps" list. If a dependency is public, they will be applied
5399 recursively.
5400
5401 For "libs" and "lib_dirs" only, the values propagated from
5402 dependencies (as described above) are applied last assuming they
5403 are not already in the list.
5404```
5405
5406#### **Examples**
5407
5408```
5409 On Windows:
5410 libs = [ "ctl3d.lib" ]
5411
5412 On Linux:
5413 libs = [ "ld" ]
5414```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005415### **metadata**: Metadata of this target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005416
5417```
5418 Metadata is a collection of keys and values relating to a particular target.
Julie Hockettd69a9c32019-01-23 14:36:18 -08005419 Values must be lists, allowing for sane and predictable collection behavior.
5420 Generally, these keys will include three types of lists: lists of ordinary
5421 strings, lists of filenames intended to be rebased according to their
5422 particular source directory, and lists of target labels intended to be used
5423 as barriers to the walk. Verfication of these categories occurs at walk time,
5424 not creation time (since it is not clear until the walk which values are
5425 intended for which purpose).
5426```
Nate Fischer8ed01d32019-01-08 17:32:01 -08005427
Julie Hockettd69a9c32019-01-23 14:36:18 -08005428#### **Example**
Nate Fischer8ed01d32019-01-08 17:32:01 -08005429
Julie Hockettd69a9c32019-01-23 14:36:18 -08005430```
Nate Fischer8ed01d32019-01-08 17:32:01 -08005431 group("doom_melon") {
5432 metadata = {
5433 # These keys are not built in to GN but are interpreted when consuming
5434 # metadata.
5435 my_barrier = []
5436 my_files = [ "a.txt", "b.txt" ]
5437 }
5438 }
5439```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005440### **"output_conversion**: Data format for generated_file targets.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005441
5442```
5443 Controls how the "contents" of a generated_file target is formatted.
Julie Hockett81ee1962019-05-13 11:24:42 -07005444 See "gn help io_conversion".
Nate Fischer8ed01d32019-01-08 17:32:01 -08005445```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005446### **output_dir**: [directory] Directory to put output file in.
Brett Wilson796ed472018-07-16 15:11:09 -07005447
5448```
5449 For library and executable targets, overrides the directory for the final
5450 output. This must be in the root_build_dir or a child thereof.
5451
5452 This should generally be in the root_out_dir or a subdirectory thereof (the
5453 root_out_dir will be the same as the root_build_dir for the default
5454 toolchain, and will be a subdirectory for other toolchains). Not putting the
5455 output in a subdirectory of root_out_dir can result in collisions between
5456 different toolchains, so you will need to take steps to ensure that your
5457 target is only present in one toolchain.
5458
5459 Normally the toolchain specifies the output directory for libraries and
5460 executables (see "gn help tool"). You will have to consult that for the
5461 default location. The default location will be used if output_dir is
5462 undefined or empty.
5463```
5464
5465#### **Example**
5466
5467```
5468 shared_library("doom_melon") {
5469 output_dir = "$root_out_dir/plugin_libs"
5470 ...
5471 }
5472```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005473### **output_extension**: Value to use for the output's file extension.
Brett Wilson796ed472018-07-16 15:11:09 -07005474
5475```
5476 Normally the file extension for a target is based on the target type and the
5477 operating system, but in rare cases you will need to override the name (for
5478 example to use "libfreetype.so.6" instead of libfreetype.so on Linux).
5479
5480 This value should not include a leading dot. If undefined, the default
5481 specified on the tool will be used. If set to the empty string, no output
5482 extension will be used.
5483
5484 The output_extension will be used to set the "{{output_extension}}" expansion
5485 which the linker tool will generally use to specify the output file name. See
5486 "gn help tool".
5487```
5488
5489#### **Example**
5490
5491```
5492 shared_library("freetype") {
5493 if (is_linux) {
5494 # Call the output "libfreetype.so.6"
5495 output_extension = "so.6"
5496 }
5497 ...
5498 }
5499
5500 # On Windows, generate a "mysettings.cpl" control panel applet. Control panel
5501 # applets are actually special shared libraries.
5502 if (is_win) {
5503 shared_library("mysettings") {
5504 output_extension = "cpl"
5505 ...
5506 }
5507 }
5508```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005509### **output_name**: Define a name for the output file other than the default.
Brett Wilson796ed472018-07-16 15:11:09 -07005510
5511```
5512 Normally the output name of a target will be based on the target name, so the
5513 target "//foo/bar:bar_unittests" will generate an output file such as
5514 "bar_unittests.exe" (using Windows as an example).
5515
5516 Sometimes you will want an alternate name to avoid collisions or if the
5517 internal name isn't appropriate for public distribution.
5518
5519 The output name should have no extension or prefixes, these will be added
5520 using the default system rules. For example, on Linux an output name of "foo"
5521 will produce a shared library "libfoo.so". There is no way to override the
5522 output prefix of a linker tool on a per- target basis. If you need more
5523 flexibility, create a copy target to produce the file you want.
5524
5525 This variable is valid for all binary output target types.
5526```
5527
5528#### **Example**
5529
5530```
5531 static_library("doom_melon") {
5532 output_name = "fluffy_bunny"
5533 }
5534```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005535### **output_prefix_override**: Don't use prefix for output name.
Brett Wilson796ed472018-07-16 15:11:09 -07005536
5537```
5538 A boolean that overrides the output prefix for a target. Defaults to false.
5539
5540 Some systems use prefixes for the names of the final target output file. The
5541 normal example is "libfoo.so" on Linux for a target named "foo".
5542
5543 The output prefix for a given target type is specified on the linker tool
5544 (see "gn help tool"). Sometimes this prefix is undesired.
5545
5546 See also "gn help output_extension".
5547```
5548
5549#### **Example**
5550
5551```
5552 shared_library("doom_melon") {
5553 # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag
5554 # will produce "doom_melon.so".
5555 output_prefix_override = true
5556 ...
5557 }
5558```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005559### **outputs**: Output files for actions and copy targets.
Brett Wilson796ed472018-07-16 15:11:09 -07005560
5561```
5562 Outputs is valid for "copy", "action", and "action_foreach" target types and
5563 indicates the resulting files. Outputs must always refer to files in the
5564 build directory.
5565
5566 copy
5567 Copy targets should have exactly one entry in the outputs list. If there is
5568 exactly one source, this can be a literal file name or a source expansion.
5569 If there is more than one source, this must contain a source expansion to
5570 map a single input name to a single output name. See "gn help copy".
5571
5572 action_foreach
5573 Action_foreach targets must always use source expansions to map input files
5574 to output files. There can be more than one output, which means that each
5575 invocation of the script will produce a set of files (presumably based on
5576 the name of the input file). See "gn help action_foreach".
5577
5578 action
5579 Action targets (excluding action_foreach) must list literal output file(s)
5580 with no source expansions. See "gn help action".
5581```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005582### **partial_info_plist**: [filename] Path plist from asset catalog compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005583
5584```
5585 Valid for create_bundle target, corresponds to the path for the partial
5586 Info.plist created by the asset catalog compiler that needs to be merged
5587 with the application Info.plist (usually done by the code signing script).
5588
5589 The file will be generated regardless of whether the asset compiler has
5590 been invoked or not. See "gn help create_bundle".
5591```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005592### **pool**: Label of the pool used by the action.
Brett Wilson796ed472018-07-16 15:11:09 -07005593
5594```
5595 A fully-qualified label representing the pool that will be used for the
5596 action. Pools are defined using the pool() {...} declaration.
5597```
5598
5599#### **Example**
5600
5601```
5602 action("action") {
5603 pool = "//build:custom_pool"
5604 ...
5605 }
5606```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005607### **precompiled_header**: [string] Header file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07005608
5609```
5610 Precompiled headers will be used when a target specifies this value, or a
5611 config applying to this target specifies this value. In addition, the tool
5612 corresponding to the source files must also specify precompiled headers (see
5613 "gn help tool"). The tool will also specify what type of precompiled headers
5614 to use, by setting precompiled_header_type to either "gcc" or "msvc".
5615
5616 The precompiled header/source variables can be specified on a target or a
5617 config, but must be the same for all configs applying to a given target since
5618 a target can only have one precompiled header.
5619
5620 If you use both C and C++ sources, the precompiled header and source file
5621 will be compiled once per language. You will want to make sure to wrap C++
5622 includes in __cplusplus #ifdefs so the file will compile in C mode.
5623```
5624
5625#### **GCC precompiled headers**
5626
5627```
5628 When using GCC-style precompiled headers, "precompiled_source" contains the
5629 path of a .h file that is precompiled and then included by all source files
5630 in targets that set "precompiled_source".
5631
5632 The value of "precompiled_header" is not used with GCC-style precompiled
5633 headers.
5634```
5635
5636#### **MSVC precompiled headers**
5637
5638```
5639 When using MSVC-style precompiled headers, the "precompiled_header" value is
5640 a string corresponding to the header. This is NOT a path to a file that GN
5641 recognises, but rather the exact string that appears in quotes after
5642 an #include line in source code. The compiler will match this string against
5643 includes or forced includes (/FI).
5644
5645 MSVC also requires a source file to compile the header with. This must be
5646 specified by the "precompiled_source" value. In contrast to the header value,
5647 this IS a GN-style file name, and tells GN which source file to compile to
5648 make the .pch file used for subsequent compiles.
5649
5650 For example, if the toolchain specifies MSVC headers:
5651
5652 toolchain("vc_x64") {
5653 ...
5654 tool("cxx") {
5655 precompiled_header_type = "msvc"
5656 ...
5657
5658 You might make a config like this:
5659
5660 config("use_precompiled_headers") {
5661 precompiled_header = "build/precompile.h"
5662 precompiled_source = "//build/precompile.cc"
5663
5664 # Either your source files should #include "build/precompile.h"
5665 # first, or you can do this to force-include the header.
5666 cflags = [ "/FI$precompiled_header" ]
5667 }
5668
5669 And then define a target that uses the config:
5670
5671 executable("doom_melon") {
5672 configs += [ ":use_precompiled_headers" ]
5673 ...
5674```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005675### **precompiled_header_type**: [string] "gcc" or "msvc".
Brett Wilson796ed472018-07-16 15:11:09 -07005676
5677```
5678 See "gn help precompiled_header".
5679```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005680### **precompiled_source**: [file name] Source file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07005681
5682```
5683 The source file that goes along with the precompiled_header when using
5684 "msvc"-style precompiled headers. It will be implicitly added to the sources
5685 of the target. See "gn help precompiled_header".
5686```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005687### **product_type**: Product type for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07005688
5689```
5690 Correspond to the type of the product of a create_bundle target. Only
5691 meaningful to Xcode (used as part of the Xcode project generation).
5692
5693 When generating Xcode project files, only create_bundle target with a
5694 non-empty product_type will have a corresponding target in Xcode project.
5695```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005696### **public**: Declare public header files for a target.
Brett Wilson796ed472018-07-16 15:11:09 -07005697
5698```
5699 A list of files that other targets can include. These permissions are checked
5700 via the "check" command (see "gn help check").
5701
5702 If no public files are declared, other targets (assuming they have visibility
5703 to depend on this target) can include any file in the sources list. If this
5704 variable is defined on a target, dependent targets may only include files on
5705 this whitelist unless that target is marked as a friend (see "gn help
5706 friend").
5707
5708 Header file permissions are also subject to visibility. A target must be
5709 visible to another target to include any files from it at all and the public
5710 headers indicate which subset of those files are permitted. See "gn help
5711 visibility" for more.
5712
5713 Public files are inherited through the dependency tree. So if there is a
5714 dependency A -> B -> C, then A can include C's public headers. However, the
5715 same is NOT true of visibility, so unless A is in C's visibility list, the
5716 include will be rejected.
5717
5718 GN only knows about files declared in the "sources" and "public" sections of
5719 targets. If a file is included that is not known to the build, it will be
5720 allowed.
5721
5722 It is common for test targets to need to include private headers for their
5723 associated code. In this case, list the test target in the "friend" list of
5724 the target that owns the private header to allow the inclusion. See
5725 "gn help friend" for more.
Julie Hockette2a29402018-07-31 10:11:42 -07005726
5727 When a binary target has no explicit or implicit public headers (a "public"
5728 list is defined but is empty), GN assumes that the target can not propagate
5729 any compile-time dependencies up the dependency tree. In this case, the build
5730 can be parallelized more efficiently.
5731 Say there are dependencies:
5732 A (shared library) -> B (shared library) -> C (action).
5733 Normally C must complete before any source files in A can compile (because
5734 there might be generated includes). But when B explicitly declares no public
5735 headers, C can execute in parallel with A's compile steps. C must still be
5736 complete before any dependents link.
Brett Wilson796ed472018-07-16 15:11:09 -07005737```
5738
5739#### **Examples**
5740
5741```
5742 These exact files are public:
5743 public = [ "foo.h", "bar.h" ]
5744
5745 No files are public (no targets may include headers from this one):
Julie Hockette2a29402018-07-31 10:11:42 -07005746 # This allows starting compilation in dependent targets earlier.
Brett Wilson796ed472018-07-16 15:11:09 -07005747 public = []
5748```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005749### **public_configs**: Configs to be applied on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07005750
5751```
5752 A list of config labels.
5753
5754 Targets directly depending on this one will have the configs listed in this
5755 variable added to them. These configs will also apply to the current target.
Julie Hockette2a29402018-07-31 10:11:42 -07005756 Generally, public configs are used to apply defines and include directories
5757 necessary to compile this target's header files.
Brett Wilson796ed472018-07-16 15:11:09 -07005758
Julie Hockette2a29402018-07-31 10:11:42 -07005759 See also "gn help all_dependent_configs".
5760```
5761
5762#### **Propagation of public configs**
5763
5764```
5765 Public configs are applied to all targets that depend directly on this one.
5766 These dependant targets can further push this target's public configs
5767 higher in the dependency tree by depending on it via public_deps (see "gn
5768 help public_deps").
5769
5770 static_library("toplevel") {
5771 # This target will get "my_config" applied to it. However, since this
5772 # target uses "deps" and not "public_deps", targets that depend on this
5773 # one won't get it.
5774 deps = [ ":intermediate" ]
5775 }
5776
5777 static_library("intermediate") {
5778 # Depending on "lower" in any way will apply "my_config" to this target.
5779 # Additionall, since this target depends on "lower" via public_deps,
5780 # targets that depend on this one will also get "my_config".
5781 public_deps = [ ":lower" ]
5782 }
5783
5784 static_library("lower") {
5785 # This will get applied to all targets that depend on this one.
5786 public_configs = [ ":my_config" ]
5787 }
5788
5789 Public config propagation happens in a second phase once a target and all of
5790 its dependencies have been resolved. Therefore, a target will not see these
Brett Wilson796ed472018-07-16 15:11:09 -07005791 force-added configs in their "configs" variable while the script is running,
5792 and they can not be removed. As a result, this capability should generally
Julie Hockette2a29402018-07-31 10:11:42 -07005793 only be used to add defines and include directories rather than setting
5794 complicated flags that some targets may not want.
Brett Wilson796ed472018-07-16 15:11:09 -07005795
Julie Hockette2a29402018-07-31 10:11:42 -07005796 Public configs may or may not be propagated across toolchain boundaries
5797 depending on the value of the propagates_configs flag (see "gn help
5798 toolchain") on the toolchain of the target declaring the public_config.
5799```
5800
5801#### **Avoiding applying public configs to this target**
5802
5803```
5804 If you want the config to apply to targets that depend on this one, but NOT
5805 this one, define an extra layer of indirection using a group:
5806
5807 # External targets depend on this group.
5808 group("my_target") {
5809 # Config to apply to all targets that depend on this one.
5810 public_configs = [ ":external_settings" ]
5811 deps = [ ":internal_target" ]
5812 }
5813
5814 # Internal target to actually compile the sources.
5815 static_library("internal_target") {
5816 # Force all external targets to depend on the group instead of directly
5817 # on this so the "external_settings" config will get applied.
5818 visibility = [ ":my_target" ]
5819 ...
5820 }
Brett Wilson796ed472018-07-16 15:11:09 -07005821```
5822
5823#### **Ordering of flags and values**
5824
5825```
5826 1. Those set on the current target (not in a config).
5827 2. Those set on the "configs" on the target in order that the
5828 configs appear in the list.
5829 3. Those set on the "all_dependent_configs" on the target in order
5830 that the configs appear in the list.
5831 4. Those set on the "public_configs" on the target in order that
5832 those configs appear in the list.
5833 5. all_dependent_configs pulled from dependencies, in the order of
5834 the "deps" list. This is done recursively. If a config appears
5835 more than once, only the first occurence will be used.
5836 6. public_configs pulled from dependencies, in the order of the
5837 "deps" list. If a dependency is public, they will be applied
5838 recursively.
5839```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005840### **public_deps**: Declare public dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005841
5842```
5843 Public dependencies are like private dependencies (see "gn help deps") but
5844 additionally express that the current target exposes the listed deps as part
5845 of its public API.
5846
5847 This has several ramifications:
5848
5849 - public_configs that are part of the dependency are forwarded to direct
5850 dependents.
5851
5852 - Public headers in the dependency are usable by dependents (includes do
5853 not require a direct dependency or visibility).
5854
5855 - If the current target is a shared library, other shared libraries that it
5856 publicly depends on (directly or indirectly) are propagated up the
5857 dependency tree to dependents for linking.
Julie Hockette2a29402018-07-31 10:11:42 -07005858
5859 See also "gn help public_configs".
Brett Wilson796ed472018-07-16 15:11:09 -07005860```
5861
5862#### **Discussion**
5863
5864```
5865 Say you have three targets: A -> B -> C. C's visibility may allow B to depend
5866 on it but not A. Normally, this would prevent A from including any headers
5867 from C, and C's public_configs would apply only to B.
5868
5869 If B lists C in its public_deps instead of regular deps, A will now inherit
5870 C's public_configs and the ability to include C's public headers.
5871
5872 Generally if you are writing a target B and you include C's headers as part
5873 of B's public headers, or targets depending on B should consider B and C to
5874 be part of a unit, you should use public_deps instead of deps.
5875```
5876
5877#### **Example**
5878
5879```
5880 # This target can include files from "c" but not from
5881 # "super_secret_implementation_details".
5882 executable("a") {
5883 deps = [ ":b" ]
5884 }
5885
5886 shared_library("b") {
5887 deps = [ ":super_secret_implementation_details" ]
5888 public_deps = [ ":c" ]
5889 }
5890```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005891### **rebase**: Rebase collected metadata as files.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005892
5893```
5894 A boolean that triggers a rebase of collected metadata strings based on their
5895 declared file. Defaults to false.
5896
5897 Metadata generally declares files as strings relative to the local build file.
5898 However, this data is often used in other contexts, and so setting this flag
5899 will force the metadata collection to be rebased according to the local build
5900 file's location and thus allow the filename to be used anywhere.
5901
5902 Setting this flag will raise an error if any target's specified metadata is
5903 not a string value.
5904
5905 See also "gn help generated_file".
5906```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005907### **response_file_contents**: Contents of a response file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07005908
5909```
5910 Sometimes the arguments passed to a script can be too long for the system's
5911 command-line capabilities. This is especially the case on Windows where the
5912 maximum command-line length is less than 8K. A response file allows you to
5913 pass an unlimited amount of data to a script in a temporary file for an
5914 action or action_foreach target.
5915
5916 If the response_file_contents variable is defined and non-empty, the list
5917 will be treated as script args (including possibly substitution patterns)
5918 that will be written to a temporary file at build time. The name of the
5919 temporary file will be substituted for "{{response_file_name}}" in the script
5920 args.
5921
5922 The response file contents will always be quoted and escaped according to
5923 Unix shell rules. To parse the response file, the Python script should use
5924 "shlex.split(file_contents)".
5925```
5926
5927#### **Example**
5928
5929```
5930 action("process_lots_of_files") {
5931 script = "process.py",
5932 inputs = [ ... huge list of files ... ]
5933
5934 # Write all the inputs to a response file for the script. Also,
5935 # make the paths relative to the script working directory.
5936 response_file_contents = rebase_path(inputs, root_build_dir)
5937
5938 # The script expects the name of the response file in --file-list.
5939 args = [
5940 "--enable-foo",
5941 "--file-list={{response_file_name}}",
5942 ]
5943 }
5944```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005945### **script**: Script file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07005946
5947```
5948 An absolute or buildfile-relative file name of a Python script to run for a
5949 action and action_foreach targets (see "gn help action" and "gn help
5950 action_foreach").
5951```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005952### **sources**: Source files for a target
Brett Wilson796ed472018-07-16 15:11:09 -07005953
5954```
5955 A list of files. Non-absolute paths will be resolved relative to the current
5956 build file.
5957```
5958
5959#### **Sources for binary targets**
5960
5961```
5962 For binary targets (source sets, executables, and libraries), the known file
5963 types will be compiled with the associated tools. Unknown file types and
5964 headers will be skipped. However, you should still list all C/C+ header files
5965 so GN knows about the existence of those files for the purposes of include
5966 checking.
5967
5968 As a special case, a file ending in ".def" will be treated as a Windows
5969 module definition file. It will be appended to the link line with a
5970 preceding "/DEF:" string. There must be at most one .def file in a target
5971 and they do not cross dependency boundaries (so specifying a .def file in a
5972 static library or source set will have no effect on the executable or shared
5973 library they're linked into).
Julie Hockettce1fa072019-05-07 17:44:37 -07005974
5975 For Rust targets that do not specify a crate_root, then the crate_root will
5976 look for a lib.rs file (or main.rs for executable) or a single file in
5977 sources, if sources contains only one file.
Brett Wilson796ed472018-07-16 15:11:09 -07005978```
5979
5980#### **Sources for non-binary targets**
5981
5982```
5983 action_foreach
5984 The sources are the set of files that the script will be executed over. The
5985 script will run once per file.
5986
5987 action
5988 The sources will be treated the same as inputs. See "gn help inputs" for
5989 more information and usage advice.
5990
5991 copy
5992 The source are the source files to copy.
5993```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005994### **testonly**: Declares a target must only be used for testing.
Brett Wilson796ed472018-07-16 15:11:09 -07005995
5996```
5997 Boolean. Defaults to false.
5998
5999 When a target is marked "testonly = true", it must only be depended on by
6000 other test-only targets. Otherwise, GN will issue an error that the
6001 depenedency is not allowed.
6002
6003 This feature is intended to prevent accidentally shipping test code in a
6004 final product.
6005```
6006
6007#### **Example**
6008
6009```
6010 source_set("test_support") {
6011 testonly = true
6012 ...
6013 }
6014```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006015### **visibility**: A list of labels that can depend on a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006016
6017```
6018 A list of labels and label patterns that define which targets can depend on
6019 the current one. These permissions are checked via the "check" command (see
6020 "gn help check").
6021
6022 If visibility is not defined, it defaults to public ("*").
6023
6024 If visibility is defined, only the targets with labels that match it can
6025 depend on the current target. The empty list means no targets can depend on
6026 the current target.
6027
6028 Tip: Often you will want the same visibility for all targets in a BUILD file.
6029 In this case you can just put the definition at the top, outside of any
6030 target, and the targets will inherit that scope and see the definition.
6031```
6032
6033#### **Patterns**
6034
6035```
6036 See "gn help label_pattern" for more details on what types of patterns are
6037 supported. If a toolchain is specified, only targets in that toolchain will
6038 be matched. If a toolchain is not specified on a pattern, targets in all
6039 toolchains will be matched.
6040```
6041
6042#### **Examples**
6043
6044```
6045 Only targets in the current buildfile ("private"):
6046 visibility = [ ":*" ]
6047
6048 No targets (used for targets that should be leaf nodes):
6049 visibility = []
6050
6051 Any target ("public", the default):
6052 visibility = [ "*" ]
6053
6054 All targets in the current directory and any subdirectory:
6055 visibility = [ "./*" ]
6056
6057 Any target in "//bar/BUILD.gn":
6058 visibility = [ "//bar:*" ]
6059
6060 Any target in "//bar/" or any subdirectory thereof:
6061 visibility = [ "//bar/*" ]
6062
6063 Just these specific targets:
6064 visibility = [ ":mything", "//foo:something_else" ]
6065
6066 Any target in the current directory and any subdirectory thereof, plus
6067 any targets in "//bar/" and any subdirectory thereof.
6068 visibility = [ "./*", "//bar/*" ]
6069```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006070### **walk_keys**: Key(s) for managing the metadata collection walk.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006071
6072```
6073 Defaults to [].
6074
6075 These keys are used to control the next step in a collection walk, acting as
6076 barriers. If a specified key is defined in a target's metadata, the walk will
6077 use the targets listed in that value to determine which targets are walked.
6078
6079 If no walk_keys are specified for a generated_file target (i.e. "[]"), the
6080 walk will touch all deps and data_deps of the specified target recursively.
6081
6082 See "gn help generated_file".
6083```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006084### **write_runtime_deps**: Writes the target's runtime_deps to the given path.
Brett Wilson796ed472018-07-16 15:11:09 -07006085
6086```
6087 Does not synchronously write the file, but rather schedules it to be written
6088 at the end of generation.
6089
6090 If the file exists and the contents are identical to that being written, the
6091 file will not be updated. This will prevent unnecessary rebuilds of targets
6092 that depend on this file.
6093
6094 Path must be within the output directory.
6095
6096 See "gn help runtime_deps" for how the runtime dependencies are computed.
6097
6098 The format of this file will list one file per line with no escaping. The
6099 files will be relative to the root_build_dir. The first line of the file will
6100 be the main output file of the target itself. The file contents will be the
6101 same as requesting the runtime deps be written on the command line (see "gn
6102 help --runtime-deps-list-file").
6103```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006104### **xcode_extra_attributes**: [scope] Extra attributes for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006105
6106```
6107 The value defined in this scope will be copied to the EXTRA_ATTRIBUTES
6108 property of the generated Xcode project. They are only meaningful when
6109 generating with --ide=xcode.
6110
6111 See "gn help create_bundle" for more information.
6112```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006113### **xcode_test_application_name**: Name for Xcode test target.
Brett Wilson796ed472018-07-16 15:11:09 -07006114
6115```
6116 Each unit and ui test target must have a test application target, and this
6117 value is used to specify the relationship. Only meaningful to Xcode (used as
6118 part of the Xcode project generation).
6119
6120 See "gn help create_bundle" for more information.
6121```
6122
Julie Hockette2a29402018-07-31 10:11:42 -07006123#### **Example**
Brett Wilson796ed472018-07-16 15:11:09 -07006124
6125```
6126 create_bundle("chrome_xctest") {
6127 test_application_name = "chrome"
6128 ...
6129 }
6130```
6131## Other help topics
6132
6133### **Build Arguments Overview**
6134
6135```
6136 Build arguments are variables passed in from outside of the build that build
6137 files can query to determine how the build works.
6138```
6139
6140#### **How build arguments are set**
6141
6142```
6143 First, system default arguments are set based on the current system. The
6144 built-in arguments are:
6145 - host_cpu
6146 - host_os
6147 - current_cpu
6148 - current_os
6149 - target_cpu
6150 - target_os
6151
6152 Next, project-specific overrides are applied. These are specified inside
6153 the default_args variable of //.gn. See "gn help dotfile" for more.
6154
6155 If specified, arguments from the --args command line flag are used. If that
6156 flag is not specified, args from previous builds in the build directory will
6157 be used (this is in the file args.gn in the build directory).
6158
6159 Last, for targets being compiled with a non-default toolchain, the toolchain
6160 overrides are applied. These are specified in the toolchain_args section of a
6161 toolchain definition. The use-case for this is that a toolchain may be
6162 building code for a different platform, and that it may want to always
6163 specify Posix, for example. See "gn help toolchain" for more.
6164
6165 If you specify an override for a build argument that never appears in a
6166 "declare_args" call, a nonfatal error will be displayed.
6167```
6168
6169#### **Examples**
6170
6171```
6172 gn args out/FooBar
6173 Create the directory out/FooBar and open an editor. You would type
6174 something like this into that file:
6175 enable_doom_melon=false
6176 os="android"
6177
6178 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
6179 This will overwrite the build directory with the given arguments. (Note
6180 that the quotes inside the args command will usually need to be escaped
6181 for your shell to pass through strings values.)
6182```
6183
6184#### **How build arguments are used**
6185
6186```
6187 If you want to use an argument, you use declare_args() and specify default
6188 values. These default values will apply if none of the steps listed in the
6189 "How build arguments are set" section above apply to the given argument, but
6190 the defaults will not override any of these.
6191
6192 Often, the root build config file will declare global arguments that will be
6193 passed to all buildfiles. Individual build files can also specify arguments
6194 that apply only to those files. It is also useful to specify build args in an
6195 "import"-ed file if you want such arguments to apply to multiple buildfiles.
6196```
6197### **.gn file**
6198
6199```
6200 When gn starts, it will search the current directory and parent directories
6201 for a file called ".gn". This indicates the source root. You can override
6202 this detection by using the --root command-line argument
6203
6204 The .gn file in the source root will be executed. The syntax is the same as a
6205 buildfile, but with very limited build setup-specific meaning.
6206
6207 If you specify --root, by default GN will look for the file .gn in that
6208 directory. If you want to specify a different file, you can additionally pass
6209 --dotfile:
6210
6211 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
6212```
6213
6214#### **Variables**
6215
6216```
6217 arg_file_template [optional]
6218 Path to a file containing the text that should be used as the default
6219 args.gn content when you run `gn args`.
6220
6221 buildconfig [required]
6222 Path to the build config file. This file will be used to set up the
6223 build file execution environment for each toolchain.
6224
6225 check_targets [optional]
6226 A list of labels and label patterns that should be checked when running
6227 "gn check" or "gn gen --check". If unspecified, all targets will be
Nate Fischer8ed01d32019-01-08 17:32:01 -08006228 checked. If it is the empty list, no targets will be checked. To
6229 bypass this list, request an explicit check of targets, like "//*".
Brett Wilson796ed472018-07-16 15:11:09 -07006230
6231 The format of this list is identical to that of "visibility" so see "gn
6232 help visibility" for examples.
6233
6234 exec_script_whitelist [optional]
6235 A list of .gn/.gni files (not labels) that have permission to call the
6236 exec_script function. If this list is defined, calls to exec_script will
6237 be checked against this list and GN will fail if the current file isn't
6238 in the list.
6239
6240 This is to allow the use of exec_script to be restricted since is easy to
6241 use inappropriately. Wildcards are not supported. Files in the
6242 secondary_source tree (if defined) should be referenced by ignoring the
6243 secondary tree and naming them as if they are in the main tree.
6244
6245 If unspecified, the ability to call exec_script is unrestricted.
6246
6247 Example:
6248 exec_script_whitelist = [
6249 "//base/BUILD.gn",
6250 "//build/my_config.gni",
6251 ]
6252
6253 root [optional]
6254 Label of the root build target. The GN build will start by loading the
6255 build file containing this target name. This defaults to "//:" which will
6256 cause the file //BUILD.gn to be loaded.
6257
6258 script_executable [optional]
6259 Path to specific Python executable or potentially a different language
6260 interpreter that is used to execute scripts in action targets and
6261 exec_script calls.
6262
6263 secondary_source [optional]
6264 Label of an alternate directory tree to find input files. When searching
6265 for a BUILD.gn file (or the build config file discussed above), the file
6266 will first be looked for in the source root. If it's not found, the
6267 secondary source root will be checked (which would contain a parallel
6268 directory hierarchy).
6269
6270 This behavior is intended to be used when BUILD.gn files can't be checked
6271 in to certain source directories for whatever reason.
6272
6273 The secondary source root must be inside the main source tree.
6274
6275 default_args [optional]
6276 Scope containing the default overrides for declared arguments. These
6277 overrides take precedence over the default values specified in the
6278 declare_args() block, but can be overriden using --args or the
6279 args.gn file.
6280
6281 This is intended to be used when subprojects declare arguments with
6282 default values that need to be changed for whatever reason.
6283```
6284
6285#### **Example .gn file contents**
6286
6287```
6288 buildconfig = "//build/config/BUILDCONFIG.gn"
6289
6290 check_targets = [
6291 "//doom_melon/*", # Check everything in this subtree.
6292 "//tools:mind_controlling_ant", # Check this specific target.
6293 ]
6294
6295 root = "//:root"
6296
6297 secondary_source = "//build/config/temporary_buildfiles/"
6298
6299 default_args = {
6300 # Default to release builds for this project.
6301 is_debug = false
6302 is_component_build = false
6303 }
6304```
6305### **Build graph and execution overview**
6306
6307#### **Overall build flow**
6308
6309```
6310 1. Look for ".gn" file (see "gn help dotfile") in the current directory and
6311 walk up the directory tree until one is found. Set this directory to be
6312 the "source root" and interpret this file to find the name of the build
6313 config file.
6314
6315 2. Execute the build config file identified by .gn to set up the global
6316 variables and default toolchain name. Any arguments, variables, defaults,
6317 etc. set up in this file will be visible to all files in the build.
6318
6319 3. Load the //BUILD.gn (in the source root directory).
6320
6321 4. Recursively evaluate rules and load BUILD.gn in other directories as
6322 necessary to resolve dependencies. If a BUILD file isn't found in the
6323 specified location, GN will look in the corresponding location inside
6324 the secondary_source defined in the dotfile (see "gn help dotfile").
6325
6326 5. When a target's dependencies are resolved, write out the `.ninja`
6327 file to disk.
6328
6329 6. When all targets are resolved, write out the root build.ninja file.
6330```
6331
6332#### **Executing target definitions and templates**
6333
6334```
6335 Build files are loaded in parallel. This means it is impossible to
6336 interrogate a target from GN code for any information not derivable from its
6337 label (see "gn help label"). The exception is the get_target_outputs()
6338 function which requires the target being interrogated to have been defined
6339 previously in the same file.
6340
6341 Targets are declared by their type and given a name:
6342
6343 static_library("my_static_library") {
6344 ... target parameter definitions ...
6345 }
6346
6347 There is also a generic "target" function for programmatically defined types
6348 (see "gn help target"). You can define new types using templates (see "gn
6349 help template"). A template defines some custom code that expands to one or
6350 more other targets.
6351
6352 Before executing the code inside the target's { }, the target defaults are
6353 applied (see "gn help set_defaults"). It will inject implicit variable
6354 definitions that can be overridden by the target code as necessary. Typically
6355 this mechanism is used to inject a default set of configs that define the
6356 global compiler and linker flags.
6357```
6358
6359#### **Which targets are built**
6360
6361```
6362 All targets encountered in the default toolchain (see "gn help toolchain")
6363 will have build rules generated for them, even if no other targets reference
6364 them. Their dependencies must resolve and they will be added to the implicit
6365 "all" rule (see "gn help ninja_rules").
6366
6367 Targets in non-default toolchains will only be generated when they are
6368 required (directly or transitively) to build a target in the default
6369 toolchain.
6370
6371 See also "gn help ninja_rules".
6372```
6373
6374#### **Dependencies**
6375
6376```
6377 The only difference between "public_deps" and "deps" except for pushing
6378 configs around the build tree and allowing includes for the purposes of "gn
6379 check".
6380
6381 A target's "data_deps" are guaranteed to be built whenever the target is
6382 built, but the ordering is not defined. The meaning of this is dependencies
6383 required at runtime. Currently data deps will be complete before the target
6384 is linked, but this is not semantically guaranteed and this is undesirable
6385 from a build performance perspective. Since we hope to change this in the
6386 future, do not rely on this behavior.
6387```
6388### **Language and grammar for GN build files**
6389
6390#### **Tokens**
6391
6392```
6393 GN build files are read as sequences of tokens. While splitting the file
6394 into tokens, the next token is the longest sequence of characters that form a
6395 valid token.
6396```
6397
6398#### **White space and comments**
6399
6400```
6401 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
6402 carriage returns (U+000D), and newlines (U+000A).
6403
6404 Comments start at the character "#" and stop at the next newline.
6405
6406 White space and comments are ignored except that they may separate tokens
6407 that would otherwise combine into a single token.
6408```
6409
6410#### **Identifiers**
6411
6412```
6413 Identifiers name variables and functions.
6414
6415 identifier = letter { letter | digit } .
6416 letter = "A" ... "Z" | "a" ... "z" | "_" .
6417 digit = "0" ... "9" .
6418```
6419
6420#### **Keywords**
6421
6422```
6423 The following keywords are reserved and may not be used as identifiers:
6424
6425 else false if true
6426```
6427
6428#### **Integer literals**
6429
6430```
6431 An integer literal represents a decimal integer value.
6432
6433 integer = [ "-" ] digit { digit } .
6434
6435 Leading zeros and negative zero are disallowed.
6436```
6437
6438#### **String literals**
6439
6440```
6441 A string literal represents a string value consisting of the quoted
6442 characters with possible escape sequences and variable expansions.
6443
6444 string = `"` { char | escape | expansion } `"` .
6445 escape = `\` ( "$" | `"` | char ) .
6446 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess "
6447 ") "}" .
6448 Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f]
6449 expansion = "$" ( identifier | BracketExpansion | Hex ) .
6450 char = /* any character except "$", `"`, or newline "
6451 "*/ .
6452
6453 After a backslash, certain sequences represent special characters:
6454
6455 \" U+0022 quotation mark
6456 \$ U+0024 dollar sign
6457 \\ U+005C backslash
6458
6459 All other backslashes represent themselves.
6460
6461 To insert an arbitrary byte value, use $0xFF. For example, to insert a
6462 newline character: "Line one$0x0ALine two".
6463
6464 An expansion will evaluate the variable following the '$' and insert a
6465 stringified version of it into the result. For example, to concat two path
6466 components with a slash separating them:
6467 "$var_one/$var_two"
6468 Use the "${var_one}" format to be explicitly deliniate the variable for
6469 otherwise-ambiguous cases.
6470```
6471
6472#### **Punctuation**
6473
6474```
6475 The following character sequences represent punctuation:
6476
6477 + += == != ( )
6478 - -= < <= [ ]
6479 ! = > >= { }
6480 && || . ,
6481```
6482
6483#### **Grammar**
6484
6485```
6486 The input tokens form a syntax tree following a context-free grammar:
6487
6488 File = StatementList .
6489
6490 Statement = Assignment | Call | Condition .
6491 LValue = identifier | ArrayAccess | ScopeAccess .
6492 Assignment = LValue AssignOp Expr .
6493 Call = identifier "(" [ ExprList ] ")" [ Block ] .
6494 Condition = "if" "(" Expr ")" Block
6495 [ "else" ( Condition | Block ) ] .
6496 Block = "{" StatementList "}" .
6497 StatementList = { Statement } .
6498
6499 ArrayAccess = identifier "[" Expr "]" .
6500 ScopeAccess = identifier "." identifier .
6501 Expr = UnaryExpr | Expr BinaryOp Expr .
6502 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
6503 PrimaryExpr = identifier | integer | string | Call
6504 | ArrayAccess | ScopeAccess | Block
6505 | "(" Expr ")"
6506 | "[" [ ExprList [ "," ] ] "]" .
6507 ExprList = Expr { "," Expr } .
6508
6509 AssignOp = "=" | "+=" | "-=" .
6510 UnaryOp = "!" .
6511 BinaryOp = "+" | "-" // highest priority
6512 | "<" | "<=" | ">" | ">="
6513 | "==" | "!="
6514 | "&&"
6515 | "||" . // lowest priority
6516
6517 All binary operators are left-associative.
6518```
6519
6520#### **Types**
6521
6522```
6523 The GN language is dynamically typed. The following types are used:
6524
6525 - Boolean: Uses the keywords "true" and "false". There is no implicit
6526 conversion between booleans and integers.
6527
6528 - Integers: All numbers in GN are signed 64-bit integers.
6529
6530 - Strings: Strings are 8-bit with no enforced encoding. When a string is
6531 used to interact with other systems with particular encodings (like the
6532 Windows and Mac filesystems) it is assumed to be UTF-8. See "String
6533 literals" above for more.
6534
6535 - Lists: Lists are arbitrary-length ordered lists of values. See "Lists"
6536 below for more.
6537
6538 - Scopes: Scopes are like dictionaries that use variable names for keys. See
6539 "Scopes" below for more.
6540```
6541
6542#### **Lists**
6543
6544```
6545 Lists are created with [] and using commas to separate items:
6546
6547 mylist = [ 0, 1, 2, "some string" ]
6548
6549 A comma after the last item is optional. Lists are dereferenced using 0-based
6550 indexing:
6551
6552 mylist[0] += 1
6553 var = mylist[2]
6554
6555 Lists can be concatenated using the '+' and '+=' operators. Bare values can
6556 not be concatenated with lists, to add a single item, it must be put into a
6557 list of length one.
6558
6559 Items can be removed from lists using the '-' and '-=' operators. This will
6560 remove all occurrences of every item in the right-hand list from the
6561 left-hand list. It is an error to remove an item not in the list. This is to
6562 prevent common typos and to detect dead code that is removing things that no
6563 longer apply.
6564
6565 It is an error to use '=' to replace a nonempty list with another nonempty
6566 list. This is to prevent accidentally overwriting data when in most cases
6567 '+=' was intended. To overwrite a list on purpose, first assign it to the
6568 empty list:
6569
6570 mylist = []
6571 mylist = otherlist
6572
6573 When assigning to a list named 'sources' using '=' or '+=', list items may be
6574 automatically filtered out. See "gn help set_sources_assignment_filter" for
6575 more.
6576```
6577
6578#### **Scopes**
6579
6580```
6581 All execution happens in the context of a scope which holds the current state
6582 (like variables). With the exception of loops and conditions, '{' introduces
6583 a new scope that has a parent reference to the old scope.
6584
6585 Variable reads recursively search all nested scopes until the variable is
6586 found or there are no more scopes. Variable writes always go into the current
6587 scope. This means that after the closing '}' (again excepting loops and
6588 conditions), all local variables will be restored to the previous values.
6589 This also means that "foo = foo" can do useful work by copying a variable
6590 into the current scope that was defined in a containing scope.
6591
6592 Scopes can also be assigned to variables. Such scopes can be created by
6593 functions like exec_script, when invoking a template (the template code
6594 refers to the variables set by the invoking code by the implicitly-created
6595 "invoker" scope), or explicitly like:
6596
6597 empty_scope = {}
6598 myvalues = {
6599 foo = 21
6600 bar = "something"
6601 }
6602
6603 Inside such a scope definition can be any GN code including conditionals and
6604 function calls. After the close of the scope, it will contain all variables
6605 explicitly set by the code contained inside it. After this, the values can be
6606 read, modified, or added to:
6607
6608 myvalues.foo += 2
6609 empty_scope.new_thing = [ 1, 2, 3 ]
Nate Fischer8ed01d32019-01-08 17:32:01 -08006610
6611 Scope equality is defined as single-level scopes identical within the current
6612 scope. That is, all values in the first scope must be present and identical
6613 within the second, and vice versa. Note that this means inherited scopes are
6614 always unequal by definition.
Brett Wilson796ed472018-07-16 15:11:09 -07006615```
Julie Hockettd69a9c32019-01-23 14:36:18 -08006616### **Input and output conversion**
Brett Wilson796ed472018-07-16 15:11:09 -07006617
6618```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006619 Input and output conversions are arguments to file and process functions
6620 that specify how to convert data to or from external formats. The possible
6621 values for parameters specifying conversions are:
6622
Brett Wilson796ed472018-07-16 15:11:09 -07006623 "" (the default)
Julie Hockett09171292018-07-31 14:35:10 -07006624 input: Discard the result and return None.
6625
6626 output: If value is a list, then "list lines"; otherwise "value".
Brett Wilson796ed472018-07-16 15:11:09 -07006627
6628 "list lines"
Julie Hockett09171292018-07-31 14:35:10 -07006629 input:
6630 Return the file contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006631 newlines will not be present in the result. The last line may or may
6632 not end in a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07006633
Julie Hockett09171292018-07-31 14:35:10 -07006634 After splitting, each individual line will be trimmed of whitespace on
6635 both ends.
6636
6637 output:
6638 Renders the value contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006639 newlines will not be present in the result. The last line will end in
6640 with a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07006641
6642 "scope"
Julie Hockett09171292018-07-31 14:35:10 -07006643 input:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006644 Execute the block as GN code and return a scope with the resulting
6645 values in it. If the input was:
Julie Hockett09171292018-07-31 14:35:10 -07006646 a = [ "hello.cc", "world.cc" ]
6647 b = 26
6648 and you read the result into a variable named "val", then you could
6649 access contents the "." operator on "val":
6650 sources = val.a
6651 some_count = val.b
6652
6653 output:
6654 Renders the value contents as a GN code block, reversing the input
6655 result above.
Brett Wilson796ed472018-07-16 15:11:09 -07006656
6657 "string"
Julie Hockett09171292018-07-31 14:35:10 -07006658 input: Return the file contents into a single string.
6659
6660 output:
6661 Render the value contents into a single string. The output is:
6662 a string renders with quotes, e.g. "str"
6663 an integer renders as a stringified integer, e.g. "6"
6664 a boolean renders as the associated string, e.g. "true"
6665 a list renders as a representation of its contents, e.g. "[\"str\", 6]"
6666 a scope renders as a GN code block of its values. If the Value was:
6667 Value val;
6668 val.a = [ "hello.cc", "world.cc" ];
6669 val.b = 26
6670 the resulting output would be:
6671 "{
6672 a = [ \"hello.cc\", \"world.cc\" ]
6673 b = 26
6674 }"
Brett Wilson796ed472018-07-16 15:11:09 -07006675
6676 "value"
Julie Hockett09171292018-07-31 14:35:10 -07006677 input:
6678 Parse the input as if it was a literal rvalue in a buildfile. Examples of
6679 typical program output using this mode:
6680 [ "foo", "bar" ] (result will be a list)
6681 or
6682 "foo bar" (result will be a string)
6683 or
6684 5 (result will be an integer)
Brett Wilson796ed472018-07-16 15:11:09 -07006685
Julie Hockett09171292018-07-31 14:35:10 -07006686 Note that if the input is empty, the result will be a null value which
6687 will produce an error if assigned to a variable.
6688
6689 output:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006690 Render the value contents as a literal rvalue. Strings render with
6691 escaped quotes.
Brett Wilson796ed472018-07-16 15:11:09 -07006692
6693 "json"
Julie Hockett09171292018-07-31 14:35:10 -07006694 input: Parse the input as a JSON and convert it to equivalent GN rvalue.
6695
6696 output: Convert the Value to equivalent JSON value.
6697
6698 The data type mapping is:
Brett Wilson796ed472018-07-16 15:11:09 -07006699 a string in JSON maps to string in GN
6700 an integer in JSON maps to integer in GN
6701 a float in JSON is unsupported and will result in an error
6702 an object in JSON maps to scope in GN
6703 an array in JSON maps to list in GN
6704 a boolean in JSON maps to boolean in GN
6705 a null in JSON is unsupported and will result in an error
6706
Julie Hockett09171292018-07-31 14:35:10 -07006707 Nota that the input dictionary keys have to be valid GN identifiers
6708 otherwise they will produce an error.
Brett Wilson796ed472018-07-16 15:11:09 -07006709
Julie Hockett09171292018-07-31 14:35:10 -07006710 "trim ..." (input only)
Brett Wilson796ed472018-07-16 15:11:09 -07006711 Prefixing any of the other transformations with the word "trim" will
6712 result in whitespace being trimmed from the beginning and end of the
6713 result before processing.
6714
6715 Examples: "trim string" or "trim list lines"
6716
6717 Note that "trim value" is useless because the value parser skips
6718 whitespace anyway.
6719```
6720### **Label patterns**
6721
6722```
6723 A label pattern is a way of expressing one or more labels in a portion of the
6724 source tree. They are not general regular expressions.
6725
6726 They can take the following forms only:
6727
6728 - Explicit (no wildcard):
6729 "//foo/bar:baz"
6730 ":baz"
6731
6732 - Wildcard target names:
6733 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
6734 ":*" (all targets in the current build file)
6735
6736 - Wildcard directory names ("*" is only supported at the end)
6737 "*" (all targets)
6738 "//foo/bar/*" (all targets in any subdir of //foo/bar)
6739 "./*" (all targets in the current build file or sub dirs)
6740
Julie Hockette2a29402018-07-31 10:11:42 -07006741 Any of the above forms can additionally take an explicit toolchain
6742 in parenthesis at the end of the label pattern. In this case, the
6743 toolchain must be fully qualified (no wildcards are supported in the
6744 toolchain name).
Brett Wilson796ed472018-07-16 15:11:09 -07006745
Julie Hockette2a29402018-07-31 10:11:42 -07006746 "//foo:bar(//build/toolchain:mac)"
Brett Wilson796ed472018-07-16 15:11:09 -07006747 An explicit target in an explicit toolchain.
6748
6749 ":*(//build/toolchain/linux:32bit)"
6750 All targets in the current build file using the 32-bit Linux toolchain.
6751
6752 "//foo/*(//build/toolchain:win)"
6753 All targets in //foo and any subdirectory using the Windows
6754 toolchain.
6755```
6756### **About labels**
6757
6758```
6759 Everything that can participate in the dependency graph (targets, configs,
6760 and toolchains) are identified by labels. A common label looks like:
6761
6762 //base/test:test_support
6763
6764 This consists of a source-root-absolute path, a colon, and a name. This means
6765 to look for the thing named "test_support" in "base/test/BUILD.gn".
6766
6767 You can also specify system absolute paths if necessary. Typically such
6768 paths would be specified via a build arg so the developer can specify where
6769 the component is on their system.
6770
6771 /usr/local/foo:bar (Posix)
6772 /C:/Program Files/MyLibs:bar (Windows)
6773```
6774
6775#### **Toolchains**
6776
6777```
6778 A canonical label includes the label of the toolchain being used. Normally,
6779 the toolchain label is implicitly inherited from the current execution
6780 context, but you can override this to specify cross-toolchain dependencies:
6781
6782 //base/test:test_support(//build/toolchain/win:msvc)
6783
6784 Here GN will look for the toolchain definition called "msvc" in the file
6785 "//build/toolchain/win" to know how to compile this target.
6786```
6787
6788#### **Relative labels**
6789
6790```
6791 If you want to refer to something in the same buildfile, you can omit
6792 the path name and just start with a colon. This format is recommended for
6793 all same-file references.
6794
6795 :base
6796
6797 Labels can be specified as being relative to the current directory.
6798 Stylistically, we prefer to use absolute paths for all non-file-local
6799 references unless a build file needs to be run in different contexts (like a
6800 project needs to be both standalone and pulled into other projects in
6801 difference places in the directory hierarchy).
6802
6803 source/plugin:myplugin
6804 ../net:url_request
6805```
6806
6807#### **Implicit names**
6808
6809```
6810 If a name is unspecified, it will inherit the directory name. Stylistically,
6811 we prefer to omit the colon and name when possible:
6812
6813 //net -> //net:net
6814 //tools/gn -> //tools/gn:gn
6815```
6816### **Ninja build rules**
6817
6818#### **The "all" and "default" rules**
6819
6820```
6821 All generated targets (see "gn help execution") will be added to an implicit
6822 build rule called "all" so "ninja all" will always compile everything. The
6823 default rule will be used by Ninja if no specific target is specified (just
6824 typing "ninja"). If there is a target named "default" in the root build file,
6825 it will be the default build rule, otherwise the implicit "all" rule will be
6826 used.
6827```
6828
6829#### **Phony rules**
6830
6831```
6832 GN generates Ninja "phony" rules for targets in the default toolchain. The
6833 phony rules can collide with each other and with the names of generated files
6834 so are generated with the following priority:
6835
6836 1. Actual files generated by the build always take precedence.
6837
6838 2. Targets in the toplevel //BUILD.gn file.
6839
6840 3. Targets in toplevel directories matching the names of the directories.
6841 So "ninja foo" can be used to compile "//foo:foo". This only applies to
6842 the first level of directories since usually these are the most
6843 important (so this won't apply to "//foo/bar:bar").
6844
6845 4. The short names of executables if there is only one executable with that
6846 short name. Use "ninja doom_melon" to compile the
6847 "//tools/fruit:doom_melon" executable.
6848
6849 5. The short names of all targets if there is only one target with that
6850 short name.
6851
6852 6. Full label name with no leading slashes. So you can use
6853 "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon".
6854
6855 7. Labels with an implicit name part (when the short names match the
6856 directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar".
6857
6858 These "phony" rules are provided only for running Ninja since this matches
6859 people's historical expectations for building. For consistency with the rest
6860 of the program, GN introspection commands accept explicit labels.
6861
6862 To explicitly compile a target in a non-default toolchain, you must give
6863 Ninja the exact name of the output file relative to the build directory.
6864```
6865### **nogncheck**: Skip an include line from checking.
6866
6867```
6868 GN's header checker helps validate that the includes match the build
6869 dependency graph. Sometimes an include might be conditional or otherwise
6870 problematic, but you want to specifically allow it. In this case, it can be
6871 whitelisted.
6872
6873 Include lines containing the substring "nogncheck" will be excluded from
6874 header checking. The most common case is a conditional include:
6875
6876 #if defined(ENABLE_DOOM_MELON)
6877 #include "tools/doom_melon/doom_melon.h" // nogncheck
6878 #endif
6879
6880 If the build file has a conditional dependency on the corresponding target
6881 that matches the conditional include, everything will always link correctly:
6882
6883 source_set("mytarget") {
6884 ...
6885 if (enable_doom_melon) {
6886 defines = [ "ENABLE_DOOM_MELON" ]
6887 deps += [ "//tools/doom_melon" ]
6888 }
6889
6890 But GN's header checker does not understand preprocessor directives, won't
6891 know it matches the build dependencies, and will flag this include as
6892 incorrect when the condition is false.
6893```
6894
6895#### **More information**
6896
6897```
6898 The topic "gn help check" has general information on how checking works and
6899 advice on fixing problems. Targets can also opt-out of checking, see
6900 "gn help check_includes".
6901```
Brett Wilson796ed472018-07-16 15:11:09 -07006902### **Runtime dependencies**
6903
6904```
6905 Runtime dependencies of a target are exposed via the "runtime_deps" category
6906 of "gn desc" (see "gn help desc") or they can be written at build generation
6907 time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help
6908 --runtime-deps-list-file").
6909
6910 To a first approximation, the runtime dependencies of a target are the set of
6911 "data" files, data directories, and the shared libraries from all transitive
6912 dependencies. Executables, shared libraries, and loadable modules are
6913 considered runtime dependencies of themselves.
6914```
6915
6916#### **Executables**
6917
6918```
6919 Executable targets and those executable targets' transitive dependencies are
6920 not considered unless that executable is listed in "data_deps". Otherwise, GN
6921 assumes that the executable (and everything it requires) is a build-time
6922 dependency only.
6923```
6924
6925#### **Actions and copies**
6926
6927```
6928 Action and copy targets that are listed as "data_deps" will have all of their
6929 outputs and data files considered as runtime dependencies. Action and copy
6930 targets that are "deps" or "public_deps" will have only their data files
6931 considered as runtime dependencies. These targets can list an output file in
6932 both the "outputs" and "data" lists to force an output file as a runtime
6933 dependency in all cases.
6934
6935 The different rules for deps and data_deps are to express build-time (deps)
6936 vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as
6937 data dependencies, there would be a lot of extra stuff, and if GN counted all
6938 run-time dependencies as regular deps, the build's parallelism would be
6939 unnecessarily constrained.
6940
6941 This rule can sometimes lead to unintuitive results. For example, given the
6942 three targets:
6943 A --[data_deps]--> B --[deps]--> ACTION
6944 GN would say that A does not have runtime deps on the result of the ACTION,
6945 which is often correct. But the purpose of the B target might be to collect
6946 many actions into one logic unit, and the "data"-ness of A's dependency is
6947 lost. Solutions:
6948
6949 - List the outputs of the action in its data section (if the results of
6950 that action are always runtime files).
6951 - Have B list the action in data_deps (if the outputs of the actions are
6952 always runtime files).
6953 - Have B list the action in both deps and data deps (if the outputs might be
6954 used in both contexts and you don't care about unnecessary entries in the
6955 list of files required at runtime).
6956 - Split B into run-time and build-time versions with the appropriate "deps"
6957 for each.
6958```
6959
6960#### **Static libraries and source sets**
6961
6962```
6963 The results of static_library or source_set targets are not considered
6964 runtime dependencies since these are assumed to be intermediate targets only.
6965 If you need to list a static library as a runtime dependency, you can
6966 manually compute the .a/.lib file name for the current platform and list it
6967 in the "data" list of a target (possibly on the static library target
6968 itself).
6969```
6970
6971#### **Multiple outputs**
6972
6973```
6974 Linker tools can specify which of their outputs should be considered when
6975 computing the runtime deps by setting runtime_outputs. If this is unset on
6976 the tool, the default will be the first output only.
6977```
6978### **How Source Expansion Works**
6979
6980```
6981 Source expansion is used for the action_foreach and copy target types to map
6982 source file names to output file names or arguments.
6983
6984 To perform source expansion in the outputs, GN maps every entry in the
6985 sources to every entry in the outputs list, producing the cross product of
6986 all combinations, expanding placeholders (see below).
6987
6988 Source expansion in the args works similarly, but performing the placeholder
6989 substitution produces a different set of arguments for each invocation of the
6990 script.
6991
6992 If no placeholders are found, the outputs or args list will be treated as a
6993 static list of literal file names that do not depend on the sources.
6994
6995 See "gn help copy" and "gn help action_foreach" for more on how this is
6996 applied.
6997```
6998
6999#### **Placeholders**
7000
7001```
7002 This section discusses only placeholders for actions. There are other
7003 placeholders used in the definition of tools. See "gn help tool" for those.
7004
7005 {{source}}
7006 The name of the source file including directory (*). This will generally
7007 be used for specifying inputs to a script in the "args" variable.
7008 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
7009
7010 {{source_file_part}}
7011 The file part of the source including the extension.
7012 "//foo/bar/baz.txt" => "baz.txt"
7013
7014 {{source_name_part}}
7015 The filename part of the source file with no directory or extension. This
7016 will generally be used for specifying a transformation from a source file
7017 to a destination file with the same name but different extension.
7018 "//foo/bar/baz.txt" => "baz"
7019
7020 {{source_dir}}
7021 The directory (*) containing the source file with no trailing slash.
7022 "//foo/bar/baz.txt" => "../../foo/bar"
7023
7024 {{source_root_relative_dir}}
7025 The path to the source file's directory relative to the source root, with
7026 no leading "//" or trailing slashes. If the path is system-absolute,
7027 (beginning in a single slash) this will just return the path with no
7028 trailing slash. This value will always be the same, regardless of whether
7029 it appears in the "outputs" or "args" section.
7030 "//foo/bar/baz.txt" => "foo/bar"
7031
7032 {{source_gen_dir}}
7033 The generated file directory (*) corresponding to the source file's path.
7034 This will be different than the target's generated file directory if the
7035 source file is in a different directory than the BUILD.gn file.
7036 "//foo/bar/baz.txt" => "gen/foo/bar"
7037
7038 {{source_out_dir}}
7039 The object file directory (*) corresponding to the source file's path,
7040 relative to the build directory. this us be different than the target's
7041 out directory if the source file is in a different directory than the
7042 build.gn file.
7043 "//foo/bar/baz.txt" => "obj/foo/bar"
7044
7045 {{source_target_relative}}
7046 The path to the source file relative to the target's directory. This will
7047 generally be used for replicating the source directory layout in the
Robert Sesek252674d2019-05-10 14:08:52 -04007048 output directory. This can only be used in actions and bundle_data
7049 targets. It is an error to use in process_file_template where there is no
7050 "target".
Brett Wilson796ed472018-07-16 15:11:09 -07007051 "//foo/bar/baz.txt" => "baz.txt"
7052```
7053
7054#### **(*) Note on directories**
7055
7056```
7057 Paths containing directories (except the source_root_relative_dir) will be
7058 different depending on what context the expansion is evaluated in. Generally
7059 it should "just work" but it means you can't concatenate strings containing
7060 these values with reasonable results.
7061
7062 Details: source expansions can be used in the "outputs" variable, the "args"
7063 variable, and in calls to "process_file_template". The "args" are passed to a
7064 script which is run from the build directory, so these directories will
7065 relative to the build directory for the script to find. In the other cases,
7066 the directories will be source- absolute (begin with a "//") because the
7067 results of those expansions will be handled by GN internally.
7068```
7069
7070#### **Examples**
7071
7072```
7073 Non-varying outputs:
7074 action("hardcoded_outputs") {
7075 sources = [ "input1.idl", "input2.idl" ]
7076 outputs = [ "$target_out_dir/output1.dat",
7077 "$target_out_dir/output2.dat" ]
7078 }
7079 The outputs in this case will be the two literal files given.
7080
7081 Varying outputs:
7082 action_foreach("varying_outputs") {
7083 sources = [ "input1.idl", "input2.idl" ]
7084 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
7085 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
7086 }
7087 Performing source expansion will result in the following output names:
7088 //out/Debug/obj/mydirectory/input1.h
7089 //out/Debug/obj/mydirectory/input1.cc
7090 //out/Debug/obj/mydirectory/input2.h
7091 //out/Debug/obj/mydirectory/input2.cc
7092```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007093### **Available global switches**
Brett Wilson796ed472018-07-16 15:11:09 -07007094
7095```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007096 Do "gn help --the_switch_you_want_help_on" for more. Individual commands may
7097 take command-specific switches not listed here. See the help on your specific
7098 command for more.
7099```
7100```
7101 * --args: Specifies build arguments overrides.
7102 * --color: Force colored output.
7103 * --dotfile: Override the name of the ".gn" file.
7104 * --fail-on-unused-args: Treat unused build args as fatal errors.
7105 * --markdown: Write help output in the Markdown format.
7106 * --nocolor: Force non-colored output.
7107 * -q: Quiet mode. Don't print output on success.
7108 * --root: Explicitly specify source root.
7109 * --runtime-deps-list-file: Save runtime dependencies for targets in file.
7110 * --script-executable: Set the executable used to execute scripts.
7111 * --threads: Specify number of worker threads.
7112 * --time: Outputs a summary of how long everything took.
7113 * --tracelog: Writes a Chrome-compatible trace log to the given file.
7114 * -v: Verbose logging.
7115 * --version: Prints the GN version number and exits.
Brett Wilson796ed472018-07-16 15:11:09 -07007116```
7117