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 ## **\--runtime-deps-list-file**: Save runtime dependencies for targets in file.
125 --runtime-deps-list-file=<filename>
127 Where <filename> is a text file consisting of the labels, one per
128 line, of the targets for which runtime dependencies are desired.
130 See "gn help runtime_deps" for a description of how runtime
131 dependencies are computed.
135 ### **Runtime deps output file**
138 For each target requested, GN will write a separate runtime dependency
139 file. The runtime dependency file will be in the output directory
140 alongside the output file of the target, with a ".runtime_deps"
141 extension. For example, if the target "//foo:bar" is listed in the
142 input file, and that target produces an output file "bar.so", GN
143 will create a file "bar.so.runtime_deps" in the build directory.
145 If a source set, action, copy, or group is listed, the runtime deps
146 file will correspond to the .stamp file corresponding to that target.
147 This is probably not useful; the use-case for this feature is
148 generally executable targets.
150 The runtime dependency file will list one file per line, with no
151 escaping. The files will be relative to the root_build_dir. The first
152 line of the file will be the main output file of the target itself
153 (in the above example, "bar.so").
157 ## **\--time**: Outputs a summary of how long everything took.
160 Hopefully self-explanatory.
167 gn gen out/Default --time
171 ## **\--tracelog**: Writes a Chrome-compatible trace log to the given file.
174 The trace log will show file loads, executions, scripts, and writes.
175 This allows performance analysis of the generation step.
177 To view the trace, open Chrome and navigate to "chrome://tracing/",
178 then press "Load" and specify the file you passed to this parameter.
185 gn gen out/Default --tracelog=mytrace.trace
189 ## **-v**: Verbose logging.
192 This will spew logging events to the console for debugging issues.
197 ## **gn args <out_dir> [\--list] [\--short] [\--args]**
200 See also "gn help buildargs" for a more high-level overview of how
201 build arguments work.
208 Open the arguments for the given build directory in an editor
209 (as specified by the EDITOR environment variable). If the given
210 build directory doesn't exist, it will be created and an empty
211 args file will be opened in the editor. You would type something
212 like this into that file:
213 enable_doom_melon=false
216 Note: you can edit the build args manually by editing the file
217 "args.gn" in the build directory and then running
220 gn args <out_dir> --list[=<exact_arg>] [--short]
221 Lists all build arguments available in the current configuration,
222 or, if an exact_arg is specified for the list flag, just that one
225 The output will list the declaration location, default value, and
226 comment preceeding the declaration. If --short is specified,
227 only the names and values will be printed.
229 If the out_dir is specified, the build configuration will be
230 taken from that build directory. The reason this is needed is that
231 the definition of some arguments is dependent on the build
232 configuration, so setting some values might add, remove, or change
233 the default values for other arguments. Specifying your exact
234 configuration allows the proper arguments to be displayed.
236 Instead of specifying the out_dir, you can also use the
237 command-line flag to specify the build configuration:
238 --args=<exact list of args to use>
245 Opens an editor with the args for out/Debug.
247 gn args out/Debug --list --short
248 Prints all arguments with their default values for the out/Debug
251 gn args out/Debug --list=target_cpu
252 Prints information about the "target_cpu" argument for the out/Debug
255 gn args --list --args="os=\"android\" enable_doom_melon=true"
256 Prints all arguments with the default values for a build with the
257 given arguments set (which may affect the values of other
262 ## **gn check <out_dir> [<label_pattern>] [\--force]**
265 "gn check" is the same thing as "gn gen" with the "--check" flag
266 except that this command does not write out any build files. It's
267 intended to be an easy way to manually trigger include file checking.
269 The <label_pattern> can take exact labels or patterns that match more
270 than one (although not general regular expressions). If specified,
271 only those matching targets will be checked. See
272 "gn help label_pattern" for details.
274 The .gn file may specify a list of targets to be checked. Only these
275 targets will be checked if no label_pattern is specified on the
276 command line. Otherwise, the command-line list is used instead. See
281 ### **Command-specific switches**
285 Ignores specifications of "check_includes = false" and checks
286 all target's files that match the target label.
296 gn check out/Default //foo:bar
297 Check only the files in the //foo:bar target.
299 gn check out/Default "//foo/*
300 Check only the files in targets in the //foo directory tree.
304 ## **gn clean <out_dir>**
307 Deletes the contents of the output directory except for args.gn and
308 creates a Ninja build environment sufficient to regenerate the build.
312 ## **gn desc <out_dir> <target label> [<what to show>] [\--blame]**
315 Displays information about a given labeled target for the given build.
316 The build parameters will be taken for the build in the given
321 ### **Possibilities for <what to show>**
323 (If unspecified an overall summary will be displayed.)
329 Additional input dependencies.
335 Whether "gn check" checks this target for include usage.
337 allow_circular_includes_from
338 Permit includes from these targets.
341 Prints which targets can depend on this one.
344 Whether this target may only be used in tests.
347 Shows configs applied to the given target, sorted in the order
348 they're specified. This includes both configs specified in the
349 "configs" variable, as well as configs pushed onto this target
350 via dependencies specifying "all" or "direct" dependent
354 Show immediate or recursive dependencies. See below for flags that
355 control deps printing.
358 all_dependent_configs
359 Shows the labels of configs applied to targets that depend on this
360 one (either directly or all of them).
362 forward_dependent_configs_from
363 Shows the labels of dependencies for which dependent configs will
364 be pushed to targets depending on the current one.
369 Actions only. The script and related values.
372 Outputs for script and copy target types.
375 include_dirs [--blame]
382 Shows the given values taken from the target and all configs
383 applying. See "--blame" below.
386 Compute all runtime deps for the given target. This is a
387 computed list and does not correspond to any GN variable, unlike
388 most other values here.
390 The output is a list of file names relative to the build
391 directory. See "gn help runtime_deps" for how this is computed.
392 This also works with "--blame" to see the source of the
401 Used with any value specified by a config, this will name
402 the config that specified the value. This doesn't currently work
403 for libs and lib_dirs because those are inherited and are more
404 complicated to figure out the blame (patches welcome).
408 ### **Flags that control how deps are printed**
412 Collects all recursive dependencies and prints a sorted flat list.
413 Also usable with --tree (see below).
415 --as=(buildfile|label|output)
416 How to print targets.
419 Prints the build files where the given target was declared as
422 Prints the label of the target.
424 Prints the first output file for the target relative to the
427 --testonly=(true|false)
428 Restrict outputs to targets with the testonly flag set
429 accordingly. When unspecified, the target's testonly flags are
433 Print a dependency tree. By default, duplicates will be elided
434 with "..." but when --all and -tree are used together, no
435 eliding will be performed.
437 The "deps", "public_deps", and "data_deps" will all be
438 included in the tree.
440 Tree output can not be used with the filtering or output flags:
441 --as, --type, --testonly.
443 --type=(action|copy|executable|group|shared_library|source_set|
445 Restrict outputs to targets matching the given type. If
446 unspecified, no filtering will be performed.
453 This command will show the full name of directories and source files,
454 but when directories and source paths are written to the build file,
455 they will be adjusted to be relative to the build directory. So the
456 values for paths displayed by this command won't match (but should
457 mean the same thing).
464 gn desc out/Debug //base:base
465 Summarizes the given target.
467 gn desc out/Foo :base_unittests deps --tree
468 Shows a dependency tree of the "base_unittests" project in
469 the current directory.
471 gn desc out/Debug //base defines --blame
472 Shows defines set for the //base:base target, annotated by where
473 each one was set from.
477 ## **gn format [\--dump-tree] [\--in-place] [\--stdin] BUILD.gn**
480 Formats .gn file to a standard format.
487 Does not change or output anything, but sets the process exit code
488 based on whether output would be different than what's on disk.
489 This is useful for presubmit/lint-type checks.
490 - Exit code 0: successful format, matches on disk.
491 - Exit code 1: general failure (parse error, etc.)
492 - Exit code 2: successful format, but differs from on disk.
495 For debugging only, dumps the parse tree.
498 Instead of writing the formatted file to stdout, replace the input
499 file with the formatted output. If no reformatting is required,
500 the input file will not be touched, and nothing printed.
503 Read input from stdin (and write to stdout). Not compatible with
504 --in-place of course.
510 gn format //some/BUILD.gn
511 gn format some\BUILD.gn
512 gn format /abspath/some/BUILD.gn
517 ## **gn gen**: Generate ninja files.
522 Generates ninja files from the current tree and puts them in the given
525 The output directory can be a source-repo-absolute path name such as:
527 Or it can be a directory relative to the current directory such as:
530 See "gn help switches" for the common command-line switches.
534 ## **gn help <anything>**
536 Yo dawg, I heard you like help on your help so I put help on the help
541 ## **gn ls <out_dir> [<label_pattern>] [\--all-toolchains] [\--as=...]**
543 [--type=...] [--testonly=...]
545 Lists all targets matching the given pattern for the given build
546 directory. By default, only targets in the default toolchain will
547 be matched unless a toolchain is explicitly supplied.
549 If the label pattern is unspecified, list all targets. The label
550 pattern is not a general regular expression (see
551 "gn help label_pattern"). If you need more complex expressions,
552 pipe the result through grep.
559 --as=(buildfile|label|output)
560 How to print targets.
563 Prints the build files where the given target was declared as
566 Prints the label of the target.
568 Prints the first output file for the target relative to the
572 Matches all toolchains. When set, if the label pattern does not
573 specify an explicit toolchain, labels from all toolchains will be
574 matched. When unset, only targets in the default toolchain will
575 be matched unless an explicit toolchain in the label is set.
577 --testonly=(true|false)
578 Restrict outputs to targets with the testonly flag set
579 accordingly. When unspecified, the target's testonly flags are
582 --type=(action|copy|executable|group|shared_library|source_set|
584 Restrict outputs to targets matching the given type. If
585 unspecified, no filtering will be performed.
593 Lists all targets in the default toolchain.
595 gn ls out/Debug "//base/*"
596 Lists all targets in the directory base and all subdirectories.
598 gn ls out/Debug "//base:*"
599 Lists all targets defined in //base/BUILD.gn.
601 gn ls out/Debug //base --as=output
602 Lists the build output file for //base:base
604 gn ls out/Debug --type=executable
605 Lists all executables produced by the build.
607 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
608 Builds all targets in //base and all subdirectories.
610 gn ls out/Debug //base --all-toolchains
611 Lists all variants of the target //base:base (it may be referenced
612 in multiple toolchains).
616 ## **gn path <out_dir> <target_one> <target_two>**
619 Finds paths of dependencies between two targets. Each unique path
620 will be printed in one group, and groups will be separate by newlines.
621 The two targets can appear in either order: paths will be found going
624 Each dependency will be annotated with its type. By default, only the
625 first path encountered will be printed, which is not necessarily the
634 Prints all paths found rather than just the first one.
641 gn path out/Default //base //tools/gn
645 ## **gn refs <out_dir> (<label_pattern>|<label>|<file>|@<response_file>)* [\--all]**
647 [--all-toolchains] [--as=...] [--testonly=...] [--type=...]
649 Finds reverse dependencies (which targets reference something). The
650 input is a list containing:
652 - Target label: The result will be which targets depend on it.
654 - Config label: The result will be which targets list the given
655 config in its "configs" or "public_configs" list.
657 - Label pattern: The result will be which targets depend on any
658 target matching the given pattern. Patterns will not match
659 configs. These are not general regular expressions, see
660 "gn help label_pattern" for details.
662 - File name: The result will be which targets list the given file in
663 its "inputs", "sources", "public", "data", or "outputs".
664 Any input that does not contain wildcards and does not match a
665 target or a config will be treated as a file.
667 - Response file: If the input starts with an "@", it will be
668 interpreted as a path to a file containing a list of labels or
669 file names, one per line. This allows us to handle long lists
670 of inputs without worrying about command line limits.
678 When used without --tree, will recurse and display all unique
679 dependencies of the given targets. For example, if the input is
680 a target, this will output all targets that depend directly or
681 indirectly on the input. If the input is a file, this will output
682 all targets that depend directly or indirectly on that file.
684 When used with --tree, turns off eliding to show a complete tree.
687 Normally only inputs in the default toolchain will be included.
688 This switch will turn on matching all toolchains.
690 For example, a file is in a target might be compiled twice:
691 once in the default toolchain and once in a secondary one. Without
692 this flag, only the default toolchain one will be matched and
693 printed (potentially with its recursive dependencies, depending on
694 the other options). With this flag, both will be printed
695 (potentially with both of their recursive dependencies).
697 --as=(buildfile|label|output)
698 How to print targets.
701 Prints the build files where the given target was declared as
704 Prints the label of the target.
706 Prints the first output file for the target relative to the
710 Quiet. If nothing matches, don't print any output. Without this
711 option, if there are no matches there will be an informational
712 message printed which might interfere with scripts processing the
715 --testonly=(true|false)
716 Restrict outputs to targets with the testonly flag set
717 accordingly. When unspecified, the target's testonly flags are
721 Outputs a reverse dependency tree from the given target.
722 Duplicates will be elided. Combine with --all to see a full
725 Tree output can not be used with the filtering or output flags:
726 --as, --type, --testonly.
728 --type=(action|copy|executable|group|shared_library|source_set|
730 Restrict outputs to targets matching the given type. If
731 unspecified, no filtering will be performed.
735 ### **Examples (target input)**
738 gn refs out/Debug //tools/gn:gn
739 Find all targets depending on the given exact target name.
741 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
742 Edit all .gn files containing references to //base:i18n
744 gn refs out/Debug //base --all
745 List all targets depending directly or indirectly on //base:base.
747 gn refs out/Debug "//base/*"
748 List all targets depending directly on any target in //base or
751 gn refs out/Debug "//base:*"
752 List all targets depending directly on any target in
755 gn refs out/Debug //base --tree
756 Print a reverse dependency tree of //base:base
760 ### **Examples (file input)**
763 gn refs out/Debug //base/macros.h
764 Print target(s) listing //base/macros.h as a source.
766 gn refs out/Debug //base/macros.h --tree
767 Display a reverse dependency tree to get to the given file. This
768 will show how dependencies will reference that file.
770 gn refs out/Debug //base/macros.h //base/at_exit.h --all
771 Display all unique targets with some dependency path to a target
772 containing either of the given files as a source.
774 gn refs out/Debug //base/macros.h --testonly=true --type=executable
776 Display the executable file names of all test executables
777 potentially affected by a change to the given file.
781 ## **action**: Declare a target that runs a script a single time.
784 This target type allows you to run a script a single time to produce
785 or more output files. If you want to run a script once for each of a
786 set of input files, see "gn help action_foreach".
793 In an action the "sources" and "inputs" are treated the same:
794 they're both input dependencies on script execution with no special
795 handling. If you want to pass the sources to your script, you must do
796 so explicitly by including them in the "args". Note also that this
797 means there is no special handling of paths since GN doesn't know
798 which of the args are paths and not. You will want to use
799 rebase_path() to convert paths to be relative to the root_build_dir.
801 You can dynamically write input dependencies (for incremental rebuilds
802 if an input file changes) by writing a depfile when the script is run
803 (see "gn help depfile"). This is more flexible than "inputs".
805 It is recommended you put inputs to your script in the "sources"
806 variable, and stuff like other Python files required to run your
807 script in the "inputs" variable.
809 The "deps" and "public_deps" for an action will always be
810 completed before any part of the action is run so it can depend on
811 the output of previous steps. The "data_deps" will be built if the
812 action is built, but may not have completed before all steps of the
813 action are started. This can give additional parallelism in the build
814 for runtime-only dependencies.
821 You should specify files created by your script by specifying them in
824 The script will be executed with the given arguments with the current
825 directory being that of the root build directory. If you pass files
826 to your script, see "gn help rebase_path" for how to convert
827 file names to be relative to the build directory (file names in the
828 sources, outputs, and inputs will be all treated as relative to the
829 current build file and converted as needed automatically).
833 ### **File name handling**
836 All output files must be inside the output directory of the build.
837 You would generally use |$target_out_dir| or |$target_gen_dir| to
838 reference the output or generated intermediate file directories,
846 args, data, data_deps, depfile, deps, outputs*, script*,
855 action("run_this_guy_once") {
856 script = "doprocessing.py"
857 sources = [ "my_configuration.txt" ]
858 outputs = [ "$target_gen_dir/insightful_output.txt" ]
860 # Our script imports this Python file so we want to rebuild if it
862 inputs = [ "helper_library.py" ]
864 # Note that we have to manually pass the sources to our script if
865 # the script needs them as inputs.
866 args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +
867 rebase_path(sources, root_build_dir)
872 ## **action_foreach**: Declare a target that runs a script over a set of files.
875 This target type allows you to run a script once-per-file over a set
876 of sources. If you want to run a script once that takes many files as
877 input, see "gn help action".
884 The script will be run once per file in the "sources" variable. The
885 "outputs" variable should specify one or more files with a source
886 expansion pattern in it (see "gn help source_expansion"). The output
887 file(s) for each script invocation should be unique. Normally you
888 use "{{source_name_part}}" in each output file.
890 If your script takes additional data as input, such as a shared
891 configuration file or a Python module it uses, those files should be
892 listed in the "inputs" variable. These files are treated as
893 dependencies of each script invocation.
895 You can dynamically write input dependencies (for incremental rebuilds
896 if an input file changes) by writing a depfile when the script is run
897 (see "gn help depfile"). This is more flexible than "inputs".
899 The "deps" and "public_deps" for an action will always be
900 completed before any part of the action is run so it can depend on
901 the output of previous steps. The "data_deps" will be built if the
902 action is built, but may not have completed before all steps of the
903 action are started. This can give additional parallelism in the build
904 for runtime-only dependencies.
911 The script will be executed with the given arguments with the current
912 directory being that of the root build directory. If you pass files
913 to your script, see "gn help rebase_path" for how to convert
914 file names to be relative to the build directory (file names in the
915 sources, outputs, and inputs will be all treated as relative to the
916 current build file and converted as needed automatically).
920 ### **File name handling**
923 All output files must be inside the output directory of the build.
924 You would generally use |$target_out_dir| or |$target_gen_dir| to
925 reference the output or generated intermediate file directories,
933 args, data, data_deps, depfile, deps, outputs*, script*,
942 # Runs the script over each IDL file. The IDL script will generate
943 # both a .cc and a .h file for each input.
944 action_foreach("my_idl") {
945 script = "idl_processor.py"
946 sources = [ "foo.idl", "bar.idl" ]
948 # Our script reads this file each time, so we need to list is as a
949 # dependency so we can rebuild if it changes.
950 inputs = [ "my_configuration.txt" ]
952 # Transformation from source file name to output file names.
953 outputs = [ "$target_gen_dir/{{source_name_part}}.h",
954 "$target_gen_dir/{{source_name_part}}.cc" ]
956 # Note that since "args" is opaque to GN, if you specify paths
957 # here, you will need to convert it to be relative to the build
958 # directory using "rebase_path()".
962 rebase_path(relative_target_gen_dir, root_build_dir) +
963 "/{{source_name_part}}.h" ]
969 ## **assert**: Assert an expression is true at generation time.
972 assert(<condition> [, <error string>])
974 If the condition is false, the build will fail with an error. If the
975 optional second argument is provided, that string will be printed
976 with the error message.
983 assert(defined(sources), "Sources must be defined")
987 ## **config**: Defines a configuration object.
990 Configuration objects can be applied to targets and specify sets of
991 compiler flags, includes, defines, etc. They provide a way to
992 conveniently group sets of this configuration information.
994 A config is referenced by its label just like a target.
996 The values in a config are additive only. If you want to remove a flag
997 you need to remove the corresponding config that sets it. The final
998 set of flags, defines, etc. for a target is generated in this order:
1000 1. The values specified directly on the target (rather than using a
1002 2. The configs specified in the target's "configs" list, in order.
1003 3. Public_configs from a breadth-first traversal of the dependency
1004 tree in the order that the targets appear in "deps".
1005 4. All dependent configs from a breadth-first traversal of the
1006 dependency tree in the order that the targets appear in "deps".
1010 ### **Variables valid in a config definition**:
1012 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1013 defines, include_dirs, ldflags, lib_dirs, libs
1014 precompiled_header, precompiled_source
1018 ### **Variables on a target used to apply configs**:
1020 all_dependent_configs, configs, public_configs,
1021 forward_dependent_configs_from
1027 config("myconfig") {
1028 includes = [ "include/common" ]
1029 defines = [ "ENABLE_DOOM_MELON" ]
1032 executable("mything") {
1033 configs = [ ":myconfig" ]
1038 ## **copy**: Declare a target that copies files.
1040 ### **File name handling**
1043 All output files must be inside the output directory of the build.
1044 You would generally use |$target_out_dir| or |$target_gen_dir| to
1045 reference the output or generated intermediate file directories,
1048 Both "sources" and "outputs" must be specified. Sources can include
1049 as many files as you want, but there can only be one item in the
1050 outputs list (plural is used for the name for consistency with
1051 other target types).
1053 If there is more than one source file, your output name should specify
1054 a mapping from each source file to an output file name using source
1055 expansion (see "gn help source_expansion"). The placeholders will
1056 look like "{{source_name_part}}", for example.
1063 # Write a rule that copies a checked-in DLL to the output directory.
1065 sources = [ "mydll.dll" ]
1066 outputs = [ "$target_out_dir/mydll.dll" ]
1069 # Write a rule to copy several files to the target generated files
1072 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1074 # Use source expansion to generate output files with the
1075 # corresponding file names in the gen dir. This will just copy each
1077 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1082 ## **declare_args**: Declare build arguments.
1085 Introduces the given arguments into the current scope. If they are
1086 not specified on the command line or in a toolchain's arguments,
1087 the default values given in the declare_args block will be used.
1088 However, these defaults will not override command-line values.
1090 See also "gn help buildargs" for an overview.
1097 enable_teleporter = true
1098 enable_doom_melon = false
1101 If you want to override the (default disabled) Doom Melon:
1102 gn --args="enable_doom_melon=true enable_teleporter=false"
1103 This also sets the teleporter, but it's already defaulted to on so
1104 it will have no effect.
1108 ## **defined**: Returns whether an identifier is defined.
1111 Returns true if the given argument is defined. This is most useful in
1112 templates to assert that the caller set things up properly.
1114 You can pass an identifier:
1116 which will return true or false depending on whether foo is defined in
1119 You can also check a named scope:
1121 which will return true or false depending on whether bar is defined in
1122 the named scope foo. It will throw an error if foo is not defined or
1130 template("mytemplate") {
1131 # To help users call this template properly...
1132 assert(defined(invoker.sources), "Sources must be defined")
1134 # If we want to accept an optional "values" argument, we don't
1135 # want to dereference something that may not be defined.
1136 if (defined(invoker.values)) {
1137 values = invoker.values
1139 values = "some default value"
1145 ## **exec_script**: Synchronously run a script and return the output.
1148 exec_script(filename,
1150 input_conversion = "",
1151 file_dependencies = [])
1153 Runs the given script, returning the stdout of the script. The build
1154 generation will fail if the script does not exist or returns a nonzero
1157 The current directory when executing the script will be the root
1158 build directory. If you are passing file names, you will want to use
1159 the rebase_path() function to make file names relative to this
1160 path (see "gn help rebase_path").
1168 File name of python script to execute. Non-absolute names will
1169 be treated as relative to the current build file.
1172 A list of strings to be passed to the script as arguments.
1173 May be unspecified or the empty list which means no arguments.
1176 Controls how the file is read and parsed.
1177 See "gn help input_conversion".
1179 If unspecified, defaults to the empty string which causes the
1180 script result to be discarded. exec script will return None.
1183 (Optional) A list of files that this script reads or otherwise
1184 depends on. These dependencies will be added to the build result
1185 such that if any of them change, the build will be regenerated and
1186 the script will be re-run.
1188 The script itself will be an implicit dependency so you do not
1196 all_lines = exec_script(
1197 "myscript.py", [some_input], "list lines",
1198 [ rebase_path("data_file.txt", root_build_dir) ])
1200 # This example just calls the script with no arguments and discards
1202 exec_script("//foo/bar/myscript.py")
1206 ## **executable**: Declare an executable target.
1211 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1212 defines, include_dirs, ldflags, lib_dirs, libs
1213 precompiled_header, precompiled_source
1214 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1215 Dependent configs: all_dependent_configs, public_configs
1216 General: check_includes, configs, data, inputs, output_name,
1217 output_extension, public, sources, testonly, visibility
1221 ## **foreach**: Iterate over a list.
1224 foreach(<loop_var>, <list>) {
1228 Executes the loop contents block over each item in the list,
1229 assigning the loop_var to each item in sequence.
1231 The block does not introduce a new scope, so that variable assignments
1232 inside the loop will be visible once the loop terminates.
1234 The loop variable will temporarily shadow any existing variables with
1235 the same name for the duration of the loop. After the loop terminates
1236 the loop variable will no longer be in scope, and the previous value
1237 (if any) will be restored.
1244 mylist = [ "a", "b", "c" ]
1245 foreach(i, mylist) {
1256 ## **forward_variables_from**: Copies variables from a different scope.
1259 forward_variables_from(from_scope, variable_list_or_star)
1261 Copies the given variables from the given scope to the local scope
1262 if they exist. This is normally used in the context of templates to
1263 use the values of variables defined in the template invocation to
1264 a template-defined target.
1266 The variables in the given variable_list will be copied if they exist
1267 in the given scope or any enclosing scope. If they do not exist,
1268 nothing will happen and they be left undefined in the current scope.
1270 As a special case, if the variable_list is a string with the value of
1271 "*", all variables from the given scope will be copied. "*" only
1272 copies variables set directly on the from_scope, not enclosing ones.
1273 Otherwise it would duplicate all global variables.
1275 When an explicit list of variables is supplied, if the variable exists
1276 in the current (destination) scope already, an error will be thrown.
1277 If "*" is specified, variables in the current scope will be
1278 clobbered (the latter is important because most targets have an
1279 implicit configs list, which means it wouldn't work at all if it
1282 The sources assignment filter (see "gn help set_sources_assignment_filter")
1283 is never applied by this function. It's assumed than any desired
1284 filtering was already done when sources was set on the from_scope.
1291 # This is a common action template. It would invoke a script with
1292 # some given parameters, and wants to use the various types of deps
1293 # and the visibility from the invoker if it's defined. It also injects
1294 # an additional dependency to all targets.
1295 template("my_test") {
1296 action(target_name) {
1297 forward_variables_from(invoker, [ "data_deps", "deps",
1298 "public_deps", "visibility" ])
1299 # Add our test code to the dependencies.
1300 # "deps" may or may not be defined at this point.
1301 if (defined(deps)) {
1302 deps += [ "//tools/doom_melon" ]
1304 deps = [ "//tools/doom_melon" ]
1309 # This is a template around either a target whose type depends on a
1310 # global variable. It forwards all values from the invoker.
1311 template("my_wrapper") {
1312 target(my_wrapper_target_type, target_name) {
1313 forward_variables_from(invoker, "*")
1319 ## **get_label_info**: Get an attribute from a target's label.
1322 get_label_info(target_label, what)
1324 Given the label of a target, returns some attribute of that target.
1325 The target need not have been previously defined in the same file,
1326 since none of the attributes depend on the actual target definition,
1327 only the label itself.
1329 See also "gn help get_target_outputs".
1333 ### **Possible values for the "what" parameter**
1337 The short name of the target. This will match the value of the
1338 "target_name" variable inside that target's declaration. For the
1339 label "//foo/bar:baz" this will return "baz".
1342 The directory containing the target's definition, with no slash at
1343 the end. For the label "//foo/bar:baz" this will return
1347 The generated file directory for the target. This will match the
1348 value of the "target_gen_dir" variable when inside that target's
1352 The root of the generated file tree for the target. This will
1353 match the value of the "root_gen_dir" variable when inside that
1354 target's declaration.
1357 The output directory for the target. This will match the
1358 value of the "target_out_dir" variable when inside that target's
1362 The root of the output file tree for the target. This will
1363 match the value of the "root_gen_dir" variable when inside that
1364 target's declaration.
1366 "label_no_toolchain"
1367 The fully qualified version of this label, not including the
1368 toolchain. For the input ":bar" it might return
1371 "label_with_toolchain"
1372 The fully qualified version of this label, including the
1373 toolchain. For the input ":bar" it might return
1374 "//foo:bar(//toolchain:x64)".
1377 The label of the toolchain. This will match the value of the
1378 "current_toolchain" variable when inside that target's
1386 get_label_info(":foo", "name")
1387 # Returns string "foo".
1389 get_label_info("//foo/bar:baz", "gen_dir")
1390 # Returns string "//out/Debug/gen/foo/bar".
1394 ## **get_path_info**: Extract parts of a file or directory name.
1397 get_path_info(input, what)
1399 The first argument is either a string representing a file or
1400 directory name, or a list of such strings. If the input is a list
1401 the return value will be a list containing the result of applying the
1402 rule to each item in the input.
1406 ### **Possible values for the "what" parameter**
1410 The substring after the last slash in the path, including the name
1411 and extension. If the input ends in a slash, the empty string will
1413 "foo/bar.txt" => "bar.txt"
1414 "bar.txt" => "bar.txt"
1419 The substring of the file name not including the extension.
1420 "foo/bar.txt" => "bar"
1425 The substring following the last period following the last slash,
1426 or the empty string if not found. The period is not included.
1427 "foo/bar.txt" => "txt"
1431 The directory portion of the name, not including the slash.
1432 "foo/bar.txt" => "foo"
1433 "//foo/bar" => "//foo"
1436 The result will never end in a slash, so if the resulting
1437 is empty, the system ("/") or source ("//") roots, a "."
1438 will be appended such that it is always legal to append a slash
1439 and a filename and get a valid path.
1442 The output file directory corresponding to the path of the
1443 given file, not including a trailing slash.
1444 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
1446 The generated file directory corresponding to the path of the
1447 given file, not including a trailing slash.
1448 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
1451 The full absolute path name to the file or directory. It will be
1452 resolved relative to the current directory, and then the source-
1453 absolute version will be returned. If the input is system-
1454 absolute, the same input will be returned.
1455 "foo/bar.txt" => "//mydir/foo/bar.txt"
1456 "foo/" => "//mydir/foo/"
1457 "//foo/bar" => "//foo/bar" (already absolute)
1458 "/usr/include" => "/usr/include" (already absolute)
1460 If you want to make the path relative to another directory, or to
1461 be system-absolute, see rebase_path().
1467 sources = [ "foo.cc", "foo.h" ]
1468 result = get_path_info(source, "abspath")
1469 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
1471 result = get_path_info("//foo/bar/baz.cc", "dir")
1472 # result will be "//foo/bar"
1474 # Extract the source-absolute directory name,
1475 result = get_path_info(get_path_info(path, "dir"), "abspath")
1479 ## **get_target_outputs**: [file list] Get the list of outputs from a target.
1482 get_target_outputs(target_label)
1484 Returns a list of output files for the named target. The named target
1485 must have been previously defined in the current file before this
1486 function is called (it can't reference targets in other files because
1487 there isn't a defined execution order, and it obviously can't
1488 reference targets that are defined after the function call).
1490 Only copy and action targets are supported. The outputs from binary
1491 targets will depend on the toolchain definition which won't
1492 necessarily have been loaded by the time a given line of code has run,
1493 and source sets and groups have no useful output file.
1497 ### **Return value**
1500 The names in the resulting list will be absolute file paths (normally
1501 like "//out/Debug/bar.exe", depending on the build directory).
1503 action targets: this will just return the files specified in the
1504 "outputs" variable of the target.
1506 action_foreach targets: this will return the result of applying
1507 the output template to the sources (see "gn help source_expansion").
1508 This will be the same result (though with guaranteed absolute file
1509 paths), as process_file_template will return for those inputs
1510 (see "gn help process_file_template").
1512 binary targets (executables, libraries): this will return a list
1513 of the resulting binary file(s). The "main output" (the actual
1514 binary or library) will always be the 0th element in the result.
1515 Depending on the platform and output type, there may be other output
1516 files as well (like import libraries) which will follow.
1518 source sets and groups: this will return a list containing the path of
1519 the "stamp" file that Ninja will produce once all outputs are
1520 generated. This probably isn't very useful.
1527 # Say this action generates a bunch of C source files.
1528 action_foreach("my_action") {
1533 # Compile the resulting source files into a source set.
1534 source_set("my_lib") {
1535 sources = get_target_outputs(":my_action")
1540 ## **getenv**: Get an environment variable.
1543 value = getenv(env_var_name)
1545 Returns the value of the given enironment variable. If the value is
1546 not found, it will try to look up the variable with the "opposite"
1547 case (based on the case of the first letter of the variable), but
1548 is otherwise case-sensitive.
1550 If the environment variable is not found, the empty string will be
1551 returned. Note: it might be nice to extend this if we had the concept
1552 of "none" in the language to indicate lookup failure.
1559 home_dir = getenv("HOME")
1563 ## **group**: Declare a named group of targets.
1566 This target type allows you to create meta-targets that just collect a
1567 set of dependencies into one named target. Groups can additionally
1568 specify configs that apply to their dependents.
1570 Depending on a group is exactly like depending directly on that
1571 group's deps. Direct dependent configs will get automatically
1572 forwarded through the group so you shouldn't need to use
1573 "forward_dependent_configs_from.
1580 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1581 Dependent configs: all_dependent_configs, public_configs
1591 "//project:unit_tests",
1597 ## **import**: Import a file into the current scope.
1600 The import command loads the rules and variables resulting from
1601 executing the given file into the current scope.
1603 By convention, imported files are named with a .gni extension.
1605 An import is different than a C++ "include". The imported file is
1606 executed in a standalone environment from the caller of the import
1607 command. The results of this execution are cached for other files that
1608 import the same .gni file.
1610 Note that you can not import a BUILD.gn file that's otherwise used
1611 in the build. Files must either be imported or implicitly loaded as
1612 a result of deps rules, but not both.
1614 The imported file's scope will be merged with the scope at the point
1615 import was called. If there is a conflict (both the current scope and
1616 the imported file define some variable or rule with the same name but
1617 different value), a runtime error will be thrown. Therefore, it's good
1618 practice to minimize the stuff that an imported file defines.
1620 Variables and templates beginning with an underscore '_' are
1621 considered private and will not be imported. Imported files can use
1622 such variables for internal computation without affecting other files.
1629 import("//build/rules/idl_compilation_rule.gni")
1631 # Looks in the current directory.
1632 import("my_vars.gni")
1636 ## **print**: Prints to the console.
1639 Prints all arguments to the console separated by spaces. A newline is
1640 automatically appended to the end.
1642 This function is intended for debugging. Note that build files are run
1643 in parallel so you may get interleaved prints. A buildfile may also
1644 be executed more than once in parallel in the context of different
1645 toolchains so the prints from one file may be duplicated or
1646 interleaved with itself.
1652 print("Hello world")
1654 print(sources, deps)
1658 ## **process_file_template**: Do template expansion over a list of files.
1661 process_file_template(source_list, template)
1663 process_file_template applies a template list to a source file list,
1664 returning the result of applying each template to each source. This is
1665 typically used for computing output file names from input files.
1667 In most cases, get_target_outputs() will give the same result with
1668 shorter, more maintainable code. This function should only be used
1669 when that function can't be used (like there's no target or the target
1670 is defined in another build file).
1677 The source_list is a list of file names.
1679 The template can be a string or a list. If it is a list, multiple
1680 output strings are generated for each input.
1682 The template should contain source expansions to which each name in
1683 the source list is applied. See "gn help source_expansion".
1694 myoutputs = process_file_template(
1696 [ "$target_gen_dir/{{source_name_part}}.cc",
1697 "$target_gen_dir/{{source_name_part}}.h" ])
1699 The result in this case will be:
1700 [ "//out/Debug/foo.cc"
1702 "//out/Debug/bar.cc"
1703 "//out/Debug/bar.h" ]
1707 ## **read_file**: Read a file into a variable.
1710 read_file(filename, input_conversion)
1712 Whitespace will be trimmed from the end of the file. Throws an error
1713 if the file can not be opened.
1721 Filename to read, relative to the build file.
1724 Controls how the file is read and parsed.
1725 See "gn help input_conversion".
1731 lines = read_file("foo.txt", "list lines")
1735 ## **rebase_path**: Rebase a file or directory to another location.
1738 converted = rebase_path(input,
1742 Takes a string argument representing a file name, or a list of such
1743 strings and converts it/them to be relative to a different base
1746 When invoking the compiler or scripts, GN will automatically convert
1747 sources and include directories to be relative to the build directory.
1748 However, if you're passing files directly in the "args" array or
1749 doing other manual manipulations where GN doesn't know something is
1750 a file name, you will need to convert paths to be relative to what
1751 your tool is expecting.
1753 The common case is to use this to convert paths relative to the
1754 current directory to be relative to the build directory (which will
1755 be the current directory when executing scripts).
1757 If you want to convert a file path to be source-absolute (that is,
1758 beginning with a double slash like "//foo/bar"), you should use
1759 the get_path_info() function. This function won't work because it will
1760 always make relative paths, and it needs to support making paths
1761 relative to the source root, so can't also generate source-absolute
1762 paths without more special-cases.
1770 A string or list of strings representing file or directory names
1771 These can be relative paths ("foo/bar.txt"), system absolute
1772 paths ("/foo/bar.txt"), or source absolute paths
1776 The directory to convert the paths to be relative to. This can be
1777 an absolute path or a relative path (which will be treated
1778 as being relative to the current BUILD-file's directory).
1780 As a special case, if new_base is the empty string (the default),
1781 all paths will be converted to system-absolute native style paths
1782 with system path separators. This is useful for invoking external
1786 Directory representing the base for relative paths in the input.
1787 If this is not an absolute path, it will be treated as being
1788 relative to the current build file. Use "." (the default) to
1789 convert paths from the current BUILD-file's directory.
1793 ### **Return value**
1796 The return value will be the same type as the input value (either a
1797 string or a list of strings). All relative and source-absolute file
1798 names will be converted to be relative to the requested output
1799 System-absolute paths will be unchanged.
1806 # Convert a file in the current directory to be relative to the build
1807 # directory (the current dir when executing compilers and scripts).
1808 foo = rebase_path("myfile.txt", root_build_dir)
1809 # might produce "../../project/myfile.txt".
1811 # Convert a file to be system absolute:
1812 foo = rebase_path("myfile.txt")
1813 # Might produce "D:\source\project\myfile.txt" on Windows or
1814 # "/home/you/source/project/myfile.txt" on Linux.
1816 # Typical usage for converting to the build directory for a script.
1817 action("myscript") {
1818 # Don't convert sources, GN will automatically convert these to be
1819 # relative to the build directory when it constructs the command
1820 # line for your script.
1821 sources = [ "foo.txt", "bar.txt" ]
1823 # Extra file args passed manually need to be explicitly converted
1824 # to be relative to the build directory:
1827 rebase_path("//mything/data/input.dat", root_build_dir),
1829 rebase_path("relative_path.txt", root_build_dir)
1830 ] + rebase_path(sources, root_build_dir)
1835 ## **set_default_toolchain**: Sets the default toolchain name.
1838 set_default_toolchain(toolchain_label)
1840 The given label should identify a toolchain definition (see
1841 "help toolchain"). This toolchain will be used for all targets
1842 unless otherwise specified.
1844 This function is only valid to call during the processing of the build
1845 configuration file. Since the build configuration file is processed
1846 separately for each toolchain, this function will be a no-op when
1847 called under any non-default toolchains.
1849 For example, the default toolchain should be appropriate for the
1850 current environment. If the current environment is 32-bit and
1851 somebody references a target with a 64-bit toolchain, we wouldn't
1852 want processing of the build config file for the 64-bit toolchain to
1853 reset the default toolchain to 64-bit, we want to keep it 32-bits.
1868 set_default_toolchain("//build/config/win:vs32")
1871 ## **set_defaults**: Set default values for a target type.
1874 set_defaults(<target_type_name>) { <values...> }
1876 Sets the default values for a given target type. Whenever
1877 target_type_name is seen in the future, the values specified in
1878 set_default's block will be copied into the current scope.
1880 When the target type is used, the variable copying is very strict.
1881 If a variable with that name is already in scope, the build will fail
1884 set_defaults can be used for built-in target types ("executable",
1885 "shared_library", etc.) and custom ones defined via the "template"
1892 set_defaults("static_library") {
1893 configs = [ "//tools/mything:settings" ]
1896 static_library("mylib")
1897 # The configs will be auto-populated as above. You can remove it if
1898 # you don't want the default for a particular default:
1899 configs -= "//tools/mything:settings"
1904 ## **set_sources_assignment_filter**: Set a pattern to filter source files.
1907 The sources assignment filter is a list of patterns that remove files
1908 from the list implicitly whenever the "sources" variable is
1909 assigned to. This is intended to be used to globally filter out files
1910 with platform-specific naming schemes when they don't apply, for
1911 example, you may want to filter out all "*_win.cc" files on non-
1914 Typically this will be called once in the master build config script
1915 to set up the filter for the current platform. Subsequent calls will
1916 overwrite the previous values.
1918 If you want to bypass the filter and add a file even if it might
1919 be filtered out, call set_sources_assignment_filter([]) to clear the
1920 list of filters. This will apply until the current scope exits
1924 ### **How to use patterns**
1927 File patterns are VERY limited regular expressions. They must match
1928 the entire input string to be counted as a match. In regular
1929 expression parlance, there is an implicit "^...$" surrounding your
1930 input. If you want to match a substring, you need to use wildcards at
1931 the beginning and end.
1933 There are only two special tokens understood by the pattern matcher.
1934 Everything else is a literal.
1936 * Matches zero or more of any character. It does not depend on the
1937 preceding character (in regular expression parlance it is
1938 equivalent to ".*").
1940 \b Matches a path boundary. This will match the beginning or end of
1941 a string, or a slash.
1945 ### **Pattern examples**
1949 Matches a string containing "asdf" anywhere.
1952 Matches only the exact string "asdf".
1955 Matches strings ending in the literal ".cc".
1958 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
1962 ### **Sources assignment example**
1965 # Filter out all _win files.
1966 set_sources_assignment_filter([ "*_win.cc", "*_win.h" ])
1967 sources = [ "a.cc", "b_win.cc" ]
1969 # Will print [ "a.cc" ]. b_win one was filtered out.
1973 ## **shared_library**: Declare a shared library target.
1976 A shared library will be specified on the linker line for targets
1977 listing the shared library in its "deps". If you don't want this
1978 (say you dynamically load the library at runtime), then you should
1979 depend on the shared library via "data_deps" instead.
1986 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1987 defines, include_dirs, ldflags, lib_dirs, libs
1988 precompiled_header, precompiled_source
1989 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1990 Dependent configs: all_dependent_configs, public_configs
1991 General: check_includes, configs, data, inputs, output_name,
1992 output_extension, public, sources, testonly, visibility
1996 ## **source_set**: Declare a source set target.
1999 A source set is a collection of sources that get compiled, but are not
2000 linked to produce any kind of library. Instead, the resulting object
2001 files are implicitly added to the linker line of all targets that
2002 depend on the source set.
2004 In most cases, a source set will behave like a static library, except
2005 no actual library file will be produced. This will make the build go
2006 a little faster by skipping creation of a large static library, while
2007 maintaining the organizational benefits of focused build targets.
2009 The main difference between a source set and a static library is
2010 around handling of exported symbols. Most linkers assume declaring
2011 a function exported means exported from the static library. The linker
2012 can then do dead code elimination to delete code not reachable from
2015 A source set will not do this code elimination since there is no link
2016 step. This allows you to link many sources sets into a shared library
2017 and have the "exported symbol" notation indicate "export from the
2018 final shared library and not from the intermediate targets." There is
2019 no way to express this concept when linking multiple static libraries
2020 into a shared library.
2027 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2028 defines, include_dirs, ldflags, lib_dirs, libs
2029 precompiled_header, precompiled_source
2030 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
2031 Dependent configs: all_dependent_configs, public_configs
2032 General: check_includes, configs, data, inputs, output_name,
2033 output_extension, public, sources, testonly, visibility
2037 ## **static_library**: Declare a static library target.
2040 Make a ".a" / ".lib" file.
2042 If you only need the static library for intermediate results in the
2043 build, you should consider a source_set instead since it will skip
2044 the (potentially slow) step of creating the intermediate library file.
2051 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
2052 defines, include_dirs, ldflags, lib_dirs, libs
2053 precompiled_header, precompiled_source
2054 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
2055 Dependent configs: all_dependent_configs, public_configs
2056 General: check_includes, configs, data, inputs, output_name,
2057 output_extension, public, sources, testonly, visibility
2061 ## **target**: Declare an target with the given programmatic type.
2064 target(target_type_string, target_name_string) { ... }
2066 The target() function is a way to invoke a built-in target or template
2067 with a type determined at runtime. This is useful for cases where the
2068 type of a target might not be known statically.
2070 Only templates and built-in target functions are supported for the
2071 target_type_string parameter. Arbitrary functions, configs, and
2072 toolchains are not supported.
2075 target("source_set", "doom_melon") {
2077 source_set("doom_melon") {
2084 if (foo_build_as_shared) {
2085 my_type = "shared_library"
2087 my_type = "source_set"
2090 target(my_type, "foo") {
2096 ## **template**: Define a template rule.
2099 A template defines a custom name that acts like a function. It
2100 provides a way to add to the built-in target types.
2102 The template() function is used to declare a template. To invoke the
2103 template, just use the name of the template like any other target
2106 Often you will want to declare your template in a special file that
2107 other files will import (see "gn help import") so your template
2108 rule can be shared across build files.
2112 ### **Variables and templates**:
2115 When you call template() it creates a closure around all variables
2116 currently in scope with the code in the template block. When the
2117 template is invoked, the closure will be executed.
2119 When the template is invoked, the code in the caller is executed and
2120 passed to the template code as an implicit "invoker" variable. The
2121 template uses this to read state out of the invoking code.
2123 One thing explicitly excluded from the closure is the "current
2124 directory" against which relative file names are resolved. The
2125 current directory will be that of the invoking code, since typically
2126 that code specifies the file names. This means all files internal
2127 to the template should use absolute names.
2129 A template will typically forward some or all variables from the
2130 invoking scope to a target that it defines. Often, such variables
2131 might be optional. Use the pattern:
2133 if (defined(invoker.deps)) {
2137 The function forward_variables_from() provides a shortcut to forward
2138 one or more or possibly all variables in this manner:
2140 forward_variables_from(invoker, ["deps", "public_deps"])
2144 ### **Target naming**:
2147 Your template should almost always define a built-in target with the
2148 name the template invoker specified. For example, if you have an IDL
2149 template and somebody does:
2151 you will normally want this to expand to something defining a
2152 source_set or static_library named "foo" (among other things you may
2153 need). This way, when another target specifies a dependency on
2154 "foo", the static_library or source_set will be linked.
2156 It is also important that any other targets your template expands to
2157 have globally unique names, or you will get collisions.
2159 Access the invoking name in your template via the implicit
2160 "target_name" variable. This should also be the basis for how other
2161 targets that a template expands to ensure uniqueness.
2163 A typical example would be a template that defines an action to
2164 generate some source files, and a source_set to compile that source.
2165 Your template would name the source_set "target_name" because
2166 that's what you want external targets to depend on to link your code.
2167 And you would name the action something like "${target_name}_action"
2168 to make it unique. The source set would have a dependency on the
2169 action to make it run.
2173 ### **Example of defining a template**:
2176 template("my_idl") {
2177 # Be nice and help callers debug problems by checking that the
2178 # variables the template requires are defined. This gives a nice
2179 # message rather than giving the user an error about an
2180 # undefined variable in the file defining the template
2182 # You can also use defined() to give default values to variables
2183 # unspecified by the invoker.
2184 assert(defined(invoker.sources),
2185 "Need sources in $target_name listing the idl files.")
2187 # Name of the intermediate target that does the code gen. This must
2188 # incorporate the target name so it's unique across template
2190 code_gen_target_name = target_name + "_code_gen"
2192 # Intermediate target to convert IDL to C source. Note that the name
2193 # is based on the name the invoker of the template specified. This
2194 # way, each time the template is invoked we get a unique
2195 # intermediate action name (since all target names are in the global
2197 action_foreach(code_gen_target_name) {
2198 # Access the scope defined by the invoker via the implicit
2199 # "invoker" variable.
2200 sources = invoker.sources
2202 # Note that we need an absolute path for our script file name.
2203 # The current directory when executing this code will be that of
2204 # the invoker (this is why we can use the "sources" directly
2205 # above without having to rebase all of the paths). But if we need
2206 # to reference a script relative to the template file, we'll need
2207 # to use an absolute path instead.
2208 script = "//tools/idl/idl_code_generator.py"
2210 # Tell GN how to expand output names given the sources.
2211 # See "gn help source_expansion" for more.
2212 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
2213 "$target_gen_dir/{{source_name_part}}.h" ]
2216 # Name the source set the same as the template invocation so
2217 # instancing this template produces something that other targets
2218 # can link to in their deps.
2219 source_set(target_name) {
2220 # Generates the list of sources, we get these from the
2221 # action_foreach above.
2222 sources = get_target_outputs(":$code_gen_target_name")
2224 # This target depends on the files produced by the above code gen
2226 deps = [ ":$code_gen_target_name" ]
2232 ### **Example of invoking the resulting template**:
2235 # This calls the template code above, defining target_name to be
2236 # "foo_idl_files" and "invoker" to be the set of stuff defined in
2237 # the curly brackets.
2238 my_idl("foo_idl_files") {
2239 # Goes into the template as "invoker.sources".
2240 sources = [ "foo.idl", "bar.idl" ]
2243 # Here is a target that depends on our template.
2244 executable("my_exe") {
2245 # Depend on the name we gave the template call above. Internally,
2246 # this will produce a dependency from executable to the source_set
2247 # inside the template (since it has this name), which will in turn
2248 # depend on the code gen action.
2249 deps = [ ":foo_idl_files" ]
2254 ## **tool**: Specify arguments to a toolchain tool.
2271 "objc": Objective C compiler
2272 "objcxx": Objective C++ compiler
2273 "rc": Resource compiler (Windows .rc files)
2277 "alink": Linker for static libraries (archives)
2278 "solink": Linker for shared libraries
2279 "link": Linker for executables
2282 "stamp": Tool for creating stamp files
2283 "copy": Tool to copy files.
2287 ### **Tool variables**
2290 command [string with substitutions]
2291 Valid for: all tools (required)
2295 default_output_extension [string]
2296 Valid for: linker tools
2298 Extension for the main output of a linkable tool. It includes
2299 the leading dot. This will be the default value for the
2300 {{output_extension}} expansion (discussed below) but will be
2301 overridden by by the "output extension" variable in a target,
2302 if one is specified. Empty string means no extension.
2304 GN doesn't actually do anything with this extension other than
2305 pass it along, potentially with target-specific overrides. One
2306 would typically use the {{output_extension}} value in the
2307 "outputs" to read this value.
2309 Example: default_output_extension = ".exe"
2312 Valid for: compiler tools (optional)
2314 If the tool can write ".d" files, this specifies the name of
2315 the resulting file. These files are used to list header file
2316 dependencies (or other implicit input dependencies) that are
2317 discovered at build time. See also "depsformat".
2319 Example: depfile = "{{output}}.d"
2322 Valid for: compiler tools (when depfile is specified)
2324 Format for the deps outputs. This is either "gcc" or "msvc".
2325 See the ninja documentation for "deps" for more information.
2327 Example: depsformat = "gcc"
2329 description [string with substitutions, optional]
2330 Valid for: all tools
2332 What to print when the command is run.
2334 Example: description = "Compiling {{source}}"
2336 lib_switch [string, optional, link tools only]
2337 lib_dir_switch [string, optional, link tools only]
2338 Valid for: Linker tools except "alink"
2340 These strings will be prepended to the libraries and library
2341 search directories, respectively, because linkers differ on how
2342 specify them. If you specified:
2344 lib_dir_switch = "-L"
2345 then the "{{libs}}" expansion for [ "freetype", "expat"]
2346 would be "-lfreetype -lexpat".
2348 outputs [list of strings with substitutions]
2349 Valid for: Linker and compiler tools (required)
2351 An array of names for the output files the tool produces. These
2352 are relative to the build output directory. There must always be
2353 at least one output file. There can be more than one output (a
2354 linker might produce a library and an import library, for
2357 This array just declares to GN what files the tool will
2358 produce. It is your responsibility to specify the tool command
2359 that actually produces these files.
2361 If you specify more than one output for shared library links,
2362 you should consider setting link_output and depend_output.
2363 Otherwise, the first entry in the outputs list should always be
2364 the main output which will be linked to.
2366 Example for a compiler tool that produces .obj files:
2368 "{{source_out_dir}}/{{source_name_part}}.obj"
2371 Example for a linker tool that produces a .dll and a .lib. The
2372 use of {{output_extension}} rather than hardcoding ".dll"
2373 allows the extension of the library to be overridden on a
2374 target-by-target basis, but in this example, it always
2375 produces a ".lib" import library:
2377 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2378 "{{root_out_dir}}/{{target_output_name}}.lib",
2381 link_output [string with substitutions]
2382 depend_output [string with substitutions]
2383 Valid for: "solink" only (optional)
2385 These two files specify whch of the outputs from the solink
2386 tool should be used for linking and dependency tracking. These
2387 should match entries in the "outputs". If unspecified, the
2388 first item in the "outputs" array will be used for both. See
2389 "Separate linking and dependencies for shared libraries"
2392 On Windows, where the tools produce a .dll shared library and
2393 a .lib import library, you will want both of these to be the
2394 import library. On Linux, if you're not doing the separate
2395 linking/dependency optimization, both of these should be the
2398 output_prefix [string]
2399 Valid for: Linker tools (optional)
2401 Prefix to use for the output name. Defaults to empty. This
2402 prefix will be prepended to the name of the target (or the
2403 output_name if one is manually specified for it) if the prefix
2404 is not already there. The result will show up in the
2405 {{output_name}} substitution pattern.
2407 This is typically used to prepend "lib" to libraries on
2409 output_prefix = "lib"
2411 precompiled_header_type [string]
2412 Valid for: "cc", "cxx", "objc", "objcxx"
2414 Type of precompiled headers. If undefined or the empty string,
2415 precompiled headers will not be used for this tool. Otherwise
2416 use "msvc" which is the only currently supported value.
2418 For precompiled headers to be used for a given target, the
2419 target (or a config applied to it) must also specify a
2420 "precompiled_header" and, for "msvc"-style headers, a
2421 "precompiled_source" value.
2423 See "gn help precompiled_header" for more.
2426 Valid for: all tools (optional, defaults to false)
2428 Requests that Ninja check the file timestamp after this tool has
2429 run to determine if anything changed. Set this if your tool has
2430 the ability to skip writing output if the output file has not
2433 Normally, Ninja will assume that when a tool runs the output
2434 be new and downstream dependents must be rebuild. When this is
2435 set to trye, Ninja can skip rebuilding downstream dependents for
2436 input changes that don't actually affect the output.
2441 rspfile [string with substitutions]
2442 Valid for: all tools (optional)
2444 Name of the response file. If empty, no response file will be
2445 used. See "rspfile_content".
2447 rspfile_content [string with substitutions]
2448 Valid for: all tools (required when "rspfile" is specified)
2450 The contents to be written to the response file. This may
2451 include all or part of the command to send to the tool which
2452 allows you to get around OS command-line length limits.
2454 This example adds the inputs and libraries to a response file,
2455 but passes the linker flags directly on the command line:
2457 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
2458 rspfile = "{{output}}.rsp"
2459 rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
2464 ### **Expansions for tool variables**
2467 All paths are relative to the root build directory, which is the
2468 current directory for running all tools. These expansions are
2469 available to all tools:
2472 The label of the current target. This is typically used in the
2473 "description" field for link tools. The toolchain will be
2474 omitted from the label for targets in the default toolchain, and
2475 will be included for targets in other toolchains.
2478 The relative path and name of the output(s) of the current
2479 build step. If there is more than one output, this will expand
2480 to a list of all of them.
2481 Example: "out/base/my_file.o"
2485 The directory of the generated file and output directories,
2486 respectively, for the current target. There is no trailing
2488 Example: "out/base/test"
2490 {{target_output_name}}
2491 The short name of the current target with no path information,
2492 or the value of the "output_name" variable if one is specified
2493 in the target. This will include the "output_prefix" if any.
2494 Example: "libfoo" for the target named "foo" and an
2495 output prefix for the linker tool of "lib".
2497 Compiler tools have the notion of a single input and a single output,
2498 along with a set of compiler-specific flags. The following expansions
2508 Strings correspond that to the processed flags/defines/include
2509 directories specified for the target.
2510 Example: "--enable-foo --enable-bar"
2512 Defines will be prefixed by "-D" and include directories will
2513 be prefixed by "-I" (these work with Posix tools as well as
2517 The relative path and name of the current input file.
2518 Example: "../../base/my_file.cc"
2520 {{source_file_part}}
2521 The file part of the source including the extension (with no
2522 directory information).
2525 {{source_name_part}}
2526 The filename part of the source file with no directory or
2532 The directory in the generated file and output directories,
2533 respectively, for the current input file. If the source file
2534 is in the same directory as the target is declared in, they will
2535 will be the same as the "target" versions above.
2536 Example: "gen/base/test"
2538 Linker tools have multiple inputs and (potentially) multiple outputs
2539 The following expansions are available:
2543 Expands to the inputs to the link step. This will be a list of
2544 object files and static libraries.
2545 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
2547 The "_newline" version will separate the input files with
2548 newlines instead of spaces. This is useful in response files:
2549 some linkers can take a "-filelist" flag which expects newline
2550 separated files, and some Microsoft tools have a fixed-sized
2551 buffer for parsing each line of a response file.
2554 Expands to the processed set of ldflags and library search paths
2555 specified for the target.
2556 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
2559 Expands to the list of system libraries to link to. Each will
2560 be prefixed by the "lib_prefix".
2562 As a special case to support Mac, libraries with names ending in
2563 ".framework" will be added to the {{libs}} with "-framework"
2564 preceeding it, and the lib prefix will be ignored.
2566 Example: "-lfoo -lbar"
2568 {{output_extension}}
2569 The value of the "output_extension" variable in the target,
2570 or the value of the "default_output_extension" value in the
2571 tool if the target does not specify an output extension.
2575 Extra libraries from shared library dependencide not specified
2576 in the {{inputs}}. This is the list of link_output files from
2577 shared libraries (if the solink tool specifies a "link_output"
2578 variable separate from the "depend_output").
2580 These should generally be treated the same as libs by your tool.
2581 Example: "libfoo.so libbar.so"
2583 The copy tool allows the common compiler/linker substitutions, plus
2584 {{source}} which is the source of the copy. The stamp tool allows
2585 only the common tool substitutions.
2589 ### **Separate linking and dependencies for shared libraries**
2592 Shared libraries are special in that not all changes to them require
2593 that dependent targets be re-linked. If the shared library is changed
2594 but no imports or exports are different, dependent code needn't be
2595 relinked, which can speed up the build.
2597 If your link step can output a list of exports from a shared library
2598 and writes the file only if the new one is different, the timestamp of
2599 this file can be used for triggering re-links, while the actual shared
2600 library would be used for linking.
2602 You will need to specify
2604 in the linker tool to make this work, so Ninja will detect if the
2605 timestamp of the dependency file has changed after linking (otherwise
2606 it will always assume that running a command updates the output):
2611 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2612 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC",
2615 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
2617 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC"
2626 toolchain("my_toolchain") {
2627 # Put these at the top to apply to all tools below.
2629 lib_dir_prefix = "-L"
2632 command = "gcc {{source}} -o {{output}}"
2633 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2634 description = "GCC {{source}}"
2637 command = "g++ {{source}} -o {{output}}"
2638 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2639 description = "G++ {{source}}"
2645 ## **toolchain**: Defines a toolchain.
2648 A toolchain is a set of commands and build flags used to compile the
2649 source code. You can have more than one toolchain in use at once in
2654 ### **Functions and variables**
2658 The tool() function call specifies the commands commands to run for
2659 a given step. See "gn help tool".
2662 List of arguments to pass to the toolchain when invoking this
2663 toolchain. This applies only to non-default toolchains. See
2664 "gn help toolchain_args" for more.
2667 Dependencies of this toolchain. These dependencies will be resolved
2668 before any target in the toolchain is compiled. To avoid circular
2669 dependencies these must be targets defined in another toolchain.
2671 This is expressed as a list of targets, and generally these targets
2672 will always specify a toolchain:
2673 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
2675 This concept is somewhat inefficient to express in Ninja (it
2676 requires a lot of duplicate of rules) so should only be used when
2677 absolutely necessary.
2680 In integer expressing the number of links that Ninja will perform in
2681 parallel. GN will create a pool for shared library and executable
2682 link steps with this many processes. Since linking is memory- and
2683 I/O-intensive, projects with many large targets may want to limit
2684 the number of parallel steps to avoid overloading the computer.
2685 Since creating static libraries is generally not as intensive
2686 there is no limit to "alink" steps.
2688 Defaults to 0 which Ninja interprets as "no limit".
2690 The value used will be the one from the default toolchain of the
2695 ### **Invoking targets in toolchains**:
2698 By default, when a target depends on another, there is an implicit
2699 toolchain label that is inherited, so the dependee has the same one
2702 You can override this and refer to any other toolchain by explicitly
2703 labeling the toolchain to use. For example:
2704 data_deps = [ "//plugins:mine(//toolchains:plugin_toolchain)" ]
2705 The string "//build/toolchains:plugin_toolchain" is a label that
2706 identifies the toolchain declaration for compiling the sources.
2708 To load a file in an alternate toolchain, GN does the following:
2710 1. Loads the file with the toolchain definition in it (as determined
2711 by the toolchain label).
2712 2. Re-runs the master build configuration file, applying the
2713 arguments specified by the toolchain_args section of the toolchain
2714 definition (see "gn help toolchain_args").
2715 3. Loads the destination build file in the context of the
2716 configuration file in the previous step.
2722 toolchain("plugin_toolchain") {
2723 concurrent_links = 8
2726 command = "gcc {{source}}"
2739 ## **toolchain_args**: Set build arguments for toolchain build setup.
2742 Used inside a toolchain definition to pass arguments to an alternate
2743 toolchain's invocation of the build.
2745 When you specify a target using an alternate toolchain, the master
2746 build configuration file is re-interpreted in the context of that
2747 toolchain (see "gn help toolchain"). The toolchain_args function
2748 allows you to control the arguments passed into this alternate
2749 invocation of the build.
2751 Any default system arguments or arguments passed in on the command-
2752 line will also be passed to the alternate invocation unless explicitly
2753 overridden by toolchain_args.
2755 The toolchain_args will be ignored when the toolchain being defined
2756 is the default. In this case, it's expected you want the default
2759 See also "gn help buildargs" for an overview of these arguments.
2765 toolchain("my_weird_toolchain") {
2768 # Override the system values for a generic Posix system.
2772 # Pass this new value for specific setup for my toolchain.
2773 is_my_weird_system = true
2779 ## **write_file**: Write a file to disk.
2782 write_file(filename, data)
2784 If data is a list, the list will be written one-item-per-line with no
2785 quoting or brackets.
2787 If the file exists and the contents are identical to that being
2788 written, the file will not be updated. This will prevent unnecessary
2789 rebuilds of targets that depend on this file.
2791 TODO(brettw) we probably need an optional third argument to control
2800 Filename to write. This must be within the output directory.
2803 The list or string to write.
2807 ## **current_cpu**: The processor architecture of the current toolchain.
2810 The build configuration usually sets this value based on the value
2811 of "host_cpu" (see "gn help host_cpu") and then threads
2812 this through the toolchain definitions to ensure that it always
2813 reflects the appropriate value.
2815 This value is not used internally by GN for any purpose. It is
2816 set it to the empty string ("") by default but is declared so
2817 that it can be overridden on the command line if so desired.
2819 See "gn help target_cpu" for a list of common values returned.
2823 ## **current_os**: The operating system of the current toolchain.
2826 The build configuration usually sets this value based on the value
2827 of "target_os" (see "gn help target_os"), and then threads this
2828 through the toolchain definitions to ensure that it always reflects
2829 the appropriate value.
2831 This value is not used internally by GN for any purpose. It is
2832 set it to the empty string ("") by default but is declared so
2833 that it can be overridden on the command line if so desired.
2835 See "gn help target_os" for a list of common values returned.
2839 ## **current_toolchain**: Label of the current toolchain.
2842 A fully-qualified label representing the current toolchain. You can
2843 use this to make toolchain-related decisions in the build. See also
2844 "default_toolchain".
2851 if (current_toolchain == "//build:64_bit_toolchain") {
2852 executable("output_thats_64_bit_only") {
2857 ## **default_toolchain**: [string] Label of the default toolchain.
2860 A fully-qualified label representing the default toolchain, which may
2861 not necessarily be the current one (see "current_toolchain").
2865 ## **host_cpu**: The processor architecture that GN is running on.
2868 This is value is exposed so that cross-compile toolchains can
2869 access the host architecture when needed.
2871 The value should generally be considered read-only, but it can be
2872 overriden in order to handle unusual cases where there might
2873 be multiple plausible values for the host architecture (e.g., if
2874 you can do either 32-bit or 64-bit builds). The value is not used
2875 internally by GN for any purpose.
2879 ### **Some possible values**:
2886 ## **host_os**: [string] The operating system that GN is running on.
2889 This value is exposed so that cross-compiles can access the host
2890 build system's settings.
2892 This value should generally be treated as read-only. It, however,
2893 is not used internally by GN for any purpose.
2897 ### **Some possible values**:
2905 ## **python_path**: Absolute path of Python.
2908 Normally used in toolchain definitions if running some command
2909 requires Python. You will normally not need this when invoking scripts
2910 since GN automatically finds it for you.
2914 ## **root_build_dir**: [string] Directory where build commands are run.
2917 This is the root build output directory which will be the current
2918 directory when executing all compilers and scripts.
2920 Most often this is used with rebase_path (see "gn help rebase_path")
2921 to convert arguments to be relative to a script's current directory.
2925 ## **root_gen_dir**: Directory for the toolchain's generated files.
2928 Absolute path to the root of the generated output directory tree for
2929 the current toolchain. An example would be "//out/Debug/gen" for the
2930 default toolchain, or "//out/Debug/arm/gen" for the "arm"
2933 This is primarily useful for setting up include paths for generated
2934 files. If you are passing this to a script, you will want to pass it
2935 through rebase_path() (see "gn help rebase_path") to convert it
2936 to be relative to the build directory.
2938 See also "target_gen_dir" which is usually a better location for
2939 generated files. It will be inside the root generated dir.
2943 ## **root_out_dir**: [string] Root directory for toolchain output files.
2946 Absolute path to the root of the output directory tree for the current
2947 toolchain. It will not have a trailing slash.
2949 For the default toolchain this will be the same as the root_build_dir.
2950 An example would be "//out/Debug" for the default toolchain, or
2951 "//out/Debug/arm" for the "arm" toolchain.
2953 This is primarily useful for setting up script calls. If you are
2954 passing this to a script, you will want to pass it through
2955 rebase_path() (see "gn help rebase_path") to convert it
2956 to be relative to the build directory.
2958 See also "target_out_dir" which is usually a better location for
2959 output files. It will be inside the root output dir.
2966 action("myscript") {
2967 # Pass the output dir to the script.
2968 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
2973 ## **target_cpu**: The desired cpu architecture for the build.
2976 This value should be used to indicate the desired architecture for
2977 the primary objects of the build. It will match the cpu architecture
2978 of the default toolchain.
2980 In many cases, this is the same as "host_cpu", but in the case
2981 of cross-compiles, this can be set to something different. This
2982 value is different from "current_cpu" in that it can be referenced
2983 from inside any toolchain. This value can also be ignored if it is
2984 not needed or meaningful for a project.
2986 This value is not used internally by GN for any purpose, so it
2987 may be set to whatever value is needed for the build.
2988 GN defaults this value to the empty string ("") and the
2989 configuration files should set it to an appropriate value
2990 (e.g., setting it to the value of "host_cpu") if it is not
2991 overridden on the command line or in the args.gn file.
2993 Where practical, use one of the following list of common values:
2997 ### **Possible values**:
3007 ## **target_gen_dir**: Directory for a target's generated files.
3010 Absolute path to the target's generated file directory. This will be
3011 the "root_gen_dir" followed by the relative path to the current
3012 build file. If your file is in "//tools/doom_melon" then
3013 target_gen_dir would be "//out/Debug/gen/tools/doom_melon". It will
3014 not have a trailing slash.
3016 This is primarily useful for setting up include paths for generated
3017 files. If you are passing this to a script, you will want to pass it
3018 through rebase_path() (see "gn help rebase_path") to convert it
3019 to be relative to the build directory.
3021 See also "gn help root_gen_dir".
3028 action("myscript") {
3029 # Pass the generated output dir to the script.
3030 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
3035 ## **target_os**: The desired operating system for the build.
3038 This value should be used to indicate the desired operating system
3039 for the primary object(s) of the build. It will match the OS of
3040 the default toolchain.
3042 In many cases, this is the same as "host_os", but in the case of
3043 cross-compiles, it may be different. This variable differs from
3044 "current_os" in that it can be referenced from inside any
3045 toolchain and will always return the initial value.
3047 This should be set to the most specific value possible. So,
3048 "android" or "chromeos" should be used instead of "linux"
3049 where applicable, even though Android and ChromeOS are both Linux
3050 variants. This can mean that one needs to write
3052 if (target_os == "android" || target_os == "linux") {
3058 This value is not used internally by GN for any purpose, so it
3059 may be set to whatever value is needed for the build.
3060 GN defaults this value to the empty string ("") and the
3061 configuration files should set it to an appropriate value
3062 (e.g., setting it to the value of "host_os") if it is not
3063 set via the command line or in the args.gn file.
3065 Where practical, use one of the following list of common values:
3069 ### **Possible values**:
3081 ## **target_out_dir**: [string] Directory for target output files.
3084 Absolute path to the target's generated file directory. If your
3085 current target is in "//tools/doom_melon" then this value might be
3086 "//out/Debug/obj/tools/doom_melon". It will not have a trailing
3089 This is primarily useful for setting up arguments for calling
3090 scripts. If you are passing this to a script, you will want to pass it
3091 through rebase_path() (see "gn help rebase_path") to convert it
3092 to be relative to the build directory.
3094 See also "gn help root_out_dir".
3101 action("myscript") {
3102 # Pass the output dir to the script.
3103 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
3108 ## **all_dependent_configs**: Configs to be forced on dependents.
3111 A list of config labels.
3113 All targets depending on this one, and recursively, all targets
3114 depending on those, will have the configs listed in this variable
3115 added to them. These configs will also apply to the current target.
3117 This addition happens in a second phase once a target and all of its
3118 dependencies have been resolved. Therefore, a target will not see
3119 these force-added configs in their "configs" variable while the
3120 script is running, and then can not be removed. As a result, this
3121 capability should generally only be used to add defines and include
3122 directories necessary to compile a target's headers.
3124 See also "public_configs".
3128 ### **Ordering of flags and values**:
3131 1. Those set on the current target (not in a config).
3132 2. Those set on the "configs" on the target in order that the
3133 configs appear in the list.
3134 3. Those set on the "all_dependent_configs" on the target in order
3135 that the configs appear in the list.
3136 4. Those set on the "public_configs" on the target in order that
3137 those configs appear in the list.
3138 5. all_dependent_configs pulled from dependencies, in the order of
3139 the "deps" list. This is done recursively. If a config appears
3140 more than once, only the first occurance will be used.
3141 6. public_configs pulled from dependencies, in the order of the
3142 "deps" list. If a dependency is public, they will be applied
3147 ## **allow_circular_includes_from**: Permit includes from deps.
3150 A list of target labels. Must be a subset of the target's "deps".
3151 These targets will be permitted to include headers from the current
3152 target despite the dependency going in the opposite direction.
3156 ### **Tedious exposition**
3159 Normally, for a file in target A to include a file from target B,
3160 A must list B as a dependency. This invariant is enforced by the
3161 "gn check" command (and the --check flag to "gn gen").
3163 Sometimes, two targets might be the same unit for linking purposes
3164 (two source sets or static libraries that would always be linked
3165 together in a final executable or shared library). In this case,
3166 you want A to be able to include B's headers, and B to include A's
3169 This list, if specified, lists which of the dependencies of the
3170 current target can include header files from the current target.
3171 That is, if A depends on B, B can only include headers from A if it is
3172 in A's allow_circular_includes_from list.
3180 deps = [ ":b", ":c" ]
3181 allow_circular_includes_from = [ ":b" ]
3187 ## **args**: Arguments passed to an action.
3190 For action and action_foreach targets, args is the list of arguments
3191 to pass to the script. Typically you would use source expansion (see
3192 "gn help source_expansion") to insert the source file names.
3194 See also "gn help action" and "gn help action_foreach".
3198 ## **cflags***: Flags passed to the C compiler.
3203 "cflags" are passed to all invocations of the C, C++, Objective C,
3204 and Objective C++ compilers.
3206 To target one of these variants individually, use "cflags_c",
3207 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3208 These variant-specific versions will be appended to the "cflags".
3212 ### **Ordering of flags and values**:
3215 1. Those set on the current target (not in a config).
3216 2. Those set on the "configs" on the target in order that the
3217 configs appear in the list.
3218 3. Those set on the "all_dependent_configs" on the target in order
3219 that the configs appear in the list.
3220 4. Those set on the "public_configs" on the target in order that
3221 those configs appear in the list.
3222 5. all_dependent_configs pulled from dependencies, in the order of
3223 the "deps" list. This is done recursively. If a config appears
3224 more than once, only the first occurance will be used.
3225 6. public_configs pulled from dependencies, in the order of the
3226 "deps" list. If a dependency is public, they will be applied
3231 ## **cflags***: Flags passed to the C compiler.
3236 "cflags" are passed to all invocations of the C, C++, Objective C,
3237 and Objective C++ compilers.
3239 To target one of these variants individually, use "cflags_c",
3240 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3241 These variant-specific versions will be appended to the "cflags".
3245 ### **Ordering of flags and values**:
3248 1. Those set on the current target (not in a config).
3249 2. Those set on the "configs" on the target in order that the
3250 configs appear in the list.
3251 3. Those set on the "all_dependent_configs" on the target in order
3252 that the configs appear in the list.
3253 4. Those set on the "public_configs" on the target in order that
3254 those configs appear in the list.
3255 5. all_dependent_configs pulled from dependencies, in the order of
3256 the "deps" list. This is done recursively. If a config appears
3257 more than once, only the first occurance will be used.
3258 6. public_configs pulled from dependencies, in the order of the
3259 "deps" list. If a dependency is public, they will be applied
3264 ## **cflags***: Flags passed to the C compiler.
3269 "cflags" are passed to all invocations of the C, C++, Objective C,
3270 and Objective C++ compilers.
3272 To target one of these variants individually, use "cflags_c",
3273 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3274 These variant-specific versions will be appended to the "cflags".
3278 ### **Ordering of flags and values**:
3281 1. Those set on the current target (not in a config).
3282 2. Those set on the "configs" on the target in order that the
3283 configs appear in the list.
3284 3. Those set on the "all_dependent_configs" on the target in order
3285 that the configs appear in the list.
3286 4. Those set on the "public_configs" on the target in order that
3287 those configs appear in the list.
3288 5. all_dependent_configs pulled from dependencies, in the order of
3289 the "deps" list. This is done recursively. If a config appears
3290 more than once, only the first occurance will be used.
3291 6. public_configs pulled from dependencies, in the order of the
3292 "deps" list. If a dependency is public, they will be applied
3297 ## **cflags***: Flags passed to the C compiler.
3302 "cflags" are passed to all invocations of the C, C++, Objective C,
3303 and Objective C++ compilers.
3305 To target one of these variants individually, use "cflags_c",
3306 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3307 These variant-specific versions will be appended to the "cflags".
3311 ### **Ordering of flags and values**:
3314 1. Those set on the current target (not in a config).
3315 2. Those set on the "configs" on the target in order that the
3316 configs appear in the list.
3317 3. Those set on the "all_dependent_configs" on the target in order
3318 that the configs appear in the list.
3319 4. Those set on the "public_configs" on the target in order that
3320 those configs appear in the list.
3321 5. all_dependent_configs pulled from dependencies, in the order of
3322 the "deps" list. This is done recursively. If a config appears
3323 more than once, only the first occurance will be used.
3324 6. public_configs pulled from dependencies, in the order of the
3325 "deps" list. If a dependency is public, they will be applied
3330 ## **cflags***: Flags passed to the C compiler.
3335 "cflags" are passed to all invocations of the C, C++, Objective C,
3336 and Objective C++ compilers.
3338 To target one of these variants individually, use "cflags_c",
3339 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3340 These variant-specific versions will be appended to the "cflags".
3344 ### **Ordering of flags and values**:
3347 1. Those set on the current target (not in a config).
3348 2. Those set on the "configs" on the target in order that the
3349 configs appear in the list.
3350 3. Those set on the "all_dependent_configs" on the target in order
3351 that the configs appear in the list.
3352 4. Those set on the "public_configs" on the target in order that
3353 those configs appear in the list.
3354 5. all_dependent_configs pulled from dependencies, in the order of
3355 the "deps" list. This is done recursively. If a config appears
3356 more than once, only the first occurance will be used.
3357 6. public_configs pulled from dependencies, in the order of the
3358 "deps" list. If a dependency is public, they will be applied
3363 ## **check_includes**: [boolean] Controls whether a target's files are checked.
3366 When true (the default), the "gn check" command (as well as
3367 "gn gen" with the --check flag) will check this target's sources
3368 and headers for proper dependencies.
3370 When false, the files in this target will be skipped by default.
3371 This does not affect other targets that depend on the current target,
3372 it just skips checking the includes of the current target's files.
3376 ### **Controlling includes individually**
3379 If only certain includes are problematic, you can annotate them
3380 individually rather than disabling header checking on an entire
3381 target. Add the string "nogncheck" to the include line:
3383 #include "foo/something_weird.h" // nogncheck (bug 12345)
3385 It is good form to include a reference to a bug (if the include is
3386 improper, or some other comment expressing why the header checker
3387 doesn't work for this particular case.
3389 The most common reason to need "nogncheck" is conditional includes.
3390 The header checker does not understand the preprocessor, so may flag
3391 some includes as improper even if the dependencies and #defines are
3392 always matched correctly:
3394 #if defined(ENABLE_DOOM_MELON)
3395 #include "doom_melon/beam_controller.h" // nogncheck
3403 source_set("busted_includes") {
3404 # This target's includes are messed up, exclude it from checking.
3405 check_includes = false
3411 ## **complete_static_lib**: [boolean] Links all deps into a static library.
3414 A static library normally doesn't include code from dependencies, but
3415 instead forwards the static libraries and source sets in its deps up
3416 the dependency chain until a linkable target (an executable or shared
3417 library) is reached. The final linkable target only links each static
3418 library once, even if it appears more than once in its dependency
3421 In some cases the static library might be the final desired output.
3422 For example, you may be producing a static library for distribution to
3423 third parties. In this case, the static library should include code
3424 for all dependencies in one complete package. Since GN does not unpack
3425 static libraries to forward their contents up the dependency chain,
3426 it is an error for complete static libraries to depend on other static
3434 static_library("foo") {
3435 complete_static_lib = true
3441 ## **configs**: Configs applying to this target.
3444 A list of config labels.
3446 The include_dirs, defines, etc. in each config are appended in the
3447 order they appear to the compile command for each file in the target.
3448 They will appear after the include_dirs, defines, etc. that the target
3451 The build configuration script will generally set up the default
3452 configs applying to a given target type (see "set_defaults").
3453 When a target is being defined, it can add to or remove from this
3458 ### **Ordering of flags and values**:
3461 1. Those set on the current target (not in a config).
3462 2. Those set on the "configs" on the target in order that the
3463 configs appear in the list.
3464 3. Those set on the "all_dependent_configs" on the target in order
3465 that the configs appear in the list.
3466 4. Those set on the "public_configs" on the target in order that
3467 those configs appear in the list.
3468 5. all_dependent_configs pulled from dependencies, in the order of
3469 the "deps" list. This is done recursively. If a config appears
3470 more than once, only the first occurance will be used.
3471 6. public_configs pulled from dependencies, in the order of the
3472 "deps" list. If a dependency is public, they will be applied
3479 static_library("foo") {
3480 configs -= "//build:no_rtti" # Don't use the default RTTI config.
3481 configs += ":mysettings" # Add some of our own settings.
3486 ## **data**: Runtime data file dependencies.
3489 Lists files or directories required to run the given target. These are
3490 typically data files or directories of data files. The paths are
3491 interpreted as being relative to the current build file. Since these
3492 are runtime dependencies, they do not affect which targets are built
3493 or when. To declare input files to a script, use "inputs".
3495 Appearing in the "data" section does not imply any special handling
3496 such as copying them to the output directory. This is just used for
3497 declaring runtime dependencies. Runtime dependencies can be queried
3498 using the "runtime_deps" category of "gn desc" or written during
3499 build generation via "--runtime-deps-list-file".
3501 GN doesn't require data files to exist at build-time. So actions that
3502 produce files that are in turn runtime dependencies can list those
3503 generated files both in the "outputs" list as well as the "data"
3506 By convention, directories are be listed with a trailing slash:
3507 data = [ "test/data/" ]
3508 However, no verification is done on these so GN doesn't enforce this.
3509 The paths are just rebased and passed along when requested.
3511 See "gn help runtime_deps" for how these are used.
3515 ## **data_deps**: Non-linked dependencies.
3518 A list of target labels.
3520 Specifies dependencies of a target that are not actually linked into
3521 the current target. Such dependencies will be built and will be
3522 available at runtime.
3524 This is normally used for things like plugins or helper programs that
3525 a target needs at runtime.
3527 See also "gn help deps" and "gn help data".
3535 data_deps = [ "//plugins:my_runtime_plugin" ]
3540 ## **defines**: C preprocessor defines.
3545 These strings will be passed to the C/C++ compiler as #defines. The
3546 strings may or may not include an "=" to assign a value.
3550 ### **Ordering of flags and values**:
3553 1. Those set on the current target (not in a config).
3554 2. Those set on the "configs" on the target in order that the
3555 configs appear in the list.
3556 3. Those set on the "all_dependent_configs" on the target in order
3557 that the configs appear in the list.
3558 4. Those set on the "public_configs" on the target in order that
3559 those configs appear in the list.
3560 5. all_dependent_configs pulled from dependencies, in the order of
3561 the "deps" list. This is done recursively. If a config appears
3562 more than once, only the first occurance will be used.
3563 6. public_configs pulled from dependencies, in the order of the
3564 "deps" list. If a dependency is public, they will be applied
3571 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
3575 ## **depfile**: [string] File name for input dependencies for actions.
3578 If nonempty, this string specifies that the current action or
3579 action_foreach target will generate the given ".d" file containing
3580 the dependencies of the input. Empty or unset means that the script
3581 doesn't generate the files.
3583 The .d file should go in the target output directory. If you have more
3584 than one source file that the script is being run over, you can use
3585 the output file expansions described in "gn help action_foreach" to
3586 name the .d file according to the input.
3587 The format is that of a Makefile, and all of the paths should be
3588 relative to the root build directory.
3594 action_foreach("myscript_target") {
3595 script = "myscript.py"
3598 # Locate the depfile in the output directory named like the
3599 # inputs but with a ".d" appended.
3600 depfile = "$relative_target_output_dir/{{source_name}}.d"
3602 # Say our script uses "-o <d file>" to indicate the depfile.
3603 args = [ "{{source}}", "-o", depfile ]
3608 ## **deps**: Private linked dependencies.
3611 A list of target labels.
3613 Specifies private dependencies of a target. Shared and dynamic
3614 libraries will be linked into the current target. Other target types
3615 that can't be linked (like actions and groups) listed in "deps" will
3616 be treated as "data_deps". Likewise, if the current target isn't
3617 linkable, then all deps will be treated as "data_deps".
3619 These dependencies are private in that it does not grant dependent
3620 targets the ability to include headers from the dependency, and direct
3621 dependent configs are not forwarded.
3623 See also "public_deps" and "data_deps".
3627 ## **forward_dependent_configs_from**
3630 A list of target labels.
3632 DEPRECATED. Use public_deps instead which will have the same effect.
3634 Exposes the public_configs from a private dependent target as
3635 public_configs of the current one. Each label in this list
3636 must also be in the deps.
3638 Generally you should use public_deps instead of this variable to
3639 express the concept of exposing a dependency as part of a target's
3640 public API. We're considering removing this variable.
3647 Sometimes you depend on a child library that exports some necessary
3648 configuration via public_configs. If your target in turn exposes the
3649 child library's headers in its public headers, it might mean that
3650 targets that depend on you won't work: they'll be seeing the child
3651 library's code but not the necessary configuration. This list
3652 specifies which of your deps' direct dependent configs to expose as
3660 If we use a given library "a" from our public headers:
3662 deps = [ ":a", ":b", ... ]
3663 forward_dependent_configs_from = [ ":a" ]
3665 This example makes a "transparent" target that forwards a dependency
3669 if (use_system_frob) {
3670 deps = ":system_frob"
3672 deps = "//third_party/fallback_frob"
3674 forward_dependent_configs_from = deps
3679 ## **include_dirs**: Additional include directories.
3682 A list of source directories.
3684 The directories in this list will be added to the include path for
3685 the files in the affected target.
3689 ### **Ordering of flags and values**:
3692 1. Those set on the current target (not in a config).
3693 2. Those set on the "configs" on the target in order that the
3694 configs appear in the list.
3695 3. Those set on the "all_dependent_configs" on the target in order
3696 that the configs appear in the list.
3697 4. Those set on the "public_configs" on the target in order that
3698 those configs appear in the list.
3699 5. all_dependent_configs pulled from dependencies, in the order of
3700 the "deps" list. This is done recursively. If a config appears
3701 more than once, only the first occurance will be used.
3702 6. public_configs pulled from dependencies, in the order of the
3703 "deps" list. If a dependency is public, they will be applied
3710 include_dirs = [ "src/include", "//third_party/foo" ]
3714 ## **inputs**: Additional compile-time dependencies.
3717 Inputs are compile-time dependencies of the current target. This means
3718 that all inputs must be available before compiling any of the sources
3719 or executing any actions.
3721 Inputs are typically only used for action and action_foreach targets.
3725 ### **Inputs for actions**
3728 For action and action_foreach targets, inputs should be the inputs to
3729 script that don't vary. These should be all .py files that the script
3730 uses via imports (the main script itself will be an implcit dependency
3731 of the action so need not be listed).
3733 For action targets, inputs should be the entire set of inputs the
3734 script needs. For action_foreach targets, inputs should be the set of
3735 dependencies that don't change. These will be applied to each script
3736 invocation over the sources.
3738 Note that another way to declare input dependencies from an action
3739 is to have the action write a depfile (see "gn help depfile"). This
3740 allows the script to dynamically write input dependencies, that might
3741 not be known until actually executing the script. This is more
3742 efficient than doing processing while running GN to determine the
3743 inputs, and is easier to keep in-sync than hardcoding the list.
3747 ### **Inputs for binary targets**
3750 Any input dependencies will be resolved before compiling any sources.
3751 Normally, all actions that a target depends on will be run before any
3752 files in a target are compiled. So if you depend on generated headers,
3753 you do not typically need to list them in the inputs section.
3760 action("myscript") {
3761 script = "domything.py"
3762 inputs = [ "input.data" ]
3767 ## **ldflags**: Flags passed to the linker.
3772 These flags are passed on the command-line to the linker and generally
3773 specify various linking options. Most targets will not need these and
3774 will use "libs" and "lib_dirs" instead.
3776 ldflags are NOT pushed to dependents, so applying ldflags to source
3777 sets or static libraries will be a no-op. If you want to apply ldflags
3778 to dependent targets, put them in a config and set it in the
3779 all_dependent_configs or public_configs.
3783 ### **Ordering of flags and values**:
3786 1. Those set on the current target (not in a config).
3787 2. Those set on the "configs" on the target in order that the
3788 configs appear in the list.
3789 3. Those set on the "all_dependent_configs" on the target in order
3790 that the configs appear in the list.
3791 4. Those set on the "public_configs" on the target in order that
3792 those configs appear in the list.
3793 5. all_dependent_configs pulled from dependencies, in the order of
3794 the "deps" list. This is done recursively. If a config appears
3795 more than once, only the first occurance will be used.
3796 6. public_configs pulled from dependencies, in the order of the
3797 "deps" list. If a dependency is public, they will be applied
3802 ## **lib_dirs**: Additional library directories.
3805 A list of directories.
3807 Specifies additional directories passed to the linker for searching
3808 for the required libraries. If an item is not an absolute path, it
3809 will be treated as being relative to the current build file.
3811 libs and lib_dirs work differently than other flags in two respects.
3812 First, then are inherited across static library boundaries until a
3813 shared library or executable target is reached. Second, they are
3814 uniquified so each one is only passed once (the first instance of it
3815 will be the one used).
3819 ### **Ordering of flags and values**:
3822 1. Those set on the current target (not in a config).
3823 2. Those set on the "configs" on the target in order that the
3824 configs appear in the list.
3825 3. Those set on the "all_dependent_configs" on the target in order
3826 that the configs appear in the list.
3827 4. Those set on the "public_configs" on the target in order that
3828 those configs appear in the list.
3829 5. all_dependent_configs pulled from dependencies, in the order of
3830 the "deps" list. This is done recursively. If a config appears
3831 more than once, only the first occurance will be used.
3832 6. public_configs pulled from dependencies, in the order of the
3833 "deps" list. If a dependency is public, they will be applied
3840 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
3844 ## **libs**: Additional libraries to link.
3849 These files will be passed to the linker, which will generally search
3850 the library include path. Unlike a normal list of files, they will be
3851 passed to the linker unmodified rather than being treated as file
3852 names relative to the current build file. Generally you would set
3853 the "lib_dirs" so your library is found. If you need to specify
3854 a path, you can use "rebase_path" to convert a path to be relative
3855 to the build directory.
3857 When constructing the linker command, the "lib_prefix" attribute of
3858 the linker tool in the current toolchain will be prepended to each
3859 library. So your BUILD file should not specify the switch prefix
3860 (like "-l"). On Mac, libraries ending in ".framework" will be
3861 special-cased: the switch "-framework" will be prepended instead of
3862 the lib_prefix, and the ".framework" suffix will be trimmed.
3864 libs and lib_dirs work differently than other flags in two respects.
3865 First, then are inherited across static library boundaries until a
3866 shared library or executable target is reached. Second, they are
3867 uniquified so each one is only passed once (the first instance of it
3868 will be the one used).
3872 ### **Ordering of flags and values**:
3875 1. Those set on the current target (not in a config).
3876 2. Those set on the "configs" on the target in order that the
3877 configs appear in the list.
3878 3. Those set on the "all_dependent_configs" on the target in order
3879 that the configs appear in the list.
3880 4. Those set on the "public_configs" on the target in order that
3881 those configs appear in the list.
3882 5. all_dependent_configs pulled from dependencies, in the order of
3883 the "deps" list. This is done recursively. If a config appears
3884 more than once, only the first occurance will be used.
3885 6. public_configs pulled from dependencies, in the order of the
3886 "deps" list. If a dependency is public, they will be applied
3894 libs = [ "ctl3d.lib" ]
3900 ## **output_extension**: Value to use for the output's file extension.
3903 Normally the file extension for a target is based on the target
3904 type and the operating system, but in rare cases you will need to
3905 override the name (for example to use "libfreetype.so.6" instead
3906 of libfreetype.so on Linux).
3909 ## **output_name**: Define a name for the output file other than the default.
3912 Normally the output name of a target will be based on the target name,
3913 so the target "//foo/bar:bar_unittests" will generate an output
3914 file such as "bar_unittests.exe" (using Windows as an example).
3916 Sometimes you will want an alternate name to avoid collisions or
3917 if the internal name isn't appropriate for public distribution.
3919 The output name should have no extension or prefixes, these will be
3920 added using the default system rules. For example, on Linux an output
3921 name of "foo" will produce a shared library "libfoo.so".
3923 This variable is valid for all binary output target types.
3929 static_library("doom_melon") {
3930 output_name = "fluffy_bunny"
3935 ## **outputs**: Output files for actions and copy targets.
3938 Outputs is valid for "copy", "action", and "action_foreach"
3939 target types and indicates the resulting files. The values may contain
3940 source expansions to generate the output names from the sources (see
3941 "gn help source_expansion").
3943 For copy targets, the outputs is the destination for the copied
3944 file(s). For actions, the outputs should be the list of files
3945 generated by the script.
3949 ## **precompiled_header**: [string] Header file to precompile.
3952 Precompiled headers will be used when a target specifies this
3953 value, or a config applying to this target specifies this value.
3954 In addition, the tool corresponding to the source files must also
3955 specify precompiled headers (see "gn help tool"). The tool
3956 will also specify what type of precompiled headers to use.
3958 The precompiled header/source variables can be specified on a target
3959 or a config, but must be the same for all configs applying to a given
3960 target since a target can only have one precompiled header.
3964 ### **MSVC precompiled headers**
3967 When using MSVC-style precompiled headers, the "precompiled_header"
3968 value is a string corresponding to the header. This is NOT a path
3969 to a file that GN recognises, but rather the exact string that appears
3970 in quotes after an #include line in source code. The compiler will
3971 match this string against includes or forced includes (/FI).
3973 MSVC also requires a source file to compile the header with. This must
3974 be specified by the "precompiled_source" value. In contrast to the
3975 header value, this IS a GN-style file name, and tells GN which source
3976 file to compile to make the .pch file used for subsequent compiles.
3978 If you use both C and C++ sources, the precompiled header and source
3979 file will be compiled using both tools. You will want to make sure
3980 to wrap C++ includes in __cplusplus #ifdefs so the file will compile
3983 For example, if the toolchain specifies MSVC headers:
3985 toolchain("vc_x64") {
3988 precompiled_header_type = "msvc"
3991 You might make a config like this:
3993 config("use_precompiled_headers") {
3994 precompiled_header = "build/precompile.h"
3995 precompiled_source = "//build/precompile.cc"
3997 # Either your source files should #include "build/precompile.h"
3998 # first, or you can do this to force-include the header.
3999 cflags = [ "/FI$precompiled_header" ]
4002 And then define a target that uses the config:
4004 executable("doom_melon") {
4005 configs += [ ":use_precompiled_headers" ]
4011 ## **precompiled_source**: [file name] Source file to precompile.
4014 The source file that goes along with the precompiled_header when
4015 using "msvc"-style precompiled headers. It will be implicitly added
4016 to the sources of the target. See "gn help precompiled_header".
4020 ## **public**: Declare public header files for a target.
4023 A list of files that other targets can include. These permissions are
4024 checked via the "check" command (see "gn help check").
4026 If no public files are declared, other targets (assuming they have
4027 visibility to depend on this target can include any file in the
4028 sources list. If this variable is defined on a target, dependent
4029 targets may only include files on this whitelist.
4031 Header file permissions are also subject to visibility. A target
4032 must be visible to another target to include any files from it at all
4033 and the public headers indicate which subset of those files are
4034 permitted. See "gn help visibility" for more.
4036 Public files are inherited through the dependency tree. So if there is
4037 a dependency A -> B -> C, then A can include C's public headers.
4038 However, the same is NOT true of visibility, so unless A is in C's
4039 visibility list, the include will be rejected.
4041 GN only knows about files declared in the "sources" and "public"
4042 sections of targets. If a file is included that is not known to the
4043 build, it will be allowed.
4049 These exact files are public:
4050 public = [ "foo.h", "bar.h" ]
4052 No files are public (no targets may include headers from this one):
4057 ## **public_configs**: Configs to be applied on dependents.
4060 A list of config labels.
4062 Targets directly depending on this one will have the configs listed in
4063 this variable added to them. These configs will also apply to the
4066 This addition happens in a second phase once a target and all of its
4067 dependencies have been resolved. Therefore, a target will not see
4068 these force-added configs in their "configs" variable while the
4069 script is running, and then can not be removed. As a result, this
4070 capability should generally only be used to add defines and include
4071 directories necessary to compile a target's headers.
4073 See also "all_dependent_configs".
4077 ### **Ordering of flags and values**:
4080 1. Those set on the current target (not in a config).
4081 2. Those set on the "configs" on the target in order that the
4082 configs appear in the list.
4083 3. Those set on the "all_dependent_configs" on the target in order
4084 that the configs appear in the list.
4085 4. Those set on the "public_configs" on the target in order that
4086 those configs appear in the list.
4087 5. all_dependent_configs pulled from dependencies, in the order of
4088 the "deps" list. This is done recursively. If a config appears
4089 more than once, only the first occurance will be used.
4090 6. public_configs pulled from dependencies, in the order of the
4091 "deps" list. If a dependency is public, they will be applied
4096 ## **public_deps**: Declare public dependencies.
4099 Public dependencies are like private dependencies ("deps") but
4100 additionally express that the current target exposes the listed deps
4101 as part of its public API.
4103 This has several ramifications:
4105 - public_configs that are part of the dependency are forwarded
4106 to direct dependents.
4108 - Public headers in the dependency are usable by dependents
4109 (includes do not require a direct dependency or visibility).
4111 - If the current target is a shared library, other shared libraries
4112 that it publicly depends on (directly or indirectly) are
4113 propagated up the dependency tree to dependents for linking.
4120 Say you have three targets: A -> B -> C. C's visibility may allow
4121 B to depend on it but not A. Normally, this would prevent A from
4122 including any headers from C, and C's public_configs would apply
4125 If B lists C in its public_deps instead of regular deps, A will now
4126 inherit C's public_configs and the ability to include C's public
4129 Generally if you are writing a target B and you include C's headers
4130 as part of B's public headers, or targets depending on B should
4131 consider B and C to be part of a unit, you should use public_deps
4139 # This target can include files from "c" but not from
4140 # "super_secret_implementation_details".
4145 shared_library("b") {
4146 deps = [ ":super_secret_implementation_details" ]
4147 public_deps = [ ":c" ]
4152 ## **script**: Script file for actions.
4155 An absolute or buildfile-relative file name of a Python script to run
4156 for a action and action_foreach targets (see "gn help action" and
4157 "gn help action_foreach").
4161 ## **sources**: Source files for a target
4164 A list of files relative to the current buildfile.
4168 ## **testonly**: Declares a target must only be used for testing.
4171 Boolean. Defaults to false.
4173 When a target is marked "testonly = true", it must only be depended
4174 on by other test-only targets. Otherwise, GN will issue an error
4175 that the depenedency is not allowed.
4177 This feature is intended to prevent accidentally shipping test code
4185 source_set("test_support") {
4192 ## **visibility**: A list of labels that can depend on a target.
4195 A list of labels and label patterns that define which targets can
4196 depend on the current one. These permissions are checked via the
4197 "check" command (see "gn help check").
4199 If visibility is not defined, it defaults to public ("*").
4201 If visibility is defined, only the targets with labels that match it
4202 can depend on the current target. The empty list means no targets
4203 can depend on the current target.
4205 Tip: Often you will want the same visibility for all targets in a
4206 BUILD file. In this case you can just put the definition at the top,
4207 outside of any target, and the targets will inherit that scope and see
4215 See "gn help label_pattern" for more details on what types of
4216 patterns are supported. If a toolchain is specified, only targets
4217 in that toolchain will be matched. If a toolchain is not specified on
4218 a pattern, targets in all toolchains will be matched.
4225 Only targets in the current buildfile ("private"):
4226 visibility = [ ":*" ]
4228 No targets (used for targets that should be leaf nodes):
4231 Any target ("public", the default):
4232 visibility = [ "*" ]
4234 All targets in the current directory and any subdirectory:
4235 visibility = [ "./*" ]
4237 Any target in "//bar/BUILD.gn":
4238 visibility = [ "//bar:*" ]
4240 Any target in "//bar/" or any subdirectory thereof:
4241 visibility = [ "//bar/*" ]
4243 Just these specific targets:
4244 visibility = [ ":mything", "//foo:something_else" ]
4246 Any target in the current directory and any subdirectory thereof, plus
4247 any targets in "//bar/" and any subdirectory thereof.
4248 visibility = [ "./*", "//bar/*" ]
4252 ## **Build Arguments Overview**
4255 Build arguments are variables passed in from outside of the build
4256 that build files can query to determine how the build works.
4260 ### **How build arguments are set**
4263 First, system default arguments are set based on the current system.
4264 The built-in arguments are:
4272 If specified, arguments from the --args command line flag are used. If
4273 that flag is not specified, args from previous builds in the build
4274 directory will be used (this is in the file args.gn in the build
4277 Last, for targets being compiled with a non-default toolchain, the
4278 toolchain overrides are applied. These are specified in the
4279 toolchain_args section of a toolchain definition. The use-case for
4280 this is that a toolchain may be building code for a different
4281 platform, and that it may want to always specify Posix, for example.
4282 See "gn help toolchain_args" for more.
4284 If you specify an override for a build argument that never appears in
4285 a "declare_args" call, a nonfatal error will be displayed.
4293 Create the directory out/FooBar and open an editor. You would type
4294 something like this into that file:
4295 enable_doom_melon=false
4298 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
4299 This will overwrite the build directory with the given arguments.
4300 (Note that the quotes inside the args command will usually need to
4301 be escaped for your shell to pass through strings values.)
4305 ### **How build arguments are used**
4308 If you want to use an argument, you use declare_args() and specify
4309 default values. These default values will apply if none of the steps
4310 listed in the "How build arguments are set" section above apply to
4311 the given argument, but the defaults will not override any of these.
4313 Often, the root build config file will declare global arguments that
4314 will be passed to all buildfiles. Individual build files can also
4315 specify arguments that apply only to those files. It is also useful
4316 to specify build args in an "import"-ed file if you want such
4317 arguments to apply to multiple buildfiles.
4324 When gn starts, it will search the current directory and parent
4325 directories for a file called ".gn". This indicates the source root.
4326 You can override this detection by using the --root command-line
4329 The .gn file in the source root will be executed. The syntax is the
4330 same as a buildfile, but with very limited build setup-specific
4333 If you specify --root, by default GN will look for the file .gn in
4334 that directory. If you want to specify a different file, you can
4335 additionally pass --dotfile:
4337 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
4344 buildconfig [required]
4345 Label of the build config file. This file will be used to set up
4346 the build file execution environment for each toolchain.
4348 check_targets [optional]
4349 A list of labels and label patterns that should be checked when
4350 running "gn check" or "gn gen --check". If unspecified, all
4351 targets will be checked. If it is the empty list, no targets will
4354 The format of this list is identical to that of "visibility"
4355 so see "gn help visibility" for examples.
4357 exec_script_whitelist [optional]
4358 A list of .gn/.gni files (not labels) that have permission to call
4359 the exec_script function. If this list is defined, calls to
4360 exec_script will be checked against this list and GN will fail if
4361 the current file isn't in the list.
4363 This is to allow the use of exec_script to be restricted since
4364 is easy to use inappropriately. Wildcards are not supported.
4365 Files in the secondary_source tree (if defined) should be
4366 referenced by ignoring the secondary tree and naming them as if
4367 they are in the main tree.
4369 If unspecified, the ability to call exec_script is unrestricted.
4372 exec_script_whitelist = [
4374 "//build/my_config.gni",
4378 Label of the root build target. The GN build will start by loading
4379 the build file containing this target name. This defaults to
4380 "//:" which will cause the file //BUILD.gn to be loaded.
4382 secondary_source [optional]
4383 Label of an alternate directory tree to find input files. When
4384 searching for a BUILD.gn file (or the build config file discussed
4385 above), the file will first be looked for in the source root.
4386 If it's not found, the secondary source root will be checked
4387 (which would contain a parallel directory hierarchy).
4389 This behavior is intended to be used when BUILD.gn files can't be
4390 checked in to certain source directories for whatever reason.
4392 The secondary source root must be inside the main source tree.
4396 ### **Example .gn file contents**
4399 buildconfig = "//build/config/BUILDCONFIG.gn"
4402 "//doom_melon/*", # Check everything in this subtree.
4403 "//tools:mind_controlling_ant", # Check this specific target.
4408 secondary_source = "//build/config/temporary_buildfiles/"
4412 ## **GN build language grammar**
4417 GN build files are read as sequences of tokens. While splitting the
4418 file into tokens, the next token is the longest sequence of characters
4419 that form a valid token.
4423 ### **White space and comments**
4426 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
4427 carriage returns (U+000D), and newlines (U+000A).
4429 Comments start at the character "#" and stop at the next newline.
4431 White space and comments are ignored except that they may separate
4432 tokens that would otherwise combine into a single token.
4439 Identifiers name variables and functions.
4441 identifier = letter { letter | digit } .
4442 letter = "A" ... "Z" | "a" ... "z" | "_" .
4443 digit = "0" ... "9" .
4450 The following keywords are reserved and may not be used as
4457 ### **Integer literals**
4460 An integer literal represents a decimal integer value.
4462 integer = [ "-" ] digit { digit } .
4464 Leading zeros and negative zero are disallowed.
4468 ### **String literals**
4471 A string literal represents a string value consisting of the quoted
4472 characters with possible escape sequences and variable expansions.
4474 string = `"` { char | escape | expansion } `"` .
4475 escape = `\` ( "$" | `"` | char ) .
4476 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" .
4477 expansion = "$" ( identifier | BracketExpansion ) .
4478 char = /* any character except "$", `"`, or newline */ .
4480 After a backslash, certain sequences represent special characters:
4482 \" U+0022 quotation mark
4483 \$ U+0024 dollar sign
4486 All other backslashes represent themselves.
4493 The following character sequences represent punctuation:
4505 The input tokens form a syntax tree following a context-free grammar:
4507 File = StatementList .
4509 Statement = Assignment | Call | Condition .
4510 Assignment = identifier AssignOp Expr .
4511 Call = identifier "(" [ ExprList ] ")" [ Block ] .
4512 Condition = "if" "(" Expr ")" Block
4513 [ "else" ( Condition | Block ) ] .
4514 Block = "{" StatementList "}" .
4515 StatementList = { Statement } .
4517 ArrayAccess = identifier "[" { identifier | integer } "]" .
4518 ScopeAccess = identifier "." identifier .
4519 Expr = UnaryExpr | Expr BinaryOp Expr .
4520 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
4521 PrimaryExpr = identifier | integer | string | Call
4522 | ArrayAccess | ScopeAccess
4524 | "[" [ ExprList [ "," ] ] "]" .
4525 ExprList = Expr { "," Expr } .
4527 AssignOp = "=" | "+=" | "-=" .
4529 BinaryOp = "+" | "-" // highest priority
4530 | "<" | "<=" | ">" | ">="
4533 | "||" . // lowest priority
4535 All binary operators are left-associative.
4539 ## **input_conversion**: Specifies how to transform input to a variable.
4542 input_conversion is an argument to read_file and exec_script that
4543 specifies how the result of the read operation should be converted
4547 Discard the result and return None.
4550 Return the file contents as a list, with a string for each line.
4551 The newlines will not be present in the result. The last line may
4552 or may not end in a newline.
4554 After splitting, each individual line will be trimmed of
4555 whitespace on both ends.
4558 Execute the block as GN code and return a scope with the
4559 resulting values in it. If the input was:
4560 a = [ "hello.cc", "world.cc" ]
4562 and you read the result into a variable named "val", then you
4563 could access contents the "." operator on "val":
4568 Return the file contents into a single string.
4571 Parse the input as if it was a literal rvalue in a buildfile.
4572 Examples of typical program output using this mode:
4573 [ "foo", "bar" ] (result will be a list)
4575 "foo bar" (result will be a string)
4577 5 (result will be an integer)
4579 Note that if the input is empty, the result will be a null value
4580 which will produce an error if assigned to a variable.
4583 Prefixing any of the other transformations with the word "trim"
4584 will result in whitespace being trimmed from the beginning and end
4585 of the result before processing.
4587 Examples: "trim string" or "trim list lines"
4589 Note that "trim value" is useless because the value parser skips
4594 ## **Label patterns**
4597 A label pattern is a way of expressing one or more labels in a portion
4598 of the source tree. They are not general regular expressions.
4600 They can take the following forms only:
4602 - Explicit (no wildcard):
4606 - Wildcard target names:
4607 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
4608 ":*" (all targets in the current build file)
4610 - Wildcard directory names ("*" is only supported at the end)
4612 "//foo/bar/*" (all targets in any subdir of //foo/bar)
4613 "./*" (all targets in the current build file or sub dirs)
4615 Any of the above forms can additionally take an explicit toolchain.
4616 In this case, the toolchain must be fully qualified (no wildcards
4617 are supported in the toolchain name).
4619 "//foo:bar(//build/toochain:mac)"
4620 An explicit target in an explicit toolchain.
4622 ":*(//build/toolchain/linux:32bit)"
4623 All targets in the current build file using the 32-bit Linux
4626 "//foo/*(//build/toolchain:win)"
4627 All targets in //foo and any subdirectory using the Windows
4632 ## **Runtime dependencies**
4635 Runtime dependencies of a target are exposed via the "runtime_deps"
4636 category of "gn desc" (see "gn help desc") or they can be written
4637 at build generation time via "--runtime-deps-list-file"
4638 (see "gn help --runtime-deps-list-file").
4640 To a first approximation, the runtime dependencies of a target are
4641 the set of "data" files, data directories, and the shared libraries
4642 from all transitive dependencies. Executables and shared libraries are
4643 considered runtime dependencies of themselves.
4650 Executable targets and those executable targets' transitive
4651 dependencies are not considered unless that executable is listed in
4652 "data_deps". Otherwise, GN assumes that the executable (and
4653 everything it requires) is a build-time dependency only.
4657 ### **Actions and copies**
4660 Action and copy targets that are listed as "data_deps" will have all
4661 of their outputs and data files considered as runtime dependencies.
4662 Action and copy targets that are "deps" or "public_deps" will have
4663 only their data files considered as runtime dependencies. These
4664 targets can list an output file in both the "outputs" and "data"
4665 lists to force an output file as a runtime dependency in all cases.
4667 The different rules for deps and data_deps are to express build-time
4668 (deps) vs. run-time (data_deps) outputs. If GN counted all build-time
4669 copy steps as data dependencies, there would be a lot of extra stuff,
4670 and if GN counted all run-time dependencies as regular deps, the
4671 build's parallelism would be unnecessarily constrained.
4673 This rule can sometimes lead to unintuitive results. For example,
4674 given the three targets:
4675 A --[data_deps]--> B --[deps]--> ACTION
4676 GN would say that A does not have runtime deps on the result of the
4677 ACTION, which is often correct. But the purpose of the B target might
4678 be to collect many actions into one logic unit, and the "data"-ness
4679 of A's dependency is lost. Solutions:
4681 - List the outputs of the action in it's data section (if the
4682 results of that action are always runtime files).
4683 - Have B list the action in data_deps (if the outputs of the actions
4684 are always runtime files).
4685 - Have B list the action in both deps and data deps (if the outputs
4686 might be used in both contexts and you don't care about unnecessary
4687 entries in the list of files required at runtime).
4688 - Split B into run-time and build-time versions with the appropriate
4693 ### **Static libraries and source sets**
4696 The results of static_library or source_set targets are not considered
4697 runtime dependencies since these are assumed to be intermediate
4698 targets only. If you need to list a static library as a runtime
4699 dependency, you can manually compute the .a/.lib file name for the
4700 current platform and list it in the "data" list of a target
4701 (possibly on the static library target itself).
4705 ### **Multiple outputs**
4708 When a tool produces more than one output, only the first output
4709 is considered. For example, a shared library target may produce a
4710 .dll and a .lib file on Windows. Only the .dll file will be considered
4711 a runtime dependency. This applies only to linker tools, scripts and
4712 copy steps with multiple outputs will also get all outputs listed.
4716 ## **How Source Expansion Works**
4719 Source expansion is used for the action_foreach and copy target types
4720 to map source file names to output file names or arguments.
4722 To perform source expansion in the outputs, GN maps every entry in the
4723 sources to every entry in the outputs list, producing the cross
4724 product of all combinations, expanding placeholders (see below).
4726 Source expansion in the args works similarly, but performing the
4727 placeholder substitution produces a different set of arguments for
4728 each invocation of the script.
4730 If no placeholders are found, the outputs or args list will be treated
4731 as a static list of literal file names that do not depend on the
4734 See "gn help copy" and "gn help action_foreach" for more on how
4739 ### **Placeholders**
4743 The name of the source file including directory (*). This will
4744 generally be used for specifying inputs to a script in the
4746 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
4748 {{source_file_part}}
4749 The file part of the source including the extension.
4750 "//foo/bar/baz.txt" => "baz.txt"
4752 {{source_name_part}}
4753 The filename part of the source file with no directory or
4754 extension. This will generally be used for specifying a
4755 transformation from a soruce file to a destination file with the
4756 same name but different extension.
4757 "//foo/bar/baz.txt" => "baz"
4760 The directory (*) containing the source file with no
4762 "//foo/bar/baz.txt" => "../../foo/bar"
4764 {{source_root_relative_dir}}
4765 The path to the source file's directory relative to the source
4766 root, with no leading "//" or trailing slashes. If the path is
4767 system-absolute, (beginning in a single slash) this will just
4768 return the path with no trailing slash. This value will always
4769 be the same, regardless of whether it appears in the "outputs"
4771 "//foo/bar/baz.txt" => "foo/bar"
4774 The generated file directory (*) corresponding to the source
4775 file's path. This will be different than the target's generated
4776 file directory if the source file is in a different directory
4777 than the BUILD.gn file.
4778 "//foo/bar/baz.txt" => "gen/foo/bar"
4781 The object file directory (*) corresponding to the source file's
4782 path, relative to the build directory. this us be different than
4783 the target's out directory if the source file is in a different
4784 directory than the build.gn file.
4785 "//foo/bar/baz.txt" => "obj/foo/bar"
4789 ### **(*) Note on directories**
4792 Paths containing directories (except the source_root_relative_dir)
4793 will be different depending on what context the expansion is evaluated
4794 in. Generally it should "just work" but it means you can't
4795 concatenate strings containing these values with reasonable results.
4797 Details: source expansions can be used in the "outputs" variable,
4798 the "args" variable, and in calls to "process_file_template". The
4799 "args" are passed to a script which is run from the build directory,
4800 so these directories will relative to the build directory for the
4801 script to find. In the other cases, the directories will be source-
4802 absolute (begin with a "//") because the results of those expansions
4803 will be handled by GN internally.
4810 Non-varying outputs:
4811 action("hardcoded_outputs") {
4812 sources = [ "input1.idl", "input2.idl" ]
4813 outputs = [ "$target_out_dir/output1.dat",
4814 "$target_out_dir/output2.dat" ]
4816 The outputs in this case will be the two literal files given.
4819 action_foreach("varying_outputs") {
4820 sources = [ "input1.idl", "input2.idl" ]
4821 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
4822 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
4824 Performing source expansion will result in the following output names:
4825 //out/Debug/obj/mydirectory/input1.h
4826 //out/Debug/obj/mydirectory/input1.cc
4827 //out/Debug/obj/mydirectory/input2.h
4828 //out/Debug/obj/mydirectory/input2.cc
4832 **Available global switches
4833 ** Do "gn help --the_switch_you_want_help_on" for more. Individual
4834 commands may take command-specific switches not listed here. See the
4835 help on your specific command for more.
4839 ** \--args**: Specifies build arguments overrides.
4840 ** \--color**: Force colored output.
4841 ** \--dotfile**: Override the name of the ".gn" file.
4842 ** \--markdown**: write the output in the Markdown format.
4843 ** \--nocolor**: Force non-colored output.
4844 ** -q**: Quiet mode. Don't print output on success.
4845 ** \--root**: Explicitly specify source root.
4846 ** \--runtime-deps-list-file**: Save runtime dependencies for targets in file.
4847 ** \--time**: Outputs a summary of how long everything took.
4848 ** \--tracelog**: Writes a Chrome-compatible trace log to the given file.
4849 ** -v**: Verbose logging.
4850 ** \--version**: Prints the GN version number and exits.