OLD | NEW |
1 # GN Reference | 1 # GN Reference |
2 | 2 |
3 *This page is automatically generated from* `gn help --markdown all`. | 3 *This page is automatically generated from* `gn help --markdown all`. |
4 | 4 |
5 ## **\--args**: Specifies build arguments overrides. | 5 ## **\--args**: Specifies build arguments overrides. |
6 | 6 |
7 ``` | 7 ``` |
8 See "gn help buildargs" for an overview of how build arguments work. | 8 See "gn help buildargs" for an overview of how build arguments work. |
9 | 9 |
10 Most operations take a build directory. The build arguments are taken | 10 Most operations take a build directory. The build arguments are taken |
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 gn desc out/Foo :base_unittests deps --tree | 617 gn desc out/Foo :base_unittests deps --tree |
618 Shows a dependency tree of the "base_unittests" project in | 618 Shows a dependency tree of the "base_unittests" project in |
619 the current directory. | 619 the current directory. |
620 | 620 |
621 gn desc out/Debug //base defines --blame | 621 gn desc out/Debug //base defines --blame |
622 Shows defines set for the //base:base target, annotated by where | 622 Shows defines set for the //base:base target, annotated by where |
623 each one was set from. | 623 each one was set from. |
624 | 624 |
625 | 625 |
626 ``` | 626 ``` |
627 ## **gn format [\--dump-tree] [\--in-place] [\--stdin] BUILD.gn** | 627 ## **gn format [\--dump-tree] (\--stdin | <build_file>)** |
628 | 628 |
629 ``` | 629 ``` |
630 Formats .gn file to a standard format. | 630 Formats .gn file to a standard format. |
631 | 631 |
632 The contents of some lists ('sources', 'deps', etc.) will be sorted to | 632 The contents of some lists ('sources', 'deps', etc.) will be sorted to |
633 a canonical order. To suppress this, you can add a comment of the form | 633 a canonical order. To suppress this, you can add a comment of the form |
634 "# NOSORT" immediately preceeding the assignment. e.g. | 634 "# NOSORT" immediately preceeding the assignment. e.g. |
635 | 635 |
636 # NOSORT | 636 # NOSORT |
637 sources = [ | 637 sources = [ |
638 "z.cc", | 638 "z.cc", |
639 "a.cc", | 639 "a.cc", |
640 ] | 640 ] |
641 | 641 |
642 ``` | 642 ``` |
643 | 643 |
644 ### **Arguments** | 644 ### **Arguments** |
| 645 |
645 ``` | 646 ``` |
646 --dry-run | 647 --dry-run |
647 Does not change or output anything, but sets the process exit code | 648 Does not change or output anything, but sets the process exit code |
648 based on whether output would be different than what's on disk. | 649 based on whether output would be different than what's on disk. |
649 This is useful for presubmit/lint-type checks. | 650 This is useful for presubmit/lint-type checks. |
650 - Exit code 0: successful format, matches on disk. | 651 - Exit code 0: successful format, matches on disk. |
651 - Exit code 1: general failure (parse error, etc.) | 652 - Exit code 1: general failure (parse error, etc.) |
652 - Exit code 2: successful format, but differs from on disk. | 653 - Exit code 2: successful format, but differs from on disk. |
653 | 654 |
654 --dump-tree | 655 --dump-tree |
655 For debugging only, dumps the parse tree. | 656 For debugging, dumps the parse tree to stdout and does not update |
656 | 657 the file or print formatted output. |
657 --in-place | |
658 Instead of writing the formatted file to stdout, replace the input | |
659 file with the formatted output. If no reformatting is required, | |
660 the input file will not be touched, and nothing printed. | |
661 | 658 |
662 --stdin | 659 --stdin |
663 Read input from stdin (and write to stdout). Not compatible with | 660 Read input from stdin and write to stdout rather than update |
664 --in-place of course. | 661 a file in-place. |
665 | 662 |
666 ``` | 663 ``` |
667 | 664 |
668 ### **Examples** | 665 ### **Examples** |
669 ``` | 666 ``` |
670 gn format //some/BUILD.gn | 667 gn format //some/BUILD.gn |
671 gn format some\BUILD.gn | 668 gn format some\BUILD.gn |
672 gn format /abspath/some/BUILD.gn | 669 gn format /abspath/some/BUILD.gn |
673 gn format --stdin | 670 gn format --stdin |
674 | 671 |
(...skipping 1881 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2556 configs -= "//tools/mything:settings" | 2553 configs -= "//tools/mything:settings" |
2557 } | 2554 } |
2558 | 2555 |
2559 | 2556 |
2560 ``` | 2557 ``` |
2561 ## **set_sources_assignment_filter**: Set a pattern to filter source files. | 2558 ## **set_sources_assignment_filter**: Set a pattern to filter source files. |
2562 | 2559 |
2563 ``` | 2560 ``` |
2564 The sources assignment filter is a list of patterns that remove files | 2561 The sources assignment filter is a list of patterns that remove files |
2565 from the list implicitly whenever the "sources" variable is | 2562 from the list implicitly whenever the "sources" variable is |
2566 assigned to. This is intended to be used to globally filter out files | 2563 assigned to. This will do nothing for non-lists. |
2567 with platform-specific naming schemes when they don't apply, for | 2564 |
2568 example, you may want to filter out all "*_win.cc" files on non- | 2565 This is intended to be used to globally filter out files with |
2569 Windows platforms. | 2566 platform-specific naming schemes when they don't apply, for example |
| 2567 you may want to filter out all "*_win.cc" files on non-Windows |
| 2568 platforms. |
2570 | 2569 |
2571 Typically this will be called once in the master build config script | 2570 Typically this will be called once in the master build config script |
2572 to set up the filter for the current platform. Subsequent calls will | 2571 to set up the filter for the current platform. Subsequent calls will |
2573 overwrite the previous values. | 2572 overwrite the previous values. |
2574 | 2573 |
2575 If you want to bypass the filter and add a file even if it might | 2574 If you want to bypass the filter and add a file even if it might |
2576 be filtered out, call set_sources_assignment_filter([]) to clear the | 2575 be filtered out, call set_sources_assignment_filter([]) to clear the |
2577 list of filters. This will apply until the current scope exits | 2576 list of filters. This will apply until the current scope exits |
2578 | 2577 |
2579 ``` | 2578 ``` |
(...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3337 The copy_bundle_data and compile_xcassets tools only allows the common | 3336 The copy_bundle_data and compile_xcassets tools only allows the common |
3338 tool substitutions. Both tools are required to create iOS/OS X bundles | 3337 tool substitutions. Both tools are required to create iOS/OS X bundles |
3339 and need only be defined on those platforms. | 3338 and need only be defined on those platforms. |
3340 | 3339 |
3341 The copy_bundle_data tool will be called with one source and needs to | 3340 The copy_bundle_data tool will be called with one source and needs to |
3342 copy (optionally optimizing the data representation) to its output. It | 3341 copy (optionally optimizing the data representation) to its output. It |
3343 may be called with a directory as input and it needs to be recursively | 3342 may be called with a directory as input and it needs to be recursively |
3344 copied. | 3343 copied. |
3345 | 3344 |
3346 The compile_xcassets tool will be called with one or more source (each | 3345 The compile_xcassets tool will be called with one or more source (each |
3347 an asset catalog) that needs to be compiled to a single output. | 3346 an asset catalog) that needs to be compiled to a single output. The |
| 3347 following substitutions are avaiable: |
| 3348 |
| 3349 {{inputs}} |
| 3350 Expands to the list of .xcassets to use as input to compile the |
| 3351 asset catalog. |
| 3352 |
| 3353 {{bundle_product_type}} |
| 3354 Expands to the product_type of the bundle that will contain the |
| 3355 compiled asset catalog. Usually corresponds to the product_type |
| 3356 property of the corresponding create_bundle target. |
3348 | 3357 |
3349 ``` | 3358 ``` |
3350 | 3359 |
3351 ### **Separate linking and dependencies for shared libraries** | 3360 ### **Separate linking and dependencies for shared libraries** |
3352 | 3361 |
3353 ``` | 3362 ``` |
3354 Shared libraries are special in that not all changes to them require | 3363 Shared libraries are special in that not all changes to them require |
3355 that dependent targets be re-linked. If the shared library is changed | 3364 that dependent targets be re-linked. If the shared library is changed |
3356 but no imports or exports are different, dependent code needn't be | 3365 but no imports or exports are different, dependent code needn't be |
3357 relinked, which can speed up the build. | 3366 relinked, which can speed up the build. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3413 | 3422 |
3414 ``` | 3423 ``` |
3415 | 3424 |
3416 ### **Functions and variables** | 3425 ### **Functions and variables** |
3417 | 3426 |
3418 ``` | 3427 ``` |
3419 tool() | 3428 tool() |
3420 The tool() function call specifies the commands commands to run for | 3429 The tool() function call specifies the commands commands to run for |
3421 a given step. See "gn help tool". | 3430 a given step. See "gn help tool". |
3422 | 3431 |
3423 toolchain_args() | 3432 toolchain_args |
3424 List of arguments to pass to the toolchain when invoking this | 3433 Overrides for build arguments to pass to the toolchain when invoking |
3425 toolchain. This applies only to non-default toolchains. See | 3434 it. This is a variable of type "scope" where the variable names |
3426 "gn help toolchain_args" for more. | 3435 correspond to variables in declare_args() blocks. |
| 3436 |
| 3437 When you specify a target using an alternate toolchain, the master |
| 3438 build configuration file is re-interpreted in the context of that |
| 3439 toolchain. toolchain_args allows you to control the arguments |
| 3440 passed into this alternate invocation of the build. |
| 3441 |
| 3442 Any default system arguments or arguments passed in via "gn args" |
| 3443 will also be passed to the alternate invocation unless explicitly |
| 3444 overridden by toolchain_args. |
| 3445 |
| 3446 The toolchain_args will be ignored when the toolchain being defined |
| 3447 is the default. In this case, it's expected you want the default |
| 3448 argument values. |
| 3449 |
| 3450 See also "gn help buildargs" for an overview of these arguments. |
3427 | 3451 |
3428 deps | 3452 deps |
3429 Dependencies of this toolchain. These dependencies will be resolved | 3453 Dependencies of this toolchain. These dependencies will be resolved |
3430 before any target in the toolchain is compiled. To avoid circular | 3454 before any target in the toolchain is compiled. To avoid circular |
3431 dependencies these must be targets defined in another toolchain. | 3455 dependencies these must be targets defined in another toolchain. |
3432 | 3456 |
3433 This is expressed as a list of targets, and generally these targets | 3457 This is expressed as a list of targets, and generally these targets |
3434 will always specify a toolchain: | 3458 will always specify a toolchain: |
3435 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ] | 3459 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ] |
3436 | 3460 |
(...skipping 15 matching lines...) Expand all Loading... |
3452 data_deps = [ "//plugins:mine(//toolchains:plugin_toolchain)" ] | 3476 data_deps = [ "//plugins:mine(//toolchains:plugin_toolchain)" ] |
3453 The string "//build/toolchains:plugin_toolchain" is a label that | 3477 The string "//build/toolchains:plugin_toolchain" is a label that |
3454 identifies the toolchain declaration for compiling the sources. | 3478 identifies the toolchain declaration for compiling the sources. |
3455 | 3479 |
3456 To load a file in an alternate toolchain, GN does the following: | 3480 To load a file in an alternate toolchain, GN does the following: |
3457 | 3481 |
3458 1. Loads the file with the toolchain definition in it (as determined | 3482 1. Loads the file with the toolchain definition in it (as determined |
3459 by the toolchain label). | 3483 by the toolchain label). |
3460 2. Re-runs the master build configuration file, applying the | 3484 2. Re-runs the master build configuration file, applying the |
3461 arguments specified by the toolchain_args section of the toolchain | 3485 arguments specified by the toolchain_args section of the toolchain |
3462 definition (see "gn help toolchain_args"). | 3486 definition. |
3463 3. Loads the destination build file in the context of the | 3487 3. Loads the destination build file in the context of the |
3464 configuration file in the previous step. | 3488 configuration file in the previous step. |
3465 | 3489 |
3466 ``` | 3490 ``` |
3467 | 3491 |
3468 ### **Example**: | 3492 ### **Example** |
| 3493 |
3469 ``` | 3494 ``` |
3470 toolchain("plugin_toolchain") { | 3495 toolchain("plugin_toolchain") { |
3471 tool("cc") { | 3496 tool("cc") { |
3472 command = "gcc {{source}}" | 3497 command = "gcc {{source}}" |
3473 ... | 3498 ... |
3474 } | 3499 } |
3475 | 3500 |
3476 toolchain_args() { | 3501 toolchain_args = { |
3477 is_plugin = true | 3502 is_plugin = true |
3478 is_32bit = true | 3503 is_32bit = true |
3479 is_64bit = false | 3504 is_64bit = false |
3480 } | 3505 } |
3481 } | 3506 } |
3482 | 3507 |
3483 | 3508 |
3484 ``` | 3509 ``` |
3485 ## **toolchain_args**: Set build arguments for toolchain build setup. | 3510 ## **toolchain_args**: Set build arguments for toolchain build setup. |
3486 | 3511 |
3487 ``` | 3512 ``` |
3488 Used inside a toolchain definition to pass arguments to an alternate | 3513 DEPRECATED. Instead use: |
3489 toolchain's invocation of the build. | 3514 toolchain_args = { ... } |
3490 | 3515 |
3491 When you specify a target using an alternate toolchain, the master | 3516 See "gn help toolchain" for documentation. |
3492 build configuration file is re-interpreted in the context of that | |
3493 toolchain (see "gn help toolchain"). The toolchain_args function | |
3494 allows you to control the arguments passed into this alternate | |
3495 invocation of the build. | |
3496 | |
3497 Any default system arguments or arguments passed in on the command- | |
3498 line will also be passed to the alternate invocation unless explicitly | |
3499 overridden by toolchain_args. | |
3500 | |
3501 The toolchain_args will be ignored when the toolchain being defined | |
3502 is the default. In this case, it's expected you want the default | |
3503 argument values. | |
3504 | |
3505 See also "gn help buildargs" for an overview of these arguments. | |
3506 | |
3507 ``` | |
3508 | |
3509 ### **Example**: | |
3510 ``` | |
3511 toolchain("my_weird_toolchain") { | |
3512 ... | |
3513 toolchain_args() { | |
3514 # Override the system values for a generic Posix system. | |
3515 is_win = false | |
3516 is_posix = true | |
3517 | |
3518 # Pass this new value for specific setup for my toolchain. | |
3519 is_my_weird_system = true | |
3520 } | |
3521 } | |
3522 | 3517 |
3523 | 3518 |
3524 ``` | 3519 ``` |
3525 ## **write_file**: Write a file to disk. | 3520 ## **write_file**: Write a file to disk. |
3526 | 3521 |
3527 ``` | 3522 ``` |
3528 write_file(filename, data) | 3523 write_file(filename, data) |
3529 | 3524 |
3530 If data is a list, the list will be written one-item-per-line with no | 3525 If data is a list, the list will be written one-item-per-line with no |
3531 quoting or brackets. | 3526 quoting or brackets. |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4168 deps = [ "//foo:bar" ] | 4163 deps = [ "//foo:bar" ] |
4169 ... | 4164 ... |
4170 assert_no_deps = [ | 4165 assert_no_deps = [ |
4171 "//evil/*", # Don't link any code from the evil directory. | 4166 "//evil/*", # Don't link any code from the evil directory. |
4172 "//foo:test_support", # This target is also disallowed. | 4167 "//foo:test_support", # This target is also disallowed. |
4173 ] | 4168 ] |
4174 } | 4169 } |
4175 | 4170 |
4176 | 4171 |
4177 ``` | 4172 ``` |
| 4173 ## **bundle_deps_filter**: [label list] A list of labels that are filtered out. |
| 4174 |
| 4175 ``` |
| 4176 A list of target labels. |
| 4177 |
| 4178 This list contains target label patterns that should be filtered out |
| 4179 when creating the bundle. Any target matching one of those label will |
| 4180 be removed from the dependencies of the create_bundle target. |
| 4181 |
| 4182 This is mostly useful when creating application extension bundle as |
| 4183 the application extension has access to runtime resources from the |
| 4184 application bundle and thus do not require a second copy. |
| 4185 |
| 4186 See "gn help create_bundle" for more information. |
| 4187 |
| 4188 ``` |
| 4189 |
| 4190 ### **Example** |
| 4191 |
| 4192 ``` |
| 4193 create_bundle("today_extension") { |
| 4194 deps = [ |
| 4195 "//base" |
| 4196 ] |
| 4197 bundle_root_dir = "$root_out_dir/today_extension.appex" |
| 4198 bundle_deps_filter = [ |
| 4199 # The extension uses //base but does not use any function calling |
| 4200 # into third_party/icu and thus does not need the icudtl.dat file. |
| 4201 "//third_party/icu:icudata", |
| 4202 ] |
| 4203 } |
| 4204 |
| 4205 |
| 4206 ``` |
4178 ## **bundle_executable_dir**: Expansion of {{bundle_executable_dir}} in create_b
undle. | 4207 ## **bundle_executable_dir**: Expansion of {{bundle_executable_dir}} in create_b
undle. |
4179 | 4208 |
4180 ``` | 4209 ``` |
4181 A string corresponding to a path in $root_build_dir. | 4210 A string corresponding to a path in $root_build_dir. |
4182 | 4211 |
4183 This string is used by the "create_bundle" target to expand the | 4212 This string is used by the "create_bundle" target to expand the |
4184 {{bundle_executable_dir}} of the "bundle_data" target it depends on. | 4213 {{bundle_executable_dir}} of the "bundle_data" target it depends on. |
4185 This must correspond to a path under "bundle_root_dir". | 4214 This must correspond to a path under "bundle_root_dir". |
4186 | 4215 |
4187 See "gn help bundle_root_dir" for examples. | 4216 See "gn help bundle_root_dir" for examples. |
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4823 propagated across all types of targets, and public_configs | 4852 propagated across all types of targets, and public_configs |
4824 are always propagated across public deps of all types of targets. | 4853 are always propagated across public deps of all types of targets. |
4825 | 4854 |
4826 Data dependencies are propagated differently. See | 4855 Data dependencies are propagated differently. See |
4827 "gn help data_deps" and "gn help runtime_deps". | 4856 "gn help data_deps" and "gn help runtime_deps". |
4828 | 4857 |
4829 See also "public_deps". | 4858 See also "public_deps". |
4830 | 4859 |
4831 | 4860 |
4832 ``` | 4861 ``` |
4833 ## **bundle_deps_filter**: [label list] A list of labels that are filtered out. | |
4834 | |
4835 ``` | |
4836 A list of target labels. | |
4837 | |
4838 This list contains target label patterns that should be filtered out | |
4839 when creating the bundle. Any target matching one of those label will | |
4840 be removed from the dependencies of the create_bundle target. | |
4841 | |
4842 This is mostly useful when creating application extension bundle as | |
4843 the application extension has access to runtime resources from the | |
4844 application bundle and thus do not require a second copy. | |
4845 | |
4846 See "gn help create_bundle" for more information. | |
4847 | |
4848 ``` | |
4849 | |
4850 ### **Example** | |
4851 | |
4852 ``` | |
4853 create_bundle("today_extension") { | |
4854 deps = [ | |
4855 "//base" | |
4856 ] | |
4857 bundle_root_dir = "$root_out_dir/today_extension.appex" | |
4858 bundle_deps_filter = [ | |
4859 # The extension uses //base but does not use any function calling | |
4860 # into third_party/icu and thus does not need the icudtl.dat file. | |
4861 "//third_party/icu:icudata", | |
4862 ] | |
4863 } | |
4864 | |
4865 | |
4866 ``` | |
4867 ## **include_dirs**: Additional include directories. | 4862 ## **include_dirs**: Additional include directories. |
4868 | 4863 |
4869 ``` | 4864 ``` |
4870 A list of source directories. | 4865 A list of source directories. |
4871 | 4866 |
4872 The directories in this list will be added to the include path for | 4867 The directories in this list will be added to the include path for |
4873 the files in the affected target. | 4868 the files in the affected target. |
4874 | 4869 |
4875 ``` | 4870 ``` |
4876 | 4871 |
(...skipping 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5734 If specified, arguments from the --args command line flag are used. If | 5729 If specified, arguments from the --args command line flag are used. If |
5735 that flag is not specified, args from previous builds in the build | 5730 that flag is not specified, args from previous builds in the build |
5736 directory will be used (this is in the file args.gn in the build | 5731 directory will be used (this is in the file args.gn in the build |
5737 directory). | 5732 directory). |
5738 | 5733 |
5739 Last, for targets being compiled with a non-default toolchain, the | 5734 Last, for targets being compiled with a non-default toolchain, the |
5740 toolchain overrides are applied. These are specified in the | 5735 toolchain overrides are applied. These are specified in the |
5741 toolchain_args section of a toolchain definition. The use-case for | 5736 toolchain_args section of a toolchain definition. The use-case for |
5742 this is that a toolchain may be building code for a different | 5737 this is that a toolchain may be building code for a different |
5743 platform, and that it may want to always specify Posix, for example. | 5738 platform, and that it may want to always specify Posix, for example. |
5744 See "gn help toolchain_args" for more. | 5739 See "gn help toolchain" for more. |
5745 | 5740 |
5746 If you specify an override for a build argument that never appears in | 5741 If you specify an override for a build argument that never appears in |
5747 a "declare_args" call, a nonfatal error will be displayed. | 5742 a "declare_args" call, a nonfatal error will be displayed. |
5748 | 5743 |
5749 ``` | 5744 ``` |
5750 | 5745 |
5751 ### **Examples** | 5746 ### **Examples** |
5752 | 5747 |
5753 ``` | 5748 ``` |
5754 gn args out/FooBar | 5749 gn args out/FooBar |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5864 "//doom_melon/*", # Check everything in this subtree. | 5859 "//doom_melon/*", # Check everything in this subtree. |
5865 "//tools:mind_controlling_ant", # Check this specific target. | 5860 "//tools:mind_controlling_ant", # Check this specific target. |
5866 ] | 5861 ] |
5867 | 5862 |
5868 root = "//:root" | 5863 root = "//:root" |
5869 | 5864 |
5870 secondary_source = "//build/config/temporary_buildfiles/" | 5865 secondary_source = "//build/config/temporary_buildfiles/" |
5871 | 5866 |
5872 | 5867 |
5873 ``` | 5868 ``` |
5874 ## **GN build language grammar** | 5869 ## **Language and grammar for GN build files** |
5875 | 5870 |
5876 ### **Tokens** | 5871 ### **Tokens** |
5877 | 5872 |
5878 ``` | 5873 ``` |
5879 GN build files are read as sequences of tokens. While splitting the | 5874 GN build files are read as sequences of tokens. While splitting the |
5880 file into tokens, the next token is the longest sequence of characters | 5875 file into tokens, the next token is the longest sequence of characters |
5881 that form a valid token. | 5876 that form a valid token. |
5882 | 5877 |
5883 ``` | 5878 ``` |
5884 | 5879 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5944 | 5939 |
5945 \" U+0022 quotation mark | 5940 \" U+0022 quotation mark |
5946 \$ U+0024 dollar sign | 5941 \$ U+0024 dollar sign |
5947 \\ U+005C backslash | 5942 \\ U+005C backslash |
5948 | 5943 |
5949 All other backslashes represent themselves. | 5944 All other backslashes represent themselves. |
5950 | 5945 |
5951 To insert an arbitrary byte value, use $0xFF. For example, to | 5946 To insert an arbitrary byte value, use $0xFF. For example, to |
5952 insert a newline character: "Line one$0x0ALine two". | 5947 insert a newline character: "Line one$0x0ALine two". |
5953 | 5948 |
| 5949 An expansion will evaluate the variable following the '$' and insert |
| 5950 a stringified version of it into the result. For example, to concat |
| 5951 two path components with a slash separating them: |
| 5952 "$var_one/$var_two" |
| 5953 Use the "${var_one}" format to be explicitly deliniate the variable |
| 5954 for otherwise-ambiguous cases. |
| 5955 |
5954 ``` | 5956 ``` |
5955 | 5957 |
5956 ### **Punctuation** | 5958 ### **Punctuation** |
5957 | 5959 |
5958 ``` | 5960 ``` |
5959 The following character sequences represent punctuation: | 5961 The following character sequences represent punctuation: |
5960 | 5962 |
5961 + += == != ( ) | 5963 + += == != ( ) |
5962 - -= < <= [ ] | 5964 - -= < <= [ ] |
5963 ! = > >= { } | 5965 ! = > >= { } |
5964 && || . , | 5966 && || . , |
5965 | 5967 |
5966 ``` | 5968 ``` |
5967 | 5969 |
5968 ### **Grammar** | 5970 ### **Grammar** |
5969 | 5971 |
5970 ``` | 5972 ``` |
5971 The input tokens form a syntax tree following a context-free grammar: | 5973 The input tokens form a syntax tree following a context-free grammar: |
5972 | 5974 |
5973 File = StatementList . | 5975 File = StatementList . |
5974 | 5976 |
5975 Statement = Assignment | Call | Condition . | 5977 Statement = Assignment | Call | Condition . |
5976 Assignment = identifier AssignOp Expr . | 5978 LValue = identifier | ArrayAccess | ScopeAccess . |
| 5979 Assignment = LValue AssignOp Expr . |
5977 Call = identifier "(" [ ExprList ] ")" [ Block ] . | 5980 Call = identifier "(" [ ExprList ] ")" [ Block ] . |
5978 Condition = "if" "(" Expr ")" Block | 5981 Condition = "if" "(" Expr ")" Block |
5979 [ "else" ( Condition | Block ) ] . | 5982 [ "else" ( Condition | Block ) ] . |
5980 Block = "{" StatementList "}" . | 5983 Block = "{" StatementList "}" . |
5981 StatementList = { Statement } . | 5984 StatementList = { Statement } . |
5982 | 5985 |
5983 ArrayAccess = identifier "[" { identifier | integer } "]" . | 5986 ArrayAccess = identifier "[" Expr "]" . |
5984 ScopeAccess = identifier "." identifier . | 5987 ScopeAccess = identifier "." identifier . |
5985 Expr = UnaryExpr | Expr BinaryOp Expr . | 5988 Expr = UnaryExpr | Expr BinaryOp Expr . |
5986 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr . | 5989 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr . |
5987 PrimaryExpr = identifier | integer | string | Call | 5990 PrimaryExpr = identifier | integer | string | Call |
5988 | ArrayAccess | ScopeAccess | 5991 | ArrayAccess | ScopeAccess | Block |
5989 | "(" Expr ")" | 5992 | "(" Expr ")" |
5990 | "[" [ ExprList [ "," ] ] "]" . | 5993 | "[" [ ExprList [ "," ] ] "]" . |
5991 ExprList = Expr { "," Expr } . | 5994 ExprList = Expr { "," Expr } . |
5992 | 5995 |
5993 AssignOp = "=" | "+=" | "-=" . | 5996 AssignOp = "=" | "+=" | "-=" . |
5994 UnaryOp = "!" . | 5997 UnaryOp = "!" . |
5995 BinaryOp = "+" | "-" // highest priority | 5998 BinaryOp = "+" | "-" // highest priority |
5996 | "<" | "<=" | ">" | ">=" | 5999 | "<" | "<=" | ">" | ">=" |
5997 | "==" | "!=" | 6000 | "==" | "!=" |
5998 | "&&" | 6001 | "&&" |
5999 | "||" . // lowest priority | 6002 | "||" . // lowest priority |
6000 | 6003 |
6001 All binary operators are left-associative. | 6004 All binary operators are left-associative. |
6002 | 6005 |
| 6006 ``` |
| 6007 |
| 6008 ### **Types** |
| 6009 |
| 6010 ``` |
| 6011 The GN language is dynamically typed. The following types are used: |
| 6012 |
| 6013 - Boolean: Uses the keywords "true" and "false". There is no |
| 6014 implicit conversion between booleans and integers. |
| 6015 |
| 6016 - Integers: All numbers in GN are signed 64-bit integers. |
| 6017 |
| 6018 - Strings: Strings are 8-bit with no enforced encoding. When a string |
| 6019 is used to interact with other systems with particular encodings |
| 6020 (like the Windows and Mac filesystems) it is assumed to be UTF-8. |
| 6021 See "String literals" above for more. |
| 6022 |
| 6023 - Lists: Lists are arbitrary-length ordered lists of values. See |
| 6024 "Lists" below for more. |
| 6025 |
| 6026 - Scopes: Scopes are like dictionaries that use variable names for |
| 6027 keys. See "Scopes" below for more. |
| 6028 |
| 6029 ``` |
| 6030 |
| 6031 ### **Lists** |
| 6032 |
| 6033 ``` |
| 6034 Lists are created with [] and using commas to separate items: |
| 6035 |
| 6036 mylist = [ 0, 1, 2, "some string" ] |
| 6037 |
| 6038 A comma after the last item is optional. Lists are dereferenced using |
| 6039 0-based indexing: |
| 6040 |
| 6041 mylist[0] += 1 |
| 6042 var = mylist[2] |
| 6043 |
| 6044 Lists can be concatenated using the '+' and '+=' operators. Bare |
| 6045 values can not be concatenated with lists, to add a single item, |
| 6046 it must be put into a list of length one. |
| 6047 |
| 6048 Items can be removed from lists using the '-' and '-=' operators. |
| 6049 This will remove all occurrences of every item in the right-hand list |
| 6050 from the left-hand list. It is an error to remove an item not in the |
| 6051 list. This is to prevent common typos and to detect dead code that |
| 6052 is removing things that no longer apply. |
| 6053 |
| 6054 It is an error to use '=' to replace a nonempty list with another |
| 6055 nonempty list. This is to prevent accidentally overwriting data |
| 6056 when in most cases '+=' was intended. To overwrite a list on purpose, |
| 6057 first assign it to the empty list: |
| 6058 |
| 6059 mylist = [] |
| 6060 mylist = otherlist |
| 6061 |
| 6062 When assigning to a list named 'sources' using '=' or '+=', list |
| 6063 items may be automatically filtered out. |
| 6064 See "gn help set_sources_assignment_filter" for more. |
| 6065 |
| 6066 ``` |
| 6067 |
| 6068 ### **Scopes** |
| 6069 |
| 6070 ``` |
| 6071 All execution happens in the context of a scope which holds the |
| 6072 current state (like variables). With the exception of loops and |
| 6073 conditions, '{' introduces a new scope that has a parent reference to |
| 6074 the old scope. |
| 6075 |
| 6076 Variable reads recursively search all nested scopes until the |
| 6077 variable is found or there are no more scopes. Variable writes always |
| 6078 go into the current scope. This means that after the closing '}' |
| 6079 (again excepting loops and conditions), all local variables will be |
| 6080 restored to the previous values. This also means that "foo = foo" |
| 6081 can do useful work by copying a variable into the current scope that |
| 6082 was defined in a containing scope. |
| 6083 |
| 6084 Scopes can also be assigned to variables. Such scopes can be created |
| 6085 by functions like exec_script, when invoking a template (the template |
| 6086 code refers to the variables set by the invoking code by the |
| 6087 implicitly-created "invoker" scope), or explicitly like: |
| 6088 |
| 6089 empty_scope = {} |
| 6090 myvalues = { |
| 6091 foo = 21 |
| 6092 bar = "something" |
| 6093 } |
| 6094 |
| 6095 Inside such a scope definition can be any GN code including |
| 6096 conditionals and function calls. After the close of the scope, it will |
| 6097 contain all variables explicitly set by the code contained inside it. |
| 6098 After this, the values can be read, modified, or added to: |
| 6099 |
| 6100 myvalues.foo += 2 |
| 6101 empty_scope.new_thing = [ 1, 2, 3 ] |
| 6102 |
6003 | 6103 |
6004 ``` | 6104 ``` |
6005 ## **input_conversion**: Specifies how to transform input to a variable. | 6105 ## **input_conversion**: Specifies how to transform input to a variable. |
6006 | 6106 |
6007 ``` | 6107 ``` |
6008 input_conversion is an argument to read_file and exec_script that | 6108 input_conversion is an argument to read_file and exec_script that |
6009 specifies how the result of the read operation should be converted | 6109 specifies how the result of the read operation should be converted |
6010 into a variable. | 6110 into a variable. |
6011 | 6111 |
6012 "" (the default) | 6112 "" (the default) |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6355 ** \--root**: Explicitly specify source root. | 6455 ** \--root**: Explicitly specify source root. |
6356 ** \--runtime-deps-list-file**: Save runtime dependencies for targets in file. | 6456 ** \--runtime-deps-list-file**: Save runtime dependencies for targets in file. |
6357 ** \--script-executable**: Set the executable used to execute scripts. | 6457 ** \--script-executable**: Set the executable used to execute scripts. |
6358 ** \--threads**: Specify number of worker threads. | 6458 ** \--threads**: Specify number of worker threads. |
6359 ** \--time**: Outputs a summary of how long everything took. | 6459 ** \--time**: Outputs a summary of how long everything took. |
6360 ** \--tracelog**: Writes a Chrome-compatible trace log to the given file. | 6460 ** \--tracelog**: Writes a Chrome-compatible trace log to the given file. |
6361 ** -v**: Verbose logging. | 6461 ** -v**: Verbose logging. |
6362 ** \--version**: Prints the GN version number and exits. | 6462 ** \--version**: Prints the GN version number and exits. |
6363 | 6463 |
6364 ``` | 6464 ``` |
OLD | NEW |