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/basictypes.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
1017 ### **Variables on a target used to apply configs**:
1019 all_dependent_configs, configs, public_configs,
1020 forward_dependent_configs_from
1026 config("myconfig") {
1027 includes = [ "include/common" ]
1028 defines = [ "ENABLE_DOOM_MELON" ]
1031 executable("mything") {
1032 configs = [ ":myconfig" ]
1037 ## **copy**: Declare a target that copies files.
1039 ### **File name handling**
1042 All output files must be inside the output directory of the build.
1043 You would generally use |$target_out_dir| or |$target_gen_dir| to
1044 reference the output or generated intermediate file directories,
1047 Both "sources" and "outputs" must be specified. Sources can
1048 as many files as you want, but there can only be one item in the
1049 outputs list (plural is used for the name for consistency with
1050 other target types).
1052 If there is more than one source file, your output name should specify
1053 a mapping from each source files to output file names using source
1054 expansion (see "gn help source_expansion"). The placeholders will
1055 will look like "{{source_name_part}}", for example.
1062 # Write a rule that copies a checked-in DLL to the output directory.
1064 sources = [ "mydll.dll" ]
1065 outputs = [ "$target_out_dir/mydll.dll" ]
1068 # Write a rule to copy several files to the target generated files
1071 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1073 # Use source expansion to generate output files with the
1074 # corresponding file names in the gen dir. This will just copy each
1076 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1081 ## **declare_args**: Declare build arguments.
1084 Introduces the given arguments into the current scope. If they are
1085 not specified on the command line or in a toolchain's arguments,
1086 the default values given in the declare_args block will be used.
1087 However, these defaults will not override command-line values.
1089 See also "gn help buildargs" for an overview.
1096 enable_teleporter = true
1097 enable_doom_melon = false
1100 If you want to override the (default disabled) Doom Melon:
1101 gn --args="enable_doom_melon=true enable_teleporter=false"
1102 This also sets the teleporter, but it's already defaulted to on so
1103 it will have no effect.
1107 ## **defined**: Returns whether an identifier is defined.
1110 Returns true if the given argument is defined. This is most useful in
1111 templates to assert that the caller set things up properly.
1113 You can pass an identifier:
1115 which will return true or false depending on whether foo is defined in
1118 You can also check a named scope:
1120 which will return true or false depending on whether bar is defined in
1121 the named scope foo. It will throw an error if foo is not defined or
1129 template("mytemplate") {
1130 # To help users call this template properly...
1131 assert(defined(invoker.sources), "Sources must be defined")
1133 # If we want to accept an optional "values" argument, we don't
1134 # want to dereference something that may not be defined.
1135 if (defined(invoker.values)) {
1136 values = invoker.values
1138 values = "some default value"
1144 ## **exec_script**: Synchronously run a script and return the output.
1147 exec_script(filename,
1149 input_conversion = "",
1150 file_dependencies = [])
1152 Runs the given script, returning the stdout of the script. The build
1153 generation will fail if the script does not exist or returns a nonzero
1156 The current directory when executing the script will be the root
1157 build directory. If you are passing file names, you will want to use
1158 the rebase_path() function to make file names relative to this
1159 path (see "gn help rebase_path").
1167 File name of python script to execute. Non-absolute names will
1168 be treated as relative to the current build file.
1171 A list of strings to be passed to the script as arguments.
1172 May be unspecified or the empty list which means no arguments.
1175 Controls how the file is read and parsed.
1176 See "gn help input_conversion".
1178 If unspecified, defaults to the empty string which causes the
1179 script result to be discarded. exec script will return None.
1182 (Optional) A list of files that this script reads or otherwise
1183 depends on. These dependencies will be added to the build result
1184 such that if any of them change, the build will be regenerated and
1185 the script will be re-run.
1187 The script itself will be an implicit dependency so you do not
1195 all_lines = exec_script(
1196 "myscript.py", [some_input], "list lines",
1197 [ rebase_path("data_file.txt", root_build_dir) ])
1199 # This example just calls the script with no arguments and discards
1201 exec_script("//foo/bar/myscript.py")
1205 ## **executable**: Declare an executable target.
1210 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1211 defines, include_dirs, ldflags, lib_dirs, libs
1212 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1213 Dependent configs: all_dependent_configs, public_configs
1214 General: check_includes, configs, data, inputs, output_name,
1215 output_extension, public, sources, testonly, visibility
1219 ## **foreach**: Iterate over a list.
1222 foreach(<loop_var>, <list>) {
1226 Executes the loop contents block over each item in the list,
1227 assigning the loop_var to each item in sequence.
1229 The block does not introduce a new scope, so that variable assignments
1230 inside the loop will be visible once the loop terminates.
1232 The loop variable will temporarily shadow any existing variables with
1233 the same name for the duration of the loop. After the loop terminates
1234 the loop variable will no longer be in scope, and the previous value
1235 (if any) will be restored.
1242 mylist = [ "a", "b", "c" ]
1243 foreach(i, mylist) {
1254 ## **get_label_info**: Get an attribute from a target's label.
1257 get_label_info(target_label, what)
1259 Given the label of a target, returns some attribute of that target.
1260 The target need not have been previously defined in the same file,
1261 since none of the attributes depend on the actual target definition,
1262 only the label itself.
1264 See also "gn help get_target_outputs".
1268 ### **Possible values for the "what" parameter**
1272 The short name of the target. This will match the value of the
1273 "target_name" variable inside that target's declaration. For the
1274 label "//foo/bar:baz" this will return "baz".
1277 The directory containing the target's definition, with no slash at
1278 the end. For the label "//foo/bar:baz" this will return
1282 The generated file directory for the target. This will match the
1283 value of the "target_gen_dir" variable when inside that target's
1287 The root of the generated file tree for the target. This will
1288 match the value of the "root_gen_dir" variable when inside that
1289 target's declaration.
1292 The output directory for the target. This will match the
1293 value of the "target_out_dir" variable when inside that target's
1297 The root of the output file tree for the target. This will
1298 match the value of the "root_gen_dir" variable when inside that
1299 target's declaration.
1301 "label_no_toolchain"
1302 The fully qualified version of this label, not including the
1303 toolchain. For the input ":bar" it might return
1306 "label_with_toolchain"
1307 The fully qualified version of this label, including the
1308 toolchain. For the input ":bar" it might return
1309 "//foo:bar(//toolchain:x64)".
1312 The label of the toolchain. This will match the value of the
1313 "current_toolchain" variable when inside that target's
1321 get_label_info(":foo", "name")
1322 # Returns string "foo".
1324 get_label_info("//foo/bar:baz", "gen_dir")
1325 # Returns string "//out/Debug/gen/foo/bar".
1329 ## **get_path_info**: Extract parts of a file or directory name.
1332 get_path_info(input, what)
1334 The first argument is either a string representing a file or
1335 directory name, or a list of such strings. If the input is a list
1336 the return value will be a list containing the result of applying the
1337 rule to each item in the input.
1341 ### **Possible values for the "what" parameter**
1345 The substring after the last slash in the path, including the name
1346 and extension. If the input ends in a slash, the empty string will
1348 "foo/bar.txt" => "bar.txt"
1349 "bar.txt" => "bar.txt"
1354 The substring of the file name not including the extension.
1355 "foo/bar.txt" => "bar"
1360 The substring following the last period following the last slash,
1361 or the empty string if not found. The period is not included.
1362 "foo/bar.txt" => "txt"
1366 The directory portion of the name, not including the slash.
1367 "foo/bar.txt" => "foo"
1368 "//foo/bar" => "//foo"
1371 The result will never end in a slash, so if the resulting
1372 is empty, the system ("/") or source ("//") roots, a "."
1373 will be appended such that it is always legal to append a slash
1374 and a filename and get a valid path.
1377 The output file directory corresponding to the path of the
1378 given file, not including a trailing slash.
1379 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
1381 The generated file directory corresponding to the path of the
1382 given file, not including a trailing slash.
1383 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
1386 The full absolute path name to the file or directory. It will be
1387 resolved relative to the currebt directory, and then the source-
1388 absolute version will be returned. If the input is system-
1389 absolute, the same input will be returned.
1390 "foo/bar.txt" => "//mydir/foo/bar.txt"
1391 "foo/" => "//mydir/foo/"
1392 "//foo/bar" => "//foo/bar" (already absolute)
1393 "/usr/include" => "/usr/include" (already absolute)
1395 If you want to make the path relative to another directory, or to
1396 be system-absolute, see rebase_path().
1402 sources = [ "foo.cc", "foo.h" ]
1403 result = get_path_info(source, "abspath")
1404 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
1406 result = get_path_info("//foo/bar/baz.cc", "dir")
1407 # result will be "//foo/bar"
1409 # Extract the source-absolute directory name,
1410 result = get_path_info(get_path_info(path, "dir"), "abspath")
1414 ## **get_target_outputs**: [file list] Get the list of outputs from a target.
1417 get_target_outputs(target_label)
1419 Returns a list of output files for the named target. The named target
1420 must have been previously defined in the current file before this
1421 function is called (it can't reference targets in other files because
1422 there isn't a defined execution order, and it obviously can't
1423 reference targets that are defined after the function call).
1425 Only copy and action targets are supported. The outputs from binary
1426 targets will depend on the toolchain definition which won't
1427 necessarily have been loaded by the time a given line of code has run,
1428 and source sets and groups have no useful output file.
1432 ### **Return value**
1435 The names in the resulting list will be absolute file paths (normally
1436 like "//out/Debug/bar.exe", depending on the build directory).
1438 action targets: this will just return the files specified in the
1439 "outputs" variable of the target.
1441 action_foreach targets: this will return the result of applying
1442 the output template to the sources (see "gn help source_expansion").
1443 This will be the same result (though with guaranteed absolute file
1444 paths), as process_file_template will return for those inputs
1445 (see "gn help process_file_template").
1447 binary targets (executables, libraries): this will return a list
1448 of the resulting binary file(s). The "main output" (the actual
1449 binary or library) will always be the 0th element in the result.
1450 Depending on the platform and output type, there may be other output
1451 files as well (like import libraries) which will follow.
1453 source sets and groups: this will return a list containing the path of
1454 the "stamp" file that Ninja will produce once all outputs are
1455 generated. This probably isn't very useful.
1462 # Say this action generates a bunch of C source files.
1463 action_foreach("my_action") {
1468 # Compile the resulting source files into a source set.
1469 source_set("my_lib") {
1470 sources = get_target_outputs(":my_action")
1475 ## **getenv**: Get an environment variable.
1478 value = getenv(env_var_name)
1480 Returns the value of the given enironment variable. If the value is
1481 not found, it will try to look up the variable with the "opposite"
1482 case (based on the case of the first letter of the variable), but
1483 is otherwise case-sensitive.
1485 If the environment variable is not found, the empty string will be
1486 returned. Note: it might be nice to extend this if we had the concept
1487 of "none" in the language to indicate lookup failure.
1494 home_dir = getenv("HOME")
1498 ## **group**: Declare a named group of targets.
1501 This target type allows you to create meta-targets that just collect a
1502 set of dependencies into one named target. Groups can additionally
1503 specify configs that apply to their dependents.
1505 Depending on a group is exactly like depending directly on that
1506 group's deps. Direct dependent configs will get automatically
1507 forwarded through the group so you shouldn't need to use
1508 "forward_dependent_configs_from.
1515 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1516 Dependent configs: all_dependent_configs, public_configs
1526 "//project:unit_tests",
1532 ## **import**: Import a file into the current scope.
1535 The import command loads the rules and variables resulting from
1536 executing the given file into the current scope.
1538 By convention, imported files are named with a .gni extension.
1540 An import is different than a C++ "include". The imported file is
1541 executed in a standalone environment from the caller of the import
1542 command. The results of this execution are cached for other files that
1543 import the same .gni file.
1545 Note that you can not import a BUILD.gn file that's otherwise used
1546 in the build. Files must either be imported or implicitly loaded as
1547 a result of deps rules, but not both.
1549 The imported file's scope will be merged with the scope at the point
1550 import was called. If there is a conflict (both the current scope and
1551 the imported file define some variable or rule with the same name but
1552 different value), a runtime error will be thrown. Therefore, it's good
1553 practice to minimize the stuff that an imported file defines.
1555 Variables and templates beginning with an underscore '_' are
1556 considered private and will not be imported. Imported files can use
1557 such variables for internal computation without affecting other files.
1564 import("//build/rules/idl_compilation_rule.gni")
1566 # Looks in the current directory.
1567 import("my_vars.gni")
1571 ## **print**: Prints to the console.
1574 Prints all arguments to the console separated by spaces. A newline is
1575 automatically appended to the end.
1577 This function is intended for debugging. Note that build files are run
1578 in parallel so you may get interleaved prints. A buildfile may also
1579 be executed more than once in parallel in the context of different
1580 toolchains so the prints from one file may be duplicated or
1581 interleaved with itself.
1587 print("Hello world")
1589 print(sources, deps)
1593 ## **process_file_template**: Do template expansion over a list of files.
1596 process_file_template(source_list, template)
1598 process_file_template applies a template list to a source file list,
1599 returning the result of applying each template to each source. This is
1600 typically used for computing output file names from input files.
1602 In most cases, get_target_outputs() will give the same result with
1603 shorter, more maintainable code. This function should only be used
1604 when that function can't be used (like there's no target or the target
1605 is defined in another build file).
1612 The source_list is a list of file names.
1614 The template can be a string or a list. If it is a list, multiple
1615 output strings are generated for each input.
1617 The template should contain source expansions to which each name in
1618 the source list is applied. See "gn help source_expansion".
1629 myoutputs = process_file_template(
1631 [ "$target_gen_dir/{{source_name_part}}.cc",
1632 "$target_gen_dir/{{source_name_part}}.h" ])
1634 The result in this case will be:
1635 [ "//out/Debug/foo.cc"
1637 "//out/Debug/bar.cc"
1638 "//out/Debug/bar.h" ]
1642 ## **read_file**: Read a file into a variable.
1645 read_file(filename, input_conversion)
1647 Whitespace will be trimmed from the end of the file. Throws an error
1648 if the file can not be opened.
1656 Filename to read, relative to the build file.
1659 Controls how the file is read and parsed.
1660 See "gn help input_conversion".
1666 lines = read_file("foo.txt", "list lines")
1670 ## **rebase_path**: Rebase a file or directory to another location.
1673 converted = rebase_path(input,
1677 Takes a string argument representing a file name, or a list of such
1678 strings and converts it/them to be relative to a different base
1681 When invoking the compiler or scripts, GN will automatically convert
1682 sources and include directories to be relative to the build directory.
1683 However, if you're passing files directly in the "args" array or
1684 doing other manual manipulations where GN doesn't know something is
1685 a file name, you will need to convert paths to be relative to what
1686 your tool is expecting.
1688 The common case is to use this to convert paths relative to the
1689 current directory to be relative to the build directory (which will
1690 be the current directory when executing scripts).
1692 If you want to convert a file path to be source-absolute (that is,
1693 beginning with a double slash like "//foo/bar"), you should use
1694 the get_path_info() function. This function won't work because it will
1695 always make relative paths, and it needs to support making paths
1696 relative to the source root, so can't also generate source-absolute
1697 paths without more special-cases.
1705 A string or list of strings representing file or directory names
1706 These can be relative paths ("foo/bar.txt"), system absolute
1707 paths ("/foo/bar.txt"), or source absolute paths
1711 The directory to convert the paths to be relative to. This can be
1712 an absolute path or a relative path (which will be treated
1713 as being relative to the current BUILD-file's directory).
1715 As a special case, if new_base is the empty string (the default),
1716 all paths will be converted to system-absolute native style paths
1717 with system path separators. This is useful for invoking external
1721 Directory representing the base for relative paths in the input.
1722 If this is not an absolute path, it will be treated as being
1723 relative to the current build file. Use "." (the default) to
1724 convert paths from the current BUILD-file's directory.
1728 ### **Return value**
1731 The return value will be the same type as the input value (either a
1732 string or a list of strings). All relative and source-absolute file
1733 names will be converted to be relative to the requested output
1734 System-absolute paths will be unchanged.
1741 # Convert a file in the current directory to be relative to the build
1742 # directory (the current dir when executing compilers and scripts).
1743 foo = rebase_path("myfile.txt", root_build_dir)
1744 # might produce "../../project/myfile.txt".
1746 # Convert a file to be system absolute:
1747 foo = rebase_path("myfile.txt")
1748 # Might produce "D:\source\project\myfile.txt" on Windows or
1749 # "/home/you/source/project/myfile.txt" on Linux.
1751 # Typical usage for converting to the build directory for a script.
1752 action("myscript") {
1753 # Don't convert sources, GN will automatically convert these to be
1754 # relative to the build directory when it constructs the command
1755 # line for your script.
1756 sources = [ "foo.txt", "bar.txt" ]
1758 # Extra file args passed manually need to be explicitly converted
1759 # to be relative to the build directory:
1762 rebase_path("//mything/data/input.dat", root_build_dir),
1764 rebase_path("relative_path.txt", root_build_dir)
1765 ] + rebase_path(sources, root_build_dir)
1770 ## **set_default_toolchain**: Sets the default toolchain name.
1773 set_default_toolchain(toolchain_label)
1775 The given label should identify a toolchain definition (see
1776 "help toolchain"). This toolchain will be used for all targets
1777 unless otherwise specified.
1779 This function is only valid to call during the processing of the build
1780 configuration file. Since the build configuration file is processed
1781 separately for each toolchain, this function will be a no-op when
1782 called under any non-default toolchains.
1784 For example, the default toolchain should be appropriate for the
1785 current environment. If the current environment is 32-bit and
1786 somebody references a target with a 64-bit toolchain, we wouldn't
1787 want processing of the build config file for the 64-bit toolchain to
1788 reset the default toolchain to 64-bit, we want to keep it 32-bits.
1803 set_default_toolchain("//build/config/win:vs32")
1806 ## **set_defaults**: Set default values for a target type.
1809 set_defaults(<target_type_name>) { <values...> }
1811 Sets the default values for a given target type. Whenever
1812 target_type_name is seen in the future, the values specified in
1813 set_default's block will be copied into the current scope.
1815 When the target type is used, the variable copying is very strict.
1816 If a variable with that name is already in scope, the build will fail
1819 set_defaults can be used for built-in target types ("executable",
1820 "shared_library", etc.) and custom ones defined via the "template"
1827 set_defaults("static_library") {
1828 configs = [ "//tools/mything:settings" ]
1831 static_library("mylib")
1832 # The configs will be auto-populated as above. You can remove it if
1833 # you don't want the default for a particular default:
1834 configs -= "//tools/mything:settings"
1839 ## **set_sources_assignment_filter**: Set a pattern to filter source files.
1842 The sources assignment filter is a list of patterns that remove files
1843 from the list implicitly whenever the "sources" variable is
1844 assigned to. This is intended to be used to globally filter out files
1845 with platform-specific naming schemes when they don't apply, for
1846 example, you may want to filter out all "*_win.cc" files on non-
1849 Typically this will be called once in the master build config script
1850 to set up the filter for the current platform. Subsequent calls will
1851 overwrite the previous values.
1853 If you want to bypass the filter and add a file even if it might
1854 be filtered out, call set_sources_assignment_filter([]) to clear the
1855 list of filters. This will apply until the current scope exits
1859 ### **How to use patterns**
1862 File patterns are VERY limited regular expressions. They must match
1863 the entire input string to be counted as a match. In regular
1864 expression parlance, there is an implicit "^...$" surrounding your
1865 input. If you want to match a substring, you need to use wildcards at
1866 the beginning and end.
1868 There are only two special tokens understood by the pattern matcher.
1869 Everything else is a literal.
1871 * Matches zero or more of any character. It does not depend on the
1872 preceding character (in regular expression parlance it is
1873 equivalent to ".*").
1875 \b Matches a path boundary. This will match the beginning or end of
1876 a string, or a slash.
1880 ### **Pattern examples**
1884 Matches a string containing "asdf" anywhere.
1887 Matches only the exact string "asdf".
1890 Matches strings ending in the literal ".cc".
1893 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
1897 ### **Sources assignment example**
1900 # Filter out all _win files.
1901 set_sources_assignment_filter([ "*_win.cc", "*_win.h" ])
1902 sources = [ "a.cc", "b_win.cc" ]
1904 # Will print [ "a.cc" ]. b_win one was filtered out.
1908 ## **shared_library**: Declare a shared library target.
1911 A shared library will be specified on the linker line for targets
1912 listing the shared library in its "deps". If you don't want this
1913 (say you dynamically load the library at runtime), then you should
1914 depend on the shared library via "data_deps" instead.
1921 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1922 defines, include_dirs, ldflags, lib_dirs, libs
1923 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1924 Dependent configs: all_dependent_configs, public_configs
1925 General: check_includes, configs, data, inputs, output_name,
1926 output_extension, public, sources, testonly, visibility
1930 ## **source_set**: Declare a source set target.
1933 A source set is a collection of sources that get compiled, but are not
1934 linked to produce any kind of library. Instead, the resulting object
1935 files are implicitly added to the linker line of all targets that
1936 depend on the source set.
1938 In most cases, a source set will behave like a static library, except
1939 no actual library file will be produced. This will make the build go
1940 a little faster by skipping creation of a large static library, while
1941 maintaining the organizational benefits of focused build targets.
1943 The main difference between a source set and a static library is
1944 around handling of exported symbols. Most linkers assume declaring
1945 a function exported means exported from the static library. The linker
1946 can then do dead code elimination to delete code not reachable from
1949 A source set will not do this code elimination since there is no link
1950 step. This allows you to link many sources sets into a shared library
1951 and have the "exported symbol" notation indicate "export from the
1952 final shared library and not from the intermediate targets." There is
1953 no way to express this concept when linking multiple static libraries
1954 into a shared library.
1961 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1962 defines, include_dirs, ldflags, lib_dirs, libs
1963 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1964 Dependent configs: all_dependent_configs, public_configs
1965 General: check_includes, configs, data, inputs, output_name,
1966 output_extension, public, sources, testonly, visibility
1970 ## **static_library**: Declare a static library target.
1973 Make a ".a" / ".lib" file.
1975 If you only need the static library for intermediate results in the
1976 build, you should consider a source_set instead since it will skip
1977 the (potentially slow) step of creating the intermediate library file.
1984 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1985 defines, include_dirs, ldflags, lib_dirs, libs
1986 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1987 Dependent configs: all_dependent_configs, public_configs
1988 General: check_includes, configs, data, inputs, output_name,
1989 output_extension, public, sources, testonly, visibility
1993 ## **template**: Define a template rule.
1996 A template defines a custom name that acts like a function. It
1997 provides a way to add to the built-in target types.
1999 The template() function is used to declare a template. To invoke the
2000 template, just use the name of the template like any other target
2003 Often you will want to declare your template in a special file that
2004 other files will import (see "gn help import") so your template
2005 rule can be shared across build files.
2009 ### **More details**:
2012 When you call template() it creates a closure around all variables
2013 currently in scope with the code in the template block. When the
2014 template is invoked, the closure will be executed.
2016 When the template is invoked, the code in the caller is executed and
2017 passed to the template code as an implicit "invoker" variable. The
2018 template uses this to read state out of the invoking code.
2020 One thing explicitly excluded from the closure is the "current
2021 directory" against which relative file names are resolved. The
2022 current directory will be that of the invoking code, since typically
2023 that code specifies the file names. This means all files internal
2024 to the template should use absolute names.
2028 ### **Target naming**:
2031 Your template should almost always define a built-in target with the
2032 name the template invoker specified. For example, if you have an IDL
2033 template and somebody does:
2035 you will normally want this to expand to something defining a
2036 source_set or static_library named "foo" (among other things you may
2037 need). This way, when another target specifies a dependency on
2038 "foo", the static_library or source_set will be linked.
2040 It is also important that any other targets your template expands to
2041 have globally unique names, or you will get collisions.
2043 Access the invoking name in your template via the implicit
2044 "target_name" variable. This should also be the basis of how other
2045 targets that a template expands to to ensure uniquness.
2047 A typical example would be a template that defines an action to
2048 generate some source files, and a source_set to compile that source.
2049 Your template would name the source_set "target_name" because
2050 that's what you want external targets to depend on to link your code.
2051 And you would name the action something like "${target_name}_action"
2052 to make it unique. The source set would have a dependency on the
2053 action to make it run.
2057 ### **Example of defining a template**:
2060 template("my_idl") {
2061 # Be nice and help callers debug problems by checking that the
2062 # variables the template requires are defined. This gives a nice
2063 # message rather than giving the user an error about an
2064 # undefined variable in the file defining the template
2066 # You can also use defined() to give default values to variables
2067 # unspecified by the invoker.
2068 assert(defined(invoker.sources),
2069 "Need sources in $target_name listing the idl files.")
2071 # Name of the intermediate target that does the code gen. This must
2072 # incorporate the target name so it's unique across template
2074 code_gen_target_name = target_name + "_code_gen"
2076 # Intermediate target to convert IDL to C source. Note that the name
2077 # is based on the name the invoker of the template specified. This
2078 # way, each time the template is invoked we get a unique
2079 # intermediate action name (since all target names are in the global
2081 action_foreach(code_gen_target_name) {
2082 # Access the scope defined by the invoker via the implicit
2083 # "invoker" variable.
2084 sources = invoker.sources
2086 # Note that we need an absolute path for our script file name.
2087 # The current directory when executing this code will be that of
2088 # the invoker (this is why we can use the "sources" directly
2089 # above without having to rebase all of the paths). But if we need
2090 # to reference a script relative to the template file, we'll need
2091 # to use an absolute path instead.
2092 script = "//tools/idl/idl_code_generator.py"
2094 # Tell GN how to expand output names given the sources.
2095 # See "gn help source_expansion" for more.
2096 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
2097 "$target_gen_dir/{{source_name_part}}.h" ]
2100 # Name the source set the same as the template invocation so
2101 # instancing this template produces something that other targets
2102 # can link to in their deps.
2103 source_set(target_name) {
2104 # Generates the list of sources, we get these from the
2105 # action_foreach above.
2106 sources = get_target_outputs(":$code_gen_target_name")
2108 # This target depends on the files produced by the above code gen
2110 deps = [ ":$code_gen_target_name" ]
2116 ### **Example of invoking the resulting template**:
2119 # This calls the template code above, defining target_name to be
2120 # "foo_idl_files" and "invoker" to be the set of stuff defined in
2121 # the curly brackets.
2122 my_idl("foo_idl_files") {
2123 # Goes into the template as "invoker.sources".
2124 sources = [ "foo.idl", "bar.idl" ]
2127 # Here is a target that depends on our template.
2128 executable("my_exe") {
2129 # Depend on the name we gave the template call above. Internally,
2130 # this will produce a dependency from executable to the source_set
2131 # inside the template (since it has this name), which will in turn
2132 # depend on the code gen action.
2133 deps = [ ":foo_idl_files" ]
2138 ## **tool**: Specify arguments to a toolchain tool.
2155 "objc": Objective C compiler
2156 "objcxx": Objective C++ compiler
2157 "rc": Resource compiler (Windows .rc files)
2161 "alink": Linker for static libraries (archives)
2162 "solink": Linker for shared libraries
2163 "link": Linker for executables
2166 "stamp": Tool for creating stamp files
2167 "copy": Tool to copy files.
2171 ### **Tool variables**
2174 command [string with substitutions]
2175 Valid for: all tools (required)
2179 default_output_extension [string]
2180 Valid for: linker tools
2182 Extension for the main output of a linkable tool. It includes
2183 the leading dot. This will be the default value for the
2184 {{output_extension}} expansion (discussed below) but will be
2185 overridden by by the "output extension" variable in a target,
2186 if one is specified. Empty string means no extension.
2188 GN doesn't actually do anything with this extension other than
2189 pass it along, potentially with target-specific overrides. One
2190 would typically use the {{output_extension}} value in the
2191 "outputs" to read this value.
2193 Example: default_output_extension = ".exe"
2196 Valid for: compiler tools (optional)
2198 If the tool can write ".d" files, this specifies the name of
2199 the resulting file. These files are used to list header file
2200 dependencies (or other implicit input dependencies) that are
2201 discovered at build time. See also "depsformat".
2203 Example: depfile = "{{output}}.d"
2206 Valid for: compiler tools (when depfile is specified)
2208 Format for the deps outputs. This is either "gcc" or "msvc".
2209 See the ninja documentation for "deps" for more information.
2211 Example: depsformat = "gcc"
2213 description [string with substitutions, optional]
2214 Valid for: all tools
2216 What to print when the command is run.
2218 Example: description = "Compiling {{source}}"
2220 lib_switch [string, optional, link tools only]
2221 lib_dir_switch [string, optional, link tools only]
2222 Valid for: Linker tools except "alink"
2224 These strings will be prepended to the libraries and library
2225 search directories, respectively, because linkers differ on how
2226 specify them. If you specified:
2228 lib_dir_switch = "-L"
2229 then the "{{libs}}" expansion for [ "freetype", "expat"]
2230 would be "-lfreetype -lexpat".
2232 outputs [list of strings with substitutions]
2233 Valid for: Linker and compiler tools (required)
2235 An array of names for the output files the tool produces. These
2236 are relative to the build output directory. There must always be
2237 at least one output file. There can be more than one output (a
2238 linker might produce a library and an import library, for
2241 This array just declares to GN what files the tool will
2242 produce. It is your responsibility to specify the tool command
2243 that actually produces these files.
2245 If you specify more than one output for shared library links,
2246 you should consider setting link_output and depend_output.
2247 Otherwise, the first entry in the outputs list should always be
2248 the main output which will be linked to.
2250 Example for a compiler tool that produces .obj files:
2252 "{{source_out_dir}}/{{source_name_part}}.obj"
2255 Example for a linker tool that produces a .dll and a .lib. The
2256 use of {{output_extension}} rather than hardcoding ".dll"
2257 allows the extension of the library to be overridden on a
2258 target-by-target basis, but in this example, it always
2259 produces a ".lib" import library:
2261 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2262 "{{root_out_dir}}/{{target_output_name}}.lib",
2265 link_output [string with substitutions]
2266 depend_output [string with substitutions]
2267 Valid for: "solink" only (optional)
2269 These two files specify whch of the outputs from the solink
2270 tool should be used for linking and dependency tracking. These
2271 should match entries in the "outputs". If unspecified, the
2272 first item in the "outputs" array will be used for both. See
2273 "Separate linking and dependencies for shared libraries"
2276 On Windows, where the tools produce a .dll shared library and
2277 a .lib import library, you will want both of these to be the
2278 import library. On Linux, if you're not doing the separate
2279 linking/dependency optimization, both of these should be the
2282 output_prefix [string]
2283 Valid for: Linker tools (optional)
2285 Prefix to use for the output name. Defaults to empty. This
2286 prefix will be prepended to the name of the target (or the
2287 output_name if one is manually specified for it) if the prefix
2288 is not already there. The result will show up in the
2289 {{output_name}} substitution pattern.
2291 This is typically used to prepend "lib" to libraries on
2293 output_prefix = "lib"
2296 Valid for: all tools (optional, defaults to false)
2298 Requests that Ninja check the file timestamp after this tool has
2299 run to determine if anything changed. Set this if your tool has
2300 the ability to skip writing output if the output file has not
2303 Normally, Ninja will assume that when a tool runs the output
2304 be new and downstream dependents must be rebuild. When this is
2305 set to trye, Ninja can skip rebuilding downstream dependents for
2306 input changes that don't actually affect the output.
2311 rspfile [string with substitutions]
2312 Valid for: all tools (optional)
2314 Name of the response file. If empty, no response file will be
2315 used. See "rspfile_content".
2317 rspfile_content [string with substitutions]
2318 Valid for: all tools (required when "rspfile" is specified)
2320 The contents to be written to the response file. This may
2321 include all or part of the command to send to the tool which
2322 allows you to get around OS command-line length limits.
2324 This example adds the inputs and libraries to a response file,
2325 but passes the linker flags directly on the command line:
2327 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
2328 rspfile = "{{output}}.rsp"
2329 rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
2334 ### **Expansions for tool variables**
2336 All paths are relative to the root build directory, which is the
2337 current directory for running all tools. These expansions are
2338 available to all tools:
2341 The label of the current target. This is typically used in the
2342 "description" field for link tools. The toolchain will be
2343 omitted from the label for targets in the default toolchain, and
2344 will be included for targets in other toolchains.
2347 The relative path and name of the output(s) of the current
2348 build step. If there is more than one output, this will expand
2349 to a list of all of them.
2350 Example: "out/base/my_file.o"
2354 The directory of the generated file and output directories,
2355 respectively, for the current target. There is no trailing
2357 Example: "out/base/test"
2359 {{target_output_name}}
2360 The short name of the current target with no path information,
2361 or the value of the "output_name" variable if one is specified
2362 in the target. This will include the "output_prefix" if any.
2363 Example: "libfoo" for the target named "foo" and an
2364 output prefix for the linker tool of "lib".
2366 Compiler tools have the notion of a single input and a single output,
2367 along with a set of compiler-specific flags. The following expansions
2377 Strings correspond that to the processed flags/defines/include
2378 directories specified for the target.
2379 Example: "--enable-foo --enable-bar"
2381 Defines will be prefixed by "-D" and include directories will
2382 be prefixed by "-I" (these work with Posix tools as well as
2386 The relative path and name of the current input file.
2387 Example: "../../base/my_file.cc"
2389 {{source_file_part}}
2390 The file part of the source including the extension (with no
2391 directory information).
2394 {{source_name_part}}
2395 The filename part of the source file with no directory or
2401 The directory in the generated file and output directories,
2402 respectively, for the current input file. If the source file
2403 is in the same directory as the target is declared in, they will
2404 will be the same as the "target" versions above.
2405 Example: "gen/base/test"
2407 Linker tools have multiple inputs and (potentially) multiple outputs
2408 The following expansions are available:
2412 Expands to the inputs to the link step. This will be a list of
2413 object files and static libraries.
2414 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
2416 The "_newline" version will separate the input files with
2417 newlines instead of spaces. This is useful in response files:
2418 some linkers can take a "-filelist" flag which expects newline
2419 separated files, and some Microsoft tools have a fixed-sized
2420 buffer for parsing each line of a response file.
2423 Expands to the processed set of ldflags and library search paths
2424 specified for the target.
2425 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
2428 Expands to the list of system libraries to link to. Each will
2429 be prefixed by the "lib_prefix".
2431 As a special case to support Mac, libraries with names ending in
2432 ".framework" will be added to the {{libs}} with "-framework"
2433 preceeding it, and the lib prefix will be ignored.
2435 Example: "-lfoo -lbar"
2437 {{output_extension}}
2438 The value of the "output_extension" variable in the target,
2439 or the value of the "default_output_extension" value in the
2440 tool if the target does not specify an output extension.
2444 Extra libraries from shared library dependencide not specified
2445 in the {{inputs}}. This is the list of link_output files from
2446 shared libraries (if the solink tool specifies a "link_output"
2447 variable separate from the "depend_output").
2449 These should generally be treated the same as libs by your tool.
2450 Example: "libfoo.so libbar.so"
2452 The copy tool allows the common compiler/linker substitutions, plus
2453 {{source}} which is the source of the copy. The stamp tool allows
2454 only the common tool substitutions.
2458 ### **Separate linking and dependencies for shared libraries**
2461 Shared libraries are special in that not all changes to them require
2462 that dependent targets be re-linked. If the shared library is changed
2463 but no imports or exports are different, dependent code needn't be
2464 relinked, which can speed up the build.
2466 If your link step can output a list of exports from a shared library
2467 and writes the file only if the new one is different, the timestamp of
2468 this file can be used for triggering re-links, while the actual shared
2469 library would be used for linking.
2471 You will need to specify
2473 in the linker tool to make this work, so Ninja will detect if the
2474 timestamp of the dependency file has changed after linking (otherwise
2475 it will always assume that running a command updates the output):
2480 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2481 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC",
2484 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
2486 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC"
2495 toolchain("my_toolchain") {
2496 # Put these at the top to apply to all tools below.
2498 lib_dir_prefix = "-L"
2501 command = "gcc {{source}} -o {{output}}"
2502 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2503 description = "GCC {{source}}"
2506 command = "g++ {{source}} -o {{output}}"
2507 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2508 description = "G++ {{source}}"
2514 ## **toolchain**: Defines a toolchain.
2517 A toolchain is a set of commands and build flags used to compile the
2518 source code. You can have more than one toolchain in use at once in
2523 ### **Functions and variables**
2527 The tool() function call specifies the commands commands to run for
2528 a given step. See "gn help tool".
2531 List of arguments to pass to the toolchain when invoking this
2532 toolchain. This applies only to non-default toolchains. See
2533 "gn help toolchain_args" for more.
2536 Dependencies of this toolchain. These dependencies will be resolved
2537 before any target in the toolchain is compiled. To avoid circular
2538 dependencies these must be targets defined in another toolchain.
2540 This is expressed as a list of targets, and generally these targets
2541 will always specify a toolchain:
2542 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
2544 This concept is somewhat inefficient to express in Ninja (it
2545 requires a lot of duplicate of rules) so should only be used when
2546 absolutely necessary.
2549 In integer expressing the number of links that Ninja will perform in
2550 parallel. GN will create a pool for shared library and executable
2551 link steps with this many processes. Since linking is memory- and
2552 I/O-intensive, projects with many large targets may want to limit
2553 the number of parallel steps to avoid overloading the computer.
2554 Since creating static libraries is generally not as intensive
2555 there is no limit to "alink" steps.
2557 Defaults to 0 which Ninja interprets as "no limit".
2559 The value used will be the one from the default toolchain of the
2564 ### **Invoking targets in toolchains**:
2567 By default, when a target depends on another, there is an implicit
2568 toolchain label that is inherited, so the dependee has the same one
2571 You can override this and refer to any other toolchain by explicitly
2572 labeling the toolchain to use. For example:
2573 data_deps = [ "//plugins:mine(//toolchains:plugin_toolchain)" ]
2574 The string "//build/toolchains:plugin_toolchain" is a label that
2575 identifies the toolchain declaration for compiling the sources.
2577 To load a file in an alternate toolchain, GN does the following:
2579 1. Loads the file with the toolchain definition in it (as determined
2580 by the toolchain label).
2581 2. Re-runs the master build configuration file, applying the
2582 arguments specified by the toolchain_args section of the toolchain
2583 definition (see "gn help toolchain_args").
2584 3. Loads the destination build file in the context of the
2585 configuration file in the previous step.
2591 toolchain("plugin_toolchain") {
2592 concurrent_links = 8
2595 command = "gcc {{source}}"
2608 ## **toolchain_args**: Set build arguments for toolchain build setup.
2611 Used inside a toolchain definition to pass arguments to an alternate
2612 toolchain's invocation of the build.
2614 When you specify a target using an alternate toolchain, the master
2615 build configuration file is re-interpreted in the context of that
2616 toolchain (see "gn help toolchain"). The toolchain_args function
2617 allows you to control the arguments passed into this alternate
2618 invocation of the build.
2620 Any default system arguments or arguments passed in on the command-
2621 line will also be passed to the alternate invocation unless explicitly
2622 overridden by toolchain_args.
2624 The toolchain_args will be ignored when the toolchain being defined
2625 is the default. In this case, it's expected you want the default
2628 See also "gn help buildargs" for an overview of these arguments.
2634 toolchain("my_weird_toolchain") {
2637 # Override the system values for a generic Posix system.
2641 # Pass this new value for specific setup for my toolchain.
2642 is_my_weird_system = true
2648 ## **write_file**: Write a file to disk.
2651 write_file(filename, data)
2653 If data is a list, the list will be written one-item-per-line with no
2654 quoting or brackets.
2656 If the file exists and the contents are identical to that being
2657 written, the file will not be updated. This will prevent unnecessary
2658 rebuilds of targets that depend on this file.
2660 TODO(brettw) we probably need an optional third argument to control
2669 Filename to write. This must be within the output directory.
2672 The list or string to write.
2676 ## **current_cpu**: The processor architecture of the current toolchain.
2679 The build configuration usually sets this value based on the value
2680 of "host_cpu" (see "gn help host_cpu") and then threads
2681 this through the toolchain definitions to ensure that it always
2682 reflects the appropriate value.
2684 This value is not used internally by GN for any purpose. It is
2685 set it to the empty string ("") by default but is declared so
2686 that it can be overridden on the command line if so desired.
2688 See "gn help target_cpu" for a list of common values returned.
2692 ## **current_os**: The operating system of the current toolchain.
2695 The build configuration usually sets this value based on the value
2696 of "target_os" (see "gn help target_os"), and then threads this
2697 through the toolchain definitions to ensure that it always reflects
2698 the appropriate value.
2700 This value is not used internally by GN for any purpose. It is
2701 set it to the empty string ("") by default but is declared so
2702 that it can be overridden on the command line if so desired.
2704 See "gn help target_os" for a list of common values returned.
2708 ## **current_toolchain**: Label of the current toolchain.
2711 A fully-qualified label representing the current toolchain. You can
2712 use this to make toolchain-related decisions in the build. See also
2713 "default_toolchain".
2720 if (current_toolchain == "//build:64_bit_toolchain") {
2721 executable("output_thats_64_bit_only") {
2726 ## **default_toolchain**: [string] Label of the default toolchain.
2729 A fully-qualified label representing the default toolchain, which may
2730 not necessarily be the current one (see "current_toolchain").
2734 ## **host_cpu**: The processor architecture that GN is running on.
2737 This is value is exposed so that cross-compile toolchains can
2738 access the host architecture when needed.
2740 The value should generally be considered read-only, but it can be
2741 overriden in order to handle unusual cases where there might
2742 be multiple plausible values for the host architecture (e.g., if
2743 you can do either 32-bit or 64-bit builds). The value is not used
2744 internally by GN for any purpose.
2748 ### **Some possible values**:
2755 ## **host_os**: [string] The operating system that GN is running on.
2758 This value is exposed so that cross-compiles can access the host
2759 build system's settings.
2761 This value should generally be treated as read-only. It, however,
2762 is not used internally by GN for any purpose.
2766 ### **Some possible values**:
2774 ## **python_path**: Absolute path of Python.
2777 Normally used in toolchain definitions if running some command
2778 requires Python. You will normally not need this when invoking scripts
2779 since GN automatically finds it for you.
2783 ## **root_build_dir**: [string] Directory where build commands are run.
2786 This is the root build output directory which will be the current
2787 directory when executing all compilers and scripts.
2789 Most often this is used with rebase_path (see "gn help rebase_path")
2790 to convert arguments to be relative to a script's current directory.
2794 ## **root_gen_dir**: Directory for the toolchain's generated files.
2797 Absolute path to the root of the generated output directory tree for
2798 the current toolchain. An example would be "//out/Debug/gen" for the
2799 default toolchain, or "//out/Debug/arm/gen" for the "arm"
2802 This is primarily useful for setting up include paths for generated
2803 files. If you are passing this to a script, you will want to pass it
2804 through rebase_path() (see "gn help rebase_path") to convert it
2805 to be relative to the build directory.
2807 See also "target_gen_dir" which is usually a better location for
2808 generated files. It will be inside the root generated dir.
2812 ## **root_out_dir**: [string] Root directory for toolchain output files.
2815 Absolute path to the root of the output directory tree for the current
2816 toolchain. It will not have a trailing slash.
2818 For the default toolchain this will be the same as the root_build_dir.
2819 An example would be "//out/Debug" for the default toolchain, or
2820 "//out/Debug/arm" for the "arm" toolchain.
2822 This is primarily useful for setting up script calls. If you are
2823 passing this to a script, you will want to pass it through
2824 rebase_path() (see "gn help rebase_path") to convert it
2825 to be relative to the build directory.
2827 See also "target_out_dir" which is usually a better location for
2828 output files. It will be inside the root output dir.
2835 action("myscript") {
2836 # Pass the output dir to the script.
2837 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
2842 ## **target_cpu**: The desired cpu architecture for the build.
2845 This value should be used to indicate the desired architecture for
2846 the primary objects of the build. It will match the cpu architecture
2847 of the default toolchain.
2849 In many cases, this is the same as "host_cpu", but in the case
2850 of cross-compiles, this can be set to something different. This
2851 value is different from "current_cpu" in that it can be referenced
2852 from inside any toolchain. This value can also be ignored if it is
2853 not needed or meaningful for a project.
2855 This value is not used internally by GN for any purpose, so it
2856 may be set to whatever value is needed for the build.
2857 GN defaults this value to the empty string ("") and the
2858 configuration files should set it to an appropriate value
2859 (e.g., setting it to the value of "host_cpu") if it is not
2860 overridden on the command line or in the args.gn file.
2862 Where practical, use one of the following list of common values:
2866 ### **Possible values**:
2876 ## **target_gen_dir**: Directory for a target's generated files.
2879 Absolute path to the target's generated file directory. This will be
2880 the "root_gen_dir" followed by the relative path to the current
2881 build file. If your file is in "//tools/doom_melon" then
2882 target_gen_dir would be "//out/Debug/gen/tools/doom_melon". It will
2883 not have a trailing slash.
2885 This is primarily useful for setting up include paths for generated
2886 files. If you are passing this to a script, you will want to pass it
2887 through rebase_path() (see "gn help rebase_path") to convert it
2888 to be relative to the build directory.
2890 See also "gn help root_gen_dir".
2897 action("myscript") {
2898 # Pass the generated output dir to the script.
2899 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
2904 ## **target_os**: The desired operating system for the build.
2907 This value should be used to indicate the desired operating system
2908 for the primary object(s) of the build. It will match the OS of
2909 the default toolchain.
2911 In many cases, this is the same as "host_os", but in the case of
2912 cross-compiles, it may be different. This variable differs from
2913 "current_os" in that it can be referenced from inside any
2914 toolchain and will always return the initial value.
2916 This should be set to the most specific value possible. So,
2917 "android" or "chromeos" should be used instead of "linux"
2918 where applicable, even though Android and ChromeOS are both Linux
2919 variants. This can mean that one needs to write
2921 if (target_os == "android" || target_os == "linux") {
2927 This value is not used internally by GN for any purpose, so it
2928 may be set to whatever value is needed for the build.
2929 GN defaults this value to the empty string ("") and the
2930 configuration files should set it to an appropriate value
2931 (e.g., setting it to the value of "host_os") if it is not
2932 set via the command line or in the args.gn file.
2934 Where practical, use one of the following list of common values:
2938 ### **Possible values**:
2950 ## **target_out_dir**: [string] Directory for target output files.
2953 Absolute path to the target's generated file directory. If your
2954 current target is in "//tools/doom_melon" then this value might be
2955 "//out/Debug/obj/tools/doom_melon". It will not have a trailing
2958 This is primarily useful for setting up arguments for calling
2959 scripts. If you are passing this to a script, you will want to pass it
2960 through rebase_path() (see "gn help rebase_path") to convert it
2961 to be relative to the build directory.
2963 See also "gn help root_out_dir".
2970 action("myscript") {
2971 # Pass the output dir to the script.
2972 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
2977 ## **all_dependent_configs**: Configs to be forced on dependents.
2980 A list of config labels.
2982 All targets depending on this one, and recursively, all targets
2983 depending on those, will have the configs listed in this variable
2984 added to them. These configs will also apply to the current target.
2986 This addition happens in a second phase once a target and all of its
2987 dependencies have been resolved. Therefore, a target will not see
2988 these force-added configs in their "configs" variable while the
2989 script is running, and then can not be removed. As a result, this
2990 capability should generally only be used to add defines and include
2991 directories necessary to compile a target's headers.
2993 See also "public_configs".
2997 ### **Ordering of flags and values**:
3000 1. Those set on the current target (not in a config).
3001 2. Those set on the "configs" on the target in order that the
3002 configs appear in the list.
3003 3. Those set on the "all_dependent_configs" on the target in order
3004 that the configs appear in the list.
3005 4. Those set on the "public_configs" on the target in order that
3006 those configs appear in the list.
3007 5. all_dependent_configs pulled from dependencies, in the order of
3008 the "deps" list. This is done recursively. If a config appears
3009 more than once, only the first occurance will be used.
3010 6. public_configs pulled from dependencies, in the order of the
3011 "deps" list. If a dependency is public, they will be applied
3016 ## **allow_circular_includes_from**: Permit includes from deps.
3019 A list of target labels. Must be a subset of the target's "deps".
3020 These targets will be permitted to include headers from the current
3021 target despite the dependency going in the opposite direction.
3025 ### **Tedious exposition**
3028 Normally, for a file in target A to include a file from target B,
3029 A must list B as a dependency. This invariant is enforced by the
3030 "gn check" command (and the --check flag to "gn gen").
3032 Sometimes, two targets might be the same unit for linking purposes
3033 (two source sets or static libraries that would always be linked
3034 together in a final executable or shared library). In this case,
3035 you want A to be able to include B's headers, and B to include A's
3038 This list, if specified, lists which of the dependencies of the
3039 current target can include header files from the current target.
3040 That is, if A depends on B, B can only include headers from A if it is
3041 in A's allow_circular_includes_from list.
3049 deps = [ ":b", ":c" ]
3050 allow_circular_includes_from = [ ":b" ]
3056 ## **args**: Arguments passed to an action.
3059 For action and action_foreach targets, args is the list of arguments
3060 to pass to the script. Typically you would use source expansion (see
3061 "gn help source_expansion") to insert the source file names.
3063 See also "gn help action" and "gn help action_foreach".
3067 ## **cflags***: Flags passed to the C compiler.
3072 "cflags" are passed to all invocations of the C, C++, Objective C,
3073 and Objective C++ compilers.
3075 To target one of these variants individually, use "cflags_c",
3076 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3077 These variant-specific versions will be appended to the "cflags".
3081 ### **Ordering of flags and values**:
3084 1. Those set on the current target (not in a config).
3085 2. Those set on the "configs" on the target in order that the
3086 configs appear in the list.
3087 3. Those set on the "all_dependent_configs" on the target in order
3088 that the configs appear in the list.
3089 4. Those set on the "public_configs" on the target in order that
3090 those configs appear in the list.
3091 5. all_dependent_configs pulled from dependencies, in the order of
3092 the "deps" list. This is done recursively. If a config appears
3093 more than once, only the first occurance will be used.
3094 6. public_configs pulled from dependencies, in the order of the
3095 "deps" list. If a dependency is public, they will be applied
3100 ## **cflags***: Flags passed to the C compiler.
3105 "cflags" are passed to all invocations of the C, C++, Objective C,
3106 and Objective C++ compilers.
3108 To target one of these variants individually, use "cflags_c",
3109 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3110 These variant-specific versions will be appended to the "cflags".
3114 ### **Ordering of flags and values**:
3117 1. Those set on the current target (not in a config).
3118 2. Those set on the "configs" on the target in order that the
3119 configs appear in the list.
3120 3. Those set on the "all_dependent_configs" on the target in order
3121 that the configs appear in the list.
3122 4. Those set on the "public_configs" on the target in order that
3123 those configs appear in the list.
3124 5. all_dependent_configs pulled from dependencies, in the order of
3125 the "deps" list. This is done recursively. If a config appears
3126 more than once, only the first occurance will be used.
3127 6. public_configs pulled from dependencies, in the order of the
3128 "deps" list. If a dependency is public, they will be applied
3133 ## **cflags***: Flags passed to the C compiler.
3138 "cflags" are passed to all invocations of the C, C++, Objective C,
3139 and Objective C++ compilers.
3141 To target one of these variants individually, use "cflags_c",
3142 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3143 These variant-specific versions will be appended to the "cflags".
3147 ### **Ordering of flags and values**:
3150 1. Those set on the current target (not in a config).
3151 2. Those set on the "configs" on the target in order that the
3152 configs appear in the list.
3153 3. Those set on the "all_dependent_configs" on the target in order
3154 that the configs appear in the list.
3155 4. Those set on the "public_configs" on the target in order that
3156 those configs appear in the list.
3157 5. all_dependent_configs pulled from dependencies, in the order of
3158 the "deps" list. This is done recursively. If a config appears
3159 more than once, only the first occurance will be used.
3160 6. public_configs pulled from dependencies, in the order of the
3161 "deps" list. If a dependency is public, they will be applied
3166 ## **cflags***: Flags passed to the C compiler.
3171 "cflags" are passed to all invocations of the C, C++, Objective C,
3172 and Objective C++ compilers.
3174 To target one of these variants individually, use "cflags_c",
3175 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3176 These variant-specific versions will be appended to the "cflags".
3180 ### **Ordering of flags and values**:
3183 1. Those set on the current target (not in a config).
3184 2. Those set on the "configs" on the target in order that the
3185 configs appear in the list.
3186 3. Those set on the "all_dependent_configs" on the target in order
3187 that the configs appear in the list.
3188 4. Those set on the "public_configs" on the target in order that
3189 those configs appear in the list.
3190 5. all_dependent_configs pulled from dependencies, in the order of
3191 the "deps" list. This is done recursively. If a config appears
3192 more than once, only the first occurance will be used.
3193 6. public_configs pulled from dependencies, in the order of the
3194 "deps" list. If a dependency is public, they will be applied
3199 ## **cflags***: Flags passed to the C compiler.
3204 "cflags" are passed to all invocations of the C, C++, Objective C,
3205 and Objective C++ compilers.
3207 To target one of these variants individually, use "cflags_c",
3208 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3209 These variant-specific versions will be appended to the "cflags".
3213 ### **Ordering of flags and values**:
3216 1. Those set on the current target (not in a config).
3217 2. Those set on the "configs" on the target in order that the
3218 configs appear in the list.
3219 3. Those set on the "all_dependent_configs" on the target in order
3220 that the configs appear in the list.
3221 4. Those set on the "public_configs" on the target in order that
3222 those configs appear in the list.
3223 5. all_dependent_configs pulled from dependencies, in the order of
3224 the "deps" list. This is done recursively. If a config appears
3225 more than once, only the first occurance will be used.
3226 6. public_configs pulled from dependencies, in the order of the
3227 "deps" list. If a dependency is public, they will be applied
3232 ## **check_includes**: [boolean] Controls whether a target's files are checked.
3235 When true (the default), the "gn check" command (as well as
3236 "gn gen" with the --check flag) will check this target's sources
3237 and headers for proper dependencies.
3239 When false, the files in this target will be skipped by default.
3240 This does not affect other targets that depend on the current target,
3241 it just skips checking the includes of the current target's files.
3248 source_set("busted_includes") {
3249 # This target's includes are messed up, exclude it from checking.
3250 check_includes = false
3256 ## **complete_static_lib**: [boolean] Links all deps into a static library.
3259 A static library normally doesn't include code from dependencies, but
3260 instead forwards the static libraries and source sets in its deps up
3261 the dependency chain until a linkable target (an executable or shared
3262 library) is reached. The final linkable target only links each static
3263 library once, even if it appears more than once in its dependency
3266 In some cases the static library might be the final desired output.
3267 For example, you may be producing a static library for distribution to
3268 third parties. In this case, the static library should include code
3269 for all dependencies in one complete package. Since GN does not unpack
3270 static libraries to forward their contents up the dependency chain,
3271 it is an error for complete static libraries to depend on other static
3279 static_library("foo") {
3280 complete_static_lib = true
3286 ## **configs**: Configs applying to this target.
3289 A list of config labels.
3291 The include_dirs, defines, etc. in each config are appended in the
3292 order they appear to the compile command for each file in the target.
3293 They will appear after the include_dirs, defines, etc. that the target
3296 The build configuration script will generally set up the default
3297 configs applying to a given target type (see "set_defaults").
3298 When a target is being defined, it can add to or remove from this
3303 ### **Ordering of flags and values**:
3306 1. Those set on the current target (not in a config).
3307 2. Those set on the "configs" on the target in order that the
3308 configs appear in the list.
3309 3. Those set on the "all_dependent_configs" on the target in order
3310 that the configs appear in the list.
3311 4. Those set on the "public_configs" on the target in order that
3312 those configs appear in the list.
3313 5. all_dependent_configs pulled from dependencies, in the order of
3314 the "deps" list. This is done recursively. If a config appears
3315 more than once, only the first occurance will be used.
3316 6. public_configs pulled from dependencies, in the order of the
3317 "deps" list. If a dependency is public, they will be applied
3324 static_library("foo") {
3325 configs -= "//build:no_rtti" # Don't use the default RTTI config.
3326 configs += ":mysettings" # Add some of our own settings.
3331 ## **data**: Runtime data file dependencies.
3334 Lists files or directories required to run the given target. These are
3335 typically data files or directories of data files. The paths are
3336 interpreted as being relative to the current build file. Since these
3337 are runtime dependencies, they do not affect which targets are built
3338 or when. To declare input files to a script, use "inputs".
3340 Appearing in the "data" section does not imply any special handling
3341 such as copying them to the output directory. This is just used for
3342 declaring runtime dependencies. Runtime dependencies can be queried
3343 using the "runtime_deps" category of "gn desc" or written during
3344 build generation via "--runtime-deps-list-file".
3346 GN doesn't require data files to exist at build-time. So actions that
3347 produce files that are in turn runtime dependencies can list those
3348 generated files both in the "outputs" list as well as the "data"
3351 By convention, directories are be listed with a trailing slash:
3352 data = [ "test/data/" ]
3353 However, no verification is done on these so GN doesn't enforce this.
3354 The paths are just rebased and passed along when requested.
3356 See "gn help runtime_deps" for how these are used.
3360 ## **data_deps**: Non-linked dependencies.
3363 A list of target labels.
3365 Specifies dependencies of a target that are not actually linked into
3366 the current target. Such dependencies will built and will be available
3369 This is normally used for things like plugins or helper programs that
3370 a target needs at runtime.
3372 See also "gn help deps" and "gn help data".
3380 data_deps = [ "//plugins:my_runtime_plugin" ]
3385 ## **defines**: C preprocessor defines.
3390 These strings will be passed to the C/C++ compiler as #defines. The
3391 strings may or may not include an "=" to assign a value.
3395 ### **Ordering of flags and values**:
3398 1. Those set on the current target (not in a config).
3399 2. Those set on the "configs" on the target in order that the
3400 configs appear in the list.
3401 3. Those set on the "all_dependent_configs" on the target in order
3402 that the configs appear in the list.
3403 4. Those set on the "public_configs" on the target in order that
3404 those configs appear in the list.
3405 5. all_dependent_configs pulled from dependencies, in the order of
3406 the "deps" list. This is done recursively. If a config appears
3407 more than once, only the first occurance will be used.
3408 6. public_configs pulled from dependencies, in the order of the
3409 "deps" list. If a dependency is public, they will be applied
3416 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
3420 ## **depfile**: [string] File name for input dependencies for actions.
3423 If nonempty, this string specifies that the current action or
3424 action_foreach target will generate the given ".d" file containing
3425 the dependencies of the input. Empty or unset means that the script
3426 doesn't generate the files.
3428 The .d file should go in the target output directory. If you have more
3429 than one source file that the script is being run over, you can use
3430 the output file expansions described in "gn help action_foreach" to
3431 name the .d file according to the input.
3432 The format is that of a Makefile, and all of the paths should be
3433 relative to the root build directory.
3439 action_foreach("myscript_target") {
3440 script = "myscript.py"
3443 # Locate the depfile in the output directory named like the
3444 # inputs but with a ".d" appended.
3445 depfile = "$relative_target_output_dir/{{source_name}}.d"
3447 # Say our script uses "-o <d file>" to indicate the depfile.
3448 args = [ "{{source}}", "-o", depfile ]
3453 ## **deps**: Private linked dependencies.
3456 A list of target labels.
3458 Specifies private dependencies of a target. Shared and dynamic
3459 libraries will be linked into the current target. Other target types
3460 that can't be linked (like actions and groups) listed in "deps" will
3461 be treated as "data_deps". Likewise, if the current target isn't
3462 linkable, then all deps will be treated as "data_deps".
3464 These dependencies are private in that it does not grant dependent
3465 targets the ability to include headers from the dependency, and direct
3466 dependent configs are not forwarded.
3468 See also "public_deps" and "data_deps".
3472 ## **forward_dependent_configs_from**
3475 A list of target labels.
3477 DEPRECATED. Use public_deps instead which will have the same effect.
3479 Exposes the public_configs from a private dependent target as
3480 public_configs of the current one. Each label in this list
3481 must also be in the deps.
3483 Generally you should use public_deps instead of this variable to
3484 express the concept of exposing a dependency as part of a target's
3485 public API. We're considering removing this variable.
3492 Sometimes you depend on a child library that exports some necessary
3493 configuration via public_configs. If your target in turn exposes the
3494 child library's headers in its public headers, it might mean that
3495 targets that depend on you won't work: they'll be seeing the child
3496 library's code but not the necessary configuration. This list
3497 specifies which of your deps' direct dependent configs to expose as
3505 If we use a given library "a" from our public headers:
3507 deps = [ ":a", ":b", ... ]
3508 forward_dependent_configs_from = [ ":a" ]
3510 This example makes a "transparent" target that forwards a dependency
3514 if (use_system_frob) {
3515 deps = ":system_frob"
3517 deps = "//third_party/fallback_frob"
3519 forward_dependent_configs_from = deps
3524 ## **include_dirs**: Additional include directories.
3527 A list of source directories.
3529 The directories in this list will be added to the include path for
3530 the files in the affected target.
3534 ### **Ordering of flags and values**:
3537 1. Those set on the current target (not in a config).
3538 2. Those set on the "configs" on the target in order that the
3539 configs appear in the list.
3540 3. Those set on the "all_dependent_configs" on the target in order
3541 that the configs appear in the list.
3542 4. Those set on the "public_configs" on the target in order that
3543 those configs appear in the list.
3544 5. all_dependent_configs pulled from dependencies, in the order of
3545 the "deps" list. This is done recursively. If a config appears
3546 more than once, only the first occurance will be used.
3547 6. public_configs pulled from dependencies, in the order of the
3548 "deps" list. If a dependency is public, they will be applied
3555 include_dirs = [ "src/include", "//third_party/foo" ]
3559 ## **inputs**: Additional compile-time dependencies.
3562 Inputs are compile-time dependencies of the current target. This means
3563 that all inputs must be available before compiling any of the sources
3564 or executing any actions.
3566 Inputs are typically only used for action and action_foreach targets.
3570 ### **Inputs for actions**
3573 For action and action_foreach targets, inputs should be the inputs to
3574 script that don't vary. These should be all .py files that the script
3575 uses via imports (the main script itself will be an implcit dependency
3576 of the action so need not be listed).
3578 For action targets, inputs should be the entire set of inputs the
3579 script needs. For action_foreach targets, inputs should be the set of
3580 dependencies that don't change. These will be applied to each script
3581 invocation over the sources.
3583 Note that another way to declare input dependencies from an action
3584 is to have the action write a depfile (see "gn help depfile"). This
3585 allows the script to dynamically write input dependencies, that might
3586 not be known until actually executing the script. This is more
3587 efficient than doing processing while running GN to determine the
3588 inputs, and is easier to keep in-sync than hardcoding the list.
3592 ### **Inputs for binary targets**
3595 Any input dependencies will be resolved before compiling any sources.
3596 Normally, all actions that a target depends on will be run before any
3597 files in a target are compiled. So if you depend on generated headers,
3598 you do not typically need to list them in the inputs section.
3605 action("myscript") {
3606 script = "domything.py"
3607 inputs = [ "input.data" ]
3612 ## **ldflags**: Flags passed to the linker.
3617 These flags are passed on the command-line to the linker and generally
3618 specify various linking options. Most targets will not need these and
3619 will use "libs" and "lib_dirs" instead.
3621 ldflags are NOT pushed to dependents, so applying ldflags to source
3622 sets or static libraries will be a no-op. If you want to apply ldflags
3623 to dependent targets, put them in a config and set it in the
3624 all_dependent_configs or public_configs.
3628 ## **lib_dirs**: Additional library directories.
3631 A list of directories.
3633 Specifies additional directories passed to the linker for searching
3634 for the required libraries. If an item is not an absolute path, it
3635 will be treated as being relative to the current build file.
3637 libs and lib_dirs work differently than other flags in two respects.
3638 First, then are inherited across static library boundaries until a
3639 shared library or executable target is reached. Second, they are
3640 uniquified so each one is only passed once (the first instance of it
3641 will be the one used).
3645 ### **Ordering of flags and values**:
3648 1. Those set on the current target (not in a config).
3649 2. Those set on the "configs" on the target in order that the
3650 configs appear in the list.
3651 3. Those set on the "all_dependent_configs" on the target in order
3652 that the configs appear in the list.
3653 4. Those set on the "public_configs" on the target in order that
3654 those configs appear in the list.
3655 5. all_dependent_configs pulled from dependencies, in the order of
3656 the "deps" list. This is done recursively. If a config appears
3657 more than once, only the first occurance will be used.
3658 6. public_configs pulled from dependencies, in the order of the
3659 "deps" list. If a dependency is public, they will be applied
3666 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
3670 ## **libs**: Additional libraries to link.
3675 These files will be passed to the linker, which will generally search
3676 the library include path. Unlike a normal list of files, they will be
3677 passed to the linker unmodified rather than being treated as file
3678 names relative to the current build file. Generally you would set
3679 the "lib_dirs" so your library is found. If you need to specify
3680 a path, you can use "rebase_path" to convert a path to be relative
3681 to the build directory.
3683 When constructing the linker command, the "lib_prefix" attribute of
3684 the linker tool in the current toolchain will be prepended to each
3685 library. So your BUILD file should not specify the switch prefix
3686 (like "-l"). On Mac, libraries ending in ".framework" will be
3687 special-cased: the switch "-framework" will be prepended instead of
3688 the lib_prefix, and the ".framework" suffix will be trimmed.
3690 libs and lib_dirs work differently than other flags in two respects.
3691 First, then are inherited across static library boundaries until a
3692 shared library or executable target is reached. Second, they are
3693 uniquified so each one is only passed once (the first instance of it
3694 will be the one used).
3698 ### **Ordering of flags and values**:
3701 1. Those set on the current target (not in a config).
3702 2. Those set on the "configs" on the target in order that the
3703 configs appear in the list.
3704 3. Those set on the "all_dependent_configs" on the target in order
3705 that the configs appear in the list.
3706 4. Those set on the "public_configs" on the target in order that
3707 those configs appear in the list.
3708 5. all_dependent_configs pulled from dependencies, in the order of
3709 the "deps" list. This is done recursively. If a config appears
3710 more than once, only the first occurance will be used.
3711 6. public_configs pulled from dependencies, in the order of the
3712 "deps" list. If a dependency is public, they will be applied
3720 libs = [ "ctl3d.lib" ]
3726 ## **output_extension**: Value to use for the output's file extension.
3729 Normally the file extension for a target is based on the target
3730 type and the operating system, but in rare cases you will need to
3731 override the name (for example to use "libfreetype.so.6" instead
3732 of libfreetype.so on Linux).
3735 ## **output_name**: Define a name for the output file other than the default.
3738 Normally the output name of a target will be based on the target name,
3739 so the target "//foo/bar:bar_unittests" will generate an output
3740 file such as "bar_unittests.exe" (using Windows as an example).
3742 Sometimes you will want an alternate name to avoid collisions or
3743 if the internal name isn't appropriate for public distribution.
3745 The output name should have no extension or prefixes, these will be
3746 added using the default system rules. For example, on Linux an output
3747 name of "foo" will produce a shared library "libfoo.so".
3749 This variable is valid for all binary output target types.
3755 static_library("doom_melon") {
3756 output_name = "fluffy_bunny"
3761 ## **outputs**: Output files for actions and copy targets.
3764 Outputs is valid for "copy", "action", and "action_foreach"
3765 target types and indicates the resulting files. The values may contain
3766 source expansions to generate the output names from the sources (see
3767 "gn help source_expansion").
3769 For copy targets, the outputs is the destination for the copied
3770 file(s). For actions, the outputs should be the list of files
3771 generated by the script.
3775 ## **public**: Declare public header files for a target.
3778 A list of files that other targets can include. These permissions are
3779 checked via the "check" command (see "gn help check").
3781 If no public files are declared, other targets (assuming they have
3782 visibility to depend on this target can include any file in the
3783 sources list. If this variable is defined on a target, dependent
3784 targets may only include files on this whitelist.
3786 Header file permissions are also subject to visibility. A target
3787 must be visible to another target to include any files from it at all
3788 and the public headers indicate which subset of those files are
3789 permitted. See "gn help visibility" for more.
3791 Public files are inherited through the dependency tree. So if there is
3792 a dependency A -> B -> C, then A can include C's public headers.
3793 However, the same is NOT true of visibility, so unless A is in C's
3794 visibility list, the include will be rejected.
3796 GN only knows about files declared in the "sources" and "public"
3797 sections of targets. If a file is included that is not known to the
3798 build, it will be allowed.
3804 These exact files are public:
3805 public = [ "foo.h", "bar.h" ]
3807 No files are public (no targets may include headers from this one):
3812 ## **public_configs**: Configs to be applied on dependents.
3815 A list of config labels.
3817 Targets directly depending on this one will have the configs listed in
3818 this variable added to them. These configs will also apply to the
3821 This addition happens in a second phase once a target and all of its
3822 dependencies have been resolved. Therefore, a target will not see
3823 these force-added configs in their "configs" variable while the
3824 script is running, and then can not be removed. As a result, this
3825 capability should generally only be used to add defines and include
3826 directories necessary to compile a target's headers.
3828 See also "all_dependent_configs".
3832 ### **Ordering of flags and values**:
3835 1. Those set on the current target (not in a config).
3836 2. Those set on the "configs" on the target in order that the
3837 configs appear in the list.
3838 3. Those set on the "all_dependent_configs" on the target in order
3839 that the configs appear in the list.
3840 4. Those set on the "public_configs" on the target in order that
3841 those configs appear in the list.
3842 5. all_dependent_configs pulled from dependencies, in the order of
3843 the "deps" list. This is done recursively. If a config appears
3844 more than once, only the first occurance will be used.
3845 6. public_configs pulled from dependencies, in the order of the
3846 "deps" list. If a dependency is public, they will be applied
3851 ## **public_deps**: Declare public dependencies.
3854 Public dependencies are like private dependencies ("deps") but
3855 additionally express that the current target exposes the listed deps
3856 as part of its public API.
3858 This has several ramifications:
3860 - public_configs that are part of the dependency are forwarded
3861 to direct dependents.
3863 - Public headers in the dependency are usable by dependents
3864 (includes do not require a direct dependency or visibility).
3866 - If the current target is a shared library, other shared libraries
3867 that it publicly depends on (directly or indirectly) are
3868 propagated up the dependency tree to dependents for linking.
3875 Say you have three targets: A -> B -> C. C's visibility may allow
3876 B to depend on it but not A. Normally, this would prevent A from
3877 including any headers from C, and C's public_configs would apply
3880 If B lists C in its public_deps instead of regular deps, A will now
3881 inherit C's public_configs and the ability to include C's public
3884 Generally if you are writing a target B and you include C's headers
3885 as part of B's public headers, or targets depending on B should
3886 consider B and C to be part of a unit, you should use public_deps
3894 # This target can include files from "c" but not from
3895 # "super_secret_implementation_details".
3900 shared_library("b") {
3901 deps = [ ":super_secret_implementation_details" ]
3902 public_deps = [ ":c" ]
3907 ## **script**: Script file for actions.
3910 An absolute or buildfile-relative file name of a Python script to run
3911 for a action and action_foreach targets (see "gn help action" and
3912 "gn help action_foreach").
3916 ## **sources**: Source files for a target
3919 A list of files relative to the current buildfile.
3923 ## **testonly**: Declares a target must only be used for testing.
3926 Boolean. Defaults to false.
3928 When a target is marked "testonly = true", it must only be depended
3929 on by other test-only targets. Otherwise, GN will issue an error
3930 that the depenedency is not allowed.
3932 This feature is intended to prevent accidentally shipping test code
3940 source_set("test_support") {
3947 ## **visibility**: A list of labels that can depend on a target.
3950 A list of labels and label patterns that define which targets can
3951 depend on the current one. These permissions are checked via the
3952 "check" command (see "gn help check").
3954 If visibility is not defined, it defaults to public ("*").
3956 If visibility is defined, only the targets with labels that match it
3957 can depend on the current target. The empty list means no targets
3958 can depend on the current target.
3960 Tip: Often you will want the same visibility for all targets in a
3961 BUILD file. In this case you can just put the definition at the top,
3962 outside of any target, and the targets will inherit that scope and see
3970 See "gn help label_pattern" for more details on what types of
3971 patterns are supported. If a toolchain is specified, only targets
3972 in that toolchain will be matched. If a toolchain is not specified on
3973 a pattern, targets in all toolchains will be matched.
3980 Only targets in the current buildfile ("private"):
3981 visibility = [ ":*" ]
3983 No targets (used for targets that should be leaf nodes):
3986 Any target ("public", the default):
3987 visibility = [ "*" ]
3989 All targets in the current directory and any subdirectory:
3990 visibility = [ "./*" ]
3992 Any target in "//bar/BUILD.gn":
3993 visibility = [ "//bar:*" ]
3995 Any target in "//bar/" or any subdirectory thereof:
3996 visibility = [ "//bar/*" ]
3998 Just these specific targets:
3999 visibility = [ ":mything", "//foo:something_else" ]
4001 Any target in the current directory and any subdirectory thereof, plus
4002 any targets in "//bar/" and any subdirectory thereof.
4003 visibility = [ "./*", "//bar/*" ]
4007 ## **Build Arguments Overview**
4010 Build arguments are variables passed in from outside of the build
4011 that build files can query to determine how the build works.
4015 ### **How build arguments are set**
4018 First, system default arguments are set based on the current system.
4019 The built-in arguments are:
4027 If specified, arguments from the --args command line flag are used. If
4028 that flag is not specified, args from previous builds in the build
4029 directory will be used (this is in the file args.gn in the build
4032 Last, for targets being compiled with a non-default toolchain, the
4033 toolchain overrides are applied. These are specified in the
4034 toolchain_args section of a toolchain definition. The use-case for
4035 this is that a toolchain may be building code for a different
4036 platform, and that it may want to always specify Posix, for example.
4037 See "gn help toolchain_args" for more.
4039 If you specify an override for a build argument that never appears in
4040 a "declare_args" call, a nonfatal error will be displayed.
4048 Create the directory out/FooBar and open an editor. You would type
4049 something like this into that file:
4050 enable_doom_melon=false
4053 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
4054 This will overwrite the build directory with the given arguments.
4055 (Note that the quotes inside the args command will usually need to
4056 be escaped for your shell to pass through strings values.)
4060 ### **How build arguments are used**
4063 If you want to use an argument, you use declare_args() and specify
4064 default values. These default values will apply if none of the steps
4065 listed in the "How build arguments are set" section above apply to
4066 the given argument, but the defaults will not override any of these.
4068 Often, the root build config file will declare global arguments that
4069 will be passed to all buildfiles. Individual build files can also
4070 specify arguments that apply only to those files. It is also useful
4071 to specify build args in an "import"-ed file if you want such
4072 arguments to apply to multiple buildfiles.
4079 When gn starts, it will search the current directory and parent
4080 directories for a file called ".gn". This indicates the source root.
4081 You can override this detection by using the --root command-line
4084 The .gn file in the source root will be executed. The syntax is the
4085 same as a buildfile, but with very limited build setup-specific
4088 If you specify --root, by default GN will look for the file .gn in
4089 that directory. If you want to specify a different file, you can
4090 additionally pass --dotfile:
4092 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
4099 buildconfig [required]
4100 Label of the build config file. This file will be used to set up
4101 the build file execution environment for each toolchain.
4103 check_targets [optional]
4104 A list of labels and label patterns that should be checked when
4105 running "gn check" or "gn gen --check". If unspecified, all
4106 targets will be checked. If it is the empty list, no targets will
4109 The format of this list is identical to that of "visibility"
4110 so see "gn help visibility" for examples.
4112 exec_script_whitelist [optional]
4113 A list of .gn/.gni files (not labels) that have permission to call
4114 the exec_script function. If this list is defined, calls to
4115 exec_script will be checked against this list and GN will fail if
4116 the current file isn't in the list.
4118 This is to allow the use of exec_script to be restricted since
4119 is easy to use inappropriately. Wildcards are not supported.
4120 Files in the secondary_source tree (if defined) should be
4121 referenced by ignoring the secondary tree and naming them as if
4122 they are in the main tree.
4124 If unspecified, the ability to call exec_script is unrestricted.
4127 exec_script_whitelist = [
4129 "//build/my_config.gni",
4133 Label of the root build target. The GN build will start by loading
4134 the build file containing this target name. This defaults to
4135 "//:" which will cause the file //BUILD.gn to be loaded.
4137 secondary_source [optional]
4138 Label of an alternate directory tree to find input files. When
4139 searching for a BUILD.gn file (or the build config file discussed
4140 above), the file will first be looked for in the source root.
4141 If it's not found, the secondary source root will be checked
4142 (which would contain a parallel directory hierarchy).
4144 This behavior is intended to be used when BUILD.gn files can't be
4145 checked in to certain source directories for whatever reason.
4147 The secondary source root must be inside the main source tree.
4151 ### **Example .gn file contents**
4154 buildconfig = "//build/config/BUILDCONFIG.gn"
4157 "//doom_melon/*", # Check everything in this subtree.
4158 "//tools:mind_controlling_ant", # Check this specific target.
4163 secondary_source = "//build/config/temporary_buildfiles/"
4167 ## **GN build language grammar**
4172 GN build files are read as sequences of tokens. While splitting the
4173 file into tokens, the next token is the longest sequence of characters
4174 that form a valid token.
4178 ### **White space and comments**
4181 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
4182 carriage returns (U+000D), and newlines (U+000A).
4184 Comments start at the character "#" and stop at the next newline.
4186 White space and comments are ignored except that they may separate
4187 tokens that would otherwise combine into a single token.
4194 Identifiers name variables and functions.
4196 identifier = letter { letter | digit } .
4197 letter = "A" ... "Z" | "a" ... "z" | "_" .
4198 digit = "0" ... "9" .
4205 The following keywords are reserved and may not be used as
4212 ### **Integer literals**
4215 An integer literal represents a decimal integer value.
4217 integer = [ "-" ] digit { digit } .
4219 Leading zeros and negative zero are disallowed.
4223 ### **String literals**
4226 A string literal represents a string value consisting of the quoted
4227 characters with possible escape sequences and variable expansions.
4229 string = `"` { char | escape | expansion } `"` .
4230 escape = `\` ( "$" | `"` | char ) .
4231 expansion = "$" ( identifier | "{" identifier "}" ) .
4232 char = /* any character except "$", `"`, or newline */ .
4234 After a backslash, certain sequences represent special characters:
4236 \" U+0022 quotation mark
4237 \$ U+0024 dollar sign
4240 All other backslashes represent themselves.
4247 The following character sequences represent punctuation:
4259 The input tokens form a syntax tree following a context-free grammar:
4261 File = StatementList .
4263 Statement = Assignment | Call | Condition .
4264 Assignment = identifier AssignOp Expr .
4265 Call = identifier "(" [ ExprList ] ")" [ Block ] .
4266 Condition = "if" "(" Expr ")" Block
4267 [ "else" ( Condition | Block ) ] .
4268 Block = "{" StatementList "}" .
4269 StatementList = { Statement } .
4271 Expr = UnaryExpr | Expr BinaryOp Expr .
4272 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
4273 PrimaryExpr = identifier | integer | string | Call
4274 | identifier "[" Expr "]"
4275 | identifier "." identifier
4277 | "[" [ ExprList [ "," ] ] "]" .
4278 ExprList = Expr { "," Expr } .
4280 AssignOp = "=" | "+=" | "-=" .
4282 BinaryOp = "+" | "-" // highest priority
4283 | "<" | "<=" | ">" | ">="
4286 | "||" . // lowest priority
4288 All binary operators are left-associative.
4292 ## **input_conversion**: Specifies how to transform input to a variable.
4295 input_conversion is an argument to read_file and exec_script that
4296 specifies how the result of the read operation should be converted
4300 Discard the result and return None.
4303 Return the file contents as a list, with a string for each line.
4304 The newlines will not be present in the result. The last line may
4305 or may not end in a newline.
4307 After splitting, each individual line will be trimmed of
4308 whitespace on both ends.
4311 Execute the block as GN code and return a scope with the
4312 resulting values in it. If the input was:
4313 a = [ "hello.cc", "world.cc" ]
4315 and you read the result into a variable named "val", then you
4316 could access contents the "." operator on "val":
4321 Return the file contents into a single string.
4324 Parse the input as if it was a literal rvalue in a buildfile.
4325 Examples of typical program output using this mode:
4326 [ "foo", "bar" ] (result will be a list)
4328 "foo bar" (result will be a string)
4330 5 (result will be an integer)
4332 Note that if the input is empty, the result will be a null value
4333 which will produce an error if assigned to a variable.
4336 Prefixing any of the other transformations with the word "trim"
4337 will result in whitespace being trimmed from the beginning and end
4338 of the result before processing.
4340 Examples: "trim string" or "trim list lines"
4342 Note that "trim value" is useless because the value parser skips
4347 ## **Label patterns**
4350 A label pattern is a way of expressing one or more labels in a portion
4351 of the source tree. They are not general regular expressions.
4353 They can take the following forms only:
4355 - Explicit (no wildcard):
4359 - Wildcard target names:
4360 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
4361 ":*" (all targets in the current build file)
4363 - Wildcard directory names ("*" is only supported at the end)
4365 "//foo/bar/*" (all targets in any subdir of //foo/bar)
4366 "./*" (all targets in the current build file or sub dirs)
4368 Any of the above forms can additionally take an explicit toolchain.
4369 In this case, the toolchain must be fully qualified (no wildcards
4370 are supported in the toolchain name).
4372 "//foo:bar(//build/toochain:mac)"
4373 An explicit target in an explicit toolchain.
4375 ":*(//build/toolchain/linux:32bit)"
4376 All targets in the current build file using the 32-bit Linux
4379 "//foo/*(//build/toolchain:win)"
4380 All targets in //foo and any subdirectory using the Windows
4385 ## **Runtime dependencies**
4388 Runtime dependencies of a target are exposed via the "runtime_deps"
4389 category of "gn desc" (see "gn help desc") or they can be written
4390 at build generation time via "--runtime-deps-list-file"
4391 (see "gn help --runtime-deps-list-file").
4393 To a first approximation, the runtime dependencies of a target are
4394 the set of "data" files, data directories, and the shared libraries
4395 from all transitive dependencies. Executables and shared libraries are
4396 considered runtime dependencies of themselves.
4403 Executable targets and those executable targets' transitive
4404 dependencies are not considered unless that executable is listed in
4405 "data_deps". Otherwise, GN assumes that the executable (and
4406 everything it requires) is a build-time dependency only.
4408 Action and copy targets that are listed as "data_deps" will have all
4409 of their outputs and data files considered as runtime dependencies.
4410 Action and copy targets that are "deps" or "public_deps" will have
4411 only their data files considered as runtime dependencies. These
4412 targets can list an output file in both the "outputs" and "data"
4413 lists to force an output file as a runtime dependency in all cases.
4415 The results of static_library or source_set targets are not considered
4416 runtime dependencies since these are assumed to be intermediate
4417 targets only. If you need to list a static library as a runtime
4418 dependency, you can manually compute the .a/.lib file name for the
4419 current platform and list it in the "data" list of a target
4420 (possibly on the static library target itself).
4422 When a tool produces more than one output, only the first output
4423 is considered. For example, a shared library target may produce a
4424 .dll and a .lib file on Windows. Only the .dll file will be considered
4425 a runtime dependency.
4429 ## **How Source Expansion Works**
4432 Source expansion is used for the action_foreach and copy target types
4433 to map source file names to output file names or arguments.
4435 To perform source expansion in the outputs, GN maps every entry in the
4436 sources to every entry in the outputs list, producing the cross
4437 product of all combinations, expanding placeholders (see below).
4439 Source expansion in the args works similarly, but performing the
4440 placeholder substitution produces a different set of arguments for
4441 each invocation of the script.
4443 If no placeholders are found, the outputs or args list will be treated
4444 as a static list of literal file names that do not depend on the
4447 See "gn help copy" and "gn help action_foreach" for more on how
4452 ### **Placeholders**
4456 The name of the source file including directory (*). This will
4457 generally be used for specifying inputs to a script in the
4459 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
4461 {{source_file_part}}
4462 The file part of the source including the extension.
4463 "//foo/bar/baz.txt" => "baz.txt"
4465 {{source_name_part}}
4466 The filename part of the source file with no directory or
4467 extension. This will generally be used for specifying a
4468 transformation from a soruce file to a destination file with the
4469 same name but different extension.
4470 "//foo/bar/baz.txt" => "baz"
4473 The directory (*) containing the source file with no
4475 "//foo/bar/baz.txt" => "../../foo/bar"
4477 {{source_root_relative_dir}}
4478 The path to the source file's directory relative to the source
4479 root, with no leading "//" or trailing slashes. If the path is
4480 system-absolute, (beginning in a single slash) this will just
4481 return the path with no trailing slash. This value will always
4482 be the same, regardless of whether it appears in the "outputs"
4484 "//foo/bar/baz.txt" => "foo/bar"
4487 The generated file directory (*) corresponding to the source
4488 file's path. This will be different than the target's generated
4489 file directory if the source file is in a different directory
4490 than the BUILD.gn file.
4491 "//foo/bar/baz.txt" => "gen/foo/bar"
4494 The object file directory (*) corresponding to the source file's
4495 path, relative to the build directory. this us be different than
4496 the target's out directory if the source file is in a different
4497 directory than the build.gn file.
4498 "//foo/bar/baz.txt" => "obj/foo/bar"
4502 ### **(*) Note on directories**
4505 Paths containing directories (except the source_root_relative_dir)
4506 will be different depending on what context the expansion is evaluated
4507 in. Generally it should "just work" but it means you can't
4508 concatenate strings containing these values with reasonable results.
4510 Details: source expansions can be used in the "outputs" variable,
4511 the "args" variable, and in calls to "process_file_template". The
4512 "args" are passed to a script which is run from the build directory,
4513 so these directories will relative to the build directory for the
4514 script to find. In the other cases, the directories will be source-
4515 absolute (begin with a "//") because the results of those expansions
4516 will be handled by GN internally.
4523 Non-varying outputs:
4524 action("hardcoded_outputs") {
4525 sources = [ "input1.idl", "input2.idl" ]
4526 outputs = [ "$target_out_dir/output1.dat",
4527 "$target_out_dir/output2.dat" ]
4529 The outputs in this case will be the two literal files given.
4532 action_foreach("varying_outputs") {
4533 sources = [ "input1.idl", "input2.idl" ]
4534 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
4535 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
4537 Performing source expansion will result in the following output names:
4538 //out/Debug/obj/mydirectory/input1.h
4539 //out/Debug/obj/mydirectory/input1.cc
4540 //out/Debug/obj/mydirectory/input2.h
4541 //out/Debug/obj/mydirectory/input2.cc
4545 **Available global switches
4546 ** Do "gn help --the_switch_you_want_help_on" for more. Individual
4547 commands may take command-specific switches not listed here. See the
4548 help on your specific command for more.
4552 ** --args**: Specifies build arguments overrides.
4553 ** --color**: Force colored output.
4554 ** --dotfile**: Override the name of the ".gn" file.
4555 ** --markdown**: write the output in the Markdown format.
4556 ** --nocolor**: Force non-colored output.
4557 ** -q**: Quiet mode. Don't print output on success.
4558 ** --root**: Explicitly specify source root.
4559 ** --runtime-deps-list-file**: Save runtime dependencies for targets in file.
4560 ** --time**: Outputs a summary of how long everything took.
4561 ** --tracelog**: Writes a Chrome-compatible trace log to the given file.
4562 ** -v**: Verbose logging.
4563 ** --version**: Prints the GN version number and exits.