3 *This page is automatically generated from* `gn help --markdown all`.
5 ## **--args**: Specifies build arguments overrides.
8 See "gn help buildargs" for an overview of how build arguments work.
10 Most operations take a build directory. The build arguments are taken
11 from the previous build done in that directory. If a command specifies
12 --args, it will override the previous arguments stored in the build
13 directory, and use the specified ones.
15 The args specified will be saved to the build directory for subsequent
16 commands. Specifying --args="" will clear all build arguments.
23 The value of the switch is interpreted in GN syntax. For typical usage
24 of string arguments, you will need to be careful about escaping of
32 gn gen out/Default --args="foo=\"bar\""
34 gn gen out/Default --args='foo="bar" enable=true blah=7'
36 gn check out/Default --args=""
37 Clears existing build args from the directory.
39 gn desc out/Default --args="some_list=[1, false, \"foo\"]"
43 ## **--[no]color**: Forces colored output on or off.
46 Normally GN will try to detect whether it is outputting to a terminal
47 and will enable or disable color accordingly. Use of these switches
48 will override the default.
55 gn gen out/Default --color
57 gn gen out/Default --nocolor
61 ## **--dotfile**: override the name of the ".gn" file.
64 Normally GN loads the ".gn"file from the source root for some basic
65 configuration (see "gn help dotfile"). This flag allows you to
68 Note that this interacts with "--root" in a possibly incorrect way.
69 It would be nice to test the edge cases and document or fix.
73 ## **--markdown**: write the output in the Markdown format.
75 ## **--[no]color**: Forces colored output on or off.
78 Normally GN will try to detect whether it is outputting to a terminal
79 and will enable or disable color accordingly. Use of these switches
80 will override the default.
87 gn gen out/Default --color
89 gn gen out/Default --nocolor
93 ## **-q**: Quiet mode. Don't print output on success.
96 This is useful when running as a part of another script.
100 ## **--root**: Explicitly specify source root.
103 Normally GN will look up in the directory tree from the current
104 directory to find a ".gn" file. The source root directory specifies
105 the meaning of "//" beginning with paths, and the BUILD.gn file
106 in that directory will be the first thing loaded.
108 Specifying --root allows GN to do builds in a specific directory
109 regardless of the current directory.
116 gn gen //out/Default --root=/home/baracko/src
118 gn desc //out/Default --root="C:\Users\BObama\My Documents\foo"
122 ## **--time**: Outputs a summary of how long everything took.
125 Hopefully self-explanatory.
132 gn gen out/Default --time
136 ## **--tracelog**: Writes a Chrome-compatible trace log to the given file.
139 The trace log will show file loads, executions, scripts, and writes.
140 This allows performance analysis of the generation step.
142 To view the trace, open Chrome and navigate to "chrome://tracing/",
143 then press "Load" and specify the file you passed to this parameter.
150 gn gen out/Default --tracelog=mytrace.trace
154 ## **-v**: Verbose logging.
157 This will spew logging events to the console for debugging issues.
162 ## **gn args <out_dir> [--list] [--short] [--args]**
165 See also "gn help buildargs" for a more high-level overview of how
166 build arguments work.
173 Open the arguments for the given build directory in an editor
174 (as specified by the EDITOR environment variable). If the given
175 build directory doesn't exist, it will be created and an empty
176 args file will be opened in the editor. You would type something
177 like this into that file:
178 enable_doom_melon=false
181 Note: you can edit the build args manually by editing the file
182 "args.gn" in the build directory and then running
185 gn args <out_dir> --list[=<exact_arg>] [--short]
186 Lists all build arguments available in the current configuration,
187 or, if an exact_arg is specified for the list flag, just that one
190 The output will list the declaration location, default value, and
191 comment preceeding the declaration. If --short is specified,
192 only the names and values will be printed.
194 If the out_dir is specified, the build configuration will be
195 taken from that build directory. The reason this is needed is that
196 the definition of some arguments is dependent on the build
197 configuration, so setting some values might add, remove, or change
198 the default values for other arguments. Specifying your exact
199 configuration allows the proper arguments to be displayed.
201 Instead of specifying the out_dir, you can also use the
202 command-line flag to specify the build configuration:
203 --args=<exact list of args to use>
210 Opens an editor with the args for out/Debug.
212 gn args out/Debug --list --short
213 Prints all arguments with their default values for the out/Debug
216 gn args out/Debug --list=target_cpu
217 Prints information about the "target_cpu" argument for the out/Debug
220 gn args --list --args="os=\"android\" enable_doom_melon=true"
221 Prints all arguments with the default values for a build with the
222 given arguments set (which may affect the values of other
227 ## **gn check <out_dir> [<label_pattern>] [--force]**
230 "gn check" is the same thing as "gn gen" with the "--check" flag
231 except that this command does not write out any build files. It's
232 intended to be an easy way to manually trigger include file checking.
234 The <label_pattern> can take exact labels or patterns that match more
235 than one (although not general regular expressions). If specified,
236 only those matching targets will be checked. See
237 "gn help label_pattern" for details.
239 The .gn file may specify a list of targets to be checked. Only these
240 targets will be checked if no label_pattern is specified on the
241 command line. Otherwise, the command-line list is used instead. See
246 ### **Command-specific switches**
250 Ignores specifications of "check_includes = false" and checks
251 all target's files that match the target label.
261 gn check out/Default //foo:bar
262 Check only the files in the //foo:bar target.
264 gn check out/Default "//foo/*
265 Check only the files in targets in the //foo directory tree.
269 ## **gn clean <out_dir>**
272 Deletes the contents of the output directory except for args.gn and
273 creates a Ninja build environment sufficient to regenerate the build.
277 ## **gn desc <out_dir> <target label> [<what to show>] [--blame]**
280 Displays information about a given labeled target for the given build.
281 The build parameters will be taken for the build in the given
286 ### **Possibilities for <what to show>**
288 (If unspecified an overall summary will be displayed.)
294 Additional input dependencies.
300 Whether "gn check" checks this target for include usage.
302 allow_circular_includes_from
303 Permit includes from these targets.
306 Prints which targets can depend on this one.
309 Whether this target may only be used in tests.
312 Shows configs applied to the given target, sorted in the order
313 they're specified. This includes both configs specified in the
314 "configs" variable, as well as configs pushed onto this target
315 via dependencies specifying "all" or "direct" dependent
319 Show immediate or recursive dependencies. See below for flags that
320 control deps printing.
323 all_dependent_configs
324 Shows the labels of configs applied to targets that depend on this
325 one (either directly or all of them).
327 forward_dependent_configs_from
328 Shows the labels of dependencies for which dependent configs will
329 be pushed to targets depending on the current one.
334 Actions only. The script and related values.
337 Outputs for script and copy target types.
340 include_dirs [--blame]
347 Shows the given values taken from the target and all configs
348 applying. See "--blame" below.
351 Used with any value specified by a config, this will name
352 the config that specified the value. This doesn't currently work
353 for libs and lib_dirs because those are inherited and are more
354 complicated to figure out the blame (patches welcome).
358 ### **Flags that control how deps are printed**
362 Collects all recursive dependencies and prints a sorted flat list.
363 Also usable with --tree (see below).
365 --as=(buildfile|label|output)
366 How to print targets.
369 Prints the build files where the given target was declared as
372 Prints the label of the target.
374 Prints the first output file for the target relative to the
377 --testonly=(true|false)
378 Restrict outputs to targets with the testonly flag set
379 accordingly. When unspecified, the target's testonly flags are
383 Print a dependency tree. By default, duplicates will be elided
384 with "..." but when --all and -tree are used together, no
385 eliding will be performed.
387 The "deps", "public_deps", and "data_deps" will all be
388 included in the tree.
390 Tree output can not be used with the filtering or output flags:
391 --as, --type, --testonly.
393 --type=(action|copy|executable|group|shared_library|source_set|
395 Restrict outputs to targets matching the given type. If
396 unspecified, no filtering will be performed.
403 This command will show the full name of directories and source files,
404 but when directories and source paths are written to the build file,
405 they will be adjusted to be relative to the build directory. So the
406 values for paths displayed by this command won't match (but should
407 mean the same thing).
414 gn desc out/Debug //base:base
415 Summarizes the given target.
417 gn desc out/Foo :base_unittests deps --tree
418 Shows a dependency tree of the "base_unittests" project in
419 the current directory.
421 gn desc out/Debug //base defines --blame
422 Shows defines set for the //base:base target, annotated by where
423 each one was set from.
427 ## **gn format [--dump-tree] [--in-place] [--stdin] BUILD.gn**
430 Formats .gn file to a standard format.
437 Does not change or output anything, but sets the process exit code
438 based on whether output would be different than what's on disk.
439 This is useful for presubmit/lint-type checks.
440 - Exit code 0: successful format, matches on disk.
441 - Exit code 1: general failure (parse error, etc.)
442 - Exit code 2: successful format, but differs from on disk.
445 For debugging only, dumps the parse tree.
448 Instead of writing the formatted file to stdout, replace the input
449 file with the formatted output. If no reformatting is required,
450 the input file will not be touched, and nothing printed.
453 Read input from stdin (and write to stdout). Not compatible with
454 --in-place of course.
460 gn format //some/BUILD.gn
461 gn format some\BUILD.gn
462 gn format /abspath/some/BUILD.gn
467 ## **gn gen**: Generate ninja files.
472 Generates ninja files from the current tree and puts them in the given
475 The output directory can be a source-repo-absolute path name such as:
477 Or it can be a directory relative to the current directory such as:
480 See "gn help" for the common command-line switches.
484 ## **gn help <anything>**
486 Yo dawg, I heard you like help on your help so I put help on the help
491 ## **gn ls <out_dir> [<label_pattern>] [--all-toolchains] [--as=...]**
493 [--type=...] [--testonly=...]
495 Lists all targets matching the given pattern for the given build
496 directory. By default, only targets in the default toolchain will
497 be matched unless a toolchain is explicitly supplied.
499 If the label pattern is unspecified, list all targets. The label
500 pattern is not a general regular expression (see
501 "gn help label_pattern"). If you need more complex expressions,
502 pipe the result through grep.
509 --as=(buildfile|label|output)
510 How to print targets.
513 Prints the build files where the given target was declared as
516 Prints the label of the target.
518 Prints the first output file for the target relative to the
522 Matches all toolchains. When set, if the label pattern does not
523 specify an explicit toolchain, labels from all toolchains will be
524 matched. When unset, only targets in the default toolchain will
525 be matched unless an explicit toolchain in the label is set.
527 --testonly=(true|false)
528 Restrict outputs to targets with the testonly flag set
529 accordingly. When unspecified, the target's testonly flags are
532 --type=(action|copy|executable|group|shared_library|source_set|
534 Restrict outputs to targets matching the given type. If
535 unspecified, no filtering will be performed.
543 Lists all targets in the default toolchain.
545 gn ls out/Debug "//base/*"
546 Lists all targets in the directory base and all subdirectories.
548 gn ls out/Debug "//base:*"
549 Lists all targets defined in //base/BUILD.gn.
551 gn ls out/Debug //base --as=output
552 Lists the build output file for //base:base
554 gn ls out/Debug --type=executable
555 Lists all executables produced by the build.
557 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
558 Builds all targets in //base and all subdirectories.
560 gn ls out/Debug //base --all-toolchains
561 Lists all variants of the target //base:base (it may be referenced
562 in multiple toolchains).
566 ## **gn refs <out_dir> (<label_pattern>|<label>|<file>|@<response_file>)* [--all]**
568 [--all-toolchains] [--as=...] [--testonly=...] [--type=...]
570 Finds reverse dependencies (which targets reference something). The
571 input is a list containing:
573 - Target label: The result will be which targets depend on it.
575 - Config label: The result will be which targets list the given
576 config in its "configs" or "public_configs" list.
578 - Label pattern: The result will be which targets depend on any
579 target matching the given pattern. Patterns will not match
580 configs. These are not general regular expressions, see
581 "gn help label_pattern" for details.
583 - File name: The result will be which targets list the given file in
584 its "inputs", "sources", "public", or "data". Any input
585 that does not contain wildcards and does not match a target or a
586 config will be treated as a file.
588 - Response file: If the input starts with an "@", it will be
589 interpreted as a path to a file containing a list of labels or
590 file names, one per line. This allows us to handle long lists
591 of inputs without worrying about command line limits.
599 When used without --tree, will recurse and display all unique
600 dependencies of the given targets. For example, if the input is
601 a target, this will output all targets that depend directly or
602 indirectly on the input. If the input is a file, this will output
603 all targets that depend directly or indirectly on that file.
605 When used with --tree, turns off eliding to show a complete tree.
608 Normally only inputs in the default toolchain will be included.
609 This switch will turn on matching all toolchains.
611 For example, a file is in a target might be compiled twice:
612 once in the default toolchain and once in a secondary one. Without
613 this flag, only the default toolchain one will be matched and
614 printed (potentially with its recursive dependencies, depending on
615 the other options). With this flag, both will be printed
616 (potentially with both of their recursive dependencies).
618 --as=(buildfile|label|output)
619 How to print targets.
622 Prints the build files where the given target was declared as
625 Prints the label of the target.
627 Prints the first output file for the target relative to the
631 Quiet. If nothing matches, don't print any output. Without this
632 option, if there are no matches there will be an informational
633 message printed which might interfere with scripts processing the
636 --testonly=(true|false)
637 Restrict outputs to targets with the testonly flag set
638 accordingly. When unspecified, the target's testonly flags are
642 Outputs a reverse dependency tree from the given target.
643 Duplicates will be elided. Combine with --all to see a full
646 Tree output can not be used with the filtering or output flags:
647 --as, --type, --testonly.
649 --type=(action|copy|executable|group|shared_library|source_set|
651 Restrict outputs to targets matching the given type. If
652 unspecified, no filtering will be performed.
656 ### **Examples (target input)**
659 gn refs out/Debug //tools/gn:gn
660 Find all targets depending on the given exact target name.
662 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
663 Edit all .gn files containing references to //base:i18n
665 gn refs out/Debug //base --all
666 List all targets depending directly or indirectly on //base:base.
668 gn refs out/Debug "//base/*"
669 List all targets depending directly on any target in //base or
672 gn refs out/Debug "//base:*"
673 List all targets depending directly on any target in
676 gn refs out/Debug //base --tree
677 Print a reverse dependency tree of //base:base
681 ### **Examples (file input)**
684 gn refs out/Debug //base/macros.h
685 Print target(s) listing //base/macros.h as a source.
687 gn refs out/Debug //base/macros.h --tree
688 Display a reverse dependency tree to get to the given file. This
689 will show how dependencies will reference that file.
691 gn refs out/Debug //base/macros.h //base/basictypes.h --all
692 Display all unique targets with some dependency path to a target
693 containing either of the given files as a source.
695 gn refs out/Debug //base/macros.h --testonly=true --type=executable
697 Display the executable file names of all test executables
698 potentially affected by a change to the given file.
702 ## **action**: Declare a target that runs a script a single time.
705 This target type allows you to run a script a single time to produce
706 or more output files. If you want to run a script once for each of a
707 set of input files, see "gn help action_foreach".
714 In an action the "sources" and "inputs" are treated the same:
715 they're both input dependencies on script execution with no special
716 handling. If you want to pass the sources to your script, you must do
717 so explicitly by including them in the "args". Note also that this
718 means there is no special handling of paths since GN doesn't know
719 which of the args are paths and not. You will want to use
720 rebase_path() to convert paths to be relative to the root_build_dir.
722 You can dynamically write input dependencies (for incremental rebuilds
723 if an input file changes) by writing a depfile when the script is run
724 (see "gn help depfile"). This is more flexible than "inputs".
726 It is recommended you put inputs to your script in the "sources"
727 variable, and stuff like other Python files required to run your
728 script in the "inputs" variable.
730 The "deps" and "public_deps" for an action will always be
731 completed before any part of the action is run so it can depend on
732 the output of previous steps. The "data_deps" will be built if the
733 action is built, but may not have completed before all steps of the
734 action are started. This can give additional parallelism in the build
735 for runtime-only dependencies.
742 You should specify files created by your script by specifying them in
745 The script will be executed with the given arguments with the current
746 directory being that of the root build directory. If you pass files
747 to your script, see "gn help rebase_path" for how to convert
748 file names to be relative to the build directory (file names in the
749 sources, outputs, and inputs will be all treated as relative to the
750 current build file and converted as needed automatically).
754 ### **File name handling**
757 All output files must be inside the output directory of the build.
758 You would generally use |$target_out_dir| or |$target_gen_dir| to
759 reference the output or generated intermediate file directories,
767 args, data, data_deps, depfile, deps, outputs*, script*,
776 action("run_this_guy_once") {
777 script = "doprocessing.py"
778 sources = [ "my_configuration.txt" ]
779 outputs = [ "$target_gen_dir/insightful_output.txt" ]
781 # Our script imports this Python file so we want to rebuild if it
783 inputs = [ "helper_library.py" ]
785 # Note that we have to manually pass the sources to our script if
786 # the script needs them as inputs.
787 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
788 rebase_path(sources, root_build_dir)
793 ## **action_foreach**: Declare a target that runs a script over a set of files.
796 This target type allows you to run a script once-per-file over a set
797 of sources. If you want to run a script once that takes many files as
798 input, see "gn help action".
805 The script will be run once per file in the "sources" variable. The
806 "outputs" variable should specify one or more files with a source
807 expansion pattern in it (see "gn help source_expansion"). The output
808 file(s) for each script invocation should be unique. Normally you
809 use "{{source_name_part}}" in each output file.
811 If your script takes additional data as input, such as a shared
812 configuration file or a Python module it uses, those files should be
813 listed in the "inputs" variable. These files are treated as
814 dependencies of each script invocation.
816 You can dynamically write input dependencies (for incremental rebuilds
817 if an input file changes) by writing a depfile when the script is run
818 (see "gn help depfile"). This is more flexible than "inputs".
820 The "deps" and "public_deps" for an action will always be
821 completed before any part of the action is run so it can depend on
822 the output of previous steps. The "data_deps" will be built if the
823 action is built, but may not have completed before all steps of the
824 action are started. This can give additional parallelism in the build
825 for runtime-only dependencies.
832 The script will be executed with the given arguments with the current
833 directory being that of the root build directory. If you pass files
834 to your script, see "gn help rebase_path" for how to convert
835 file names to be relative to the build directory (file names in the
836 sources, outputs, and inputs will be all treated as relative to the
837 current build file and converted as needed automatically).
841 ### **File name handling**
844 All output files must be inside the output directory of the build.
845 You would generally use |$target_out_dir| or |$target_gen_dir| to
846 reference the output or generated intermediate file directories,
854 args, data, data_deps, depfile, deps, outputs*, script*,
863 # Runs the script over each IDL file. The IDL script will generate
864 # both a .cc and a .h file for each input.
865 action_foreach("my_idl") {
866 script = "idl_processor.py"
867 sources = [ "foo.idl", "bar.idl" ]
869 # Our script reads this file each time, so we need to list is as a
870 # dependency so we can rebuild if it changes.
871 inputs = [ "my_configuration.txt" ]
873 # Transformation from source file name to output file names.
874 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
875 "$target_gen_dir/{{source_name_part}}.cc" ]
877 # Note that since "args" is opaque to GN, if you specify paths
878 # here, you will need to convert it to be relative to the build
879 # directory using "rebase_path()".
883 rebase_path(relative_target_gen_dir, root_build_dir) +
884 "/{{source_name_part}}.h" ]
890 ## **assert**: Assert an expression is true at generation time.
893 assert(<condition> [, <error string>])
895 If the condition is false, the build will fail with an error. If the
896 optional second argument is provided, that string will be printed
897 with the error message.
904 assert(defined(sources), "Sources must be defined")
908 ## **config**: Defines a configuration object.
911 Configuration objects can be applied to targets and specify sets of
912 compiler flags, includes, defines, etc. They provide a way to
913 conveniently group sets of this configuration information.
915 A config is referenced by its label just like a target.
917 The values in a config are additive only. If you want to remove a flag
918 you need to remove the corresponding config that sets it. The final
919 set of flags, defines, etc. for a target is generated in this order:
921 1. The values specified directly on the target (rather than using a
923 2. The configs specified in the target's "configs" list, in order.
924 3. Public_configs from a breadth-first traversal of the dependency
925 tree in the order that the targets appear in "deps".
926 4. All dependent configs from a breadth-first traversal of the
927 dependency tree in the order that the targets appear in "deps".
931 ### **Variables valid in a config definition**:
933 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
934 defines, include_dirs, ldflags, lib_dirs, libs
938 ### **Variables on a target used to apply configs**:
940 all_dependent_configs, configs, public_configs,
941 forward_dependent_configs_from
948 includes = [ "include/common" ]
949 defines = [ "ENABLE_DOOM_MELON" ]
952 executable("mything") {
953 configs = [ ":myconfig" ]
958 ## **copy**: Declare a target that copies files.
960 ### **File name handling**
963 All output files must be inside the output directory of the build.
964 You would generally use |$target_out_dir| or |$target_gen_dir| to
965 reference the output or generated intermediate file directories,
968 Both "sources" and "outputs" must be specified. Sources can
969 as many files as you want, but there can only be one item in the
970 outputs list (plural is used for the name for consistency with
973 If there is more than one source file, your output name should specify
974 a mapping from each source files to output file names using source
975 expansion (see "gn help source_expansion"). The placeholders will
976 will look like "{{source_name_part}}", for example.
983 # Write a rule that copies a checked-in DLL to the output directory.
985 sources = [ "mydll.dll" ]
986 outputs = [ "$target_out_dir/mydll.dll" ]
989 # Write a rule to copy several files to the target generated files
992 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
994 # Use source expansion to generate output files with the
995 # corresponding file names in the gen dir. This will just copy each
997 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1002 ## **declare_args**: Declare build arguments.
1005 Introduces the given arguments into the current scope. If they are
1006 not specified on the command line or in a toolchain's arguments,
1007 the default values given in the declare_args block will be used.
1008 However, these defaults will not override command-line values.
1010 See also "gn help buildargs" for an overview.
1017 enable_teleporter = true
1018 enable_doom_melon = false
1021 If you want to override the (default disabled) Doom Melon:
1022 gn --args="enable_doom_melon=true enable_teleporter=false"
1023 This also sets the teleporter, but it's already defaulted to on so
1024 it will have no effect.
1028 ## **defined**: Returns whether an identifier is defined.
1031 Returns true if the given argument is defined. This is most useful in
1032 templates to assert that the caller set things up properly.
1034 You can pass an identifier:
1036 which will return true or false depending on whether foo is defined in
1039 You can also check a named scope:
1041 which will return true or false depending on whether bar is defined in
1042 the named scope foo. It will throw an error if foo is not defined or
1050 template("mytemplate") {
1051 # To help users call this template properly...
1052 assert(defined(invoker.sources), "Sources must be defined")
1054 # If we want to accept an optional "values" argument, we don't
1055 # want to dereference something that may not be defined.
1056 if (defined(invoker.values)) {
1057 values = invoker.values
1059 values = "some default value"
1065 ## **exec_script**: Synchronously run a script and return the output.
1068 exec_script(filename,
1070 input_conversion = "",
1071 file_dependencies = [])
1073 Runs the given script, returning the stdout of the script. The build
1074 generation will fail if the script does not exist or returns a nonzero
1077 The current directory when executing the script will be the root
1078 build directory. If you are passing file names, you will want to use
1079 the rebase_path() function to make file names relative to this
1080 path (see "gn help rebase_path").
1088 File name of python script to execute. Non-absolute names will
1089 be treated as relative to the current build file.
1092 A list of strings to be passed to the script as arguments.
1093 May be unspecified or the empty list which means no arguments.
1096 Controls how the file is read and parsed.
1097 See "gn help input_conversion".
1099 If unspecified, defaults to the empty string which causes the
1100 script result to be discarded. exec script will return None.
1103 (Optional) A list of files that this script reads or otherwise
1104 depends on. These dependencies will be added to the build result
1105 such that if any of them change, the build will be regenerated and
1106 the script will be re-run.
1108 The script itself will be an implicit dependency so you do not
1116 all_lines = exec_script(
1117 "myscript.py", [some_input], "list lines",
1118 [ rebase_path("data_file.txt", root_build_dir) ])
1120 # This example just calls the script with no arguments and discards
1122 exec_script("//foo/bar/myscript.py")
1126 ## **executable**: Declare an executable target.
1131 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1132 defines, include_dirs, ldflags, lib_dirs, libs
1133 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1134 Dependent configs: all_dependent_configs, public_configs
1135 General: check_includes, configs, data, inputs, output_name,
1136 output_extension, public, sources, testonly, visibility
1140 ## **foreach**: Iterate over a list.
1143 foreach(<loop_var>, <list>) {
1147 Executes the loop contents block over each item in the list,
1148 assigning the loop_var to each item in sequence.
1150 The block does not introduce a new scope, so that variable assignments
1151 inside the loop will be visible once the loop terminates.
1153 The loop variable will temporarily shadow any existing variables with
1154 the same name for the duration of the loop. After the loop terminates
1155 the loop variable will no longer be in scope, and the previous value
1156 (if any) will be restored.
1163 mylist = [ "a", "b", "c" ]
1164 foreach(i, mylist) {
1175 ## **get_label_info**: Get an attribute from a target's label.
1178 get_label_info(target_label, what)
1180 Given the label of a target, returns some attribute of that target.
1181 The target need not have been previously defined in the same file,
1182 since none of the attributes depend on the actual target definition,
1183 only the label itself.
1185 See also "gn help get_target_outputs".
1189 ### **Possible values for the "what" parameter**
1193 The short name of the target. This will match the value of the
1194 "target_name" variable inside that target's declaration. For the
1195 label "//foo/bar:baz" this will return "baz".
1198 The directory containing the target's definition, with no slash at
1199 the end. For the label "//foo/bar:baz" this will return
1203 The generated file directory for the target. This will match the
1204 value of the "target_gen_dir" variable when inside that target's
1208 The root of the generated file tree for the target. This will
1209 match the value of the "root_gen_dir" variable when inside that
1210 target's declaration.
1213 The output directory for the target. This will match the
1214 value of the "target_out_dir" variable when inside that target's
1218 The root of the output file tree for the target. This will
1219 match the value of the "root_gen_dir" variable when inside that
1220 target's declaration.
1222 "label_no_toolchain"
1223 The fully qualified version of this label, not including the
1224 toolchain. For the input ":bar" it might return
1227 "label_with_toolchain"
1228 The fully qualified version of this label, including the
1229 toolchain. For the input ":bar" it might return
1230 "//foo:bar(//toolchain:x64)".
1233 The label of the toolchain. This will match the value of the
1234 "current_toolchain" variable when inside that target's
1242 get_label_info(":foo", "name")
1243 # Returns string "foo".
1245 get_label_info("//foo/bar:baz", "gen_dir")
1246 # Returns string "//out/Debug/gen/foo/bar".
1250 ## **get_path_info**: Extract parts of a file or directory name.
1253 get_path_info(input, what)
1255 The first argument is either a string representing a file or
1256 directory name, or a list of such strings. If the input is a list
1257 the return value will be a list containing the result of applying the
1258 rule to each item in the input.
1262 ### **Possible values for the "what" parameter**
1266 The substring after the last slash in the path, including the name
1267 and extension. If the input ends in a slash, the empty string will
1269 "foo/bar.txt" => "bar.txt"
1270 "bar.txt" => "bar.txt"
1275 The substring of the file name not including the extension.
1276 "foo/bar.txt" => "bar"
1281 The substring following the last period following the last slash,
1282 or the empty string if not found. The period is not included.
1283 "foo/bar.txt" => "txt"
1287 The directory portion of the name, not including the slash.
1288 "foo/bar.txt" => "foo"
1289 "//foo/bar" => "//foo"
1292 The result will never end in a slash, so if the resulting
1293 is empty, the system ("/") or source ("//") roots, a "."
1294 will be appended such that it is always legal to append a slash
1295 and a filename and get a valid path.
1298 The output file directory corresponding to the path of the
1299 given file, not including a trailing slash.
1300 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
1302 The generated file directory corresponding to the path of the
1303 given file, not including a trailing slash.
1304 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
1307 The full absolute path name to the file or directory. It will be
1308 resolved relative to the currebt directory, and then the source-
1309 absolute version will be returned. If the input is system-
1310 absolute, the same input will be returned.
1311 "foo/bar.txt" => "//mydir/foo/bar.txt"
1312 "foo/" => "//mydir/foo/"
1313 "//foo/bar" => "//foo/bar" (already absolute)
1314 "/usr/include" => "/usr/include" (already absolute)
1316 If you want to make the path relative to another directory, or to
1317 be system-absolute, see rebase_path().
1323 sources = [ "foo.cc", "foo.h" ]
1324 result = get_path_info(source, "abspath")
1325 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
1327 result = get_path_info("//foo/bar/baz.cc", "dir")
1328 # result will be "//foo/bar"
1330 # Extract the source-absolute directory name,
1331 result = get_path_info(get_path_info(path, "dir"), "abspath")
1335 ## **get_target_outputs**: [file list] Get the list of outputs from a target.
1338 get_target_outputs(target_label)
1340 Returns a list of output files for the named target. The named target
1341 must have been previously defined in the current file before this
1342 function is called (it can't reference targets in other files because
1343 there isn't a defined execution order, and it obviously can't
1344 reference targets that are defined after the function call).
1346 Only copy and action targets are supported. The outputs from binary
1347 targets will depend on the toolchain definition which won't
1348 necessarily have been loaded by the time a given line of code has run,
1349 and source sets and groups have no useful output file.
1353 ### **Return value**
1356 The names in the resulting list will be absolute file paths (normally
1357 like "//out/Debug/bar.exe", depending on the build directory).
1359 action targets: this will just return the files specified in the
1360 "outputs" variable of the target.
1362 action_foreach targets: this will return the result of applying
1363 the output template to the sources (see "gn help source_expansion").
1364 This will be the same result (though with guaranteed absolute file
1365 paths), as process_file_template will return for those inputs
1366 (see "gn help process_file_template").
1368 binary targets (executables, libraries): this will return a list
1369 of the resulting binary file(s). The "main output" (the actual
1370 binary or library) will always be the 0th element in the result.
1371 Depending on the platform and output type, there may be other output
1372 files as well (like import libraries) which will follow.
1374 source sets and groups: this will return a list containing the path of
1375 the "stamp" file that Ninja will produce once all outputs are
1376 generated. This probably isn't very useful.
1383 # Say this action generates a bunch of C source files.
1384 action_foreach("my_action") {
1389 # Compile the resulting source files into a source set.
1390 source_set("my_lib") {
1391 sources = get_target_outputs(":my_action")
1396 ## **getenv**: Get an environment variable.
1399 value = getenv(env_var_name)
1401 Returns the value of the given enironment variable. If the value is
1402 not found, it will try to look up the variable with the "opposite"
1403 case (based on the case of the first letter of the variable), but
1404 is otherwise case-sensitive.
1406 If the environment variable is not found, the empty string will be
1407 returned. Note: it might be nice to extend this if we had the concept
1408 of "none" in the language to indicate lookup failure.
1415 home_dir = getenv("HOME")
1419 ## **group**: Declare a named group of targets.
1422 This target type allows you to create meta-targets that just collect a
1423 set of dependencies into one named target. Groups can additionally
1424 specify configs that apply to their dependents.
1426 Depending on a group is exactly like depending directly on that
1427 group's deps. Direct dependent configs will get automatically
1428 forwarded through the group so you shouldn't need to use
1429 "forward_dependent_configs_from.
1436 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1437 Dependent configs: all_dependent_configs, public_configs
1447 "//project:unit_tests",
1453 ## **import**: Import a file into the current scope.
1456 The import command loads the rules and variables resulting from
1457 executing the given file into the current scope.
1459 By convention, imported files are named with a .gni extension.
1461 An import is different than a C++ "include". The imported file is
1462 executed in a standalone environment from the caller of the import
1463 command. The results of this execution are cached for other files that
1464 import the same .gni file.
1466 Note that you can not import a BUILD.gn file that's otherwise used
1467 in the build. Files must either be imported or implicitly loaded as
1468 a result of deps rules, but not both.
1470 The imported file's scope will be merged with the scope at the point
1471 import was called. If there is a conflict (both the current scope and
1472 the imported file define some variable or rule with the same name but
1473 different value), a runtime error will be thrown. Therefore, it's good
1474 practice to minimize the stuff that an imported file defines.
1476 Variables and templates beginning with an underscore '_' are
1477 considered private and will not be imported. Imported files can use
1478 such variables for internal computation without affecting other files.
1485 import("//build/rules/idl_compilation_rule.gni")
1487 # Looks in the current directory.
1488 import("my_vars.gni")
1492 ## **print**: Prints to the console.
1495 Prints all arguments to the console separated by spaces. A newline is
1496 automatically appended to the end.
1498 This function is intended for debugging. Note that build files are run
1499 in parallel so you may get interleaved prints. A buildfile may also
1500 be executed more than once in parallel in the context of different
1501 toolchains so the prints from one file may be duplicated or
1502 interleaved with itself.
1508 print("Hello world")
1510 print(sources, deps)
1514 ## **process_file_template**: Do template expansion over a list of files.
1517 process_file_template(source_list, template)
1519 process_file_template applies a template list to a source file list,
1520 returning the result of applying each template to each source. This is
1521 typically used for computing output file names from input files.
1523 In most cases, get_target_outputs() will give the same result with
1524 shorter, more maintainable code. This function should only be used
1525 when that function can't be used (like there's no target or the target
1526 is defined in another build file).
1533 The source_list is a list of file names.
1535 The template can be a string or a list. If it is a list, multiple
1536 output strings are generated for each input.
1538 The template should contain source expansions to which each name in
1539 the source list is applied. See "gn help source_expansion".
1550 myoutputs = process_file_template(
1552 [ "$target_gen_dir/{{source_name_part}}.cc",
1553 "$target_gen_dir/{{source_name_part}}.h" ])
1555 The result in this case will be:
1556 [ "//out/Debug/foo.cc"
1558 "//out/Debug/bar.cc"
1559 "//out/Debug/bar.h" ]
1563 ## **read_file**: Read a file into a variable.
1566 read_file(filename, input_conversion)
1568 Whitespace will be trimmed from the end of the file. Throws an error
1569 if the file can not be opened.
1577 Filename to read, relative to the build file.
1580 Controls how the file is read and parsed.
1581 See "gn help input_conversion".
1587 lines = read_file("foo.txt", "list lines")
1591 ## **rebase_path**: Rebase a file or directory to another location.
1594 converted = rebase_path(input,
1598 Takes a string argument representing a file name, or a list of such
1599 strings and converts it/them to be relative to a different base
1602 When invoking the compiler or scripts, GN will automatically convert
1603 sources and include directories to be relative to the build directory.
1604 However, if you're passing files directly in the "args" array or
1605 doing other manual manipulations where GN doesn't know something is
1606 a file name, you will need to convert paths to be relative to what
1607 your tool is expecting.
1609 The common case is to use this to convert paths relative to the
1610 current directory to be relative to the build directory (which will
1611 be the current directory when executing scripts).
1613 If you want to convert a file path to be source-absolute (that is,
1614 beginning with a double slash like "//foo/bar"), you should use
1615 the get_path_info() function. This function won't work because it will
1616 always make relative paths, and it needs to support making paths
1617 relative to the source root, so can't also generate source-absolute
1618 paths without more special-cases.
1626 A string or list of strings representing file or directory names
1627 These can be relative paths ("foo/bar.txt"), system absolute
1628 paths ("/foo/bar.txt"), or source absolute paths
1632 The directory to convert the paths to be relative to. This can be
1633 an absolute path or a relative path (which will be treated
1634 as being relative to the current BUILD-file's directory).
1636 As a special case, if new_base is the empty string (the default),
1637 all paths will be converted to system-absolute native style paths
1638 with system path separators. This is useful for invoking external
1642 Directory representing the base for relative paths in the input.
1643 If this is not an absolute path, it will be treated as being
1644 relative to the current build file. Use "." (the default) to
1645 convert paths from the current BUILD-file's directory.
1649 ### **Return value**
1652 The return value will be the same type as the input value (either a
1653 string or a list of strings). All relative and source-absolute file
1654 names will be converted to be relative to the requested output
1655 System-absolute paths will be unchanged.
1662 # Convert a file in the current directory to be relative to the build
1663 # directory (the current dir when executing compilers and scripts).
1664 foo = rebase_path("myfile.txt", root_build_dir)
1665 # might produce "../../project/myfile.txt".
1667 # Convert a file to be system absolute:
1668 foo = rebase_path("myfile.txt")
1669 # Might produce "D:\source\project\myfile.txt" on Windows or
1670 # "/home/you/source/project/myfile.txt" on Linux.
1672 # Typical usage for converting to the build directory for a script.
1673 action("myscript") {
1674 # Don't convert sources, GN will automatically convert these to be
1675 # relative to the build directory when it constructs the command
1676 # line for your script.
1677 sources = [ "foo.txt", "bar.txt" ]
1679 # Extra file args passed manually need to be explicitly converted
1680 # to be relative to the build directory:
1683 rebase_path("//mything/data/input.dat", root_build_dir),
1685 rebase_path("relative_path.txt", root_build_dir)
1686 ] + rebase_path(sources, root_build_dir)
1691 ## **set_default_toolchain**: Sets the default toolchain name.
1694 set_default_toolchain(toolchain_label)
1696 The given label should identify a toolchain definition (see
1697 "help toolchain"). This toolchain will be used for all targets
1698 unless otherwise specified.
1700 This function is only valid to call during the processing of the build
1701 configuration file. Since the build configuration file is processed
1702 separately for each toolchain, this function will be a no-op when
1703 called under any non-default toolchains.
1705 For example, the default toolchain should be appropriate for the
1706 current environment. If the current environment is 32-bit and
1707 somebody references a target with a 64-bit toolchain, we wouldn't
1708 want processing of the build config file for the 64-bit toolchain to
1709 reset the default toolchain to 64-bit, we want to keep it 32-bits.
1724 set_default_toolchain("//build/config/win:vs32")
1727 ## **set_defaults**: Set default values for a target type.
1730 set_defaults(<target_type_name>) { <values...> }
1732 Sets the default values for a given target type. Whenever
1733 target_type_name is seen in the future, the values specified in
1734 set_default's block will be copied into the current scope.
1736 When the target type is used, the variable copying is very strict.
1737 If a variable with that name is already in scope, the build will fail
1740 set_defaults can be used for built-in target types ("executable",
1741 "shared_library", etc.) and custom ones defined via the "template"
1748 set_defaults("static_library") {
1749 configs = [ "//tools/mything:settings" ]
1752 static_library("mylib")
1753 # The configs will be auto-populated as above. You can remove it if
1754 # you don't want the default for a particular default:
1755 configs -= "//tools/mything:settings"
1760 ## **set_sources_assignment_filter**: Set a pattern to filter source files.
1763 The sources assignment filter is a list of patterns that remove files
1764 from the list implicitly whenever the "sources" variable is
1765 assigned to. This is intended to be used to globally filter out files
1766 with platform-specific naming schemes when they don't apply, for
1767 example, you may want to filter out all "*_win.cc" files on non-
1770 Typically this will be called once in the master build config script
1771 to set up the filter for the current platform. Subsequent calls will
1772 overwrite the previous values.
1774 If you want to bypass the filter and add a file even if it might
1775 be filtered out, call set_sources_assignment_filter([]) to clear the
1776 list of filters. This will apply until the current scope exits
1780 ### **How to use patterns**
1783 File patterns are VERY limited regular expressions. They must match
1784 the entire input string to be counted as a match. In regular
1785 expression parlance, there is an implicit "^...$" surrounding your
1786 input. If you want to match a substring, you need to use wildcards at
1787 the beginning and end.
1789 There are only two special tokens understood by the pattern matcher.
1790 Everything else is a literal.
1792 * Matches zero or more of any character. It does not depend on the
1793 preceding character (in regular expression parlance it is
1794 equivalent to ".*").
1796 \b Matches a path boundary. This will match the beginning or end of
1797 a string, or a slash.
1801 ### **Pattern examples**
1805 Matches a string containing "asdf" anywhere.
1808 Matches only the exact string "asdf".
1811 Matches strings ending in the literal ".cc".
1814 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
1818 ### **Sources assignment example**
1821 # Filter out all _win files.
1822 set_sources_assignment_filter([ "*_win.cc", "*_win.h" ])
1823 sources = [ "a.cc", "b_win.cc" ]
1825 # Will print [ "a.cc" ]. b_win one was filtered out.
1829 ## **shared_library**: Declare a shared library target.
1832 A shared library will be specified on the linker line for targets
1833 listing the shared library in its "deps". If you don't want this
1834 (say you dynamically load the library at runtime), then you should
1835 depend on the shared library via "data_deps" instead.
1842 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1843 defines, include_dirs, ldflags, lib_dirs, libs
1844 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1845 Dependent configs: all_dependent_configs, public_configs
1846 General: check_includes, configs, data, inputs, output_name,
1847 output_extension, public, sources, testonly, visibility
1851 ## **source_set**: Declare a source set target.
1854 A source set is a collection of sources that get compiled, but are not
1855 linked to produce any kind of library. Instead, the resulting object
1856 files are implicitly added to the linker line of all targets that
1857 depend on the source set.
1859 In most cases, a source set will behave like a static library, except
1860 no actual library file will be produced. This will make the build go
1861 a little faster by skipping creation of a large static library, while
1862 maintaining the organizational benefits of focused build targets.
1864 The main difference between a source set and a static library is
1865 around handling of exported symbols. Most linkers assume declaring
1866 a function exported means exported from the static library. The linker
1867 can then do dead code elimination to delete code not reachable from
1870 A source set will not do this code elimination since there is no link
1871 step. This allows you to link many sources sets into a shared library
1872 and have the "exported symbol" notation indicate "export from the
1873 final shared library and not from the intermediate targets." There is
1874 no way to express this concept when linking multiple static libraries
1875 into a shared library.
1882 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1883 defines, include_dirs, ldflags, lib_dirs, libs
1884 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1885 Dependent configs: all_dependent_configs, public_configs
1886 General: check_includes, configs, data, inputs, output_name,
1887 output_extension, public, sources, testonly, visibility
1891 ## **static_library**: Declare a static library target.
1894 Make a ".a" / ".lib" file.
1896 If you only need the static library for intermediate results in the
1897 build, you should consider a source_set instead since it will skip
1898 the (potentially slow) step of creating the intermediate library file.
1905 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1906 defines, include_dirs, ldflags, lib_dirs, libs
1907 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1908 Dependent configs: all_dependent_configs, public_configs
1909 General: check_includes, configs, data, inputs, output_name,
1910 output_extension, public, sources, testonly, visibility
1914 ## **template**: Define a template rule.
1917 A template defines a custom name that acts like a function. It
1918 provides a way to add to the built-in target types.
1920 The template() function is used to declare a template. To invoke the
1921 template, just use the name of the template like any other target
1924 Often you will want to declare your template in a special file that
1925 other files will import (see "gn help import") so your template
1926 rule can be shared across build files.
1930 ### **More details**:
1933 When you call template() it creates a closure around all variables
1934 currently in scope with the code in the template block. When the
1935 template is invoked, the closure will be executed.
1937 When the template is invoked, the code in the caller is executed and
1938 passed to the template code as an implicit "invoker" variable. The
1939 template uses this to read state out of the invoking code.
1941 One thing explicitly excluded from the closure is the "current
1942 directory" against which relative file names are resolved. The
1943 current directory will be that of the invoking code, since typically
1944 that code specifies the file names. This means all files internal
1945 to the template should use absolute names.
1949 ### **Target naming**:
1952 Your template should almost always define a built-in target with the
1953 name the template invoker specified. For example, if you have an IDL
1954 template and somebody does:
1956 you will normally want this to expand to something defining a
1957 source_set or static_library named "foo" (among other things you may
1958 need). This way, when another target specifies a dependency on
1959 "foo", the static_library or source_set will be linked.
1961 It is also important that any other targets your template expands to
1962 have globally unique names, or you will get collisions.
1964 Access the invoking name in your template via the implicit
1965 "target_name" variable. This should also be the basis of how other
1966 targets that a template expands to to ensure uniquness.
1968 A typical example would be a template that defines an action to
1969 generate some source files, and a source_set to compile that source.
1970 Your template would name the source_set "target_name" because
1971 that's what you want external targets to depend on to link your code.
1972 And you would name the action something like "${target_name}_action"
1973 to make it unique. The source set would have a dependency on the
1974 action to make it run.
1978 ### **Example of defining a template**:
1981 template("my_idl") {
1982 # Be nice and help callers debug problems by checking that the
1983 # variables the template requires are defined. This gives a nice
1984 # message rather than giving the user an error about an
1985 # undefined variable in the file defining the template
1987 # You can also use defined() to give default values to variables
1988 # unspecified by the invoker.
1989 assert(defined(invoker.sources),
1990 "Need sources in $target_name listing the idl files.")
1992 # Name of the intermediate target that does the code gen. This must
1993 # incorporate the target name so it's unique across template
1995 code_gen_target_name = target_name + "_code_gen"
1997 # Intermediate target to convert IDL to C source. Note that the name
1998 # is based on the name the invoker of the template specified. This
1999 # way, each time the template is invoked we get a unique
2000 # intermediate action name (since all target names are in the global
2002 action_foreach(code_gen_target_name) {
2003 # Access the scope defined by the invoker via the implicit
2004 # "invoker" variable.
2005 sources = invoker.sources
2007 # Note that we need an absolute path for our script file name.
2008 # The current directory when executing this code will be that of
2009 # the invoker (this is why we can use the "sources" directly
2010 # above without having to rebase all of the paths). But if we need
2011 # to reference a script relative to the template file, we'll need
2012 # to use an absolute path instead.
2013 script = "//tools/idl/idl_code_generator.py"
2015 # Tell GN how to expand output names given the sources.
2016 # See "gn help source_expansion" for more.
2017 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
2018 "$target_gen_dir/{{source_name_part}}.h" ]
2021 # Name the source set the same as the template invocation so
2022 # instancing this template produces something that other targets
2023 # can link to in their deps.
2024 source_set(target_name) {
2025 # Generates the list of sources, we get these from the
2026 # action_foreach above.
2027 sources = get_target_outputs(":$code_gen_target_name")
2029 # This target depends on the files produced by the above code gen
2031 deps = [ ":$code_gen_target_name" ]
2037 ### **Example of invoking the resulting template**:
2040 # This calls the template code above, defining target_name to be
2041 # "foo_idl_files" and "invoker" to be the set of stuff defined in
2042 # the curly brackets.
2043 my_idl("foo_idl_files") {
2044 # Goes into the template as "invoker.sources".
2045 sources = [ "foo.idl", "bar.idl" ]
2048 # Here is a target that depends on our template.
2049 executable("my_exe") {
2050 # Depend on the name we gave the template call above. Internally,
2051 # this will produce a dependency from executable to the source_set
2052 # inside the template (since it has this name), which will in turn
2053 # depend on the code gen action.
2054 deps = [ ":foo_idl_files" ]
2059 ## **tool**: Specify arguments to a toolchain tool.
2076 "objc": Objective C compiler
2077 "objcxx": Objective C++ compiler
2078 "rc": Resource compiler (Windows .rc files)
2082 "alink": Linker for static libraries (archives)
2083 "solink": Linker for shared libraries
2084 "link": Linker for executables
2087 "stamp": Tool for creating stamp files
2088 "copy": Tool to copy files.
2092 ### **Tool variables**
2095 command [string with substitutions]
2096 Valid for: all tools (required)
2100 default_output_extension [string]
2101 Valid for: linker tools
2103 Extension for the main output of a linkable tool. It includes
2104 the leading dot. This will be the default value for the
2105 {{output_extension}} expansion (discussed below) but will be
2106 overridden by by the "output extension" variable in a target,
2107 if one is specified. Empty string means no extension.
2109 GN doesn't actually do anything with this extension other than
2110 pass it along, potentially with target-specific overrides. One
2111 would typically use the {{output_extension}} value in the
2112 "outputs" to read this value.
2114 Example: default_output_extension = ".exe"
2117 Valid for: compiler tools (optional)
2119 If the tool can write ".d" files, this specifies the name of
2120 the resulting file. These files are used to list header file
2121 dependencies (or other implicit input dependencies) that are
2122 discovered at build time. See also "depsformat".
2124 Example: depfile = "{{output}}.d"
2127 Valid for: compiler tools (when depfile is specified)
2129 Format for the deps outputs. This is either "gcc" or "msvc".
2130 See the ninja documentation for "deps" for more information.
2132 Example: depsformat = "gcc"
2134 description [string with substitutions, optional]
2135 Valid for: all tools
2137 What to print when the command is run.
2139 Example: description = "Compiling {{source}}"
2141 lib_switch [string, optional, link tools only]
2142 lib_dir_switch [string, optional, link tools only]
2143 Valid for: Linker tools except "alink"
2145 These strings will be prepended to the libraries and library
2146 search directories, respectively, because linkers differ on how
2147 specify them. If you specified:
2149 lib_dir_switch = "-L"
2150 then the "{{libs}}" expansion for [ "freetype", "expat"]
2151 would be "-lfreetype -lexpat".
2153 outputs [list of strings with substitutions]
2154 Valid for: Linker and compiler tools (required)
2156 An array of names for the output files the tool produces. These
2157 are relative to the build output directory. There must always be
2158 at least one output file. There can be more than one output (a
2159 linker might produce a library and an import library, for
2162 This array just declares to GN what files the tool will
2163 produce. It is your responsibility to specify the tool command
2164 that actually produces these files.
2166 If you specify more than one output for shared library links,
2167 you should consider setting link_output and depend_output.
2168 Otherwise, the first entry in the outputs list should always be
2169 the main output which will be linked to.
2171 Example for a compiler tool that produces .obj files:
2173 "{{source_out_dir}}/{{source_name_part}}.obj"
2176 Example for a linker tool that produces a .dll and a .lib. The
2177 use of {{output_extension}} rather than hardcoding ".dll"
2178 allows the extension of the library to be overridden on a
2179 target-by-target basis, but in this example, it always
2180 produces a ".lib" import library:
2182 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2183 "{{root_out_dir}}/{{target_output_name}}.lib",
2186 link_output [string with substitutions]
2187 depend_output [string with substitutions]
2188 Valid for: "solink" only (optional)
2190 These two files specify whch of the outputs from the solink
2191 tool should be used for linking and dependency tracking. These
2192 should match entries in the "outputs". If unspecified, the
2193 first item in the "outputs" array will be used for both. See
2194 "Separate linking and dependencies for shared libraries"
2197 On Windows, where the tools produce a .dll shared library and
2198 a .lib import library, you will want both of these to be the
2199 import library. On Linux, if you're not doing the separate
2200 linking/dependency optimization, both of these should be the
2203 output_prefix [string]
2204 Valid for: Linker tools (optional)
2206 Prefix to use for the output name. Defaults to empty. This
2207 prefix will be prepended to the name of the target (or the
2208 output_name if one is manually specified for it) if the prefix
2209 is not already there. The result will show up in the
2210 {{output_name}} substitution pattern.
2212 This is typically used to prepend "lib" to libraries on
2214 output_prefix = "lib"
2217 Valid for: all tools (optional, defaults to false)
2219 Requests that Ninja check the file timestamp after this tool has
2220 run to determine if anything changed. Set this if your tool has
2221 the ability to skip writing output if the output file has not
2224 Normally, Ninja will assume that when a tool runs the output
2225 be new and downstream dependents must be rebuild. When this is
2226 set to trye, Ninja can skip rebuilding downstream dependents for
2227 input changes that don't actually affect the output.
2232 rspfile [string with substitutions]
2233 Valid for: all tools (optional)
2235 Name of the response file. If empty, no response file will be
2236 used. See "rspfile_content".
2238 rspfile_content [string with substitutions]
2239 Valid for: all tools (required when "rspfile" is specified)
2241 The contents to be written to the response file. This may
2242 include all or part of the command to send to the tool which
2243 allows you to get around OS command-line length limits.
2245 This example adds the inputs and libraries to a response file,
2246 but passes the linker flags directly on the command line:
2248 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
2249 rspfile = "{{output}}.rsp"
2250 rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
2255 ### **Expansions for tool variables**
2257 All paths are relative to the root build directory, which is the
2258 current directory for running all tools. These expansions are
2259 available to all tools:
2262 The label of the current target. This is typically used in the
2263 "description" field for link tools. The toolchain will be
2264 omitted from the label for targets in the default toolchain, and
2265 will be included for targets in other toolchains.
2268 The relative path and name of the output(s) of the current
2269 build step. If there is more than one output, this will expand
2270 to a list of all of them.
2271 Example: "out/base/my_file.o"
2275 The directory of the generated file and output directories,
2276 respectively, for the current target. There is no trailing
2278 Example: "out/base/test"
2280 {{target_output_name}}
2281 The short name of the current target with no path information,
2282 or the value of the "output_name" variable if one is specified
2283 in the target. This will include the "output_prefix" if any.
2284 Example: "libfoo" for the target named "foo" and an
2285 output prefix for the linker tool of "lib".
2287 Compiler tools have the notion of a single input and a single output,
2288 along with a set of compiler-specific flags. The following expansions
2298 Strings correspond that to the processed flags/defines/include
2299 directories specified for the target.
2300 Example: "--enable-foo --enable-bar"
2302 Defines will be prefixed by "-D" and include directories will
2303 be prefixed by "-I" (these work with Posix tools as well as
2307 The relative path and name of the current input file.
2308 Example: "../../base/my_file.cc"
2310 {{source_file_part}}
2311 The file part of the source including the extension (with no
2312 directory information).
2315 {{source_name_part}}
2316 The filename part of the source file with no directory or
2322 The directory in the generated file and output directories,
2323 respectively, for the current input file. If the source file
2324 is in the same directory as the target is declared in, they will
2325 will be the same as the "target" versions above.
2326 Example: "gen/base/test"
2328 Linker tools have multiple inputs and (potentially) multiple outputs
2329 The following expansions are available:
2333 Expands to the inputs to the link step. This will be a list of
2334 object files and static libraries.
2335 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
2337 The "_newline" version will separate the input files with
2338 newlines instead of spaces. This is useful in response files:
2339 some linkers can take a "-filelist" flag which expects newline
2340 separated files, and some Microsoft tools have a fixed-sized
2341 buffer for parsing each line of a response file.
2344 Expands to the processed set of ldflags and library search paths
2345 specified for the target.
2346 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
2349 Expands to the list of system libraries to link to. Each will
2350 be prefixed by the "lib_prefix".
2352 As a special case to support Mac, libraries with names ending in
2353 ".framework" will be added to the {{libs}} with "-framework"
2354 preceeding it, and the lib prefix will be ignored.
2356 Example: "-lfoo -lbar"
2358 {{output_extension}}
2359 The value of the "output_extension" variable in the target,
2360 or the value of the "default_output_extension" value in the
2361 tool if the target does not specify an output extension.
2365 Extra libraries from shared library dependencide not specified
2366 in the {{inputs}}. This is the list of link_output files from
2367 shared libraries (if the solink tool specifies a "link_output"
2368 variable separate from the "depend_output").
2370 These should generally be treated the same as libs by your tool.
2371 Example: "libfoo.so libbar.so"
2373 The copy tool allows the common compiler/linker substitutions, plus
2374 {{source}} which is the source of the copy. The stamp tool allows
2375 only the common tool substitutions.
2379 ### **Separate linking and dependencies for shared libraries**
2382 Shared libraries are special in that not all changes to them require
2383 that dependent targets be re-linked. If the shared library is changed
2384 but no imports or exports are different, dependent code needn't be
2385 relinked, which can speed up the build.
2387 If your link step can output a list of exports from a shared library
2388 and writes the file only if the new one is different, the timestamp of
2389 this file can be used for triggering re-links, while the actual shared
2390 library would be used for linking.
2392 You will need to specify
2394 in the linker tool to make this work, so Ninja will detect if the
2395 timestamp of the dependency file has changed after linking (otherwise
2396 it will always assume that running a command updates the output):
2401 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2402 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC",
2405 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
2407 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC"
2416 toolchain("my_toolchain") {
2417 # Put these at the top to apply to all tools below.
2419 lib_dir_prefix = "-L"
2422 command = "gcc {{source}} -o {{output}}"
2423 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2424 description = "GCC {{source}}"
2427 command = "g++ {{source}} -o {{output}}"
2428 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2429 description = "G++ {{source}}"
2435 ## **toolchain**: Defines a toolchain.
2438 A toolchain is a set of commands and build flags used to compile the
2439 source code. You can have more than one toolchain in use at once in
2444 ### **Functions and variables**
2448 The tool() function call specifies the commands commands to run for
2449 a given step. See "gn help tool".
2452 List of arguments to pass to the toolchain when invoking this
2453 toolchain. This applies only to non-default toolchains. See
2454 "gn help toolchain_args" for more.
2457 Dependencies of this toolchain. These dependencies will be resolved
2458 before any target in the toolchain is compiled. To avoid circular
2459 dependencies these must be targets defined in another toolchain.
2461 This is expressed as a list of targets, and generally these targets
2462 will always specify a toolchain:
2463 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
2465 This concept is somewhat inefficient to express in Ninja (it
2466 requires a lot of duplicate of rules) so should only be used when
2467 absolutely necessary.
2470 In integer expressing the number of links that Ninja will perform in
2471 parallel. GN will create a pool for shared library and executable
2472 link steps with this many processes. Since linking is memory- and
2473 I/O-intensive, projects with many large targets may want to limit
2474 the number of parallel steps to avoid overloading the computer.
2475 Since creating static libraries is generally not as intensive
2476 there is no limit to "alink" steps.
2478 Defaults to 0 which Ninja interprets as "no limit".
2480 The value used will be the one from the default toolchain of the
2485 ### **Invoking targets in toolchains**:
2488 By default, when a target depends on another, there is an implicit
2489 toolchain label that is inherited, so the dependee has the same one
2492 You can override this and refer to any other toolchain by explicitly
2493 labeling the toolchain to use. For example:
2494 data_deps = [ "//plugins:mine(//toolchains:plugin_toolchain)" ]
2495 The string "//build/toolchains:plugin_toolchain" is a label that
2496 identifies the toolchain declaration for compiling the sources.
2498 To load a file in an alternate toolchain, GN does the following:
2500 1. Loads the file with the toolchain definition in it (as determined
2501 by the toolchain label).
2502 2. Re-runs the master build configuration file, applying the
2503 arguments specified by the toolchain_args section of the toolchain
2504 definition (see "gn help toolchain_args").
2505 3. Loads the destination build file in the context of the
2506 configuration file in the previous step.
2512 toolchain("plugin_toolchain") {
2513 concurrent_links = 8
2516 command = "gcc {{source}}"
2529 ## **toolchain_args**: Set build arguments for toolchain build setup.
2532 Used inside a toolchain definition to pass arguments to an alternate
2533 toolchain's invocation of the build.
2535 When you specify a target using an alternate toolchain, the master
2536 build configuration file is re-interpreted in the context of that
2537 toolchain (see "gn help toolchain"). The toolchain_args function
2538 allows you to control the arguments passed into this alternate
2539 invocation of the build.
2541 Any default system arguments or arguments passed in on the command-
2542 line will also be passed to the alternate invocation unless explicitly
2543 overridden by toolchain_args.
2545 The toolchain_args will be ignored when the toolchain being defined
2546 is the default. In this case, it's expected you want the default
2549 See also "gn help buildargs" for an overview of these arguments.
2555 toolchain("my_weird_toolchain") {
2558 # Override the system values for a generic Posix system.
2562 # Pass this new value for specific setup for my toolchain.
2563 is_my_weird_system = true
2569 ## **write_file**: Write a file to disk.
2572 write_file(filename, data)
2574 If data is a list, the list will be written one-item-per-line with no
2575 quoting or brackets.
2577 If the file exists and the contents are identical to that being
2578 written, the file will not be updated. This will prevent unnecessary
2579 rebuilds of targets that depend on this file.
2581 TODO(brettw) we probably need an optional third argument to control
2590 Filename to write. This must be within the output directory.
2593 The list or string to write.
2597 ## **current_cpu**: The processor architecture of the current toolchain.
2600 The build configuration usually sets this value based on the value
2601 of "host_cpu" (see "gn help host_cpu") and then threads
2602 this through the toolchain definitions to ensure that it always
2603 reflects the appropriate value.
2605 This value is not used internally by GN for any purpose. It is
2606 set it to the empty string ("") by default but is declared so
2607 that it can be overridden on the command line if so desired.
2609 See "gn help target_cpu" for a list of common values returned.
2613 ## **current_os**: The operating system of the current toolchain.
2616 The build configuration usually sets this value based on the value
2617 of "target_os" (see "gn help target_os"), and then threads this
2618 through the toolchain definitions to ensure that it always reflects
2619 the appropriate value.
2621 This value is not used internally by GN for any purpose. It is
2622 set it to the empty string ("") by default but is declared so
2623 that it can be overridden on the command line if so desired.
2625 See "gn help target_os" for a list of common values returned.
2629 ## **current_toolchain**: Label of the current toolchain.
2632 A fully-qualified label representing the current toolchain. You can
2633 use this to make toolchain-related decisions in the build. See also
2634 "default_toolchain".
2641 if (current_toolchain == "//build:64_bit_toolchain") {
2642 executable("output_thats_64_bit_only") {
2647 ## **default_toolchain**: [string] Label of the default toolchain.
2650 A fully-qualified label representing the default toolchain, which may
2651 not necessarily be the current one (see "current_toolchain").
2655 ## **host_cpu**: The processor architecture that GN is running on.
2658 This is value is exposed so that cross-compile toolchains can
2659 access the host architecture when needed.
2661 The value should generally be considered read-only, but it can be
2662 overriden in order to handle unusual cases where there might
2663 be multiple plausible values for the host architecture (e.g., if
2664 you can do either 32-bit or 64-bit builds). The value is not used
2665 internally by GN for any purpose.
2669 ### **Some possible values**:
2676 ## **host_os**: [string] The operating system that GN is running on.
2679 This value is exposed so that cross-compiles can access the host
2680 build system's settings.
2682 This value should generally be treated as read-only. It, however,
2683 is not used internally by GN for any purpose.
2687 ### **Some possible values**:
2695 ## **python_path**: Absolute path of Python.
2698 Normally used in toolchain definitions if running some command
2699 requires Python. You will normally not need this when invoking scripts
2700 since GN automatically finds it for you.
2704 ## **root_build_dir**: [string] Directory where build commands are run.
2707 This is the root build output directory which will be the current
2708 directory when executing all compilers and scripts.
2710 Most often this is used with rebase_path (see "gn help rebase_path")
2711 to convert arguments to be relative to a script's current directory.
2715 ## **root_gen_dir**: Directory for the toolchain's generated files.
2718 Absolute path to the root of the generated output directory tree for
2719 the current toolchain. An example would be "//out/Debug/gen" for the
2720 default toolchain, or "//out/Debug/arm/gen" for the "arm"
2723 This is primarily useful for setting up include paths for generated
2724 files. If you are passing this to a script, you will want to pass it
2725 through rebase_path() (see "gn help rebase_path") to convert it
2726 to be relative to the build directory.
2728 See also "target_gen_dir" which is usually a better location for
2729 generated files. It will be inside the root generated dir.
2733 ## **root_out_dir**: [string] Root directory for toolchain output files.
2736 Absolute path to the root of the output directory tree for the current
2737 toolchain. It will not have a trailing slash.
2739 For the default toolchain this will be the same as the root_build_dir.
2740 An example would be "//out/Debug" for the default toolchain, or
2741 "//out/Debug/arm" for the "arm" toolchain.
2743 This is primarily useful for setting up script calls. If you are
2744 passing this to a script, you will want to pass it through
2745 rebase_path() (see "gn help rebase_path") to convert it
2746 to be relative to the build directory.
2748 See also "target_out_dir" which is usually a better location for
2749 output files. It will be inside the root output dir.
2756 action("myscript") {
2757 # Pass the output dir to the script.
2758 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
2763 ## **target_cpu**: The desired cpu architecture for the build.
2766 This value should be used to indicate the desired architecture for
2767 the primary objects of the build. It will match the cpu architecture
2768 of the default toolchain.
2770 In many cases, this is the same as "host_cpu", but in the case
2771 of cross-compiles, this can be set to something different. This
2772 value is different from "current_cpu" in that it can be referenced
2773 from inside any toolchain. This value can also be ignored if it is
2774 not needed or meaningful for a project.
2776 This value is not used internally by GN for any purpose, so it
2777 may be set to whatever value is needed for the build.
2778 GN defaults this value to the empty string ("") and the
2779 configuration files should set it to an appropriate value
2780 (e.g., setting it to the value of "host_cpu") if it is not
2781 overridden on the command line or in the args.gn file.
2783 Where practical, use one of the following list of common values:
2787 ### **Possible values**:
2797 ## **target_gen_dir**: Directory for a target's generated files.
2800 Absolute path to the target's generated file directory. This will be
2801 the "root_gen_dir" followed by the relative path to the current
2802 build file. If your file is in "//tools/doom_melon" then
2803 target_gen_dir would be "//out/Debug/gen/tools/doom_melon". It will
2804 not have a trailing slash.
2806 This is primarily useful for setting up include paths for generated
2807 files. If you are passing this to a script, you will want to pass it
2808 through rebase_path() (see "gn help rebase_path") to convert it
2809 to be relative to the build directory.
2811 See also "gn help root_gen_dir".
2818 action("myscript") {
2819 # Pass the generated output dir to the script.
2820 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
2825 ## **target_os**: The desired operating system for the build.
2828 This value should be used to indicate the desired operating system
2829 for the primary object(s) of the build. It will match the OS of
2830 the default toolchain.
2832 In many cases, this is the same as "host_os", but in the case of
2833 cross-compiles, it may be different. This variable differs from
2834 "current_os" in that it can be referenced from inside any
2835 toolchain and will always return the initial value.
2837 This should be set to the most specific value possible. So,
2838 "android" or "chromeos" should be used instead of "linux"
2839 where applicable, even though Android and ChromeOS are both Linux
2840 variants. This can mean that one needs to write
2842 if (target_os == "android" || target_os == "linux") {
2848 This value is not used internally by GN for any purpose, so it
2849 may be set to whatever value is needed for the build.
2850 GN defaults this value to the empty string ("") and the
2851 configuration files should set it to an appropriate value
2852 (e.g., setting it to the value of "host_os") if it is not
2853 set via the command line or in the args.gn file.
2855 Where practical, use one of the following list of common values:
2859 ### **Possible values**:
2871 ## **target_out_dir**: [string] Directory for target output files.
2874 Absolute path to the target's generated file directory. If your
2875 current target is in "//tools/doom_melon" then this value might be
2876 "//out/Debug/obj/tools/doom_melon". It will not have a trailing
2879 This is primarily useful for setting up arguments for calling
2880 scripts. If you are passing this to a script, you will want to pass it
2881 through rebase_path() (see "gn help rebase_path") to convert it
2882 to be relative to the build directory.
2884 See also "gn help root_out_dir".
2891 action("myscript") {
2892 # Pass the output dir to the script.
2893 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
2898 ## **all_dependent_configs**: Configs to be forced on dependents.
2901 A list of config labels.
2903 All targets depending on this one, and recursively, all targets
2904 depending on those, will have the configs listed in this variable
2905 added to them. These configs will also apply to the current target.
2907 This addition happens in a second phase once a target and all of its
2908 dependencies have been resolved. Therefore, a target will not see
2909 these force-added configs in their "configs" variable while the
2910 script is running, and then can not be removed. As a result, this
2911 capability should generally only be used to add defines and include
2912 directories necessary to compile a target's headers.
2914 See also "public_configs".
2918 ### **Ordering of flags and values**:
2921 1. Those set on the current target (not in a config).
2922 2. Those set on the "configs" on the target in order that the
2923 configs appear in the list.
2924 3. Those set on the "all_dependent_configs" on the target in order
2925 that the configs appear in the list.
2926 4. Those set on the "public_configs" on the target in order that
2927 those configs appear in the list.
2928 5. all_dependent_configs pulled from dependencies, in the order of
2929 the "deps" list. This is done recursively. If a config appears
2930 more than once, only the first occurance will be used.
2931 6. public_configs pulled from dependencies, in the order of the
2932 "deps" list. If a dependency is public, they will be applied
2937 ## **allow_circular_includes_from**: Permit includes from deps.
2940 A list of target labels. Must be a subset of the target's "deps".
2941 These targets will be permitted to include headers from the current
2942 target despite the dependency going in the opposite direction.
2946 ### **Tedious exposition**
2949 Normally, for a file in target A to include a file from target B,
2950 A must list B as a dependency. This invariant is enforced by the
2951 "gn check" command (and the --check flag to "gn gen").
2953 Sometimes, two targets might be the same unit for linking purposes
2954 (two source sets or static libraries that would always be linked
2955 together in a final executable or shared library). In this case,
2956 you want A to be able to include B's headers, and B to include A's
2959 This list, if specified, lists which of the dependencies of the
2960 current target can include header files from the current target.
2961 That is, if A depends on B, B can only include headers from A if it is
2962 in A's allow_circular_includes_from list.
2970 deps = [ ":b", ":c" ]
2971 allow_circular_includes_from = [ ":b" ]
2977 ## **args**: Arguments passed to an action.
2980 For action and action_foreach targets, args is the list of arguments
2981 to pass to the script. Typically you would use source expansion (see
2982 "gn help source_expansion") to insert the source file names.
2984 See also "gn help action" and "gn help action_foreach".
2988 ## **cflags***: Flags passed to the C compiler.
2993 "cflags" are passed to all invocations of the C, C++, Objective C,
2994 and Objective C++ compilers.
2996 To target one of these variants individually, use "cflags_c",
2997 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
2998 These variant-specific versions will be appended to the "cflags".
3002 ### **Ordering of flags and values**:
3005 1. Those set on the current target (not in a config).
3006 2. Those set on the "configs" on the target in order that the
3007 configs appear in the list.
3008 3. Those set on the "all_dependent_configs" on the target in order
3009 that the configs appear in the list.
3010 4. Those set on the "public_configs" on the target in order that
3011 those configs appear in the list.
3012 5. all_dependent_configs pulled from dependencies, in the order of
3013 the "deps" list. This is done recursively. If a config appears
3014 more than once, only the first occurance will be used.
3015 6. public_configs pulled from dependencies, in the order of the
3016 "deps" list. If a dependency is public, they will be applied
3021 ## **cflags***: Flags passed to the C compiler.
3026 "cflags" are passed to all invocations of the C, C++, Objective C,
3027 and Objective C++ compilers.
3029 To target one of these variants individually, use "cflags_c",
3030 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3031 These variant-specific versions will be appended to the "cflags".
3035 ### **Ordering of flags and values**:
3038 1. Those set on the current target (not in a config).
3039 2. Those set on the "configs" on the target in order that the
3040 configs appear in the list.
3041 3. Those set on the "all_dependent_configs" on the target in order
3042 that the configs appear in the list.
3043 4. Those set on the "public_configs" on the target in order that
3044 those configs appear in the list.
3045 5. all_dependent_configs pulled from dependencies, in the order of
3046 the "deps" list. This is done recursively. If a config appears
3047 more than once, only the first occurance will be used.
3048 6. public_configs pulled from dependencies, in the order of the
3049 "deps" list. If a dependency is public, they will be applied
3054 ## **cflags***: Flags passed to the C compiler.
3059 "cflags" are passed to all invocations of the C, C++, Objective C,
3060 and Objective C++ compilers.
3062 To target one of these variants individually, use "cflags_c",
3063 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3064 These variant-specific versions will be appended to the "cflags".
3068 ### **Ordering of flags and values**:
3071 1. Those set on the current target (not in a config).
3072 2. Those set on the "configs" on the target in order that the
3073 configs appear in the list.
3074 3. Those set on the "all_dependent_configs" on the target in order
3075 that the configs appear in the list.
3076 4. Those set on the "public_configs" on the target in order that
3077 those configs appear in the list.
3078 5. all_dependent_configs pulled from dependencies, in the order of
3079 the "deps" list. This is done recursively. If a config appears
3080 more than once, only the first occurance will be used.
3081 6. public_configs pulled from dependencies, in the order of the
3082 "deps" list. If a dependency is public, they will be applied
3087 ## **cflags***: Flags passed to the C compiler.
3092 "cflags" are passed to all invocations of the C, C++, Objective C,
3093 and Objective C++ compilers.
3095 To target one of these variants individually, use "cflags_c",
3096 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3097 These variant-specific versions will be appended to the "cflags".
3101 ### **Ordering of flags and values**:
3104 1. Those set on the current target (not in a config).
3105 2. Those set on the "configs" on the target in order that the
3106 configs appear in the list.
3107 3. Those set on the "all_dependent_configs" on the target in order
3108 that the configs appear in the list.
3109 4. Those set on the "public_configs" on the target in order that
3110 those configs appear in the list.
3111 5. all_dependent_configs pulled from dependencies, in the order of
3112 the "deps" list. This is done recursively. If a config appears
3113 more than once, only the first occurance will be used.
3114 6. public_configs pulled from dependencies, in the order of the
3115 "deps" list. If a dependency is public, they will be applied
3120 ## **cflags***: Flags passed to the C compiler.
3125 "cflags" are passed to all invocations of the C, C++, Objective C,
3126 and Objective C++ compilers.
3128 To target one of these variants individually, use "cflags_c",
3129 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3130 These variant-specific versions will be appended to the "cflags".
3134 ### **Ordering of flags and values**:
3137 1. Those set on the current target (not in a config).
3138 2. Those set on the "configs" on the target in order that the
3139 configs appear in the list.
3140 3. Those set on the "all_dependent_configs" on the target in order
3141 that the configs appear in the list.
3142 4. Those set on the "public_configs" on the target in order that
3143 those configs appear in the list.
3144 5. all_dependent_configs pulled from dependencies, in the order of
3145 the "deps" list. This is done recursively. If a config appears
3146 more than once, only the first occurance will be used.
3147 6. public_configs pulled from dependencies, in the order of the
3148 "deps" list. If a dependency is public, they will be applied
3153 ## **check_includes**: [boolean] Controls whether a target's files are checked.
3156 When true (the default), the "gn check" command (as well as
3157 "gn gen" with the --check flag) will check this target's sources
3158 and headers for proper dependencies.
3160 When false, the files in this target will be skipped by default.
3161 This does not affect other targets that depend on the current target,
3162 it just skips checking the includes of the current target's files.
3169 source_set("busted_includes") {
3170 # This target's includes are messed up, exclude it from checking.
3171 check_includes = false
3177 ## **complete_static_lib**: [boolean] Links all deps into a static library.
3180 A static library normally doesn't include code from dependencies, but
3181 instead forwards the static libraries and source sets in its deps up
3182 the dependency chain until a linkable target (an executable or shared
3183 library) is reached. The final linkable target only links each static
3184 library once, even if it appears more than once in its dependency
3187 In some cases the static library might be the final desired output.
3188 For example, you may be producing a static library for distribution to
3189 third parties. In this case, the static library should include code
3190 for all dependencies in one complete package. Since GN does not unpack
3191 static libraries to forward their contents up the dependency chain,
3192 it is an error for complete static libraries to depend on other static
3200 static_library("foo") {
3201 complete_static_lib = true
3207 ## **configs**: Configs applying to this target.
3210 A list of config labels.
3212 The include_dirs, defines, etc. in each config are appended in the
3213 order they appear to the compile command for each file in the target.
3214 They will appear after the include_dirs, defines, etc. that the target
3217 The build configuration script will generally set up the default
3218 configs applying to a given target type (see "set_defaults").
3219 When a target is being defined, it can add to or remove from this
3224 ### **Ordering of flags and values**:
3227 1. Those set on the current target (not in a config).
3228 2. Those set on the "configs" on the target in order that the
3229 configs appear in the list.
3230 3. Those set on the "all_dependent_configs" on the target in order
3231 that the configs appear in the list.
3232 4. Those set on the "public_configs" on the target in order that
3233 those configs appear in the list.
3234 5. all_dependent_configs pulled from dependencies, in the order of
3235 the "deps" list. This is done recursively. If a config appears
3236 more than once, only the first occurance will be used.
3237 6. public_configs pulled from dependencies, in the order of the
3238 "deps" list. If a dependency is public, they will be applied
3245 static_library("foo") {
3246 configs -= "//build:no_rtti" # Don't use the default RTTI config.
3247 configs += ":mysettings" # Add some of our own settings.
3252 ## **data**: Runtime data file dependencies.
3255 Lists files required to run the given target. These are typically
3258 Appearing in the "data" section does not imply any special handling
3259 such as copying them to the output directory. This is just used for
3260 declaring runtime dependencies. There currently isn't a good use for
3261 these but it is envisioned that test data can be listed here for use
3262 running automated tests.
3264 See also "gn help inputs" and "gn help data_deps", both of
3265 which actually affect the build in concrete ways.
3269 ## **data_deps**: Non-linked dependencies.
3272 A list of target labels.
3274 Specifies dependencies of a target that are not actually linked into
3275 the current target. Such dependencies will built and will be available
3278 This is normally used for things like plugins or helper programs that
3279 a target needs at runtime.
3281 See also "gn help deps" and "gn help data".
3289 data_deps = [ "//plugins:my_runtime_plugin" ]
3294 ## **defines**: C preprocessor defines.
3299 These strings will be passed to the C/C++ compiler as #defines. The
3300 strings may or may not include an "=" to assign a value.
3304 ### **Ordering of flags and values**:
3307 1. Those set on the current target (not in a config).
3308 2. Those set on the "configs" on the target in order that the
3309 configs appear in the list.
3310 3. Those set on the "all_dependent_configs" on the target in order
3311 that the configs appear in the list.
3312 4. Those set on the "public_configs" on the target in order that
3313 those configs appear in the list.
3314 5. all_dependent_configs pulled from dependencies, in the order of
3315 the "deps" list. This is done recursively. If a config appears
3316 more than once, only the first occurance will be used.
3317 6. public_configs pulled from dependencies, in the order of the
3318 "deps" list. If a dependency is public, they will be applied
3325 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
3329 ## **depfile**: [string] File name for input dependencies for actions.
3332 If nonempty, this string specifies that the current action or
3333 action_foreach target will generate the given ".d" file containing
3334 the dependencies of the input. Empty or unset means that the script
3335 doesn't generate the files.
3337 The .d file should go in the target output directory. If you have more
3338 than one source file that the script is being run over, you can use
3339 the output file expansions described in "gn help action_foreach" to
3340 name the .d file according to the input.
3341 The format is that of a Makefile, and all of the paths should be
3342 relative to the root build directory.
3348 action_foreach("myscript_target") {
3349 script = "myscript.py"
3352 # Locate the depfile in the output directory named like the
3353 # inputs but with a ".d" appended.
3354 depfile = "$relative_target_output_dir/{{source_name}}.d"
3356 # Say our script uses "-o <d file>" to indicate the depfile.
3357 args = [ "{{source}}", "-o", depfile ]
3362 ## **deps**: Private linked dependencies.
3365 A list of target labels.
3367 Specifies private dependencies of a target. Shared and dynamic
3368 libraries will be linked into the current target. Other target types
3369 that can't be linked (like actions and groups) listed in "deps" will
3370 be treated as "data_deps". Likewise, if the current target isn't
3371 linkable, then all deps will be treated as "data_deps".
3373 These dependencies are private in that it does not grant dependent
3374 targets the ability to include headers from the dependency, and direct
3375 dependent configs are not forwarded.
3377 See also "public_deps" and "data_deps".
3381 ## **forward_dependent_configs_from**
3384 A list of target labels.
3386 DEPRECATED. Use public_deps instead which will have the same effect.
3388 Exposes the public_configs from a private dependent target as
3389 public_configs of the current one. Each label in this list
3390 must also be in the deps.
3392 Generally you should use public_deps instead of this variable to
3393 express the concept of exposing a dependency as part of a target's
3394 public API. We're considering removing this variable.
3401 Sometimes you depend on a child library that exports some necessary
3402 configuration via public_configs. If your target in turn exposes the
3403 child library's headers in its public headers, it might mean that
3404 targets that depend on you won't work: they'll be seeing the child
3405 library's code but not the necessary configuration. This list
3406 specifies which of your deps' direct dependent configs to expose as
3414 If we use a given library "a" from our public headers:
3416 deps = [ ":a", ":b", ... ]
3417 forward_dependent_configs_from = [ ":a" ]
3419 This example makes a "transparent" target that forwards a dependency
3423 if (use_system_frob) {
3424 deps = ":system_frob"
3426 deps = "//third_party/fallback_frob"
3428 forward_dependent_configs_from = deps
3433 ## **include_dirs**: Additional include directories.
3436 A list of source directories.
3438 The directories in this list will be added to the include path for
3439 the files in the affected target.
3443 ### **Ordering of flags and values**:
3446 1. Those set on the current target (not in a config).
3447 2. Those set on the "configs" on the target in order that the
3448 configs appear in the list.
3449 3. Those set on the "all_dependent_configs" on the target in order
3450 that the configs appear in the list.
3451 4. Those set on the "public_configs" on the target in order that
3452 those configs appear in the list.
3453 5. all_dependent_configs pulled from dependencies, in the order of
3454 the "deps" list. This is done recursively. If a config appears
3455 more than once, only the first occurance will be used.
3456 6. public_configs pulled from dependencies, in the order of the
3457 "deps" list. If a dependency is public, they will be applied
3464 include_dirs = [ "src/include", "//third_party/foo" ]
3468 ## **inputs**: Additional compile-time dependencies.
3471 Inputs are compile-time dependencies of the current target. This means
3472 that all inputs must be available before compiling any of the sources
3473 or executing any actions.
3475 Inputs are typically only used for action and action_foreach targets.
3479 ### **Inputs for actions**
3482 For action and action_foreach targets, inputs should be the inputs to
3483 script that don't vary. These should be all .py files that the script
3484 uses via imports (the main script itself will be an implcit dependency
3485 of the action so need not be listed).
3487 For action targets, inputs should be the entire set of inputs the
3488 script needs. For action_foreach targets, inputs should be the set of
3489 dependencies that don't change. These will be applied to each script
3490 invocation over the sources.
3492 Note that another way to declare input dependencies from an action
3493 is to have the action write a depfile (see "gn help depfile"). This
3494 allows the script to dynamically write input dependencies, that might
3495 not be known until actually executing the script. This is more
3496 efficient than doing processing while running GN to determine the
3497 inputs, and is easier to keep in-sync than hardcoding the list.
3501 ### **Inputs for binary targets**
3504 Any input dependencies will be resolved before compiling any sources.
3505 Normally, all actions that a target depends on will be run before any
3506 files in a target are compiled. So if you depend on generated headers,
3507 you do not typically need to list them in the inputs section.
3514 action("myscript") {
3515 script = "domything.py"
3516 inputs = [ "input.data" ]
3521 ## **ldflags**: Flags passed to the linker.
3526 These flags are passed on the command-line to the linker and generally
3527 specify various linking options. Most targets will not need these and
3528 will use "libs" and "lib_dirs" instead.
3530 ldflags are NOT pushed to dependents, so applying ldflags to source
3531 sets or static libraries will be a no-op. If you want to apply ldflags
3532 to dependent targets, put them in a config and set it in the
3533 all_dependent_configs or public_configs.
3537 ## **lib_dirs**: Additional library directories.
3540 A list of directories.
3542 Specifies additional directories passed to the linker for searching
3543 for the required libraries. If an item is not an absolute path, it
3544 will be treated as being relative to the current build file.
3546 libs and lib_dirs work differently than other flags in two respects.
3547 First, then are inherited across static library boundaries until a
3548 shared library or executable target is reached. Second, they are
3549 uniquified so each one is only passed once (the first instance of it
3550 will be the one used).
3554 ### **Ordering of flags and values**:
3557 1. Those set on the current target (not in a config).
3558 2. Those set on the "configs" on the target in order that the
3559 configs appear in the list.
3560 3. Those set on the "all_dependent_configs" on the target in order
3561 that the configs appear in the list.
3562 4. Those set on the "public_configs" on the target in order that
3563 those configs appear in the list.
3564 5. all_dependent_configs pulled from dependencies, in the order of
3565 the "deps" list. This is done recursively. If a config appears
3566 more than once, only the first occurance will be used.
3567 6. public_configs pulled from dependencies, in the order of the
3568 "deps" list. If a dependency is public, they will be applied
3575 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
3579 ## **libs**: Additional libraries to link.
3584 These files will be passed to the linker, which will generally search
3585 the library include path. Unlike a normal list of files, they will be
3586 passed to the linker unmodified rather than being treated as file
3587 names relative to the current build file. Generally you would set
3588 the "lib_dirs" so your library is found. If you need to specify
3589 a path, you can use "rebase_path" to convert a path to be relative
3590 to the build directory.
3592 When constructing the linker command, the "lib_prefix" attribute of
3593 the linker tool in the current toolchain will be prepended to each
3594 library. So your BUILD file should not specify the switch prefix
3595 (like "-l"). On Mac, libraries ending in ".framework" will be
3596 special-cased: the switch "-framework" will be prepended instead of
3597 the lib_prefix, and the ".framework" suffix will be trimmed.
3599 libs and lib_dirs work differently than other flags in two respects.
3600 First, then are inherited across static library boundaries until a
3601 shared library or executable target is reached. Second, they are
3602 uniquified so each one is only passed once (the first instance of it
3603 will be the one used).
3607 ### **Ordering of flags and values**:
3610 1. Those set on the current target (not in a config).
3611 2. Those set on the "configs" on the target in order that the
3612 configs appear in the list.
3613 3. Those set on the "all_dependent_configs" on the target in order
3614 that the configs appear in the list.
3615 4. Those set on the "public_configs" on the target in order that
3616 those configs appear in the list.
3617 5. all_dependent_configs pulled from dependencies, in the order of
3618 the "deps" list. This is done recursively. If a config appears
3619 more than once, only the first occurance will be used.
3620 6. public_configs pulled from dependencies, in the order of the
3621 "deps" list. If a dependency is public, they will be applied
3629 libs = [ "ctl3d.lib" ]
3635 ## **output_extension**: Value to use for the output's file extension.
3638 Normally the file extension for a target is based on the target
3639 type and the operating system, but in rare cases you will need to
3640 override the name (for example to use "libfreetype.so.6" instead
3641 of libfreetype.so on Linux).
3644 ## **output_name**: Define a name for the output file other than the default.
3647 Normally the output name of a target will be based on the target name,
3648 so the target "//foo/bar:bar_unittests" will generate an output
3649 file such as "bar_unittests.exe" (using Windows as an example).
3651 Sometimes you will want an alternate name to avoid collisions or
3652 if the internal name isn't appropriate for public distribution.
3654 The output name should have no extension or prefixes, these will be
3655 added using the default system rules. For example, on Linux an output
3656 name of "foo" will produce a shared library "libfoo.so".
3658 This variable is valid for all binary output target types.
3664 static_library("doom_melon") {
3665 output_name = "fluffy_bunny"
3670 ## **outputs**: Output files for actions and copy targets.
3673 Outputs is valid for "copy", "action", and "action_foreach"
3674 target types and indicates the resulting files. The values may contain
3675 source expansions to generate the output names from the sources (see
3676 "gn help source_expansion").
3678 For copy targets, the outputs is the destination for the copied
3679 file(s). For actions, the outputs should be the list of files
3680 generated by the script.
3684 ## **public**: Declare public header files for a target.
3687 A list of files that other targets can include. These permissions are
3688 checked via the "check" command (see "gn help check").
3690 If no public files are declared, other targets (assuming they have
3691 visibility to depend on this target can include any file in the
3692 sources list. If this variable is defined on a target, dependent
3693 targets may only include files on this whitelist.
3695 Header file permissions are also subject to visibility. A target
3696 must be visible to another target to include any files from it at all
3697 and the public headers indicate which subset of those files are
3698 permitted. See "gn help visibility" for more.
3700 Public files are inherited through the dependency tree. So if there is
3701 a dependency A -> B -> C, then A can include C's public headers.
3702 However, the same is NOT true of visibility, so unless A is in C's
3703 visibility list, the include will be rejected.
3705 GN only knows about files declared in the "sources" and "public"
3706 sections of targets. If a file is included that is not known to the
3707 build, it will be allowed.
3713 These exact files are public:
3714 public = [ "foo.h", "bar.h" ]
3716 No files are public (no targets may include headers from this one):
3721 ## **public_configs**: Configs to be applied on dependents.
3724 A list of config labels.
3726 Targets directly depending on this one will have the configs listed in
3727 this variable added to them. These configs will also apply to the
3730 This addition happens in a second phase once a target and all of its
3731 dependencies have been resolved. Therefore, a target will not see
3732 these force-added configs in their "configs" variable while the
3733 script is running, and then can not be removed. As a result, this
3734 capability should generally only be used to add defines and include
3735 directories necessary to compile a target's headers.
3737 See also "all_dependent_configs".
3741 ### **Ordering of flags and values**:
3744 1. Those set on the current target (not in a config).
3745 2. Those set on the "configs" on the target in order that the
3746 configs appear in the list.
3747 3. Those set on the "all_dependent_configs" on the target in order
3748 that the configs appear in the list.
3749 4. Those set on the "public_configs" on the target in order that
3750 those configs appear in the list.
3751 5. all_dependent_configs pulled from dependencies, in the order of
3752 the "deps" list. This is done recursively. If a config appears
3753 more than once, only the first occurance will be used.
3754 6. public_configs pulled from dependencies, in the order of the
3755 "deps" list. If a dependency is public, they will be applied
3760 ## **public_deps**: Declare public dependencies.
3763 Public dependencies are like private dependencies ("deps") but
3764 additionally express that the current target exposes the listed deps
3765 as part of its public API.
3767 This has several ramifications:
3769 - public_configs that are part of the dependency are forwarded
3770 to direct dependents.
3772 - Public headers in the dependency are usable by dependents
3773 (includes do not require a direct dependency or visibility).
3775 - If the current target is a shared library, other shared libraries
3776 that it publicly depends on (directly or indirectly) are
3777 propagated up the dependency tree to dependents for linking.
3784 Say you have three targets: A -> B -> C. C's visibility may allow
3785 B to depend on it but not A. Normally, this would prevent A from
3786 including any headers from C, and C's public_configs would apply
3789 If B lists C in its public_deps instead of regular deps, A will now
3790 inherit C's public_configs and the ability to include C's public
3793 Generally if you are writing a target B and you include C's headers
3794 as part of B's public headers, or targets depending on B should
3795 consider B and C to be part of a unit, you should use public_deps
3803 # This target can include files from "c" but not from
3804 # "super_secret_implementation_details".
3809 shared_library("b") {
3810 deps = [ ":super_secret_implementation_details" ]
3811 public_deps = [ ":c" ]
3816 ## **script**: Script file for actions.
3819 An absolute or buildfile-relative file name of a Python script to run
3820 for a action and action_foreach targets (see "gn help action" and
3821 "gn help action_foreach").
3825 ## **sources**: Source files for a target
3828 A list of files relative to the current buildfile.
3832 ## **testonly**: Declares a target must only be used for testing.
3835 Boolean. Defaults to false.
3837 When a target is marked "testonly = true", it must only be depended
3838 on by other test-only targets. Otherwise, GN will issue an error
3839 that the depenedency is not allowed.
3841 This feature is intended to prevent accidentally shipping test code
3849 source_set("test_support") {
3856 ## **visibility**: A list of labels that can depend on a target.
3859 A list of labels and label patterns that define which targets can
3860 depend on the current one. These permissions are checked via the
3861 "check" command (see "gn help check").
3863 If visibility is not defined, it defaults to public ("*").
3865 If visibility is defined, only the targets with labels that match it
3866 can depend on the current target. The empty list means no targets
3867 can depend on the current target.
3869 Tip: Often you will want the same visibility for all targets in a
3870 BUILD file. In this case you can just put the definition at the top,
3871 outside of any target, and the targets will inherit that scope and see
3879 See "gn help label_pattern" for more details on what types of
3880 patterns are supported. If a toolchain is specified, only targets
3881 in that toolchain will be matched. If a toolchain is not specified on
3882 a pattern, targets in all toolchains will be matched.
3889 Only targets in the current buildfile ("private"):
3890 visibility = [ ":*" ]
3892 No targets (used for targets that should be leaf nodes):
3895 Any target ("public", the default):
3896 visibility = [ "*" ]
3898 All targets in the current directory and any subdirectory:
3899 visibility = [ "./*" ]
3901 Any target in "//bar/BUILD.gn":
3902 visibility = [ "//bar:*" ]
3904 Any target in "//bar/" or any subdirectory thereof:
3905 visibility = [ "//bar/*" ]
3907 Just these specific targets:
3908 visibility = [ ":mything", "//foo:something_else" ]
3910 Any target in the current directory and any subdirectory thereof, plus
3911 any targets in "//bar/" and any subdirectory thereof.
3912 visibility = [ "./*", "//bar/*" ]
3916 ## **Build Arguments Overview**
3919 Build arguments are variables passed in from outside of the build
3920 that build files can query to determine how the build works.
3924 ### **How build arguments are set**
3927 First, system default arguments are set based on the current system.
3928 The built-in arguments are:
3936 If specified, arguments from the --args command line flag are used. If
3937 that flag is not specified, args from previous builds in the build
3938 directory will be used (this is in the file args.gn in the build
3941 Last, for targets being compiled with a non-default toolchain, the
3942 toolchain overrides are applied. These are specified in the
3943 toolchain_args section of a toolchain definition. The use-case for
3944 this is that a toolchain may be building code for a different
3945 platform, and that it may want to always specify Posix, for example.
3946 See "gn help toolchain_args" for more.
3948 If you specify an override for a build argument that never appears in
3949 a "declare_args" call, a nonfatal error will be displayed.
3957 Create the directory out/FooBar and open an editor. You would type
3958 something like this into that file:
3959 enable_doom_melon=false
3962 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
3963 This will overwrite the build directory with the given arguments.
3964 (Note that the quotes inside the args command will usually need to
3965 be escaped for your shell to pass through strings values.)
3969 ### **How build arguments are used**
3972 If you want to use an argument, you use declare_args() and specify
3973 default values. These default values will apply if none of the steps
3974 listed in the "How build arguments are set" section above apply to
3975 the given argument, but the defaults will not override any of these.
3977 Often, the root build config file will declare global arguments that
3978 will be passed to all buildfiles. Individual build files can also
3979 specify arguments that apply only to those files. It is also useful
3980 to specify build args in an "import"-ed file if you want such
3981 arguments to apply to multiple buildfiles.
3988 When gn starts, it will search the current directory and parent
3989 directories for a file called ".gn". This indicates the source root.
3990 You can override this detection by using the --root command-line
3993 The .gn file in the source root will be executed. The syntax is the
3994 same as a buildfile, but with very limited build setup-specific
3997 If you specify --root, by default GN will look for the file .gn in
3998 that directory. If you want to specify a different file, you can
3999 additionally pass --dotfile:
4001 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
4008 buildconfig [required]
4009 Label of the build config file. This file will be used to set up
4010 the build file execution environment for each toolchain.
4012 check_targets [optional]
4013 A list of labels and label patterns that should be checked when
4014 running "gn check" or "gn gen --check". If unspecified, all
4015 targets will be checked. If it is the empty list, no targets will
4018 The format of this list is identical to that of "visibility"
4019 so see "gn help visibility" for examples.
4021 exec_script_whitelist [optional]
4022 A list of .gn/.gni files (not labels) that have permission to call
4023 the exec_script function. If this list is defined, calls to
4024 exec_script will be checked against this list and GN will fail if
4025 the current file isn't in the list.
4027 This is to allow the use of exec_script to be restricted since
4028 is easy to use inappropriately. Wildcards are not supported.
4029 Files in the secondary_source tree (if defined) should be
4030 referenced by ignoring the secondary tree and naming them as if
4031 they are in the main tree.
4033 If unspecified, the ability to call exec_script is unrestricted.
4036 exec_script_whitelist = [
4038 "//build/my_config.gni",
4042 Label of the root build target. The GN build will start by loading
4043 the build file containing this target name. This defaults to
4044 "//:" which will cause the file //BUILD.gn to be loaded.
4046 secondary_source [optional]
4047 Label of an alternate directory tree to find input files. When
4048 searching for a BUILD.gn file (or the build config file discussed
4049 above), the file will first be looked for in the source root.
4050 If it's not found, the secondary source root will be checked
4051 (which would contain a parallel directory hierarchy).
4053 This behavior is intended to be used when BUILD.gn files can't be
4054 checked in to certain source directories for whatever reason.
4056 The secondary source root must be inside the main source tree.
4060 ### **Example .gn file contents**
4063 buildconfig = "//build/config/BUILDCONFIG.gn"
4066 "//doom_melon/*", # Check everything in this subtree.
4067 "//tools:mind_controlling_ant", # Check this specific target.
4072 secondary_source = "//build/config/temporary_buildfiles/"
4076 ## **GN build language grammar**
4081 GN build files are read as sequences of tokens. While splitting the
4082 file into tokens, the next token is the longest sequence of characters
4083 that form a valid token.
4087 ### **White space and comments**
4090 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
4091 carriage returns (U+000D), and newlines (U+000A).
4093 Comments start at the character "#" and stop at the next newline.
4095 White space and comments are ignored except that they may separate
4096 tokens that would otherwise combine into a single token.
4103 Identifiers name variables and functions.
4105 identifier = letter { letter | digit } .
4106 letter = "A" ... "Z" | "a" ... "z" | "_" .
4107 digit = "0" ... "9" .
4114 The following keywords are reserved and may not be used as
4121 ### **Integer literals**
4124 An integer literal represents a decimal integer value.
4126 integer = [ "-" ] digit { digit } .
4128 Leading zeros and negative zero are disallowed.
4132 ### **String literals**
4135 A string literal represents a string value consisting of the quoted
4136 characters with possible escape sequences and variable expansions.
4138 string = `"` { char | escape | expansion } `"` .
4139 escape = `\` ( "$" | `"` | char ) .
4140 expansion = "$" ( identifier | "{" identifier "}" ) .
4141 char = /* any character except "$", `"`, or newline */ .
4143 After a backslash, certain sequences represent special characters:
4145 \" U+0022 quotation mark
4146 \$ U+0024 dollar sign
4149 All other backslashes represent themselves.
4156 The following character sequences represent punctuation:
4168 The input tokens form a syntax tree following a context-free grammar:
4170 File = StatementList .
4172 Statement = Assignment | Call | Condition .
4173 Assignment = identifier AssignOp Expr .
4174 Call = identifier "(" [ ExprList ] ")" [ Block ] .
4175 Condition = "if" "(" Expr ")" Block
4176 [ "else" ( Condition | Block ) ] .
4177 Block = "{" StatementList "}" .
4178 StatementList = { Statement } .
4180 Expr = UnaryExpr | Expr BinaryOp Expr .
4181 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
4182 PrimaryExpr = identifier | integer | string | Call
4183 | identifier "[" Expr "]"
4184 | identifier "." identifier
4186 | "[" [ ExprList [ "," ] ] "]" .
4187 ExprList = Expr { "," Expr } .
4189 AssignOp = "=" | "+=" | "-=" .
4191 BinaryOp = "+" | "-" // highest priority
4192 | "<" | "<=" | ">" | ">="
4195 | "||" . // lowest priority
4197 All binary operators are left-associative.
4201 ## **input_conversion**: Specifies how to transform input to a variable.
4204 input_conversion is an argument to read_file and exec_script that
4205 specifies how the result of the read operation should be converted
4209 Discard the result and return None.
4212 Return the file contents as a list, with a string for each line.
4213 The newlines will not be present in the result. The last line may
4214 or may not end in a newline.
4216 After splitting, each individual line will be trimmed of
4217 whitespace on both ends.
4220 Execute the block as GN code and return a scope with the
4221 resulting values in it. If the input was:
4222 a = [ "hello.cc", "world.cc" ]
4224 and you read the result into a variable named "val", then you
4225 could access contents the "." operator on "val":
4230 Return the file contents into a single string.
4233 Parse the input as if it was a literal rvalue in a buildfile.
4234 Examples of typical program output using this mode:
4235 [ "foo", "bar" ] (result will be a list)
4237 "foo bar" (result will be a string)
4239 5 (result will be an integer)
4241 Note that if the input is empty, the result will be a null value
4242 which will produce an error if assigned to a variable.
4245 Prefixing any of the other transformations with the word "trim"
4246 will result in whitespace being trimmed from the beginning and end
4247 of the result before processing.
4249 Examples: "trim string" or "trim list lines"
4251 Note that "trim value" is useless because the value parser skips
4256 ## **Label patterns**
4259 A label pattern is a way of expressing one or more labels in a portion
4260 of the source tree. They are not general regular expressions.
4262 They can take the following forms only:
4264 - Explicit (no wildcard):
4268 - Wildcard target names:
4269 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
4270 ":*" (all targets in the current build file)
4272 - Wildcard directory names ("*" is only supported at the end)
4274 "//foo/bar/*" (all targets in any subdir of //foo/bar)
4275 "./*" (all targets in the current build file or sub dirs)
4277 Any of the above forms can additionally take an explicit toolchain.
4278 In this case, the toolchain must be fully qualified (no wildcards
4279 are supported in the toolchain name).
4281 "//foo:bar(//build/toochain:mac)"
4282 An explicit target in an explicit toolchain.
4284 ":*(//build/toolchain/linux:32bit)"
4285 All targets in the current build file using the 32-bit Linux
4288 "//foo/*(//build/toolchain:win)"
4289 All targets in //foo and any subdirectory using the Windows
4294 ## **How Source Expansion Works**
4297 Source expansion is used for the action_foreach and copy target types
4298 to map source file names to output file names or arguments.
4300 To perform source expansion in the outputs, GN maps every entry in the
4301 sources to every entry in the outputs list, producing the cross
4302 product of all combinations, expanding placeholders (see below).
4304 Source expansion in the args works similarly, but performing the
4305 placeholder substitution produces a different set of arguments for
4306 each invocation of the script.
4308 If no placeholders are found, the outputs or args list will be treated
4309 as a static list of literal file names that do not depend on the
4312 See "gn help copy" and "gn help action_foreach" for more on how
4317 ### **Placeholders**
4321 The name of the source file including directory (*). This will
4322 generally be used for specifying inputs to a script in the
4324 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
4326 {{source_file_part}}
4327 The file part of the source including the extension.
4328 "//foo/bar/baz.txt" => "baz.txt"
4330 {{source_name_part}}
4331 The filename part of the source file with no directory or
4332 extension. This will generally be used for specifying a
4333 transformation from a soruce file to a destination file with the
4334 same name but different extension.
4335 "//foo/bar/baz.txt" => "baz"
4338 The directory (*) containing the source file with no
4340 "//foo/bar/baz.txt" => "../../foo/bar"
4342 {{source_root_relative_dir}}
4343 The path to the source file's directory relative to the source
4344 root, with no leading "//" or trailing slashes. If the path is
4345 system-absolute, (beginning in a single slash) this will just
4346 return the path with no trailing slash. This value will always
4347 be the same, regardless of whether it appears in the "outputs"
4349 "//foo/bar/baz.txt" => "foo/bar"
4352 The generated file directory (*) corresponding to the source
4353 file's path. This will be different than the target's generated
4354 file directory if the source file is in a different directory
4355 than the BUILD.gn file.
4356 "//foo/bar/baz.txt" => "gen/foo/bar"
4359 The object file directory (*) corresponding to the source file's
4360 path, relative to the build directory. this us be different than
4361 the target's out directory if the source file is in a different
4362 directory than the build.gn file.
4363 "//foo/bar/baz.txt" => "obj/foo/bar"
4367 ### **(*) Note on directories**
4370 Paths containing directories (except the source_root_relative_dir)
4371 will be different depending on what context the expansion is evaluated
4372 in. Generally it should "just work" but it means you can't
4373 concatenate strings containing these values with reasonable results.
4375 Details: source expansions can be used in the "outputs" variable,
4376 the "args" variable, and in calls to "process_file_template". The
4377 "args" are passed to a script which is run from the build directory,
4378 so these directories will relative to the build directory for the
4379 script to find. In the other cases, the directories will be source-
4380 absolute (begin with a "//") because the results of those expansions
4381 will be handled by GN internally.
4388 Non-varying outputs:
4389 action("hardcoded_outputs") {
4390 sources = [ "input1.idl", "input2.idl" ]
4391 outputs = [ "$target_out_dir/output1.dat",
4392 "$target_out_dir/output2.dat" ]
4394 The outputs in this case will be the two literal files given.
4397 action_foreach("varying_outputs") {
4398 sources = [ "input1.idl", "input2.idl" ]
4399 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
4400 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
4402 Performing source expansion will result in the following output names:
4403 //out/Debug/obj/mydirectory/input1.h
4404 //out/Debug/obj/mydirectory/input1.cc
4405 //out/Debug/obj/mydirectory/input2.h
4406 //out/Debug/obj/mydirectory/input2.cc
4410 **Available global switches
4411 ** Do "gn help --the_switch_you_want_help_on" for more. Individual
4412 commands may take command-specific switches not listed here. See the
4413 help on your specific command for more.
4417 ** --args**: Specifies build arguments overrides.
4418 ** --color**: Force colored output.
4419 ** --dotfile**: override the name of the ".gn" file.
4420 ** --markdown**: write the output in the Markdown format.
4421 ** --nocolor**: Force non-colored output.
4422 ** -q**: Quiet mode. Don't print output on success.
4423 ** --root**: Explicitly specify source root.
4424 ** --time**: Outputs a summary of how long everything took.
4425 ** --tracelog**: Writes a Chrome-compatible trace log to the given file.
4426 ** -v**: Verbose logging.
4427 ** --version**: Prints the GN version number and exits.