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