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" 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
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 files to output file names using source
1055 expansion (see "gn help source_expansion"). The placeholders will
1056 will 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 ## **get_label_info**: Get an attribute from a target's label.
1259 get_label_info(target_label, what)
1261 Given the label of a target, returns some attribute of that target.
1262 The target need not have been previously defined in the same file,
1263 since none of the attributes depend on the actual target definition,
1264 only the label itself.
1266 See also "gn help get_target_outputs".
1270 ### **Possible values for the "what" parameter**
1274 The short name of the target. This will match the value of the
1275 "target_name" variable inside that target's declaration. For the
1276 label "//foo/bar:baz" this will return "baz".
1279 The directory containing the target's definition, with no slash at
1280 the end. For the label "//foo/bar:baz" this will return
1284 The generated file directory for the target. This will match the
1285 value of the "target_gen_dir" variable when inside that target's
1289 The root of the generated file tree for the target. This will
1290 match the value of the "root_gen_dir" variable when inside that
1291 target's declaration.
1294 The output directory for the target. This will match the
1295 value of the "target_out_dir" variable when inside that target's
1299 The root of the output file tree for the target. This will
1300 match the value of the "root_gen_dir" variable when inside that
1301 target's declaration.
1303 "label_no_toolchain"
1304 The fully qualified version of this label, not including the
1305 toolchain. For the input ":bar" it might return
1308 "label_with_toolchain"
1309 The fully qualified version of this label, including the
1310 toolchain. For the input ":bar" it might return
1311 "//foo:bar(//toolchain:x64)".
1314 The label of the toolchain. This will match the value of the
1315 "current_toolchain" variable when inside that target's
1323 get_label_info(":foo", "name")
1324 # Returns string "foo".
1326 get_label_info("//foo/bar:baz", "gen_dir")
1327 # Returns string "//out/Debug/gen/foo/bar".
1331 ## **get_path_info**: Extract parts of a file or directory name.
1334 get_path_info(input, what)
1336 The first argument is either a string representing a file or
1337 directory name, or a list of such strings. If the input is a list
1338 the return value will be a list containing the result of applying the
1339 rule to each item in the input.
1343 ### **Possible values for the "what" parameter**
1347 The substring after the last slash in the path, including the name
1348 and extension. If the input ends in a slash, the empty string will
1350 "foo/bar.txt" => "bar.txt"
1351 "bar.txt" => "bar.txt"
1356 The substring of the file name not including the extension.
1357 "foo/bar.txt" => "bar"
1362 The substring following the last period following the last slash,
1363 or the empty string if not found. The period is not included.
1364 "foo/bar.txt" => "txt"
1368 The directory portion of the name, not including the slash.
1369 "foo/bar.txt" => "foo"
1370 "//foo/bar" => "//foo"
1373 The result will never end in a slash, so if the resulting
1374 is empty, the system ("/") or source ("//") roots, a "."
1375 will be appended such that it is always legal to append a slash
1376 and a filename and get a valid path.
1379 The output file directory corresponding to the path of the
1380 given file, not including a trailing slash.
1381 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
1383 The generated file directory corresponding to the path of the
1384 given file, not including a trailing slash.
1385 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
1388 The full absolute path name to the file or directory. It will be
1389 resolved relative to the currebt directory, and then the source-
1390 absolute version will be returned. If the input is system-
1391 absolute, the same input will be returned.
1392 "foo/bar.txt" => "//mydir/foo/bar.txt"
1393 "foo/" => "//mydir/foo/"
1394 "//foo/bar" => "//foo/bar" (already absolute)
1395 "/usr/include" => "/usr/include" (already absolute)
1397 If you want to make the path relative to another directory, or to
1398 be system-absolute, see rebase_path().
1404 sources = [ "foo.cc", "foo.h" ]
1405 result = get_path_info(source, "abspath")
1406 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
1408 result = get_path_info("//foo/bar/baz.cc", "dir")
1409 # result will be "//foo/bar"
1411 # Extract the source-absolute directory name,
1412 result = get_path_info(get_path_info(path, "dir"), "abspath")
1416 ## **get_target_outputs**: [file list] Get the list of outputs from a target.
1419 get_target_outputs(target_label)
1421 Returns a list of output files for the named target. The named target
1422 must have been previously defined in the current file before this
1423 function is called (it can't reference targets in other files because
1424 there isn't a defined execution order, and it obviously can't
1425 reference targets that are defined after the function call).
1427 Only copy and action targets are supported. The outputs from binary
1428 targets will depend on the toolchain definition which won't
1429 necessarily have been loaded by the time a given line of code has run,
1430 and source sets and groups have no useful output file.
1434 ### **Return value**
1437 The names in the resulting list will be absolute file paths (normally
1438 like "//out/Debug/bar.exe", depending on the build directory).
1440 action targets: this will just return the files specified in the
1441 "outputs" variable of the target.
1443 action_foreach targets: this will return the result of applying
1444 the output template to the sources (see "gn help source_expansion").
1445 This will be the same result (though with guaranteed absolute file
1446 paths), as process_file_template will return for those inputs
1447 (see "gn help process_file_template").
1449 binary targets (executables, libraries): this will return a list
1450 of the resulting binary file(s). The "main output" (the actual
1451 binary or library) will always be the 0th element in the result.
1452 Depending on the platform and output type, there may be other output
1453 files as well (like import libraries) which will follow.
1455 source sets and groups: this will return a list containing the path of
1456 the "stamp" file that Ninja will produce once all outputs are
1457 generated. This probably isn't very useful.
1464 # Say this action generates a bunch of C source files.
1465 action_foreach("my_action") {
1470 # Compile the resulting source files into a source set.
1471 source_set("my_lib") {
1472 sources = get_target_outputs(":my_action")
1477 ## **getenv**: Get an environment variable.
1480 value = getenv(env_var_name)
1482 Returns the value of the given enironment variable. If the value is
1483 not found, it will try to look up the variable with the "opposite"
1484 case (based on the case of the first letter of the variable), but
1485 is otherwise case-sensitive.
1487 If the environment variable is not found, the empty string will be
1488 returned. Note: it might be nice to extend this if we had the concept
1489 of "none" in the language to indicate lookup failure.
1496 home_dir = getenv("HOME")
1500 ## **group**: Declare a named group of targets.
1503 This target type allows you to create meta-targets that just collect a
1504 set of dependencies into one named target. Groups can additionally
1505 specify configs that apply to their dependents.
1507 Depending on a group is exactly like depending directly on that
1508 group's deps. Direct dependent configs will get automatically
1509 forwarded through the group so you shouldn't need to use
1510 "forward_dependent_configs_from.
1517 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1518 Dependent configs: all_dependent_configs, public_configs
1528 "//project:unit_tests",
1534 ## **import**: Import a file into the current scope.
1537 The import command loads the rules and variables resulting from
1538 executing the given file into the current scope.
1540 By convention, imported files are named with a .gni extension.
1542 An import is different than a C++ "include". The imported file is
1543 executed in a standalone environment from the caller of the import
1544 command. The results of this execution are cached for other files that
1545 import the same .gni file.
1547 Note that you can not import a BUILD.gn file that's otherwise used
1548 in the build. Files must either be imported or implicitly loaded as
1549 a result of deps rules, but not both.
1551 The imported file's scope will be merged with the scope at the point
1552 import was called. If there is a conflict (both the current scope and
1553 the imported file define some variable or rule with the same name but
1554 different value), a runtime error will be thrown. Therefore, it's good
1555 practice to minimize the stuff that an imported file defines.
1557 Variables and templates beginning with an underscore '_' are
1558 considered private and will not be imported. Imported files can use
1559 such variables for internal computation without affecting other files.
1566 import("//build/rules/idl_compilation_rule.gni")
1568 # Looks in the current directory.
1569 import("my_vars.gni")
1573 ## **print**: Prints to the console.
1576 Prints all arguments to the console separated by spaces. A newline is
1577 automatically appended to the end.
1579 This function is intended for debugging. Note that build files are run
1580 in parallel so you may get interleaved prints. A buildfile may also
1581 be executed more than once in parallel in the context of different
1582 toolchains so the prints from one file may be duplicated or
1583 interleaved with itself.
1589 print("Hello world")
1591 print(sources, deps)
1595 ## **process_file_template**: Do template expansion over a list of files.
1598 process_file_template(source_list, template)
1600 process_file_template applies a template list to a source file list,
1601 returning the result of applying each template to each source. This is
1602 typically used for computing output file names from input files.
1604 In most cases, get_target_outputs() will give the same result with
1605 shorter, more maintainable code. This function should only be used
1606 when that function can't be used (like there's no target or the target
1607 is defined in another build file).
1614 The source_list is a list of file names.
1616 The template can be a string or a list. If it is a list, multiple
1617 output strings are generated for each input.
1619 The template should contain source expansions to which each name in
1620 the source list is applied. See "gn help source_expansion".
1631 myoutputs = process_file_template(
1633 [ "$target_gen_dir/{{source_name_part}}.cc",
1634 "$target_gen_dir/{{source_name_part}}.h" ])
1636 The result in this case will be:
1637 [ "//out/Debug/foo.cc"
1639 "//out/Debug/bar.cc"
1640 "//out/Debug/bar.h" ]
1644 ## **read_file**: Read a file into a variable.
1647 read_file(filename, input_conversion)
1649 Whitespace will be trimmed from the end of the file. Throws an error
1650 if the file can not be opened.
1658 Filename to read, relative to the build file.
1661 Controls how the file is read and parsed.
1662 See "gn help input_conversion".
1668 lines = read_file("foo.txt", "list lines")
1672 ## **rebase_path**: Rebase a file or directory to another location.
1675 converted = rebase_path(input,
1679 Takes a string argument representing a file name, or a list of such
1680 strings and converts it/them to be relative to a different base
1683 When invoking the compiler or scripts, GN will automatically convert
1684 sources and include directories to be relative to the build directory.
1685 However, if you're passing files directly in the "args" array or
1686 doing other manual manipulations where GN doesn't know something is
1687 a file name, you will need to convert paths to be relative to what
1688 your tool is expecting.
1690 The common case is to use this to convert paths relative to the
1691 current directory to be relative to the build directory (which will
1692 be the current directory when executing scripts).
1694 If you want to convert a file path to be source-absolute (that is,
1695 beginning with a double slash like "//foo/bar"), you should use
1696 the get_path_info() function. This function won't work because it will
1697 always make relative paths, and it needs to support making paths
1698 relative to the source root, so can't also generate source-absolute
1699 paths without more special-cases.
1707 A string or list of strings representing file or directory names
1708 These can be relative paths ("foo/bar.txt"), system absolute
1709 paths ("/foo/bar.txt"), or source absolute paths
1713 The directory to convert the paths to be relative to. This can be
1714 an absolute path or a relative path (which will be treated
1715 as being relative to the current BUILD-file's directory).
1717 As a special case, if new_base is the empty string (the default),
1718 all paths will be converted to system-absolute native style paths
1719 with system path separators. This is useful for invoking external
1723 Directory representing the base for relative paths in the input.
1724 If this is not an absolute path, it will be treated as being
1725 relative to the current build file. Use "." (the default) to
1726 convert paths from the current BUILD-file's directory.
1730 ### **Return value**
1733 The return value will be the same type as the input value (either a
1734 string or a list of strings). All relative and source-absolute file
1735 names will be converted to be relative to the requested output
1736 System-absolute paths will be unchanged.
1743 # Convert a file in the current directory to be relative to the build
1744 # directory (the current dir when executing compilers and scripts).
1745 foo = rebase_path("myfile.txt", root_build_dir)
1746 # might produce "../../project/myfile.txt".
1748 # Convert a file to be system absolute:
1749 foo = rebase_path("myfile.txt")
1750 # Might produce "D:\source\project\myfile.txt" on Windows or
1751 # "/home/you/source/project/myfile.txt" on Linux.
1753 # Typical usage for converting to the build directory for a script.
1754 action("myscript") {
1755 # Don't convert sources, GN will automatically convert these to be
1756 # relative to the build directory when it constructs the command
1757 # line for your script.
1758 sources = [ "foo.txt", "bar.txt" ]
1760 # Extra file args passed manually need to be explicitly converted
1761 # to be relative to the build directory:
1764 rebase_path("//mything/data/input.dat", root_build_dir),
1766 rebase_path("relative_path.txt", root_build_dir)
1767 ] + rebase_path(sources, root_build_dir)
1772 ## **set_default_toolchain**: Sets the default toolchain name.
1775 set_default_toolchain(toolchain_label)
1777 The given label should identify a toolchain definition (see
1778 "help toolchain"). This toolchain will be used for all targets
1779 unless otherwise specified.
1781 This function is only valid to call during the processing of the build
1782 configuration file. Since the build configuration file is processed
1783 separately for each toolchain, this function will be a no-op when
1784 called under any non-default toolchains.
1786 For example, the default toolchain should be appropriate for the
1787 current environment. If the current environment is 32-bit and
1788 somebody references a target with a 64-bit toolchain, we wouldn't
1789 want processing of the build config file for the 64-bit toolchain to
1790 reset the default toolchain to 64-bit, we want to keep it 32-bits.
1805 set_default_toolchain("//build/config/win:vs32")
1808 ## **set_defaults**: Set default values for a target type.
1811 set_defaults(<target_type_name>) { <values...> }
1813 Sets the default values for a given target type. Whenever
1814 target_type_name is seen in the future, the values specified in
1815 set_default's block will be copied into the current scope.
1817 When the target type is used, the variable copying is very strict.
1818 If a variable with that name is already in scope, the build will fail
1821 set_defaults can be used for built-in target types ("executable",
1822 "shared_library", etc.) and custom ones defined via the "template"
1829 set_defaults("static_library") {
1830 configs = [ "//tools/mything:settings" ]
1833 static_library("mylib")
1834 # The configs will be auto-populated as above. You can remove it if
1835 # you don't want the default for a particular default:
1836 configs -= "//tools/mything:settings"
1841 ## **set_sources_assignment_filter**: Set a pattern to filter source files.
1844 The sources assignment filter is a list of patterns that remove files
1845 from the list implicitly whenever the "sources" variable is
1846 assigned to. This is intended to be used to globally filter out files
1847 with platform-specific naming schemes when they don't apply, for
1848 example, you may want to filter out all "*_win.cc" files on non-
1851 Typically this will be called once in the master build config script
1852 to set up the filter for the current platform. Subsequent calls will
1853 overwrite the previous values.
1855 If you want to bypass the filter and add a file even if it might
1856 be filtered out, call set_sources_assignment_filter([]) to clear the
1857 list of filters. This will apply until the current scope exits
1861 ### **How to use patterns**
1864 File patterns are VERY limited regular expressions. They must match
1865 the entire input string to be counted as a match. In regular
1866 expression parlance, there is an implicit "^...$" surrounding your
1867 input. If you want to match a substring, you need to use wildcards at
1868 the beginning and end.
1870 There are only two special tokens understood by the pattern matcher.
1871 Everything else is a literal.
1873 * Matches zero or more of any character. It does not depend on the
1874 preceding character (in regular expression parlance it is
1875 equivalent to ".*").
1877 \b Matches a path boundary. This will match the beginning or end of
1878 a string, or a slash.
1882 ### **Pattern examples**
1886 Matches a string containing "asdf" anywhere.
1889 Matches only the exact string "asdf".
1892 Matches strings ending in the literal ".cc".
1895 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
1899 ### **Sources assignment example**
1902 # Filter out all _win files.
1903 set_sources_assignment_filter([ "*_win.cc", "*_win.h" ])
1904 sources = [ "a.cc", "b_win.cc" ]
1906 # Will print [ "a.cc" ]. b_win one was filtered out.
1910 ## **shared_library**: Declare a shared library target.
1913 A shared library will be specified on the linker line for targets
1914 listing the shared library in its "deps". If you don't want this
1915 (say you dynamically load the library at runtime), then you should
1916 depend on the shared library via "data_deps" instead.
1923 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1924 defines, include_dirs, ldflags, lib_dirs, libs
1925 precompiled_header, precompiled_source
1926 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1927 Dependent configs: all_dependent_configs, public_configs
1928 General: check_includes, configs, data, inputs, output_name,
1929 output_extension, public, sources, testonly, visibility
1933 ## **source_set**: Declare a source set target.
1936 A source set is a collection of sources that get compiled, but are not
1937 linked to produce any kind of library. Instead, the resulting object
1938 files are implicitly added to the linker line of all targets that
1939 depend on the source set.
1941 In most cases, a source set will behave like a static library, except
1942 no actual library file will be produced. This will make the build go
1943 a little faster by skipping creation of a large static library, while
1944 maintaining the organizational benefits of focused build targets.
1946 The main difference between a source set and a static library is
1947 around handling of exported symbols. Most linkers assume declaring
1948 a function exported means exported from the static library. The linker
1949 can then do dead code elimination to delete code not reachable from
1952 A source set will not do this code elimination since there is no link
1953 step. This allows you to link many sources sets into a shared library
1954 and have the "exported symbol" notation indicate "export from the
1955 final shared library and not from the intermediate targets." There is
1956 no way to express this concept when linking multiple static libraries
1957 into a shared library.
1964 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1965 defines, include_dirs, ldflags, lib_dirs, libs
1966 precompiled_header, precompiled_source
1967 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1968 Dependent configs: all_dependent_configs, public_configs
1969 General: check_includes, configs, data, inputs, output_name,
1970 output_extension, public, sources, testonly, visibility
1974 ## **static_library**: Declare a static library target.
1977 Make a ".a" / ".lib" file.
1979 If you only need the static library for intermediate results in the
1980 build, you should consider a source_set instead since it will skip
1981 the (potentially slow) step of creating the intermediate library file.
1988 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1989 defines, include_dirs, ldflags, lib_dirs, libs
1990 precompiled_header, precompiled_source
1991 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1992 Dependent configs: all_dependent_configs, public_configs
1993 General: check_includes, configs, data, inputs, output_name,
1994 output_extension, public, sources, testonly, visibility
1998 ## **template**: Define a template rule.
2001 A template defines a custom name that acts like a function. It
2002 provides a way to add to the built-in target types.
2004 The template() function is used to declare a template. To invoke the
2005 template, just use the name of the template like any other target
2008 Often you will want to declare your template in a special file that
2009 other files will import (see "gn help import") so your template
2010 rule can be shared across build files.
2014 ### **More details**:
2017 When you call template() it creates a closure around all variables
2018 currently in scope with the code in the template block. When the
2019 template is invoked, the closure will be executed.
2021 When the template is invoked, the code in the caller is executed and
2022 passed to the template code as an implicit "invoker" variable. The
2023 template uses this to read state out of the invoking code.
2025 One thing explicitly excluded from the closure is the "current
2026 directory" against which relative file names are resolved. The
2027 current directory will be that of the invoking code, since typically
2028 that code specifies the file names. This means all files internal
2029 to the template should use absolute names.
2033 ### **Target naming**:
2036 Your template should almost always define a built-in target with the
2037 name the template invoker specified. For example, if you have an IDL
2038 template and somebody does:
2040 you will normally want this to expand to something defining a
2041 source_set or static_library named "foo" (among other things you may
2042 need). This way, when another target specifies a dependency on
2043 "foo", the static_library or source_set will be linked.
2045 It is also important that any other targets your template expands to
2046 have globally unique names, or you will get collisions.
2048 Access the invoking name in your template via the implicit
2049 "target_name" variable. This should also be the basis of how other
2050 targets that a template expands to to ensure uniquness.
2052 A typical example would be a template that defines an action to
2053 generate some source files, and a source_set to compile that source.
2054 Your template would name the source_set "target_name" because
2055 that's what you want external targets to depend on to link your code.
2056 And you would name the action something like "${target_name}_action"
2057 to make it unique. The source set would have a dependency on the
2058 action to make it run.
2062 ### **Example of defining a template**:
2065 template("my_idl") {
2066 # Be nice and help callers debug problems by checking that the
2067 # variables the template requires are defined. This gives a nice
2068 # message rather than giving the user an error about an
2069 # undefined variable in the file defining the template
2071 # You can also use defined() to give default values to variables
2072 # unspecified by the invoker.
2073 assert(defined(invoker.sources),
2074 "Need sources in $target_name listing the idl files.")
2076 # Name of the intermediate target that does the code gen. This must
2077 # incorporate the target name so it's unique across template
2079 code_gen_target_name = target_name + "_code_gen"
2081 # Intermediate target to convert IDL to C source. Note that the name
2082 # is based on the name the invoker of the template specified. This
2083 # way, each time the template is invoked we get a unique
2084 # intermediate action name (since all target names are in the global
2086 action_foreach(code_gen_target_name) {
2087 # Access the scope defined by the invoker via the implicit
2088 # "invoker" variable.
2089 sources = invoker.sources
2091 # Note that we need an absolute path for our script file name.
2092 # The current directory when executing this code will be that of
2093 # the invoker (this is why we can use the "sources" directly
2094 # above without having to rebase all of the paths). But if we need
2095 # to reference a script relative to the template file, we'll need
2096 # to use an absolute path instead.
2097 script = "//tools/idl/idl_code_generator.py"
2099 # Tell GN how to expand output names given the sources.
2100 # See "gn help source_expansion" for more.
2101 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
2102 "$target_gen_dir/{{source_name_part}}.h" ]
2105 # Name the source set the same as the template invocation so
2106 # instancing this template produces something that other targets
2107 # can link to in their deps.
2108 source_set(target_name) {
2109 # Generates the list of sources, we get these from the
2110 # action_foreach above.
2111 sources = get_target_outputs(":$code_gen_target_name")
2113 # This target depends on the files produced by the above code gen
2115 deps = [ ":$code_gen_target_name" ]
2121 ### **Example of invoking the resulting template**:
2124 # This calls the template code above, defining target_name to be
2125 # "foo_idl_files" and "invoker" to be the set of stuff defined in
2126 # the curly brackets.
2127 my_idl("foo_idl_files") {
2128 # Goes into the template as "invoker.sources".
2129 sources = [ "foo.idl", "bar.idl" ]
2132 # Here is a target that depends on our template.
2133 executable("my_exe") {
2134 # Depend on the name we gave the template call above. Internally,
2135 # this will produce a dependency from executable to the source_set
2136 # inside the template (since it has this name), which will in turn
2137 # depend on the code gen action.
2138 deps = [ ":foo_idl_files" ]
2143 ## **tool**: Specify arguments to a toolchain tool.
2160 "objc": Objective C compiler
2161 "objcxx": Objective C++ compiler
2162 "rc": Resource compiler (Windows .rc files)
2166 "alink": Linker for static libraries (archives)
2167 "solink": Linker for shared libraries
2168 "link": Linker for executables
2171 "stamp": Tool for creating stamp files
2172 "copy": Tool to copy files.
2176 ### **Tool variables**
2179 command [string with substitutions]
2180 Valid for: all tools (required)
2184 default_output_extension [string]
2185 Valid for: linker tools
2187 Extension for the main output of a linkable tool. It includes
2188 the leading dot. This will be the default value for the
2189 {{output_extension}} expansion (discussed below) but will be
2190 overridden by by the "output extension" variable in a target,
2191 if one is specified. Empty string means no extension.
2193 GN doesn't actually do anything with this extension other than
2194 pass it along, potentially with target-specific overrides. One
2195 would typically use the {{output_extension}} value in the
2196 "outputs" to read this value.
2198 Example: default_output_extension = ".exe"
2201 Valid for: compiler tools (optional)
2203 If the tool can write ".d" files, this specifies the name of
2204 the resulting file. These files are used to list header file
2205 dependencies (or other implicit input dependencies) that are
2206 discovered at build time. See also "depsformat".
2208 Example: depfile = "{{output}}.d"
2211 Valid for: compiler tools (when depfile is specified)
2213 Format for the deps outputs. This is either "gcc" or "msvc".
2214 See the ninja documentation for "deps" for more information.
2216 Example: depsformat = "gcc"
2218 description [string with substitutions, optional]
2219 Valid for: all tools
2221 What to print when the command is run.
2223 Example: description = "Compiling {{source}}"
2225 lib_switch [string, optional, link tools only]
2226 lib_dir_switch [string, optional, link tools only]
2227 Valid for: Linker tools except "alink"
2229 These strings will be prepended to the libraries and library
2230 search directories, respectively, because linkers differ on how
2231 specify them. If you specified:
2233 lib_dir_switch = "-L"
2234 then the "{{libs}}" expansion for [ "freetype", "expat"]
2235 would be "-lfreetype -lexpat".
2237 outputs [list of strings with substitutions]
2238 Valid for: Linker and compiler tools (required)
2240 An array of names for the output files the tool produces. These
2241 are relative to the build output directory. There must always be
2242 at least one output file. There can be more than one output (a
2243 linker might produce a library and an import library, for
2246 This array just declares to GN what files the tool will
2247 produce. It is your responsibility to specify the tool command
2248 that actually produces these files.
2250 If you specify more than one output for shared library links,
2251 you should consider setting link_output and depend_output.
2252 Otherwise, the first entry in the outputs list should always be
2253 the main output which will be linked to.
2255 Example for a compiler tool that produces .obj files:
2257 "{{source_out_dir}}/{{source_name_part}}.obj"
2260 Example for a linker tool that produces a .dll and a .lib. The
2261 use of {{output_extension}} rather than hardcoding ".dll"
2262 allows the extension of the library to be overridden on a
2263 target-by-target basis, but in this example, it always
2264 produces a ".lib" import library:
2266 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2267 "{{root_out_dir}}/{{target_output_name}}.lib",
2270 link_output [string with substitutions]
2271 depend_output [string with substitutions]
2272 Valid for: "solink" only (optional)
2274 These two files specify whch of the outputs from the solink
2275 tool should be used for linking and dependency tracking. These
2276 should match entries in the "outputs". If unspecified, the
2277 first item in the "outputs" array will be used for both. See
2278 "Separate linking and dependencies for shared libraries"
2281 On Windows, where the tools produce a .dll shared library and
2282 a .lib import library, you will want both of these to be the
2283 import library. On Linux, if you're not doing the separate
2284 linking/dependency optimization, both of these should be the
2287 output_prefix [string]
2288 Valid for: Linker tools (optional)
2290 Prefix to use for the output name. Defaults to empty. This
2291 prefix will be prepended to the name of the target (or the
2292 output_name if one is manually specified for it) if the prefix
2293 is not already there. The result will show up in the
2294 {{output_name}} substitution pattern.
2296 This is typically used to prepend "lib" to libraries on
2298 output_prefix = "lib"
2300 precompiled_header_type [string]
2301 Valid for: "cc", "cxx", "objc", "objcxx"
2303 Type of precompiled headers. If undefined or the empty string,
2304 precompiled headers will not be used for this tool. Otherwise
2305 use "msvc" which is the only currently supported value.
2307 For precompiled headers to be used for a given target, the
2308 target (or a config applied to it) must also specify a
2309 "precompiled_header" and, for "msvc"-style headers, a
2310 "precompiled_source" value.
2312 See "gn help precompiled_header" for more.
2315 Valid for: all tools (optional, defaults to false)
2317 Requests that Ninja check the file timestamp after this tool has
2318 run to determine if anything changed. Set this if your tool has
2319 the ability to skip writing output if the output file has not
2322 Normally, Ninja will assume that when a tool runs the output
2323 be new and downstream dependents must be rebuild. When this is
2324 set to trye, Ninja can skip rebuilding downstream dependents for
2325 input changes that don't actually affect the output.
2330 rspfile [string with substitutions]
2331 Valid for: all tools (optional)
2333 Name of the response file. If empty, no response file will be
2334 used. See "rspfile_content".
2336 rspfile_content [string with substitutions]
2337 Valid for: all tools (required when "rspfile" is specified)
2339 The contents to be written to the response file. This may
2340 include all or part of the command to send to the tool which
2341 allows you to get around OS command-line length limits.
2343 This example adds the inputs and libraries to a response file,
2344 but passes the linker flags directly on the command line:
2346 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
2347 rspfile = "{{output}}.rsp"
2348 rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
2353 ### **Expansions for tool variables**
2356 All paths are relative to the root build directory, which is the
2357 current directory for running all tools. These expansions are
2358 available to all tools:
2361 The label of the current target. This is typically used in the
2362 "description" field for link tools. The toolchain will be
2363 omitted from the label for targets in the default toolchain, and
2364 will be included for targets in other toolchains.
2367 The relative path and name of the output(s) of the current
2368 build step. If there is more than one output, this will expand
2369 to a list of all of them.
2370 Example: "out/base/my_file.o"
2374 The directory of the generated file and output directories,
2375 respectively, for the current target. There is no trailing
2377 Example: "out/base/test"
2379 {{target_output_name}}
2380 The short name of the current target with no path information,
2381 or the value of the "output_name" variable if one is specified
2382 in the target. This will include the "output_prefix" if any.
2383 Example: "libfoo" for the target named "foo" and an
2384 output prefix for the linker tool of "lib".
2386 Compiler tools have the notion of a single input and a single output,
2387 along with a set of compiler-specific flags. The following expansions
2397 Strings correspond that to the processed flags/defines/include
2398 directories specified for the target.
2399 Example: "--enable-foo --enable-bar"
2401 Defines will be prefixed by "-D" and include directories will
2402 be prefixed by "-I" (these work with Posix tools as well as
2406 The relative path and name of the current input file.
2407 Example: "../../base/my_file.cc"
2409 {{source_file_part}}
2410 The file part of the source including the extension (with no
2411 directory information).
2414 {{source_name_part}}
2415 The filename part of the source file with no directory or
2421 The directory in the generated file and output directories,
2422 respectively, for the current input file. If the source file
2423 is in the same directory as the target is declared in, they will
2424 will be the same as the "target" versions above.
2425 Example: "gen/base/test"
2427 Linker tools have multiple inputs and (potentially) multiple outputs
2428 The following expansions are available:
2432 Expands to the inputs to the link step. This will be a list of
2433 object files and static libraries.
2434 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
2436 The "_newline" version will separate the input files with
2437 newlines instead of spaces. This is useful in response files:
2438 some linkers can take a "-filelist" flag which expects newline
2439 separated files, and some Microsoft tools have a fixed-sized
2440 buffer for parsing each line of a response file.
2443 Expands to the processed set of ldflags and library search paths
2444 specified for the target.
2445 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
2448 Expands to the list of system libraries to link to. Each will
2449 be prefixed by the "lib_prefix".
2451 As a special case to support Mac, libraries with names ending in
2452 ".framework" will be added to the {{libs}} with "-framework"
2453 preceeding it, and the lib prefix will be ignored.
2455 Example: "-lfoo -lbar"
2457 {{output_extension}}
2458 The value of the "output_extension" variable in the target,
2459 or the value of the "default_output_extension" value in the
2460 tool if the target does not specify an output extension.
2464 Extra libraries from shared library dependencide not specified
2465 in the {{inputs}}. This is the list of link_output files from
2466 shared libraries (if the solink tool specifies a "link_output"
2467 variable separate from the "depend_output").
2469 These should generally be treated the same as libs by your tool.
2470 Example: "libfoo.so libbar.so"
2472 The copy tool allows the common compiler/linker substitutions, plus
2473 {{source}} which is the source of the copy. The stamp tool allows
2474 only the common tool substitutions.
2478 ### **Separate linking and dependencies for shared libraries**
2481 Shared libraries are special in that not all changes to them require
2482 that dependent targets be re-linked. If the shared library is changed
2483 but no imports or exports are different, dependent code needn't be
2484 relinked, which can speed up the build.
2486 If your link step can output a list of exports from a shared library
2487 and writes the file only if the new one is different, the timestamp of
2488 this file can be used for triggering re-links, while the actual shared
2489 library would be used for linking.
2491 You will need to specify
2493 in the linker tool to make this work, so Ninja will detect if the
2494 timestamp of the dependency file has changed after linking (otherwise
2495 it will always assume that running a command updates the output):
2500 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2501 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC",
2504 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
2506 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC"
2515 toolchain("my_toolchain") {
2516 # Put these at the top to apply to all tools below.
2518 lib_dir_prefix = "-L"
2521 command = "gcc {{source}} -o {{output}}"
2522 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2523 description = "GCC {{source}}"
2526 command = "g++ {{source}} -o {{output}}"
2527 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2528 description = "G++ {{source}}"
2534 ## **toolchain**: Defines a toolchain.
2537 A toolchain is a set of commands and build flags used to compile the
2538 source code. You can have more than one toolchain in use at once in
2543 ### **Functions and variables**
2547 The tool() function call specifies the commands commands to run for
2548 a given step. See "gn help tool".
2551 List of arguments to pass to the toolchain when invoking this
2552 toolchain. This applies only to non-default toolchains. See
2553 "gn help toolchain_args" for more.
2556 Dependencies of this toolchain. These dependencies will be resolved
2557 before any target in the toolchain is compiled. To avoid circular
2558 dependencies these must be targets defined in another toolchain.
2560 This is expressed as a list of targets, and generally these targets
2561 will always specify a toolchain:
2562 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
2564 This concept is somewhat inefficient to express in Ninja (it
2565 requires a lot of duplicate of rules) so should only be used when
2566 absolutely necessary.
2569 In integer expressing the number of links that Ninja will perform in
2570 parallel. GN will create a pool for shared library and executable
2571 link steps with this many processes. Since linking is memory- and
2572 I/O-intensive, projects with many large targets may want to limit
2573 the number of parallel steps to avoid overloading the computer.
2574 Since creating static libraries is generally not as intensive
2575 there is no limit to "alink" steps.
2577 Defaults to 0 which Ninja interprets as "no limit".
2579 The value used will be the one from the default toolchain of the
2584 ### **Invoking targets in toolchains**:
2587 By default, when a target depends on another, there is an implicit
2588 toolchain label that is inherited, so the dependee has the same one
2591 You can override this and refer to any other toolchain by explicitly
2592 labeling the toolchain to use. For example:
2593 data_deps = [ "//plugins:mine(//toolchains:plugin_toolchain)" ]
2594 The string "//build/toolchains:plugin_toolchain" is a label that
2595 identifies the toolchain declaration for compiling the sources.
2597 To load a file in an alternate toolchain, GN does the following:
2599 1. Loads the file with the toolchain definition in it (as determined
2600 by the toolchain label).
2601 2. Re-runs the master build configuration file, applying the
2602 arguments specified by the toolchain_args section of the toolchain
2603 definition (see "gn help toolchain_args").
2604 3. Loads the destination build file in the context of the
2605 configuration file in the previous step.
2611 toolchain("plugin_toolchain") {
2612 concurrent_links = 8
2615 command = "gcc {{source}}"
2628 ## **toolchain_args**: Set build arguments for toolchain build setup.
2631 Used inside a toolchain definition to pass arguments to an alternate
2632 toolchain's invocation of the build.
2634 When you specify a target using an alternate toolchain, the master
2635 build configuration file is re-interpreted in the context of that
2636 toolchain (see "gn help toolchain"). The toolchain_args function
2637 allows you to control the arguments passed into this alternate
2638 invocation of the build.
2640 Any default system arguments or arguments passed in on the command-
2641 line will also be passed to the alternate invocation unless explicitly
2642 overridden by toolchain_args.
2644 The toolchain_args will be ignored when the toolchain being defined
2645 is the default. In this case, it's expected you want the default
2648 See also "gn help buildargs" for an overview of these arguments.
2654 toolchain("my_weird_toolchain") {
2657 # Override the system values for a generic Posix system.
2661 # Pass this new value for specific setup for my toolchain.
2662 is_my_weird_system = true
2668 ## **write_file**: Write a file to disk.
2671 write_file(filename, data)
2673 If data is a list, the list will be written one-item-per-line with no
2674 quoting or brackets.
2676 If the file exists and the contents are identical to that being
2677 written, the file will not be updated. This will prevent unnecessary
2678 rebuilds of targets that depend on this file.
2680 TODO(brettw) we probably need an optional third argument to control
2689 Filename to write. This must be within the output directory.
2692 The list or string to write.
2696 ## **current_cpu**: The processor architecture of the current toolchain.
2699 The build configuration usually sets this value based on the value
2700 of "host_cpu" (see "gn help host_cpu") and then threads
2701 this through the toolchain definitions to ensure that it always
2702 reflects the appropriate value.
2704 This value is not used internally by GN for any purpose. It is
2705 set it to the empty string ("") by default but is declared so
2706 that it can be overridden on the command line if so desired.
2708 See "gn help target_cpu" for a list of common values returned.
2712 ## **current_os**: The operating system of the current toolchain.
2715 The build configuration usually sets this value based on the value
2716 of "target_os" (see "gn help target_os"), and then threads this
2717 through the toolchain definitions to ensure that it always reflects
2718 the appropriate value.
2720 This value is not used internally by GN for any purpose. It is
2721 set it to the empty string ("") by default but is declared so
2722 that it can be overridden on the command line if so desired.
2724 See "gn help target_os" for a list of common values returned.
2728 ## **current_toolchain**: Label of the current toolchain.
2731 A fully-qualified label representing the current toolchain. You can
2732 use this to make toolchain-related decisions in the build. See also
2733 "default_toolchain".
2740 if (current_toolchain == "//build:64_bit_toolchain") {
2741 executable("output_thats_64_bit_only") {
2746 ## **default_toolchain**: [string] Label of the default toolchain.
2749 A fully-qualified label representing the default toolchain, which may
2750 not necessarily be the current one (see "current_toolchain").
2754 ## **host_cpu**: The processor architecture that GN is running on.
2757 This is value is exposed so that cross-compile toolchains can
2758 access the host architecture when needed.
2760 The value should generally be considered read-only, but it can be
2761 overriden in order to handle unusual cases where there might
2762 be multiple plausible values for the host architecture (e.g., if
2763 you can do either 32-bit or 64-bit builds). The value is not used
2764 internally by GN for any purpose.
2768 ### **Some possible values**:
2775 ## **host_os**: [string] The operating system that GN is running on.
2778 This value is exposed so that cross-compiles can access the host
2779 build system's settings.
2781 This value should generally be treated as read-only. It, however,
2782 is not used internally by GN for any purpose.
2786 ### **Some possible values**:
2794 ## **python_path**: Absolute path of Python.
2797 Normally used in toolchain definitions if running some command
2798 requires Python. You will normally not need this when invoking scripts
2799 since GN automatically finds it for you.
2803 ## **root_build_dir**: [string] Directory where build commands are run.
2806 This is the root build output directory which will be the current
2807 directory when executing all compilers and scripts.
2809 Most often this is used with rebase_path (see "gn help rebase_path")
2810 to convert arguments to be relative to a script's current directory.
2814 ## **root_gen_dir**: Directory for the toolchain's generated files.
2817 Absolute path to the root of the generated output directory tree for
2818 the current toolchain. An example would be "//out/Debug/gen" for the
2819 default toolchain, or "//out/Debug/arm/gen" for the "arm"
2822 This is primarily useful for setting up include paths for generated
2823 files. If you are passing this to a script, you will want to pass it
2824 through rebase_path() (see "gn help rebase_path") to convert it
2825 to be relative to the build directory.
2827 See also "target_gen_dir" which is usually a better location for
2828 generated files. It will be inside the root generated dir.
2832 ## **root_out_dir**: [string] Root directory for toolchain output files.
2835 Absolute path to the root of the output directory tree for the current
2836 toolchain. It will not have a trailing slash.
2838 For the default toolchain this will be the same as the root_build_dir.
2839 An example would be "//out/Debug" for the default toolchain, or
2840 "//out/Debug/arm" for the "arm" toolchain.
2842 This is primarily useful for setting up script calls. If you are
2843 passing this to a script, you will want to pass it through
2844 rebase_path() (see "gn help rebase_path") to convert it
2845 to be relative to the build directory.
2847 See also "target_out_dir" which is usually a better location for
2848 output files. It will be inside the root output dir.
2855 action("myscript") {
2856 # Pass the output dir to the script.
2857 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
2862 ## **target_cpu**: The desired cpu architecture for the build.
2865 This value should be used to indicate the desired architecture for
2866 the primary objects of the build. It will match the cpu architecture
2867 of the default toolchain.
2869 In many cases, this is the same as "host_cpu", but in the case
2870 of cross-compiles, this can be set to something different. This
2871 value is different from "current_cpu" in that it can be referenced
2872 from inside any toolchain. This value can also be ignored if it is
2873 not needed or meaningful for a project.
2875 This value is not used internally by GN for any purpose, so it
2876 may be set to whatever value is needed for the build.
2877 GN defaults this value to the empty string ("") and the
2878 configuration files should set it to an appropriate value
2879 (e.g., setting it to the value of "host_cpu") if it is not
2880 overridden on the command line or in the args.gn file.
2882 Where practical, use one of the following list of common values:
2886 ### **Possible values**:
2896 ## **target_gen_dir**: Directory for a target's generated files.
2899 Absolute path to the target's generated file directory. This will be
2900 the "root_gen_dir" followed by the relative path to the current
2901 build file. If your file is in "//tools/doom_melon" then
2902 target_gen_dir would be "//out/Debug/gen/tools/doom_melon". It will
2903 not have a trailing slash.
2905 This is primarily useful for setting up include paths for generated
2906 files. If you are passing this to a script, you will want to pass it
2907 through rebase_path() (see "gn help rebase_path") to convert it
2908 to be relative to the build directory.
2910 See also "gn help root_gen_dir".
2917 action("myscript") {
2918 # Pass the generated output dir to the script.
2919 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
2924 ## **target_os**: The desired operating system for the build.
2927 This value should be used to indicate the desired operating system
2928 for the primary object(s) of the build. It will match the OS of
2929 the default toolchain.
2931 In many cases, this is the same as "host_os", but in the case of
2932 cross-compiles, it may be different. This variable differs from
2933 "current_os" in that it can be referenced from inside any
2934 toolchain and will always return the initial value.
2936 This should be set to the most specific value possible. So,
2937 "android" or "chromeos" should be used instead of "linux"
2938 where applicable, even though Android and ChromeOS are both Linux
2939 variants. This can mean that one needs to write
2941 if (target_os == "android" || target_os == "linux") {
2947 This value is not used internally by GN for any purpose, so it
2948 may be set to whatever value is needed for the build.
2949 GN defaults this value to the empty string ("") and the
2950 configuration files should set it to an appropriate value
2951 (e.g., setting it to the value of "host_os") if it is not
2952 set via the command line or in the args.gn file.
2954 Where practical, use one of the following list of common values:
2958 ### **Possible values**:
2970 ## **target_out_dir**: [string] Directory for target output files.
2973 Absolute path to the target's generated file directory. If your
2974 current target is in "//tools/doom_melon" then this value might be
2975 "//out/Debug/obj/tools/doom_melon". It will not have a trailing
2978 This is primarily useful for setting up arguments for calling
2979 scripts. If you are passing this to a script, you will want to pass it
2980 through rebase_path() (see "gn help rebase_path") to convert it
2981 to be relative to the build directory.
2983 See also "gn help root_out_dir".
2990 action("myscript") {
2991 # Pass the output dir to the script.
2992 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
2997 ## **all_dependent_configs**: Configs to be forced on dependents.
3000 A list of config labels.
3002 All targets depending on this one, and recursively, all targets
3003 depending on those, will have the configs listed in this variable
3004 added to them. These configs will also apply to the current target.
3006 This addition happens in a second phase once a target and all of its
3007 dependencies have been resolved. Therefore, a target will not see
3008 these force-added configs in their "configs" variable while the
3009 script is running, and then can not be removed. As a result, this
3010 capability should generally only be used to add defines and include
3011 directories necessary to compile a target's headers.
3013 See also "public_configs".
3017 ### **Ordering of flags and values**:
3020 1. Those set on the current target (not in a config).
3021 2. Those set on the "configs" on the target in order that the
3022 configs appear in the list.
3023 3. Those set on the "all_dependent_configs" on the target in order
3024 that the configs appear in the list.
3025 4. Those set on the "public_configs" on the target in order that
3026 those configs appear in the list.
3027 5. all_dependent_configs pulled from dependencies, in the order of
3028 the "deps" list. This is done recursively. If a config appears
3029 more than once, only the first occurance will be used.
3030 6. public_configs pulled from dependencies, in the order of the
3031 "deps" list. If a dependency is public, they will be applied
3036 ## **allow_circular_includes_from**: Permit includes from deps.
3039 A list of target labels. Must be a subset of the target's "deps".
3040 These targets will be permitted to include headers from the current
3041 target despite the dependency going in the opposite direction.
3045 ### **Tedious exposition**
3048 Normally, for a file in target A to include a file from target B,
3049 A must list B as a dependency. This invariant is enforced by the
3050 "gn check" command (and the --check flag to "gn gen").
3052 Sometimes, two targets might be the same unit for linking purposes
3053 (two source sets or static libraries that would always be linked
3054 together in a final executable or shared library). In this case,
3055 you want A to be able to include B's headers, and B to include A's
3058 This list, if specified, lists which of the dependencies of the
3059 current target can include header files from the current target.
3060 That is, if A depends on B, B can only include headers from A if it is
3061 in A's allow_circular_includes_from list.
3069 deps = [ ":b", ":c" ]
3070 allow_circular_includes_from = [ ":b" ]
3076 ## **args**: Arguments passed to an action.
3079 For action and action_foreach targets, args is the list of arguments
3080 to pass to the script. Typically you would use source expansion (see
3081 "gn help source_expansion") to insert the source file names.
3083 See also "gn help action" and "gn help action_foreach".
3087 ## **cflags***: Flags passed to the C compiler.
3092 "cflags" are passed to all invocations of the C, C++, Objective C,
3093 and Objective C++ compilers.
3095 To target one of these variants individually, use "cflags_c",
3096 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3097 These variant-specific versions will be appended to the "cflags".
3101 ### **Ordering of flags and values**:
3104 1. Those set on the current target (not in a config).
3105 2. Those set on the "configs" on the target in order that the
3106 configs appear in the list.
3107 3. Those set on the "all_dependent_configs" on the target in order
3108 that the configs appear in the list.
3109 4. Those set on the "public_configs" on the target in order that
3110 those configs appear in the list.
3111 5. all_dependent_configs pulled from dependencies, in the order of
3112 the "deps" list. This is done recursively. If a config appears
3113 more than once, only the first occurance will be used.
3114 6. public_configs pulled from dependencies, in the order of the
3115 "deps" list. If a dependency is public, they will be applied
3120 ## **cflags***: Flags passed to the C compiler.
3125 "cflags" are passed to all invocations of the C, C++, Objective C,
3126 and Objective C++ compilers.
3128 To target one of these variants individually, use "cflags_c",
3129 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3130 These variant-specific versions will be appended to the "cflags".
3134 ### **Ordering of flags and values**:
3137 1. Those set on the current target (not in a config).
3138 2. Those set on the "configs" on the target in order that the
3139 configs appear in the list.
3140 3. Those set on the "all_dependent_configs" on the target in order
3141 that the configs appear in the list.
3142 4. Those set on the "public_configs" on the target in order that
3143 those configs appear in the list.
3144 5. all_dependent_configs pulled from dependencies, in the order of
3145 the "deps" list. This is done recursively. If a config appears
3146 more than once, only the first occurance will be used.
3147 6. public_configs pulled from dependencies, in the order of the
3148 "deps" list. If a dependency is public, they will be applied
3153 ## **cflags***: Flags passed to the C compiler.
3158 "cflags" are passed to all invocations of the C, C++, Objective C,
3159 and Objective C++ compilers.
3161 To target one of these variants individually, use "cflags_c",
3162 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3163 These variant-specific versions will be appended to the "cflags".
3167 ### **Ordering of flags and values**:
3170 1. Those set on the current target (not in a config).
3171 2. Those set on the "configs" on the target in order that the
3172 configs appear in the list.
3173 3. Those set on the "all_dependent_configs" on the target in order
3174 that the configs appear in the list.
3175 4. Those set on the "public_configs" on the target in order that
3176 those configs appear in the list.
3177 5. all_dependent_configs pulled from dependencies, in the order of
3178 the "deps" list. This is done recursively. If a config appears
3179 more than once, only the first occurance will be used.
3180 6. public_configs pulled from dependencies, in the order of the
3181 "deps" list. If a dependency is public, they will be applied
3186 ## **cflags***: Flags passed to the C compiler.
3191 "cflags" are passed to all invocations of the C, C++, Objective C,
3192 and Objective C++ compilers.
3194 To target one of these variants individually, use "cflags_c",
3195 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3196 These variant-specific versions will be appended to the "cflags".
3200 ### **Ordering of flags and values**:
3203 1. Those set on the current target (not in a config).
3204 2. Those set on the "configs" on the target in order that the
3205 configs appear in the list.
3206 3. Those set on the "all_dependent_configs" on the target in order
3207 that the configs appear in the list.
3208 4. Those set on the "public_configs" on the target in order that
3209 those configs appear in the list.
3210 5. all_dependent_configs pulled from dependencies, in the order of
3211 the "deps" list. This is done recursively. If a config appears
3212 more than once, only the first occurance will be used.
3213 6. public_configs pulled from dependencies, in the order of the
3214 "deps" list. If a dependency is public, they will be applied
3219 ## **cflags***: Flags passed to the C compiler.
3224 "cflags" are passed to all invocations of the C, C++, Objective C,
3225 and Objective C++ compilers.
3227 To target one of these variants individually, use "cflags_c",
3228 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3229 These variant-specific versions will be appended to the "cflags".
3233 ### **Ordering of flags and values**:
3236 1. Those set on the current target (not in a config).
3237 2. Those set on the "configs" on the target in order that the
3238 configs appear in the list.
3239 3. Those set on the "all_dependent_configs" on the target in order
3240 that the configs appear in the list.
3241 4. Those set on the "public_configs" on the target in order that
3242 those configs appear in the list.
3243 5. all_dependent_configs pulled from dependencies, in the order of
3244 the "deps" list. This is done recursively. If a config appears
3245 more than once, only the first occurance will be used.
3246 6. public_configs pulled from dependencies, in the order of the
3247 "deps" list. If a dependency is public, they will be applied
3252 ## **check_includes**: [boolean] Controls whether a target's files are checked.
3255 When true (the default), the "gn check" command (as well as
3256 "gn gen" with the --check flag) will check this target's sources
3257 and headers for proper dependencies.
3259 When false, the files in this target will be skipped by default.
3260 This does not affect other targets that depend on the current target,
3261 it just skips checking the includes of the current target's files.
3265 ### **Controlling includes individually**
3268 If only certain includes are problematic, you can annotate them
3269 individually rather than disabling header checking on an entire
3270 target. Add the string "nogncheck" to the include line:
3272 #include "foo/something_weird.h" // nogncheck (bug 12345)
3274 It is good form to include a reference to a bug (if the include is
3275 improper, or some other comment expressing why the header checker
3276 doesn't work for this particular case.
3278 The most common reason to need "nogncheck" is conditional includes.
3279 The header checker does not understand the preprocessor, so may flag
3280 some includes as improper even if the dependencies and #defines are
3281 always matched correctly:
3283 #if defined(ENABLE_DOOM_MELON)
3284 #include "doom_melon/beam_controller.h" // nogncheck
3292 source_set("busted_includes") {
3293 # This target's includes are messed up, exclude it from checking.
3294 check_includes = false
3300 ## **complete_static_lib**: [boolean] Links all deps into a static library.
3303 A static library normally doesn't include code from dependencies, but
3304 instead forwards the static libraries and source sets in its deps up
3305 the dependency chain until a linkable target (an executable or shared
3306 library) is reached. The final linkable target only links each static
3307 library once, even if it appears more than once in its dependency
3310 In some cases the static library might be the final desired output.
3311 For example, you may be producing a static library for distribution to
3312 third parties. In this case, the static library should include code
3313 for all dependencies in one complete package. Since GN does not unpack
3314 static libraries to forward their contents up the dependency chain,
3315 it is an error for complete static libraries to depend on other static
3323 static_library("foo") {
3324 complete_static_lib = true
3330 ## **configs**: Configs applying to this target.
3333 A list of config labels.
3335 The include_dirs, defines, etc. in each config are appended in the
3336 order they appear to the compile command for each file in the target.
3337 They will appear after the include_dirs, defines, etc. that the target
3340 The build configuration script will generally set up the default
3341 configs applying to a given target type (see "set_defaults").
3342 When a target is being defined, it can add to or remove from this
3347 ### **Ordering of flags and values**:
3350 1. Those set on the current target (not in a config).
3351 2. Those set on the "configs" on the target in order that the
3352 configs appear in the list.
3353 3. Those set on the "all_dependent_configs" on the target in order
3354 that the configs appear in the list.
3355 4. Those set on the "public_configs" on the target in order that
3356 those configs appear in the list.
3357 5. all_dependent_configs pulled from dependencies, in the order of
3358 the "deps" list. This is done recursively. If a config appears
3359 more than once, only the first occurance will be used.
3360 6. public_configs pulled from dependencies, in the order of the
3361 "deps" list. If a dependency is public, they will be applied
3368 static_library("foo") {
3369 configs -= "//build:no_rtti" # Don't use the default RTTI config.
3370 configs += ":mysettings" # Add some of our own settings.
3375 ## **data**: Runtime data file dependencies.
3378 Lists files or directories required to run the given target. These are
3379 typically data files or directories of data files. The paths are
3380 interpreted as being relative to the current build file. Since these
3381 are runtime dependencies, they do not affect which targets are built
3382 or when. To declare input files to a script, use "inputs".
3384 Appearing in the "data" section does not imply any special handling
3385 such as copying them to the output directory. This is just used for
3386 declaring runtime dependencies. Runtime dependencies can be queried
3387 using the "runtime_deps" category of "gn desc" or written during
3388 build generation via "--runtime-deps-list-file".
3390 GN doesn't require data files to exist at build-time. So actions that
3391 produce files that are in turn runtime dependencies can list those
3392 generated files both in the "outputs" list as well as the "data"
3395 By convention, directories are be listed with a trailing slash:
3396 data = [ "test/data/" ]
3397 However, no verification is done on these so GN doesn't enforce this.
3398 The paths are just rebased and passed along when requested.
3400 See "gn help runtime_deps" for how these are used.
3404 ## **data_deps**: Non-linked dependencies.
3407 A list of target labels.
3409 Specifies dependencies of a target that are not actually linked into
3410 the current target. Such dependencies will built and will be available
3413 This is normally used for things like plugins or helper programs that
3414 a target needs at runtime.
3416 See also "gn help deps" and "gn help data".
3424 data_deps = [ "//plugins:my_runtime_plugin" ]
3429 ## **defines**: C preprocessor defines.
3434 These strings will be passed to the C/C++ compiler as #defines. The
3435 strings may or may not include an "=" to assign a value.
3439 ### **Ordering of flags and values**:
3442 1. Those set on the current target (not in a config).
3443 2. Those set on the "configs" on the target in order that the
3444 configs appear in the list.
3445 3. Those set on the "all_dependent_configs" on the target in order
3446 that the configs appear in the list.
3447 4. Those set on the "public_configs" on the target in order that
3448 those configs appear in the list.
3449 5. all_dependent_configs pulled from dependencies, in the order of
3450 the "deps" list. This is done recursively. If a config appears
3451 more than once, only the first occurance will be used.
3452 6. public_configs pulled from dependencies, in the order of the
3453 "deps" list. If a dependency is public, they will be applied
3460 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
3464 ## **depfile**: [string] File name for input dependencies for actions.
3467 If nonempty, this string specifies that the current action or
3468 action_foreach target will generate the given ".d" file containing
3469 the dependencies of the input. Empty or unset means that the script
3470 doesn't generate the files.
3472 The .d file should go in the target output directory. If you have more
3473 than one source file that the script is being run over, you can use
3474 the output file expansions described in "gn help action_foreach" to
3475 name the .d file according to the input.
3476 The format is that of a Makefile, and all of the paths should be
3477 relative to the root build directory.
3483 action_foreach("myscript_target") {
3484 script = "myscript.py"
3487 # Locate the depfile in the output directory named like the
3488 # inputs but with a ".d" appended.
3489 depfile = "$relative_target_output_dir/{{source_name}}.d"
3491 # Say our script uses "-o <d file>" to indicate the depfile.
3492 args = [ "{{source}}", "-o", depfile ]
3497 ## **deps**: Private linked dependencies.
3500 A list of target labels.
3502 Specifies private dependencies of a target. Shared and dynamic
3503 libraries will be linked into the current target. Other target types
3504 that can't be linked (like actions and groups) listed in "deps" will
3505 be treated as "data_deps". Likewise, if the current target isn't
3506 linkable, then all deps will be treated as "data_deps".
3508 These dependencies are private in that it does not grant dependent
3509 targets the ability to include headers from the dependency, and direct
3510 dependent configs are not forwarded.
3512 See also "public_deps" and "data_deps".
3516 ## **forward_dependent_configs_from**
3519 A list of target labels.
3521 DEPRECATED. Use public_deps instead which will have the same effect.
3523 Exposes the public_configs from a private dependent target as
3524 public_configs of the current one. Each label in this list
3525 must also be in the deps.
3527 Generally you should use public_deps instead of this variable to
3528 express the concept of exposing a dependency as part of a target's
3529 public API. We're considering removing this variable.
3536 Sometimes you depend on a child library that exports some necessary
3537 configuration via public_configs. If your target in turn exposes the
3538 child library's headers in its public headers, it might mean that
3539 targets that depend on you won't work: they'll be seeing the child
3540 library's code but not the necessary configuration. This list
3541 specifies which of your deps' direct dependent configs to expose as
3549 If we use a given library "a" from our public headers:
3551 deps = [ ":a", ":b", ... ]
3552 forward_dependent_configs_from = [ ":a" ]
3554 This example makes a "transparent" target that forwards a dependency
3558 if (use_system_frob) {
3559 deps = ":system_frob"
3561 deps = "//third_party/fallback_frob"
3563 forward_dependent_configs_from = deps
3568 ## **include_dirs**: Additional include directories.
3571 A list of source directories.
3573 The directories in this list will be added to the include path for
3574 the files in the affected target.
3578 ### **Ordering of flags and values**:
3581 1. Those set on the current target (not in a config).
3582 2. Those set on the "configs" on the target in order that the
3583 configs appear in the list.
3584 3. Those set on the "all_dependent_configs" on the target in order
3585 that the configs appear in the list.
3586 4. Those set on the "public_configs" on the target in order that
3587 those configs appear in the list.
3588 5. all_dependent_configs pulled from dependencies, in the order of
3589 the "deps" list. This is done recursively. If a config appears
3590 more than once, only the first occurance will be used.
3591 6. public_configs pulled from dependencies, in the order of the
3592 "deps" list. If a dependency is public, they will be applied
3599 include_dirs = [ "src/include", "//third_party/foo" ]
3603 ## **inputs**: Additional compile-time dependencies.
3606 Inputs are compile-time dependencies of the current target. This means
3607 that all inputs must be available before compiling any of the sources
3608 or executing any actions.
3610 Inputs are typically only used for action and action_foreach targets.
3614 ### **Inputs for actions**
3617 For action and action_foreach targets, inputs should be the inputs to
3618 script that don't vary. These should be all .py files that the script
3619 uses via imports (the main script itself will be an implcit dependency
3620 of the action so need not be listed).
3622 For action targets, inputs should be the entire set of inputs the
3623 script needs. For action_foreach targets, inputs should be the set of
3624 dependencies that don't change. These will be applied to each script
3625 invocation over the sources.
3627 Note that another way to declare input dependencies from an action
3628 is to have the action write a depfile (see "gn help depfile"). This
3629 allows the script to dynamically write input dependencies, that might
3630 not be known until actually executing the script. This is more
3631 efficient than doing processing while running GN to determine the
3632 inputs, and is easier to keep in-sync than hardcoding the list.
3636 ### **Inputs for binary targets**
3639 Any input dependencies will be resolved before compiling any sources.
3640 Normally, all actions that a target depends on will be run before any
3641 files in a target are compiled. So if you depend on generated headers,
3642 you do not typically need to list them in the inputs section.
3649 action("myscript") {
3650 script = "domything.py"
3651 inputs = [ "input.data" ]
3656 ## **ldflags**: Flags passed to the linker.
3661 These flags are passed on the command-line to the linker and generally
3662 specify various linking options. Most targets will not need these and
3663 will use "libs" and "lib_dirs" instead.
3665 ldflags are NOT pushed to dependents, so applying ldflags to source
3666 sets or static libraries will be a no-op. If you want to apply ldflags
3667 to dependent targets, put them in a config and set it in the
3668 all_dependent_configs or public_configs.
3672 ## **lib_dirs**: Additional library directories.
3675 A list of directories.
3677 Specifies additional directories passed to the linker for searching
3678 for the required libraries. If an item is not an absolute path, it
3679 will be treated as being relative to the current build file.
3681 libs and lib_dirs work differently than other flags in two respects.
3682 First, then are inherited across static library boundaries until a
3683 shared library or executable target is reached. Second, they are
3684 uniquified so each one is only passed once (the first instance of it
3685 will be the one used).
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 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
3714 ## **libs**: Additional libraries to link.
3719 These files will be passed to the linker, which will generally search
3720 the library include path. Unlike a normal list of files, they will be
3721 passed to the linker unmodified rather than being treated as file
3722 names relative to the current build file. Generally you would set
3723 the "lib_dirs" so your library is found. If you need to specify
3724 a path, you can use "rebase_path" to convert a path to be relative
3725 to the build directory.
3727 When constructing the linker command, the "lib_prefix" attribute of
3728 the linker tool in the current toolchain will be prepended to each
3729 library. So your BUILD file should not specify the switch prefix
3730 (like "-l"). On Mac, libraries ending in ".framework" will be
3731 special-cased: the switch "-framework" will be prepended instead of
3732 the lib_prefix, and the ".framework" suffix will be trimmed.
3734 libs and lib_dirs work differently than other flags in two respects.
3735 First, then are inherited across static library boundaries until a
3736 shared library or executable target is reached. Second, they are
3737 uniquified so each one is only passed once (the first instance of it
3738 will be the one used).
3742 ### **Ordering of flags and values**:
3745 1. Those set on the current target (not in a config).
3746 2. Those set on the "configs" on the target in order that the
3747 configs appear in the list.
3748 3. Those set on the "all_dependent_configs" on the target in order
3749 that the configs appear in the list.
3750 4. Those set on the "public_configs" on the target in order that
3751 those configs appear in the list.
3752 5. all_dependent_configs pulled from dependencies, in the order of
3753 the "deps" list. This is done recursively. If a config appears
3754 more than once, only the first occurance will be used.
3755 6. public_configs pulled from dependencies, in the order of the
3756 "deps" list. If a dependency is public, they will be applied
3764 libs = [ "ctl3d.lib" ]
3770 ## **output_extension**: Value to use for the output's file extension.
3773 Normally the file extension for a target is based on the target
3774 type and the operating system, but in rare cases you will need to
3775 override the name (for example to use "libfreetype.so.6" instead
3776 of libfreetype.so on Linux).
3779 ## **output_name**: Define a name for the output file other than the default.
3782 Normally the output name of a target will be based on the target name,
3783 so the target "//foo/bar:bar_unittests" will generate an output
3784 file such as "bar_unittests.exe" (using Windows as an example).
3786 Sometimes you will want an alternate name to avoid collisions or
3787 if the internal name isn't appropriate for public distribution.
3789 The output name should have no extension or prefixes, these will be
3790 added using the default system rules. For example, on Linux an output
3791 name of "foo" will produce a shared library "libfoo.so".
3793 This variable is valid for all binary output target types.
3799 static_library("doom_melon") {
3800 output_name = "fluffy_bunny"
3805 ## **outputs**: Output files for actions and copy targets.
3808 Outputs is valid for "copy", "action", and "action_foreach"
3809 target types and indicates the resulting files. The values may contain
3810 source expansions to generate the output names from the sources (see
3811 "gn help source_expansion").
3813 For copy targets, the outputs is the destination for the copied
3814 file(s). For actions, the outputs should be the list of files
3815 generated by the script.
3819 ## **precompiled_header**: [string] Header file to precompile.
3822 Precompiled headers will be used when a target specifies this
3823 value, or a config applying to this target specifies this value.
3824 In addition, the tool corresponding to the source files must also
3825 specify precompiled headers (see "gn help tool"). The tool
3826 will also specify what type of precompiled headers to use.
3828 The precompiled header/source variables can be specified on a target
3829 or a config, but must be the same for all configs applying to a given
3830 target since a target can only have one precompiled header.
3834 ### **MSVC precompiled headers**
3837 When using MSVC-style precompiled headers, the "precompiled_header"
3838 value is a string corresponding to the header. This is NOT a path
3839 to a file that GN recognises, but rather the exact string that appears
3840 in quotes after an #include line in source code. The compiler will
3841 match this string against includes or forced includes (/FI).
3843 MSVC also requires a source file to compile the header with. This must
3844 be specified by the "precompiled_source" value. In contrast to the
3845 header value, this IS a GN-style file name, and tells GN which source
3846 file to compile to make the .pch file used for subsequent compiles.
3848 If you use both C and C++ sources, the precompiled header and source
3849 file will be compiled using both tools. You will want to make sure
3850 to wrap C++ includes in __cplusplus #ifdefs so the file will compile
3853 For example, if the toolchain specifies MSVC headers:
3855 toolchain("vc_x64") {
3858 precompiled_header_type = "msvc"
3861 You might make a config like this:
3863 config("use_precompiled_headers") {
3864 precompiled_header = "build/precompile.h"
3865 precompiled_source = "//build/precompile.cc"
3867 # Either your source files should #include "build/precompile.h"
3868 # first, or you can do this to force-include the header.
3869 cflags = [ "/FI$precompiled_header" ]
3872 And then define a target that uses the config:
3874 executable("doom_melon") {
3875 configs += [ ":use_precompiled_headers" ]
3881 ## **precompiled_source**: [file name] Source file to precompile.
3884 The source file that goes along with the precompiled_header when
3885 using "msvc"-style precompiled headers. It will be implicitly added
3886 to the sources of the target. See "gn help precompiled_header".
3890 ## **public**: Declare public header files for a target.
3893 A list of files that other targets can include. These permissions are
3894 checked via the "check" command (see "gn help check").
3896 If no public files are declared, other targets (assuming they have
3897 visibility to depend on this target can include any file in the
3898 sources list. If this variable is defined on a target, dependent
3899 targets may only include files on this whitelist.
3901 Header file permissions are also subject to visibility. A target
3902 must be visible to another target to include any files from it at all
3903 and the public headers indicate which subset of those files are
3904 permitted. See "gn help visibility" for more.
3906 Public files are inherited through the dependency tree. So if there is
3907 a dependency A -> B -> C, then A can include C's public headers.
3908 However, the same is NOT true of visibility, so unless A is in C's
3909 visibility list, the include will be rejected.
3911 GN only knows about files declared in the "sources" and "public"
3912 sections of targets. If a file is included that is not known to the
3913 build, it will be allowed.
3919 These exact files are public:
3920 public = [ "foo.h", "bar.h" ]
3922 No files are public (no targets may include headers from this one):
3927 ## **public_configs**: Configs to be applied on dependents.
3930 A list of config labels.
3932 Targets directly depending on this one will have the configs listed in
3933 this variable added to them. These configs will also apply to the
3936 This addition happens in a second phase once a target and all of its
3937 dependencies have been resolved. Therefore, a target will not see
3938 these force-added configs in their "configs" variable while the
3939 script is running, and then can not be removed. As a result, this
3940 capability should generally only be used to add defines and include
3941 directories necessary to compile a target's headers.
3943 See also "all_dependent_configs".
3947 ### **Ordering of flags and values**:
3950 1. Those set on the current target (not in a config).
3951 2. Those set on the "configs" on the target in order that the
3952 configs appear in the list.
3953 3. Those set on the "all_dependent_configs" on the target in order
3954 that the configs appear in the list.
3955 4. Those set on the "public_configs" on the target in order that
3956 those configs appear in the list.
3957 5. all_dependent_configs pulled from dependencies, in the order of
3958 the "deps" list. This is done recursively. If a config appears
3959 more than once, only the first occurance will be used.
3960 6. public_configs pulled from dependencies, in the order of the
3961 "deps" list. If a dependency is public, they will be applied
3966 ## **public_deps**: Declare public dependencies.
3969 Public dependencies are like private dependencies ("deps") but
3970 additionally express that the current target exposes the listed deps
3971 as part of its public API.
3973 This has several ramifications:
3975 - public_configs that are part of the dependency are forwarded
3976 to direct dependents.
3978 - Public headers in the dependency are usable by dependents
3979 (includes do not require a direct dependency or visibility).
3981 - If the current target is a shared library, other shared libraries
3982 that it publicly depends on (directly or indirectly) are
3983 propagated up the dependency tree to dependents for linking.
3990 Say you have three targets: A -> B -> C. C's visibility may allow
3991 B to depend on it but not A. Normally, this would prevent A from
3992 including any headers from C, and C's public_configs would apply
3995 If B lists C in its public_deps instead of regular deps, A will now
3996 inherit C's public_configs and the ability to include C's public
3999 Generally if you are writing a target B and you include C's headers
4000 as part of B's public headers, or targets depending on B should
4001 consider B and C to be part of a unit, you should use public_deps
4009 # This target can include files from "c" but not from
4010 # "super_secret_implementation_details".
4015 shared_library("b") {
4016 deps = [ ":super_secret_implementation_details" ]
4017 public_deps = [ ":c" ]
4022 ## **script**: Script file for actions.
4025 An absolute or buildfile-relative file name of a Python script to run
4026 for a action and action_foreach targets (see "gn help action" and
4027 "gn help action_foreach").
4031 ## **sources**: Source files for a target
4034 A list of files relative to the current buildfile.
4038 ## **testonly**: Declares a target must only be used for testing.
4041 Boolean. Defaults to false.
4043 When a target is marked "testonly = true", it must only be depended
4044 on by other test-only targets. Otherwise, GN will issue an error
4045 that the depenedency is not allowed.
4047 This feature is intended to prevent accidentally shipping test code
4055 source_set("test_support") {
4062 ## **visibility**: A list of labels that can depend on a target.
4065 A list of labels and label patterns that define which targets can
4066 depend on the current one. These permissions are checked via the
4067 "check" command (see "gn help check").
4069 If visibility is not defined, it defaults to public ("*").
4071 If visibility is defined, only the targets with labels that match it
4072 can depend on the current target. The empty list means no targets
4073 can depend on the current target.
4075 Tip: Often you will want the same visibility for all targets in a
4076 BUILD file. In this case you can just put the definition at the top,
4077 outside of any target, and the targets will inherit that scope and see
4085 See "gn help label_pattern" for more details on what types of
4086 patterns are supported. If a toolchain is specified, only targets
4087 in that toolchain will be matched. If a toolchain is not specified on
4088 a pattern, targets in all toolchains will be matched.
4095 Only targets in the current buildfile ("private"):
4096 visibility = [ ":*" ]
4098 No targets (used for targets that should be leaf nodes):
4101 Any target ("public", the default):
4102 visibility = [ "*" ]
4104 All targets in the current directory and any subdirectory:
4105 visibility = [ "./*" ]
4107 Any target in "//bar/BUILD.gn":
4108 visibility = [ "//bar:*" ]
4110 Any target in "//bar/" or any subdirectory thereof:
4111 visibility = [ "//bar/*" ]
4113 Just these specific targets:
4114 visibility = [ ":mything", "//foo:something_else" ]
4116 Any target in the current directory and any subdirectory thereof, plus
4117 any targets in "//bar/" and any subdirectory thereof.
4118 visibility = [ "./*", "//bar/*" ]
4122 ## **Build Arguments Overview**
4125 Build arguments are variables passed in from outside of the build
4126 that build files can query to determine how the build works.
4130 ### **How build arguments are set**
4133 First, system default arguments are set based on the current system.
4134 The built-in arguments are:
4142 If specified, arguments from the --args command line flag are used. If
4143 that flag is not specified, args from previous builds in the build
4144 directory will be used (this is in the file args.gn in the build
4147 Last, for targets being compiled with a non-default toolchain, the
4148 toolchain overrides are applied. These are specified in the
4149 toolchain_args section of a toolchain definition. The use-case for
4150 this is that a toolchain may be building code for a different
4151 platform, and that it may want to always specify Posix, for example.
4152 See "gn help toolchain_args" for more.
4154 If you specify an override for a build argument that never appears in
4155 a "declare_args" call, a nonfatal error will be displayed.
4163 Create the directory out/FooBar and open an editor. You would type
4164 something like this into that file:
4165 enable_doom_melon=false
4168 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
4169 This will overwrite the build directory with the given arguments.
4170 (Note that the quotes inside the args command will usually need to
4171 be escaped for your shell to pass through strings values.)
4175 ### **How build arguments are used**
4178 If you want to use an argument, you use declare_args() and specify
4179 default values. These default values will apply if none of the steps
4180 listed in the "How build arguments are set" section above apply to
4181 the given argument, but the defaults will not override any of these.
4183 Often, the root build config file will declare global arguments that
4184 will be passed to all buildfiles. Individual build files can also
4185 specify arguments that apply only to those files. It is also useful
4186 to specify build args in an "import"-ed file if you want such
4187 arguments to apply to multiple buildfiles.
4194 When gn starts, it will search the current directory and parent
4195 directories for a file called ".gn". This indicates the source root.
4196 You can override this detection by using the --root command-line
4199 The .gn file in the source root will be executed. The syntax is the
4200 same as a buildfile, but with very limited build setup-specific
4203 If you specify --root, by default GN will look for the file .gn in
4204 that directory. If you want to specify a different file, you can
4205 additionally pass --dotfile:
4207 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
4214 buildconfig [required]
4215 Label of the build config file. This file will be used to set up
4216 the build file execution environment for each toolchain.
4218 check_targets [optional]
4219 A list of labels and label patterns that should be checked when
4220 running "gn check" or "gn gen --check". If unspecified, all
4221 targets will be checked. If it is the empty list, no targets will
4224 The format of this list is identical to that of "visibility"
4225 so see "gn help visibility" for examples.
4227 exec_script_whitelist [optional]
4228 A list of .gn/.gni files (not labels) that have permission to call
4229 the exec_script function. If this list is defined, calls to
4230 exec_script will be checked against this list and GN will fail if
4231 the current file isn't in the list.
4233 This is to allow the use of exec_script to be restricted since
4234 is easy to use inappropriately. Wildcards are not supported.
4235 Files in the secondary_source tree (if defined) should be
4236 referenced by ignoring the secondary tree and naming them as if
4237 they are in the main tree.
4239 If unspecified, the ability to call exec_script is unrestricted.
4242 exec_script_whitelist = [
4244 "//build/my_config.gni",
4248 Label of the root build target. The GN build will start by loading
4249 the build file containing this target name. This defaults to
4250 "//:" which will cause the file //BUILD.gn to be loaded.
4252 secondary_source [optional]
4253 Label of an alternate directory tree to find input files. When
4254 searching for a BUILD.gn file (or the build config file discussed
4255 above), the file will first be looked for in the source root.
4256 If it's not found, the secondary source root will be checked
4257 (which would contain a parallel directory hierarchy).
4259 This behavior is intended to be used when BUILD.gn files can't be
4260 checked in to certain source directories for whatever reason.
4262 The secondary source root must be inside the main source tree.
4266 ### **Example .gn file contents**
4269 buildconfig = "//build/config/BUILDCONFIG.gn"
4272 "//doom_melon/*", # Check everything in this subtree.
4273 "//tools:mind_controlling_ant", # Check this specific target.
4278 secondary_source = "//build/config/temporary_buildfiles/"
4282 ## **GN build language grammar**
4287 GN build files are read as sequences of tokens. While splitting the
4288 file into tokens, the next token is the longest sequence of characters
4289 that form a valid token.
4293 ### **White space and comments**
4296 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
4297 carriage returns (U+000D), and newlines (U+000A).
4299 Comments start at the character "#" and stop at the next newline.
4301 White space and comments are ignored except that they may separate
4302 tokens that would otherwise combine into a single token.
4309 Identifiers name variables and functions.
4311 identifier = letter { letter | digit } .
4312 letter = "A" ... "Z" | "a" ... "z" | "_" .
4313 digit = "0" ... "9" .
4320 The following keywords are reserved and may not be used as
4327 ### **Integer literals**
4330 An integer literal represents a decimal integer value.
4332 integer = [ "-" ] digit { digit } .
4334 Leading zeros and negative zero are disallowed.
4338 ### **String literals**
4341 A string literal represents a string value consisting of the quoted
4342 characters with possible escape sequences and variable expansions.
4344 string = `"` { char | escape | expansion } `"` .
4345 escape = `\` ( "$" | `"` | char ) .
4346 expansion = "$" ( identifier | "{" identifier "}" ) .
4347 char = /* any character except "$", `"`, or newline */ .
4349 After a backslash, certain sequences represent special characters:
4351 \" U+0022 quotation mark
4352 \$ U+0024 dollar sign
4355 All other backslashes represent themselves.
4362 The following character sequences represent punctuation:
4374 The input tokens form a syntax tree following a context-free grammar:
4376 File = StatementList .
4378 Statement = Assignment | Call | Condition .
4379 Assignment = identifier AssignOp Expr .
4380 Call = identifier "(" [ ExprList ] ")" [ Block ] .
4381 Condition = "if" "(" Expr ")" Block
4382 [ "else" ( Condition | Block ) ] .
4383 Block = "{" StatementList "}" .
4384 StatementList = { Statement } .
4386 Expr = UnaryExpr | Expr BinaryOp Expr .
4387 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
4388 PrimaryExpr = identifier | integer | string | Call
4389 | identifier "[" Expr "]"
4390 | identifier "." identifier
4392 | "[" [ ExprList [ "," ] ] "]" .
4393 ExprList = Expr { "," Expr } .
4395 AssignOp = "=" | "+=" | "-=" .
4397 BinaryOp = "+" | "-" // highest priority
4398 | "<" | "<=" | ">" | ">="
4401 | "||" . // lowest priority
4403 All binary operators are left-associative.
4407 ## **input_conversion**: Specifies how to transform input to a variable.
4410 input_conversion is an argument to read_file and exec_script that
4411 specifies how the result of the read operation should be converted
4415 Discard the result and return None.
4418 Return the file contents as a list, with a string for each line.
4419 The newlines will not be present in the result. The last line may
4420 or may not end in a newline.
4422 After splitting, each individual line will be trimmed of
4423 whitespace on both ends.
4426 Execute the block as GN code and return a scope with the
4427 resulting values in it. If the input was:
4428 a = [ "hello.cc", "world.cc" ]
4430 and you read the result into a variable named "val", then you
4431 could access contents the "." operator on "val":
4436 Return the file contents into a single string.
4439 Parse the input as if it was a literal rvalue in a buildfile.
4440 Examples of typical program output using this mode:
4441 [ "foo", "bar" ] (result will be a list)
4443 "foo bar" (result will be a string)
4445 5 (result will be an integer)
4447 Note that if the input is empty, the result will be a null value
4448 which will produce an error if assigned to a variable.
4451 Prefixing any of the other transformations with the word "trim"
4452 will result in whitespace being trimmed from the beginning and end
4453 of the result before processing.
4455 Examples: "trim string" or "trim list lines"
4457 Note that "trim value" is useless because the value parser skips
4462 ## **Label patterns**
4465 A label pattern is a way of expressing one or more labels in a portion
4466 of the source tree. They are not general regular expressions.
4468 They can take the following forms only:
4470 - Explicit (no wildcard):
4474 - Wildcard target names:
4475 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
4476 ":*" (all targets in the current build file)
4478 - Wildcard directory names ("*" is only supported at the end)
4480 "//foo/bar/*" (all targets in any subdir of //foo/bar)
4481 "./*" (all targets in the current build file or sub dirs)
4483 Any of the above forms can additionally take an explicit toolchain.
4484 In this case, the toolchain must be fully qualified (no wildcards
4485 are supported in the toolchain name).
4487 "//foo:bar(//build/toochain:mac)"
4488 An explicit target in an explicit toolchain.
4490 ":*(//build/toolchain/linux:32bit)"
4491 All targets in the current build file using the 32-bit Linux
4494 "//foo/*(//build/toolchain:win)"
4495 All targets in //foo and any subdirectory using the Windows
4500 ## **Runtime dependencies**
4503 Runtime dependencies of a target are exposed via the "runtime_deps"
4504 category of "gn desc" (see "gn help desc") or they can be written
4505 at build generation time via "--runtime-deps-list-file"
4506 (see "gn help --runtime-deps-list-file").
4508 To a first approximation, the runtime dependencies of a target are
4509 the set of "data" files, data directories, and the shared libraries
4510 from all transitive dependencies. Executables and shared libraries are
4511 considered runtime dependencies of themselves.
4518 Executable targets and those executable targets' transitive
4519 dependencies are not considered unless that executable is listed in
4520 "data_deps". Otherwise, GN assumes that the executable (and
4521 everything it requires) is a build-time dependency only.
4523 Action and copy targets that are listed as "data_deps" will have all
4524 of their outputs and data files considered as runtime dependencies.
4525 Action and copy targets that are "deps" or "public_deps" will have
4526 only their data files considered as runtime dependencies. These
4527 targets can list an output file in both the "outputs" and "data"
4528 lists to force an output file as a runtime dependency in all cases.
4530 The results of static_library or source_set targets are not considered
4531 runtime dependencies since these are assumed to be intermediate
4532 targets only. If you need to list a static library as a runtime
4533 dependency, you can manually compute the .a/.lib file name for the
4534 current platform and list it in the "data" list of a target
4535 (possibly on the static library target itself).
4537 When a tool produces more than one output, only the first output
4538 is considered. For example, a shared library target may produce a
4539 .dll and a .lib file on Windows. Only the .dll file will be considered
4540 a runtime dependency.
4544 ## **How Source Expansion Works**
4547 Source expansion is used for the action_foreach and copy target types
4548 to map source file names to output file names or arguments.
4550 To perform source expansion in the outputs, GN maps every entry in the
4551 sources to every entry in the outputs list, producing the cross
4552 product of all combinations, expanding placeholders (see below).
4554 Source expansion in the args works similarly, but performing the
4555 placeholder substitution produces a different set of arguments for
4556 each invocation of the script.
4558 If no placeholders are found, the outputs or args list will be treated
4559 as a static list of literal file names that do not depend on the
4562 See "gn help copy" and "gn help action_foreach" for more on how
4567 ### **Placeholders**
4571 The name of the source file including directory (*). This will
4572 generally be used for specifying inputs to a script in the
4574 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
4576 {{source_file_part}}
4577 The file part of the source including the extension.
4578 "//foo/bar/baz.txt" => "baz.txt"
4580 {{source_name_part}}
4581 The filename part of the source file with no directory or
4582 extension. This will generally be used for specifying a
4583 transformation from a soruce file to a destination file with the
4584 same name but different extension.
4585 "//foo/bar/baz.txt" => "baz"
4588 The directory (*) containing the source file with no
4590 "//foo/bar/baz.txt" => "../../foo/bar"
4592 {{source_root_relative_dir}}
4593 The path to the source file's directory relative to the source
4594 root, with no leading "//" or trailing slashes. If the path is
4595 system-absolute, (beginning in a single slash) this will just
4596 return the path with no trailing slash. This value will always
4597 be the same, regardless of whether it appears in the "outputs"
4599 "//foo/bar/baz.txt" => "foo/bar"
4602 The generated file directory (*) corresponding to the source
4603 file's path. This will be different than the target's generated
4604 file directory if the source file is in a different directory
4605 than the BUILD.gn file.
4606 "//foo/bar/baz.txt" => "gen/foo/bar"
4609 The object file directory (*) corresponding to the source file's
4610 path, relative to the build directory. this us be different than
4611 the target's out directory if the source file is in a different
4612 directory than the build.gn file.
4613 "//foo/bar/baz.txt" => "obj/foo/bar"
4617 ### **(*) Note on directories**
4620 Paths containing directories (except the source_root_relative_dir)
4621 will be different depending on what context the expansion is evaluated
4622 in. Generally it should "just work" but it means you can't
4623 concatenate strings containing these values with reasonable results.
4625 Details: source expansions can be used in the "outputs" variable,
4626 the "args" variable, and in calls to "process_file_template". The
4627 "args" are passed to a script which is run from the build directory,
4628 so these directories will relative to the build directory for the
4629 script to find. In the other cases, the directories will be source-
4630 absolute (begin with a "//") because the results of those expansions
4631 will be handled by GN internally.
4638 Non-varying outputs:
4639 action("hardcoded_outputs") {
4640 sources = [ "input1.idl", "input2.idl" ]
4641 outputs = [ "$target_out_dir/output1.dat",
4642 "$target_out_dir/output2.dat" ]
4644 The outputs in this case will be the two literal files given.
4647 action_foreach("varying_outputs") {
4648 sources = [ "input1.idl", "input2.idl" ]
4649 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
4650 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
4652 Performing source expansion will result in the following output names:
4653 //out/Debug/obj/mydirectory/input1.h
4654 //out/Debug/obj/mydirectory/input1.cc
4655 //out/Debug/obj/mydirectory/input2.h
4656 //out/Debug/obj/mydirectory/input2.cc
4660 **Available global switches
4661 ** Do "gn help --the_switch_you_want_help_on" for more. Individual
4662 commands may take command-specific switches not listed here. See the
4663 help on your specific command for more.
4667 ** --args**: Specifies build arguments overrides.
4668 ** --color**: Force colored output.
4669 ** --dotfile**: Override the name of the ".gn" file.
4670 ** --markdown**: write the output in the Markdown format.
4671 ** --nocolor**: Force non-colored output.
4672 ** -q**: Quiet mode. Don't print output on success.
4673 ** --root**: Explicitly specify source root.
4674 ** --runtime-deps-list-file**: Save runtime dependencies for targets in file.
4675 ** --time**: Outputs a summary of how long everything took.
4676 ** --tracelog**: Writes a Chrome-compatible trace log to the given file.
4677 ** -v**: Verbose logging.
4678 ** --version**: Prints the GN version number and exits.