blob: 68bd12c837a064fb3878dab92eb16b007e703302 [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
848 of target_name is supplied, only targets that are reachable from the list
849 of target_name will be used for “command objects” generation, otherwise
850 all available targets will be used. This is used for various Clang-based
851 tooling, allowing for the replay of individual compilations independent
852 of the build system.
Julie Hockett11e09912018-07-31 13:15:08 -0700853```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800854### **gn help <anything>**
Brett Wilson796ed472018-07-16 15:11:09 -0700855
856```
857 Yo dawg, I heard you like help on your help so I put help on the help in the
858 help.
859
860 You can also use "all" as the parameter to get all help at once.
861```
862
863#### **Switches**
864
865```
866 --markdown
867 Format output in markdown syntax.
868```
869
870#### **Example**
871
872```
873 gn help --markdown all
874 Dump all help to stdout in markdown format.
875```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400876### **gn ls <out_dir> [<label_pattern>] [\--default-toolchain] [\--as=...]**
Brett Wilson796ed472018-07-16 15:11:09 -0700877```
878 [--type=...] [--testonly=...]
879
880 Lists all targets matching the given pattern for the given build directory.
881 By default, only targets in the default toolchain will be matched unless a
882 toolchain is explicitly supplied.
883
884 If the label pattern is unspecified, list all targets. The label pattern is
885 not a general regular expression (see "gn help label_pattern"). If you need
886 more complex expressions, pipe the result through grep.
887```
888
889#### **Options**
890
891```
892 --as=(buildfile|label|output)
893 How to print targets.
894
895 buildfile
896 Prints the build files where the given target was declared as
897 file names.
898 label (default)
899 Prints the label of the target.
900 output
901 Prints the first output file for the target relative to the
902 root build directory.
903
Robert Sesekd0a6f072020-05-15 11:21:22 -0400904 --default-toolchain
905 Normally wildcard targets are matched in all toolchains. This
906 switch makes wildcard labels with no explicit toolchain reference
907 only match targets in the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -0700908
Robert Sesekd0a6f072020-05-15 11:21:22 -0400909 Non-wildcard inputs with no explicit toolchain specification will
910 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -0700911
912 --testonly=(true|false)
913 Restrict outputs to targets with the testonly flag set
914 accordingly. When unspecified, the target's testonly flags are
915 ignored.
916
917 --type=(action|copy|executable|group|loadable_module|shared_library|
918 source_set|static_library)
919 Restrict outputs to targets matching the given type. If
920 unspecified, no filtering will be performed.
921```
922
923#### **Examples**
924
925```
926 gn ls out/Debug
927 Lists all targets in the default toolchain.
928
929 gn ls out/Debug "//base/*"
930 Lists all targets in the directory base and all subdirectories.
931
932 gn ls out/Debug "//base:*"
933 Lists all targets defined in //base/BUILD.gn.
934
935 gn ls out/Debug //base --as=output
936 Lists the build output file for //base:base
937
938 gn ls out/Debug --type=executable
939 Lists all executables produced by the build.
940
941 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
942 Builds all targets in //base and all subdirectories.
Brett Wilson796ed472018-07-16 15:11:09 -0700943```
Joe Armstrongb199e542019-03-21 09:40:08 +0800944### **gn meta**
945
Nate Fischer8ed01d32019-01-08 17:32:01 -0800946```
Joe Armstrongb199e542019-03-21 09:40:08 +0800947 gn meta * --data=[,*]* [--walk=[,*]*]
948 [--rebase=]
Nate Fischer8ed01d32019-01-08 17:32:01 -0800949
950 Lists collected metaresults of all given targets for the given data key(s),
951 collecting metadata dependencies as specified by the given walk key(s).
952
953 See `gn help generated_file` for more information on the walk.
954```
955
956#### **Arguments**
957
958```
959
960 A list of target labels from which to initiate the walk.
961
962 --data
963 A list of keys from which to extract data. In each target walked, its metadata
964 scope is checked for the presence of these keys. If present, the contents of
965 those variable in the scope are appended to the results list.
966
967 --walk (optional)
968 A list of keys from which to control the walk. In each target walked, its
969 metadata scope is checked for the presence of any of these keys. If present,
970 the contents of those variables is checked to ensure that it is a label of
971 a valid dependency of the target and then added to the set of targets to walk.
972 If the empty string ("") is present in any of these keys, all deps and data_deps
973 are added to the walk set.
974
975 --rebase (optional)
976 A destination directory onto which to rebase any paths found. If set, all
977 collected metadata will be rebased onto this path. This option will throw errors
978 if collected metadata is not a list of strings.
979```
980
981#### **Examples**
982
983```
984 gn meta out/Debug "//base/foo" --data=files
985 Lists collected metaresults for the `files` key in the //base/foo:foo
986 target and all of its dependency tree.
987
988 gn meta out/Debug "//base/foo" --data=files --data=other
989 Lists collected metaresults for the `files` and `other` keys in the
990 //base/foo:foo target and all of its dependency tree.
991
992 gn meta out/Debug "//base/foo" --data=files --walk=stop
993 Lists collected metaresults for the `files` key in the //base/foo:foo
994 target and all of the dependencies listed in the `stop` key (and so on).
995
996 gn meta out/Debug "//base/foo" --data=files --rebase="/"
997 Lists collected metaresults for the `files` key in the //base/foo:foo
998 target and all of its dependency tree, rebasing the strings in the `files`
999 key onto the source directory of the target's declaration relative to "/".
1000```
Hector Dearman5233fa12020-09-11 12:18:21 +01001001### **gn outputs <out_dir> <list of target or file names...>**
Brett Wilson74397092020-03-18 16:34:14 -07001002
1003```
1004 Lists the output files corresponding to the given target(s) or file name(s).
1005 There can be multiple outputs because there can be more than one output
1006 generated by a build step, and there can be more than one toolchain matched.
1007 You can also list multiple inputs which will generate a union of all the
1008 outputs from those inputs.
1009
1010 - The input target/file names are relative to the current directory.
1011
1012 - The output file names are relative to the root build directory.
1013
1014 This command is useful for finding a ninja command that will build only a
1015 portion of the build.
1016```
1017
1018#### **Target outputs**
1019
1020```
1021 If the parameter is a target name that includes a toolchain, it will match
1022 only that target in that toolchain. If no toolchain is specified, it will
1023 match all targets with that name in any toolchain.
1024
1025 The result will be the outputs specified by that target which could be a
1026 library, executable, output of an action, a stamp file, etc.
1027```
1028
1029#### **File outputs**
1030
1031```
1032 If the parameter is a file name it will compute the output for that compile
1033 step for all targets in all toolchains that contain that file as a source
1034 file.
1035
1036 If the source is not compiled (e.g. a header or text file), the command will
1037 produce no output.
1038
1039 If the source is listed as an "input" to a binary target or action will
1040 resolve to that target's outputs.
1041```
1042
1043#### **Example**
1044
1045```
1046 gn outputs out/debug some/directory:some_target
1047 Find the outputs of a given target.
1048
1049 gn outputs out/debug src/project/my_file.cc | xargs ninja -C out/debug
1050 Compiles just the given source file in all toolchains it's referenced in.
1051
1052 git diff --name-only | xargs gn outputs out/x64 | xargs ninja -C out/x64
1053 Compiles all files changed in git.
1054```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001055### **gn path <out_dir> <target_one> <target_two>**
Brett Wilson796ed472018-07-16 15:11:09 -07001056
1057```
1058 Finds paths of dependencies between two targets. Each unique path will be
1059 printed in one group, and groups will be separate by newlines. The two
1060 targets can appear in either order (paths will be found going in either
1061 direction).
1062
1063 By default, a single path will be printed. If there is a path with only
1064 public dependencies, the shortest public path will be printed. Otherwise, the
1065 shortest path using either public or private dependencies will be printed. If
1066 --with-data is specified, data deps will also be considered. If there are
1067 multiple shortest paths, an arbitrary one will be selected.
1068```
1069
1070#### **Interesting paths**
1071
1072```
1073 In a large project, there can be 100's of millions of unique paths between a
1074 very high level and a common low-level target. To make the output more useful
1075 (and terminate in a reasonable time), GN will not revisit sub-paths
1076 previously known to lead to the target.
1077```
1078
1079#### **Options**
1080
1081```
1082 --all
1083 Prints all "interesting" paths found rather than just the first one.
1084 Public paths will be printed first in order of increasing length, followed
1085 by non-public paths in order of increasing length.
1086
1087 --public
1088 Considers only public paths. Can't be used with --with-data.
1089
1090 --with-data
1091 Additionally follows data deps. Without this flag, only public and private
1092 linked deps will be followed. Can't be used with --public.
1093```
1094
1095#### **Example**
1096
1097```
Keir Mierle45611e32019-11-12 11:18:00 -08001098 gn path out/Default //base //gn
Brett Wilson796ed472018-07-16 15:11:09 -07001099```
Joe Armstrongb199e542019-03-21 09:40:08 +08001100### **gn refs**
1101
Brett Wilson796ed472018-07-16 15:11:09 -07001102```
Nico Weberad9eab22020-11-15 22:20:28 -05001103 gn refs (|
1104 [--default-toolchain] [--as=...] [--testonly=...] [--type=...]
Brett Wilson796ed472018-07-16 15:11:09 -07001105
1106 Finds reverse dependencies (which targets reference something). The input is
1107 a list containing:
1108
1109 - Target label: The result will be which targets depend on it.
1110
1111 - Config label: The result will be which targets list the given config in
1112 its "configs" or "public_configs" list.
1113
1114 - Label pattern: The result will be which targets depend on any target
1115 matching the given pattern. Patterns will not match configs. These are not
1116 general regular expressions, see "gn help label_pattern" for details.
1117
1118 - File name: The result will be which targets list the given file in its
1119 "inputs", "sources", "public", "data", or "outputs". Any input that does
1120 not contain wildcards and does not match a target or a config will be
1121 treated as a file.
1122
1123 - Response file: If the input starts with an "@", it will be interpreted as
1124 a path to a file containing a list of labels or file names, one per line.
1125 This allows us to handle long lists of inputs without worrying about
1126 command line limits.
1127```
1128
1129#### **Options**
1130
1131```
1132 --all
1133 When used without --tree, will recurse and display all unique
1134 dependencies of the given targets. For example, if the input is a target,
1135 this will output all targets that depend directly or indirectly on the
1136 input. If the input is a file, this will output all targets that depend
1137 directly or indirectly on that file.
1138
1139 When used with --tree, turns off eliding to show a complete tree.
Brett Wilson796ed472018-07-16 15:11:09 -07001140
1141 --as=(buildfile|label|output)
1142 How to print targets.
1143
1144 buildfile
1145 Prints the build files where the given target was declared as
1146 file names.
1147 label (default)
1148 Prints the label of the target.
1149 output
1150 Prints the first output file for the target relative to the
1151 root build directory.
1152
Robert Sesekd0a6f072020-05-15 11:21:22 -04001153 --default-toolchain
1154 Normally wildcard targets are matched in all toolchains. This
1155 switch makes wildcard labels with no explicit toolchain reference
1156 only match targets in the default toolchain.
1157
1158 Non-wildcard inputs with no explicit toolchain specification will
1159 always match only a target in the default toolchain if one exists.
1160
Brett Wilson796ed472018-07-16 15:11:09 -07001161 -q
1162 Quiet. If nothing matches, don't print any output. Without this option, if
1163 there are no matches there will be an informational message printed which
1164 might interfere with scripts processing the output.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001165
Brett Wilson796ed472018-07-16 15:11:09 -07001166 --testonly=(true|false)
1167 Restrict outputs to targets with the testonly flag set
1168 accordingly. When unspecified, the target's testonly flags are
1169 ignored.
1170
1171 --tree
1172 Outputs a reverse dependency tree from the given target. Duplicates will
1173 be elided. Combine with --all to see a full dependency tree.
1174
1175 Tree output can not be used with the filtering or output flags: --as,
1176 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001177
Brett Wilson796ed472018-07-16 15:11:09 -07001178 --type=(action|copy|executable|group|loadable_module|shared_library|
1179 source_set|static_library)
1180 Restrict outputs to targets matching the given type. If
1181 unspecified, no filtering will be performed.
1182```
1183
1184#### **Examples (target input)**
1185
1186```
Keir Mierle45611e32019-11-12 11:18:00 -08001187 gn refs out/Debug //gn:gn
Brett Wilson796ed472018-07-16 15:11:09 -07001188 Find all targets depending on the given exact target name.
1189
1190 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
1191 Edit all .gn files containing references to //base:i18n
1192
1193 gn refs out/Debug //base --all
1194 List all targets depending directly or indirectly on //base:base.
1195
1196 gn refs out/Debug "//base/*"
1197 List all targets depending directly on any target in //base or
1198 its subdirectories.
1199
1200 gn refs out/Debug "//base:*"
1201 List all targets depending directly on any target in
1202 //base/BUILD.gn.
1203
1204 gn refs out/Debug //base --tree
1205 Print a reverse dependency tree of //base:base
1206```
1207
1208#### **Examples (file input)**
1209
1210```
1211 gn refs out/Debug //base/macros.h
1212 Print target(s) listing //base/macros.h as a source.
1213
1214 gn refs out/Debug //base/macros.h --tree
1215 Display a reverse dependency tree to get to the given file. This
1216 will show how dependencies will reference that file.
1217
1218 gn refs out/Debug //base/macros.h //base/at_exit.h --all
1219 Display all unique targets with some dependency path to a target
1220 containing either of the given files as a source.
1221
1222 gn refs out/Debug //base/macros.h --testonly=true --type=executable
1223 --all --as=output
1224 Display the executable file names of all test executables
1225 potentially affected by a change to the given file.
1226```
1227## Target declarations
1228
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001229### **action**: Declare a target that runs a script a single time.
Brett Wilson796ed472018-07-16 15:11:09 -07001230
1231```
1232 This target type allows you to run a script a single time to produce one or
1233 more output files. If you want to run a script once for each of a set of
1234 input files, see "gn help action_foreach".
1235```
1236
1237#### **Inputs**
1238
1239```
1240 In an action the "sources" and "inputs" are treated the same: they're both
1241 input dependencies on script execution with no special handling. If you want
1242 to pass the sources to your script, you must do so explicitly by including
1243 them in the "args". Note also that this means there is no special handling of
1244 paths since GN doesn't know which of the args are paths and not. You will
1245 want to use rebase_path() to convert paths to be relative to the
1246 root_build_dir.
1247
1248 You can dynamically write input dependencies (for incremental rebuilds if an
1249 input file changes) by writing a depfile when the script is run (see "gn help
1250 depfile"). This is more flexible than "inputs".
1251
1252 If the command line length is very long, you can use response files to pass
1253 args to your script. See "gn help response_file_contents".
1254
1255 It is recommended you put inputs to your script in the "sources" variable,
1256 and stuff like other Python files required to run your script in the "inputs"
1257 variable.
Julie Hockette2a29402018-07-31 10:11:42 -07001258
Brett Wilson796ed472018-07-16 15:11:09 -07001259 The "deps" and "public_deps" for an action will always be
1260 completed before any part of the action is run so it can depend on
1261 the output of previous steps. The "data_deps" will be built if the
1262 action is built, but may not have completed before all steps of the
1263 action are started. This can give additional parallelism in the build
1264 for runtime-only dependencies.
1265```
1266
1267#### **Outputs**
1268
1269```
1270 You should specify files created by your script by specifying them in the
1271 "outputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001272
Brett Wilson796ed472018-07-16 15:11:09 -07001273 The script will be executed with the given arguments with the current
1274 directory being that of the root build directory. If you pass files
1275 to your script, see "gn help rebase_path" for how to convert
1276 file names to be relative to the build directory (file names in the
1277 sources, outputs, and inputs will be all treated as relative to the
1278 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001279
1280 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1281 that Ninja will check the timestamp of the output after the action
1282 completes. If output timestamp is unchanged, the step will be treated
1283 as if it never needed to be rebuilt, potentially eliminating some
1284 downstream steps for incremental builds. Scripts can improve build
1285 performance by taking care not to change the timstamp of the output
1286 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001287```
1288
1289#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001290
Brett Wilson796ed472018-07-16 15:11:09 -07001291```
1292 All output files must be inside the output directory of the build.
1293 You would generally use |$target_out_dir| or |$target_gen_dir| to
1294 reference the output or generated intermediate file directories,
1295 respectively.
1296```
1297
1298#### **Variables**
1299
1300```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001301 args, data, data_deps, depfile, deps, inputs, metadata, outputs*, pool,
Brett Wilson796ed472018-07-16 15:11:09 -07001302 response_file_contents, script*, sources
1303 * = required
1304```
1305
1306#### **Example**
1307
1308```
1309 action("run_this_guy_once") {
1310 script = "doprocessing.py"
1311 sources = [ "my_configuration.txt" ]
1312 outputs = [ "$target_gen_dir/insightful_output.txt" ]
1313
1314 # Our script imports this Python file so we want to rebuild if it changes.
1315 inputs = [ "helper_library.py" ]
1316
1317 # Note that we have to manually pass the sources to our script if the
1318 # script needs them as inputs.
1319 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
1320 rebase_path(sources, root_build_dir)
1321 }
1322```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001323### **action_foreach**: Declare a target that runs a script over a set of files.
Brett Wilson796ed472018-07-16 15:11:09 -07001324
1325```
1326 This target type allows you to run a script once-per-file over a set of
1327 sources. If you want to run a script once that takes many files as input, see
1328 "gn help action".
1329```
1330
1331#### **Inputs**
1332
1333```
1334 The script will be run once per file in the "sources" variable. The "outputs"
1335 variable should specify one or more files with a source expansion pattern in
1336 it (see "gn help source_expansion"). The output file(s) for each script
1337 invocation should be unique. Normally you use "{{source_name_part}}" in each
1338 output file.
1339
1340 If your script takes additional data as input, such as a shared configuration
1341 file or a Python module it uses, those files should be listed in the "inputs"
1342 variable. These files are treated as dependencies of each script invocation.
1343
1344 If the command line length is very long, you can use response files to pass
1345 args to your script. See "gn help response_file_contents".
1346
1347 You can dynamically write input dependencies (for incremental rebuilds if an
1348 input file changes) by writing a depfile when the script is run (see "gn help
1349 depfile"). This is more flexible than "inputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001350
Brett Wilson796ed472018-07-16 15:11:09 -07001351 The "deps" and "public_deps" for an action will always be
1352 completed before any part of the action is run so it can depend on
1353 the output of previous steps. The "data_deps" will be built if the
1354 action is built, but may not have completed before all steps of the
1355 action are started. This can give additional parallelism in the build
1356 for runtime-only dependencies.
1357```
1358
1359#### **Outputs**
Julie Hockette2a29402018-07-31 10:11:42 -07001360
Brett Wilson796ed472018-07-16 15:11:09 -07001361```
1362 The script will be executed with the given arguments with the current
1363 directory being that of the root build directory. If you pass files
1364 to your script, see "gn help rebase_path" for how to convert
1365 file names to be relative to the build directory (file names in the
1366 sources, outputs, and inputs will be all treated as relative to the
1367 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001368
1369 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1370 that Ninja will check the timestamp of the output after the action
1371 completes. If output timestamp is unchanged, the step will be treated
1372 as if it never needed to be rebuilt, potentially eliminating some
1373 downstream steps for incremental builds. Scripts can improve build
1374 performance by taking care not to change the timstamp of the output
1375 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001376```
1377
1378#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001379
Brett Wilson796ed472018-07-16 15:11:09 -07001380```
1381 All output files must be inside the output directory of the build.
1382 You would generally use |$target_out_dir| or |$target_gen_dir| to
1383 reference the output or generated intermediate file directories,
1384 respectively.
1385```
1386
1387#### **Variables**
1388
1389```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001390 args, data, data_deps, depfile, deps, inputs, metadata, outputs*, pool,
Brett Wilson796ed472018-07-16 15:11:09 -07001391 response_file_contents, script*, sources*
1392 * = required
1393```
1394
1395#### **Example**
1396
1397```
1398 # Runs the script over each IDL file. The IDL script will generate both a .cc
1399 # and a .h file for each input.
1400 action_foreach("my_idl") {
1401 script = "idl_processor.py"
1402 sources = [ "foo.idl", "bar.idl" ]
1403
Joe Armstrong73e89072019-05-10 11:23:52 +08001404 # Our script reads this file each time, so we need to list it as a
Brett Wilson796ed472018-07-16 15:11:09 -07001405 # dependency so we can rebuild if it changes.
1406 inputs = [ "my_configuration.txt" ]
1407
1408 # Transformation from source file name to output file names.
1409 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
1410 "$target_gen_dir/{{source_name_part}}.cc" ]
1411
1412 # Note that since "args" is opaque to GN, if you specify paths here, you
1413 # will need to convert it to be relative to the build directory using
1414 # rebase_path().
1415 args = [
1416 "{{source}}",
1417 "-o",
1418 rebase_path(relative_target_gen_dir, root_build_dir) +
1419 "/{{source_name_part}}.h" ]
1420 }
1421```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001422### **bundle_data**: [iOS/macOS] Declare a target without output.
Brett Wilson796ed472018-07-16 15:11:09 -07001423
1424```
1425 This target type allows to declare data that is required at runtime. It is
1426 used to inform "create_bundle" targets of the files to copy into generated
1427 bundle, see "gn help create_bundle" for help.
1428
1429 The target must define a list of files as "sources" and a single "outputs".
1430 If there are multiple files, source expansions must be used to express the
1431 output. The output must reference a file inside of {{bundle_root_dir}}.
1432
1433 This target can be used on all platforms though it is designed only to
1434 generate iOS/macOS bundle. In cross-platform projects, it is advised to put it
1435 behind iOS/macOS conditionals.
1436
1437 See "gn help create_bundle" for more information.
1438```
1439
1440#### **Variables**
1441
1442```
Nate Fischer8ed01d32019-01-08 17:32:01 -08001443 sources*, outputs*, deps, data_deps, metadata, public_deps, visibility
Brett Wilson796ed472018-07-16 15:11:09 -07001444 * = required
1445```
1446
1447#### **Examples**
1448
1449```
1450 bundle_data("icudata") {
1451 sources = [ "sources/data/in/icudtl.dat" ]
1452 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
1453 }
1454
1455 bundle_data("base_unittests_bundle_data]") {
1456 sources = [ "test/data" ]
1457 outputs = [
1458 "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" +
1459 "{{source_file_part}}"
1460 ]
1461 }
1462
1463 bundle_data("material_typography_bundle_data") {
1464 sources = [
1465 "src/MaterialTypography.bundle/Roboto-Bold.ttf",
1466 "src/MaterialTypography.bundle/Roboto-Italic.ttf",
1467 "src/MaterialTypography.bundle/Roboto-Regular.ttf",
1468 "src/MaterialTypography.bundle/Roboto-Thin.ttf",
1469 ]
1470 outputs = [
1471 "{{bundle_resources_dir}}/MaterialTypography.bundle/"
1472 "{{source_file_part}}"
1473 ]
1474 }
1475```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001476### **copy**: Declare a target that copies files.
Brett Wilson796ed472018-07-16 15:11:09 -07001477
1478#### **File name handling**
1479
1480```
1481 All output files must be inside the output directory of the build. You would
1482 generally use |$target_out_dir| or |$target_gen_dir| to reference the output
1483 or generated intermediate file directories, respectively.
1484
1485 Both "sources" and "outputs" must be specified. Sources can include as many
1486 files as you want, but there can only be one item in the outputs list (plural
1487 is used for the name for consistency with other target types).
1488
1489 If there is more than one source file, your output name should specify a
1490 mapping from each source file to an output file name using source expansion
1491 (see "gn help source_expansion"). The placeholders will look like
1492 "{{source_name_part}}", for example.
1493```
1494
1495#### **Examples**
1496
1497```
1498 # Write a rule that copies a checked-in DLL to the output directory.
1499 copy("mydll") {
1500 sources = [ "mydll.dll" ]
1501 outputs = [ "$target_out_dir/mydll.dll" ]
1502 }
1503
1504 # Write a rule to copy several files to the target generated files directory.
1505 copy("myfiles") {
1506 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1507
1508 # Use source expansion to generate output files with the corresponding file
1509 # names in the gen dir. This will just copy each file.
1510 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1511 }
1512```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001513### **create_bundle**: [ios/macOS] Build an iOS or macOS bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07001514
1515```
1516 This target generates an iOS or macOS bundle (which is a directory with a
1517 well-know structure). This target does not define any sources, instead they
1518 are computed from all "bundle_data" target this one depends on transitively
1519 (the recursion stops at "create_bundle" targets).
1520
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001521 The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in
1522 "bundle_data" outputs. The properties are optional but must be defined if any
1523 of the "bundle_data" target use them.
Brett Wilson796ed472018-07-16 15:11:09 -07001524
1525 This target can be used on all platforms though it is designed only to
1526 generate iOS or macOS bundle. In cross-platform projects, it is advised to put
1527 it behind iOS/macOS conditionals.
1528
1529 If a create_bundle is specified as a data_deps for another target, the bundle
1530 is considered a leaf, and its public and private dependencies will not
1531 contribute to any data or data_deps. Required runtime dependencies should be
1532 placed in the bundle. A create_bundle can declare its own explicit data and
1533 data_deps, however.
1534```
1535
1536#### **Code signing**
1537
1538```
1539 Some bundle needs to be code signed as part of the build (on iOS all
1540 application needs to be code signed to run on a device). The code signature
1541 can be configured via the code_signing_script variable.
1542
1543 If set, code_signing_script is the path of a script that invoked after all
1544 files have been moved into the bundle. The script must not change any file in
1545 the bundle, but may add new files.
1546
1547 If code_signing_script is defined, then code_signing_outputs must also be
1548 defined and non-empty to inform when the script needs to be re-run. The
1549 code_signing_args will be passed as is to the script (so path have to be
1550 rebased) and additional inputs may be listed with the variable
1551 code_signing_sources.
1552```
1553
1554#### **Variables**
1555
1556```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001557 bundle_root_dir, bundle_contents_dir, bundle_resources_dir,
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001558 bundle_executable_dir, bundle_deps_filter, deps, data_deps, public_deps,
1559 visibility, product_type, code_signing_args, code_signing_script,
1560 code_signing_sources, code_signing_outputs, xcode_extra_attributes,
1561 xcode_test_application_name, partial_info_plist, metadata
Brett Wilson796ed472018-07-16 15:11:09 -07001562```
1563
1564#### **Example**
1565
1566```
1567 # Defines a template to create an application. On most platform, this is just
1568 # an alias for an "executable" target, but on iOS/macOS, it builds an
1569 # application bundle.
1570 template("app") {
1571 if (!is_ios && !is_mac) {
1572 executable(target_name) {
1573 forward_variables_from(invoker, "*")
1574 }
1575 } else {
1576 app_name = target_name
1577 gen_path = target_gen_dir
1578
1579 action("${app_name}_generate_info_plist") {
1580 script = [ "//build/ios/ios_gen_plist.py" ]
1581 sources = [ "templates/Info.plist" ]
1582 outputs = [ "$gen_path/Info.plist" ]
1583 args = rebase_path(sources, root_build_dir) +
1584 rebase_path(outputs, root_build_dir)
1585 }
1586
1587 bundle_data("${app_name}_bundle_info_plist") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001588 public_deps = [ ":${app_name}_generate_info_plist" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001589 sources = [ "$gen_path/Info.plist" ]
1590 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
1591 }
1592
1593 executable("${app_name}_generate_executable") {
1594 forward_variables_from(invoker, "*", [
1595 "output_name",
1596 "visibility",
1597 ])
1598 output_name =
1599 rebase_path("$gen_path/$app_name", root_build_dir)
1600 }
1601
1602 code_signing =
1603 defined(invoker.code_signing) && invoker.code_signing
1604
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001605 if (!is_ios || !code_signing) {
Brett Wilson796ed472018-07-16 15:11:09 -07001606 bundle_data("${app_name}_bundle_executable") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001607 public_deps = [ ":${app_name}_generate_executable" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001608 sources = [ "$gen_path/$app_name" ]
1609 outputs = [ "{{bundle_executable_dir}}/$app_name" ]
1610 }
1611 }
1612
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001613 create_bundle("$app_name.app") {
Brett Wilson796ed472018-07-16 15:11:09 -07001614 product_type = "com.apple.product-type.application"
1615
1616 if (is_ios) {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001617 bundle_root_dir = "$root_build_dir/$target_name"
Brett Wilson796ed472018-07-16 15:11:09 -07001618 bundle_contents_dir = bundle_root_dir
1619 bundle_resources_dir = bundle_contents_dir
1620 bundle_executable_dir = bundle_contents_dir
Brett Wilson796ed472018-07-16 15:11:09 -07001621
Sylvain Defresne7b0690e2020-06-26 15:33:01 +02001622 xcode_extra_attributes = {
Brett Wilson796ed472018-07-16 15:11:09 -07001623 ONLY_ACTIVE_ARCH = "YES"
1624 DEBUG_INFORMATION_FORMAT = "dwarf"
1625 }
1626 } else {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001627 bundle_root_dir = "$root_build_dir/$target_name"
1628 bundle_contents_dir = "$bundle_root_dir/Contents"
1629 bundle_resources_dir = "$bundle_contents_dir/Resources"
1630 bundle_executable_dir = "$bundle_contents_dir/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07001631 }
1632 deps = [ ":${app_name}_bundle_info_plist" ]
1633 if (is_ios && code_signing) {
1634 deps += [ ":${app_name}_generate_executable" ]
1635 code_signing_script = "//build/config/ios/codesign.py"
1636 code_signing_sources = [
1637 invoker.entitlements_path,
1638 "$target_gen_dir/$app_name",
1639 ]
1640 code_signing_outputs = [
1641 "$bundle_root_dir/$app_name",
1642 "$bundle_root_dir/_CodeSignature/CodeResources",
1643 "$bundle_root_dir/embedded.mobileprovision",
1644 "$target_gen_dir/$app_name.xcent",
1645 ]
1646 code_signing_args = [
1647 "-i=" + ios_code_signing_identity,
1648 "-b=" + rebase_path(
1649 "$target_gen_dir/$app_name", root_build_dir),
1650 "-e=" + rebase_path(
1651 invoker.entitlements_path, root_build_dir),
1652 "-e=" + rebase_path(
1653 "$target_gen_dir/$app_name.xcent", root_build_dir),
1654 rebase_path(bundle_root_dir, root_build_dir),
1655 ]
1656 } else {
1657 deps += [ ":${app_name}_bundle_executable" ]
1658 }
1659 }
1660 }
1661 }
1662```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001663### **executable**: Declare an executable target.
Brett Wilson796ed472018-07-16 15:11:09 -07001664
Julie Hockettce1fa072019-05-07 17:44:37 -07001665#### **Language and compilation**
1666
1667```
1668 The tools and commands used to create this target type will be
1669 determined by the source files in its sources. Targets containing
1670 multiple compiler-incompatible languages are not allowed (e.g. a
1671 target containing both C and C++ sources is acceptable, but a
1672 target containing C and Rust sources is not).
1673```
1674
Brett Wilson796ed472018-07-16 15:11:09 -07001675#### **Variables**
1676
1677```
1678 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1679 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001680 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001681 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07001682 Deps: data_deps, deps, public_deps
1683 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001684 General: check_includes, configs, data, friend, inputs, metadata,
1685 output_name, output_extension, public, sources, testonly,
1686 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001687 Rust variables: aliased_deps, crate_root, crate_name
Nate Fischer8ed01d32019-01-08 17:32:01 -08001688```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001689### **generated_file**: Declare a generated_file target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001690
1691```
1692 Writes data value(s) to disk on resolution. This target type mirrors some
1693 functionality of the write_file() function, but also provides the ability to
1694 collect metadata from its dependencies on resolution rather than writing out
Keir Mierle45611e32019-11-12 11:18:00 -08001695 at parse time.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001696
1697 The `outputs` variable is required to be a list with a single element,
1698 specifying the intended location of the output file.
1699
1700 The `output_conversion` variable specified the format to write the
1701 value. See `gn help output_conversion`.
1702
1703 One of `contents` or `data_keys` must be specified; use of `data` will write
1704 the contents of that value to file, while use of `data_keys` will trigger a
1705 metadata collection walk based on the dependencies of the target and the
1706 optional values of the `rebase` and `walk_keys` variables. See
1707 `gn help metadata`.
1708
1709 Collected metadata, if specified, will be returned in postorder of
1710 dependencies. See the example for details.
1711```
1712
1713#### **Example (metadata collection)**
1714
1715```
1716 Given the following targets defined in //base/BUILD.gn, where A depends on B
1717 and B depends on C and D:
1718
1719 group("a") {
1720 metadata = {
1721 doom_melon = [ "enable" ]
1722 my_files = [ "foo.cpp" ]
1723
Keir Mierle45611e32019-11-12 11:18:00 -08001724 # Note: this is functionally equivalent to not defining `my_barrier`
1725 # at all in this target's metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001726 my_barrier = [ "" ]
1727 }
1728
1729 deps = [ ":b" ]
1730 }
1731
Julie Hockettd69a9c32019-01-23 14:36:18 -08001732 group("b") {
Nate Fischer8ed01d32019-01-08 17:32:01 -08001733 metadata = {
1734 my_files = [ "bar.cpp" ]
1735 my_barrier = [ ":c" ]
1736 }
1737
1738 deps = [ ":c", ":d" ]
1739 }
1740
1741 group("c") {
1742 metadata = {
1743 doom_melon = [ "disable" ]
1744 my_files = [ "baz.cpp" ]
1745 }
1746 }
1747
1748 group("d") {
1749 metadata = {
1750 my_files = [ "missing.cpp" ]
1751 }
1752 }
1753
1754 If the following generated_file target is defined:
1755
1756 generated_file("my_files_metadata") {
1757 outputs = [ "$root_build_dir/my_files.json" ]
1758 data_keys = [ "my_files" ]
1759
1760 deps = [ "//base:a" ]
1761 }
1762
1763 The following will be written to "$root_build_dir/my_files.json" (less the
1764 comments):
1765 [
1766 "baz.cpp", // from //base:c via //base:b
1767 "missing.cpp" // from //base:d via //base:b
1768 "bar.cpp", // from //base:b via //base:a
1769 "foo.cpp", // from //base:a
1770 ]
1771
1772 Alternatively, as an example of using walk_keys, if the following
1773 generated_file target is defined:
1774
1775 generated_file("my_files_metadata") {
1776 outputs = [ "$root_build_dir/my_files.json" ]
1777 data_keys = [ "my_files" ]
1778 walk_keys = [ "my_barrier" ]
1779
1780 deps = [ "//base:a" ]
1781 }
1782
1783 The following will be written to "$root_build_dir/my_files.json" (again less
1784 the comments):
1785 [
1786 "baz.cpp", // from //base:c via //base:b
1787 "bar.cpp", // from //base:b via //base:a
1788 "foo.cpp", // from //base:a
1789 ]
1790
1791 If `rebase` is used in the following generated_file target:
1792
1793 generated_file("my_files_metadata") {
1794 outputs = [ "$root_build_dir/my_files.json" ]
1795 data_keys = [ "my_files" ]
1796 walk_keys = [ "my_barrier" ]
1797 rebase = root_build_dir
1798
1799 deps = [ "//base:a" ]
1800 }
1801
1802 The following will be written to "$root_build_dir/my_files.json" (again less
1803 the comments) (assuming root_build_dir = "//out"):
1804 [
1805 "../base/baz.cpp", // from //base:c via //base:b
1806 "../base/bar.cpp", // from //base:b via //base:a
1807 "../base/foo.cpp", // from //base:a
1808 ]
1809```
1810
1811#### **Variables**
1812
1813```
Julie Hockettd69a9c32019-01-23 14:36:18 -08001814 contents
Nate Fischer8ed01d32019-01-08 17:32:01 -08001815 data_keys
1816 rebase
1817 walk_keys
1818 output_conversion
1819 Deps: data_deps, deps, public_deps
1820 Dependent configs: all_dependent_configs, public_configs
Brett Wilson796ed472018-07-16 15:11:09 -07001821```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001822### **group**: Declare a named group of targets.
Brett Wilson796ed472018-07-16 15:11:09 -07001823
1824```
1825 This target type allows you to create meta-targets that just collect a set of
1826 dependencies into one named target. Groups can additionally specify configs
1827 that apply to their dependents.
1828```
1829
1830#### **Variables**
1831
1832```
1833 Deps: data_deps, deps, public_deps
1834 Dependent configs: all_dependent_configs, public_configs
1835```
1836
1837#### **Example**
1838
1839```
1840 group("all") {
1841 deps = [
1842 "//project:runner",
1843 "//project:unit_tests",
1844 ]
1845 }
1846```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001847### **loadable_module**: Declare a loadable module target.
Brett Wilson796ed472018-07-16 15:11:09 -07001848
1849```
1850 This target type allows you to create an object file that is (and can only
1851 be) loaded and unloaded at runtime.
1852
1853 A loadable module will be specified on the linker line for targets listing
1854 the loadable module in its "deps". If you don't want this (if you don't need
1855 to dynamically load the library at runtime), then you should use a
1856 "shared_library" target type instead.
1857```
1858
Julie Hockettce1fa072019-05-07 17:44:37 -07001859#### **Language and compilation**
1860
1861```
1862 The tools and commands used to create this target type will be
1863 determined by the source files in its sources. Targets containing
1864 multiple compiler-incompatible languages are not allowed (e.g. a
1865 target containing both C and C++ sources is acceptable, but a
1866 target containing C and Rust sources is not).
1867```
1868
Brett Wilson796ed472018-07-16 15:11:09 -07001869#### **Variables**
1870
1871```
1872 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1873 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001874 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001875 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07001876 Deps: data_deps, deps, public_deps
1877 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001878 General: check_includes, configs, data, friend, inputs, metadata,
1879 output_name, output_extension, public, sources, testonly,
1880 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001881 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Julie Hockettce1fa072019-05-07 17:44:37 -07001882```
1883### **rust_library**: Declare a Rust library target.
1884
1885```
1886 A Rust library is an archive containing additional rust-c provided metadata.
1887 These are the files produced by the rustc compiler with the `.rlib`
1888 extension, and are the intermediate step for most Rust-based binaries.
1889```
1890
1891#### **Language and compilation**
1892
1893```
1894 The tools and commands used to create this target type will be
1895 determined by the source files in its sources. Targets containing
1896 multiple compiler-incompatible languages are not allowed (e.g. a
1897 target containing both C and C++ sources is acceptable, but a
1898 target containing C and Rust sources is not).
1899```
1900
1901#### **Variables**
1902
1903```
1904 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1905 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
1906 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001907 rustenv, swiftflags
Julie Hockettce1fa072019-05-07 17:44:37 -07001908 Deps: data_deps, deps, public_deps
1909 Dependent configs: all_dependent_configs, public_configs
1910 General: check_includes, configs, data, friend, inputs, metadata,
1911 output_name, output_extension, public, sources, testonly,
1912 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001913 Rust variables: aliased_deps, crate_root, crate_name
Brett Wilson796ed472018-07-16 15:11:09 -07001914```
Petr Hosekfe36c7c2019-11-26 14:38:08 -08001915### **rust_proc_macro**: Declare a Rust procedural macro target.
1916
1917```
1918 A Rust procedural macro allows creating syntax extensions as execution of a
1919 function. They are compiled as dynamic libraries and used by the compiler at
1920 runtime.
1921
1922 Their use is the same as of other Rust libraries, but their build has some
1923 additional restrictions in terms of supported flags.
1924```
1925
1926#### **Language and compilation**
1927
1928```
1929 The tools and commands used to create this target type will be
1930 determined by the source files in its sources. Targets containing
1931 multiple compiler-incompatible languages are not allowed (e.g. a
1932 target containing both C and C++ sources is acceptable, but a
1933 target containing C and Rust sources is not).
1934```
1935
1936#### **Variables**
1937
1938```
1939 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1940 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
1941 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001942 rustenv, swiftflags
Petr Hosekfe36c7c2019-11-26 14:38:08 -08001943 Deps: data_deps, deps, public_deps
1944 Dependent configs: all_dependent_configs, public_configs
1945 General: check_includes, configs, data, friend, inputs, metadata,
1946 output_name, output_extension, public, sources, testonly,
1947 visibility
1948 Rust variables: aliased_deps, crate_root, crate_name
1949```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001950### **shared_library**: Declare a shared library target.
Brett Wilson796ed472018-07-16 15:11:09 -07001951
1952```
1953 A shared library will be specified on the linker line for targets listing the
1954 shared library in its "deps". If you don't want this (say you dynamically
1955 load the library at runtime), then you should depend on the shared library
1956 via "data_deps" or, on Darwin platforms, use a "loadable_module" target type
1957 instead.
1958```
1959
Julie Hockettce1fa072019-05-07 17:44:37 -07001960#### **Language and compilation**
1961
1962```
1963 The tools and commands used to create this target type will be
1964 determined by the source files in its sources. Targets containing
1965 multiple compiler-incompatible languages are not allowed (e.g. a
1966 target containing both C and C++ sources is acceptable, but a
1967 target containing C and Rust sources is not).
1968```
1969
Brett Wilson796ed472018-07-16 15:11:09 -07001970#### **Variables**
1971
1972```
1973 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1974 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07001975 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02001976 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07001977 Deps: data_deps, deps, public_deps
1978 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001979 General: check_includes, configs, data, friend, inputs, metadata,
1980 output_name, output_extension, public, sources, testonly,
1981 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001982 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Brett Wilson796ed472018-07-16 15:11:09 -07001983```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001984### **source_set**: Declare a source set target.
Brett Wilson796ed472018-07-16 15:11:09 -07001985
1986```
Robert Sesekd0a6f072020-05-15 11:21:22 -04001987 Only C-language source sets are supported at the moment.
Julie Hockettce1fa072019-05-07 17:44:37 -07001988```
1989
1990#### **C-language source_sets**
1991
1992```
Brett Wilson796ed472018-07-16 15:11:09 -07001993 A source set is a collection of sources that get compiled, but are not linked
1994 to produce any kind of library. Instead, the resulting object files are
1995 implicitly added to the linker line of all targets that depend on the source
1996 set.
1997
1998 In most cases, a source set will behave like a static library, except no
1999 actual library file will be produced. This will make the build go a little
2000 faster by skipping creation of a large static library, while maintaining the
2001 organizational benefits of focused build targets.
2002
2003 The main difference between a source set and a static library is around
2004 handling of exported symbols. Most linkers assume declaring a function
2005 exported means exported from the static library. The linker can then do dead
2006 code elimination to delete code not reachable from exported functions.
2007
2008 A source set will not do this code elimination since there is no link step.
Julie Hockette2a29402018-07-31 10:11:42 -07002009 This allows you to link many source sets into a shared library and have the
Brett Wilson796ed472018-07-16 15:11:09 -07002010 "exported symbol" notation indicate "export from the final shared library and
2011 not from the intermediate targets." There is no way to express this concept
2012 when linking multiple static libraries into a shared library.
2013```
2014
2015#### **Variables**
2016
2017```
2018 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2019 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002020 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02002021 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07002022 Deps: data_deps, deps, public_deps
2023 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002024 General: check_includes, configs, data, friend, inputs, metadata,
2025 output_name, output_extension, public, sources, testonly,
2026 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002027```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002028### **static_library**: Declare a static library target.
Brett Wilson796ed472018-07-16 15:11:09 -07002029
2030```
2031 Make a ".a" / ".lib" file.
2032
2033 If you only need the static library for intermediate results in the build,
2034 you should consider a source_set instead since it will skip the (potentially
2035 slow) step of creating the intermediate library file.
2036```
2037
2038#### **Variables**
2039
2040```
2041 complete_static_lib
2042 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2043 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002044 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02002045 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07002046 Deps: data_deps, deps, public_deps
2047 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002048 General: check_includes, configs, data, friend, inputs, metadata,
2049 output_name, output_extension, public, sources, testonly,
2050 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002051 Rust variables: aliased_deps, crate_root, crate_name
Julie Hockettce1fa072019-05-07 17:44:37 -07002052
2053 The tools and commands used to create this target type will be
2054 determined by the source files in its sources. Targets containing
2055 multiple compiler-incompatible languages are not allowed (e.g. a
2056 target containing both C and C++ sources is acceptable, but a
2057 target containing C and Rust sources is not).
Brett Wilson796ed472018-07-16 15:11:09 -07002058```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002059### **target**: Declare an target with the given programmatic type.
Brett Wilson796ed472018-07-16 15:11:09 -07002060
2061```
2062 target(target_type_string, target_name_string) { ... }
2063
2064 The target() function is a way to invoke a built-in target or template with a
2065 type determined at runtime. This is useful for cases where the type of a
2066 target might not be known statically.
2067
2068 Only templates and built-in target functions are supported for the
2069 target_type_string parameter. Arbitrary functions, configs, and toolchains
2070 are not supported.
2071
2072 The call:
2073 target("source_set", "doom_melon") {
2074 Is equivalent to:
2075 source_set("doom_melon") {
2076```
2077
2078#### **Example**
2079
2080```
2081 if (foo_build_as_shared) {
2082 my_type = "shared_library"
2083 } else {
2084 my_type = "source_set"
2085 }
2086
2087 target(my_type, "foo") {
2088 ...
2089 }
2090```
2091## Buildfile functions
2092
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002093### **assert**: Assert an expression is true at generation time.
Brett Wilson796ed472018-07-16 15:11:09 -07002094
2095```
2096 assert( [, ])
2097
2098 If the condition is false, the build will fail with an error. If the
2099 optional second argument is provided, that string will be printed
2100 with the error message.
2101```
2102
2103#### **Examples**
2104
2105```
2106 assert(is_win)
2107 assert(defined(sources), "Sources must be defined");
2108```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002109### **config**: Defines a configuration object.
Brett Wilson796ed472018-07-16 15:11:09 -07002110
2111```
2112 Configuration objects can be applied to targets and specify sets of compiler
2113 flags, includes, defines, etc. They provide a way to conveniently group sets
2114 of this configuration information.
2115
2116 A config is referenced by its label just like a target.
2117
2118 The values in a config are additive only. If you want to remove a flag you
2119 need to remove the corresponding config that sets it. The final set of flags,
2120 defines, etc. for a target is generated in this order:
2121
2122 1. The values specified directly on the target (rather than using a config.
2123 2. The configs specified in the target's "configs" list, in order.
2124 3. Public_configs from a breadth-first traversal of the dependency tree in
2125 the order that the targets appear in "deps".
2126 4. All dependent configs from a breadth-first traversal of the dependency
2127 tree in the order that the targets appear in "deps".
2128```
2129
Joe Armstrong39413942019-03-15 10:34:03 +08002130#### **More background**
2131
2132```
2133 Configs solve a problem where the build system needs to have a higher-level
2134 understanding of various compiler settings. For example, some compiler flags
2135 have to appear in a certain order relative to each other, some settings like
2136 defines and flags logically go together, and the build system needs to
2137 de-duplicate flags even though raw command-line parameters can't always be
2138 operated on in that way.
2139
2140 The config gives a name to a group of settings that can then be reasoned
2141 about by GN. GN can know that configs with the same label are the same thing
2142 so can be de-duplicated. It allows related settings to be grouped so they
2143 are added or removed as a unit. And it allows targets to refer to settings
2144 with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than
2145 having to hard-coding every compiler's flags each time they are referred to.
2146```
2147
Brett Wilson796ed472018-07-16 15:11:09 -07002148#### **Variables valid in a config definition**
Joe Armstrong39413942019-03-15 10:34:03 +08002149
Brett Wilson796ed472018-07-16 15:11:09 -07002150```
2151 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2152 asmflags, defines, include_dirs, inputs, ldflags, lib_dirs,
Julie Hockettce1fa072019-05-07 17:44:37 -07002153 libs, precompiled_header, precompiled_source, rustflags,
Sylvain Defresne89e64252020-08-07 13:01:06 +02002154 rustenv, swiftflags
Brett Wilson796ed472018-07-16 15:11:09 -07002155 Nested configs: configs
2156```
2157
2158#### **Variables on a target used to apply configs**
2159
2160```
2161 all_dependent_configs, configs, public_configs
2162```
2163
2164#### **Example**
2165
2166```
2167 config("myconfig") {
Joe Armstrongf5ad8072019-05-08 08:42:56 +08002168 include_dirs = [ "include/common" ]
Brett Wilson796ed472018-07-16 15:11:09 -07002169 defines = [ "ENABLE_DOOM_MELON" ]
2170 }
2171
2172 executable("mything") {
2173 configs = [ ":myconfig" ]
2174 }
2175```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002176### **declare_args**: Declare build arguments.
Brett Wilson796ed472018-07-16 15:11:09 -07002177
2178```
2179 Introduces the given arguments into the current scope. If they are not
2180 specified on the command line or in a toolchain's arguments, the default
2181 values given in the declare_args block will be used. However, these defaults
2182 will not override command-line values.
2183
2184 See also "gn help buildargs" for an overview.
2185
2186 The precise behavior of declare args is:
2187
2188 1. The declare_args() block executes. Any variable defined in the enclosing
2189 scope is available for reading, but any variable defined earlier in
2190 the current scope is not (since the overrides haven't been applied yet).
2191
2192 2. At the end of executing the block, any variables set within that scope
Petr Hosek7617b592019-10-02 09:52:21 -07002193 are saved, with the values specified in the block used as the "default value"
2194 for that argument. Once saved, these variables are available for override
2195 via args.gn.
Brett Wilson796ed472018-07-16 15:11:09 -07002196
2197 3. User-defined overrides are applied. Anything set in "gn args" now
2198 overrides any default values. The resulting set of variables is promoted
2199 to be readable from the following code in the file.
2200
2201 This has some ramifications that may not be obvious:
2202
2203 - You should not perform difficult work inside a declare_args block since
2204 this only sets a default value that may be discarded. In particular,
2205 don't use the result of exec_script() to set the default value. If you
2206 want to have a script-defined default, set some default "undefined" value
2207 like [], "", or -1, and after the declare_args block, call exec_script if
2208 the value is unset by the user.
2209
2210 - Because you cannot read the value of a variable defined in the same
2211 block, if you need to make the default value of one arg depend
2212 on the possibly-overridden value of another, write two separate
2213 declare_args() blocks:
2214
2215 declare_args() {
2216 enable_foo = true
2217 }
2218 declare_args() {
2219 # Bar defaults to same user-overridden state as foo.
2220 enable_bar = enable_foo
2221 }
2222```
2223
2224#### **Example**
2225
2226```
2227 declare_args() {
2228 enable_teleporter = true
2229 enable_doom_melon = false
2230 }
2231
2232 If you want to override the (default disabled) Doom Melon:
2233 gn --args="enable_doom_melon=true enable_teleporter=true"
2234 This also sets the teleporter, but it's already defaulted to on so it will
2235 have no effect.
2236```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002237### **defined**: Returns whether an identifier is defined.
Brett Wilson796ed472018-07-16 15:11:09 -07002238
2239```
2240 Returns true if the given argument is defined. This is most useful in
2241 templates to assert that the caller set things up properly.
2242
2243 You can pass an identifier:
2244 defined(foo)
2245 which will return true or false depending on whether foo is defined in the
2246 current scope.
2247
2248 You can also check a named scope:
2249 defined(foo.bar)
2250 which will return true or false depending on whether bar is defined in the
2251 named scope foo. It will throw an error if foo is not defined or is not a
2252 scope.
2253```
2254
2255#### **Example**
2256
2257```
2258 template("mytemplate") {
2259 # To help users call this template properly...
2260 assert(defined(invoker.sources), "Sources must be defined")
2261
2262 # If we want to accept an optional "values" argument, we don't
2263 # want to dereference something that may not be defined.
2264 if (defined(invoker.values)) {
2265 values = invoker.values
2266 } else {
2267 values = "some default value"
2268 }
2269 }
2270```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002271### **exec_script**: Synchronously run a script and return the output.
Brett Wilson796ed472018-07-16 15:11:09 -07002272
2273```
2274 exec_script(filename,
2275 arguments = [],
2276 input_conversion = "",
2277 file_dependencies = [])
2278
2279 Runs the given script, returning the stdout of the script. The build
2280 generation will fail if the script does not exist or returns a nonzero exit
2281 code.
2282
2283 The current directory when executing the script will be the root build
2284 directory. If you are passing file names, you will want to use the
2285 rebase_path() function to make file names relative to this path (see "gn help
2286 rebase_path").
Gary Miguelce7fa362018-09-17 12:48:17 -07002287
2288 The default script interpreter is Python ("python" on POSIX, "python.exe" or
2289 "python.bat" on Windows). This can be configured by the script_executable
2290 variable, see "gn help dotfile".
Brett Wilson796ed472018-07-16 15:11:09 -07002291```
2292
2293#### **Arguments**:
2294
2295```
2296 filename:
Gary Miguelce7fa362018-09-17 12:48:17 -07002297 File name of script to execute. Non-absolute names will be treated as
2298 relative to the current build file.
Brett Wilson796ed472018-07-16 15:11:09 -07002299
2300 arguments:
2301 A list of strings to be passed to the script as arguments. May be
2302 unspecified or the empty list which means no arguments.
2303
2304 input_conversion:
Julie Hockett81ee1962019-05-13 11:24:42 -07002305 Controls how the file is read and parsed. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07002306
2307 If unspecified, defaults to the empty string which causes the script
2308 result to be discarded. exec script will return None.
2309
2310 dependencies:
2311 (Optional) A list of files that this script reads or otherwise depends
2312 on. These dependencies will be added to the build result such that if any
2313 of them change, the build will be regenerated and the script will be
2314 re-run.
2315
2316 The script itself will be an implicit dependency so you do not need to
2317 list it.
2318```
2319
2320#### **Example**
2321
2322```
2323 all_lines = exec_script(
2324 "myscript.py", [some_input], "list lines",
2325 [ rebase_path("data_file.txt", root_build_dir) ])
2326
2327 # This example just calls the script with no arguments and discards the
2328 # result.
2329 exec_script("//foo/bar/myscript.py")
2330```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01002331### **filter_exclude**: Remove values that match a set of patterns.
2332
2333```
2334 filter_exclude(values, exclude_patterns)
2335
2336 The argument values must be a list of strings.
2337
2338 The argument exclude_patterns must be a list of file patterns (see
2339 "gn help file_pattern"). Any elements in values matching at least one
2340 of those patterns will be excluded.
2341```
2342
2343#### **Examples**
2344```
2345 values = [ "foo.cc", "foo.h", "foo.proto" ]
2346 result = filter_exclude(values, [ "*.proto" ])
2347 # result will be [ "foo.cc", "foo.h" ]
2348```
2349### **filter_include**: Remove values that do not match a set of patterns.
2350
2351```
2352 filter_include(values, include_patterns)
2353
2354 The argument values must be a list of strings.
2355
2356 The argument include_patterns must be a list of file patterns (see
2357 "gn help file_pattern"). Only elements from values matching at least
2358 one of the pattern will be included.
2359```
2360
2361#### **Examples**
2362```
2363 values = [ "foo.cc", "foo.h", "foo.proto" ]
2364 result = filter_include(values, [ "*.proto" ])
2365 # result will be [ "foo.proto" ]
2366```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002367### **foreach**: Iterate over a list.
Brett Wilson796ed472018-07-16 15:11:09 -07002368
2369```
2370 foreach(, ) {
2371
2372 }
2373
2374 Executes the loop contents block over each item in the list, assigning the
2375 loop_var to each item in sequence. The will be a copy so assigning
2376 to it will not mutate the list. The loop will iterate over a copy of
2377 so mutating it inside the loop will not affect iteration.
2378
2379 The block does not introduce a new scope, so that variable assignments inside
2380 the loop will be visible once the loop terminates.
2381
2382 The loop variable will temporarily shadow any existing variables with the
2383 same name for the duration of the loop. After the loop terminates the loop
2384 variable will no longer be in scope, and the previous value (if any) will be
2385 restored.
2386```
2387
2388#### **Example**
2389
2390```
2391 mylist = [ "a", "b", "c" ]
2392 foreach(i, mylist) {
2393 print(i)
2394 }
2395
2396 Prints:
2397 a
2398 b
2399 c
2400```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002401### **forward_variables_from**: Copies variables from a different scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002402
2403```
2404 forward_variables_from(from_scope, variable_list_or_star,
2405 variable_to_not_forward_list = [])
2406
2407 Copies the given variables from the given scope to the local scope if they
2408 exist. This is normally used in the context of templates to use the values of
2409 variables defined in the template invocation to a template-defined target.
2410
2411 The variables in the given variable_list will be copied if they exist in the
2412 given scope or any enclosing scope. If they do not exist, nothing will happen
2413 and they be left undefined in the current scope.
2414
2415 As a special case, if the variable_list is a string with the value of "*",
2416 all variables from the given scope will be copied. "*" only copies variables
2417 set directly on the from_scope, not enclosing ones. Otherwise it would
2418 duplicate all global variables.
2419
2420 When an explicit list of variables is supplied, if the variable exists in the
2421 current (destination) scope already, an error will be thrown. If "*" is
2422 specified, variables in the current scope will be clobbered (the latter is
2423 important because most targets have an implicit configs list, which means it
2424 wouldn't work at all if it didn't clobber).
2425
Brett Wilson796ed472018-07-16 15:11:09 -07002426 If variables_to_not_forward_list is non-empty, then it must contains a list
2427 of variable names that will not be forwarded. This is mostly useful when
2428 variable_list_or_star has a value of "*".
2429```
2430
2431#### **Examples**
2432
2433```
Andrew Grieve57a964c2018-09-14 10:50:56 -04002434 # forward_variables_from(invoker, ["foo"])
2435 # is equivalent to:
2436 assert(!defined(foo))
2437 if (defined(invoker.foo)) {
2438 foo = invoker.foo
2439 }
2440
Brett Wilson796ed472018-07-16 15:11:09 -07002441 # This is a common action template. It would invoke a script with some given
2442 # parameters, and wants to use the various types of deps and the visibility
2443 # from the invoker if it's defined. It also injects an additional dependency
2444 # to all targets.
2445 template("my_test") {
2446 action(target_name) {
2447 forward_variables_from(invoker, [ "data_deps", "deps",
Julie Hockette2a29402018-07-31 10:11:42 -07002448 "public_deps", "visibility"])
Brett Wilson796ed472018-07-16 15:11:09 -07002449 # Add our test code to the dependencies.
2450 # "deps" may or may not be defined at this point.
2451 if (defined(deps)) {
2452 deps += [ "//tools/doom_melon" ]
2453 } else {
2454 deps = [ "//tools/doom_melon" ]
2455 }
2456 }
2457 }
2458
Julie Hockette2a29402018-07-31 10:11:42 -07002459 # This is a template around a target whose type depends on a global variable.
2460 # It forwards all values from the invoker.
Brett Wilson796ed472018-07-16 15:11:09 -07002461 template("my_wrapper") {
2462 target(my_wrapper_target_type, target_name) {
2463 forward_variables_from(invoker, "*")
2464 }
2465 }
2466
2467 # A template that wraps another. It adds behavior based on one
2468 # variable, and forwards all others to the nested target.
2469 template("my_ios_test_app") {
2470 ios_test_app(target_name) {
2471 forward_variables_from(invoker, "*", ["test_bundle_name"])
2472 if (!defined(extra_substitutions)) {
2473 extra_substitutions = []
2474 }
2475 extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ]
2476 }
2477 }
2478```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002479### **get_label_info**: Get an attribute from a target's label.
Brett Wilson796ed472018-07-16 15:11:09 -07002480
2481```
2482 get_label_info(target_label, what)
2483
2484 Given the label of a target, returns some attribute of that target. The
2485 target need not have been previously defined in the same file, since none of
2486 the attributes depend on the actual target definition, only the label itself.
2487
2488 See also "gn help get_target_outputs".
2489```
2490
2491#### **Possible values for the "what" parameter**
2492
2493```
2494 "name"
2495 The short name of the target. This will match the value of the
2496 "target_name" variable inside that target's declaration. For the label
2497 "//foo/bar:baz" this will return "baz".
2498
2499 "dir"
2500 The directory containing the target's definition, with no slash at the
2501 end. For the label "//foo/bar:baz" this will return "//foo/bar".
2502
2503 "target_gen_dir"
2504 The generated file directory for the target. This will match the value of
2505 the "target_gen_dir" variable when inside that target's declaration.
2506
2507 "root_gen_dir"
2508 The root of the generated file tree for the target. This will match the
2509 value of the "root_gen_dir" variable when inside that target's
2510 declaration.
2511
2512 "target_out_dir
2513 The output directory for the target. This will match the value of the
2514 "target_out_dir" variable when inside that target's declaration.
2515
2516 "root_out_dir"
2517 The root of the output file tree for the target. This will match the
2518 value of the "root_out_dir" variable when inside that target's
2519 declaration.
2520
2521 "label_no_toolchain"
2522 The fully qualified version of this label, not including the toolchain.
2523 For the input ":bar" it might return "//foo:bar".
2524
2525 "label_with_toolchain"
2526 The fully qualified version of this label, including the toolchain. For
2527 the input ":bar" it might return "//foo:bar(//toolchain:x64)".
2528
2529 "toolchain"
2530 The label of the toolchain. This will match the value of the
2531 "current_toolchain" variable when inside that target's declaration.
2532```
2533
2534#### **Examples**
2535
2536```
2537 get_label_info(":foo", "name")
2538 # Returns string "foo".
2539
2540 get_label_info("//foo/bar:baz", "target_gen_dir")
2541 # Returns string "//out/Debug/gen/foo/bar".
2542```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002543### **get_path_info**: Extract parts of a file or directory name.
Brett Wilson796ed472018-07-16 15:11:09 -07002544
2545```
2546 get_path_info(input, what)
2547
2548 The first argument is either a string representing a file or directory name,
2549 or a list of such strings. If the input is a list the return value will be a
2550 list containing the result of applying the rule to each item in the input.
2551```
2552
2553#### **Possible values for the "what" parameter**
2554
2555```
2556 "file"
2557 The substring after the last slash in the path, including the name and
2558 extension. If the input ends in a slash, the empty string will be
2559 returned.
2560 "foo/bar.txt" => "bar.txt"
2561 "bar.txt" => "bar.txt"
2562 "foo/" => ""
2563 "" => ""
2564
2565 "name"
2566 The substring of the file name not including the extension.
2567 "foo/bar.txt" => "bar"
2568 "foo/bar" => "bar"
2569 "foo/" => ""
2570
2571 "extension"
2572 The substring following the last period following the last slash, or the
2573 empty string if not found. The period is not included.
2574 "foo/bar.txt" => "txt"
2575 "foo/bar" => ""
2576
2577 "dir"
2578 The directory portion of the name, not including the slash.
2579 "foo/bar.txt" => "foo"
2580 "//foo/bar" => "//foo"
2581 "foo" => "."
2582
2583 The result will never end in a slash, so if the resulting is empty, the
2584 system ("/") or source ("//") roots, a "." will be appended such that it
2585 is always legal to append a slash and a filename and get a valid path.
2586
2587 "out_dir"
2588 The output file directory corresponding to the path of the given file,
2589 not including a trailing slash.
2590 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
2591
2592 "gen_dir"
2593 The generated file directory corresponding to the path of the given file,
2594 not including a trailing slash.
2595 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
2596
2597 "abspath"
2598 The full absolute path name to the file or directory. It will be resolved
2599 relative to the current directory, and then the source- absolute version
2600 will be returned. If the input is system- absolute, the same input will
2601 be returned.
2602 "foo/bar.txt" => "//mydir/foo/bar.txt"
2603 "foo/" => "//mydir/foo/"
2604 "//foo/bar" => "//foo/bar" (already absolute)
2605 "/usr/include" => "/usr/include" (already absolute)
2606
2607 If you want to make the path relative to another directory, or to be
2608 system-absolute, see rebase_path().
2609```
2610
2611#### **Examples**
2612```
2613 sources = [ "foo.cc", "foo.h" ]
2614 result = get_path_info(source, "abspath")
2615 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
2616
2617 result = get_path_info("//foo/bar/baz.cc", "dir")
2618 # result will be "//foo/bar"
2619
2620 # Extract the source-absolute directory name,
Joe Armstrong39413942019-03-15 10:34:03 +08002621 result = get_path_info(get_path_info(path, "dir"), "abspath")
Brett Wilson796ed472018-07-16 15:11:09 -07002622```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002623### **get_target_outputs**: [file list] Get the list of outputs from a target.
Brett Wilson796ed472018-07-16 15:11:09 -07002624
2625```
2626 get_target_outputs(target_label)
2627
2628 Returns a list of output files for the named target. The named target must
2629 have been previously defined in the current file before this function is
2630 called (it can't reference targets in other files because there isn't a
2631 defined execution order, and it obviously can't reference targets that are
2632 defined after the function call).
2633
Julie Hockettce1fa072019-05-07 17:44:37 -07002634 Only copy, generated_file, and action targets are supported. The outputs from
2635 binary targets will depend on the toolchain definition which won't
2636 necessarily have been loaded by the time a given line of code has run, and
2637 source sets and groups have no useful output file.
Brett Wilson796ed472018-07-16 15:11:09 -07002638```
2639
2640#### **Return value**
2641
2642```
2643 The names in the resulting list will be absolute file paths (normally like
2644 "//out/Debug/bar.exe", depending on the build directory).
2645
Julie Hockettce1fa072019-05-07 17:44:37 -07002646 action, copy, and generated_file targets: this will just return the files
2647 specified in the "outputs" variable of the target.
Brett Wilson796ed472018-07-16 15:11:09 -07002648
2649 action_foreach targets: this will return the result of applying the output
2650 template to the sources (see "gn help source_expansion"). This will be the
2651 same result (though with guaranteed absolute file paths), as
2652 process_file_template will return for those inputs (see "gn help
2653 process_file_template").
2654
Brett Wilson796ed472018-07-16 15:11:09 -07002655 source sets and groups: this will return a list containing the path of the
RJ Ascani843b5002020-11-04 14:49:23 -08002656 "stamp" file that Ninja will produce once all outputs are generated. This
Brett Wilson796ed472018-07-16 15:11:09 -07002657 probably isn't very useful.
2658```
2659
2660#### **Example**
2661
2662```
2663 # Say this action generates a bunch of C source files.
2664 action_foreach("my_action") {
2665 sources = [ ... ]
2666 outputs = [ ... ]
2667 }
2668
2669 # Compile the resulting source files into a source set.
2670 source_set("my_lib") {
2671 sources = get_target_outputs(":my_action")
2672 }
2673```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002674### **getenv**: Get an environment variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002675
2676```
2677 value = getenv(env_var_name)
2678
2679 Returns the value of the given environment variable. If the value is not
2680 found, it will try to look up the variable with the "opposite" case (based on
2681 the case of the first letter of the variable), but is otherwise
2682 case-sensitive.
2683
2684 If the environment variable is not found, the empty string will be returned.
2685 Note: it might be nice to extend this if we had the concept of "none" in the
2686 language to indicate lookup failure.
2687```
2688
2689#### **Example**
2690
2691```
2692 home_dir = getenv("HOME")
2693```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002694### **import**: Import a file into the current scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002695
2696```
2697 The import command loads the rules and variables resulting from executing the
2698 given file into the current scope.
2699
2700 By convention, imported files are named with a .gni extension.
2701
2702 An import is different than a C++ "include". The imported file is executed in
2703 a standalone environment from the caller of the import command. The results
2704 of this execution are cached for other files that import the same .gni file.
2705
2706 Note that you can not import a BUILD.gn file that's otherwise used in the
2707 build. Files must either be imported or implicitly loaded as a result of deps
2708 rules, but not both.
2709
2710 The imported file's scope will be merged with the scope at the point import
2711 was called. If there is a conflict (both the current scope and the imported
2712 file define some variable or rule with the same name but different value), a
2713 runtime error will be thrown. Therefore, it's good practice to minimize the
2714 stuff that an imported file defines.
2715
2716 Variables and templates beginning with an underscore '_' are considered
2717 private and will not be imported. Imported files can use such variables for
2718 internal computation without affecting other files.
2719```
2720
2721#### **Examples**
2722
2723```
2724 import("//build/rules/idl_compilation_rule.gni")
2725
2726 # Looks in the current directory.
2727 import("my_vars.gni")
2728```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002729### **not_needed**: Mark variables from scope as not needed.
Brett Wilson796ed472018-07-16 15:11:09 -07002730
2731```
2732 not_needed(variable_list_or_star, variable_to_ignore_list = [])
2733 not_needed(from_scope, variable_list_or_star,
2734 variable_to_ignore_list = [])
2735
2736 Mark the variables in the current or given scope as not needed, which means
2737 you will not get an error about unused variables for these. The
2738 variable_to_ignore_list allows excluding variables from "all matches" if
2739 variable_list_or_star is "*".
2740```
2741
2742#### **Example**
2743
2744```
2745 not_needed("*", [ "config" ])
2746 not_needed([ "data_deps", "deps" ])
2747 not_needed(invoker, "*", [ "config" ])
2748 not_needed(invoker, [ "data_deps", "deps" ])
2749```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002750### **pool**: Defines a pool object.
Brett Wilson796ed472018-07-16 15:11:09 -07002751
2752```
2753 Pool objects can be applied to a tool to limit the parallelism of the
2754 build. This object has a single property "depth" corresponding to
2755 the number of tasks that may run simultaneously.
2756
2757 As the file containing the pool definition may be executed in the
2758 context of more than one toolchain it is recommended to specify an
2759 explicit toolchain when defining and referencing a pool.
2760
Julie Hockette2a29402018-07-31 10:11:42 -07002761 A pool named "console" defined in the root build file represents Ninja's
2762 console pool. Targets using this pool will have access to the console's
2763 stdin and stdout, and output will not be buffered. This special pool must
2764 have a depth of 1. Pools not defined in the root must not be named "console".
2765 The console pool can only be defined for the default toolchain.
2766 Refer to the Ninja documentation on the console pool for more info.
2767
Brett Wilson796ed472018-07-16 15:11:09 -07002768 A pool is referenced by its label just like a target.
2769```
2770
2771#### **Variables**
2772
2773```
2774 depth*
2775 * = required
2776```
2777
2778#### **Example**
2779
2780```
2781 if (current_toolchain == default_toolchain) {
2782 pool("link_pool") {
2783 depth = 1
2784 }
2785 }
2786
2787 toolchain("toolchain") {
2788 tool("link") {
2789 command = "..."
Petr Hosekadd1bb12019-09-11 14:39:38 -07002790 pool = ":link_pool($default_toolchain)"
Brett Wilson796ed472018-07-16 15:11:09 -07002791 }
2792 }
2793```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002794### **print**: Prints to the console.
Brett Wilson796ed472018-07-16 15:11:09 -07002795
2796```
2797 Prints all arguments to the console separated by spaces. A newline is
2798 automatically appended to the end.
2799
2800 This function is intended for debugging. Note that build files are run in
2801 parallel so you may get interleaved prints. A buildfile may also be executed
2802 more than once in parallel in the context of different toolchains so the
2803 prints from one file may be duplicated or
2804 interleaved with itself.
2805```
2806
2807#### **Examples**
2808
2809```
2810 print("Hello world")
2811
2812 print(sources, deps)
2813```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002814### **process_file_template**: Do template expansion over a list of files.
Brett Wilson796ed472018-07-16 15:11:09 -07002815
2816```
2817 process_file_template(source_list, template)
2818
2819 process_file_template applies a template list to a source file list,
2820 returning the result of applying each template to each source. This is
2821 typically used for computing output file names from input files.
2822
2823 In most cases, get_target_outputs() will give the same result with shorter,
2824 more maintainable code. This function should only be used when that function
2825 can't be used (like there's no target or the target is defined in another
2826 build file).
2827```
2828
2829#### **Arguments**
2830
2831```
2832 The source_list is a list of file names.
2833
2834 The template can be a string or a list. If it is a list, multiple output
2835 strings are generated for each input.
2836
2837 The template should contain source expansions to which each name in the
2838 source list is applied. See "gn help source_expansion".
2839```
2840
2841#### **Example**
2842
2843```
2844 sources = [
2845 "foo.idl",
2846 "bar.idl",
2847 ]
2848 myoutputs = process_file_template(
2849 sources,
2850 [ "$target_gen_dir/{{source_name_part}}.cc",
2851 "$target_gen_dir/{{source_name_part}}.h" ])
2852
2853 The result in this case will be:
2854 [ "//out/Debug/foo.cc"
2855 "//out/Debug/foo.h"
2856 "//out/Debug/bar.cc"
2857 "//out/Debug/bar.h" ]
2858```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002859### **read_file**: Read a file into a variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002860
2861```
2862 read_file(filename, input_conversion)
2863
2864 Whitespace will be trimmed from the end of the file. Throws an error if the
2865 file can not be opened.
2866```
2867
2868#### **Arguments**
2869
2870```
2871 filename
2872 Filename to read, relative to the build file.
2873
2874 input_conversion
Julie Hockett81ee1962019-05-13 11:24:42 -07002875 Controls how the file is read and parsed. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07002876```
2877
2878#### **Example**
2879
2880```
2881 lines = read_file("foo.txt", "list lines")
2882```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002883### **rebase_path**: Rebase a file or directory to another location.
Brett Wilson796ed472018-07-16 15:11:09 -07002884
2885```
2886 converted = rebase_path(input,
2887 new_base = "",
2888 current_base = ".")
2889
2890 Takes a string argument representing a file name, or a list of such strings
2891 and converts it/them to be relative to a different base directory.
2892
2893 When invoking the compiler or scripts, GN will automatically convert sources
2894 and include directories to be relative to the build directory. However, if
2895 you're passing files directly in the "args" array or doing other manual
2896 manipulations where GN doesn't know something is a file name, you will need
2897 to convert paths to be relative to what your tool is expecting.
2898
2899 The common case is to use this to convert paths relative to the current
2900 directory to be relative to the build directory (which will be the current
2901 directory when executing scripts).
2902
2903 If you want to convert a file path to be source-absolute (that is, beginning
2904 with a double slash like "//foo/bar"), you should use the get_path_info()
2905 function. This function won't work because it will always make relative
2906 paths, and it needs to support making paths relative to the source root, so
Robert Sesekd0a6f072020-05-15 11:21:22 -04002907 it can't also generate source-absolute paths without more special-cases.
Brett Wilson796ed472018-07-16 15:11:09 -07002908```
2909
2910#### **Arguments**
2911
2912```
2913 input
Robert Sesekd0a6f072020-05-15 11:21:22 -04002914 A string or list of strings representing file or directory names. These
Brett Wilson796ed472018-07-16 15:11:09 -07002915 can be relative paths ("foo/bar.txt"), system absolute paths
2916 ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt").
2917
2918 new_base
2919 The directory to convert the paths to be relative to. This can be an
2920 absolute path or a relative path (which will be treated as being relative
2921 to the current BUILD-file's directory).
2922
2923 As a special case, if new_base is the empty string (the default), all
2924 paths will be converted to system-absolute native style paths with system
2925 path separators. This is useful for invoking external programs.
2926
2927 current_base
2928 Directory representing the base for relative paths in the input. If this
2929 is not an absolute path, it will be treated as being relative to the
2930 current build file. Use "." (the default) to convert paths from the
2931 current BUILD-file's directory.
2932```
2933
2934#### **Return value**
2935
2936```
2937 The return value will be the same type as the input value (either a string or
2938 a list of strings). All relative and source-absolute file names will be
2939 converted to be relative to the requested output System-absolute paths will
2940 be unchanged.
2941
2942 Whether an output path will end in a slash will match whether the
2943 corresponding input path ends in a slash. It will return "." or "./"
2944 (depending on whether the input ends in a slash) to avoid returning empty
2945 strings. This means if you want a root path ("//" or "/") not ending in a
2946 slash, you can add a dot ("//.").
2947```
2948
2949#### **Example**
2950
2951```
2952 # Convert a file in the current directory to be relative to the build
2953 # directory (the current dir when executing compilers and scripts).
2954 foo = rebase_path("myfile.txt", root_build_dir)
2955 # might produce "../../project/myfile.txt".
2956
2957 # Convert a file to be system absolute:
2958 foo = rebase_path("myfile.txt")
2959 # Might produce "D:\\source\\project\\myfile.txt" on Windows or
2960 # "/home/you/source/project/myfile.txt" on Linux.
2961
2962 # Typical usage for converting to the build directory for a script.
2963 action("myscript") {
2964 # Don't convert sources, GN will automatically convert these to be relative
2965 # to the build directory when it constructs the command line for your
2966 # script.
2967 sources = [ "foo.txt", "bar.txt" ]
2968
2969 # Extra file args passed manually need to be explicitly converted
2970 # to be relative to the build directory:
2971 args = [
2972 "--data",
2973 rebase_path("//mything/data/input.dat", root_build_dir),
2974 "--rel",
2975 rebase_path("relative_path.txt", root_build_dir)
2976 ] + rebase_path(sources, root_build_dir)
2977 }
2978```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002979### **set_default_toolchain**: Sets the default toolchain name.
Brett Wilson796ed472018-07-16 15:11:09 -07002980
2981```
2982 set_default_toolchain(toolchain_label)
2983
2984 The given label should identify a toolchain definition (see "gn help
2985 toolchain"). This toolchain will be used for all targets unless otherwise
2986 specified.
2987
2988 This function is only valid to call during the processing of the build
2989 configuration file. Since the build configuration file is processed
2990 separately for each toolchain, this function will be a no-op when called
2991 under any non-default toolchains.
2992
2993 For example, the default toolchain should be appropriate for the current
2994 environment. If the current environment is 32-bit and somebody references a
2995 target with a 64-bit toolchain, we wouldn't want processing of the build
2996 config file for the 64-bit toolchain to reset the default toolchain to
2997 64-bit, we want to keep it 32-bits.
2998```
2999
3000#### **Argument**
3001
3002```
3003 toolchain_label
3004 Toolchain name.
3005```
3006
3007#### **Example**
3008
3009```
3010 # Set default toolchain only has an effect when run in the context of the
3011 # default toolchain. Pick the right one according to the current CPU
3012 # architecture.
3013 if (target_cpu == "x64") {
3014 set_default_toolchain("//toolchains:64")
3015 } else if (target_cpu == "x86") {
3016 set_default_toolchain("//toolchains:32")
3017 }
3018```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003019### **set_defaults**: Set default values for a target type.
Brett Wilson796ed472018-07-16 15:11:09 -07003020
3021```
3022 set_defaults() { }
3023
3024 Sets the default values for a given target type. Whenever target_type_name is
3025 seen in the future, the values specified in set_default's block will be
3026 copied into the current scope.
3027
3028 When the target type is used, the variable copying is very strict. If a
3029 variable with that name is already in scope, the build will fail with an
3030 error.
3031
3032 set_defaults can be used for built-in target types ("executable",
3033 "shared_library", etc.) and custom ones defined via the "template" command.
3034 It can be called more than once and the most recent call in any scope will
3035 apply, but there is no way to refer to the previous defaults and modify them
3036 (each call to set_defaults must supply a complete list of all defaults it
3037 wants). If you want to share defaults, store them in a separate variable.
3038```
3039
3040#### **Example**
3041
3042```
3043 set_defaults("static_library") {
3044 configs = [ "//tools/mything:settings" ]
3045 }
3046
Nico Webere49cb722018-08-28 13:10:29 -04003047 static_library("mylib") {
Brett Wilson796ed472018-07-16 15:11:09 -07003048 # The configs will be auto-populated as above. You can remove it if
3049 # you don't want the default for a particular default:
3050 configs -= [ "//tools/mything:settings" ]
3051 }
3052```
Sylvain Defresnead0a7ad2020-10-15 15:03:44 +02003053### **set_sources_assignment_filter**: Deprecated feature.
Brett Wilson796ed472018-07-16 15:11:09 -07003054
3055```
Sylvain Defresnead0a7ad2020-10-15 15:03:44 +02003056 This feature is deprecated. It will be removed once all usages have been
3057 removed. Only supports a single argument that needs to be an empty list.
Brett Wilson796ed472018-07-16 15:11:09 -07003058```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003059### **split_list**: Splits a list into N different sub-lists.
Brett Wilson796ed472018-07-16 15:11:09 -07003060
3061```
3062 result = split_list(input, n)
3063
3064 Given a list and a number N, splits the list into N sub-lists of
3065 approximately equal size. The return value is a list of the sub-lists. The
3066 result will always be a list of size N. If N is greater than the number of
3067 elements in the input, it will be padded with empty lists.
3068
3069 The expected use is to divide source files into smaller uniform chunks.
3070```
3071
3072#### **Example**
3073
3074```
3075 The code:
3076 mylist = [1, 2, 3, 4, 5, 6]
3077 print(split_list(mylist, 3))
3078
3079 Will print:
3080 [[1, 2], [3, 4], [5, 6]
3081```
Keir Mierle6ae63302019-11-08 23:02:18 -08003082### **string_join**: Concatenates a list of strings with a separator.
3083
3084```
3085 result = string_join(separator, strings)
3086
3087 Concatenate a list of strings with intervening occurrences of separator.
3088```
3089
3090#### **Examples**
3091
3092```
3093 string_join("", ["a", "b", "c"]) --> "abc"
3094 string_join("|", ["a", "b", "c"]) --> "a|b|c"
3095 string_join(", ", ["a", "b", "c"]) --> "a, b, c"
3096 string_join("s", ["", ""]) --> "s"
3097```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003098### **string_replace**: Replaces substring in the given string.
Petr Hosekc0744ed2018-08-04 16:13:59 -07003099
3100```
3101 result = string_replace(str, old, new[, max])
3102
3103 Returns a copy of the string str in which the occurrences of old have been
3104 replaced with new, optionally restricting the number of replacements. The
3105 replacement is performed sequentially, so if new contains old, it won't be
3106 replaced.
3107```
3108
3109#### **Example**
3110
3111```
3112 The code:
3113 mystr = "Hello, world!"
3114 print(string_replace(mystr, "world", "GN"))
3115
3116 Will print:
3117 Hello, GN!
3118```
Keir Mierle6ae63302019-11-08 23:02:18 -08003119### **string_split**: Split string into a list of strings.
3120
3121```
3122 result = string_split(str[, sep])
3123
3124 Split string into all substrings separated by separator and returns a list
3125 of the substrings between those separators.
3126
3127 If the separator argument is omitted, the split is by any whitespace, and
3128 any leading/trailing whitespace is ignored; similar to Python's str.split().
3129```
3130
3131#### **Examples without a separator (split on whitespace)**:
3132
3133```
3134 string_split("") --> []
3135 string_split("a") --> ["a"]
3136 string_split(" aa bb") --> ["aa", "bb"]
3137```
3138
3139#### **Examples with a separator (split on separators)**:
3140
3141```
3142 string_split("", "|") --> [""]
3143 string_split(" a b ", " ") --> ["", "", "a", "b", "", ""]
3144 string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"]
3145```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003146### **template**: Define a template rule.
Brett Wilson796ed472018-07-16 15:11:09 -07003147
3148```
3149 A template defines a custom name that acts like a function. It provides a way
3150 to add to the built-in target types.
3151
3152 The template() function is used to declare a template. To invoke the
3153 template, just use the name of the template like any other target type.
3154
3155 Often you will want to declare your template in a special file that other
3156 files will import (see "gn help import") so your template rule can be shared
3157 across build files.
3158```
3159
3160#### **Variables and templates**:
3161
3162```
3163 When you call template() it creates a closure around all variables currently
3164 in scope with the code in the template block. When the template is invoked,
3165 the closure will be executed.
3166
3167 When the template is invoked, the code in the caller is executed and passed
3168 to the template code as an implicit "invoker" variable. The template uses
3169 this to read state out of the invoking code.
3170
3171 One thing explicitly excluded from the closure is the "current directory"
3172 against which relative file names are resolved. The current directory will be
3173 that of the invoking code, since typically that code specifies the file
3174 names. This means all files internal to the template should use absolute
3175 names.
3176
3177 A template will typically forward some or all variables from the invoking
3178 scope to a target that it defines. Often, such variables might be optional.
3179 Use the pattern:
3180
3181 if (defined(invoker.deps)) {
3182 deps = invoker.deps
3183 }
3184
3185 The function forward_variables_from() provides a shortcut to forward one or
3186 more or possibly all variables in this manner:
3187
3188 forward_variables_from(invoker, ["deps", "public_deps"])
3189```
3190
3191#### **Target naming**
3192
3193```
3194 Your template should almost always define a built-in target with the name the
3195 template invoker specified. For example, if you have an IDL template and
3196 somebody does:
3197 idl("foo") {...
3198 you will normally want this to expand to something defining a source_set or
3199 static_library named "foo" (among other things you may need). This way, when
3200 another target specifies a dependency on "foo", the static_library or
3201 source_set will be linked.
3202
3203 It is also important that any other targets your template expands to have
3204 unique names, or you will get collisions.
3205
3206 Access the invoking name in your template via the implicit "target_name"
3207 variable. This should also be the basis for how other targets that a template
3208 expands to ensure uniqueness.
3209
3210 A typical example would be a template that defines an action to generate some
3211 source files, and a source_set to compile that source. Your template would
3212 name the source_set "target_name" because that's what you want external
3213 targets to depend on to link your code. And you would name the action
3214 something like "${target_name}_action" to make it unique. The source set
3215 would have a dependency on the action to make it run.
3216```
3217
3218#### **Overriding builtin targets**
3219
3220```
3221 You can use template to redefine a built-in target in which case your template
3222 takes a precedence over the built-in one. All uses of the target from within
3223 the template definition will refer to the built-in target which makes it
3224 possible to extend the behavior of the built-in target:
3225
3226 template("shared_library") {
3227 shared_library(shlib) {
Julie Hockette2a29402018-07-31 10:11:42 -07003228 forward_variables_from(invoker, "*")
Brett Wilson796ed472018-07-16 15:11:09 -07003229 ...
3230 }
3231 }
3232```
3233
3234#### **Example of defining a template**
3235
3236```
3237 template("my_idl") {
3238 # Be nice and help callers debug problems by checking that the variables
3239 # the template requires are defined. This gives a nice message rather than
3240 # giving the user an error about an undefined variable in the file defining
3241 # the template
3242 #
3243 # You can also use defined() to give default values to variables
3244 # unspecified by the invoker.
3245 assert(defined(invoker.sources),
3246 "Need sources in $target_name listing the idl files.")
3247
3248 # Name of the intermediate target that does the code gen. This must
3249 # incorporate the target name so it's unique across template
3250 # instantiations.
3251 code_gen_target_name = target_name + "_code_gen"
3252
3253 # Intermediate target to convert IDL to C source. Note that the name is
3254 # based on the name the invoker of the template specified. This way, each
3255 # time the template is invoked we get a unique intermediate action name
3256 # (since all target names are in the global scope).
3257 action_foreach(code_gen_target_name) {
3258 # Access the scope defined by the invoker via the implicit "invoker"
3259 # variable.
3260 sources = invoker.sources
3261
3262 # Note that we need an absolute path for our script file name. The
3263 # current directory when executing this code will be that of the invoker
3264 # (this is why we can use the "sources" directly above without having to
3265 # rebase all of the paths). But if we need to reference a script relative
3266 # to the template file, we'll need to use an absolute path instead.
3267 script = "//tools/idl/idl_code_generator.py"
3268
3269 # Tell GN how to expand output names given the sources.
3270 # See "gn help source_expansion" for more.
3271 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
3272 "$target_gen_dir/{{source_name_part}}.h" ]
3273 }
3274
3275 # Name the source set the same as the template invocation so instancing
3276 # this template produces something that other targets can link to in their
3277 # deps.
3278 source_set(target_name) {
3279 # Generates the list of sources, we get these from the action_foreach
3280 # above.
3281 sources = get_target_outputs(":$code_gen_target_name")
3282
3283 # This target depends on the files produced by the above code gen target.
3284 deps = [ ":$code_gen_target_name" ]
3285 }
3286 }
3287```
3288
3289#### **Example of invoking the resulting template**
3290
3291```
3292 # This calls the template code above, defining target_name to be
3293 # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly
3294 # brackets.
3295 my_idl("foo_idl_files") {
3296 # Goes into the template as "invoker.sources".
3297 sources = [ "foo.idl", "bar.idl" ]
3298 }
3299
3300 # Here is a target that depends on our template.
3301 executable("my_exe") {
3302 # Depend on the name we gave the template call above. Internally, this will
3303 # produce a dependency from executable to the source_set inside the
3304 # template (since it has this name), which will in turn depend on the code
3305 # gen action.
3306 deps = [ ":foo_idl_files" ]
3307 }
3308```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003309### **tool**: Specify arguments to a toolchain tool.
Brett Wilson796ed472018-07-16 15:11:09 -07003310
3311#### **Usage**
3312
3313```
3314 tool() {
3315
3316 }
3317```
3318
3319#### **Tool types**
3320
3321```
3322 Compiler tools:
3323 "cc": C compiler
3324 "cxx": C++ compiler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003325 "cxx_module": C++ compiler used for Clang .modulemap files
Brett Wilson796ed472018-07-16 15:11:09 -07003326 "objc": Objective C compiler
3327 "objcxx": Objective C++ compiler
3328 "rc": Resource compiler (Windows .rc files)
3329 "asm": Assembler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003330 "swift": Swift compiler driver
Brett Wilson796ed472018-07-16 15:11:09 -07003331
3332 Linker tools:
3333 "alink": Linker for static libraries (archives)
3334 "solink": Linker for shared libraries
3335 "link": Linker for executables
3336
3337 Other tools:
3338 "stamp": Tool for creating stamp files
3339 "copy": Tool to copy files.
3340 "action": Defaults for actions
3341
3342 Platform specific tools:
3343 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
3344 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
Julie Hockettce1fa072019-05-07 17:44:37 -07003345
3346 Rust tools:
Petr Hosek7617b592019-10-02 09:52:21 -07003347 "rust_bin": Tool for compiling Rust binaries
3348 "rust_cdylib": Tool for compiling C-compatible dynamic libraries.
3349 "rust_dylib": Tool for compiling Rust dynamic libraries.
3350 "rust_macro": Tool for compiling Rust procedural macros.
3351 "rust_rlib": Tool for compiling Rust libraries.
3352 "rust_staticlib": Tool for compiling Rust static libraries.
Brett Wilson796ed472018-07-16 15:11:09 -07003353```
3354
3355#### **Tool variables**
3356
3357```
3358 command [string with substitutions]
3359 Valid for: all tools except "action" (required)
3360
3361 The command to run.
3362
Joe Armstrong23d22bc2019-06-28 15:31:03 +08003363 command_launcher [string]
3364 Valid for: all tools except "action" (optional)
3365
3366 The prefix with which to launch the command (e.g. the path to a Goma or
3367 CCache compiler launcher).
3368
3369 Note that this prefix will not be included in the compilation database or
3370 IDE files generated from the build.
3371
Brett Wilson796ed472018-07-16 15:11:09 -07003372 default_output_dir [string with substitutions]
3373 Valid for: linker tools
3374
3375 Default directory name for the output file relative to the
3376 root_build_dir. It can contain other substitution patterns. This will
3377 be the default value for the {{output_dir}} expansion (discussed below)
3378 but will be overridden by the "output_dir" variable in a target, if one
3379 is specified.
3380
3381 GN doesn't do anything with this string other than pass it along,
3382 potentially with target-specific overrides. It is the tool's job to use
3383 the expansion so that the files will be in the right place.
3384
3385 default_output_extension [string]
3386 Valid for: linker tools
3387
3388 Extension for the main output of a linkable tool. It includes the
3389 leading dot. This will be the default value for the
3390 {{output_extension}} expansion (discussed below) but will be overridden
3391 by by the "output extension" variable in a target, if one is specified.
3392 Empty string means no extension.
3393
3394 GN doesn't actually do anything with this extension other than pass it
3395 along, potentially with target-specific overrides. One would typically
3396 use the {{output_extension}} value in the "outputs" to read this value.
3397
3398 Example: default_output_extension = ".exe"
3399
3400 depfile [string with substitutions]
3401 Valid for: compiler tools (optional)
3402
3403 If the tool can write ".d" files, this specifies the name of the
3404 resulting file. These files are used to list header file dependencies
3405 (or other implicit input dependencies) that are discovered at build
3406 time. See also "depsformat".
3407
3408 Example: depfile = "{{output}}.d"
3409
3410 depsformat [string]
3411 Valid for: compiler tools (when depfile is specified)
3412
3413 Format for the deps outputs. This is either "gcc" or "msvc". See the
3414 ninja documentation for "deps" for more information.
3415
3416 Example: depsformat = "gcc"
3417
3418 description [string with substitutions, optional]
3419 Valid for: all tools
3420
3421 What to print when the command is run.
3422
3423 Example: description = "Compiling {{source}}"
3424
Julie Hockettce1fa072019-05-07 17:44:37 -07003425 exe_output_extension [string, optional, rust tools only]
3426 rlib_output_extension [string, optional, rust tools only]
3427 dylib_output_extension [string, optional, rust tools only]
3428 cdylib_output_extension [string, optional, rust tools only]
Petr Hosekfe36c7c2019-11-26 14:38:08 -08003429 rust_proc_macro_output_extension [string, optional, rust tools only]
Julie Hockettce1fa072019-05-07 17:44:37 -07003430 Valid for: Rust tools
3431
3432 These specify the default tool output for each of the crate types.
3433 The default is empty for executables, shared, and static libraries and
3434 ".rlib" for rlibs. Note that the Rust compiler complains with an error
3435 if external crates do not take the form `lib.rlib` or
3436 `lib.`, where `` is `.so`,
3437 `.dylib`, or `.dll` as appropriate for the platform.
3438
Brett Wilson796ed472018-07-16 15:11:09 -07003439 lib_switch [string, optional, link tools only]
3440 lib_dir_switch [string, optional, link tools only]
3441 Valid for: Linker tools except "alink"
3442
3443 These strings will be prepended to the libraries and library search
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003444 directories, respectively, because linkers differ on how to specify
3445 them.
3446
Brett Wilson796ed472018-07-16 15:11:09 -07003447 If you specified:
3448 lib_switch = "-l"
3449 lib_dir_switch = "-L"
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003450 then the "{{libs}}" expansion for
3451 [ "freetype", "expat" ]
3452 would be
3453 "-lfreetype -lexpat".
3454
3455 framework_switch [string, optional, link tools only]
Robert Sesekd0a6f072020-05-15 11:21:22 -04003456 weak_framework_switch [string, optional, link tools only]
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003457 framework_dir_switch [string, optional, link tools only]
3458 Valid for: Linker tools
3459
3460 These strings will be prepended to the frameworks and framework search
3461 path directories, respectively, because linkers differ on how to specify
3462 them.
3463
3464 If you specified:
3465 framework_switch = "-framework "
Robert Sesekd0a6f072020-05-15 11:21:22 -04003466 weak_framework_switch = "-weak_framework "
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003467 framework_dir_switch = "-F"
Robert Sesekd0a6f072020-05-15 11:21:22 -04003468 and:
3469 framework_dirs = [ "$root_out_dir" ]
3470 frameworks = [ "UIKit.framework", "Foo.framework" ]
3471 weak_frameworks = [ "MediaPlayer.framework" ]
3472 would be:
3473 "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer"
Brett Wilson796ed472018-07-16 15:11:09 -07003474
Sylvain Defresne89e64252020-08-07 13:01:06 +02003475 swiftmodule_switch [string, optional, link tools only]
3476 Valid for: Linker tools except "alink"
3477
3478 The string will be prependend to the path to the .swiftmodule files
3479 that are embedded in the linker output.
3480
3481 If you specified:
3482 swiftmodule_swift = "-Wl,-add_ast_path,"
3483 then the "{{swiftmodules}}" expansion for
3484 [ "obj/foo/Foo.swiftmodule" ]
3485 would be
3486 "-Wl,-add_ast_path,obj/foo/Foo.swiftmodule"
3487
Brett Wilson796ed472018-07-16 15:11:09 -07003488 outputs [list of strings with substitutions]
3489 Valid for: Linker and compiler tools (required)
3490
3491 An array of names for the output files the tool produces. These are
3492 relative to the build output directory. There must always be at least
3493 one output file. There can be more than one output (a linker might
3494 produce a library and an import library, for example).
3495
3496 This array just declares to GN what files the tool will produce. It is
3497 your responsibility to specify the tool command that actually produces
3498 these files.
3499
3500 If you specify more than one output for shared library links, you
3501 should consider setting link_output, depend_output, and
3502 runtime_outputs.
3503
3504 Example for a compiler tool that produces .obj files:
3505 outputs = [
3506 "{{source_out_dir}}/{{source_name_part}}.obj"
3507 ]
3508
3509 Example for a linker tool that produces a .dll and a .lib. The use of
3510 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
3511 the target to override these values.
3512 outputs = [
Nico Weberad9eab22020-11-15 22:20:28 -05003513 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Brett Wilson796ed472018-07-16 15:11:09 -07003514 "{{output_dir}}/{{target_output_name}}.lib",
3515 ]
3516
Sylvain Defresne89e64252020-08-07 13:01:06 +02003517 partial_outputs [list of strings with substitutions]
3518 Valid for: "swift" only
3519
3520 An array of names for the partial outputs the tool produces. These
3521 are relative to the build output directory. The expansion will be
3522 evaluated for each file listed in the "sources" of the target.
3523
3524 This is used to deal with whole module optimization, allowing to
3525 list one object file per source file when whole module optimization
3526 is disabled.
3527
Brett Wilson796ed472018-07-16 15:11:09 -07003528 pool [label, optional]
3529 Valid for: all tools (optional)
3530
3531 Label of the pool to use for the tool. Pools are used to limit the
3532 number of tasks that can execute concurrently during the build.
3533
3534 See also "gn help pool".
3535
3536 link_output [string with substitutions]
3537 depend_output [string with substitutions]
3538 Valid for: "solink" only (optional)
3539
3540 These two files specify which of the outputs from the solink tool
3541 should be used for linking and dependency tracking. These should match
3542 entries in the "outputs". If unspecified, the first item in the
3543 "outputs" array will be used for all. See "Separate linking and
3544 dependencies for shared libraries" below for more.
3545
3546 On Windows, where the tools produce a .dll shared library and a .lib
3547 import library, you will want the first two to be the import library
3548 and the third one to be the .dll file. On Linux, if you're not doing
3549 the separate linking/dependency optimization, all of these should be
3550 the .so output.
3551
3552 output_prefix [string]
3553 Valid for: Linker tools (optional)
3554
3555 Prefix to use for the output name. Defaults to empty. This prefix will
3556 be prepended to the name of the target (or the output_name if one is
3557 manually specified for it) if the prefix is not already there. The
3558 result will show up in the {{output_name}} substitution pattern.
3559
3560 Individual targets can opt-out of the output prefix by setting:
3561 output_prefix_override = true
3562 (see "gn help output_prefix_override").
3563
3564 This is typically used to prepend "lib" to libraries on
3565 Posix systems:
3566 output_prefix = "lib"
3567
3568 precompiled_header_type [string]
3569 Valid for: "cc", "cxx", "objc", "objcxx"
3570
3571 Type of precompiled headers. If undefined or the empty string,
3572 precompiled headers will not be used for this tool. Otherwise use "gcc"
3573 or "msvc".
3574
3575 For precompiled headers to be used for a given target, the target (or a
3576 config applied to it) must also specify a "precompiled_header" and, for
3577 "msvc"-style headers, a "precompiled_source" value. If the type is
3578 "gcc", then both "precompiled_header" and "precompiled_source" must
3579 resolve to the same file, despite the different formats required for
3580 each."
3581
3582 See "gn help precompiled_header" for more.
3583
3584 restat [boolean]
3585 Valid for: all tools (optional, defaults to false)
3586
3587 Requests that Ninja check the file timestamp after this tool has run to
3588 determine if anything changed. Set this if your tool has the ability to
3589 skip writing output if the output file has not changed.
3590
3591 Normally, Ninja will assume that when a tool runs the output be new and
3592 downstream dependents must be rebuild. When this is set to trye, Ninja
3593 can skip rebuilding downstream dependents for input changes that don't
3594 actually affect the output.
3595
3596 Example:
3597 restat = true
3598
3599 rspfile [string with substitutions]
3600 Valid for: all tools except "action" (optional)
3601
3602 Name of the response file. If empty, no response file will be
3603 used. See "rspfile_content".
3604
3605 rspfile_content [string with substitutions]
3606 Valid for: all tools except "action" (required when "rspfile" is used)
3607
3608 The contents to be written to the response file. This may include all
3609 or part of the command to send to the tool which allows you to get
3610 around OS command-line length limits.
3611
3612 This example adds the inputs and libraries to a response file, but
3613 passes the linker flags directly on the command line:
3614 tool("link") {
3615 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
3616 rspfile = "{{output}}.rsp"
Adrian Taylorac6cc952020-04-26 20:49:18 -07003617 rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}"
Brett Wilson796ed472018-07-16 15:11:09 -07003618 }
3619
3620 runtime_outputs [string list with substitutions]
3621 Valid for: linker tools
3622
3623 If specified, this list is the subset of the outputs that should be
3624 added to runtime deps (see "gn help runtime_deps"). By default (if
3625 runtime_outputs is empty or unspecified), it will be the link_output.
3626```
3627
3628#### **Expansions for tool variables**
3629
3630```
3631 All paths are relative to the root build directory, which is the current
3632 directory for running all tools. These expansions are available to all tools:
3633
3634 {{label}}
3635 The label of the current target. This is typically used in the
3636 "description" field for link tools. The toolchain will be omitted from
3637 the label for targets in the default toolchain, and will be included
3638 for targets in other toolchains.
3639
3640 {{label_name}}
3641 The short name of the label of the target. This is the part after the
3642 colon. For "//foo/bar:baz" this will be "baz". Unlike
3643 {{target_output_name}}, this is not affected by the "output_prefix" in
3644 the tool or the "output_name" set on the target.
3645
Scott Graham501b49a2020-08-05 13:03:37 -07003646 {{label_no_toolchain}}
3647 The label of the current target, never including the toolchain
3648 (otherwise, this is identical to {{label}}). This is used as the module
3649 name when using .modulemap files.
3650
Brett Wilson796ed472018-07-16 15:11:09 -07003651 {{output}}
3652 The relative path and name of the output(s) of the current build step.
3653 If there is more than one output, this will expand to a list of all of
3654 them. Example: "out/base/my_file.o"
3655
3656 {{target_gen_dir}}
3657 {{target_out_dir}}
3658 The directory of the generated file and output directories,
3659 respectively, for the current target. There is no trailing slash. See
3660 also {{output_dir}} for linker tools. Example: "out/base/test"
3661
3662 {{target_output_name}}
3663 The short name of the current target with no path information, or the
3664 value of the "output_name" variable if one is specified in the target.
3665 This will include the "output_prefix" if any. See also {{label_name}}.
3666
3667 Example: "libfoo" for the target named "foo" and an output prefix for
3668 the linker tool of "lib".
3669
3670 Compiler tools have the notion of a single input and a single output, along
3671 with a set of compiler-specific flags. The following expansions are
3672 available:
3673
3674 {{asmflags}}
3675 {{cflags}}
3676 {{cflags_c}}
3677 {{cflags_cc}}
3678 {{cflags_objc}}
3679 {{cflags_objcc}}
3680 {{defines}}
3681 {{include_dirs}}
3682 Strings correspond that to the processed flags/defines/include
3683 directories specified for the target.
3684 Example: "--enable-foo --enable-bar"
3685
3686 Defines will be prefixed by "-D" and include directories will be
3687 prefixed by "-I" (these work with Posix tools as well as Microsoft
3688 ones).
3689
Scott Graham484245b2020-08-11 10:58:50 -07003690 {{module_deps}}
3691 {{module_deps_no_self}}
3692 Strings that correspond to the flags necessary to depend upon the Clang
3693 modules referenced by the current target. The "_no_self" version doesn't
3694 include the module for the current target, and can be used to compile
3695 the pcm itself.
3696
Brett Wilson796ed472018-07-16 15:11:09 -07003697 {{source}}
3698 The relative path and name of the current input file.
3699 Example: "../../base/my_file.cc"
3700
3701 {{source_file_part}}
3702 The file part of the source including the extension (with no directory
3703 information).
3704 Example: "foo.cc"
3705
3706 {{source_name_part}}
3707 The filename part of the source file with no directory or extension.
3708 Example: "foo"
3709
3710 {{source_gen_dir}}
3711 {{source_out_dir}}
3712 The directory in the generated file and output directories,
3713 respectively, for the current input file. If the source file is in the
3714 same directory as the target is declared in, they will will be the same
3715 as the "target" versions above. Example: "gen/base/test"
3716
Julie Hockette2a29402018-07-31 10:11:42 -07003717 Linker tools have multiple inputs and (potentially) multiple outputs. The
Brett Wilson796ed472018-07-16 15:11:09 -07003718 static library tool ("alink") is not considered a linker tool. The following
3719 expansions are available:
3720
3721 {{inputs}}
3722 {{inputs_newline}}
3723 Expands to the inputs to the link step. This will be a list of object
3724 files and static libraries.
3725 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
3726
3727 The "_newline" version will separate the input files with newlines
3728 instead of spaces. This is useful in response files: some linkers can
3729 take a "-filelist" flag which expects newline separated files, and some
3730 Microsoft tools have a fixed-sized buffer for parsing each line of a
3731 response file.
3732
3733 {{ldflags}}
3734 Expands to the processed set of ldflags and library search paths
3735 specified for the target.
3736 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
3737
3738 {{libs}}
3739 Expands to the list of system libraries to link to. Each will be
3740 prefixed by the "lib_switch".
3741
Brett Wilson796ed472018-07-16 15:11:09 -07003742 Example: "-lfoo -lbar"
3743
3744 {{output_dir}}
3745 The value of the "output_dir" variable in the target, or the the value
3746 of the "default_output_dir" value in the tool if the target does not
3747 override the output directory. This will be relative to the
3748 root_build_dir and will not end in a slash. Will be "." for output to
3749 the root_build_dir.
3750
3751 This is subtly different than {{target_out_dir}} which is defined by GN
3752 based on the target's path and not overridable. {{output_dir}} is for
3753 the final output, {{target_out_dir}} is generally for object files and
3754 other outputs.
3755
3756 Usually {{output_dir}} would be defined in terms of either
3757 {{target_out_dir}} or {{root_out_dir}}
3758
3759 {{output_extension}}
3760 The value of the "output_extension" variable in the target, or the
3761 value of the "default_output_extension" value in the tool if the target
3762 does not specify an output extension.
3763 Example: ".so"
3764
3765 {{solibs}}
3766 Extra libraries from shared library dependencies not specified in the
3767 {{inputs}}. This is the list of link_output files from shared libraries
3768 (if the solink tool specifies a "link_output" variable separate from
3769 the "depend_output").
3770
3771 These should generally be treated the same as libs by your tool.
3772
3773 Example: "libfoo.so libbar.so"
3774
Adrian Taylorac6cc952020-04-26 20:49:18 -07003775 {{rlibs}}
3776 Any Rust .rlibs which need to be linked into a final C++ target.
3777 These should be treated as {{inputs}} except that sometimes
3778 they might have different linker directives applied.
3779
3780 Example: "obj/foo/libfoo.rlib"
3781
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003782 {{frameworks}}
3783 Shared libraries packaged as framework bundle. This is principally
3784 used on Apple's platforms (macOS and iOS). All name must be ending
3785 with ".framework" suffix; the suffix will be stripped when expanding
Robert Sesekd0a6f072020-05-15 11:21:22 -04003786 {{frameworks}} and each item will be preceded by "-framework" or
3787 "-weak_framework".
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003788
Sylvain Defresne89e64252020-08-07 13:01:06 +02003789 {{swiftmodules}}
3790 Swift .swiftmodule files that needs to be embedded into the binary.
3791 This is necessary to correctly link with object generated by the
3792 Swift compiler (the .swiftmodule file cannot be embedded in object
3793 files directly). Those will be prefixed with "swiftmodule_switch"
3794 value.
3795
Brett Wilson796ed472018-07-16 15:11:09 -07003796 The static library ("alink") tool allows {{arflags}} plus the common tool
3797 substitutions.
3798
3799 The copy tool allows the common compiler/linker substitutions, plus
3800 {{source}} which is the source of the copy. The stamp tool allows only the
3801 common tool substitutions.
3802
3803 The copy_bundle_data and compile_xcassets tools only allows the common tool
3804 substitutions. Both tools are required to create iOS/macOS bundles and need
3805 only be defined on those platforms.
3806
3807 The copy_bundle_data tool will be called with one source and needs to copy
3808 (optionally optimizing the data representation) to its output. It may be
3809 called with a directory as input and it needs to be recursively copied.
3810
3811 The compile_xcassets tool will be called with one or more source (each an
3812 asset catalog) that needs to be compiled to a single output. The following
3813 substitutions are available:
3814
3815 {{inputs}}
3816 Expands to the list of .xcassets to use as input to compile the asset
3817 catalog.
3818
3819 {{bundle_product_type}}
3820 Expands to the product_type of the bundle that will contain the
3821 compiled asset catalog. Usually corresponds to the product_type
3822 property of the corresponding create_bundle target.
3823
3824 {{bundle_partial_info_plist}}
3825 Expands to the path to the partial Info.plist generated by the
3826 assets catalog compiler. Usually based on the target_name of
3827 the create_bundle target.
Julie Hockettce1fa072019-05-07 17:44:37 -07003828
Harley Li0a9affb2020-06-03 10:38:42 -04003829 {{xcasset_compiler_flags}}
3830 Expands to the list of flags specified in corresponding
3831 create_bundle target.
3832
Sylvain Defresne89e64252020-08-07 13:01:06 +02003833 The Swift tool has multiple input and outputs. It must have exactly one
3834 output of .swiftmodule type, but can have one or more object file outputs,
3835 in addition to other type of ouputs. The following expansions are available:
3836
3837 {{module_name}}
3838 Expands to the string representing the module name of target under
3839 compilation (see "module_name" variable).
3840
3841 {{module_dirs}}
3842 Expands to the list of -I for the target Swift module search
3843 path computed from target dependencies.
3844
3845 {{swiftflags}}
3846 Expands to the list of strings representing Swift compiler flags.
3847
Julie Hockettce1fa072019-05-07 17:44:37 -07003848 Rust tools have the notion of a single input and a single output, along
3849 with a set of compiler-specific flags. The following expansions are
3850 available:
3851
3852 {{crate_name}}
3853 Expands to the string representing the crate name of target under
3854 compilation.
3855
3856 {{crate_type}}
3857 Expands to the string representing the type of crate for the target
3858 under compilation.
3859
3860 {{externs}}
3861 Expands to the list of --extern flags needed to include addition Rust
3862 libraries in this target. Includes any specified renamed dependencies.
3863
Julie Hockettce1fa072019-05-07 17:44:37 -07003864 {{rustdeps}}
3865 Expands to the list of -Ldependency= strings needed to compile
3866 this target.
3867
3868 {{rustenv}}
3869 Expands to the list of environment variables.
3870
3871 {{rustflags}}
3872 Expands to the list of strings representing Rust compiler flags.
Brett Wilson796ed472018-07-16 15:11:09 -07003873```
3874
3875#### **Separate linking and dependencies for shared libraries**
3876
3877```
3878 Shared libraries are special in that not all changes to them require that
3879 dependent targets be re-linked. If the shared library is changed but no
3880 imports or exports are different, dependent code needn't be relinked, which
3881 can speed up the build.
3882
3883 If your link step can output a list of exports from a shared library and
3884 writes the file only if the new one is different, the timestamp of this file
3885 can be used for triggering re-links, while the actual shared library would be
3886 used for linking.
3887
3888 You will need to specify
3889 restat = true
3890 in the linker tool to make this work, so Ninja will detect if the timestamp
3891 of the dependency file has changed after linking (otherwise it will always
3892 assume that running a command updates the output):
3893
3894 tool("solink") {
3895 command = "..."
3896 outputs = [
3897 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Nico Weberad9eab22020-11-15 22:20:28 -05003898 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC",
Brett Wilson796ed472018-07-16 15:11:09 -07003899 ]
3900 link_output =
3901 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
3902 depend_output =
Nico Weberad9eab22020-11-15 22:20:28 -05003903 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC"
Brett Wilson796ed472018-07-16 15:11:09 -07003904 restat = true
3905 }
3906```
3907
3908#### **Example**
3909
3910```
3911 toolchain("my_toolchain") {
3912 # Put these at the top to apply to all tools below.
3913 lib_switch = "-l"
3914 lib_dir_switch = "-L"
3915
3916 tool("cc") {
3917 command = "gcc {{source}} -o {{output}}"
3918 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
3919 description = "GCC {{source}}"
3920 }
3921 tool("cxx") {
3922 command = "g++ {{source}} -o {{output}}"
3923 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
3924 description = "G++ {{source}}"
3925 }
3926 };
3927```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003928### **toolchain**: Defines a toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07003929
3930```
3931 A toolchain is a set of commands and build flags used to compile the source
3932 code. The toolchain() function defines these commands.
3933```
3934
3935#### **Toolchain overview**
3936
3937```
3938 You can have more than one toolchain in use at once in a build and a target
3939 can exist simultaneously in multiple toolchains. A build file is executed
3940 once for each toolchain it is referenced in so the GN code can vary all
3941 parameters of each target (or which targets exist) on a per-toolchain basis.
3942
3943 When you have a simple build with only one toolchain, the build config file
3944 is loaded only once at the beginning of the build. It must call
3945 set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the
3946 label of the toolchain definition to use. The "toolchain_args" section of the
3947 toolchain definition is ignored.
3948
3949 When a target has a dependency on a target using different toolchain (see "gn
3950 help labels" for how to specify this), GN will start a build using that
3951 secondary toolchain to resolve the target. GN will load the build config file
3952 with the build arguments overridden as specified in the toolchain_args.
3953 Because the default toolchain is already known, calls to
3954 set_default_toolchain() are ignored.
3955
3956 To load a file in an alternate toolchain, GN does the following:
3957
3958 1. Loads the file with the toolchain definition in it (as determined by the
3959 toolchain label).
3960 2. Re-runs the master build configuration file, applying the arguments
3961 specified by the toolchain_args section of the toolchain definition.
3962 3. Loads the destination build file in the context of the configuration file
3963 in the previous step.
3964
3965 The toolchain configuration is two-way. In the default toolchain (i.e. the
3966 main build target) the configuration flows from the build config file to the
3967 toolchain. The build config file looks at the state of the build (OS type,
3968 CPU architecture, etc.) and decides which toolchain to use (via
3969 set_default_toolchain()). In secondary toolchains, the configuration flows
3970 from the toolchain to the build config file: the "toolchain_args" in the
3971 toolchain definition specifies the arguments to re-invoke the build.
3972```
3973
3974#### **Functions and variables**
3975
3976```
3977 tool()
Julie Hockette2a29402018-07-31 10:11:42 -07003978 The tool() function call specifies the commands to run for a given step. See
3979 "gn help tool".
Brett Wilson796ed472018-07-16 15:11:09 -07003980
Julie Hockette2a29402018-07-31 10:11:42 -07003981 toolchain_args [scope]
Brett Wilson796ed472018-07-16 15:11:09 -07003982 Overrides for build arguments to pass to the toolchain when invoking it.
3983 This is a variable of type "scope" where the variable names correspond to
3984 variables in declare_args() blocks.
3985
3986 When you specify a target using an alternate toolchain, the master build
3987 configuration file is re-interpreted in the context of that toolchain.
3988 toolchain_args allows you to control the arguments passed into this
3989 alternate invocation of the build.
3990
3991 Any default system arguments or arguments passed in via "gn args" will also
3992 be passed to the alternate invocation unless explicitly overridden by
3993 toolchain_args.
3994
3995 The toolchain_args will be ignored when the toolchain being defined is the
3996 default. In this case, it's expected you want the default argument values.
3997
3998 See also "gn help buildargs" for an overview of these arguments.
3999
Julie Hockette2a29402018-07-31 10:11:42 -07004000 propagates_configs [boolean, default=false]
4001 Determines whether public_configs and all_dependent_configs in this
4002 toolchain propagate to targets in other toolchains.
4003
4004 When false (the default), this toolchain will not propagate any configs to
4005 targets in other toolchains that depend on it targets inside this
4006 toolchain. This matches the most common usage of toolchains where they
4007 represent different architectures or compilers and the settings that apply
4008 to one won't necessarily apply to others.
4009
4010 When true, configs (public and all-dependent) will cross the boundary out
4011 of this toolchain as if the toolchain boundary wasn't there. This only
4012 affects one direction of dependencies: a toolchain can't control whether
4013 it accepts such configs, only whether it pushes them. The build is
4014 responsible for ensuring that any external targets depending on targets in
4015 this toolchain are compatible with the compiler flags, etc. that may be
4016 propagated.
4017
4018 deps [string list]
Brett Wilson796ed472018-07-16 15:11:09 -07004019 Dependencies of this toolchain. These dependencies will be resolved before
4020 any target in the toolchain is compiled. To avoid circular dependencies
4021 these must be targets defined in another toolchain.
4022
4023 This is expressed as a list of targets, and generally these targets will
4024 always specify a toolchain:
4025 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
4026
4027 This concept is somewhat inefficient to express in Ninja (it requires a lot
4028 of duplicate of rules) so should only be used when absolutely necessary.
4029```
4030
4031#### **Example of defining a toolchain**
4032
4033```
4034 toolchain("32") {
4035 tool("cc") {
4036 command = "gcc {{source}}"
4037 ...
4038 }
4039
4040 toolchain_args = {
4041 use_doom_melon = true # Doom melon always required for 32-bit builds.
4042 current_cpu = "x86"
4043 }
4044 }
4045
4046 toolchain("64") {
4047 tool("cc") {
4048 command = "gcc {{source}}"
4049 ...
4050 }
4051
4052 toolchain_args = {
4053 # use_doom_melon is not overridden here, it will take the default.
4054 current_cpu = "x64"
4055 }
4056 }
4057```
4058
4059#### **Example of cross-toolchain dependencies**
4060
4061```
4062 If a 64-bit target wants to depend on a 32-bit binary, it would specify a
4063 dependency using data_deps (data deps are like deps that are only needed at
4064 runtime and aren't linked, since you can't link a 32-bit and a 64-bit
4065 library).
4066
4067 executable("my_program") {
4068 ...
4069 if (target_cpu == "x64") {
4070 # The 64-bit build needs this 32-bit helper.
4071 data_deps = [ ":helper(//toolchains:32)" ]
4072 }
4073 }
4074
4075 if (target_cpu == "x86") {
4076 # Our helper library is only compiled in 32-bits.
4077 shared_library("helper") {
4078 ...
4079 }
4080 }
4081```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004082### **write_file**: Write a file to disk.
Brett Wilson796ed472018-07-16 15:11:09 -07004083
4084```
Julie Hockett09171292018-07-31 14:35:10 -07004085 write_file(filename, data, output_conversion = "")
Brett Wilson796ed472018-07-16 15:11:09 -07004086
4087 If data is a list, the list will be written one-item-per-line with no quoting
4088 or brackets.
4089
4090 If the file exists and the contents are identical to that being written, the
4091 file will not be updated. This will prevent unnecessary rebuilds of targets
4092 that depend on this file.
4093
4094 One use for write_file is to write a list of inputs to an script that might
4095 be too long for the command line. However, it is preferable to use response
4096 files for this purpose. See "gn help response_file_contents".
Brett Wilson796ed472018-07-16 15:11:09 -07004097```
4098
4099#### **Arguments**
4100
4101```
4102 filename
4103 Filename to write. This must be within the output directory.
4104
4105 data
4106 The list or string to write.
Julie Hockett09171292018-07-31 14:35:10 -07004107
4108 output_conversion
Julie Hockett81ee1962019-05-13 11:24:42 -07004109 Controls how the output is written. See "gn help io_conversion".
Brett Wilson796ed472018-07-16 15:11:09 -07004110```
4111## Built-in predefined variables
4112
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004113### **current_cpu**: The processor architecture of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004114
4115```
4116 The build configuration usually sets this value based on the value of
4117 "host_cpu" (see "gn help host_cpu") and then threads this through the
4118 toolchain definitions to ensure that it always reflects the appropriate
4119 value.
4120
Julie Hockette2a29402018-07-31 10:11:42 -07004121 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004122 empty string ("") by default but is declared so that it can be overridden on
4123 the command line if so desired.
4124
4125 See "gn help target_cpu" for a list of common values returned.
4126```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004127### **current_os**: The operating system of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004128
4129```
4130 The build configuration usually sets this value based on the value of
4131 "target_os" (see "gn help target_os"), and then threads this through the
4132 toolchain definitions to ensure that it always reflects the appropriate
4133 value.
4134
Julie Hockette2a29402018-07-31 10:11:42 -07004135 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004136 empty string ("") by default but is declared so that it can be overridden on
4137 the command line if so desired.
4138
4139 See "gn help target_os" for a list of common values returned.
4140```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004141### **current_toolchain**: Label of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004142
4143```
4144 A fully-qualified label representing the current toolchain. You can use this
4145 to make toolchain-related decisions in the build. See also
4146 "default_toolchain".
4147```
4148
4149#### **Example**
4150
4151```
4152 if (current_toolchain == "//build:64_bit_toolchain") {
4153 executable("output_thats_64_bit_only") {
4154 ...
4155```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004156### **default_toolchain**: [string] Label of the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004157
4158```
4159 A fully-qualified label representing the default toolchain, which may not
4160 necessarily be the current one (see "current_toolchain").
4161```
James Robinson61377e32020-02-13 15:20:07 -08004162### **gn_version**: [number] The version of gn.
4163
4164```
4165 Corresponds to the number printed by `gn --version`.
4166```
4167
4168#### **Example**
4169
4170```
4171 assert(gn_version >= 1700, "need GN version 1700 for the frobulate feature")
4172```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004173### **host_cpu**: The processor architecture that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004174
4175```
4176 This is value is exposed so that cross-compile toolchains can access the host
4177 architecture when needed.
4178
4179 The value should generally be considered read-only, but it can be overriden
4180 in order to handle unusual cases where there might be multiple plausible
4181 values for the host architecture (e.g., if you can do either 32-bit or 64-bit
4182 builds). The value is not used internally by GN for any purpose.
4183```
4184
4185#### **Some possible values**
4186
4187```
4188 - "x64"
4189 - "x86"
4190```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004191### **host_os**: [string] The operating system that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004192
4193```
4194 This value is exposed so that cross-compiles can access the host build
4195 system's settings.
4196
4197 This value should generally be treated as read-only. It, however, is not used
4198 internally by GN for any purpose.
4199```
4200
4201#### **Some possible values**
4202
4203```
4204 - "linux"
4205 - "mac"
4206 - "win"
4207```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004208### **invoker**: [string] The invoking scope inside a template.
Brett Wilson796ed472018-07-16 15:11:09 -07004209
4210```
4211 Inside a template invocation, this variable refers to the scope of the
4212 invoker of the template. Outside of template invocations, this variable is
4213 undefined.
4214
4215 All of the variables defined inside the template invocation are accessible as
4216 members of the "invoker" scope. This is the way that templates read values
4217 set by the callers.
4218
4219 This is often used with "defined" to see if a value is set on the invoking
4220 scope.
4221
4222 See "gn help template" for more examples.
4223```
4224
4225#### **Example**
4226
4227```
4228 template("my_template") {
4229 print(invoker.sources) # Prints [ "a.cc", "b.cc" ]
4230 print(defined(invoker.foo)) # Prints false.
4231 print(defined(invoker.bar)) # Prints true.
4232 }
4233
4234 my_template("doom_melon") {
4235 sources = [ "a.cc", "b.cc" ]
4236 bar = 123
4237 }
4238```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004239### **python_path**: Absolute path of Python.
Brett Wilson796ed472018-07-16 15:11:09 -07004240
4241```
4242 Normally used in toolchain definitions if running some command requires
4243 Python. You will normally not need this when invoking scripts since GN
4244 automatically finds it for you.
4245```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004246### **root_build_dir**: [string] Directory where build commands are run.
Brett Wilson796ed472018-07-16 15:11:09 -07004247
4248```
4249 This is the root build output directory which will be the current directory
4250 when executing all compilers and scripts.
4251
4252 Most often this is used with rebase_path (see "gn help rebase_path") to
4253 convert arguments to be relative to a script's current directory.
4254```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004255### **root_gen_dir**: Directory for the toolchain's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004256
4257```
4258 Absolute path to the root of the generated output directory tree for the
4259 current toolchain. An example would be "//out/Debug/gen" for the default
4260 toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain.
4261
4262 This is primarily useful for setting up include paths for generated files. If
4263 you are passing this to a script, you will want to pass it through
4264 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4265 build directory.
4266
4267 See also "target_gen_dir" which is usually a better location for generated
4268 files. It will be inside the root generated dir.
4269```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004270### **root_out_dir**: [string] Root directory for toolchain output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004271
4272```
4273 Absolute path to the root of the output directory tree for the current
4274 toolchain. It will not have a trailing slash.
4275
4276 For the default toolchain this will be the same as the root_build_dir. An
4277 example would be "//out/Debug" for the default toolchain, or
4278 "//out/Debug/arm" for the "arm" toolchain.
4279
4280 This is primarily useful for setting up script calls. If you are passing this
4281 to a script, you will want to pass it through rebase_path() (see "gn help
4282 rebase_path") to convert it to be relative to the build directory.
4283
4284 See also "target_out_dir" which is usually a better location for output
4285 files. It will be inside the root output dir.
4286```
4287
4288#### **Example**
4289
4290```
4291 action("myscript") {
4292 # Pass the output dir to the script.
4293 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
4294 }
4295```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004296### **target_cpu**: The desired cpu architecture for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004297
4298```
4299 This value should be used to indicate the desired architecture for the
4300 primary objects of the build. It will match the cpu architecture of the
4301 default toolchain, but not necessarily the current toolchain.
4302
4303 In many cases, this is the same as "host_cpu", but in the case of
4304 cross-compiles, this can be set to something different. This value is
4305 different from "current_cpu" in that it does not change based on the current
4306 toolchain. When writing rules, "current_cpu" should be used rather than
4307 "target_cpu" most of the time.
4308
4309 This value is not used internally by GN for any purpose, so it may be set to
4310 whatever value is needed for the build. GN defaults this value to the empty
4311 string ("") and the configuration files should set it to an appropriate value
4312 (e.g., setting it to the value of "host_cpu") if it is not overridden on the
4313 command line or in the args.gn file.
4314```
4315
4316#### **Possible values**
4317
4318```
4319 - "x86"
4320 - "x64"
4321 - "arm"
4322 - "arm64"
4323 - "mipsel"
4324```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004325### **target_gen_dir**: Directory for a target's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004326
4327```
4328 Absolute path to the target's generated file directory. This will be the
4329 "root_gen_dir" followed by the relative path to the current build file. If
4330 your file is in "//tools/doom_melon" then target_gen_dir would be
4331 "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash.
4332
4333 This is primarily useful for setting up include paths for generated files. If
4334 you are passing this to a script, you will want to pass it through
4335 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4336 build directory.
4337
4338 See also "gn help root_gen_dir".
4339```
4340
4341#### **Example**
4342
4343```
4344 action("myscript") {
4345 # Pass the generated output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004346 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004347 }
4348```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004349### **target_name**: [string] The name of the current target.
Brett Wilson796ed472018-07-16 15:11:09 -07004350
4351```
4352 Inside a target or template invocation, this variable refers to the name
4353 given to the target or template invocation. Outside of these, this variable
4354 is undefined.
4355
4356 This is most often used in template definitions to name targets defined in
4357 the template based on the name of the invocation. This is necessary both to
4358 ensure generated targets have unique names and to generate a target with the
4359 exact name of the invocation that other targets can depend on.
4360
4361 Be aware that this value will always reflect the innermost scope. So when
4362 defining a target inside a template, target_name will refer to the target
4363 rather than the template invocation. To get the name of the template
4364 invocation in this case, you should save target_name to a temporary variable
4365 outside of any target definitions.
4366
4367 See "gn help template" for more examples.
4368```
4369
4370#### **Example**
4371
4372```
4373 executable("doom_melon") {
4374 print(target_name) # Prints "doom_melon".
4375 }
4376
4377 template("my_template") {
4378 print(target_name) # Prints "space_ray" when invoked below.
4379
4380 executable(target_name + "_impl") {
4381 print(target_name) # Prints "space_ray_impl".
4382 }
4383 }
4384
4385 my_template("space_ray") {
4386 }
4387```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004388### **target_os**: The desired operating system for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004389
4390```
4391 This value should be used to indicate the desired operating system for the
4392 primary object(s) of the build. It will match the OS of the default
4393 toolchain.
4394
4395 In many cases, this is the same as "host_os", but in the case of
4396 cross-compiles, it may be different. This variable differs from "current_os"
4397 in that it can be referenced from inside any toolchain and will always return
4398 the initial value.
4399
4400 This should be set to the most specific value possible. So, "android" or
4401 "chromeos" should be used instead of "linux" where applicable, even though
4402 Android and ChromeOS are both Linux variants. This can mean that one needs to
4403 write
4404
4405 if (target_os == "android" || target_os == "linux") {
4406 # ...
4407 }
4408
4409 and so forth.
4410
4411 This value is not used internally by GN for any purpose, so it may be set to
4412 whatever value is needed for the build. GN defaults this value to the empty
4413 string ("") and the configuration files should set it to an appropriate value
4414 (e.g., setting it to the value of "host_os") if it is not set via the command
4415 line or in the args.gn file.
4416```
4417
4418#### **Possible values**
4419
4420```
4421 - "android"
4422 - "chromeos"
4423 - "ios"
4424 - "linux"
4425 - "nacl"
4426 - "mac"
4427 - "win"
4428```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004429### **target_out_dir**: [string] Directory for target output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004430
4431```
4432 Absolute path to the target's generated file directory. If your current
4433 target is in "//tools/doom_melon" then this value might be
4434 "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash.
4435
4436 This is primarily useful for setting up arguments for calling scripts. If you
4437 are passing this to a script, you will want to pass it through rebase_path()
4438 (see "gn help rebase_path") to convert it to be relative to the build
4439 directory.
4440
4441 See also "gn help root_out_dir".
4442```
4443
4444#### **Example**
4445
4446```
4447 action("myscript") {
4448 # Pass the output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004449 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004450 }
4451```
4452## Variables you set in targets
4453
Julie Hockettce1fa072019-05-07 17:44:37 -07004454### **aliased_deps**: [scope] Set of crate-dependency pairs.
4455
4456```
4457 Valid for `rust_library` targets and `executable`, `static_library`, and
4458 `shared_library` targets that contain Rust sources.
4459
4460 A scope, each key indicating the renamed crate and the corresponding value
4461 specifying the label of the dependency producing the relevant binary.
4462
4463 All dependencies listed in this field *must* be listed as deps of the target.
4464
4465 executable("foo") {
4466 sources = [ "main.rs" ]
4467 deps = [ "//bar" ]
4468 }
4469
4470 This target would compile the `foo` crate with the following `extern` flag:
4471 `rustc ...command... --extern bar=/obj/bar`
4472
4473 executable("foo") {
4474 sources = [ "main.rs" ]
4475 deps = [ ":bar" ]
4476 aliased_deps = {
4477 bar_renamed = ":bar"
4478 }
4479 }
4480
4481 With the addition of `aliased_deps`, above target would instead compile with:
4482 `rustc ...command... --extern bar_renamed=/obj/bar`
4483```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004484### **all_dependent_configs**: Configs to be forced on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07004485
4486```
4487 A list of config labels.
4488
4489 All targets depending on this one, and recursively, all targets depending on
4490 those, will have the configs listed in this variable added to them. These
4491 configs will also apply to the current target.
4492
4493 This addition happens in a second phase once a target and all of its
4494 dependencies have been resolved. Therefore, a target will not see these
4495 force-added configs in their "configs" variable while the script is running,
4496 and they can not be removed. As a result, this capability should generally
4497 only be used to add defines and include directories necessary to compile a
4498 target's headers.
4499
4500 See also "public_configs".
4501```
4502
4503#### **Ordering of flags and values**
4504
4505```
4506 1. Those set on the current target (not in a config).
4507 2. Those set on the "configs" on the target in order that the
4508 configs appear in the list.
4509 3. Those set on the "all_dependent_configs" on the target in order
4510 that the configs appear in the list.
4511 4. Those set on the "public_configs" on the target in order that
4512 those configs appear in the list.
4513 5. all_dependent_configs pulled from dependencies, in the order of
4514 the "deps" list. This is done recursively. If a config appears
4515 more than once, only the first occurence will be used.
4516 6. public_configs pulled from dependencies, in the order of the
4517 "deps" list. If a dependency is public, they will be applied
4518 recursively.
4519```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004520### **allow_circular_includes_from**: Permit includes from deps.
Brett Wilson796ed472018-07-16 15:11:09 -07004521
4522```
4523 A list of target labels. Must be a subset of the target's "deps". These
4524 targets will be permitted to include headers from the current target despite
4525 the dependency going in the opposite direction.
4526
4527 When you use this, both targets must be included in a final binary for it to
4528 link. To keep linker errors from happening, it is good practice to have all
4529 external dependencies depend only on one of the two targets, and to set the
4530 visibility on the other to enforce this. Thus the targets will always be
4531 linked together in any output.
4532```
4533
4534#### **Details**
4535
4536```
4537 Normally, for a file in target A to include a file from target B, A must list
4538 B as a dependency. This invariant is enforced by the "gn check" command (and
4539 the --check flag to "gn gen" -- see "gn help check").
4540
4541 Sometimes, two targets might be the same unit for linking purposes (two
4542 source sets or static libraries that would always be linked together in a
4543 final executable or shared library) and they each include headers from the
4544 other: you want A to be able to include B's headers, and B to include A's
4545 headers. This is not an ideal situation but is sometimes unavoidable.
4546
4547 This list, if specified, lists which of the dependencies of the current
4548 target can include header files from the current target. That is, if A
4549 depends on B, B can only include headers from A if it is in A's
4550 allow_circular_includes_from list. Normally includes must follow the
4551 direction of dependencies, this flag allows them to go in the opposite
4552 direction.
4553```
4554
4555#### **Danger**
4556
4557```
4558 In the above example, A's headers are likely to include headers from A's
4559 dependencies. Those dependencies may have public_configs that apply flags,
4560 defines, and include paths that make those headers work properly.
4561
4562 With allow_circular_includes_from, B can include A's headers, and
4563 transitively from A's dependencies, without having the dependencies that
4564 would bring in the public_configs those headers need. The result may be
4565 errors or inconsistent builds.
4566
4567 So when you use allow_circular_includes_from, make sure that any compiler
4568 settings, flags, and include directories are the same between both targets
4569 (consider putting such things in a shared config they can both reference).
4570 Make sure the dependencies are also the same (you might consider a group to
4571 collect such dependencies they both depend on).
4572```
4573
4574#### **Example**
4575
4576```
4577 source_set("a") {
4578 deps = [ ":b", ":a_b_shared_deps" ]
4579 allow_circular_includes_from = [ ":b" ]
4580 ...
4581 }
4582
4583 source_set("b") {
4584 deps = [ ":a_b_shared_deps" ]
4585 # Sources here can include headers from a despite lack of deps.
4586 ...
4587 }
4588
4589 group("a_b_shared_deps") {
4590 public_deps = [ ":c" ]
4591 }
4592```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004593### **arflags**: Arguments passed to static_library archiver.
Brett Wilson796ed472018-07-16 15:11:09 -07004594
4595```
4596 A list of flags passed to the archive/lib command that creates static
4597 libraries.
4598
4599 arflags are NOT pushed to dependents, so applying arflags to source sets or
4600 any other target type will be a no-op. As with ldflags, you could put the
4601 arflags in a config and set that as a public or "all dependent" config, but
4602 that will likely not be what you want. If you have a chain of static
4603 libraries dependent on each other, this can cause the flags to propagate up
4604 to other static libraries. Due to the nature of how arflags are typically
4605 used, you will normally want to apply them directly on static_library targets
4606 themselves.
4607```
4608
4609#### **Ordering of flags and values**
4610
4611```
4612 1. Those set on the current target (not in a config).
4613 2. Those set on the "configs" on the target in order that the
4614 configs appear in the list.
4615 3. Those set on the "all_dependent_configs" on the target in order
4616 that the configs appear in the list.
4617 4. Those set on the "public_configs" on the target in order that
4618 those configs appear in the list.
4619 5. all_dependent_configs pulled from dependencies, in the order of
4620 the "deps" list. This is done recursively. If a config appears
4621 more than once, only the first occurence will be used.
4622 6. public_configs pulled from dependencies, in the order of the
4623 "deps" list. If a dependency is public, they will be applied
4624 recursively.
4625```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004626### **args**: (target variable) Arguments passed to an action.
Brett Wilson796ed472018-07-16 15:11:09 -07004627
4628```
4629 For action and action_foreach targets, args is the list of arguments to pass
4630 to the script. Typically you would use source expansion (see "gn help
4631 source_expansion") to insert the source file names.
4632
4633 See also "gn help action" and "gn help action_foreach".
4634```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004635### **asmflags**: Flags passed to the assembler.
Brett Wilson796ed472018-07-16 15:11:09 -07004636
4637```
4638 A list of strings.
4639
4640 "asmflags" are passed to any invocation of a tool that takes an .asm or .S
4641 file as input.
4642```
4643
4644#### **Ordering of flags and values**
4645
4646```
4647 1. Those set on the current target (not in a config).
4648 2. Those set on the "configs" on the target in order that the
4649 configs appear in the list.
4650 3. Those set on the "all_dependent_configs" on the target in order
4651 that the configs appear in the list.
4652 4. Those set on the "public_configs" on the target in order that
4653 those configs appear in the list.
4654 5. all_dependent_configs pulled from dependencies, in the order of
4655 the "deps" list. This is done recursively. If a config appears
4656 more than once, only the first occurence will be used.
4657 6. public_configs pulled from dependencies, in the order of the
4658 "deps" list. If a dependency is public, they will be applied
4659 recursively.
4660```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004661### **assert_no_deps**: Ensure no deps on these targets.
Brett Wilson796ed472018-07-16 15:11:09 -07004662
4663```
4664 A list of label patterns.
4665
4666 This list is a list of patterns that must not match any of the transitive
4667 dependencies of the target. These include all public, private, and data
4668 dependencies, and cross shared library boundaries. This allows you to express
4669 that undesirable code isn't accidentally added to downstream dependencies in
4670 a way that might otherwise be difficult to notice.
4671
4672 Checking does not cross executable boundaries. If a target depends on an
4673 executable, it's assumed that the executable is a tool that is producing part
4674 of the build rather than something that is linked and distributed. This
4675 allows assert_no_deps to express what is distributed in the final target
4676 rather than depend on the internal build steps (which may include
4677 non-distributable code).
4678
4679 See "gn help label_pattern" for the format of the entries in the list. These
4680 patterns allow blacklisting individual targets or whole directory
4681 hierarchies.
4682
4683 Sometimes it is desirable to enforce that many targets have no dependencies
4684 on a target or set of targets. One efficient way to express this is to create
4685 a group with the assert_no_deps rule on it, and make that group depend on all
4686 targets you want to apply that assertion to.
4687```
4688
4689#### **Example**
4690
4691```
4692 executable("doom_melon") {
4693 deps = [ "//foo:bar" ]
4694 ...
4695 assert_no_deps = [
4696 "//evil/*", # Don't link any code from the evil directory.
4697 "//foo:test_support", # This target is also disallowed.
4698 ]
4699 }
4700```
Sylvain Defresne89e64252020-08-07 13:01:06 +02004701### **bridge_header**: [string] Path to C/Objective-C compatibility header.
4702
4703```
4704 Valid for binary targets that contain Swift sources.
4705
4706 Path to an header that includes C/Objective-C functions and types that
4707 needs to be made available to the Swift module.
4708```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004709### **bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in
Brett Wilson796ed472018-07-16 15:11:09 -07004710```
4711 create_bundle.
4712
4713 A string corresponding to a path in $root_build_dir.
4714
4715 This string is used by the "create_bundle" target to expand the
4716 {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must
4717 correspond to a path under "bundle_root_dir".
4718
4719 See "gn help bundle_root_dir" for examples.
4720```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004721### **bundle_deps_filter**: [label list] A list of labels that are filtered out.
Brett Wilson796ed472018-07-16 15:11:09 -07004722
4723```
4724 A list of target labels.
4725
4726 This list contains target label patterns that should be filtered out when
4727 creating the bundle. Any target matching one of those label will be removed
4728 from the dependencies of the create_bundle target.
4729
4730 This is mostly useful when creating application extension bundle as the
4731 application extension has access to runtime resources from the application
4732 bundle and thus do not require a second copy.
4733
4734 See "gn help create_bundle" for more information.
4735```
4736
4737#### **Example**
4738
4739```
4740 create_bundle("today_extension") {
4741 deps = [
4742 "//base"
4743 ]
4744 bundle_root_dir = "$root_out_dir/today_extension.appex"
4745 bundle_deps_filter = [
4746 # The extension uses //base but does not use any function calling into
4747 # third_party/icu and thus does not need the icudtl.dat file.
4748 "//third_party/icu:icudata",
4749 ]
4750 }
4751```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004752### **bundle_executable_dir**
Brett Wilson796ed472018-07-16 15:11:09 -07004753
Joe Armstrongb199e542019-03-21 09:40:08 +08004754```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004755 bundle_executable_dir: Expansion of {{bundle_executable_dir}} in
4756 create_bundle.
4757
Brett Wilson796ed472018-07-16 15:11:09 -07004758 A string corresponding to a path in $root_build_dir.
4759
4760 This string is used by the "create_bundle" target to expand the
4761 {{bundle_executable_dir}} of the "bundle_data" target it depends on. This
4762 must correspond to a path under "bundle_root_dir".
4763
4764 See "gn help bundle_root_dir" for examples.
4765```
Joe Armstrongb199e542019-03-21 09:40:08 +08004766### **bundle_resources_dir**
4767
Brett Wilson796ed472018-07-16 15:11:09 -07004768```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02004769 bundle_resources_dir: Expansion of {{bundle_resources_dir}} in
4770 create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07004771
4772 A string corresponding to a path in $root_build_dir.
4773
4774 This string is used by the "create_bundle" target to expand the
4775 {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must
4776 correspond to a path under "bundle_root_dir".
4777
4778 See "gn help bundle_root_dir" for examples.
4779```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004780### **bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07004781
4782```
4783 A string corresponding to a path in root_build_dir.
4784
4785 This string is used by the "create_bundle" target to expand the
4786 {{bundle_root_dir}} of the "bundle_data" target it depends on. This must
4787 correspond to a path under root_build_dir.
4788```
4789
4790#### **Example**
4791
4792```
4793 bundle_data("info_plist") {
4794 sources = [ "Info.plist" ]
4795 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
4796 }
4797
4798 create_bundle("doom_melon.app") {
4799 deps = [ ":info_plist" ]
4800 bundle_root_dir = "${root_build_dir}/doom_melon.app"
4801 bundle_contents_dir = "${bundle_root_dir}/Contents"
4802 bundle_resources_dir = "${bundle_contents_dir}/Resources"
4803 bundle_executable_dir = "${bundle_contents_dir}/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07004804 }
4805```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004806### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004807
4808```
4809 A list of strings.
4810
4811 "cflags" are passed to all invocations of the C, C++, Objective C, and
4812 Objective C++ compilers.
4813
4814 To target one of these variants individually, use "cflags_c", "cflags_cc",
4815 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4816 versions of cflags* will be appended on the compiler command line after
4817 "cflags".
4818
Sylvain Defresne89e64252020-08-07 13:01:06 +02004819 See also "asmflags" for flags for assembly-language files and "swiftflags"
4820 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004821```
4822
4823#### **Ordering of flags and values**
4824
4825```
4826 1. Those set on the current target (not in a config).
4827 2. Those set on the "configs" on the target in order that the
4828 configs appear in the list.
4829 3. Those set on the "all_dependent_configs" on the target in order
4830 that the configs appear in the list.
4831 4. Those set on the "public_configs" on the target in order that
4832 those configs appear in the list.
4833 5. all_dependent_configs pulled from dependencies, in the order of
4834 the "deps" list. This is done recursively. If a config appears
4835 more than once, only the first occurence will be used.
4836 6. public_configs pulled from dependencies, in the order of the
4837 "deps" list. If a dependency is public, they will be applied
4838 recursively.
4839```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004840### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004841
4842```
4843 A list of strings.
4844
4845 "cflags" are passed to all invocations of the C, C++, Objective C, and
4846 Objective C++ compilers.
4847
4848 To target one of these variants individually, use "cflags_c", "cflags_cc",
4849 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4850 versions of cflags* will be appended on the compiler command line after
4851 "cflags".
4852
Sylvain Defresne89e64252020-08-07 13:01:06 +02004853 See also "asmflags" for flags for assembly-language files and "swiftflags"
4854 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004855```
4856
4857#### **Ordering of flags and values**
4858
4859```
4860 1. Those set on the current target (not in a config).
4861 2. Those set on the "configs" on the target in order that the
4862 configs appear in the list.
4863 3. Those set on the "all_dependent_configs" on the target in order
4864 that the configs appear in the list.
4865 4. Those set on the "public_configs" on the target in order that
4866 those configs appear in the list.
4867 5. all_dependent_configs pulled from dependencies, in the order of
4868 the "deps" list. This is done recursively. If a config appears
4869 more than once, only the first occurence will be used.
4870 6. public_configs pulled from dependencies, in the order of the
4871 "deps" list. If a dependency is public, they will be applied
4872 recursively.
4873```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004874### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004875
4876```
4877 A list of strings.
4878
4879 "cflags" are passed to all invocations of the C, C++, Objective C, and
4880 Objective C++ compilers.
4881
4882 To target one of these variants individually, use "cflags_c", "cflags_cc",
4883 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4884 versions of cflags* will be appended on the compiler command line after
4885 "cflags".
4886
Sylvain Defresne89e64252020-08-07 13:01:06 +02004887 See also "asmflags" for flags for assembly-language files and "swiftflags"
4888 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004889```
4890
4891#### **Ordering of flags and values**
4892
4893```
4894 1. Those set on the current target (not in a config).
4895 2. Those set on the "configs" on the target in order that the
4896 configs appear in the list.
4897 3. Those set on the "all_dependent_configs" on the target in order
4898 that the configs appear in the list.
4899 4. Those set on the "public_configs" on the target in order that
4900 those configs appear in the list.
4901 5. all_dependent_configs pulled from dependencies, in the order of
4902 the "deps" list. This is done recursively. If a config appears
4903 more than once, only the first occurence will be used.
4904 6. public_configs pulled from dependencies, in the order of the
4905 "deps" list. If a dependency is public, they will be applied
4906 recursively.
4907```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004908### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004909
4910```
4911 A list of strings.
4912
4913 "cflags" are passed to all invocations of the C, C++, Objective C, and
4914 Objective C++ compilers.
4915
4916 To target one of these variants individually, use "cflags_c", "cflags_cc",
4917 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4918 versions of cflags* will be appended on the compiler command line after
4919 "cflags".
4920
Sylvain Defresne89e64252020-08-07 13:01:06 +02004921 See also "asmflags" for flags for assembly-language files and "swiftflags"
4922 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004923```
4924
4925#### **Ordering of flags and values**
4926
4927```
4928 1. Those set on the current target (not in a config).
4929 2. Those set on the "configs" on the target in order that the
4930 configs appear in the list.
4931 3. Those set on the "all_dependent_configs" on the target in order
4932 that the configs appear in the list.
4933 4. Those set on the "public_configs" on the target in order that
4934 those configs appear in the list.
4935 5. all_dependent_configs pulled from dependencies, in the order of
4936 the "deps" list. This is done recursively. If a config appears
4937 more than once, only the first occurence will be used.
4938 6. public_configs pulled from dependencies, in the order of the
4939 "deps" list. If a dependency is public, they will be applied
4940 recursively.
4941```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004942### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07004943
4944```
4945 A list of strings.
4946
4947 "cflags" are passed to all invocations of the C, C++, Objective C, and
4948 Objective C++ compilers.
4949
4950 To target one of these variants individually, use "cflags_c", "cflags_cc",
4951 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
4952 versions of cflags* will be appended on the compiler command line after
4953 "cflags".
4954
Sylvain Defresne89e64252020-08-07 13:01:06 +02004955 See also "asmflags" for flags for assembly-language files and "swiftflags"
4956 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07004957```
4958
4959#### **Ordering of flags and values**
4960
4961```
4962 1. Those set on the current target (not in a config).
4963 2. Those set on the "configs" on the target in order that the
4964 configs appear in the list.
4965 3. Those set on the "all_dependent_configs" on the target in order
4966 that the configs appear in the list.
4967 4. Those set on the "public_configs" on the target in order that
4968 those configs appear in the list.
4969 5. all_dependent_configs pulled from dependencies, in the order of
4970 the "deps" list. This is done recursively. If a config appears
4971 more than once, only the first occurence will be used.
4972 6. public_configs pulled from dependencies, in the order of the
4973 "deps" list. If a dependency is public, they will be applied
4974 recursively.
4975```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004976### **check_includes**: [boolean] Controls whether a target's files are checked.
Brett Wilson796ed472018-07-16 15:11:09 -07004977
4978```
4979 When true (the default), the "gn check" command (as well as "gn gen" with the
4980 --check flag) will check this target's sources and headers for proper
4981 dependencies.
4982
4983 When false, the files in this target will be skipped by default. This does
4984 not affect other targets that depend on the current target, it just skips
4985 checking the includes of the current target's files.
4986
4987 If there are a few conditionally included headers that trip up checking, you
4988 can exclude headers individually by annotating them with "nogncheck" (see "gn
4989 help nogncheck").
4990
4991 The topic "gn help check" has general information on how checking works and
4992 advice on how to pass a check in problematic cases.
4993```
4994
4995#### **Example**
4996
4997```
4998 source_set("busted_includes") {
4999 # This target's includes are messed up, exclude it from checking.
5000 check_includes = false
5001 ...
5002 }
5003```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005004### **code_signing_args**: [string list] Arguments passed to code signing script.
Brett Wilson796ed472018-07-16 15:11:09 -07005005
5006```
5007 For create_bundle targets, code_signing_args is the list of arguments to pass
5008 to the code signing script. Typically you would use source expansion (see "gn
5009 help source_expansion") to insert the source file names.
5010
5011 See also "gn help create_bundle".
5012```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005013### **code_signing_outputs**: [file list] Output files for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005014
5015```
5016 Outputs from the code signing step of a create_bundle target. Must refer to
5017 files in the build directory.
5018
5019 See also "gn help create_bundle".
5020```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005021### **code_signing_script**: [file name] Script for code signing."
Brett Wilson796ed472018-07-16 15:11:09 -07005022
5023```
5024 An absolute or buildfile-relative file name of a Python script to run for a
5025 create_bundle target to perform code signing step.
5026
5027 See also "gn help create_bundle".
5028```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005029### **code_signing_sources**: [file list] Sources for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005030
5031```
5032 A list of files used as input for code signing script step of a create_bundle
5033 target. Non-absolute paths will be resolved relative to the current build
5034 file.
5035
5036 See also "gn help create_bundle".
5037```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005038### **complete_static_lib**: [boolean] Links all deps into a static library.
Brett Wilson796ed472018-07-16 15:11:09 -07005039
5040```
5041 A static library normally doesn't include code from dependencies, but instead
5042 forwards the static libraries and source sets in its deps up the dependency
5043 chain until a linkable target (an executable or shared library) is reached.
5044 The final linkable target only links each static library once, even if it
5045 appears more than once in its dependency graph.
5046
5047 In some cases the static library might be the final desired output. For
5048 example, you may be producing a static library for distribution to third
5049 parties. In this case, the static library should include code for all
5050 dependencies in one complete package. However, complete static libraries
5051 themselves are never linked into other complete static libraries. All
5052 complete static libraries are for distribution and linking them in would
5053 cause code duplication in this case. If the static library is not for
5054 distribution, it should not be complete.
5055
5056 GN treats non-complete static libraries as source sets when they are linked
5057 into complete static libraries. This is done because some tools like AR do
5058 not handle dependent static libraries properly. This makes it easier to write
5059 "alink" rules.
5060
5061 In rare cases it makes sense to list a header in more than one target if it
5062 could be considered conceptually a member of both. libraries.
5063```
5064
5065#### **Example**
5066
5067```
5068 static_library("foo") {
5069 complete_static_lib = true
5070 deps = [ "bar" ]
5071 }
5072```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005073### **configs**: Configs applying to this target or config.
Brett Wilson796ed472018-07-16 15:11:09 -07005074
5075```
5076 A list of config labels.
5077```
5078
5079#### **Configs on a target**
5080
5081```
5082 When used on a target, the include_dirs, defines, etc. in each config are
5083 appended in the order they appear to the compile command for each file in the
5084 target. They will appear after the include_dirs, defines, etc. that the
5085 target sets directly.
5086
5087 Since configs apply after the values set on a target, directly setting a
5088 compiler flag will prepend it to the command line. If you want to append a
5089 flag instead, you can put that flag in a one-off config and append that
5090 config to the target's configs list.
5091
5092 The build configuration script will generally set up the default configs
5093 applying to a given target type (see "set_defaults"). When a target is being
5094 defined, it can add to or remove from this list.
5095```
5096
5097#### **Configs on a config**
5098
5099```
5100 It is possible to create composite configs by specifying configs on a config.
5101 One might do this to forward values, or to factor out blocks of settings from
5102 very large configs into more manageable named chunks.
5103
5104 In this case, the composite config is expanded to be the concatenation of its
5105 own values, and in order, the values from its sub-configs *before* anything
5106 else happens. This has some ramifications:
5107
5108 - A target has no visibility into a config's sub-configs. Target code only
5109 sees the name of the composite config. It can't remove sub-configs or opt
5110 in to only parts of it. The composite config may not even be defined
5111 before the target is.
5112
5113 - You can get duplication of values if a config is listed twice, say, on a
5114 target and in a sub-config that also applies. In other cases, the configs
5115 applying to a target are de-duped. It's expected that if a config is
5116 listed as a sub-config that it is only used in that context. (Note that
5117 it's possible to fix this and de-dupe, but it's not normally relevant and
5118 complicates the implementation.)
5119```
5120
5121#### **Ordering of flags and values**
5122
5123```
5124 1. Those set on the current target (not in a config).
5125 2. Those set on the "configs" on the target in order that the
5126 configs appear in the list.
5127 3. Those set on the "all_dependent_configs" on the target in order
5128 that the configs appear in the list.
5129 4. Those set on the "public_configs" on the target in order that
5130 those configs appear in the list.
5131 5. all_dependent_configs pulled from dependencies, in the order of
5132 the "deps" list. This is done recursively. If a config appears
5133 more than once, only the first occurence will be used.
5134 6. public_configs pulled from dependencies, in the order of the
5135 "deps" list. If a dependency is public, they will be applied
5136 recursively.
5137```
5138
5139#### **Example**
5140
5141```
5142 # Configs on a target.
5143 source_set("foo") {
5144 # Don't use the default RTTI config that BUILDCONFIG applied to us.
5145 configs -= [ "//build:no_rtti" ]
5146
5147 # Add some of our own settings.
5148 configs += [ ":mysettings" ]
5149 }
5150
5151 # Create a default_optimization config that forwards to one of a set of more
5152 # specialized configs depending on build flags. This pattern is useful
5153 # because it allows a target to opt in to either a default set, or a more
5154 # specific set, while avoid duplicating the settings in two places.
5155 config("super_optimization") {
5156 cflags = [ ... ]
5157 }
5158 config("default_optimization") {
5159 if (optimize_everything) {
5160 configs = [ ":super_optimization" ]
5161 } else {
5162 configs = [ ":no_optimization" ]
5163 }
5164 }
5165```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005166### **contents**: Contents to write to file.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005167
5168```
5169 The contents of the file for a generated_file target.
5170 See "gn help generated_file".
5171```
Julie Hockettce1fa072019-05-07 17:44:37 -07005172### **crate_name**: [string] The name for the compiled crate.
5173
5174```
5175 Valid for `rust_library` targets and `executable`, `static_library`,
5176 `shared_library`, and `source_set` targets that contain Rust sources.
5177
5178 If crate_name is not set, then this rule will use the target name.
5179```
5180### **crate_root**: [string] The root source file for a binary or library.
5181
5182```
5183 Valid for `rust_library` targets and `executable`, `static_library`,
5184 `shared_library`, and `source_set` targets that contain Rust sources.
5185
5186 This file is usually the `main.rs` or `lib.rs` for binaries and libraries,
5187 respectively.
5188
5189 If crate_root is not set, then this rule will look for a lib.rs file (or
5190 main.rs for executable) or a single file in sources, if sources contains
5191 only one file.
5192```
5193### **crate_type**: [string] The type of linkage to use on a shared_library.
5194
5195```
5196 Valid for `rust_library` targets and `executable`, `static_library`,
5197 `shared_library`, and `source_set` targets that contain Rust sources.
5198
5199 Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib".
5200 This field sets the `crate-type` attribute for the `rustc` tool on static
5201 libraries, as well as the appropiate output extension in the
5202 `rust_output_extension` attribute. Since outputs must be explicit, the `lib`
5203 crate type (where the Rust compiler produces what it thinks is the
5204 appropriate library type) is not supported.
5205
5206 It should be noted that the "dylib" crate type in Rust is unstable in the set
5207 of symbols it exposes, and most usages today are potentially wrong and will
5208 be broken in the future.
5209
5210 Static libraries, rust libraries, and executables have this field set
5211 automatically.
5212```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005213### **data**: Runtime data file dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005214
5215```
5216 Lists files or directories required to run the given target. These are
5217 typically data files or directories of data files. The paths are interpreted
5218 as being relative to the current build file. Since these are runtime
5219 dependencies, they do not affect which targets are built or when. To declare
5220 input files to a script, use "inputs".
5221
5222 Appearing in the "data" section does not imply any special handling such as
5223 copying them to the output directory. This is just used for declaring runtime
5224 dependencies. Runtime dependencies can be queried using the "runtime_deps"
5225 category of "gn desc" or written during build generation via
5226 "--runtime-deps-list-file".
5227
5228 GN doesn't require data files to exist at build-time. So actions that produce
5229 files that are in turn runtime dependencies can list those generated files
5230 both in the "outputs" list as well as the "data" list.
5231
5232 By convention, directories are listed with a trailing slash:
5233 data = [ "test/data/" ]
5234 However, no verification is done on these so GN doesn't enforce this. The
5235 paths are just rebased and passed along when requested.
5236
5237 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5238 gathering data. See "gn help create_bundle" for details.
5239
5240 See "gn help runtime_deps" for how these are used.
5241```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005242### **data_deps**: Non-linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005243
5244```
5245 A list of target labels.
5246
5247 Specifies dependencies of a target that are not actually linked into the
5248 current target. Such dependencies will be built and will be available at
5249 runtime.
5250
5251 This is normally used for things like plugins or helper programs that a
5252 target needs at runtime.
5253
5254 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5255 gathering data_deps. See "gn help create_bundle" for details.
5256
5257 See also "gn help deps" and "gn help data".
5258```
5259
5260#### **Example**
5261
5262```
5263 executable("foo") {
5264 deps = [ "//base" ]
5265 data_deps = [ "//plugins:my_runtime_plugin" ]
5266 }
5267```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005268### **data_keys**: Keys from which to collect metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005269
5270```
5271 These keys are used to identify metadata to collect. If a walked target
5272 defines this key in its metadata, its value will be appended to the resulting
5273 collection.
5274
5275 See "gn help generated_file".
5276```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005277### **defines**: C preprocessor defines.
Brett Wilson796ed472018-07-16 15:11:09 -07005278
5279```
5280 A list of strings
5281
5282 These strings will be passed to the C/C++ compiler as #defines. The strings
5283 may or may not include an "=" to assign a value.
5284```
5285
5286#### **Ordering of flags and values**
5287
5288```
5289 1. Those set on the current target (not in a config).
5290 2. Those set on the "configs" on the target in order that the
5291 configs appear in the list.
5292 3. Those set on the "all_dependent_configs" on the target in order
5293 that the configs appear in the list.
5294 4. Those set on the "public_configs" on the target in order that
5295 those configs appear in the list.
5296 5. all_dependent_configs pulled from dependencies, in the order of
5297 the "deps" list. This is done recursively. If a config appears
5298 more than once, only the first occurence will be used.
5299 6. public_configs pulled from dependencies, in the order of the
5300 "deps" list. If a dependency is public, they will be applied
5301 recursively.
5302```
5303
5304#### **Example**
5305
5306```
5307 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
5308```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005309### **depfile**: [string] File name for input dependencies for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07005310
5311```
5312 If nonempty, this string specifies that the current action or action_foreach
5313 target will generate the given ".d" file containing the dependencies of the
5314 input. Empty or unset means that the script doesn't generate the files.
5315
5316 A depfile should be used only when a target depends on files that are not
5317 already specified by a target's inputs and sources. Likewise, depfiles should
5318 specify only those dependencies not already included in sources or inputs.
5319
5320 The .d file should go in the target output directory. If you have more than
5321 one source file that the script is being run over, you can use the output
5322 file expansions described in "gn help action_foreach" to name the .d file
Nico Weberad9eab22020-11-15 22:20:28 -05005323 according to the input.
Brett Wilson796ed472018-07-16 15:11:09 -07005324
5325 The format is that of a Makefile and all paths must be relative to the root
5326 build directory. Only one output may be listed and it must match the first
5327 output of the action.
5328
5329 Although depfiles are created by an action, they should not be listed in the
5330 action's "outputs" unless another target will use the file as an input.
5331```
5332
5333#### **Example**
5334
5335```
5336 action_foreach("myscript_target") {
5337 script = "myscript.py"
5338 sources = [ ... ]
5339
5340 # Locate the depfile in the output directory named like the
5341 # inputs but with a ".d" appended.
5342 depfile = "$relative_target_output_dir/{{source_name}}.d"
5343
5344 # Say our script uses "-o " to indicate the depfile.
5345 args = [ "{{source}}", "-o", depfile ]
5346 }
5347```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005348### **deps**: Private linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005349
5350```
5351 A list of target labels.
5352
5353 Specifies private dependencies of a target. Private dependencies are
5354 propagated up the dependency tree and linked to dependent targets, but do not
5355 grant the ability to include headers from the dependency. Public configs are
5356 not forwarded.
5357```
5358
5359#### **Details of dependency propagation**
5360
5361```
5362 Source sets, shared libraries, and non-complete static libraries will be
5363 propagated up the dependency tree across groups, non-complete static
5364 libraries and source sets.
5365
5366 Executables, shared libraries, and complete static libraries will link all
5367 propagated targets and stop propagation. Actions and copy steps also stop
5368 propagation, allowing them to take a library as an input but not force
5369 dependents to link to it.
5370
5371 Propagation of all_dependent_configs and public_configs happens independently
5372 of target type. all_dependent_configs are always propagated across all types
5373 of targets, and public_configs are always propagated across public deps of
5374 all types of targets.
5375
5376 Data dependencies are propagated differently. See "gn help data_deps" and
5377 "gn help runtime_deps".
5378
5379 See also "public_deps".
5380```
Petr Hoseka1413862020-01-03 12:54:33 -08005381### **externs**: [scope] Set of Rust crate-dependency pairs.
5382
5383```
5384 A list, each value being a scope indicating a pair of crate name and the path
5385 to the Rust library.
5386
5387 These libraries will be passed as `--extern crate_name=path` to compiler
5388 invocation containing the current target.
5389```
5390
5391#### **Examples**
5392
5393```
5394 executable("foo") {
5395 sources = [ "main.rs" ]
5396 externs = [{
5397 crate_name = "bar",
5398 path = "path/to/bar.rlib"
5399 }]
5400 }
5401
5402 This target would compile the `foo` crate with the following `extern` flag:
5403 `--extern bar=path/to/bar.rlib`.
5404```
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005405### **framework_dirs**: [directory list] Additional framework search directories.
5406
5407```
5408 A list of source directories.
5409
5410 The directories in this list will be added to the framework search path for
5411 the files in the affected target.
5412```
5413
5414#### **Ordering of flags and values**
5415
5416```
5417 1. Those set on the current target (not in a config).
5418 2. Those set on the "configs" on the target in order that the
5419 configs appear in the list.
5420 3. Those set on the "all_dependent_configs" on the target in order
5421 that the configs appear in the list.
5422 4. Those set on the "public_configs" on the target in order that
5423 those configs appear in the list.
5424 5. all_dependent_configs pulled from dependencies, in the order of
5425 the "deps" list. This is done recursively. If a config appears
5426 more than once, only the first occurence will be used.
5427 6. public_configs pulled from dependencies, in the order of the
5428 "deps" list. If a dependency is public, they will be applied
5429 recursively.
5430```
5431
5432#### **Example**
5433
5434```
5435 framework_dirs = [ "src/include", "//third_party/foo" ]
5436```
5437### **frameworks**: [name list] Name of frameworks that must be linked.
5438
5439```
5440 A list of framework names.
5441
5442 The frameworks named in that list will be linked with any dynamic link
5443 type target.
5444```
5445
5446#### **Ordering of flags and values**
5447
5448```
5449 1. Those set on the current target (not in a config).
5450 2. Those set on the "configs" on the target in order that the
5451 configs appear in the list.
5452 3. Those set on the "all_dependent_configs" on the target in order
5453 that the configs appear in the list.
5454 4. Those set on the "public_configs" on the target in order that
5455 those configs appear in the list.
5456 5. all_dependent_configs pulled from dependencies, in the order of
5457 the "deps" list. This is done recursively. If a config appears
5458 more than once, only the first occurence will be used.
5459 6. public_configs pulled from dependencies, in the order of the
5460 "deps" list. If a dependency is public, they will be applied
5461 recursively.
5462```
5463
5464#### **Example**
5465
5466```
5467 frameworks = [ "Foundation.framework", "Foo.framework" ]
5468```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005469### **friend**: Allow targets to include private headers.
Brett Wilson796ed472018-07-16 15:11:09 -07005470
5471```
5472 A list of label patterns (see "gn help label_pattern") that allow dependent
5473 targets to include private headers. Applies to all binary targets.
5474
5475 Normally if a target lists headers in the "public" list (see "gn help
5476 public"), other headers are implicitly marked as private. Private headers
5477 can not be included by other targets, even with a public dependency path.
5478 The "gn check" function performs this validation.
5479
5480 A friend declaration allows one or more targets to include private headers.
5481 This is useful for things like unit tests that are closely associated with a
5482 target and require internal knowledge without opening up all headers to be
5483 included by all dependents.
5484
5485 A friend target does not allow that target to include headers when no
5486 dependency exists. A public dependency path must still exist between two
5487 targets to include any headers from a destination target. The friend
5488 annotation merely allows the use of headers that would otherwise be
5489 prohibited because they are private.
5490
5491 The friend annotation is matched only against the target containing the file
5492 with the include directive. Friend annotations are not propagated across
5493 public or private dependencies. Friend annotations do not affect visibility.
5494```
5495
5496#### **Example**
5497
5498```
5499 static_library("lib") {
5500 # This target can include our private headers.
5501 friend = [ ":unit_tests" ]
5502
5503 public = [
5504 "public_api.h", # Normal public API for dependent targets.
5505 ]
5506
5507 # Private API and sources.
5508 sources = [
5509 "a_source_file.cc",
5510
5511 # Normal targets that depend on this one won't be able to include this
5512 # because this target defines a list of "public" headers. Without the
5513 # "public" list, all headers are implicitly public.
5514 "private_api.h",
5515 ]
5516 }
5517
5518 executable("unit_tests") {
5519 sources = [
5520 # This can include "private_api.h" from the :lib target because it
5521 # depends on that target and because of the friend annotation.
5522 "my_test.cc",
5523 ]
5524
5525 deps = [
5526 ":lib", # Required for the include to be allowed.
5527 ]
5528 }
5529```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005530### **include_dirs**: Additional include directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005531
5532```
5533 A list of source directories.
5534
5535 The directories in this list will be added to the include path for the files
5536 in the affected target.
5537```
5538
5539#### **Ordering of flags and values**
5540
5541```
5542 1. Those set on the current target (not in a config).
5543 2. Those set on the "configs" on the target in order that the
5544 configs appear in the list.
5545 3. Those set on the "all_dependent_configs" on the target in order
5546 that the configs appear in the list.
5547 4. Those set on the "public_configs" on the target in order that
5548 those configs appear in the list.
5549 5. all_dependent_configs pulled from dependencies, in the order of
5550 the "deps" list. This is done recursively. If a config appears
5551 more than once, only the first occurence will be used.
5552 6. public_configs pulled from dependencies, in the order of the
5553 "deps" list. If a dependency is public, they will be applied
5554 recursively.
5555```
5556
5557#### **Example**
5558
5559```
5560 include_dirs = [ "src/include", "//third_party/foo" ]
5561```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005562### **inputs**: Additional compile-time dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005563
5564```
5565 Inputs are compile-time dependencies of the current target. This means that
5566 all inputs must be available before compiling any of the sources or executing
5567 any actions.
5568
5569 Inputs are typically only used for action and action_foreach targets.
5570```
5571
5572#### **Inputs for actions**
5573
5574```
5575 For action and action_foreach targets, inputs should be the inputs to script
5576 that don't vary. These should be all .py files that the script uses via
5577 imports (the main script itself will be an implicit dependency of the action
5578 so need not be listed).
5579
5580 For action targets, inputs and sources are treated the same, but from a style
5581 perspective, it's recommended to follow the same rule as action_foreach and
5582 put helper files in the inputs, and the data used by the script (if any) in
5583 sources.
5584
5585 Note that another way to declare input dependencies from an action is to have
5586 the action write a depfile (see "gn help depfile"). This allows the script to
5587 dynamically write input dependencies, that might not be known until actually
5588 executing the script. This is more efficient than doing processing while
5589 running GN to determine the inputs, and is easier to keep in-sync than
5590 hardcoding the list.
5591```
5592
5593#### **Script input gotchas**
5594
5595```
5596 It may be tempting to write a script that enumerates all files in a directory
5597 as inputs. Don't do this! Even if you specify all the files in the inputs or
5598 sources in the GN target (or worse, enumerate the files in an exec_script
5599 call when running GN, which will be slow), the dependencies will be broken.
5600
5601 The problem happens if a file is ever removed because the inputs are not
5602 listed on the command line to the script. Because the script hasn't changed
5603 and all inputs are up to date, the script will not re-run and you will get a
5604 stale build. Instead, either list all inputs on the command line to the
5605 script, or if there are many, create a separate list file that the script
5606 reads. As long as this file is listed in the inputs, the build will detect
5607 when it has changed in any way and the action will re-run.
5608```
5609
5610#### **Inputs for binary targets**
5611
5612```
Julie Hockette2a29402018-07-31 10:11:42 -07005613 Any input dependencies will be resolved before compiling any sources or
5614 linking the target. Normally, all actions that a target depends on will be run
5615 before any files in a target are compiled. So if you depend on generated
5616 headers, you do not typically need to list them in the inputs section.
Brett Wilson796ed472018-07-16 15:11:09 -07005617
5618 Inputs for binary targets will be treated as implicit dependencies, meaning
5619 that changes in any of the inputs will force all sources in the target to be
5620 recompiled. If an input only applies to a subset of source files, you may
5621 want to split those into a separate target to avoid unnecessary recompiles.
5622```
5623
5624#### **Example**
5625
5626```
5627 action("myscript") {
5628 script = "domything.py"
5629 inputs = [ "input.data" ]
5630 }
5631```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005632### **ldflags**: Flags passed to the linker.
Brett Wilson796ed472018-07-16 15:11:09 -07005633
5634```
5635 A list of strings.
5636
5637 These flags are passed on the command-line to the linker and generally
5638 specify various linking options. Most targets will not need these and will
5639 use "libs" and "lib_dirs" instead.
5640
5641 ldflags are NOT pushed to dependents, so applying ldflags to source sets or
5642 static libraries will be a no-op. If you want to apply ldflags to dependent
5643 targets, put them in a config and set it in the all_dependent_configs or
5644 public_configs.
5645```
5646
5647#### **Ordering of flags and values**
5648
5649```
5650 1. Those set on the current target (not in a config).
5651 2. Those set on the "configs" on the target in order that the
5652 configs appear in the list.
5653 3. Those set on the "all_dependent_configs" on the target in order
5654 that the configs appear in the list.
5655 4. Those set on the "public_configs" on the target in order that
5656 those configs appear in the list.
5657 5. all_dependent_configs pulled from dependencies, in the order of
5658 the "deps" list. This is done recursively. If a config appears
5659 more than once, only the first occurence will be used.
5660 6. public_configs pulled from dependencies, in the order of the
5661 "deps" list. If a dependency is public, they will be applied
5662 recursively.
5663```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005664### **lib_dirs**: Additional library directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005665
5666```
5667 A list of directories.
5668
5669 Specifies additional directories passed to the linker for searching for the
5670 required libraries. If an item is not an absolute path, it will be treated as
5671 being relative to the current build file.
5672
5673 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005674 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005675 shared library or executable target is reached. Second, they are
5676 uniquified so each one is only passed once (the first instance of it
5677 will be the one used).
5678```
5679
5680#### **Ordering of flags and values**
5681
5682```
5683 1. Those set on the current target (not in a config).
5684 2. Those set on the "configs" on the target in order that the
5685 configs appear in the list.
5686 3. Those set on the "all_dependent_configs" on the target in order
5687 that the configs appear in the list.
5688 4. Those set on the "public_configs" on the target in order that
5689 those configs appear in the list.
5690 5. all_dependent_configs pulled from dependencies, in the order of
5691 the "deps" list. This is done recursively. If a config appears
5692 more than once, only the first occurence will be used.
5693 6. public_configs pulled from dependencies, in the order of the
5694 "deps" list. If a dependency is public, they will be applied
5695 recursively.
5696
5697 For "libs" and "lib_dirs" only, the values propagated from
5698 dependencies (as described above) are applied last assuming they
5699 are not already in the list.
5700```
5701
5702#### **Example**
5703
5704```
5705 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
5706```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005707### **libs**: Additional libraries to link.
Brett Wilson796ed472018-07-16 15:11:09 -07005708
5709```
5710 A list of library names or library paths.
5711
5712 These libraries will be linked into the final binary (executable or shared
5713 library) containing the current target.
5714
5715 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005716 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005717 shared library or executable target is reached. Second, they are
5718 uniquified so each one is only passed once (the first instance of it
5719 will be the one used).
5720```
5721
5722#### **Types of libs**
5723
5724```
5725 There are several different things that can be expressed in libs:
5726
5727 File paths
5728 Values containing '/' will be treated as references to files in the
5729 checkout. They will be rebased to be relative to the build directory and
5730 specified in the "libs" for linker tools. This facility should be used
5731 for libraries that are checked in to the version control. For libraries
5732 that are generated by the build, use normal GN deps to link them.
5733
5734 System libraries
5735 Values not containing '/' will be treated as system library names. These
5736 will be passed unmodified to the linker and prefixed with the
5737 "lib_switch" attribute of the linker tool. Generally you would set the
5738 "lib_dirs" so the given library is found. Your BUILD.gn file should not
5739 specify the switch (like "-l"): this will be encoded in the "lib_switch"
5740 of the tool.
Brett Wilson796ed472018-07-16 15:11:09 -07005741```
5742
5743#### **Ordering of flags and values**
5744
5745```
5746 1. Those set on the current target (not in a config).
5747 2. Those set on the "configs" on the target in order that the
5748 configs appear in the list.
5749 3. Those set on the "all_dependent_configs" on the target in order
5750 that the configs appear in the list.
5751 4. Those set on the "public_configs" on the target in order that
5752 those configs appear in the list.
5753 5. all_dependent_configs pulled from dependencies, in the order of
5754 the "deps" list. This is done recursively. If a config appears
5755 more than once, only the first occurence will be used.
5756 6. public_configs pulled from dependencies, in the order of the
5757 "deps" list. If a dependency is public, they will be applied
5758 recursively.
5759
5760 For "libs" and "lib_dirs" only, the values propagated from
5761 dependencies (as described above) are applied last assuming they
5762 are not already in the list.
5763```
5764
5765#### **Examples**
5766
5767```
5768 On Windows:
5769 libs = [ "ctl3d.lib" ]
5770
5771 On Linux:
5772 libs = [ "ld" ]
5773```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005774### **metadata**: Metadata of this target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005775
5776```
5777 Metadata is a collection of keys and values relating to a particular target.
Julie Hockettd69a9c32019-01-23 14:36:18 -08005778 Values must be lists, allowing for sane and predictable collection behavior.
5779 Generally, these keys will include three types of lists: lists of ordinary
5780 strings, lists of filenames intended to be rebased according to their
5781 particular source directory, and lists of target labels intended to be used
5782 as barriers to the walk. Verfication of these categories occurs at walk time,
5783 not creation time (since it is not clear until the walk which values are
5784 intended for which purpose).
5785```
Nate Fischer8ed01d32019-01-08 17:32:01 -08005786
Julie Hockettd69a9c32019-01-23 14:36:18 -08005787#### **Example**
Nate Fischer8ed01d32019-01-08 17:32:01 -08005788
Julie Hockettd69a9c32019-01-23 14:36:18 -08005789```
Nate Fischer8ed01d32019-01-08 17:32:01 -08005790 group("doom_melon") {
5791 metadata = {
5792 # These keys are not built in to GN but are interpreted when consuming
5793 # metadata.
5794 my_barrier = []
5795 my_files = [ "a.txt", "b.txt" ]
5796 }
5797 }
5798```
Sylvain Defresne89e64252020-08-07 13:01:06 +02005799### **module_name**: [string] The name for the compiled module.
5800
5801```
5802 Valid for binary targets that contain Swift sources.
5803
5804 If module_name is not set, then this rule will use the target name.
5805```
Wesley Moyd874bb92020-01-21 16:01:04 -08005806### **output_conversion**: Data format for generated_file targets.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005807
5808```
5809 Controls how the "contents" of a generated_file target is formatted.
Julie Hockett81ee1962019-05-13 11:24:42 -07005810 See "gn help io_conversion".
Nate Fischer8ed01d32019-01-08 17:32:01 -08005811```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005812### **output_dir**: [directory] Directory to put output file in.
Brett Wilson796ed472018-07-16 15:11:09 -07005813
5814```
5815 For library and executable targets, overrides the directory for the final
5816 output. This must be in the root_build_dir or a child thereof.
5817
5818 This should generally be in the root_out_dir or a subdirectory thereof (the
5819 root_out_dir will be the same as the root_build_dir for the default
5820 toolchain, and will be a subdirectory for other toolchains). Not putting the
5821 output in a subdirectory of root_out_dir can result in collisions between
5822 different toolchains, so you will need to take steps to ensure that your
5823 target is only present in one toolchain.
5824
5825 Normally the toolchain specifies the output directory for libraries and
5826 executables (see "gn help tool"). You will have to consult that for the
5827 default location. The default location will be used if output_dir is
5828 undefined or empty.
5829```
5830
5831#### **Example**
5832
5833```
5834 shared_library("doom_melon") {
5835 output_dir = "$root_out_dir/plugin_libs"
5836 ...
5837 }
5838```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005839### **output_extension**: Value to use for the output's file extension.
Brett Wilson796ed472018-07-16 15:11:09 -07005840
5841```
5842 Normally the file extension for a target is based on the target type and the
5843 operating system, but in rare cases you will need to override the name (for
5844 example to use "libfreetype.so.6" instead of libfreetype.so on Linux).
5845
5846 This value should not include a leading dot. If undefined, the default
5847 specified on the tool will be used. If set to the empty string, no output
5848 extension will be used.
5849
5850 The output_extension will be used to set the "{{output_extension}}" expansion
5851 which the linker tool will generally use to specify the output file name. See
5852 "gn help tool".
5853```
5854
5855#### **Example**
5856
5857```
5858 shared_library("freetype") {
5859 if (is_linux) {
5860 # Call the output "libfreetype.so.6"
5861 output_extension = "so.6"
5862 }
5863 ...
5864 }
5865
5866 # On Windows, generate a "mysettings.cpl" control panel applet. Control panel
5867 # applets are actually special shared libraries.
5868 if (is_win) {
5869 shared_library("mysettings") {
5870 output_extension = "cpl"
5871 ...
5872 }
5873 }
5874```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005875### **output_name**: Define a name for the output file other than the default.
Brett Wilson796ed472018-07-16 15:11:09 -07005876
5877```
5878 Normally the output name of a target will be based on the target name, so the
5879 target "//foo/bar:bar_unittests" will generate an output file such as
5880 "bar_unittests.exe" (using Windows as an example).
5881
5882 Sometimes you will want an alternate name to avoid collisions or if the
5883 internal name isn't appropriate for public distribution.
5884
5885 The output name should have no extension or prefixes, these will be added
5886 using the default system rules. For example, on Linux an output name of "foo"
5887 will produce a shared library "libfoo.so". There is no way to override the
5888 output prefix of a linker tool on a per- target basis. If you need more
5889 flexibility, create a copy target to produce the file you want.
5890
5891 This variable is valid for all binary output target types.
5892```
5893
5894#### **Example**
5895
5896```
5897 static_library("doom_melon") {
5898 output_name = "fluffy_bunny"
5899 }
5900```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005901### **output_prefix_override**: Don't use prefix for output name.
Brett Wilson796ed472018-07-16 15:11:09 -07005902
5903```
5904 A boolean that overrides the output prefix for a target. Defaults to false.
5905
5906 Some systems use prefixes for the names of the final target output file. The
5907 normal example is "libfoo.so" on Linux for a target named "foo".
5908
5909 The output prefix for a given target type is specified on the linker tool
5910 (see "gn help tool"). Sometimes this prefix is undesired.
5911
5912 See also "gn help output_extension".
5913```
5914
5915#### **Example**
5916
5917```
5918 shared_library("doom_melon") {
5919 # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag
5920 # will produce "doom_melon.so".
5921 output_prefix_override = true
5922 ...
5923 }
5924```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005925### **outputs**: Output files for actions and copy targets.
Brett Wilson796ed472018-07-16 15:11:09 -07005926
5927```
5928 Outputs is valid for "copy", "action", and "action_foreach" target types and
5929 indicates the resulting files. Outputs must always refer to files in the
5930 build directory.
5931
5932 copy
5933 Copy targets should have exactly one entry in the outputs list. If there is
5934 exactly one source, this can be a literal file name or a source expansion.
5935 If there is more than one source, this must contain a source expansion to
5936 map a single input name to a single output name. See "gn help copy".
5937
5938 action_foreach
5939 Action_foreach targets must always use source expansions to map input files
5940 to output files. There can be more than one output, which means that each
5941 invocation of the script will produce a set of files (presumably based on
5942 the name of the input file). See "gn help action_foreach".
5943
5944 action
5945 Action targets (excluding action_foreach) must list literal output file(s)
5946 with no source expansions. See "gn help action".
5947```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005948### **partial_info_plist**: [filename] Path plist from asset catalog compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005949
5950```
5951 Valid for create_bundle target, corresponds to the path for the partial
5952 Info.plist created by the asset catalog compiler that needs to be merged
5953 with the application Info.plist (usually done by the code signing script).
5954
5955 The file will be generated regardless of whether the asset compiler has
5956 been invoked or not. See "gn help create_bundle".
5957```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005958### **pool**: Label of the pool used by the action.
Brett Wilson796ed472018-07-16 15:11:09 -07005959
5960```
5961 A fully-qualified label representing the pool that will be used for the
5962 action. Pools are defined using the pool() {...} declaration.
5963```
5964
5965#### **Example**
5966
5967```
5968 action("action") {
5969 pool = "//build:custom_pool"
5970 ...
5971 }
5972```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005973### **precompiled_header**: [string] Header file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07005974
5975```
5976 Precompiled headers will be used when a target specifies this value, or a
5977 config applying to this target specifies this value. In addition, the tool
5978 corresponding to the source files must also specify precompiled headers (see
5979 "gn help tool"). The tool will also specify what type of precompiled headers
5980 to use, by setting precompiled_header_type to either "gcc" or "msvc".
5981
5982 The precompiled header/source variables can be specified on a target or a
5983 config, but must be the same for all configs applying to a given target since
5984 a target can only have one precompiled header.
5985
5986 If you use both C and C++ sources, the precompiled header and source file
5987 will be compiled once per language. You will want to make sure to wrap C++
5988 includes in __cplusplus #ifdefs so the file will compile in C mode.
5989```
5990
5991#### **GCC precompiled headers**
5992
5993```
5994 When using GCC-style precompiled headers, "precompiled_source" contains the
5995 path of a .h file that is precompiled and then included by all source files
5996 in targets that set "precompiled_source".
5997
5998 The value of "precompiled_header" is not used with GCC-style precompiled
5999 headers.
6000```
6001
6002#### **MSVC precompiled headers**
6003
6004```
6005 When using MSVC-style precompiled headers, the "precompiled_header" value is
6006 a string corresponding to the header. This is NOT a path to a file that GN
6007 recognises, but rather the exact string that appears in quotes after
6008 an #include line in source code. The compiler will match this string against
6009 includes or forced includes (/FI).
6010
6011 MSVC also requires a source file to compile the header with. This must be
6012 specified by the "precompiled_source" value. In contrast to the header value,
6013 this IS a GN-style file name, and tells GN which source file to compile to
6014 make the .pch file used for subsequent compiles.
6015
6016 For example, if the toolchain specifies MSVC headers:
6017
6018 toolchain("vc_x64") {
6019 ...
6020 tool("cxx") {
6021 precompiled_header_type = "msvc"
6022 ...
6023
6024 You might make a config like this:
6025
6026 config("use_precompiled_headers") {
6027 precompiled_header = "build/precompile.h"
6028 precompiled_source = "//build/precompile.cc"
6029
6030 # Either your source files should #include "build/precompile.h"
6031 # first, or you can do this to force-include the header.
6032 cflags = [ "/FI$precompiled_header" ]
6033 }
6034
6035 And then define a target that uses the config:
6036
6037 executable("doom_melon") {
6038 configs += [ ":use_precompiled_headers" ]
6039 ...
6040```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006041### **precompiled_header_type**: [string] "gcc" or "msvc".
Brett Wilson796ed472018-07-16 15:11:09 -07006042
6043```
6044 See "gn help precompiled_header".
6045```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006046### **precompiled_source**: [file name] Source file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07006047
6048```
6049 The source file that goes along with the precompiled_header when using
6050 "msvc"-style precompiled headers. It will be implicitly added to the sources
6051 of the target. See "gn help precompiled_header".
6052```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006053### **product_type**: Product type for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006054
6055```
6056 Correspond to the type of the product of a create_bundle target. Only
6057 meaningful to Xcode (used as part of the Xcode project generation).
6058
6059 When generating Xcode project files, only create_bundle target with a
6060 non-empty product_type will have a corresponding target in Xcode project.
6061```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006062### **public**: Declare public header files for a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006063
6064```
6065 A list of files that other targets can include. These permissions are checked
6066 via the "check" command (see "gn help check").
6067
6068 If no public files are declared, other targets (assuming they have visibility
6069 to depend on this target) can include any file in the sources list. If this
6070 variable is defined on a target, dependent targets may only include files on
6071 this whitelist unless that target is marked as a friend (see "gn help
6072 friend").
6073
6074 Header file permissions are also subject to visibility. A target must be
6075 visible to another target to include any files from it at all and the public
6076 headers indicate which subset of those files are permitted. See "gn help
6077 visibility" for more.
6078
6079 Public files are inherited through the dependency tree. So if there is a
6080 dependency A -> B -> C, then A can include C's public headers. However, the
6081 same is NOT true of visibility, so unless A is in C's visibility list, the
6082 include will be rejected.
6083
6084 GN only knows about files declared in the "sources" and "public" sections of
6085 targets. If a file is included that is not known to the build, it will be
6086 allowed.
6087
6088 It is common for test targets to need to include private headers for their
6089 associated code. In this case, list the test target in the "friend" list of
6090 the target that owns the private header to allow the inclusion. See
6091 "gn help friend" for more.
Julie Hockette2a29402018-07-31 10:11:42 -07006092
6093 When a binary target has no explicit or implicit public headers (a "public"
6094 list is defined but is empty), GN assumes that the target can not propagate
6095 any compile-time dependencies up the dependency tree. In this case, the build
6096 can be parallelized more efficiently.
6097 Say there are dependencies:
6098 A (shared library) -> B (shared library) -> C (action).
6099 Normally C must complete before any source files in A can compile (because
6100 there might be generated includes). But when B explicitly declares no public
6101 headers, C can execute in parallel with A's compile steps. C must still be
6102 complete before any dependents link.
Brett Wilson796ed472018-07-16 15:11:09 -07006103```
6104
6105#### **Examples**
6106
6107```
6108 These exact files are public:
6109 public = [ "foo.h", "bar.h" ]
6110
6111 No files are public (no targets may include headers from this one):
Julie Hockette2a29402018-07-31 10:11:42 -07006112 # This allows starting compilation in dependent targets earlier.
Brett Wilson796ed472018-07-16 15:11:09 -07006113 public = []
6114```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006115### **public_configs**: Configs to be applied on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07006116
6117```
6118 A list of config labels.
6119
6120 Targets directly depending on this one will have the configs listed in this
6121 variable added to them. These configs will also apply to the current target.
Julie Hockette2a29402018-07-31 10:11:42 -07006122 Generally, public configs are used to apply defines and include directories
6123 necessary to compile this target's header files.
Brett Wilson796ed472018-07-16 15:11:09 -07006124
Julie Hockette2a29402018-07-31 10:11:42 -07006125 See also "gn help all_dependent_configs".
6126```
6127
6128#### **Propagation of public configs**
6129
6130```
6131 Public configs are applied to all targets that depend directly on this one.
6132 These dependant targets can further push this target's public configs
6133 higher in the dependency tree by depending on it via public_deps (see "gn
6134 help public_deps").
6135
6136 static_library("toplevel") {
6137 # This target will get "my_config" applied to it. However, since this
6138 # target uses "deps" and not "public_deps", targets that depend on this
6139 # one won't get it.
6140 deps = [ ":intermediate" ]
6141 }
6142
6143 static_library("intermediate") {
6144 # Depending on "lower" in any way will apply "my_config" to this target.
6145 # Additionall, since this target depends on "lower" via public_deps,
6146 # targets that depend on this one will also get "my_config".
6147 public_deps = [ ":lower" ]
6148 }
6149
6150 static_library("lower") {
6151 # This will get applied to all targets that depend on this one.
6152 public_configs = [ ":my_config" ]
6153 }
6154
6155 Public config propagation happens in a second phase once a target and all of
6156 its dependencies have been resolved. Therefore, a target will not see these
Brett Wilson796ed472018-07-16 15:11:09 -07006157 force-added configs in their "configs" variable while the script is running,
6158 and they can not be removed. As a result, this capability should generally
Julie Hockette2a29402018-07-31 10:11:42 -07006159 only be used to add defines and include directories rather than setting
6160 complicated flags that some targets may not want.
Brett Wilson796ed472018-07-16 15:11:09 -07006161
Julie Hockette2a29402018-07-31 10:11:42 -07006162 Public configs may or may not be propagated across toolchain boundaries
6163 depending on the value of the propagates_configs flag (see "gn help
6164 toolchain") on the toolchain of the target declaring the public_config.
6165```
6166
6167#### **Avoiding applying public configs to this target**
6168
6169```
6170 If you want the config to apply to targets that depend on this one, but NOT
6171 this one, define an extra layer of indirection using a group:
6172
6173 # External targets depend on this group.
6174 group("my_target") {
6175 # Config to apply to all targets that depend on this one.
6176 public_configs = [ ":external_settings" ]
6177 deps = [ ":internal_target" ]
6178 }
6179
6180 # Internal target to actually compile the sources.
6181 static_library("internal_target") {
6182 # Force all external targets to depend on the group instead of directly
6183 # on this so the "external_settings" config will get applied.
6184 visibility = [ ":my_target" ]
6185 ...
6186 }
Brett Wilson796ed472018-07-16 15:11:09 -07006187```
6188
6189#### **Ordering of flags and values**
6190
6191```
6192 1. Those set on the current target (not in a config).
6193 2. Those set on the "configs" on the target in order that the
6194 configs appear in the list.
6195 3. Those set on the "all_dependent_configs" on the target in order
6196 that the configs appear in the list.
6197 4. Those set on the "public_configs" on the target in order that
6198 those configs appear in the list.
6199 5. all_dependent_configs pulled from dependencies, in the order of
6200 the "deps" list. This is done recursively. If a config appears
6201 more than once, only the first occurence will be used.
6202 6. public_configs pulled from dependencies, in the order of the
6203 "deps" list. If a dependency is public, they will be applied
6204 recursively.
6205```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006206### **public_deps**: Declare public dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07006207
6208```
6209 Public dependencies are like private dependencies (see "gn help deps") but
6210 additionally express that the current target exposes the listed deps as part
6211 of its public API.
6212
6213 This has several ramifications:
6214
6215 - public_configs that are part of the dependency are forwarded to direct
6216 dependents.
6217
6218 - Public headers in the dependency are usable by dependents (includes do
6219 not require a direct dependency or visibility).
6220
6221 - If the current target is a shared library, other shared libraries that it
6222 publicly depends on (directly or indirectly) are propagated up the
6223 dependency tree to dependents for linking.
Julie Hockette2a29402018-07-31 10:11:42 -07006224
6225 See also "gn help public_configs".
Brett Wilson796ed472018-07-16 15:11:09 -07006226```
6227
6228#### **Discussion**
6229
6230```
6231 Say you have three targets: A -> B -> C. C's visibility may allow B to depend
6232 on it but not A. Normally, this would prevent A from including any headers
6233 from C, and C's public_configs would apply only to B.
6234
6235 If B lists C in its public_deps instead of regular deps, A will now inherit
6236 C's public_configs and the ability to include C's public headers.
6237
6238 Generally if you are writing a target B and you include C's headers as part
6239 of B's public headers, or targets depending on B should consider B and C to
6240 be part of a unit, you should use public_deps instead of deps.
6241```
6242
6243#### **Example**
6244
6245```
6246 # This target can include files from "c" but not from
6247 # "super_secret_implementation_details".
6248 executable("a") {
6249 deps = [ ":b" ]
6250 }
6251
6252 shared_library("b") {
6253 deps = [ ":super_secret_implementation_details" ]
6254 public_deps = [ ":c" ]
6255 }
6256```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006257### **rebase**: Rebase collected metadata as files.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006258
6259```
6260 A boolean that triggers a rebase of collected metadata strings based on their
6261 declared file. Defaults to false.
6262
6263 Metadata generally declares files as strings relative to the local build file.
6264 However, this data is often used in other contexts, and so setting this flag
6265 will force the metadata collection to be rebased according to the local build
6266 file's location and thus allow the filename to be used anywhere.
6267
6268 Setting this flag will raise an error if any target's specified metadata is
6269 not a string value.
6270
6271 See also "gn help generated_file".
6272```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006273### **response_file_contents**: Contents of a response file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006274
6275```
6276 Sometimes the arguments passed to a script can be too long for the system's
6277 command-line capabilities. This is especially the case on Windows where the
6278 maximum command-line length is less than 8K. A response file allows you to
6279 pass an unlimited amount of data to a script in a temporary file for an
6280 action or action_foreach target.
6281
6282 If the response_file_contents variable is defined and non-empty, the list
6283 will be treated as script args (including possibly substitution patterns)
6284 that will be written to a temporary file at build time. The name of the
6285 temporary file will be substituted for "{{response_file_name}}" in the script
6286 args.
6287
6288 The response file contents will always be quoted and escaped according to
6289 Unix shell rules. To parse the response file, the Python script should use
6290 "shlex.split(file_contents)".
6291```
6292
6293#### **Example**
6294
6295```
6296 action("process_lots_of_files") {
6297 script = "process.py",
6298 inputs = [ ... huge list of files ... ]
6299
6300 # Write all the inputs to a response file for the script. Also,
6301 # make the paths relative to the script working directory.
6302 response_file_contents = rebase_path(inputs, root_build_dir)
6303
6304 # The script expects the name of the response file in --file-list.
6305 args = [
6306 "--enable-foo",
6307 "--file-list={{response_file_name}}",
6308 ]
6309 }
6310```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006311### **script**: Script file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006312
6313```
6314 An absolute or buildfile-relative file name of a Python script to run for a
6315 action and action_foreach targets (see "gn help action" and "gn help
6316 action_foreach").
6317```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006318### **sources**: Source files for a target
Brett Wilson796ed472018-07-16 15:11:09 -07006319
6320```
6321 A list of files. Non-absolute paths will be resolved relative to the current
6322 build file.
6323```
6324
6325#### **Sources for binary targets**
6326
6327```
6328 For binary targets (source sets, executables, and libraries), the known file
6329 types will be compiled with the associated tools. Unknown file types and
6330 headers will be skipped. However, you should still list all C/C+ header files
6331 so GN knows about the existence of those files for the purposes of include
6332 checking.
6333
6334 As a special case, a file ending in ".def" will be treated as a Windows
6335 module definition file. It will be appended to the link line with a
6336 preceding "/DEF:" string. There must be at most one .def file in a target
6337 and they do not cross dependency boundaries (so specifying a .def file in a
6338 static library or source set will have no effect on the executable or shared
6339 library they're linked into).
Julie Hockettce1fa072019-05-07 17:44:37 -07006340
6341 For Rust targets that do not specify a crate_root, then the crate_root will
6342 look for a lib.rs file (or main.rs for executable) or a single file in
6343 sources, if sources contains only one file.
Brett Wilson796ed472018-07-16 15:11:09 -07006344```
6345
6346#### **Sources for non-binary targets**
6347
6348```
6349 action_foreach
6350 The sources are the set of files that the script will be executed over. The
6351 script will run once per file.
6352
6353 action
6354 The sources will be treated the same as inputs. See "gn help inputs" for
6355 more information and usage advice.
6356
6357 copy
6358 The source are the source files to copy.
6359```
Sylvain Defresne89e64252020-08-07 13:01:06 +02006360### **swiftflags**: Flags passed to the swift compiler.
6361
6362```
6363 A list of strings.
6364
6365 "swiftflags" are passed to any invocation of a tool that takes an .swift
6366 file as input.
6367```
6368
6369#### **Ordering of flags and values**
6370
6371```
6372 1. Those set on the current target (not in a config).
6373 2. Those set on the "configs" on the target in order that the
6374 configs appear in the list.
6375 3. Those set on the "all_dependent_configs" on the target in order
6376 that the configs appear in the list.
6377 4. Those set on the "public_configs" on the target in order that
6378 those configs appear in the list.
6379 5. all_dependent_configs pulled from dependencies, in the order of
6380 the "deps" list. This is done recursively. If a config appears
6381 more than once, only the first occurence will be used.
6382 6. public_configs pulled from dependencies, in the order of the
6383 "deps" list. If a dependency is public, they will be applied
6384 recursively.
6385```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006386### **testonly**: Declares a target must only be used for testing.
Brett Wilson796ed472018-07-16 15:11:09 -07006387
6388```
6389 Boolean. Defaults to false.
6390
6391 When a target is marked "testonly = true", it must only be depended on by
6392 other test-only targets. Otherwise, GN will issue an error that the
6393 depenedency is not allowed.
6394
6395 This feature is intended to prevent accidentally shipping test code in a
6396 final product.
6397```
6398
6399#### **Example**
6400
6401```
6402 source_set("test_support") {
6403 testonly = true
6404 ...
6405 }
6406```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006407### **visibility**: A list of labels that can depend on a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006408
6409```
6410 A list of labels and label patterns that define which targets can depend on
6411 the current one. These permissions are checked via the "check" command (see
6412 "gn help check").
6413
6414 If visibility is not defined, it defaults to public ("*").
6415
6416 If visibility is defined, only the targets with labels that match it can
6417 depend on the current target. The empty list means no targets can depend on
6418 the current target.
6419
6420 Tip: Often you will want the same visibility for all targets in a BUILD file.
6421 In this case you can just put the definition at the top, outside of any
6422 target, and the targets will inherit that scope and see the definition.
6423```
6424
6425#### **Patterns**
6426
6427```
6428 See "gn help label_pattern" for more details on what types of patterns are
6429 supported. If a toolchain is specified, only targets in that toolchain will
6430 be matched. If a toolchain is not specified on a pattern, targets in all
6431 toolchains will be matched.
6432```
6433
6434#### **Examples**
6435
6436```
6437 Only targets in the current buildfile ("private"):
6438 visibility = [ ":*" ]
6439
6440 No targets (used for targets that should be leaf nodes):
6441 visibility = []
6442
6443 Any target ("public", the default):
6444 visibility = [ "*" ]
6445
6446 All targets in the current directory and any subdirectory:
6447 visibility = [ "./*" ]
6448
6449 Any target in "//bar/BUILD.gn":
6450 visibility = [ "//bar:*" ]
6451
6452 Any target in "//bar/" or any subdirectory thereof:
6453 visibility = [ "//bar/*" ]
6454
6455 Just these specific targets:
6456 visibility = [ ":mything", "//foo:something_else" ]
6457
6458 Any target in the current directory and any subdirectory thereof, plus
6459 any targets in "//bar/" and any subdirectory thereof.
6460 visibility = [ "./*", "//bar/*" ]
6461```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006462### **walk_keys**: Key(s) for managing the metadata collection walk.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006463
6464```
Julie Hockett152c5142019-07-12 09:53:43 -06006465 Defaults to [""].
Nate Fischer8ed01d32019-01-08 17:32:01 -08006466
6467 These keys are used to control the next step in a collection walk, acting as
6468 barriers. If a specified key is defined in a target's metadata, the walk will
6469 use the targets listed in that value to determine which targets are walked.
6470
Julie Hockett152c5142019-07-12 09:53:43 -06006471 If no walk_keys are specified for a generated_file target (i.e. "[""]"), the
Nate Fischer8ed01d32019-01-08 17:32:01 -08006472 walk will touch all deps and data_deps of the specified target recursively.
6473
6474 See "gn help generated_file".
6475```
Robert Sesekd0a6f072020-05-15 11:21:22 -04006476### **weak_frameworks**: [name list] Name of frameworks that must be weak linked.
6477
6478```
6479 A list of framework names.
6480
6481 The frameworks named in that list will be weak linked with any dynamic link
6482 type target. Weak linking instructs the dynamic loader to attempt to load
6483 the framework, but if it is not able to do so, it leaves any imported symbols
6484 unresolved. This is typically used when a framework is present in a new
6485 version of an SDK but not on older versions of the OS that the software runs
6486 on.
6487```
6488
6489#### **Ordering of flags and values**
6490
6491```
6492 1. Those set on the current target (not in a config).
6493 2. Those set on the "configs" on the target in order that the
6494 configs appear in the list.
6495 3. Those set on the "all_dependent_configs" on the target in order
6496 that the configs appear in the list.
6497 4. Those set on the "public_configs" on the target in order that
6498 those configs appear in the list.
6499 5. all_dependent_configs pulled from dependencies, in the order of
6500 the "deps" list. This is done recursively. If a config appears
6501 more than once, only the first occurence will be used.
6502 6. public_configs pulled from dependencies, in the order of the
6503 "deps" list. If a dependency is public, they will be applied
6504 recursively.
6505```
6506
6507#### **Example**
6508
6509```
6510 weak_frameworks = [ "OnlyOnNewerOSes.framework" ]
6511```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006512### **write_runtime_deps**: Writes the target's runtime_deps to the given path.
Brett Wilson796ed472018-07-16 15:11:09 -07006513
6514```
6515 Does not synchronously write the file, but rather schedules it to be written
6516 at the end of generation.
6517
6518 If the file exists and the contents are identical to that being written, the
6519 file will not be updated. This will prevent unnecessary rebuilds of targets
6520 that depend on this file.
6521
6522 Path must be within the output directory.
6523
6524 See "gn help runtime_deps" for how the runtime dependencies are computed.
6525
6526 The format of this file will list one file per line with no escaping. The
6527 files will be relative to the root_build_dir. The first line of the file will
6528 be the main output file of the target itself. The file contents will be the
6529 same as requesting the runtime deps be written on the command line (see "gn
6530 help --runtime-deps-list-file").
6531```
Harley Li0a9affb2020-06-03 10:38:42 -04006532### **xcasset_compiler_flags**: Flags passed to xcassets compiler.
6533
6534```
6535 A list of strings.
6536
6537 Valid for create_bundle target. Those flags are directly passed to
6538 xcassets compiler, corresponding to {{xcasset_compiler_flags}} substitution
6539 in compile_xcassets tool.
6540```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006541### **xcode_extra_attributes**: [scope] Extra attributes for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006542
6543```
6544 The value defined in this scope will be copied to the EXTRA_ATTRIBUTES
6545 property of the generated Xcode project. They are only meaningful when
6546 generating with --ide=xcode.
6547
6548 See "gn help create_bundle" for more information.
6549```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006550### **xcode_test_application_name**: Name for Xcode test target.
Brett Wilson796ed472018-07-16 15:11:09 -07006551
6552```
6553 Each unit and ui test target must have a test application target, and this
6554 value is used to specify the relationship. Only meaningful to Xcode (used as
6555 part of the Xcode project generation).
6556
6557 See "gn help create_bundle" for more information.
6558```
6559
Julie Hockette2a29402018-07-31 10:11:42 -07006560#### **Example**
Brett Wilson796ed472018-07-16 15:11:09 -07006561
6562```
6563 create_bundle("chrome_xctest") {
6564 test_application_name = "chrome"
6565 ...
6566 }
6567```
6568## Other help topics
6569
6570### **Build Arguments Overview**
6571
6572```
6573 Build arguments are variables passed in from outside of the build that build
6574 files can query to determine how the build works.
6575```
6576
6577#### **How build arguments are set**
6578
6579```
6580 First, system default arguments are set based on the current system. The
6581 built-in arguments are:
6582 - host_cpu
6583 - host_os
6584 - current_cpu
6585 - current_os
6586 - target_cpu
6587 - target_os
6588
6589 Next, project-specific overrides are applied. These are specified inside
6590 the default_args variable of //.gn. See "gn help dotfile" for more.
6591
6592 If specified, arguments from the --args command line flag are used. If that
6593 flag is not specified, args from previous builds in the build directory will
6594 be used (this is in the file args.gn in the build directory).
6595
6596 Last, for targets being compiled with a non-default toolchain, the toolchain
6597 overrides are applied. These are specified in the toolchain_args section of a
6598 toolchain definition. The use-case for this is that a toolchain may be
6599 building code for a different platform, and that it may want to always
6600 specify Posix, for example. See "gn help toolchain" for more.
6601
6602 If you specify an override for a build argument that never appears in a
6603 "declare_args" call, a nonfatal error will be displayed.
6604```
6605
6606#### **Examples**
6607
6608```
6609 gn args out/FooBar
6610 Create the directory out/FooBar and open an editor. You would type
6611 something like this into that file:
6612 enable_doom_melon=false
6613 os="android"
6614
6615 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
6616 This will overwrite the build directory with the given arguments. (Note
6617 that the quotes inside the args command will usually need to be escaped
6618 for your shell to pass through strings values.)
6619```
6620
6621#### **How build arguments are used**
6622
6623```
6624 If you want to use an argument, you use declare_args() and specify default
6625 values. These default values will apply if none of the steps listed in the
6626 "How build arguments are set" section above apply to the given argument, but
6627 the defaults will not override any of these.
6628
6629 Often, the root build config file will declare global arguments that will be
6630 passed to all buildfiles. Individual build files can also specify arguments
6631 that apply only to those files. It is also useful to specify build args in an
6632 "import"-ed file if you want such arguments to apply to multiple buildfiles.
6633```
6634### **.gn file**
6635
6636```
6637 When gn starts, it will search the current directory and parent directories
6638 for a file called ".gn". This indicates the source root. You can override
6639 this detection by using the --root command-line argument
6640
6641 The .gn file in the source root will be executed. The syntax is the same as a
6642 buildfile, but with very limited build setup-specific meaning.
6643
6644 If you specify --root, by default GN will look for the file .gn in that
6645 directory. If you want to specify a different file, you can additionally pass
6646 --dotfile:
6647
6648 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
6649```
6650
6651#### **Variables**
6652
6653```
6654 arg_file_template [optional]
6655 Path to a file containing the text that should be used as the default
6656 args.gn content when you run `gn args`.
6657
6658 buildconfig [required]
6659 Path to the build config file. This file will be used to set up the
6660 build file execution environment for each toolchain.
6661
6662 check_targets [optional]
6663 A list of labels and label patterns that should be checked when running
Erik Staab5d109d72020-06-29 22:53:23 -07006664 "gn check" or "gn gen --check". If neither check_targets or
6665 no_check_targets (see below) is specified, all targets will be checked.
6666 It is an error to specify both check_targets and no_check_targets. If it
6667 is the empty list, no targets will be checked. To bypass this list,
6668 request an explicit check of targets, like "//*".
6669
6670 The format of this list is identical to that of "visibility" so see "gn
6671 help visibility" for examples.
6672
6673 no_check_targets [optional]
6674 A list of labels and label patterns that should *not* be checked when
6675 running "gn check" or "gn gen --check". All other targets will be checked.
6676 If neither check_targets (see above) or no_check_targets is specified, all
6677 targets will be checked. It is an error to specify both check_targets and
6678 no_check_targets.
Brett Wilson796ed472018-07-16 15:11:09 -07006679
6680 The format of this list is identical to that of "visibility" so see "gn
6681 help visibility" for examples.
6682
James Robinson49f59032020-01-08 14:05:51 -08006683 check_system_includes [optional]
6684 Boolean to control whether system style includes are checked by default
6685 when running "gn check" or "gn gen --check". System style includes are
6686 includes that use angle brackets <> instead of double quotes "". If this
6687 setting is omitted or set to false, these includes will be ignored by
6688 default. They can be checked explicitly by running
6689 "gn check --check-system" or "gn gen --check=system"
6690
Brett Wilson796ed472018-07-16 15:11:09 -07006691 exec_script_whitelist [optional]
6692 A list of .gn/.gni files (not labels) that have permission to call the
6693 exec_script function. If this list is defined, calls to exec_script will
6694 be checked against this list and GN will fail if the current file isn't
6695 in the list.
6696
6697 This is to allow the use of exec_script to be restricted since is easy to
6698 use inappropriately. Wildcards are not supported. Files in the
6699 secondary_source tree (if defined) should be referenced by ignoring the
6700 secondary tree and naming them as if they are in the main tree.
6701
6702 If unspecified, the ability to call exec_script is unrestricted.
6703
6704 Example:
6705 exec_script_whitelist = [
6706 "//base/BUILD.gn",
6707 "//build/my_config.gni",
6708 ]
6709
6710 root [optional]
6711 Label of the root build target. The GN build will start by loading the
6712 build file containing this target name. This defaults to "//:" which will
Harley Li0a9affb2020-06-03 10:38:42 -04006713 cause the file //BUILD.gn to be loaded. Note that build_file_extension
6714 applies to the default case as well.
Brett Wilson796ed472018-07-16 15:11:09 -07006715
Nico Weberad9eab22020-11-15 22:20:28 -05006716 The command-line switch --root-target will override this value (see "gn
6717 help --root-target").
6718
Brett Wilson796ed472018-07-16 15:11:09 -07006719 script_executable [optional]
James Robinson61377e32020-02-13 15:20:07 -08006720 Path to specific Python executable or other interpreter to use in
6721 action targets and exec_script calls. By default GN searches the
6722 PATH for Python to execute these scripts.
6723
6724 If set to the empty string, the path specified in action targets
6725 and exec_script calls will be executed directly.
Brett Wilson796ed472018-07-16 15:11:09 -07006726
6727 secondary_source [optional]
6728 Label of an alternate directory tree to find input files. When searching
6729 for a BUILD.gn file (or the build config file discussed above), the file
6730 will first be looked for in the source root. If it's not found, the
6731 secondary source root will be checked (which would contain a parallel
6732 directory hierarchy).
6733
6734 This behavior is intended to be used when BUILD.gn files can't be checked
6735 in to certain source directories for whatever reason.
6736
6737 The secondary source root must be inside the main source tree.
6738
6739 default_args [optional]
6740 Scope containing the default overrides for declared arguments. These
6741 overrides take precedence over the default values specified in the
6742 declare_args() block, but can be overriden using --args or the
6743 args.gn file.
6744
6745 This is intended to be used when subprojects declare arguments with
6746 default values that need to be changed for whatever reason.
Harley Li0a9affb2020-06-03 10:38:42 -04006747
6748 build_file_extension [optional]
6749 If set to a non-empty string, this is added to the name of all build files
6750 to load.
6751 GN will look for build files named "BUILD.$build_file_extension.gn".
6752 This is intended to be used during migrations or other situations where
6753 there are two independent GN builds in the same directories.
Petr Hosek317fdc12020-05-28 10:23:42 -07006754
6755 ninja_required_version [optional]
6756 When set specifies the minimum required version of Ninja. The default
6757 required version is 1.7.2. Specifying a higher version might enable the
6758 use of some of newer features that can make the build more efficient.
Brett Wilson796ed472018-07-16 15:11:09 -07006759```
6760
6761#### **Example .gn file contents**
6762
6763```
6764 buildconfig = "//build/config/BUILDCONFIG.gn"
6765
6766 check_targets = [
6767 "//doom_melon/*", # Check everything in this subtree.
6768 "//tools:mind_controlling_ant", # Check this specific target.
6769 ]
6770
6771 root = "//:root"
6772
6773 secondary_source = "//build/config/temporary_buildfiles/"
6774
6775 default_args = {
6776 # Default to release builds for this project.
6777 is_debug = false
6778 is_component_build = false
6779 }
6780```
6781### **Build graph and execution overview**
6782
6783#### **Overall build flow**
6784
6785```
6786 1. Look for ".gn" file (see "gn help dotfile") in the current directory and
6787 walk up the directory tree until one is found. Set this directory to be
6788 the "source root" and interpret this file to find the name of the build
6789 config file.
6790
6791 2. Execute the build config file identified by .gn to set up the global
6792 variables and default toolchain name. Any arguments, variables, defaults,
6793 etc. set up in this file will be visible to all files in the build.
6794
6795 3. Load the //BUILD.gn (in the source root directory).
6796
6797 4. Recursively evaluate rules and load BUILD.gn in other directories as
6798 necessary to resolve dependencies. If a BUILD file isn't found in the
6799 specified location, GN will look in the corresponding location inside
6800 the secondary_source defined in the dotfile (see "gn help dotfile").
6801
6802 5. When a target's dependencies are resolved, write out the `.ninja`
6803 file to disk.
6804
6805 6. When all targets are resolved, write out the root build.ninja file.
Harley Li0a9affb2020-06-03 10:38:42 -04006806
6807 Note that the BUILD.gn file name may be modulated by .gn arguments such as
6808 build_file_extension.
Brett Wilson796ed472018-07-16 15:11:09 -07006809```
6810
6811#### **Executing target definitions and templates**
6812
6813```
6814 Build files are loaded in parallel. This means it is impossible to
6815 interrogate a target from GN code for any information not derivable from its
6816 label (see "gn help label"). The exception is the get_target_outputs()
6817 function which requires the target being interrogated to have been defined
6818 previously in the same file.
6819
6820 Targets are declared by their type and given a name:
6821
6822 static_library("my_static_library") {
6823 ... target parameter definitions ...
6824 }
6825
6826 There is also a generic "target" function for programmatically defined types
6827 (see "gn help target"). You can define new types using templates (see "gn
6828 help template"). A template defines some custom code that expands to one or
6829 more other targets.
6830
6831 Before executing the code inside the target's { }, the target defaults are
6832 applied (see "gn help set_defaults"). It will inject implicit variable
6833 definitions that can be overridden by the target code as necessary. Typically
6834 this mechanism is used to inject a default set of configs that define the
6835 global compiler and linker flags.
6836```
6837
6838#### **Which targets are built**
6839
6840```
6841 All targets encountered in the default toolchain (see "gn help toolchain")
6842 will have build rules generated for them, even if no other targets reference
6843 them. Their dependencies must resolve and they will be added to the implicit
6844 "all" rule (see "gn help ninja_rules").
6845
6846 Targets in non-default toolchains will only be generated when they are
6847 required (directly or transitively) to build a target in the default
6848 toolchain.
6849
6850 See also "gn help ninja_rules".
6851```
6852
6853#### **Dependencies**
6854
6855```
6856 The only difference between "public_deps" and "deps" except for pushing
6857 configs around the build tree and allowing includes for the purposes of "gn
6858 check".
6859
6860 A target's "data_deps" are guaranteed to be built whenever the target is
6861 built, but the ordering is not defined. The meaning of this is dependencies
6862 required at runtime. Currently data deps will be complete before the target
6863 is linked, but this is not semantically guaranteed and this is undesirable
6864 from a build performance perspective. Since we hope to change this in the
6865 future, do not rely on this behavior.
6866```
6867### **Language and grammar for GN build files**
6868
6869#### **Tokens**
6870
6871```
6872 GN build files are read as sequences of tokens. While splitting the file
6873 into tokens, the next token is the longest sequence of characters that form a
6874 valid token.
6875```
6876
6877#### **White space and comments**
6878
6879```
6880 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
6881 carriage returns (U+000D), and newlines (U+000A).
6882
6883 Comments start at the character "#" and stop at the next newline.
6884
6885 White space and comments are ignored except that they may separate tokens
6886 that would otherwise combine into a single token.
6887```
6888
6889#### **Identifiers**
6890
6891```
6892 Identifiers name variables and functions.
6893
6894 identifier = letter { letter | digit } .
6895 letter = "A" ... "Z" | "a" ... "z" | "_" .
6896 digit = "0" ... "9" .
6897```
6898
6899#### **Keywords**
6900
6901```
6902 The following keywords are reserved and may not be used as identifiers:
6903
6904 else false if true
6905```
6906
6907#### **Integer literals**
6908
6909```
6910 An integer literal represents a decimal integer value.
6911
6912 integer = [ "-" ] digit { digit } .
6913
6914 Leading zeros and negative zero are disallowed.
6915```
6916
6917#### **String literals**
6918
6919```
6920 A string literal represents a string value consisting of the quoted
6921 characters with possible escape sequences and variable expansions.
6922
6923 string = `"` { char | escape | expansion } `"` .
6924 escape = `\` ( "$" | `"` | char ) .
Nico Weberad9eab22020-11-15 22:20:28 -05006925 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" .
Brett Wilson796ed472018-07-16 15:11:09 -07006926 Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f]
6927 expansion = "$" ( identifier | BracketExpansion | Hex ) .
Nico Weberad9eab22020-11-15 22:20:28 -05006928 char = /* any character except "$", `"`, or newline */ .
Brett Wilson796ed472018-07-16 15:11:09 -07006929
6930 After a backslash, certain sequences represent special characters:
6931
6932 \" U+0022 quotation mark
6933 \$ U+0024 dollar sign
6934 \\ U+005C backslash
6935
6936 All other backslashes represent themselves.
6937
6938 To insert an arbitrary byte value, use $0xFF. For example, to insert a
6939 newline character: "Line one$0x0ALine two".
6940
6941 An expansion will evaluate the variable following the '$' and insert a
6942 stringified version of it into the result. For example, to concat two path
6943 components with a slash separating them:
6944 "$var_one/$var_two"
6945 Use the "${var_one}" format to be explicitly deliniate the variable for
6946 otherwise-ambiguous cases.
6947```
6948
6949#### **Punctuation**
6950
6951```
6952 The following character sequences represent punctuation:
6953
6954 + += == != ( )
6955 - -= < <= [ ]
6956 ! = > >= { }
6957 && || . ,
6958```
6959
6960#### **Grammar**
6961
6962```
6963 The input tokens form a syntax tree following a context-free grammar:
6964
6965 File = StatementList .
6966
6967 Statement = Assignment | Call | Condition .
6968 LValue = identifier | ArrayAccess | ScopeAccess .
6969 Assignment = LValue AssignOp Expr .
6970 Call = identifier "(" [ ExprList ] ")" [ Block ] .
6971 Condition = "if" "(" Expr ")" Block
6972 [ "else" ( Condition | Block ) ] .
6973 Block = "{" StatementList "}" .
6974 StatementList = { Statement } .
6975
6976 ArrayAccess = identifier "[" Expr "]" .
6977 ScopeAccess = identifier "." identifier .
6978 Expr = UnaryExpr | Expr BinaryOp Expr .
6979 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
6980 PrimaryExpr = identifier | integer | string | Call
6981 | ArrayAccess | ScopeAccess | Block
6982 | "(" Expr ")"
6983 | "[" [ ExprList [ "," ] ] "]" .
6984 ExprList = Expr { "," Expr } .
6985
6986 AssignOp = "=" | "+=" | "-=" .
6987 UnaryOp = "!" .
6988 BinaryOp = "+" | "-" // highest priority
6989 | "<" | "<=" | ">" | ">="
6990 | "==" | "!="
6991 | "&&"
6992 | "||" . // lowest priority
6993
6994 All binary operators are left-associative.
6995```
6996
6997#### **Types**
6998
6999```
7000 The GN language is dynamically typed. The following types are used:
7001
7002 - Boolean: Uses the keywords "true" and "false". There is no implicit
7003 conversion between booleans and integers.
7004
7005 - Integers: All numbers in GN are signed 64-bit integers.
7006
7007 - Strings: Strings are 8-bit with no enforced encoding. When a string is
7008 used to interact with other systems with particular encodings (like the
7009 Windows and Mac filesystems) it is assumed to be UTF-8. See "String
7010 literals" above for more.
7011
7012 - Lists: Lists are arbitrary-length ordered lists of values. See "Lists"
7013 below for more.
7014
7015 - Scopes: Scopes are like dictionaries that use variable names for keys. See
7016 "Scopes" below for more.
7017```
7018
7019#### **Lists**
7020
7021```
7022 Lists are created with [] and using commas to separate items:
7023
7024 mylist = [ 0, 1, 2, "some string" ]
7025
7026 A comma after the last item is optional. Lists are dereferenced using 0-based
7027 indexing:
7028
7029 mylist[0] += 1
7030 var = mylist[2]
7031
7032 Lists can be concatenated using the '+' and '+=' operators. Bare values can
7033 not be concatenated with lists, to add a single item, it must be put into a
7034 list of length one.
7035
7036 Items can be removed from lists using the '-' and '-=' operators. This will
7037 remove all occurrences of every item in the right-hand list from the
7038 left-hand list. It is an error to remove an item not in the list. This is to
7039 prevent common typos and to detect dead code that is removing things that no
7040 longer apply.
7041
7042 It is an error to use '=' to replace a nonempty list with another nonempty
7043 list. This is to prevent accidentally overwriting data when in most cases
7044 '+=' was intended. To overwrite a list on purpose, first assign it to the
7045 empty list:
7046
7047 mylist = []
7048 mylist = otherlist
Brett Wilson796ed472018-07-16 15:11:09 -07007049```
7050
7051#### **Scopes**
7052
7053```
7054 All execution happens in the context of a scope which holds the current state
7055 (like variables). With the exception of loops and conditions, '{' introduces
7056 a new scope that has a parent reference to the old scope.
7057
7058 Variable reads recursively search all nested scopes until the variable is
7059 found or there are no more scopes. Variable writes always go into the current
7060 scope. This means that after the closing '}' (again excepting loops and
7061 conditions), all local variables will be restored to the previous values.
7062 This also means that "foo = foo" can do useful work by copying a variable
7063 into the current scope that was defined in a containing scope.
7064
7065 Scopes can also be assigned to variables. Such scopes can be created by
7066 functions like exec_script, when invoking a template (the template code
7067 refers to the variables set by the invoking code by the implicitly-created
7068 "invoker" scope), or explicitly like:
7069
7070 empty_scope = {}
7071 myvalues = {
7072 foo = 21
7073 bar = "something"
7074 }
7075
7076 Inside such a scope definition can be any GN code including conditionals and
7077 function calls. After the close of the scope, it will contain all variables
7078 explicitly set by the code contained inside it. After this, the values can be
7079 read, modified, or added to:
7080
7081 myvalues.foo += 2
7082 empty_scope.new_thing = [ 1, 2, 3 ]
Nate Fischer8ed01d32019-01-08 17:32:01 -08007083
7084 Scope equality is defined as single-level scopes identical within the current
7085 scope. That is, all values in the first scope must be present and identical
7086 within the second, and vice versa. Note that this means inherited scopes are
7087 always unequal by definition.
Brett Wilson796ed472018-07-16 15:11:09 -07007088```
Julie Hockettd69a9c32019-01-23 14:36:18 -08007089### **Input and output conversion**
Brett Wilson796ed472018-07-16 15:11:09 -07007090
7091```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007092 Input and output conversions are arguments to file and process functions
7093 that specify how to convert data to or from external formats. The possible
7094 values for parameters specifying conversions are:
7095
Brett Wilson796ed472018-07-16 15:11:09 -07007096 "" (the default)
Julie Hockett09171292018-07-31 14:35:10 -07007097 input: Discard the result and return None.
7098
7099 output: If value is a list, then "list lines"; otherwise "value".
Brett Wilson796ed472018-07-16 15:11:09 -07007100
7101 "list lines"
Julie Hockett09171292018-07-31 14:35:10 -07007102 input:
7103 Return the file contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007104 newlines will not be present in the result. The last line may or may
7105 not end in a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007106
Julie Hockett09171292018-07-31 14:35:10 -07007107 After splitting, each individual line will be trimmed of whitespace on
7108 both ends.
7109
7110 output:
7111 Renders the value contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007112 newlines will not be present in the result. The last line will end in
7113 with a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007114
7115 "scope"
Julie Hockett09171292018-07-31 14:35:10 -07007116 input:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007117 Execute the block as GN code and return a scope with the resulting
7118 values in it. If the input was:
Julie Hockett09171292018-07-31 14:35:10 -07007119 a = [ "hello.cc", "world.cc" ]
7120 b = 26
7121 and you read the result into a variable named "val", then you could
7122 access contents the "." operator on "val":
7123 sources = val.a
7124 some_count = val.b
7125
7126 output:
7127 Renders the value contents as a GN code block, reversing the input
7128 result above.
Brett Wilson796ed472018-07-16 15:11:09 -07007129
7130 "string"
Julie Hockett09171292018-07-31 14:35:10 -07007131 input: Return the file contents into a single string.
7132
7133 output:
7134 Render the value contents into a single string. The output is:
7135 a string renders with quotes, e.g. "str"
7136 an integer renders as a stringified integer, e.g. "6"
7137 a boolean renders as the associated string, e.g. "true"
7138 a list renders as a representation of its contents, e.g. "[\"str\", 6]"
7139 a scope renders as a GN code block of its values. If the Value was:
7140 Value val;
7141 val.a = [ "hello.cc", "world.cc" ];
7142 val.b = 26
7143 the resulting output would be:
7144 "{
7145 a = [ \"hello.cc\", \"world.cc\" ]
7146 b = 26
7147 }"
Brett Wilson796ed472018-07-16 15:11:09 -07007148
7149 "value"
Julie Hockett09171292018-07-31 14:35:10 -07007150 input:
7151 Parse the input as if it was a literal rvalue in a buildfile. Examples of
7152 typical program output using this mode:
7153 [ "foo", "bar" ] (result will be a list)
7154 or
7155 "foo bar" (result will be a string)
7156 or
7157 5 (result will be an integer)
Brett Wilson796ed472018-07-16 15:11:09 -07007158
Julie Hockett09171292018-07-31 14:35:10 -07007159 Note that if the input is empty, the result will be a null value which
7160 will produce an error if assigned to a variable.
7161
7162 output:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007163 Render the value contents as a literal rvalue. Strings render with
7164 escaped quotes.
Brett Wilson796ed472018-07-16 15:11:09 -07007165
7166 "json"
Julie Hockett09171292018-07-31 14:35:10 -07007167 input: Parse the input as a JSON and convert it to equivalent GN rvalue.
7168
7169 output: Convert the Value to equivalent JSON value.
7170
7171 The data type mapping is:
Brett Wilson796ed472018-07-16 15:11:09 -07007172 a string in JSON maps to string in GN
7173 an integer in JSON maps to integer in GN
7174 a float in JSON is unsupported and will result in an error
7175 an object in JSON maps to scope in GN
7176 an array in JSON maps to list in GN
7177 a boolean in JSON maps to boolean in GN
7178 a null in JSON is unsupported and will result in an error
7179
Julie Hockett09171292018-07-31 14:35:10 -07007180 Nota that the input dictionary keys have to be valid GN identifiers
7181 otherwise they will produce an error.
Brett Wilson796ed472018-07-16 15:11:09 -07007182
Julie Hockett09171292018-07-31 14:35:10 -07007183 "trim ..." (input only)
Brett Wilson796ed472018-07-16 15:11:09 -07007184 Prefixing any of the other transformations with the word "trim" will
7185 result in whitespace being trimmed from the beginning and end of the
7186 result before processing.
7187
7188 Examples: "trim string" or "trim list lines"
7189
7190 Note that "trim value" is useless because the value parser skips
7191 whitespace anyway.
7192```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01007193### **File patterns**
7194
7195```
7196 File patterns are VERY limited regular expressions. They must match the
7197 entire input string to be counted as a match. In regular expression parlance,
7198 there is an implicit "^...$" surrounding your input. If you want to match a
7199 substring, you need to use wildcards at the beginning and end.
7200
7201 There are only two special tokens understood by the pattern matcher.
7202 Everything else is a literal.
7203
7204 - "*" Matches zero or more of any character. It does not depend on the
7205 preceding character (in regular expression parlance it is equivalent to
7206 ".*").
7207
7208 - "\b" Matches a path boundary. This will match the beginning or end of a
7209 string, or a slash.
7210```
7211
7212#### **Pattern examples**
7213
7214```
7215 "*asdf*"
7216 Matches a string containing "asdf" anywhere.
7217
7218 "asdf"
7219 Matches only the exact string "asdf".
7220
7221 "*.cc"
7222 Matches strings ending in the literal ".cc".
7223
7224 "\bwin/*"
7225 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
7226```
Brett Wilson796ed472018-07-16 15:11:09 -07007227### **Label patterns**
7228
7229```
7230 A label pattern is a way of expressing one or more labels in a portion of the
7231 source tree. They are not general regular expressions.
7232
7233 They can take the following forms only:
7234
7235 - Explicit (no wildcard):
7236 "//foo/bar:baz"
7237 ":baz"
7238
7239 - Wildcard target names:
7240 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
7241 ":*" (all targets in the current build file)
7242
7243 - Wildcard directory names ("*" is only supported at the end)
7244 "*" (all targets)
7245 "//foo/bar/*" (all targets in any subdir of //foo/bar)
7246 "./*" (all targets in the current build file or sub dirs)
7247
Julie Hockette2a29402018-07-31 10:11:42 -07007248 Any of the above forms can additionally take an explicit toolchain
7249 in parenthesis at the end of the label pattern. In this case, the
7250 toolchain must be fully qualified (no wildcards are supported in the
7251 toolchain name).
Brett Wilson796ed472018-07-16 15:11:09 -07007252
Julie Hockette2a29402018-07-31 10:11:42 -07007253 "//foo:bar(//build/toolchain:mac)"
Brett Wilson796ed472018-07-16 15:11:09 -07007254 An explicit target in an explicit toolchain.
7255
7256 ":*(//build/toolchain/linux:32bit)"
7257 All targets in the current build file using the 32-bit Linux toolchain.
7258
7259 "//foo/*(//build/toolchain:win)"
7260 All targets in //foo and any subdirectory using the Windows
7261 toolchain.
7262```
7263### **About labels**
7264
7265```
7266 Everything that can participate in the dependency graph (targets, configs,
7267 and toolchains) are identified by labels. A common label looks like:
7268
7269 //base/test:test_support
7270
7271 This consists of a source-root-absolute path, a colon, and a name. This means
7272 to look for the thing named "test_support" in "base/test/BUILD.gn".
7273
7274 You can also specify system absolute paths if necessary. Typically such
7275 paths would be specified via a build arg so the developer can specify where
7276 the component is on their system.
7277
7278 /usr/local/foo:bar (Posix)
7279 /C:/Program Files/MyLibs:bar (Windows)
7280```
7281
7282#### **Toolchains**
7283
7284```
7285 A canonical label includes the label of the toolchain being used. Normally,
7286 the toolchain label is implicitly inherited from the current execution
7287 context, but you can override this to specify cross-toolchain dependencies:
7288
7289 //base/test:test_support(//build/toolchain/win:msvc)
7290
7291 Here GN will look for the toolchain definition called "msvc" in the file
7292 "//build/toolchain/win" to know how to compile this target.
7293```
7294
7295#### **Relative labels**
7296
7297```
7298 If you want to refer to something in the same buildfile, you can omit
7299 the path name and just start with a colon. This format is recommended for
7300 all same-file references.
7301
7302 :base
7303
7304 Labels can be specified as being relative to the current directory.
7305 Stylistically, we prefer to use absolute paths for all non-file-local
7306 references unless a build file needs to be run in different contexts (like a
7307 project needs to be both standalone and pulled into other projects in
7308 difference places in the directory hierarchy).
7309
7310 source/plugin:myplugin
7311 ../net:url_request
7312```
7313
7314#### **Implicit names**
7315
7316```
7317 If a name is unspecified, it will inherit the directory name. Stylistically,
7318 we prefer to omit the colon and name when possible:
7319
7320 //net -> //net:net
7321 //tools/gn -> //tools/gn:gn
7322```
Julie Hockett152c5142019-07-12 09:53:43 -06007323### **Metadata Collection**
7324
7325```
7326 Metadata is information attached to targets throughout the dependency tree. GN
7327 allows for the collection of this data into files written during the generation
Keir Mierle45611e32019-11-12 11:18:00 -08007328 step, enabling users to expose and aggregate this data based on the dependency
Julie Hockett152c5142019-07-12 09:53:43 -06007329 tree.
7330```
7331
7332#### **generated_file targets**
7333
7334```
7335 Similar to the write_file() function, the generated_file target type
7336 creates a file in the specified location with the specified content. The
Keir Mierle45611e32019-11-12 11:18:00 -08007337 primary difference between write_file() and this target type is that the
Julie Hockett152c5142019-07-12 09:53:43 -06007338 write_file function does the file write at parse time, while the
7339 generated_file target type writes at target resolution time. See
7340 "gn help generated_file" for more detail.
7341
Keir Mierle45611e32019-11-12 11:18:00 -08007342 When written at target resolution time, generated_file enables GN to
Julie Hockett152c5142019-07-12 09:53:43 -06007343 collect and write aggregated metadata from dependents.
7344
Keir Mierle45611e32019-11-12 11:18:00 -08007345 A generated_file target can declare either 'contents' to write statically
7346 known contents to a file or 'data_keys' to aggregate metadata and write the
7347 result to a file. It can also specify 'walk_keys' (to restrict the metadata
Julie Hockett152c5142019-07-12 09:53:43 -06007348 collection), 'output_conversion', and 'rebase'.
7349```
7350
7351#### **Collection and Aggregation**
7352
7353```
7354 Targets can declare a 'metadata' variable containing a scope, and this
Keir Mierle45611e32019-11-12 11:18:00 -08007355 metadata may be collected and written out to a file specified by
7356 generated_file aggregation targets. The 'metadata' scope must contain
7357 only list values since the aggregation step collects a list of these values.
Julie Hockett152c5142019-07-12 09:53:43 -06007358
7359 During the target resolution, generated_file targets will walk their
7360 dependencies recursively, collecting metadata based on the specified
7361 'data_keys'. 'data_keys' is specified as a list of strings, used by the walk
7362 to identify which variables in dependencies' 'metadata' scopes to collect.
7363
Keir Mierle45611e32019-11-12 11:18:00 -08007364 The walk begins with the listed dependencies of the 'generated_file' target.
7365 The 'metadata' scope for each dependency is inspected for matching elements
7366 of the 'generated_file' target's 'data_keys' list. If a match is found, the
7367 data from the dependent's matching key list is appended to the aggregate walk
7368 list. Note that this means that if more than one walk key is specified, the
7369 data in all of them will be aggregated into one list. From there, the walk
7370 will then recurse into the dependencies of each target it encounters,
7371 collecting the specified metadata for each.
Julie Hockett152c5142019-07-12 09:53:43 -06007372
7373 For example:
7374
7375 group("a") {
7376 metadata = {
7377 doom_melon = [ "enable" ]
7378 my_files = [ "foo.cpp" ]
7379 my_extra_files = [ "bar.cpp" ]
7380 }
7381
7382 deps = [ ":b" ]
7383 }
7384
7385 group("b") {
7386 metadata = {
7387 my_files = [ "baz.cpp" ]
7388 }
7389 }
7390
7391 generated_file("metadata") {
7392 outputs = [ "$root_build_dir/my_files.json" ]
7393 data_keys = [ "my_files", "my_extra_files" ]
7394
7395 deps = [ ":a" ]
7396 }
7397
7398 The above will produce the following file data:
7399
7400 foo.cpp
7401 bar.cpp
7402 baz.cpp
7403
Keir Mierle45611e32019-11-12 11:18:00 -08007404 The dependency walk can be limited by using the 'walk_keys'. This is a list of
Julie Hockett152c5142019-07-12 09:53:43 -06007405 labels that should be included in the walk. All labels specified here should
7406 also be in one of the deps lists. These keys act as barriers, where the walk
Keir Mierle45611e32019-11-12 11:18:00 -08007407 will only recurse into the targets listed. An empty list in all specified
Julie Hockett152c5142019-07-12 09:53:43 -06007408 barriers will end that portion of the walk.
7409
7410 group("a") {
7411 metadata = {
7412 my_files = [ "foo.cpp" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02007413 my_files_barrier = [ ":b" ]
Julie Hockett152c5142019-07-12 09:53:43 -06007414 }
7415
7416 deps = [ ":b", ":c" ]
7417 }
7418
7419 group("b") {
7420 metadata = {
7421 my_files = [ "bar.cpp" ]
7422 }
7423 }
7424
7425 group("c") {
7426 metadata = {
7427 my_files = [ "doom_melon.cpp" ]
7428 }
7429 }
7430
7431 generated_file("metadata") {
7432 outputs = [ "$root_build_dir/my_files.json" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02007433 data_keys = [ "my_files" ]
7434 walk_keys = [ "my_files_barrier" ]
Julie Hockett152c5142019-07-12 09:53:43 -06007435
7436 deps = [ ":a" ]
7437 }
7438
7439 The above will produce the following file data (note that `doom_melon.cpp` is
7440 not included):
7441
7442 foo.cpp
7443 bar.cpp
7444
7445 A common example of this sort of barrier is in builds that have host tools
7446 built as part of the tree, but do not want the metadata from those host tools
7447 to be collected with the target-side code.
7448```
7449
7450#### **Common Uses**
7451
7452```
7453 Metadata can be used to collect information about the different targets in the
7454 build, and so a common use is to provide post-build tooling with a set of data
7455 necessary to do aggregation tasks. For example, if each test target specifies
7456 the output location of its binary to run in a metadata field, that can be
7457 collected into a single file listing the locations of all tests in the
7458 dependency tree. A local build tool (or continuous integration infrastructure)
7459 can then use that file to know which tests exist, and where, and run them
7460 accordingly.
7461
7462 Another use is in image creation, where a post-build image tool needs to know
7463 various pieces of information about the components it should include in order
7464 to put together the correct image.
7465```
Brett Wilson796ed472018-07-16 15:11:09 -07007466### **Ninja build rules**
7467
7468#### **The "all" and "default" rules**
7469
7470```
7471 All generated targets (see "gn help execution") will be added to an implicit
7472 build rule called "all" so "ninja all" will always compile everything. The
7473 default rule will be used by Ninja if no specific target is specified (just
7474 typing "ninja"). If there is a target named "default" in the root build file,
7475 it will be the default build rule, otherwise the implicit "all" rule will be
7476 used.
7477```
7478
7479#### **Phony rules**
7480
7481```
7482 GN generates Ninja "phony" rules for targets in the default toolchain. The
7483 phony rules can collide with each other and with the names of generated files
7484 so are generated with the following priority:
7485
7486 1. Actual files generated by the build always take precedence.
7487
7488 2. Targets in the toplevel //BUILD.gn file.
7489
7490 3. Targets in toplevel directories matching the names of the directories.
7491 So "ninja foo" can be used to compile "//foo:foo". This only applies to
7492 the first level of directories since usually these are the most
7493 important (so this won't apply to "//foo/bar:bar").
7494
7495 4. The short names of executables if there is only one executable with that
7496 short name. Use "ninja doom_melon" to compile the
7497 "//tools/fruit:doom_melon" executable.
7498
7499 5. The short names of all targets if there is only one target with that
7500 short name.
7501
7502 6. Full label name with no leading slashes. So you can use
7503 "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon".
7504
7505 7. Labels with an implicit name part (when the short names match the
7506 directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar".
7507
7508 These "phony" rules are provided only for running Ninja since this matches
7509 people's historical expectations for building. For consistency with the rest
7510 of the program, GN introspection commands accept explicit labels.
7511
7512 To explicitly compile a target in a non-default toolchain, you must give
7513 Ninja the exact name of the output file relative to the build directory.
7514```
7515### **nogncheck**: Skip an include line from checking.
7516
7517```
7518 GN's header checker helps validate that the includes match the build
7519 dependency graph. Sometimes an include might be conditional or otherwise
7520 problematic, but you want to specifically allow it. In this case, it can be
7521 whitelisted.
7522
7523 Include lines containing the substring "nogncheck" will be excluded from
7524 header checking. The most common case is a conditional include:
7525
7526 #if defined(ENABLE_DOOM_MELON)
7527 #include "tools/doom_melon/doom_melon.h" // nogncheck
7528 #endif
7529
7530 If the build file has a conditional dependency on the corresponding target
7531 that matches the conditional include, everything will always link correctly:
7532
7533 source_set("mytarget") {
7534 ...
7535 if (enable_doom_melon) {
7536 defines = [ "ENABLE_DOOM_MELON" ]
7537 deps += [ "//tools/doom_melon" ]
7538 }
7539
7540 But GN's header checker does not understand preprocessor directives, won't
7541 know it matches the build dependencies, and will flag this include as
7542 incorrect when the condition is false.
7543```
7544
7545#### **More information**
7546
7547```
7548 The topic "gn help check" has general information on how checking works and
7549 advice on fixing problems. Targets can also opt-out of checking, see
7550 "gn help check_includes".
7551```
Brett Wilson796ed472018-07-16 15:11:09 -07007552### **Runtime dependencies**
7553
7554```
7555 Runtime dependencies of a target are exposed via the "runtime_deps" category
7556 of "gn desc" (see "gn help desc") or they can be written at build generation
7557 time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help
7558 --runtime-deps-list-file").
7559
7560 To a first approximation, the runtime dependencies of a target are the set of
7561 "data" files, data directories, and the shared libraries from all transitive
7562 dependencies. Executables, shared libraries, and loadable modules are
7563 considered runtime dependencies of themselves.
7564```
7565
7566#### **Executables**
7567
7568```
7569 Executable targets and those executable targets' transitive dependencies are
7570 not considered unless that executable is listed in "data_deps". Otherwise, GN
7571 assumes that the executable (and everything it requires) is a build-time
7572 dependency only.
7573```
7574
7575#### **Actions and copies**
7576
7577```
7578 Action and copy targets that are listed as "data_deps" will have all of their
7579 outputs and data files considered as runtime dependencies. Action and copy
7580 targets that are "deps" or "public_deps" will have only their data files
7581 considered as runtime dependencies. These targets can list an output file in
7582 both the "outputs" and "data" lists to force an output file as a runtime
7583 dependency in all cases.
7584
7585 The different rules for deps and data_deps are to express build-time (deps)
7586 vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as
7587 data dependencies, there would be a lot of extra stuff, and if GN counted all
7588 run-time dependencies as regular deps, the build's parallelism would be
7589 unnecessarily constrained.
7590
7591 This rule can sometimes lead to unintuitive results. For example, given the
7592 three targets:
7593 A --[data_deps]--> B --[deps]--> ACTION
7594 GN would say that A does not have runtime deps on the result of the ACTION,
7595 which is often correct. But the purpose of the B target might be to collect
7596 many actions into one logic unit, and the "data"-ness of A's dependency is
7597 lost. Solutions:
7598
7599 - List the outputs of the action in its data section (if the results of
7600 that action are always runtime files).
7601 - Have B list the action in data_deps (if the outputs of the actions are
7602 always runtime files).
7603 - Have B list the action in both deps and data deps (if the outputs might be
7604 used in both contexts and you don't care about unnecessary entries in the
7605 list of files required at runtime).
7606 - Split B into run-time and build-time versions with the appropriate "deps"
7607 for each.
7608```
7609
7610#### **Static libraries and source sets**
7611
7612```
7613 The results of static_library or source_set targets are not considered
7614 runtime dependencies since these are assumed to be intermediate targets only.
7615 If you need to list a static library as a runtime dependency, you can
7616 manually compute the .a/.lib file name for the current platform and list it
7617 in the "data" list of a target (possibly on the static library target
7618 itself).
7619```
7620
7621#### **Multiple outputs**
7622
7623```
7624 Linker tools can specify which of their outputs should be considered when
7625 computing the runtime deps by setting runtime_outputs. If this is unset on
7626 the tool, the default will be the first output only.
7627```
7628### **How Source Expansion Works**
7629
7630```
7631 Source expansion is used for the action_foreach and copy target types to map
7632 source file names to output file names or arguments.
7633
7634 To perform source expansion in the outputs, GN maps every entry in the
7635 sources to every entry in the outputs list, producing the cross product of
7636 all combinations, expanding placeholders (see below).
7637
7638 Source expansion in the args works similarly, but performing the placeholder
7639 substitution produces a different set of arguments for each invocation of the
7640 script.
7641
7642 If no placeholders are found, the outputs or args list will be treated as a
7643 static list of literal file names that do not depend on the sources.
7644
7645 See "gn help copy" and "gn help action_foreach" for more on how this is
7646 applied.
7647```
7648
7649#### **Placeholders**
7650
7651```
7652 This section discusses only placeholders for actions. There are other
7653 placeholders used in the definition of tools. See "gn help tool" for those.
7654
7655 {{source}}
7656 The name of the source file including directory (*). This will generally
7657 be used for specifying inputs to a script in the "args" variable.
7658 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
7659
7660 {{source_file_part}}
7661 The file part of the source including the extension.
7662 "//foo/bar/baz.txt" => "baz.txt"
7663
7664 {{source_name_part}}
7665 The filename part of the source file with no directory or extension. This
7666 will generally be used for specifying a transformation from a source file
7667 to a destination file with the same name but different extension.
7668 "//foo/bar/baz.txt" => "baz"
7669
7670 {{source_dir}}
7671 The directory (*) containing the source file with no trailing slash.
7672 "//foo/bar/baz.txt" => "../../foo/bar"
7673
7674 {{source_root_relative_dir}}
7675 The path to the source file's directory relative to the source root, with
7676 no leading "//" or trailing slashes. If the path is system-absolute,
7677 (beginning in a single slash) this will just return the path with no
7678 trailing slash. This value will always be the same, regardless of whether
7679 it appears in the "outputs" or "args" section.
7680 "//foo/bar/baz.txt" => "foo/bar"
7681
7682 {{source_gen_dir}}
7683 The generated file directory (*) corresponding to the source file's path.
7684 This will be different than the target's generated file directory if the
7685 source file is in a different directory than the BUILD.gn file.
7686 "//foo/bar/baz.txt" => "gen/foo/bar"
7687
7688 {{source_out_dir}}
7689 The object file directory (*) corresponding to the source file's path,
7690 relative to the build directory. this us be different than the target's
7691 out directory if the source file is in a different directory than the
7692 build.gn file.
7693 "//foo/bar/baz.txt" => "obj/foo/bar"
7694
7695 {{source_target_relative}}
7696 The path to the source file relative to the target's directory. This will
7697 generally be used for replicating the source directory layout in the
Robert Sesek252674d2019-05-10 14:08:52 -04007698 output directory. This can only be used in actions and bundle_data
7699 targets. It is an error to use in process_file_template where there is no
7700 "target".
Brett Wilson796ed472018-07-16 15:11:09 -07007701 "//foo/bar/baz.txt" => "baz.txt"
7702```
7703
7704#### **(*) Note on directories**
7705
7706```
7707 Paths containing directories (except the source_root_relative_dir) will be
7708 different depending on what context the expansion is evaluated in. Generally
7709 it should "just work" but it means you can't concatenate strings containing
7710 these values with reasonable results.
7711
7712 Details: source expansions can be used in the "outputs" variable, the "args"
7713 variable, and in calls to "process_file_template". The "args" are passed to a
7714 script which is run from the build directory, so these directories will
7715 relative to the build directory for the script to find. In the other cases,
7716 the directories will be source- absolute (begin with a "//") because the
7717 results of those expansions will be handled by GN internally.
7718```
7719
7720#### **Examples**
7721
7722```
7723 Non-varying outputs:
7724 action("hardcoded_outputs") {
7725 sources = [ "input1.idl", "input2.idl" ]
7726 outputs = [ "$target_out_dir/output1.dat",
7727 "$target_out_dir/output2.dat" ]
7728 }
7729 The outputs in this case will be the two literal files given.
7730
7731 Varying outputs:
7732 action_foreach("varying_outputs") {
7733 sources = [ "input1.idl", "input2.idl" ]
7734 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
7735 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
7736 }
7737 Performing source expansion will result in the following output names:
7738 //out/Debug/obj/mydirectory/input1.h
7739 //out/Debug/obj/mydirectory/input1.cc
7740 //out/Debug/obj/mydirectory/input2.h
7741 //out/Debug/obj/mydirectory/input2.cc
7742```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007743### **Available global switches**
Brett Wilson796ed472018-07-16 15:11:09 -07007744
7745```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007746 Do "gn help --the_switch_you_want_help_on" for more. Individual commands may
7747 take command-specific switches not listed here. See the help on your specific
7748 command for more.
7749```
7750```
7751 * --args: Specifies build arguments overrides.
7752 * --color: Force colored output.
7753 * --dotfile: Override the name of the ".gn" file.
7754 * --fail-on-unused-args: Treat unused build args as fatal errors.
7755 * --markdown: Write help output in the Markdown format.
7756 * --nocolor: Force non-colored output.
7757 * -q: Quiet mode. Don't print output on success.
7758 * --root: Explicitly specify source root.
Nico Weberad9eab22020-11-15 22:20:28 -05007759 * --root-target: Override the root target.
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007760 * --runtime-deps-list-file: Save runtime dependencies for targets in file.
7761 * --script-executable: Set the executable used to execute scripts.
7762 * --threads: Specify number of worker threads.
7763 * --time: Outputs a summary of how long everything took.
7764 * --tracelog: Writes a Chrome-compatible trace log to the given file.
7765 * -v: Verbose logging.
7766 * --version: Prints the GN version number and exits.
Brett Wilson796ed472018-07-16 15:11:09 -07007767```
7768