blob: 06ab5a01104708bfbb3a45e88e1edbb447d6c927 [file] [log] [blame] [view]
Brett Wilson796ed472018-07-16 15:11:09 -07001# GN Reference
2
3*This page is automatically generated from* `gn help --markdown all`.
4
5## Contents
6
7* [Commands](#commands)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008 * [analyze: Analyze which targets are affected by a list of files.](#cmd_analyze)
9 * [args: Display or configure arguments declared by the build.](#cmd_args)
10 * [check: Check header dependencies.](#cmd_check)
11 * [clean: Cleans the output directory.](#cmd_clean)
12 * [desc: Show lots of insightful information about a target or config.](#cmd_desc)
Julie Hockettd69a9c32019-01-23 14:36:18 -080013 * [format: Format .gn files.](#cmd_format)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080014 * [gen: Generate ninja files.](#cmd_gen)
15 * [help: Does what you think.](#cmd_help)
16 * [ls: List matching targets.](#cmd_ls)
17 * [meta: List target metadata collection results.](#cmd_meta)
Brett Wilson74397092020-03-18 16:34:14 -070018 * [outputs: Which files a source/target make.](#cmd_outputs)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080019 * [path: Find paths between two targets.](#cmd_path)
20 * [refs: Find stuff referencing a target or file.](#cmd_refs)
Brett Wilson796ed472018-07-16 15:11:09 -070021* [Target declarations](#targets)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080022 * [action: Declare a target that runs a script a single time.](#func_action)
23 * [action_foreach: Declare a target that runs a script over a set of files.](#func_action_foreach)
24 * [bundle_data: [iOS/macOS] Declare a target without output.](#func_bundle_data)
25 * [copy: Declare a target that copies files.](#func_copy)
26 * [create_bundle: [iOS/macOS] Build an iOS or macOS bundle.](#func_create_bundle)
27 * [executable: Declare an executable target.](#func_executable)
28 * [generated_file: Declare a generated_file target.](#func_generated_file)
29 * [group: Declare a named group of targets.](#func_group)
30 * [loadable_module: Declare a loadable module target.](#func_loadable_module)
Julie Hockettce1fa072019-05-07 17:44:37 -070031 * [rust_library: Declare a Rust library target.](#func_rust_library)
Petr Hosekfe36c7c2019-11-26 14:38:08 -080032 * [rust_proc_macro: Declare a Rust procedural macro target.](#func_rust_proc_macro)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080033 * [shared_library: Declare a shared library target.](#func_shared_library)
34 * [source_set: Declare a source set target.](#func_source_set)
35 * [static_library: Declare a static library target.](#func_static_library)
36 * [target: Declare an target with the given programmatic type.](#func_target)
Brett Wilson796ed472018-07-16 15:11:09 -070037* [Buildfile functions](#functions)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080038 * [assert: Assert an expression is true at generation time.](#func_assert)
39 * [config: Defines a configuration object.](#func_config)
40 * [declare_args: Declare build arguments.](#func_declare_args)
41 * [defined: Returns whether an identifier is defined.](#func_defined)
42 * [exec_script: Synchronously run a script and return the output.](#func_exec_script)
Sylvain Defresneaff489a2020-03-11 18:27:43 +010043 * [filter_exclude: Remove values that match a set of patterns.](#func_filter_exclude)
44 * [filter_include: Remove values that do not match a set of patterns.](#func_filter_include)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080045 * [foreach: Iterate over a list.](#func_foreach)
46 * [forward_variables_from: Copies variables from a different scope.](#func_forward_variables_from)
47 * [get_label_info: Get an attribute from a target's label.](#func_get_label_info)
48 * [get_path_info: Extract parts of a file or directory name.](#func_get_path_info)
49 * [get_target_outputs: [file list] Get the list of outputs from a target.](#func_get_target_outputs)
50 * [getenv: Get an environment variable.](#func_getenv)
51 * [import: Import a file into the current scope.](#func_import)
52 * [not_needed: Mark variables from scope as not needed.](#func_not_needed)
53 * [pool: Defines a pool object.](#func_pool)
54 * [print: Prints to the console.](#func_print)
55 * [process_file_template: Do template expansion over a list of files.](#func_process_file_template)
56 * [read_file: Read a file into a variable.](#func_read_file)
57 * [rebase_path: Rebase a file or directory to another location.](#func_rebase_path)
58 * [set_default_toolchain: Sets the default toolchain name.](#func_set_default_toolchain)
59 * [set_defaults: Set default values for a target type.](#func_set_defaults)
Sylvain Defresnead0a7ad2020-10-15 15:03:44 +020060 * [set_sources_assignment_filter: Deprecated feature.](#func_set_sources_assignment_filter)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080061 * [split_list: Splits a list into N different sub-lists.](#func_split_list)
Keir Mierle6ae63302019-11-08 23:02:18 -080062 * [string_join: Concatenates a list of strings with a separator.](#func_string_join)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080063 * [string_replace: Replaces substring in the given string.](#func_string_replace)
Keir Mierle6ae63302019-11-08 23:02:18 -080064 * [string_split: Split string into a list of strings.](#func_string_split)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080065 * [template: Define a template rule.](#func_template)
66 * [tool: Specify arguments to a toolchain tool.](#func_tool)
67 * [toolchain: Defines a toolchain.](#func_toolchain)
68 * [write_file: Write a file to disk.](#func_write_file)
Brett Wilson796ed472018-07-16 15:11:09 -070069* [Built-in predefined variables](#predefined_variables)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080070 * [current_cpu: [string] The processor architecture of the current toolchain.](#var_current_cpu)
71 * [current_os: [string] The operating system of the current toolchain.](#var_current_os)
72 * [current_toolchain: [string] Label of the current toolchain.](#var_current_toolchain)
73 * [default_toolchain: [string] Label of the default toolchain.](#var_default_toolchain)
James Robinson61377e32020-02-13 15:20:07 -080074 * [gn_version: [number] The version of gn.](#var_gn_version)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080075 * [host_cpu: [string] The processor architecture that GN is running on.](#var_host_cpu)
76 * [host_os: [string] The operating system that GN is running on.](#var_host_os)
77 * [invoker: [string] The invoking scope inside a template.](#var_invoker)
78 * [python_path: [string] Absolute path of Python.](#var_python_path)
79 * [root_build_dir: [string] Directory where build commands are run.](#var_root_build_dir)
80 * [root_gen_dir: [string] Directory for the toolchain's generated files.](#var_root_gen_dir)
81 * [root_out_dir: [string] Root directory for toolchain output files.](#var_root_out_dir)
82 * [target_cpu: [string] The desired cpu architecture for the build.](#var_target_cpu)
83 * [target_gen_dir: [string] Directory for a target's generated files.](#var_target_gen_dir)
84 * [target_name: [string] The name of the current target.](#var_target_name)
85 * [target_os: [string] The desired operating system for the build.](#var_target_os)
86 * [target_out_dir: [string] Directory for target output files.](#var_target_out_dir)
Brett Wilson796ed472018-07-16 15:11:09 -070087* [Variables you set in targets](#target_variables)
Julie Hockettce1fa072019-05-07 17:44:37 -070088 * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080089 * [all_dependent_configs: [label list] Configs to be forced on dependents.](#var_all_dependent_configs)
90 * [allow_circular_includes_from: [label list] Permit includes from deps.](#var_allow_circular_includes_from)
91 * [arflags: [string list] Arguments passed to static_library archiver.](#var_arflags)
92 * [args: [string list] Arguments passed to an action.](#var_args)
93 * [asmflags: [string list] Flags passed to the assembler.](#var_asmflags)
94 * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#var_assert_no_deps)
Sylvain Defresne89e64252020-08-07 13:01:06 +020095 * [bridge_header: [string] Path to C/Objective-C compatibility header.](#var_bridge_header)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080096 * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#var_bundle_contents_dir)
97 * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#var_bundle_deps_filter)
98 * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#var_bundle_executable_dir)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080099 * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#var_bundle_resources_dir)
100 * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#var_bundle_root_dir)
101 * [cflags: [string list] Flags passed to all C compiler variants.](#var_cflags)
102 * [cflags_c: [string list] Flags passed to the C compiler.](#var_cflags_c)
103 * [cflags_cc: [string list] Flags passed to the C++ compiler.](#var_cflags_cc)
104 * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#var_cflags_objc)
105 * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#var_cflags_objcc)
106 * [check_includes: [boolean] Controls whether a target's files are checked.](#var_check_includes)
107 * [code_signing_args: [string list] Arguments passed to code signing script.](#var_code_signing_args)
108 * [code_signing_outputs: [file list] Output files for code signing step.](#var_code_signing_outputs)
109 * [code_signing_script: [file name] Script for code signing.](#var_code_signing_script)
110 * [code_signing_sources: [file list] Sources for code signing step.](#var_code_signing_sources)
111 * [complete_static_lib: [boolean] Links all deps into a static library.](#var_complete_static_lib)
112 * [configs: [label list] Configs applying to this target or config.](#var_configs)
113 * [contents: Contents to write to file.](#var_contents)
Julie Hockettce1fa072019-05-07 17:44:37 -0700114 * [crate_name: [string] The name for the compiled crate.](#var_crate_name)
115 * [crate_root: [string] The root source file for a binary or library.](#var_crate_root)
116 * [crate_type: [string] The type of linkage to use on a shared_library.](#var_crate_type)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800117 * [data: [file list] Runtime data file dependencies.](#var_data)
118 * [data_deps: [label list] Non-linked dependencies.](#var_data_deps)
119 * [data_keys: [string list] Keys from which to collect metadata.](#var_data_keys)
120 * [defines: [string list] C preprocessor defines.](#var_defines)
121 * [depfile: [string] File name for input dependencies for actions.](#var_depfile)
122 * [deps: [label list] Private linked dependencies.](#var_deps)
Petr Hoseka1413862020-01-03 12:54:33 -0800123 * [externs: [scope] Set of Rust crate-dependency pairs.](#var_externs)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100124 * [framework_dirs: [directory list] Additional framework search directories.](#var_framework_dirs)
125 * [frameworks: [name list] Name of frameworks that must be linked.](#var_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800126 * [friend: [label pattern list] Allow targets to include private headers.](#var_friend)
127 * [include_dirs: [directory list] Additional include directories.](#var_include_dirs)
128 * [inputs: [file list] Additional compile-time dependencies.](#var_inputs)
129 * [ldflags: [string list] Flags passed to the linker.](#var_ldflags)
130 * [lib_dirs: [directory list] Additional library directories.](#var_lib_dirs)
131 * [libs: [string list] Additional libraries to link.](#var_libs)
132 * [metadata: [scope] Metadata of this target.](#var_metadata)
Sylvain Defresne89e64252020-08-07 13:01:06 +0200133 * [module_name: [string] The name for the compiled module.](#var_module_name)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800134 * [output_conversion: Data format for generated_file targets.](#var_output_conversion)
135 * [output_dir: [directory] Directory to put output file in.](#var_output_dir)
136 * [output_extension: [string] Value to use for the output's file extension.](#var_output_extension)
137 * [output_name: [string] Name for the output file other than the default.](#var_output_name)
138 * [output_prefix_override: [boolean] Don't use prefix for output name.](#var_output_prefix_override)
139 * [outputs: [file list] Output files for actions and copy targets.](#var_outputs)
140 * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#var_partial_info_plist)
141 * [pool: [string] Label of the pool used by the action.](#var_pool)
142 * [precompiled_header: [string] Header file to precompile.](#var_precompiled_header)
143 * [precompiled_header_type: [string] "gcc" or "msvc".](#var_precompiled_header_type)
144 * [precompiled_source: [file name] Source file to precompile.](#var_precompiled_source)
145 * [product_type: [string] Product type for Xcode projects.](#var_product_type)
146 * [public: [file list] Declare public header files for a target.](#var_public)
147 * [public_configs: [label list] Configs applied to dependents.](#var_public_configs)
148 * [public_deps: [label list] Declare public dependencies.](#var_public_deps)
149 * [rebase: [boolean] Rebase collected metadata as files.](#var_rebase)
150 * [response_file_contents: [string list] Contents of .rsp file for actions.](#var_response_file_contents)
151 * [script: [file name] Script file for actions.](#var_script)
152 * [sources: [file list] Source files for a target.](#var_sources)
Sylvain Defresne89e64252020-08-07 13:01:06 +0200153 * [swiftflags: [string list] Flags passed to the swift compiler.](#var_swiftflags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800154 * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly)
155 * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility)
156 * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys)
Robert Sesekd0a6f072020-05-15 11:21:22 -0400157 * [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800158 * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps)
Harley Li0a9affb2020-06-03 10:38:42 -0400159 * [xcasset_compiler_flags: [string list] Flags passed to xcassets compiler](#var_xcasset_compiler_flags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800160 * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes)
161 * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name)
Brett Wilson796ed472018-07-16 15:11:09 -0700162* [Other help topics](#other)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800163 * all: Print all the help at once
Brett Wilson796ed472018-07-16 15:11:09 -0700164 * [buildargs: How build arguments work.](#buildargs)
165 * [dotfile: Info about the toplevel .gn file.](#dotfile)
166 * [execution: Build graph and execution overview.](#execution)
167 * [grammar: Language and grammar for GN build files.](#grammar)
Julie Hockett81ee1962019-05-13 11:24:42 -0700168 * [input_conversion: Processing input from exec_script and read_file.](#io_conversion)
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100169 * [file_pattern: Matching more than one file.](#file_pattern)
Brett Wilson796ed472018-07-16 15:11:09 -0700170 * [label_pattern: Matching more than one label.](#label_pattern)
171 * [labels: About labels.](#labels)
Julie Hockett152c5142019-07-12 09:53:43 -0600172 * [metadata_collection: About metadata and its collection.](#metadata_collection)
Brett Wilson796ed472018-07-16 15:11:09 -0700173 * [ninja_rules: How Ninja build rules are named.](#ninja_rules)
174 * [nogncheck: Annotating includes for checking.](#nogncheck)
Julie Hockett81ee1962019-05-13 11:24:42 -0700175 * [output_conversion: Specifies how to transform a value to output.](#io_conversion)
Brett Wilson796ed472018-07-16 15:11:09 -0700176 * [runtime_deps: How runtime dependency computation works.](#runtime_deps)
177 * [source_expansion: Map sources to outputs for scripts.](#source_expansion)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800178 * [switches: Show available command-line switches.](#switch_list)
Brett Wilson796ed472018-07-16 15:11:09 -0700179
180## Commands
181
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800182### **gn analyze <out_dir> <input_path> <output_path>**
Brett Wilson796ed472018-07-16 15:11:09 -0700183
184```
185 Analyze which targets are affected by a list of files.
186
187 This command takes three arguments:
188
189 out_dir is the path to the build directory.
190
191 input_path is a path to a file containing a JSON object with three fields:
192
193 - "files": A list of the filenames to check.
194
195 - "test_targets": A list of the labels for targets that are needed to run
196 the tests we wish to run.
197
198 - "additional_compile_targets": A list of the labels for targets that we
199 wish to rebuild, but aren't necessarily needed for testing. The important
200 difference between this field and "test_targets" is that if an item in
201 the additional_compile_targets list refers to a group, then any
202 dependencies of that group will be returned if they are out of date, but
203 the group itself does not need to be. If the dependencies themselves are
204 groups, the same filtering is repeated. This filtering can be used to
205 avoid rebuilding dependencies of a group that are unaffected by the input
206 files. The list may also contain the string "all" to refer to a
207 pseudo-group that contains every root target in the build graph.
208
209 This filtering behavior is also known as "pruning" the list of compile
210 targets.
211
Keir Mierle45611e32019-11-12 11:18:00 -0800212 If input_path is -, input is read from stdin.
213
Brett Wilson796ed472018-07-16 15:11:09 -0700214 output_path is a path indicating where the results of the command are to be
215 written. The results will be a file containing a JSON object with one or more
216 of following fields:
217
218 - "compile_targets": A list of the labels derived from the input
219 compile_targets list that are affected by the input files. Due to the way
220 the filtering works for compile targets as described above, this list may
221 contain targets that do not appear in the input list.
222
223 - "test_targets": A list of the labels from the input test_targets list that
224 are affected by the input files. This list will be a proper subset of the
225 input list.
226
227 - "invalid_targets": A list of any names from the input that do not exist in
228 the build graph. If this list is non-empty, the "error" field will also be
229 set to "Invalid targets".
230
231 - "status": A string containing one of three values:
232
233 - "Found dependency"
234 - "No dependency"
Sylvain Defresne7b0690e2020-06-26 15:33:01 +0200235 - "Found dependency (all)"
Brett Wilson796ed472018-07-16 15:11:09 -0700236
237 In the first case, the lists returned in compile_targets and test_targets
238 should be passed to ninja to build. In the second case, nothing was
239 affected and no build is necessary. In the third case, GN could not
240 determine the correct answer and returned the input as the output in order
241 to be safe.
242
243 - "error": This will only be present if an error occurred, and will contain
244 a string describing the error. This includes cases where the input file is
245 not in the right format, or contains invalid targets.
246
Keir Mierle45611e32019-11-12 11:18:00 -0800247 If output_path is -, output is written to stdout.
248
Brett Wilson796ed472018-07-16 15:11:09 -0700249 The command returns 1 if it is unable to read the input file or write the
250 output file, or if there is something wrong with the build such that gen
251 would also fail, and 0 otherwise. In particular, it returns 0 even if the
252 "error" key is non-empty and a non-fatal error occurred. In other words, it
253 tries really hard to always write something to the output JSON and convey
254 errors that way rather than via return codes.
255```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800256### **gn args**: (command-line tool)
Brett Wilson796ed472018-07-16 15:11:09 -0700257
258```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800259 Display or configure arguments declared by the build.
260
261 gn args [--list] [--short] [--args] [--overrides-only]
262
Brett Wilson796ed472018-07-16 15:11:09 -0700263 See also "gn help buildargs" for a more high-level overview of how
264 build arguments work.
265```
266
267#### **Usage**
268
269```
270 gn args
271 Open the arguments for the given build directory in an editor. If the
272 given build directory doesn't exist, it will be created and an empty args
273 file will be opened in the editor. You would type something like this
274 into that file:
275 enable_doom_melon=false
276 os="android"
277
278 To find your editor on Posix, GN will search the environment variables in
279 order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command
280 associated with .txt files.
281
282 Note: you can edit the build args manually by editing the file "args.gn"
283 in the build directory and then running "gn gen ".
284
285 gn args --list[=] [--short] [--overrides-only] [--json]
286 Lists all build arguments available in the current configuration, or, if
287 an exact_arg is specified for the list flag, just that one build
288 argument.
289
290 The output will list the declaration location, current value for the
291 build, default value (if different than the current value), and comment
292 preceding the declaration.
293
294 If --short is specified, only the names and current values will be
295 printed.
296
297 If --overrides-only is specified, only the names and current values of
298 arguments that have been overridden (i.e. non-default arguments) will
299 be printed. Overrides come from the /args.gn file and //.gn
300
301 If --json is specified, the output will be emitted in json format.
302 JSON schema for output:
303 [
304 {
305 "name": variable_name,
306 "current": {
307 "value": overridden_value,
308 "file": file_name,
309 "line": line_no
310 },
311 "default": {
312 "value": default_value,
313 "file": file_name,
314 "line": line_no
315 },
316 "comment": comment_string
317 },
318 ...
319 ]
320```
321
322#### **Examples**
323
324```
325 gn args out/Debug
326 Opens an editor with the args for out/Debug.
327
328 gn args out/Debug --list --short
329 Prints all arguments with their default values for the out/Debug
330 build.
331
332 gn args out/Debug --list --short --overrides-only
333 Prints overridden arguments for the out/Debug build.
334
335 gn args out/Debug --list=target_cpu
336 Prints information about the "target_cpu" argument for the "
337 "out/Debug
338 build.
339
340 gn args --list --args="os=\"android\" enable_doom_melon=true"
341 Prints all arguments with the default values for a build with the
342 given arguments set (which may affect the values of other
343 arguments).
344```
Joe Armstrong39413942019-03-15 10:34:03 +0800345### **gn check <out_dir> [<label_pattern>] [\--force] [\--check-generated]**
Brett Wilson796ed472018-07-16 15:11:09 -0700346
347```
348 GN's include header checker validates that the includes for C-like source
349 files match the build dependency graph.
350
351 "gn check" is the same thing as "gn gen" with the "--check" flag except that
352 this command does not write out any build files. It's intended to be an easy
353 way to manually trigger include file checking.
354
355 The can take exact labels or patterns that match more than
356 one (although not general regular expressions). If specified, only those
357 matching targets will be checked. See "gn help label_pattern" for details.
358```
359
360#### **Command-specific switches**
361
362```
Joe Armstrong39413942019-03-15 10:34:03 +0800363 --check-generated
364 Generated files are normally not checked since they do not exist
365 until after a build. With this flag, those generated files that
366 can be found on disk are also checked.
James Robinson49f59032020-01-08 14:05:51 -0800367
368 --check-system
369 Check system style includes (using ) in addition to
370 "double quote" includes.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400371
372 --default-toolchain
373 Normally wildcard targets are matched in all toolchains. This
374 switch makes wildcard labels with no explicit toolchain reference
375 only match targets in the default toolchain.
376
377 Non-wildcard inputs with no explicit toolchain specification will
378 always match only a target in the default toolchain if one exists.
379
380 --force
381 Ignores specifications of "check_includes = false" and checks all
382 target's files that match the target label.
Brett Wilson796ed472018-07-16 15:11:09 -0700383```
384
385#### **What gets checked**
386
387```
Nate Fischer8ed01d32019-01-08 17:32:01 -0800388 The .gn file may specify a list of targets to be checked in the list
Erik Staab5d109d72020-06-29 22:53:23 -0700389 check_targets (see "gn help dotfile"). Alternatively, the .gn file may
390 specify a list of targets not to be checked in no_check_targets. If a label
391 pattern is specified on the command line, neither check_targets or
392 no_check_targets is used.
Brett Wilson796ed472018-07-16 15:11:09 -0700393
394 Targets can opt-out from checking with "check_includes = false" (see
395 "gn help check_includes").
396
397 For targets being checked:
398
399 - GN opens all C-like source files in the targets to be checked and scans
400 the top for includes.
401
Joe Armstrong39413942019-03-15 10:34:03 +0800402 - Generated files (that might not exist yet) are ignored unless
403 the --check-generated flag is provided.
404
Brett Wilson796ed472018-07-16 15:11:09 -0700405 - Includes with a "nogncheck" annotation are skipped (see
406 "gn help nogncheck").
407
James Robinson49f59032020-01-08 14:05:51 -0800408 - Includes using "quotes" are always checked.
409 If system style checking is enabled, includes using
410 are also checked.
Brett Wilson796ed472018-07-16 15:11:09 -0700411
412 - Include paths are assumed to be relative to any of the "include_dirs" for
413 the target (including the implicit current dir).
414
415 - GN does not run the preprocessor so will not understand conditional
416 includes.
417
418 - Only includes matching known files in the build are checked: includes
419 matching unknown paths are ignored.
420
421 For an include to be valid:
422
423 - The included file must be in the current target, or there must be a path
424 following only public dependencies to a target with the file in it
425 ("gn path" is a good way to diagnose problems).
426
427 - There can be multiple targets with an included file: only one needs to be
428 valid for the include to be allowed.
429
430 - If there are only "sources" in a target, all are considered to be public
431 and can be included by other targets with a valid public dependency path.
432
433 - If a target lists files as "public", only those files are able to be
434 included by other targets. Anything in the sources will be considered
435 private and will not be includable regardless of dependency paths.
436
437 - Outputs from actions are treated like public sources on that target.
438
439 - A target can include headers from a target that depends on it if the
440 other target is annotated accordingly. See "gn help
441 allow_circular_includes_from".
442```
443
444#### **Advice on fixing problems**
445
446```
447 If you have a third party project that is difficult to fix or doesn't care
448 about include checks it's generally best to exclude that target from checking
449 altogether via "check_includes = false".
450
451 If you have conditional includes, make sure the build conditions and the
452 preprocessor conditions match, and annotate the line with "nogncheck" (see
453 "gn help nogncheck" for an example).
454
455 If two targets are hopelessly intertwined, use the
456 "allow_circular_includes_from" annotation. Ideally each should have identical
457 dependencies so configs inherited from those dependencies are consistent (see
458 "gn help allow_circular_includes_from").
459
460 If you have a standalone header file or files that need to be shared between
461 a few targets, you can consider making a source_set listing only those
462 headers as public sources. With only header files, the source set will be a
463 no-op from a build perspective, but will give a central place to refer to
464 those headers. That source set's files will still need to pass "gn check" in
465 isolation.
466
467 In rare cases it makes sense to list a header in more than one target if it
468 could be considered conceptually a member of both.
469```
470
471#### **Examples**
472
473```
474 gn check out/Debug
475 Check everything.
476
477 gn check out/Default //foo:bar
478 Check only the files in the //foo:bar target.
479
480 gn check out/Default "//foo/*
481 Check only the files in targets in the //foo directory tree.
482```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400483### **gn clean <out_dir>...**
Brett Wilson796ed472018-07-16 15:11:09 -0700484
485```
486 Deletes the contents of the output directory except for args.gn and
487 creates a Ninja build environment sufficient to regenerate the build.
488```
Joe Armstrongb199e542019-03-21 09:40:08 +0800489### **gn desc**
Brett Wilson796ed472018-07-16 15:11:09 -0700490
491```
Joe Armstrongb199e542019-03-21 09:40:08 +0800492 gn desc
493 [--format=json]
494
Julie Hockette2a29402018-07-31 10:11:42 -0700495 Displays information about a given target or config. The build parameters
496 will be taken for the build in the given .
Brett Wilson796ed472018-07-16 15:11:09 -0700497
498 The
499 pattern (see "gn help label_pattern"). A label pattern will only match
500 targets.
501```
502
Nico Weber75eb3c72018-08-28 16:01:52 -0400503#### **Possibilities for <what to show>**
Brett Wilson796ed472018-07-16 15:11:09 -0700504
505```
506 (If unspecified an overall summary will be displayed.)
507
508 all_dependent_configs
509 allow_circular_includes_from
510 arflags [--blame]
511 args
512 cflags [--blame]
Julie Hockette2a29402018-07-31 10:11:42 -0700513 cflags_c [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700514 cflags_cc [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700515 check_includes
516 configs [--tree] (see below)
Nate Fischer8ed01d32019-01-08 17:32:01 -0800517 data_keys
Brett Wilson796ed472018-07-16 15:11:09 -0700518 defines [--blame]
519 depfile
520 deps [--all] [--tree] (see below)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100521 framework_dirs
522 frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700523 include_dirs [--blame]
524 inputs
525 ldflags [--blame]
526 lib_dirs
527 libs
Nate Fischer8ed01d32019-01-08 17:32:01 -0800528 metadata
529 output_conversion
Brett Wilson796ed472018-07-16 15:11:09 -0700530 outputs
531 public_configs
532 public
Nate Fischer8ed01d32019-01-08 17:32:01 -0800533 rebase
Brett Wilson796ed472018-07-16 15:11:09 -0700534 script
535 sources
536 testonly
537 visibility
Nate Fischer8ed01d32019-01-08 17:32:01 -0800538 walk_keys
Robert Sesekd0a6f072020-05-15 11:21:22 -0400539 weak_frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700540
541 runtime_deps
542 Compute all runtime deps for the given target. This is a computed list
543 and does not correspond to any GN variable, unlike most other values
544 here.
545
546 The output is a list of file names relative to the build directory. See
547 "gn help runtime_deps" for how this is computed. This also works with
548 "--blame" to see the source of the dependency.
549```
550
551#### **Shared flags**
Brett Wilson796ed472018-07-16 15:11:09 -0700552
Robert Sesekd0a6f072020-05-15 11:21:22 -0400553```
554 --default-toolchain
555 Normally wildcard targets are matched in all toolchains. This
556 switch makes wildcard labels with no explicit toolchain reference
557 only match targets in the default toolchain.
558
559 Non-wildcard inputs with no explicit toolchain specification will
560 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -0700561
562 --format=json
563 Format the output as JSON instead of text.
564```
565
566#### **Target flags**
567
568```
569 --blame
570 Used with any value specified on a config, this will name the config that
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100571 causes that target to get the flag. This doesn't currently work for libs,
Robert Sesekd0a6f072020-05-15 11:21:22 -0400572 lib_dirs, frameworks, weak_frameworks and framework_dirs because those are
573 inherited and are more complicated to figure out the blame (patches
574 welcome).
Brett Wilson796ed472018-07-16 15:11:09 -0700575```
576
577#### **Configs**
578
579```
580 The "configs" section will list all configs that apply. For targets this will
581 include configs specified in the "configs" variable of the target, and also
582 configs pushed onto this target via public or "all dependent" configs.
583
584 Configs can have child configs. Specifying --tree will show the hierarchy.
585```
586
587#### **Printing outputs**
588
589```
590 The "outputs" section will list all outputs that apply, including the outputs
591 computed from the tool definition (eg for "executable", "static_library", ...
592 targets).
593```
594
595#### **Printing deps**
596
597```
598 Deps will include all public, private, and data deps (TODO this could be
599 clarified and enhanced) sorted in order applying. The following may be used:
600
601 --all
602 Collects all recursive dependencies and prints a sorted flat list. Also
603 usable with --tree (see below).
Robert Sesekd0a6f072020-05-15 11:21:22 -0400604
Brett Wilson796ed472018-07-16 15:11:09 -0700605 --as=(buildfile|label|output)
606 How to print targets.
607
608 buildfile
609 Prints the build files where the given target was declared as
610 file names.
611 label (default)
612 Prints the label of the target.
613 output
614 Prints the first output file for the target relative to the
615 root build directory.
616
617 --testonly=(true|false)
618 Restrict outputs to targets with the testonly flag set
619 accordingly. When unspecified, the target's testonly flags are
620 ignored.
621
622 --tree
623 Print a dependency tree. By default, duplicates will be elided with "..."
624 but when --all and -tree are used together, no eliding will be performed.
625
626 The "deps", "public_deps", and "data_deps" will all be included in the
627 tree.
628
629 Tree output can not be used with the filtering or output flags: --as,
630 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400631
Brett Wilson796ed472018-07-16 15:11:09 -0700632 --type=(action|copy|executable|group|loadable_module|shared_library|
633 source_set|static_library)
634 Restrict outputs to targets matching the given type. If
635 unspecified, no filtering will be performed.
636```
637
638#### **Note**
639
640```
641 This command will show the full name of directories and source files, but
642 when directories and source paths are written to the build file, they will be
643 adjusted to be relative to the build directory. So the values for paths
644 displayed by this command won't match (but should mean the same thing).
645```
646
647#### **Examples**
648
649```
650 gn desc out/Debug //base:base
651 Summarizes the given target.
652
653 gn desc out/Foo :base_unittests deps --tree
654 Shows a dependency tree of the "base_unittests" project in
655 the current directory.
656
657 gn desc out/Debug //base defines --blame
658 Shows defines set for the //base:base target, annotated by where
659 each one was set from.
660```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800661### **gn format [\--dump-tree] (\--stdin | <list of build_files...>)**
Brett Wilson796ed472018-07-16 15:11:09 -0700662
663```
664 Formats .gn file to a standard format.
665
666 The contents of some lists ('sources', 'deps', etc.) will be sorted to a
667 canonical order. To suppress this, you can add a comment of the form "#
668 NOSORT" immediately preceding the assignment. e.g.
669
670 # NOSORT
671 sources = [
672 "z.cc",
673 "a.cc",
674 ]
675```
676
677#### **Arguments**
678
679```
680 --dry-run
Nico Weberad9eab22020-11-15 22:20:28 -0500681 Prints the list of files that would be reformatted but does not write
682 anything to disk. This is useful for presubmit/lint-type checks.
Brett Wilson796ed472018-07-16 15:11:09 -0700683 - Exit code 0: successful format, matches on disk.
684 - Exit code 1: general failure (parse error, etc.)
685 - Exit code 2: successful format, but differs from on disk.
686
Julie Hockettd69a9c32019-01-23 14:36:18 -0800687 --dump-tree[=( text | json )]
688 Dumps the parse tree to stdout and does not update the file or print
689 formatted output. If no format is specified, text format will be used.
Brett Wilson796ed472018-07-16 15:11:09 -0700690
691 --stdin
692 Read input from stdin and write to stdout rather than update a file
693 in-place.
694```
695
696#### **Examples**
697```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800698 gn format //some/BUILD.gn //some/other/BUILD.gn //and/another/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700699 gn format some\\BUILD.gn
700 gn format /abspath/some/BUILD.gn
701 gn format --stdin
702```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800703### **gn gen [\--check] [<ide options>] <out_dir>**
Brett Wilson796ed472018-07-16 15:11:09 -0700704
705```
706 Generates ninja files from the current tree and puts them in the given output
707 directory.
708
709 The output directory can be a source-repo-absolute path name such as:
710 //out/foo
711 Or it can be a directory relative to the current directory such as:
712 out/foo
713
James Robinson49f59032020-01-08 14:05:51 -0800714 "gn gen --check" is the same as running "gn check". "gn gen --check=system" is
715 the same as running "gn check --check-system". See "gn help check" for
716 documentation on that mode.
Brett Wilson796ed472018-07-16 15:11:09 -0700717
718 See "gn help switches" for the common command-line switches.
719```
720
721#### **IDE options**
722
723```
Harley Li0a9affb2020-06-03 10:38:42 -0400724 GN optionally generates files for IDE. Files won't be overwritten if their
725 contents don't change. Possibilities for
Brett Wilson796ed472018-07-16 15:11:09 -0700726
727 --ide=
728 Generate files for an IDE. Currently supported values:
729 "eclipse" - Eclipse CDT settings file.
730 "vs" - Visual Studio project/solution files.
Aaron Gablea5bcbd72019-12-30 15:08:52 -0800731 (default Visual Studio version: 2019)
Brett Wilson796ed472018-07-16 15:11:09 -0700732 "vs2013" - Visual Studio 2013 project/solution files.
733 "vs2015" - Visual Studio 2015 project/solution files.
734 "vs2017" - Visual Studio 2017 project/solution files.
Sylvain Defresned46f92f2019-04-03 16:14:29 +0200735 "vs2019" - Visual Studio 2019 project/solution files.
Brett Wilson796ed472018-07-16 15:11:09 -0700736 "xcode" - Xcode workspace/solution files.
737 "qtcreator" - QtCreator project files.
738 "json" - JSON file containing target information
739
740 --filters=
741 Semicolon-separated list of label patterns used to limit the set of
742 generated projects (see "gn help label_pattern"). Only matching targets
743 and their dependencies will be included in the solution. Only used for
744 Visual Studio, Xcode and JSON.
745```
746
747#### **Visual Studio Flags**
748
749```
750 --sln=
751 Override default sln file name ("all"). Solution file is written to the
752 root build directory.
753
754 --no-deps
755 Don't include targets dependencies to the solution. Changes the way how
756 --filters option works. Only directly matching targets are included.
757
758 --winsdk=
759 Use the specified Windows 10 SDK version to generate project files.
760 As an example, "10.0.15063.0" can be specified to use Creators Update SDK
761 instead of the default one.
762
763 --ninja-extra-args=
764 This string is passed without any quoting to the ninja invocation
765 command-line. Can be used to configure ninja flags, like "-j".
766```
767
768#### **Xcode Flags**
769
770```
Sylvain Defresnedb55efd2020-06-16 12:23:03 +0200771 --xcode-project=
772 Override defaut Xcode project file name ("all"). The project file is
Brett Wilson796ed472018-07-16 15:11:09 -0700773 written to the root build directory.
774
Sylvain Defresne7b0690e2020-06-26 15:33:01 +0200775 --xcode-build-system=
776 Configure the build system to use for the Xcode project. Supported
777 values are (default to "legacy"):
778 "legacy" - Legacy Build system
779 "new" - New Build System
780
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100781 --ninja-executable=
782 Can be used to specify the ninja executable to use when building.
783
Brett Wilson796ed472018-07-16 15:11:09 -0700784 --ninja-extra-args=
785 This string is passed without any quoting to the ninja invocation
786 command-line. Can be used to configure ninja flags, like "-j".
787
Sylvain Defresnee431b2f2020-11-04 10:49:47 +0100788 --ide-root-target=
Brett Wilson796ed472018-07-16 15:11:09 -0700789 Name of the target corresponding to "All" target in Xcode. If unset,
790 "All" invokes ninja without any target and builds everything.
791```
792
793#### **QtCreator Flags**
794
795```
Sylvain Defresnee431b2f2020-11-04 10:49:47 +0100796 --ide-root-target=
Brett Wilson796ed472018-07-16 15:11:09 -0700797 Name of the root target for which the QtCreator project will be generated
798 to contain files of it and its dependencies. If unset, the whole build
799 graph will be emitted.
800```
801
802#### **Eclipse IDE Support**
803
804```
805 GN DOES NOT generate Eclipse CDT projects. Instead, it generates a settings
806 file which can be imported into an Eclipse CDT project. The XML file contains
807 a list of include paths and defines. Because GN does not generate a full
808 .cproject definition, it is not possible to properly define includes/defines
809 for each file individually. Instead, one set of includes/defines is generated
810 for the entire project. This works fairly well but may still result in a few
811 indexer issues here and there.
812```
813
814#### **Generic JSON Output**
815
816```
817 Dumps target information to a JSON file and optionally invokes a
818 python script on the generated file. See the comments at the beginning
819 of json_project_writer.cc and desc_builder.cc for an overview of the JSON
820 file format.
821
822 --json-file-name=
823 Overrides default file name (project.json) of generated JSON file.
824
825 --json-ide-script=
Harley Li0a9affb2020-06-03 10:38:42 -0400826 Executes python script after the JSON file is generated or updated with
827 new content. Path can be project absolute (//), system absolute (/) or
828 relative, in which case the output directory will be base. Path to
829 generated JSON file will be first argument when invoking script.
Brett Wilson796ed472018-07-16 15:11:09 -0700830
831 --json-ide-script-args=
832 Optional second argument that will passed to executed script.
833```
Julie Hockett11e09912018-07-31 13:15:08 -0700834
835#### **Compilation Database**
836
837```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400838 --export-rust-project
839 Produces a rust-project.json file in the root of the build directory
840 This is used for various tools in the Rust ecosystem allowing for the
841 replay of individual compilations independent of the build system.
842 This is an unstable format and likely to change without warning.
843
Robert Sesek252674d2019-05-10 14:08:52 -0400844 --export-compile-commands[=]
Julie Hockett11e09912018-07-31 13:15:08 -0700845 Produces a compile_commands.json file in the root of the build directory
846 containing an array of “command objects”, where each command object
Robert Sesek252674d2019-05-10 14:08:52 -0400847 specifies one way a translation unit is compiled in the project. If a list
Aaron Wood2d9fd622020-12-10 21:28:19 -0800848 of target_name is supplied, only targets that are reachable from any
849 target in any build file whose name is target_name will be used for
850 “command objects” generation, otherwise all available targets will be used.
851 This is used for various Clang-based tooling, allowing for the replay of
852 individual compilations independent of the build system.
853 e.g. "foo" will match:
854 - "//path/to/src:foo"
855 - "//other/path:foo"
856 - "//foo:foo"
857 and not match:
858 - "//foo:bar"
859
Julie Hockett11e09912018-07-31 13:15:08 -0700860```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800861### **gn help <anything>**
Brett Wilson796ed472018-07-16 15:11:09 -0700862
863```
864 Yo dawg, I heard you like help on your help so I put help on the help in the
865 help.
866
867 You can also use "all" as the parameter to get all help at once.
868```
869
870#### **Switches**
871
872```
873 --markdown
874 Format output in markdown syntax.
875```
876
877#### **Example**
878
879```
880 gn help --markdown all
881 Dump all help to stdout in markdown format.
882```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400883### **gn ls <out_dir> [<label_pattern>] [\--default-toolchain] [\--as=...]**
Brett Wilson796ed472018-07-16 15:11:09 -0700884```
885 [--type=...] [--testonly=...]
886
887 Lists all targets matching the given pattern for the given build directory.
888 By default, only targets in the default toolchain will be matched unless a
889 toolchain is explicitly supplied.
890
891 If the label pattern is unspecified, list all targets. The label pattern is
892 not a general regular expression (see "gn help label_pattern"). If you need
893 more complex expressions, pipe the result through grep.
894```
895
896#### **Options**
897
898```
899 --as=(buildfile|label|output)
900 How to print targets.
901
902 buildfile
903 Prints the build files where the given target was declared as
904 file names.
905 label (default)
906 Prints the label of the target.
907 output
908 Prints the first output file for the target relative to the
909 root build directory.
910
Robert Sesekd0a6f072020-05-15 11:21:22 -0400911 --default-toolchain
912 Normally wildcard targets are matched in all toolchains. This
913 switch makes wildcard labels with no explicit toolchain reference
914 only match targets in the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -0700915
Robert Sesekd0a6f072020-05-15 11:21:22 -0400916 Non-wildcard inputs with no explicit toolchain specification will
917 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -0700918
919 --testonly=(true|false)
920 Restrict outputs to targets with the testonly flag set
921 accordingly. When unspecified, the target's testonly flags are
922 ignored.
923
924 --type=(action|copy|executable|group|loadable_module|shared_library|
925 source_set|static_library)
926 Restrict outputs to targets matching the given type. If
927 unspecified, no filtering will be performed.
928```
929
930#### **Examples**
931
932```
933 gn ls out/Debug
934 Lists all targets in the default toolchain.
935
936 gn ls out/Debug "//base/*"
937 Lists all targets in the directory base and all subdirectories.
938
939 gn ls out/Debug "//base:*"
940 Lists all targets defined in //base/BUILD.gn.
941
942 gn ls out/Debug //base --as=output
943 Lists the build output file for //base:base
944
945 gn ls out/Debug --type=executable
946 Lists all executables produced by the build.
947
948 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
949 Builds all targets in //base and all subdirectories.
Brett Wilson796ed472018-07-16 15:11:09 -0700950```
Joe Armstrongb199e542019-03-21 09:40:08 +0800951### **gn meta**
952
Nate Fischer8ed01d32019-01-08 17:32:01 -0800953```
Joe Armstrongb199e542019-03-21 09:40:08 +0800954 gn meta * --data=[,*]* [--walk=[,*]*]
955 [--rebase=]
Nate Fischer8ed01d32019-01-08 17:32:01 -0800956
957 Lists collected metaresults of all given targets for the given data key(s),
958 collecting metadata dependencies as specified by the given walk key(s).
959
960 See `gn help generated_file` for more information on the walk.
961```
962
963#### **Arguments**
964
965```
966
967 A list of target labels from which to initiate the walk.
968
969 --data
970 A list of keys from which to extract data. In each target walked, its metadata
971 scope is checked for the presence of these keys. If present, the contents of
972 those variable in the scope are appended to the results list.
973
974 --walk (optional)
975 A list of keys from which to control the walk. In each target walked, its
976 metadata scope is checked for the presence of any of these keys. If present,
977 the contents of those variables is checked to ensure that it is a label of
978 a valid dependency of the target and then added to the set of targets to walk.
979 If the empty string ("") is present in any of these keys, all deps and data_deps
980 are added to the walk set.
981
982 --rebase (optional)
983 A destination directory onto which to rebase any paths found. If set, all
984 collected metadata will be rebased onto this path. This option will throw errors
985 if collected metadata is not a list of strings.
986```
987
988#### **Examples**
989
990```
991 gn meta out/Debug "//base/foo" --data=files
992 Lists collected metaresults for the `files` key in the //base/foo:foo
993 target and all of its dependency tree.
994
995 gn meta out/Debug "//base/foo" --data=files --data=other
996 Lists collected metaresults for the `files` and `other` keys in the
997 //base/foo:foo target and all of its dependency tree.
998
999 gn meta out/Debug "//base/foo" --data=files --walk=stop
1000 Lists collected metaresults for the `files` key in the //base/foo:foo
1001 target and all of the dependencies listed in the `stop` key (and so on).
1002
1003 gn meta out/Debug "//base/foo" --data=files --rebase="/"
1004 Lists collected metaresults for the `files` key in the //base/foo:foo
1005 target and all of its dependency tree, rebasing the strings in the `files`
1006 key onto the source directory of the target's declaration relative to "/".
1007```
Hector Dearman5233fa12020-09-11 12:18:21 +01001008### **gn outputs <out_dir> <list of target or file names...>**
Brett Wilson74397092020-03-18 16:34:14 -07001009
1010```
1011 Lists the output files corresponding to the given target(s) or file name(s).
1012 There can be multiple outputs because there can be more than one output
1013 generated by a build step, and there can be more than one toolchain matched.
1014 You can also list multiple inputs which will generate a union of all the
1015 outputs from those inputs.
1016
1017 - The input target/file names are relative to the current directory.
1018
1019 - The output file names are relative to the root build directory.
1020
1021 This command is useful for finding a ninja command that will build only a
1022 portion of the build.
1023```
1024
1025#### **Target outputs**
1026
1027```
1028 If the parameter is a target name that includes a toolchain, it will match
1029 only that target in that toolchain. If no toolchain is specified, it will
1030 match all targets with that name in any toolchain.
1031
1032 The result will be the outputs specified by that target which could be a
1033 library, executable, output of an action, a stamp file, etc.
1034```
1035
1036#### **File outputs**
1037
1038```
1039 If the parameter is a file name it will compute the output for that compile
1040 step for all targets in all toolchains that contain that file as a source
1041 file.
1042
1043 If the source is not compiled (e.g. a header or text file), the command will
1044 produce no output.
1045
1046 If the source is listed as an "input" to a binary target or action will
1047 resolve to that target's outputs.
1048```
1049
1050#### **Example**
1051
1052```
1053 gn outputs out/debug some/directory:some_target
1054 Find the outputs of a given target.
1055
1056 gn outputs out/debug src/project/my_file.cc | xargs ninja -C out/debug
1057 Compiles just the given source file in all toolchains it's referenced in.
1058
1059 git diff --name-only | xargs gn outputs out/x64 | xargs ninja -C out/x64
1060 Compiles all files changed in git.
1061```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001062### **gn path <out_dir> <target_one> <target_two>**
Brett Wilson796ed472018-07-16 15:11:09 -07001063
1064```
1065 Finds paths of dependencies between two targets. Each unique path will be
1066 printed in one group, and groups will be separate by newlines. The two
1067 targets can appear in either order (paths will be found going in either
1068 direction).
1069
1070 By default, a single path will be printed. If there is a path with only
1071 public dependencies, the shortest public path will be printed. Otherwise, the
1072 shortest path using either public or private dependencies will be printed. If
1073 --with-data is specified, data deps will also be considered. If there are
1074 multiple shortest paths, an arbitrary one will be selected.
1075```
1076
1077#### **Interesting paths**
1078
1079```
1080 In a large project, there can be 100's of millions of unique paths between a
1081 very high level and a common low-level target. To make the output more useful
1082 (and terminate in a reasonable time), GN will not revisit sub-paths
1083 previously known to lead to the target.
1084```
1085
1086#### **Options**
1087
1088```
1089 --all
1090 Prints all "interesting" paths found rather than just the first one.
1091 Public paths will be printed first in order of increasing length, followed
1092 by non-public paths in order of increasing length.
1093
1094 --public
1095 Considers only public paths. Can't be used with --with-data.
1096
1097 --with-data
1098 Additionally follows data deps. Without this flag, only public and private
1099 linked deps will be followed. Can't be used with --public.
1100```
1101
1102#### **Example**
1103
1104```
Keir Mierle45611e32019-11-12 11:18:00 -08001105 gn path out/Default //base //gn
Brett Wilson796ed472018-07-16 15:11:09 -07001106```
Joe Armstrongb199e542019-03-21 09:40:08 +08001107### **gn refs**
1108
Brett Wilson796ed472018-07-16 15:11:09 -07001109```
Nico Weberad9eab22020-11-15 22:20:28 -05001110 gn refs (|
1111 [--default-toolchain] [--as=...] [--testonly=...] [--type=...]
Brett Wilson796ed472018-07-16 15:11:09 -07001112
1113 Finds reverse dependencies (which targets reference something). The input is
1114 a list containing:
1115
1116 - Target label: The result will be which targets depend on it.
1117
1118 - Config label: The result will be which targets list the given config in
1119 its "configs" or "public_configs" list.
1120
1121 - Label pattern: The result will be which targets depend on any target
1122 matching the given pattern. Patterns will not match configs. These are not
1123 general regular expressions, see "gn help label_pattern" for details.
1124
1125 - File name: The result will be which targets list the given file in its
1126 "inputs", "sources", "public", "data", or "outputs". Any input that does
1127 not contain wildcards and does not match a target or a config will be
1128 treated as a file.
1129
1130 - Response file: If the input starts with an "@", it will be interpreted as
1131 a path to a file containing a list of labels or file names, one per line.
1132 This allows us to handle long lists of inputs without worrying about
1133 command line limits.
1134```
1135
1136#### **Options**
1137
1138```
1139 --all
1140 When used without --tree, will recurse and display all unique
1141 dependencies of the given targets. For example, if the input is a target,
1142 this will output all targets that depend directly or indirectly on the
1143 input. If the input is a file, this will output all targets that depend
1144 directly or indirectly on that file.
1145
1146 When used with --tree, turns off eliding to show a complete tree.
Brett Wilson796ed472018-07-16 15:11:09 -07001147
1148 --as=(buildfile|label|output)
1149 How to print targets.
1150
1151 buildfile
1152 Prints the build files where the given target was declared as
1153 file names.
1154 label (default)
1155 Prints the label of the target.
1156 output
1157 Prints the first output file for the target relative to the
1158 root build directory.
1159
Robert Sesekd0a6f072020-05-15 11:21:22 -04001160 --default-toolchain
1161 Normally wildcard targets are matched in all toolchains. This
1162 switch makes wildcard labels with no explicit toolchain reference
1163 only match targets in the default toolchain.
1164
1165 Non-wildcard inputs with no explicit toolchain specification will
1166 always match only a target in the default toolchain if one exists.
1167
Brett Wilson796ed472018-07-16 15:11:09 -07001168 -q
1169 Quiet. If nothing matches, don't print any output. Without this option, if
1170 there are no matches there will be an informational message printed which
1171 might interfere with scripts processing the output.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001172
Brett Wilson796ed472018-07-16 15:11:09 -07001173 --testonly=(true|false)
1174 Restrict outputs to targets with the testonly flag set
1175 accordingly. When unspecified, the target's testonly flags are
1176 ignored.
1177
1178 --tree
1179 Outputs a reverse dependency tree from the given target. Duplicates will
1180 be elided. Combine with --all to see a full dependency tree.
1181
1182 Tree output can not be used with the filtering or output flags: --as,
1183 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001184
Brett Wilson796ed472018-07-16 15:11:09 -07001185 --type=(action|copy|executable|group|loadable_module|shared_library|
1186 source_set|static_library)
1187 Restrict outputs to targets matching the given type. If
1188 unspecified, no filtering will be performed.
1189```
1190
1191#### **Examples (target input)**
1192
1193```
Keir Mierle45611e32019-11-12 11:18:00 -08001194 gn refs out/Debug //gn:gn
Brett Wilson796ed472018-07-16 15:11:09 -07001195 Find all targets depending on the given exact target name.
1196
1197 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
1198 Edit all .gn files containing references to //base:i18n
1199
1200 gn refs out/Debug //base --all
1201 List all targets depending directly or indirectly on //base:base.
1202
1203 gn refs out/Debug "//base/*"
1204 List all targets depending directly on any target in //base or
1205 its subdirectories.
1206
1207 gn refs out/Debug "//base:*"
1208 List all targets depending directly on any target in
1209 //base/BUILD.gn.
1210
1211 gn refs out/Debug //base --tree
1212 Print a reverse dependency tree of //base:base
1213```
1214
1215#### **Examples (file input)**
1216
1217```
1218 gn refs out/Debug //base/macros.h
1219 Print target(s) listing //base/macros.h as a source.
1220
1221 gn refs out/Debug //base/macros.h --tree
1222 Display a reverse dependency tree to get to the given file. This
1223 will show how dependencies will reference that file.
1224
1225 gn refs out/Debug //base/macros.h //base/at_exit.h --all
1226 Display all unique targets with some dependency path to a target
1227 containing either of the given files as a source.
1228
1229 gn refs out/Debug //base/macros.h --testonly=true --type=executable
1230 --all --as=output
1231 Display the executable file names of all test executables
1232 potentially affected by a change to the given file.
1233```
1234## Target declarations
1235
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001236### **action**: Declare a target that runs a script a single time.
Brett Wilson796ed472018-07-16 15:11:09 -07001237
1238```
1239 This target type allows you to run a script a single time to produce one or
1240 more output files. If you want to run a script once for each of a set of
1241 input files, see "gn help action_foreach".
1242```
1243
1244#### **Inputs**
1245
1246```
1247 In an action the "sources" and "inputs" are treated the same: they're both
1248 input dependencies on script execution with no special handling. If you want
1249 to pass the sources to your script, you must do so explicitly by including
1250 them in the "args". Note also that this means there is no special handling of
1251 paths since GN doesn't know which of the args are paths and not. You will
1252 want to use rebase_path() to convert paths to be relative to the
1253 root_build_dir.
1254
1255 You can dynamically write input dependencies (for incremental rebuilds if an
1256 input file changes) by writing a depfile when the script is run (see "gn help
1257 depfile"). This is more flexible than "inputs".
1258
1259 If the command line length is very long, you can use response files to pass
1260 args to your script. See "gn help response_file_contents".
1261
1262 It is recommended you put inputs to your script in the "sources" variable,
1263 and stuff like other Python files required to run your script in the "inputs"
1264 variable.
Julie Hockette2a29402018-07-31 10:11:42 -07001265
Brett Wilson796ed472018-07-16 15:11:09 -07001266 The "deps" and "public_deps" for an action will always be
1267 completed before any part of the action is run so it can depend on
1268 the output of previous steps. The "data_deps" will be built if the
1269 action is built, but may not have completed before all steps of the
1270 action are started. This can give additional parallelism in the build
1271 for runtime-only dependencies.
1272```
1273
1274#### **Outputs**
1275
1276```
1277 You should specify files created by your script by specifying them in the
1278 "outputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001279
Brett Wilson796ed472018-07-16 15:11:09 -07001280 The script will be executed with the given arguments with the current
1281 directory being that of the root build directory. If you pass files
1282 to your script, see "gn help rebase_path" for how to convert
1283 file names to be relative to the build directory (file names in the
1284 sources, outputs, and inputs will be all treated as relative to the
1285 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001286
1287 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1288 that Ninja will check the timestamp of the output after the action
1289 completes. If output timestamp is unchanged, the step will be treated
1290 as if it never needed to be rebuilt, potentially eliminating some
1291 downstream steps for incremental builds. Scripts can improve build
1292 performance by taking care not to change the timstamp of the output
1293 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001294```
1295
1296#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001297
Brett Wilson796ed472018-07-16 15:11:09 -07001298```
1299 All output files must be inside the output directory of the build.
1300 You would generally use |$target_out_dir| or |$target_gen_dir| to
1301 reference the output or generated intermediate file directories,
1302 respectively.
1303```
1304
1305#### **Variables**
1306
1307```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001308 args, data, data_deps, depfile, deps, inputs, metadata, outputs*, pool,
Brett Wilson796ed472018-07-16 15:11:09 -07001309 response_file_contents, script*, sources
1310 * = required
1311```
1312
1313#### **Example**
1314
1315```
1316 action("run_this_guy_once") {
1317 script = "doprocessing.py"
1318 sources = [ "my_configuration.txt" ]
1319 outputs = [ "$target_gen_dir/insightful_output.txt" ]
1320
1321 # Our script imports this Python file so we want to rebuild if it changes.
1322 inputs = [ "helper_library.py" ]
1323
1324 # Note that we have to manually pass the sources to our script if the
1325 # script needs them as inputs.
1326 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
1327 rebase_path(sources, root_build_dir)
1328 }
1329```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001330### **action_foreach**: Declare a target that runs a script over a set of files.
Brett Wilson796ed472018-07-16 15:11:09 -07001331
1332```
1333 This target type allows you to run a script once-per-file over a set of
1334 sources. If you want to run a script once that takes many files as input, see
1335 "gn help action".
1336```
1337
1338#### **Inputs**
1339
1340```
1341 The script will be run once per file in the "sources" variable. The "outputs"
1342 variable should specify one or more files with a source expansion pattern in
1343 it (see "gn help source_expansion"). The output file(s) for each script
1344 invocation should be unique. Normally you use "{{source_name_part}}" in each
1345 output file.
1346
1347 If your script takes additional data as input, such as a shared configuration
1348 file or a Python module it uses, those files should be listed in the "inputs"
1349 variable. These files are treated as dependencies of each script invocation.
1350
1351 If the command line length is very long, you can use response files to pass
1352 args to your script. See "gn help response_file_contents".
1353
1354 You can dynamically write input dependencies (for incremental rebuilds if an
1355 input file changes) by writing a depfile when the script is run (see "gn help
1356 depfile"). This is more flexible than "inputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001357
Brett Wilson796ed472018-07-16 15:11:09 -07001358 The "deps" and "public_deps" for an action will always be
1359 completed before any part of the action is run so it can depend on
1360 the output of previous steps. The "data_deps" will be built if the
1361 action is built, but may not have completed before all steps of the
1362 action are started. This can give additional parallelism in the build
1363 for runtime-only dependencies.
1364```
1365
1366#### **Outputs**
Julie Hockette2a29402018-07-31 10:11:42 -07001367
Brett Wilson796ed472018-07-16 15:11:09 -07001368```
1369 The script will be executed with the given arguments with the current
1370 directory being that of the root build directory. If you pass files
1371 to your script, see "gn help rebase_path" for how to convert
1372 file names to be relative to the build directory (file names in the
1373 sources, outputs, and inputs will be all treated as relative to the
1374 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001375
1376 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1377 that Ninja will check the timestamp of the output after the action
1378 completes. If output timestamp is unchanged, the step will be treated
1379 as if it never needed to be rebuilt, potentially eliminating some
1380 downstream steps for incremental builds. Scripts can improve build
1381 performance by taking care not to change the timstamp of the output
1382 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001383```
1384
1385#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001386
Brett Wilson796ed472018-07-16 15:11:09 -07001387```
1388 All output files must be inside the output directory of the build.
1389 You would generally use |$target_out_dir| or |$target_gen_dir| to
1390 reference the output or generated intermediate file directories,
1391 respectively.
1392```
1393
1394#### **Variables**
1395
1396```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001397 args, data, data_deps, depfile, deps, inputs, metadata, outputs*, pool,
Brett Wilson796ed472018-07-16 15:11:09 -07001398 response_file_contents, script*, sources*
1399 * = required
1400```
1401
1402#### **Example**
1403
1404```
1405 # Runs the script over each IDL file. The IDL script will generate both a .cc
1406 # and a .h file for each input.
1407 action_foreach("my_idl") {
1408 script = "idl_processor.py"
1409 sources = [ "foo.idl", "bar.idl" ]
1410
Joe Armstrong73e89072019-05-10 11:23:52 +08001411 # Our script reads this file each time, so we need to list it as a
Brett Wilson796ed472018-07-16 15:11:09 -07001412 # dependency so we can rebuild if it changes.
1413 inputs = [ "my_configuration.txt" ]
1414
1415 # Transformation from source file name to output file names.
1416 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
1417 "$target_gen_dir/{{source_name_part}}.cc" ]
1418
1419 # Note that since "args" is opaque to GN, if you specify paths here, you
1420 # will need to convert it to be relative to the build directory using
1421 # rebase_path().
1422 args = [
1423 "{{source}}",
1424 "-o",
1425 rebase_path(relative_target_gen_dir, root_build_dir) +
1426 "/{{source_name_part}}.h" ]
1427 }
1428```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001429### **bundle_data**: [iOS/macOS] Declare a target without output.
Brett Wilson796ed472018-07-16 15:11:09 -07001430
1431```
1432 This target type allows to declare data that is required at runtime. It is
1433 used to inform "create_bundle" targets of the files to copy into generated
1434 bundle, see "gn help create_bundle" for help.
1435
1436 The target must define a list of files as "sources" and a single "outputs".
1437 If there are multiple files, source expansions must be used to express the
1438 output. The output must reference a file inside of {{bundle_root_dir}}.
1439
1440 This target can be used on all platforms though it is designed only to
1441 generate iOS/macOS bundle. In cross-platform projects, it is advised to put it
1442 behind iOS/macOS conditionals.
1443
1444 See "gn help create_bundle" for more information.
1445```
1446
1447#### **Variables**
1448
1449```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001450 sources*, outputs*, deps, data_deps, metadata, public_deps, visibility
Brett Wilson796ed472018-07-16 15:11:09 -07001451 * = required
1452```
1453
1454#### **Examples**
1455
1456```
1457 bundle_data("icudata") {
1458 sources = [ "sources/data/in/icudtl.dat" ]
1459 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
1460 }
1461
1462 bundle_data("base_unittests_bundle_data]") {
1463 sources = [ "test/data" ]
1464 outputs = [
1465 "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" +
1466 "{{source_file_part}}"
1467 ]
1468 }
1469
1470 bundle_data("material_typography_bundle_data") {
1471 sources = [
1472 "src/MaterialTypography.bundle/Roboto-Bold.ttf",
1473 "src/MaterialTypography.bundle/Roboto-Italic.ttf",
1474 "src/MaterialTypography.bundle/Roboto-Regular.ttf",
1475 "src/MaterialTypography.bundle/Roboto-Thin.ttf",
1476 ]
1477 outputs = [
1478 "{{bundle_resources_dir}}/MaterialTypography.bundle/"
1479 "{{source_file_part}}"
1480 ]
1481 }
1482```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001483### **copy**: Declare a target that copies files.
Brett Wilson796ed472018-07-16 15:11:09 -07001484
1485#### **File name handling**
1486
1487```
1488 All output files must be inside the output directory of the build. You would
1489 generally use |$target_out_dir| or |$target_gen_dir| to reference the output
1490 or generated intermediate file directories, respectively.
1491
1492 Both "sources" and "outputs" must be specified. Sources can include as many
1493 files as you want, but there can only be one item in the outputs list (plural
1494 is used for the name for consistency with other target types).
1495
1496 If there is more than one source file, your output name should specify a
1497 mapping from each source file to an output file name using source expansion
1498 (see "gn help source_expansion"). The placeholders will look like
1499 "{{source_name_part}}", for example.
1500```
1501
1502#### **Examples**
1503
1504```
1505 # Write a rule that copies a checked-in DLL to the output directory.
1506 copy("mydll") {
1507 sources = [ "mydll.dll" ]
1508 outputs = [ "$target_out_dir/mydll.dll" ]
1509 }
1510
1511 # Write a rule to copy several files to the target generated files directory.
1512 copy("myfiles") {
1513 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1514
1515 # Use source expansion to generate output files with the corresponding file
1516 # names in the gen dir. This will just copy each file.
1517 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1518 }
1519```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001520### **create_bundle**: [ios/macOS] Build an iOS or macOS bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07001521
1522```
1523 This target generates an iOS or macOS bundle (which is a directory with a
1524 well-know structure). This target does not define any sources, instead they
1525 are computed from all "bundle_data" target this one depends on transitively
1526 (the recursion stops at "create_bundle" targets).
1527
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001528 The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in
1529 "bundle_data" outputs. The properties are optional but must be defined if any
1530 of the "bundle_data" target use them.
Brett Wilson796ed472018-07-16 15:11:09 -07001531
1532 This target can be used on all platforms though it is designed only to
1533 generate iOS or macOS bundle. In cross-platform projects, it is advised to put
1534 it behind iOS/macOS conditionals.
1535
1536 If a create_bundle is specified as a data_deps for another target, the bundle
1537 is considered a leaf, and its public and private dependencies will not
1538 contribute to any data or data_deps. Required runtime dependencies should be
1539 placed in the bundle. A create_bundle can declare its own explicit data and
1540 data_deps, however.
1541```
1542
1543#### **Code signing**
1544
1545```
1546 Some bundle needs to be code signed as part of the build (on iOS all
1547 application needs to be code signed to run on a device). The code signature
1548 can be configured via the code_signing_script variable.
1549
1550 If set, code_signing_script is the path of a script that invoked after all
1551 files have been moved into the bundle. The script must not change any file in
1552 the bundle, but may add new files.
1553
1554 If code_signing_script is defined, then code_signing_outputs must also be
1555 defined and non-empty to inform when the script needs to be re-run. The
1556 code_signing_args will be passed as is to the script (so path have to be
1557 rebased) and additional inputs may be listed with the variable
1558 code_signing_sources.
1559```
1560
1561#### **Variables**
1562
1563```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001564 bundle_root_dir, bundle_contents_dir, bundle_resources_dir,
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001565 bundle_executable_dir, bundle_deps_filter, deps, data_deps, public_deps,
1566 visibility, product_type, code_signing_args, code_signing_script,
1567 code_signing_sources, code_signing_outputs, xcode_extra_attributes,
1568 xcode_test_application_name, partial_info_plist, metadata
Brett Wilson796ed472018-07-16 15:11:09 -07001569```
1570
1571#### **Example**
1572
1573```
1574 # Defines a template to create an application. On most platform, this is just
1575 # an alias for an "executable" target, but on iOS/macOS, it builds an
1576 # application bundle.
1577 template("app") {
1578 if (!is_ios && !is_mac) {
1579 executable(target_name) {
1580 forward_variables_from(invoker, "*")
1581 }
1582 } else {
1583 app_name = target_name
1584 gen_path = target_gen_dir
1585
1586 action("${app_name}_generate_info_plist") {
1587 script = [ "//build/ios/ios_gen_plist.py" ]
1588 sources = [ "templates/Info.plist" ]
1589 outputs = [ "$gen_path/Info.plist" ]
1590 args = rebase_path(sources, root_build_dir) +
1591 rebase_path(outputs, root_build_dir)
1592 }
1593
1594 bundle_data("${app_name}_bundle_info_plist") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001595 public_deps = [ ":${app_name}_generate_info_plist" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001596 sources = [ "$gen_path/Info.plist" ]
1597 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
1598 }
1599
1600 executable("${app_name}_generate_executable") {
1601 forward_variables_from(invoker, "*", [
1602 "output_name",
1603 "visibility",
1604 ])
1605 output_name =
1606 rebase_path("$gen_path/$app_name", root_build_dir)
1607 }
1608
1609 code_signing =
1610 defined(invoker.code_signing) && invoker.code_signing
1611
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001612 if (!is_ios || !code_signing) {
Brett Wilson796ed472018-07-16 15:11:09 -07001613 bundle_data("${app_name}_bundle_executable") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001614 public_deps = [ ":${app_name}_generate_executable" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001615 sources = [ "$gen_path/$app_name" ]
1616 outputs = [ "{{bundle_executable_dir}}/$app_name" ]
1617 }
1618 }
1619
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001620 create_bundle("$app_name.app") {
Brett Wilson796ed472018-07-16 15:11:09 -07001621 product_type = "com.apple.product-type.application"
1622
1623 if (is_ios) {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001624 bundle_root_dir = "$root_build_dir/$target_name"
Brett Wilson796ed472018-07-16 15:11:09 -07001625 bundle_contents_dir = bundle_root_dir
1626 bundle_resources_dir = bundle_contents_dir
1627 bundle_executable_dir = bundle_contents_dir
Brett Wilson796ed472018-07-16 15:11:09 -07001628
Sylvain Defresne7b0690e2020-06-26 15:33:01 +02001629 xcode_extra_attributes = {
Brett Wilson796ed472018-07-16 15:11:09 -07001630 ONLY_ACTIVE_ARCH = "YES"
1631 DEBUG_INFORMATION_FORMAT = "dwarf"
1632 }
1633 } else {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001634 bundle_root_dir = "$root_build_dir/$target_name"
1635 bundle_contents_dir = "$bundle_root_dir/Contents"
1636 bundle_resources_dir = "$bundle_contents_dir/Resources"
1637 bundle_executable_dir = "$bundle_contents_dir/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07001638 }
1639 deps = [ ":${app_name}_bundle_info_plist" ]
1640 if (is_ios && code_signing) {
1641 deps += [ ":${app_name}_generate_executable" ]
1642 code_signing_script = "//build/config/ios/codesign.py"
1643 code_signing_sources = [
1644 invoker.entitlements_path,
1645 "$target_gen_dir/$app_name",
1646 ]
1647 code_signing_outputs = [
1648 "$bundle_root_dir/$app_name",
1649 "$bundle_root_dir/_CodeSignature/CodeResources",
1650 "$bundle_root_dir/embedded.mobileprovision",
1651 "$target_gen_dir/$app_name.xcent",
1652 ]
1653 code_signing_args = [
1654 "-i=" + ios_code_signing_identity,
1655 "-b=" + rebase_path(
1656 "$target_gen_dir/$app_name", root_build_dir),
1657 "-e=" + rebase_path(
1658 invoker.entitlements_path, root_build_dir),
1659 "-e=" + rebase_path(
1660 "$target_gen_dir/$app_name.xcent", root_build_dir),
1661 rebase_path(bundle_root_dir, root_build_dir),
1662 ]
1663 } else {
1664 deps += [ ":${app_name}_bundle_executable" ]
1665 }
1666 }
1667 }
1668 }
1669```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001670### **executable**: Declare an executable target.
Brett Wilson796ed472018-07-16 15:11:09 -07001671
Julie Hockettce1fa072019-05-07 17:44:37 -07001672#### **Language and compilation**
1673
1674```
1675 The tools and commands used to create this target type will be
1676 determined by the source files in its sources. Targets containing
1677 multiple compiler-incompatible languages are not allowed (e.g. a
1678 target containing both C and C++ sources is acceptable, but a
1679 target containing C and Rust sources is not).
1680```
1681
Brett Wilson796ed472018-07-16 15:11:09 -07001682#### **Variables**
1683
1684```
1685 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1686 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001687 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001688 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07001689 Deps: data_deps, deps, public_deps
1690 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001691 General: check_includes, configs, data, friend, inputs, metadata,
1692 output_name, output_extension, public, sources, testonly,
1693 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001694 Rust variables: aliased_deps, crate_root, crate_name
Nate Fischer8ed01d32019-01-08 17:32:01 -08001695```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001696### **generated_file**: Declare a generated_file target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001697
1698```
1699 Writes data value(s) to disk on resolution. This target type mirrors some
1700 functionality of the write_file() function, but also provides the ability to
1701 collect metadata from its dependencies on resolution rather than writing out
Keir Mierle45611e32019-11-12 11:18:00 -08001702 at parse time.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001703
1704 The `outputs` variable is required to be a list with a single element,
1705 specifying the intended location of the output file.
1706
1707 The `output_conversion` variable specified the format to write the
1708 value. See `gn help output_conversion`.
1709
1710 One of `contents` or `data_keys` must be specified; use of `data` will write
1711 the contents of that value to file, while use of `data_keys` will trigger a
1712 metadata collection walk based on the dependencies of the target and the
1713 optional values of the `rebase` and `walk_keys` variables. See
1714 `gn help metadata`.
1715
1716 Collected metadata, if specified, will be returned in postorder of
1717 dependencies. See the example for details.
1718```
1719
1720#### **Example (metadata collection)**
1721
1722```
1723 Given the following targets defined in //base/BUILD.gn, where A depends on B
1724 and B depends on C and D:
1725
1726 group("a") {
1727 metadata = {
1728 doom_melon = [ "enable" ]
1729 my_files = [ "foo.cpp" ]
1730
Keir Mierle45611e32019-11-12 11:18:00 -08001731 # Note: this is functionally equivalent to not defining `my_barrier`
1732 # at all in this target's metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001733 my_barrier = [ "" ]
1734 }
1735
1736 deps = [ ":b" ]
1737 }
1738
Julie Hockettd69a9c32019-01-23 14:36:18 -08001739 group("b") {
Nate Fischer8ed01d32019-01-08 17:32:01 -08001740 metadata = {
1741 my_files = [ "bar.cpp" ]
1742 my_barrier = [ ":c" ]
1743 }
1744
1745 deps = [ ":c", ":d" ]
1746 }
1747
1748 group("c") {
1749 metadata = {
1750 doom_melon = [ "disable" ]
1751 my_files = [ "baz.cpp" ]
1752 }
1753 }
1754
1755 group("d") {
1756 metadata = {
1757 my_files = [ "missing.cpp" ]
1758 }
1759 }
1760
1761 If the following generated_file target is defined:
1762
1763 generated_file("my_files_metadata") {
1764 outputs = [ "$root_build_dir/my_files.json" ]
1765 data_keys = [ "my_files" ]
1766
1767 deps = [ "//base:a" ]
1768 }
1769
1770 The following will be written to "$root_build_dir/my_files.json" (less the
1771 comments):
1772 [
1773 "baz.cpp", // from //base:c via //base:b
1774 "missing.cpp" // from //base:d via //base:b
1775 "bar.cpp", // from //base:b via //base:a
1776 "foo.cpp", // from //base:a
1777 ]
1778
1779 Alternatively, as an example of using walk_keys, if the following
1780 generated_file target is defined:
1781
1782 generated_file("my_files_metadata") {
1783 outputs = [ "$root_build_dir/my_files.json" ]
1784 data_keys = [ "my_files" ]
1785 walk_keys = [ "my_barrier" ]
1786
1787 deps = [ "//base:a" ]
1788 }
1789
1790 The following will be written to "$root_build_dir/my_files.json" (again less
1791 the comments):
1792 [
1793 "baz.cpp", // from //base:c via //base:b
1794 "bar.cpp", // from //base:b via //base:a
1795 "foo.cpp", // from //base:a
1796 ]
1797
1798 If `rebase` is used in the following generated_file target:
1799
1800 generated_file("my_files_metadata") {
1801 outputs = [ "$root_build_dir/my_files.json" ]
1802 data_keys = [ "my_files" ]
1803 walk_keys = [ "my_barrier" ]
1804 rebase = root_build_dir
1805
1806 deps = [ "//base:a" ]
1807 }
1808
1809 The following will be written to "$root_build_dir/my_files.json" (again less
1810 the comments) (assuming root_build_dir = "//out"):
1811 [
1812 "../base/baz.cpp", // from //base:c via //base:b
1813 "../base/bar.cpp", // from //base:b via //base:a
1814 "../base/foo.cpp", // from //base:a
1815 ]
1816```
1817
1818#### **Variables**
1819
1820```
Julie Hockettd69a9c32019-01-23 14:36:18 -08001821 contents
Nate Fischer8ed01d32019-01-08 17:32:01 -08001822 data_keys
1823 rebase
1824 walk_keys
1825 output_conversion
1826 Deps: data_deps, deps, public_deps
1827 Dependent configs: all_dependent_configs, public_configs
Brett Wilson796ed472018-07-16 15:11:09 -07001828```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001829### **group**: Declare a named group of targets.
Brett Wilson796ed472018-07-16 15:11:09 -07001830
1831```
1832 This target type allows you to create meta-targets that just collect a set of
1833 dependencies into one named target. Groups can additionally specify configs
1834 that apply to their dependents.
1835```
1836
1837#### **Variables**
1838
1839```
1840 Deps: data_deps, deps, public_deps
1841 Dependent configs: all_dependent_configs, public_configs
1842```
1843
1844#### **Example**
1845
1846```
1847 group("all") {
1848 deps = [
1849 "//project:runner",
1850 "//project:unit_tests",
1851 ]
1852 }
1853```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001854### **loadable_module**: Declare a loadable module target.
Brett Wilson796ed472018-07-16 15:11:09 -07001855
1856```
1857 This target type allows you to create an object file that is (and can only
1858 be) loaded and unloaded at runtime.
1859
1860 A loadable module will be specified on the linker line for targets listing
1861 the loadable module in its "deps". If you don't want this (if you don't need
1862 to dynamically load the library at runtime), then you should use a
1863 "shared_library" target type instead.
1864```
1865
Julie Hockettce1fa072019-05-07 17:44:37 -07001866#### **Language and compilation**
1867
1868```
1869 The tools and commands used to create this target type will be
1870 determined by the source files in its sources. Targets containing
1871 multiple compiler-incompatible languages are not allowed (e.g. a
1872 target containing both C and C++ sources is acceptable, but a
1873 target containing C and Rust sources is not).
1874```
1875
Brett Wilson796ed472018-07-16 15:11:09 -07001876#### **Variables**
1877
1878```
1879 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1880 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001881 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001882 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07001883 Deps: data_deps, deps, public_deps
1884 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001885 General: check_includes, configs, data, friend, inputs, metadata,
1886 output_name, output_extension, public, sources, testonly,
1887 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001888 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Julie Hockettce1fa072019-05-07 17:44:37 -07001889```
1890### **rust_library**: Declare a Rust library target.
1891
1892```
1893 A Rust library is an archive containing additional rust-c provided metadata.
1894 These are the files produced by the rustc compiler with the `.rlib`
1895 extension, and are the intermediate step for most Rust-based binaries.
1896```
1897
1898#### **Language and compilation**
1899
1900```
1901 The tools and commands used to create this target type will be
1902 determined by the source files in its sources. Targets containing
1903 multiple compiler-incompatible languages are not allowed (e.g. a
1904 target containing both C and C++ sources is acceptable, but a
1905 target containing C and Rust sources is not).
1906```
1907
1908#### **Variables**
1909
1910```
1911 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1912 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
1913 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001914 rustenv, swiftflags
Julie Hockettce1fa072019-05-07 17:44:37 -07001915 Deps: data_deps, deps, public_deps
1916 Dependent configs: all_dependent_configs, public_configs
1917 General: check_includes, configs, data, friend, inputs, metadata,
1918 output_name, output_extension, public, sources, testonly,
1919 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001920 Rust variables: aliased_deps, crate_root, crate_name
Brett Wilson796ed472018-07-16 15:11:09 -07001921```
Petr Hosekfe36c7c2019-11-26 14:38:08 -08001922### **rust_proc_macro**: Declare a Rust procedural macro target.
1923
1924```
1925 A Rust procedural macro allows creating syntax extensions as execution of a
1926 function. They are compiled as dynamic libraries and used by the compiler at
1927 runtime.
1928
1929 Their use is the same as of other Rust libraries, but their build has some
1930 additional restrictions in terms of supported flags.
1931```
1932
1933#### **Language and compilation**
1934
1935```
1936 The tools and commands used to create this target type will be
1937 determined by the source files in its sources. Targets containing
1938 multiple compiler-incompatible languages are not allowed (e.g. a
1939 target containing both C and C++ sources is acceptable, but a
1940 target containing C and Rust sources is not).
1941```
1942
1943#### **Variables**
1944
1945```
1946 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1947 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
1948 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001949 rustenv, swiftflags
Petr Hosekfe36c7c2019-11-26 14:38:08 -08001950 Deps: data_deps, deps, public_deps
1951 Dependent configs: all_dependent_configs, public_configs
1952 General: check_includes, configs, data, friend, inputs, metadata,
1953 output_name, output_extension, public, sources, testonly,
1954 visibility
1955 Rust variables: aliased_deps, crate_root, crate_name
1956```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001957### **shared_library**: Declare a shared library target.
Brett Wilson796ed472018-07-16 15:11:09 -07001958
1959```
1960 A shared library will be specified on the linker line for targets listing the
1961 shared library in its "deps". If you don't want this (say you dynamically
1962 load the library at runtime), then you should depend on the shared library
1963 via "data_deps" or, on Darwin platforms, use a "loadable_module" target type
1964 instead.
1965```
1966
Julie Hockettce1fa072019-05-07 17:44:37 -07001967#### **Language and compilation**
1968
1969```
1970 The tools and commands used to create this target type will be
1971 determined by the source files in its sources. Targets containing
1972 multiple compiler-incompatible languages are not allowed (e.g. a
1973 target containing both C and C++ sources is acceptable, but a
1974 target containing C and Rust sources is not).
1975```
1976
Brett Wilson796ed472018-07-16 15:11:09 -07001977#### **Variables**
1978
1979```
1980 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1981 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001982 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001983 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07001984 Deps: data_deps, deps, public_deps
1985 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001986 General: check_includes, configs, data, friend, inputs, metadata,
1987 output_name, output_extension, public, sources, testonly,
1988 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001989 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Brett Wilson796ed472018-07-16 15:11:09 -07001990```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001991### **source_set**: Declare a source set target.
Brett Wilson796ed472018-07-16 15:11:09 -07001992
1993```
Robert Sesekd0a6f072020-05-15 11:21:22 -04001994 Only C-language source sets are supported at the moment.
Julie Hockettce1fa072019-05-07 17:44:37 -07001995```
1996
1997#### **C-language source_sets**
1998
1999```
Brett Wilson796ed472018-07-16 15:11:09 -07002000 A source set is a collection of sources that get compiled, but are not linked
2001 to produce any kind of library. Instead, the resulting object files are
2002 implicitly added to the linker line of all targets that depend on the source
2003 set.
2004
2005 In most cases, a source set will behave like a static library, except no
2006 actual library file will be produced. This will make the build go a little
2007 faster by skipping creation of a large static library, while maintaining the
2008 organizational benefits of focused build targets.
2009
2010 The main difference between a source set and a static library is around
2011 handling of exported symbols. Most linkers assume declaring a function
2012 exported means exported from the static library. The linker can then do dead
2013 code elimination to delete code not reachable from exported functions.
2014
2015 A source set will not do this code elimination since there is no link step.
Julie Hockette2a29402018-07-31 10:11:42 -07002016 This allows you to link many source sets into a shared library and have the
Brett Wilson796ed472018-07-16 15:11:09 -07002017 "exported symbol" notation indicate "export from the final shared library and
2018 not from the intermediate targets." There is no way to express this concept
2019 when linking multiple static libraries into a shared library.
2020```
2021
2022#### **Variables**
2023
2024```
2025 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2026 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002027 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02002028 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07002029 Deps: data_deps, deps, public_deps
2030 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002031 General: check_includes, configs, data, friend, inputs, metadata,
2032 output_name, output_extension, public, sources, testonly,
2033 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002034```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002035### **static_library**: Declare a static library target.
Brett Wilson796ed472018-07-16 15:11:09 -07002036
2037```
2038 Make a ".a" / ".lib" file.
2039
2040 If you only need the static library for intermediate results in the build,
2041 you should consider a source_set instead since it will skip the (potentially
2042 slow) step of creating the intermediate library file.
2043```
2044
2045#### **Variables**
2046
2047```
2048 complete_static_lib
2049 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2050 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002051 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02002052 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07002053 Deps: data_deps, deps, public_deps
2054 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002055 General: check_includes, configs, data, friend, inputs, metadata,
2056 output_name, output_extension, public, sources, testonly,
2057 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002058 Rust variables: aliased_deps, crate_root, crate_name
Julie Hockettce1fa072019-05-07 17:44:37 -07002059
2060 The tools and commands used to create this target type will be
2061 determined by the source files in its sources. Targets containing
2062 multiple compiler-incompatible languages are not allowed (e.g. a
2063 target containing both C and C++ sources is acceptable, but a
2064 target containing C and Rust sources is not).
Brett Wilson796ed472018-07-16 15:11:09 -07002065```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002066### **target**: Declare an target with the given programmatic type.
Brett Wilson796ed472018-07-16 15:11:09 -07002067
2068```
2069 target(target_type_string, target_name_string) { ... }
2070
2071 The target() function is a way to invoke a built-in target or template with a
2072 type determined at runtime. This is useful for cases where the type of a
2073 target might not be known statically.
2074
2075 Only templates and built-in target functions are supported for the
2076 target_type_string parameter. Arbitrary functions, configs, and toolchains
2077 are not supported.
2078
2079 The call:
2080 target("source_set", "doom_melon") {
2081 Is equivalent to:
2082 source_set("doom_melon") {
2083```
2084
2085#### **Example**
2086
2087```
2088 if (foo_build_as_shared) {
2089 my_type = "shared_library"
2090 } else {
2091 my_type = "source_set"
2092 }
2093
2094 target(my_type, "foo") {
2095 ...
2096 }
2097```
2098## Buildfile functions
2099
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002100### **assert**: Assert an expression is true at generation time.
Brett Wilson796ed472018-07-16 15:11:09 -07002101
2102```
2103 assert( [, ])
2104
2105 If the condition is false, the build will fail with an error. If the
2106 optional second argument is provided, that string will be printed
2107 with the error message.
2108```
2109
2110#### **Examples**
2111
2112```
2113 assert(is_win)
2114 assert(defined(sources), "Sources must be defined");
2115```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002116### **config**: Defines a configuration object.
Brett Wilson796ed472018-07-16 15:11:09 -07002117
2118```
2119 Configuration objects can be applied to targets and specify sets of compiler
2120 flags, includes, defines, etc. They provide a way to conveniently group sets
2121 of this configuration information.
2122
2123 A config is referenced by its label just like a target.
2124
2125 The values in a config are additive only. If you want to remove a flag you
2126 need to remove the corresponding config that sets it. The final set of flags,
2127 defines, etc. for a target is generated in this order:
2128
2129 1. The values specified directly on the target (rather than using a config.
2130 2. The configs specified in the target's "configs" list, in order.
2131 3. Public_configs from a breadth-first traversal of the dependency tree in
2132 the order that the targets appear in "deps".
2133 4. All dependent configs from a breadth-first traversal of the dependency
2134 tree in the order that the targets appear in "deps".
2135```
2136
Joe Armstrong39413942019-03-15 10:34:03 +08002137#### **More background**
2138
2139```
2140 Configs solve a problem where the build system needs to have a higher-level
2141 understanding of various compiler settings. For example, some compiler flags
2142 have to appear in a certain order relative to each other, some settings like
2143 defines and flags logically go together, and the build system needs to
2144 de-duplicate flags even though raw command-line parameters can't always be
2145 operated on in that way.
2146
2147 The config gives a name to a group of settings that can then be reasoned
2148 about by GN. GN can know that configs with the same label are the same thing
2149 so can be de-duplicated. It allows related settings to be grouped so they
2150 are added or removed as a unit. And it allows targets to refer to settings
2151 with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than
2152 having to hard-coding every compiler's flags each time they are referred to.
2153```
2154
Brett Wilson796ed472018-07-16 15:11:09 -07002155#### **Variables valid in a config definition**
Joe Armstrong39413942019-03-15 10:34:03 +08002156
Brett Wilson796ed472018-07-16 15:11:09 -07002157```
2158 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2159 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002160 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02002161 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07002162 Nested configs: configs
2163```
2164
2165#### **Variables on a target used to apply configs**
2166
2167```
2168 all_dependent_configs, configs, public_configs
2169```
2170
2171#### **Example**
2172
2173```
2174 config("myconfig") {
Joe Armstrongf5ad8072019-05-08 08:42:56 +08002175 include_dirs = [ "include/common" ]
Brett Wilson796ed472018-07-16 15:11:09 -07002176 defines = [ "ENABLE_DOOM_MELON" ]
2177 }
2178
2179 executable("mything") {
2180 configs = [ ":myconfig" ]
2181 }
2182```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002183### **declare_args**: Declare build arguments.
Brett Wilson796ed472018-07-16 15:11:09 -07002184
2185```
2186 Introduces the given arguments into the current scope. If they are not
2187 specified on the command line or in a toolchain's arguments, the default
2188 values given in the declare_args block will be used. However, these defaults
2189 will not override command-line values.
2190
2191 See also "gn help buildargs" for an overview.
2192
2193 The precise behavior of declare args is:
2194
2195 1. The declare_args() block executes. Any variable defined in the enclosing
2196 scope is available for reading, but any variable defined earlier in
2197 the current scope is not (since the overrides haven't been applied yet).
2198
2199 2. At the end of executing the block, any variables set within that scope
Petr Hosek7617b592019-10-02 09:52:21 -07002200 are saved, with the values specified in the block used as the "default value"
2201 for that argument. Once saved, these variables are available for override
2202 via args.gn.
Brett Wilson796ed472018-07-16 15:11:09 -07002203
2204 3. User-defined overrides are applied. Anything set in "gn args" now
2205 overrides any default values. The resulting set of variables is promoted
2206 to be readable from the following code in the file.
2207
2208 This has some ramifications that may not be obvious:
2209
2210 - You should not perform difficult work inside a declare_args block since
2211 this only sets a default value that may be discarded. In particular,
2212 don't use the result of exec_script() to set the default value. If you
2213 want to have a script-defined default, set some default "undefined" value
2214 like [], "", or -1, and after the declare_args block, call exec_script if
2215 the value is unset by the user.
2216
2217 - Because you cannot read the value of a variable defined in the same
2218 block, if you need to make the default value of one arg depend
2219 on the possibly-overridden value of another, write two separate
2220 declare_args() blocks:
2221
2222 declare_args() {
2223 enable_foo = true
2224 }
2225 declare_args() {
2226 # Bar defaults to same user-overridden state as foo.
2227 enable_bar = enable_foo
2228 }
2229```
2230
2231#### **Example**
2232
2233```
2234 declare_args() {
2235 enable_teleporter = true
2236 enable_doom_melon = false
2237 }
2238
2239 If you want to override the (default disabled) Doom Melon:
2240 gn --args="enable_doom_melon=true enable_teleporter=true"
2241 This also sets the teleporter, but it's already defaulted to on so it will
2242 have no effect.
2243```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002244### **defined**: Returns whether an identifier is defined.
Brett Wilson796ed472018-07-16 15:11:09 -07002245
2246```
2247 Returns true if the given argument is defined. This is most useful in
2248 templates to assert that the caller set things up properly.
2249
2250 You can pass an identifier:
2251 defined(foo)
2252 which will return true or false depending on whether foo is defined in the
2253 current scope.
2254
2255 You can also check a named scope:
2256 defined(foo.bar)
2257 which will return true or false depending on whether bar is defined in the
2258 named scope foo. It will throw an error if foo is not defined or is not a
2259 scope.
2260```
2261
2262#### **Example**
2263
2264```
2265 template("mytemplate") {
2266 # To help users call this template properly...
2267 assert(defined(invoker.sources), "Sources must be defined")
2268
2269 # If we want to accept an optional "values" argument, we don't
2270 # want to dereference something that may not be defined.
2271 if (defined(invoker.values)) {
2272 values = invoker.values
2273 } else {
2274 values = "some default value"
2275 }
2276 }
2277```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002278### **exec_script**: Synchronously run a script and return the output.
Brett Wilson796ed472018-07-16 15:11:09 -07002279
2280```
2281 exec_script(filename,
2282 arguments = [],
2283 input_conversion = "",
2284 file_dependencies = [])
2285
2286 Runs the given script, returning the stdout of the script. The build
2287 generation will fail if the script does not exist or returns a nonzero exit
2288 code.
2289
2290 The current directory when executing the script will be the root build
2291 directory. If you are passing file names, you will want to use the
2292 rebase_path() function to make file names relative to this path (see "gn help
2293 rebase_path").
Gary Miguelce7fa362018-09-17 12:48:17 -07002294
2295 The default script interpreter is Python ("python" on POSIX, "python.exe" or
2296 "python.bat" on Windows). This can be configured by the script_executable
2297 variable, see "gn help dotfile".
Brett Wilson796ed472018-07-16 15:11:09 -07002298```
2299
2300#### **Arguments**:
2301
2302```
2303 filename:
Gary Miguelce7fa362018-09-17 12:48:17 -07002304 File name of script to execute. Non-absolute names will be treated as
2305 relative to the current build file.
Brett Wilson796ed472018-07-16 15:11:09 -07002306
2307 arguments:
2308 A list of strings to be passed to the script as arguments. May be
2309 unspecified or the empty list which means no arguments.
2310
2311 input_conversion:
Julie Hockett81ee1962019-05-13 11:24:42 -07002312 Controls how the file is read and parsed. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07002313
2314 If unspecified, defaults to the empty string which causes the script
2315 result to be discarded. exec script will return None.
2316
2317 dependencies:
2318 (Optional) A list of files that this script reads or otherwise depends
2319 on. These dependencies will be added to the build result such that if any
2320 of them change, the build will be regenerated and the script will be
2321 re-run.
2322
2323 The script itself will be an implicit dependency so you do not need to
2324 list it.
2325```
2326
2327#### **Example**
2328
2329```
2330 all_lines = exec_script(
2331 "myscript.py", [some_input], "list lines",
2332 [ rebase_path("data_file.txt", root_build_dir) ])
2333
2334 # This example just calls the script with no arguments and discards the
2335 # result.
2336 exec_script("//foo/bar/myscript.py")
2337```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01002338### **filter_exclude**: Remove values that match a set of patterns.
2339
2340```
2341 filter_exclude(values, exclude_patterns)
2342
2343 The argument values must be a list of strings.
2344
2345 The argument exclude_patterns must be a list of file patterns (see
2346 "gn help file_pattern"). Any elements in values matching at least one
2347 of those patterns will be excluded.
2348```
2349
2350#### **Examples**
2351```
2352 values = [ "foo.cc", "foo.h", "foo.proto" ]
2353 result = filter_exclude(values, [ "*.proto" ])
2354 # result will be [ "foo.cc", "foo.h" ]
2355```
2356### **filter_include**: Remove values that do not match a set of patterns.
2357
2358```
2359 filter_include(values, include_patterns)
2360
2361 The argument values must be a list of strings.
2362
2363 The argument include_patterns must be a list of file patterns (see
2364 "gn help file_pattern"). Only elements from values matching at least
2365 one of the pattern will be included.
2366```
2367
2368#### **Examples**
2369```
2370 values = [ "foo.cc", "foo.h", "foo.proto" ]
2371 result = filter_include(values, [ "*.proto" ])
2372 # result will be [ "foo.proto" ]
2373```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002374### **foreach**: Iterate over a list.
Brett Wilson796ed472018-07-16 15:11:09 -07002375
2376```
2377 foreach(, ) {
2378
2379 }
2380
2381 Executes the loop contents block over each item in the list, assigning the
2382 loop_var to each item in sequence. The will be a copy so assigning
2383 to it will not mutate the list. The loop will iterate over a copy of
2384 so mutating it inside the loop will not affect iteration.
2385
2386 The block does not introduce a new scope, so that variable assignments inside
2387 the loop will be visible once the loop terminates.
2388
2389 The loop variable will temporarily shadow any existing variables with the
2390 same name for the duration of the loop. After the loop terminates the loop
2391 variable will no longer be in scope, and the previous value (if any) will be
2392 restored.
2393```
2394
2395#### **Example**
2396
2397```
2398 mylist = [ "a", "b", "c" ]
2399 foreach(i, mylist) {
2400 print(i)
2401 }
2402
2403 Prints:
2404 a
2405 b
2406 c
2407```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002408### **forward_variables_from**: Copies variables from a different scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002409
2410```
2411 forward_variables_from(from_scope, variable_list_or_star,
2412 variable_to_not_forward_list = [])
2413
2414 Copies the given variables from the given scope to the local scope if they
2415 exist. This is normally used in the context of templates to use the values of
2416 variables defined in the template invocation to a template-defined target.
2417
2418 The variables in the given variable_list will be copied if they exist in the
2419 given scope or any enclosing scope. If they do not exist, nothing will happen
2420 and they be left undefined in the current scope.
2421
2422 As a special case, if the variable_list is a string with the value of "*",
2423 all variables from the given scope will be copied. "*" only copies variables
2424 set directly on the from_scope, not enclosing ones. Otherwise it would
2425 duplicate all global variables.
2426
2427 When an explicit list of variables is supplied, if the variable exists in the
2428 current (destination) scope already, an error will be thrown. If "*" is
2429 specified, variables in the current scope will be clobbered (the latter is
2430 important because most targets have an implicit configs list, which means it
2431 wouldn't work at all if it didn't clobber).
2432
Brett Wilson796ed472018-07-16 15:11:09 -07002433 If variables_to_not_forward_list is non-empty, then it must contains a list
2434 of variable names that will not be forwarded. This is mostly useful when
2435 variable_list_or_star has a value of "*".
2436```
2437
2438#### **Examples**
2439
2440```
Andrew Grieve57a964c2018-09-14 10:50:56 -04002441 # forward_variables_from(invoker, ["foo"])
2442 # is equivalent to:
2443 assert(!defined(foo))
2444 if (defined(invoker.foo)) {
2445 foo = invoker.foo
2446 }
2447
Brett Wilson796ed472018-07-16 15:11:09 -07002448 # This is a common action template. It would invoke a script with some given
2449 # parameters, and wants to use the various types of deps and the visibility
2450 # from the invoker if it's defined. It also injects an additional dependency
2451 # to all targets.
2452 template("my_test") {
2453 action(target_name) {
2454 forward_variables_from(invoker, [ "data_deps", "deps",
Julie Hockette2a29402018-07-31 10:11:42 -07002455 "public_deps", "visibility"])
Brett Wilson796ed472018-07-16 15:11:09 -07002456 # Add our test code to the dependencies.
2457 # "deps" may or may not be defined at this point.
2458 if (defined(deps)) {
2459 deps += [ "//tools/doom_melon" ]
2460 } else {
2461 deps = [ "//tools/doom_melon" ]
2462 }
2463 }
2464 }
2465
Julie Hockette2a29402018-07-31 10:11:42 -07002466 # This is a template around a target whose type depends on a global variable.
2467 # It forwards all values from the invoker.
Brett Wilson796ed472018-07-16 15:11:09 -07002468 template("my_wrapper") {
2469 target(my_wrapper_target_type, target_name) {
2470 forward_variables_from(invoker, "*")
2471 }
2472 }
2473
2474 # A template that wraps another. It adds behavior based on one
2475 # variable, and forwards all others to the nested target.
2476 template("my_ios_test_app") {
2477 ios_test_app(target_name) {
2478 forward_variables_from(invoker, "*", ["test_bundle_name"])
2479 if (!defined(extra_substitutions)) {
2480 extra_substitutions = []
2481 }
2482 extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ]
2483 }
2484 }
2485```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002486### **get_label_info**: Get an attribute from a target's label.
Brett Wilson796ed472018-07-16 15:11:09 -07002487
2488```
2489 get_label_info(target_label, what)
2490
2491 Given the label of a target, returns some attribute of that target. The
2492 target need not have been previously defined in the same file, since none of
2493 the attributes depend on the actual target definition, only the label itself.
2494
2495 See also "gn help get_target_outputs".
2496```
2497
2498#### **Possible values for the "what" parameter**
2499
2500```
2501 "name"
2502 The short name of the target. This will match the value of the
2503 "target_name" variable inside that target's declaration. For the label
2504 "//foo/bar:baz" this will return "baz".
2505
2506 "dir"
2507 The directory containing the target's definition, with no slash at the
2508 end. For the label "//foo/bar:baz" this will return "//foo/bar".
2509
2510 "target_gen_dir"
2511 The generated file directory for the target. This will match the value of
2512 the "target_gen_dir" variable when inside that target's declaration.
2513
2514 "root_gen_dir"
2515 The root of the generated file tree for the target. This will match the
2516 value of the "root_gen_dir" variable when inside that target's
2517 declaration.
2518
2519 "target_out_dir
2520 The output directory for the target. This will match the value of the
2521 "target_out_dir" variable when inside that target's declaration.
2522
2523 "root_out_dir"
2524 The root of the output file tree for the target. This will match the
2525 value of the "root_out_dir" variable when inside that target's
2526 declaration.
2527
2528 "label_no_toolchain"
2529 The fully qualified version of this label, not including the toolchain.
2530 For the input ":bar" it might return "//foo:bar".
2531
2532 "label_with_toolchain"
2533 The fully qualified version of this label, including the toolchain. For
2534 the input ":bar" it might return "//foo:bar(//toolchain:x64)".
2535
2536 "toolchain"
2537 The label of the toolchain. This will match the value of the
2538 "current_toolchain" variable when inside that target's declaration.
2539```
2540
2541#### **Examples**
2542
2543```
2544 get_label_info(":foo", "name")
2545 # Returns string "foo".
2546
2547 get_label_info("//foo/bar:baz", "target_gen_dir")
2548 # Returns string "//out/Debug/gen/foo/bar".
2549```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002550### **get_path_info**: Extract parts of a file or directory name.
Brett Wilson796ed472018-07-16 15:11:09 -07002551
2552```
2553 get_path_info(input, what)
2554
2555 The first argument is either a string representing a file or directory name,
2556 or a list of such strings. If the input is a list the return value will be a
2557 list containing the result of applying the rule to each item in the input.
2558```
2559
2560#### **Possible values for the "what" parameter**
2561
2562```
2563 "file"
2564 The substring after the last slash in the path, including the name and
2565 extension. If the input ends in a slash, the empty string will be
2566 returned.
2567 "foo/bar.txt" => "bar.txt"
2568 "bar.txt" => "bar.txt"
2569 "foo/" => ""
2570 "" => ""
2571
2572 "name"
2573 The substring of the file name not including the extension.
2574 "foo/bar.txt" => "bar"
2575 "foo/bar" => "bar"
2576 "foo/" => ""
2577
2578 "extension"
2579 The substring following the last period following the last slash, or the
2580 empty string if not found. The period is not included.
2581 "foo/bar.txt" => "txt"
2582 "foo/bar" => ""
2583
2584 "dir"
2585 The directory portion of the name, not including the slash.
2586 "foo/bar.txt" => "foo"
2587 "//foo/bar" => "//foo"
2588 "foo" => "."
2589
2590 The result will never end in a slash, so if the resulting is empty, the
2591 system ("/") or source ("//") roots, a "." will be appended such that it
2592 is always legal to append a slash and a filename and get a valid path.
2593
2594 "out_dir"
2595 The output file directory corresponding to the path of the given file,
2596 not including a trailing slash.
2597 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
2598
2599 "gen_dir"
2600 The generated file directory corresponding to the path of the given file,
2601 not including a trailing slash.
2602 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
2603
2604 "abspath"
2605 The full absolute path name to the file or directory. It will be resolved
2606 relative to the current directory, and then the source- absolute version
2607 will be returned. If the input is system- absolute, the same input will
2608 be returned.
2609 "foo/bar.txt" => "//mydir/foo/bar.txt"
2610 "foo/" => "//mydir/foo/"
2611 "//foo/bar" => "//foo/bar" (already absolute)
2612 "/usr/include" => "/usr/include" (already absolute)
2613
2614 If you want to make the path relative to another directory, or to be
2615 system-absolute, see rebase_path().
2616```
2617
2618#### **Examples**
2619```
2620 sources = [ "foo.cc", "foo.h" ]
2621 result = get_path_info(source, "abspath")
2622 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
2623
2624 result = get_path_info("//foo/bar/baz.cc", "dir")
2625 # result will be "//foo/bar"
2626
2627 # Extract the source-absolute directory name,
Joe Armstrong39413942019-03-15 10:34:03 +08002628 result = get_path_info(get_path_info(path, "dir"), "abspath")
Brett Wilson796ed472018-07-16 15:11:09 -07002629```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002630### **get_target_outputs**: [file list] Get the list of outputs from a target.
Brett Wilson796ed472018-07-16 15:11:09 -07002631
2632```
2633 get_target_outputs(target_label)
2634
2635 Returns a list of output files for the named target. The named target must
2636 have been previously defined in the current file before this function is
2637 called (it can't reference targets in other files because there isn't a
2638 defined execution order, and it obviously can't reference targets that are
2639 defined after the function call).
2640
Julie Hockettce1fa072019-05-07 17:44:37 -07002641 Only copy, generated_file, and action targets are supported. The outputs from
2642 binary targets will depend on the toolchain definition which won't
2643 necessarily have been loaded by the time a given line of code has run, and
2644 source sets and groups have no useful output file.
Brett Wilson796ed472018-07-16 15:11:09 -07002645```
2646
2647#### **Return value**
2648
2649```
2650 The names in the resulting list will be absolute file paths (normally like
2651 "//out/Debug/bar.exe", depending on the build directory).
2652
Julie Hockettce1fa072019-05-07 17:44:37 -07002653 action, copy, and generated_file targets: this will just return the files
2654 specified in the "outputs" variable of the target.
Brett Wilson796ed472018-07-16 15:11:09 -07002655
2656 action_foreach targets: this will return the result of applying the output
2657 template to the sources (see "gn help source_expansion"). This will be the
2658 same result (though with guaranteed absolute file paths), as
2659 process_file_template will return for those inputs (see "gn help
2660 process_file_template").
2661
Brett Wilson796ed472018-07-16 15:11:09 -07002662 source sets and groups: this will return a list containing the path of the
RJ Ascani843b5002020-11-04 14:49:23 -08002663 "stamp" file that Ninja will produce once all outputs are generated. This
Brett Wilson796ed472018-07-16 15:11:09 -07002664 probably isn't very useful.
2665```
2666
2667#### **Example**
2668
2669```
2670 # Say this action generates a bunch of C source files.
2671 action_foreach("my_action") {
2672 sources = [ ... ]
2673 outputs = [ ... ]
2674 }
2675
2676 # Compile the resulting source files into a source set.
2677 source_set("my_lib") {
2678 sources = get_target_outputs(":my_action")
2679 }
2680```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002681### **getenv**: Get an environment variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002682
2683```
2684 value = getenv(env_var_name)
2685
2686 Returns the value of the given environment variable. If the value is not
2687 found, it will try to look up the variable with the "opposite" case (based on
2688 the case of the first letter of the variable), but is otherwise
2689 case-sensitive.
2690
2691 If the environment variable is not found, the empty string will be returned.
2692 Note: it might be nice to extend this if we had the concept of "none" in the
2693 language to indicate lookup failure.
2694```
2695
2696#### **Example**
2697
2698```
2699 home_dir = getenv("HOME")
2700```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002701### **import**: Import a file into the current scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002702
2703```
2704 The import command loads the rules and variables resulting from executing the
2705 given file into the current scope.
2706
2707 By convention, imported files are named with a .gni extension.
2708
2709 An import is different than a C++ "include". The imported file is executed in
2710 a standalone environment from the caller of the import command. The results
2711 of this execution are cached for other files that import the same .gni file.
2712
2713 Note that you can not import a BUILD.gn file that's otherwise used in the
2714 build. Files must either be imported or implicitly loaded as a result of deps
2715 rules, but not both.
2716
2717 The imported file's scope will be merged with the scope at the point import
2718 was called. If there is a conflict (both the current scope and the imported
2719 file define some variable or rule with the same name but different value), a
2720 runtime error will be thrown. Therefore, it's good practice to minimize the
2721 stuff that an imported file defines.
2722
2723 Variables and templates beginning with an underscore '_' are considered
2724 private and will not be imported. Imported files can use such variables for
2725 internal computation without affecting other files.
2726```
2727
2728#### **Examples**
2729
2730```
2731 import("//build/rules/idl_compilation_rule.gni")
2732
2733 # Looks in the current directory.
2734 import("my_vars.gni")
2735```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002736### **not_needed**: Mark variables from scope as not needed.
Brett Wilson796ed472018-07-16 15:11:09 -07002737
2738```
2739 not_needed(variable_list_or_star, variable_to_ignore_list = [])
2740 not_needed(from_scope, variable_list_or_star,
2741 variable_to_ignore_list = [])
2742
2743 Mark the variables in the current or given scope as not needed, which means
2744 you will not get an error about unused variables for these. The
2745 variable_to_ignore_list allows excluding variables from "all matches" if
2746 variable_list_or_star is "*".
2747```
2748
2749#### **Example**
2750
2751```
2752 not_needed("*", [ "config" ])
2753 not_needed([ "data_deps", "deps" ])
2754 not_needed(invoker, "*", [ "config" ])
2755 not_needed(invoker, [ "data_deps", "deps" ])
2756```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002757### **pool**: Defines a pool object.
Brett Wilson796ed472018-07-16 15:11:09 -07002758
2759```
2760 Pool objects can be applied to a tool to limit the parallelism of the
2761 build. This object has a single property "depth" corresponding to
2762 the number of tasks that may run simultaneously.
2763
2764 As the file containing the pool definition may be executed in the
2765 context of more than one toolchain it is recommended to specify an
2766 explicit toolchain when defining and referencing a pool.
2767
Julie Hockette2a29402018-07-31 10:11:42 -07002768 A pool named "console" defined in the root build file represents Ninja's
2769 console pool. Targets using this pool will have access to the console's
2770 stdin and stdout, and output will not be buffered. This special pool must
2771 have a depth of 1. Pools not defined in the root must not be named "console".
2772 The console pool can only be defined for the default toolchain.
2773 Refer to the Ninja documentation on the console pool for more info.
2774
Brett Wilson796ed472018-07-16 15:11:09 -07002775 A pool is referenced by its label just like a target.
2776```
2777
2778#### **Variables**
2779
2780```
2781 depth*
2782 * = required
2783```
2784
2785#### **Example**
2786
2787```
2788 if (current_toolchain == default_toolchain) {
2789 pool("link_pool") {
2790 depth = 1
2791 }
2792 }
2793
2794 toolchain("toolchain") {
2795 tool("link") {
2796 command = "..."
Petr Hosekadd1bb12019-09-11 14:39:38 -07002797 pool = ":link_pool($default_toolchain)"
Brett Wilson796ed472018-07-16 15:11:09 -07002798 }
2799 }
2800```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002801### **print**: Prints to the console.
Brett Wilson796ed472018-07-16 15:11:09 -07002802
2803```
2804 Prints all arguments to the console separated by spaces. A newline is
2805 automatically appended to the end.
2806
2807 This function is intended for debugging. Note that build files are run in
2808 parallel so you may get interleaved prints. A buildfile may also be executed
2809 more than once in parallel in the context of different toolchains so the
2810 prints from one file may be duplicated or
2811 interleaved with itself.
2812```
2813
2814#### **Examples**
2815
2816```
2817 print("Hello world")
2818
2819 print(sources, deps)
2820```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002821### **process_file_template**: Do template expansion over a list of files.
Brett Wilson796ed472018-07-16 15:11:09 -07002822
2823```
2824 process_file_template(source_list, template)
2825
2826 process_file_template applies a template list to a source file list,
2827 returning the result of applying each template to each source. This is
2828 typically used for computing output file names from input files.
2829
2830 In most cases, get_target_outputs() will give the same result with shorter,
2831 more maintainable code. This function should only be used when that function
2832 can't be used (like there's no target or the target is defined in another
2833 build file).
2834```
2835
2836#### **Arguments**
2837
2838```
2839 The source_list is a list of file names.
2840
2841 The template can be a string or a list. If it is a list, multiple output
2842 strings are generated for each input.
2843
2844 The template should contain source expansions to which each name in the
2845 source list is applied. See "gn help source_expansion".
2846```
2847
2848#### **Example**
2849
2850```
2851 sources = [
2852 "foo.idl",
2853 "bar.idl",
2854 ]
2855 myoutputs = process_file_template(
2856 sources,
2857 [ "$target_gen_dir/{{source_name_part}}.cc",
2858 "$target_gen_dir/{{source_name_part}}.h" ])
2859
2860 The result in this case will be:
2861 [ "//out/Debug/foo.cc"
2862 "//out/Debug/foo.h"
2863 "//out/Debug/bar.cc"
2864 "//out/Debug/bar.h" ]
2865```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002866### **read_file**: Read a file into a variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002867
2868```
2869 read_file(filename, input_conversion)
2870
2871 Whitespace will be trimmed from the end of the file. Throws an error if the
2872 file can not be opened.
2873```
2874
2875#### **Arguments**
2876
2877```
2878 filename
2879 Filename to read, relative to the build file.
2880
2881 input_conversion
Julie Hockett81ee1962019-05-13 11:24:42 -07002882 Controls how the file is read and parsed. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07002883```
2884
2885#### **Example**
2886
2887```
2888 lines = read_file("foo.txt", "list lines")
2889```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002890### **rebase_path**: Rebase a file or directory to another location.
Brett Wilson796ed472018-07-16 15:11:09 -07002891
2892```
2893 converted = rebase_path(input,
2894 new_base = "",
2895 current_base = ".")
2896
2897 Takes a string argument representing a file name, or a list of such strings
2898 and converts it/them to be relative to a different base directory.
2899
2900 When invoking the compiler or scripts, GN will automatically convert sources
2901 and include directories to be relative to the build directory. However, if
2902 you're passing files directly in the "args" array or doing other manual
2903 manipulations where GN doesn't know something is a file name, you will need
2904 to convert paths to be relative to what your tool is expecting.
2905
2906 The common case is to use this to convert paths relative to the current
2907 directory to be relative to the build directory (which will be the current
2908 directory when executing scripts).
2909
2910 If you want to convert a file path to be source-absolute (that is, beginning
2911 with a double slash like "//foo/bar"), you should use the get_path_info()
2912 function. This function won't work because it will always make relative
2913 paths, and it needs to support making paths relative to the source root, so
Robert Sesekd0a6f072020-05-15 11:21:22 -04002914 it can't also generate source-absolute paths without more special-cases.
Brett Wilson796ed472018-07-16 15:11:09 -07002915```
2916
2917#### **Arguments**
2918
2919```
2920 input
Robert Sesekd0a6f072020-05-15 11:21:22 -04002921 A string or list of strings representing file or directory names. These
Brett Wilson796ed472018-07-16 15:11:09 -07002922 can be relative paths ("foo/bar.txt"), system absolute paths
2923 ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt").
2924
2925 new_base
2926 The directory to convert the paths to be relative to. This can be an
2927 absolute path or a relative path (which will be treated as being relative
2928 to the current BUILD-file's directory).
2929
2930 As a special case, if new_base is the empty string (the default), all
2931 paths will be converted to system-absolute native style paths with system
2932 path separators. This is useful for invoking external programs.
2933
2934 current_base
2935 Directory representing the base for relative paths in the input. If this
2936 is not an absolute path, it will be treated as being relative to the
2937 current build file. Use "." (the default) to convert paths from the
2938 current BUILD-file's directory.
2939```
2940
2941#### **Return value**
2942
2943```
2944 The return value will be the same type as the input value (either a string or
2945 a list of strings). All relative and source-absolute file names will be
2946 converted to be relative to the requested output System-absolute paths will
2947 be unchanged.
2948
2949 Whether an output path will end in a slash will match whether the
2950 corresponding input path ends in a slash. It will return "." or "./"
2951 (depending on whether the input ends in a slash) to avoid returning empty
2952 strings. This means if you want a root path ("//" or "/") not ending in a
2953 slash, you can add a dot ("//.").
2954```
2955
2956#### **Example**
2957
2958```
2959 # Convert a file in the current directory to be relative to the build
2960 # directory (the current dir when executing compilers and scripts).
2961 foo = rebase_path("myfile.txt", root_build_dir)
2962 # might produce "../../project/myfile.txt".
2963
2964 # Convert a file to be system absolute:
2965 foo = rebase_path("myfile.txt")
2966 # Might produce "D:\\source\\project\\myfile.txt" on Windows or
2967 # "/home/you/source/project/myfile.txt" on Linux.
2968
2969 # Typical usage for converting to the build directory for a script.
2970 action("myscript") {
2971 # Don't convert sources, GN will automatically convert these to be relative
2972 # to the build directory when it constructs the command line for your
2973 # script.
2974 sources = [ "foo.txt", "bar.txt" ]
2975
2976 # Extra file args passed manually need to be explicitly converted
2977 # to be relative to the build directory:
2978 args = [
2979 "--data",
2980 rebase_path("//mything/data/input.dat", root_build_dir),
2981 "--rel",
2982 rebase_path("relative_path.txt", root_build_dir)
2983 ] + rebase_path(sources, root_build_dir)
2984 }
2985```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002986### **set_default_toolchain**: Sets the default toolchain name.
Brett Wilson796ed472018-07-16 15:11:09 -07002987
2988```
2989 set_default_toolchain(toolchain_label)
2990
2991 The given label should identify a toolchain definition (see "gn help
2992 toolchain"). This toolchain will be used for all targets unless otherwise
2993 specified.
2994
2995 This function is only valid to call during the processing of the build
2996 configuration file. Since the build configuration file is processed
2997 separately for each toolchain, this function will be a no-op when called
2998 under any non-default toolchains.
2999
3000 For example, the default toolchain should be appropriate for the current
3001 environment. If the current environment is 32-bit and somebody references a
3002 target with a 64-bit toolchain, we wouldn't want processing of the build
3003 config file for the 64-bit toolchain to reset the default toolchain to
3004 64-bit, we want to keep it 32-bits.
3005```
3006
3007#### **Argument**
3008
3009```
3010 toolchain_label
3011 Toolchain name.
3012```
3013
3014#### **Example**
3015
3016```
3017 # Set default toolchain only has an effect when run in the context of the
3018 # default toolchain. Pick the right one according to the current CPU
3019 # architecture.
3020 if (target_cpu == "x64") {
3021 set_default_toolchain("//toolchains:64")
3022 } else if (target_cpu == "x86") {
3023 set_default_toolchain("//toolchains:32")
3024 }
3025```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003026### **set_defaults**: Set default values for a target type.
Brett Wilson796ed472018-07-16 15:11:09 -07003027
3028```
3029 set_defaults() { }
3030
3031 Sets the default values for a given target type. Whenever target_type_name is
3032 seen in the future, the values specified in set_default's block will be
3033 copied into the current scope.
3034
3035 When the target type is used, the variable copying is very strict. If a
3036 variable with that name is already in scope, the build will fail with an
3037 error.
3038
3039 set_defaults can be used for built-in target types ("executable",
3040 "shared_library", etc.) and custom ones defined via the "template" command.
3041 It can be called more than once and the most recent call in any scope will
3042 apply, but there is no way to refer to the previous defaults and modify them
3043 (each call to set_defaults must supply a complete list of all defaults it
3044 wants). If you want to share defaults, store them in a separate variable.
3045```
3046
3047#### **Example**
3048
3049```
3050 set_defaults("static_library") {
3051 configs = [ "//tools/mything:settings" ]
3052 }
3053
Nico Webere49cb722018-08-28 13:10:29 -04003054 static_library("mylib") {
Brett Wilson796ed472018-07-16 15:11:09 -07003055 # The configs will be auto-populated as above. You can remove it if
3056 # you don't want the default for a particular default:
3057 configs -= [ "//tools/mything:settings" ]
3058 }
3059```
Sylvain Defresnead0a7ad2020-10-15 15:03:44 +02003060### **set_sources_assignment_filter**: Deprecated feature.
Brett Wilson796ed472018-07-16 15:11:09 -07003061
3062```
Sylvain Defresnead0a7ad2020-10-15 15:03:44 +02003063 This feature is deprecated. It will be removed once all usages have been
3064 removed. Only supports a single argument that needs to be an empty list.
Brett Wilson796ed472018-07-16 15:11:09 -07003065```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003066### **split_list**: Splits a list into N different sub-lists.
Brett Wilson796ed472018-07-16 15:11:09 -07003067
3068```
3069 result = split_list(input, n)
3070
3071 Given a list and a number N, splits the list into N sub-lists of
3072 approximately equal size. The return value is a list of the sub-lists. The
3073 result will always be a list of size N. If N is greater than the number of
3074 elements in the input, it will be padded with empty lists.
3075
3076 The expected use is to divide source files into smaller uniform chunks.
3077```
3078
3079#### **Example**
3080
3081```
3082 The code:
3083 mylist = [1, 2, 3, 4, 5, 6]
3084 print(split_list(mylist, 3))
3085
3086 Will print:
3087 [[1, 2], [3, 4], [5, 6]
3088```
Keir Mierle6ae63302019-11-08 23:02:18 -08003089### **string_join**: Concatenates a list of strings with a separator.
3090
3091```
3092 result = string_join(separator, strings)
3093
3094 Concatenate a list of strings with intervening occurrences of separator.
3095```
3096
3097#### **Examples**
3098
3099```
3100 string_join("", ["a", "b", "c"]) --> "abc"
3101 string_join("|", ["a", "b", "c"]) --> "a|b|c"
3102 string_join(", ", ["a", "b", "c"]) --> "a, b, c"
3103 string_join("s", ["", ""]) --> "s"
3104```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003105### **string_replace**: Replaces substring in the given string.
Petr Hosekc0744ed2018-08-04 16:13:59 -07003106
3107```
3108 result = string_replace(str, old, new[, max])
3109
3110 Returns a copy of the string str in which the occurrences of old have been
3111 replaced with new, optionally restricting the number of replacements. The
3112 replacement is performed sequentially, so if new contains old, it won't be
3113 replaced.
3114```
3115
3116#### **Example**
3117
3118```
3119 The code:
3120 mystr = "Hello, world!"
3121 print(string_replace(mystr, "world", "GN"))
3122
3123 Will print:
3124 Hello, GN!
3125```
Keir Mierle6ae63302019-11-08 23:02:18 -08003126### **string_split**: Split string into a list of strings.
3127
3128```
3129 result = string_split(str[, sep])
3130
3131 Split string into all substrings separated by separator and returns a list
3132 of the substrings between those separators.
3133
3134 If the separator argument is omitted, the split is by any whitespace, and
3135 any leading/trailing whitespace is ignored; similar to Python's str.split().
3136```
3137
3138#### **Examples without a separator (split on whitespace)**:
3139
3140```
3141 string_split("") --> []
3142 string_split("a") --> ["a"]
3143 string_split(" aa bb") --> ["aa", "bb"]
3144```
3145
3146#### **Examples with a separator (split on separators)**:
3147
3148```
3149 string_split("", "|") --> [""]
3150 string_split(" a b ", " ") --> ["", "", "a", "b", "", ""]
3151 string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"]
3152```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003153### **template**: Define a template rule.
Brett Wilson796ed472018-07-16 15:11:09 -07003154
3155```
3156 A template defines a custom name that acts like a function. It provides a way
3157 to add to the built-in target types.
3158
3159 The template() function is used to declare a template. To invoke the
3160 template, just use the name of the template like any other target type.
3161
3162 Often you will want to declare your template in a special file that other
3163 files will import (see "gn help import") so your template rule can be shared
3164 across build files.
3165```
3166
3167#### **Variables and templates**:
3168
3169```
3170 When you call template() it creates a closure around all variables currently
3171 in scope with the code in the template block. When the template is invoked,
3172 the closure will be executed.
3173
3174 When the template is invoked, the code in the caller is executed and passed
3175 to the template code as an implicit "invoker" variable. The template uses
3176 this to read state out of the invoking code.
3177
3178 One thing explicitly excluded from the closure is the "current directory"
3179 against which relative file names are resolved. The current directory will be
3180 that of the invoking code, since typically that code specifies the file
3181 names. This means all files internal to the template should use absolute
3182 names.
3183
3184 A template will typically forward some or all variables from the invoking
3185 scope to a target that it defines. Often, such variables might be optional.
3186 Use the pattern:
3187
3188 if (defined(invoker.deps)) {
3189 deps = invoker.deps
3190 }
3191
3192 The function forward_variables_from() provides a shortcut to forward one or
3193 more or possibly all variables in this manner:
3194
3195 forward_variables_from(invoker, ["deps", "public_deps"])
3196```
3197
3198#### **Target naming**
3199
3200```
3201 Your template should almost always define a built-in target with the name the
3202 template invoker specified. For example, if you have an IDL template and
3203 somebody does:
3204 idl("foo") {...
3205 you will normally want this to expand to something defining a source_set or
3206 static_library named "foo" (among other things you may need). This way, when
3207 another target specifies a dependency on "foo", the static_library or
3208 source_set will be linked.
3209
3210 It is also important that any other targets your template expands to have
3211 unique names, or you will get collisions.
3212
3213 Access the invoking name in your template via the implicit "target_name"
3214 variable. This should also be the basis for how other targets that a template
3215 expands to ensure uniqueness.
3216
3217 A typical example would be a template that defines an action to generate some
3218 source files, and a source_set to compile that source. Your template would
3219 name the source_set "target_name" because that's what you want external
3220 targets to depend on to link your code. And you would name the action
3221 something like "${target_name}_action" to make it unique. The source set
3222 would have a dependency on the action to make it run.
3223```
3224
3225#### **Overriding builtin targets**
3226
3227```
3228 You can use template to redefine a built-in target in which case your template
3229 takes a precedence over the built-in one. All uses of the target from within
3230 the template definition will refer to the built-in target which makes it
3231 possible to extend the behavior of the built-in target:
3232
3233 template("shared_library") {
3234 shared_library(shlib) {
Julie Hockette2a29402018-07-31 10:11:42 -07003235 forward_variables_from(invoker, "*")
Brett Wilson796ed472018-07-16 15:11:09 -07003236 ...
3237 }
3238 }
3239```
3240
3241#### **Example of defining a template**
3242
3243```
3244 template("my_idl") {
3245 # Be nice and help callers debug problems by checking that the variables
3246 # the template requires are defined. This gives a nice message rather than
3247 # giving the user an error about an undefined variable in the file defining
3248 # the template
3249 #
3250 # You can also use defined() to give default values to variables
3251 # unspecified by the invoker.
3252 assert(defined(invoker.sources),
3253 "Need sources in $target_name listing the idl files.")
3254
3255 # Name of the intermediate target that does the code gen. This must
3256 # incorporate the target name so it's unique across template
3257 # instantiations.
3258 code_gen_target_name = target_name + "_code_gen"
3259
3260 # Intermediate target to convert IDL to C source. Note that the name is
3261 # based on the name the invoker of the template specified. This way, each
3262 # time the template is invoked we get a unique intermediate action name
3263 # (since all target names are in the global scope).
3264 action_foreach(code_gen_target_name) {
3265 # Access the scope defined by the invoker via the implicit "invoker"
3266 # variable.
3267 sources = invoker.sources
3268
3269 # Note that we need an absolute path for our script file name. The
3270 # current directory when executing this code will be that of the invoker
3271 # (this is why we can use the "sources" directly above without having to
3272 # rebase all of the paths). But if we need to reference a script relative
3273 # to the template file, we'll need to use an absolute path instead.
3274 script = "//tools/idl/idl_code_generator.py"
3275
3276 # Tell GN how to expand output names given the sources.
3277 # See "gn help source_expansion" for more.
3278 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
3279 "$target_gen_dir/{{source_name_part}}.h" ]
3280 }
3281
3282 # Name the source set the same as the template invocation so instancing
3283 # this template produces something that other targets can link to in their
3284 # deps.
3285 source_set(target_name) {
3286 # Generates the list of sources, we get these from the action_foreach
3287 # above.
3288 sources = get_target_outputs(":$code_gen_target_name")
3289
3290 # This target depends on the files produced by the above code gen target.
3291 deps = [ ":$code_gen_target_name" ]
3292 }
3293 }
3294```
3295
3296#### **Example of invoking the resulting template**
3297
3298```
3299 # This calls the template code above, defining target_name to be
3300 # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly
3301 # brackets.
3302 my_idl("foo_idl_files") {
3303 # Goes into the template as "invoker.sources".
3304 sources = [ "foo.idl", "bar.idl" ]
3305 }
3306
3307 # Here is a target that depends on our template.
3308 executable("my_exe") {
3309 # Depend on the name we gave the template call above. Internally, this will
3310 # produce a dependency from executable to the source_set inside the
3311 # template (since it has this name), which will in turn depend on the code
3312 # gen action.
3313 deps = [ ":foo_idl_files" ]
3314 }
3315```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003316### **tool**: Specify arguments to a toolchain tool.
Brett Wilson796ed472018-07-16 15:11:09 -07003317
3318#### **Usage**
3319
3320```
3321 tool() {
3322
3323 }
3324```
3325
3326#### **Tool types**
3327
3328```
3329 Compiler tools:
3330 "cc": C compiler
3331 "cxx": C++ compiler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003332 "cxx_module": C++ compiler used for Clang .modulemap files
Brett Wilson796ed472018-07-16 15:11:09 -07003333 "objc": Objective C compiler
3334 "objcxx": Objective C++ compiler
3335 "rc": Resource compiler (Windows .rc files)
3336 "asm": Assembler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003337 "swift": Swift compiler driver
Brett Wilson796ed472018-07-16 15:11:09 -07003338
3339 Linker tools:
3340 "alink": Linker for static libraries (archives)
3341 "solink": Linker for shared libraries
3342 "link": Linker for executables
3343
3344 Other tools:
3345 "stamp": Tool for creating stamp files
3346 "copy": Tool to copy files.
3347 "action": Defaults for actions
3348
3349 Platform specific tools:
3350 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
3351 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
Julie Hockettce1fa072019-05-07 17:44:37 -07003352
3353 Rust tools:
Petr Hosek7617b592019-10-02 09:52:21 -07003354 "rust_bin": Tool for compiling Rust binaries
3355 "rust_cdylib": Tool for compiling C-compatible dynamic libraries.
3356 "rust_dylib": Tool for compiling Rust dynamic libraries.
3357 "rust_macro": Tool for compiling Rust procedural macros.
3358 "rust_rlib": Tool for compiling Rust libraries.
3359 "rust_staticlib": Tool for compiling Rust static libraries.
Brett Wilson796ed472018-07-16 15:11:09 -07003360```
3361
3362#### **Tool variables**
3363
3364```
3365 command [string with substitutions]
3366 Valid for: all tools except "action" (required)
3367
3368 The command to run.
3369
Joe Armstrong23d22bc2019-06-28 15:31:03 +08003370 command_launcher [string]
3371 Valid for: all tools except "action" (optional)
3372
3373 The prefix with which to launch the command (e.g. the path to a Goma or
3374 CCache compiler launcher).
3375
3376 Note that this prefix will not be included in the compilation database or
3377 IDE files generated from the build.
3378
Brett Wilson796ed472018-07-16 15:11:09 -07003379 default_output_dir [string with substitutions]
3380 Valid for: linker tools
3381
3382 Default directory name for the output file relative to the
3383 root_build_dir. It can contain other substitution patterns. This will
3384 be the default value for the {{output_dir}} expansion (discussed below)
3385 but will be overridden by the "output_dir" variable in a target, if one
3386 is specified.
3387
3388 GN doesn't do anything with this string other than pass it along,
3389 potentially with target-specific overrides. It is the tool's job to use
3390 the expansion so that the files will be in the right place.
3391
3392 default_output_extension [string]
3393 Valid for: linker tools
3394
3395 Extension for the main output of a linkable tool. It includes the
3396 leading dot. This will be the default value for the
3397 {{output_extension}} expansion (discussed below) but will be overridden
3398 by by the "output extension" variable in a target, if one is specified.
3399 Empty string means no extension.
3400
3401 GN doesn't actually do anything with this extension other than pass it
3402 along, potentially with target-specific overrides. One would typically
3403 use the {{output_extension}} value in the "outputs" to read this value.
3404
3405 Example: default_output_extension = ".exe"
3406
3407 depfile [string with substitutions]
3408 Valid for: compiler tools (optional)
3409
3410 If the tool can write ".d" files, this specifies the name of the
3411 resulting file. These files are used to list header file dependencies
3412 (or other implicit input dependencies) that are discovered at build
3413 time. See also "depsformat".
3414
3415 Example: depfile = "{{output}}.d"
3416
3417 depsformat [string]
3418 Valid for: compiler tools (when depfile is specified)
3419
3420 Format for the deps outputs. This is either "gcc" or "msvc". See the
3421 ninja documentation for "deps" for more information.
3422
3423 Example: depsformat = "gcc"
3424
3425 description [string with substitutions, optional]
3426 Valid for: all tools
3427
3428 What to print when the command is run.
3429
3430 Example: description = "Compiling {{source}}"
3431
Julie Hockettce1fa072019-05-07 17:44:37 -07003432 exe_output_extension [string, optional, rust tools only]
3433 rlib_output_extension [string, optional, rust tools only]
3434 dylib_output_extension [string, optional, rust tools only]
3435 cdylib_output_extension [string, optional, rust tools only]
Petr Hosekfe36c7c2019-11-26 14:38:08 -08003436 rust_proc_macro_output_extension [string, optional, rust tools only]
Julie Hockettce1fa072019-05-07 17:44:37 -07003437 Valid for: Rust tools
3438
3439 These specify the default tool output for each of the crate types.
3440 The default is empty for executables, shared, and static libraries and
3441 ".rlib" for rlibs. Note that the Rust compiler complains with an error
3442 if external crates do not take the form `lib.rlib` or
3443 `lib.`, where `` is `.so`,
3444 `.dylib`, or `.dll` as appropriate for the platform.
3445
Brett Wilson796ed472018-07-16 15:11:09 -07003446 lib_switch [string, optional, link tools only]
3447 lib_dir_switch [string, optional, link tools only]
3448 Valid for: Linker tools except "alink"
3449
3450 These strings will be prepended to the libraries and library search
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003451 directories, respectively, because linkers differ on how to specify
3452 them.
3453
Brett Wilson796ed472018-07-16 15:11:09 -07003454 If you specified:
3455 lib_switch = "-l"
3456 lib_dir_switch = "-L"
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003457 then the "{{libs}}" expansion for
3458 [ "freetype", "expat" ]
3459 would be
3460 "-lfreetype -lexpat".
3461
3462 framework_switch [string, optional, link tools only]
Robert Sesekd0a6f072020-05-15 11:21:22 -04003463 weak_framework_switch [string, optional, link tools only]
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003464 framework_dir_switch [string, optional, link tools only]
3465 Valid for: Linker tools
3466
3467 These strings will be prepended to the frameworks and framework search
3468 path directories, respectively, because linkers differ on how to specify
3469 them.
3470
3471 If you specified:
3472 framework_switch = "-framework "
Robert Sesekd0a6f072020-05-15 11:21:22 -04003473 weak_framework_switch = "-weak_framework "
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003474 framework_dir_switch = "-F"
Robert Sesekd0a6f072020-05-15 11:21:22 -04003475 and:
3476 framework_dirs = [ "$root_out_dir" ]
3477 frameworks = [ "UIKit.framework", "Foo.framework" ]
3478 weak_frameworks = [ "MediaPlayer.framework" ]
3479 would be:
3480 "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer"
Brett Wilson796ed472018-07-16 15:11:09 -07003481
Sylvain Defresne89e64252020-08-07 13:01:06 +02003482 swiftmodule_switch [string, optional, link tools only]
3483 Valid for: Linker tools except "alink"
3484
3485 The string will be prependend to the path to the .swiftmodule files
3486 that are embedded in the linker output.
3487
3488 If you specified:
3489 swiftmodule_swift = "-Wl,-add_ast_path,"
3490 then the "{{swiftmodules}}" expansion for
3491 [ "obj/foo/Foo.swiftmodule" ]
3492 would be
3493 "-Wl,-add_ast_path,obj/foo/Foo.swiftmodule"
3494
Brett Wilson796ed472018-07-16 15:11:09 -07003495 outputs [list of strings with substitutions]
3496 Valid for: Linker and compiler tools (required)
3497
3498 An array of names for the output files the tool produces. These are
3499 relative to the build output directory. There must always be at least
3500 one output file. There can be more than one output (a linker might
3501 produce a library and an import library, for example).
3502
3503 This array just declares to GN what files the tool will produce. It is
3504 your responsibility to specify the tool command that actually produces
3505 these files.
3506
3507 If you specify more than one output for shared library links, you
3508 should consider setting link_output, depend_output, and
3509 runtime_outputs.
3510
3511 Example for a compiler tool that produces .obj files:
3512 outputs = [
3513 "{{source_out_dir}}/{{source_name_part}}.obj"
3514 ]
3515
3516 Example for a linker tool that produces a .dll and a .lib. The use of
3517 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
3518 the target to override these values.
3519 outputs = [
Nico Weberad9eab22020-11-15 22:20:28 -05003520 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Brett Wilson796ed472018-07-16 15:11:09 -07003521 "{{output_dir}}/{{target_output_name}}.lib",
3522 ]
3523
Sylvain Defresne89e64252020-08-07 13:01:06 +02003524 partial_outputs [list of strings with substitutions]
3525 Valid for: "swift" only
3526
3527 An array of names for the partial outputs the tool produces. These
3528 are relative to the build output directory. The expansion will be
3529 evaluated for each file listed in the "sources" of the target.
3530
3531 This is used to deal with whole module optimization, allowing to
3532 list one object file per source file when whole module optimization
3533 is disabled.
3534
Brett Wilson796ed472018-07-16 15:11:09 -07003535 pool [label, optional]
3536 Valid for: all tools (optional)
3537
3538 Label of the pool to use for the tool. Pools are used to limit the
3539 number of tasks that can execute concurrently during the build.
3540
3541 See also "gn help pool".
3542
3543 link_output [string with substitutions]
3544 depend_output [string with substitutions]
3545 Valid for: "solink" only (optional)
3546
3547 These two files specify which of the outputs from the solink tool
3548 should be used for linking and dependency tracking. These should match
3549 entries in the "outputs". If unspecified, the first item in the
3550 "outputs" array will be used for all. See "Separate linking and
3551 dependencies for shared libraries" below for more.
3552
3553 On Windows, where the tools produce a .dll shared library and a .lib
3554 import library, you will want the first two to be the import library
3555 and the third one to be the .dll file. On Linux, if you're not doing
3556 the separate linking/dependency optimization, all of these should be
3557 the .so output.
3558
3559 output_prefix [string]
3560 Valid for: Linker tools (optional)
3561
3562 Prefix to use for the output name. Defaults to empty. This prefix will
3563 be prepended to the name of the target (or the output_name if one is
3564 manually specified for it) if the prefix is not already there. The
3565 result will show up in the {{output_name}} substitution pattern.
3566
3567 Individual targets can opt-out of the output prefix by setting:
3568 output_prefix_override = true
3569 (see "gn help output_prefix_override").
3570
3571 This is typically used to prepend "lib" to libraries on
3572 Posix systems:
3573 output_prefix = "lib"
3574
3575 precompiled_header_type [string]
3576 Valid for: "cc", "cxx", "objc", "objcxx"
3577
3578 Type of precompiled headers. If undefined or the empty string,
3579 precompiled headers will not be used for this tool. Otherwise use "gcc"
3580 or "msvc".
3581
3582 For precompiled headers to be used for a given target, the target (or a
3583 config applied to it) must also specify a "precompiled_header" and, for
3584 "msvc"-style headers, a "precompiled_source" value. If the type is
3585 "gcc", then both "precompiled_header" and "precompiled_source" must
3586 resolve to the same file, despite the different formats required for
3587 each."
3588
3589 See "gn help precompiled_header" for more.
3590
3591 restat [boolean]
3592 Valid for: all tools (optional, defaults to false)
3593
3594 Requests that Ninja check the file timestamp after this tool has run to
3595 determine if anything changed. Set this if your tool has the ability to
3596 skip writing output if the output file has not changed.
3597
3598 Normally, Ninja will assume that when a tool runs the output be new and
3599 downstream dependents must be rebuild. When this is set to trye, Ninja
3600 can skip rebuilding downstream dependents for input changes that don't
3601 actually affect the output.
3602
3603 Example:
3604 restat = true
3605
3606 rspfile [string with substitutions]
3607 Valid for: all tools except "action" (optional)
3608
3609 Name of the response file. If empty, no response file will be
3610 used. See "rspfile_content".
3611
3612 rspfile_content [string with substitutions]
3613 Valid for: all tools except "action" (required when "rspfile" is used)
3614
3615 The contents to be written to the response file. This may include all
3616 or part of the command to send to the tool which allows you to get
3617 around OS command-line length limits.
3618
3619 This example adds the inputs and libraries to a response file, but
3620 passes the linker flags directly on the command line:
3621 tool("link") {
3622 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
3623 rspfile = "{{output}}.rsp"
Adrian Taylorac6cc952020-04-26 20:49:18 -07003624 rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}"
Brett Wilson796ed472018-07-16 15:11:09 -07003625 }
3626
3627 runtime_outputs [string list with substitutions]
3628 Valid for: linker tools
3629
3630 If specified, this list is the subset of the outputs that should be
3631 added to runtime deps (see "gn help runtime_deps"). By default (if
3632 runtime_outputs is empty or unspecified), it will be the link_output.
3633```
3634
3635#### **Expansions for tool variables**
3636
3637```
3638 All paths are relative to the root build directory, which is the current
3639 directory for running all tools. These expansions are available to all tools:
3640
3641 {{label}}
3642 The label of the current target. This is typically used in the
3643 "description" field for link tools. The toolchain will be omitted from
3644 the label for targets in the default toolchain, and will be included
3645 for targets in other toolchains.
3646
3647 {{label_name}}
3648 The short name of the label of the target. This is the part after the
3649 colon. For "//foo/bar:baz" this will be "baz". Unlike
3650 {{target_output_name}}, this is not affected by the "output_prefix" in
3651 the tool or the "output_name" set on the target.
3652
Scott Graham501b49a2020-08-05 13:03:37 -07003653 {{label_no_toolchain}}
3654 The label of the current target, never including the toolchain
3655 (otherwise, this is identical to {{label}}). This is used as the module
3656 name when using .modulemap files.
3657
Brett Wilson796ed472018-07-16 15:11:09 -07003658 {{output}}
3659 The relative path and name of the output(s) of the current build step.
3660 If there is more than one output, this will expand to a list of all of
3661 them. Example: "out/base/my_file.o"
3662
3663 {{target_gen_dir}}
3664 {{target_out_dir}}
3665 The directory of the generated file and output directories,
3666 respectively, for the current target. There is no trailing slash. See
3667 also {{output_dir}} for linker tools. Example: "out/base/test"
3668
3669 {{target_output_name}}
3670 The short name of the current target with no path information, or the
3671 value of the "output_name" variable if one is specified in the target.
3672 This will include the "output_prefix" if any. See also {{label_name}}.
3673
3674 Example: "libfoo" for the target named "foo" and an output prefix for
3675 the linker tool of "lib".
3676
3677 Compiler tools have the notion of a single input and a single output, along
3678 with a set of compiler-specific flags. The following expansions are
3679 available:
3680
3681 {{asmflags}}
3682 {{cflags}}
3683 {{cflags_c}}
3684 {{cflags_cc}}
3685 {{cflags_objc}}
3686 {{cflags_objcc}}
3687 {{defines}}
3688 {{include_dirs}}
3689 Strings correspond that to the processed flags/defines/include
3690 directories specified for the target.
3691 Example: "--enable-foo --enable-bar"
3692
3693 Defines will be prefixed by "-D" and include directories will be
3694 prefixed by "-I" (these work with Posix tools as well as Microsoft
3695 ones).
3696
Scott Graham484245b2020-08-11 10:58:50 -07003697 {{module_deps}}
3698 {{module_deps_no_self}}
3699 Strings that correspond to the flags necessary to depend upon the Clang
3700 modules referenced by the current target. The "_no_self" version doesn't
3701 include the module for the current target, and can be used to compile
3702 the pcm itself.
3703
Brett Wilson796ed472018-07-16 15:11:09 -07003704 {{source}}
3705 The relative path and name of the current input file.
3706 Example: "../../base/my_file.cc"
3707
3708 {{source_file_part}}
3709 The file part of the source including the extension (with no directory
3710 information).
3711 Example: "foo.cc"
3712
3713 {{source_name_part}}
3714 The filename part of the source file with no directory or extension.
3715 Example: "foo"
3716
3717 {{source_gen_dir}}
3718 {{source_out_dir}}
3719 The directory in the generated file and output directories,
3720 respectively, for the current input file. If the source file is in the
3721 same directory as the target is declared in, they will will be the same
3722 as the "target" versions above. Example: "gen/base/test"
3723
Julie Hockette2a29402018-07-31 10:11:42 -07003724 Linker tools have multiple inputs and (potentially) multiple outputs. The
Brett Wilson796ed472018-07-16 15:11:09 -07003725 static library tool ("alink") is not considered a linker tool. The following
3726 expansions are available:
3727
3728 {{inputs}}
3729 {{inputs_newline}}
3730 Expands to the inputs to the link step. This will be a list of object
3731 files and static libraries.
3732 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
3733
3734 The "_newline" version will separate the input files with newlines
3735 instead of spaces. This is useful in response files: some linkers can
3736 take a "-filelist" flag which expects newline separated files, and some
3737 Microsoft tools have a fixed-sized buffer for parsing each line of a
3738 response file.
3739
3740 {{ldflags}}
3741 Expands to the processed set of ldflags and library search paths
3742 specified for the target.
3743 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
3744
3745 {{libs}}
3746 Expands to the list of system libraries to link to. Each will be
3747 prefixed by the "lib_switch".
3748
Brett Wilson796ed472018-07-16 15:11:09 -07003749 Example: "-lfoo -lbar"
3750
3751 {{output_dir}}
3752 The value of the "output_dir" variable in the target, or the the value
3753 of the "default_output_dir" value in the tool if the target does not
3754 override the output directory. This will be relative to the
3755 root_build_dir and will not end in a slash. Will be "." for output to
3756 the root_build_dir.
3757
3758 This is subtly different than {{target_out_dir}} which is defined by GN
3759 based on the target's path and not overridable. {{output_dir}} is for
3760 the final output, {{target_out_dir}} is generally for object files and
3761 other outputs.
3762
3763 Usually {{output_dir}} would be defined in terms of either
3764 {{target_out_dir}} or {{root_out_dir}}
3765
3766 {{output_extension}}
3767 The value of the "output_extension" variable in the target, or the
3768 value of the "default_output_extension" value in the tool if the target
3769 does not specify an output extension.
3770 Example: ".so"
3771
3772 {{solibs}}
3773 Extra libraries from shared library dependencies not specified in the
3774 {{inputs}}. This is the list of link_output files from shared libraries
3775 (if the solink tool specifies a "link_output" variable separate from
3776 the "depend_output").
3777
3778 These should generally be treated the same as libs by your tool.
3779
3780 Example: "libfoo.so libbar.so"
3781
Adrian Taylorac6cc952020-04-26 20:49:18 -07003782 {{rlibs}}
3783 Any Rust .rlibs which need to be linked into a final C++ target.
3784 These should be treated as {{inputs}} except that sometimes
3785 they might have different linker directives applied.
3786
3787 Example: "obj/foo/libfoo.rlib"
3788
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003789 {{frameworks}}
3790 Shared libraries packaged as framework bundle. This is principally
3791 used on Apple's platforms (macOS and iOS). All name must be ending
3792 with ".framework" suffix; the suffix will be stripped when expanding
Robert Sesekd0a6f072020-05-15 11:21:22 -04003793 {{frameworks}} and each item will be preceded by "-framework" or
3794 "-weak_framework".
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003795
Sylvain Defresne89e64252020-08-07 13:01:06 +02003796 {{swiftmodules}}
3797 Swift .swiftmodule files that needs to be embedded into the binary.
3798 This is necessary to correctly link with object generated by the
3799 Swift compiler (the .swiftmodule file cannot be embedded in object
3800 files directly). Those will be prefixed with "swiftmodule_switch"
3801 value.
3802
Brett Wilson796ed472018-07-16 15:11:09 -07003803 The static library ("alink") tool allows {{arflags}} plus the common tool
3804 substitutions.
3805
3806 The copy tool allows the common compiler/linker substitutions, plus
3807 {{source}} which is the source of the copy. The stamp tool allows only the
3808 common tool substitutions.
3809
3810 The copy_bundle_data and compile_xcassets tools only allows the common tool
3811 substitutions. Both tools are required to create iOS/macOS bundles and need
3812 only be defined on those platforms.
3813
3814 The copy_bundle_data tool will be called with one source and needs to copy
3815 (optionally optimizing the data representation) to its output. It may be
3816 called with a directory as input and it needs to be recursively copied.
3817
3818 The compile_xcassets tool will be called with one or more source (each an
3819 asset catalog) that needs to be compiled to a single output. The following
3820 substitutions are available:
3821
3822 {{inputs}}
3823 Expands to the list of .xcassets to use as input to compile the asset
3824 catalog.
3825
3826 {{bundle_product_type}}
3827 Expands to the product_type of the bundle that will contain the
3828 compiled asset catalog. Usually corresponds to the product_type
3829 property of the corresponding create_bundle target.
3830
3831 {{bundle_partial_info_plist}}
3832 Expands to the path to the partial Info.plist generated by the
3833 assets catalog compiler. Usually based on the target_name of
3834 the create_bundle target.
Julie Hockettce1fa072019-05-07 17:44:37 -07003835
Harley Li0a9affb2020-06-03 10:38:42 -04003836 {{xcasset_compiler_flags}}
3837 Expands to the list of flags specified in corresponding
3838 create_bundle target.
3839
Sylvain Defresne89e64252020-08-07 13:01:06 +02003840 The Swift tool has multiple input and outputs. It must have exactly one
3841 output of .swiftmodule type, but can have one or more object file outputs,
3842 in addition to other type of ouputs. The following expansions are available:
3843
3844 {{module_name}}
3845 Expands to the string representing the module name of target under
3846 compilation (see "module_name" variable).
3847
3848 {{module_dirs}}
3849 Expands to the list of -I for the target Swift module search
3850 path computed from target dependencies.
3851
3852 {{swiftflags}}
3853 Expands to the list of strings representing Swift compiler flags.
3854
Julie Hockettce1fa072019-05-07 17:44:37 -07003855 Rust tools have the notion of a single input and a single output, along
3856 with a set of compiler-specific flags. The following expansions are
3857 available:
3858
3859 {{crate_name}}
3860 Expands to the string representing the crate name of target under
3861 compilation.
3862
3863 {{crate_type}}
3864 Expands to the string representing the type of crate for the target
3865 under compilation.
3866
3867 {{externs}}
3868 Expands to the list of --extern flags needed to include addition Rust
3869 libraries in this target. Includes any specified renamed dependencies.
3870
Julie Hockettce1fa072019-05-07 17:44:37 -07003871 {{rustdeps}}
3872 Expands to the list of -Ldependency= strings needed to compile
3873 this target.
3874
3875 {{rustenv}}
3876 Expands to the list of environment variables.
3877
3878 {{rustflags}}
3879 Expands to the list of strings representing Rust compiler flags.
Brett Wilson796ed472018-07-16 15:11:09 -07003880```
3881
3882#### **Separate linking and dependencies for shared libraries**
3883
3884```
3885 Shared libraries are special in that not all changes to them require that
3886 dependent targets be re-linked. If the shared library is changed but no
3887 imports or exports are different, dependent code needn't be relinked, which
3888 can speed up the build.
3889
3890 If your link step can output a list of exports from a shared library and
3891 writes the file only if the new one is different, the timestamp of this file
3892 can be used for triggering re-links, while the actual shared library would be
3893 used for linking.
3894
3895 You will need to specify
3896 restat = true
3897 in the linker tool to make this work, so Ninja will detect if the timestamp
3898 of the dependency file has changed after linking (otherwise it will always
3899 assume that running a command updates the output):
3900
3901 tool("solink") {
3902 command = "..."
3903 outputs = [
3904 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Nico Weberad9eab22020-11-15 22:20:28 -05003905 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC",
Brett Wilson796ed472018-07-16 15:11:09 -07003906 ]
3907 link_output =
3908 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
3909 depend_output =
Nico Weberad9eab22020-11-15 22:20:28 -05003910 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC"
Brett Wilson796ed472018-07-16 15:11:09 -07003911 restat = true
3912 }
3913```
3914
3915#### **Example**
3916
3917```
3918 toolchain("my_toolchain") {
3919 # Put these at the top to apply to all tools below.
3920 lib_switch = "-l"
3921 lib_dir_switch = "-L"
3922
3923 tool("cc") {
3924 command = "gcc {{source}} -o {{output}}"
3925 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
3926 description = "GCC {{source}}"
3927 }
3928 tool("cxx") {
3929 command = "g++ {{source}} -o {{output}}"
3930 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
3931 description = "G++ {{source}}"
3932 }
3933 };
3934```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003935### **toolchain**: Defines a toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07003936
3937```
3938 A toolchain is a set of commands and build flags used to compile the source
3939 code. The toolchain() function defines these commands.
3940```
3941
3942#### **Toolchain overview**
3943
3944```
3945 You can have more than one toolchain in use at once in a build and a target
3946 can exist simultaneously in multiple toolchains. A build file is executed
3947 once for each toolchain it is referenced in so the GN code can vary all
3948 parameters of each target (or which targets exist) on a per-toolchain basis.
3949
3950 When you have a simple build with only one toolchain, the build config file
3951 is loaded only once at the beginning of the build. It must call
3952 set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the
3953 label of the toolchain definition to use. The "toolchain_args" section of the
3954 toolchain definition is ignored.
3955
3956 When a target has a dependency on a target using different toolchain (see "gn
3957 help labels" for how to specify this), GN will start a build using that
3958 secondary toolchain to resolve the target. GN will load the build config file
3959 with the build arguments overridden as specified in the toolchain_args.
3960 Because the default toolchain is already known, calls to
3961 set_default_toolchain() are ignored.
3962
3963 To load a file in an alternate toolchain, GN does the following:
3964
3965 1. Loads the file with the toolchain definition in it (as determined by the
3966 toolchain label).
3967 2. Re-runs the master build configuration file, applying the arguments
3968 specified by the toolchain_args section of the toolchain definition.
3969 3. Loads the destination build file in the context of the configuration file
3970 in the previous step.
3971
3972 The toolchain configuration is two-way. In the default toolchain (i.e. the
3973 main build target) the configuration flows from the build config file to the
3974 toolchain. The build config file looks at the state of the build (OS type,
3975 CPU architecture, etc.) and decides which toolchain to use (via
3976 set_default_toolchain()). In secondary toolchains, the configuration flows
3977 from the toolchain to the build config file: the "toolchain_args" in the
3978 toolchain definition specifies the arguments to re-invoke the build.
3979```
3980
3981#### **Functions and variables**
3982
3983```
3984 tool()
Julie Hockette2a29402018-07-31 10:11:42 -07003985 The tool() function call specifies the commands to run for a given step. See
3986 "gn help tool".
Brett Wilson796ed472018-07-16 15:11:09 -07003987
Julie Hockette2a29402018-07-31 10:11:42 -07003988 toolchain_args [scope]
Brett Wilson796ed472018-07-16 15:11:09 -07003989 Overrides for build arguments to pass to the toolchain when invoking it.
3990 This is a variable of type "scope" where the variable names correspond to
3991 variables in declare_args() blocks.
3992
3993 When you specify a target using an alternate toolchain, the master build
3994 configuration file is re-interpreted in the context of that toolchain.
3995 toolchain_args allows you to control the arguments passed into this
3996 alternate invocation of the build.
3997
3998 Any default system arguments or arguments passed in via "gn args" will also
3999 be passed to the alternate invocation unless explicitly overridden by
4000 toolchain_args.
4001
4002 The toolchain_args will be ignored when the toolchain being defined is the
4003 default. In this case, it's expected you want the default argument values.
4004
4005 See also "gn help buildargs" for an overview of these arguments.
4006
Julie Hockette2a29402018-07-31 10:11:42 -07004007 propagates_configs [boolean, default=false]
4008 Determines whether public_configs and all_dependent_configs in this
4009 toolchain propagate to targets in other toolchains.
4010
4011 When false (the default), this toolchain will not propagate any configs to
4012 targets in other toolchains that depend on it targets inside this
4013 toolchain. This matches the most common usage of toolchains where they
4014 represent different architectures or compilers and the settings that apply
4015 to one won't necessarily apply to others.
4016
4017 When true, configs (public and all-dependent) will cross the boundary out
4018 of this toolchain as if the toolchain boundary wasn't there. This only
4019 affects one direction of dependencies: a toolchain can't control whether
4020 it accepts such configs, only whether it pushes them. The build is
4021 responsible for ensuring that any external targets depending on targets in
4022 this toolchain are compatible with the compiler flags, etc. that may be
4023 propagated.
4024
4025 deps [string list]
Brett Wilson796ed472018-07-16 15:11:09 -07004026 Dependencies of this toolchain. These dependencies will be resolved before
4027 any target in the toolchain is compiled. To avoid circular dependencies
4028 these must be targets defined in another toolchain.
4029
4030 This is expressed as a list of targets, and generally these targets will
4031 always specify a toolchain:
4032 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
4033
4034 This concept is somewhat inefficient to express in Ninja (it requires a lot
4035 of duplicate of rules) so should only be used when absolutely necessary.
4036```
4037
4038#### **Example of defining a toolchain**
4039
4040```
4041 toolchain("32") {
4042 tool("cc") {
4043 command = "gcc {{source}}"
4044 ...
4045 }
4046
4047 toolchain_args = {
4048 use_doom_melon = true # Doom melon always required for 32-bit builds.
4049 current_cpu = "x86"
4050 }
4051 }
4052
4053 toolchain("64") {
4054 tool("cc") {
4055 command = "gcc {{source}}"
4056 ...
4057 }
4058
4059 toolchain_args = {
4060 # use_doom_melon is not overridden here, it will take the default.
4061 current_cpu = "x64"
4062 }
4063 }
4064```
4065
4066#### **Example of cross-toolchain dependencies**
4067
4068```
4069 If a 64-bit target wants to depend on a 32-bit binary, it would specify a
4070 dependency using data_deps (data deps are like deps that are only needed at
4071 runtime and aren't linked, since you can't link a 32-bit and a 64-bit
4072 library).
4073
4074 executable("my_program") {
4075 ...
4076 if (target_cpu == "x64") {
4077 # The 64-bit build needs this 32-bit helper.
4078 data_deps = [ ":helper(//toolchains:32)" ]
4079 }
4080 }
4081
4082 if (target_cpu == "x86") {
4083 # Our helper library is only compiled in 32-bits.
4084 shared_library("helper") {
4085 ...
4086 }
4087 }
4088```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004089### **write_file**: Write a file to disk.
Brett Wilson796ed472018-07-16 15:11:09 -07004090
4091```
Julie Hockett09171292018-07-31 14:35:10 -07004092 write_file(filename, data, output_conversion = "")
Brett Wilson796ed472018-07-16 15:11:09 -07004093
4094 If data is a list, the list will be written one-item-per-line with no quoting
4095 or brackets.
4096
4097 If the file exists and the contents are identical to that being written, the
4098 file will not be updated. This will prevent unnecessary rebuilds of targets
4099 that depend on this file.
4100
4101 One use for write_file is to write a list of inputs to an script that might
4102 be too long for the command line. However, it is preferable to use response
4103 files for this purpose. See "gn help response_file_contents".
Brett Wilson796ed472018-07-16 15:11:09 -07004104```
4105
4106#### **Arguments**
4107
4108```
4109 filename
4110 Filename to write. This must be within the output directory.
4111
4112 data
4113 The list or string to write.
Julie Hockett09171292018-07-31 14:35:10 -07004114
4115 output_conversion
Julie Hockett81ee1962019-05-13 11:24:42 -07004116 Controls how the output is written. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07004117```
4118## Built-in predefined variables
4119
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004120### **current_cpu**: The processor architecture of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004121
4122```
4123 The build configuration usually sets this value based on the value of
4124 "host_cpu" (see "gn help host_cpu") and then threads this through the
4125 toolchain definitions to ensure that it always reflects the appropriate
4126 value.
4127
Julie Hockette2a29402018-07-31 10:11:42 -07004128 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004129 empty string ("") by default but is declared so that it can be overridden on
4130 the command line if so desired.
4131
4132 See "gn help target_cpu" for a list of common values returned.
4133```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004134### **current_os**: The operating system of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004135
4136```
4137 The build configuration usually sets this value based on the value of
4138 "target_os" (see "gn help target_os"), and then threads this through the
4139 toolchain definitions to ensure that it always reflects the appropriate
4140 value.
4141
Julie Hockette2a29402018-07-31 10:11:42 -07004142 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004143 empty string ("") by default but is declared so that it can be overridden on
4144 the command line if so desired.
4145
4146 See "gn help target_os" for a list of common values returned.
4147```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004148### **current_toolchain**: Label of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004149
4150```
4151 A fully-qualified label representing the current toolchain. You can use this
4152 to make toolchain-related decisions in the build. See also
4153 "default_toolchain".
4154```
4155
4156#### **Example**
4157
4158```
4159 if (current_toolchain == "//build:64_bit_toolchain") {
4160 executable("output_thats_64_bit_only") {
4161 ...
4162```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004163### **default_toolchain**: [string] Label of the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004164
4165```
4166 A fully-qualified label representing the default toolchain, which may not
4167 necessarily be the current one (see "current_toolchain").
4168```
James Robinson61377e32020-02-13 15:20:07 -08004169### **gn_version**: [number] The version of gn.
4170
4171```
4172 Corresponds to the number printed by `gn --version`.
4173```
4174
4175#### **Example**
4176
4177```
4178 assert(gn_version >= 1700, "need GN version 1700 for the frobulate feature")
4179```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004180### **host_cpu**: The processor architecture that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004181
4182```
4183 This is value is exposed so that cross-compile toolchains can access the host
4184 architecture when needed.
4185
4186 The value should generally be considered read-only, but it can be overriden
4187 in order to handle unusual cases where there might be multiple plausible
4188 values for the host architecture (e.g., if you can do either 32-bit or 64-bit
4189 builds). The value is not used internally by GN for any purpose.
4190```
4191
4192#### **Some possible values**
4193
4194```
4195 - "x64"
4196 - "x86"
4197```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004198### **host_os**: [string] The operating system that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004199
4200```
4201 This value is exposed so that cross-compiles can access the host build
4202 system's settings.
4203
4204 This value should generally be treated as read-only. It, however, is not used
4205 internally by GN for any purpose.
4206```
4207
4208#### **Some possible values**
4209
4210```
4211 - "linux"
4212 - "mac"
4213 - "win"
4214```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004215### **invoker**: [string] The invoking scope inside a template.
Brett Wilson796ed472018-07-16 15:11:09 -07004216
4217```
4218 Inside a template invocation, this variable refers to the scope of the
4219 invoker of the template. Outside of template invocations, this variable is
4220 undefined.
4221
4222 All of the variables defined inside the template invocation are accessible as
4223 members of the "invoker" scope. This is the way that templates read values
4224 set by the callers.
4225
4226 This is often used with "defined" to see if a value is set on the invoking
4227 scope.
4228
4229 See "gn help template" for more examples.
4230```
4231
4232#### **Example**
4233
4234```
4235 template("my_template") {
4236 print(invoker.sources) # Prints [ "a.cc", "b.cc" ]
4237 print(defined(invoker.foo)) # Prints false.
4238 print(defined(invoker.bar)) # Prints true.
4239 }
4240
4241 my_template("doom_melon") {
4242 sources = [ "a.cc", "b.cc" ]
4243 bar = 123
4244 }
4245```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004246### **python_path**: Absolute path of Python.
Brett Wilson796ed472018-07-16 15:11:09 -07004247
4248```
4249 Normally used in toolchain definitions if running some command requires
4250 Python. You will normally not need this when invoking scripts since GN
4251 automatically finds it for you.
4252```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004253### **root_build_dir**: [string] Directory where build commands are run.
Brett Wilson796ed472018-07-16 15:11:09 -07004254
4255```
4256 This is the root build output directory which will be the current directory
4257 when executing all compilers and scripts.
4258
4259 Most often this is used with rebase_path (see "gn help rebase_path") to
4260 convert arguments to be relative to a script's current directory.
4261```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004262### **root_gen_dir**: Directory for the toolchain's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004263
4264```
4265 Absolute path to the root of the generated output directory tree for the
4266 current toolchain. An example would be "//out/Debug/gen" for the default
4267 toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain.
4268
4269 This is primarily useful for setting up include paths for generated files. If
4270 you are passing this to a script, you will want to pass it through
4271 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4272 build directory.
4273
4274 See also "target_gen_dir" which is usually a better location for generated
4275 files. It will be inside the root generated dir.
4276```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004277### **root_out_dir**: [string] Root directory for toolchain output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004278
4279```
4280 Absolute path to the root of the output directory tree for the current
4281 toolchain. It will not have a trailing slash.
4282
4283 For the default toolchain this will be the same as the root_build_dir. An
4284 example would be "//out/Debug" for the default toolchain, or
4285 "//out/Debug/arm" for the "arm" toolchain.
4286
4287 This is primarily useful for setting up script calls. If you are passing this
4288 to a script, you will want to pass it through rebase_path() (see "gn help
4289 rebase_path") to convert it to be relative to the build directory.
4290
4291 See also "target_out_dir" which is usually a better location for output
4292 files. It will be inside the root output dir.
4293```
4294
4295#### **Example**
4296
4297```
4298 action("myscript") {
4299 # Pass the output dir to the script.
4300 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
4301 }
4302```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004303### **target_cpu**: The desired cpu architecture for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004304
4305```
4306 This value should be used to indicate the desired architecture for the
4307 primary objects of the build. It will match the cpu architecture of the
4308 default toolchain, but not necessarily the current toolchain.
4309
4310 In many cases, this is the same as "host_cpu", but in the case of
4311 cross-compiles, this can be set to something different. This value is
4312 different from "current_cpu" in that it does not change based on the current
4313 toolchain. When writing rules, "current_cpu" should be used rather than
4314 "target_cpu" most of the time.
4315
4316 This value is not used internally by GN for any purpose, so it may be set to
4317 whatever value is needed for the build. GN defaults this value to the empty
4318 string ("") and the configuration files should set it to an appropriate value
4319 (e.g., setting it to the value of "host_cpu") if it is not overridden on the
4320 command line or in the args.gn file.
4321```
4322
4323#### **Possible values**
4324
4325```
4326 - "x86"
4327 - "x64"
4328 - "arm"
4329 - "arm64"
4330 - "mipsel"
4331```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004332### **target_gen_dir**: Directory for a target's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004333
4334```
4335 Absolute path to the target's generated file directory. This will be the
4336 "root_gen_dir" followed by the relative path to the current build file. If
4337 your file is in "//tools/doom_melon" then target_gen_dir would be
4338 "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash.
4339
4340 This is primarily useful for setting up include paths for generated files. If
4341 you are passing this to a script, you will want to pass it through
4342 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4343 build directory.
4344
4345 See also "gn help root_gen_dir".
4346```
4347
4348#### **Example**
4349
4350```
4351 action("myscript") {
4352 # Pass the generated output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004353 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004354 }
4355```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004356### **target_name**: [string] The name of the current target.
Brett Wilson796ed472018-07-16 15:11:09 -07004357
4358```
4359 Inside a target or template invocation, this variable refers to the name
4360 given to the target or template invocation. Outside of these, this variable
4361 is undefined.
4362
4363 This is most often used in template definitions to name targets defined in
4364 the template based on the name of the invocation. This is necessary both to
4365 ensure generated targets have unique names and to generate a target with the
4366 exact name of the invocation that other targets can depend on.
4367
4368 Be aware that this value will always reflect the innermost scope. So when
4369 defining a target inside a template, target_name will refer to the target
4370 rather than the template invocation. To get the name of the template
4371 invocation in this case, you should save target_name to a temporary variable
4372 outside of any target definitions.
4373
4374 See "gn help template" for more examples.
4375```
4376
4377#### **Example**
4378
4379```
4380 executable("doom_melon") {
4381 print(target_name) # Prints "doom_melon".
4382 }
4383
4384 template("my_template") {
4385 print(target_name) # Prints "space_ray" when invoked below.
4386
4387 executable(target_name + "_impl") {
4388 print(target_name) # Prints "space_ray_impl".
4389 }
4390 }
4391
4392 my_template("space_ray") {
4393 }
4394```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004395### **target_os**: The desired operating system for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004396
4397```
4398 This value should be used to indicate the desired operating system for the
4399 primary object(s) of the build. It will match the OS of the default
4400 toolchain.
4401
4402 In many cases, this is the same as "host_os", but in the case of
4403 cross-compiles, it may be different. This variable differs from "current_os"
4404 in that it can be referenced from inside any toolchain and will always return
4405 the initial value.
4406
4407 This should be set to the most specific value possible. So, "android" or
4408 "chromeos" should be used instead of "linux" where applicable, even though
4409 Android and ChromeOS are both Linux variants. This can mean that one needs to
4410 write
4411
4412 if (target_os == "android" || target_os == "linux") {
4413 # ...
4414 }
4415
4416 and so forth.
4417
4418 This value is not used internally by GN for any purpose, so it may be set to
4419 whatever value is needed for the build. GN defaults this value to the empty
4420 string ("") and the configuration files should set it to an appropriate value
4421 (e.g., setting it to the value of "host_os") if it is not set via the command
4422 line or in the args.gn file.
4423```
4424
4425#### **Possible values**
4426
4427```
4428 - "android"
4429 - "chromeos"
4430 - "ios"
4431 - "linux"
4432 - "nacl"
4433 - "mac"
4434 - "win"
4435```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004436### **target_out_dir**: [string] Directory for target output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004437
4438```
4439 Absolute path to the target's generated file directory. If your current
4440 target is in "//tools/doom_melon" then this value might be
4441 "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash.
4442
4443 This is primarily useful for setting up arguments for calling scripts. If you
4444 are passing this to a script, you will want to pass it through rebase_path()
4445 (see "gn help rebase_path") to convert it to be relative to the build
4446 directory.
4447
4448 See also "gn help root_out_dir".
4449```
4450
4451#### **Example**
4452
4453```
4454 action("myscript") {
4455 # Pass the output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004456 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004457 }
4458```
4459## Variables you set in targets
4460
Julie Hockettce1fa072019-05-07 17:44:37 -07004461### **aliased_deps**: [scope] Set of crate-dependency pairs.
4462
4463```
4464 Valid for `rust_library` targets and `executable`, `static_library`, and
4465 `shared_library` targets that contain Rust sources.
4466
4467 A scope, each key indicating the renamed crate and the corresponding value
4468 specifying the label of the dependency producing the relevant binary.
4469
4470 All dependencies listed in this field *must* be listed as deps of the target.
4471
4472 executable("foo") {
4473 sources = [ "main.rs" ]
4474 deps = [ "//bar" ]
4475 }
4476
4477 This target would compile the `foo` crate with the following `extern` flag:
4478 `rustc ...command... --extern bar=/obj/bar`
4479
4480 executable("foo") {
4481 sources = [ "main.rs" ]
4482 deps = [ ":bar" ]
4483 aliased_deps = {
4484 bar_renamed = ":bar"
4485 }
4486 }
4487
4488 With the addition of `aliased_deps`, above target would instead compile with:
4489 `rustc ...command... --extern bar_renamed=/obj/bar`
4490```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004491### **all_dependent_configs**: Configs to be forced on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07004492
4493```
4494 A list of config labels.
4495
4496 All targets depending on this one, and recursively, all targets depending on
4497 those, will have the configs listed in this variable added to them. These
4498 configs will also apply to the current target.
4499
4500 This addition happens in a second phase once a target and all of its
4501 dependencies have been resolved. Therefore, a target will not see these
4502 force-added configs in their "configs" variable while the script is running,
4503 and they can not be removed. As a result, this capability should generally
4504 only be used to add defines and include directories necessary to compile a
4505 target's headers.
4506
4507 See also "public_configs".
4508```
4509
4510#### **Ordering of flags and values**
4511
4512```
4513 1. Those set on the current target (not in a config).
4514 2. Those set on the "configs" on the target in order that the
4515 configs appear in the list.
4516 3. Those set on the "all_dependent_configs" on the target in order
4517 that the configs appear in the list.
4518 4. Those set on the "public_configs" on the target in order that
4519 those configs appear in the list.
4520 5. all_dependent_configs pulled from dependencies, in the order of
4521 the "deps" list. This is done recursively. If a config appears
4522 more than once, only the first occurence will be used.
4523 6. public_configs pulled from dependencies, in the order of the
4524 "deps" list. If a dependency is public, they will be applied
4525 recursively.
4526```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004527### **allow_circular_includes_from**: Permit includes from deps.
Brett Wilson796ed472018-07-16 15:11:09 -07004528
4529```
4530 A list of target labels. Must be a subset of the target's "deps". These
4531 targets will be permitted to include headers from the current target despite
4532 the dependency going in the opposite direction.
4533
4534 When you use this, both targets must be included in a final binary for it to
4535 link. To keep linker errors from happening, it is good practice to have all
4536 external dependencies depend only on one of the two targets, and to set the
4537 visibility on the other to enforce this. Thus the targets will always be
4538 linked together in any output.
4539```
4540
4541#### **Details**
4542
4543```
4544 Normally, for a file in target A to include a file from target B, A must list
4545 B as a dependency. This invariant is enforced by the "gn check" command (and
4546 the --check flag to "gn gen" -- see "gn help check").
4547
4548 Sometimes, two targets might be the same unit for linking purposes (two
4549 source sets or static libraries that would always be linked together in a
4550 final executable or shared library) and they each include headers from the
4551 other: you want A to be able to include B's headers, and B to include A's
4552 headers. This is not an ideal situation but is sometimes unavoidable.
4553
4554 This list, if specified, lists which of the dependencies of the current
4555 target can include header files from the current target. That is, if A
4556 depends on B, B can only include headers from A if it is in A's
4557 allow_circular_includes_from list. Normally includes must follow the
4558 direction of dependencies, this flag allows them to go in the opposite
4559 direction.
4560```
4561
4562#### **Danger**
4563
4564```
4565 In the above example, A's headers are likely to include headers from A's
4566 dependencies. Those dependencies may have public_configs that apply flags,
4567 defines, and include paths that make those headers work properly.
4568
4569 With allow_circular_includes_from, B can include A's headers, and
4570 transitively from A's dependencies, without having the dependencies that
4571 would bring in the public_configs those headers need. The result may be
4572 errors or inconsistent builds.
4573
4574 So when you use allow_circular_includes_from, make sure that any compiler
4575 settings, flags, and include directories are the same between both targets
4576 (consider putting such things in a shared config they can both reference).
4577 Make sure the dependencies are also the same (you might consider a group to
4578 collect such dependencies they both depend on).
4579```
4580
4581#### **Example**
4582
4583```
4584 source_set("a") {
4585 deps = [ ":b", ":a_b_shared_deps" ]
4586 allow_circular_includes_from = [ ":b" ]
4587 ...
4588 }
4589
4590 source_set("b") {
4591 deps = [ ":a_b_shared_deps" ]
4592 # Sources here can include headers from a despite lack of deps.
4593 ...
4594 }
4595
4596 group("a_b_shared_deps") {
4597 public_deps = [ ":c" ]
4598 }
4599```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004600### **arflags**: Arguments passed to static_library archiver.
Brett Wilson796ed472018-07-16 15:11:09 -07004601
4602```
4603 A list of flags passed to the archive/lib command that creates static
4604 libraries.
4605
4606 arflags are NOT pushed to dependents, so applying arflags to source sets or
4607 any other target type will be a no-op. As with ldflags, you could put the
4608 arflags in a config and set that as a public or "all dependent" config, but
4609 that will likely not be what you want. If you have a chain of static
4610 libraries dependent on each other, this can cause the flags to propagate up
4611 to other static libraries. Due to the nature of how arflags are typically
4612 used, you will normally want to apply them directly on static_library targets
4613 themselves.
4614```
4615
4616#### **Ordering of flags and values**
4617
4618```
4619 1. Those set on the current target (not in a config).
4620 2. Those set on the "configs" on the target in order that the
4621 configs appear in the list.
4622 3. Those set on the "all_dependent_configs" on the target in order
4623 that the configs appear in the list.
4624 4. Those set on the "public_configs" on the target in order that
4625 those configs appear in the list.
4626 5. all_dependent_configs pulled from dependencies, in the order of
4627 the "deps" list. This is done recursively. If a config appears
4628 more than once, only the first occurence will be used.
4629 6. public_configs pulled from dependencies, in the order of the
4630 "deps" list. If a dependency is public, they will be applied
4631 recursively.
4632```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004633### **args**: (target variable) Arguments passed to an action.
Brett Wilson796ed472018-07-16 15:11:09 -07004634
4635```
4636 For action and action_foreach targets, args is the list of arguments to pass
4637 to the script. Typically you would use source expansion (see "gn help
4638 source_expansion") to insert the source file names.
4639
4640 See also "gn help action" and "gn help action_foreach".
4641```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004642### **asmflags**: Flags passed to the assembler.
Brett Wilson796ed472018-07-16 15:11:09 -07004643
4644```
4645 A list of strings.
4646
4647 "asmflags" are passed to any invocation of a tool that takes an .asm or .S
4648 file as input.
4649```
4650
4651#### **Ordering of flags and values**
4652
4653```
4654 1. Those set on the current target (not in a config).
4655 2. Those set on the "configs" on the target in order that the
4656 configs appear in the list.
4657 3. Those set on the "all_dependent_configs" on the target in order
4658 that the configs appear in the list.
4659 4. Those set on the "public_configs" on the target in order that
4660 those configs appear in the list.
4661 5. all_dependent_configs pulled from dependencies, in the order of
4662 the "deps" list. This is done recursively. If a config appears
4663 more than once, only the first occurence will be used.
4664 6. public_configs pulled from dependencies, in the order of the
4665 "deps" list. If a dependency is public, they will be applied
4666 recursively.
4667```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004668### **assert_no_deps**: Ensure no deps on these targets.
Brett Wilson796ed472018-07-16 15:11:09 -07004669
4670```
4671 A list of label patterns.
4672
4673 This list is a list of patterns that must not match any of the transitive
4674 dependencies of the target. These include all public, private, and data
4675 dependencies, and cross shared library boundaries. This allows you to express
4676 that undesirable code isn't accidentally added to downstream dependencies in
4677 a way that might otherwise be difficult to notice.
4678
4679 Checking does not cross executable boundaries. If a target depends on an
4680 executable, it's assumed that the executable is a tool that is producing part
4681 of the build rather than something that is linked and distributed. This
4682 allows assert_no_deps to express what is distributed in the final target
4683 rather than depend on the internal build steps (which may include
4684 non-distributable code).
4685
4686 See "gn help label_pattern" for the format of the entries in the list. These
4687 patterns allow blacklisting individual targets or whole directory
4688 hierarchies.
4689
4690 Sometimes it is desirable to enforce that many targets have no dependencies
4691 on a target or set of targets. One efficient way to express this is to create
4692 a group with the assert_no_deps rule on it, and make that group depend on all
4693 targets you want to apply that assertion to.
4694```
4695
4696#### **Example**
4697
4698```
4699 executable("doom_melon") {
4700 deps = [ "//foo:bar" ]
4701 ...
4702 assert_no_deps = [
4703 "//evil/*", # Don't link any code from the evil directory.
4704 "//foo:test_support", # This target is also disallowed.
4705 ]
4706 }
4707```
Sylvain Defresne89e64252020-08-07 13:01:06 +02004708### **bridge_header**: [string] Path to C/Objective-C compatibility header.
4709
4710```
4711 Valid for binary targets that contain Swift sources.
4712
4713 Path to an header that includes C/Objective-C functions and types that
4714 needs to be made available to the Swift module.
4715```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004716### **bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in
Brett Wilson796ed472018-07-16 15:11:09 -07004717```
4718 create_bundle.
4719
4720 A string corresponding to a path in $root_build_dir.
4721
4722 This string is used by the "create_bundle" target to expand the
4723 {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must
4724 correspond to a path under "bundle_root_dir".
4725
4726 See "gn help bundle_root_dir" for examples.
4727```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004728### **bundle_deps_filter**: [label list] A list of labels that are filtered out.
Brett Wilson796ed472018-07-16 15:11:09 -07004729
4730```
4731 A list of target labels.
4732
4733 This list contains target label patterns that should be filtered out when
4734 creating the bundle. Any target matching one of those label will be removed
4735 from the dependencies of the create_bundle target.
4736
4737 This is mostly useful when creating application extension bundle as the
4738 application extension has access to runtime resources from the application
4739 bundle and thus do not require a second copy.
4740
4741 See "gn help create_bundle" for more information.
4742```
4743
4744#### **Example**
4745
4746```
4747 create_bundle("today_extension") {
4748 deps = [
4749 "//base"
4750 ]
4751 bundle_root_dir = "$root_out_dir/today_extension.appex"
4752 bundle_deps_filter = [
4753 # The extension uses //base but does not use any function calling into
4754 # third_party/icu and thus does not need the icudtl.dat file.
4755 "//third_party/icu:icudata",
4756 ]
4757 }
4758```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004759### **bundle_executable_dir**
Brett Wilson796ed472018-07-16 15:11:09 -07004760
Joe Armstrongb199e542019-03-21 09:40:08 +08004761```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004762 bundle_executable_dir: Expansion of {{bundle_executable_dir}} in
4763 create_bundle.
4764
Brett Wilson796ed472018-07-16 15:11:09 -07004765 A string corresponding to a path in $root_build_dir.
4766
4767 This string is used by the "create_bundle" target to expand the
4768 {{bundle_executable_dir}} of the "bundle_data" target it depends on. This
4769 must correspond to a path under "bundle_root_dir".
4770
4771 See "gn help bundle_root_dir" for examples.
4772```
Joe Armstrongb199e542019-03-21 09:40:08 +08004773### **bundle_resources_dir**
4774
Brett Wilson796ed472018-07-16 15:11:09 -07004775```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004776 bundle_resources_dir: Expansion of {{bundle_resources_dir}} in
4777 create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07004778
4779 A string corresponding to a path in $root_build_dir.
4780
4781 This string is used by the "create_bundle" target to expand the
4782 {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must
4783 correspond to a path under "bundle_root_dir".
4784
4785 See "gn help bundle_root_dir" for examples.
4786```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004787### **bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07004788
4789```
4790 A string corresponding to a path in root_build_dir.
4791
4792 This string is used by the "create_bundle" target to expand the
4793 {{bundle_root_dir}} of the "bundle_data" target it depends on. This must
4794 correspond to a path under root_build_dir.
4795```
4796
4797#### **Example**
4798
4799```
4800 bundle_data("info_plist") {
4801 sources = [ "Info.plist" ]
4802 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
4803 }
4804
4805 create_bundle("doom_melon.app") {
4806 deps = [ ":info_plist" ]
4807 bundle_root_dir = "${root_build_dir}/doom_melon.app"
4808 bundle_contents_dir = "${bundle_root_dir}/Contents"
4809 bundle_resources_dir = "${bundle_contents_dir}/Resources"
4810 bundle_executable_dir = "${bundle_contents_dir}/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07004811 }
4812```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004813### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004814
4815```
4816 A list of strings.
4817
4818 "cflags" are passed to all invocations of the C, C++, Objective C, and
4819 Objective C++ compilers.
4820
4821 To target one of these variants individually, use "cflags_c", "cflags_cc",
4822 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4823 versions of cflags* will be appended on the compiler command line after
4824 "cflags".
4825
Sylvain Defresne89e64252020-08-07 13:01:06 +02004826 See also "asmflags" for flags for assembly-language files and "swiftflags"
4827 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004828```
4829
4830#### **Ordering of flags and values**
4831
4832```
4833 1. Those set on the current target (not in a config).
4834 2. Those set on the "configs" on the target in order that the
4835 configs appear in the list.
4836 3. Those set on the "all_dependent_configs" on the target in order
4837 that the configs appear in the list.
4838 4. Those set on the "public_configs" on the target in order that
4839 those configs appear in the list.
4840 5. all_dependent_configs pulled from dependencies, in the order of
4841 the "deps" list. This is done recursively. If a config appears
4842 more than once, only the first occurence will be used.
4843 6. public_configs pulled from dependencies, in the order of the
4844 "deps" list. If a dependency is public, they will be applied
4845 recursively.
4846```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004847### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004848
4849```
4850 A list of strings.
4851
4852 "cflags" are passed to all invocations of the C, C++, Objective C, and
4853 Objective C++ compilers.
4854
4855 To target one of these variants individually, use "cflags_c", "cflags_cc",
4856 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4857 versions of cflags* will be appended on the compiler command line after
4858 "cflags".
4859
Sylvain Defresne89e64252020-08-07 13:01:06 +02004860 See also "asmflags" for flags for assembly-language files and "swiftflags"
4861 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004862```
4863
4864#### **Ordering of flags and values**
4865
4866```
4867 1. Those set on the current target (not in a config).
4868 2. Those set on the "configs" on the target in order that the
4869 configs appear in the list.
4870 3. Those set on the "all_dependent_configs" on the target in order
4871 that the configs appear in the list.
4872 4. Those set on the "public_configs" on the target in order that
4873 those configs appear in the list.
4874 5. all_dependent_configs pulled from dependencies, in the order of
4875 the "deps" list. This is done recursively. If a config appears
4876 more than once, only the first occurence will be used.
4877 6. public_configs pulled from dependencies, in the order of the
4878 "deps" list. If a dependency is public, they will be applied
4879 recursively.
4880```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004881### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004882
4883```
4884 A list of strings.
4885
4886 "cflags" are passed to all invocations of the C, C++, Objective C, and
4887 Objective C++ compilers.
4888
4889 To target one of these variants individually, use "cflags_c", "cflags_cc",
4890 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4891 versions of cflags* will be appended on the compiler command line after
4892 "cflags".
4893
Sylvain Defresne89e64252020-08-07 13:01:06 +02004894 See also "asmflags" for flags for assembly-language files and "swiftflags"
4895 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004896```
4897
4898#### **Ordering of flags and values**
4899
4900```
4901 1. Those set on the current target (not in a config).
4902 2. Those set on the "configs" on the target in order that the
4903 configs appear in the list.
4904 3. Those set on the "all_dependent_configs" on the target in order
4905 that the configs appear in the list.
4906 4. Those set on the "public_configs" on the target in order that
4907 those configs appear in the list.
4908 5. all_dependent_configs pulled from dependencies, in the order of
4909 the "deps" list. This is done recursively. If a config appears
4910 more than once, only the first occurence will be used.
4911 6. public_configs pulled from dependencies, in the order of the
4912 "deps" list. If a dependency is public, they will be applied
4913 recursively.
4914```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004915### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004916
4917```
4918 A list of strings.
4919
4920 "cflags" are passed to all invocations of the C, C++, Objective C, and
4921 Objective C++ compilers.
4922
4923 To target one of these variants individually, use "cflags_c", "cflags_cc",
4924 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4925 versions of cflags* will be appended on the compiler command line after
4926 "cflags".
4927
Sylvain Defresne89e64252020-08-07 13:01:06 +02004928 See also "asmflags" for flags for assembly-language files and "swiftflags"
4929 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004930```
4931
4932#### **Ordering of flags and values**
4933
4934```
4935 1. Those set on the current target (not in a config).
4936 2. Those set on the "configs" on the target in order that the
4937 configs appear in the list.
4938 3. Those set on the "all_dependent_configs" on the target in order
4939 that the configs appear in the list.
4940 4. Those set on the "public_configs" on the target in order that
4941 those configs appear in the list.
4942 5. all_dependent_configs pulled from dependencies, in the order of
4943 the "deps" list. This is done recursively. If a config appears
4944 more than once, only the first occurence will be used.
4945 6. public_configs pulled from dependencies, in the order of the
4946 "deps" list. If a dependency is public, they will be applied
4947 recursively.
4948```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004949### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004950
4951```
4952 A list of strings.
4953
4954 "cflags" are passed to all invocations of the C, C++, Objective C, and
4955 Objective C++ compilers.
4956
4957 To target one of these variants individually, use "cflags_c", "cflags_cc",
4958 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4959 versions of cflags* will be appended on the compiler command line after
4960 "cflags".
4961
Sylvain Defresne89e64252020-08-07 13:01:06 +02004962 See also "asmflags" for flags for assembly-language files and "swiftflags"
4963 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004964```
4965
4966#### **Ordering of flags and values**
4967
4968```
4969 1. Those set on the current target (not in a config).
4970 2. Those set on the "configs" on the target in order that the
4971 configs appear in the list.
4972 3. Those set on the "all_dependent_configs" on the target in order
4973 that the configs appear in the list.
4974 4. Those set on the "public_configs" on the target in order that
4975 those configs appear in the list.
4976 5. all_dependent_configs pulled from dependencies, in the order of
4977 the "deps" list. This is done recursively. If a config appears
4978 more than once, only the first occurence will be used.
4979 6. public_configs pulled from dependencies, in the order of the
4980 "deps" list. If a dependency is public, they will be applied
4981 recursively.
4982```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004983### **check_includes**: [boolean] Controls whether a target's files are checked.
Brett Wilson796ed472018-07-16 15:11:09 -07004984
4985```
4986 When true (the default), the "gn check" command (as well as "gn gen" with the
4987 --check flag) will check this target's sources and headers for proper
4988 dependencies.
4989
4990 When false, the files in this target will be skipped by default. This does
4991 not affect other targets that depend on the current target, it just skips
4992 checking the includes of the current target's files.
4993
4994 If there are a few conditionally included headers that trip up checking, you
4995 can exclude headers individually by annotating them with "nogncheck" (see "gn
4996 help nogncheck").
4997
4998 The topic "gn help check" has general information on how checking works and
4999 advice on how to pass a check in problematic cases.
5000```
5001
5002#### **Example**
5003
5004```
5005 source_set("busted_includes") {
5006 # This target's includes are messed up, exclude it from checking.
5007 check_includes = false
5008 ...
5009 }
5010```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005011### **code_signing_args**: [string list] Arguments passed to code signing script.
Brett Wilson796ed472018-07-16 15:11:09 -07005012
5013```
5014 For create_bundle targets, code_signing_args is the list of arguments to pass
5015 to the code signing script. Typically you would use source expansion (see "gn
5016 help source_expansion") to insert the source file names.
5017
5018 See also "gn help create_bundle".
5019```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005020### **code_signing_outputs**: [file list] Output files for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005021
5022```
5023 Outputs from the code signing step of a create_bundle target. Must refer to
5024 files in the build directory.
5025
5026 See also "gn help create_bundle".
5027```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005028### **code_signing_script**: [file name] Script for code signing."
Brett Wilson796ed472018-07-16 15:11:09 -07005029
5030```
5031 An absolute or buildfile-relative file name of a Python script to run for a
5032 create_bundle target to perform code signing step.
5033
5034 See also "gn help create_bundle".
5035```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005036### **code_signing_sources**: [file list] Sources for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005037
5038```
5039 A list of files used as input for code signing script step of a create_bundle
5040 target. Non-absolute paths will be resolved relative to the current build
5041 file.
5042
5043 See also "gn help create_bundle".
5044```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005045### **complete_static_lib**: [boolean] Links all deps into a static library.
Brett Wilson796ed472018-07-16 15:11:09 -07005046
5047```
5048 A static library normally doesn't include code from dependencies, but instead
5049 forwards the static libraries and source sets in its deps up the dependency
5050 chain until a linkable target (an executable or shared library) is reached.
5051 The final linkable target only links each static library once, even if it
5052 appears more than once in its dependency graph.
5053
5054 In some cases the static library might be the final desired output. For
5055 example, you may be producing a static library for distribution to third
5056 parties. In this case, the static library should include code for all
5057 dependencies in one complete package. However, complete static libraries
5058 themselves are never linked into other complete static libraries. All
5059 complete static libraries are for distribution and linking them in would
5060 cause code duplication in this case. If the static library is not for
5061 distribution, it should not be complete.
5062
5063 GN treats non-complete static libraries as source sets when they are linked
5064 into complete static libraries. This is done because some tools like AR do
5065 not handle dependent static libraries properly. This makes it easier to write
5066 "alink" rules.
5067
5068 In rare cases it makes sense to list a header in more than one target if it
5069 could be considered conceptually a member of both. libraries.
5070```
5071
5072#### **Example**
5073
5074```
5075 static_library("foo") {
5076 complete_static_lib = true
5077 deps = [ "bar" ]
5078 }
5079```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005080### **configs**: Configs applying to this target or config.
Brett Wilson796ed472018-07-16 15:11:09 -07005081
5082```
5083 A list of config labels.
5084```
5085
5086#### **Configs on a target**
5087
5088```
5089 When used on a target, the include_dirs, defines, etc. in each config are
5090 appended in the order they appear to the compile command for each file in the
5091 target. They will appear after the include_dirs, defines, etc. that the
5092 target sets directly.
5093
5094 Since configs apply after the values set on a target, directly setting a
5095 compiler flag will prepend it to the command line. If you want to append a
5096 flag instead, you can put that flag in a one-off config and append that
5097 config to the target's configs list.
5098
5099 The build configuration script will generally set up the default configs
5100 applying to a given target type (see "set_defaults"). When a target is being
5101 defined, it can add to or remove from this list.
5102```
5103
5104#### **Configs on a config**
5105
5106```
5107 It is possible to create composite configs by specifying configs on a config.
5108 One might do this to forward values, or to factor out blocks of settings from
5109 very large configs into more manageable named chunks.
5110
5111 In this case, the composite config is expanded to be the concatenation of its
5112 own values, and in order, the values from its sub-configs *before* anything
5113 else happens. This has some ramifications:
5114
5115 - A target has no visibility into a config's sub-configs. Target code only
5116 sees the name of the composite config. It can't remove sub-configs or opt
5117 in to only parts of it. The composite config may not even be defined
5118 before the target is.
5119
5120 - You can get duplication of values if a config is listed twice, say, on a
5121 target and in a sub-config that also applies. In other cases, the configs
5122 applying to a target are de-duped. It's expected that if a config is
5123 listed as a sub-config that it is only used in that context. (Note that
5124 it's possible to fix this and de-dupe, but it's not normally relevant and
5125 complicates the implementation.)
5126```
5127
5128#### **Ordering of flags and values**
5129
5130```
5131 1. Those set on the current target (not in a config).
5132 2. Those set on the "configs" on the target in order that the
5133 configs appear in the list.
5134 3. Those set on the "all_dependent_configs" on the target in order
5135 that the configs appear in the list.
5136 4. Those set on the "public_configs" on the target in order that
5137 those configs appear in the list.
5138 5. all_dependent_configs pulled from dependencies, in the order of
5139 the "deps" list. This is done recursively. If a config appears
5140 more than once, only the first occurence will be used.
5141 6. public_configs pulled from dependencies, in the order of the
5142 "deps" list. If a dependency is public, they will be applied
5143 recursively.
5144```
5145
5146#### **Example**
5147
5148```
5149 # Configs on a target.
5150 source_set("foo") {
5151 # Don't use the default RTTI config that BUILDCONFIG applied to us.
5152 configs -= [ "//build:no_rtti" ]
5153
5154 # Add some of our own settings.
5155 configs += [ ":mysettings" ]
5156 }
5157
5158 # Create a default_optimization config that forwards to one of a set of more
5159 # specialized configs depending on build flags. This pattern is useful
5160 # because it allows a target to opt in to either a default set, or a more
5161 # specific set, while avoid duplicating the settings in two places.
5162 config("super_optimization") {
5163 cflags = [ ... ]
5164 }
5165 config("default_optimization") {
5166 if (optimize_everything) {
5167 configs = [ ":super_optimization" ]
5168 } else {
5169 configs = [ ":no_optimization" ]
5170 }
5171 }
5172```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005173### **contents**: Contents to write to file.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005174
5175```
5176 The contents of the file for a generated_file target.
5177 See "gn help generated_file".
5178```
Julie Hockettce1fa072019-05-07 17:44:37 -07005179### **crate_name**: [string] The name for the compiled crate.
5180
5181```
5182 Valid for `rust_library` targets and `executable`, `static_library`,
5183 `shared_library`, and `source_set` targets that contain Rust sources.
5184
5185 If crate_name is not set, then this rule will use the target name.
5186```
5187### **crate_root**: [string] The root source file for a binary or library.
5188
5189```
5190 Valid for `rust_library` targets and `executable`, `static_library`,
5191 `shared_library`, and `source_set` targets that contain Rust sources.
5192
5193 This file is usually the `main.rs` or `lib.rs` for binaries and libraries,
5194 respectively.
5195
5196 If crate_root is not set, then this rule will look for a lib.rs file (or
5197 main.rs for executable) or a single file in sources, if sources contains
5198 only one file.
5199```
5200### **crate_type**: [string] The type of linkage to use on a shared_library.
5201
5202```
5203 Valid for `rust_library` targets and `executable`, `static_library`,
5204 `shared_library`, and `source_set` targets that contain Rust sources.
5205
5206 Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib".
5207 This field sets the `crate-type` attribute for the `rustc` tool on static
5208 libraries, as well as the appropiate output extension in the
5209 `rust_output_extension` attribute. Since outputs must be explicit, the `lib`
5210 crate type (where the Rust compiler produces what it thinks is the
5211 appropriate library type) is not supported.
5212
5213 It should be noted that the "dylib" crate type in Rust is unstable in the set
5214 of symbols it exposes, and most usages today are potentially wrong and will
5215 be broken in the future.
5216
5217 Static libraries, rust libraries, and executables have this field set
5218 automatically.
5219```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005220### **data**: Runtime data file dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005221
5222```
5223 Lists files or directories required to run the given target. These are
5224 typically data files or directories of data files. The paths are interpreted
5225 as being relative to the current build file. Since these are runtime
5226 dependencies, they do not affect which targets are built or when. To declare
5227 input files to a script, use "inputs".
5228
5229 Appearing in the "data" section does not imply any special handling such as
5230 copying them to the output directory. This is just used for declaring runtime
5231 dependencies. Runtime dependencies can be queried using the "runtime_deps"
5232 category of "gn desc" or written during build generation via
5233 "--runtime-deps-list-file".
5234
5235 GN doesn't require data files to exist at build-time. So actions that produce
5236 files that are in turn runtime dependencies can list those generated files
5237 both in the "outputs" list as well as the "data" list.
5238
5239 By convention, directories are listed with a trailing slash:
5240 data = [ "test/data/" ]
5241 However, no verification is done on these so GN doesn't enforce this. The
5242 paths are just rebased and passed along when requested.
5243
5244 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5245 gathering data. See "gn help create_bundle" for details.
5246
5247 See "gn help runtime_deps" for how these are used.
5248```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005249### **data_deps**: Non-linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005250
5251```
5252 A list of target labels.
5253
5254 Specifies dependencies of a target that are not actually linked into the
5255 current target. Such dependencies will be built and will be available at
5256 runtime.
5257
5258 This is normally used for things like plugins or helper programs that a
5259 target needs at runtime.
5260
5261 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5262 gathering data_deps. See "gn help create_bundle" for details.
5263
5264 See also "gn help deps" and "gn help data".
5265```
5266
5267#### **Example**
5268
5269```
5270 executable("foo") {
5271 deps = [ "//base" ]
5272 data_deps = [ "//plugins:my_runtime_plugin" ]
5273 }
5274```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005275### **data_keys**: Keys from which to collect metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005276
5277```
5278 These keys are used to identify metadata to collect. If a walked target
5279 defines this key in its metadata, its value will be appended to the resulting
5280 collection.
5281
5282 See "gn help generated_file".
5283```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005284### **defines**: C preprocessor defines.
Brett Wilson796ed472018-07-16 15:11:09 -07005285
5286```
5287 A list of strings
5288
5289 These strings will be passed to the C/C++ compiler as #defines. The strings
5290 may or may not include an "=" to assign a value.
5291```
5292
5293#### **Ordering of flags and values**
5294
5295```
5296 1. Those set on the current target (not in a config).
5297 2. Those set on the "configs" on the target in order that the
5298 configs appear in the list.
5299 3. Those set on the "all_dependent_configs" on the target in order
5300 that the configs appear in the list.
5301 4. Those set on the "public_configs" on the target in order that
5302 those configs appear in the list.
5303 5. all_dependent_configs pulled from dependencies, in the order of
5304 the "deps" list. This is done recursively. If a config appears
5305 more than once, only the first occurence will be used.
5306 6. public_configs pulled from dependencies, in the order of the
5307 "deps" list. If a dependency is public, they will be applied
5308 recursively.
5309```
5310
5311#### **Example**
5312
5313```
5314 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
5315```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005316### **depfile**: [string] File name for input dependencies for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07005317
5318```
5319 If nonempty, this string specifies that the current action or action_foreach
5320 target will generate the given ".d" file containing the dependencies of the
5321 input. Empty or unset means that the script doesn't generate the files.
5322
5323 A depfile should be used only when a target depends on files that are not
5324 already specified by a target's inputs and sources. Likewise, depfiles should
5325 specify only those dependencies not already included in sources or inputs.
5326
5327 The .d file should go in the target output directory. If you have more than
5328 one source file that the script is being run over, you can use the output
5329 file expansions described in "gn help action_foreach" to name the .d file
Nico Weberad9eab22020-11-15 22:20:28 -05005330 according to the input.
Brett Wilson796ed472018-07-16 15:11:09 -07005331
5332 The format is that of a Makefile and all paths must be relative to the root
5333 build directory. Only one output may be listed and it must match the first
5334 output of the action.
5335
5336 Although depfiles are created by an action, they should not be listed in the
5337 action's "outputs" unless another target will use the file as an input.
5338```
5339
5340#### **Example**
5341
5342```
5343 action_foreach("myscript_target") {
5344 script = "myscript.py"
5345 sources = [ ... ]
5346
5347 # Locate the depfile in the output directory named like the
5348 # inputs but with a ".d" appended.
5349 depfile = "$relative_target_output_dir/{{source_name}}.d"
5350
5351 # Say our script uses "-o " to indicate the depfile.
5352 args = [ "{{source}}", "-o", depfile ]
5353 }
5354```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005355### **deps**: Private linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005356
5357```
5358 A list of target labels.
5359
5360 Specifies private dependencies of a target. Private dependencies are
5361 propagated up the dependency tree and linked to dependent targets, but do not
5362 grant the ability to include headers from the dependency. Public configs are
5363 not forwarded.
5364```
5365
5366#### **Details of dependency propagation**
5367
5368```
5369 Source sets, shared libraries, and non-complete static libraries will be
5370 propagated up the dependency tree across groups, non-complete static
5371 libraries and source sets.
5372
5373 Executables, shared libraries, and complete static libraries will link all
5374 propagated targets and stop propagation. Actions and copy steps also stop
5375 propagation, allowing them to take a library as an input but not force
5376 dependents to link to it.
5377
5378 Propagation of all_dependent_configs and public_configs happens independently
5379 of target type. all_dependent_configs are always propagated across all types
5380 of targets, and public_configs are always propagated across public deps of
5381 all types of targets.
5382
5383 Data dependencies are propagated differently. See "gn help data_deps" and
5384 "gn help runtime_deps".
5385
5386 See also "public_deps".
5387```
Petr Hoseka1413862020-01-03 12:54:33 -08005388### **externs**: [scope] Set of Rust crate-dependency pairs.
5389
5390```
5391 A list, each value being a scope indicating a pair of crate name and the path
5392 to the Rust library.
5393
5394 These libraries will be passed as `--extern crate_name=path` to compiler
5395 invocation containing the current target.
5396```
5397
5398#### **Examples**
5399
5400```
5401 executable("foo") {
5402 sources = [ "main.rs" ]
5403 externs = [{
5404 crate_name = "bar",
5405 path = "path/to/bar.rlib"
5406 }]
5407 }
5408
5409 This target would compile the `foo` crate with the following `extern` flag:
5410 `--extern bar=path/to/bar.rlib`.
5411```
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005412### **framework_dirs**: [directory list] Additional framework search directories.
5413
5414```
5415 A list of source directories.
5416
5417 The directories in this list will be added to the framework search path for
5418 the files in the affected target.
5419```
5420
5421#### **Ordering of flags and values**
5422
5423```
5424 1. Those set on the current target (not in a config).
5425 2. Those set on the "configs" on the target in order that the
5426 configs appear in the list.
5427 3. Those set on the "all_dependent_configs" on the target in order
5428 that the configs appear in the list.
5429 4. Those set on the "public_configs" on the target in order that
5430 those configs appear in the list.
5431 5. all_dependent_configs pulled from dependencies, in the order of
5432 the "deps" list. This is done recursively. If a config appears
5433 more than once, only the first occurence will be used.
5434 6. public_configs pulled from dependencies, in the order of the
5435 "deps" list. If a dependency is public, they will be applied
5436 recursively.
5437```
5438
5439#### **Example**
5440
5441```
5442 framework_dirs = [ "src/include", "//third_party/foo" ]
5443```
5444### **frameworks**: [name list] Name of frameworks that must be linked.
5445
5446```
5447 A list of framework names.
5448
5449 The frameworks named in that list will be linked with any dynamic link
5450 type target.
5451```
5452
5453#### **Ordering of flags and values**
5454
5455```
5456 1. Those set on the current target (not in a config).
5457 2. Those set on the "configs" on the target in order that the
5458 configs appear in the list.
5459 3. Those set on the "all_dependent_configs" on the target in order
5460 that the configs appear in the list.
5461 4. Those set on the "public_configs" on the target in order that
5462 those configs appear in the list.
5463 5. all_dependent_configs pulled from dependencies, in the order of
5464 the "deps" list. This is done recursively. If a config appears
5465 more than once, only the first occurence will be used.
5466 6. public_configs pulled from dependencies, in the order of the
5467 "deps" list. If a dependency is public, they will be applied
5468 recursively.
5469```
5470
5471#### **Example**
5472
5473```
5474 frameworks = [ "Foundation.framework", "Foo.framework" ]
5475```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005476### **friend**: Allow targets to include private headers.
Brett Wilson796ed472018-07-16 15:11:09 -07005477
5478```
5479 A list of label patterns (see "gn help label_pattern") that allow dependent
5480 targets to include private headers. Applies to all binary targets.
5481
5482 Normally if a target lists headers in the "public" list (see "gn help
5483 public"), other headers are implicitly marked as private. Private headers
5484 can not be included by other targets, even with a public dependency path.
5485 The "gn check" function performs this validation.
5486
5487 A friend declaration allows one or more targets to include private headers.
5488 This is useful for things like unit tests that are closely associated with a
5489 target and require internal knowledge without opening up all headers to be
5490 included by all dependents.
5491
5492 A friend target does not allow that target to include headers when no
5493 dependency exists. A public dependency path must still exist between two
5494 targets to include any headers from a destination target. The friend
5495 annotation merely allows the use of headers that would otherwise be
5496 prohibited because they are private.
5497
5498 The friend annotation is matched only against the target containing the file
5499 with the include directive. Friend annotations are not propagated across
5500 public or private dependencies. Friend annotations do not affect visibility.
5501```
5502
5503#### **Example**
5504
5505```
5506 static_library("lib") {
5507 # This target can include our private headers.
5508 friend = [ ":unit_tests" ]
5509
5510 public = [
5511 "public_api.h", # Normal public API for dependent targets.
5512 ]
5513
5514 # Private API and sources.
5515 sources = [
5516 "a_source_file.cc",
5517
5518 # Normal targets that depend on this one won't be able to include this
5519 # because this target defines a list of "public" headers. Without the
5520 # "public" list, all headers are implicitly public.
5521 "private_api.h",
5522 ]
5523 }
5524
5525 executable("unit_tests") {
5526 sources = [
5527 # This can include "private_api.h" from the :lib target because it
5528 # depends on that target and because of the friend annotation.
5529 "my_test.cc",
5530 ]
5531
5532 deps = [
5533 ":lib", # Required for the include to be allowed.
5534 ]
5535 }
5536```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005537### **include_dirs**: Additional include directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005538
5539```
5540 A list of source directories.
5541
5542 The directories in this list will be added to the include path for the files
5543 in the affected target.
5544```
5545
5546#### **Ordering of flags and values**
5547
5548```
5549 1. Those set on the current target (not in a config).
5550 2. Those set on the "configs" on the target in order that the
5551 configs appear in the list.
5552 3. Those set on the "all_dependent_configs" on the target in order
5553 that the configs appear in the list.
5554 4. Those set on the "public_configs" on the target in order that
5555 those configs appear in the list.
5556 5. all_dependent_configs pulled from dependencies, in the order of
5557 the "deps" list. This is done recursively. If a config appears
5558 more than once, only the first occurence will be used.
5559 6. public_configs pulled from dependencies, in the order of the
5560 "deps" list. If a dependency is public, they will be applied
5561 recursively.
5562```
5563
5564#### **Example**
5565
5566```
5567 include_dirs = [ "src/include", "//third_party/foo" ]
5568```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005569### **inputs**: Additional compile-time dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005570
5571```
5572 Inputs are compile-time dependencies of the current target. This means that
5573 all inputs must be available before compiling any of the sources or executing
5574 any actions.
5575
5576 Inputs are typically only used for action and action_foreach targets.
5577```
5578
5579#### **Inputs for actions**
5580
5581```
5582 For action and action_foreach targets, inputs should be the inputs to script
5583 that don't vary. These should be all .py files that the script uses via
5584 imports (the main script itself will be an implicit dependency of the action
5585 so need not be listed).
5586
5587 For action targets, inputs and sources are treated the same, but from a style
5588 perspective, it's recommended to follow the same rule as action_foreach and
5589 put helper files in the inputs, and the data used by the script (if any) in
5590 sources.
5591
5592 Note that another way to declare input dependencies from an action is to have
5593 the action write a depfile (see "gn help depfile"). This allows the script to
5594 dynamically write input dependencies, that might not be known until actually
5595 executing the script. This is more efficient than doing processing while
5596 running GN to determine the inputs, and is easier to keep in-sync than
5597 hardcoding the list.
5598```
5599
5600#### **Script input gotchas**
5601
5602```
5603 It may be tempting to write a script that enumerates all files in a directory
5604 as inputs. Don't do this! Even if you specify all the files in the inputs or
5605 sources in the GN target (or worse, enumerate the files in an exec_script
5606 call when running GN, which will be slow), the dependencies will be broken.
5607
5608 The problem happens if a file is ever removed because the inputs are not
5609 listed on the command line to the script. Because the script hasn't changed
5610 and all inputs are up to date, the script will not re-run and you will get a
5611 stale build. Instead, either list all inputs on the command line to the
5612 script, or if there are many, create a separate list file that the script
5613 reads. As long as this file is listed in the inputs, the build will detect
5614 when it has changed in any way and the action will re-run.
5615```
5616
5617#### **Inputs for binary targets**
5618
5619```
Julie Hockette2a29402018-07-31 10:11:42 -07005620 Any input dependencies will be resolved before compiling any sources or
5621 linking the target. Normally, all actions that a target depends on will be run
5622 before any files in a target are compiled. So if you depend on generated
5623 headers, you do not typically need to list them in the inputs section.
Brett Wilson796ed472018-07-16 15:11:09 -07005624
5625 Inputs for binary targets will be treated as implicit dependencies, meaning
5626 that changes in any of the inputs will force all sources in the target to be
5627 recompiled. If an input only applies to a subset of source files, you may
5628 want to split those into a separate target to avoid unnecessary recompiles.
5629```
5630
5631#### **Example**
5632
5633```
5634 action("myscript") {
5635 script = "domything.py"
5636 inputs = [ "input.data" ]
5637 }
5638```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005639### **ldflags**: Flags passed to the linker.
Brett Wilson796ed472018-07-16 15:11:09 -07005640
5641```
5642 A list of strings.
5643
5644 These flags are passed on the command-line to the linker and generally
5645 specify various linking options. Most targets will not need these and will
5646 use "libs" and "lib_dirs" instead.
5647
5648 ldflags are NOT pushed to dependents, so applying ldflags to source sets or
5649 static libraries will be a no-op. If you want to apply ldflags to dependent
5650 targets, put them in a config and set it in the all_dependent_configs or
5651 public_configs.
5652```
5653
5654#### **Ordering of flags and values**
5655
5656```
5657 1. Those set on the current target (not in a config).
5658 2. Those set on the "configs" on the target in order that the
5659 configs appear in the list.
5660 3. Those set on the "all_dependent_configs" on the target in order
5661 that the configs appear in the list.
5662 4. Those set on the "public_configs" on the target in order that
5663 those configs appear in the list.
5664 5. all_dependent_configs pulled from dependencies, in the order of
5665 the "deps" list. This is done recursively. If a config appears
5666 more than once, only the first occurence will be used.
5667 6. public_configs pulled from dependencies, in the order of the
5668 "deps" list. If a dependency is public, they will be applied
5669 recursively.
5670```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005671### **lib_dirs**: Additional library directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005672
5673```
5674 A list of directories.
5675
5676 Specifies additional directories passed to the linker for searching for the
5677 required libraries. If an item is not an absolute path, it will be treated as
5678 being relative to the current build file.
5679
5680 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005681 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005682 shared library or executable target is reached. Second, they are
5683 uniquified so each one is only passed once (the first instance of it
5684 will be the one used).
5685```
5686
5687#### **Ordering of flags and values**
5688
5689```
5690 1. Those set on the current target (not in a config).
5691 2. Those set on the "configs" on the target in order that the
5692 configs appear in the list.
5693 3. Those set on the "all_dependent_configs" on the target in order
5694 that the configs appear in the list.
5695 4. Those set on the "public_configs" on the target in order that
5696 those configs appear in the list.
5697 5. all_dependent_configs pulled from dependencies, in the order of
5698 the "deps" list. This is done recursively. If a config appears
5699 more than once, only the first occurence will be used.
5700 6. public_configs pulled from dependencies, in the order of the
5701 "deps" list. If a dependency is public, they will be applied
5702 recursively.
5703
5704 For "libs" and "lib_dirs" only, the values propagated from
5705 dependencies (as described above) are applied last assuming they
5706 are not already in the list.
5707```
5708
5709#### **Example**
5710
5711```
5712 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
5713```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005714### **libs**: Additional libraries to link.
Brett Wilson796ed472018-07-16 15:11:09 -07005715
5716```
5717 A list of library names or library paths.
5718
5719 These libraries will be linked into the final binary (executable or shared
5720 library) containing the current target.
5721
5722 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005723 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005724 shared library or executable target is reached. Second, they are
5725 uniquified so each one is only passed once (the first instance of it
5726 will be the one used).
5727```
5728
5729#### **Types of libs**
5730
5731```
5732 There are several different things that can be expressed in libs:
5733
5734 File paths
5735 Values containing '/' will be treated as references to files in the
5736 checkout. They will be rebased to be relative to the build directory and
5737 specified in the "libs" for linker tools. This facility should be used
5738 for libraries that are checked in to the version control. For libraries
5739 that are generated by the build, use normal GN deps to link them.
5740
5741 System libraries
5742 Values not containing '/' will be treated as system library names. These
5743 will be passed unmodified to the linker and prefixed with the
5744 "lib_switch" attribute of the linker tool. Generally you would set the
5745 "lib_dirs" so the given library is found. Your BUILD.gn file should not
5746 specify the switch (like "-l"): this will be encoded in the "lib_switch"
5747 of the tool.
Brett Wilson796ed472018-07-16 15:11:09 -07005748```
5749
5750#### **Ordering of flags and values**
5751
5752```
5753 1. Those set on the current target (not in a config).
5754 2. Those set on the "configs" on the target in order that the
5755 configs appear in the list.
5756 3. Those set on the "all_dependent_configs" on the target in order
5757 that the configs appear in the list.
5758 4. Those set on the "public_configs" on the target in order that
5759 those configs appear in the list.
5760 5. all_dependent_configs pulled from dependencies, in the order of
5761 the "deps" list. This is done recursively. If a config appears
5762 more than once, only the first occurence will be used.
5763 6. public_configs pulled from dependencies, in the order of the
5764 "deps" list. If a dependency is public, they will be applied
5765 recursively.
5766
5767 For "libs" and "lib_dirs" only, the values propagated from
5768 dependencies (as described above) are applied last assuming they
5769 are not already in the list.
5770```
5771
5772#### **Examples**
5773
5774```
5775 On Windows:
5776 libs = [ "ctl3d.lib" ]
5777
5778 On Linux:
5779 libs = [ "ld" ]
5780```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005781### **metadata**: Metadata of this target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005782
5783```
5784 Metadata is a collection of keys and values relating to a particular target.
Julie Hockettd69a9c32019-01-23 14:36:18 -08005785 Values must be lists, allowing for sane and predictable collection behavior.
5786 Generally, these keys will include three types of lists: lists of ordinary
5787 strings, lists of filenames intended to be rebased according to their
5788 particular source directory, and lists of target labels intended to be used
5789 as barriers to the walk. Verfication of these categories occurs at walk time,
5790 not creation time (since it is not clear until the walk which values are
5791 intended for which purpose).
5792```
Nate Fischer8ed01d32019-01-08 17:32:01 -08005793
Julie Hockettd69a9c32019-01-23 14:36:18 -08005794#### **Example**
Nate Fischer8ed01d32019-01-08 17:32:01 -08005795
Julie Hockettd69a9c32019-01-23 14:36:18 -08005796```
Nate Fischer8ed01d32019-01-08 17:32:01 -08005797 group("doom_melon") {
5798 metadata = {
5799 # These keys are not built in to GN but are interpreted when consuming
5800 # metadata.
5801 my_barrier = []
5802 my_files = [ "a.txt", "b.txt" ]
5803 }
5804 }
5805```
Sylvain Defresne89e64252020-08-07 13:01:06 +02005806### **module_name**: [string] The name for the compiled module.
5807
5808```
5809 Valid for binary targets that contain Swift sources.
5810
5811 If module_name is not set, then this rule will use the target name.
5812```
Wesley Moyd874bb92020-01-21 16:01:04 -08005813### **output_conversion**: Data format for generated_file targets.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005814
5815```
5816 Controls how the "contents" of a generated_file target is formatted.
Julie Hockett81ee1962019-05-13 11:24:42 -07005817 See "gn help io_conversion".
Nate Fischer8ed01d32019-01-08 17:32:01 -08005818```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005819### **output_dir**: [directory] Directory to put output file in.
Brett Wilson796ed472018-07-16 15:11:09 -07005820
5821```
5822 For library and executable targets, overrides the directory for the final
5823 output. This must be in the root_build_dir or a child thereof.
5824
5825 This should generally be in the root_out_dir or a subdirectory thereof (the
5826 root_out_dir will be the same as the root_build_dir for the default
5827 toolchain, and will be a subdirectory for other toolchains). Not putting the
5828 output in a subdirectory of root_out_dir can result in collisions between
5829 different toolchains, so you will need to take steps to ensure that your
5830 target is only present in one toolchain.
5831
5832 Normally the toolchain specifies the output directory for libraries and
5833 executables (see "gn help tool"). You will have to consult that for the
5834 default location. The default location will be used if output_dir is
5835 undefined or empty.
5836```
5837
5838#### **Example**
5839
5840```
5841 shared_library("doom_melon") {
5842 output_dir = "$root_out_dir/plugin_libs"
5843 ...
5844 }
5845```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005846### **output_extension**: Value to use for the output's file extension.
Brett Wilson796ed472018-07-16 15:11:09 -07005847
5848```
5849 Normally the file extension for a target is based on the target type and the
5850 operating system, but in rare cases you will need to override the name (for
5851 example to use "libfreetype.so.6" instead of libfreetype.so on Linux).
5852
5853 This value should not include a leading dot. If undefined, the default
5854 specified on the tool will be used. If set to the empty string, no output
5855 extension will be used.
5856
5857 The output_extension will be used to set the "{{output_extension}}" expansion
5858 which the linker tool will generally use to specify the output file name. See
5859 "gn help tool".
5860```
5861
5862#### **Example**
5863
5864```
5865 shared_library("freetype") {
5866 if (is_linux) {
5867 # Call the output "libfreetype.so.6"
5868 output_extension = "so.6"
5869 }
5870 ...
5871 }
5872
5873 # On Windows, generate a "mysettings.cpl" control panel applet. Control panel
5874 # applets are actually special shared libraries.
5875 if (is_win) {
5876 shared_library("mysettings") {
5877 output_extension = "cpl"
5878 ...
5879 }
5880 }
5881```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005882### **output_name**: Define a name for the output file other than the default.
Brett Wilson796ed472018-07-16 15:11:09 -07005883
5884```
5885 Normally the output name of a target will be based on the target name, so the
5886 target "//foo/bar:bar_unittests" will generate an output file such as
5887 "bar_unittests.exe" (using Windows as an example).
5888
5889 Sometimes you will want an alternate name to avoid collisions or if the
5890 internal name isn't appropriate for public distribution.
5891
5892 The output name should have no extension or prefixes, these will be added
5893 using the default system rules. For example, on Linux an output name of "foo"
5894 will produce a shared library "libfoo.so". There is no way to override the
5895 output prefix of a linker tool on a per- target basis. If you need more
5896 flexibility, create a copy target to produce the file you want.
5897
5898 This variable is valid for all binary output target types.
5899```
5900
5901#### **Example**
5902
5903```
5904 static_library("doom_melon") {
5905 output_name = "fluffy_bunny"
5906 }
5907```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005908### **output_prefix_override**: Don't use prefix for output name.
Brett Wilson796ed472018-07-16 15:11:09 -07005909
5910```
5911 A boolean that overrides the output prefix for a target. Defaults to false.
5912
5913 Some systems use prefixes for the names of the final target output file. The
5914 normal example is "libfoo.so" on Linux for a target named "foo".
5915
5916 The output prefix for a given target type is specified on the linker tool
5917 (see "gn help tool"). Sometimes this prefix is undesired.
5918
5919 See also "gn help output_extension".
5920```
5921
5922#### **Example**
5923
5924```
5925 shared_library("doom_melon") {
5926 # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag
5927 # will produce "doom_melon.so".
5928 output_prefix_override = true
5929 ...
5930 }
5931```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005932### **outputs**: Output files for actions and copy targets.
Brett Wilson796ed472018-07-16 15:11:09 -07005933
5934```
5935 Outputs is valid for "copy", "action", and "action_foreach" target types and
5936 indicates the resulting files. Outputs must always refer to files in the
5937 build directory.
5938
5939 copy
5940 Copy targets should have exactly one entry in the outputs list. If there is
5941 exactly one source, this can be a literal file name or a source expansion.
5942 If there is more than one source, this must contain a source expansion to
5943 map a single input name to a single output name. See "gn help copy".
5944
5945 action_foreach
5946 Action_foreach targets must always use source expansions to map input files
5947 to output files. There can be more than one output, which means that each
5948 invocation of the script will produce a set of files (presumably based on
5949 the name of the input file). See "gn help action_foreach".
5950
5951 action
5952 Action targets (excluding action_foreach) must list literal output file(s)
5953 with no source expansions. See "gn help action".
5954```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005955### **partial_info_plist**: [filename] Path plist from asset catalog compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005956
5957```
5958 Valid for create_bundle target, corresponds to the path for the partial
5959 Info.plist created by the asset catalog compiler that needs to be merged
5960 with the application Info.plist (usually done by the code signing script).
5961
5962 The file will be generated regardless of whether the asset compiler has
5963 been invoked or not. See "gn help create_bundle".
5964```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005965### **pool**: Label of the pool used by the action.
Brett Wilson796ed472018-07-16 15:11:09 -07005966
5967```
5968 A fully-qualified label representing the pool that will be used for the
5969 action. Pools are defined using the pool() {...} declaration.
5970```
5971
5972#### **Example**
5973
5974```
5975 action("action") {
5976 pool = "//build:custom_pool"
5977 ...
5978 }
5979```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005980### **precompiled_header**: [string] Header file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07005981
5982```
5983 Precompiled headers will be used when a target specifies this value, or a
5984 config applying to this target specifies this value. In addition, the tool
5985 corresponding to the source files must also specify precompiled headers (see
5986 "gn help tool"). The tool will also specify what type of precompiled headers
5987 to use, by setting precompiled_header_type to either "gcc" or "msvc".
5988
5989 The precompiled header/source variables can be specified on a target or a
5990 config, but must be the same for all configs applying to a given target since
5991 a target can only have one precompiled header.
5992
5993 If you use both C and C++ sources, the precompiled header and source file
5994 will be compiled once per language. You will want to make sure to wrap C++
5995 includes in __cplusplus #ifdefs so the file will compile in C mode.
5996```
5997
5998#### **GCC precompiled headers**
5999
6000```
6001 When using GCC-style precompiled headers, "precompiled_source" contains the
6002 path of a .h file that is precompiled and then included by all source files
6003 in targets that set "precompiled_source".
6004
6005 The value of "precompiled_header" is not used with GCC-style precompiled
6006 headers.
6007```
6008
6009#### **MSVC precompiled headers**
6010
6011```
6012 When using MSVC-style precompiled headers, the "precompiled_header" value is
6013 a string corresponding to the header. This is NOT a path to a file that GN
6014 recognises, but rather the exact string that appears in quotes after
6015 an #include line in source code. The compiler will match this string against
6016 includes or forced includes (/FI).
6017
6018 MSVC also requires a source file to compile the header with. This must be
6019 specified by the "precompiled_source" value. In contrast to the header value,
6020 this IS a GN-style file name, and tells GN which source file to compile to
6021 make the .pch file used for subsequent compiles.
6022
6023 For example, if the toolchain specifies MSVC headers:
6024
6025 toolchain("vc_x64") {
6026 ...
6027 tool("cxx") {
6028 precompiled_header_type = "msvc"
6029 ...
6030
6031 You might make a config like this:
6032
6033 config("use_precompiled_headers") {
6034 precompiled_header = "build/precompile.h"
6035 precompiled_source = "//build/precompile.cc"
6036
6037 # Either your source files should #include "build/precompile.h"
6038 # first, or you can do this to force-include the header.
6039 cflags = [ "/FI$precompiled_header" ]
6040 }
6041
6042 And then define a target that uses the config:
6043
6044 executable("doom_melon") {
6045 configs += [ ":use_precompiled_headers" ]
6046 ...
6047```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006048### **precompiled_header_type**: [string] "gcc" or "msvc".
Brett Wilson796ed472018-07-16 15:11:09 -07006049
6050```
6051 See "gn help precompiled_header".
6052```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006053### **precompiled_source**: [file name] Source file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07006054
6055```
6056 The source file that goes along with the precompiled_header when using
6057 "msvc"-style precompiled headers. It will be implicitly added to the sources
6058 of the target. See "gn help precompiled_header".
6059```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006060### **product_type**: Product type for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006061
6062```
6063 Correspond to the type of the product of a create_bundle target. Only
6064 meaningful to Xcode (used as part of the Xcode project generation).
6065
6066 When generating Xcode project files, only create_bundle target with a
6067 non-empty product_type will have a corresponding target in Xcode project.
6068```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006069### **public**: Declare public header files for a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006070
6071```
6072 A list of files that other targets can include. These permissions are checked
6073 via the "check" command (see "gn help check").
6074
6075 If no public files are declared, other targets (assuming they have visibility
6076 to depend on this target) can include any file in the sources list. If this
6077 variable is defined on a target, dependent targets may only include files on
6078 this whitelist unless that target is marked as a friend (see "gn help
6079 friend").
6080
6081 Header file permissions are also subject to visibility. A target must be
6082 visible to another target to include any files from it at all and the public
6083 headers indicate which subset of those files are permitted. See "gn help
6084 visibility" for more.
6085
6086 Public files are inherited through the dependency tree. So if there is a
6087 dependency A -> B -> C, then A can include C's public headers. However, the
6088 same is NOT true of visibility, so unless A is in C's visibility list, the
6089 include will be rejected.
6090
6091 GN only knows about files declared in the "sources" and "public" sections of
6092 targets. If a file is included that is not known to the build, it will be
6093 allowed.
6094
6095 It is common for test targets to need to include private headers for their
6096 associated code. In this case, list the test target in the "friend" list of
6097 the target that owns the private header to allow the inclusion. See
6098 "gn help friend" for more.
Julie Hockette2a29402018-07-31 10:11:42 -07006099
6100 When a binary target has no explicit or implicit public headers (a "public"
6101 list is defined but is empty), GN assumes that the target can not propagate
6102 any compile-time dependencies up the dependency tree. In this case, the build
6103 can be parallelized more efficiently.
6104 Say there are dependencies:
6105 A (shared library) -> B (shared library) -> C (action).
6106 Normally C must complete before any source files in A can compile (because
6107 there might be generated includes). But when B explicitly declares no public
6108 headers, C can execute in parallel with A's compile steps. C must still be
6109 complete before any dependents link.
Brett Wilson796ed472018-07-16 15:11:09 -07006110```
6111
6112#### **Examples**
6113
6114```
6115 These exact files are public:
6116 public = [ "foo.h", "bar.h" ]
6117
6118 No files are public (no targets may include headers from this one):
Julie Hockette2a29402018-07-31 10:11:42 -07006119 # This allows starting compilation in dependent targets earlier.
Brett Wilson796ed472018-07-16 15:11:09 -07006120 public = []
6121```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006122### **public_configs**: Configs to be applied on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07006123
6124```
6125 A list of config labels.
6126
6127 Targets directly depending on this one will have the configs listed in this
6128 variable added to them. These configs will also apply to the current target.
Julie Hockette2a29402018-07-31 10:11:42 -07006129 Generally, public configs are used to apply defines and include directories
6130 necessary to compile this target's header files.
Brett Wilson796ed472018-07-16 15:11:09 -07006131
Julie Hockette2a29402018-07-31 10:11:42 -07006132 See also "gn help all_dependent_configs".
6133```
6134
6135#### **Propagation of public configs**
6136
6137```
6138 Public configs are applied to all targets that depend directly on this one.
6139 These dependant targets can further push this target's public configs
6140 higher in the dependency tree by depending on it via public_deps (see "gn
6141 help public_deps").
6142
6143 static_library("toplevel") {
6144 # This target will get "my_config" applied to it. However, since this
6145 # target uses "deps" and not "public_deps", targets that depend on this
6146 # one won't get it.
6147 deps = [ ":intermediate" ]
6148 }
6149
6150 static_library("intermediate") {
6151 # Depending on "lower" in any way will apply "my_config" to this target.
6152 # Additionall, since this target depends on "lower" via public_deps,
6153 # targets that depend on this one will also get "my_config".
6154 public_deps = [ ":lower" ]
6155 }
6156
6157 static_library("lower") {
6158 # This will get applied to all targets that depend on this one.
6159 public_configs = [ ":my_config" ]
6160 }
6161
6162 Public config propagation happens in a second phase once a target and all of
6163 its dependencies have been resolved. Therefore, a target will not see these
Brett Wilson796ed472018-07-16 15:11:09 -07006164 force-added configs in their "configs" variable while the script is running,
6165 and they can not be removed. As a result, this capability should generally
Julie Hockette2a29402018-07-31 10:11:42 -07006166 only be used to add defines and include directories rather than setting
6167 complicated flags that some targets may not want.
Brett Wilson796ed472018-07-16 15:11:09 -07006168
Julie Hockette2a29402018-07-31 10:11:42 -07006169 Public configs may or may not be propagated across toolchain boundaries
6170 depending on the value of the propagates_configs flag (see "gn help
6171 toolchain") on the toolchain of the target declaring the public_config.
6172```
6173
6174#### **Avoiding applying public configs to this target**
6175
6176```
6177 If you want the config to apply to targets that depend on this one, but NOT
6178 this one, define an extra layer of indirection using a group:
6179
6180 # External targets depend on this group.
6181 group("my_target") {
6182 # Config to apply to all targets that depend on this one.
6183 public_configs = [ ":external_settings" ]
6184 deps = [ ":internal_target" ]
6185 }
6186
6187 # Internal target to actually compile the sources.
6188 static_library("internal_target") {
6189 # Force all external targets to depend on the group instead of directly
6190 # on this so the "external_settings" config will get applied.
6191 visibility = [ ":my_target" ]
6192 ...
6193 }
Brett Wilson796ed472018-07-16 15:11:09 -07006194```
6195
6196#### **Ordering of flags and values**
6197
6198```
6199 1. Those set on the current target (not in a config).
6200 2. Those set on the "configs" on the target in order that the
6201 configs appear in the list.
6202 3. Those set on the "all_dependent_configs" on the target in order
6203 that the configs appear in the list.
6204 4. Those set on the "public_configs" on the target in order that
6205 those configs appear in the list.
6206 5. all_dependent_configs pulled from dependencies, in the order of
6207 the "deps" list. This is done recursively. If a config appears
6208 more than once, only the first occurence will be used.
6209 6. public_configs pulled from dependencies, in the order of the
6210 "deps" list. If a dependency is public, they will be applied
6211 recursively.
6212```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006213### **public_deps**: Declare public dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07006214
6215```
6216 Public dependencies are like private dependencies (see "gn help deps") but
6217 additionally express that the current target exposes the listed deps as part
6218 of its public API.
6219
6220 This has several ramifications:
6221
6222 - public_configs that are part of the dependency are forwarded to direct
6223 dependents.
6224
6225 - Public headers in the dependency are usable by dependents (includes do
6226 not require a direct dependency or visibility).
6227
6228 - If the current target is a shared library, other shared libraries that it
6229 publicly depends on (directly or indirectly) are propagated up the
6230 dependency tree to dependents for linking.
Julie Hockette2a29402018-07-31 10:11:42 -07006231
6232 See also "gn help public_configs".
Brett Wilson796ed472018-07-16 15:11:09 -07006233```
6234
6235#### **Discussion**
6236
6237```
6238 Say you have three targets: A -> B -> C. C's visibility may allow B to depend
6239 on it but not A. Normally, this would prevent A from including any headers
6240 from C, and C's public_configs would apply only to B.
6241
6242 If B lists C in its public_deps instead of regular deps, A will now inherit
6243 C's public_configs and the ability to include C's public headers.
6244
6245 Generally if you are writing a target B and you include C's headers as part
6246 of B's public headers, or targets depending on B should consider B and C to
6247 be part of a unit, you should use public_deps instead of deps.
6248```
6249
6250#### **Example**
6251
6252```
6253 # This target can include files from "c" but not from
6254 # "super_secret_implementation_details".
6255 executable("a") {
6256 deps = [ ":b" ]
6257 }
6258
6259 shared_library("b") {
6260 deps = [ ":super_secret_implementation_details" ]
6261 public_deps = [ ":c" ]
6262 }
6263```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006264### **rebase**: Rebase collected metadata as files.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006265
6266```
6267 A boolean that triggers a rebase of collected metadata strings based on their
6268 declared file. Defaults to false.
6269
6270 Metadata generally declares files as strings relative to the local build file.
6271 However, this data is often used in other contexts, and so setting this flag
6272 will force the metadata collection to be rebased according to the local build
6273 file's location and thus allow the filename to be used anywhere.
6274
6275 Setting this flag will raise an error if any target's specified metadata is
6276 not a string value.
6277
6278 See also "gn help generated_file".
6279```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006280### **response_file_contents**: Contents of a response file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006281
6282```
6283 Sometimes the arguments passed to a script can be too long for the system's
6284 command-line capabilities. This is especially the case on Windows where the
6285 maximum command-line length is less than 8K. A response file allows you to
6286 pass an unlimited amount of data to a script in a temporary file for an
6287 action or action_foreach target.
6288
6289 If the response_file_contents variable is defined and non-empty, the list
6290 will be treated as script args (including possibly substitution patterns)
6291 that will be written to a temporary file at build time. The name of the
6292 temporary file will be substituted for "{{response_file_name}}" in the script
6293 args.
6294
6295 The response file contents will always be quoted and escaped according to
6296 Unix shell rules. To parse the response file, the Python script should use
6297 "shlex.split(file_contents)".
6298```
6299
6300#### **Example**
6301
6302```
6303 action("process_lots_of_files") {
6304 script = "process.py",
6305 inputs = [ ... huge list of files ... ]
6306
6307 # Write all the inputs to a response file for the script. Also,
6308 # make the paths relative to the script working directory.
6309 response_file_contents = rebase_path(inputs, root_build_dir)
6310
6311 # The script expects the name of the response file in --file-list.
6312 args = [
6313 "--enable-foo",
6314 "--file-list={{response_file_name}}",
6315 ]
6316 }
6317```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006318### **script**: Script file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006319
6320```
6321 An absolute or buildfile-relative file name of a Python script to run for a
6322 action and action_foreach targets (see "gn help action" and "gn help
6323 action_foreach").
6324```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006325### **sources**: Source files for a target
Brett Wilson796ed472018-07-16 15:11:09 -07006326
6327```
6328 A list of files. Non-absolute paths will be resolved relative to the current
6329 build file.
6330```
6331
6332#### **Sources for binary targets**
6333
6334```
6335 For binary targets (source sets, executables, and libraries), the known file
6336 types will be compiled with the associated tools. Unknown file types and
6337 headers will be skipped. However, you should still list all C/C+ header files
6338 so GN knows about the existence of those files for the purposes of include
6339 checking.
6340
6341 As a special case, a file ending in ".def" will be treated as a Windows
6342 module definition file. It will be appended to the link line with a
6343 preceding "/DEF:" string. There must be at most one .def file in a target
6344 and they do not cross dependency boundaries (so specifying a .def file in a
6345 static library or source set will have no effect on the executable or shared
6346 library they're linked into).
Julie Hockettce1fa072019-05-07 17:44:37 -07006347
6348 For Rust targets that do not specify a crate_root, then the crate_root will
6349 look for a lib.rs file (or main.rs for executable) or a single file in
6350 sources, if sources contains only one file.
Brett Wilson796ed472018-07-16 15:11:09 -07006351```
6352
6353#### **Sources for non-binary targets**
6354
6355```
6356 action_foreach
6357 The sources are the set of files that the script will be executed over. The
6358 script will run once per file.
6359
6360 action
6361 The sources will be treated the same as inputs. See "gn help inputs" for
6362 more information and usage advice.
6363
6364 copy
6365 The source are the source files to copy.
6366```
Sylvain Defresne89e64252020-08-07 13:01:06 +02006367### **swiftflags**: Flags passed to the swift compiler.
6368
6369```
6370 A list of strings.
6371
6372 "swiftflags" are passed to any invocation of a tool that takes an .swift
6373 file as input.
6374```
6375
6376#### **Ordering of flags and values**
6377
6378```
6379 1. Those set on the current target (not in a config).
6380 2. Those set on the "configs" on the target in order that the
6381 configs appear in the list.
6382 3. Those set on the "all_dependent_configs" on the target in order
6383 that the configs appear in the list.
6384 4. Those set on the "public_configs" on the target in order that
6385 those configs appear in the list.
6386 5. all_dependent_configs pulled from dependencies, in the order of
6387 the "deps" list. This is done recursively. If a config appears
6388 more than once, only the first occurence will be used.
6389 6. public_configs pulled from dependencies, in the order of the
6390 "deps" list. If a dependency is public, they will be applied
6391 recursively.
6392```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006393### **testonly**: Declares a target must only be used for testing.
Brett Wilson796ed472018-07-16 15:11:09 -07006394
6395```
6396 Boolean. Defaults to false.
6397
6398 When a target is marked "testonly = true", it must only be depended on by
6399 other test-only targets. Otherwise, GN will issue an error that the
6400 depenedency is not allowed.
6401
6402 This feature is intended to prevent accidentally shipping test code in a
6403 final product.
6404```
6405
6406#### **Example**
6407
6408```
6409 source_set("test_support") {
6410 testonly = true
6411 ...
6412 }
6413```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006414### **visibility**: A list of labels that can depend on a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006415
6416```
6417 A list of labels and label patterns that define which targets can depend on
6418 the current one. These permissions are checked via the "check" command (see
6419 "gn help check").
6420
6421 If visibility is not defined, it defaults to public ("*").
6422
6423 If visibility is defined, only the targets with labels that match it can
6424 depend on the current target. The empty list means no targets can depend on
6425 the current target.
6426
6427 Tip: Often you will want the same visibility for all targets in a BUILD file.
6428 In this case you can just put the definition at the top, outside of any
6429 target, and the targets will inherit that scope and see the definition.
6430```
6431
6432#### **Patterns**
6433
6434```
6435 See "gn help label_pattern" for more details on what types of patterns are
6436 supported. If a toolchain is specified, only targets in that toolchain will
6437 be matched. If a toolchain is not specified on a pattern, targets in all
6438 toolchains will be matched.
6439```
6440
6441#### **Examples**
6442
6443```
6444 Only targets in the current buildfile ("private"):
6445 visibility = [ ":*" ]
6446
6447 No targets (used for targets that should be leaf nodes):
6448 visibility = []
6449
6450 Any target ("public", the default):
6451 visibility = [ "*" ]
6452
6453 All targets in the current directory and any subdirectory:
6454 visibility = [ "./*" ]
6455
6456 Any target in "//bar/BUILD.gn":
6457 visibility = [ "//bar:*" ]
6458
6459 Any target in "//bar/" or any subdirectory thereof:
6460 visibility = [ "//bar/*" ]
6461
6462 Just these specific targets:
6463 visibility = [ ":mything", "//foo:something_else" ]
6464
6465 Any target in the current directory and any subdirectory thereof, plus
6466 any targets in "//bar/" and any subdirectory thereof.
6467 visibility = [ "./*", "//bar/*" ]
6468```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006469### **walk_keys**: Key(s) for managing the metadata collection walk.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006470
6471```
Julie Hockett152c5142019-07-12 09:53:43 -06006472 Defaults to [""].
Nate Fischer8ed01d32019-01-08 17:32:01 -08006473
6474 These keys are used to control the next step in a collection walk, acting as
6475 barriers. If a specified key is defined in a target's metadata, the walk will
6476 use the targets listed in that value to determine which targets are walked.
6477
Julie Hockett152c5142019-07-12 09:53:43 -06006478 If no walk_keys are specified for a generated_file target (i.e. "[""]"), the
Nate Fischer8ed01d32019-01-08 17:32:01 -08006479 walk will touch all deps and data_deps of the specified target recursively.
6480
6481 See "gn help generated_file".
6482```
Robert Sesekd0a6f072020-05-15 11:21:22 -04006483### **weak_frameworks**: [name list] Name of frameworks that must be weak linked.
6484
6485```
6486 A list of framework names.
6487
6488 The frameworks named in that list will be weak linked with any dynamic link
6489 type target. Weak linking instructs the dynamic loader to attempt to load
6490 the framework, but if it is not able to do so, it leaves any imported symbols
6491 unresolved. This is typically used when a framework is present in a new
6492 version of an SDK but not on older versions of the OS that the software runs
6493 on.
6494```
6495
6496#### **Ordering of flags and values**
6497
6498```
6499 1. Those set on the current target (not in a config).
6500 2. Those set on the "configs" on the target in order that the
6501 configs appear in the list.
6502 3. Those set on the "all_dependent_configs" on the target in order
6503 that the configs appear in the list.
6504 4. Those set on the "public_configs" on the target in order that
6505 those configs appear in the list.
6506 5. all_dependent_configs pulled from dependencies, in the order of
6507 the "deps" list. This is done recursively. If a config appears
6508 more than once, only the first occurence will be used.
6509 6. public_configs pulled from dependencies, in the order of the
6510 "deps" list. If a dependency is public, they will be applied
6511 recursively.
6512```
6513
6514#### **Example**
6515
6516```
6517 weak_frameworks = [ "OnlyOnNewerOSes.framework" ]
6518```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006519### **write_runtime_deps**: Writes the target's runtime_deps to the given path.
Brett Wilson796ed472018-07-16 15:11:09 -07006520
6521```
6522 Does not synchronously write the file, but rather schedules it to be written
6523 at the end of generation.
6524
6525 If the file exists and the contents are identical to that being written, the
6526 file will not be updated. This will prevent unnecessary rebuilds of targets
6527 that depend on this file.
6528
6529 Path must be within the output directory.
6530
6531 See "gn help runtime_deps" for how the runtime dependencies are computed.
6532
6533 The format of this file will list one file per line with no escaping. The
6534 files will be relative to the root_build_dir. The first line of the file will
6535 be the main output file of the target itself. The file contents will be the
6536 same as requesting the runtime deps be written on the command line (see "gn
6537 help --runtime-deps-list-file").
6538```
Harley Li0a9affb2020-06-03 10:38:42 -04006539### **xcasset_compiler_flags**: Flags passed to xcassets compiler.
6540
6541```
6542 A list of strings.
6543
6544 Valid for create_bundle target. Those flags are directly passed to
6545 xcassets compiler, corresponding to {{xcasset_compiler_flags}} substitution
6546 in compile_xcassets tool.
6547```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006548### **xcode_extra_attributes**: [scope] Extra attributes for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006549
6550```
6551 The value defined in this scope will be copied to the EXTRA_ATTRIBUTES
6552 property of the generated Xcode project. They are only meaningful when
6553 generating with --ide=xcode.
6554
6555 See "gn help create_bundle" for more information.
6556```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006557### **xcode_test_application_name**: Name for Xcode test target.
Brett Wilson796ed472018-07-16 15:11:09 -07006558
6559```
6560 Each unit and ui test target must have a test application target, and this
6561 value is used to specify the relationship. Only meaningful to Xcode (used as
6562 part of the Xcode project generation).
6563
6564 See "gn help create_bundle" for more information.
6565```
6566
Julie Hockette2a29402018-07-31 10:11:42 -07006567#### **Example**
Brett Wilson796ed472018-07-16 15:11:09 -07006568
6569```
6570 create_bundle("chrome_xctest") {
6571 test_application_name = "chrome"
6572 ...
6573 }
6574```
6575## Other help topics
6576
6577### **Build Arguments Overview**
6578
6579```
6580 Build arguments are variables passed in from outside of the build that build
6581 files can query to determine how the build works.
6582```
6583
6584#### **How build arguments are set**
6585
6586```
6587 First, system default arguments are set based on the current system. The
6588 built-in arguments are:
6589 - host_cpu
6590 - host_os
6591 - current_cpu
6592 - current_os
6593 - target_cpu
6594 - target_os
6595
6596 Next, project-specific overrides are applied. These are specified inside
6597 the default_args variable of //.gn. See "gn help dotfile" for more.
6598
6599 If specified, arguments from the --args command line flag are used. If that
6600 flag is not specified, args from previous builds in the build directory will
6601 be used (this is in the file args.gn in the build directory).
6602
6603 Last, for targets being compiled with a non-default toolchain, the toolchain
6604 overrides are applied. These are specified in the toolchain_args section of a
6605 toolchain definition. The use-case for this is that a toolchain may be
6606 building code for a different platform, and that it may want to always
6607 specify Posix, for example. See "gn help toolchain" for more.
6608
6609 If you specify an override for a build argument that never appears in a
6610 "declare_args" call, a nonfatal error will be displayed.
6611```
6612
6613#### **Examples**
6614
6615```
6616 gn args out/FooBar
6617 Create the directory out/FooBar and open an editor. You would type
6618 something like this into that file:
6619 enable_doom_melon=false
6620 os="android"
6621
6622 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
6623 This will overwrite the build directory with the given arguments. (Note
6624 that the quotes inside the args command will usually need to be escaped
6625 for your shell to pass through strings values.)
6626```
6627
6628#### **How build arguments are used**
6629
6630```
6631 If you want to use an argument, you use declare_args() and specify default
6632 values. These default values will apply if none of the steps listed in the
6633 "How build arguments are set" section above apply to the given argument, but
6634 the defaults will not override any of these.
6635
6636 Often, the root build config file will declare global arguments that will be
6637 passed to all buildfiles. Individual build files can also specify arguments
6638 that apply only to those files. It is also useful to specify build args in an
6639 "import"-ed file if you want such arguments to apply to multiple buildfiles.
6640```
6641### **.gn file**
6642
6643```
6644 When gn starts, it will search the current directory and parent directories
6645 for a file called ".gn". This indicates the source root. You can override
6646 this detection by using the --root command-line argument
6647
6648 The .gn file in the source root will be executed. The syntax is the same as a
6649 buildfile, but with very limited build setup-specific meaning.
6650
6651 If you specify --root, by default GN will look for the file .gn in that
6652 directory. If you want to specify a different file, you can additionally pass
6653 --dotfile:
6654
6655 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
6656```
6657
6658#### **Variables**
6659
6660```
6661 arg_file_template [optional]
6662 Path to a file containing the text that should be used as the default
6663 args.gn content when you run `gn args`.
6664
6665 buildconfig [required]
6666 Path to the build config file. This file will be used to set up the
6667 build file execution environment for each toolchain.
6668
6669 check_targets [optional]
6670 A list of labels and label patterns that should be checked when running
Erik Staab5d109d72020-06-29 22:53:23 -07006671 "gn check" or "gn gen --check". If neither check_targets or
6672 no_check_targets (see below) is specified, all targets will be checked.
6673 It is an error to specify both check_targets and no_check_targets. If it
6674 is the empty list, no targets will be checked. To bypass this list,
6675 request an explicit check of targets, like "//*".
6676
6677 The format of this list is identical to that of "visibility" so see "gn
6678 help visibility" for examples.
6679
6680 no_check_targets [optional]
6681 A list of labels and label patterns that should *not* be checked when
6682 running "gn check" or "gn gen --check". All other targets will be checked.
6683 If neither check_targets (see above) or no_check_targets is specified, all
6684 targets will be checked. It is an error to specify both check_targets and
6685 no_check_targets.
Brett Wilson796ed472018-07-16 15:11:09 -07006686
6687 The format of this list is identical to that of "visibility" so see "gn
6688 help visibility" for examples.
6689
James Robinson49f59032020-01-08 14:05:51 -08006690 check_system_includes [optional]
6691 Boolean to control whether system style includes are checked by default
6692 when running "gn check" or "gn gen --check". System style includes are
6693 includes that use angle brackets <> instead of double quotes "". If this
6694 setting is omitted or set to false, these includes will be ignored by
6695 default. They can be checked explicitly by running
6696 "gn check --check-system" or "gn gen --check=system"
6697
Brett Wilson796ed472018-07-16 15:11:09 -07006698 exec_script_whitelist [optional]
6699 A list of .gn/.gni files (not labels) that have permission to call the
6700 exec_script function. If this list is defined, calls to exec_script will
6701 be checked against this list and GN will fail if the current file isn't
6702 in the list.
6703
6704 This is to allow the use of exec_script to be restricted since is easy to
6705 use inappropriately. Wildcards are not supported. Files in the
6706 secondary_source tree (if defined) should be referenced by ignoring the
6707 secondary tree and naming them as if they are in the main tree.
6708
6709 If unspecified, the ability to call exec_script is unrestricted.
6710
6711 Example:
6712 exec_script_whitelist = [
6713 "//base/BUILD.gn",
6714 "//build/my_config.gni",
6715 ]
6716
6717 root [optional]
6718 Label of the root build target. The GN build will start by loading the
6719 build file containing this target name. This defaults to "//:" which will
Harley Li0a9affb2020-06-03 10:38:42 -04006720 cause the file //BUILD.gn to be loaded. Note that build_file_extension
6721 applies to the default case as well.
Brett Wilson796ed472018-07-16 15:11:09 -07006722
Nico Weberad9eab22020-11-15 22:20:28 -05006723 The command-line switch --root-target will override this value (see "gn
6724 help --root-target").
6725
Brett Wilson796ed472018-07-16 15:11:09 -07006726 script_executable [optional]
James Robinson61377e32020-02-13 15:20:07 -08006727 Path to specific Python executable or other interpreter to use in
6728 action targets and exec_script calls. By default GN searches the
6729 PATH for Python to execute these scripts.
6730
6731 If set to the empty string, the path specified in action targets
6732 and exec_script calls will be executed directly.
Brett Wilson796ed472018-07-16 15:11:09 -07006733
6734 secondary_source [optional]
6735 Label of an alternate directory tree to find input files. When searching
6736 for a BUILD.gn file (or the build config file discussed above), the file
6737 will first be looked for in the source root. If it's not found, the
6738 secondary source root will be checked (which would contain a parallel
6739 directory hierarchy).
6740
6741 This behavior is intended to be used when BUILD.gn files can't be checked
6742 in to certain source directories for whatever reason.
6743
6744 The secondary source root must be inside the main source tree.
6745
6746 default_args [optional]
6747 Scope containing the default overrides for declared arguments. These
6748 overrides take precedence over the default values specified in the
6749 declare_args() block, but can be overriden using --args or the
6750 args.gn file.
6751
6752 This is intended to be used when subprojects declare arguments with
6753 default values that need to be changed for whatever reason.
Harley Li0a9affb2020-06-03 10:38:42 -04006754
6755 build_file_extension [optional]
6756 If set to a non-empty string, this is added to the name of all build files
6757 to load.
6758 GN will look for build files named "BUILD.$build_file_extension.gn".
6759 This is intended to be used during migrations or other situations where
6760 there are two independent GN builds in the same directories.
Petr Hosek317fdc12020-05-28 10:23:42 -07006761
6762 ninja_required_version [optional]
6763 When set specifies the minimum required version of Ninja. The default
6764 required version is 1.7.2. Specifying a higher version might enable the
6765 use of some of newer features that can make the build more efficient.
Brett Wilson796ed472018-07-16 15:11:09 -07006766```
6767
6768#### **Example .gn file contents**
6769
6770```
6771 buildconfig = "//build/config/BUILDCONFIG.gn"
6772
6773 check_targets = [
6774 "//doom_melon/*", # Check everything in this subtree.
6775 "//tools:mind_controlling_ant", # Check this specific target.
6776 ]
6777
6778 root = "//:root"
6779
6780 secondary_source = "//build/config/temporary_buildfiles/"
6781
6782 default_args = {
6783 # Default to release builds for this project.
6784 is_debug = false
6785 is_component_build = false
6786 }
6787```
6788### **Build graph and execution overview**
6789
6790#### **Overall build flow**
6791
6792```
6793 1. Look for ".gn" file (see "gn help dotfile") in the current directory and
6794 walk up the directory tree until one is found. Set this directory to be
6795 the "source root" and interpret this file to find the name of the build
6796 config file.
6797
6798 2. Execute the build config file identified by .gn to set up the global
6799 variables and default toolchain name. Any arguments, variables, defaults,
6800 etc. set up in this file will be visible to all files in the build.
6801
6802 3. Load the //BUILD.gn (in the source root directory).
6803
6804 4. Recursively evaluate rules and load BUILD.gn in other directories as
6805 necessary to resolve dependencies. If a BUILD file isn't found in the
6806 specified location, GN will look in the corresponding location inside
6807 the secondary_source defined in the dotfile (see "gn help dotfile").
6808
6809 5. When a target's dependencies are resolved, write out the `.ninja`
6810 file to disk.
6811
6812 6. When all targets are resolved, write out the root build.ninja file.
Harley Li0a9affb2020-06-03 10:38:42 -04006813
6814 Note that the BUILD.gn file name may be modulated by .gn arguments such as
6815 build_file_extension.
Brett Wilson796ed472018-07-16 15:11:09 -07006816```
6817
6818#### **Executing target definitions and templates**
6819
6820```
6821 Build files are loaded in parallel. This means it is impossible to
6822 interrogate a target from GN code for any information not derivable from its
6823 label (see "gn help label"). The exception is the get_target_outputs()
6824 function which requires the target being interrogated to have been defined
6825 previously in the same file.
6826
6827 Targets are declared by their type and given a name:
6828
6829 static_library("my_static_library") {
6830 ... target parameter definitions ...
6831 }
6832
6833 There is also a generic "target" function for programmatically defined types
6834 (see "gn help target"). You can define new types using templates (see "gn
6835 help template"). A template defines some custom code that expands to one or
6836 more other targets.
6837
6838 Before executing the code inside the target's { }, the target defaults are
6839 applied (see "gn help set_defaults"). It will inject implicit variable
6840 definitions that can be overridden by the target code as necessary. Typically
6841 this mechanism is used to inject a default set of configs that define the
6842 global compiler and linker flags.
6843```
6844
6845#### **Which targets are built**
6846
6847```
6848 All targets encountered in the default toolchain (see "gn help toolchain")
6849 will have build rules generated for them, even if no other targets reference
6850 them. Their dependencies must resolve and they will be added to the implicit
6851 "all" rule (see "gn help ninja_rules").
6852
6853 Targets in non-default toolchains will only be generated when they are
6854 required (directly or transitively) to build a target in the default
6855 toolchain.
6856
6857 See also "gn help ninja_rules".
6858```
6859
6860#### **Dependencies**
6861
6862```
6863 The only difference between "public_deps" and "deps" except for pushing
6864 configs around the build tree and allowing includes for the purposes of "gn
6865 check".
6866
6867 A target's "data_deps" are guaranteed to be built whenever the target is
6868 built, but the ordering is not defined. The meaning of this is dependencies
6869 required at runtime. Currently data deps will be complete before the target
6870 is linked, but this is not semantically guaranteed and this is undesirable
6871 from a build performance perspective. Since we hope to change this in the
6872 future, do not rely on this behavior.
6873```
6874### **Language and grammar for GN build files**
6875
6876#### **Tokens**
6877
6878```
6879 GN build files are read as sequences of tokens. While splitting the file
6880 into tokens, the next token is the longest sequence of characters that form a
6881 valid token.
6882```
6883
6884#### **White space and comments**
6885
6886```
6887 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
6888 carriage returns (U+000D), and newlines (U+000A).
6889
6890 Comments start at the character "#" and stop at the next newline.
6891
6892 White space and comments are ignored except that they may separate tokens
6893 that would otherwise combine into a single token.
6894```
6895
6896#### **Identifiers**
6897
6898```
6899 Identifiers name variables and functions.
6900
6901 identifier = letter { letter | digit } .
6902 letter = "A" ... "Z" | "a" ... "z" | "_" .
6903 digit = "0" ... "9" .
6904```
6905
6906#### **Keywords**
6907
6908```
6909 The following keywords are reserved and may not be used as identifiers:
6910
6911 else false if true
6912```
6913
6914#### **Integer literals**
6915
6916```
6917 An integer literal represents a decimal integer value.
6918
6919 integer = [ "-" ] digit { digit } .
6920
6921 Leading zeros and negative zero are disallowed.
6922```
6923
6924#### **String literals**
6925
6926```
6927 A string literal represents a string value consisting of the quoted
6928 characters with possible escape sequences and variable expansions.
6929
6930 string = `"` { char | escape | expansion } `"` .
6931 escape = `\` ( "$" | `"` | char ) .
Nico Weberad9eab22020-11-15 22:20:28 -05006932 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" .
Brett Wilson796ed472018-07-16 15:11:09 -07006933 Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f]
6934 expansion = "$" ( identifier | BracketExpansion | Hex ) .
Nico Weberad9eab22020-11-15 22:20:28 -05006935 char = /* any character except "$", `"`, or newline */ .
Brett Wilson796ed472018-07-16 15:11:09 -07006936
6937 After a backslash, certain sequences represent special characters:
6938
6939 \" U+0022 quotation mark
6940 \$ U+0024 dollar sign
6941 \\ U+005C backslash
6942
6943 All other backslashes represent themselves.
6944
6945 To insert an arbitrary byte value, use $0xFF. For example, to insert a
6946 newline character: "Line one$0x0ALine two".
6947
6948 An expansion will evaluate the variable following the '$' and insert a
6949 stringified version of it into the result. For example, to concat two path
6950 components with a slash separating them:
6951 "$var_one/$var_two"
6952 Use the "${var_one}" format to be explicitly deliniate the variable for
6953 otherwise-ambiguous cases.
6954```
6955
6956#### **Punctuation**
6957
6958```
6959 The following character sequences represent punctuation:
6960
6961 + += == != ( )
6962 - -= < <= [ ]
6963 ! = > >= { }
6964 && || . ,
6965```
6966
6967#### **Grammar**
6968
6969```
6970 The input tokens form a syntax tree following a context-free grammar:
6971
6972 File = StatementList .
6973
6974 Statement = Assignment | Call | Condition .
6975 LValue = identifier | ArrayAccess | ScopeAccess .
6976 Assignment = LValue AssignOp Expr .
6977 Call = identifier "(" [ ExprList ] ")" [ Block ] .
6978 Condition = "if" "(" Expr ")" Block
6979 [ "else" ( Condition | Block ) ] .
6980 Block = "{" StatementList "}" .
6981 StatementList = { Statement } .
6982
6983 ArrayAccess = identifier "[" Expr "]" .
6984 ScopeAccess = identifier "." identifier .
6985 Expr = UnaryExpr | Expr BinaryOp Expr .
6986 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
6987 PrimaryExpr = identifier | integer | string | Call
6988 | ArrayAccess | ScopeAccess | Block
6989 | "(" Expr ")"
6990 | "[" [ ExprList [ "," ] ] "]" .
6991 ExprList = Expr { "," Expr } .
6992
6993 AssignOp = "=" | "+=" | "-=" .
6994 UnaryOp = "!" .
6995 BinaryOp = "+" | "-" // highest priority
6996 | "<" | "<=" | ">" | ">="
6997 | "==" | "!="
6998 | "&&"
6999 | "||" . // lowest priority
7000
7001 All binary operators are left-associative.
7002```
7003
7004#### **Types**
7005
7006```
7007 The GN language is dynamically typed. The following types are used:
7008
7009 - Boolean: Uses the keywords "true" and "false". There is no implicit
7010 conversion between booleans and integers.
7011
7012 - Integers: All numbers in GN are signed 64-bit integers.
7013
7014 - Strings: Strings are 8-bit with no enforced encoding. When a string is
7015 used to interact with other systems with particular encodings (like the
7016 Windows and Mac filesystems) it is assumed to be UTF-8. See "String
7017 literals" above for more.
7018
7019 - Lists: Lists are arbitrary-length ordered lists of values. See "Lists"
7020 below for more.
7021
7022 - Scopes: Scopes are like dictionaries that use variable names for keys. See
7023 "Scopes" below for more.
7024```
7025
7026#### **Lists**
7027
7028```
7029 Lists are created with [] and using commas to separate items:
7030
7031 mylist = [ 0, 1, 2, "some string" ]
7032
7033 A comma after the last item is optional. Lists are dereferenced using 0-based
7034 indexing:
7035
7036 mylist[0] += 1
7037 var = mylist[2]
7038
7039 Lists can be concatenated using the '+' and '+=' operators. Bare values can
7040 not be concatenated with lists, to add a single item, it must be put into a
7041 list of length one.
7042
7043 Items can be removed from lists using the '-' and '-=' operators. This will
7044 remove all occurrences of every item in the right-hand list from the
7045 left-hand list. It is an error to remove an item not in the list. This is to
7046 prevent common typos and to detect dead code that is removing things that no
7047 longer apply.
7048
7049 It is an error to use '=' to replace a nonempty list with another nonempty
7050 list. This is to prevent accidentally overwriting data when in most cases
7051 '+=' was intended. To overwrite a list on purpose, first assign it to the
7052 empty list:
7053
7054 mylist = []
7055 mylist = otherlist
Brett Wilson796ed472018-07-16 15:11:09 -07007056```
7057
7058#### **Scopes**
7059
7060```
7061 All execution happens in the context of a scope which holds the current state
7062 (like variables). With the exception of loops and conditions, '{' introduces
7063 a new scope that has a parent reference to the old scope.
7064
7065 Variable reads recursively search all nested scopes until the variable is
7066 found or there are no more scopes. Variable writes always go into the current
7067 scope. This means that after the closing '}' (again excepting loops and
7068 conditions), all local variables will be restored to the previous values.
7069 This also means that "foo = foo" can do useful work by copying a variable
7070 into the current scope that was defined in a containing scope.
7071
7072 Scopes can also be assigned to variables. Such scopes can be created by
7073 functions like exec_script, when invoking a template (the template code
7074 refers to the variables set by the invoking code by the implicitly-created
7075 "invoker" scope), or explicitly like:
7076
7077 empty_scope = {}
7078 myvalues = {
7079 foo = 21
7080 bar = "something"
7081 }
7082
7083 Inside such a scope definition can be any GN code including conditionals and
7084 function calls. After the close of the scope, it will contain all variables
7085 explicitly set by the code contained inside it. After this, the values can be
7086 read, modified, or added to:
7087
7088 myvalues.foo += 2
7089 empty_scope.new_thing = [ 1, 2, 3 ]
Nate Fischer8ed01d32019-01-08 17:32:01 -08007090
7091 Scope equality is defined as single-level scopes identical within the current
7092 scope. That is, all values in the first scope must be present and identical
7093 within the second, and vice versa. Note that this means inherited scopes are
7094 always unequal by definition.
Brett Wilson796ed472018-07-16 15:11:09 -07007095```
Julie Hockettd69a9c32019-01-23 14:36:18 -08007096### **Input and output conversion**
Brett Wilson796ed472018-07-16 15:11:09 -07007097
7098```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007099 Input and output conversions are arguments to file and process functions
7100 that specify how to convert data to or from external formats. The possible
7101 values for parameters specifying conversions are:
7102
Brett Wilson796ed472018-07-16 15:11:09 -07007103 "" (the default)
Julie Hockett09171292018-07-31 14:35:10 -07007104 input: Discard the result and return None.
7105
7106 output: If value is a list, then "list lines"; otherwise "value".
Brett Wilson796ed472018-07-16 15:11:09 -07007107
7108 "list lines"
Julie Hockett09171292018-07-31 14:35:10 -07007109 input:
7110 Return the file contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007111 newlines will not be present in the result. The last line may or may
7112 not end in a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007113
Julie Hockett09171292018-07-31 14:35:10 -07007114 After splitting, each individual line will be trimmed of whitespace on
7115 both ends.
7116
7117 output:
7118 Renders the value contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007119 newlines will not be present in the result. The last line will end in
7120 with a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007121
7122 "scope"
Julie Hockett09171292018-07-31 14:35:10 -07007123 input:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007124 Execute the block as GN code and return a scope with the resulting
7125 values in it. If the input was:
Julie Hockett09171292018-07-31 14:35:10 -07007126 a = [ "hello.cc", "world.cc" ]
7127 b = 26
7128 and you read the result into a variable named "val", then you could
7129 access contents the "." operator on "val":
7130 sources = val.a
7131 some_count = val.b
7132
7133 output:
7134 Renders the value contents as a GN code block, reversing the input
7135 result above.
Brett Wilson796ed472018-07-16 15:11:09 -07007136
7137 "string"
Julie Hockett09171292018-07-31 14:35:10 -07007138 input: Return the file contents into a single string.
7139
7140 output:
7141 Render the value contents into a single string. The output is:
7142 a string renders with quotes, e.g. "str"
7143 an integer renders as a stringified integer, e.g. "6"
7144 a boolean renders as the associated string, e.g. "true"
7145 a list renders as a representation of its contents, e.g. "[\"str\", 6]"
7146 a scope renders as a GN code block of its values. If the Value was:
7147 Value val;
7148 val.a = [ "hello.cc", "world.cc" ];
7149 val.b = 26
7150 the resulting output would be:
7151 "{
7152 a = [ \"hello.cc\", \"world.cc\" ]
7153 b = 26
7154 }"
Brett Wilson796ed472018-07-16 15:11:09 -07007155
7156 "value"
Julie Hockett09171292018-07-31 14:35:10 -07007157 input:
7158 Parse the input as if it was a literal rvalue in a buildfile. Examples of
7159 typical program output using this mode:
7160 [ "foo", "bar" ] (result will be a list)
7161 or
7162 "foo bar" (result will be a string)
7163 or
7164 5 (result will be an integer)
Brett Wilson796ed472018-07-16 15:11:09 -07007165
Julie Hockett09171292018-07-31 14:35:10 -07007166 Note that if the input is empty, the result will be a null value which
7167 will produce an error if assigned to a variable.
7168
7169 output:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007170 Render the value contents as a literal rvalue. Strings render with
7171 escaped quotes.
Brett Wilson796ed472018-07-16 15:11:09 -07007172
7173 "json"
Julie Hockett09171292018-07-31 14:35:10 -07007174 input: Parse the input as a JSON and convert it to equivalent GN rvalue.
7175
7176 output: Convert the Value to equivalent JSON value.
7177
7178 The data type mapping is:
Brett Wilson796ed472018-07-16 15:11:09 -07007179 a string in JSON maps to string in GN
7180 an integer in JSON maps to integer in GN
7181 a float in JSON is unsupported and will result in an error
7182 an object in JSON maps to scope in GN
7183 an array in JSON maps to list in GN
7184 a boolean in JSON maps to boolean in GN
7185 a null in JSON is unsupported and will result in an error
7186
Julie Hockett09171292018-07-31 14:35:10 -07007187 Nota that the input dictionary keys have to be valid GN identifiers
7188 otherwise they will produce an error.
Brett Wilson796ed472018-07-16 15:11:09 -07007189
Julie Hockett09171292018-07-31 14:35:10 -07007190 "trim ..." (input only)
Brett Wilson796ed472018-07-16 15:11:09 -07007191 Prefixing any of the other transformations with the word "trim" will
7192 result in whitespace being trimmed from the beginning and end of the
7193 result before processing.
7194
7195 Examples: "trim string" or "trim list lines"
7196
7197 Note that "trim value" is useless because the value parser skips
7198 whitespace anyway.
7199```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01007200### **File patterns**
7201
7202```
7203 File patterns are VERY limited regular expressions. They must match the
7204 entire input string to be counted as a match. In regular expression parlance,
7205 there is an implicit "^...$" surrounding your input. If you want to match a
7206 substring, you need to use wildcards at the beginning and end.
7207
7208 There are only two special tokens understood by the pattern matcher.
7209 Everything else is a literal.
7210
7211 - "*" Matches zero or more of any character. It does not depend on the
7212 preceding character (in regular expression parlance it is equivalent to
7213 ".*").
7214
7215 - "\b" Matches a path boundary. This will match the beginning or end of a
7216 string, or a slash.
7217```
7218
7219#### **Pattern examples**
7220
7221```
7222 "*asdf*"
7223 Matches a string containing "asdf" anywhere.
7224
7225 "asdf"
7226 Matches only the exact string "asdf".
7227
7228 "*.cc"
7229 Matches strings ending in the literal ".cc".
7230
7231 "\bwin/*"
7232 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
7233```
Brett Wilson796ed472018-07-16 15:11:09 -07007234### **Label patterns**
7235
7236```
7237 A label pattern is a way of expressing one or more labels in a portion of the
7238 source tree. They are not general regular expressions.
7239
7240 They can take the following forms only:
7241
7242 - Explicit (no wildcard):
7243 "//foo/bar:baz"
7244 ":baz"
7245
7246 - Wildcard target names:
7247 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
7248 ":*" (all targets in the current build file)
7249
7250 - Wildcard directory names ("*" is only supported at the end)
7251 "*" (all targets)
7252 "//foo/bar/*" (all targets in any subdir of //foo/bar)
7253 "./*" (all targets in the current build file or sub dirs)
7254
Julie Hockette2a29402018-07-31 10:11:42 -07007255 Any of the above forms can additionally take an explicit toolchain
7256 in parenthesis at the end of the label pattern. In this case, the
7257 toolchain must be fully qualified (no wildcards are supported in the
7258 toolchain name).
Brett Wilson796ed472018-07-16 15:11:09 -07007259
Julie Hockette2a29402018-07-31 10:11:42 -07007260 "//foo:bar(//build/toolchain:mac)"
Brett Wilson796ed472018-07-16 15:11:09 -07007261 An explicit target in an explicit toolchain.
7262
7263 ":*(//build/toolchain/linux:32bit)"
7264 All targets in the current build file using the 32-bit Linux toolchain.
7265
7266 "//foo/*(//build/toolchain:win)"
7267 All targets in //foo and any subdirectory using the Windows
7268 toolchain.
7269```
7270### **About labels**
7271
7272```
7273 Everything that can participate in the dependency graph (targets, configs,
7274 and toolchains) are identified by labels. A common label looks like:
7275
7276 //base/test:test_support
7277
7278 This consists of a source-root-absolute path, a colon, and a name. This means
7279 to look for the thing named "test_support" in "base/test/BUILD.gn".
7280
7281 You can also specify system absolute paths if necessary. Typically such
7282 paths would be specified via a build arg so the developer can specify where
7283 the component is on their system.
7284
7285 /usr/local/foo:bar (Posix)
7286 /C:/Program Files/MyLibs:bar (Windows)
7287```
7288
7289#### **Toolchains**
7290
7291```
7292 A canonical label includes the label of the toolchain being used. Normally,
7293 the toolchain label is implicitly inherited from the current execution
7294 context, but you can override this to specify cross-toolchain dependencies:
7295
7296 //base/test:test_support(//build/toolchain/win:msvc)
7297
7298 Here GN will look for the toolchain definition called "msvc" in the file
7299 "//build/toolchain/win" to know how to compile this target.
7300```
7301
7302#### **Relative labels**
7303
7304```
7305 If you want to refer to something in the same buildfile, you can omit
7306 the path name and just start with a colon. This format is recommended for
7307 all same-file references.
7308
7309 :base
7310
7311 Labels can be specified as being relative to the current directory.
7312 Stylistically, we prefer to use absolute paths for all non-file-local
7313 references unless a build file needs to be run in different contexts (like a
7314 project needs to be both standalone and pulled into other projects in
7315 difference places in the directory hierarchy).
7316
7317 source/plugin:myplugin
7318 ../net:url_request
7319```
7320
7321#### **Implicit names**
7322
7323```
7324 If a name is unspecified, it will inherit the directory name. Stylistically,
7325 we prefer to omit the colon and name when possible:
7326
7327 //net -> //net:net
7328 //tools/gn -> //tools/gn:gn
7329```
Julie Hockett152c5142019-07-12 09:53:43 -06007330### **Metadata Collection**
7331
7332```
7333 Metadata is information attached to targets throughout the dependency tree. GN
7334 allows for the collection of this data into files written during the generation
Keir Mierle45611e32019-11-12 11:18:00 -08007335 step, enabling users to expose and aggregate this data based on the dependency
Julie Hockett152c5142019-07-12 09:53:43 -06007336 tree.
7337```
7338
7339#### **generated_file targets**
7340
7341```
7342 Similar to the write_file() function, the generated_file target type
7343 creates a file in the specified location with the specified content. The
Keir Mierle45611e32019-11-12 11:18:00 -08007344 primary difference between write_file() and this target type is that the
Julie Hockett152c5142019-07-12 09:53:43 -06007345 write_file function does the file write at parse time, while the
7346 generated_file target type writes at target resolution time. See
7347 "gn help generated_file" for more detail.
7348
Keir Mierle45611e32019-11-12 11:18:00 -08007349 When written at target resolution time, generated_file enables GN to
Julie Hockett152c5142019-07-12 09:53:43 -06007350 collect and write aggregated metadata from dependents.
7351
Keir Mierle45611e32019-11-12 11:18:00 -08007352 A generated_file target can declare either 'contents' to write statically
7353 known contents to a file or 'data_keys' to aggregate metadata and write the
7354 result to a file. It can also specify 'walk_keys' (to restrict the metadata
Julie Hockett152c5142019-07-12 09:53:43 -06007355 collection), 'output_conversion', and 'rebase'.
7356```
7357
7358#### **Collection and Aggregation**
7359
7360```
7361 Targets can declare a 'metadata' variable containing a scope, and this
Keir Mierle45611e32019-11-12 11:18:00 -08007362 metadata may be collected and written out to a file specified by
7363 generated_file aggregation targets. The 'metadata' scope must contain
7364 only list values since the aggregation step collects a list of these values.
Julie Hockett152c5142019-07-12 09:53:43 -06007365
7366 During the target resolution, generated_file targets will walk their
7367 dependencies recursively, collecting metadata based on the specified
7368 'data_keys'. 'data_keys' is specified as a list of strings, used by the walk
7369 to identify which variables in dependencies' 'metadata' scopes to collect.
7370
Keir Mierle45611e32019-11-12 11:18:00 -08007371 The walk begins with the listed dependencies of the 'generated_file' target.
7372 The 'metadata' scope for each dependency is inspected for matching elements
7373 of the 'generated_file' target's 'data_keys' list. If a match is found, the
7374 data from the dependent's matching key list is appended to the aggregate walk
7375 list. Note that this means that if more than one walk key is specified, the
7376 data in all of them will be aggregated into one list. From there, the walk
7377 will then recurse into the dependencies of each target it encounters,
7378 collecting the specified metadata for each.
Julie Hockett152c5142019-07-12 09:53:43 -06007379
7380 For example:
7381
7382 group("a") {
7383 metadata = {
7384 doom_melon = [ "enable" ]
7385 my_files = [ "foo.cpp" ]
7386 my_extra_files = [ "bar.cpp" ]
7387 }
7388
7389 deps = [ ":b" ]
7390 }
7391
7392 group("b") {
7393 metadata = {
7394 my_files = [ "baz.cpp" ]
7395 }
7396 }
7397
7398 generated_file("metadata") {
7399 outputs = [ "$root_build_dir/my_files.json" ]
7400 data_keys = [ "my_files", "my_extra_files" ]
7401
7402 deps = [ ":a" ]
7403 }
7404
7405 The above will produce the following file data:
7406
7407 foo.cpp
7408 bar.cpp
7409 baz.cpp
7410
Keir Mierle45611e32019-11-12 11:18:00 -08007411 The dependency walk can be limited by using the 'walk_keys'. This is a list of
Julie Hockett152c5142019-07-12 09:53:43 -06007412 labels that should be included in the walk. All labels specified here should
7413 also be in one of the deps lists. These keys act as barriers, where the walk
Keir Mierle45611e32019-11-12 11:18:00 -08007414 will only recurse into the targets listed. An empty list in all specified
Julie Hockett152c5142019-07-12 09:53:43 -06007415 barriers will end that portion of the walk.
7416
7417 group("a") {
7418 metadata = {
7419 my_files = [ "foo.cpp" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02007420 my_files_barrier = [ ":b" ]
Julie Hockett152c5142019-07-12 09:53:43 -06007421 }
7422
7423 deps = [ ":b", ":c" ]
7424 }
7425
7426 group("b") {
7427 metadata = {
7428 my_files = [ "bar.cpp" ]
7429 }
7430 }
7431
7432 group("c") {
7433 metadata = {
7434 my_files = [ "doom_melon.cpp" ]
7435 }
7436 }
7437
7438 generated_file("metadata") {
7439 outputs = [ "$root_build_dir/my_files.json" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02007440 data_keys = [ "my_files" ]
7441 walk_keys = [ "my_files_barrier" ]
Julie Hockett152c5142019-07-12 09:53:43 -06007442
7443 deps = [ ":a" ]
7444 }
7445
7446 The above will produce the following file data (note that `doom_melon.cpp` is
7447 not included):
7448
7449 foo.cpp
7450 bar.cpp
7451
7452 A common example of this sort of barrier is in builds that have host tools
7453 built as part of the tree, but do not want the metadata from those host tools
7454 to be collected with the target-side code.
7455```
7456
7457#### **Common Uses**
7458
7459```
7460 Metadata can be used to collect information about the different targets in the
7461 build, and so a common use is to provide post-build tooling with a set of data
7462 necessary to do aggregation tasks. For example, if each test target specifies
7463 the output location of its binary to run in a metadata field, that can be
7464 collected into a single file listing the locations of all tests in the
7465 dependency tree. A local build tool (or continuous integration infrastructure)
7466 can then use that file to know which tests exist, and where, and run them
7467 accordingly.
7468
7469 Another use is in image creation, where a post-build image tool needs to know
7470 various pieces of information about the components it should include in order
7471 to put together the correct image.
7472```
Brett Wilson796ed472018-07-16 15:11:09 -07007473### **Ninja build rules**
7474
7475#### **The "all" and "default" rules**
7476
7477```
7478 All generated targets (see "gn help execution") will be added to an implicit
7479 build rule called "all" so "ninja all" will always compile everything. The
7480 default rule will be used by Ninja if no specific target is specified (just
7481 typing "ninja"). If there is a target named "default" in the root build file,
7482 it will be the default build rule, otherwise the implicit "all" rule will be
7483 used.
7484```
7485
7486#### **Phony rules**
7487
7488```
7489 GN generates Ninja "phony" rules for targets in the default toolchain. The
7490 phony rules can collide with each other and with the names of generated files
7491 so are generated with the following priority:
7492
7493 1. Actual files generated by the build always take precedence.
7494
7495 2. Targets in the toplevel //BUILD.gn file.
7496
7497 3. Targets in toplevel directories matching the names of the directories.
7498 So "ninja foo" can be used to compile "//foo:foo". This only applies to
7499 the first level of directories since usually these are the most
7500 important (so this won't apply to "//foo/bar:bar").
7501
7502 4. The short names of executables if there is only one executable with that
7503 short name. Use "ninja doom_melon" to compile the
7504 "//tools/fruit:doom_melon" executable.
7505
7506 5. The short names of all targets if there is only one target with that
7507 short name.
7508
7509 6. Full label name with no leading slashes. So you can use
7510 "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon".
7511
7512 7. Labels with an implicit name part (when the short names match the
7513 directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar".
7514
7515 These "phony" rules are provided only for running Ninja since this matches
7516 people's historical expectations for building. For consistency with the rest
7517 of the program, GN introspection commands accept explicit labels.
7518
7519 To explicitly compile a target in a non-default toolchain, you must give
7520 Ninja the exact name of the output file relative to the build directory.
7521```
7522### **nogncheck**: Skip an include line from checking.
7523
7524```
7525 GN's header checker helps validate that the includes match the build
7526 dependency graph. Sometimes an include might be conditional or otherwise
7527 problematic, but you want to specifically allow it. In this case, it can be
7528 whitelisted.
7529
7530 Include lines containing the substring "nogncheck" will be excluded from
7531 header checking. The most common case is a conditional include:
7532
7533 #if defined(ENABLE_DOOM_MELON)
7534 #include "tools/doom_melon/doom_melon.h" // nogncheck
7535 #endif
7536
7537 If the build file has a conditional dependency on the corresponding target
7538 that matches the conditional include, everything will always link correctly:
7539
7540 source_set("mytarget") {
7541 ...
7542 if (enable_doom_melon) {
7543 defines = [ "ENABLE_DOOM_MELON" ]
7544 deps += [ "//tools/doom_melon" ]
7545 }
7546
7547 But GN's header checker does not understand preprocessor directives, won't
7548 know it matches the build dependencies, and will flag this include as
7549 incorrect when the condition is false.
7550```
7551
7552#### **More information**
7553
7554```
7555 The topic "gn help check" has general information on how checking works and
7556 advice on fixing problems. Targets can also opt-out of checking, see
7557 "gn help check_includes".
7558```
Brett Wilson796ed472018-07-16 15:11:09 -07007559### **Runtime dependencies**
7560
7561```
7562 Runtime dependencies of a target are exposed via the "runtime_deps" category
7563 of "gn desc" (see "gn help desc") or they can be written at build generation
7564 time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help
7565 --runtime-deps-list-file").
7566
7567 To a first approximation, the runtime dependencies of a target are the set of
7568 "data" files, data directories, and the shared libraries from all transitive
7569 dependencies. Executables, shared libraries, and loadable modules are
7570 considered runtime dependencies of themselves.
7571```
7572
7573#### **Executables**
7574
7575```
7576 Executable targets and those executable targets' transitive dependencies are
7577 not considered unless that executable is listed in "data_deps". Otherwise, GN
7578 assumes that the executable (and everything it requires) is a build-time
7579 dependency only.
7580```
7581
7582#### **Actions and copies**
7583
7584```
7585 Action and copy targets that are listed as "data_deps" will have all of their
7586 outputs and data files considered as runtime dependencies. Action and copy
7587 targets that are "deps" or "public_deps" will have only their data files
7588 considered as runtime dependencies. These targets can list an output file in
7589 both the "outputs" and "data" lists to force an output file as a runtime
7590 dependency in all cases.
7591
7592 The different rules for deps and data_deps are to express build-time (deps)
7593 vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as
7594 data dependencies, there would be a lot of extra stuff, and if GN counted all
7595 run-time dependencies as regular deps, the build's parallelism would be
7596 unnecessarily constrained.
7597
7598 This rule can sometimes lead to unintuitive results. For example, given the
7599 three targets:
7600 A --[data_deps]--> B --[deps]--> ACTION
7601 GN would say that A does not have runtime deps on the result of the ACTION,
7602 which is often correct. But the purpose of the B target might be to collect
7603 many actions into one logic unit, and the "data"-ness of A's dependency is
7604 lost. Solutions:
7605
7606 - List the outputs of the action in its data section (if the results of
7607 that action are always runtime files).
7608 - Have B list the action in data_deps (if the outputs of the actions are
7609 always runtime files).
7610 - Have B list the action in both deps and data deps (if the outputs might be
7611 used in both contexts and you don't care about unnecessary entries in the
7612 list of files required at runtime).
7613 - Split B into run-time and build-time versions with the appropriate "deps"
7614 for each.
7615```
7616
7617#### **Static libraries and source sets**
7618
7619```
7620 The results of static_library or source_set targets are not considered
7621 runtime dependencies since these are assumed to be intermediate targets only.
7622 If you need to list a static library as a runtime dependency, you can
7623 manually compute the .a/.lib file name for the current platform and list it
7624 in the "data" list of a target (possibly on the static library target
7625 itself).
7626```
7627
7628#### **Multiple outputs**
7629
7630```
7631 Linker tools can specify which of their outputs should be considered when
7632 computing the runtime deps by setting runtime_outputs. If this is unset on
7633 the tool, the default will be the first output only.
7634```
7635### **How Source Expansion Works**
7636
7637```
7638 Source expansion is used for the action_foreach and copy target types to map
7639 source file names to output file names or arguments.
7640
7641 To perform source expansion in the outputs, GN maps every entry in the
7642 sources to every entry in the outputs list, producing the cross product of
7643 all combinations, expanding placeholders (see below).
7644
7645 Source expansion in the args works similarly, but performing the placeholder
7646 substitution produces a different set of arguments for each invocation of the
7647 script.
7648
7649 If no placeholders are found, the outputs or args list will be treated as a
7650 static list of literal file names that do not depend on the sources.
7651
7652 See "gn help copy" and "gn help action_foreach" for more on how this is
7653 applied.
7654```
7655
7656#### **Placeholders**
7657
7658```
7659 This section discusses only placeholders for actions. There are other
7660 placeholders used in the definition of tools. See "gn help tool" for those.
7661
7662 {{source}}
7663 The name of the source file including directory (*). This will generally
7664 be used for specifying inputs to a script in the "args" variable.
7665 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
7666
7667 {{source_file_part}}
7668 The file part of the source including the extension.
7669 "//foo/bar/baz.txt" => "baz.txt"
7670
7671 {{source_name_part}}
7672 The filename part of the source file with no directory or extension. This
7673 will generally be used for specifying a transformation from a source file
7674 to a destination file with the same name but different extension.
7675 "//foo/bar/baz.txt" => "baz"
7676
7677 {{source_dir}}
7678 The directory (*) containing the source file with no trailing slash.
7679 "//foo/bar/baz.txt" => "../../foo/bar"
7680
7681 {{source_root_relative_dir}}
7682 The path to the source file's directory relative to the source root, with
7683 no leading "//" or trailing slashes. If the path is system-absolute,
7684 (beginning in a single slash) this will just return the path with no
7685 trailing slash. This value will always be the same, regardless of whether
7686 it appears in the "outputs" or "args" section.
7687 "//foo/bar/baz.txt" => "foo/bar"
7688
7689 {{source_gen_dir}}
7690 The generated file directory (*) corresponding to the source file's path.
7691 This will be different than the target's generated file directory if the
7692 source file is in a different directory than the BUILD.gn file.
7693 "//foo/bar/baz.txt" => "gen/foo/bar"
7694
7695 {{source_out_dir}}
7696 The object file directory (*) corresponding to the source file's path,
7697 relative to the build directory. this us be different than the target's
7698 out directory if the source file is in a different directory than the
7699 build.gn file.
7700 "//foo/bar/baz.txt" => "obj/foo/bar"
7701
7702 {{source_target_relative}}
7703 The path to the source file relative to the target's directory. This will
7704 generally be used for replicating the source directory layout in the
Robert Sesek252674d2019-05-10 14:08:52 -04007705 output directory. This can only be used in actions and bundle_data
7706 targets. It is an error to use in process_file_template where there is no
7707 "target".
Brett Wilson796ed472018-07-16 15:11:09 -07007708 "//foo/bar/baz.txt" => "baz.txt"
7709```
7710
7711#### **(*) Note on directories**
7712
7713```
7714 Paths containing directories (except the source_root_relative_dir) will be
7715 different depending on what context the expansion is evaluated in. Generally
7716 it should "just work" but it means you can't concatenate strings containing
7717 these values with reasonable results.
7718
7719 Details: source expansions can be used in the "outputs" variable, the "args"
7720 variable, and in calls to "process_file_template". The "args" are passed to a
7721 script which is run from the build directory, so these directories will
7722 relative to the build directory for the script to find. In the other cases,
7723 the directories will be source- absolute (begin with a "//") because the
7724 results of those expansions will be handled by GN internally.
7725```
7726
7727#### **Examples**
7728
7729```
7730 Non-varying outputs:
7731 action("hardcoded_outputs") {
7732 sources = [ "input1.idl", "input2.idl" ]
7733 outputs = [ "$target_out_dir/output1.dat",
7734 "$target_out_dir/output2.dat" ]
7735 }
7736 The outputs in this case will be the two literal files given.
7737
7738 Varying outputs:
7739 action_foreach("varying_outputs") {
7740 sources = [ "input1.idl", "input2.idl" ]
7741 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
7742 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
7743 }
7744 Performing source expansion will result in the following output names:
7745 //out/Debug/obj/mydirectory/input1.h
7746 //out/Debug/obj/mydirectory/input1.cc
7747 //out/Debug/obj/mydirectory/input2.h
7748 //out/Debug/obj/mydirectory/input2.cc
7749```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007750### **Available global switches**
Brett Wilson796ed472018-07-16 15:11:09 -07007751
7752```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007753 Do "gn help --the_switch_you_want_help_on" for more. Individual commands may
7754 take command-specific switches not listed here. See the help on your specific
7755 command for more.
7756```
7757```
7758 * --args: Specifies build arguments overrides.
7759 * --color: Force colored output.
7760 * --dotfile: Override the name of the ".gn" file.
7761 * --fail-on-unused-args: Treat unused build args as fatal errors.
7762 * --markdown: Write help output in the Markdown format.
7763 * --nocolor: Force non-colored output.
7764 * -q: Quiet mode. Don't print output on success.
7765 * --root: Explicitly specify source root.
Nico Weberad9eab22020-11-15 22:20:28 -05007766 * --root-target: Override the root target.
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007767 * --runtime-deps-list-file: Save runtime dependencies for targets in file.
7768 * --script-executable: Set the executable used to execute scripts.
7769 * --threads: Specify number of worker threads.
7770 * --time: Outputs a summary of how long everything took.
7771 * --tracelog: Writes a Chrome-compatible trace log to the given file.
7772 * -v: Verbose logging.
7773 * --version: Prints the GN version number and exits.
Brett Wilson796ed472018-07-16 15:11:09 -07007774```
7775