blob: ad3ac85676af7484335a90a07b9eeb3dbfb8eeb0 [file] [log] [blame] [view]
dprankee7700dd2015-04-17 22:55:451# GN Reference
2
dprankee7700dd2015-04-17 22:55:453*This page is automatically generated from* `gn help --markdown all`.
4
5## **--args**: Specifies build arguments overrides.
6
7```
8 See "gn help buildargs" for an overview of how build arguments work.
9
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.
14
15 The args specified will be saved to the build directory for subsequent
16 commands. Specifying --args="" will clear all build arguments.
17
18```
19
20### **Formatting**
21
22```
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
25 quotes.
26
27```
28
29### **Examples**
30
31```
32 gn gen out/Default --args="foo=\"bar\""
33
34 gn gen out/Default --args='foo="bar" enable=true blah=7'
35
36 gn check out/Default --args=""
37 Clears existing build args from the directory.
38
39 gn desc out/Default --args="some_list=[1, false, \"foo\"]"
40
41
42```
43## **--[no]color**: Forces colored output on or off.
44
45```
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.
49
50```
51
52### **Examples**
53
54```
55 gn gen out/Default --color
56
57 gn gen out/Default --nocolor
58
59
60```
dpranke5316b8c2015-06-09 16:03:2661## **--dotfile**: Override the name of the ".gn" file.
dprankee7700dd2015-04-17 22:55:4562
63```
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
66 use a different file.
67
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.
70
71
72```
73## **--markdown**: write the output in the Markdown format.
74
75## **--[no]color**: Forces colored output on or off.
76
77```
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.
81
82```
83
84### **Examples**
85
86```
87 gn gen out/Default --color
88
89 gn gen out/Default --nocolor
90
91
92```
93## **-q**: Quiet mode. Don't print output on success.
94
95```
96 This is useful when running as a part of another script.
97
98
99```
100## **--root**: Explicitly specify source root.
101
102```
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.
107
108 Specifying --root allows GN to do builds in a specific directory
109 regardless of the current directory.
110
111```
112
113### **Examples**
114
115```
116 gn gen //out/Default --root=/home/baracko/src
117
118 gn desc //out/Default --root="C:\Users\BObama\My Documents\foo"
119
120
121```
dpranke5316b8c2015-06-09 16:03:26122## **--runtime-deps-list-file**: Save runtime dependencies for targets in file.
123
124```
125 --runtime-deps-list-file=<filename>
126
127 Where <filename> is a text file consisting of the labels, one per
128 line, of the targets for which runtime dependencies are desired.
129
130 See "gn help runtime_deps" for a description of how runtime
131 dependencies are computed.
132
133```
134
135### **Runtime deps output file**
136
137```
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.
144
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.
149
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").
154
155
156```
dprankee7700dd2015-04-17 22:55:45157## **--time**: Outputs a summary of how long everything took.
158
159```
160 Hopefully self-explanatory.
161
162```
163
164### **Examples**
165
166```
167 gn gen out/Default --time
168
169
170```
171## **--tracelog**: Writes a Chrome-compatible trace log to the given file.
172
173```
174 The trace log will show file loads, executions, scripts, and writes.
175 This allows performance analysis of the generation step.
176
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.
179
180```
181
182### **Examples**
183
184```
185 gn gen out/Default --tracelog=mytrace.trace
186
187
188```
189## **-v**: Verbose logging.
190
191```
192 This will spew logging events to the console for debugging issues.
193 Good luck!
194
195
196```
197## **gn args <out_dir> [--list] [--short] [--args]**
198
199```
200 See also "gn help buildargs" for a more high-level overview of how
201 build arguments work.
202
203```
204
205### **Usage**
206```
207 gn args <out_dir>
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
214 os="android"
215
216 Note: you can edit the build args manually by editing the file
217 "args.gn" in the build directory and then running
218 "gn gen <out_dir>".
219
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
223 build argument.
224
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.
228
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.
235
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>
239
240```
241
242### **Examples**
243```
244 gn args out/Debug
245 Opens an editor with the args for out/Debug.
246
247 gn args out/Debug --list --short
248 Prints all arguments with their default values for the out/Debug
249 build.
250
251 gn args out/Debug --list=target_cpu
252 Prints information about the "target_cpu" argument for the out/Debug
253 build.
254
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
258 arguments).
259
260
261```
262## **gn check <out_dir> [<label_pattern>] [--force]**
263
264```
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.
268
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.
273
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
277 "gn help dotfile".
278
279```
280
281### **Command-specific switches**
282
283```
284 --force
285 Ignores specifications of "check_includes = false" and checks
286 all target's files that match the target label.
287
288```
289
290### **Examples**
291
292```
293 gn check out/Debug
294 Check everything.
295
296 gn check out/Default //foo:bar
297 Check only the files in the //foo:bar target.
298
299 gn check out/Default "//foo/*
300 Check only the files in targets in the //foo directory tree.
301
302
303```
304## **gn clean <out_dir>**
305
306```
307 Deletes the contents of the output directory except for args.gn and
308 creates a Ninja build environment sufficient to regenerate the build.
309
310
311```
312## **gn desc <out_dir> <target label> [<what to show>] [--blame]**
313
314```
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
317 <out_dir>.
318
319```
320
321### **Possibilities for <what to show>**
322```
323 (If unspecified an overall summary will be displayed.)
324
325 sources
326 Source files.
327
328 inputs
329 Additional input dependencies.
330
331 public
332 Public header files.
333
334 check_includes
335 Whether "gn check" checks this target for include usage.
336
337 allow_circular_includes_from
338 Permit includes from these targets.
339
340 visibility
341 Prints which targets can depend on this one.
342
343 testonly
344 Whether this target may only be used in tests.
345
346 configs
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
351 configs.
352
353 deps
354 Show immediate or recursive dependencies. See below for flags that
355 control deps printing.
356
357 public_configs
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).
361
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.
365
366 script
367 args
368 depfile
369 Actions only. The script and related values.
370
371 outputs
372 Outputs for script and copy target types.
373
374 defines [--blame]
375 include_dirs [--blame]
376 cflags [--blame]
377 cflags_cc [--blame]
378 cflags_cxx [--blame]
379 ldflags [--blame]
380 lib_dirs
381 libs
382 Shows the given values taken from the target and all configs
383 applying. See "--blame" below.
384
dpranke5316b8c2015-06-09 16:03:26385 runtime_deps
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.
389
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
393 dependency.
394
395```
396
397### **Shared flags**
398
399```
dprankee7700dd2015-04-17 22:55:45400 --blame
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).
405
406```
407
408### **Flags that control how deps are printed**
409
410```
411 --all
412 Collects all recursive dependencies and prints a sorted flat list.
413 Also usable with --tree (see below).
414
415 --as=(buildfile|label|output)
416 How to print targets.
417
418 buildfile
419 Prints the build files where the given target was declared as
420 file names.
421 label (default)
422 Prints the label of the target.
423 output
424 Prints the first output file for the target relative to the
425 current directory.
426
427 --testonly=(true|false)
428 Restrict outputs to targets with the testonly flag set
429 accordingly. When unspecified, the target's testonly flags are
430 ignored.
431
432 --tree
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.
436
437 The "deps", "public_deps", and "data_deps" will all be
438 included in the tree.
439
440 Tree output can not be used with the filtering or output flags:
441 --as, --type, --testonly.
442
443 --type=(action|copy|executable|group|shared_library|source_set|
444 static_library)
445 Restrict outputs to targets matching the given type. If
446 unspecified, no filtering will be performed.
447
448```
449
450### **Note**
451
452```
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).
458
459```
460
461### **Examples**
462
463```
464 gn desc out/Debug //base:base
465 Summarizes the given target.
466
467 gn desc out/Foo :base_unittests deps --tree
468 Shows a dependency tree of the "base_unittests" project in
469 the current directory.
470
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.
474
475
476```
477## **gn format [--dump-tree] [--in-place] [--stdin] BUILD.gn**
478
479```
480 Formats .gn file to a standard format.
481
482```
483
484### **Arguments**
485```
486 --dry-run
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.
493
494 --dump-tree
495 For debugging only, dumps the parse tree.
496
497 --in-place
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.
501
502 --stdin
503 Read input from stdin (and write to stdout). Not compatible with
504 --in-place of course.
505
506```
507
508### **Examples**
509```
510 gn format //some/BUILD.gn
511 gn format some\BUILD.gn
512 gn format /abspath/some/BUILD.gn
513 gn format --stdin
514
515
516```
517## **gn gen**: Generate ninja files.
518
519```
520 gn gen <out_dir>
521
522 Generates ninja files from the current tree and puts them in the given
523 output directory.
524
525 The output directory can be a source-repo-absolute path name such as:
526 //out/foo
527 Or it can be a directory relative to the current directory such as:
528 out/foo
529
530 See "gn help" for the common command-line switches.
531
532
533```
534## **gn help <anything>**
535```
536 Yo dawg, I heard you like help on your help so I put help on the help
537 in the help.
538
539
540```
541## **gn ls <out_dir> [<label_pattern>] [--all-toolchains] [--as=...]**
542```
543 [--type=...] [--testonly=...]
544
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.
548
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.
553
554```
555
556### **Options**
557
558```
559 --as=(buildfile|label|output)
560 How to print targets.
561
562 buildfile
563 Prints the build files where the given target was declared as
564 file names.
565 label (default)
566 Prints the label of the target.
567 output
568 Prints the first output file for the target relative to the
569 current directory.
570
571 --all-toolchains
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.
576
577 --testonly=(true|false)
578 Restrict outputs to targets with the testonly flag set
579 accordingly. When unspecified, the target's testonly flags are
580 ignored.
581
582 --type=(action|copy|executable|group|shared_library|source_set|
583 static_library)
584 Restrict outputs to targets matching the given type. If
585 unspecified, no filtering will be performed.
586
587```
588
589### **Examples**
590
591```
592 gn ls out/Debug
593 Lists all targets in the default toolchain.
594
595 gn ls out/Debug "//base/*"
596 Lists all targets in the directory base and all subdirectories.
597
598 gn ls out/Debug "//base:*"
599 Lists all targets defined in //base/BUILD.gn.
600
601 gn ls out/Debug //base --as=output
602 Lists the build output file for //base:base
603
604 gn ls out/Debug --type=executable
605 Lists all executables produced by the build.
606
607 gn ls out/Debug "//base/*" --as=output | xargs ninja -C out/Debug
608 Builds all targets in //base and all subdirectories.
609
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).
613
614
615```
dpranke5316b8c2015-06-09 16:03:26616## **gn path <out_dir> <target_one> <target_two>**
617
618```
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
622 in either direction.
623
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
626 shortest path.
627
628```
629
630### **Options**
631
632```
633 --all
634 Prints all paths found rather than just the first one.
635
636```
637
638### **Example**
639
640```
641 gn path out/Default //base //tools/gn
642
643
644```
dpranke36a275d2015-04-21 23:45:53645## **gn refs <out_dir> (<label_pattern>|<label>|<file>|@<response_file>)* [--all]**
dprankee7700dd2015-04-17 22:55:45646```
647 [--all-toolchains] [--as=...] [--testonly=...] [--type=...]
648
649 Finds reverse dependencies (which targets reference something). The
650 input is a list containing:
651
652 - Target label: The result will be which targets depend on it.
653
654 - Config label: The result will be which targets list the given
655 config in its "configs" or "public_configs" list.
656
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.
661
662 - File name: The result will be which targets list the given file in
dpranke5316b8c2015-06-09 16:03:26663 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.
dprankee7700dd2015-04-17 22:55:45666
dpranke36a275d2015-04-21 23:45:53667 - 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.
671
dprankee7700dd2015-04-17 22:55:45672```
673
674### **Options**
675
676```
677 --all
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.
683
684 When used with --tree, turns off eliding to show a complete tree.
685
686 --all-toolchains
687 Normally only inputs in the default toolchain will be included.
688 This switch will turn on matching all toolchains.
689
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).
696
697 --as=(buildfile|label|output)
698 How to print targets.
699
700 buildfile
701 Prints the build files where the given target was declared as
702 file names.
703 label (default)
704 Prints the label of the target.
705 output
706 Prints the first output file for the target relative to the
707 current directory.
708
709 -q
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
713 output.
714
715 --testonly=(true|false)
716 Restrict outputs to targets with the testonly flag set
717 accordingly. When unspecified, the target's testonly flags are
718 ignored.
719
720 --tree
721 Outputs a reverse dependency tree from the given target.
722 Duplicates will be elided. Combine with --all to see a full
723 dependency tree.
724
725 Tree output can not be used with the filtering or output flags:
726 --as, --type, --testonly.
727
728 --type=(action|copy|executable|group|shared_library|source_set|
729 static_library)
730 Restrict outputs to targets matching the given type. If
731 unspecified, no filtering will be performed.
732
733```
734
735### **Examples (target input)**
736
737```
738 gn refs out/Debug //tools/gn:gn
739 Find all targets depending on the given exact target name.
740
741 gn refs out/Debug //base:i18n --as=buildfiles | xargs gvim
742 Edit all .gn files containing references to //base:i18n
743
744 gn refs out/Debug //base --all
745 List all targets depending directly or indirectly on //base:base.
746
747 gn refs out/Debug "//base/*"
748 List all targets depending directly on any target in //base or
749 its subdirectories.
750
751 gn refs out/Debug "//base:*"
752 List all targets depending directly on any target in
753 //base/BUILD.gn.
754
755 gn refs out/Debug //base --tree
756 Print a reverse dependency tree of //base:base
757
758```
759
760### **Examples (file input)**
761
762```
763 gn refs out/Debug //base/macros.h
764 Print target(s) listing //base/macros.h as a source.
765
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.
769
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.
773
774 gn refs out/Debug //base/macros.h --testonly=true --type=executable
775 --all --as=output
776 Display the executable file names of all test executables
777 potentially affected by a change to the given file.
778
779
780```
781## **action**: Declare a target that runs a script a single time.
782
783```
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".
787
788```
789
790### **Inputs**
791
792```
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.
800
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".
804
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.
808
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.
815
816```
817
818### **Outputs**
819
820```
821 You should specify files created by your script by specifying them in
822 the "outputs".
823
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).
830
831```
832
833### **File name handling**
834
835```
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,
839 respectively.
840
841```
842
843### **Variables**
844
845```
846 args, data, data_deps, depfile, deps, outputs*, script*,
847 inputs, sources
848 * = required
849
850```
851
852### **Example**
853
854```
855 action("run_this_guy_once") {
856 script = "doprocessing.py"
857 sources = [ "my_configuration.txt" ]
858 outputs = [ "$target_gen_dir/insightful_output.txt" ]
859
860 # Our script imports this Python file so we want to rebuild if it
861 # changes.
862 inputs = [ "helper_library.py" ]
863
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)
868 }
869
870
871```
872## **action_foreach**: Declare a target that runs a script over a set of files.
873
874```
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".
878
879```
880
881### **Inputs**
882
883```
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.
889
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.
894
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".
898
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.
905
906```
907
908### **Outputs**
909
910```
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).
917
918```
919
920### **File name handling**
921
922```
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,
926 respectively.
927
928```
929
930### **Variables**
931
932```
933 args, data, data_deps, depfile, deps, outputs*, script*,
934 inputs, sources*
935 * = required
936
937```
938
939### **Example**
940
941```
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" ]
947
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" ]
951
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" ]
955
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()".
959 args = [
960 "{{source}}",
961 "-o",
962 rebase_path(relative_target_gen_dir, root_build_dir) +
963 "/{{source_name_part}}.h" ]
964 }
965
966
967
968```
969## **assert**: Assert an expression is true at generation time.
970
971```
972 assert(<condition> [, <error string>])
973
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.
977
978```
979
980### **Examples**:
981```
982 assert(is_win)
983 assert(defined(sources), "Sources must be defined")
984
985
986```
987## **config**: Defines a configuration object.
988
989```
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.
993
994 A config is referenced by its label just like a target.
995
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:
999
1000 1. The values specified directly on the target (rather than using a
1001 config.
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".
1007
1008```
1009
1010### **Variables valid in a config definition**:
1011```
1012 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1013 defines, include_dirs, ldflags, lib_dirs, libs
brettw20982dc2015-07-06 22:37:061014 precompiled_header, precompiled_source
dprankee7700dd2015-04-17 22:55:451015
1016```
1017
1018### **Variables on a target used to apply configs**:
1019```
1020 all_dependent_configs, configs, public_configs,
1021 forward_dependent_configs_from
1022
1023```
1024
1025### **Example**:
1026```
1027 config("myconfig") {
1028 includes = [ "include/common" ]
1029 defines = [ "ENABLE_DOOM_MELON" ]
1030 }
1031
1032 executable("mything") {
1033 configs = [ ":myconfig" ]
1034 }
1035
1036
1037```
1038## **copy**: Declare a target that copies files.
1039
1040### **File name handling**
1041
1042```
1043 All output files must be inside the output directory of the build.
1044 You would generally use |$target_out_dir| or |$target_gen_dir| to
1045 reference the output or generated intermediate file directories,
1046 respectively.
1047
1048 Both "sources" and "outputs" must be specified. Sources can
1049 as many files as you want, but there can only be one item in the
1050 outputs list (plural is used for the name for consistency with
1051 other target types).
1052
1053 If there is more than one source file, your output name should specify
1054 a mapping from each source files to output file names using source
1055 expansion (see "gn help source_expansion"). The placeholders will
1056 will look like "{{source_name_part}}", for example.
1057
1058```
1059
1060### **Examples**
1061
1062```
1063 # Write a rule that copies a checked-in DLL to the output directory.
1064 copy("mydll") {
1065 sources = [ "mydll.dll" ]
1066 outputs = [ "$target_out_dir/mydll.dll" ]
1067 }
1068
1069 # Write a rule to copy several files to the target generated files
1070 # directory.
1071 copy("myfiles") {
1072 sources = [ "data1.dat", "data2.dat", "data3.dat" ]
1073
1074 # Use source expansion to generate output files with the
1075 # corresponding file names in the gen dir. This will just copy each
1076 # file.
1077 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
1078 }
1079
1080
1081```
1082## **declare_args**: Declare build arguments.
1083
1084```
1085 Introduces the given arguments into the current scope. If they are
1086 not specified on the command line or in a toolchain's arguments,
1087 the default values given in the declare_args block will be used.
1088 However, these defaults will not override command-line values.
1089
1090 See also "gn help buildargs" for an overview.
1091
1092```
1093
1094### **Example**:
1095```
1096 declare_args() {
1097 enable_teleporter = true
1098 enable_doom_melon = false
1099 }
1100
1101 If you want to override the (default disabled) Doom Melon:
1102 gn --args="enable_doom_melon=true enable_teleporter=false"
1103 This also sets the teleporter, but it's already defaulted to on so
1104 it will have no effect.
1105
1106
1107```
1108## **defined**: Returns whether an identifier is defined.
1109
1110```
1111 Returns true if the given argument is defined. This is most useful in
1112 templates to assert that the caller set things up properly.
1113
1114 You can pass an identifier:
1115 defined(foo)
1116 which will return true or false depending on whether foo is defined in
1117 the current scope.
1118
1119 You can also check a named scope:
1120 defined(foo.bar)
1121 which will return true or false depending on whether bar is defined in
1122 the named scope foo. It will throw an error if foo is not defined or
1123 is not a scope.
1124
1125```
1126
1127### **Example**:
1128
1129```
1130 template("mytemplate") {
1131 # To help users call this template properly...
1132 assert(defined(invoker.sources), "Sources must be defined")
1133
1134 # If we want to accept an optional "values" argument, we don't
1135 # want to dereference something that may not be defined.
1136 if (defined(invoker.values)) {
1137 values = invoker.values
1138 } else {
1139 values = "some default value"
1140 }
1141 }
1142
1143
1144```
1145## **exec_script**: Synchronously run a script and return the output.
1146
1147```
1148 exec_script(filename,
1149 arguments = [],
1150 input_conversion = "",
1151 file_dependencies = [])
1152
1153 Runs the given script, returning the stdout of the script. The build
1154 generation will fail if the script does not exist or returns a nonzero
1155 exit code.
1156
1157 The current directory when executing the script will be the root
1158 build directory. If you are passing file names, you will want to use
1159 the rebase_path() function to make file names relative to this
1160 path (see "gn help rebase_path").
1161
1162```
1163
1164### **Arguments**:
1165
1166```
1167 filename:
1168 File name of python script to execute. Non-absolute names will
1169 be treated as relative to the current build file.
1170
1171 arguments:
1172 A list of strings to be passed to the script as arguments.
1173 May be unspecified or the empty list which means no arguments.
1174
1175 input_conversion:
1176 Controls how the file is read and parsed.
1177 See "gn help input_conversion".
1178
1179 If unspecified, defaults to the empty string which causes the
1180 script result to be discarded. exec script will return None.
1181
1182 dependencies:
1183 (Optional) A list of files that this script reads or otherwise
1184 depends on. These dependencies will be added to the build result
1185 such that if any of them change, the build will be regenerated and
1186 the script will be re-run.
1187
1188 The script itself will be an implicit dependency so you do not
1189 need to list it.
1190
1191```
1192
1193### **Example**:
1194
1195```
1196 all_lines = exec_script(
1197 "myscript.py", [some_input], "list lines",
1198 [ rebase_path("data_file.txt", root_build_dir) ])
1199
1200 # This example just calls the script with no arguments and discards
1201 # the result.
1202 exec_script("//foo/bar/myscript.py")
1203
1204
1205```
1206## **executable**: Declare an executable target.
1207
1208### **Variables**
1209
1210```
1211 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1212 defines, include_dirs, ldflags, lib_dirs, libs
brettw20982dc2015-07-06 22:37:061213 precompiled_header, precompiled_source
dprankee7700dd2015-04-17 22:55:451214 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1215 Dependent configs: all_dependent_configs, public_configs
1216 General: check_includes, configs, data, inputs, output_name,
1217 output_extension, public, sources, testonly, visibility
1218
1219
1220```
1221## **foreach**: Iterate over a list.
1222
1223```
1224 foreach(<loop_var>, <list>) {
1225 <loop contents>
1226 }
1227
1228 Executes the loop contents block over each item in the list,
1229 assigning the loop_var to each item in sequence.
1230
1231 The block does not introduce a new scope, so that variable assignments
1232 inside the loop will be visible once the loop terminates.
1233
1234 The loop variable will temporarily shadow any existing variables with
1235 the same name for the duration of the loop. After the loop terminates
1236 the loop variable will no longer be in scope, and the previous value
1237 (if any) will be restored.
1238
1239```
1240
1241### **Example**
1242
1243```
1244 mylist = [ "a", "b", "c" ]
1245 foreach(i, mylist) {
1246 print(i)
1247 }
1248
1249 Prints:
1250 a
1251 b
1252 c
1253
1254
1255```
1256## **get_label_info**: Get an attribute from a target's label.
1257
1258```
1259 get_label_info(target_label, what)
1260
1261 Given the label of a target, returns some attribute of that target.
1262 The target need not have been previously defined in the same file,
1263 since none of the attributes depend on the actual target definition,
1264 only the label itself.
1265
1266 See also "gn help get_target_outputs".
1267
1268```
1269
1270### **Possible values for the "what" parameter**
1271
1272```
1273 "name"
1274 The short name of the target. This will match the value of the
1275 "target_name" variable inside that target's declaration. For the
1276 label "//foo/bar:baz" this will return "baz".
1277
1278 "dir"
1279 The directory containing the target's definition, with no slash at
1280 the end. For the label "//foo/bar:baz" this will return
1281 "//foo/bar".
1282
1283 "target_gen_dir"
1284 The generated file directory for the target. This will match the
1285 value of the "target_gen_dir" variable when inside that target's
1286 declaration.
1287
1288 "root_gen_dir"
1289 The root of the generated file tree for the target. This will
1290 match the value of the "root_gen_dir" variable when inside that
1291 target's declaration.
1292
1293 "target_out_dir
1294 The output directory for the target. This will match the
1295 value of the "target_out_dir" variable when inside that target's
1296 declaration.
1297
1298 "root_out_dir"
1299 The root of the output file tree for the target. This will
1300 match the value of the "root_gen_dir" variable when inside that
1301 target's declaration.
1302
1303 "label_no_toolchain"
1304 The fully qualified version of this label, not including the
1305 toolchain. For the input ":bar" it might return
1306 "//foo:bar".
1307
1308 "label_with_toolchain"
1309 The fully qualified version of this label, including the
1310 toolchain. For the input ":bar" it might return
1311 "//foo:bar(//toolchain:x64)".
1312
1313 "toolchain"
1314 The label of the toolchain. This will match the value of the
1315 "current_toolchain" variable when inside that target's
1316 declaration.
1317
1318```
1319
1320### **Examples**
1321
1322```
1323 get_label_info(":foo", "name")
1324 # Returns string "foo".
1325
1326 get_label_info("//foo/bar:baz", "gen_dir")
1327 # Returns string "//out/Debug/gen/foo/bar".
1328
1329
1330```
1331## **get_path_info**: Extract parts of a file or directory name.
1332
1333```
1334 get_path_info(input, what)
1335
1336 The first argument is either a string representing a file or
1337 directory name, or a list of such strings. If the input is a list
1338 the return value will be a list containing the result of applying the
1339 rule to each item in the input.
1340
1341```
1342
1343### **Possible values for the "what" parameter**
1344
1345```
1346 "file"
1347 The substring after the last slash in the path, including the name
1348 and extension. If the input ends in a slash, the empty string will
1349 be returned.
1350 "foo/bar.txt" => "bar.txt"
1351 "bar.txt" => "bar.txt"
1352 "foo/" => ""
1353 "" => ""
1354
1355 "name"
1356 The substring of the file name not including the extension.
1357 "foo/bar.txt" => "bar"
1358 "foo/bar" => "bar"
1359 "foo/" => ""
1360
1361 "extension"
1362 The substring following the last period following the last slash,
1363 or the empty string if not found. The period is not included.
1364 "foo/bar.txt" => "txt"
1365 "foo/bar" => ""
1366
1367 "dir"
1368 The directory portion of the name, not including the slash.
1369 "foo/bar.txt" => "foo"
1370 "//foo/bar" => "//foo"
1371 "foo" => "."
1372
1373 The result will never end in a slash, so if the resulting
1374 is empty, the system ("/") or source ("//") roots, a "."
1375 will be appended such that it is always legal to append a slash
1376 and a filename and get a valid path.
1377
1378 "out_dir"
1379 The output file directory corresponding to the path of the
1380 given file, not including a trailing slash.
1381 "//foo/bar/baz.txt" => "//out/Default/obj/foo/bar"
1382 "gen_dir"
1383 The generated file directory corresponding to the path of the
1384 given file, not including a trailing slash.
1385 "//foo/bar/baz.txt" => "//out/Default/gen/foo/bar"
1386
1387 "abspath"
1388 The full absolute path name to the file or directory. It will be
1389 resolved relative to the currebt directory, and then the source-
1390 absolute version will be returned. If the input is system-
1391 absolute, the same input will be returned.
1392 "foo/bar.txt" => "//mydir/foo/bar.txt"
1393 "foo/" => "//mydir/foo/"
1394 "//foo/bar" => "//foo/bar" (already absolute)
1395 "/usr/include" => "/usr/include" (already absolute)
1396
1397 If you want to make the path relative to another directory, or to
1398 be system-absolute, see rebase_path().
1399
1400```
1401
1402### **Examples**
1403```
1404 sources = [ "foo.cc", "foo.h" ]
1405 result = get_path_info(source, "abspath")
1406 # result will be [ "//mydir/foo.cc", "//mydir/foo.h" ]
1407
1408 result = get_path_info("//foo/bar/baz.cc", "dir")
1409 # result will be "//foo/bar"
1410
1411 # Extract the source-absolute directory name,
1412 result = get_path_info(get_path_info(path, "dir"), "abspath")
1413
1414
1415```
1416## **get_target_outputs**: [file list] Get the list of outputs from a target.
1417
1418```
1419 get_target_outputs(target_label)
1420
1421 Returns a list of output files for the named target. The named target
1422 must have been previously defined in the current file before this
1423 function is called (it can't reference targets in other files because
1424 there isn't a defined execution order, and it obviously can't
1425 reference targets that are defined after the function call).
1426
1427 Only copy and action targets are supported. The outputs from binary
1428 targets will depend on the toolchain definition which won't
1429 necessarily have been loaded by the time a given line of code has run,
1430 and source sets and groups have no useful output file.
1431
1432```
1433
1434### **Return value**
1435
1436```
1437 The names in the resulting list will be absolute file paths (normally
1438 like "//out/Debug/bar.exe", depending on the build directory).
1439
1440 action targets: this will just return the files specified in the
1441 "outputs" variable of the target.
1442
1443 action_foreach targets: this will return the result of applying
1444 the output template to the sources (see "gn help source_expansion").
1445 This will be the same result (though with guaranteed absolute file
1446 paths), as process_file_template will return for those inputs
1447 (see "gn help process_file_template").
1448
1449 binary targets (executables, libraries): this will return a list
1450 of the resulting binary file(s). The "main output" (the actual
1451 binary or library) will always be the 0th element in the result.
1452 Depending on the platform and output type, there may be other output
1453 files as well (like import libraries) which will follow.
1454
1455 source sets and groups: this will return a list containing the path of
1456 the "stamp" file that Ninja will produce once all outputs are
1457 generated. This probably isn't very useful.
1458
1459```
1460
1461### **Example**
1462
1463```
1464 # Say this action generates a bunch of C source files.
1465 action_foreach("my_action") {
1466 sources = [ ... ]
1467 outputs = [ ... ]
1468 }
1469
1470 # Compile the resulting source files into a source set.
1471 source_set("my_lib") {
1472 sources = get_target_outputs(":my_action")
1473 }
1474
1475
1476```
1477## **getenv**: Get an environment variable.
1478
1479```
1480 value = getenv(env_var_name)
1481
1482 Returns the value of the given enironment variable. If the value is
1483 not found, it will try to look up the variable with the "opposite"
1484 case (based on the case of the first letter of the variable), but
1485 is otherwise case-sensitive.
1486
1487 If the environment variable is not found, the empty string will be
1488 returned. Note: it might be nice to extend this if we had the concept
1489 of "none" in the language to indicate lookup failure.
1490
1491```
1492
1493### **Example**:
1494
1495```
1496 home_dir = getenv("HOME")
1497
1498
1499```
1500## **group**: Declare a named group of targets.
1501
1502```
1503 This target type allows you to create meta-targets that just collect a
1504 set of dependencies into one named target. Groups can additionally
1505 specify configs that apply to their dependents.
1506
1507 Depending on a group is exactly like depending directly on that
1508 group's deps. Direct dependent configs will get automatically
1509 forwarded through the group so you shouldn't need to use
1510 "forward_dependent_configs_from.
1511
1512```
1513
1514### **Variables**
1515
1516```
1517 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1518 Dependent configs: all_dependent_configs, public_configs
1519
1520```
1521
1522### **Example**
1523
1524```
1525 group("all") {
1526 deps = [
1527 "//project:runner",
1528 "//project:unit_tests",
1529 ]
1530 }
1531
1532
1533```
1534## **import**: Import a file into the current scope.
1535
1536```
1537 The import command loads the rules and variables resulting from
1538 executing the given file into the current scope.
1539
1540 By convention, imported files are named with a .gni extension.
1541
1542 An import is different than a C++ "include". The imported file is
1543 executed in a standalone environment from the caller of the import
1544 command. The results of this execution are cached for other files that
1545 import the same .gni file.
1546
1547 Note that you can not import a BUILD.gn file that's otherwise used
1548 in the build. Files must either be imported or implicitly loaded as
1549 a result of deps rules, but not both.
1550
1551 The imported file's scope will be merged with the scope at the point
1552 import was called. If there is a conflict (both the current scope and
1553 the imported file define some variable or rule with the same name but
1554 different value), a runtime error will be thrown. Therefore, it's good
1555 practice to minimize the stuff that an imported file defines.
1556
1557 Variables and templates beginning with an underscore '_' are
1558 considered private and will not be imported. Imported files can use
1559 such variables for internal computation without affecting other files.
1560
1561```
1562
1563### **Examples**:
1564
1565```
1566 import("//build/rules/idl_compilation_rule.gni")
1567
1568 # Looks in the current directory.
1569 import("my_vars.gni")
1570
1571
1572```
1573## **print**: Prints to the console.
1574
1575```
1576 Prints all arguments to the console separated by spaces. A newline is
1577 automatically appended to the end.
1578
1579 This function is intended for debugging. Note that build files are run
1580 in parallel so you may get interleaved prints. A buildfile may also
1581 be executed more than once in parallel in the context of different
1582 toolchains so the prints from one file may be duplicated or
1583 interleaved with itself.
1584
1585```
1586
1587### **Examples**:
1588```
1589 print("Hello world")
1590
1591 print(sources, deps)
1592
1593
1594```
1595## **process_file_template**: Do template expansion over a list of files.
1596
1597```
1598 process_file_template(source_list, template)
1599
1600 process_file_template applies a template list to a source file list,
1601 returning the result of applying each template to each source. This is
1602 typically used for computing output file names from input files.
1603
1604 In most cases, get_target_outputs() will give the same result with
1605 shorter, more maintainable code. This function should only be used
1606 when that function can't be used (like there's no target or the target
1607 is defined in another build file).
1608
1609```
1610
1611### **Arguments**:
1612
1613```
1614 The source_list is a list of file names.
1615
1616 The template can be a string or a list. If it is a list, multiple
1617 output strings are generated for each input.
1618
1619 The template should contain source expansions to which each name in
1620 the source list is applied. See "gn help source_expansion".
1621
1622```
1623
1624### **Example**:
1625
1626```
1627 sources = [
1628 "foo.idl",
1629 "bar.idl",
1630 ]
1631 myoutputs = process_file_template(
1632 sources,
1633 [ "$target_gen_dir/{{source_name_part}}.cc",
1634 "$target_gen_dir/{{source_name_part}}.h" ])
1635
1636 The result in this case will be:
1637 [ "//out/Debug/foo.cc"
1638 "//out/Debug/foo.h"
1639 "//out/Debug/bar.cc"
1640 "//out/Debug/bar.h" ]
1641
1642
1643```
1644## **read_file**: Read a file into a variable.
1645
1646```
1647 read_file(filename, input_conversion)
1648
1649 Whitespace will be trimmed from the end of the file. Throws an error
1650 if the file can not be opened.
1651
1652```
1653
1654### **Arguments**:
1655
1656```
1657 filename
1658 Filename to read, relative to the build file.
1659
1660 input_conversion
1661 Controls how the file is read and parsed.
1662 See "gn help input_conversion".
1663
1664```
1665
1666### **Example**
1667```
1668 lines = read_file("foo.txt", "list lines")
1669
1670
1671```
1672## **rebase_path**: Rebase a file or directory to another location.
1673
1674```
1675 converted = rebase_path(input,
1676 new_base = "",
1677 current_base = ".")
1678
1679 Takes a string argument representing a file name, or a list of such
1680 strings and converts it/them to be relative to a different base
1681 directory.
1682
1683 When invoking the compiler or scripts, GN will automatically convert
1684 sources and include directories to be relative to the build directory.
1685 However, if you're passing files directly in the "args" array or
1686 doing other manual manipulations where GN doesn't know something is
1687 a file name, you will need to convert paths to be relative to what
1688 your tool is expecting.
1689
1690 The common case is to use this to convert paths relative to the
1691 current directory to be relative to the build directory (which will
1692 be the current directory when executing scripts).
1693
1694 If you want to convert a file path to be source-absolute (that is,
1695 beginning with a double slash like "//foo/bar"), you should use
1696 the get_path_info() function. This function won't work because it will
1697 always make relative paths, and it needs to support making paths
1698 relative to the source root, so can't also generate source-absolute
1699 paths without more special-cases.
1700
1701```
1702
1703### **Arguments**:
1704
1705```
1706 input
1707 A string or list of strings representing file or directory names
1708 These can be relative paths ("foo/bar.txt"), system absolute
1709 paths ("/foo/bar.txt"), or source absolute paths
1710 ("//foo/bar.txt").
1711
1712 new_base
1713 The directory to convert the paths to be relative to. This can be
1714 an absolute path or a relative path (which will be treated
1715 as being relative to the current BUILD-file's directory).
1716
1717 As a special case, if new_base is the empty string (the default),
1718 all paths will be converted to system-absolute native style paths
1719 with system path separators. This is useful for invoking external
1720 programs.
1721
1722 current_base
1723 Directory representing the base for relative paths in the input.
1724 If this is not an absolute path, it will be treated as being
1725 relative to the current build file. Use "." (the default) to
1726 convert paths from the current BUILD-file's directory.
1727
1728```
1729
1730### **Return value**
1731
1732```
1733 The return value will be the same type as the input value (either a
1734 string or a list of strings). All relative and source-absolute file
1735 names will be converted to be relative to the requested output
1736 System-absolute paths will be unchanged.
1737
1738```
1739
1740### **Example**
1741
1742```
1743 # Convert a file in the current directory to be relative to the build
1744 # directory (the current dir when executing compilers and scripts).
1745 foo = rebase_path("myfile.txt", root_build_dir)
1746 # might produce "../../project/myfile.txt".
1747
1748 # Convert a file to be system absolute:
1749 foo = rebase_path("myfile.txt")
1750 # Might produce "D:\source\project\myfile.txt" on Windows or
1751 # "/home/you/source/project/myfile.txt" on Linux.
1752
1753 # Typical usage for converting to the build directory for a script.
1754 action("myscript") {
1755 # Don't convert sources, GN will automatically convert these to be
1756 # relative to the build directory when it constructs the command
1757 # line for your script.
1758 sources = [ "foo.txt", "bar.txt" ]
1759
1760 # Extra file args passed manually need to be explicitly converted
1761 # to be relative to the build directory:
1762 args = [
1763 "--data",
1764 rebase_path("//mything/data/input.dat", root_build_dir),
1765 "--rel",
1766 rebase_path("relative_path.txt", root_build_dir)
1767 ] + rebase_path(sources, root_build_dir)
1768 }
1769
1770
1771```
1772## **set_default_toolchain**: Sets the default toolchain name.
1773
1774```
1775 set_default_toolchain(toolchain_label)
1776
1777 The given label should identify a toolchain definition (see
1778 "help toolchain"). This toolchain will be used for all targets
1779 unless otherwise specified.
1780
1781 This function is only valid to call during the processing of the build
1782 configuration file. Since the build configuration file is processed
1783 separately for each toolchain, this function will be a no-op when
1784 called under any non-default toolchains.
1785
1786 For example, the default toolchain should be appropriate for the
1787 current environment. If the current environment is 32-bit and
1788 somebody references a target with a 64-bit toolchain, we wouldn't
1789 want processing of the build config file for the 64-bit toolchain to
1790 reset the default toolchain to 64-bit, we want to keep it 32-bits.
1791
1792```
1793
1794### **Argument**:
1795
1796```
1797 toolchain_label
1798 Toolchain name.
1799
1800```
1801
1802### **Example**:
1803
1804```
1805 set_default_toolchain("//build/config/win:vs32")
1806
1807```
1808## **set_defaults**: Set default values for a target type.
1809
1810```
1811 set_defaults(<target_type_name>) { <values...> }
1812
1813 Sets the default values for a given target type. Whenever
1814 target_type_name is seen in the future, the values specified in
1815 set_default's block will be copied into the current scope.
1816
1817 When the target type is used, the variable copying is very strict.
1818 If a variable with that name is already in scope, the build will fail
1819 with an error.
1820
1821 set_defaults can be used for built-in target types ("executable",
1822 "shared_library", etc.) and custom ones defined via the "template"
1823 command.
1824
1825```
1826
1827### **Example**:
1828```
1829 set_defaults("static_library") {
1830 configs = [ "//tools/mything:settings" ]
1831 }
1832
1833 static_library("mylib")
1834 # The configs will be auto-populated as above. You can remove it if
1835 # you don't want the default for a particular default:
1836 configs -= "//tools/mything:settings"
1837 }
1838
1839
1840```
1841## **set_sources_assignment_filter**: Set a pattern to filter source files.
1842
1843```
1844 The sources assignment filter is a list of patterns that remove files
1845 from the list implicitly whenever the "sources" variable is
1846 assigned to. This is intended to be used to globally filter out files
1847 with platform-specific naming schemes when they don't apply, for
1848 example, you may want to filter out all "*_win.cc" files on non-
1849 Windows platforms.
1850
1851 Typically this will be called once in the master build config script
1852 to set up the filter for the current platform. Subsequent calls will
1853 overwrite the previous values.
1854
1855 If you want to bypass the filter and add a file even if it might
1856 be filtered out, call set_sources_assignment_filter([]) to clear the
1857 list of filters. This will apply until the current scope exits
1858
1859```
1860
1861### **How to use patterns**
1862
1863```
1864 File patterns are VERY limited regular expressions. They must match
1865 the entire input string to be counted as a match. In regular
1866 expression parlance, there is an implicit "^...$" surrounding your
1867 input. If you want to match a substring, you need to use wildcards at
1868 the beginning and end.
1869
1870 There are only two special tokens understood by the pattern matcher.
1871 Everything else is a literal.
1872
1873 * Matches zero or more of any character. It does not depend on the
1874 preceding character (in regular expression parlance it is
1875 equivalent to ".*").
1876
1877 \b Matches a path boundary. This will match the beginning or end of
1878 a string, or a slash.
1879
1880```
1881
1882### **Pattern examples**
1883
1884```
1885 "*asdf*"
1886 Matches a string containing "asdf" anywhere.
1887
1888 "asdf"
1889 Matches only the exact string "asdf".
1890
1891 "*.cc"
1892 Matches strings ending in the literal ".cc".
1893
1894 "\bwin/*"
1895 Matches "win/foo" and "foo/win/bar.cc" but not "iwin/foo".
1896
1897```
1898
1899### **Sources assignment example**
1900
1901```
1902 # Filter out all _win files.
1903 set_sources_assignment_filter([ "*_win.cc", "*_win.h" ])
1904 sources = [ "a.cc", "b_win.cc" ]
1905 print(sources)
1906 # Will print [ "a.cc" ]. b_win one was filtered out.
1907
1908
1909```
1910## **shared_library**: Declare a shared library target.
1911
1912```
1913 A shared library will be specified on the linker line for targets
1914 listing the shared library in its "deps". If you don't want this
1915 (say you dynamically load the library at runtime), then you should
1916 depend on the shared library via "data_deps" instead.
1917
1918```
1919
1920### **Variables**
1921
1922```
1923 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1924 defines, include_dirs, ldflags, lib_dirs, libs
brettw20982dc2015-07-06 22:37:061925 precompiled_header, precompiled_source
dprankee7700dd2015-04-17 22:55:451926 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1927 Dependent configs: all_dependent_configs, public_configs
1928 General: check_includes, configs, data, inputs, output_name,
1929 output_extension, public, sources, testonly, visibility
1930
1931
1932```
1933## **source_set**: Declare a source set target.
1934
1935```
1936 A source set is a collection of sources that get compiled, but are not
1937 linked to produce any kind of library. Instead, the resulting object
1938 files are implicitly added to the linker line of all targets that
1939 depend on the source set.
1940
1941 In most cases, a source set will behave like a static library, except
1942 no actual library file will be produced. This will make the build go
1943 a little faster by skipping creation of a large static library, while
1944 maintaining the organizational benefits of focused build targets.
1945
1946 The main difference between a source set and a static library is
1947 around handling of exported symbols. Most linkers assume declaring
1948 a function exported means exported from the static library. The linker
1949 can then do dead code elimination to delete code not reachable from
1950 exported functions.
1951
1952 A source set will not do this code elimination since there is no link
1953 step. This allows you to link many sources sets into a shared library
1954 and have the "exported symbol" notation indicate "export from the
1955 final shared library and not from the intermediate targets." There is
1956 no way to express this concept when linking multiple static libraries
1957 into a shared library.
1958
1959```
1960
1961### **Variables**
1962
1963```
1964 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1965 defines, include_dirs, ldflags, lib_dirs, libs
brettw20982dc2015-07-06 22:37:061966 precompiled_header, precompiled_source
dprankee7700dd2015-04-17 22:55:451967 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1968 Dependent configs: all_dependent_configs, public_configs
1969 General: check_includes, configs, data, inputs, output_name,
1970 output_extension, public, sources, testonly, visibility
1971
1972
1973```
1974## **static_library**: Declare a static library target.
1975
1976```
1977 Make a ".a" / ".lib" file.
1978
1979 If you only need the static library for intermediate results in the
1980 build, you should consider a source_set instead since it will skip
1981 the (potentially slow) step of creating the intermediate library file.
1982
1983```
1984
1985### **Variables**
1986
1987```
1988 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc,
1989 defines, include_dirs, ldflags, lib_dirs, libs
brettw20982dc2015-07-06 22:37:061990 precompiled_header, precompiled_source
dprankee7700dd2015-04-17 22:55:451991 Deps: data_deps, deps, forward_dependent_configs_from, public_deps
1992 Dependent configs: all_dependent_configs, public_configs
1993 General: check_includes, configs, data, inputs, output_name,
1994 output_extension, public, sources, testonly, visibility
1995
1996
1997```
1998## **template**: Define a template rule.
1999
2000```
2001 A template defines a custom name that acts like a function. It
2002 provides a way to add to the built-in target types.
2003
2004 The template() function is used to declare a template. To invoke the
2005 template, just use the name of the template like any other target
2006 type.
2007
2008 Often you will want to declare your template in a special file that
2009 other files will import (see "gn help import") so your template
2010 rule can be shared across build files.
2011
2012```
2013
2014### **More details**:
2015
2016```
2017 When you call template() it creates a closure around all variables
2018 currently in scope with the code in the template block. When the
2019 template is invoked, the closure will be executed.
2020
2021 When the template is invoked, the code in the caller is executed and
2022 passed to the template code as an implicit "invoker" variable. The
2023 template uses this to read state out of the invoking code.
2024
2025 One thing explicitly excluded from the closure is the "current
2026 directory" against which relative file names are resolved. The
2027 current directory will be that of the invoking code, since typically
2028 that code specifies the file names. This means all files internal
2029 to the template should use absolute names.
2030
2031```
2032
2033### **Target naming**:
2034
2035```
2036 Your template should almost always define a built-in target with the
2037 name the template invoker specified. For example, if you have an IDL
2038 template and somebody does:
2039 idl("foo") {...
2040 you will normally want this to expand to something defining a
2041 source_set or static_library named "foo" (among other things you may
2042 need). This way, when another target specifies a dependency on
2043 "foo", the static_library or source_set will be linked.
2044
2045 It is also important that any other targets your template expands to
2046 have globally unique names, or you will get collisions.
2047
2048 Access the invoking name in your template via the implicit
2049 "target_name" variable. This should also be the basis of how other
2050 targets that a template expands to to ensure uniquness.
2051
2052 A typical example would be a template that defines an action to
2053 generate some source files, and a source_set to compile that source.
2054 Your template would name the source_set "target_name" because
2055 that's what you want external targets to depend on to link your code.
2056 And you would name the action something like "${target_name}_action"
2057 to make it unique. The source set would have a dependency on the
2058 action to make it run.
2059
2060```
2061
2062### **Example of defining a template**:
2063
2064```
2065 template("my_idl") {
2066 # Be nice and help callers debug problems by checking that the
2067 # variables the template requires are defined. This gives a nice
2068 # message rather than giving the user an error about an
2069 # undefined variable in the file defining the template
2070 #
2071 # You can also use defined() to give default values to variables
2072 # unspecified by the invoker.
2073 assert(defined(invoker.sources),
2074 "Need sources in $target_name listing the idl files.")
2075
2076 # Name of the intermediate target that does the code gen. This must
2077 # incorporate the target name so it's unique across template
2078 # instantiations.
2079 code_gen_target_name = target_name + "_code_gen"
2080
2081 # Intermediate target to convert IDL to C source. Note that the name
2082 # is based on the name the invoker of the template specified. This
2083 # way, each time the template is invoked we get a unique
2084 # intermediate action name (since all target names are in the global
2085 # scope).
2086 action_foreach(code_gen_target_name) {
2087 # Access the scope defined by the invoker via the implicit
2088 # "invoker" variable.
2089 sources = invoker.sources
2090
2091 # Note that we need an absolute path for our script file name.
2092 # The current directory when executing this code will be that of
2093 # the invoker (this is why we can use the "sources" directly
2094 # above without having to rebase all of the paths). But if we need
2095 # to reference a script relative to the template file, we'll need
2096 # to use an absolute path instead.
2097 script = "//tools/idl/idl_code_generator.py"
2098
2099 # Tell GN how to expand output names given the sources.
2100 # See "gn help source_expansion" for more.
2101 outputs = [ "$target_gen_dir/{{source_name_part}}.cc",
2102 "$target_gen_dir/{{source_name_part}}.h" ]
2103 }
2104
2105 # Name the source set the same as the template invocation so
2106 # instancing this template produces something that other targets
2107 # can link to in their deps.
2108 source_set(target_name) {
2109 # Generates the list of sources, we get these from the
2110 # action_foreach above.
2111 sources = get_target_outputs(":$code_gen_target_name")
2112
2113 # This target depends on the files produced by the above code gen
2114 # target.
2115 deps = [ ":$code_gen_target_name" ]
2116 }
2117 }
2118
2119```
2120
2121### **Example of invoking the resulting template**:
2122
2123```
2124 # This calls the template code above, defining target_name to be
2125 # "foo_idl_files" and "invoker" to be the set of stuff defined in
2126 # the curly brackets.
2127 my_idl("foo_idl_files") {
2128 # Goes into the template as "invoker.sources".
2129 sources = [ "foo.idl", "bar.idl" ]
2130 }
2131
2132 # Here is a target that depends on our template.
2133 executable("my_exe") {
2134 # Depend on the name we gave the template call above. Internally,
2135 # this will produce a dependency from executable to the source_set
2136 # inside the template (since it has this name), which will in turn
2137 # depend on the code gen action.
2138 deps = [ ":foo_idl_files" ]
2139 }
2140
2141
2142```
2143## **tool**: Specify arguments to a toolchain tool.
2144
2145### **Usage**:
2146
2147```
2148 tool(<tool type>) {
2149 <tool variables...>
2150 }
2151
2152```
2153
2154### **Tool types**
2155
2156```
2157 Compiler tools:
2158 "cc": C compiler
2159 "cxx": C++ compiler
2160 "objc": Objective C compiler
2161 "objcxx": Objective C++ compiler
2162 "rc": Resource compiler (Windows .rc files)
2163 "asm": Assembler
2164
2165 Linker tools:
2166 "alink": Linker for static libraries (archives)
2167 "solink": Linker for shared libraries
2168 "link": Linker for executables
2169
2170 Other tools:
2171 "stamp": Tool for creating stamp files
2172 "copy": Tool to copy files.
2173
2174```
2175
2176### **Tool variables**
2177
2178```
2179 command [string with substitutions]
2180 Valid for: all tools (required)
2181
2182 The command to run.
2183
2184 default_output_extension [string]
2185 Valid for: linker tools
2186
2187 Extension for the main output of a linkable tool. It includes
2188 the leading dot. This will be the default value for the
2189 {{output_extension}} expansion (discussed below) but will be
2190 overridden by by the "output extension" variable in a target,
2191 if one is specified. Empty string means no extension.
2192
2193 GN doesn't actually do anything with this extension other than
2194 pass it along, potentially with target-specific overrides. One
2195 would typically use the {{output_extension}} value in the
2196 "outputs" to read this value.
2197
2198 Example: default_output_extension = ".exe"
2199
2200 depfile [string]
2201 Valid for: compiler tools (optional)
2202
2203 If the tool can write ".d" files, this specifies the name of
2204 the resulting file. These files are used to list header file
2205 dependencies (or other implicit input dependencies) that are
2206 discovered at build time. See also "depsformat".
2207
2208 Example: depfile = "{{output}}.d"
2209
2210 depsformat [string]
2211 Valid for: compiler tools (when depfile is specified)
2212
2213 Format for the deps outputs. This is either "gcc" or "msvc".
2214 See the ninja documentation for "deps" for more information.
2215
2216 Example: depsformat = "gcc"
2217
2218 description [string with substitutions, optional]
2219 Valid for: all tools
2220
2221 What to print when the command is run.
2222
2223 Example: description = "Compiling {{source}}"
2224
2225 lib_switch [string, optional, link tools only]
2226 lib_dir_switch [string, optional, link tools only]
2227 Valid for: Linker tools except "alink"
2228
2229 These strings will be prepended to the libraries and library
2230 search directories, respectively, because linkers differ on how
2231 specify them. If you specified:
2232 lib_switch = "-l"
2233 lib_dir_switch = "-L"
2234 then the "{{libs}}" expansion for [ "freetype", "expat"]
2235 would be "-lfreetype -lexpat".
2236
2237 outputs [list of strings with substitutions]
2238 Valid for: Linker and compiler tools (required)
2239
2240 An array of names for the output files the tool produces. These
2241 are relative to the build output directory. There must always be
2242 at least one output file. There can be more than one output (a
2243 linker might produce a library and an import library, for
2244 example).
2245
2246 This array just declares to GN what files the tool will
2247 produce. It is your responsibility to specify the tool command
2248 that actually produces these files.
2249
2250 If you specify more than one output for shared library links,
2251 you should consider setting link_output and depend_output.
2252 Otherwise, the first entry in the outputs list should always be
2253 the main output which will be linked to.
2254
2255 Example for a compiler tool that produces .obj files:
2256 outputs = [
2257 "{{source_out_dir}}/{{source_name_part}}.obj"
2258 ]
2259
2260 Example for a linker tool that produces a .dll and a .lib. The
2261 use of {{output_extension}} rather than hardcoding ".dll"
2262 allows the extension of the library to be overridden on a
2263 target-by-target basis, but in this example, it always
2264 produces a ".lib" import library:
2265 outputs = [
2266 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2267 "{{root_out_dir}}/{{target_output_name}}.lib",
2268 ]
2269
2270 link_output [string with substitutions]
2271 depend_output [string with substitutions]
2272 Valid for: "solink" only (optional)
2273
2274 These two files specify whch of the outputs from the solink
2275 tool should be used for linking and dependency tracking. These
2276 should match entries in the "outputs". If unspecified, the
2277 first item in the "outputs" array will be used for both. See
2278 "Separate linking and dependencies for shared libraries"
2279 below for more.
2280
2281 On Windows, where the tools produce a .dll shared library and
2282 a .lib import library, you will want both of these to be the
2283 import library. On Linux, if you're not doing the separate
2284 linking/dependency optimization, both of these should be the
2285 .so output.
2286
2287 output_prefix [string]
2288 Valid for: Linker tools (optional)
2289
2290 Prefix to use for the output name. Defaults to empty. This
2291 prefix will be prepended to the name of the target (or the
2292 output_name if one is manually specified for it) if the prefix
2293 is not already there. The result will show up in the
2294 {{output_name}} substitution pattern.
2295
2296 This is typically used to prepend "lib" to libraries on
2297 Posix systems:
2298 output_prefix = "lib"
2299
brettw20982dc2015-07-06 22:37:062300 precompiled_header_type [string]
2301 Valid for: "cc", "cxx", "objc", "objcxx"
2302
2303 Type of precompiled headers. If undefined or the empty string,
2304 precompiled headers will not be used for this tool. Otherwise
2305 use "msvc" which is the only currently supported value.
2306
2307 For precompiled headers to be used for a given target, the
2308 target (or a config applied to it) must also specify a
2309 "precompiled_header" and, for "msvc"-style headers, a
2310 "precompiled_source" value.
2311
2312 See "gn help precompiled_header" for more.
2313
dprankee7700dd2015-04-17 22:55:452314 restat [boolean]
2315 Valid for: all tools (optional, defaults to false)
2316
2317 Requests that Ninja check the file timestamp after this tool has
2318 run to determine if anything changed. Set this if your tool has
2319 the ability to skip writing output if the output file has not
2320 changed.
2321
2322 Normally, Ninja will assume that when a tool runs the output
2323 be new and downstream dependents must be rebuild. When this is
2324 set to trye, Ninja can skip rebuilding downstream dependents for
2325 input changes that don't actually affect the output.
2326
2327 Example:
2328 restat = true
2329
2330 rspfile [string with substitutions]
2331 Valid for: all tools (optional)
2332
2333 Name of the response file. If empty, no response file will be
2334 used. See "rspfile_content".
2335
2336 rspfile_content [string with substitutions]
2337 Valid for: all tools (required when "rspfile" is specified)
2338
2339 The contents to be written to the response file. This may
2340 include all or part of the command to send to the tool which
2341 allows you to get around OS command-line length limits.
2342
2343 This example adds the inputs and libraries to a response file,
2344 but passes the linker flags directly on the command line:
2345 tool("link") {
2346 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
2347 rspfile = "{{output}}.rsp"
2348 rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
2349 }
2350
2351```
2352
2353### **Expansions for tool variables**
brettw20982dc2015-07-06 22:37:062354
dprankee7700dd2015-04-17 22:55:452355```
2356 All paths are relative to the root build directory, which is the
2357 current directory for running all tools. These expansions are
2358 available to all tools:
2359
2360 {{label}}
2361 The label of the current target. This is typically used in the
2362 "description" field for link tools. The toolchain will be
2363 omitted from the label for targets in the default toolchain, and
2364 will be included for targets in other toolchains.
2365
2366 {{output}}
2367 The relative path and name of the output(s) of the current
2368 build step. If there is more than one output, this will expand
2369 to a list of all of them.
2370 Example: "out/base/my_file.o"
2371
2372 {{target_gen_dir}}
2373 {{target_out_dir}}
2374 The directory of the generated file and output directories,
2375 respectively, for the current target. There is no trailing
2376 slash.
2377 Example: "out/base/test"
2378
2379 {{target_output_name}}
2380 The short name of the current target with no path information,
2381 or the value of the "output_name" variable if one is specified
2382 in the target. This will include the "output_prefix" if any.
2383 Example: "libfoo" for the target named "foo" and an
2384 output prefix for the linker tool of "lib".
2385
2386 Compiler tools have the notion of a single input and a single output,
2387 along with a set of compiler-specific flags. The following expansions
2388 are available:
2389
2390 {{cflags}}
2391 {{cflags_c}}
2392 {{cflags_cc}}
2393 {{cflags_objc}}
2394 {{cflags_objcc}}
2395 {{defines}}
2396 {{include_dirs}}
2397 Strings correspond that to the processed flags/defines/include
2398 directories specified for the target.
2399 Example: "--enable-foo --enable-bar"
2400
2401 Defines will be prefixed by "-D" and include directories will
2402 be prefixed by "-I" (these work with Posix tools as well as
2403 Microsoft ones).
2404
2405 {{source}}
2406 The relative path and name of the current input file.
2407 Example: "../../base/my_file.cc"
2408
2409 {{source_file_part}}
2410 The file part of the source including the extension (with no
2411 directory information).
2412 Example: "foo.cc"
2413
2414 {{source_name_part}}
2415 The filename part of the source file with no directory or
2416 extension.
2417 Example: "foo"
2418
2419 {{source_gen_dir}}
2420 {{source_out_dir}}
2421 The directory in the generated file and output directories,
2422 respectively, for the current input file. If the source file
2423 is in the same directory as the target is declared in, they will
2424 will be the same as the "target" versions above.
2425 Example: "gen/base/test"
2426
2427 Linker tools have multiple inputs and (potentially) multiple outputs
2428 The following expansions are available:
2429
2430 {{inputs}}
2431 {{inputs_newline}}
2432 Expands to the inputs to the link step. This will be a list of
2433 object files and static libraries.
2434 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
2435
2436 The "_newline" version will separate the input files with
2437 newlines instead of spaces. This is useful in response files:
2438 some linkers can take a "-filelist" flag which expects newline
2439 separated files, and some Microsoft tools have a fixed-sized
2440 buffer for parsing each line of a response file.
2441
2442 {{ldflags}}
2443 Expands to the processed set of ldflags and library search paths
2444 specified for the target.
2445 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
2446
2447 {{libs}}
2448 Expands to the list of system libraries to link to. Each will
2449 be prefixed by the "lib_prefix".
2450
2451 As a special case to support Mac, libraries with names ending in
2452 ".framework" will be added to the {{libs}} with "-framework"
2453 preceeding it, and the lib prefix will be ignored.
2454
2455 Example: "-lfoo -lbar"
2456
2457 {{output_extension}}
2458 The value of the "output_extension" variable in the target,
2459 or the value of the "default_output_extension" value in the
2460 tool if the target does not specify an output extension.
2461 Example: ".so"
2462
2463 {{solibs}}
2464 Extra libraries from shared library dependencide not specified
2465 in the {{inputs}}. This is the list of link_output files from
2466 shared libraries (if the solink tool specifies a "link_output"
2467 variable separate from the "depend_output").
2468
2469 These should generally be treated the same as libs by your tool.
2470 Example: "libfoo.so libbar.so"
2471
2472 The copy tool allows the common compiler/linker substitutions, plus
2473 {{source}} which is the source of the copy. The stamp tool allows
2474 only the common tool substitutions.
2475
2476```
2477
2478### **Separate linking and dependencies for shared libraries**
2479
2480```
2481 Shared libraries are special in that not all changes to them require
2482 that dependent targets be re-linked. If the shared library is changed
2483 but no imports or exports are different, dependent code needn't be
2484 relinked, which can speed up the build.
2485
2486 If your link step can output a list of exports from a shared library
2487 and writes the file only if the new one is different, the timestamp of
2488 this file can be used for triggering re-links, while the actual shared
2489 library would be used for linking.
2490
2491 You will need to specify
2492 restat = true
2493 in the linker tool to make this work, so Ninja will detect if the
2494 timestamp of the dependency file has changed after linking (otherwise
2495 it will always assume that running a command updates the output):
2496
2497 tool("solink") {
2498 command = "..."
2499 outputs = [
2500 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
2501 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC",
2502 ]
2503 link_output =
2504 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
2505 depend_output =
2506 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}.TOC"
2507 restat = true
2508 }
2509
2510```
2511
2512### **Example**
2513
2514```
2515 toolchain("my_toolchain") {
2516 # Put these at the top to apply to all tools below.
2517 lib_prefix = "-l"
2518 lib_dir_prefix = "-L"
2519
2520 tool("cc") {
2521 command = "gcc {{source}} -o {{output}}"
2522 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2523 description = "GCC {{source}}"
2524 }
2525 tool("cxx") {
2526 command = "g++ {{source}} -o {{output}}"
2527 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
2528 description = "G++ {{source}}"
2529 }
2530 }
2531
2532
2533```
2534## **toolchain**: Defines a toolchain.
2535
2536```
2537 A toolchain is a set of commands and build flags used to compile the
2538 source code. You can have more than one toolchain in use at once in
2539 a build.
2540
2541```
2542
2543### **Functions and variables**
2544
2545```
2546 tool()
2547 The tool() function call specifies the commands commands to run for
2548 a given step. See "gn help tool".
2549
2550 toolchain_args()
2551 List of arguments to pass to the toolchain when invoking this
2552 toolchain. This applies only to non-default toolchains. See
2553 "gn help toolchain_args" for more.
2554
2555 deps
2556 Dependencies of this toolchain. These dependencies will be resolved
2557 before any target in the toolchain is compiled. To avoid circular
2558 dependencies these must be targets defined in another toolchain.
2559
2560 This is expressed as a list of targets, and generally these targets
2561 will always specify a toolchain:
2562 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
2563
2564 This concept is somewhat inefficient to express in Ninja (it
2565 requires a lot of duplicate of rules) so should only be used when
2566 absolutely necessary.
2567
2568 concurrent_links
2569 In integer expressing the number of links that Ninja will perform in
2570 parallel. GN will create a pool for shared library and executable
2571 link steps with this many processes. Since linking is memory- and
2572 I/O-intensive, projects with many large targets may want to limit
2573 the number of parallel steps to avoid overloading the computer.
2574 Since creating static libraries is generally not as intensive
2575 there is no limit to "alink" steps.
2576
2577 Defaults to 0 which Ninja interprets as "no limit".
2578
2579 The value used will be the one from the default toolchain of the
2580 current build.
2581
2582```
2583
2584### **Invoking targets in toolchains**:
2585
2586```
2587 By default, when a target depends on another, there is an implicit
2588 toolchain label that is inherited, so the dependee has the same one
2589 as the dependent.
2590
2591 You can override this and refer to any other toolchain by explicitly
2592 labeling the toolchain to use. For example:
2593 data_deps = [ "//plugins:mine(//toolchains:plugin_toolchain)" ]
2594 The string "//build/toolchains:plugin_toolchain" is a label that
2595 identifies the toolchain declaration for compiling the sources.
2596
2597 To load a file in an alternate toolchain, GN does the following:
2598
2599 1. Loads the file with the toolchain definition in it (as determined
2600 by the toolchain label).
2601 2. Re-runs the master build configuration file, applying the
2602 arguments specified by the toolchain_args section of the toolchain
2603 definition (see "gn help toolchain_args").
2604 3. Loads the destination build file in the context of the
2605 configuration file in the previous step.
2606
2607```
2608
2609### **Example**:
2610```
2611 toolchain("plugin_toolchain") {
2612 concurrent_links = 8
2613
2614 tool("cc") {
2615 command = "gcc {{source}}"
2616 ...
2617 }
2618
2619 toolchain_args() {
2620 is_plugin = true
2621 is_32bit = true
2622 is_64bit = false
2623 }
2624 }
2625
2626
2627```
2628## **toolchain_args**: Set build arguments for toolchain build setup.
2629
2630```
2631 Used inside a toolchain definition to pass arguments to an alternate
2632 toolchain's invocation of the build.
2633
2634 When you specify a target using an alternate toolchain, the master
2635 build configuration file is re-interpreted in the context of that
2636 toolchain (see "gn help toolchain"). The toolchain_args function
2637 allows you to control the arguments passed into this alternate
2638 invocation of the build.
2639
2640 Any default system arguments or arguments passed in on the command-
2641 line will also be passed to the alternate invocation unless explicitly
2642 overridden by toolchain_args.
2643
2644 The toolchain_args will be ignored when the toolchain being defined
2645 is the default. In this case, it's expected you want the default
2646 argument values.
2647
2648 See also "gn help buildargs" for an overview of these arguments.
2649
2650```
2651
2652### **Example**:
2653```
2654 toolchain("my_weird_toolchain") {
2655 ...
2656 toolchain_args() {
2657 # Override the system values for a generic Posix system.
2658 is_win = false
2659 is_posix = true
2660
2661 # Pass this new value for specific setup for my toolchain.
2662 is_my_weird_system = true
2663 }
2664 }
2665
2666
2667```
2668## **write_file**: Write a file to disk.
2669
2670```
2671 write_file(filename, data)
2672
2673 If data is a list, the list will be written one-item-per-line with no
2674 quoting or brackets.
2675
2676 If the file exists and the contents are identical to that being
2677 written, the file will not be updated. This will prevent unnecessary
2678 rebuilds of targets that depend on this file.
2679
2680 TODO(brettw) we probably need an optional third argument to control
2681 list formatting.
2682
2683```
2684
2685### **Arguments**:
2686
2687```
2688 filename
2689 Filename to write. This must be within the output directory.
2690
2691 data:
2692 The list or string to write.
2693
2694
2695```
2696## **current_cpu**: The processor architecture of the current toolchain.
2697
2698```
2699 The build configuration usually sets this value based on the value
2700 of "host_cpu" (see "gn help host_cpu") and then threads
2701 this through the toolchain definitions to ensure that it always
2702 reflects the appropriate value.
2703
2704 This value is not used internally by GN for any purpose. It is
2705 set it to the empty string ("") by default but is declared so
2706 that it can be overridden on the command line if so desired.
2707
2708 See "gn help target_cpu" for a list of common values returned.
2709
2710
2711```
2712## **current_os**: The operating system of the current toolchain.
2713
2714```
2715 The build configuration usually sets this value based on the value
2716 of "target_os" (see "gn help target_os"), and then threads this
2717 through the toolchain definitions to ensure that it always reflects
2718 the appropriate value.
2719
2720 This value is not used internally by GN for any purpose. It is
2721 set it to the empty string ("") by default but is declared so
2722 that it can be overridden on the command line if so desired.
2723
2724 See "gn help target_os" for a list of common values returned.
2725
2726
2727```
2728## **current_toolchain**: Label of the current toolchain.
2729
2730```
2731 A fully-qualified label representing the current toolchain. You can
2732 use this to make toolchain-related decisions in the build. See also
2733 "default_toolchain".
2734
2735```
2736
2737### **Example**:
2738
2739```
2740 if (current_toolchain == "//build:64_bit_toolchain") {
2741 executable("output_thats_64_bit_only") {
2742 ...
2743
2744
2745```
2746## **default_toolchain**: [string] Label of the default toolchain.
2747
2748```
2749 A fully-qualified label representing the default toolchain, which may
2750 not necessarily be the current one (see "current_toolchain").
2751
2752
2753```
2754## **host_cpu**: The processor architecture that GN is running on.
2755
2756```
2757 This is value is exposed so that cross-compile toolchains can
2758 access the host architecture when needed.
2759
2760 The value should generally be considered read-only, but it can be
2761 overriden in order to handle unusual cases where there might
2762 be multiple plausible values for the host architecture (e.g., if
2763 you can do either 32-bit or 64-bit builds). The value is not used
2764 internally by GN for any purpose.
2765
2766```
2767
2768### **Some possible values**:
2769```
2770 - "x64"
2771 - "x86"
2772
2773
2774```
2775## **host_os**: [string] The operating system that GN is running on.
2776
2777```
2778 This value is exposed so that cross-compiles can access the host
2779 build system's settings.
2780
2781 This value should generally be treated as read-only. It, however,
2782 is not used internally by GN for any purpose.
2783
2784```
2785
2786### **Some possible values**:
2787```
2788 - "linux"
2789 - "mac"
2790 - "win"
2791
2792
2793```
2794## **python_path**: Absolute path of Python.
2795
2796```
2797 Normally used in toolchain definitions if running some command
2798 requires Python. You will normally not need this when invoking scripts
2799 since GN automatically finds it for you.
2800
2801
2802```
2803## **root_build_dir**: [string] Directory where build commands are run.
2804
2805```
2806 This is the root build output directory which will be the current
2807 directory when executing all compilers and scripts.
2808
2809 Most often this is used with rebase_path (see "gn help rebase_path")
2810 to convert arguments to be relative to a script's current directory.
2811
2812
2813```
2814## **root_gen_dir**: Directory for the toolchain's generated files.
2815
2816```
2817 Absolute path to the root of the generated output directory tree for
2818 the current toolchain. An example would be "//out/Debug/gen" for the
2819 default toolchain, or "//out/Debug/arm/gen" for the "arm"
2820 toolchain.
2821
2822 This is primarily useful for setting up include paths for generated
2823 files. If you are passing this to a script, you will want to pass it
2824 through rebase_path() (see "gn help rebase_path") to convert it
2825 to be relative to the build directory.
2826
2827 See also "target_gen_dir" which is usually a better location for
2828 generated files. It will be inside the root generated dir.
2829
2830
2831```
2832## **root_out_dir**: [string] Root directory for toolchain output files.
2833
2834```
2835 Absolute path to the root of the output directory tree for the current
2836 toolchain. It will not have a trailing slash.
2837
2838 For the default toolchain this will be the same as the root_build_dir.
2839 An example would be "//out/Debug" for the default toolchain, or
2840 "//out/Debug/arm" for the "arm" toolchain.
2841
2842 This is primarily useful for setting up script calls. If you are
2843 passing this to a script, you will want to pass it through
2844 rebase_path() (see "gn help rebase_path") to convert it
2845 to be relative to the build directory.
2846
2847 See also "target_out_dir" which is usually a better location for
2848 output files. It will be inside the root output dir.
2849
2850```
2851
2852### **Example**:
2853
2854```
2855 action("myscript") {
2856 # Pass the output dir to the script.
2857 args = [ "-o", rebase_path(root_out_dir, root_build_dir) ]
2858 }
2859
2860
2861```
2862## **target_cpu**: The desired cpu architecture for the build.
2863
2864```
2865 This value should be used to indicate the desired architecture for
2866 the primary objects of the build. It will match the cpu architecture
2867 of the default toolchain.
2868
2869 In many cases, this is the same as "host_cpu", but in the case
2870 of cross-compiles, this can be set to something different. This
2871 value is different from "current_cpu" in that it can be referenced
2872 from inside any toolchain. This value can also be ignored if it is
2873 not needed or meaningful for a project.
2874
2875 This value is not used internally by GN for any purpose, so it
2876 may be set to whatever value is needed for the build.
2877 GN defaults this value to the empty string ("") and the
2878 configuration files should set it to an appropriate value
2879 (e.g., setting it to the value of "host_cpu") if it is not
2880 overridden on the command line or in the args.gn file.
2881
2882 Where practical, use one of the following list of common values:
2883
2884```
2885
2886### **Possible values**:
2887```
2888 - "x86"
2889 - "x64"
2890 - "arm"
2891 - "arm64"
2892 - "mipsel"
2893
2894
2895```
2896## **target_gen_dir**: Directory for a target's generated files.
2897
2898```
2899 Absolute path to the target's generated file directory. This will be
2900 the "root_gen_dir" followed by the relative path to the current
2901 build file. If your file is in "//tools/doom_melon" then
2902 target_gen_dir would be "//out/Debug/gen/tools/doom_melon". It will
2903 not have a trailing slash.
2904
2905 This is primarily useful for setting up include paths for generated
2906 files. If you are passing this to a script, you will want to pass it
2907 through rebase_path() (see "gn help rebase_path") to convert it
2908 to be relative to the build directory.
2909
2910 See also "gn help root_gen_dir".
2911
2912```
2913
2914### **Example**:
2915
2916```
2917 action("myscript") {
2918 # Pass the generated output dir to the script.
2919 args = [ "-o", rebase_path(target_gen_dir, root_build_dir) ]
2920 }
2921
2922
2923```
2924## **target_os**: The desired operating system for the build.
2925
2926```
2927 This value should be used to indicate the desired operating system
2928 for the primary object(s) of the build. It will match the OS of
2929 the default toolchain.
2930
2931 In many cases, this is the same as "host_os", but in the case of
2932 cross-compiles, it may be different. This variable differs from
2933 "current_os" in that it can be referenced from inside any
2934 toolchain and will always return the initial value.
2935
2936 This should be set to the most specific value possible. So,
2937 "android" or "chromeos" should be used instead of "linux"
2938 where applicable, even though Android and ChromeOS are both Linux
2939 variants. This can mean that one needs to write
2940
2941 if (target_os == "android" || target_os == "linux") {
2942 # ...
2943 }
2944
2945 and so forth.
2946
2947 This value is not used internally by GN for any purpose, so it
2948 may be set to whatever value is needed for the build.
2949 GN defaults this value to the empty string ("") and the
2950 configuration files should set it to an appropriate value
2951 (e.g., setting it to the value of "host_os") if it is not
2952 set via the command line or in the args.gn file.
2953
2954 Where practical, use one of the following list of common values:
2955
2956```
2957
2958### **Possible values**:
2959```
2960 - "android"
2961 - "chromeos"
2962 - "ios"
2963 - "linux"
2964 - "nacl"
2965 - "mac"
2966 - "win"
2967
2968
2969```
2970## **target_out_dir**: [string] Directory for target output files.
2971
2972```
2973 Absolute path to the target's generated file directory. If your
2974 current target is in "//tools/doom_melon" then this value might be
2975 "//out/Debug/obj/tools/doom_melon". It will not have a trailing
2976 slash.
2977
2978 This is primarily useful for setting up arguments for calling
2979 scripts. If you are passing this to a script, you will want to pass it
2980 through rebase_path() (see "gn help rebase_path") to convert it
2981 to be relative to the build directory.
2982
2983 See also "gn help root_out_dir".
2984
2985```
2986
2987### **Example**:
2988
2989```
2990 action("myscript") {
2991 # Pass the output dir to the script.
2992 args = [ "-o", rebase_path(target_out_dir, root_build_dir) ]
2993 }
2994
2995
2996```
2997## **all_dependent_configs**: Configs to be forced on dependents.
2998
2999```
3000 A list of config labels.
3001
3002 All targets depending on this one, and recursively, all targets
3003 depending on those, will have the configs listed in this variable
3004 added to them. These configs will also apply to the current target.
3005
3006 This addition happens in a second phase once a target and all of its
3007 dependencies have been resolved. Therefore, a target will not see
3008 these force-added configs in their "configs" variable while the
3009 script is running, and then can not be removed. As a result, this
3010 capability should generally only be used to add defines and include
3011 directories necessary to compile a target's headers.
3012
3013 See also "public_configs".
3014
3015```
3016
3017### **Ordering of flags and values**:
3018
3019```
3020 1. Those set on the current target (not in a config).
3021 2. Those set on the "configs" on the target in order that the
3022 configs appear in the list.
3023 3. Those set on the "all_dependent_configs" on the target in order
3024 that the configs appear in the list.
3025 4. Those set on the "public_configs" on the target in order that
3026 those configs appear in the list.
3027 5. all_dependent_configs pulled from dependencies, in the order of
3028 the "deps" list. This is done recursively. If a config appears
3029 more than once, only the first occurance will be used.
3030 6. public_configs pulled from dependencies, in the order of the
3031 "deps" list. If a dependency is public, they will be applied
3032 recursively.
3033
3034
3035```
3036## **allow_circular_includes_from**: Permit includes from deps.
3037
3038```
3039 A list of target labels. Must be a subset of the target's "deps".
3040 These targets will be permitted to include headers from the current
3041 target despite the dependency going in the opposite direction.
3042
3043```
3044
3045### **Tedious exposition**
3046
3047```
3048 Normally, for a file in target A to include a file from target B,
3049 A must list B as a dependency. This invariant is enforced by the
3050 "gn check" command (and the --check flag to "gn gen").
3051
3052 Sometimes, two targets might be the same unit for linking purposes
3053 (two source sets or static libraries that would always be linked
3054 together in a final executable or shared library). In this case,
3055 you want A to be able to include B's headers, and B to include A's
3056 headers.
3057
3058 This list, if specified, lists which of the dependencies of the
3059 current target can include header files from the current target.
3060 That is, if A depends on B, B can only include headers from A if it is
3061 in A's allow_circular_includes_from list.
3062
3063```
3064
3065### **Example**
3066
3067```
3068 source_set("a") {
3069 deps = [ ":b", ":c" ]
3070 allow_circular_includes_from = [ ":b" ]
3071 ...
3072 }
3073
3074
3075```
3076## **args**: Arguments passed to an action.
3077
3078```
3079 For action and action_foreach targets, args is the list of arguments
3080 to pass to the script. Typically you would use source expansion (see
3081 "gn help source_expansion") to insert the source file names.
3082
3083 See also "gn help action" and "gn help action_foreach".
3084
3085
3086```
3087## **cflags***: Flags passed to the C compiler.
3088
3089```
3090 A list of strings.
3091
3092 "cflags" are passed to all invocations of the C, C++, Objective C,
3093 and Objective C++ compilers.
3094
3095 To target one of these variants individually, use "cflags_c",
3096 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3097 These variant-specific versions will be appended to the "cflags".
3098
3099```
3100
3101### **Ordering of flags and values**:
3102
3103```
3104 1. Those set on the current target (not in a config).
3105 2. Those set on the "configs" on the target in order that the
3106 configs appear in the list.
3107 3. Those set on the "all_dependent_configs" on the target in order
3108 that the configs appear in the list.
3109 4. Those set on the "public_configs" on the target in order that
3110 those configs appear in the list.
3111 5. all_dependent_configs pulled from dependencies, in the order of
3112 the "deps" list. This is done recursively. If a config appears
3113 more than once, only the first occurance will be used.
3114 6. public_configs pulled from dependencies, in the order of the
3115 "deps" list. If a dependency is public, they will be applied
3116 recursively.
3117
3118
3119```
3120## **cflags***: Flags passed to the C compiler.
3121
3122```
3123 A list of strings.
3124
3125 "cflags" are passed to all invocations of the C, C++, Objective C,
3126 and Objective C++ compilers.
3127
3128 To target one of these variants individually, use "cflags_c",
3129 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3130 These variant-specific versions will be appended to the "cflags".
3131
3132```
3133
3134### **Ordering of flags and values**:
3135
3136```
3137 1. Those set on the current target (not in a config).
3138 2. Those set on the "configs" on the target in order that the
3139 configs appear in the list.
3140 3. Those set on the "all_dependent_configs" on the target in order
3141 that the configs appear in the list.
3142 4. Those set on the "public_configs" on the target in order that
3143 those configs appear in the list.
3144 5. all_dependent_configs pulled from dependencies, in the order of
3145 the "deps" list. This is done recursively. If a config appears
3146 more than once, only the first occurance will be used.
3147 6. public_configs pulled from dependencies, in the order of the
3148 "deps" list. If a dependency is public, they will be applied
3149 recursively.
3150
3151
3152```
3153## **cflags***: Flags passed to the C compiler.
3154
3155```
3156 A list of strings.
3157
3158 "cflags" are passed to all invocations of the C, C++, Objective C,
3159 and Objective C++ compilers.
3160
3161 To target one of these variants individually, use "cflags_c",
3162 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3163 These variant-specific versions will be appended to the "cflags".
3164
3165```
3166
3167### **Ordering of flags and values**:
3168
3169```
3170 1. Those set on the current target (not in a config).
3171 2. Those set on the "configs" on the target in order that the
3172 configs appear in the list.
3173 3. Those set on the "all_dependent_configs" on the target in order
3174 that the configs appear in the list.
3175 4. Those set on the "public_configs" on the target in order that
3176 those configs appear in the list.
3177 5. all_dependent_configs pulled from dependencies, in the order of
3178 the "deps" list. This is done recursively. If a config appears
3179 more than once, only the first occurance will be used.
3180 6. public_configs pulled from dependencies, in the order of the
3181 "deps" list. If a dependency is public, they will be applied
3182 recursively.
3183
3184
3185```
3186## **cflags***: Flags passed to the C compiler.
3187
3188```
3189 A list of strings.
3190
3191 "cflags" are passed to all invocations of the C, C++, Objective C,
3192 and Objective C++ compilers.
3193
3194 To target one of these variants individually, use "cflags_c",
3195 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3196 These variant-specific versions will be appended to the "cflags".
3197
3198```
3199
3200### **Ordering of flags and values**:
3201
3202```
3203 1. Those set on the current target (not in a config).
3204 2. Those set on the "configs" on the target in order that the
3205 configs appear in the list.
3206 3. Those set on the "all_dependent_configs" on the target in order
3207 that the configs appear in the list.
3208 4. Those set on the "public_configs" on the target in order that
3209 those configs appear in the list.
3210 5. all_dependent_configs pulled from dependencies, in the order of
3211 the "deps" list. This is done recursively. If a config appears
3212 more than once, only the first occurance will be used.
3213 6. public_configs pulled from dependencies, in the order of the
3214 "deps" list. If a dependency is public, they will be applied
3215 recursively.
3216
3217
3218```
3219## **cflags***: Flags passed to the C compiler.
3220
3221```
3222 A list of strings.
3223
3224 "cflags" are passed to all invocations of the C, C++, Objective C,
3225 and Objective C++ compilers.
3226
3227 To target one of these variants individually, use "cflags_c",
3228 "cflags_cc", "cflags_objc", and "cflags_objcc", respectively.
3229 These variant-specific versions will be appended to the "cflags".
3230
3231```
3232
3233### **Ordering of flags and values**:
3234
3235```
3236 1. Those set on the current target (not in a config).
3237 2. Those set on the "configs" on the target in order that the
3238 configs appear in the list.
3239 3. Those set on the "all_dependent_configs" on the target in order
3240 that the configs appear in the list.
3241 4. Those set on the "public_configs" on the target in order that
3242 those configs appear in the list.
3243 5. all_dependent_configs pulled from dependencies, in the order of
3244 the "deps" list. This is done recursively. If a config appears
3245 more than once, only the first occurance will be used.
3246 6. public_configs pulled from dependencies, in the order of the
3247 "deps" list. If a dependency is public, they will be applied
3248 recursively.
3249
3250
3251```
3252## **check_includes**: [boolean] Controls whether a target's files are checked.
3253
3254```
3255 When true (the default), the "gn check" command (as well as
3256 "gn gen" with the --check flag) will check this target's sources
3257 and headers for proper dependencies.
3258
3259 When false, the files in this target will be skipped by default.
3260 This does not affect other targets that depend on the current target,
3261 it just skips checking the includes of the current target's files.
3262
3263```
3264
brettw20982dc2015-07-06 22:37:063265### **Controlling includes individually**
3266
3267```
3268 If only certain includes are problematic, you can annotate them
3269 individually rather than disabling header checking on an entire
3270 target. Add the string "nogncheck" to the include line:
3271
3272 #include "foo/something_weird.h" // nogncheck (bug 12345)
3273
3274 It is good form to include a reference to a bug (if the include is
3275 improper, or some other comment expressing why the header checker
3276 doesn't work for this particular case.
3277
3278 The most common reason to need "nogncheck" is conditional includes.
3279 The header checker does not understand the preprocessor, so may flag
3280 some includes as improper even if the dependencies and #defines are
3281 always matched correctly:
3282
3283 #if defined(ENABLE_DOOM_MELON)
3284 #include "doom_melon/beam_controller.h" // nogncheck
3285 #endif
3286
3287```
3288
dprankee7700dd2015-04-17 22:55:453289### **Example**
3290
3291```
3292 source_set("busted_includes") {
3293 # This target's includes are messed up, exclude it from checking.
3294 check_includes = false
3295 ...
3296 }
3297
3298
3299```
3300## **complete_static_lib**: [boolean] Links all deps into a static library.
3301
3302```
3303 A static library normally doesn't include code from dependencies, but
3304 instead forwards the static libraries and source sets in its deps up
3305 the dependency chain until a linkable target (an executable or shared
3306 library) is reached. The final linkable target only links each static
3307 library once, even if it appears more than once in its dependency
3308 graph.
3309
3310 In some cases the static library might be the final desired output.
3311 For example, you may be producing a static library for distribution to
3312 third parties. In this case, the static library should include code
3313 for all dependencies in one complete package. Since GN does not unpack
3314 static libraries to forward their contents up the dependency chain,
3315 it is an error for complete static libraries to depend on other static
3316 libraries.
3317
3318```
3319
3320### **Example**
3321
3322```
3323 static_library("foo") {
3324 complete_static_lib = true
3325 deps = [ "bar" ]
3326 }
3327
3328
3329```
3330## **configs**: Configs applying to this target.
3331
3332```
3333 A list of config labels.
3334
3335 The include_dirs, defines, etc. in each config are appended in the
3336 order they appear to the compile command for each file in the target.
3337 They will appear after the include_dirs, defines, etc. that the target
3338 sets directly.
3339
3340 The build configuration script will generally set up the default
3341 configs applying to a given target type (see "set_defaults").
3342 When a target is being defined, it can add to or remove from this
3343 list.
3344
3345```
3346
3347### **Ordering of flags and values**:
3348
3349```
3350 1. Those set on the current target (not in a config).
3351 2. Those set on the "configs" on the target in order that the
3352 configs appear in the list.
3353 3. Those set on the "all_dependent_configs" on the target in order
3354 that the configs appear in the list.
3355 4. Those set on the "public_configs" on the target in order that
3356 those configs appear in the list.
3357 5. all_dependent_configs pulled from dependencies, in the order of
3358 the "deps" list. This is done recursively. If a config appears
3359 more than once, only the first occurance will be used.
3360 6. public_configs pulled from dependencies, in the order of the
3361 "deps" list. If a dependency is public, they will be applied
3362 recursively.
3363
3364```
3365
3366### **Example**:
3367```
3368 static_library("foo") {
3369 configs -= "//build:no_rtti" # Don't use the default RTTI config.
3370 configs += ":mysettings" # Add some of our own settings.
3371 }
3372
3373
3374```
3375## **data**: Runtime data file dependencies.
3376
3377```
dpranke5316b8c2015-06-09 16:03:263378 Lists files or directories required to run the given target. These are
3379 typically data files or directories of data files. The paths are
3380 interpreted as being relative to the current build file. Since these
3381 are runtime dependencies, they do not affect which targets are built
3382 or when. To declare input files to a script, use "inputs".
dprankee7700dd2015-04-17 22:55:453383
3384 Appearing in the "data" section does not imply any special handling
3385 such as copying them to the output directory. This is just used for
dpranke5316b8c2015-06-09 16:03:263386 declaring runtime dependencies. Runtime dependencies can be queried
3387 using the "runtime_deps" category of "gn desc" or written during
3388 build generation via "--runtime-deps-list-file".
dprankee7700dd2015-04-17 22:55:453389
dpranke5316b8c2015-06-09 16:03:263390 GN doesn't require data files to exist at build-time. So actions that
3391 produce files that are in turn runtime dependencies can list those
3392 generated files both in the "outputs" list as well as the "data"
3393 list.
3394
3395 By convention, directories are be listed with a trailing slash:
3396 data = [ "test/data/" ]
3397 However, no verification is done on these so GN doesn't enforce this.
3398 The paths are just rebased and passed along when requested.
3399
3400 See "gn help runtime_deps" for how these are used.
dprankee7700dd2015-04-17 22:55:453401
3402
3403```
3404## **data_deps**: Non-linked dependencies.
3405
3406```
3407 A list of target labels.
3408
3409 Specifies dependencies of a target that are not actually linked into
3410 the current target. Such dependencies will built and will be available
3411 at runtime.
3412
3413 This is normally used for things like plugins or helper programs that
3414 a target needs at runtime.
3415
3416 See also "gn help deps" and "gn help data".
3417
3418```
3419
3420### **Example**:
3421```
3422 executable("foo") {
3423 deps = [ "//base" ]
3424 data_deps = [ "//plugins:my_runtime_plugin" ]
3425 }
3426
3427
3428```
3429## **defines**: C preprocessor defines.
3430
3431```
3432 A list of strings
3433
3434 These strings will be passed to the C/C++ compiler as #defines. The
3435 strings may or may not include an "=" to assign a value.
3436
3437```
3438
3439### **Ordering of flags and values**:
3440
3441```
3442 1. Those set on the current target (not in a config).
3443 2. Those set on the "configs" on the target in order that the
3444 configs appear in the list.
3445 3. Those set on the "all_dependent_configs" on the target in order
3446 that the configs appear in the list.
3447 4. Those set on the "public_configs" on the target in order that
3448 those configs appear in the list.
3449 5. all_dependent_configs pulled from dependencies, in the order of
3450 the "deps" list. This is done recursively. If a config appears
3451 more than once, only the first occurance will be used.
3452 6. public_configs pulled from dependencies, in the order of the
3453 "deps" list. If a dependency is public, they will be applied
3454 recursively.
3455
3456```
3457
3458### **Example**:
3459```
3460 defines = [ "AWESOME_FEATURE", "LOG_LEVEL=3" ]
3461
3462
3463```
3464## **depfile**: [string] File name for input dependencies for actions.
3465
3466```
3467 If nonempty, this string specifies that the current action or
3468 action_foreach target will generate the given ".d" file containing
3469 the dependencies of the input. Empty or unset means that the script
3470 doesn't generate the files.
3471
3472 The .d file should go in the target output directory. If you have more
3473 than one source file that the script is being run over, you can use
3474 the output file expansions described in "gn help action_foreach" to
3475 name the .d file according to the input.
3476 The format is that of a Makefile, and all of the paths should be
3477 relative to the root build directory.
3478
3479```
3480
3481### **Example**:
3482```
3483 action_foreach("myscript_target") {
3484 script = "myscript.py"
3485 sources = [ ... ]
3486
3487 # Locate the depfile in the output directory named like the
3488 # inputs but with a ".d" appended.
3489 depfile = "$relative_target_output_dir/{{source_name}}.d"
3490
3491 # Say our script uses "-o <d file>" to indicate the depfile.
3492 args = [ "{{source}}", "-o", depfile ]
3493 }
3494
3495
3496```
3497## **deps**: Private linked dependencies.
3498
3499```
3500 A list of target labels.
3501
3502 Specifies private dependencies of a target. Shared and dynamic
3503 libraries will be linked into the current target. Other target types
3504 that can't be linked (like actions and groups) listed in "deps" will
3505 be treated as "data_deps". Likewise, if the current target isn't
3506 linkable, then all deps will be treated as "data_deps".
3507
3508 These dependencies are private in that it does not grant dependent
3509 targets the ability to include headers from the dependency, and direct
3510 dependent configs are not forwarded.
3511
3512 See also "public_deps" and "data_deps".
3513
3514
3515```
3516## **forward_dependent_configs_from**
3517
3518```
3519 A list of target labels.
3520
3521 DEPRECATED. Use public_deps instead which will have the same effect.
3522
3523 Exposes the public_configs from a private dependent target as
3524 public_configs of the current one. Each label in this list
3525 must also be in the deps.
3526
3527 Generally you should use public_deps instead of this variable to
3528 express the concept of exposing a dependency as part of a target's
3529 public API. We're considering removing this variable.
3530
3531```
3532
3533### **Discussion**
3534
3535```
3536 Sometimes you depend on a child library that exports some necessary
3537 configuration via public_configs. If your target in turn exposes the
3538 child library's headers in its public headers, it might mean that
3539 targets that depend on you won't work: they'll be seeing the child
3540 library's code but not the necessary configuration. This list
3541 specifies which of your deps' direct dependent configs to expose as
3542 your own.
3543
3544```
3545
3546### **Examples**
3547
3548```
3549 If we use a given library "a" from our public headers:
3550
3551 deps = [ ":a", ":b", ... ]
3552 forward_dependent_configs_from = [ ":a" ]
3553
3554 This example makes a "transparent" target that forwards a dependency
3555 to another:
3556
3557 group("frob") {
3558 if (use_system_frob) {
3559 deps = ":system_frob"
3560 } else {
3561 deps = "//third_party/fallback_frob"
3562 }
3563 forward_dependent_configs_from = deps
3564 }
3565
3566
3567```
3568## **include_dirs**: Additional include directories.
3569
3570```
3571 A list of source directories.
3572
3573 The directories in this list will be added to the include path for
3574 the files in the affected target.
3575
3576```
3577
3578### **Ordering of flags and values**:
3579
3580```
3581 1. Those set on the current target (not in a config).
3582 2. Those set on the "configs" on the target in order that the
3583 configs appear in the list.
3584 3. Those set on the "all_dependent_configs" on the target in order
3585 that the configs appear in the list.
3586 4. Those set on the "public_configs" on the target in order that
3587 those configs appear in the list.
3588 5. all_dependent_configs pulled from dependencies, in the order of
3589 the "deps" list. This is done recursively. If a config appears
3590 more than once, only the first occurance will be used.
3591 6. public_configs pulled from dependencies, in the order of the
3592 "deps" list. If a dependency is public, they will be applied
3593 recursively.
3594
3595```
3596
3597### **Example**:
3598```
3599 include_dirs = [ "src/include", "//third_party/foo" ]
3600
3601
3602```
3603## **inputs**: Additional compile-time dependencies.
3604
3605```
3606 Inputs are compile-time dependencies of the current target. This means
3607 that all inputs must be available before compiling any of the sources
3608 or executing any actions.
3609
3610 Inputs are typically only used for action and action_foreach targets.
3611
3612```
3613
3614### **Inputs for actions**
3615
3616```
3617 For action and action_foreach targets, inputs should be the inputs to
3618 script that don't vary. These should be all .py files that the script
3619 uses via imports (the main script itself will be an implcit dependency
3620 of the action so need not be listed).
3621
3622 For action targets, inputs should be the entire set of inputs the
3623 script needs. For action_foreach targets, inputs should be the set of
3624 dependencies that don't change. These will be applied to each script
3625 invocation over the sources.
3626
3627 Note that another way to declare input dependencies from an action
3628 is to have the action write a depfile (see "gn help depfile"). This
3629 allows the script to dynamically write input dependencies, that might
3630 not be known until actually executing the script. This is more
3631 efficient than doing processing while running GN to determine the
3632 inputs, and is easier to keep in-sync than hardcoding the list.
3633
3634```
3635
3636### **Inputs for binary targets**
3637
3638```
3639 Any input dependencies will be resolved before compiling any sources.
3640 Normally, all actions that a target depends on will be run before any
3641 files in a target are compiled. So if you depend on generated headers,
3642 you do not typically need to list them in the inputs section.
3643
3644```
3645
3646### **Example**
3647
3648```
3649 action("myscript") {
3650 script = "domything.py"
3651 inputs = [ "input.data" ]
3652 }
3653
3654
3655```
3656## **ldflags**: Flags passed to the linker.
3657
3658```
3659 A list of strings.
3660
3661 These flags are passed on the command-line to the linker and generally
3662 specify various linking options. Most targets will not need these and
3663 will use "libs" and "lib_dirs" instead.
3664
3665 ldflags are NOT pushed to dependents, so applying ldflags to source
3666 sets or static libraries will be a no-op. If you want to apply ldflags
3667 to dependent targets, put them in a config and set it in the
3668 all_dependent_configs or public_configs.
3669
3670
3671```
3672## **lib_dirs**: Additional library directories.
3673
3674```
3675 A list of directories.
3676
3677 Specifies additional directories passed to the linker for searching
3678 for the required libraries. If an item is not an absolute path, it
3679 will be treated as being relative to the current build file.
3680
3681 libs and lib_dirs work differently than other flags in two respects.
3682 First, then are inherited across static library boundaries until a
3683 shared library or executable target is reached. Second, they are
3684 uniquified so each one is only passed once (the first instance of it
3685 will be the one used).
3686
3687```
3688
3689### **Ordering of flags and values**:
3690
3691```
3692 1. Those set on the current target (not in a config).
3693 2. Those set on the "configs" on the target in order that the
3694 configs appear in the list.
3695 3. Those set on the "all_dependent_configs" on the target in order
3696 that the configs appear in the list.
3697 4. Those set on the "public_configs" on the target in order that
3698 those configs appear in the list.
3699 5. all_dependent_configs pulled from dependencies, in the order of
3700 the "deps" list. This is done recursively. If a config appears
3701 more than once, only the first occurance will be used.
3702 6. public_configs pulled from dependencies, in the order of the
3703 "deps" list. If a dependency is public, they will be applied
3704 recursively.
3705
3706```
3707
3708### **Example**:
3709```
3710 lib_dirs = [ "/usr/lib/foo", "lib/doom_melon" ]
3711
3712
3713```
3714## **libs**: Additional libraries to link.
3715
3716```
3717 A list of strings.
3718
3719 These files will be passed to the linker, which will generally search
3720 the library include path. Unlike a normal list of files, they will be
3721 passed to the linker unmodified rather than being treated as file
3722 names relative to the current build file. Generally you would set
3723 the "lib_dirs" so your library is found. If you need to specify
3724 a path, you can use "rebase_path" to convert a path to be relative
3725 to the build directory.
3726
3727 When constructing the linker command, the "lib_prefix" attribute of
3728 the linker tool in the current toolchain will be prepended to each
3729 library. So your BUILD file should not specify the switch prefix
3730 (like "-l"). On Mac, libraries ending in ".framework" will be
3731 special-cased: the switch "-framework" will be prepended instead of
3732 the lib_prefix, and the ".framework" suffix will be trimmed.
3733
3734 libs and lib_dirs work differently than other flags in two respects.
3735 First, then are inherited across static library boundaries until a
3736 shared library or executable target is reached. Second, they are
3737 uniquified so each one is only passed once (the first instance of it
3738 will be the one used).
3739
3740```
3741
3742### **Ordering of flags and values**:
3743
3744```
3745 1. Those set on the current target (not in a config).
3746 2. Those set on the "configs" on the target in order that the
3747 configs appear in the list.
3748 3. Those set on the "all_dependent_configs" on the target in order
3749 that the configs appear in the list.
3750 4. Those set on the "public_configs" on the target in order that
3751 those configs appear in the list.
3752 5. all_dependent_configs pulled from dependencies, in the order of
3753 the "deps" list. This is done recursively. If a config appears
3754 more than once, only the first occurance will be used.
3755 6. public_configs pulled from dependencies, in the order of the
3756 "deps" list. If a dependency is public, they will be applied
3757 recursively.
3758
3759```
3760
3761### **Examples**:
3762```
3763 On Windows:
3764 libs = [ "ctl3d.lib" ]
3765 On Linux:
3766 libs = [ "ld" ]
3767
3768
3769```
3770## **output_extension**: Value to use for the output's file extension.
3771
3772```
3773 Normally the file extension for a target is based on the target
3774 type and the operating system, but in rare cases you will need to
3775 override the name (for example to use "libfreetype.so.6" instead
3776 of libfreetype.so on Linux).
3777
3778```
3779## **output_name**: Define a name for the output file other than the default.
3780
3781```
3782 Normally the output name of a target will be based on the target name,
3783 so the target "//foo/bar:bar_unittests" will generate an output
3784 file such as "bar_unittests.exe" (using Windows as an example).
3785
3786 Sometimes you will want an alternate name to avoid collisions or
3787 if the internal name isn't appropriate for public distribution.
3788
3789 The output name should have no extension or prefixes, these will be
3790 added using the default system rules. For example, on Linux an output
3791 name of "foo" will produce a shared library "libfoo.so".
3792
3793 This variable is valid for all binary output target types.
3794
3795```
3796
3797### **Example**:
3798```
3799 static_library("doom_melon") {
3800 output_name = "fluffy_bunny"
3801 }
3802
3803
3804```
3805## **outputs**: Output files for actions and copy targets.
3806
3807```
3808 Outputs is valid for "copy", "action", and "action_foreach"
3809 target types and indicates the resulting files. The values may contain
3810 source expansions to generate the output names from the sources (see
3811 "gn help source_expansion").
3812
3813 For copy targets, the outputs is the destination for the copied
3814 file(s). For actions, the outputs should be the list of files
3815 generated by the script.
3816
3817
3818```
brettw20982dc2015-07-06 22:37:063819## **precompiled_header**: [string] Header file to precompile.
3820
3821```
3822 Precompiled headers will be used when a target specifies this
3823 value, or a config applying to this target specifies this value.
3824 In addition, the tool corresponding to the source files must also
3825 specify precompiled headers (see "gn help tool"). The tool
3826 will also specify what type of precompiled headers to use.
3827
3828 The precompiled header/source variables can be specified on a target
3829 or a config, but must be the same for all configs applying to a given
3830 target since a target can only have one precompiled header.
3831
3832```
3833
3834### **MSVC precompiled headers**
3835
3836```
3837 When using MSVC-style precompiled headers, the "precompiled_header"
3838 value is a string corresponding to the header. This is NOT a path
3839 to a file that GN recognises, but rather the exact string that appears
3840 in quotes after an #include line in source code. The compiler will
3841 match this string against includes or forced includes (/FI).
3842
3843 MSVC also requires a source file to compile the header with. This must
3844 be specified by the "precompiled_source" value. In contrast to the
3845 header value, this IS a GN-style file name, and tells GN which source
3846 file to compile to make the .pch file used for subsequent compiles.
3847
3848 If you use both C and C++ sources, the precompiled header and source
3849 file will be compiled using both tools. You will want to make sure
3850 to wrap C++ includes in __cplusplus #ifdefs so the file will compile
3851 in C mode.
3852
3853 For example, if the toolchain specifies MSVC headers:
3854
3855 toolchain("vc_x64") {
3856 ...
3857 tool("cxx") {
3858 precompiled_header_type = "msvc"
3859 ...
3860
3861 You might make a config like this:
3862
3863 config("use_precompiled_headers") {
3864 precompiled_header = "build/precompile.h"
3865 precompiled_source = "//build/precompile.cc"
3866
3867 # Either your source files should #include "build/precompile.h"
3868 # first, or you can do this to force-include the header.
3869 cflags = [ "/FI$precompiled_header" ]
3870 }
3871
3872 And then define a target that uses the config:
3873
3874 executable("doom_melon") {
3875 configs += [ ":use_precompiled_headers" ]
3876 ...
3877
3878
3879
3880```
3881## **precompiled_source**: [file name] Source file to precompile.
3882
3883```
3884 The source file that goes along with the precompiled_header when
3885 using "msvc"-style precompiled headers. It will be implicitly added
3886 to the sources of the target. See "gn help precompiled_header".
3887
3888
3889```
dprankee7700dd2015-04-17 22:55:453890## **public**: Declare public header files for a target.
3891
3892```
3893 A list of files that other targets can include. These permissions are
3894 checked via the "check" command (see "gn help check").
3895
3896 If no public files are declared, other targets (assuming they have
3897 visibility to depend on this target can include any file in the
3898 sources list. If this variable is defined on a target, dependent
3899 targets may only include files on this whitelist.
3900
3901 Header file permissions are also subject to visibility. A target
3902 must be visible to another target to include any files from it at all
3903 and the public headers indicate which subset of those files are
3904 permitted. See "gn help visibility" for more.
3905
3906 Public files are inherited through the dependency tree. So if there is
3907 a dependency A -> B -> C, then A can include C's public headers.
3908 However, the same is NOT true of visibility, so unless A is in C's
3909 visibility list, the include will be rejected.
3910
3911 GN only knows about files declared in the "sources" and "public"
3912 sections of targets. If a file is included that is not known to the
3913 build, it will be allowed.
3914
3915```
3916
3917### **Examples**:
3918```
3919 These exact files are public:
3920 public = [ "foo.h", "bar.h" ]
3921
3922 No files are public (no targets may include headers from this one):
3923 public = []
3924
3925
3926```
3927## **public_configs**: Configs to be applied on dependents.
3928
3929```
3930 A list of config labels.
3931
3932 Targets directly depending on this one will have the configs listed in
3933 this variable added to them. These configs will also apply to the
3934 current target.
3935
3936 This addition happens in a second phase once a target and all of its
3937 dependencies have been resolved. Therefore, a target will not see
3938 these force-added configs in their "configs" variable while the
3939 script is running, and then can not be removed. As a result, this
3940 capability should generally only be used to add defines and include
3941 directories necessary to compile a target's headers.
3942
3943 See also "all_dependent_configs".
3944
3945```
3946
3947### **Ordering of flags and values**:
3948
3949```
3950 1. Those set on the current target (not in a config).
3951 2. Those set on the "configs" on the target in order that the
3952 configs appear in the list.
3953 3. Those set on the "all_dependent_configs" on the target in order
3954 that the configs appear in the list.
3955 4. Those set on the "public_configs" on the target in order that
3956 those configs appear in the list.
3957 5. all_dependent_configs pulled from dependencies, in the order of
3958 the "deps" list. This is done recursively. If a config appears
3959 more than once, only the first occurance will be used.
3960 6. public_configs pulled from dependencies, in the order of the
3961 "deps" list. If a dependency is public, they will be applied
3962 recursively.
3963
3964
3965```
3966## **public_deps**: Declare public dependencies.
3967
3968```
3969 Public dependencies are like private dependencies ("deps") but
3970 additionally express that the current target exposes the listed deps
3971 as part of its public API.
3972
3973 This has several ramifications:
3974
3975 - public_configs that are part of the dependency are forwarded
3976 to direct dependents.
3977
3978 - Public headers in the dependency are usable by dependents
3979 (includes do not require a direct dependency or visibility).
3980
3981 - If the current target is a shared library, other shared libraries
3982 that it publicly depends on (directly or indirectly) are
3983 propagated up the dependency tree to dependents for linking.
3984
3985```
3986
3987### **Discussion**
3988
3989```
3990 Say you have three targets: A -> B -> C. C's visibility may allow
3991 B to depend on it but not A. Normally, this would prevent A from
3992 including any headers from C, and C's public_configs would apply
3993 only to B.
3994
3995 If B lists C in its public_deps instead of regular deps, A will now
3996 inherit C's public_configs and the ability to include C's public
3997 headers.
3998
3999 Generally if you are writing a target B and you include C's headers
4000 as part of B's public headers, or targets depending on B should
4001 consider B and C to be part of a unit, you should use public_deps
4002 instead of deps.
4003
4004```
4005
4006### **Example**
4007
4008```
4009 # This target can include files from "c" but not from
4010 # "super_secret_implementation_details".
4011 executable("a") {
4012 deps = [ ":b" ]
4013 }
4014
4015 shared_library("b") {
4016 deps = [ ":super_secret_implementation_details" ]
4017 public_deps = [ ":c" ]
4018 }
4019
4020
4021```
4022## **script**: Script file for actions.
4023
4024```
4025 An absolute or buildfile-relative file name of a Python script to run
4026 for a action and action_foreach targets (see "gn help action" and
4027 "gn help action_foreach").
4028
4029
4030```
4031## **sources**: Source files for a target
4032
4033```
4034 A list of files relative to the current buildfile.
4035
4036
4037```
4038## **testonly**: Declares a target must only be used for testing.
4039
4040```
4041 Boolean. Defaults to false.
4042
4043 When a target is marked "testonly = true", it must only be depended
4044 on by other test-only targets. Otherwise, GN will issue an error
4045 that the depenedency is not allowed.
4046
4047 This feature is intended to prevent accidentally shipping test code
4048 in a final product.
4049
4050```
4051
4052### **Example**
4053
4054```
4055 source_set("test_support") {
4056 testonly = true
4057 ...
4058 }
4059
4060
4061```
4062## **visibility**: A list of labels that can depend on a target.
4063
4064```
4065 A list of labels and label patterns that define which targets can
4066 depend on the current one. These permissions are checked via the
4067 "check" command (see "gn help check").
4068
4069 If visibility is not defined, it defaults to public ("*").
4070
4071 If visibility is defined, only the targets with labels that match it
4072 can depend on the current target. The empty list means no targets
4073 can depend on the current target.
4074
4075 Tip: Often you will want the same visibility for all targets in a
4076 BUILD file. In this case you can just put the definition at the top,
4077 outside of any target, and the targets will inherit that scope and see
4078 the definition.
4079
4080```
4081
4082### **Patterns**
4083
4084```
4085 See "gn help label_pattern" for more details on what types of
4086 patterns are supported. If a toolchain is specified, only targets
4087 in that toolchain will be matched. If a toolchain is not specified on
4088 a pattern, targets in all toolchains will be matched.
4089
4090```
4091
4092### **Examples**
4093
4094```
4095 Only targets in the current buildfile ("private"):
4096 visibility = [ ":*" ]
4097
4098 No targets (used for targets that should be leaf nodes):
4099 visibility = []
4100
4101 Any target ("public", the default):
4102 visibility = [ "*" ]
4103
4104 All targets in the current directory and any subdirectory:
4105 visibility = [ "./*" ]
4106
4107 Any target in "//bar/BUILD.gn":
4108 visibility = [ "//bar:*" ]
4109
4110 Any target in "//bar/" or any subdirectory thereof:
4111 visibility = [ "//bar/*" ]
4112
4113 Just these specific targets:
4114 visibility = [ ":mything", "//foo:something_else" ]
4115
4116 Any target in the current directory and any subdirectory thereof, plus
4117 any targets in "//bar/" and any subdirectory thereof.
4118 visibility = [ "./*", "//bar/*" ]
4119
4120
4121```
4122## **Build Arguments Overview**
4123
4124```
4125 Build arguments are variables passed in from outside of the build
4126 that build files can query to determine how the build works.
4127
4128```
4129
4130### **How build arguments are set**
4131
4132```
4133 First, system default arguments are set based on the current system.
4134 The built-in arguments are:
4135 - host_cpu
4136 - host_os
4137 - current_cpu
4138 - current_os
4139 - target_cpu
4140 - target_os
4141
4142 If specified, arguments from the --args command line flag are used. If
4143 that flag is not specified, args from previous builds in the build
4144 directory will be used (this is in the file args.gn in the build
4145 directory).
4146
4147 Last, for targets being compiled with a non-default toolchain, the
4148 toolchain overrides are applied. These are specified in the
4149 toolchain_args section of a toolchain definition. The use-case for
4150 this is that a toolchain may be building code for a different
4151 platform, and that it may want to always specify Posix, for example.
4152 See "gn help toolchain_args" for more.
4153
4154 If you specify an override for a build argument that never appears in
4155 a "declare_args" call, a nonfatal error will be displayed.
4156
4157```
4158
4159### **Examples**
4160
4161```
4162 gn args out/FooBar
4163 Create the directory out/FooBar and open an editor. You would type
4164 something like this into that file:
4165 enable_doom_melon=false
4166 os="android"
4167
4168 gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""
4169 This will overwrite the build directory with the given arguments.
4170 (Note that the quotes inside the args command will usually need to
4171 be escaped for your shell to pass through strings values.)
4172
4173```
4174
4175### **How build arguments are used**
4176
4177```
4178 If you want to use an argument, you use declare_args() and specify
4179 default values. These default values will apply if none of the steps
4180 listed in the "How build arguments are set" section above apply to
4181 the given argument, but the defaults will not override any of these.
4182
4183 Often, the root build config file will declare global arguments that
4184 will be passed to all buildfiles. Individual build files can also
4185 specify arguments that apply only to those files. It is also useful
4186 to specify build args in an "import"-ed file if you want such
4187 arguments to apply to multiple buildfiles.
4188
4189
4190```
4191## **.gn file**
4192
4193```
4194 When gn starts, it will search the current directory and parent
4195 directories for a file called ".gn". This indicates the source root.
4196 You can override this detection by using the --root command-line
4197 argument
4198
4199 The .gn file in the source root will be executed. The syntax is the
4200 same as a buildfile, but with very limited build setup-specific
4201 meaning.
4202
4203 If you specify --root, by default GN will look for the file .gn in
4204 that directory. If you want to specify a different file, you can
4205 additionally pass --dotfile:
4206
4207 gn gen out/Debug --root=/home/build --dotfile=/home/my_gn_file.gn
4208
4209```
4210
4211### **Variables**
4212
4213```
4214 buildconfig [required]
4215 Label of the build config file. This file will be used to set up
4216 the build file execution environment for each toolchain.
4217
4218 check_targets [optional]
4219 A list of labels and label patterns that should be checked when
4220 running "gn check" or "gn gen --check". If unspecified, all
4221 targets will be checked. If it is the empty list, no targets will
4222 be checked.
4223
4224 The format of this list is identical to that of "visibility"
4225 so see "gn help visibility" for examples.
4226
4227 exec_script_whitelist [optional]
4228 A list of .gn/.gni files (not labels) that have permission to call
4229 the exec_script function. If this list is defined, calls to
4230 exec_script will be checked against this list and GN will fail if
4231 the current file isn't in the list.
4232
4233 This is to allow the use of exec_script to be restricted since
4234 is easy to use inappropriately. Wildcards are not supported.
4235 Files in the secondary_source tree (if defined) should be
4236 referenced by ignoring the secondary tree and naming them as if
4237 they are in the main tree.
4238
4239 If unspecified, the ability to call exec_script is unrestricted.
4240
4241 Example:
4242 exec_script_whitelist = [
4243 "//base/BUILD.gn",
4244 "//build/my_config.gni",
4245 ]
4246
4247 root [optional]
4248 Label of the root build target. The GN build will start by loading
4249 the build file containing this target name. This defaults to
4250 "//:" which will cause the file //BUILD.gn to be loaded.
4251
4252 secondary_source [optional]
4253 Label of an alternate directory tree to find input files. When
4254 searching for a BUILD.gn file (or the build config file discussed
4255 above), the file will first be looked for in the source root.
4256 If it's not found, the secondary source root will be checked
4257 (which would contain a parallel directory hierarchy).
4258
4259 This behavior is intended to be used when BUILD.gn files can't be
4260 checked in to certain source directories for whatever reason.
4261
4262 The secondary source root must be inside the main source tree.
4263
4264```
4265
4266### **Example .gn file contents**
4267
4268```
4269 buildconfig = "//build/config/BUILDCONFIG.gn"
4270
4271 check_targets = [
4272 "//doom_melon/*", # Check everything in this subtree.
4273 "//tools:mind_controlling_ant", # Check this specific target.
4274 ]
4275
4276 root = "//:root"
4277
4278 secondary_source = "//build/config/temporary_buildfiles/"
4279
4280
4281```
dprankee7678eb2015-04-24 22:24:494282## **GN build language grammar**
4283
4284### **Tokens**
4285
4286```
4287 GN build files are read as sequences of tokens. While splitting the
4288 file into tokens, the next token is the longest sequence of characters
4289 that form a valid token.
4290
4291```
4292
4293### **White space and comments**
4294
4295```
4296 White space is comprised of spaces (U+0020), horizontal tabs (U+0009),
4297 carriage returns (U+000D), and newlines (U+000A).
4298
4299 Comments start at the character "#" and stop at the next newline.
4300
4301 White space and comments are ignored except that they may separate
4302 tokens that would otherwise combine into a single token.
4303
4304```
4305
4306### **Identifiers**
4307
4308```
4309 Identifiers name variables and functions.
4310
4311 identifier = letter { letter | digit } .
4312 letter = "A" ... "Z" | "a" ... "z" | "_" .
4313 digit = "0" ... "9" .
4314
4315```
4316
4317### **Keywords**
4318
4319```
4320 The following keywords are reserved and may not be used as
4321 identifiers:
4322
4323 else false if true
4324
4325```
4326
4327### **Integer literals**
4328
4329```
4330 An integer literal represents a decimal integer value.
4331
4332 integer = [ "-" ] digit { digit } .
4333
4334 Leading zeros and negative zero are disallowed.
4335
4336```
4337
4338### **String literals**
4339
4340```
4341 A string literal represents a string value consisting of the quoted
4342 characters with possible escape sequences and variable expansions.
4343
4344 string = `"` { char | escape | expansion } `"` .
4345 escape = `\` ( "$" | `"` | char ) .
4346 expansion = "$" ( identifier | "{" identifier "}" ) .
4347 char = /* any character except "$", `"`, or newline */ .
4348
4349 After a backslash, certain sequences represent special characters:
4350
4351 \" U+0022 quotation mark
4352 \$ U+0024 dollar sign
4353 \\ U+005C backslash
4354
4355 All other backslashes represent themselves.
4356
4357```
4358
4359### **Punctuation**
4360
4361```
4362 The following character sequences represent punctuation:
4363
4364 + += == != ( )
4365 - -= < <= [ ]
4366 ! = > >= { }
4367 && || . ,
4368
4369```
4370
4371### **Grammar**
4372
4373```
4374 The input tokens form a syntax tree following a context-free grammar:
4375
4376 File = StatementList .
4377
4378 Statement = Assignment | Call | Condition .
4379 Assignment = identifier AssignOp Expr .
4380 Call = identifier "(" [ ExprList ] ")" [ Block ] .
4381 Condition = "if" "(" Expr ")" Block
4382 [ "else" ( Condition | Block ) ] .
4383 Block = "{" StatementList "}" .
4384 StatementList = { Statement } .
4385
4386 Expr = UnaryExpr | Expr BinaryOp Expr .
4387 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr .
4388 PrimaryExpr = identifier | integer | string | Call
4389 | identifier "[" Expr "]"
4390 | identifier "." identifier
4391 | "(" Expr ")"
4392 | "[" [ ExprList [ "," ] ] "]" .
4393 ExprList = Expr { "," Expr } .
4394
4395 AssignOp = "=" | "+=" | "-=" .
4396 UnaryOp = "!" .
4397 BinaryOp = "+" | "-" // highest priority
4398 | "<" | "<=" | ">" | ">="
4399 | "==" | "!="
4400 | "&&"
4401 | "||" . // lowest priority
4402
4403 All binary operators are left-associative.
4404
4405
4406```
dprankee7700dd2015-04-17 22:55:454407## **input_conversion**: Specifies how to transform input to a variable.
4408
4409```
4410 input_conversion is an argument to read_file and exec_script that
4411 specifies how the result of the read operation should be converted
4412 into a variable.
4413
4414 "" (the default)
4415 Discard the result and return None.
4416
4417 "list lines"
4418 Return the file contents as a list, with a string for each line.
4419 The newlines will not be present in the result. The last line may
4420 or may not end in a newline.
4421
4422 After splitting, each individual line will be trimmed of
4423 whitespace on both ends.
4424
4425 "scope"
4426 Execute the block as GN code and return a scope with the
4427 resulting values in it. If the input was:
4428 a = [ "hello.cc", "world.cc" ]
4429 b = 26
4430 and you read the result into a variable named "val", then you
4431 could access contents the "." operator on "val":
4432 sources = val.a
4433 some_count = val.b
4434
4435 "string"
4436 Return the file contents into a single string.
4437
4438 "value"
4439 Parse the input as if it was a literal rvalue in a buildfile.
4440 Examples of typical program output using this mode:
4441 [ "foo", "bar" ] (result will be a list)
4442 or
4443 "foo bar" (result will be a string)
4444 or
4445 5 (result will be an integer)
4446
4447 Note that if the input is empty, the result will be a null value
4448 which will produce an error if assigned to a variable.
4449
4450 "trim ..."
4451 Prefixing any of the other transformations with the word "trim"
4452 will result in whitespace being trimmed from the beginning and end
4453 of the result before processing.
4454
4455 Examples: "trim string" or "trim list lines"
4456
4457 Note that "trim value" is useless because the value parser skips
4458 whitespace anyway.
4459
4460
4461```
4462## **Label patterns**
4463
4464```
4465 A label pattern is a way of expressing one or more labels in a portion
4466 of the source tree. They are not general regular expressions.
4467
4468 They can take the following forms only:
4469
4470 - Explicit (no wildcard):
4471 "//foo/bar:baz"
4472 ":baz"
4473
4474 - Wildcard target names:
4475 "//foo/bar:*" (all targets in the //foo/bar/BUILD.gn file)
4476 ":*" (all targets in the current build file)
4477
4478 - Wildcard directory names ("*" is only supported at the end)
4479 "*" (all targets)
4480 "//foo/bar/*" (all targets in any subdir of //foo/bar)
4481 "./*" (all targets in the current build file or sub dirs)
4482
4483 Any of the above forms can additionally take an explicit toolchain.
4484 In this case, the toolchain must be fully qualified (no wildcards
4485 are supported in the toolchain name).
4486
4487 "//foo:bar(//build/toochain:mac)"
4488 An explicit target in an explicit toolchain.
4489
4490 ":*(//build/toolchain/linux:32bit)"
4491 All targets in the current build file using the 32-bit Linux
4492 toolchain.
4493
4494 "//foo/*(//build/toolchain:win)"
4495 All targets in //foo and any subdirectory using the Windows
4496 toolchain.
4497
4498
4499```
dpranke5316b8c2015-06-09 16:03:264500## **Runtime dependencies**
4501
4502```
4503 Runtime dependencies of a target are exposed via the "runtime_deps"
4504 category of "gn desc" (see "gn help desc") or they can be written
4505 at build generation time via "--runtime-deps-list-file"
4506 (see "gn help --runtime-deps-list-file").
4507
4508 To a first approximation, the runtime dependencies of a target are
4509 the set of "data" files, data directories, and the shared libraries
4510 from all transitive dependencies. Executables and shared libraries are
4511 considered runtime dependencies of themselves.
4512
4513```
4514
4515### **Details**
4516
4517```
4518 Executable targets and those executable targets' transitive
4519 dependencies are not considered unless that executable is listed in
4520 "data_deps". Otherwise, GN assumes that the executable (and
4521 everything it requires) is a build-time dependency only.
4522
4523 Action and copy targets that are listed as "data_deps" will have all
4524 of their outputs and data files considered as runtime dependencies.
4525 Action and copy targets that are "deps" or "public_deps" will have
4526 only their data files considered as runtime dependencies. These
4527 targets can list an output file in both the "outputs" and "data"
4528 lists to force an output file as a runtime dependency in all cases.
4529
4530 The results of static_library or source_set targets are not considered
4531 runtime dependencies since these are assumed to be intermediate
4532 targets only. If you need to list a static library as a runtime
4533 dependency, you can manually compute the .a/.lib file name for the
4534 current platform and list it in the "data" list of a target
4535 (possibly on the static library target itself).
4536
4537 When a tool produces more than one output, only the first output
4538 is considered. For example, a shared library target may produce a
4539 .dll and a .lib file on Windows. Only the .dll file will be considered
4540 a runtime dependency.
4541
4542
4543```
dprankee7700dd2015-04-17 22:55:454544## **How Source Expansion Works**
4545
4546```
4547 Source expansion is used for the action_foreach and copy target types
4548 to map source file names to output file names or arguments.
4549
4550 To perform source expansion in the outputs, GN maps every entry in the
4551 sources to every entry in the outputs list, producing the cross
4552 product of all combinations, expanding placeholders (see below).
4553
4554 Source expansion in the args works similarly, but performing the
4555 placeholder substitution produces a different set of arguments for
4556 each invocation of the script.
4557
4558 If no placeholders are found, the outputs or args list will be treated
4559 as a static list of literal file names that do not depend on the
4560 sources.
4561
4562 See "gn help copy" and "gn help action_foreach" for more on how
4563 this is applied.
4564
4565```
4566
4567### **Placeholders**
4568
4569```
4570 {{source}}
4571 The name of the source file including directory (*). This will
4572 generally be used for specifying inputs to a script in the
4573 "args" variable.
4574 "//foo/bar/baz.txt" => "../../foo/bar/baz.txt"
4575
4576 {{source_file_part}}
4577 The file part of the source including the extension.
4578 "//foo/bar/baz.txt" => "baz.txt"
4579
4580 {{source_name_part}}
4581 The filename part of the source file with no directory or
4582 extension. This will generally be used for specifying a
4583 transformation from a soruce file to a destination file with the
4584 same name but different extension.
4585 "//foo/bar/baz.txt" => "baz"
4586
4587 {{source_dir}}
4588 The directory (*) containing the source file with no
4589 trailing slash.
4590 "//foo/bar/baz.txt" => "../../foo/bar"
4591
4592 {{source_root_relative_dir}}
4593 The path to the source file's directory relative to the source
4594 root, with no leading "//" or trailing slashes. If the path is
4595 system-absolute, (beginning in a single slash) this will just
4596 return the path with no trailing slash. This value will always
4597 be the same, regardless of whether it appears in the "outputs"
4598 or "args" section.
4599 "//foo/bar/baz.txt" => "foo/bar"
4600
4601 {{source_gen_dir}}
4602 The generated file directory (*) corresponding to the source
4603 file's path. This will be different than the target's generated
4604 file directory if the source file is in a different directory
4605 than the BUILD.gn file.
4606 "//foo/bar/baz.txt" => "gen/foo/bar"
4607
4608 {{source_out_dir}}
4609 The object file directory (*) corresponding to the source file's
4610 path, relative to the build directory. this us be different than
4611 the target's out directory if the source file is in a different
4612 directory than the build.gn file.
4613 "//foo/bar/baz.txt" => "obj/foo/bar"
4614
4615```
4616
4617### **(*) Note on directories**
4618
4619```
4620 Paths containing directories (except the source_root_relative_dir)
4621 will be different depending on what context the expansion is evaluated
4622 in. Generally it should "just work" but it means you can't
4623 concatenate strings containing these values with reasonable results.
4624
4625 Details: source expansions can be used in the "outputs" variable,
4626 the "args" variable, and in calls to "process_file_template". The
4627 "args" are passed to a script which is run from the build directory,
4628 so these directories will relative to the build directory for the
4629 script to find. In the other cases, the directories will be source-
4630 absolute (begin with a "//") because the results of those expansions
4631 will be handled by GN internally.
4632
4633```
4634
4635### **Examples**
4636
4637```
4638 Non-varying outputs:
4639 action("hardcoded_outputs") {
4640 sources = [ "input1.idl", "input2.idl" ]
4641 outputs = [ "$target_out_dir/output1.dat",
4642 "$target_out_dir/output2.dat" ]
4643 }
4644 The outputs in this case will be the two literal files given.
4645
4646 Varying outputs:
4647 action_foreach("varying_outputs") {
4648 sources = [ "input1.idl", "input2.idl" ]
4649 outputs = [ "{{source_gen_dir}}/{{source_name_part}}.h",
4650 "{{source_gen_dir}}/{{source_name_part}}.cc" ]
4651 }
4652 Performing source expansion will result in the following output names:
4653 //out/Debug/obj/mydirectory/input1.h
4654 //out/Debug/obj/mydirectory/input1.cc
4655 //out/Debug/obj/mydirectory/input2.h
4656 //out/Debug/obj/mydirectory/input2.cc
4657
4658
4659```
4660**Available global switches
4661** Do "gn help --the_switch_you_want_help_on" for more. Individual
4662 commands may take command-specific switches not listed here. See the
4663 help on your specific command for more.
4664
4665```
4666
4667** --args**: Specifies build arguments overrides.
4668** --color**: Force colored output.
dpranke5316b8c2015-06-09 16:03:264669** --dotfile**: Override the name of the ".gn" file.
dprankee7700dd2015-04-17 22:55:454670** --markdown**: write the output in the Markdown format.
4671** --nocolor**: Force non-colored output.
4672** -q**: Quiet mode. Don't print output on success.
4673** --root**: Explicitly specify source root.
dpranke5316b8c2015-06-09 16:03:264674** --runtime-deps-list-file**: Save runtime dependencies for targets in file.
dprankee7700dd2015-04-17 22:55:454675** --time**: Outputs a summary of how long everything took.
4676** --tracelog**: Writes a Chrome-compatible trace log to the given file.
4677** -v**: Verbose logging.
4678** --version**: Prints the GN version number and exits.
4679
4680```