blob: 09417a546e553bac7889fea32fb44caab775f526 [file] [log] [blame] [view]
Brett Wilson796ed472018-07-16 15:11:09 -07001# GN Reference
2
3*This page is automatically generated from* `gn help --markdown all`.
4
5## Contents
6
7* [Commands](#commands)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008 * [analyze: Analyze which targets are affected by a list of files.](#cmd_analyze)
9 * [args: Display or configure arguments declared by the build.](#cmd_args)
10 * [check: Check header dependencies.](#cmd_check)
11 * [clean: Cleans the output directory.](#cmd_clean)
RJ Ascani4e260f12020-10-19 17:41:51 -070012 * [clean_stale: Cleans the stale output files from the output directory.](#cmd_clean_stale)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080013 * [desc: Show lots of insightful information about a target or config.](#cmd_desc)
Julie Hockettd69a9c32019-01-23 14:36:18 -080014 * [format: Format .gn files.](#cmd_format)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080015 * [gen: Generate ninja files.](#cmd_gen)
16 * [help: Does what you think.](#cmd_help)
17 * [ls: List matching targets.](#cmd_ls)
18 * [meta: List target metadata collection results.](#cmd_meta)
Brett Wilson74397092020-03-18 16:34:14 -070019 * [outputs: Which files a source/target make.](#cmd_outputs)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080020 * [path: Find paths between two targets.](#cmd_path)
21 * [refs: Find stuff referencing a target or file.](#cmd_refs)
Brett Wilson796ed472018-07-16 15:11:09 -070022* [Target declarations](#targets)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080023 * [action: Declare a target that runs a script a single time.](#func_action)
24 * [action_foreach: Declare a target that runs a script over a set of files.](#func_action_foreach)
25 * [bundle_data: [iOS/macOS] Declare a target without output.](#func_bundle_data)
26 * [copy: Declare a target that copies files.](#func_copy)
27 * [create_bundle: [iOS/macOS] Build an iOS or macOS bundle.](#func_create_bundle)
28 * [executable: Declare an executable target.](#func_executable)
29 * [generated_file: Declare a generated_file target.](#func_generated_file)
30 * [group: Declare a named group of targets.](#func_group)
31 * [loadable_module: Declare a loadable module target.](#func_loadable_module)
Julie Hockettce1fa072019-05-07 17:44:37 -070032 * [rust_library: Declare a Rust library target.](#func_rust_library)
Petr Hosekfe36c7c2019-11-26 14:38:08 -080033 * [rust_proc_macro: Declare a Rust procedural macro target.](#func_rust_proc_macro)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080034 * [shared_library: Declare a shared library target.](#func_shared_library)
35 * [source_set: Declare a source set target.](#func_source_set)
36 * [static_library: Declare a static library target.](#func_static_library)
Antonio Sartoriecaaf4b2022-07-01 05:57:08 +000037 * [target: Declare a target with the given programmatic type.](#func_target)
Brett Wilson796ed472018-07-16 15:11:09 -070038* [Buildfile functions](#functions)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080039 * [assert: Assert an expression is true at generation time.](#func_assert)
40 * [config: Defines a configuration object.](#func_config)
41 * [declare_args: Declare build arguments.](#func_declare_args)
42 * [defined: Returns whether an identifier is defined.](#func_defined)
43 * [exec_script: Synchronously run a script and return the output.](#func_exec_script)
Sylvain Defresneaff489a2020-03-11 18:27:43 +010044 * [filter_exclude: Remove values that match a set of patterns.](#func_filter_exclude)
45 * [filter_include: Remove values that do not match a set of patterns.](#func_filter_include)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080046 * [foreach: Iterate over a list.](#func_foreach)
47 * [forward_variables_from: Copies variables from a different scope.](#func_forward_variables_from)
48 * [get_label_info: Get an attribute from a target's label.](#func_get_label_info)
49 * [get_path_info: Extract parts of a file or directory name.](#func_get_path_info)
50 * [get_target_outputs: [file list] Get the list of outputs from a target.](#func_get_target_outputs)
51 * [getenv: Get an environment variable.](#func_getenv)
52 * [import: Import a file into the current scope.](#func_import)
53 * [not_needed: Mark variables from scope as not needed.](#func_not_needed)
54 * [pool: Defines a pool object.](#func_pool)
55 * [print: Prints to the console.](#func_print)
Aaron Wood24062bb2022-04-25 20:37:48 -070056 * [print_stack_trace: Prints a stack trace.](#func_print_stack_trace)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080057 * [process_file_template: Do template expansion over a list of files.](#func_process_file_template)
58 * [read_file: Read a file into a variable.](#func_read_file)
59 * [rebase_path: Rebase a file or directory to another location.](#func_rebase_path)
60 * [set_default_toolchain: Sets the default toolchain name.](#func_set_default_toolchain)
61 * [set_defaults: Set default values for a target type.](#func_set_defaults)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080062 * [split_list: Splits a list into N different sub-lists.](#func_split_list)
Keir Mierle6ae63302019-11-08 23:02:18 -080063 * [string_join: Concatenates a list of strings with a separator.](#func_string_join)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080064 * [string_replace: Replaces substring in the given string.](#func_string_replace)
Keir Mierle6ae63302019-11-08 23:02:18 -080065 * [string_split: Split string into a list of strings.](#func_string_split)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080066 * [template: Define a template rule.](#func_template)
67 * [tool: Specify arguments to a toolchain tool.](#func_tool)
68 * [toolchain: Defines a toolchain.](#func_toolchain)
69 * [write_file: Write a file to disk.](#func_write_file)
Brett Wilson796ed472018-07-16 15:11:09 -070070* [Built-in predefined variables](#predefined_variables)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080071 * [current_cpu: [string] The processor architecture of the current toolchain.](#var_current_cpu)
72 * [current_os: [string] The operating system of the current toolchain.](#var_current_os)
73 * [current_toolchain: [string] Label of the current toolchain.](#var_current_toolchain)
74 * [default_toolchain: [string] Label of the default toolchain.](#var_default_toolchain)
James Robinson61377e32020-02-13 15:20:07 -080075 * [gn_version: [number] The version of gn.](#var_gn_version)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080076 * [host_cpu: [string] The processor architecture that GN is running on.](#var_host_cpu)
77 * [host_os: [string] The operating system that GN is running on.](#var_host_os)
78 * [invoker: [string] The invoking scope inside a template.](#var_invoker)
79 * [python_path: [string] Absolute path of Python.](#var_python_path)
80 * [root_build_dir: [string] Directory where build commands are run.](#var_root_build_dir)
81 * [root_gen_dir: [string] Directory for the toolchain's generated files.](#var_root_gen_dir)
82 * [root_out_dir: [string] Root directory for toolchain output files.](#var_root_out_dir)
83 * [target_cpu: [string] The desired cpu architecture for the build.](#var_target_cpu)
84 * [target_gen_dir: [string] Directory for a target's generated files.](#var_target_gen_dir)
85 * [target_name: [string] The name of the current target.](#var_target_name)
86 * [target_os: [string] The desired operating system for the build.](#var_target_os)
87 * [target_out_dir: [string] Directory for target output files.](#var_target_out_dir)
Brett Wilson796ed472018-07-16 15:11:09 -070088* [Variables you set in targets](#target_variables)
Julie Hockettce1fa072019-05-07 17:44:37 -070089 * [aliased_deps: [scope] Set of crate-dependency pairs.](#var_aliased_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080090 * [all_dependent_configs: [label list] Configs to be forced on dependents.](#var_all_dependent_configs)
91 * [allow_circular_includes_from: [label list] Permit includes from deps.](#var_allow_circular_includes_from)
92 * [arflags: [string list] Arguments passed to static_library archiver.](#var_arflags)
93 * [args: [string list] Arguments passed to an action.](#var_args)
94 * [asmflags: [string list] Flags passed to the assembler.](#var_asmflags)
95 * [assert_no_deps: [label pattern list] Ensure no deps on these targets.](#var_assert_no_deps)
Sylvain Defresne89e64252020-08-07 13:01:06 +020096 * [bridge_header: [string] Path to C/Objective-C compatibility header.](#var_bridge_header)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -080097 * [bundle_contents_dir: Expansion of {{bundle_contents_dir}} in create_bundle.](#var_bundle_contents_dir)
98 * [bundle_deps_filter: [label list] A list of labels that are filtered out.](#var_bundle_deps_filter)
99 * [bundle_executable_dir: Expansion of {{bundle_executable_dir}} in create_bundle](#var_bundle_executable_dir)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800100 * [bundle_resources_dir: Expansion of {{bundle_resources_dir}} in create_bundle.](#var_bundle_resources_dir)
101 * [bundle_root_dir: Expansion of {{bundle_root_dir}} in create_bundle.](#var_bundle_root_dir)
102 * [cflags: [string list] Flags passed to all C compiler variants.](#var_cflags)
103 * [cflags_c: [string list] Flags passed to the C compiler.](#var_cflags_c)
104 * [cflags_cc: [string list] Flags passed to the C++ compiler.](#var_cflags_cc)
105 * [cflags_objc: [string list] Flags passed to the Objective C compiler.](#var_cflags_objc)
106 * [cflags_objcc: [string list] Flags passed to the Objective C++ compiler.](#var_cflags_objcc)
107 * [check_includes: [boolean] Controls whether a target's files are checked.](#var_check_includes)
108 * [code_signing_args: [string list] Arguments passed to code signing script.](#var_code_signing_args)
109 * [code_signing_outputs: [file list] Output files for code signing step.](#var_code_signing_outputs)
110 * [code_signing_script: [file name] Script for code signing.](#var_code_signing_script)
111 * [code_signing_sources: [file list] Sources for code signing step.](#var_code_signing_sources)
112 * [complete_static_lib: [boolean] Links all deps into a static library.](#var_complete_static_lib)
113 * [configs: [label list] Configs applying to this target or config.](#var_configs)
114 * [contents: Contents to write to file.](#var_contents)
Julie Hockettce1fa072019-05-07 17:44:37 -0700115 * [crate_name: [string] The name for the compiled crate.](#var_crate_name)
116 * [crate_root: [string] The root source file for a binary or library.](#var_crate_root)
117 * [crate_type: [string] The type of linkage to use on a shared_library.](#var_crate_type)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800118 * [data: [file list] Runtime data file dependencies.](#var_data)
119 * [data_deps: [label list] Non-linked dependencies.](#var_data_deps)
120 * [data_keys: [string list] Keys from which to collect metadata.](#var_data_keys)
121 * [defines: [string list] C preprocessor defines.](#var_defines)
122 * [depfile: [string] File name for input dependencies for actions.](#var_depfile)
123 * [deps: [label list] Private linked dependencies.](#var_deps)
Petr Hoseka1413862020-01-03 12:54:33 -0800124 * [externs: [scope] Set of Rust crate-dependency pairs.](#var_externs)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100125 * [framework_dirs: [directory list] Additional framework search directories.](#var_framework_dirs)
126 * [frameworks: [name list] Name of frameworks that must be linked.](#var_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800127 * [friend: [label pattern list] Allow targets to include private headers.](#var_friend)
Tyler Mandry4a648092022-02-15 19:47:09 +0000128 * [gen_deps: [label list] Declares targets that should generate when this one does.](#var_gen_deps)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800129 * [include_dirs: [directory list] Additional include directories.](#var_include_dirs)
130 * [inputs: [file list] Additional compile-time dependencies.](#var_inputs)
131 * [ldflags: [string list] Flags passed to the linker.](#var_ldflags)
132 * [lib_dirs: [directory list] Additional library directories.](#var_lib_dirs)
133 * [libs: [string list] Additional libraries to link.](#var_libs)
134 * [metadata: [scope] Metadata of this target.](#var_metadata)
Sylvain Defresne89e64252020-08-07 13:01:06 +0200135 * [module_name: [string] The name for the compiled module.](#var_module_name)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800136 * [output_conversion: Data format for generated_file targets.](#var_output_conversion)
137 * [output_dir: [directory] Directory to put output file in.](#var_output_dir)
138 * [output_extension: [string] Value to use for the output's file extension.](#var_output_extension)
139 * [output_name: [string] Name for the output file other than the default.](#var_output_name)
140 * [output_prefix_override: [boolean] Don't use prefix for output name.](#var_output_prefix_override)
141 * [outputs: [file list] Output files for actions and copy targets.](#var_outputs)
142 * [partial_info_plist: [filename] Path plist from asset catalog compiler.](#var_partial_info_plist)
Petr Hosek7a6231e2022-10-22 23:14:18 +0000143 * [pool: [string] Label of the pool used by binary targets and actions.](#var_pool)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800144 * [precompiled_header: [string] Header file to precompile.](#var_precompiled_header)
145 * [precompiled_header_type: [string] "gcc" or "msvc".](#var_precompiled_header_type)
146 * [precompiled_source: [file name] Source file to precompile.](#var_precompiled_source)
147 * [product_type: [string] Product type for Xcode projects.](#var_product_type)
148 * [public: [file list] Declare public header files for a target.](#var_public)
149 * [public_configs: [label list] Configs applied to dependents.](#var_public_configs)
150 * [public_deps: [label list] Declare public dependencies.](#var_public_deps)
151 * [rebase: [boolean] Rebase collected metadata as files.](#var_rebase)
152 * [response_file_contents: [string list] Contents of .rsp file for actions.](#var_response_file_contents)
153 * [script: [file name] Script file for actions.](#var_script)
154 * [sources: [file list] Source files for a target.](#var_sources)
Sylvain Defresne89e64252020-08-07 13:01:06 +0200155 * [swiftflags: [string list] Flags passed to the swift compiler.](#var_swiftflags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800156 * [testonly: [boolean] Declares a target must only be used for testing.](#var_testonly)
157 * [visibility: [label list] A list of labels that can depend on a target.](#var_visibility)
158 * [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys)
Robert Sesekd0a6f072020-05-15 11:21:22 -0400159 * [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800160 * [write_runtime_deps: Writes the target's runtime_deps to the given path.](#var_write_runtime_deps)
Harley Li0a9affb2020-06-03 10:38:42 -0400161 * [xcasset_compiler_flags: [string list] Flags passed to xcassets compiler](#var_xcasset_compiler_flags)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800162 * [xcode_extra_attributes: [scope] Extra attributes for Xcode projects.](#var_xcode_extra_attributes)
163 * [xcode_test_application_name: [string] Name for Xcode test target.](#var_xcode_test_application_name)
Brett Wilson796ed472018-07-16 15:11:09 -0700164* [Other help topics](#other)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800165 * all: Print all the help at once
Brett Wilson796ed472018-07-16 15:11:09 -0700166 * [buildargs: How build arguments work.](#buildargs)
167 * [dotfile: Info about the toplevel .gn file.](#dotfile)
168 * [execution: Build graph and execution overview.](#execution)
169 * [grammar: Language and grammar for GN build files.](#grammar)
Julie Hockett81ee1962019-05-13 11:24:42 -0700170 * [input_conversion: Processing input from exec_script and read_file.](#io_conversion)
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100171 * [file_pattern: Matching more than one file.](#file_pattern)
Brett Wilson796ed472018-07-16 15:11:09 -0700172 * [label_pattern: Matching more than one label.](#label_pattern)
173 * [labels: About labels.](#labels)
Julie Hockett152c5142019-07-12 09:53:43 -0600174 * [metadata_collection: About metadata and its collection.](#metadata_collection)
Brett Wilson796ed472018-07-16 15:11:09 -0700175 * [ninja_rules: How Ninja build rules are named.](#ninja_rules)
176 * [nogncheck: Annotating includes for checking.](#nogncheck)
Julie Hockett81ee1962019-05-13 11:24:42 -0700177 * [output_conversion: Specifies how to transform a value to output.](#io_conversion)
Brett Wilson796ed472018-07-16 15:11:09 -0700178 * [runtime_deps: How runtime dependency computation works.](#runtime_deps)
179 * [source_expansion: Map sources to outputs for scripts.](#source_expansion)
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800180 * [switches: Show available command-line switches.](#switch_list)
Brett Wilson796ed472018-07-16 15:11:09 -0700181
182## Commands
183
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800184### **gn analyze <out_dir> <input_path> <output_path>**
Brett Wilson796ed472018-07-16 15:11:09 -0700185
186```
187 Analyze which targets are affected by a list of files.
188
189 This command takes three arguments:
190
191 out_dir is the path to the build directory.
192
193 input_path is a path to a file containing a JSON object with three fields:
194
195 - "files": A list of the filenames to check.
196
197 - "test_targets": A list of the labels for targets that are needed to run
198 the tests we wish to run.
199
Nico Weberdba01722021-04-06 08:56:46 -0400200 - "additional_compile_targets" (optional): A list of the labels for targets
201 that we wish to rebuild, but aren't necessarily needed for testing. The
202 important difference between this field and "test_targets" is that if an
203 item in the additional_compile_targets list refers to a group, then any
Brett Wilson796ed472018-07-16 15:11:09 -0700204 dependencies of that group will be returned if they are out of date, but
205 the group itself does not need to be. If the dependencies themselves are
206 groups, the same filtering is repeated. This filtering can be used to
207 avoid rebuilding dependencies of a group that are unaffected by the input
208 files. The list may also contain the string "all" to refer to a
209 pseudo-group that contains every root target in the build graph.
210
211 This filtering behavior is also known as "pruning" the list of compile
212 targets.
213
Nico Weberdba01722021-04-06 08:56:46 -0400214 If "additional_compile_targets" is absent, it defaults to the empty list.
215
Keir Mierle45611e32019-11-12 11:18:00 -0800216 If input_path is -, input is read from stdin.
217
Brett Wilson796ed472018-07-16 15:11:09 -0700218 output_path is a path indicating where the results of the command are to be
219 written. The results will be a file containing a JSON object with one or more
220 of following fields:
221
222 - "compile_targets": A list of the labels derived from the input
223 compile_targets list that are affected by the input files. Due to the way
224 the filtering works for compile targets as described above, this list may
225 contain targets that do not appear in the input list.
226
227 - "test_targets": A list of the labels from the input test_targets list that
228 are affected by the input files. This list will be a proper subset of the
229 input list.
230
231 - "invalid_targets": A list of any names from the input that do not exist in
232 the build graph. If this list is non-empty, the "error" field will also be
233 set to "Invalid targets".
234
235 - "status": A string containing one of three values:
236
237 - "Found dependency"
238 - "No dependency"
Sylvain Defresne7b0690e2020-06-26 15:33:01 +0200239 - "Found dependency (all)"
Brett Wilson796ed472018-07-16 15:11:09 -0700240
241 In the first case, the lists returned in compile_targets and test_targets
242 should be passed to ninja to build. In the second case, nothing was
243 affected and no build is necessary. In the third case, GN could not
244 determine the correct answer and returned the input as the output in order
245 to be safe.
246
247 - "error": This will only be present if an error occurred, and will contain
248 a string describing the error. This includes cases where the input file is
249 not in the right format, or contains invalid targets.
250
Keir Mierle45611e32019-11-12 11:18:00 -0800251 If output_path is -, output is written to stdout.
252
Brett Wilson796ed472018-07-16 15:11:09 -0700253 The command returns 1 if it is unable to read the input file or write the
254 output file, or if there is something wrong with the build such that gen
255 would also fail, and 0 otherwise. In particular, it returns 0 even if the
256 "error" key is non-empty and a non-fatal error occurred. In other words, it
257 tries really hard to always write something to the output JSON and convey
258 errors that way rather than via return codes.
259```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800260### **gn args**: (command-line tool)
Brett Wilson796ed472018-07-16 15:11:09 -0700261
262```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800263 Display or configure arguments declared by the build.
264
265 gn args [--list] [--short] [--args] [--overrides-only]
266
Brett Wilson796ed472018-07-16 15:11:09 -0700267 See also "gn help buildargs" for a more high-level overview of how
268 build arguments work.
269```
270
271#### **Usage**
272
273```
274 gn args
275 Open the arguments for the given build directory in an editor. If the
276 given build directory doesn't exist, it will be created and an empty args
277 file will be opened in the editor. You would type something like this
278 into that file:
279 enable_doom_melon=false
280 os="android"
281
282 To find your editor on Posix, GN will search the environment variables in
283 order: GN_EDITOR, VISUAL, and EDITOR. On Windows GN will open the command
284 associated with .txt files.
285
286 Note: you can edit the build args manually by editing the file "args.gn"
287 in the build directory and then running "gn gen ".
288
289 gn args --list[=] [--short] [--overrides-only] [--json]
290 Lists all build arguments available in the current configuration, or, if
291 an exact_arg is specified for the list flag, just that one build
292 argument.
293
294 The output will list the declaration location, current value for the
295 build, default value (if different than the current value), and comment
296 preceding the declaration.
297
298 If --short is specified, only the names and current values will be
299 printed.
300
301 If --overrides-only is specified, only the names and current values of
302 arguments that have been overridden (i.e. non-default arguments) will
303 be printed. Overrides come from the /args.gn file and //.gn
304
305 If --json is specified, the output will be emitted in json format.
306 JSON schema for output:
307 [
308 {
309 "name": variable_name,
310 "current": {
311 "value": overridden_value,
312 "file": file_name,
313 "line": line_no
314 },
315 "default": {
316 "value": default_value,
317 "file": file_name,
318 "line": line_no
319 },
320 "comment": comment_string
321 },
322 ...
323 ]
324```
325
326#### **Examples**
327
328```
329 gn args out/Debug
330 Opens an editor with the args for out/Debug.
331
332 gn args out/Debug --list --short
333 Prints all arguments with their default values for the out/Debug
334 build.
335
336 gn args out/Debug --list --short --overrides-only
337 Prints overridden arguments for the out/Debug build.
338
339 gn args out/Debug --list=target_cpu
340 Prints information about the "target_cpu" argument for the "
341 "out/Debug
342 build.
343
344 gn args --list --args="os=\"android\" enable_doom_melon=true"
345 Prints all arguments with the default values for a build with the
346 given arguments set (which may affect the values of other
347 arguments).
348```
Joe Armstrong39413942019-03-15 10:34:03 +0800349### **gn check <out_dir> [<label_pattern>] [\--force] [\--check-generated]**
Brett Wilson796ed472018-07-16 15:11:09 -0700350
351```
352 GN's include header checker validates that the includes for C-like source
353 files match the build dependency graph.
354
355 "gn check" is the same thing as "gn gen" with the "--check" flag except that
356 this command does not write out any build files. It's intended to be an easy
357 way to manually trigger include file checking.
358
359 The can take exact labels or patterns that match more than
360 one (although not general regular expressions). If specified, only those
361 matching targets will be checked. See "gn help label_pattern" for details.
362```
363
364#### **Command-specific switches**
365
366```
Joe Armstrong39413942019-03-15 10:34:03 +0800367 --check-generated
368 Generated files are normally not checked since they do not exist
369 until after a build. With this flag, those generated files that
370 can be found on disk are also checked.
James Robinson49f59032020-01-08 14:05:51 -0800371
372 --check-system
373 Check system style includes (using ) in addition to
374 "double quote" includes.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400375
376 --default-toolchain
377 Normally wildcard targets are matched in all toolchains. This
378 switch makes wildcard labels with no explicit toolchain reference
379 only match targets in the default toolchain.
380
381 Non-wildcard inputs with no explicit toolchain specification will
382 always match only a target in the default toolchain if one exists.
383
384 --force
385 Ignores specifications of "check_includes = false" and checks all
386 target's files that match the target label.
Brett Wilson796ed472018-07-16 15:11:09 -0700387```
388
389#### **What gets checked**
390
391```
Nate Fischer8ed01d32019-01-08 17:32:01 -0800392 The .gn file may specify a list of targets to be checked in the list
Erik Staab5d109d72020-06-29 22:53:23 -0700393 check_targets (see "gn help dotfile"). Alternatively, the .gn file may
394 specify a list of targets not to be checked in no_check_targets. If a label
395 pattern is specified on the command line, neither check_targets or
396 no_check_targets is used.
Brett Wilson796ed472018-07-16 15:11:09 -0700397
398 Targets can opt-out from checking with "check_includes = false" (see
399 "gn help check_includes").
400
401 For targets being checked:
402
403 - GN opens all C-like source files in the targets to be checked and scans
404 the top for includes.
405
Joe Armstrong39413942019-03-15 10:34:03 +0800406 - Generated files (that might not exist yet) are ignored unless
407 the --check-generated flag is provided.
408
Brett Wilson796ed472018-07-16 15:11:09 -0700409 - Includes with a "nogncheck" annotation are skipped (see
410 "gn help nogncheck").
411
James Robinson49f59032020-01-08 14:05:51 -0800412 - Includes using "quotes" are always checked.
413 If system style checking is enabled, includes using
414 are also checked.
Brett Wilson796ed472018-07-16 15:11:09 -0700415
416 - Include paths are assumed to be relative to any of the "include_dirs" for
417 the target (including the implicit current dir).
418
419 - GN does not run the preprocessor so will not understand conditional
420 includes.
421
422 - Only includes matching known files in the build are checked: includes
423 matching unknown paths are ignored.
424
425 For an include to be valid:
426
427 - The included file must be in the current target, or there must be a path
428 following only public dependencies to a target with the file in it
429 ("gn path" is a good way to diagnose problems).
430
431 - There can be multiple targets with an included file: only one needs to be
432 valid for the include to be allowed.
433
434 - If there are only "sources" in a target, all are considered to be public
435 and can be included by other targets with a valid public dependency path.
436
437 - If a target lists files as "public", only those files are able to be
438 included by other targets. Anything in the sources will be considered
439 private and will not be includable regardless of dependency paths.
440
441 - Outputs from actions are treated like public sources on that target.
442
443 - A target can include headers from a target that depends on it if the
444 other target is annotated accordingly. See "gn help
445 allow_circular_includes_from".
446```
447
448#### **Advice on fixing problems**
449
450```
451 If you have a third party project that is difficult to fix or doesn't care
452 about include checks it's generally best to exclude that target from checking
453 altogether via "check_includes = false".
454
455 If you have conditional includes, make sure the build conditions and the
456 preprocessor conditions match, and annotate the line with "nogncheck" (see
457 "gn help nogncheck" for an example).
458
459 If two targets are hopelessly intertwined, use the
460 "allow_circular_includes_from" annotation. Ideally each should have identical
461 dependencies so configs inherited from those dependencies are consistent (see
462 "gn help allow_circular_includes_from").
463
464 If you have a standalone header file or files that need to be shared between
465 a few targets, you can consider making a source_set listing only those
466 headers as public sources. With only header files, the source set will be a
467 no-op from a build perspective, but will give a central place to refer to
468 those headers. That source set's files will still need to pass "gn check" in
469 isolation.
470
471 In rare cases it makes sense to list a header in more than one target if it
472 could be considered conceptually a member of both.
473```
474
475#### **Examples**
476
477```
478 gn check out/Debug
479 Check everything.
480
481 gn check out/Default //foo:bar
482 Check only the files in the //foo:bar target.
483
484 gn check out/Default "//foo/*
485 Check only the files in targets in the //foo directory tree.
486```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400487### **gn clean <out_dir>...**
Brett Wilson796ed472018-07-16 15:11:09 -0700488
489```
490 Deletes the contents of the output directory except for args.gn and
491 creates a Ninja build environment sufficient to regenerate the build.
492```
RJ Ascani4e260f12020-10-19 17:41:51 -0700493### **gn clean_stale [\--ninja-executable=...] <out_dir>...**
494
495```
496 Removes the no longer needed output files from the build directory and prunes
497 their records from the ninja build log and dependency database. These are
498 output files that were generated from previous builds, but the current build
499 graph no longer references them.
500
501 This command requires a ninja executable of at least version 1.10.0. The
502 executable must be provided by the --ninja-executable switch.
503```
504
505#### **Options**
506
507```
508 --ninja-executable=
509 Can be used to specify the ninja executable to use.
510```
Joe Armstrongb199e542019-03-21 09:40:08 +0800511### **gn desc**
Brett Wilson796ed472018-07-16 15:11:09 -0700512
513```
Joe Armstrongb199e542019-03-21 09:40:08 +0800514 gn desc
515 [--format=json]
516
Julie Hockette2a29402018-07-31 10:11:42 -0700517 Displays information about a given target or config. The build parameters
518 will be taken for the build in the given .
Brett Wilson796ed472018-07-16 15:11:09 -0700519
520 The
521 pattern (see "gn help label_pattern"). A label pattern will only match
522 targets.
523```
524
Nico Weber75eb3c72018-08-28 16:01:52 -0400525#### **Possibilities for <what to show>**
Brett Wilson796ed472018-07-16 15:11:09 -0700526
527```
528 (If unspecified an overall summary will be displayed.)
529
530 all_dependent_configs
531 allow_circular_includes_from
532 arflags [--blame]
533 args
534 cflags [--blame]
Julie Hockette2a29402018-07-31 10:11:42 -0700535 cflags_c [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700536 cflags_cc [--blame]
Brett Wilson796ed472018-07-16 15:11:09 -0700537 check_includes
538 configs [--tree] (see below)
Nate Fischer8ed01d32019-01-08 17:32:01 -0800539 data_keys
Brett Wilson796ed472018-07-16 15:11:09 -0700540 defines [--blame]
541 depfile
542 deps [--all] [--tree] (see below)
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100543 framework_dirs
544 frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700545 include_dirs [--blame]
546 inputs
547 ldflags [--blame]
548 lib_dirs
549 libs
Nate Fischer8ed01d32019-01-08 17:32:01 -0800550 metadata
551 output_conversion
Brett Wilson796ed472018-07-16 15:11:09 -0700552 outputs
553 public_configs
554 public
Nate Fischer8ed01d32019-01-08 17:32:01 -0800555 rebase
Brett Wilson796ed472018-07-16 15:11:09 -0700556 script
557 sources
558 testonly
559 visibility
Nate Fischer8ed01d32019-01-08 17:32:01 -0800560 walk_keys
Robert Sesekd0a6f072020-05-15 11:21:22 -0400561 weak_frameworks
Brett Wilson796ed472018-07-16 15:11:09 -0700562
563 runtime_deps
564 Compute all runtime deps for the given target. This is a computed list
565 and does not correspond to any GN variable, unlike most other values
566 here.
567
568 The output is a list of file names relative to the build directory. See
569 "gn help runtime_deps" for how this is computed. This also works with
570 "--blame" to see the source of the dependency.
571```
572
573#### **Shared flags**
Brett Wilson796ed472018-07-16 15:11:09 -0700574
Robert Sesekd0a6f072020-05-15 11:21:22 -0400575```
576 --default-toolchain
577 Normally wildcard targets are matched in all toolchains. This
578 switch makes wildcard labels with no explicit toolchain reference
579 only match targets in the default toolchain.
580
581 Non-wildcard inputs with no explicit toolchain specification will
582 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -0700583
584 --format=json
585 Format the output as JSON instead of text.
586```
587
588#### **Target flags**
589
590```
591 --blame
592 Used with any value specified on a config, this will name the config that
Sylvain Defresnea09ec162020-01-16 12:36:40 +0100593 causes that target to get the flag. This doesn't currently work for libs,
Robert Sesekd0a6f072020-05-15 11:21:22 -0400594 lib_dirs, frameworks, weak_frameworks and framework_dirs because those are
595 inherited and are more complicated to figure out the blame (patches
596 welcome).
Brett Wilson796ed472018-07-16 15:11:09 -0700597```
598
599#### **Configs**
600
601```
602 The "configs" section will list all configs that apply. For targets this will
603 include configs specified in the "configs" variable of the target, and also
604 configs pushed onto this target via public or "all dependent" configs.
605
606 Configs can have child configs. Specifying --tree will show the hierarchy.
607```
608
609#### **Printing outputs**
610
611```
612 The "outputs" section will list all outputs that apply, including the outputs
613 computed from the tool definition (eg for "executable", "static_library", ...
614 targets).
615```
616
617#### **Printing deps**
618
619```
620 Deps will include all public, private, and data deps (TODO this could be
621 clarified and enhanced) sorted in order applying. The following may be used:
622
623 --all
624 Collects all recursive dependencies and prints a sorted flat list. Also
625 usable with --tree (see below).
Robert Sesekd0a6f072020-05-15 11:21:22 -0400626
Brett Wilson796ed472018-07-16 15:11:09 -0700627 --as=(buildfile|label|output)
628 How to print targets.
629
630 buildfile
631 Prints the build files where the given target was declared as
632 file names.
633 label (default)
634 Prints the label of the target.
635 output
636 Prints the first output file for the target relative to the
637 root build directory.
638
639 --testonly=(true|false)
640 Restrict outputs to targets with the testonly flag set
641 accordingly. When unspecified, the target's testonly flags are
642 ignored.
643
644 --tree
645 Print a dependency tree. By default, duplicates will be elided with "..."
646 but when --all and -tree are used together, no eliding will be performed.
647
648 The "deps", "public_deps", and "data_deps" will all be included in the
649 tree.
650
651 Tree output can not be used with the filtering or output flags: --as,
652 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -0400653
Brett Wilson796ed472018-07-16 15:11:09 -0700654 --type=(action|copy|executable|group|loadable_module|shared_library|
655 source_set|static_library)
656 Restrict outputs to targets matching the given type. If
657 unspecified, no filtering will be performed.
658```
659
660#### **Note**
661
662```
663 This command will show the full name of directories and source files, but
664 when directories and source paths are written to the build file, they will be
665 adjusted to be relative to the build directory. So the values for paths
666 displayed by this command won't match (but should mean the same thing).
667```
668
669#### **Examples**
670
671```
672 gn desc out/Debug //base:base
673 Summarizes the given target.
674
675 gn desc out/Foo :base_unittests deps --tree
676 Shows a dependency tree of the "base_unittests" project in
677 the current directory.
678
679 gn desc out/Debug //base defines --blame
680 Shows defines set for the //base:base target, annotated by where
681 each one was set from.
682```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800683### **gn format [\--dump-tree] (\--stdin | <list of build_files...>)**
Brett Wilson796ed472018-07-16 15:11:09 -0700684
685```
686 Formats .gn file to a standard format.
687
688 The contents of some lists ('sources', 'deps', etc.) will be sorted to a
689 canonical order. To suppress this, you can add a comment of the form "#
690 NOSORT" immediately preceding the assignment. e.g.
691
692 # NOSORT
693 sources = [
694 "z.cc",
695 "a.cc",
696 ]
697```
698
699#### **Arguments**
700
701```
702 --dry-run
Nico Weberad9eab22020-11-15 22:20:28 -0500703 Prints the list of files that would be reformatted but does not write
704 anything to disk. This is useful for presubmit/lint-type checks.
Brett Wilson796ed472018-07-16 15:11:09 -0700705 - Exit code 0: successful format, matches on disk.
706 - Exit code 1: general failure (parse error, etc.)
707 - Exit code 2: successful format, but differs from on disk.
708
Julie Hockettd69a9c32019-01-23 14:36:18 -0800709 --dump-tree[=( text | json )]
710 Dumps the parse tree to stdout and does not update the file or print
711 formatted output. If no format is specified, text format will be used.
Brett Wilson796ed472018-07-16 15:11:09 -0700712
713 --stdin
714 Read input from stdin and write to stdout rather than update a file
715 in-place.
RJ Ascani4e260f12020-10-19 17:41:51 -0700716
717 --read-tree=json
718 Reads an AST from stdin in the format output by --dump-tree=json and
719 uses that as the parse tree. (The only read-tree format currently
720 supported is json.) The given .gn file will be overwritten. This can be
721 used to programmatically transform .gn files.
Brett Wilson796ed472018-07-16 15:11:09 -0700722```
723
724#### **Examples**
725```
Julie Hockettd69a9c32019-01-23 14:36:18 -0800726 gn format //some/BUILD.gn //some/other/BUILD.gn //and/another/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700727 gn format some\\BUILD.gn
728 gn format /abspath/some/BUILD.gn
729 gn format --stdin
RJ Ascani4e260f12020-10-19 17:41:51 -0700730 gn format --read-tree=json //rewritten/BUILD.gn
Brett Wilson796ed472018-07-16 15:11:09 -0700731```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800732### **gn gen [\--check] [<ide options>] <out_dir>**
Brett Wilson796ed472018-07-16 15:11:09 -0700733
734```
735 Generates ninja files from the current tree and puts them in the given output
736 directory.
737
738 The output directory can be a source-repo-absolute path name such as:
739 //out/foo
740 Or it can be a directory relative to the current directory such as:
741 out/foo
742
James Robinson49f59032020-01-08 14:05:51 -0800743 "gn gen --check" is the same as running "gn check". "gn gen --check=system" is
744 the same as running "gn check --check-system". See "gn help check" for
745 documentation on that mode.
Brett Wilson796ed472018-07-16 15:11:09 -0700746
747 See "gn help switches" for the common command-line switches.
748```
749
RJ Ascani6966efb2020-10-19 16:50:11 -0700750#### **General options**
751
752```
753 --ninja-executable=
754 Can be used to specify the ninja executable to use. This executable will
755 be used as an IDE option to indicate which ninja to use for building. This
756 executable will also be used as part of the gen process for triggering a
RJ Ascani4e260f12020-10-19 17:41:51 -0700757 restat on generated ninja files and for use with --clean-stale.
758
759 --clean-stale
760 This option will cause no longer needed output files to be removed from
761 the build directory, and their records pruned from the ninja build log and
762 dependency database after the ninja build graph has been generated. This
763 option requires a ninja executable of at least version 1.10.0. It can be
764 provided by the --ninja-executable switch. Also see "gn help clean_stale".
RJ Ascani6966efb2020-10-19 16:50:11 -0700765```
766
Brett Wilson796ed472018-07-16 15:11:09 -0700767#### **IDE options**
768
769```
Harley Li0a9affb2020-06-03 10:38:42 -0400770 GN optionally generates files for IDE. Files won't be overwritten if their
771 contents don't change. Possibilities for
Brett Wilson796ed472018-07-16 15:11:09 -0700772
773 --ide=
774 Generate files for an IDE. Currently supported values:
775 "eclipse" - Eclipse CDT settings file.
776 "vs" - Visual Studio project/solution files.
Aaron Gablea5bcbd72019-12-30 15:08:52 -0800777 (default Visual Studio version: 2019)
Brett Wilson796ed472018-07-16 15:11:09 -0700778 "vs2013" - Visual Studio 2013 project/solution files.
779 "vs2015" - Visual Studio 2015 project/solution files.
780 "vs2017" - Visual Studio 2017 project/solution files.
Sylvain Defresned46f92f2019-04-03 16:14:29 +0200781 "vs2019" - Visual Studio 2019 project/solution files.
Tyler Mandry4a648092022-02-15 19:47:09 +0000782 "vs2022" - Visual Studio 2022 project/solution files.
Brett Wilson796ed472018-07-16 15:11:09 -0700783 "xcode" - Xcode workspace/solution files.
784 "qtcreator" - QtCreator project files.
785 "json" - JSON file containing target information
786
787 --filters=
788 Semicolon-separated list of label patterns used to limit the set of
789 generated projects (see "gn help label_pattern"). Only matching targets
790 and their dependencies will be included in the solution. Only used for
791 Visual Studio, Xcode and JSON.
792```
793
794#### **Visual Studio Flags**
795
796```
797 --sln=
798 Override default sln file name ("all"). Solution file is written to the
799 root build directory.
800
801 --no-deps
802 Don't include targets dependencies to the solution. Changes the way how
803 --filters option works. Only directly matching targets are included.
804
805 --winsdk=
806 Use the specified Windows 10 SDK version to generate project files.
807 As an example, "10.0.15063.0" can be specified to use Creators Update SDK
808 instead of the default one.
809
Shezan Baigc0a2d232021-07-01 00:29:49 -0400810 --ninja-executable=
811 Can be used to specify the ninja executable to use when building.
812
Brett Wilson796ed472018-07-16 15:11:09 -0700813 --ninja-extra-args=
814 This string is passed without any quoting to the ninja invocation
815 command-line. Can be used to configure ninja flags, like "-j".
816```
817
818#### **Xcode Flags**
819
820```
Sylvain Defresnedb55efd2020-06-16 12:23:03 +0200821 --xcode-project=
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +0100822 Override default Xcode project file name ("all"). The project file is
Brett Wilson796ed472018-07-16 15:11:09 -0700823 written to the root build directory.
824
Sylvain Defresne7b0690e2020-06-26 15:33:01 +0200825 --xcode-build-system=
826 Configure the build system to use for the Xcode project. Supported
827 values are (default to "legacy"):
828 "legacy" - Legacy Build system
829 "new" - New Build System
830
Sylvain Defresne88830702022-06-20 17:11:49 +0200831 --xcode-configs=
832 Configure the list of build configuration supported by the generated
833 project. If specified, must be a list of semicolon-separated strings.
834 If ommitted, a single configuration will be used in the generated
835 project derived from the build directory.
836
837 --xcode-config-build-dir=
838 If present, must be a path relative to the source directory. It will
839 default to $root_out_dir if ommitted. The path is assumed to point to
840 the directory where ninja needs to be invoked. This variable can be
841 used to build for multiple configuration / platform / environment from
842 the same generated Xcode project (assuming that the user has created a
843 gn build directory with the correct args.gn for each).
844
845 One useful value is to use Xcode variables such as '${CONFIGURATION}'
846 or '${EFFECTIVE_PLATFORM}'.
847
Sylvain Defresneae474cc2022-06-21 14:44:02 +0200848 --xcode-additional-files-patterns=
849 If present, must be a list of semicolon-separated file patterns. It
850 will be used to add all files matching the pattern located in the
851 source tree to the project. It can be used to add, e.g. documentation
852 files to the project to allow easily edit them.
853
854 --xcode-additional-files-roots=
855 If present, must be a list of semicolon-separated paths. It will be used
856 as roots when looking for additional files to add. If ommitted, defaults
857 to "//".
858
Sylvain Defresneaff489a2020-03-11 18:27:43 +0100859 --ninja-executable=
860 Can be used to specify the ninja executable to use when building.
861
Brett Wilson796ed472018-07-16 15:11:09 -0700862 --ninja-extra-args=
863 This string is passed without any quoting to the ninja invocation
864 command-line. Can be used to configure ninja flags, like "-j".
865
Sylvain Defresnee431b2f2020-11-04 10:49:47 +0100866 --ide-root-target=
Brett Wilson796ed472018-07-16 15:11:09 -0700867 Name of the target corresponding to "All" target in Xcode. If unset,
868 "All" invokes ninja without any target and builds everything.
869```
870
871#### **QtCreator Flags**
872
873```
Sylvain Defresnee431b2f2020-11-04 10:49:47 +0100874 --ide-root-target=
Brett Wilson796ed472018-07-16 15:11:09 -0700875 Name of the root target for which the QtCreator project will be generated
876 to contain files of it and its dependencies. If unset, the whole build
877 graph will be emitted.
878```
879
880#### **Eclipse IDE Support**
881
882```
883 GN DOES NOT generate Eclipse CDT projects. Instead, it generates a settings
884 file which can be imported into an Eclipse CDT project. The XML file contains
885 a list of include paths and defines. Because GN does not generate a full
886 .cproject definition, it is not possible to properly define includes/defines
887 for each file individually. Instead, one set of includes/defines is generated
888 for the entire project. This works fairly well but may still result in a few
889 indexer issues here and there.
890```
891
892#### **Generic JSON Output**
893
894```
895 Dumps target information to a JSON file and optionally invokes a
896 python script on the generated file. See the comments at the beginning
897 of json_project_writer.cc and desc_builder.cc for an overview of the JSON
898 file format.
899
900 --json-file-name=
901 Overrides default file name (project.json) of generated JSON file.
902
903 --json-ide-script=
Harley Li0a9affb2020-06-03 10:38:42 -0400904 Executes python script after the JSON file is generated or updated with
905 new content. Path can be project absolute (//), system absolute (/) or
906 relative, in which case the output directory will be base. Path to
907 generated JSON file will be first argument when invoking script.
Brett Wilson796ed472018-07-16 15:11:09 -0700908
909 --json-ide-script-args=
910 Optional second argument that will passed to executed script.
911```
Julie Hockett11e09912018-07-31 13:15:08 -0700912
913#### **Compilation Database**
914
915```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400916 --export-rust-project
917 Produces a rust-project.json file in the root of the build directory
918 This is used for various tools in the Rust ecosystem allowing for the
919 replay of individual compilations independent of the build system.
920 This is an unstable format and likely to change without warning.
921
Brett Wilson1da84bb2022-09-14 15:35:29 -0700922 --add-export-compile-commands=
923 Adds an additional label pattern (see "gn help label_pattern") of a
924 target to add to the compilation database. This pattern is appended to any
925 list values specified in the export_compile_commands variable in the
926 .gn file (see "gn help dotfile"). This allows the user to add additional
927 targets to the compilation database that the project doesn't add by default.
928
929 To add more than one value, specify this switch more than once. Each
930 invocation adds an additional label pattern.
931
932 Example:
933 --add-export-compile-commands=//tools:my_tool
934 --add-export-compile-commands="//base/*"
935
Robert Sesek252674d2019-05-10 14:08:52 -0400936 --export-compile-commands[=]
Brett Wilson1da84bb2022-09-14 15:35:29 -0700937 DEPRECATED https://bugs.chromium.org/p/gn/issues/detail?id=302.
938 Please use --add-export-compile-commands for per-user configuration, and
939 the "export_compile_commands" value in the project-level .gn file (see
940 "gn help dotfile") for per-project configuration.
941
942 Overrides the value of the export_compile_commands in the .gn file (see
943 "gn help dotfile") as well as the --add-export-compile-commands switch.
944
945 Unlike the .gn setting, this switch takes a legacy format which is a list
946 of target names that are matched in any directory. For example, "foo" will
947 match:
948 - "//path/to/src:foo"
949 - "//other/path:foo"
950 - "//foo:foo"
Aaron Wood2d9fd622020-12-10 21:28:19 -0800951 and not match:
Brett Wilson1da84bb2022-09-14 15:35:29 -0700952 - "//foo:bar"
Julie Hockett11e09912018-07-31 13:15:08 -0700953```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -0800954### **gn help <anything>**
Brett Wilson796ed472018-07-16 15:11:09 -0700955
956```
957 Yo dawg, I heard you like help on your help so I put help on the help in the
958 help.
959
960 You can also use "all" as the parameter to get all help at once.
961```
962
963#### **Switches**
964
965```
966 --markdown
967 Format output in markdown syntax.
968```
969
970#### **Example**
971
972```
973 gn help --markdown all
974 Dump all help to stdout in markdown format.
975```
Robert Sesekd0a6f072020-05-15 11:21:22 -0400976### **gn ls <out_dir> [<label_pattern>] [\--default-toolchain] [\--as=...]**
Brett Wilson796ed472018-07-16 15:11:09 -0700977```
978 [--type=...] [--testonly=...]
979
980 Lists all targets matching the given pattern for the given build directory.
981 By default, only targets in the default toolchain will be matched unless a
982 toolchain is explicitly supplied.
983
984 If the label pattern is unspecified, list all targets. The label pattern is
985 not a general regular expression (see "gn help label_pattern"). If you need
986 more complex expressions, pipe the result through grep.
987```
988
989#### **Options**
990
991```
992 --as=(buildfile|label|output)
993 How to print targets.
994
995 buildfile
996 Prints the build files where the given target was declared as
997 file names.
998 label (default)
999 Prints the label of the target.
1000 output
1001 Prints the first output file for the target relative to the
1002 root build directory.
1003
Robert Sesekd0a6f072020-05-15 11:21:22 -04001004 --default-toolchain
1005 Normally wildcard targets are matched in all toolchains. This
1006 switch makes wildcard labels with no explicit toolchain reference
1007 only match targets in the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07001008
Robert Sesekd0a6f072020-05-15 11:21:22 -04001009 Non-wildcard inputs with no explicit toolchain specification will
1010 always match only a target in the default toolchain if one exists.
Brett Wilson796ed472018-07-16 15:11:09 -07001011
1012 --testonly=(true|false)
1013 Restrict outputs to targets with the testonly flag set
1014 accordingly. When unspecified, the target's testonly flags are
1015 ignored.
1016
1017 --type=(action|copy|executable|group|loadable_module|shared_library|
1018 source_set|static_library)
1019 Restrict outputs to targets matching the given type. If
1020 unspecified, no filtering will be performed.
1021```
1022
1023#### **Examples**
1024
1025```
1026 gn ls out/Debug
1027 Lists all targets in the default toolchain.
1028
1029 gn ls out/Debug "//base/*"
1030 Lists all targets in the directory base and all subdirectories.
1031
1032 gn ls out/Debug "//base:*"
1033 Lists all targets defined in //base/BUILD.gn.
1034
1035 gn ls out/Debug //base --as=output
1036 Lists the build output file for //base:base
1037
1038 gn ls out/Debug --type=executable
1039 Lists all executables produced by the build.
1040
1041 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
1042 Builds all targets in //base and all subdirectories.
Brett Wilson796ed472018-07-16 15:11:09 -07001043```
Joe Armstrongb199e542019-03-21 09:40:08 +08001044### **gn meta**
1045
Nate Fischer8ed01d32019-01-08 17:32:01 -08001046```
Joe Armstrongb199e542019-03-21 09:40:08 +08001047 gn meta * --data=[,*]* [--walk=[,*]*]
1048 [--rebase=]
Nate Fischer8ed01d32019-01-08 17:32:01 -08001049
1050 Lists collected metaresults of all given targets for the given data key(s),
1051 collecting metadata dependencies as specified by the given walk key(s).
1052
1053 See `gn help generated_file` for more information on the walk.
1054```
1055
1056#### **Arguments**
1057
1058```
1059
1060 A list of target labels from which to initiate the walk.
1061
1062 --data
Tyler Mandry4a648092022-02-15 19:47:09 +00001063 A comma-separated list of keys from which to extract data. In each target
1064 walked, its metadata scope is checked for the presence of these keys. If
1065 present, the contents of those variable in the scope are appended to the
1066 results list.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001067
1068 --walk (optional)
Tyler Mandry4a648092022-02-15 19:47:09 +00001069 A comma-separated list of keys from which to control the walk. In each
1070 target walked, its metadata scope is checked for the presence of any of
1071 these keys. If present, the contents of those variables is checked to ensure
1072 that it is a label of a valid dependency of the target and then added to the
1073 set of targets to walk. If the empty string ("") is present in any of these
1074 keys, all deps and data_deps are added to the walk set.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001075
1076 --rebase (optional)
1077 A destination directory onto which to rebase any paths found. If set, all
1078 collected metadata will be rebased onto this path. This option will throw errors
1079 if collected metadata is not a list of strings.
1080```
1081
1082#### **Examples**
1083
1084```
1085 gn meta out/Debug "//base/foo" --data=files
1086 Lists collected metaresults for the `files` key in the //base/foo:foo
1087 target and all of its dependency tree.
1088
Tyler Mandry4a648092022-02-15 19:47:09 +00001089 gn meta out/Debug "//base/foo" --data=files,other
Nate Fischer8ed01d32019-01-08 17:32:01 -08001090 Lists collected metaresults for the `files` and `other` keys in the
1091 //base/foo:foo target and all of its dependency tree.
1092
1093 gn meta out/Debug "//base/foo" --data=files --walk=stop
1094 Lists collected metaresults for the `files` key in the //base/foo:foo
1095 target and all of the dependencies listed in the `stop` key (and so on).
1096
1097 gn meta out/Debug "//base/foo" --data=files --rebase="/"
1098 Lists collected metaresults for the `files` key in the //base/foo:foo
1099 target and all of its dependency tree, rebasing the strings in the `files`
1100 key onto the source directory of the target's declaration relative to "/".
1101```
Hector Dearman5233fa12020-09-11 12:18:21 +01001102### **gn outputs <out_dir> <list of target or file names...>**
Brett Wilson74397092020-03-18 16:34:14 -07001103
1104```
1105 Lists the output files corresponding to the given target(s) or file name(s).
1106 There can be multiple outputs because there can be more than one output
1107 generated by a build step, and there can be more than one toolchain matched.
1108 You can also list multiple inputs which will generate a union of all the
1109 outputs from those inputs.
1110
1111 - The input target/file names are relative to the current directory.
1112
1113 - The output file names are relative to the root build directory.
1114
1115 This command is useful for finding a ninja command that will build only a
1116 portion of the build.
1117```
1118
1119#### **Target outputs**
1120
1121```
1122 If the parameter is a target name that includes a toolchain, it will match
1123 only that target in that toolchain. If no toolchain is specified, it will
1124 match all targets with that name in any toolchain.
1125
1126 The result will be the outputs specified by that target which could be a
1127 library, executable, output of an action, a stamp file, etc.
1128```
1129
1130#### **File outputs**
1131
1132```
1133 If the parameter is a file name it will compute the output for that compile
1134 step for all targets in all toolchains that contain that file as a source
1135 file.
1136
1137 If the source is not compiled (e.g. a header or text file), the command will
1138 produce no output.
1139
1140 If the source is listed as an "input" to a binary target or action will
1141 resolve to that target's outputs.
1142```
1143
1144#### **Example**
1145
1146```
1147 gn outputs out/debug some/directory:some_target
1148 Find the outputs of a given target.
1149
1150 gn outputs out/debug src/project/my_file.cc | xargs ninja -C out/debug
1151 Compiles just the given source file in all toolchains it's referenced in.
1152
1153 git diff --name-only | xargs gn outputs out/x64 | xargs ninja -C out/x64
1154 Compiles all files changed in git.
1155```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001156### **gn path <out_dir> <target_one> <target_two>**
Brett Wilson796ed472018-07-16 15:11:09 -07001157
1158```
1159 Finds paths of dependencies between two targets. Each unique path will be
1160 printed in one group, and groups will be separate by newlines. The two
1161 targets can appear in either order (paths will be found going in either
1162 direction).
1163
1164 By default, a single path will be printed. If there is a path with only
1165 public dependencies, the shortest public path will be printed. Otherwise, the
1166 shortest path using either public or private dependencies will be printed. If
1167 --with-data is specified, data deps will also be considered. If there are
1168 multiple shortest paths, an arbitrary one will be selected.
1169```
1170
1171#### **Interesting paths**
1172
1173```
1174 In a large project, there can be 100's of millions of unique paths between a
1175 very high level and a common low-level target. To make the output more useful
1176 (and terminate in a reasonable time), GN will not revisit sub-paths
1177 previously known to lead to the target.
1178```
1179
1180#### **Options**
1181
1182```
1183 --all
1184 Prints all "interesting" paths found rather than just the first one.
1185 Public paths will be printed first in order of increasing length, followed
1186 by non-public paths in order of increasing length.
1187
1188 --public
1189 Considers only public paths. Can't be used with --with-data.
1190
1191 --with-data
1192 Additionally follows data deps. Without this flag, only public and private
1193 linked deps will be followed. Can't be used with --public.
1194```
1195
1196#### **Example**
1197
1198```
Keir Mierle45611e32019-11-12 11:18:00 -08001199 gn path out/Default //base //gn
Brett Wilson796ed472018-07-16 15:11:09 -07001200```
Joe Armstrongb199e542019-03-21 09:40:08 +08001201### **gn refs**
1202
Brett Wilson796ed472018-07-16 15:11:09 -07001203```
Nico Weberad9eab22020-11-15 22:20:28 -05001204 gn refs (|
1205 [--default-toolchain] [--as=...] [--testonly=...] [--type=...]
Brett Wilson796ed472018-07-16 15:11:09 -07001206
1207 Finds reverse dependencies (which targets reference something). The input is
1208 a list containing:
1209
1210 - Target label: The result will be which targets depend on it.
1211
1212 - Config label: The result will be which targets list the given config in
1213 its "configs" or "public_configs" list.
1214
1215 - Label pattern: The result will be which targets depend on any target
1216 matching the given pattern. Patterns will not match configs. These are not
1217 general regular expressions, see "gn help label_pattern" for details.
1218
1219 - File name: The result will be which targets list the given file in its
1220 "inputs", "sources", "public", "data", or "outputs". Any input that does
1221 not contain wildcards and does not match a target or a config will be
1222 treated as a file.
1223
1224 - Response file: If the input starts with an "@", it will be interpreted as
1225 a path to a file containing a list of labels or file names, one per line.
1226 This allows us to handle long lists of inputs without worrying about
1227 command line limits.
1228```
1229
1230#### **Options**
1231
1232```
1233 --all
1234 When used without --tree, will recurse and display all unique
1235 dependencies of the given targets. For example, if the input is a target,
1236 this will output all targets that depend directly or indirectly on the
1237 input. If the input is a file, this will output all targets that depend
1238 directly or indirectly on that file.
1239
1240 When used with --tree, turns off eliding to show a complete tree.
Brett Wilson796ed472018-07-16 15:11:09 -07001241
1242 --as=(buildfile|label|output)
1243 How to print targets.
1244
1245 buildfile
1246 Prints the build files where the given target was declared as
1247 file names.
1248 label (default)
1249 Prints the label of the target.
1250 output
1251 Prints the first output file for the target relative to the
1252 root build directory.
1253
Robert Sesekd0a6f072020-05-15 11:21:22 -04001254 --default-toolchain
1255 Normally wildcard targets are matched in all toolchains. This
1256 switch makes wildcard labels with no explicit toolchain reference
1257 only match targets in the default toolchain.
1258
1259 Non-wildcard inputs with no explicit toolchain specification will
1260 always match only a target in the default toolchain if one exists.
1261
Brett Wilson796ed472018-07-16 15:11:09 -07001262 -q
1263 Quiet. If nothing matches, don't print any output. Without this option, if
1264 there are no matches there will be an informational message printed which
1265 might interfere with scripts processing the output.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001266
Brett Wilson796ed472018-07-16 15:11:09 -07001267 --testonly=(true|false)
1268 Restrict outputs to targets with the testonly flag set
1269 accordingly. When unspecified, the target's testonly flags are
1270 ignored.
1271
1272 --tree
1273 Outputs a reverse dependency tree from the given target. Duplicates will
1274 be elided. Combine with --all to see a full dependency tree.
1275
1276 Tree output can not be used with the filtering or output flags: --as,
1277 --type, --testonly.
Robert Sesekd0a6f072020-05-15 11:21:22 -04001278
Brett Wilson796ed472018-07-16 15:11:09 -07001279 --type=(action|copy|executable|group|loadable_module|shared_library|
1280 source_set|static_library)
1281 Restrict outputs to targets matching the given type. If
1282 unspecified, no filtering will be performed.
1283```
1284
1285#### **Examples (target input)**
1286
1287```
Keir Mierle45611e32019-11-12 11:18:00 -08001288 gn refs out/Debug //gn:gn
Brett Wilson796ed472018-07-16 15:11:09 -07001289 Find all targets depending on the given exact target name.
1290
1291 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
1292 Edit all .gn files containing references to //base:i18n
1293
1294 gn refs out/Debug //base --all
1295 List all targets depending directly or indirectly on //base:base.
1296
1297 gn refs out/Debug "//base/*"
1298 List all targets depending directly on any target in //base or
1299 its subdirectories.
1300
1301 gn refs out/Debug "//base:*"
1302 List all targets depending directly on any target in
1303 //base/BUILD.gn.
1304
1305 gn refs out/Debug //base --tree
1306 Print a reverse dependency tree of //base:base
1307```
1308
1309#### **Examples (file input)**
1310
1311```
1312 gn refs out/Debug //base/macros.h
1313 Print target(s) listing //base/macros.h as a source.
1314
1315 gn refs out/Debug //base/macros.h --tree
1316 Display a reverse dependency tree to get to the given file. This
1317 will show how dependencies will reference that file.
1318
1319 gn refs out/Debug //base/macros.h //base/at_exit.h --all
1320 Display all unique targets with some dependency path to a target
1321 containing either of the given files as a source.
1322
1323 gn refs out/Debug //base/macros.h --testonly=true --type=executable
1324 --all --as=output
1325 Display the executable file names of all test executables
1326 potentially affected by a change to the given file.
1327```
1328## Target declarations
1329
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001330### **action**: Declare a target that runs a script a single time.
Brett Wilson796ed472018-07-16 15:11:09 -07001331
1332```
1333 This target type allows you to run a script a single time to produce one or
1334 more output files. If you want to run a script once for each of a set of
1335 input files, see "gn help action_foreach".
1336```
1337
1338#### **Inputs**
1339
1340```
1341 In an action the "sources" and "inputs" are treated the same: they're both
1342 input dependencies on script execution with no special handling. If you want
1343 to pass the sources to your script, you must do so explicitly by including
1344 them in the "args". Note also that this means there is no special handling of
1345 paths since GN doesn't know which of the args are paths and not. You will
1346 want to use rebase_path() to convert paths to be relative to the
1347 root_build_dir.
1348
1349 You can dynamically write input dependencies (for incremental rebuilds if an
1350 input file changes) by writing a depfile when the script is run (see "gn help
1351 depfile"). This is more flexible than "inputs".
1352
1353 If the command line length is very long, you can use response files to pass
1354 args to your script. See "gn help response_file_contents".
1355
1356 It is recommended you put inputs to your script in the "sources" variable,
1357 and stuff like other Python files required to run your script in the "inputs"
1358 variable.
Julie Hockette2a29402018-07-31 10:11:42 -07001359
Adrian Taylorc0d4f8b2022-03-25 15:05:57 -07001360 Actions can take the configs and public_configs lists, as well as any of the
1361 configs variables (defines, include_dirs, etc.) set directly on the target.
1362 These behave exactly as they would on a binary target and can be accessed
1363 using substitution patterns in the script args (see "gn help args") to
1364 implement custom compiler-like tools.
1365
Brett Wilson796ed472018-07-16 15:11:09 -07001366 The "deps" and "public_deps" for an action will always be
1367 completed before any part of the action is run so it can depend on
1368 the output of previous steps. The "data_deps" will be built if the
1369 action is built, but may not have completed before all steps of the
1370 action are started. This can give additional parallelism in the build
1371 for runtime-only dependencies.
1372```
1373
1374#### **Outputs**
1375
1376```
1377 You should specify files created by your script by specifying them in the
1378 "outputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001379
Brett Wilson796ed472018-07-16 15:11:09 -07001380 The script will be executed with the given arguments with the current
1381 directory being that of the root build directory. If you pass files
1382 to your script, see "gn help rebase_path" for how to convert
1383 file names to be relative to the build directory (file names in the
1384 sources, outputs, and inputs will be all treated as relative to the
1385 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001386
1387 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1388 that Ninja will check the timestamp of the output after the action
1389 completes. If output timestamp is unchanged, the step will be treated
1390 as if it never needed to be rebuilt, potentially eliminating some
1391 downstream steps for incremental builds. Scripts can improve build
1392 performance by taking care not to change the timstamp of the output
1393 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001394```
1395
1396#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001397
Brett Wilson796ed472018-07-16 15:11:09 -07001398```
1399 All output files must be inside the output directory of the build.
1400 You would generally use |$target_out_dir| or |$target_gen_dir| to
1401 reference the output or generated intermediate file directories,
1402 respectively.
1403```
1404
1405#### **Variables**
1406
1407```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001408 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1409 cflags_objcc, defines, include_dirs, inputs, ldflags,
1410 lib_dirs, libs, precompiled_header, precompiled_source,
1411 rustenv, rustflags, swiftflags, testonly
1412 Dependent configs: all_dependent_configs, public_configs
1413 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1414 write_runtime_deps
1415 General: check_includes, configs, data, friend, inputs, metadata,
1416 output_extension, output_name, public, sources, testonly,
1417 visibility
1418 Action variables: args, bridge_header, configs, data, depfile,
1419 framework_dirs, inputs, module_deps, module_name,
1420 outputs*, pool, response_file_contents, script*,
1421 sources
Brett Wilson796ed472018-07-16 15:11:09 -07001422 * = required
1423```
1424
1425#### **Example**
1426
1427```
1428 action("run_this_guy_once") {
1429 script = "doprocessing.py"
1430 sources = [ "my_configuration.txt" ]
1431 outputs = [ "$target_gen_dir/insightful_output.txt" ]
1432
1433 # Our script imports this Python file so we want to rebuild if it changes.
1434 inputs = [ "helper_library.py" ]
1435
1436 # Note that we have to manually pass the sources to our script if the
1437 # script needs them as inputs.
1438 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
1439 rebase_path(sources, root_build_dir)
1440 }
1441```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001442### **action_foreach**: Declare a target that runs a script over a set of files.
Brett Wilson796ed472018-07-16 15:11:09 -07001443
1444```
1445 This target type allows you to run a script once-per-file over a set of
1446 sources. If you want to run a script once that takes many files as input, see
1447 "gn help action".
1448```
1449
1450#### **Inputs**
1451
1452```
1453 The script will be run once per file in the "sources" variable. The "outputs"
1454 variable should specify one or more files with a source expansion pattern in
1455 it (see "gn help source_expansion"). The output file(s) for each script
1456 invocation should be unique. Normally you use "{{source_name_part}}" in each
1457 output file.
1458
1459 If your script takes additional data as input, such as a shared configuration
1460 file or a Python module it uses, those files should be listed in the "inputs"
1461 variable. These files are treated as dependencies of each script invocation.
1462
1463 If the command line length is very long, you can use response files to pass
1464 args to your script. See "gn help response_file_contents".
1465
1466 You can dynamically write input dependencies (for incremental rebuilds if an
1467 input file changes) by writing a depfile when the script is run (see "gn help
1468 depfile"). This is more flexible than "inputs".
Julie Hockette2a29402018-07-31 10:11:42 -07001469
Brett Wilson796ed472018-07-16 15:11:09 -07001470 The "deps" and "public_deps" for an action will always be
1471 completed before any part of the action is run so it can depend on
1472 the output of previous steps. The "data_deps" will be built if the
1473 action is built, but may not have completed before all steps of the
1474 action are started. This can give additional parallelism in the build
1475 for runtime-only dependencies.
1476```
1477
1478#### **Outputs**
Julie Hockette2a29402018-07-31 10:11:42 -07001479
Brett Wilson796ed472018-07-16 15:11:09 -07001480```
1481 The script will be executed with the given arguments with the current
1482 directory being that of the root build directory. If you pass files
1483 to your script, see "gn help rebase_path" for how to convert
1484 file names to be relative to the build directory (file names in the
1485 sources, outputs, and inputs will be all treated as relative to the
1486 current build file and converted as needed automatically).
Brett Wilson1e1ea9d2020-02-04 15:34:05 -08001487
1488 GN sets Ninja's flag 'restat = 1` for all action commands. This means
1489 that Ninja will check the timestamp of the output after the action
1490 completes. If output timestamp is unchanged, the step will be treated
1491 as if it never needed to be rebuilt, potentially eliminating some
1492 downstream steps for incremental builds. Scripts can improve build
1493 performance by taking care not to change the timstamp of the output
1494 file(s) if the contents have not changed.
Brett Wilson796ed472018-07-16 15:11:09 -07001495```
1496
1497#### **File name handling**
Julie Hockette2a29402018-07-31 10:11:42 -07001498
Brett Wilson796ed472018-07-16 15:11:09 -07001499```
1500 All output files must be inside the output directory of the build.
1501 You would generally use |$target_out_dir| or |$target_gen_dir| to
1502 reference the output or generated intermediate file directories,
1503 respectively.
1504```
1505
1506#### **Variables**
1507
1508```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001509 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1510 cflags_objcc, defines, include_dirs, inputs, ldflags,
1511 lib_dirs, libs, precompiled_header, precompiled_source,
1512 rustenv, rustflags, swiftflags, testonly
1513 Dependent configs: all_dependent_configs, public_configs
1514 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1515 write_runtime_deps
1516 General: check_includes, configs, data, friend, inputs, metadata,
1517 output_extension, output_name, public, sources, testonly,
1518 visibility
1519 Action variables: args, bridge_header, configs, data, depfile,
1520 framework_dirs, inputs, module_deps, module_name,
1521 outputs*, pool, response_file_contents, script*,
1522 sources
1523 * = required
Brett Wilson796ed472018-07-16 15:11:09 -07001524```
1525
1526#### **Example**
1527
1528```
1529 # Runs the script over each IDL file. The IDL script will generate both a .cc
1530 # and a .h file for each input.
1531 action_foreach("my_idl") {
1532 script = "idl_processor.py"
1533 sources = [ "foo.idl", "bar.idl" ]
1534
Joe Armstrong73e89072019-05-10 11:23:52 +08001535 # Our script reads this file each time, so we need to list it as a
Brett Wilson796ed472018-07-16 15:11:09 -07001536 # dependency so we can rebuild if it changes.
1537 inputs = [ "my_configuration.txt" ]
1538
1539 # Transformation from source file name to output file names.
1540 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
1541 "$target_gen_dir/{{source_name_part}}.cc" ]
1542
1543 # Note that since "args" is opaque to GN, if you specify paths here, you
1544 # will need to convert it to be relative to the build directory using
1545 # rebase_path().
1546 args = [
1547 "{{source}}",
1548 "-o",
Hector Dearman0d67e272020-12-22 01:37:16 +00001549 rebase_path(target_gen_dir, root_build_dir) +
Brett Wilson796ed472018-07-16 15:11:09 -07001550 "/{{source_name_part}}.h" ]
1551 }
1552```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001553### **bundle_data**: [iOS/macOS] Declare a target without output.
Brett Wilson796ed472018-07-16 15:11:09 -07001554
1555```
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01001556 This target type allows one to declare data that is required at runtime. It is
Brett Wilson796ed472018-07-16 15:11:09 -07001557 used to inform "create_bundle" targets of the files to copy into generated
1558 bundle, see "gn help create_bundle" for help.
1559
1560 The target must define a list of files as "sources" and a single "outputs".
1561 If there are multiple files, source expansions must be used to express the
1562 output. The output must reference a file inside of {{bundle_root_dir}}.
1563
1564 This target can be used on all platforms though it is designed only to
1565 generate iOS/macOS bundle. In cross-platform projects, it is advised to put it
1566 behind iOS/macOS conditionals.
1567
1568 See "gn help create_bundle" for more information.
1569```
1570
1571#### **Variables**
1572
1573```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001574 Dependent configs: all_dependent_configs, public_configs
1575 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1576 write_runtime_deps
1577 General: check_includes, configs, data, friend, inputs, metadata,
1578 output_extension, output_name, public, sources, testonly,
1579 visibility
1580 Bundle-specific: sources*, outputs*
Brett Wilson796ed472018-07-16 15:11:09 -07001581 * = required
1582```
1583
1584#### **Examples**
1585
1586```
1587 bundle_data("icudata") {
1588 sources = [ "sources/data/in/icudtl.dat" ]
1589 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
1590 }
1591
1592 bundle_data("base_unittests_bundle_data]") {
1593 sources = [ "test/data" ]
1594 outputs = [
1595 "{{bundle_resources_dir}}/{{source_root_relative_dir}}/" +
1596 "{{source_file_part}}"
1597 ]
1598 }
1599
1600 bundle_data("material_typography_bundle_data") {
1601 sources = [
1602 "src/MaterialTypography.bundle/Roboto-Bold.ttf",
1603 "src/MaterialTypography.bundle/Roboto-Italic.ttf",
1604 "src/MaterialTypography.bundle/Roboto-Regular.ttf",
1605 "src/MaterialTypography.bundle/Roboto-Thin.ttf",
1606 ]
1607 outputs = [
1608 "{{bundle_resources_dir}}/MaterialTypography.bundle/"
1609 "{{source_file_part}}"
1610 ]
1611 }
1612```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001613### **copy**: Declare a target that copies files.
Brett Wilson796ed472018-07-16 15:11:09 -07001614
1615#### **File name handling**
1616
1617```
1618 All output files must be inside the output directory of the build. You would
1619 generally use |$target_out_dir| or |$target_gen_dir| to reference the output
1620 or generated intermediate file directories, respectively.
1621
1622 Both "sources" and "outputs" must be specified. Sources can include as many
1623 files as you want, but there can only be one item in the outputs list (plural
1624 is used for the name for consistency with other target types).
1625
1626 If there is more than one source file, your output name should specify a
1627 mapping from each source file to an output file name using source expansion
1628 (see "gn help source_expansion"). The placeholders will look like
1629 "{{source_name_part}}", for example.
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001630
1631 If you want to copy the output of a previous build step, the target that
1632 generates the file to copy must be reachable from the deps or public_deps of
1633 the copy target.
1634```
1635
1636#### **Variables**
1637
1638```
1639 Dependent configs: all_dependent_configs, public_configs
1640 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1641 write_runtime_deps
1642 General: check_includes, configs, data, friend, inputs, metadata,
1643 output_extension, output_name, public, sources, testonly,
1644 visibility
1645 Copy variables: sources*, outputs*
1646 * = required
Brett Wilson796ed472018-07-16 15:11:09 -07001647```
1648
1649#### **Examples**
1650
1651```
1652 # Write a rule that copies a checked-in DLL to the output directory.
1653 copy("mydll") {
1654 sources = [ "mydll.dll" ]
1655 outputs = [ "$target_out_dir/mydll.dll" ]
1656 }
1657
1658 # Write a rule to copy several files to the target generated files directory.
1659 copy("myfiles") {
1660 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1661
1662 # Use source expansion to generate output files with the corresponding file
1663 # names in the gen dir. This will just copy each file.
1664 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1665 }
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001666
1667 # Copy the output of a generated executable.
1668 copy("package_melon") {
1669 # This example uses get_label_info() to compute the output directory of the
1670 # dependency. This allows the copy rule to work regardless of the toolchain.
1671 #
1672 # In some cases (particularly actions defined previously in the same file)
1673 # you can use get_target_outputs() to get the input file which can eliminate
1674 # the assumptions about the output file name of the dependency.
1675
1676 input_dir = get_label_info("//src/tools/melon", "root_out_dir");
1677 sources = [ "$input_dir/melon" ]
1678
1679 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1680
1681 # Depend on the target to build the file before copying.
1682 deps = [ "//src/tools/melon" ]
1683 }
Brett Wilson796ed472018-07-16 15:11:09 -07001684```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001685### **create_bundle**: [ios/macOS] Build an iOS or macOS bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07001686
1687```
1688 This target generates an iOS or macOS bundle (which is a directory with a
1689 well-know structure). This target does not define any sources, instead they
1690 are computed from all "bundle_data" target this one depends on transitively
1691 (the recursion stops at "create_bundle" targets).
1692
Sylvain Defresned46f92f2019-04-03 16:14:29 +02001693 The "bundle_*_dir" are be used for the expansion of {{bundle_*_dir}} rules in
1694 "bundle_data" outputs. The properties are optional but must be defined if any
1695 of the "bundle_data" target use them.
Brett Wilson796ed472018-07-16 15:11:09 -07001696
1697 This target can be used on all platforms though it is designed only to
1698 generate iOS or macOS bundle. In cross-platform projects, it is advised to put
1699 it behind iOS/macOS conditionals.
1700
1701 If a create_bundle is specified as a data_deps for another target, the bundle
1702 is considered a leaf, and its public and private dependencies will not
1703 contribute to any data or data_deps. Required runtime dependencies should be
1704 placed in the bundle. A create_bundle can declare its own explicit data and
1705 data_deps, however.
1706```
1707
1708#### **Code signing**
1709
1710```
1711 Some bundle needs to be code signed as part of the build (on iOS all
1712 application needs to be code signed to run on a device). The code signature
1713 can be configured via the code_signing_script variable.
1714
1715 If set, code_signing_script is the path of a script that invoked after all
1716 files have been moved into the bundle. The script must not change any file in
1717 the bundle, but may add new files.
1718
1719 If code_signing_script is defined, then code_signing_outputs must also be
1720 defined and non-empty to inform when the script needs to be re-run. The
1721 code_signing_args will be passed as is to the script (so path have to be
1722 rebased) and additional inputs may be listed with the variable
1723 code_signing_sources.
1724```
1725
1726#### **Variables**
1727
1728```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001729 Dependent configs: all_dependent_configs, public_configs
1730 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1731 write_runtime_deps
1732 General: check_includes, configs, data, friend, inputs, metadata,
1733 output_extension, output_name, public, sources, testonly,
1734 visibility
1735 Bundle vars: bundle_root_dir, bundle_contents_dir, bundle_resources_dir,
1736 bundle_executable_dir, bundle_deps_filter, product_type,
1737 code_signing_args, code_signing_script, code_signing_sources,
1738 code_signing_outputs, xcode_extra_attributes,
1739 xcode_test_application_name, partial_info_plist
Brett Wilson796ed472018-07-16 15:11:09 -07001740```
1741
1742#### **Example**
1743
1744```
1745 # Defines a template to create an application. On most platform, this is just
1746 # an alias for an "executable" target, but on iOS/macOS, it builds an
1747 # application bundle.
1748 template("app") {
1749 if (!is_ios && !is_mac) {
1750 executable(target_name) {
1751 forward_variables_from(invoker, "*")
1752 }
1753 } else {
1754 app_name = target_name
1755 gen_path = target_gen_dir
1756
1757 action("${app_name}_generate_info_plist") {
1758 script = [ "//build/ios/ios_gen_plist.py" ]
1759 sources = [ "templates/Info.plist" ]
1760 outputs = [ "$gen_path/Info.plist" ]
1761 args = rebase_path(sources, root_build_dir) +
1762 rebase_path(outputs, root_build_dir)
1763 }
1764
1765 bundle_data("${app_name}_bundle_info_plist") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001766 public_deps = [ ":${app_name}_generate_info_plist" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001767 sources = [ "$gen_path/Info.plist" ]
1768 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
1769 }
1770
1771 executable("${app_name}_generate_executable") {
1772 forward_variables_from(invoker, "*", [
1773 "output_name",
1774 "visibility",
1775 ])
1776 output_name =
1777 rebase_path("$gen_path/$app_name", root_build_dir)
1778 }
1779
1780 code_signing =
1781 defined(invoker.code_signing) && invoker.code_signing
1782
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001783 if (!is_ios || !code_signing) {
Brett Wilson796ed472018-07-16 15:11:09 -07001784 bundle_data("${app_name}_bundle_executable") {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001785 public_deps = [ ":${app_name}_generate_executable" ]
Brett Wilson796ed472018-07-16 15:11:09 -07001786 sources = [ "$gen_path/$app_name" ]
1787 outputs = [ "{{bundle_executable_dir}}/$app_name" ]
1788 }
1789 }
1790
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001791 create_bundle("$app_name.app") {
Brett Wilson796ed472018-07-16 15:11:09 -07001792 product_type = "com.apple.product-type.application"
1793
1794 if (is_ios) {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001795 bundle_root_dir = "$root_build_dir/$target_name"
Brett Wilson796ed472018-07-16 15:11:09 -07001796 bundle_contents_dir = bundle_root_dir
1797 bundle_resources_dir = bundle_contents_dir
1798 bundle_executable_dir = bundle_contents_dir
Brett Wilson796ed472018-07-16 15:11:09 -07001799
Sylvain Defresne7b0690e2020-06-26 15:33:01 +02001800 xcode_extra_attributes = {
Brett Wilson796ed472018-07-16 15:11:09 -07001801 ONLY_ACTIVE_ARCH = "YES"
1802 DEBUG_INFORMATION_FORMAT = "dwarf"
1803 }
1804 } else {
Sylvain Defresnedcab2f92019-04-03 16:34:05 +02001805 bundle_root_dir = "$root_build_dir/$target_name"
1806 bundle_contents_dir = "$bundle_root_dir/Contents"
1807 bundle_resources_dir = "$bundle_contents_dir/Resources"
1808 bundle_executable_dir = "$bundle_contents_dir/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07001809 }
1810 deps = [ ":${app_name}_bundle_info_plist" ]
1811 if (is_ios && code_signing) {
1812 deps += [ ":${app_name}_generate_executable" ]
1813 code_signing_script = "//build/config/ios/codesign.py"
1814 code_signing_sources = [
1815 invoker.entitlements_path,
1816 "$target_gen_dir/$app_name",
1817 ]
1818 code_signing_outputs = [
1819 "$bundle_root_dir/$app_name",
1820 "$bundle_root_dir/_CodeSignature/CodeResources",
1821 "$bundle_root_dir/embedded.mobileprovision",
1822 "$target_gen_dir/$app_name.xcent",
1823 ]
1824 code_signing_args = [
1825 "-i=" + ios_code_signing_identity,
1826 "-b=" + rebase_path(
1827 "$target_gen_dir/$app_name", root_build_dir),
1828 "-e=" + rebase_path(
1829 invoker.entitlements_path, root_build_dir),
1830 "-e=" + rebase_path(
1831 "$target_gen_dir/$app_name.xcent", root_build_dir),
1832 rebase_path(bundle_root_dir, root_build_dir),
1833 ]
1834 } else {
1835 deps += [ ":${app_name}_bundle_executable" ]
1836 }
1837 }
1838 }
1839 }
1840```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001841### **executable**: Declare an executable target.
Brett Wilson796ed472018-07-16 15:11:09 -07001842
Julie Hockettce1fa072019-05-07 17:44:37 -07001843#### **Language and compilation**
1844
1845```
1846 The tools and commands used to create this target type will be
1847 determined by the source files in its sources. Targets containing
1848 multiple compiler-incompatible languages are not allowed (e.g. a
1849 target containing both C and C++ sources is acceptable, but a
1850 target containing C and Rust sources is not).
1851```
1852
Brett Wilson796ed472018-07-16 15:11:09 -07001853#### **Variables**
1854
1855```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001856 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
1857 cflags_objcc, defines, include_dirs, inputs, ldflags,
1858 lib_dirs, libs, precompiled_header, precompiled_source,
1859 rustenv, rustflags, swiftflags, testonly
1860 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1861 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07001862 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08001863 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001864 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08001865 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07001866 Rust variables: aliased_deps, crate_root, crate_name
Nate Fischer8ed01d32019-01-08 17:32:01 -08001867```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08001868### **generated_file**: Declare a generated_file target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001869
1870```
1871 Writes data value(s) to disk on resolution. This target type mirrors some
1872 functionality of the write_file() function, but also provides the ability to
1873 collect metadata from its dependencies on resolution rather than writing out
Keir Mierle45611e32019-11-12 11:18:00 -08001874 at parse time.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001875
1876 The `outputs` variable is required to be a list with a single element,
1877 specifying the intended location of the output file.
1878
1879 The `output_conversion` variable specified the format to write the
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02001880 value. See `gn help io_conversion`.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001881
Tomasz Åšniatowski46b572c2021-09-03 21:47:17 +02001882 One of `contents` or `data_keys` must be specified; use of `contents` will
1883 write the contents of that value to file, while use of `data_keys` will
1884 trigger a metadata collection walk based on the dependencies of the target and
1885 the optional values of the `rebase` and `walk_keys` variables. See
Nate Fischer8ed01d32019-01-08 17:32:01 -08001886 `gn help metadata`.
1887
1888 Collected metadata, if specified, will be returned in postorder of
1889 dependencies. See the example for details.
1890```
1891
Brett Wilsond0f8bc92022-04-22 14:01:40 -07001892#### **Variables**
1893
1894```
1895 Dependent configs: all_dependent_configs, public_configs
1896 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
1897 write_runtime_deps
1898 General: check_includes, configs, data, friend, inputs, metadata,
1899 output_extension, output_name, public, sources, testonly,
1900 visibility
1901 Generated file: contents, data_keys, rebase, walk_keys, output_conversion
1902```
1903
Nate Fischer8ed01d32019-01-08 17:32:01 -08001904#### **Example (metadata collection)**
1905
1906```
1907 Given the following targets defined in //base/BUILD.gn, where A depends on B
1908 and B depends on C and D:
1909
1910 group("a") {
1911 metadata = {
1912 doom_melon = [ "enable" ]
1913 my_files = [ "foo.cpp" ]
1914
Keir Mierle45611e32019-11-12 11:18:00 -08001915 # Note: this is functionally equivalent to not defining `my_barrier`
1916 # at all in this target's metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08001917 my_barrier = [ "" ]
1918 }
1919
1920 deps = [ ":b" ]
1921 }
1922
Julie Hockettd69a9c32019-01-23 14:36:18 -08001923 group("b") {
Nate Fischer8ed01d32019-01-08 17:32:01 -08001924 metadata = {
1925 my_files = [ "bar.cpp" ]
1926 my_barrier = [ ":c" ]
1927 }
1928
1929 deps = [ ":c", ":d" ]
1930 }
1931
1932 group("c") {
1933 metadata = {
1934 doom_melon = [ "disable" ]
1935 my_files = [ "baz.cpp" ]
1936 }
1937 }
1938
1939 group("d") {
1940 metadata = {
1941 my_files = [ "missing.cpp" ]
1942 }
1943 }
1944
1945 If the following generated_file target is defined:
1946
1947 generated_file("my_files_metadata") {
1948 outputs = [ "$root_build_dir/my_files.json" ]
1949 data_keys = [ "my_files" ]
1950
1951 deps = [ "//base:a" ]
1952 }
1953
1954 The following will be written to "$root_build_dir/my_files.json" (less the
1955 comments):
1956 [
1957 "baz.cpp", // from //base:c via //base:b
1958 "missing.cpp" // from //base:d via //base:b
1959 "bar.cpp", // from //base:b via //base:a
1960 "foo.cpp", // from //base:a
1961 ]
1962
1963 Alternatively, as an example of using walk_keys, if the following
1964 generated_file target is defined:
1965
1966 generated_file("my_files_metadata") {
1967 outputs = [ "$root_build_dir/my_files.json" ]
1968 data_keys = [ "my_files" ]
1969 walk_keys = [ "my_barrier" ]
1970
1971 deps = [ "//base:a" ]
1972 }
1973
1974 The following will be written to "$root_build_dir/my_files.json" (again less
1975 the comments):
1976 [
1977 "baz.cpp", // from //base:c via //base:b
1978 "bar.cpp", // from //base:b via //base:a
1979 "foo.cpp", // from //base:a
1980 ]
1981
1982 If `rebase` is used in the following generated_file target:
1983
1984 generated_file("my_files_metadata") {
1985 outputs = [ "$root_build_dir/my_files.json" ]
1986 data_keys = [ "my_files" ]
1987 walk_keys = [ "my_barrier" ]
1988 rebase = root_build_dir
1989
1990 deps = [ "//base:a" ]
1991 }
1992
1993 The following will be written to "$root_build_dir/my_files.json" (again less
1994 the comments) (assuming root_build_dir = "//out"):
1995 [
1996 "../base/baz.cpp", // from //base:c via //base:b
1997 "../base/bar.cpp", // from //base:b via //base:a
1998 "../base/foo.cpp", // from //base:a
1999 ]
2000```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002001### **group**: Declare a named group of targets.
Brett Wilson796ed472018-07-16 15:11:09 -07002002
2003```
2004 This target type allows you to create meta-targets that just collect a set of
2005 dependencies into one named target. Groups can additionally specify configs
2006 that apply to their dependents.
2007```
2008
2009#### **Variables**
2010
2011```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002012 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2013 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002014 Dependent configs: all_dependent_configs, public_configs
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002015 General: check_includes, configs, data, friend, inputs, metadata,
2016 output_extension, output_name, public, sources, testonly,
2017 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002018```
2019
2020#### **Example**
2021
2022```
2023 group("all") {
2024 deps = [
2025 "//project:runner",
2026 "//project:unit_tests",
2027 ]
2028 }
2029```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002030### **loadable_module**: Declare a loadable module target.
Brett Wilson796ed472018-07-16 15:11:09 -07002031
2032```
2033 This target type allows you to create an object file that is (and can only
2034 be) loaded and unloaded at runtime.
2035
2036 A loadable module will be specified on the linker line for targets listing
2037 the loadable module in its "deps". If you don't want this (if you don't need
2038 to dynamically load the library at runtime), then you should use a
2039 "shared_library" target type instead.
2040```
2041
Julie Hockettce1fa072019-05-07 17:44:37 -07002042#### **Language and compilation**
2043
2044```
2045 The tools and commands used to create this target type will be
2046 determined by the source files in its sources. Targets containing
2047 multiple compiler-incompatible languages are not allowed (e.g. a
2048 target containing both C and C++ sources is acceptable, but a
2049 target containing C and Rust sources is not).
2050```
2051
Brett Wilson796ed472018-07-16 15:11:09 -07002052#### **Variables**
2053
2054```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002055 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2056 cflags_objcc, defines, include_dirs, inputs, ldflags,
2057 lib_dirs, libs, precompiled_header, precompiled_source,
2058 rustenv, rustflags, swiftflags, testonly
2059 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2060 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002061 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002062 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002063 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002064 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002065 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Julie Hockettce1fa072019-05-07 17:44:37 -07002066```
2067### **rust_library**: Declare a Rust library target.
2068
2069```
2070 A Rust library is an archive containing additional rust-c provided metadata.
2071 These are the files produced by the rustc compiler with the `.rlib`
2072 extension, and are the intermediate step for most Rust-based binaries.
2073```
2074
2075#### **Language and compilation**
2076
2077```
2078 The tools and commands used to create this target type will be
2079 determined by the source files in its sources. Targets containing
2080 multiple compiler-incompatible languages are not allowed (e.g. a
2081 target containing both C and C++ sources is acceptable, but a
2082 target containing C and Rust sources is not).
2083```
2084
2085#### **Variables**
2086
2087```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002088 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2089 cflags_objcc, defines, include_dirs, inputs, ldflags,
2090 lib_dirs, libs, precompiled_header, precompiled_source,
2091 rustenv, rustflags, swiftflags, testonly
2092 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2093 write_runtime_deps
Julie Hockettce1fa072019-05-07 17:44:37 -07002094 Dependent configs: all_dependent_configs, public_configs
2095 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002096 output_extension, output_name, public, sources, testonly,
Julie Hockettce1fa072019-05-07 17:44:37 -07002097 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002098 Rust variables: aliased_deps, crate_root, crate_name
Brett Wilson796ed472018-07-16 15:11:09 -07002099```
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002100### **rust_proc_macro**: Declare a Rust procedural macro target.
2101
2102```
2103 A Rust procedural macro allows creating syntax extensions as execution of a
2104 function. They are compiled as dynamic libraries and used by the compiler at
2105 runtime.
2106
2107 Their use is the same as of other Rust libraries, but their build has some
2108 additional restrictions in terms of supported flags.
2109```
2110
2111#### **Language and compilation**
2112
2113```
2114 The tools and commands used to create this target type will be
2115 determined by the source files in its sources. Targets containing
2116 multiple compiler-incompatible languages are not allowed (e.g. a
2117 target containing both C and C++ sources is acceptable, but a
2118 target containing C and Rust sources is not).
2119```
2120
2121#### **Variables**
2122
2123```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002124 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2125 cflags_objcc, defines, include_dirs, inputs, ldflags,
2126 lib_dirs, libs, precompiled_header, precompiled_source,
2127 rustenv, rustflags, swiftflags, testonly
2128 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2129 write_runtime_deps
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002130 Dependent configs: all_dependent_configs, public_configs
2131 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002132 output_extension, output_name, public, sources, testonly,
Petr Hosekfe36c7c2019-11-26 14:38:08 -08002133 visibility
2134 Rust variables: aliased_deps, crate_root, crate_name
2135```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002136### **shared_library**: Declare a shared library target.
Brett Wilson796ed472018-07-16 15:11:09 -07002137
2138```
2139 A shared library will be specified on the linker line for targets listing the
2140 shared library in its "deps". If you don't want this (say you dynamically
2141 load the library at runtime), then you should depend on the shared library
2142 via "data_deps" or, on Darwin platforms, use a "loadable_module" target type
2143 instead.
2144```
2145
Julie Hockettce1fa072019-05-07 17:44:37 -07002146#### **Language and compilation**
2147
2148```
2149 The tools and commands used to create this target type will be
2150 determined by the source files in its sources. Targets containing
2151 multiple compiler-incompatible languages are not allowed (e.g. a
2152 target containing both C and C++ sources is acceptable, but a
2153 target containing C and Rust sources is not).
2154```
2155
Brett Wilson796ed472018-07-16 15:11:09 -07002156#### **Variables**
2157
2158```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002159 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2160 cflags_objcc, defines, include_dirs, inputs, ldflags,
2161 lib_dirs, libs, precompiled_header, precompiled_source,
2162 rustenv, rustflags, swiftflags, testonly
2163 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2164 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002165 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002166 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002167 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002168 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002169 Rust variables: aliased_deps, crate_root, crate_name, crate_type
Brett Wilson796ed472018-07-16 15:11:09 -07002170```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002171### **source_set**: Declare a source set target.
Brett Wilson796ed472018-07-16 15:11:09 -07002172
2173```
Robert Sesekd0a6f072020-05-15 11:21:22 -04002174 Only C-language source sets are supported at the moment.
Julie Hockettce1fa072019-05-07 17:44:37 -07002175```
2176
2177#### **C-language source_sets**
2178
2179```
Brett Wilson796ed472018-07-16 15:11:09 -07002180 A source set is a collection of sources that get compiled, but are not linked
2181 to produce any kind of library. Instead, the resulting object files are
2182 implicitly added to the linker line of all targets that depend on the source
2183 set.
2184
2185 In most cases, a source set will behave like a static library, except no
2186 actual library file will be produced. This will make the build go a little
2187 faster by skipping creation of a large static library, while maintaining the
2188 organizational benefits of focused build targets.
2189
2190 The main difference between a source set and a static library is around
2191 handling of exported symbols. Most linkers assume declaring a function
2192 exported means exported from the static library. The linker can then do dead
2193 code elimination to delete code not reachable from exported functions.
2194
2195 A source set will not do this code elimination since there is no link step.
Julie Hockette2a29402018-07-31 10:11:42 -07002196 This allows you to link many source sets into a shared library and have the
Brett Wilson796ed472018-07-16 15:11:09 -07002197 "exported symbol" notation indicate "export from the final shared library and
2198 not from the intermediate targets." There is no way to express this concept
2199 when linking multiple static libraries into a shared library.
2200```
2201
2202#### **Variables**
2203
2204```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002205 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2206 cflags_objcc, defines, include_dirs, inputs, ldflags,
2207 lib_dirs, libs, precompiled_header, precompiled_source,
2208 rustenv, rustflags, swiftflags, testonly
2209 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2210 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002211 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002212 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002213 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002214 visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002215```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002216### **static_library**: Declare a static library target.
Brett Wilson796ed472018-07-16 15:11:09 -07002217
2218```
2219 Make a ".a" / ".lib" file.
2220
2221 If you only need the static library for intermediate results in the build,
2222 you should consider a source_set instead since it will skip the (potentially
2223 slow) step of creating the intermediate library file.
2224```
2225
2226#### **Variables**
2227
2228```
2229 complete_static_lib
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002230 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2231 cflags_objcc, defines, include_dirs, inputs, ldflags,
2232 lib_dirs, libs, precompiled_header, precompiled_source,
2233 rustenv, rustflags, swiftflags, testonly
2234 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2235 write_runtime_deps
Brett Wilson796ed472018-07-16 15:11:09 -07002236 Dependent configs: all_dependent_configs, public_configs
Nate Fischer8ed01d32019-01-08 17:32:01 -08002237 General: check_includes, configs, data, friend, inputs, metadata,
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002238 output_extension, output_name, public, sources, testonly,
Nate Fischer8ed01d32019-01-08 17:32:01 -08002239 visibility
Petr Hosekadd1bb12019-09-11 14:39:38 -07002240 Rust variables: aliased_deps, crate_root, crate_name
Julie Hockettce1fa072019-05-07 17:44:37 -07002241
2242 The tools and commands used to create this target type will be
2243 determined by the source files in its sources. Targets containing
2244 multiple compiler-incompatible languages are not allowed (e.g. a
2245 target containing both C and C++ sources is acceptable, but a
2246 target containing C and Rust sources is not).
Brett Wilson796ed472018-07-16 15:11:09 -07002247```
Antonio Sartoriecaaf4b2022-07-01 05:57:08 +00002248### **target**: Declare a target with the given programmatic type.
Brett Wilson796ed472018-07-16 15:11:09 -07002249
2250```
2251 target(target_type_string, target_name_string) { ... }
2252
2253 The target() function is a way to invoke a built-in target or template with a
2254 type determined at runtime. This is useful for cases where the type of a
2255 target might not be known statically.
2256
2257 Only templates and built-in target functions are supported for the
2258 target_type_string parameter. Arbitrary functions, configs, and toolchains
2259 are not supported.
2260
2261 The call:
2262 target("source_set", "doom_melon") {
2263 Is equivalent to:
2264 source_set("doom_melon") {
2265```
2266
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002267#### **Common target variables**
2268
2269```
2270 Deps: assert_no_deps, data_deps, deps, public_deps, runtime_deps,
2271 write_runtime_deps
2272 Dependent configs: all_dependent_configs, public_configs
2273 General: check_includes, configs, data, friend, inputs, metadata,
2274 output_extension, output_name, public, sources, testonly,
2275 visibility
2276
2277 Targets will also have variables specific to that type, see "gn help "
2278 for more.
2279```
2280
Brett Wilson796ed472018-07-16 15:11:09 -07002281#### **Example**
2282
2283```
2284 if (foo_build_as_shared) {
2285 my_type = "shared_library"
2286 } else {
2287 my_type = "source_set"
2288 }
2289
2290 target(my_type, "foo") {
2291 ...
2292 }
2293```
2294## Buildfile functions
2295
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002296### **assert**: Assert an expression is true at generation time.
Brett Wilson796ed472018-07-16 15:11:09 -07002297
2298```
2299 assert( [, ])
2300
2301 If the condition is false, the build will fail with an error. If the
2302 optional second argument is provided, that string will be printed
2303 with the error message.
2304```
2305
2306#### **Examples**
2307
2308```
2309 assert(is_win)
2310 assert(defined(sources), "Sources must be defined");
2311```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002312### **config**: Defines a configuration object.
Brett Wilson796ed472018-07-16 15:11:09 -07002313
2314```
2315 Configuration objects can be applied to targets and specify sets of compiler
2316 flags, includes, defines, etc. They provide a way to conveniently group sets
2317 of this configuration information.
2318
2319 A config is referenced by its label just like a target.
2320
2321 The values in a config are additive only. If you want to remove a flag you
2322 need to remove the corresponding config that sets it. The final set of flags,
2323 defines, etc. for a target is generated in this order:
2324
Nico Weber693f9fb2021-10-08 15:33:57 -04002325 1. The values specified directly on the target (rather than using a config).
Brett Wilson796ed472018-07-16 15:11:09 -07002326 2. The configs specified in the target's "configs" list, in order.
2327 3. Public_configs from a breadth-first traversal of the dependency tree in
2328 the order that the targets appear in "deps".
2329 4. All dependent configs from a breadth-first traversal of the dependency
2330 tree in the order that the targets appear in "deps".
2331```
2332
Joe Armstrong39413942019-03-15 10:34:03 +08002333#### **More background**
2334
2335```
2336 Configs solve a problem where the build system needs to have a higher-level
2337 understanding of various compiler settings. For example, some compiler flags
2338 have to appear in a certain order relative to each other, some settings like
2339 defines and flags logically go together, and the build system needs to
2340 de-duplicate flags even though raw command-line parameters can't always be
2341 operated on in that way.
2342
2343 The config gives a name to a group of settings that can then be reasoned
2344 about by GN. GN can know that configs with the same label are the same thing
2345 so can be de-duplicated. It allows related settings to be grouped so they
2346 are added or removed as a unit. And it allows targets to refer to settings
2347 with conceptual names ("no_rtti", "enable_exceptions", etc.) rather than
2348 having to hard-coding every compiler's flags each time they are referred to.
2349```
2350
Brett Wilson796ed472018-07-16 15:11:09 -07002351#### **Variables valid in a config definition**
Joe Armstrong39413942019-03-15 10:34:03 +08002352
Brett Wilson796ed472018-07-16 15:11:09 -07002353```
Brett Wilsond0f8bc92022-04-22 14:01:40 -07002354 Flags: asmflags, cflags, cflags_c, cflags_cc, cflags_objc,
2355 cflags_objcc, defines, include_dirs, inputs, ldflags,
2356 lib_dirs, libs, precompiled_header, precompiled_source,
2357 rustenv, rustflags, swiftflags, testonly
Brett Wilson796ed472018-07-16 15:11:09 -07002358 Nested configs: configs
Tomasz Åšniatowski46b572c2021-09-03 21:47:17 +02002359 General: visibility
Brett Wilson796ed472018-07-16 15:11:09 -07002360```
2361
2362#### **Variables on a target used to apply configs**
2363
2364```
2365 all_dependent_configs, configs, public_configs
2366```
2367
2368#### **Example**
2369
2370```
2371 config("myconfig") {
Joe Armstrongf5ad8072019-05-08 08:42:56 +08002372 include_dirs = [ "include/common" ]
Brett Wilson796ed472018-07-16 15:11:09 -07002373 defines = [ "ENABLE_DOOM_MELON" ]
2374 }
2375
2376 executable("mything") {
2377 configs = [ ":myconfig" ]
2378 }
2379```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002380### **declare_args**: Declare build arguments.
Brett Wilson796ed472018-07-16 15:11:09 -07002381
2382```
2383 Introduces the given arguments into the current scope. If they are not
2384 specified on the command line or in a toolchain's arguments, the default
2385 values given in the declare_args block will be used. However, these defaults
2386 will not override command-line values.
2387
2388 See also "gn help buildargs" for an overview.
2389
2390 The precise behavior of declare args is:
2391
2392 1. The declare_args() block executes. Any variable defined in the enclosing
2393 scope is available for reading, but any variable defined earlier in
2394 the current scope is not (since the overrides haven't been applied yet).
2395
2396 2. At the end of executing the block, any variables set within that scope
Petr Hosek7617b592019-10-02 09:52:21 -07002397 are saved, with the values specified in the block used as the "default value"
2398 for that argument. Once saved, these variables are available for override
2399 via args.gn.
Brett Wilson796ed472018-07-16 15:11:09 -07002400
2401 3. User-defined overrides are applied. Anything set in "gn args" now
2402 overrides any default values. The resulting set of variables is promoted
2403 to be readable from the following code in the file.
2404
2405 This has some ramifications that may not be obvious:
2406
2407 - You should not perform difficult work inside a declare_args block since
2408 this only sets a default value that may be discarded. In particular,
2409 don't use the result of exec_script() to set the default value. If you
2410 want to have a script-defined default, set some default "undefined" value
2411 like [], "", or -1, and after the declare_args block, call exec_script if
2412 the value is unset by the user.
2413
2414 - Because you cannot read the value of a variable defined in the same
2415 block, if you need to make the default value of one arg depend
2416 on the possibly-overridden value of another, write two separate
2417 declare_args() blocks:
2418
2419 declare_args() {
2420 enable_foo = true
2421 }
2422 declare_args() {
2423 # Bar defaults to same user-overridden state as foo.
2424 enable_bar = enable_foo
2425 }
2426```
2427
2428#### **Example**
2429
2430```
2431 declare_args() {
2432 enable_teleporter = true
2433 enable_doom_melon = false
2434 }
2435
2436 If you want to override the (default disabled) Doom Melon:
2437 gn --args="enable_doom_melon=true enable_teleporter=true"
2438 This also sets the teleporter, but it's already defaulted to on so it will
2439 have no effect.
2440```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002441### **defined**: Returns whether an identifier is defined.
Brett Wilson796ed472018-07-16 15:11:09 -07002442
2443```
2444 Returns true if the given argument is defined. This is most useful in
2445 templates to assert that the caller set things up properly.
2446
2447 You can pass an identifier:
2448 defined(foo)
2449 which will return true or false depending on whether foo is defined in the
2450 current scope.
2451
2452 You can also check a named scope:
2453 defined(foo.bar)
2454 which will return true or false depending on whether bar is defined in the
2455 named scope foo. It will throw an error if foo is not defined or is not a
2456 scope.
2457```
2458
2459#### **Example**
2460
2461```
2462 template("mytemplate") {
2463 # To help users call this template properly...
2464 assert(defined(invoker.sources), "Sources must be defined")
2465
2466 # If we want to accept an optional "values" argument, we don't
2467 # want to dereference something that may not be defined.
2468 if (defined(invoker.values)) {
2469 values = invoker.values
2470 } else {
2471 values = "some default value"
2472 }
2473 }
2474```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002475### **exec_script**: Synchronously run a script and return the output.
Brett Wilson796ed472018-07-16 15:11:09 -07002476
2477```
2478 exec_script(filename,
2479 arguments = [],
2480 input_conversion = "",
2481 file_dependencies = [])
2482
2483 Runs the given script, returning the stdout of the script. The build
2484 generation will fail if the script does not exist or returns a nonzero exit
2485 code.
2486
2487 The current directory when executing the script will be the root build
2488 directory. If you are passing file names, you will want to use the
2489 rebase_path() function to make file names relative to this path (see "gn help
2490 rebase_path").
Gary Miguelce7fa362018-09-17 12:48:17 -07002491
2492 The default script interpreter is Python ("python" on POSIX, "python.exe" or
2493 "python.bat" on Windows). This can be configured by the script_executable
2494 variable, see "gn help dotfile".
Brett Wilson796ed472018-07-16 15:11:09 -07002495```
2496
2497#### **Arguments**:
2498
2499```
2500 filename:
Gary Miguelce7fa362018-09-17 12:48:17 -07002501 File name of script to execute. Non-absolute names will be treated as
2502 relative to the current build file.
Brett Wilson796ed472018-07-16 15:11:09 -07002503
2504 arguments:
2505 A list of strings to be passed to the script as arguments. May be
2506 unspecified or the empty list which means no arguments.
2507
2508 input_conversion:
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02002509 Controls how the file is read and parsed. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07002510
2511 If unspecified, defaults to the empty string which causes the script
2512 result to be discarded. exec script will return None.
2513
2514 dependencies:
2515 (Optional) A list of files that this script reads or otherwise depends
2516 on. These dependencies will be added to the build result such that if any
2517 of them change, the build will be regenerated and the script will be
2518 re-run.
2519
2520 The script itself will be an implicit dependency so you do not need to
2521 list it.
2522```
2523
2524#### **Example**
2525
2526```
2527 all_lines = exec_script(
2528 "myscript.py", [some_input], "list lines",
2529 [ rebase_path("data_file.txt", root_build_dir) ])
2530
2531 # This example just calls the script with no arguments and discards the
2532 # result.
2533 exec_script("//foo/bar/myscript.py")
2534```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01002535### **filter_exclude**: Remove values that match a set of patterns.
2536
2537```
2538 filter_exclude(values, exclude_patterns)
2539
2540 The argument values must be a list of strings.
2541
2542 The argument exclude_patterns must be a list of file patterns (see
2543 "gn help file_pattern"). Any elements in values matching at least one
2544 of those patterns will be excluded.
2545```
2546
2547#### **Examples**
2548```
2549 values = [ "foo.cc", "foo.h", "foo.proto" ]
2550 result = filter_exclude(values, [ "*.proto" ])
2551 # result will be [ "foo.cc", "foo.h" ]
2552```
2553### **filter_include**: Remove values that do not match a set of patterns.
2554
2555```
2556 filter_include(values, include_patterns)
2557
2558 The argument values must be a list of strings.
2559
2560 The argument include_patterns must be a list of file patterns (see
2561 "gn help file_pattern"). Only elements from values matching at least
2562 one of the pattern will be included.
2563```
2564
2565#### **Examples**
2566```
2567 values = [ "foo.cc", "foo.h", "foo.proto" ]
2568 result = filter_include(values, [ "*.proto" ])
2569 # result will be [ "foo.proto" ]
2570```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002571### **foreach**: Iterate over a list.
Brett Wilson796ed472018-07-16 15:11:09 -07002572
2573```
2574 foreach(, ) {
2575
2576 }
2577
2578 Executes the loop contents block over each item in the list, assigning the
2579 loop_var to each item in sequence. The will be a copy so assigning
2580 to it will not mutate the list. The loop will iterate over a copy of
2581 so mutating it inside the loop will not affect iteration.
2582
2583 The block does not introduce a new scope, so that variable assignments inside
2584 the loop will be visible once the loop terminates.
2585
2586 The loop variable will temporarily shadow any existing variables with the
2587 same name for the duration of the loop. After the loop terminates the loop
2588 variable will no longer be in scope, and the previous value (if any) will be
2589 restored.
2590```
2591
2592#### **Example**
2593
2594```
2595 mylist = [ "a", "b", "c" ]
2596 foreach(i, mylist) {
2597 print(i)
2598 }
2599
2600 Prints:
2601 a
2602 b
2603 c
2604```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002605### **forward_variables_from**: Copies variables from a different scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002606
2607```
2608 forward_variables_from(from_scope, variable_list_or_star,
2609 variable_to_not_forward_list = [])
2610
2611 Copies the given variables from the given scope to the local scope if they
2612 exist. This is normally used in the context of templates to use the values of
2613 variables defined in the template invocation to a template-defined target.
2614
2615 The variables in the given variable_list will be copied if they exist in the
2616 given scope or any enclosing scope. If they do not exist, nothing will happen
2617 and they be left undefined in the current scope.
2618
2619 As a special case, if the variable_list is a string with the value of "*",
2620 all variables from the given scope will be copied. "*" only copies variables
2621 set directly on the from_scope, not enclosing ones. Otherwise it would
2622 duplicate all global variables.
2623
2624 When an explicit list of variables is supplied, if the variable exists in the
2625 current (destination) scope already, an error will be thrown. If "*" is
2626 specified, variables in the current scope will be clobbered (the latter is
2627 important because most targets have an implicit configs list, which means it
2628 wouldn't work at all if it didn't clobber).
2629
Brett Wilson796ed472018-07-16 15:11:09 -07002630 If variables_to_not_forward_list is non-empty, then it must contains a list
2631 of variable names that will not be forwarded. This is mostly useful when
2632 variable_list_or_star has a value of "*".
2633```
2634
2635#### **Examples**
2636
2637```
Andrew Grieve57a964c2018-09-14 10:50:56 -04002638 # forward_variables_from(invoker, ["foo"])
2639 # is equivalent to:
2640 assert(!defined(foo))
2641 if (defined(invoker.foo)) {
2642 foo = invoker.foo
2643 }
2644
Brett Wilson796ed472018-07-16 15:11:09 -07002645 # This is a common action template. It would invoke a script with some given
2646 # parameters, and wants to use the various types of deps and the visibility
2647 # from the invoker if it's defined. It also injects an additional dependency
2648 # to all targets.
2649 template("my_test") {
2650 action(target_name) {
2651 forward_variables_from(invoker, [ "data_deps", "deps",
Julie Hockette2a29402018-07-31 10:11:42 -07002652 "public_deps", "visibility"])
Brett Wilson796ed472018-07-16 15:11:09 -07002653 # Add our test code to the dependencies.
2654 # "deps" may or may not be defined at this point.
2655 if (defined(deps)) {
2656 deps += [ "//tools/doom_melon" ]
2657 } else {
2658 deps = [ "//tools/doom_melon" ]
2659 }
2660 }
2661 }
2662
Julie Hockette2a29402018-07-31 10:11:42 -07002663 # This is a template around a target whose type depends on a global variable.
2664 # It forwards all values from the invoker.
Brett Wilson796ed472018-07-16 15:11:09 -07002665 template("my_wrapper") {
2666 target(my_wrapper_target_type, target_name) {
2667 forward_variables_from(invoker, "*")
2668 }
2669 }
2670
2671 # A template that wraps another. It adds behavior based on one
2672 # variable, and forwards all others to the nested target.
2673 template("my_ios_test_app") {
2674 ios_test_app(target_name) {
2675 forward_variables_from(invoker, "*", ["test_bundle_name"])
2676 if (!defined(extra_substitutions)) {
2677 extra_substitutions = []
2678 }
2679 extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$test_bundle_name" ]
2680 }
2681 }
2682```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002683### **get_label_info**: Get an attribute from a target's label.
Brett Wilson796ed472018-07-16 15:11:09 -07002684
2685```
2686 get_label_info(target_label, what)
2687
2688 Given the label of a target, returns some attribute of that target. The
2689 target need not have been previously defined in the same file, since none of
2690 the attributes depend on the actual target definition, only the label itself.
2691
2692 See also "gn help get_target_outputs".
2693```
2694
2695#### **Possible values for the "what" parameter**
2696
2697```
2698 "name"
2699 The short name of the target. This will match the value of the
2700 "target_name" variable inside that target's declaration. For the label
2701 "//foo/bar:baz" this will return "baz".
2702
2703 "dir"
2704 The directory containing the target's definition, with no slash at the
2705 end. For the label "//foo/bar:baz" this will return "//foo/bar".
2706
2707 "target_gen_dir"
2708 The generated file directory for the target. This will match the value of
2709 the "target_gen_dir" variable when inside that target's declaration.
2710
2711 "root_gen_dir"
2712 The root of the generated file tree for the target. This will match the
2713 value of the "root_gen_dir" variable when inside that target's
2714 declaration.
2715
2716 "target_out_dir
2717 The output directory for the target. This will match the value of the
2718 "target_out_dir" variable when inside that target's declaration.
2719
2720 "root_out_dir"
2721 The root of the output file tree for the target. This will match the
2722 value of the "root_out_dir" variable when inside that target's
2723 declaration.
2724
2725 "label_no_toolchain"
2726 The fully qualified version of this label, not including the toolchain.
2727 For the input ":bar" it might return "//foo:bar".
2728
2729 "label_with_toolchain"
2730 The fully qualified version of this label, including the toolchain. For
2731 the input ":bar" it might return "//foo:bar(//toolchain:x64)".
2732
2733 "toolchain"
2734 The label of the toolchain. This will match the value of the
2735 "current_toolchain" variable when inside that target's declaration.
2736```
2737
2738#### **Examples**
2739
2740```
2741 get_label_info(":foo", "name")
2742 # Returns string "foo".
2743
2744 get_label_info("//foo/bar:baz", "target_gen_dir")
2745 # Returns string "//out/Debug/gen/foo/bar".
2746```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002747### **get_path_info**: Extract parts of a file or directory name.
Brett Wilson796ed472018-07-16 15:11:09 -07002748
2749```
2750 get_path_info(input, what)
2751
2752 The first argument is either a string representing a file or directory name,
2753 or a list of such strings. If the input is a list the return value will be a
2754 list containing the result of applying the rule to each item in the input.
2755```
2756
2757#### **Possible values for the "what" parameter**
2758
2759```
2760 "file"
2761 The substring after the last slash in the path, including the name and
2762 extension. If the input ends in a slash, the empty string will be
2763 returned.
2764 "foo/bar.txt" => "bar.txt"
2765 "bar.txt" => "bar.txt"
2766 "foo/" => ""
2767 "" => ""
2768
2769 "name"
2770 The substring of the file name not including the extension.
2771 "foo/bar.txt" => "bar"
2772 "foo/bar" => "bar"
2773 "foo/" => ""
2774
2775 "extension"
2776 The substring following the last period following the last slash, or the
2777 empty string if not found. The period is not included.
2778 "foo/bar.txt" => "txt"
2779 "foo/bar" => ""
2780
2781 "dir"
2782 The directory portion of the name, not including the slash.
2783 "foo/bar.txt" => "foo"
2784 "//foo/bar" => "//foo"
2785 "foo" => "."
2786
2787 The result will never end in a slash, so if the resulting is empty, the
2788 system ("/") or source ("//") roots, a "." will be appended such that it
2789 is always legal to append a slash and a filename and get a valid path.
2790
2791 "out_dir"
2792 The output file directory corresponding to the path of the given file,
2793 not including a trailing slash.
2794 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
2795
2796 "gen_dir"
2797 The generated file directory corresponding to the path of the given file,
2798 not including a trailing slash.
2799 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
2800
2801 "abspath"
2802 The full absolute path name to the file or directory. It will be resolved
2803 relative to the current directory, and then the source- absolute version
2804 will be returned. If the input is system- absolute, the same input will
2805 be returned.
2806 "foo/bar.txt" => "//mydir/foo/bar.txt"
2807 "foo/" => "//mydir/foo/"
2808 "//foo/bar" => "//foo/bar" (already absolute)
2809 "/usr/include" => "/usr/include" (already absolute)
2810
2811 If you want to make the path relative to another directory, or to be
2812 system-absolute, see rebase_path().
2813```
2814
2815#### **Examples**
2816```
2817 sources = [ "foo.cc", "foo.h" ]
2818 result = get_path_info(source, "abspath")
2819 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
2820
2821 result = get_path_info("//foo/bar/baz.cc", "dir")
2822 # result will be "//foo/bar"
2823
2824 # Extract the source-absolute directory name,
Joe Armstrong39413942019-03-15 10:34:03 +08002825 result = get_path_info(get_path_info(path, "dir"), "abspath")
Brett Wilson796ed472018-07-16 15:11:09 -07002826```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002827### **get_target_outputs**: [file list] Get the list of outputs from a target.
Brett Wilson796ed472018-07-16 15:11:09 -07002828
2829```
2830 get_target_outputs(target_label)
2831
2832 Returns a list of output files for the named target. The named target must
2833 have been previously defined in the current file before this function is
2834 called (it can't reference targets in other files because there isn't a
2835 defined execution order, and it obviously can't reference targets that are
2836 defined after the function call).
2837
Julie Hockettce1fa072019-05-07 17:44:37 -07002838 Only copy, generated_file, and action targets are supported. The outputs from
2839 binary targets will depend on the toolchain definition which won't
2840 necessarily have been loaded by the time a given line of code has run, and
2841 source sets and groups have no useful output file.
Brett Wilson796ed472018-07-16 15:11:09 -07002842```
2843
2844#### **Return value**
2845
2846```
2847 The names in the resulting list will be absolute file paths (normally like
2848 "//out/Debug/bar.exe", depending on the build directory).
2849
Julie Hockettce1fa072019-05-07 17:44:37 -07002850 action, copy, and generated_file targets: this will just return the files
2851 specified in the "outputs" variable of the target.
Brett Wilson796ed472018-07-16 15:11:09 -07002852
2853 action_foreach targets: this will return the result of applying the output
2854 template to the sources (see "gn help source_expansion"). This will be the
2855 same result (though with guaranteed absolute file paths), as
2856 process_file_template will return for those inputs (see "gn help
2857 process_file_template").
2858
Brett Wilson796ed472018-07-16 15:11:09 -07002859 source sets and groups: this will return a list containing the path of the
RJ Ascani843b5002020-11-04 14:49:23 -08002860 "stamp" file that Ninja will produce once all outputs are generated. This
Brett Wilson796ed472018-07-16 15:11:09 -07002861 probably isn't very useful.
2862```
2863
2864#### **Example**
2865
2866```
2867 # Say this action generates a bunch of C source files.
2868 action_foreach("my_action") {
2869 sources = [ ... ]
2870 outputs = [ ... ]
2871 }
2872
2873 # Compile the resulting source files into a source set.
2874 source_set("my_lib") {
2875 sources = get_target_outputs(":my_action")
2876 }
2877```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002878### **getenv**: Get an environment variable.
Brett Wilson796ed472018-07-16 15:11:09 -07002879
2880```
2881 value = getenv(env_var_name)
2882
2883 Returns the value of the given environment variable. If the value is not
2884 found, it will try to look up the variable with the "opposite" case (based on
2885 the case of the first letter of the variable), but is otherwise
2886 case-sensitive.
2887
2888 If the environment variable is not found, the empty string will be returned.
2889 Note: it might be nice to extend this if we had the concept of "none" in the
2890 language to indicate lookup failure.
2891```
2892
2893#### **Example**
2894
2895```
2896 home_dir = getenv("HOME")
2897```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002898### **import**: Import a file into the current scope.
Brett Wilson796ed472018-07-16 15:11:09 -07002899
2900```
2901 The import command loads the rules and variables resulting from executing the
2902 given file into the current scope.
2903
2904 By convention, imported files are named with a .gni extension.
2905
2906 An import is different than a C++ "include". The imported file is executed in
2907 a standalone environment from the caller of the import command. The results
2908 of this execution are cached for other files that import the same .gni file.
2909
2910 Note that you can not import a BUILD.gn file that's otherwise used in the
2911 build. Files must either be imported or implicitly loaded as a result of deps
2912 rules, but not both.
2913
2914 The imported file's scope will be merged with the scope at the point import
2915 was called. If there is a conflict (both the current scope and the imported
2916 file define some variable or rule with the same name but different value), a
2917 runtime error will be thrown. Therefore, it's good practice to minimize the
2918 stuff that an imported file defines.
2919
2920 Variables and templates beginning with an underscore '_' are considered
2921 private and will not be imported. Imported files can use such variables for
2922 internal computation without affecting other files.
2923```
2924
2925#### **Examples**
2926
2927```
2928 import("//build/rules/idl_compilation_rule.gni")
2929
2930 # Looks in the current directory.
2931 import("my_vars.gni")
2932```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002933### **not_needed**: Mark variables from scope as not needed.
Brett Wilson796ed472018-07-16 15:11:09 -07002934
2935```
2936 not_needed(variable_list_or_star, variable_to_ignore_list = [])
2937 not_needed(from_scope, variable_list_or_star,
2938 variable_to_ignore_list = [])
2939
2940 Mark the variables in the current or given scope as not needed, which means
2941 you will not get an error about unused variables for these. The
2942 variable_to_ignore_list allows excluding variables from "all matches" if
2943 variable_list_or_star is "*".
2944```
2945
2946#### **Example**
2947
2948```
2949 not_needed("*", [ "config" ])
2950 not_needed([ "data_deps", "deps" ])
2951 not_needed(invoker, "*", [ "config" ])
2952 not_needed(invoker, [ "data_deps", "deps" ])
2953```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002954### **pool**: Defines a pool object.
Brett Wilson796ed472018-07-16 15:11:09 -07002955
2956```
2957 Pool objects can be applied to a tool to limit the parallelism of the
2958 build. This object has a single property "depth" corresponding to
2959 the number of tasks that may run simultaneously.
2960
2961 As the file containing the pool definition may be executed in the
2962 context of more than one toolchain it is recommended to specify an
2963 explicit toolchain when defining and referencing a pool.
2964
Julie Hockette2a29402018-07-31 10:11:42 -07002965 A pool named "console" defined in the root build file represents Ninja's
2966 console pool. Targets using this pool will have access to the console's
2967 stdin and stdout, and output will not be buffered. This special pool must
2968 have a depth of 1. Pools not defined in the root must not be named "console".
2969 The console pool can only be defined for the default toolchain.
2970 Refer to the Ninja documentation on the console pool for more info.
2971
Brett Wilson796ed472018-07-16 15:11:09 -07002972 A pool is referenced by its label just like a target.
2973```
2974
2975#### **Variables**
2976
2977```
2978 depth*
2979 * = required
2980```
2981
2982#### **Example**
2983
2984```
2985 if (current_toolchain == default_toolchain) {
2986 pool("link_pool") {
2987 depth = 1
2988 }
2989 }
2990
2991 toolchain("toolchain") {
2992 tool("link") {
2993 command = "..."
Petr Hosekadd1bb12019-09-11 14:39:38 -07002994 pool = ":link_pool($default_toolchain)"
Brett Wilson796ed472018-07-16 15:11:09 -07002995 }
2996 }
2997```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08002998### **print**: Prints to the console.
Brett Wilson796ed472018-07-16 15:11:09 -07002999
3000```
3001 Prints all arguments to the console separated by spaces. A newline is
3002 automatically appended to the end.
3003
3004 This function is intended for debugging. Note that build files are run in
3005 parallel so you may get interleaved prints. A buildfile may also be executed
3006 more than once in parallel in the context of different toolchains so the
3007 prints from one file may be duplicated or
3008 interleaved with itself.
3009```
3010
3011#### **Examples**
3012
3013```
3014 print("Hello world")
3015
3016 print(sources, deps)
3017```
Aaron Wood24062bb2022-04-25 20:37:48 -07003018### **print_stack_trace**: Prints a stack trace.
3019
3020```
3021 Prints the current file location, and all template invocations that led up to
3022 this location, to the console.
3023```
3024
3025#### **Examples**
3026
3027```
3028 template("foo"){
3029 print_stack_trace()
3030 }
3031 template("bar"){
3032 foo(target_name + ".foo") {
3033 baz = invoker.baz
3034 }
3035 }
3036 bar("lala") {
3037 baz = 42
3038 }
3039
3040 will print out the following:
3041
3042 print_stack_trace() initiated at //build.gn:2
3043 bar("lala") //BUILD.gn:9
3044 foo("lala.foo") //BUILD.gn:5
3045 print_stack_trace() //BUILD.gn:2
3046```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003047### **process_file_template**: Do template expansion over a list of files.
Brett Wilson796ed472018-07-16 15:11:09 -07003048
3049```
3050 process_file_template(source_list, template)
3051
3052 process_file_template applies a template list to a source file list,
3053 returning the result of applying each template to each source. This is
3054 typically used for computing output file names from input files.
3055
3056 In most cases, get_target_outputs() will give the same result with shorter,
3057 more maintainable code. This function should only be used when that function
3058 can't be used (like there's no target or the target is defined in another
3059 build file).
3060```
3061
3062#### **Arguments**
3063
3064```
3065 The source_list is a list of file names.
3066
3067 The template can be a string or a list. If it is a list, multiple output
3068 strings are generated for each input.
3069
3070 The template should contain source expansions to which each name in the
3071 source list is applied. See "gn help source_expansion".
3072```
3073
3074#### **Example**
3075
3076```
3077 sources = [
3078 "foo.idl",
3079 "bar.idl",
3080 ]
3081 myoutputs = process_file_template(
3082 sources,
3083 [ "$target_gen_dir/{{source_name_part}}.cc",
3084 "$target_gen_dir/{{source_name_part}}.h" ])
3085
3086 The result in this case will be:
3087 [ "//out/Debug/foo.cc"
3088 "//out/Debug/foo.h"
3089 "//out/Debug/bar.cc"
3090 "//out/Debug/bar.h" ]
3091```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003092### **read_file**: Read a file into a variable.
Brett Wilson796ed472018-07-16 15:11:09 -07003093
3094```
3095 read_file(filename, input_conversion)
3096
3097 Whitespace will be trimmed from the end of the file. Throws an error if the
3098 file can not be opened.
3099```
3100
3101#### **Arguments**
3102
3103```
3104 filename
3105 Filename to read, relative to the build file.
3106
3107 input_conversion
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02003108 Controls how the file is read and parsed. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07003109```
3110
3111#### **Example**
3112
3113```
3114 lines = read_file("foo.txt", "list lines")
3115```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003116### **rebase_path**: Rebase a file or directory to another location.
Brett Wilson796ed472018-07-16 15:11:09 -07003117
3118```
3119 converted = rebase_path(input,
3120 new_base = "",
3121 current_base = ".")
3122
3123 Takes a string argument representing a file name, or a list of such strings
3124 and converts it/them to be relative to a different base directory.
3125
3126 When invoking the compiler or scripts, GN will automatically convert sources
3127 and include directories to be relative to the build directory. However, if
3128 you're passing files directly in the "args" array or doing other manual
3129 manipulations where GN doesn't know something is a file name, you will need
3130 to convert paths to be relative to what your tool is expecting.
3131
3132 The common case is to use this to convert paths relative to the current
3133 directory to be relative to the build directory (which will be the current
3134 directory when executing scripts).
3135
3136 If you want to convert a file path to be source-absolute (that is, beginning
3137 with a double slash like "//foo/bar"), you should use the get_path_info()
3138 function. This function won't work because it will always make relative
3139 paths, and it needs to support making paths relative to the source root, so
Robert Sesekd0a6f072020-05-15 11:21:22 -04003140 it can't also generate source-absolute paths without more special-cases.
Brett Wilson796ed472018-07-16 15:11:09 -07003141```
3142
3143#### **Arguments**
3144
3145```
3146 input
Robert Sesekd0a6f072020-05-15 11:21:22 -04003147 A string or list of strings representing file or directory names. These
Brett Wilson796ed472018-07-16 15:11:09 -07003148 can be relative paths ("foo/bar.txt"), system absolute paths
3149 ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt").
3150
3151 new_base
3152 The directory to convert the paths to be relative to. This can be an
3153 absolute path or a relative path (which will be treated as being relative
3154 to the current BUILD-file's directory).
3155
3156 As a special case, if new_base is the empty string (the default), all
3157 paths will be converted to system-absolute native style paths with system
3158 path separators. This is useful for invoking external programs.
3159
3160 current_base
3161 Directory representing the base for relative paths in the input. If this
3162 is not an absolute path, it will be treated as being relative to the
3163 current build file. Use "." (the default) to convert paths from the
3164 current BUILD-file's directory.
3165```
3166
3167#### **Return value**
3168
3169```
3170 The return value will be the same type as the input value (either a string or
3171 a list of strings). All relative and source-absolute file names will be
3172 converted to be relative to the requested output System-absolute paths will
3173 be unchanged.
3174
3175 Whether an output path will end in a slash will match whether the
3176 corresponding input path ends in a slash. It will return "." or "./"
3177 (depending on whether the input ends in a slash) to avoid returning empty
3178 strings. This means if you want a root path ("//" or "/") not ending in a
3179 slash, you can add a dot ("//.").
3180```
3181
3182#### **Example**
3183
3184```
3185 # Convert a file in the current directory to be relative to the build
3186 # directory (the current dir when executing compilers and scripts).
3187 foo = rebase_path("myfile.txt", root_build_dir)
3188 # might produce "../../project/myfile.txt".
3189
3190 # Convert a file to be system absolute:
3191 foo = rebase_path("myfile.txt")
3192 # Might produce "D:\\source\\project\\myfile.txt" on Windows or
3193 # "/home/you/source/project/myfile.txt" on Linux.
3194
3195 # Typical usage for converting to the build directory for a script.
3196 action("myscript") {
3197 # Don't convert sources, GN will automatically convert these to be relative
3198 # to the build directory when it constructs the command line for your
3199 # script.
3200 sources = [ "foo.txt", "bar.txt" ]
3201
3202 # Extra file args passed manually need to be explicitly converted
3203 # to be relative to the build directory:
3204 args = [
3205 "--data",
3206 rebase_path("//mything/data/input.dat", root_build_dir),
3207 "--rel",
3208 rebase_path("relative_path.txt", root_build_dir)
3209 ] + rebase_path(sources, root_build_dir)
3210 }
3211```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003212### **set_default_toolchain**: Sets the default toolchain name.
Brett Wilson796ed472018-07-16 15:11:09 -07003213
3214```
3215 set_default_toolchain(toolchain_label)
3216
3217 The given label should identify a toolchain definition (see "gn help
3218 toolchain"). This toolchain will be used for all targets unless otherwise
3219 specified.
3220
3221 This function is only valid to call during the processing of the build
3222 configuration file. Since the build configuration file is processed
3223 separately for each toolchain, this function will be a no-op when called
3224 under any non-default toolchains.
3225
3226 For example, the default toolchain should be appropriate for the current
3227 environment. If the current environment is 32-bit and somebody references a
3228 target with a 64-bit toolchain, we wouldn't want processing of the build
3229 config file for the 64-bit toolchain to reset the default toolchain to
3230 64-bit, we want to keep it 32-bits.
3231```
3232
3233#### **Argument**
3234
3235```
3236 toolchain_label
3237 Toolchain name.
3238```
3239
3240#### **Example**
3241
3242```
3243 # Set default toolchain only has an effect when run in the context of the
3244 # default toolchain. Pick the right one according to the current CPU
3245 # architecture.
3246 if (target_cpu == "x64") {
3247 set_default_toolchain("//toolchains:64")
3248 } else if (target_cpu == "x86") {
3249 set_default_toolchain("//toolchains:32")
3250 }
3251```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003252### **set_defaults**: Set default values for a target type.
Brett Wilson796ed472018-07-16 15:11:09 -07003253
3254```
3255 set_defaults() { }
3256
3257 Sets the default values for a given target type. Whenever target_type_name is
3258 seen in the future, the values specified in set_default's block will be
3259 copied into the current scope.
3260
3261 When the target type is used, the variable copying is very strict. If a
3262 variable with that name is already in scope, the build will fail with an
3263 error.
3264
3265 set_defaults can be used for built-in target types ("executable",
3266 "shared_library", etc.) and custom ones defined via the "template" command.
3267 It can be called more than once and the most recent call in any scope will
3268 apply, but there is no way to refer to the previous defaults and modify them
3269 (each call to set_defaults must supply a complete list of all defaults it
3270 wants). If you want to share defaults, store them in a separate variable.
3271```
3272
3273#### **Example**
3274
3275```
3276 set_defaults("static_library") {
3277 configs = [ "//tools/mything:settings" ]
3278 }
3279
Nico Webere49cb722018-08-28 13:10:29 -04003280 static_library("mylib") {
Brett Wilson796ed472018-07-16 15:11:09 -07003281 # The configs will be auto-populated as above. You can remove it if
3282 # you don't want the default for a particular default:
3283 configs -= [ "//tools/mything:settings" ]
3284 }
3285```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003286### **split_list**: Splits a list into N different sub-lists.
Brett Wilson796ed472018-07-16 15:11:09 -07003287
3288```
3289 result = split_list(input, n)
3290
3291 Given a list and a number N, splits the list into N sub-lists of
3292 approximately equal size. The return value is a list of the sub-lists. The
3293 result will always be a list of size N. If N is greater than the number of
3294 elements in the input, it will be padded with empty lists.
3295
3296 The expected use is to divide source files into smaller uniform chunks.
3297```
3298
3299#### **Example**
3300
3301```
3302 The code:
3303 mylist = [1, 2, 3, 4, 5, 6]
3304 print(split_list(mylist, 3))
3305
3306 Will print:
3307 [[1, 2], [3, 4], [5, 6]
3308```
Keir Mierle6ae63302019-11-08 23:02:18 -08003309### **string_join**: Concatenates a list of strings with a separator.
3310
3311```
3312 result = string_join(separator, strings)
3313
3314 Concatenate a list of strings with intervening occurrences of separator.
3315```
3316
3317#### **Examples**
3318
3319```
3320 string_join("", ["a", "b", "c"]) --> "abc"
3321 string_join("|", ["a", "b", "c"]) --> "a|b|c"
3322 string_join(", ", ["a", "b", "c"]) --> "a, b, c"
3323 string_join("s", ["", ""]) --> "s"
3324```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003325### **string_replace**: Replaces substring in the given string.
Petr Hosekc0744ed2018-08-04 16:13:59 -07003326
3327```
3328 result = string_replace(str, old, new[, max])
3329
3330 Returns a copy of the string str in which the occurrences of old have been
3331 replaced with new, optionally restricting the number of replacements. The
3332 replacement is performed sequentially, so if new contains old, it won't be
3333 replaced.
3334```
3335
3336#### **Example**
3337
3338```
3339 The code:
3340 mystr = "Hello, world!"
3341 print(string_replace(mystr, "world", "GN"))
3342
3343 Will print:
3344 Hello, GN!
3345```
Keir Mierle6ae63302019-11-08 23:02:18 -08003346### **string_split**: Split string into a list of strings.
3347
3348```
3349 result = string_split(str[, sep])
3350
3351 Split string into all substrings separated by separator and returns a list
3352 of the substrings between those separators.
3353
3354 If the separator argument is omitted, the split is by any whitespace, and
3355 any leading/trailing whitespace is ignored; similar to Python's str.split().
3356```
3357
3358#### **Examples without a separator (split on whitespace)**:
3359
3360```
3361 string_split("") --> []
3362 string_split("a") --> ["a"]
3363 string_split(" aa bb") --> ["aa", "bb"]
3364```
3365
3366#### **Examples with a separator (split on separators)**:
3367
3368```
3369 string_split("", "|") --> [""]
3370 string_split(" a b ", " ") --> ["", "", "a", "b", "", ""]
3371 string_split("aa+-bb+-c", "+-") --> ["aa", "bb", "c"]
3372```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003373### **template**: Define a template rule.
Brett Wilson796ed472018-07-16 15:11:09 -07003374
3375```
3376 A template defines a custom name that acts like a function. It provides a way
3377 to add to the built-in target types.
3378
3379 The template() function is used to declare a template. To invoke the
3380 template, just use the name of the template like any other target type.
3381
3382 Often you will want to declare your template in a special file that other
3383 files will import (see "gn help import") so your template rule can be shared
3384 across build files.
3385```
3386
3387#### **Variables and templates**:
3388
3389```
3390 When you call template() it creates a closure around all variables currently
3391 in scope with the code in the template block. When the template is invoked,
3392 the closure will be executed.
3393
3394 When the template is invoked, the code in the caller is executed and passed
3395 to the template code as an implicit "invoker" variable. The template uses
3396 this to read state out of the invoking code.
3397
3398 One thing explicitly excluded from the closure is the "current directory"
3399 against which relative file names are resolved. The current directory will be
3400 that of the invoking code, since typically that code specifies the file
3401 names. This means all files internal to the template should use absolute
3402 names.
3403
3404 A template will typically forward some or all variables from the invoking
3405 scope to a target that it defines. Often, such variables might be optional.
3406 Use the pattern:
3407
3408 if (defined(invoker.deps)) {
3409 deps = invoker.deps
3410 }
3411
3412 The function forward_variables_from() provides a shortcut to forward one or
3413 more or possibly all variables in this manner:
3414
3415 forward_variables_from(invoker, ["deps", "public_deps"])
3416```
3417
3418#### **Target naming**
3419
3420```
3421 Your template should almost always define a built-in target with the name the
3422 template invoker specified. For example, if you have an IDL template and
3423 somebody does:
3424 idl("foo") {...
3425 you will normally want this to expand to something defining a source_set or
3426 static_library named "foo" (among other things you may need). This way, when
3427 another target specifies a dependency on "foo", the static_library or
3428 source_set will be linked.
3429
3430 It is also important that any other targets your template expands to have
3431 unique names, or you will get collisions.
3432
3433 Access the invoking name in your template via the implicit "target_name"
3434 variable. This should also be the basis for how other targets that a template
3435 expands to ensure uniqueness.
3436
3437 A typical example would be a template that defines an action to generate some
3438 source files, and a source_set to compile that source. Your template would
3439 name the source_set "target_name" because that's what you want external
3440 targets to depend on to link your code. And you would name the action
3441 something like "${target_name}_action" to make it unique. The source set
3442 would have a dependency on the action to make it run.
3443```
3444
3445#### **Overriding builtin targets**
3446
3447```
3448 You can use template to redefine a built-in target in which case your template
3449 takes a precedence over the built-in one. All uses of the target from within
3450 the template definition will refer to the built-in target which makes it
3451 possible to extend the behavior of the built-in target:
3452
3453 template("shared_library") {
3454 shared_library(shlib) {
Julie Hockette2a29402018-07-31 10:11:42 -07003455 forward_variables_from(invoker, "*")
Brett Wilson796ed472018-07-16 15:11:09 -07003456 ...
3457 }
3458 }
3459```
3460
3461#### **Example of defining a template**
3462
3463```
3464 template("my_idl") {
3465 # Be nice and help callers debug problems by checking that the variables
3466 # the template requires are defined. This gives a nice message rather than
3467 # giving the user an error about an undefined variable in the file defining
3468 # the template
3469 #
3470 # You can also use defined() to give default values to variables
3471 # unspecified by the invoker.
3472 assert(defined(invoker.sources),
3473 "Need sources in $target_name listing the idl files.")
3474
3475 # Name of the intermediate target that does the code gen. This must
3476 # incorporate the target name so it's unique across template
3477 # instantiations.
3478 code_gen_target_name = target_name + "_code_gen"
3479
3480 # Intermediate target to convert IDL to C source. Note that the name is
3481 # based on the name the invoker of the template specified. This way, each
3482 # time the template is invoked we get a unique intermediate action name
3483 # (since all target names are in the global scope).
3484 action_foreach(code_gen_target_name) {
3485 # Access the scope defined by the invoker via the implicit "invoker"
3486 # variable.
3487 sources = invoker.sources
3488
3489 # Note that we need an absolute path for our script file name. The
3490 # current directory when executing this code will be that of the invoker
3491 # (this is why we can use the "sources" directly above without having to
3492 # rebase all of the paths). But if we need to reference a script relative
3493 # to the template file, we'll need to use an absolute path instead.
3494 script = "//tools/idl/idl_code_generator.py"
3495
3496 # Tell GN how to expand output names given the sources.
3497 # See "gn help source_expansion" for more.
3498 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
3499 "$target_gen_dir/{{source_name_part}}.h" ]
3500 }
3501
3502 # Name the source set the same as the template invocation so instancing
3503 # this template produces something that other targets can link to in their
3504 # deps.
3505 source_set(target_name) {
3506 # Generates the list of sources, we get these from the action_foreach
3507 # above.
3508 sources = get_target_outputs(":$code_gen_target_name")
3509
3510 # This target depends on the files produced by the above code gen target.
3511 deps = [ ":$code_gen_target_name" ]
3512 }
3513 }
3514```
3515
3516#### **Example of invoking the resulting template**
3517
3518```
3519 # This calls the template code above, defining target_name to be
3520 # "foo_idl_files" and "invoker" to be the set of stuff defined in the curly
3521 # brackets.
3522 my_idl("foo_idl_files") {
3523 # Goes into the template as "invoker.sources".
3524 sources = [ "foo.idl", "bar.idl" ]
3525 }
3526
3527 # Here is a target that depends on our template.
3528 executable("my_exe") {
3529 # Depend on the name we gave the template call above. Internally, this will
3530 # produce a dependency from executable to the source_set inside the
3531 # template (since it has this name), which will in turn depend on the code
3532 # gen action.
3533 deps = [ ":foo_idl_files" ]
3534 }
3535```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08003536### **tool**: Specify arguments to a toolchain tool.
Brett Wilson796ed472018-07-16 15:11:09 -07003537
3538#### **Usage**
3539
3540```
3541 tool() {
3542
3543 }
3544```
3545
3546#### **Tool types**
3547
3548```
3549 Compiler tools:
3550 "cc": C compiler
3551 "cxx": C++ compiler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003552 "cxx_module": C++ compiler used for Clang .modulemap files
Brett Wilson796ed472018-07-16 15:11:09 -07003553 "objc": Objective C compiler
3554 "objcxx": Objective C++ compiler
3555 "rc": Resource compiler (Windows .rc files)
3556 "asm": Assembler
Sylvain Defresne89e64252020-08-07 13:01:06 +02003557 "swift": Swift compiler driver
Brett Wilson796ed472018-07-16 15:11:09 -07003558
3559 Linker tools:
3560 "alink": Linker for static libraries (archives)
3561 "solink": Linker for shared libraries
3562 "link": Linker for executables
3563
3564 Other tools:
3565 "stamp": Tool for creating stamp files
3566 "copy": Tool to copy files.
3567 "action": Defaults for actions
3568
3569 Platform specific tools:
3570 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
3571 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
Julie Hockettce1fa072019-05-07 17:44:37 -07003572
3573 Rust tools:
Petr Hosek7617b592019-10-02 09:52:21 -07003574 "rust_bin": Tool for compiling Rust binaries
3575 "rust_cdylib": Tool for compiling C-compatible dynamic libraries.
3576 "rust_dylib": Tool for compiling Rust dynamic libraries.
3577 "rust_macro": Tool for compiling Rust procedural macros.
3578 "rust_rlib": Tool for compiling Rust libraries.
3579 "rust_staticlib": Tool for compiling Rust static libraries.
Brett Wilson796ed472018-07-16 15:11:09 -07003580```
3581
3582#### **Tool variables**
3583
3584```
3585 command [string with substitutions]
3586 Valid for: all tools except "action" (required)
3587
3588 The command to run.
3589
Joe Armstrong23d22bc2019-06-28 15:31:03 +08003590 command_launcher [string]
3591 Valid for: all tools except "action" (optional)
3592
3593 The prefix with which to launch the command (e.g. the path to a Goma or
3594 CCache compiler launcher).
3595
3596 Note that this prefix will not be included in the compilation database or
3597 IDE files generated from the build.
3598
Brett Wilson796ed472018-07-16 15:11:09 -07003599 default_output_dir [string with substitutions]
3600 Valid for: linker tools
3601
3602 Default directory name for the output file relative to the
3603 root_build_dir. It can contain other substitution patterns. This will
3604 be the default value for the {{output_dir}} expansion (discussed below)
3605 but will be overridden by the "output_dir" variable in a target, if one
3606 is specified.
3607
3608 GN doesn't do anything with this string other than pass it along,
3609 potentially with target-specific overrides. It is the tool's job to use
3610 the expansion so that the files will be in the right place.
3611
3612 default_output_extension [string]
3613 Valid for: linker tools
3614
3615 Extension for the main output of a linkable tool. It includes the
3616 leading dot. This will be the default value for the
3617 {{output_extension}} expansion (discussed below) but will be overridden
3618 by by the "output extension" variable in a target, if one is specified.
3619 Empty string means no extension.
3620
3621 GN doesn't actually do anything with this extension other than pass it
3622 along, potentially with target-specific overrides. One would typically
3623 use the {{output_extension}} value in the "outputs" to read this value.
3624
3625 Example: default_output_extension = ".exe"
3626
3627 depfile [string with substitutions]
3628 Valid for: compiler tools (optional)
3629
3630 If the tool can write ".d" files, this specifies the name of the
3631 resulting file. These files are used to list header file dependencies
3632 (or other implicit input dependencies) that are discovered at build
3633 time. See also "depsformat".
3634
3635 Example: depfile = "{{output}}.d"
3636
3637 depsformat [string]
3638 Valid for: compiler tools (when depfile is specified)
3639
3640 Format for the deps outputs. This is either "gcc" or "msvc". See the
3641 ninja documentation for "deps" for more information.
3642
3643 Example: depsformat = "gcc"
3644
3645 description [string with substitutions, optional]
3646 Valid for: all tools
3647
3648 What to print when the command is run.
3649
3650 Example: description = "Compiling {{source}}"
3651
Julie Hockettce1fa072019-05-07 17:44:37 -07003652 exe_output_extension [string, optional, rust tools only]
3653 rlib_output_extension [string, optional, rust tools only]
3654 dylib_output_extension [string, optional, rust tools only]
3655 cdylib_output_extension [string, optional, rust tools only]
Petr Hosekfe36c7c2019-11-26 14:38:08 -08003656 rust_proc_macro_output_extension [string, optional, rust tools only]
Julie Hockettce1fa072019-05-07 17:44:37 -07003657 Valid for: Rust tools
3658
3659 These specify the default tool output for each of the crate types.
3660 The default is empty for executables, shared, and static libraries and
3661 ".rlib" for rlibs. Note that the Rust compiler complains with an error
3662 if external crates do not take the form `lib.rlib` or
3663 `lib.`, where `` is `.so`,
3664 `.dylib`, or `.dll` as appropriate for the platform.
3665
Brett Wilson796ed472018-07-16 15:11:09 -07003666 lib_switch [string, optional, link tools only]
3667 lib_dir_switch [string, optional, link tools only]
3668 Valid for: Linker tools except "alink"
3669
3670 These strings will be prepended to the libraries and library search
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003671 directories, respectively, because linkers differ on how to specify
3672 them.
3673
Brett Wilson796ed472018-07-16 15:11:09 -07003674 If you specified:
3675 lib_switch = "-l"
3676 lib_dir_switch = "-L"
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003677 then the "{{libs}}" expansion for
3678 [ "freetype", "expat" ]
3679 would be
3680 "-lfreetype -lexpat".
3681
3682 framework_switch [string, optional, link tools only]
Robert Sesekd0a6f072020-05-15 11:21:22 -04003683 weak_framework_switch [string, optional, link tools only]
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003684 framework_dir_switch [string, optional, link tools only]
3685 Valid for: Linker tools
3686
3687 These strings will be prepended to the frameworks and framework search
3688 path directories, respectively, because linkers differ on how to specify
3689 them.
3690
3691 If you specified:
3692 framework_switch = "-framework "
Robert Sesekd0a6f072020-05-15 11:21:22 -04003693 weak_framework_switch = "-weak_framework "
Sylvain Defresnea09ec162020-01-16 12:36:40 +01003694 framework_dir_switch = "-F"
Robert Sesekd0a6f072020-05-15 11:21:22 -04003695 and:
3696 framework_dirs = [ "$root_out_dir" ]
3697 frameworks = [ "UIKit.framework", "Foo.framework" ]
3698 weak_frameworks = [ "MediaPlayer.framework" ]
3699 would be:
3700 "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer"
Brett Wilson796ed472018-07-16 15:11:09 -07003701
Sylvain Defresne89e64252020-08-07 13:01:06 +02003702 swiftmodule_switch [string, optional, link tools only]
3703 Valid for: Linker tools except "alink"
3704
3705 The string will be prependend to the path to the .swiftmodule files
3706 that are embedded in the linker output.
3707
3708 If you specified:
3709 swiftmodule_swift = "-Wl,-add_ast_path,"
3710 then the "{{swiftmodules}}" expansion for
3711 [ "obj/foo/Foo.swiftmodule" ]
3712 would be
3713 "-Wl,-add_ast_path,obj/foo/Foo.swiftmodule"
3714
Brett Wilson796ed472018-07-16 15:11:09 -07003715 outputs [list of strings with substitutions]
3716 Valid for: Linker and compiler tools (required)
3717
3718 An array of names for the output files the tool produces. These are
3719 relative to the build output directory. There must always be at least
3720 one output file. There can be more than one output (a linker might
3721 produce a library and an import library, for example).
3722
3723 This array just declares to GN what files the tool will produce. It is
3724 your responsibility to specify the tool command that actually produces
3725 these files.
3726
3727 If you specify more than one output for shared library links, you
3728 should consider setting link_output, depend_output, and
3729 runtime_outputs.
3730
3731 Example for a compiler tool that produces .obj files:
3732 outputs = [
3733 "{{source_out_dir}}/{{source_name_part}}.obj"
3734 ]
3735
3736 Example for a linker tool that produces a .dll and a .lib. The use of
3737 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
3738 the target to override these values.
3739 outputs = [
Nico Weberad9eab22020-11-15 22:20:28 -05003740 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Brett Wilson796ed472018-07-16 15:11:09 -07003741 "{{output_dir}}/{{target_output_name}}.lib",
3742 ]
3743
Sylvain Defresne89e64252020-08-07 13:01:06 +02003744 partial_outputs [list of strings with substitutions]
3745 Valid for: "swift" only
3746
3747 An array of names for the partial outputs the tool produces. These
3748 are relative to the build output directory. The expansion will be
3749 evaluated for each file listed in the "sources" of the target.
3750
3751 This is used to deal with whole module optimization, allowing to
3752 list one object file per source file when whole module optimization
3753 is disabled.
3754
Brett Wilson796ed472018-07-16 15:11:09 -07003755 pool [label, optional]
3756 Valid for: all tools (optional)
3757
3758 Label of the pool to use for the tool. Pools are used to limit the
3759 number of tasks that can execute concurrently during the build.
3760
3761 See also "gn help pool".
3762
3763 link_output [string with substitutions]
3764 depend_output [string with substitutions]
3765 Valid for: "solink" only (optional)
3766
3767 These two files specify which of the outputs from the solink tool
3768 should be used for linking and dependency tracking. These should match
3769 entries in the "outputs". If unspecified, the first item in the
3770 "outputs" array will be used for all. See "Separate linking and
3771 dependencies for shared libraries" below for more.
3772
3773 On Windows, where the tools produce a .dll shared library and a .lib
3774 import library, you will want the first two to be the import library
3775 and the third one to be the .dll file. On Linux, if you're not doing
3776 the separate linking/dependency optimization, all of these should be
3777 the .so output.
3778
3779 output_prefix [string]
3780 Valid for: Linker tools (optional)
3781
3782 Prefix to use for the output name. Defaults to empty. This prefix will
3783 be prepended to the name of the target (or the output_name if one is
3784 manually specified for it) if the prefix is not already there. The
3785 result will show up in the {{output_name}} substitution pattern.
3786
3787 Individual targets can opt-out of the output prefix by setting:
3788 output_prefix_override = true
3789 (see "gn help output_prefix_override").
3790
3791 This is typically used to prepend "lib" to libraries on
3792 Posix systems:
3793 output_prefix = "lib"
3794
3795 precompiled_header_type [string]
3796 Valid for: "cc", "cxx", "objc", "objcxx"
3797
3798 Type of precompiled headers. If undefined or the empty string,
3799 precompiled headers will not be used for this tool. Otherwise use "gcc"
3800 or "msvc".
3801
3802 For precompiled headers to be used for a given target, the target (or a
3803 config applied to it) must also specify a "precompiled_header" and, for
3804 "msvc"-style headers, a "precompiled_source" value. If the type is
3805 "gcc", then both "precompiled_header" and "precompiled_source" must
3806 resolve to the same file, despite the different formats required for
3807 each."
3808
3809 See "gn help precompiled_header" for more.
3810
3811 restat [boolean]
3812 Valid for: all tools (optional, defaults to false)
3813
3814 Requests that Ninja check the file timestamp after this tool has run to
3815 determine if anything changed. Set this if your tool has the ability to
3816 skip writing output if the output file has not changed.
3817
3818 Normally, Ninja will assume that when a tool runs the output be new and
3819 downstream dependents must be rebuild. When this is set to trye, Ninja
3820 can skip rebuilding downstream dependents for input changes that don't
3821 actually affect the output.
3822
3823 Example:
3824 restat = true
3825
3826 rspfile [string with substitutions]
3827 Valid for: all tools except "action" (optional)
3828
3829 Name of the response file. If empty, no response file will be
3830 used. See "rspfile_content".
3831
3832 rspfile_content [string with substitutions]
3833 Valid for: all tools except "action" (required when "rspfile" is used)
3834
3835 The contents to be written to the response file. This may include all
3836 or part of the command to send to the tool which allows you to get
3837 around OS command-line length limits.
3838
3839 This example adds the inputs and libraries to a response file, but
3840 passes the linker flags directly on the command line:
3841 tool("link") {
3842 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
3843 rspfile = "{{output}}.rsp"
Adrian Taylorac6cc952020-04-26 20:49:18 -07003844 rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}"
Brett Wilson796ed472018-07-16 15:11:09 -07003845 }
3846
3847 runtime_outputs [string list with substitutions]
3848 Valid for: linker tools
3849
3850 If specified, this list is the subset of the outputs that should be
3851 added to runtime deps (see "gn help runtime_deps"). By default (if
3852 runtime_outputs is empty or unspecified), it will be the link_output.
Adrian Taylor4aa9bdf2021-11-15 19:00:06 -08003853
3854 rust_sysroot
3855 Valid for: Rust tools
3856
3857 A path relative to root_out_dir. This is not used in the build
3858 process, but may be used when generating metadata for rust-analyzer.
3859 (See --export-rust-project). It enables such metadata to include
3860 information about the Rust standard library.
Brett Wilson796ed472018-07-16 15:11:09 -07003861```
3862
3863#### **Expansions for tool variables**
3864
3865```
3866 All paths are relative to the root build directory, which is the current
3867 directory for running all tools. These expansions are available to all tools:
3868
3869 {{label}}
3870 The label of the current target. This is typically used in the
3871 "description" field for link tools. The toolchain will be omitted from
3872 the label for targets in the default toolchain, and will be included
3873 for targets in other toolchains.
3874
3875 {{label_name}}
3876 The short name of the label of the target. This is the part after the
3877 colon. For "//foo/bar:baz" this will be "baz". Unlike
3878 {{target_output_name}}, this is not affected by the "output_prefix" in
3879 the tool or the "output_name" set on the target.
3880
Scott Graham501b49a2020-08-05 13:03:37 -07003881 {{label_no_toolchain}}
3882 The label of the current target, never including the toolchain
3883 (otherwise, this is identical to {{label}}). This is used as the module
3884 name when using .modulemap files.
3885
Brett Wilson796ed472018-07-16 15:11:09 -07003886 {{output}}
3887 The relative path and name of the output(s) of the current build step.
3888 If there is more than one output, this will expand to a list of all of
3889 them. Example: "out/base/my_file.o"
3890
3891 {{target_gen_dir}}
3892 {{target_out_dir}}
3893 The directory of the generated file and output directories,
3894 respectively, for the current target. There is no trailing slash. See
3895 also {{output_dir}} for linker tools. Example: "out/base/test"
3896
3897 {{target_output_name}}
3898 The short name of the current target with no path information, or the
3899 value of the "output_name" variable if one is specified in the target.
3900 This will include the "output_prefix" if any. See also {{label_name}}.
3901
3902 Example: "libfoo" for the target named "foo" and an output prefix for
3903 the linker tool of "lib".
3904
3905 Compiler tools have the notion of a single input and a single output, along
3906 with a set of compiler-specific flags. The following expansions are
3907 available:
3908
3909 {{asmflags}}
3910 {{cflags}}
3911 {{cflags_c}}
3912 {{cflags_cc}}
3913 {{cflags_objc}}
3914 {{cflags_objcc}}
3915 {{defines}}
3916 {{include_dirs}}
3917 Strings correspond that to the processed flags/defines/include
3918 directories specified for the target.
3919 Example: "--enable-foo --enable-bar"
3920
3921 Defines will be prefixed by "-D" and include directories will be
3922 prefixed by "-I" (these work with Posix tools as well as Microsoft
3923 ones).
3924
Scott Graham484245b2020-08-11 10:58:50 -07003925 {{module_deps}}
3926 {{module_deps_no_self}}
3927 Strings that correspond to the flags necessary to depend upon the Clang
3928 modules referenced by the current target. The "_no_self" version doesn't
3929 include the module for the current target, and can be used to compile
3930 the pcm itself.
3931
Brett Wilson796ed472018-07-16 15:11:09 -07003932 {{source}}
3933 The relative path and name of the current input file.
3934 Example: "../../base/my_file.cc"
3935
3936 {{source_file_part}}
3937 The file part of the source including the extension (with no directory
3938 information).
3939 Example: "foo.cc"
3940
3941 {{source_name_part}}
3942 The filename part of the source file with no directory or extension.
3943 Example: "foo"
3944
3945 {{source_gen_dir}}
3946 {{source_out_dir}}
3947 The directory in the generated file and output directories,
3948 respectively, for the current input file. If the source file is in the
3949 same directory as the target is declared in, they will will be the same
3950 as the "target" versions above. Example: "gen/base/test"
3951
Julie Hockette2a29402018-07-31 10:11:42 -07003952 Linker tools have multiple inputs and (potentially) multiple outputs. The
Brett Wilson796ed472018-07-16 15:11:09 -07003953 static library tool ("alink") is not considered a linker tool. The following
3954 expansions are available:
3955
3956 {{inputs}}
3957 {{inputs_newline}}
3958 Expands to the inputs to the link step. This will be a list of object
3959 files and static libraries.
3960 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
3961
3962 The "_newline" version will separate the input files with newlines
3963 instead of spaces. This is useful in response files: some linkers can
3964 take a "-filelist" flag which expects newline separated files, and some
3965 Microsoft tools have a fixed-sized buffer for parsing each line of a
3966 response file.
3967
3968 {{ldflags}}
3969 Expands to the processed set of ldflags and library search paths
3970 specified for the target.
3971 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
3972
3973 {{libs}}
3974 Expands to the list of system libraries to link to. Each will be
3975 prefixed by the "lib_switch".
3976
Brett Wilson796ed472018-07-16 15:11:09 -07003977 Example: "-lfoo -lbar"
3978
3979 {{output_dir}}
3980 The value of the "output_dir" variable in the target, or the the value
3981 of the "default_output_dir" value in the tool if the target does not
3982 override the output directory. This will be relative to the
3983 root_build_dir and will not end in a slash. Will be "." for output to
3984 the root_build_dir.
3985
3986 This is subtly different than {{target_out_dir}} which is defined by GN
3987 based on the target's path and not overridable. {{output_dir}} is for
3988 the final output, {{target_out_dir}} is generally for object files and
3989 other outputs.
3990
3991 Usually {{output_dir}} would be defined in terms of either
3992 {{target_out_dir}} or {{root_out_dir}}
3993
3994 {{output_extension}}
3995 The value of the "output_extension" variable in the target, or the
3996 value of the "default_output_extension" value in the tool if the target
3997 does not specify an output extension.
3998 Example: ".so"
3999
4000 {{solibs}}
4001 Extra libraries from shared library dependencies not specified in the
4002 {{inputs}}. This is the list of link_output files from shared libraries
4003 (if the solink tool specifies a "link_output" variable separate from
4004 the "depend_output").
4005
4006 These should generally be treated the same as libs by your tool.
4007
4008 Example: "libfoo.so libbar.so"
4009
Adrian Taylorac6cc952020-04-26 20:49:18 -07004010 {{rlibs}}
4011 Any Rust .rlibs which need to be linked into a final C++ target.
4012 These should be treated as {{inputs}} except that sometimes
4013 they might have different linker directives applied.
4014
4015 Example: "obj/foo/libfoo.rlib"
4016
Sylvain Defresnea09ec162020-01-16 12:36:40 +01004017 {{frameworks}}
4018 Shared libraries packaged as framework bundle. This is principally
4019 used on Apple's platforms (macOS and iOS). All name must be ending
4020 with ".framework" suffix; the suffix will be stripped when expanding
Robert Sesekd0a6f072020-05-15 11:21:22 -04004021 {{frameworks}} and each item will be preceded by "-framework" or
4022 "-weak_framework".
Sylvain Defresnea09ec162020-01-16 12:36:40 +01004023
Sylvain Defresne89e64252020-08-07 13:01:06 +02004024 {{swiftmodules}}
4025 Swift .swiftmodule files that needs to be embedded into the binary.
4026 This is necessary to correctly link with object generated by the
4027 Swift compiler (the .swiftmodule file cannot be embedded in object
4028 files directly). Those will be prefixed with "swiftmodule_switch"
4029 value.
4030
Brett Wilson796ed472018-07-16 15:11:09 -07004031 The static library ("alink") tool allows {{arflags}} plus the common tool
4032 substitutions.
4033
4034 The copy tool allows the common compiler/linker substitutions, plus
4035 {{source}} which is the source of the copy. The stamp tool allows only the
4036 common tool substitutions.
4037
4038 The copy_bundle_data and compile_xcassets tools only allows the common tool
4039 substitutions. Both tools are required to create iOS/macOS bundles and need
4040 only be defined on those platforms.
4041
4042 The copy_bundle_data tool will be called with one source and needs to copy
4043 (optionally optimizing the data representation) to its output. It may be
4044 called with a directory as input and it needs to be recursively copied.
4045
4046 The compile_xcassets tool will be called with one or more source (each an
4047 asset catalog) that needs to be compiled to a single output. The following
4048 substitutions are available:
4049
4050 {{inputs}}
4051 Expands to the list of .xcassets to use as input to compile the asset
4052 catalog.
4053
4054 {{bundle_product_type}}
4055 Expands to the product_type of the bundle that will contain the
4056 compiled asset catalog. Usually corresponds to the product_type
4057 property of the corresponding create_bundle target.
4058
4059 {{bundle_partial_info_plist}}
4060 Expands to the path to the partial Info.plist generated by the
4061 assets catalog compiler. Usually based on the target_name of
4062 the create_bundle target.
Julie Hockettce1fa072019-05-07 17:44:37 -07004063
Harley Li0a9affb2020-06-03 10:38:42 -04004064 {{xcasset_compiler_flags}}
4065 Expands to the list of flags specified in corresponding
4066 create_bundle target.
4067
Sylvain Defresne89e64252020-08-07 13:01:06 +02004068 The Swift tool has multiple input and outputs. It must have exactly one
4069 output of .swiftmodule type, but can have one or more object file outputs,
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004070 in addition to other type of outputs. The following expansions are available:
Sylvain Defresne89e64252020-08-07 13:01:06 +02004071
4072 {{module_name}}
4073 Expands to the string representing the module name of target under
4074 compilation (see "module_name" variable).
4075
4076 {{module_dirs}}
4077 Expands to the list of -I for the target Swift module search
4078 path computed from target dependencies.
4079
4080 {{swiftflags}}
4081 Expands to the list of strings representing Swift compiler flags.
4082
Julie Hockettce1fa072019-05-07 17:44:37 -07004083 Rust tools have the notion of a single input and a single output, along
4084 with a set of compiler-specific flags. The following expansions are
4085 available:
4086
4087 {{crate_name}}
4088 Expands to the string representing the crate name of target under
4089 compilation.
4090
4091 {{crate_type}}
4092 Expands to the string representing the type of crate for the target
4093 under compilation.
4094
4095 {{externs}}
4096 Expands to the list of --extern flags needed to include addition Rust
4097 libraries in this target. Includes any specified renamed dependencies.
4098
Julie Hockettce1fa072019-05-07 17:44:37 -07004099 {{rustdeps}}
4100 Expands to the list of -Ldependency= strings needed to compile
4101 this target.
4102
4103 {{rustenv}}
4104 Expands to the list of environment variables.
4105
4106 {{rustflags}}
4107 Expands to the list of strings representing Rust compiler flags.
Brett Wilson796ed472018-07-16 15:11:09 -07004108```
4109
4110#### **Separate linking and dependencies for shared libraries**
4111
4112```
4113 Shared libraries are special in that not all changes to them require that
4114 dependent targets be re-linked. If the shared library is changed but no
4115 imports or exports are different, dependent code needn't be relinked, which
4116 can speed up the build.
4117
4118 If your link step can output a list of exports from a shared library and
4119 writes the file only if the new one is different, the timestamp of this file
4120 can be used for triggering re-links, while the actual shared library would be
4121 used for linking.
4122
4123 You will need to specify
4124 restat = true
4125 in the linker tool to make this work, so Ninja will detect if the timestamp
4126 of the dependency file has changed after linking (otherwise it will always
4127 assume that running a command updates the output):
4128
4129 tool("solink") {
4130 command = "..."
4131 outputs = [
4132 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
Nico Weberad9eab22020-11-15 22:20:28 -05004133 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC",
Brett Wilson796ed472018-07-16 15:11:09 -07004134 ]
4135 link_output =
4136 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
4137 depend_output =
Nico Weberad9eab22020-11-15 22:20:28 -05004138 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC"
Brett Wilson796ed472018-07-16 15:11:09 -07004139 restat = true
4140 }
4141```
4142
4143#### **Example**
4144
4145```
4146 toolchain("my_toolchain") {
4147 # Put these at the top to apply to all tools below.
4148 lib_switch = "-l"
4149 lib_dir_switch = "-L"
4150
4151 tool("cc") {
4152 command = "gcc {{source}} -o {{output}}"
4153 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
4154 description = "GCC {{source}}"
4155 }
4156 tool("cxx") {
4157 command = "g++ {{source}} -o {{output}}"
4158 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
4159 description = "G++ {{source}}"
4160 }
4161 };
4162```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004163### **toolchain**: Defines a toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004164
4165```
4166 A toolchain is a set of commands and build flags used to compile the source
4167 code. The toolchain() function defines these commands.
4168```
4169
4170#### **Toolchain overview**
4171
4172```
4173 You can have more than one toolchain in use at once in a build and a target
4174 can exist simultaneously in multiple toolchains. A build file is executed
4175 once for each toolchain it is referenced in so the GN code can vary all
4176 parameters of each target (or which targets exist) on a per-toolchain basis.
4177
4178 When you have a simple build with only one toolchain, the build config file
4179 is loaded only once at the beginning of the build. It must call
4180 set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the
4181 label of the toolchain definition to use. The "toolchain_args" section of the
4182 toolchain definition is ignored.
4183
4184 When a target has a dependency on a target using different toolchain (see "gn
4185 help labels" for how to specify this), GN will start a build using that
4186 secondary toolchain to resolve the target. GN will load the build config file
4187 with the build arguments overridden as specified in the toolchain_args.
4188 Because the default toolchain is already known, calls to
4189 set_default_toolchain() are ignored.
4190
4191 To load a file in an alternate toolchain, GN does the following:
4192
4193 1. Loads the file with the toolchain definition in it (as determined by the
4194 toolchain label).
4195 2. Re-runs the master build configuration file, applying the arguments
4196 specified by the toolchain_args section of the toolchain definition.
4197 3. Loads the destination build file in the context of the configuration file
4198 in the previous step.
4199
4200 The toolchain configuration is two-way. In the default toolchain (i.e. the
4201 main build target) the configuration flows from the build config file to the
4202 toolchain. The build config file looks at the state of the build (OS type,
4203 CPU architecture, etc.) and decides which toolchain to use (via
4204 set_default_toolchain()). In secondary toolchains, the configuration flows
4205 from the toolchain to the build config file: the "toolchain_args" in the
4206 toolchain definition specifies the arguments to re-invoke the build.
4207```
4208
4209#### **Functions and variables**
4210
4211```
4212 tool()
Julie Hockette2a29402018-07-31 10:11:42 -07004213 The tool() function call specifies the commands to run for a given step. See
4214 "gn help tool".
Brett Wilson796ed472018-07-16 15:11:09 -07004215
Julie Hockette2a29402018-07-31 10:11:42 -07004216 toolchain_args [scope]
Brett Wilson796ed472018-07-16 15:11:09 -07004217 Overrides for build arguments to pass to the toolchain when invoking it.
4218 This is a variable of type "scope" where the variable names correspond to
4219 variables in declare_args() blocks.
4220
4221 When you specify a target using an alternate toolchain, the master build
4222 configuration file is re-interpreted in the context of that toolchain.
4223 toolchain_args allows you to control the arguments passed into this
4224 alternate invocation of the build.
4225
4226 Any default system arguments or arguments passed in via "gn args" will also
4227 be passed to the alternate invocation unless explicitly overridden by
4228 toolchain_args.
4229
4230 The toolchain_args will be ignored when the toolchain being defined is the
4231 default. In this case, it's expected you want the default argument values.
4232
4233 See also "gn help buildargs" for an overview of these arguments.
4234
Julie Hockette2a29402018-07-31 10:11:42 -07004235 propagates_configs [boolean, default=false]
4236 Determines whether public_configs and all_dependent_configs in this
4237 toolchain propagate to targets in other toolchains.
4238
4239 When false (the default), this toolchain will not propagate any configs to
4240 targets in other toolchains that depend on it targets inside this
4241 toolchain. This matches the most common usage of toolchains where they
4242 represent different architectures or compilers and the settings that apply
4243 to one won't necessarily apply to others.
4244
4245 When true, configs (public and all-dependent) will cross the boundary out
4246 of this toolchain as if the toolchain boundary wasn't there. This only
4247 affects one direction of dependencies: a toolchain can't control whether
4248 it accepts such configs, only whether it pushes them. The build is
4249 responsible for ensuring that any external targets depending on targets in
4250 this toolchain are compatible with the compiler flags, etc. that may be
4251 propagated.
4252
4253 deps [string list]
Brett Wilson796ed472018-07-16 15:11:09 -07004254 Dependencies of this toolchain. These dependencies will be resolved before
4255 any target in the toolchain is compiled. To avoid circular dependencies
4256 these must be targets defined in another toolchain.
4257
4258 This is expressed as a list of targets, and generally these targets will
4259 always specify a toolchain:
4260 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
4261
4262 This concept is somewhat inefficient to express in Ninja (it requires a lot
4263 of duplicate of rules) so should only be used when absolutely necessary.
4264```
4265
4266#### **Example of defining a toolchain**
4267
4268```
4269 toolchain("32") {
4270 tool("cc") {
4271 command = "gcc {{source}}"
4272 ...
4273 }
4274
4275 toolchain_args = {
4276 use_doom_melon = true # Doom melon always required for 32-bit builds.
4277 current_cpu = "x86"
4278 }
4279 }
4280
4281 toolchain("64") {
4282 tool("cc") {
4283 command = "gcc {{source}}"
4284 ...
4285 }
4286
4287 toolchain_args = {
4288 # use_doom_melon is not overridden here, it will take the default.
4289 current_cpu = "x64"
4290 }
4291 }
4292```
4293
4294#### **Example of cross-toolchain dependencies**
4295
4296```
4297 If a 64-bit target wants to depend on a 32-bit binary, it would specify a
4298 dependency using data_deps (data deps are like deps that are only needed at
4299 runtime and aren't linked, since you can't link a 32-bit and a 64-bit
4300 library).
4301
4302 executable("my_program") {
4303 ...
4304 if (target_cpu == "x64") {
4305 # The 64-bit build needs this 32-bit helper.
4306 data_deps = [ ":helper(//toolchains:32)" ]
4307 }
4308 }
4309
4310 if (target_cpu == "x86") {
4311 # Our helper library is only compiled in 32-bits.
4312 shared_library("helper") {
4313 ...
4314 }
4315 }
4316```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004317### **write_file**: Write a file to disk.
Brett Wilson796ed472018-07-16 15:11:09 -07004318
4319```
Julie Hockett09171292018-07-31 14:35:10 -07004320 write_file(filename, data, output_conversion = "")
Brett Wilson796ed472018-07-16 15:11:09 -07004321
4322 If data is a list, the list will be written one-item-per-line with no quoting
4323 or brackets.
4324
4325 If the file exists and the contents are identical to that being written, the
4326 file will not be updated. This will prevent unnecessary rebuilds of targets
4327 that depend on this file.
4328
4329 One use for write_file is to write a list of inputs to an script that might
4330 be too long for the command line. However, it is preferable to use response
4331 files for this purpose. See "gn help response_file_contents".
Brett Wilson796ed472018-07-16 15:11:09 -07004332```
4333
4334#### **Arguments**
4335
4336```
4337 filename
4338 Filename to write. This must be within the output directory.
4339
4340 data
4341 The list or string to write.
Julie Hockett09171292018-07-31 14:35:10 -07004342
4343 output_conversion
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02004344 Controls how the output is written. See `gn help io_conversion`.
Brett Wilson796ed472018-07-16 15:11:09 -07004345```
4346## Built-in predefined variables
4347
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004348### **current_cpu**: The processor architecture of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004349
4350```
4351 The build configuration usually sets this value based on the value of
4352 "host_cpu" (see "gn help host_cpu") and then threads this through the
4353 toolchain definitions to ensure that it always reflects the appropriate
4354 value.
4355
Julie Hockette2a29402018-07-31 10:11:42 -07004356 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004357 empty string ("") by default but is declared so that it can be overridden on
4358 the command line if so desired.
4359
4360 See "gn help target_cpu" for a list of common values returned.
4361```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004362### **current_os**: The operating system of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004363
4364```
4365 The build configuration usually sets this value based on the value of
4366 "target_os" (see "gn help target_os"), and then threads this through the
4367 toolchain definitions to ensure that it always reflects the appropriate
4368 value.
4369
Julie Hockette2a29402018-07-31 10:11:42 -07004370 This value is not used internally by GN for any purpose. It is set to the
Brett Wilson796ed472018-07-16 15:11:09 -07004371 empty string ("") by default but is declared so that it can be overridden on
4372 the command line if so desired.
4373
4374 See "gn help target_os" for a list of common values returned.
4375```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004376### **current_toolchain**: Label of the current toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004377
4378```
4379 A fully-qualified label representing the current toolchain. You can use this
4380 to make toolchain-related decisions in the build. See also
4381 "default_toolchain".
4382```
4383
4384#### **Example**
4385
4386```
4387 if (current_toolchain == "//build:64_bit_toolchain") {
4388 executable("output_thats_64_bit_only") {
4389 ...
4390```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004391### **default_toolchain**: [string] Label of the default toolchain.
Brett Wilson796ed472018-07-16 15:11:09 -07004392
4393```
4394 A fully-qualified label representing the default toolchain, which may not
4395 necessarily be the current one (see "current_toolchain").
4396```
James Robinson61377e32020-02-13 15:20:07 -08004397### **gn_version**: [number] The version of gn.
4398
4399```
4400 Corresponds to the number printed by `gn --version`.
4401```
4402
4403#### **Example**
4404
4405```
4406 assert(gn_version >= 1700, "need GN version 1700 for the frobulate feature")
4407```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004408### **host_cpu**: The processor architecture that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004409
4410```
4411 This is value is exposed so that cross-compile toolchains can access the host
4412 architecture when needed.
4413
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004414 The value should generally be considered read-only, but it can be overridden
Brett Wilson796ed472018-07-16 15:11:09 -07004415 in order to handle unusual cases where there might be multiple plausible
4416 values for the host architecture (e.g., if you can do either 32-bit or 64-bit
4417 builds). The value is not used internally by GN for any purpose.
4418```
4419
4420#### **Some possible values**
4421
4422```
4423 - "x64"
4424 - "x86"
4425```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004426### **host_os**: [string] The operating system that GN is running on.
Brett Wilson796ed472018-07-16 15:11:09 -07004427
4428```
4429 This value is exposed so that cross-compiles can access the host build
4430 system's settings.
4431
4432 This value should generally be treated as read-only. It, however, is not used
4433 internally by GN for any purpose.
4434```
4435
4436#### **Some possible values**
4437
4438```
4439 - "linux"
4440 - "mac"
4441 - "win"
4442```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004443### **invoker**: [string] The invoking scope inside a template.
Brett Wilson796ed472018-07-16 15:11:09 -07004444
4445```
4446 Inside a template invocation, this variable refers to the scope of the
4447 invoker of the template. Outside of template invocations, this variable is
4448 undefined.
4449
4450 All of the variables defined inside the template invocation are accessible as
4451 members of the "invoker" scope. This is the way that templates read values
4452 set by the callers.
4453
4454 This is often used with "defined" to see if a value is set on the invoking
4455 scope.
4456
4457 See "gn help template" for more examples.
4458```
4459
4460#### **Example**
4461
4462```
4463 template("my_template") {
4464 print(invoker.sources) # Prints [ "a.cc", "b.cc" ]
4465 print(defined(invoker.foo)) # Prints false.
4466 print(defined(invoker.bar)) # Prints true.
4467 }
4468
4469 my_template("doom_melon") {
4470 sources = [ "a.cc", "b.cc" ]
4471 bar = 123
4472 }
4473```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004474### **python_path**: Absolute path of Python.
Brett Wilson796ed472018-07-16 15:11:09 -07004475
4476```
4477 Normally used in toolchain definitions if running some command requires
4478 Python. You will normally not need this when invoking scripts since GN
4479 automatically finds it for you.
4480```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004481### **root_build_dir**: [string] Directory where build commands are run.
Brett Wilson796ed472018-07-16 15:11:09 -07004482
4483```
4484 This is the root build output directory which will be the current directory
4485 when executing all compilers and scripts.
4486
4487 Most often this is used with rebase_path (see "gn help rebase_path") to
4488 convert arguments to be relative to a script's current directory.
4489```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004490### **root_gen_dir**: Directory for the toolchain's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004491
4492```
4493 Absolute path to the root of the generated output directory tree for the
4494 current toolchain. An example would be "//out/Debug/gen" for the default
4495 toolchain, or "//out/Debug/arm/gen" for the "arm" toolchain.
4496
4497 This is primarily useful for setting up include paths for generated files. If
4498 you are passing this to a script, you will want to pass it through
4499 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4500 build directory.
4501
4502 See also "target_gen_dir" which is usually a better location for generated
4503 files. It will be inside the root generated dir.
4504```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004505### **root_out_dir**: [string] Root directory for toolchain output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004506
4507```
4508 Absolute path to the root of the output directory tree for the current
4509 toolchain. It will not have a trailing slash.
4510
4511 For the default toolchain this will be the same as the root_build_dir. An
4512 example would be "//out/Debug" for the default toolchain, or
4513 "//out/Debug/arm" for the "arm" toolchain.
4514
4515 This is primarily useful for setting up script calls. If you are passing this
4516 to a script, you will want to pass it through rebase_path() (see "gn help
4517 rebase_path") to convert it to be relative to the build directory.
4518
4519 See also "target_out_dir" which is usually a better location for output
4520 files. It will be inside the root output dir.
4521```
4522
4523#### **Example**
4524
4525```
4526 action("myscript") {
4527 # Pass the output dir to the script.
4528 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
4529 }
4530```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004531### **target_cpu**: The desired cpu architecture for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004532
4533```
4534 This value should be used to indicate the desired architecture for the
4535 primary objects of the build. It will match the cpu architecture of the
4536 default toolchain, but not necessarily the current toolchain.
4537
4538 In many cases, this is the same as "host_cpu", but in the case of
4539 cross-compiles, this can be set to something different. This value is
4540 different from "current_cpu" in that it does not change based on the current
4541 toolchain. When writing rules, "current_cpu" should be used rather than
4542 "target_cpu" most of the time.
4543
4544 This value is not used internally by GN for any purpose, so it may be set to
4545 whatever value is needed for the build. GN defaults this value to the empty
4546 string ("") and the configuration files should set it to an appropriate value
4547 (e.g., setting it to the value of "host_cpu") if it is not overridden on the
4548 command line or in the args.gn file.
4549```
4550
4551#### **Possible values**
4552
4553```
4554 - "x86"
4555 - "x64"
4556 - "arm"
4557 - "arm64"
4558 - "mipsel"
Tyler Mandry4a648092022-02-15 19:47:09 +00004559 - "mips64el"
4560 - "s390x"
4561 - "ppc64"
4562 - "riscv32"
4563 - "riscv64"
4564 - "e2k"
4565 - "loong64"
Brett Wilson796ed472018-07-16 15:11:09 -07004566```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004567### **target_gen_dir**: Directory for a target's generated files.
Brett Wilson796ed472018-07-16 15:11:09 -07004568
4569```
4570 Absolute path to the target's generated file directory. This will be the
4571 "root_gen_dir" followed by the relative path to the current build file. If
4572 your file is in "//tools/doom_melon" then target_gen_dir would be
4573 "//out/Debug/gen/tools/doom_melon". It will not have a trailing slash.
4574
4575 This is primarily useful for setting up include paths for generated files. If
4576 you are passing this to a script, you will want to pass it through
4577 rebase_path() (see "gn help rebase_path") to convert it to be relative to the
4578 build directory.
4579
4580 See also "gn help root_gen_dir".
4581```
4582
4583#### **Example**
4584
4585```
4586 action("myscript") {
4587 # Pass the generated output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004588 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004589 }
4590```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004591### **target_name**: [string] The name of the current target.
Brett Wilson796ed472018-07-16 15:11:09 -07004592
4593```
4594 Inside a target or template invocation, this variable refers to the name
4595 given to the target or template invocation. Outside of these, this variable
4596 is undefined.
4597
4598 This is most often used in template definitions to name targets defined in
4599 the template based on the name of the invocation. This is necessary both to
4600 ensure generated targets have unique names and to generate a target with the
4601 exact name of the invocation that other targets can depend on.
4602
4603 Be aware that this value will always reflect the innermost scope. So when
4604 defining a target inside a template, target_name will refer to the target
4605 rather than the template invocation. To get the name of the template
4606 invocation in this case, you should save target_name to a temporary variable
4607 outside of any target definitions.
4608
4609 See "gn help template" for more examples.
4610```
4611
4612#### **Example**
4613
4614```
4615 executable("doom_melon") {
4616 print(target_name) # Prints "doom_melon".
4617 }
4618
4619 template("my_template") {
4620 print(target_name) # Prints "space_ray" when invoked below.
4621
4622 executable(target_name + "_impl") {
4623 print(target_name) # Prints "space_ray_impl".
4624 }
4625 }
4626
4627 my_template("space_ray") {
4628 }
4629```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004630### **target_os**: The desired operating system for the build.
Brett Wilson796ed472018-07-16 15:11:09 -07004631
4632```
4633 This value should be used to indicate the desired operating system for the
4634 primary object(s) of the build. It will match the OS of the default
4635 toolchain.
4636
4637 In many cases, this is the same as "host_os", but in the case of
4638 cross-compiles, it may be different. This variable differs from "current_os"
4639 in that it can be referenced from inside any toolchain and will always return
4640 the initial value.
4641
4642 This should be set to the most specific value possible. So, "android" or
4643 "chromeos" should be used instead of "linux" where applicable, even though
4644 Android and ChromeOS are both Linux variants. This can mean that one needs to
4645 write
4646
4647 if (target_os == "android" || target_os == "linux") {
4648 # ...
4649 }
4650
4651 and so forth.
4652
4653 This value is not used internally by GN for any purpose, so it may be set to
4654 whatever value is needed for the build. GN defaults this value to the empty
4655 string ("") and the configuration files should set it to an appropriate value
4656 (e.g., setting it to the value of "host_os") if it is not set via the command
4657 line or in the args.gn file.
4658```
4659
4660#### **Possible values**
4661
4662```
4663 - "android"
4664 - "chromeos"
4665 - "ios"
4666 - "linux"
4667 - "nacl"
4668 - "mac"
4669 - "win"
4670```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004671### **target_out_dir**: [string] Directory for target output files.
Brett Wilson796ed472018-07-16 15:11:09 -07004672
4673```
4674 Absolute path to the target's generated file directory. If your current
4675 target is in "//tools/doom_melon" then this value might be
4676 "//out/Debug/obj/tools/doom_melon". It will not have a trailing slash.
4677
4678 This is primarily useful for setting up arguments for calling scripts. If you
4679 are passing this to a script, you will want to pass it through rebase_path()
4680 (see "gn help rebase_path") to convert it to be relative to the build
4681 directory.
4682
4683 See also "gn help root_out_dir".
4684```
4685
4686#### **Example**
4687
4688```
4689 action("myscript") {
4690 # Pass the output dir to the script.
Nico Weberad9eab22020-11-15 22:20:28 -05004691 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
Brett Wilson796ed472018-07-16 15:11:09 -07004692 }
4693```
4694## Variables you set in targets
4695
Julie Hockettce1fa072019-05-07 17:44:37 -07004696### **aliased_deps**: [scope] Set of crate-dependency pairs.
4697
4698```
4699 Valid for `rust_library` targets and `executable`, `static_library`, and
4700 `shared_library` targets that contain Rust sources.
4701
4702 A scope, each key indicating the renamed crate and the corresponding value
4703 specifying the label of the dependency producing the relevant binary.
4704
4705 All dependencies listed in this field *must* be listed as deps of the target.
4706
4707 executable("foo") {
4708 sources = [ "main.rs" ]
4709 deps = [ "//bar" ]
4710 }
4711
4712 This target would compile the `foo` crate with the following `extern` flag:
4713 `rustc ...command... --extern bar=/obj/bar`
4714
4715 executable("foo") {
4716 sources = [ "main.rs" ]
4717 deps = [ ":bar" ]
4718 aliased_deps = {
4719 bar_renamed = ":bar"
4720 }
4721 }
4722
4723 With the addition of `aliased_deps`, above target would instead compile with:
4724 `rustc ...command... --extern bar_renamed=/obj/bar`
4725```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004726### **all_dependent_configs**: Configs to be forced on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07004727
4728```
4729 A list of config labels.
4730
4731 All targets depending on this one, and recursively, all targets depending on
4732 those, will have the configs listed in this variable added to them. These
4733 configs will also apply to the current target.
4734
4735 This addition happens in a second phase once a target and all of its
4736 dependencies have been resolved. Therefore, a target will not see these
4737 force-added configs in their "configs" variable while the script is running,
4738 and they can not be removed. As a result, this capability should generally
4739 only be used to add defines and include directories necessary to compile a
4740 target's headers.
4741
4742 See also "public_configs".
4743```
4744
4745#### **Ordering of flags and values**
4746
4747```
4748 1. Those set on the current target (not in a config).
4749 2. Those set on the "configs" on the target in order that the
4750 configs appear in the list.
4751 3. Those set on the "all_dependent_configs" on the target in order
4752 that the configs appear in the list.
4753 4. Those set on the "public_configs" on the target in order that
4754 those configs appear in the list.
4755 5. all_dependent_configs pulled from dependencies, in the order of
4756 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004757 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07004758 6. public_configs pulled from dependencies, in the order of the
4759 "deps" list. If a dependency is public, they will be applied
4760 recursively.
4761```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004762### **allow_circular_includes_from**: Permit includes from deps.
Brett Wilson796ed472018-07-16 15:11:09 -07004763
4764```
4765 A list of target labels. Must be a subset of the target's "deps". These
4766 targets will be permitted to include headers from the current target despite
4767 the dependency going in the opposite direction.
4768
4769 When you use this, both targets must be included in a final binary for it to
4770 link. To keep linker errors from happening, it is good practice to have all
4771 external dependencies depend only on one of the two targets, and to set the
4772 visibility on the other to enforce this. Thus the targets will always be
4773 linked together in any output.
4774```
4775
4776#### **Details**
4777
4778```
4779 Normally, for a file in target A to include a file from target B, A must list
4780 B as a dependency. This invariant is enforced by the "gn check" command (and
4781 the --check flag to "gn gen" -- see "gn help check").
4782
4783 Sometimes, two targets might be the same unit for linking purposes (two
4784 source sets or static libraries that would always be linked together in a
4785 final executable or shared library) and they each include headers from the
4786 other: you want A to be able to include B's headers, and B to include A's
4787 headers. This is not an ideal situation but is sometimes unavoidable.
4788
4789 This list, if specified, lists which of the dependencies of the current
4790 target can include header files from the current target. That is, if A
4791 depends on B, B can only include headers from A if it is in A's
4792 allow_circular_includes_from list. Normally includes must follow the
4793 direction of dependencies, this flag allows them to go in the opposite
4794 direction.
4795```
4796
4797#### **Danger**
4798
4799```
4800 In the above example, A's headers are likely to include headers from A's
4801 dependencies. Those dependencies may have public_configs that apply flags,
4802 defines, and include paths that make those headers work properly.
4803
4804 With allow_circular_includes_from, B can include A's headers, and
4805 transitively from A's dependencies, without having the dependencies that
4806 would bring in the public_configs those headers need. The result may be
4807 errors or inconsistent builds.
4808
4809 So when you use allow_circular_includes_from, make sure that any compiler
4810 settings, flags, and include directories are the same between both targets
4811 (consider putting such things in a shared config they can both reference).
4812 Make sure the dependencies are also the same (you might consider a group to
4813 collect such dependencies they both depend on).
4814```
4815
4816#### **Example**
4817
4818```
4819 source_set("a") {
4820 deps = [ ":b", ":a_b_shared_deps" ]
4821 allow_circular_includes_from = [ ":b" ]
4822 ...
4823 }
4824
4825 source_set("b") {
4826 deps = [ ":a_b_shared_deps" ]
4827 # Sources here can include headers from a despite lack of deps.
4828 ...
4829 }
4830
4831 group("a_b_shared_deps") {
4832 public_deps = [ ":c" ]
4833 }
4834```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004835### **arflags**: Arguments passed to static_library archiver.
Brett Wilson796ed472018-07-16 15:11:09 -07004836
4837```
4838 A list of flags passed to the archive/lib command that creates static
4839 libraries.
4840
4841 arflags are NOT pushed to dependents, so applying arflags to source sets or
4842 any other target type will be a no-op. As with ldflags, you could put the
4843 arflags in a config and set that as a public or "all dependent" config, but
4844 that will likely not be what you want. If you have a chain of static
4845 libraries dependent on each other, this can cause the flags to propagate up
4846 to other static libraries. Due to the nature of how arflags are typically
4847 used, you will normally want to apply them directly on static_library targets
4848 themselves.
4849```
4850
4851#### **Ordering of flags and values**
4852
4853```
4854 1. Those set on the current target (not in a config).
4855 2. Those set on the "configs" on the target in order that the
4856 configs appear in the list.
4857 3. Those set on the "all_dependent_configs" on the target in order
4858 that the configs appear in the list.
4859 4. Those set on the "public_configs" on the target in order that
4860 those configs appear in the list.
4861 5. all_dependent_configs pulled from dependencies, in the order of
4862 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004863 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07004864 6. public_configs pulled from dependencies, in the order of the
4865 "deps" list. If a dependency is public, they will be applied
4866 recursively.
4867```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004868### **args**: (target variable) Arguments passed to an action.
Brett Wilson796ed472018-07-16 15:11:09 -07004869
4870```
4871 For action and action_foreach targets, args is the list of arguments to pass
4872 to the script. Typically you would use source expansion (see "gn help
4873 source_expansion") to insert the source file names.
4874
Adrian Taylorc0d4f8b2022-03-25 15:05:57 -07004875 Args can also expand the substitution patterns corresponding to config
4876 variables in the same way that compiler tools (see "gn help tool") do. These
4877 allow actions that run compiler or compiler-like tools to access the results
4878 of propagating configs through the build graph. For example:
4879
4880 args = [ "{{defines}}", "{{include_dirs}}", "{{rustenv}}", "--input-file",
4881 "{{source}}" ]
4882
Brett Wilson796ed472018-07-16 15:11:09 -07004883 See also "gn help action" and "gn help action_foreach".
4884```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004885### **asmflags**: Flags passed to the assembler.
Brett Wilson796ed472018-07-16 15:11:09 -07004886
4887```
4888 A list of strings.
4889
4890 "asmflags" are passed to any invocation of a tool that takes an .asm or .S
4891 file as input.
4892```
4893
4894#### **Ordering of flags and values**
4895
4896```
4897 1. Those set on the current target (not in a config).
4898 2. Those set on the "configs" on the target in order that the
4899 configs appear in the list.
4900 3. Those set on the "all_dependent_configs" on the target in order
4901 that the configs appear in the list.
4902 4. Those set on the "public_configs" on the target in order that
4903 those configs appear in the list.
4904 5. all_dependent_configs pulled from dependencies, in the order of
4905 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01004906 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07004907 6. public_configs pulled from dependencies, in the order of the
4908 "deps" list. If a dependency is public, they will be applied
4909 recursively.
4910```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004911### **assert_no_deps**: Ensure no deps on these targets.
Brett Wilson796ed472018-07-16 15:11:09 -07004912
4913```
4914 A list of label patterns.
4915
4916 This list is a list of patterns that must not match any of the transitive
4917 dependencies of the target. These include all public, private, and data
4918 dependencies, and cross shared library boundaries. This allows you to express
4919 that undesirable code isn't accidentally added to downstream dependencies in
4920 a way that might otherwise be difficult to notice.
4921
4922 Checking does not cross executable boundaries. If a target depends on an
4923 executable, it's assumed that the executable is a tool that is producing part
4924 of the build rather than something that is linked and distributed. This
4925 allows assert_no_deps to express what is distributed in the final target
4926 rather than depend on the internal build steps (which may include
4927 non-distributable code).
4928
4929 See "gn help label_pattern" for the format of the entries in the list. These
4930 patterns allow blacklisting individual targets or whole directory
4931 hierarchies.
4932
4933 Sometimes it is desirable to enforce that many targets have no dependencies
4934 on a target or set of targets. One efficient way to express this is to create
4935 a group with the assert_no_deps rule on it, and make that group depend on all
4936 targets you want to apply that assertion to.
4937```
4938
4939#### **Example**
4940
4941```
4942 executable("doom_melon") {
4943 deps = [ "//foo:bar" ]
4944 ...
4945 assert_no_deps = [
4946 "//evil/*", # Don't link any code from the evil directory.
4947 "//foo:test_support", # This target is also disallowed.
4948 ]
4949 }
4950```
Sylvain Defresne89e64252020-08-07 13:01:06 +02004951### **bridge_header**: [string] Path to C/Objective-C compatibility header.
4952
4953```
4954 Valid for binary targets that contain Swift sources.
4955
4956 Path to an header that includes C/Objective-C functions and types that
4957 needs to be made available to the Swift module.
4958```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004959### **bundle_contents_dir**: Expansion of {{bundle_contents_dir}} in
Brett Wilson796ed472018-07-16 15:11:09 -07004960```
4961 create_bundle.
4962
4963 A string corresponding to a path in $root_build_dir.
4964
4965 This string is used by the "create_bundle" target to expand the
4966 {{bundle_contents_dir}} of the "bundle_data" target it depends on. This must
4967 correspond to a path under "bundle_root_dir".
4968
4969 See "gn help bundle_root_dir" for examples.
4970```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08004971### **bundle_deps_filter**: [label list] A list of labels that are filtered out.
Brett Wilson796ed472018-07-16 15:11:09 -07004972
4973```
4974 A list of target labels.
4975
4976 This list contains target label patterns that should be filtered out when
4977 creating the bundle. Any target matching one of those label will be removed
4978 from the dependencies of the create_bundle target.
4979
4980 This is mostly useful when creating application extension bundle as the
4981 application extension has access to runtime resources from the application
4982 bundle and thus do not require a second copy.
4983
4984 See "gn help create_bundle" for more information.
4985```
4986
4987#### **Example**
4988
4989```
4990 create_bundle("today_extension") {
4991 deps = [
4992 "//base"
4993 ]
4994 bundle_root_dir = "$root_out_dir/today_extension.appex"
4995 bundle_deps_filter = [
4996 # The extension uses //base but does not use any function calling into
4997 # third_party/icu and thus does not need the icudtl.dat file.
4998 "//third_party/icu:icudata",
4999 ]
5000 }
5001```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02005002### **bundle_executable_dir**
Brett Wilson796ed472018-07-16 15:11:09 -07005003
Joe Armstrongb199e542019-03-21 09:40:08 +08005004```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02005005 bundle_executable_dir: Expansion of {{bundle_executable_dir}} in
5006 create_bundle.
5007
Brett Wilson796ed472018-07-16 15:11:09 -07005008 A string corresponding to a path in $root_build_dir.
5009
5010 This string is used by the "create_bundle" target to expand the
5011 {{bundle_executable_dir}} of the "bundle_data" target it depends on. This
5012 must correspond to a path under "bundle_root_dir".
5013
5014 See "gn help bundle_root_dir" for examples.
5015```
Joe Armstrongb199e542019-03-21 09:40:08 +08005016### **bundle_resources_dir**
5017
Brett Wilson796ed472018-07-16 15:11:09 -07005018```
Sylvain Defresned46f92f2019-04-03 16:14:29 +02005019 bundle_resources_dir: Expansion of {{bundle_resources_dir}} in
5020 create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07005021
5022 A string corresponding to a path in $root_build_dir.
5023
5024 This string is used by the "create_bundle" target to expand the
5025 {{bundle_resources_dir}} of the "bundle_data" target it depends on. This must
5026 correspond to a path under "bundle_root_dir".
5027
5028 See "gn help bundle_root_dir" for examples.
5029```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005030### **bundle_root_dir**: Expansion of {{bundle_root_dir}} in create_bundle.
Brett Wilson796ed472018-07-16 15:11:09 -07005031
5032```
5033 A string corresponding to a path in root_build_dir.
5034
5035 This string is used by the "create_bundle" target to expand the
5036 {{bundle_root_dir}} of the "bundle_data" target it depends on. This must
5037 correspond to a path under root_build_dir.
5038```
5039
5040#### **Example**
5041
5042```
5043 bundle_data("info_plist") {
5044 sources = [ "Info.plist" ]
5045 outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
5046 }
5047
5048 create_bundle("doom_melon.app") {
5049 deps = [ ":info_plist" ]
5050 bundle_root_dir = "${root_build_dir}/doom_melon.app"
5051 bundle_contents_dir = "${bundle_root_dir}/Contents"
5052 bundle_resources_dir = "${bundle_contents_dir}/Resources"
5053 bundle_executable_dir = "${bundle_contents_dir}/MacOS"
Brett Wilson796ed472018-07-16 15:11:09 -07005054 }
5055```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005056### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005057
5058```
5059 A list of strings.
5060
5061 "cflags" are passed to all invocations of the C, C++, Objective C, and
5062 Objective C++ compilers.
5063
5064 To target one of these variants individually, use "cflags_c", "cflags_cc",
5065 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5066 versions of cflags* will be appended on the compiler command line after
5067 "cflags".
5068
Sylvain Defresne89e64252020-08-07 13:01:06 +02005069 See also "asmflags" for flags for assembly-language files and "swiftflags"
5070 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005071```
5072
5073#### **Ordering of flags and values**
5074
5075```
5076 1. Those set on the current target (not in a config).
5077 2. Those set on the "configs" on the target in order that the
5078 configs appear in the list.
5079 3. Those set on the "all_dependent_configs" on the target in order
5080 that the configs appear in the list.
5081 4. Those set on the "public_configs" on the target in order that
5082 those configs appear in the list.
5083 5. all_dependent_configs pulled from dependencies, in the order of
5084 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005085 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005086 6. public_configs pulled from dependencies, in the order of the
5087 "deps" list. If a dependency is public, they will be applied
5088 recursively.
5089```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005090### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005091
5092```
5093 A list of strings.
5094
5095 "cflags" are passed to all invocations of the C, C++, Objective C, and
5096 Objective C++ compilers.
5097
5098 To target one of these variants individually, use "cflags_c", "cflags_cc",
5099 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5100 versions of cflags* will be appended on the compiler command line after
5101 "cflags".
5102
Sylvain Defresne89e64252020-08-07 13:01:06 +02005103 See also "asmflags" for flags for assembly-language files and "swiftflags"
5104 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005105```
5106
5107#### **Ordering of flags and values**
5108
5109```
5110 1. Those set on the current target (not in a config).
5111 2. Those set on the "configs" on the target in order that the
5112 configs appear in the list.
5113 3. Those set on the "all_dependent_configs" on the target in order
5114 that the configs appear in the list.
5115 4. Those set on the "public_configs" on the target in order that
5116 those configs appear in the list.
5117 5. all_dependent_configs pulled from dependencies, in the order of
5118 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005119 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005120 6. public_configs pulled from dependencies, in the order of the
5121 "deps" list. If a dependency is public, they will be applied
5122 recursively.
5123```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005124### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005125
5126```
5127 A list of strings.
5128
5129 "cflags" are passed to all invocations of the C, C++, Objective C, and
5130 Objective C++ compilers.
5131
5132 To target one of these variants individually, use "cflags_c", "cflags_cc",
5133 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5134 versions of cflags* will be appended on the compiler command line after
5135 "cflags".
5136
Sylvain Defresne89e64252020-08-07 13:01:06 +02005137 See also "asmflags" for flags for assembly-language files and "swiftflags"
5138 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005139```
5140
5141#### **Ordering of flags and values**
5142
5143```
5144 1. Those set on the current target (not in a config).
5145 2. Those set on the "configs" on the target in order that the
5146 configs appear in the list.
5147 3. Those set on the "all_dependent_configs" on the target in order
5148 that the configs appear in the list.
5149 4. Those set on the "public_configs" on the target in order that
5150 those configs appear in the list.
5151 5. all_dependent_configs pulled from dependencies, in the order of
5152 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005153 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005154 6. public_configs pulled from dependencies, in the order of the
5155 "deps" list. If a dependency is public, they will be applied
5156 recursively.
5157```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005158### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005159
5160```
5161 A list of strings.
5162
5163 "cflags" are passed to all invocations of the C, C++, Objective C, and
5164 Objective C++ compilers.
5165
5166 To target one of these variants individually, use "cflags_c", "cflags_cc",
5167 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5168 versions of cflags* will be appended on the compiler command line after
5169 "cflags".
5170
Sylvain Defresne89e64252020-08-07 13:01:06 +02005171 See also "asmflags" for flags for assembly-language files and "swiftflags"
5172 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005173```
5174
5175#### **Ordering of flags and values**
5176
5177```
5178 1. Those set on the current target (not in a config).
5179 2. Those set on the "configs" on the target in order that the
5180 configs appear in the list.
5181 3. Those set on the "all_dependent_configs" on the target in order
5182 that the configs appear in the list.
5183 4. Those set on the "public_configs" on the target in order that
5184 those configs appear in the list.
5185 5. all_dependent_configs pulled from dependencies, in the order of
5186 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005187 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005188 6. public_configs pulled from dependencies, in the order of the
5189 "deps" list. If a dependency is public, they will be applied
5190 recursively.
5191```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005192### **cflags***: Flags passed to the C compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07005193
5194```
5195 A list of strings.
5196
5197 "cflags" are passed to all invocations of the C, C++, Objective C, and
5198 Objective C++ compilers.
5199
5200 To target one of these variants individually, use "cflags_c", "cflags_cc",
5201 "cflags_objc", and "cflags_objcc", respectively. These variant-specific
5202 versions of cflags* will be appended on the compiler command line after
5203 "cflags".
5204
Sylvain Defresne89e64252020-08-07 13:01:06 +02005205 See also "asmflags" for flags for assembly-language files and "swiftflags"
5206 for swift files.
Brett Wilson796ed472018-07-16 15:11:09 -07005207```
5208
5209#### **Ordering of flags and values**
5210
5211```
5212 1. Those set on the current target (not in a config).
5213 2. Those set on the "configs" on the target in order that the
5214 configs appear in the list.
5215 3. Those set on the "all_dependent_configs" on the target in order
5216 that the configs appear in the list.
5217 4. Those set on the "public_configs" on the target in order that
5218 those configs appear in the list.
5219 5. all_dependent_configs pulled from dependencies, in the order of
5220 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005221 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005222 6. public_configs pulled from dependencies, in the order of the
5223 "deps" list. If a dependency is public, they will be applied
5224 recursively.
5225```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005226### **check_includes**: [boolean] Controls whether a target's files are checked.
Brett Wilson796ed472018-07-16 15:11:09 -07005227
5228```
5229 When true (the default), the "gn check" command (as well as "gn gen" with the
5230 --check flag) will check this target's sources and headers for proper
5231 dependencies.
5232
5233 When false, the files in this target will be skipped by default. This does
5234 not affect other targets that depend on the current target, it just skips
5235 checking the includes of the current target's files.
5236
5237 If there are a few conditionally included headers that trip up checking, you
5238 can exclude headers individually by annotating them with "nogncheck" (see "gn
5239 help nogncheck").
5240
5241 The topic "gn help check" has general information on how checking works and
5242 advice on how to pass a check in problematic cases.
5243```
5244
5245#### **Example**
5246
5247```
5248 source_set("busted_includes") {
5249 # This target's includes are messed up, exclude it from checking.
5250 check_includes = false
5251 ...
5252 }
5253```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005254### **code_signing_args**: [string list] Arguments passed to code signing script.
Brett Wilson796ed472018-07-16 15:11:09 -07005255
5256```
5257 For create_bundle targets, code_signing_args is the list of arguments to pass
5258 to the code signing script. Typically you would use source expansion (see "gn
5259 help source_expansion") to insert the source file names.
5260
5261 See also "gn help create_bundle".
5262```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005263### **code_signing_outputs**: [file list] Output files for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005264
5265```
5266 Outputs from the code signing step of a create_bundle target. Must refer to
5267 files in the build directory.
5268
5269 See also "gn help create_bundle".
5270```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005271### **code_signing_script**: [file name] Script for code signing."
Brett Wilson796ed472018-07-16 15:11:09 -07005272
5273```
5274 An absolute or buildfile-relative file name of a Python script to run for a
5275 create_bundle target to perform code signing step.
5276
5277 See also "gn help create_bundle".
5278```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005279### **code_signing_sources**: [file list] Sources for code signing step.
Brett Wilson796ed472018-07-16 15:11:09 -07005280
5281```
5282 A list of files used as input for code signing script step of a create_bundle
5283 target. Non-absolute paths will be resolved relative to the current build
5284 file.
5285
5286 See also "gn help create_bundle".
5287```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005288### **complete_static_lib**: [boolean] Links all deps into a static library.
Brett Wilson796ed472018-07-16 15:11:09 -07005289
5290```
5291 A static library normally doesn't include code from dependencies, but instead
5292 forwards the static libraries and source sets in its deps up the dependency
5293 chain until a linkable target (an executable or shared library) is reached.
5294 The final linkable target only links each static library once, even if it
5295 appears more than once in its dependency graph.
5296
5297 In some cases the static library might be the final desired output. For
5298 example, you may be producing a static library for distribution to third
5299 parties. In this case, the static library should include code for all
5300 dependencies in one complete package. However, complete static libraries
5301 themselves are never linked into other complete static libraries. All
5302 complete static libraries are for distribution and linking them in would
5303 cause code duplication in this case. If the static library is not for
5304 distribution, it should not be complete.
5305
5306 GN treats non-complete static libraries as source sets when they are linked
5307 into complete static libraries. This is done because some tools like AR do
5308 not handle dependent static libraries properly. This makes it easier to write
5309 "alink" rules.
5310
5311 In rare cases it makes sense to list a header in more than one target if it
5312 could be considered conceptually a member of both. libraries.
5313```
5314
5315#### **Example**
5316
5317```
5318 static_library("foo") {
5319 complete_static_lib = true
5320 deps = [ "bar" ]
5321 }
5322```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005323### **configs**: Configs applying to this target or config.
Brett Wilson796ed472018-07-16 15:11:09 -07005324
5325```
5326 A list of config labels.
5327```
5328
5329#### **Configs on a target**
5330
5331```
5332 When used on a target, the include_dirs, defines, etc. in each config are
5333 appended in the order they appear to the compile command for each file in the
5334 target. They will appear after the include_dirs, defines, etc. that the
5335 target sets directly.
5336
5337 Since configs apply after the values set on a target, directly setting a
5338 compiler flag will prepend it to the command line. If you want to append a
5339 flag instead, you can put that flag in a one-off config and append that
5340 config to the target's configs list.
5341
5342 The build configuration script will generally set up the default configs
5343 applying to a given target type (see "set_defaults"). When a target is being
5344 defined, it can add to or remove from this list.
5345```
5346
5347#### **Configs on a config**
5348
5349```
5350 It is possible to create composite configs by specifying configs on a config.
5351 One might do this to forward values, or to factor out blocks of settings from
5352 very large configs into more manageable named chunks.
5353
5354 In this case, the composite config is expanded to be the concatenation of its
5355 own values, and in order, the values from its sub-configs *before* anything
5356 else happens. This has some ramifications:
5357
5358 - A target has no visibility into a config's sub-configs. Target code only
5359 sees the name of the composite config. It can't remove sub-configs or opt
5360 in to only parts of it. The composite config may not even be defined
5361 before the target is.
5362
5363 - You can get duplication of values if a config is listed twice, say, on a
5364 target and in a sub-config that also applies. In other cases, the configs
5365 applying to a target are de-duped. It's expected that if a config is
5366 listed as a sub-config that it is only used in that context. (Note that
5367 it's possible to fix this and de-dupe, but it's not normally relevant and
5368 complicates the implementation.)
5369```
5370
5371#### **Ordering of flags and values**
5372
5373```
5374 1. Those set on the current target (not in a config).
5375 2. Those set on the "configs" on the target in order that the
5376 configs appear in the list.
5377 3. Those set on the "all_dependent_configs" on the target in order
5378 that the configs appear in the list.
5379 4. Those set on the "public_configs" on the target in order that
5380 those configs appear in the list.
5381 5. all_dependent_configs pulled from dependencies, in the order of
5382 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005383 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005384 6. public_configs pulled from dependencies, in the order of the
5385 "deps" list. If a dependency is public, they will be applied
5386 recursively.
5387```
5388
5389#### **Example**
5390
5391```
5392 # Configs on a target.
5393 source_set("foo") {
5394 # Don't use the default RTTI config that BUILDCONFIG applied to us.
5395 configs -= [ "//build:no_rtti" ]
5396
5397 # Add some of our own settings.
5398 configs += [ ":mysettings" ]
5399 }
5400
5401 # Create a default_optimization config that forwards to one of a set of more
5402 # specialized configs depending on build flags. This pattern is useful
5403 # because it allows a target to opt in to either a default set, or a more
5404 # specific set, while avoid duplicating the settings in two places.
5405 config("super_optimization") {
5406 cflags = [ ... ]
5407 }
5408 config("default_optimization") {
5409 if (optimize_everything) {
5410 configs = [ ":super_optimization" ]
5411 } else {
5412 configs = [ ":no_optimization" ]
5413 }
5414 }
5415```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005416### **contents**: Contents to write to file.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005417
5418```
5419 The contents of the file for a generated_file target.
5420 See "gn help generated_file".
5421```
Julie Hockettce1fa072019-05-07 17:44:37 -07005422### **crate_name**: [string] The name for the compiled crate.
5423
5424```
5425 Valid for `rust_library` targets and `executable`, `static_library`,
5426 `shared_library`, and `source_set` targets that contain Rust sources.
5427
5428 If crate_name is not set, then this rule will use the target name.
5429```
5430### **crate_root**: [string] The root source file for a binary or library.
5431
5432```
5433 Valid for `rust_library` targets and `executable`, `static_library`,
5434 `shared_library`, and `source_set` targets that contain Rust sources.
5435
5436 This file is usually the `main.rs` or `lib.rs` for binaries and libraries,
5437 respectively.
5438
5439 If crate_root is not set, then this rule will look for a lib.rs file (or
5440 main.rs for executable) or a single file in sources, if sources contains
5441 only one file.
5442```
5443### **crate_type**: [string] The type of linkage to use on a shared_library.
5444
5445```
5446 Valid for `rust_library` targets and `executable`, `static_library`,
5447 `shared_library`, and `source_set` targets that contain Rust sources.
5448
5449 Options for this field are "cdylib", "staticlib", "proc-macro", and "dylib".
5450 This field sets the `crate-type` attribute for the `rustc` tool on static
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005451 libraries, as well as the appropriate output extension in the
Julie Hockettce1fa072019-05-07 17:44:37 -07005452 `rust_output_extension` attribute. Since outputs must be explicit, the `lib`
5453 crate type (where the Rust compiler produces what it thinks is the
5454 appropriate library type) is not supported.
5455
5456 It should be noted that the "dylib" crate type in Rust is unstable in the set
5457 of symbols it exposes, and most usages today are potentially wrong and will
5458 be broken in the future.
5459
5460 Static libraries, rust libraries, and executables have this field set
5461 automatically.
5462```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005463### **data**: Runtime data file dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005464
5465```
5466 Lists files or directories required to run the given target. These are
5467 typically data files or directories of data files. The paths are interpreted
5468 as being relative to the current build file. Since these are runtime
5469 dependencies, they do not affect which targets are built or when. To declare
5470 input files to a script, use "inputs".
5471
5472 Appearing in the "data" section does not imply any special handling such as
5473 copying them to the output directory. This is just used for declaring runtime
5474 dependencies. Runtime dependencies can be queried using the "runtime_deps"
5475 category of "gn desc" or written during build generation via
5476 "--runtime-deps-list-file".
5477
5478 GN doesn't require data files to exist at build-time. So actions that produce
5479 files that are in turn runtime dependencies can list those generated files
5480 both in the "outputs" list as well as the "data" list.
5481
5482 By convention, directories are listed with a trailing slash:
5483 data = [ "test/data/" ]
5484 However, no verification is done on these so GN doesn't enforce this. The
5485 paths are just rebased and passed along when requested.
5486
5487 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5488 gathering data. See "gn help create_bundle" for details.
5489
5490 See "gn help runtime_deps" for how these are used.
5491```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005492### **data_deps**: Non-linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005493
5494```
5495 A list of target labels.
5496
5497 Specifies dependencies of a target that are not actually linked into the
5498 current target. Such dependencies will be built and will be available at
5499 runtime.
5500
5501 This is normally used for things like plugins or helper programs that a
5502 target needs at runtime.
5503
5504 Note: On iOS and macOS, create_bundle targets will not be recursed into when
5505 gathering data_deps. See "gn help create_bundle" for details.
5506
5507 See also "gn help deps" and "gn help data".
5508```
5509
5510#### **Example**
5511
5512```
5513 executable("foo") {
5514 deps = [ "//base" ]
5515 data_deps = [ "//plugins:my_runtime_plugin" ]
5516 }
5517```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005518### **data_keys**: Keys from which to collect metadata.
Nate Fischer8ed01d32019-01-08 17:32:01 -08005519
5520```
5521 These keys are used to identify metadata to collect. If a walked target
5522 defines this key in its metadata, its value will be appended to the resulting
5523 collection.
5524
5525 See "gn help generated_file".
5526```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005527### **defines**: C preprocessor defines.
Brett Wilson796ed472018-07-16 15:11:09 -07005528
5529```
5530 A list of strings
5531
5532 These strings will be passed to the C/C++ compiler as #defines. The strings
5533 may or may not include an "=" to assign a value.
5534```
5535
5536#### **Ordering of flags and values**
5537
5538```
5539 1. Those set on the current target (not in a config).
5540 2. Those set on the "configs" on the target in order that the
5541 configs appear in the list.
5542 3. Those set on the "all_dependent_configs" on the target in order
5543 that the configs appear in the list.
5544 4. Those set on the "public_configs" on the target in order that
5545 those configs appear in the list.
5546 5. all_dependent_configs pulled from dependencies, in the order of
5547 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005548 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005549 6. public_configs pulled from dependencies, in the order of the
5550 "deps" list. If a dependency is public, they will be applied
5551 recursively.
5552```
5553
5554#### **Example**
5555
5556```
5557 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
5558```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005559### **depfile**: [string] File name for input dependencies for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07005560
5561```
5562 If nonempty, this string specifies that the current action or action_foreach
5563 target will generate the given ".d" file containing the dependencies of the
5564 input. Empty or unset means that the script doesn't generate the files.
5565
5566 A depfile should be used only when a target depends on files that are not
5567 already specified by a target's inputs and sources. Likewise, depfiles should
5568 specify only those dependencies not already included in sources or inputs.
5569
5570 The .d file should go in the target output directory. If you have more than
5571 one source file that the script is being run over, you can use the output
5572 file expansions described in "gn help action_foreach" to name the .d file
Nico Weberad9eab22020-11-15 22:20:28 -05005573 according to the input.
Brett Wilson796ed472018-07-16 15:11:09 -07005574
5575 The format is that of a Makefile and all paths must be relative to the root
5576 build directory. Only one output may be listed and it must match the first
5577 output of the action.
5578
5579 Although depfiles are created by an action, they should not be listed in the
5580 action's "outputs" unless another target will use the file as an input.
5581```
5582
5583#### **Example**
5584
5585```
5586 action_foreach("myscript_target") {
5587 script = "myscript.py"
5588 sources = [ ... ]
5589
5590 # Locate the depfile in the output directory named like the
5591 # inputs but with a ".d" appended.
5592 depfile = "$relative_target_output_dir/{{source_name}}.d"
5593
5594 # Say our script uses "-o " to indicate the depfile.
5595 args = [ "{{source}}", "-o", depfile ]
5596 }
5597```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005598### **deps**: Private linked dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005599
5600```
5601 A list of target labels.
5602
5603 Specifies private dependencies of a target. Private dependencies are
5604 propagated up the dependency tree and linked to dependent targets, but do not
5605 grant the ability to include headers from the dependency. Public configs are
5606 not forwarded.
5607```
5608
5609#### **Details of dependency propagation**
5610
5611```
5612 Source sets, shared libraries, and non-complete static libraries will be
5613 propagated up the dependency tree across groups, non-complete static
5614 libraries and source sets.
5615
5616 Executables, shared libraries, and complete static libraries will link all
5617 propagated targets and stop propagation. Actions and copy steps also stop
5618 propagation, allowing them to take a library as an input but not force
5619 dependents to link to it.
5620
5621 Propagation of all_dependent_configs and public_configs happens independently
5622 of target type. all_dependent_configs are always propagated across all types
5623 of targets, and public_configs are always propagated across public deps of
5624 all types of targets.
5625
5626 Data dependencies are propagated differently. See "gn help data_deps" and
5627 "gn help runtime_deps".
5628
5629 See also "public_deps".
5630```
Petr Hoseka1413862020-01-03 12:54:33 -08005631### **externs**: [scope] Set of Rust crate-dependency pairs.
5632
5633```
5634 A list, each value being a scope indicating a pair of crate name and the path
5635 to the Rust library.
5636
5637 These libraries will be passed as `--extern crate_name=path` to compiler
5638 invocation containing the current target.
5639```
5640
5641#### **Examples**
5642
5643```
5644 executable("foo") {
5645 sources = [ "main.rs" ]
5646 externs = [{
5647 crate_name = "bar",
5648 path = "path/to/bar.rlib"
5649 }]
5650 }
5651
5652 This target would compile the `foo` crate with the following `extern` flag:
5653 `--extern bar=path/to/bar.rlib`.
5654```
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005655### **framework_dirs**: [directory list] Additional framework search directories.
5656
5657```
5658 A list of source directories.
5659
5660 The directories in this list will be added to the framework search path for
5661 the files in the affected target.
5662```
5663
5664#### **Ordering of flags and values**
5665
5666```
5667 1. Those set on the current target (not in a config).
5668 2. Those set on the "configs" on the target in order that the
5669 configs appear in the list.
5670 3. Those set on the "all_dependent_configs" on the target in order
5671 that the configs appear in the list.
5672 4. Those set on the "public_configs" on the target in order that
5673 those configs appear in the list.
5674 5. all_dependent_configs pulled from dependencies, in the order of
5675 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005676 more than once, only the first occurrence will be used.
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005677 6. public_configs pulled from dependencies, in the order of the
5678 "deps" list. If a dependency is public, they will be applied
5679 recursively.
5680```
5681
5682#### **Example**
5683
5684```
5685 framework_dirs = [ "src/include", "//third_party/foo" ]
5686```
5687### **frameworks**: [name list] Name of frameworks that must be linked.
5688
5689```
5690 A list of framework names.
5691
5692 The frameworks named in that list will be linked with any dynamic link
5693 type target.
5694```
5695
5696#### **Ordering of flags and values**
5697
5698```
5699 1. Those set on the current target (not in a config).
5700 2. Those set on the "configs" on the target in order that the
5701 configs appear in the list.
5702 3. Those set on the "all_dependent_configs" on the target in order
5703 that the configs appear in the list.
5704 4. Those set on the "public_configs" on the target in order that
5705 those configs appear in the list.
5706 5. all_dependent_configs pulled from dependencies, in the order of
5707 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005708 more than once, only the first occurrence will be used.
Sylvain Defresnea09ec162020-01-16 12:36:40 +01005709 6. public_configs pulled from dependencies, in the order of the
5710 "deps" list. If a dependency is public, they will be applied
5711 recursively.
5712```
5713
5714#### **Example**
5715
5716```
5717 frameworks = [ "Foundation.framework", "Foo.framework" ]
5718```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005719### **friend**: Allow targets to include private headers.
Brett Wilson796ed472018-07-16 15:11:09 -07005720
5721```
5722 A list of label patterns (see "gn help label_pattern") that allow dependent
5723 targets to include private headers. Applies to all binary targets.
5724
5725 Normally if a target lists headers in the "public" list (see "gn help
5726 public"), other headers are implicitly marked as private. Private headers
5727 can not be included by other targets, even with a public dependency path.
5728 The "gn check" function performs this validation.
5729
5730 A friend declaration allows one or more targets to include private headers.
5731 This is useful for things like unit tests that are closely associated with a
5732 target and require internal knowledge without opening up all headers to be
5733 included by all dependents.
5734
5735 A friend target does not allow that target to include headers when no
5736 dependency exists. A public dependency path must still exist between two
5737 targets to include any headers from a destination target. The friend
5738 annotation merely allows the use of headers that would otherwise be
5739 prohibited because they are private.
5740
5741 The friend annotation is matched only against the target containing the file
5742 with the include directive. Friend annotations are not propagated across
5743 public or private dependencies. Friend annotations do not affect visibility.
5744```
5745
5746#### **Example**
5747
5748```
5749 static_library("lib") {
5750 # This target can include our private headers.
5751 friend = [ ":unit_tests" ]
5752
5753 public = [
5754 "public_api.h", # Normal public API for dependent targets.
5755 ]
5756
5757 # Private API and sources.
5758 sources = [
5759 "a_source_file.cc",
5760
5761 # Normal targets that depend on this one won't be able to include this
5762 # because this target defines a list of "public" headers. Without the
5763 # "public" list, all headers are implicitly public.
5764 "private_api.h",
5765 ]
5766 }
5767
5768 executable("unit_tests") {
5769 sources = [
5770 # This can include "private_api.h" from the :lib target because it
5771 # depends on that target and because of the friend annotation.
5772 "my_test.cc",
5773 ]
5774
5775 deps = [
5776 ":lib", # Required for the include to be allowed.
5777 ]
5778 }
5779```
Tyler Mandry4a648092022-02-15 19:47:09 +00005780### **gen_deps**: Declares targets that should generate when this one does.
5781
5782```
5783 A list of target labels.
5784
5785 Not all GN targets that get evaluated are actually turned into ninja targets
5786 (see "gn help execution"). If this target is generated, then any targets in
5787 the "gen_deps" list will also be generated, regardless of the usual critera.
5788
5789 Since "gen_deps" are not build time dependencies, there can be cycles between
5790 "deps" and "gen_deps" or within "gen_deps" itself.
5791```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005792### **include_dirs**: Additional include directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005793
5794```
5795 A list of source directories.
5796
5797 The directories in this list will be added to the include path for the files
5798 in the affected target.
5799```
5800
5801#### **Ordering of flags and values**
5802
5803```
5804 1. Those set on the current target (not in a config).
5805 2. Those set on the "configs" on the target in order that the
5806 configs appear in the list.
5807 3. Those set on the "all_dependent_configs" on the target in order
5808 that the configs appear in the list.
5809 4. Those set on the "public_configs" on the target in order that
5810 those configs appear in the list.
5811 5. all_dependent_configs pulled from dependencies, in the order of
5812 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005813 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005814 6. public_configs pulled from dependencies, in the order of the
5815 "deps" list. If a dependency is public, they will be applied
5816 recursively.
5817```
5818
5819#### **Example**
5820
5821```
5822 include_dirs = [ "src/include", "//third_party/foo" ]
5823```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005824### **inputs**: Additional compile-time dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07005825
5826```
5827 Inputs are compile-time dependencies of the current target. This means that
5828 all inputs must be available before compiling any of the sources or executing
5829 any actions.
5830
5831 Inputs are typically only used for action and action_foreach targets.
5832```
5833
5834#### **Inputs for actions**
5835
5836```
5837 For action and action_foreach targets, inputs should be the inputs to script
5838 that don't vary. These should be all .py files that the script uses via
5839 imports (the main script itself will be an implicit dependency of the action
5840 so need not be listed).
5841
5842 For action targets, inputs and sources are treated the same, but from a style
5843 perspective, it's recommended to follow the same rule as action_foreach and
5844 put helper files in the inputs, and the data used by the script (if any) in
5845 sources.
5846
5847 Note that another way to declare input dependencies from an action is to have
5848 the action write a depfile (see "gn help depfile"). This allows the script to
5849 dynamically write input dependencies, that might not be known until actually
5850 executing the script. This is more efficient than doing processing while
5851 running GN to determine the inputs, and is easier to keep in-sync than
5852 hardcoding the list.
5853```
5854
5855#### **Script input gotchas**
5856
5857```
5858 It may be tempting to write a script that enumerates all files in a directory
5859 as inputs. Don't do this! Even if you specify all the files in the inputs or
5860 sources in the GN target (or worse, enumerate the files in an exec_script
5861 call when running GN, which will be slow), the dependencies will be broken.
5862
5863 The problem happens if a file is ever removed because the inputs are not
5864 listed on the command line to the script. Because the script hasn't changed
5865 and all inputs are up to date, the script will not re-run and you will get a
5866 stale build. Instead, either list all inputs on the command line to the
5867 script, or if there are many, create a separate list file that the script
5868 reads. As long as this file is listed in the inputs, the build will detect
5869 when it has changed in any way and the action will re-run.
5870```
5871
5872#### **Inputs for binary targets**
5873
5874```
Julie Hockette2a29402018-07-31 10:11:42 -07005875 Any input dependencies will be resolved before compiling any sources or
5876 linking the target. Normally, all actions that a target depends on will be run
5877 before any files in a target are compiled. So if you depend on generated
5878 headers, you do not typically need to list them in the inputs section.
Brett Wilson796ed472018-07-16 15:11:09 -07005879
5880 Inputs for binary targets will be treated as implicit dependencies, meaning
5881 that changes in any of the inputs will force all sources in the target to be
5882 recompiled. If an input only applies to a subset of source files, you may
5883 want to split those into a separate target to avoid unnecessary recompiles.
5884```
5885
5886#### **Example**
5887
5888```
5889 action("myscript") {
5890 script = "domything.py"
5891 inputs = [ "input.data" ]
5892 }
5893```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005894### **ldflags**: Flags passed to the linker.
Brett Wilson796ed472018-07-16 15:11:09 -07005895
5896```
5897 A list of strings.
5898
5899 These flags are passed on the command-line to the linker and generally
5900 specify various linking options. Most targets will not need these and will
5901 use "libs" and "lib_dirs" instead.
5902
5903 ldflags are NOT pushed to dependents, so applying ldflags to source sets or
5904 static libraries will be a no-op. If you want to apply ldflags to dependent
5905 targets, put them in a config and set it in the all_dependent_configs or
5906 public_configs.
5907```
5908
5909#### **Ordering of flags and values**
5910
5911```
5912 1. Those set on the current target (not in a config).
5913 2. Those set on the "configs" on the target in order that the
5914 configs appear in the list.
5915 3. Those set on the "all_dependent_configs" on the target in order
5916 that the configs appear in the list.
5917 4. Those set on the "public_configs" on the target in order that
5918 those configs appear in the list.
5919 5. all_dependent_configs pulled from dependencies, in the order of
5920 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005921 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005922 6. public_configs pulled from dependencies, in the order of the
5923 "deps" list. If a dependency is public, they will be applied
5924 recursively.
5925```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005926### **lib_dirs**: Additional library directories.
Brett Wilson796ed472018-07-16 15:11:09 -07005927
5928```
5929 A list of directories.
5930
5931 Specifies additional directories passed to the linker for searching for the
5932 required libraries. If an item is not an absolute path, it will be treated as
5933 being relative to the current build file.
5934
5935 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005936 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005937 shared library or executable target is reached. Second, they are
5938 uniquified so each one is only passed once (the first instance of it
5939 will be the one used).
5940```
5941
5942#### **Ordering of flags and values**
5943
5944```
5945 1. Those set on the current target (not in a config).
5946 2. Those set on the "configs" on the target in order that the
5947 configs appear in the list.
5948 3. Those set on the "all_dependent_configs" on the target in order
5949 that the configs appear in the list.
5950 4. Those set on the "public_configs" on the target in order that
5951 those configs appear in the list.
5952 5. all_dependent_configs pulled from dependencies, in the order of
5953 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01005954 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07005955 6. public_configs pulled from dependencies, in the order of the
5956 "deps" list. If a dependency is public, they will be applied
5957 recursively.
5958
5959 For "libs" and "lib_dirs" only, the values propagated from
5960 dependencies (as described above) are applied last assuming they
5961 are not already in the list.
5962```
5963
5964#### **Example**
5965
5966```
5967 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
5968```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08005969### **libs**: Additional libraries to link.
Brett Wilson796ed472018-07-16 15:11:09 -07005970
5971```
5972 A list of library names or library paths.
5973
5974 These libraries will be linked into the final binary (executable or shared
5975 library) containing the current target.
5976
5977 libs and lib_dirs work differently than other flags in two respects.
Joe Armstrong23d22bc2019-06-28 15:31:03 +08005978 First, they are inherited across static library boundaries until a
Brett Wilson796ed472018-07-16 15:11:09 -07005979 shared library or executable target is reached. Second, they are
5980 uniquified so each one is only passed once (the first instance of it
5981 will be the one used).
5982```
5983
5984#### **Types of libs**
5985
5986```
5987 There are several different things that can be expressed in libs:
5988
5989 File paths
5990 Values containing '/' will be treated as references to files in the
5991 checkout. They will be rebased to be relative to the build directory and
5992 specified in the "libs" for linker tools. This facility should be used
5993 for libraries that are checked in to the version control. For libraries
5994 that are generated by the build, use normal GN deps to link them.
5995
5996 System libraries
5997 Values not containing '/' will be treated as system library names. These
5998 will be passed unmodified to the linker and prefixed with the
5999 "lib_switch" attribute of the linker tool. Generally you would set the
6000 "lib_dirs" so the given library is found. Your BUILD.gn file should not
6001 specify the switch (like "-l"): this will be encoded in the "lib_switch"
6002 of the tool.
Brett Wilson796ed472018-07-16 15:11:09 -07006003```
6004
6005#### **Ordering of flags and values**
6006
6007```
6008 1. Those set on the current target (not in a config).
6009 2. Those set on the "configs" on the target in order that the
6010 configs appear in the list.
6011 3. Those set on the "all_dependent_configs" on the target in order
6012 that the configs appear in the list.
6013 4. Those set on the "public_configs" on the target in order that
6014 those configs appear in the list.
6015 5. all_dependent_configs pulled from dependencies, in the order of
6016 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006017 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006018 6. public_configs pulled from dependencies, in the order of the
6019 "deps" list. If a dependency is public, they will be applied
6020 recursively.
6021
6022 For "libs" and "lib_dirs" only, the values propagated from
6023 dependencies (as described above) are applied last assuming they
6024 are not already in the list.
6025```
6026
6027#### **Examples**
6028
6029```
6030 On Windows:
6031 libs = [ "ctl3d.lib" ]
6032
6033 On Linux:
6034 libs = [ "ld" ]
6035```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006036### **metadata**: Metadata of this target.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006037
6038```
6039 Metadata is a collection of keys and values relating to a particular target.
Julie Hockettd69a9c32019-01-23 14:36:18 -08006040 Values must be lists, allowing for sane and predictable collection behavior.
6041 Generally, these keys will include three types of lists: lists of ordinary
6042 strings, lists of filenames intended to be rebased according to their
6043 particular source directory, and lists of target labels intended to be used
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006044 as barriers to the walk. Verification of these categories occurs at walk time,
Julie Hockettd69a9c32019-01-23 14:36:18 -08006045 not creation time (since it is not clear until the walk which values are
6046 intended for which purpose).
6047```
Nate Fischer8ed01d32019-01-08 17:32:01 -08006048
Julie Hockettd69a9c32019-01-23 14:36:18 -08006049#### **Example**
Nate Fischer8ed01d32019-01-08 17:32:01 -08006050
Julie Hockettd69a9c32019-01-23 14:36:18 -08006051```
Nate Fischer8ed01d32019-01-08 17:32:01 -08006052 group("doom_melon") {
6053 metadata = {
6054 # These keys are not built in to GN but are interpreted when consuming
6055 # metadata.
6056 my_barrier = []
6057 my_files = [ "a.txt", "b.txt" ]
6058 }
6059 }
6060```
Sylvain Defresne89e64252020-08-07 13:01:06 +02006061### **module_name**: [string] The name for the compiled module.
6062
6063```
6064 Valid for binary targets that contain Swift sources.
6065
6066 If module_name is not set, then this rule will use the target name.
6067```
Wesley Moyd874bb92020-01-21 16:01:04 -08006068### **output_conversion**: Data format for generated_file targets.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006069
6070```
6071 Controls how the "contents" of a generated_file target is formatted.
Sylvain Defresne72d5a6e2021-06-15 17:01:32 +02006072 See `gn help io_conversion`.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006073```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006074### **output_dir**: [directory] Directory to put output file in.
Brett Wilson796ed472018-07-16 15:11:09 -07006075
6076```
6077 For library and executable targets, overrides the directory for the final
6078 output. This must be in the root_build_dir or a child thereof.
6079
6080 This should generally be in the root_out_dir or a subdirectory thereof (the
6081 root_out_dir will be the same as the root_build_dir for the default
6082 toolchain, and will be a subdirectory for other toolchains). Not putting the
6083 output in a subdirectory of root_out_dir can result in collisions between
6084 different toolchains, so you will need to take steps to ensure that your
6085 target is only present in one toolchain.
6086
6087 Normally the toolchain specifies the output directory for libraries and
6088 executables (see "gn help tool"). You will have to consult that for the
6089 default location. The default location will be used if output_dir is
6090 undefined or empty.
6091```
6092
6093#### **Example**
6094
6095```
6096 shared_library("doom_melon") {
6097 output_dir = "$root_out_dir/plugin_libs"
6098 ...
6099 }
6100```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006101### **output_extension**: Value to use for the output's file extension.
Brett Wilson796ed472018-07-16 15:11:09 -07006102
6103```
6104 Normally the file extension for a target is based on the target type and the
6105 operating system, but in rare cases you will need to override the name (for
6106 example to use "libfreetype.so.6" instead of libfreetype.so on Linux).
6107
6108 This value should not include a leading dot. If undefined, the default
6109 specified on the tool will be used. If set to the empty string, no output
6110 extension will be used.
6111
6112 The output_extension will be used to set the "{{output_extension}}" expansion
6113 which the linker tool will generally use to specify the output file name. See
6114 "gn help tool".
6115```
6116
6117#### **Example**
6118
6119```
6120 shared_library("freetype") {
6121 if (is_linux) {
6122 # Call the output "libfreetype.so.6"
6123 output_extension = "so.6"
6124 }
6125 ...
6126 }
6127
6128 # On Windows, generate a "mysettings.cpl" control panel applet. Control panel
6129 # applets are actually special shared libraries.
6130 if (is_win) {
6131 shared_library("mysettings") {
6132 output_extension = "cpl"
6133 ...
6134 }
6135 }
6136```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006137### **output_name**: Define a name for the output file other than the default.
Brett Wilson796ed472018-07-16 15:11:09 -07006138
6139```
6140 Normally the output name of a target will be based on the target name, so the
6141 target "//foo/bar:bar_unittests" will generate an output file such as
6142 "bar_unittests.exe" (using Windows as an example).
6143
6144 Sometimes you will want an alternate name to avoid collisions or if the
6145 internal name isn't appropriate for public distribution.
6146
6147 The output name should have no extension or prefixes, these will be added
6148 using the default system rules. For example, on Linux an output name of "foo"
6149 will produce a shared library "libfoo.so". There is no way to override the
6150 output prefix of a linker tool on a per- target basis. If you need more
6151 flexibility, create a copy target to produce the file you want.
6152
6153 This variable is valid for all binary output target types.
6154```
6155
6156#### **Example**
6157
6158```
6159 static_library("doom_melon") {
6160 output_name = "fluffy_bunny"
6161 }
6162```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006163### **output_prefix_override**: Don't use prefix for output name.
Brett Wilson796ed472018-07-16 15:11:09 -07006164
6165```
6166 A boolean that overrides the output prefix for a target. Defaults to false.
6167
6168 Some systems use prefixes for the names of the final target output file. The
6169 normal example is "libfoo.so" on Linux for a target named "foo".
6170
6171 The output prefix for a given target type is specified on the linker tool
6172 (see "gn help tool"). Sometimes this prefix is undesired.
6173
6174 See also "gn help output_extension".
6175```
6176
6177#### **Example**
6178
6179```
6180 shared_library("doom_melon") {
6181 # Normally this will produce "libdoom_melon.so" on Linux. Setting this flag
6182 # will produce "doom_melon.so".
6183 output_prefix_override = true
6184 ...
6185 }
6186```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006187### **outputs**: Output files for actions and copy targets.
Brett Wilson796ed472018-07-16 15:11:09 -07006188
6189```
6190 Outputs is valid for "copy", "action", and "action_foreach" target types and
6191 indicates the resulting files. Outputs must always refer to files in the
6192 build directory.
6193
6194 copy
6195 Copy targets should have exactly one entry in the outputs list. If there is
6196 exactly one source, this can be a literal file name or a source expansion.
6197 If there is more than one source, this must contain a source expansion to
6198 map a single input name to a single output name. See "gn help copy".
6199
6200 action_foreach
6201 Action_foreach targets must always use source expansions to map input files
6202 to output files. There can be more than one output, which means that each
6203 invocation of the script will produce a set of files (presumably based on
6204 the name of the input file). See "gn help action_foreach".
6205
6206 action
6207 Action targets (excluding action_foreach) must list literal output file(s)
6208 with no source expansions. See "gn help action".
6209```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006210### **partial_info_plist**: [filename] Path plist from asset catalog compiler.
Brett Wilson796ed472018-07-16 15:11:09 -07006211
6212```
6213 Valid for create_bundle target, corresponds to the path for the partial
6214 Info.plist created by the asset catalog compiler that needs to be merged
6215 with the application Info.plist (usually done by the code signing script).
6216
6217 The file will be generated regardless of whether the asset compiler has
6218 been invoked or not. See "gn help create_bundle".
6219```
Petr Hosek7a6231e2022-10-22 23:14:18 +00006220### **pool**: Label of the pool used by binary targets actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006221
6222```
Petr Hosek7a6231e2022-10-22 23:14:18 +00006223 A fully-qualified label representing the pool that will be used for binary
6224 targets and actions. Pools are defined using the pool() {...} declaration.
Brett Wilson796ed472018-07-16 15:11:09 -07006225```
6226
6227#### **Example**
6228
6229```
Petr Hosek7a6231e2022-10-22 23:14:18 +00006230 executable("binary") {
6231 pool = "//build:custom_pool"
6232 ...
6233 }
6234
Brett Wilson796ed472018-07-16 15:11:09 -07006235 action("action") {
6236 pool = "//build:custom_pool"
6237 ...
6238 }
6239```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006240### **precompiled_header**: [string] Header file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07006241
6242```
6243 Precompiled headers will be used when a target specifies this value, or a
6244 config applying to this target specifies this value. In addition, the tool
6245 corresponding to the source files must also specify precompiled headers (see
6246 "gn help tool"). The tool will also specify what type of precompiled headers
6247 to use, by setting precompiled_header_type to either "gcc" or "msvc".
6248
6249 The precompiled header/source variables can be specified on a target or a
6250 config, but must be the same for all configs applying to a given target since
6251 a target can only have one precompiled header.
6252
6253 If you use both C and C++ sources, the precompiled header and source file
6254 will be compiled once per language. You will want to make sure to wrap C++
6255 includes in __cplusplus #ifdefs so the file will compile in C mode.
6256```
6257
6258#### **GCC precompiled headers**
6259
6260```
6261 When using GCC-style precompiled headers, "precompiled_source" contains the
6262 path of a .h file that is precompiled and then included by all source files
6263 in targets that set "precompiled_source".
6264
6265 The value of "precompiled_header" is not used with GCC-style precompiled
6266 headers.
6267```
6268
6269#### **MSVC precompiled headers**
6270
6271```
6272 When using MSVC-style precompiled headers, the "precompiled_header" value is
6273 a string corresponding to the header. This is NOT a path to a file that GN
6274 recognises, but rather the exact string that appears in quotes after
6275 an #include line in source code. The compiler will match this string against
6276 includes or forced includes (/FI).
6277
6278 MSVC also requires a source file to compile the header with. This must be
6279 specified by the "precompiled_source" value. In contrast to the header value,
6280 this IS a GN-style file name, and tells GN which source file to compile to
6281 make the .pch file used for subsequent compiles.
6282
6283 For example, if the toolchain specifies MSVC headers:
6284
6285 toolchain("vc_x64") {
6286 ...
6287 tool("cxx") {
6288 precompiled_header_type = "msvc"
6289 ...
6290
6291 You might make a config like this:
6292
6293 config("use_precompiled_headers") {
6294 precompiled_header = "build/precompile.h"
6295 precompiled_source = "//build/precompile.cc"
6296
6297 # Either your source files should #include "build/precompile.h"
6298 # first, or you can do this to force-include the header.
6299 cflags = [ "/FI$precompiled_header" ]
6300 }
6301
6302 And then define a target that uses the config:
6303
6304 executable("doom_melon") {
6305 configs += [ ":use_precompiled_headers" ]
6306 ...
6307```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006308### **precompiled_header_type**: [string] "gcc" or "msvc".
Brett Wilson796ed472018-07-16 15:11:09 -07006309
6310```
6311 See "gn help precompiled_header".
6312```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006313### **precompiled_source**: [file name] Source file to precompile.
Brett Wilson796ed472018-07-16 15:11:09 -07006314
6315```
6316 The source file that goes along with the precompiled_header when using
6317 "msvc"-style precompiled headers. It will be implicitly added to the sources
6318 of the target. See "gn help precompiled_header".
6319```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006320### **product_type**: Product type for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006321
6322```
6323 Correspond to the type of the product of a create_bundle target. Only
6324 meaningful to Xcode (used as part of the Xcode project generation).
6325
6326 When generating Xcode project files, only create_bundle target with a
6327 non-empty product_type will have a corresponding target in Xcode project.
6328```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006329### **public**: Declare public header files for a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006330
6331```
6332 A list of files that other targets can include. These permissions are checked
6333 via the "check" command (see "gn help check").
6334
6335 If no public files are declared, other targets (assuming they have visibility
6336 to depend on this target) can include any file in the sources list. If this
6337 variable is defined on a target, dependent targets may only include files on
6338 this whitelist unless that target is marked as a friend (see "gn help
6339 friend").
6340
6341 Header file permissions are also subject to visibility. A target must be
6342 visible to another target to include any files from it at all and the public
6343 headers indicate which subset of those files are permitted. See "gn help
6344 visibility" for more.
6345
6346 Public files are inherited through the dependency tree. So if there is a
6347 dependency A -> B -> C, then A can include C's public headers. However, the
6348 same is NOT true of visibility, so unless A is in C's visibility list, the
6349 include will be rejected.
6350
6351 GN only knows about files declared in the "sources" and "public" sections of
6352 targets. If a file is included that is not known to the build, it will be
6353 allowed.
6354
6355 It is common for test targets to need to include private headers for their
6356 associated code. In this case, list the test target in the "friend" list of
6357 the target that owns the private header to allow the inclusion. See
6358 "gn help friend" for more.
Julie Hockette2a29402018-07-31 10:11:42 -07006359
6360 When a binary target has no explicit or implicit public headers (a "public"
6361 list is defined but is empty), GN assumes that the target can not propagate
6362 any compile-time dependencies up the dependency tree. In this case, the build
6363 can be parallelized more efficiently.
6364 Say there are dependencies:
6365 A (shared library) -> B (shared library) -> C (action).
6366 Normally C must complete before any source files in A can compile (because
6367 there might be generated includes). But when B explicitly declares no public
6368 headers, C can execute in parallel with A's compile steps. C must still be
6369 complete before any dependents link.
Brett Wilson796ed472018-07-16 15:11:09 -07006370```
6371
6372#### **Examples**
6373
6374```
6375 These exact files are public:
6376 public = [ "foo.h", "bar.h" ]
6377
6378 No files are public (no targets may include headers from this one):
Julie Hockette2a29402018-07-31 10:11:42 -07006379 # This allows starting compilation in dependent targets earlier.
Brett Wilson796ed472018-07-16 15:11:09 -07006380 public = []
6381```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006382### **public_configs**: Configs to be applied on dependents.
Brett Wilson796ed472018-07-16 15:11:09 -07006383
6384```
6385 A list of config labels.
6386
6387 Targets directly depending on this one will have the configs listed in this
6388 variable added to them. These configs will also apply to the current target.
Julie Hockette2a29402018-07-31 10:11:42 -07006389 Generally, public configs are used to apply defines and include directories
6390 necessary to compile this target's header files.
Brett Wilson796ed472018-07-16 15:11:09 -07006391
Julie Hockette2a29402018-07-31 10:11:42 -07006392 See also "gn help all_dependent_configs".
6393```
6394
6395#### **Propagation of public configs**
6396
6397```
6398 Public configs are applied to all targets that depend directly on this one.
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006399 These dependent targets can further push this target's public configs
Julie Hockette2a29402018-07-31 10:11:42 -07006400 higher in the dependency tree by depending on it via public_deps (see "gn
6401 help public_deps").
6402
6403 static_library("toplevel") {
6404 # This target will get "my_config" applied to it. However, since this
6405 # target uses "deps" and not "public_deps", targets that depend on this
6406 # one won't get it.
6407 deps = [ ":intermediate" ]
6408 }
6409
6410 static_library("intermediate") {
6411 # Depending on "lower" in any way will apply "my_config" to this target.
6412 # Additionall, since this target depends on "lower" via public_deps,
6413 # targets that depend on this one will also get "my_config".
6414 public_deps = [ ":lower" ]
6415 }
6416
6417 static_library("lower") {
6418 # This will get applied to all targets that depend on this one.
6419 public_configs = [ ":my_config" ]
6420 }
6421
6422 Public config propagation happens in a second phase once a target and all of
6423 its dependencies have been resolved. Therefore, a target will not see these
Brett Wilson796ed472018-07-16 15:11:09 -07006424 force-added configs in their "configs" variable while the script is running,
6425 and they can not be removed. As a result, this capability should generally
Julie Hockette2a29402018-07-31 10:11:42 -07006426 only be used to add defines and include directories rather than setting
6427 complicated flags that some targets may not want.
Brett Wilson796ed472018-07-16 15:11:09 -07006428
Julie Hockette2a29402018-07-31 10:11:42 -07006429 Public configs may or may not be propagated across toolchain boundaries
6430 depending on the value of the propagates_configs flag (see "gn help
6431 toolchain") on the toolchain of the target declaring the public_config.
6432```
6433
6434#### **Avoiding applying public configs to this target**
6435
6436```
6437 If you want the config to apply to targets that depend on this one, but NOT
6438 this one, define an extra layer of indirection using a group:
6439
6440 # External targets depend on this group.
6441 group("my_target") {
6442 # Config to apply to all targets that depend on this one.
6443 public_configs = [ ":external_settings" ]
6444 deps = [ ":internal_target" ]
6445 }
6446
6447 # Internal target to actually compile the sources.
6448 static_library("internal_target") {
6449 # Force all external targets to depend on the group instead of directly
6450 # on this so the "external_settings" config will get applied.
6451 visibility = [ ":my_target" ]
6452 ...
6453 }
Brett Wilson796ed472018-07-16 15:11:09 -07006454```
6455
6456#### **Ordering of flags and values**
6457
6458```
6459 1. Those set on the current target (not in a config).
6460 2. Those set on the "configs" on the target in order that the
6461 configs appear in the list.
6462 3. Those set on the "all_dependent_configs" on the target in order
6463 that the configs appear in the list.
6464 4. Those set on the "public_configs" on the target in order that
6465 those configs appear in the list.
6466 5. all_dependent_configs pulled from dependencies, in the order of
6467 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006468 more than once, only the first occurrence will be used.
Brett Wilson796ed472018-07-16 15:11:09 -07006469 6. public_configs pulled from dependencies, in the order of the
6470 "deps" list. If a dependency is public, they will be applied
6471 recursively.
6472```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006473### **public_deps**: Declare public dependencies.
Brett Wilson796ed472018-07-16 15:11:09 -07006474
6475```
6476 Public dependencies are like private dependencies (see "gn help deps") but
6477 additionally express that the current target exposes the listed deps as part
6478 of its public API.
6479
6480 This has several ramifications:
6481
6482 - public_configs that are part of the dependency are forwarded to direct
6483 dependents.
6484
6485 - Public headers in the dependency are usable by dependents (includes do
6486 not require a direct dependency or visibility).
6487
6488 - If the current target is a shared library, other shared libraries that it
6489 publicly depends on (directly or indirectly) are propagated up the
6490 dependency tree to dependents for linking.
Julie Hockette2a29402018-07-31 10:11:42 -07006491
6492 See also "gn help public_configs".
Brett Wilson796ed472018-07-16 15:11:09 -07006493```
6494
6495#### **Discussion**
6496
6497```
6498 Say you have three targets: A -> B -> C. C's visibility may allow B to depend
6499 on it but not A. Normally, this would prevent A from including any headers
6500 from C, and C's public_configs would apply only to B.
6501
6502 If B lists C in its public_deps instead of regular deps, A will now inherit
6503 C's public_configs and the ability to include C's public headers.
6504
6505 Generally if you are writing a target B and you include C's headers as part
6506 of B's public headers, or targets depending on B should consider B and C to
6507 be part of a unit, you should use public_deps instead of deps.
6508```
6509
6510#### **Example**
6511
6512```
6513 # This target can include files from "c" but not from
6514 # "super_secret_implementation_details".
6515 executable("a") {
6516 deps = [ ":b" ]
6517 }
6518
6519 shared_library("b") {
6520 deps = [ ":super_secret_implementation_details" ]
6521 public_deps = [ ":c" ]
6522 }
6523```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006524### **rebase**: Rebase collected metadata as files.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006525
6526```
6527 A boolean that triggers a rebase of collected metadata strings based on their
6528 declared file. Defaults to false.
6529
6530 Metadata generally declares files as strings relative to the local build file.
6531 However, this data is often used in other contexts, and so setting this flag
6532 will force the metadata collection to be rebased according to the local build
6533 file's location and thus allow the filename to be used anywhere.
6534
6535 Setting this flag will raise an error if any target's specified metadata is
6536 not a string value.
6537
6538 See also "gn help generated_file".
6539```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006540### **response_file_contents**: Contents of a response file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006541
6542```
6543 Sometimes the arguments passed to a script can be too long for the system's
6544 command-line capabilities. This is especially the case on Windows where the
6545 maximum command-line length is less than 8K. A response file allows you to
6546 pass an unlimited amount of data to a script in a temporary file for an
6547 action or action_foreach target.
6548
6549 If the response_file_contents variable is defined and non-empty, the list
6550 will be treated as script args (including possibly substitution patterns)
6551 that will be written to a temporary file at build time. The name of the
6552 temporary file will be substituted for "{{response_file_name}}" in the script
6553 args.
6554
6555 The response file contents will always be quoted and escaped according to
6556 Unix shell rules. To parse the response file, the Python script should use
6557 "shlex.split(file_contents)".
6558```
6559
6560#### **Example**
6561
6562```
6563 action("process_lots_of_files") {
6564 script = "process.py",
6565 inputs = [ ... huge list of files ... ]
6566
6567 # Write all the inputs to a response file for the script. Also,
6568 # make the paths relative to the script working directory.
6569 response_file_contents = rebase_path(inputs, root_build_dir)
6570
6571 # The script expects the name of the response file in --file-list.
6572 args = [
6573 "--enable-foo",
6574 "--file-list={{response_file_name}}",
6575 ]
6576 }
6577```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006578### **script**: Script file for actions.
Brett Wilson796ed472018-07-16 15:11:09 -07006579
6580```
6581 An absolute or buildfile-relative file name of a Python script to run for a
6582 action and action_foreach targets (see "gn help action" and "gn help
6583 action_foreach").
6584```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006585### **sources**: Source files for a target
Brett Wilson796ed472018-07-16 15:11:09 -07006586
6587```
6588 A list of files. Non-absolute paths will be resolved relative to the current
6589 build file.
6590```
6591
6592#### **Sources for binary targets**
6593
6594```
6595 For binary targets (source sets, executables, and libraries), the known file
6596 types will be compiled with the associated tools. Unknown file types and
6597 headers will be skipped. However, you should still list all C/C+ header files
6598 so GN knows about the existence of those files for the purposes of include
6599 checking.
6600
6601 As a special case, a file ending in ".def" will be treated as a Windows
6602 module definition file. It will be appended to the link line with a
6603 preceding "/DEF:" string. There must be at most one .def file in a target
6604 and they do not cross dependency boundaries (so specifying a .def file in a
6605 static library or source set will have no effect on the executable or shared
6606 library they're linked into).
Julie Hockettce1fa072019-05-07 17:44:37 -07006607
6608 For Rust targets that do not specify a crate_root, then the crate_root will
6609 look for a lib.rs file (or main.rs for executable) or a single file in
6610 sources, if sources contains only one file.
Brett Wilson796ed472018-07-16 15:11:09 -07006611```
6612
6613#### **Sources for non-binary targets**
6614
6615```
6616 action_foreach
6617 The sources are the set of files that the script will be executed over. The
6618 script will run once per file.
6619
6620 action
6621 The sources will be treated the same as inputs. See "gn help inputs" for
6622 more information and usage advice.
6623
6624 copy
6625 The source are the source files to copy.
6626```
Sylvain Defresne89e64252020-08-07 13:01:06 +02006627### **swiftflags**: Flags passed to the swift compiler.
6628
6629```
6630 A list of strings.
6631
6632 "swiftflags" are passed to any invocation of a tool that takes an .swift
6633 file as input.
6634```
6635
6636#### **Ordering of flags and values**
6637
6638```
6639 1. Those set on the current target (not in a config).
6640 2. Those set on the "configs" on the target in order that the
6641 configs appear in the list.
6642 3. Those set on the "all_dependent_configs" on the target in order
6643 that the configs appear in the list.
6644 4. Those set on the "public_configs" on the target in order that
6645 those configs appear in the list.
6646 5. all_dependent_configs pulled from dependencies, in the order of
6647 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006648 more than once, only the first occurrence will be used.
Sylvain Defresne89e64252020-08-07 13:01:06 +02006649 6. public_configs pulled from dependencies, in the order of the
6650 "deps" list. If a dependency is public, they will be applied
6651 recursively.
6652```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006653### **testonly**: Declares a target must only be used for testing.
Brett Wilson796ed472018-07-16 15:11:09 -07006654
6655```
6656 Boolean. Defaults to false.
6657
6658 When a target is marked "testonly = true", it must only be depended on by
6659 other test-only targets. Otherwise, GN will issue an error that the
6660 depenedency is not allowed.
6661
6662 This feature is intended to prevent accidentally shipping test code in a
6663 final product.
6664```
6665
6666#### **Example**
6667
6668```
6669 source_set("test_support") {
6670 testonly = true
6671 ...
6672 }
6673```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006674### **visibility**: A list of labels that can depend on a target.
Brett Wilson796ed472018-07-16 15:11:09 -07006675
6676```
6677 A list of labels and label patterns that define which targets can depend on
6678 the current one. These permissions are checked via the "check" command (see
6679 "gn help check").
6680
6681 If visibility is not defined, it defaults to public ("*").
6682
6683 If visibility is defined, only the targets with labels that match it can
6684 depend on the current target. The empty list means no targets can depend on
6685 the current target.
6686
6687 Tip: Often you will want the same visibility for all targets in a BUILD file.
6688 In this case you can just put the definition at the top, outside of any
6689 target, and the targets will inherit that scope and see the definition.
6690```
6691
6692#### **Patterns**
6693
6694```
6695 See "gn help label_pattern" for more details on what types of patterns are
6696 supported. If a toolchain is specified, only targets in that toolchain will
6697 be matched. If a toolchain is not specified on a pattern, targets in all
6698 toolchains will be matched.
6699```
6700
6701#### **Examples**
6702
6703```
6704 Only targets in the current buildfile ("private"):
6705 visibility = [ ":*" ]
6706
6707 No targets (used for targets that should be leaf nodes):
6708 visibility = []
6709
6710 Any target ("public", the default):
6711 visibility = [ "*" ]
6712
6713 All targets in the current directory and any subdirectory:
6714 visibility = [ "./*" ]
6715
6716 Any target in "//bar/BUILD.gn":
6717 visibility = [ "//bar:*" ]
6718
6719 Any target in "//bar/" or any subdirectory thereof:
6720 visibility = [ "//bar/*" ]
6721
6722 Just these specific targets:
6723 visibility = [ ":mything", "//foo:something_else" ]
6724
6725 Any target in the current directory and any subdirectory thereof, plus
Petr Hosek7a6231e2022-10-22 23:14:18 +00006726 any targets in "//bar/" and any subdirectory thereof.
Brett Wilson796ed472018-07-16 15:11:09 -07006727 visibility = [ "./*", "//bar/*" ]
6728```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006729### **walk_keys**: Key(s) for managing the metadata collection walk.
Nate Fischer8ed01d32019-01-08 17:32:01 -08006730
6731```
Julie Hockett152c5142019-07-12 09:53:43 -06006732 Defaults to [""].
Nate Fischer8ed01d32019-01-08 17:32:01 -08006733
6734 These keys are used to control the next step in a collection walk, acting as
6735 barriers. If a specified key is defined in a target's metadata, the walk will
6736 use the targets listed in that value to determine which targets are walked.
6737
Julie Hockett152c5142019-07-12 09:53:43 -06006738 If no walk_keys are specified for a generated_file target (i.e. "[""]"), the
Nate Fischer8ed01d32019-01-08 17:32:01 -08006739 walk will touch all deps and data_deps of the specified target recursively.
6740
6741 See "gn help generated_file".
6742```
Robert Sesekd0a6f072020-05-15 11:21:22 -04006743### **weak_frameworks**: [name list] Name of frameworks that must be weak linked.
6744
6745```
6746 A list of framework names.
6747
6748 The frameworks named in that list will be weak linked with any dynamic link
6749 type target. Weak linking instructs the dynamic loader to attempt to load
6750 the framework, but if it is not able to do so, it leaves any imported symbols
6751 unresolved. This is typically used when a framework is present in a new
6752 version of an SDK but not on older versions of the OS that the software runs
6753 on.
6754```
6755
6756#### **Ordering of flags and values**
6757
6758```
6759 1. Those set on the current target (not in a config).
6760 2. Those set on the "configs" on the target in order that the
6761 configs appear in the list.
6762 3. Those set on the "all_dependent_configs" on the target in order
6763 that the configs appear in the list.
6764 4. Those set on the "public_configs" on the target in order that
6765 those configs appear in the list.
6766 5. all_dependent_configs pulled from dependencies, in the order of
6767 the "deps" list. This is done recursively. If a config appears
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01006768 more than once, only the first occurrence will be used.
Robert Sesekd0a6f072020-05-15 11:21:22 -04006769 6. public_configs pulled from dependencies, in the order of the
6770 "deps" list. If a dependency is public, they will be applied
6771 recursively.
6772```
6773
6774#### **Example**
6775
6776```
6777 weak_frameworks = [ "OnlyOnNewerOSes.framework" ]
6778```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006779### **write_runtime_deps**: Writes the target's runtime_deps to the given path.
Brett Wilson796ed472018-07-16 15:11:09 -07006780
6781```
6782 Does not synchronously write the file, but rather schedules it to be written
6783 at the end of generation.
6784
6785 If the file exists and the contents are identical to that being written, the
6786 file will not be updated. This will prevent unnecessary rebuilds of targets
6787 that depend on this file.
6788
6789 Path must be within the output directory.
6790
6791 See "gn help runtime_deps" for how the runtime dependencies are computed.
6792
6793 The format of this file will list one file per line with no escaping. The
6794 files will be relative to the root_build_dir. The first line of the file will
6795 be the main output file of the target itself. The file contents will be the
6796 same as requesting the runtime deps be written on the command line (see "gn
6797 help --runtime-deps-list-file").
6798```
Harley Li0a9affb2020-06-03 10:38:42 -04006799### **xcasset_compiler_flags**: Flags passed to xcassets compiler.
6800
6801```
6802 A list of strings.
6803
6804 Valid for create_bundle target. Those flags are directly passed to
6805 xcassets compiler, corresponding to {{xcasset_compiler_flags}} substitution
6806 in compile_xcassets tool.
6807```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006808### **xcode_extra_attributes**: [scope] Extra attributes for Xcode projects.
Brett Wilson796ed472018-07-16 15:11:09 -07006809
6810```
6811 The value defined in this scope will be copied to the EXTRA_ATTRIBUTES
6812 property of the generated Xcode project. They are only meaningful when
6813 generating with --ide=xcode.
6814
6815 See "gn help create_bundle" for more information.
6816```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08006817### **xcode_test_application_name**: Name for Xcode test target.
Brett Wilson796ed472018-07-16 15:11:09 -07006818
6819```
6820 Each unit and ui test target must have a test application target, and this
6821 value is used to specify the relationship. Only meaningful to Xcode (used as
6822 part of the Xcode project generation).
6823
6824 See "gn help create_bundle" for more information.
6825```
6826
Julie Hockette2a29402018-07-31 10:11:42 -07006827#### **Example**
Brett Wilson796ed472018-07-16 15:11:09 -07006828
6829```
6830 create_bundle("chrome_xctest") {
6831 test_application_name = "chrome"
6832 ...
6833 }
6834```
6835## Other help topics
6836
6837### **Build Arguments Overview**
6838
6839```
6840 Build arguments are variables passed in from outside of the build that build
6841 files can query to determine how the build works.
6842```
6843
6844#### **How build arguments are set**
6845
6846```
6847 First, system default arguments are set based on the current system. The
6848 built-in arguments are:
6849 - host_cpu
6850 - host_os
6851 - current_cpu
6852 - current_os
6853 - target_cpu
6854 - target_os
6855
6856 Next, project-specific overrides are applied. These are specified inside
6857 the default_args variable of //.gn. See "gn help dotfile" for more.
6858
6859 If specified, arguments from the --args command line flag are used. If that
6860 flag is not specified, args from previous builds in the build directory will
6861 be used (this is in the file args.gn in the build directory).
6862
6863 Last, for targets being compiled with a non-default toolchain, the toolchain
6864 overrides are applied. These are specified in the toolchain_args section of a
6865 toolchain definition. The use-case for this is that a toolchain may be
6866 building code for a different platform, and that it may want to always
6867 specify Posix, for example. See "gn help toolchain" for more.
6868
6869 If you specify an override for a build argument that never appears in a
6870 "declare_args" call, a nonfatal error will be displayed.
6871```
6872
6873#### **Examples**
6874
6875```
6876 gn args out/FooBar
6877 Create the directory out/FooBar and open an editor. You would type
6878 something like this into that file:
6879 enable_doom_melon=false
6880 os="android"
6881
6882 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
6883 This will overwrite the build directory with the given arguments. (Note
6884 that the quotes inside the args command will usually need to be escaped
6885 for your shell to pass through strings values.)
6886```
6887
6888#### **How build arguments are used**
6889
6890```
6891 If you want to use an argument, you use declare_args() and specify default
6892 values. These default values will apply if none of the steps listed in the
6893 "How build arguments are set" section above apply to the given argument, but
6894 the defaults will not override any of these.
6895
6896 Often, the root build config file will declare global arguments that will be
6897 passed to all buildfiles. Individual build files can also specify arguments
6898 that apply only to those files. It is also useful to specify build args in an
6899 "import"-ed file if you want such arguments to apply to multiple buildfiles.
6900```
6901### **.gn file**
6902
6903```
6904 When gn starts, it will search the current directory and parent directories
6905 for a file called ".gn". This indicates the source root. You can override
6906 this detection by using the --root command-line argument
6907
6908 The .gn file in the source root will be executed. The syntax is the same as a
6909 buildfile, but with very limited build setup-specific meaning.
6910
6911 If you specify --root, by default GN will look for the file .gn in that
6912 directory. If you want to specify a different file, you can additionally pass
6913 --dotfile:
6914
6915 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
6916```
6917
6918#### **Variables**
6919
6920```
6921 arg_file_template [optional]
6922 Path to a file containing the text that should be used as the default
6923 args.gn content when you run `gn args`.
6924
6925 buildconfig [required]
6926 Path to the build config file. This file will be used to set up the
6927 build file execution environment for each toolchain.
6928
6929 check_targets [optional]
6930 A list of labels and label patterns that should be checked when running
Erik Staab5d109d72020-06-29 22:53:23 -07006931 "gn check" or "gn gen --check". If neither check_targets or
6932 no_check_targets (see below) is specified, all targets will be checked.
6933 It is an error to specify both check_targets and no_check_targets. If it
6934 is the empty list, no targets will be checked. To bypass this list,
6935 request an explicit check of targets, like "//*".
6936
6937 The format of this list is identical to that of "visibility" so see "gn
6938 help visibility" for examples.
6939
6940 no_check_targets [optional]
6941 A list of labels and label patterns that should *not* be checked when
6942 running "gn check" or "gn gen --check". All other targets will be checked.
6943 If neither check_targets (see above) or no_check_targets is specified, all
6944 targets will be checked. It is an error to specify both check_targets and
6945 no_check_targets.
Brett Wilson796ed472018-07-16 15:11:09 -07006946
6947 The format of this list is identical to that of "visibility" so see "gn
6948 help visibility" for examples.
6949
James Robinson49f59032020-01-08 14:05:51 -08006950 check_system_includes [optional]
6951 Boolean to control whether system style includes are checked by default
6952 when running "gn check" or "gn gen --check". System style includes are
6953 includes that use angle brackets <> instead of double quotes "". If this
6954 setting is omitted or set to false, these includes will be ignored by
6955 default. They can be checked explicitly by running
6956 "gn check --check-system" or "gn gen --check=system"
6957
Brett Wilson796ed472018-07-16 15:11:09 -07006958 exec_script_whitelist [optional]
6959 A list of .gn/.gni files (not labels) that have permission to call the
6960 exec_script function. If this list is defined, calls to exec_script will
6961 be checked against this list and GN will fail if the current file isn't
6962 in the list.
6963
6964 This is to allow the use of exec_script to be restricted since is easy to
6965 use inappropriately. Wildcards are not supported. Files in the
6966 secondary_source tree (if defined) should be referenced by ignoring the
6967 secondary tree and naming them as if they are in the main tree.
6968
6969 If unspecified, the ability to call exec_script is unrestricted.
6970
6971 Example:
6972 exec_script_whitelist = [
6973 "//base/BUILD.gn",
6974 "//build/my_config.gni",
6975 ]
6976
Brett Wilson1da84bb2022-09-14 15:35:29 -07006977 export_compile_commands [optional]
6978 A list of label patterns for which to generate a Clang compilation
6979 database (see "gn help label_pattern" for the string format).
6980
6981 When specified, GN will generate a compile_commands.json file in the root
6982 of the build directory containing information on how to compile each
6983 source file reachable from any label matching any pattern in the list.
6984 This is used for Clang-based tooling and some editor integration. See
6985 https://clang.llvm.org/docs/JSONCompilationDatabase.html
6986
6987 The switch --add-export-compile-commands to "gn gen" (see "gn help gen")
6988 appends to this value which provides a per-user way to customize it.
6989
6990 The deprecated switch --export-compile-commands to "gn gen" (see "gn help
6991 gen") adds to the export target list using a different format.
6992
6993 Example:
6994 export_compile_commands = [
6995 "//base/*",
6996 "//tools:doom_melon",
6997 ]
6998
Brett Wilson796ed472018-07-16 15:11:09 -07006999 root [optional]
7000 Label of the root build target. The GN build will start by loading the
7001 build file containing this target name. This defaults to "//:" which will
Harley Li0a9affb2020-06-03 10:38:42 -04007002 cause the file //BUILD.gn to be loaded. Note that build_file_extension
7003 applies to the default case as well.
Brett Wilson796ed472018-07-16 15:11:09 -07007004
Nico Weberad9eab22020-11-15 22:20:28 -05007005 The command-line switch --root-target will override this value (see "gn
7006 help --root-target").
7007
Brett Wilson796ed472018-07-16 15:11:09 -07007008 script_executable [optional]
Brett Wilsond0f8bc92022-04-22 14:01:40 -07007009 By default, GN runs the scripts used in action targets and exec_script
7010 calls using the Python interpreter found in PATH. This value specifies the
7011 Python executable or other interpreter to use instead.
James Robinson61377e32020-02-13 15:20:07 -08007012
Brett Wilsond0f8bc92022-04-22 14:01:40 -07007013 If set to the empty string, the scripts will be executed directly.
7014
7015 The command-line switch --script-executable will override this value (see
7016 "gn help --script-executable")
Brett Wilson796ed472018-07-16 15:11:09 -07007017
7018 secondary_source [optional]
7019 Label of an alternate directory tree to find input files. When searching
7020 for a BUILD.gn file (or the build config file discussed above), the file
7021 will first be looked for in the source root. If it's not found, the
7022 secondary source root will be checked (which would contain a parallel
7023 directory hierarchy).
7024
7025 This behavior is intended to be used when BUILD.gn files can't be checked
7026 in to certain source directories for whatever reason.
7027
7028 The secondary source root must be inside the main source tree.
7029
7030 default_args [optional]
7031 Scope containing the default overrides for declared arguments. These
7032 overrides take precedence over the default values specified in the
Ricardo Ribalda09c9e5e2021-01-25 19:07:20 +01007033 declare_args() block, but can be overridden using --args or the
Brett Wilson796ed472018-07-16 15:11:09 -07007034 args.gn file.
7035
7036 This is intended to be used when subprojects declare arguments with
7037 default values that need to be changed for whatever reason.
Harley Li0a9affb2020-06-03 10:38:42 -04007038
7039 build_file_extension [optional]
7040 If set to a non-empty string, this is added to the name of all build files
7041 to load.
7042 GN will look for build files named "BUILD.$build_file_extension.gn".
7043 This is intended to be used during migrations or other situations where
7044 there are two independent GN builds in the same directories.
Petr Hosek317fdc12020-05-28 10:23:42 -07007045
7046 ninja_required_version [optional]
7047 When set specifies the minimum required version of Ninja. The default
7048 required version is 1.7.2. Specifying a higher version might enable the
7049 use of some of newer features that can make the build more efficient.
Brett Wilson796ed472018-07-16 15:11:09 -07007050```
7051
7052#### **Example .gn file contents**
7053
7054```
7055 buildconfig = "//build/config/BUILDCONFIG.gn"
7056
7057 check_targets = [
7058 "//doom_melon/*", # Check everything in this subtree.
7059 "//tools:mind_controlling_ant", # Check this specific target.
7060 ]
7061
7062 root = "//:root"
7063
7064 secondary_source = "//build/config/temporary_buildfiles/"
7065
7066 default_args = {
7067 # Default to release builds for this project.
7068 is_debug = false
7069 is_component_build = false
7070 }
7071```
7072### **Build graph and execution overview**
7073
7074#### **Overall build flow**
7075
7076```
7077 1. Look for ".gn" file (see "gn help dotfile") in the current directory and
7078 walk up the directory tree until one is found. Set this directory to be
7079 the "source root" and interpret this file to find the name of the build
7080 config file.
7081
7082 2. Execute the build config file identified by .gn to set up the global
7083 variables and default toolchain name. Any arguments, variables, defaults,
7084 etc. set up in this file will be visible to all files in the build.
7085
7086 3. Load the //BUILD.gn (in the source root directory).
7087
7088 4. Recursively evaluate rules and load BUILD.gn in other directories as
7089 necessary to resolve dependencies. If a BUILD file isn't found in the
7090 specified location, GN will look in the corresponding location inside
7091 the secondary_source defined in the dotfile (see "gn help dotfile").
7092
7093 5. When a target's dependencies are resolved, write out the `.ninja`
7094 file to disk.
7095
7096 6. When all targets are resolved, write out the root build.ninja file.
Harley Li0a9affb2020-06-03 10:38:42 -04007097
7098 Note that the BUILD.gn file name may be modulated by .gn arguments such as
7099 build_file_extension.
Brett Wilson796ed472018-07-16 15:11:09 -07007100```
7101
7102#### **Executing target definitions and templates**
7103
7104```
7105 Build files are loaded in parallel. This means it is impossible to
7106 interrogate a target from GN code for any information not derivable from its
7107 label (see "gn help label"). The exception is the get_target_outputs()
7108 function which requires the target being interrogated to have been defined
7109 previously in the same file.
7110
7111 Targets are declared by their type and given a name:
7112
7113 static_library("my_static_library") {
7114 ... target parameter definitions ...
7115 }
7116
7117 There is also a generic "target" function for programmatically defined types
7118 (see "gn help target"). You can define new types using templates (see "gn
7119 help template"). A template defines some custom code that expands to one or
7120 more other targets.
7121
7122 Before executing the code inside the target's { }, the target defaults are
7123 applied (see "gn help set_defaults"). It will inject implicit variable
7124 definitions that can be overridden by the target code as necessary. Typically
7125 this mechanism is used to inject a default set of configs that define the
7126 global compiler and linker flags.
7127```
7128
7129#### **Which targets are built**
7130
7131```
7132 All targets encountered in the default toolchain (see "gn help toolchain")
7133 will have build rules generated for them, even if no other targets reference
7134 them. Their dependencies must resolve and they will be added to the implicit
7135 "all" rule (see "gn help ninja_rules").
7136
7137 Targets in non-default toolchains will only be generated when they are
7138 required (directly or transitively) to build a target in the default
7139 toolchain.
7140
Tyler Mandry4a648092022-02-15 19:47:09 +00007141 Some targets might be associated but without a formal build dependency (for
7142 example, related tools or optional variants). A target that is marked as
7143 "generated" can propagate its generated state to an associated target using
7144 "gen_deps". This will make the referenced dependency have Ninja rules
7145 generated in the same cases the source target has but without a build-time
7146 dependency and even in non-default toolchains.
7147
Brett Wilson796ed472018-07-16 15:11:09 -07007148 See also "gn help ninja_rules".
7149```
7150
7151#### **Dependencies**
7152
7153```
7154 The only difference between "public_deps" and "deps" except for pushing
7155 configs around the build tree and allowing includes for the purposes of "gn
7156 check".
7157
7158 A target's "data_deps" are guaranteed to be built whenever the target is
7159 built, but the ordering is not defined. The meaning of this is dependencies
7160 required at runtime. Currently data deps will be complete before the target
7161 is linked, but this is not semantically guaranteed and this is undesirable
7162 from a build performance perspective. Since we hope to change this in the
7163 future, do not rely on this behavior.
7164```
7165### **Language and grammar for GN build files**
7166
7167#### **Tokens**
7168
7169```
7170 GN build files are read as sequences of tokens. While splitting the file
7171 into tokens, the next token is the longest sequence of characters that form a
7172 valid token.
7173```
7174
7175#### **White space and comments**
7176
7177```
7178 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
7179 carriage returns (U+000D), and newlines (U+000A).
7180
7181 Comments start at the character "#" and stop at the next newline.
7182
7183 White space and comments are ignored except that they may separate tokens
7184 that would otherwise combine into a single token.
7185```
7186
7187#### **Identifiers**
7188
7189```
7190 Identifiers name variables and functions.
7191
7192 identifier = letter { letter | digit } .
7193 letter = "A" ... "Z" | "a" ... "z" | "_" .
7194 digit = "0" ... "9" .
7195```
7196
7197#### **Keywords**
7198
7199```
7200 The following keywords are reserved and may not be used as identifiers:
7201
7202 else false if true
7203```
7204
7205#### **Integer literals**
7206
7207```
7208 An integer literal represents a decimal integer value.
7209
7210 integer = [ "-" ] digit { digit } .
7211
7212 Leading zeros and negative zero are disallowed.
7213```
7214
7215#### **String literals**
7216
7217```
7218 A string literal represents a string value consisting of the quoted
7219 characters with possible escape sequences and variable expansions.
7220
7221 string = `"` { char | escape | expansion } `"` .
7222 escape = `\` ( "$" | `"` | char ) .
Nico Weberad9eab22020-11-15 22:20:28 -05007223 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" .
Brett Wilson796ed472018-07-16 15:11:09 -07007224 Hex = "0x" [0-9A-Fa-f][0-9A-Fa-f]
7225 expansion = "$" ( identifier | BracketExpansion | Hex ) .
Nico Weberad9eab22020-11-15 22:20:28 -05007226 char = /* any character except "$", `"`, or newline */ .
Brett Wilson796ed472018-07-16 15:11:09 -07007227
7228 After a backslash, certain sequences represent special characters:
7229
7230 \" U+0022 quotation mark
7231 \$ U+0024 dollar sign
7232 \\ U+005C backslash
7233
7234 All other backslashes represent themselves.
7235
7236 To insert an arbitrary byte value, use $0xFF. For example, to insert a
7237 newline character: "Line one$0x0ALine two".
7238
7239 An expansion will evaluate the variable following the '$' and insert a
7240 stringified version of it into the result. For example, to concat two path
7241 components with a slash separating them:
7242 "$var_one/$var_two"
7243 Use the "${var_one}" format to be explicitly deliniate the variable for
7244 otherwise-ambiguous cases.
7245```
7246
7247#### **Punctuation**
7248
7249```
7250 The following character sequences represent punctuation:
7251
7252 + += == != ( )
7253 - -= < <= [ ]
7254 ! = > >= { }
7255 && || . ,
7256```
7257
7258#### **Grammar**
7259
7260```
7261 The input tokens form a syntax tree following a context-free grammar:
7262
7263 File = StatementList .
7264
7265 Statement = Assignment | Call | Condition .
7266 LValue = identifier | ArrayAccess | ScopeAccess .
7267 Assignment = LValue AssignOp Expr .
7268 Call = identifier "(" [ ExprList ] ")" [ Block ] .
7269 Condition = "if" "(" Expr ")" Block
7270 [ "else" ( Condition | Block ) ] .
7271 Block = "{" StatementList "}" .
7272 StatementList = { Statement } .
7273
7274 ArrayAccess = identifier "[" Expr "]" .
7275 ScopeAccess = identifier "." identifier .
7276 Expr = UnaryExpr | Expr BinaryOp Expr .
7277 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
7278 PrimaryExpr = identifier | integer | string | Call
7279 | ArrayAccess | ScopeAccess | Block
7280 | "(" Expr ")"
7281 | "[" [ ExprList [ "," ] ] "]" .
7282 ExprList = Expr { "," Expr } .
7283
7284 AssignOp = "=" | "+=" | "-=" .
7285 UnaryOp = "!" .
7286 BinaryOp = "+" | "-" // highest priority
7287 | "<" | "<=" | ">" | ">="
7288 | "==" | "!="
7289 | "&&"
7290 | "||" . // lowest priority
7291
7292 All binary operators are left-associative.
7293```
7294
7295#### **Types**
7296
7297```
7298 The GN language is dynamically typed. The following types are used:
7299
7300 - Boolean: Uses the keywords "true" and "false". There is no implicit
7301 conversion between booleans and integers.
7302
7303 - Integers: All numbers in GN are signed 64-bit integers.
7304
7305 - Strings: Strings are 8-bit with no enforced encoding. When a string is
7306 used to interact with other systems with particular encodings (like the
7307 Windows and Mac filesystems) it is assumed to be UTF-8. See "String
7308 literals" above for more.
7309
7310 - Lists: Lists are arbitrary-length ordered lists of values. See "Lists"
7311 below for more.
7312
7313 - Scopes: Scopes are like dictionaries that use variable names for keys. See
7314 "Scopes" below for more.
7315```
7316
7317#### **Lists**
7318
7319```
7320 Lists are created with [] and using commas to separate items:
7321
7322 mylist = [ 0, 1, 2, "some string" ]
7323
7324 A comma after the last item is optional. Lists are dereferenced using 0-based
7325 indexing:
7326
7327 mylist[0] += 1
7328 var = mylist[2]
7329
7330 Lists can be concatenated using the '+' and '+=' operators. Bare values can
7331 not be concatenated with lists, to add a single item, it must be put into a
7332 list of length one.
7333
7334 Items can be removed from lists using the '-' and '-=' operators. This will
7335 remove all occurrences of every item in the right-hand list from the
7336 left-hand list. It is an error to remove an item not in the list. This is to
7337 prevent common typos and to detect dead code that is removing things that no
7338 longer apply.
7339
7340 It is an error to use '=' to replace a nonempty list with another nonempty
7341 list. This is to prevent accidentally overwriting data when in most cases
7342 '+=' was intended. To overwrite a list on purpose, first assign it to the
7343 empty list:
7344
7345 mylist = []
7346 mylist = otherlist
Brett Wilson796ed472018-07-16 15:11:09 -07007347```
7348
7349#### **Scopes**
7350
7351```
7352 All execution happens in the context of a scope which holds the current state
7353 (like variables). With the exception of loops and conditions, '{' introduces
Tyler Mandry4a648092022-02-15 19:47:09 +00007354 a new scope.
Brett Wilson796ed472018-07-16 15:11:09 -07007355
Tyler Mandry4a648092022-02-15 19:47:09 +00007356 Most scopes have a parent reference to the old scope. Variable reads
7357 recursively search all parent scopes until the variable is found or there are
7358 no more scopes. Variable writes always go into the current scope. This means
7359 that after the closing '}' (again excepting loops and conditions), all local
7360 variables will be restored to the previous values. This also means that "foo
7361 = foo" can do useful work by copying a variable into the current scope that
7362 was defined in a containing scope.
Brett Wilson796ed472018-07-16 15:11:09 -07007363
Tyler Mandry4a648092022-02-15 19:47:09 +00007364 Scopes can be assigned to variables. Examples of such scopes are the
7365 implicitly-created "invoker" when invoking a template (which refers to the
7366 variables set by the invoking code), scopes created by functions like
7367 exec_script, and scopes explicitly created like
Brett Wilson796ed472018-07-16 15:11:09 -07007368
7369 empty_scope = {}
7370 myvalues = {
7371 foo = 21
7372 bar = "something"
7373 }
7374
Tyler Mandry4a648092022-02-15 19:47:09 +00007375 In the case of explicitly created scopes and scopes created by functions like
7376 exec_script, there is no reference to the parent scope. Such scopes are fully
7377 self-contained and do not "inherit" values from their defining scope.
7378
7379 Inside an explicit scope definition can be any GN code including conditionals
7380 and function calls. After the close of the scope, it will contain all
7381 variables explicitly set by the code contained inside it. After this, the
7382 values can be read, modified, or added to:
Brett Wilson796ed472018-07-16 15:11:09 -07007383
7384 myvalues.foo += 2
7385 empty_scope.new_thing = [ 1, 2, 3 ]
Nate Fischer8ed01d32019-01-08 17:32:01 -08007386
7387 Scope equality is defined as single-level scopes identical within the current
7388 scope. That is, all values in the first scope must be present and identical
7389 within the second, and vice versa. Note that this means inherited scopes are
7390 always unequal by definition.
Brett Wilson796ed472018-07-16 15:11:09 -07007391```
Julie Hockettd69a9c32019-01-23 14:36:18 -08007392### **Input and output conversion**
Brett Wilson796ed472018-07-16 15:11:09 -07007393
7394```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007395 Input and output conversions are arguments to file and process functions
7396 that specify how to convert data to or from external formats. The possible
7397 values for parameters specifying conversions are:
7398
Brett Wilson796ed472018-07-16 15:11:09 -07007399 "" (the default)
Julie Hockett09171292018-07-31 14:35:10 -07007400 input: Discard the result and return None.
7401
7402 output: If value is a list, then "list lines"; otherwise "value".
Brett Wilson796ed472018-07-16 15:11:09 -07007403
7404 "list lines"
Julie Hockett09171292018-07-31 14:35:10 -07007405 input:
7406 Return the file contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007407 newlines will not be present in the result. The last line may or may
7408 not end in a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007409
Julie Hockett09171292018-07-31 14:35:10 -07007410 After splitting, each individual line will be trimmed of whitespace on
7411 both ends.
7412
7413 output:
7414 Renders the value contents as a list, with a string for each line. The
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007415 newlines will not be present in the result. The last line will end in
7416 with a newline.
Brett Wilson796ed472018-07-16 15:11:09 -07007417
7418 "scope"
Julie Hockett09171292018-07-31 14:35:10 -07007419 input:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007420 Execute the block as GN code and return a scope with the resulting
7421 values in it. If the input was:
Julie Hockett09171292018-07-31 14:35:10 -07007422 a = [ "hello.cc", "world.cc" ]
7423 b = 26
7424 and you read the result into a variable named "val", then you could
7425 access contents the "." operator on "val":
7426 sources = val.a
7427 some_count = val.b
7428
7429 output:
7430 Renders the value contents as a GN code block, reversing the input
7431 result above.
Brett Wilson796ed472018-07-16 15:11:09 -07007432
7433 "string"
Julie Hockett09171292018-07-31 14:35:10 -07007434 input: Return the file contents into a single string.
7435
7436 output:
7437 Render the value contents into a single string. The output is:
7438 a string renders with quotes, e.g. "str"
7439 an integer renders as a stringified integer, e.g. "6"
7440 a boolean renders as the associated string, e.g. "true"
7441 a list renders as a representation of its contents, e.g. "[\"str\", 6]"
7442 a scope renders as a GN code block of its values. If the Value was:
7443 Value val;
7444 val.a = [ "hello.cc", "world.cc" ];
7445 val.b = 26
7446 the resulting output would be:
7447 "{
7448 a = [ \"hello.cc\", \"world.cc\" ]
7449 b = 26
7450 }"
Brett Wilson796ed472018-07-16 15:11:09 -07007451
7452 "value"
Julie Hockett09171292018-07-31 14:35:10 -07007453 input:
7454 Parse the input as if it was a literal rvalue in a buildfile. Examples of
7455 typical program output using this mode:
7456 [ "foo", "bar" ] (result will be a list)
7457 or
7458 "foo bar" (result will be a string)
7459 or
7460 5 (result will be an integer)
Brett Wilson796ed472018-07-16 15:11:09 -07007461
Julie Hockett09171292018-07-31 14:35:10 -07007462 Note that if the input is empty, the result will be a null value which
7463 will produce an error if assigned to a variable.
7464
7465 output:
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08007466 Render the value contents as a literal rvalue. Strings render with
7467 escaped quotes.
Brett Wilson796ed472018-07-16 15:11:09 -07007468
7469 "json"
Julie Hockett09171292018-07-31 14:35:10 -07007470 input: Parse the input as a JSON and convert it to equivalent GN rvalue.
7471
7472 output: Convert the Value to equivalent JSON value.
7473
7474 The data type mapping is:
Brett Wilson796ed472018-07-16 15:11:09 -07007475 a string in JSON maps to string in GN
7476 an integer in JSON maps to integer in GN
7477 a float in JSON is unsupported and will result in an error
7478 an object in JSON maps to scope in GN
7479 an array in JSON maps to list in GN
7480 a boolean in JSON maps to boolean in GN
7481 a null in JSON is unsupported and will result in an error
7482
Julie Hockett09171292018-07-31 14:35:10 -07007483 Nota that the input dictionary keys have to be valid GN identifiers
7484 otherwise they will produce an error.
Brett Wilson796ed472018-07-16 15:11:09 -07007485
Julie Hockett09171292018-07-31 14:35:10 -07007486 "trim ..." (input only)
Brett Wilson796ed472018-07-16 15:11:09 -07007487 Prefixing any of the other transformations with the word "trim" will
7488 result in whitespace being trimmed from the beginning and end of the
7489 result before processing.
7490
7491 Examples: "trim string" or "trim list lines"
7492
7493 Note that "trim value" is useless because the value parser skips
7494 whitespace anyway.
7495```
Sylvain Defresneaff489a2020-03-11 18:27:43 +01007496### **File patterns**
7497
7498```
7499 File patterns are VERY limited regular expressions. They must match the
7500 entire input string to be counted as a match. In regular expression parlance,
7501 there is an implicit "^...$" surrounding your input. If you want to match a
7502 substring, you need to use wildcards at the beginning and end.
7503
7504 There are only two special tokens understood by the pattern matcher.
7505 Everything else is a literal.
7506
7507 - "*" Matches zero or more of any character. It does not depend on the
7508 preceding character (in regular expression parlance it is equivalent to
7509 ".*").
7510
7511 - "\b" Matches a path boundary. This will match the beginning or end of a
7512 string, or a slash.
7513```
7514
7515#### **Pattern examples**
7516
7517```
7518 "*asdf*"
7519 Matches a string containing "asdf" anywhere.
7520
7521 "asdf"
7522 Matches only the exact string "asdf".
7523
7524 "*.cc"
7525 Matches strings ending in the literal ".cc".
7526
7527 "\bwin/*"
7528 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
7529```
Brett Wilson796ed472018-07-16 15:11:09 -07007530### **Label patterns**
7531
7532```
7533 A label pattern is a way of expressing one or more labels in a portion of the
7534 source tree. They are not general regular expressions.
7535
7536 They can take the following forms only:
7537
7538 - Explicit (no wildcard):
7539 "//foo/bar:baz"
7540 ":baz"
7541
7542 - Wildcard target names:
7543 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
7544 ":*" (all targets in the current build file)
7545
7546 - Wildcard directory names ("*" is only supported at the end)
7547 "*" (all targets)
7548 "//foo/bar/*" (all targets in any subdir of //foo/bar)
7549 "./*" (all targets in the current build file or sub dirs)
7550
Julie Hockette2a29402018-07-31 10:11:42 -07007551 Any of the above forms can additionally take an explicit toolchain
7552 in parenthesis at the end of the label pattern. In this case, the
7553 toolchain must be fully qualified (no wildcards are supported in the
7554 toolchain name).
Brett Wilson796ed472018-07-16 15:11:09 -07007555
Julie Hockette2a29402018-07-31 10:11:42 -07007556 "//foo:bar(//build/toolchain:mac)"
Brett Wilson796ed472018-07-16 15:11:09 -07007557 An explicit target in an explicit toolchain.
7558
7559 ":*(//build/toolchain/linux:32bit)"
7560 All targets in the current build file using the 32-bit Linux toolchain.
7561
7562 "//foo/*(//build/toolchain:win)"
7563 All targets in //foo and any subdirectory using the Windows
7564 toolchain.
7565```
7566### **About labels**
7567
7568```
7569 Everything that can participate in the dependency graph (targets, configs,
7570 and toolchains) are identified by labels. A common label looks like:
7571
7572 //base/test:test_support
7573
7574 This consists of a source-root-absolute path, a colon, and a name. This means
7575 to look for the thing named "test_support" in "base/test/BUILD.gn".
7576
7577 You can also specify system absolute paths if necessary. Typically such
7578 paths would be specified via a build arg so the developer can specify where
7579 the component is on their system.
7580
7581 /usr/local/foo:bar (Posix)
7582 /C:/Program Files/MyLibs:bar (Windows)
7583```
7584
7585#### **Toolchains**
7586
7587```
7588 A canonical label includes the label of the toolchain being used. Normally,
7589 the toolchain label is implicitly inherited from the current execution
7590 context, but you can override this to specify cross-toolchain dependencies:
7591
7592 //base/test:test_support(//build/toolchain/win:msvc)
7593
7594 Here GN will look for the toolchain definition called "msvc" in the file
7595 "//build/toolchain/win" to know how to compile this target.
7596```
7597
7598#### **Relative labels**
7599
7600```
7601 If you want to refer to something in the same buildfile, you can omit
7602 the path name and just start with a colon. This format is recommended for
7603 all same-file references.
7604
7605 :base
7606
7607 Labels can be specified as being relative to the current directory.
7608 Stylistically, we prefer to use absolute paths for all non-file-local
7609 references unless a build file needs to be run in different contexts (like a
7610 project needs to be both standalone and pulled into other projects in
7611 difference places in the directory hierarchy).
7612
7613 source/plugin:myplugin
7614 ../net:url_request
7615```
7616
7617#### **Implicit names**
7618
7619```
7620 If a name is unspecified, it will inherit the directory name. Stylistically,
7621 we prefer to omit the colon and name when possible:
7622
7623 //net -> //net:net
7624 //tools/gn -> //tools/gn:gn
7625```
Julie Hockett152c5142019-07-12 09:53:43 -06007626### **Metadata Collection**
7627
7628```
7629 Metadata is information attached to targets throughout the dependency tree. GN
7630 allows for the collection of this data into files written during the generation
Keir Mierle45611e32019-11-12 11:18:00 -08007631 step, enabling users to expose and aggregate this data based on the dependency
Julie Hockett152c5142019-07-12 09:53:43 -06007632 tree.
7633```
7634
7635#### **generated_file targets**
7636
7637```
7638 Similar to the write_file() function, the generated_file target type
7639 creates a file in the specified location with the specified content. The
Keir Mierle45611e32019-11-12 11:18:00 -08007640 primary difference between write_file() and this target type is that the
Julie Hockett152c5142019-07-12 09:53:43 -06007641 write_file function does the file write at parse time, while the
7642 generated_file target type writes at target resolution time. See
7643 "gn help generated_file" for more detail.
7644
Keir Mierle45611e32019-11-12 11:18:00 -08007645 When written at target resolution time, generated_file enables GN to
Julie Hockett152c5142019-07-12 09:53:43 -06007646 collect and write aggregated metadata from dependents.
7647
Keir Mierle45611e32019-11-12 11:18:00 -08007648 A generated_file target can declare either 'contents' to write statically
7649 known contents to a file or 'data_keys' to aggregate metadata and write the
7650 result to a file. It can also specify 'walk_keys' (to restrict the metadata
Julie Hockett152c5142019-07-12 09:53:43 -06007651 collection), 'output_conversion', and 'rebase'.
7652```
7653
7654#### **Collection and Aggregation**
7655
7656```
7657 Targets can declare a 'metadata' variable containing a scope, and this
Keir Mierle45611e32019-11-12 11:18:00 -08007658 metadata may be collected and written out to a file specified by
7659 generated_file aggregation targets. The 'metadata' scope must contain
7660 only list values since the aggregation step collects a list of these values.
Julie Hockett152c5142019-07-12 09:53:43 -06007661
7662 During the target resolution, generated_file targets will walk their
7663 dependencies recursively, collecting metadata based on the specified
7664 'data_keys'. 'data_keys' is specified as a list of strings, used by the walk
7665 to identify which variables in dependencies' 'metadata' scopes to collect.
7666
Keir Mierle45611e32019-11-12 11:18:00 -08007667 The walk begins with the listed dependencies of the 'generated_file' target.
7668 The 'metadata' scope for each dependency is inspected for matching elements
7669 of the 'generated_file' target's 'data_keys' list. If a match is found, the
7670 data from the dependent's matching key list is appended to the aggregate walk
7671 list. Note that this means that if more than one walk key is specified, the
7672 data in all of them will be aggregated into one list. From there, the walk
7673 will then recurse into the dependencies of each target it encounters,
7674 collecting the specified metadata for each.
Julie Hockett152c5142019-07-12 09:53:43 -06007675
7676 For example:
7677
7678 group("a") {
7679 metadata = {
7680 doom_melon = [ "enable" ]
7681 my_files = [ "foo.cpp" ]
7682 my_extra_files = [ "bar.cpp" ]
7683 }
7684
7685 deps = [ ":b" ]
7686 }
7687
7688 group("b") {
7689 metadata = {
7690 my_files = [ "baz.cpp" ]
7691 }
7692 }
7693
7694 generated_file("metadata") {
7695 outputs = [ "$root_build_dir/my_files.json" ]
7696 data_keys = [ "my_files", "my_extra_files" ]
7697
7698 deps = [ ":a" ]
7699 }
7700
7701 The above will produce the following file data:
7702
7703 foo.cpp
7704 bar.cpp
7705 baz.cpp
7706
Keir Mierle45611e32019-11-12 11:18:00 -08007707 The dependency walk can be limited by using the 'walk_keys'. This is a list of
Julie Hockett152c5142019-07-12 09:53:43 -06007708 labels that should be included in the walk. All labels specified here should
7709 also be in one of the deps lists. These keys act as barriers, where the walk
Keir Mierle45611e32019-11-12 11:18:00 -08007710 will only recurse into the targets listed. An empty list in all specified
Julie Hockett152c5142019-07-12 09:53:43 -06007711 barriers will end that portion of the walk.
7712
7713 group("a") {
7714 metadata = {
7715 my_files = [ "foo.cpp" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02007716 my_files_barrier = [ ":b" ]
Julie Hockett152c5142019-07-12 09:53:43 -06007717 }
7718
7719 deps = [ ":b", ":c" ]
7720 }
7721
7722 group("b") {
7723 metadata = {
7724 my_files = [ "bar.cpp" ]
7725 }
7726 }
7727
7728 group("c") {
7729 metadata = {
7730 my_files = [ "doom_melon.cpp" ]
7731 }
7732 }
7733
7734 generated_file("metadata") {
7735 outputs = [ "$root_build_dir/my_files.json" ]
Sylvain Defresne3028c6a2020-07-20 11:28:59 +02007736 data_keys = [ "my_files" ]
7737 walk_keys = [ "my_files_barrier" ]
Julie Hockett152c5142019-07-12 09:53:43 -06007738
7739 deps = [ ":a" ]
7740 }
7741
7742 The above will produce the following file data (note that `doom_melon.cpp` is
7743 not included):
7744
7745 foo.cpp
7746 bar.cpp
7747
7748 A common example of this sort of barrier is in builds that have host tools
7749 built as part of the tree, but do not want the metadata from those host tools
7750 to be collected with the target-side code.
7751```
7752
7753#### **Common Uses**
7754
7755```
7756 Metadata can be used to collect information about the different targets in the
7757 build, and so a common use is to provide post-build tooling with a set of data
7758 necessary to do aggregation tasks. For example, if each test target specifies
7759 the output location of its binary to run in a metadata field, that can be
7760 collected into a single file listing the locations of all tests in the
7761 dependency tree. A local build tool (or continuous integration infrastructure)
7762 can then use that file to know which tests exist, and where, and run them
7763 accordingly.
7764
7765 Another use is in image creation, where a post-build image tool needs to know
7766 various pieces of information about the components it should include in order
7767 to put together the correct image.
7768```
Brett Wilson796ed472018-07-16 15:11:09 -07007769### **Ninja build rules**
7770
7771#### **The "all" and "default" rules**
7772
7773```
7774 All generated targets (see "gn help execution") will be added to an implicit
7775 build rule called "all" so "ninja all" will always compile everything. The
7776 default rule will be used by Ninja if no specific target is specified (just
7777 typing "ninja"). If there is a target named "default" in the root build file,
7778 it will be the default build rule, otherwise the implicit "all" rule will be
7779 used.
7780```
7781
7782#### **Phony rules**
7783
7784```
7785 GN generates Ninja "phony" rules for targets in the default toolchain. The
7786 phony rules can collide with each other and with the names of generated files
7787 so are generated with the following priority:
7788
7789 1. Actual files generated by the build always take precedence.
7790
7791 2. Targets in the toplevel //BUILD.gn file.
7792
7793 3. Targets in toplevel directories matching the names of the directories.
7794 So "ninja foo" can be used to compile "//foo:foo". This only applies to
7795 the first level of directories since usually these are the most
7796 important (so this won't apply to "//foo/bar:bar").
7797
7798 4. The short names of executables if there is only one executable with that
7799 short name. Use "ninja doom_melon" to compile the
7800 "//tools/fruit:doom_melon" executable.
7801
7802 5. The short names of all targets if there is only one target with that
7803 short name.
7804
7805 6. Full label name with no leading slashes. So you can use
7806 "ninja tools/fruit:doom_melon" to build "//tools/fruit:doom_melon".
7807
7808 7. Labels with an implicit name part (when the short names match the
7809 directory). So you can use "ninja foo/bar" to compile "//foo/bar:bar".
7810
7811 These "phony" rules are provided only for running Ninja since this matches
7812 people's historical expectations for building. For consistency with the rest
7813 of the program, GN introspection commands accept explicit labels.
7814
7815 To explicitly compile a target in a non-default toolchain, you must give
7816 Ninja the exact name of the output file relative to the build directory.
7817```
7818### **nogncheck**: Skip an include line from checking.
7819
7820```
7821 GN's header checker helps validate that the includes match the build
7822 dependency graph. Sometimes an include might be conditional or otherwise
7823 problematic, but you want to specifically allow it. In this case, it can be
7824 whitelisted.
7825
7826 Include lines containing the substring "nogncheck" will be excluded from
7827 header checking. The most common case is a conditional include:
7828
7829 #if defined(ENABLE_DOOM_MELON)
7830 #include "tools/doom_melon/doom_melon.h" // nogncheck
7831 #endif
7832
7833 If the build file has a conditional dependency on the corresponding target
7834 that matches the conditional include, everything will always link correctly:
7835
7836 source_set("mytarget") {
7837 ...
7838 if (enable_doom_melon) {
7839 defines = [ "ENABLE_DOOM_MELON" ]
7840 deps += [ "//tools/doom_melon" ]
7841 }
7842
7843 But GN's header checker does not understand preprocessor directives, won't
7844 know it matches the build dependencies, and will flag this include as
7845 incorrect when the condition is false.
7846```
7847
7848#### **More information**
7849
7850```
7851 The topic "gn help check" has general information on how checking works and
7852 advice on fixing problems. Targets can also opt-out of checking, see
7853 "gn help check_includes".
7854```
Brett Wilson796ed472018-07-16 15:11:09 -07007855### **Runtime dependencies**
7856
7857```
7858 Runtime dependencies of a target are exposed via the "runtime_deps" category
7859 of "gn desc" (see "gn help desc") or they can be written at build generation
7860 time via write_runtime_deps(), or --runtime-deps-list-file (see "gn help
7861 --runtime-deps-list-file").
7862
7863 To a first approximation, the runtime dependencies of a target are the set of
7864 "data" files, data directories, and the shared libraries from all transitive
7865 dependencies. Executables, shared libraries, and loadable modules are
7866 considered runtime dependencies of themselves.
7867```
7868
7869#### **Executables**
7870
7871```
7872 Executable targets and those executable targets' transitive dependencies are
7873 not considered unless that executable is listed in "data_deps". Otherwise, GN
7874 assumes that the executable (and everything it requires) is a build-time
7875 dependency only.
7876```
7877
7878#### **Actions and copies**
7879
7880```
7881 Action and copy targets that are listed as "data_deps" will have all of their
7882 outputs and data files considered as runtime dependencies. Action and copy
7883 targets that are "deps" or "public_deps" will have only their data files
7884 considered as runtime dependencies. These targets can list an output file in
7885 both the "outputs" and "data" lists to force an output file as a runtime
7886 dependency in all cases.
7887
7888 The different rules for deps and data_deps are to express build-time (deps)
7889 vs. run-time (data_deps) outputs. If GN counted all build-time copy steps as
7890 data dependencies, there would be a lot of extra stuff, and if GN counted all
7891 run-time dependencies as regular deps, the build's parallelism would be
7892 unnecessarily constrained.
7893
7894 This rule can sometimes lead to unintuitive results. For example, given the
7895 three targets:
7896 A --[data_deps]--> B --[deps]--> ACTION
7897 GN would say that A does not have runtime deps on the result of the ACTION,
7898 which is often correct. But the purpose of the B target might be to collect
7899 many actions into one logic unit, and the "data"-ness of A's dependency is
7900 lost. Solutions:
7901
7902 - List the outputs of the action in its data section (if the results of
7903 that action are always runtime files).
7904 - Have B list the action in data_deps (if the outputs of the actions are
7905 always runtime files).
7906 - Have B list the action in both deps and data deps (if the outputs might be
7907 used in both contexts and you don't care about unnecessary entries in the
7908 list of files required at runtime).
7909 - Split B into run-time and build-time versions with the appropriate "deps"
7910 for each.
7911```
7912
7913#### **Static libraries and source sets**
7914
7915```
7916 The results of static_library or source_set targets are not considered
7917 runtime dependencies since these are assumed to be intermediate targets only.
7918 If you need to list a static library as a runtime dependency, you can
7919 manually compute the .a/.lib file name for the current platform and list it
7920 in the "data" list of a target (possibly on the static library target
7921 itself).
7922```
7923
7924#### **Multiple outputs**
7925
7926```
7927 Linker tools can specify which of their outputs should be considered when
7928 computing the runtime deps by setting runtime_outputs. If this is unset on
7929 the tool, the default will be the first output only.
7930```
7931### **How Source Expansion Works**
7932
7933```
7934 Source expansion is used for the action_foreach and copy target types to map
7935 source file names to output file names or arguments.
7936
7937 To perform source expansion in the outputs, GN maps every entry in the
7938 sources to every entry in the outputs list, producing the cross product of
7939 all combinations, expanding placeholders (see below).
7940
7941 Source expansion in the args works similarly, but performing the placeholder
7942 substitution produces a different set of arguments for each invocation of the
7943 script.
7944
7945 If no placeholders are found, the outputs or args list will be treated as a
7946 static list of literal file names that do not depend on the sources.
7947
7948 See "gn help copy" and "gn help action_foreach" for more on how this is
7949 applied.
7950```
7951
7952#### **Placeholders**
7953
7954```
7955 This section discusses only placeholders for actions. There are other
7956 placeholders used in the definition of tools. See "gn help tool" for those.
7957
7958 {{source}}
7959 The name of the source file including directory (*). This will generally
7960 be used for specifying inputs to a script in the "args" variable.
7961 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
7962
7963 {{source_file_part}}
7964 The file part of the source including the extension.
7965 "//foo/bar/baz.txt" => "baz.txt"
7966
7967 {{source_name_part}}
7968 The filename part of the source file with no directory or extension. This
7969 will generally be used for specifying a transformation from a source file
7970 to a destination file with the same name but different extension.
7971 "//foo/bar/baz.txt" => "baz"
7972
7973 {{source_dir}}
7974 The directory (*) containing the source file with no trailing slash.
7975 "//foo/bar/baz.txt" => "../../foo/bar"
7976
7977 {{source_root_relative_dir}}
7978 The path to the source file's directory relative to the source root, with
7979 no leading "//" or trailing slashes. If the path is system-absolute,
7980 (beginning in a single slash) this will just return the path with no
7981 trailing slash. This value will always be the same, regardless of whether
7982 it appears in the "outputs" or "args" section.
7983 "//foo/bar/baz.txt" => "foo/bar"
7984
7985 {{source_gen_dir}}
7986 The generated file directory (*) corresponding to the source file's path.
7987 This will be different than the target's generated file directory if the
7988 source file is in a different directory than the BUILD.gn file.
7989 "//foo/bar/baz.txt" => "gen/foo/bar"
7990
7991 {{source_out_dir}}
7992 The object file directory (*) corresponding to the source file's path,
7993 relative to the build directory. this us be different than the target's
7994 out directory if the source file is in a different directory than the
7995 build.gn file.
7996 "//foo/bar/baz.txt" => "obj/foo/bar"
7997
7998 {{source_target_relative}}
7999 The path to the source file relative to the target's directory. This will
8000 generally be used for replicating the source directory layout in the
Robert Sesek252674d2019-05-10 14:08:52 -04008001 output directory. This can only be used in actions and bundle_data
8002 targets. It is an error to use in process_file_template where there is no
8003 "target".
Brett Wilson796ed472018-07-16 15:11:09 -07008004 "//foo/bar/baz.txt" => "baz.txt"
8005```
8006
8007#### **(*) Note on directories**
8008
8009```
8010 Paths containing directories (except the source_root_relative_dir) will be
8011 different depending on what context the expansion is evaluated in. Generally
8012 it should "just work" but it means you can't concatenate strings containing
8013 these values with reasonable results.
8014
8015 Details: source expansions can be used in the "outputs" variable, the "args"
8016 variable, and in calls to "process_file_template". The "args" are passed to a
8017 script which is run from the build directory, so these directories will
8018 relative to the build directory for the script to find. In the other cases,
8019 the directories will be source- absolute (begin with a "//") because the
8020 results of those expansions will be handled by GN internally.
8021```
8022
8023#### **Examples**
8024
8025```
8026 Non-varying outputs:
8027 action("hardcoded_outputs") {
8028 sources = [ "input1.idl", "input2.idl" ]
8029 outputs = [ "$target_out_dir/output1.dat",
8030 "$target_out_dir/output2.dat" ]
8031 }
8032 The outputs in this case will be the two literal files given.
8033
8034 Varying outputs:
8035 action_foreach("varying_outputs") {
8036 sources = [ "input1.idl", "input2.idl" ]
8037 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
8038 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
8039 }
8040 Performing source expansion will result in the following output names:
8041 //out/Debug/obj/mydirectory/input1.h
8042 //out/Debug/obj/mydirectory/input1.cc
8043 //out/Debug/obj/mydirectory/input2.h
8044 //out/Debug/obj/mydirectory/input2.cc
8045```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008046### **Available global switches**
Brett Wilson796ed472018-07-16 15:11:09 -07008047
8048```
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008049 Do "gn help --the_switch_you_want_help_on" for more. Individual commands may
8050 take command-specific switches not listed here. See the help on your specific
8051 command for more.
8052```
8053```
8054 * --args: Specifies build arguments overrides.
8055 * --color: Force colored output.
8056 * --dotfile: Override the name of the ".gn" file.
8057 * --fail-on-unused-args: Treat unused build args as fatal errors.
8058 * --markdown: Write help output in the Markdown format.
RJ Ascani6966efb2020-10-19 16:50:11 -07008059 * --ninja-executable: Set the Ninja executable.
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008060 * --nocolor: Force non-colored output.
8061 * -q: Quiet mode. Don't print output on success.
8062 * --root: Explicitly specify source root.
Nico Weberad9eab22020-11-15 22:20:28 -05008063 * --root-target: Override the root target.
Brett Wilsonb5a2ac62018-12-21 21:20:05 -08008064 * --runtime-deps-list-file: Save runtime dependencies for targets in file.
8065 * --script-executable: Set the executable used to execute scripts.
8066 * --threads: Specify number of worker threads.
8067 * --time: Outputs a summary of how long everything took.
8068 * --tracelog: Writes a Chrome-compatible trace log to the given file.
8069 * -v: Verbose logging.
8070 * --version: Prints the GN version number and exits.
Brett Wilson796ed472018-07-16 15:11:09 -07008071```
8072