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 1235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1246 print(i) | 1246 print(i) |
1247 } | 1247 } |
1248 | 1248 |
1249 Prints: | 1249 Prints: |
1250 a | 1250 a |
1251 b | 1251 b |
1252 c | 1252 c |
1253 | 1253 |
1254 | 1254 |
1255 ``` | 1255 ``` |
| 1256 ## **forward_variables_from**: Copies variables from a different scope. |
| 1257 |
| 1258 ``` |
| 1259 forward_variables_from(from_scope, variable_list_or_star) |
| 1260 |
| 1261 Copies the given variables from the given scope to the local scope |
| 1262 if they exist. This is normally used in the context of templates to |
| 1263 use the values of variables defined in the template invocation to |
| 1264 a template-defined target. |
| 1265 |
| 1266 The variables in the given variable_list will be copied if they exist |
| 1267 in the given scope or any enclosing scope. If they do not exist, |
| 1268 nothing will happen and they be left undefined in the current scope. |
| 1269 |
| 1270 As a special case, if the variable_list is a string with the value of |
| 1271 "*", all variables from the given scope will be copied. "*" only |
| 1272 copies variables set directly on the from_scope, not enclosing ones. |
| 1273 Otherwise it would duplicate all global variables. |
| 1274 |
| 1275 When an explicit list of variables is supplied, if the variable exists |
| 1276 in the current (destination) scope already, an error will be thrown. |
| 1277 If "*" is specified, variables in the current scope will be |
| 1278 clobbered (the latter is important because most targets have an |
| 1279 implicit configs list, which means it wouldn't work at all if it |
| 1280 didn't clobber). |
| 1281 |
| 1282 The sources assignment filter (see "gn help set_sources_assignment_filter") |
| 1283 is never applied by this function. It's assumed than any desired |
| 1284 filtering was already done when sources was set on the from_scope. |
| 1285 |
| 1286 ``` |
| 1287 |
| 1288 ### **Examples** |
| 1289 |
| 1290 ``` |
| 1291 # This is a common action template. It would invoke a script with |
| 1292 # some given parameters, and wants to use the various types of deps |
| 1293 # and the visibility from the invoker if it's defined. It also injects |
| 1294 # an additional dependency to all targets. |
| 1295 template("my_test") { |
| 1296 action(target_name) { |
| 1297 forward_variables_from(invoker, [ "data_deps", "deps", |
| 1298 "public_deps", "visibility" ]) |
| 1299 # Add our test code to the dependencies. |
| 1300 # "deps" may or may not be defined at this point. |
| 1301 if (defined(deps)) { |
| 1302 deps += [ "//tools/doom_melon" ] |
| 1303 } else { |
| 1304 deps = [ "//tools/doom_melon" ] |
| 1305 } |
| 1306 } |
| 1307 } |
| 1308 |
| 1309 # This is a template around either a target whose type depends on a |
| 1310 # global variable. It forwards all values from the invoker. |
| 1311 template("my_wrapper") { |
| 1312 target(my_wrapper_target_type, target_name) { |
| 1313 forward_variables_from(invoker, "*") |
| 1314 } |
| 1315 } |
| 1316 |
| 1317 |
| 1318 ``` |
1256 ## **get_label_info**: Get an attribute from a target's label. | 1319 ## **get_label_info**: Get an attribute from a target's label. |
1257 | 1320 |
1258 ``` | 1321 ``` |
1259 get_label_info(target_label, what) | 1322 get_label_info(target_label, what) |
1260 | 1323 |
1261 Given the label of a target, returns some attribute of that target. | 1324 Given the label of a target, returns some attribute of that target. |
1262 The target need not have been previously defined in the same file, | 1325 The target need not have been previously defined in the same file, |
1263 since none of the attributes depend on the actual target definition, | 1326 since none of the attributes depend on the actual target definition, |
1264 only the label itself. | 1327 only the label itself. |
1265 | 1328 |
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1988 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc, | 2051 Flags: cflags, cflags_c, cflags_cc, cflags_objc, cflags_objcc, |
1989 defines, include_dirs, ldflags, lib_dirs, libs | 2052 defines, include_dirs, ldflags, lib_dirs, libs |
1990 precompiled_header, precompiled_source | 2053 precompiled_header, precompiled_source |
1991 Deps: data_deps, deps, forward_dependent_configs_from, public_deps | 2054 Deps: data_deps, deps, forward_dependent_configs_from, public_deps |
1992 Dependent configs: all_dependent_configs, public_configs | 2055 Dependent configs: all_dependent_configs, public_configs |
1993 General: check_includes, configs, data, inputs, output_name, | 2056 General: check_includes, configs, data, inputs, output_name, |
1994 output_extension, public, sources, testonly, visibility | 2057 output_extension, public, sources, testonly, visibility |
1995 | 2058 |
1996 | 2059 |
1997 ``` | 2060 ``` |
| 2061 ## **target**: Declare an target with the given programmatic type. |
| 2062 |
| 2063 ``` |
| 2064 target(target_type_string, target_name_string) { ... } |
| 2065 |
| 2066 The target() function is a way to invoke a built-in target or template |
| 2067 with a type determined at runtime. This is useful for cases where the |
| 2068 type of a target might not be known statically. |
| 2069 |
| 2070 Only templates and built-in target functions are supported for the |
| 2071 target_type_string parameter. Arbitrary functions, configs, and |
| 2072 toolchains are not supported. |
| 2073 |
| 2074 The call: |
| 2075 target("source_set", "doom_melon") { |
| 2076 Is equivalent to: |
| 2077 source_set("doom_melon") { |
| 2078 |
| 2079 ``` |
| 2080 |
| 2081 ### **Example** |
| 2082 |
| 2083 ``` |
| 2084 if (foo_build_as_shared) { |
| 2085 my_type = "shared_library" |
| 2086 } else { |
| 2087 my_type = "source_set" |
| 2088 } |
| 2089 |
| 2090 target(my_type, "foo") { |
| 2091 ... |
| 2092 } |
| 2093 |
| 2094 |
| 2095 ``` |
1998 ## **template**: Define a template rule. | 2096 ## **template**: Define a template rule. |
1999 | 2097 |
2000 ``` | 2098 ``` |
2001 A template defines a custom name that acts like a function. It | 2099 A template defines a custom name that acts like a function. It |
2002 provides a way to add to the built-in target types. | 2100 provides a way to add to the built-in target types. |
2003 | 2101 |
2004 The template() function is used to declare a template. To invoke the | 2102 The template() function is used to declare a template. To invoke the |
2005 template, just use the name of the template like any other target | 2103 template, just use the name of the template like any other target |
2006 type. | 2104 type. |
2007 | 2105 |
2008 Often you will want to declare your template in a special file that | 2106 Often you will want to declare your template in a special file that |
2009 other files will import (see "gn help import") so your template | 2107 other files will import (see "gn help import") so your template |
2010 rule can be shared across build files. | 2108 rule can be shared across build files. |
2011 | 2109 |
2012 ``` | 2110 ``` |
2013 | 2111 |
2014 ### **More details**: | 2112 ### **Variables and templates**: |
2015 | 2113 |
2016 ``` | 2114 ``` |
2017 When you call template() it creates a closure around all variables | 2115 When you call template() it creates a closure around all variables |
2018 currently in scope with the code in the template block. When the | 2116 currently in scope with the code in the template block. When the |
2019 template is invoked, the closure will be executed. | 2117 template is invoked, the closure will be executed. |
2020 | 2118 |
2021 When the template is invoked, the code in the caller is executed and | 2119 When the template is invoked, the code in the caller is executed and |
2022 passed to the template code as an implicit "invoker" variable. The | 2120 passed to the template code as an implicit "invoker" variable. The |
2023 template uses this to read state out of the invoking code. | 2121 template uses this to read state out of the invoking code. |
2024 | 2122 |
2025 One thing explicitly excluded from the closure is the "current | 2123 One thing explicitly excluded from the closure is the "current |
2026 directory" against which relative file names are resolved. The | 2124 directory" against which relative file names are resolved. The |
2027 current directory will be that of the invoking code, since typically | 2125 current directory will be that of the invoking code, since typically |
2028 that code specifies the file names. This means all files internal | 2126 that code specifies the file names. This means all files internal |
2029 to the template should use absolute names. | 2127 to the template should use absolute names. |
2030 | 2128 |
| 2129 A template will typically forward some or all variables from the |
| 2130 invoking scope to a target that it defines. Often, such variables |
| 2131 might be optional. Use the pattern: |
| 2132 |
| 2133 if (defined(invoker.deps)) { |
| 2134 deps = invoker.deps |
| 2135 } |
| 2136 |
| 2137 The function forward_variables_from() provides a shortcut to forward |
| 2138 one or more or possibly all variables in this manner: |
| 2139 |
| 2140 forward_variables_from(invoker, ["deps", "public_deps"]) |
| 2141 |
2031 ``` | 2142 ``` |
2032 | 2143 |
2033 ### **Target naming**: | 2144 ### **Target naming**: |
2034 | 2145 |
2035 ``` | 2146 ``` |
2036 Your template should almost always define a built-in target with the | 2147 Your template should almost always define a built-in target with the |
2037 name the template invoker specified. For example, if you have an IDL | 2148 name the template invoker specified. For example, if you have an IDL |
2038 template and somebody does: | 2149 template and somebody does: |
2039 idl("foo") {... | 2150 idl("foo") {... |
2040 you will normally want this to expand to something defining a | 2151 you will normally want this to expand to something defining a |
(...skipping 2293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4334 Leading zeros and negative zero are disallowed. | 4445 Leading zeros and negative zero are disallowed. |
4335 | 4446 |
4336 ``` | 4447 ``` |
4337 | 4448 |
4338 ### **String literals** | 4449 ### **String literals** |
4339 | 4450 |
4340 ``` | 4451 ``` |
4341 A string literal represents a string value consisting of the quoted | 4452 A string literal represents a string value consisting of the quoted |
4342 characters with possible escape sequences and variable expansions. | 4453 characters with possible escape sequences and variable expansions. |
4343 | 4454 |
4344 string = `"` { char | escape | expansion } `"` . | 4455 string = `"` { char | escape | expansion } `"` . |
4345 escape = `\` ( "$" | `"` | char ) . | 4456 escape = `\` ( "$" | `"` | char ) . |
4346 expansion = "$" ( identifier | "{" identifier "}" ) . | 4457 BracketExpansion = "{" ( identifier | ArrayAccess | ScopeAccess ) "}" . |
4347 char = /* any character except "$", `"`, or newline */ . | 4458 expansion = "$" ( identifier | BracketExpansion ) . |
| 4459 char = /* any character except "$", `"`, or newline */ . |
4348 | 4460 |
4349 After a backslash, certain sequences represent special characters: | 4461 After a backslash, certain sequences represent special characters: |
4350 | 4462 |
4351 \" U+0022 quotation mark | 4463 \" U+0022 quotation mark |
4352 \$ U+0024 dollar sign | 4464 \$ U+0024 dollar sign |
4353 \\ U+005C backslash | 4465 \\ U+005C backslash |
4354 | 4466 |
4355 All other backslashes represent themselves. | 4467 All other backslashes represent themselves. |
4356 | 4468 |
4357 ``` | 4469 ``` |
(...skipping 18 matching lines...) Expand all Loading... |
4376 File = StatementList . | 4488 File = StatementList . |
4377 | 4489 |
4378 Statement = Assignment | Call | Condition . | 4490 Statement = Assignment | Call | Condition . |
4379 Assignment = identifier AssignOp Expr . | 4491 Assignment = identifier AssignOp Expr . |
4380 Call = identifier "(" [ ExprList ] ")" [ Block ] . | 4492 Call = identifier "(" [ ExprList ] ")" [ Block ] . |
4381 Condition = "if" "(" Expr ")" Block | 4493 Condition = "if" "(" Expr ")" Block |
4382 [ "else" ( Condition | Block ) ] . | 4494 [ "else" ( Condition | Block ) ] . |
4383 Block = "{" StatementList "}" . | 4495 Block = "{" StatementList "}" . |
4384 StatementList = { Statement } . | 4496 StatementList = { Statement } . |
4385 | 4497 |
| 4498 ArrayAccess = identifier "[" { identifier | integer } "]" . |
| 4499 ScopeAccess = identifier "." identifier . |
4386 Expr = UnaryExpr | Expr BinaryOp Expr . | 4500 Expr = UnaryExpr | Expr BinaryOp Expr . |
4387 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr . | 4501 UnaryExpr = PrimaryExpr | UnaryOp UnaryExpr . |
4388 PrimaryExpr = identifier | integer | string | Call | 4502 PrimaryExpr = identifier | integer | string | Call |
4389 | identifier "[" Expr "]" | 4503 | ArrayAccess | ScopeAccess |
4390 | identifier "." identifier | |
4391 | "(" Expr ")" | 4504 | "(" Expr ")" |
4392 | "[" [ ExprList [ "," ] ] "]" . | 4505 | "[" [ ExprList [ "," ] ] "]" . |
4393 ExprList = Expr { "," Expr } . | 4506 ExprList = Expr { "," Expr } . |
4394 | 4507 |
4395 AssignOp = "=" | "+=" | "-=" . | 4508 AssignOp = "=" | "+=" | "-=" . |
4396 UnaryOp = "!" . | 4509 UnaryOp = "!" . |
4397 BinaryOp = "+" | "-" // highest priority | 4510 BinaryOp = "+" | "-" // highest priority |
4398 | "<" | "<=" | ">" | ">=" | 4511 | "<" | "<=" | ">" | ">=" |
4399 | "==" | "!=" | 4512 | "==" | "!=" |
4400 | "&&" | 4513 | "&&" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4505 at build generation time via "--runtime-deps-list-file" | 4618 at build generation time via "--runtime-deps-list-file" |
4506 (see "gn help --runtime-deps-list-file"). | 4619 (see "gn help --runtime-deps-list-file"). |
4507 | 4620 |
4508 To a first approximation, the runtime dependencies of a target are | 4621 To a first approximation, the runtime dependencies of a target are |
4509 the set of "data" files, data directories, and the shared libraries | 4622 the set of "data" files, data directories, and the shared libraries |
4510 from all transitive dependencies. Executables and shared libraries are | 4623 from all transitive dependencies. Executables and shared libraries are |
4511 considered runtime dependencies of themselves. | 4624 considered runtime dependencies of themselves. |
4512 | 4625 |
4513 ``` | 4626 ``` |
4514 | 4627 |
4515 ### **Details** | 4628 ### **Executables** |
4516 | 4629 |
4517 ``` | 4630 ``` |
4518 Executable targets and those executable targets' transitive | 4631 Executable targets and those executable targets' transitive |
4519 dependencies are not considered unless that executable is listed in | 4632 dependencies are not considered unless that executable is listed in |
4520 "data_deps". Otherwise, GN assumes that the executable (and | 4633 "data_deps". Otherwise, GN assumes that the executable (and |
4521 everything it requires) is a build-time dependency only. | 4634 everything it requires) is a build-time dependency only. |
4522 | 4635 |
| 4636 ``` |
| 4637 |
| 4638 ### **Actions and copies** |
| 4639 |
| 4640 ``` |
4523 Action and copy targets that are listed as "data_deps" will have all | 4641 Action and copy targets that are listed as "data_deps" will have all |
4524 of their outputs and data files considered as runtime dependencies. | 4642 of their outputs and data files considered as runtime dependencies. |
4525 Action and copy targets that are "deps" or "public_deps" will have | 4643 Action and copy targets that are "deps" or "public_deps" will have |
4526 only their data files considered as runtime dependencies. These | 4644 only their data files considered as runtime dependencies. These |
4527 targets can list an output file in both the "outputs" and "data" | 4645 targets can list an output file in both the "outputs" and "data" |
4528 lists to force an output file as a runtime dependency in all cases. | 4646 lists to force an output file as a runtime dependency in all cases. |
4529 | 4647 |
| 4648 The different rules for deps and data_deps are to express build-time |
| 4649 (deps) vs. run-time (data_deps) outputs. If GN counted all build-time |
| 4650 copy steps as data dependencies, there would be a lot of extra stuff, |
| 4651 and if GN counted all run-time dependencies as regular deps, the |
| 4652 build's parallelism would be unnecessarily constrained. |
| 4653 |
| 4654 This rule can sometimes lead to unintuitive results. For example, |
| 4655 given the three targets: |
| 4656 A --[data_deps]--> B --[deps]--> ACTION |
| 4657 GN would say that A does not have runtime deps on the result of the |
| 4658 ACTION, which is often correct. But the purpose of the B target might |
| 4659 be to collect many actions into one logic unit, and the "data"-ness |
| 4660 of A's dependency is lost. Solutions: |
| 4661 |
| 4662 - List the outputs of the action in it's data section (if the |
| 4663 results of that action are always runtime files). |
| 4664 - Have B list the action in data_deps (if the outputs of the actions |
| 4665 are always runtime files). |
| 4666 - Have B list the action in both deps and data deps (if the outputs |
| 4667 might be used in both contexts and you don't care about unnecessary |
| 4668 entries in the list of files required at runtime). |
| 4669 - Split B into run-time and build-time versions with the appropriate |
| 4670 "deps" for each. |
| 4671 |
| 4672 ``` |
| 4673 |
| 4674 ### **Static libraries and source sets** |
| 4675 |
| 4676 ``` |
4530 The results of static_library or source_set targets are not considered | 4677 The results of static_library or source_set targets are not considered |
4531 runtime dependencies since these are assumed to be intermediate | 4678 runtime dependencies since these are assumed to be intermediate |
4532 targets only. If you need to list a static library as a runtime | 4679 targets only. If you need to list a static library as a runtime |
4533 dependency, you can manually compute the .a/.lib file name for the | 4680 dependency, you can manually compute the .a/.lib file name for the |
4534 current platform and list it in the "data" list of a target | 4681 current platform and list it in the "data" list of a target |
4535 (possibly on the static library target itself). | 4682 (possibly on the static library target itself). |
4536 | 4683 |
| 4684 ``` |
| 4685 |
| 4686 ### **Multiple outputs** |
| 4687 |
| 4688 ``` |
4537 When a tool produces more than one output, only the first output | 4689 When a tool produces more than one output, only the first output |
4538 is considered. For example, a shared library target may produce a | 4690 is considered. For example, a shared library target may produce a |
4539 .dll and a .lib file on Windows. Only the .dll file will be considered | 4691 .dll and a .lib file on Windows. Only the .dll file will be considered |
4540 a runtime dependency. | 4692 a runtime dependency. This applies only to linker tools, scripts and |
| 4693 copy steps with multiple outputs will also get all outputs listed. |
4541 | 4694 |
4542 | 4695 |
4543 ``` | 4696 ``` |
4544 ## **How Source Expansion Works** | 4697 ## **How Source Expansion Works** |
4545 | 4698 |
4546 ``` | 4699 ``` |
4547 Source expansion is used for the action_foreach and copy target types | 4700 Source expansion is used for the action_foreach and copy target types |
4548 to map source file names to output file names or arguments. | 4701 to map source file names to output file names or arguments. |
4549 | 4702 |
4550 To perform source expansion in the outputs, GN maps every entry in the | 4703 To perform source expansion in the outputs, GN maps every entry in the |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4671 ** --nocolor**: Force non-colored output. | 4824 ** --nocolor**: Force non-colored output. |
4672 ** -q**: Quiet mode. Don't print output on success. | 4825 ** -q**: Quiet mode. Don't print output on success. |
4673 ** --root**: Explicitly specify source root. | 4826 ** --root**: Explicitly specify source root. |
4674 ** --runtime-deps-list-file**: Save runtime dependencies for targets in file. | 4827 ** --runtime-deps-list-file**: Save runtime dependencies for targets in file. |
4675 ** --time**: Outputs a summary of how long everything took. | 4828 ** --time**: Outputs a summary of how long everything took. |
4676 ** --tracelog**: Writes a Chrome-compatible trace log to the given file. | 4829 ** --tracelog**: Writes a Chrome-compatible trace log to the given file. |
4677 ** -v**: Verbose logging. | 4830 ** -v**: Verbose logging. |
4678 ** --version**: Prints the GN version number and exits. | 4831 ** --version**: Prints the GN version number and exits. |
4679 | 4832 |
4680 ``` | 4833 ``` |
OLD | NEW |