1# GN Language and Operation
2
3[TOC]
4
5## Introduction
6
7This page describes many of the language details and behaviors.
8
9### Use the built-in help!
10
11GN has an extensive built-in help system which provides a reference for
12every function and built-in variable. This page is more high-level.
13
14```
15gn help
16```
17
18You can also see the
19[slides](https://docs.google.com/presentation/d/15Zwb53JcncHfEwHpnG_PoIbbzQ3GQi_cpujYwbpcbZo/edit?usp=sharing)
20from a March, 2016 introduction to GN. The speaker notes contain the full
21content.
22
23### Design philosophy
24
25  * Writing build files should not be a creative endeavour. Ideally two
26    people should produce the same buildfile given the same
27    requirements. There should be no flexibility unless it's absolutely
28    needed. As many things should be fatal errors as possible.
29
30  * The definition should read more like code than rules. I don't want
31    to write or debug Prolog. But everybody on our team can write and
32    debug C++ and Python.
33
34  * The build language should be opinionated as to how the build should
35    work. It should not necessarily be easy or even possible to express
36    arbitrary things. We should be changing source and tooling to make
37    the build simpler rather than making everything more complicated to
38    conform to external requirements (within reason).
39
40  * Be like Blaze when it makes sense (see "Differences and similarities
41    to Blaze" below).
42
43## Language
44
45GN uses an extremely simple, dynamically typed language. The types are:
46
47  * Boolean (`true`, `false`).
48  * 64-bit signed integers.
49  * Strings.
50  * Lists (of any other types).
51  * Scopes (sort of like a dictionary, only for built-in stuff).
52
53There are some built-in variables whose values depend on the current
54environment. See `gn help` for more.
55
56There are purposefully many omissions in the language. There are no
57user-defined function calls, for example (templates are the closest thing). As
58per the above design philosophy, if you need this kind of thing you're probably
59doing it wrong.
60
61The full grammar for language nerds is available in `gn help grammar`.
62
63### Strings
64
65Strings are enclosed in double-quotes and use backslash as the escape
66character. The only escape sequences supported are:
67
68  * `\"` (for literal quote)
69  * `\$` (for literal dollars sign)
70  * `\\` (for literal backslash)
71
72Any other use of a backslash is treated as a literal backslash. So, for
73example, `\b` used in patterns does not need to be escaped, nor do most Windows
74paths like `"C:\foo\bar.h"`.
75
76Simple variable substitution is supported via `$`, where the word
77following the dollars sign is replaced with the value of the variable.
78You can optionally surround the name with `{}` if there is not a
79non-variable-name character to terminate the variable name. More complex
80expressions are not supported, only variable name substitution.
81
82```
83a = "mypath"
84b = "$a/foo.cc"  # b -> "mypath/foo.cc"
85c = "foo${a}bar.cc"  # c -> "foomypathbar.cc"
86```
87
88You can encode 8-bit characters using "$0xFF" syntax, so a string with newlines
89(hex 0A) would `"look$0x0Alike$0x0Athis"`.
90
91### Lists
92
93Aside from telling empty lists from non empty lists (`a == []`), there is no
94way to get the length of a list. If you find yourself wanting to do this kind
95of thing, you're trying to do too much work in the build.
96
97Lists support appending:
98
99```
100a = [ "first" ]
101a += [ "second" ]  # [ "first", "second" ]
102a += [ "third", "fourth" ]  # [ "first", "second", "third", "fourth" ]
103b = a + [ "fifth" ]  # [ "first", "second", "third", "fourth", "fifth" ]
104```
105
106Appending a list to another list appends the items in the second list
107rather than appending the list as a nested member.
108
109You can remove items from a list:
110
111```
112a = [ "first", "second", "third", "first" ]
113b = a - [ "first" ]  # [ "second", "third" ]
114a -= [ "second" ]  # [ "first", "third", "first" ]
115```
116
117The - operator on a list searches for matches and removes all matching
118items. Subtracting a list from another list will remove each item in the
119second list.
120
121If no matching items are found, an error will be thrown, so you need to
122know in advance that the item is there before removing it. Given that
123there is no way to test for inclusion, the main use-case is to set up a
124master list of files or flags, and to remove ones that don't apply to
125the current build based on various conditions.
126
127Stylistically, prefer to only add to lists and have each source file or
128dependency appear once. This is the opposite of the advice Chrome-team used to
129give for GYP (GYP would prefer to list all files, and then remove the ones you
130didn't want in conditionals).
131
132Lists support zero-based subscripting to extract values:
133
134```
135a = [ "first", "second", "third" ]
136b = a[1]  # -> "second"
137```
138
139The \[\] operator is read-only and can not be used to mutate the
140list. The primary use-case of this is when an external script returns
141several known values and you want to extract them.
142
143There are some cases where it's easy to overwrite a list when you mean
144to append to it instead. To help catch this case, it is an error to
145assign a nonempty list to a variable containing an existing nonempty
146list. If you want to get around this restriction, first assign the
147destination variable to the empty list.
148
149```
150a = [ "one" ]
151a = [ "two" ]  # Error: overwriting nonempty list with a nonempty list.
152a = []         # OK
153a = [ "two" ]  # OK
154```
155
156Note that execution of the build script is done without intrinsic
157knowledge of the meaning of the underlying data. This means that it
158doesn't know that `sources` is a list of file names, for example. So if
159you remove an item, it must match the literal string rather than
160specifying a different name that will resolve to the same file name.
161
162### Conditionals
163
164Conditionals look like C:
165
166```
167  if (is_linux || (is_win && target_cpu == "x86")) {
168    sources -= [ "something.cc" ]
169  } else if (...) {
170    ...
171  } else {
172    ...
173  }
174```
175
176You can use them in most places, even around entire targets if the
177target should only be declared in certain circumstances.
178
179### Looping
180
181You can iterate over a list with `foreach`. This is discouraged. Most things
182the build should do can normally be expressed without doing this, and if you
183find it necessary it may be an indication you're doing too much work in the
184metabuild.
185
186```
187foreach(i, mylist) {
188  print(i)  # Note: i is a copy of each element, not a reference to it.
189}
190```
191
192### Function calls
193
194Simple function calls look like most other languages:
195
196```
197print("hello, world")
198assert(is_win, "This should only be executed on Windows")
199```
200
201Such functions are built-in and the user can not define new ones.
202
203Some functions take a block of code enclosed by `{ }` following them:
204
205```
206static_library("mylibrary") {
207  sources = [ "a.cc" ]
208}
209```
210
211Most of these define targets. The user can define new functions like this
212with the template mechanism discussed below.
213
214Precisely, this expression means that the block becomes an argument to the
215function for the function to execute. Most of the block-style functions execute
216the block and treat the resulting scope as a dictionary of variables to read.
217
218### Scoping and execution
219
220Files and function calls followed by `{ }` blocks introduce new scopes. Scopes
221are nested. When you read a variable, the containing scopes will be searched in
222reverse order until a matching name is found. Variable writes always go to the
223innermost scope.
224
225There is no way to modify any enclosing scope other than the innermost
226one. This means that when you define a target, for example, nothing you
227do inside of the block will "leak out" into the rest of the file.
228
229`if`/`else`/`foreach` statements, even though they use `{ }`, do not introduce
230a new scope so changes will persist outside of the statement.
231
232## Naming things
233
234### File and directory names
235
236File and directory names are strings and are interpreted as relative to
237the current build file's directory. There are three possible forms:
238
239Relative names:
240
241```
242"foo.cc"
243"src/foo.cc"
244"../src/foo.cc"
245```
246
247Source-tree absolute names:
248
249```
250"//net/foo.cc"
251"//base/test/foo.cc"
252```
253
254System absolute names (rare, normally used for include directories):
255
256```
257"/usr/local/include/"
258"/C:/Program Files/Windows Kits/Include"
259```
260
261## Build configuration
262
263## Targets
264
265A target is a node in the build graph. It usually represents some kind
266of executable or library file that will be generated. Targets depend on
267other targets. The built-in target types (see `gn help <targettype>` for
268more help) are:
269
270  * `action`: Run a script to generate a file.
271  * `action_foreach`: Run a script once for each source file.
272  * `bundle_data`: Declare data to go into a Mac/iOS bundle.
273  * `create_bundle`: Creates a Mac/iOS bundle.
274  * `executable`: Generates an executable file.
275  * `group`: A virtual dependency node that refers to one or more other
276    targets.
277  * `shared_library`: A .dll or .so.
278  * `loadable_module`: A .dll or .so loadable only at runtime.
279  * `source_set`: A lightweight virtual static library (usually
280    preferrable over a real static library since it will build faster).
281  * `static_library`: A .lib or .a file (normally you'll want a
282    `source_set` instead).
283
284You can extend this to make custom target types using templates (see below). In
285Chrome some of the more commonly-used templates are:
286
287  * `component`: Either a source set or shared library, depending on the
288    build type.
289  * `test`: A test executable. On mobile this will create the appropriate
290    native app type for tests.
291  * `app`: Executable or Mac/iOS application.
292  * `android_apk`: Make an APK. There are a _lot_ of other Android ones, see
293    `//build/config/android/rules.gni`.
294
295## Configs
296
297Configs are named objects that specify sets of flags, include
298directories, and defines. They can be applied to a target and pushed to
299dependent targets.
300
301To define a config:
302
303```
304config("myconfig") {
305  includes = [ "src/include" ]
306  defines = [ "ENABLE_DOOM_MELON" ]
307}
308```
309
310To apply a config to a target:
311
312```
313executable("doom_melon") {
314  configs = [ ":myconfig" ]
315}
316```
317
318It is common for the build config file to specify target defaults that
319set a default list of configs. Targets can add or remove to this list as
320needed. So in practice you would usually use `configs += ":myconfig"` to
321append to the list of defaults.
322
323See `gn help config` for more information about how configs are declared
324and applied.
325
326### Public configs
327
328A target can apply settings to other targets that depend on it. The most
329common example is a third party target that requires some defines or
330include directories for its headers to compile properly. You want these
331settings to apply both to the compile of the third party library itself,
332as well as all targets that use the library.
333
334To do this, you write a config with the settings you want to apply:
335
336```
337config("my_external_library_config") {
338  includes = "."
339  defines = [ "DISABLE_JANK" ]
340}
341```
342
343Then this config is added to the target as a "public" config. It will
344apply both to the target as well as targets that directly depend on it.
345
346```
347shared_library("my_external_library") {
348  ...
349  # Targets that depend on this get this config applied.
350  public_configs = [ ":my_external_library_config" ]
351}
352```
353
354Dependent targets can in turn forward this up the dependency tree
355another level by adding your target as a "public" dependency.
356
357```
358static_library("intermediate_library") {
359  ...
360  # Targets that depend on this one also get the configs from "my external library".
361  public_deps = [ ":my_external_library" ]
362}
363```
364
365A target can forward a config to all dependents until a link boundary is
366reached by setting it as an `all_dependent_config`. This is strongly
367discouraged as it can spray flags and defines over more of the build than
368necessary. Instead, use public_deps to control which flags apply where.
369
370In Chrome, prefer the build flag header system (`build/buildflag_header.gni`)
371for defines which prevents most screw-ups with compiler defines.
372
373## Templates
374
375Templates are GN's primary way to re-use code. Typically, a template
376would expand to one or more other target types.
377
378```
379# Declares a script that compiles IDL files to source, and then compiles those
380# source files.
381template("idl") {
382  # Always base helper targets on target_name so they're unique. Target name
383  # will be the string passed as the name when the template is invoked.
384  idl_target_name = "${target_name}_generate"
385  action_foreach(idl_target_name) {
386    ...
387  }
388
389  # Your template should always define a target with the name target_name.
390  # When other targets depend on your template invocation, this will be the
391  # destination of that dependency.
392  source_set(target_name) {
393    ...
394    deps = [ ":$idl_target_name" ]  # Require the sources to be compiled.
395  }
396}
397```
398
399Typically your template definition would go in a `.gni` file and users
400would import that file to see the template definition:
401
402```
403import("//tools/idl_compiler.gni")
404
405idl("my_interfaces") {
406  sources = [ "a.idl", "b.idl" ]
407}
408```
409
410Declaring a template creates a closure around the variables in scope at
411that time. When the template is invoked, the magic variable `invoker` is
412used to read variables out of the invoking scope. The template would
413generally copy the values its interested in into its own scope:
414
415```
416template("idl") {
417  source_set(target_name) {
418    sources = invoker.sources
419  }
420}
421```
422
423The current directory when a template executes will be that of the
424invoking build file rather than the template source file. This is so
425files passed in from the template invoker will be correct (this
426generally accounts for most file handling in a template). However, if
427the template has files itself (perhaps it generates an action that runs
428a script), you will want to use absolute paths ("//foo/...") to refer to
429these files to account for the fact that the current directory will be
430unpredictable during invocation. See `gn help template` for more
431information and more complete examples.
432
433## Other features
434
435### Imports
436
437You can import `.gni` files into the current scope with the `import`
438function. This is _not_ an include in the C++ sense. The imported file is
439executed independently and the resulting scope is copied into the current file
440(C++ executes the included file in the current context of when the
441include directive appeared). This allows the results of the import to be
442cached, and also prevents some of the more "creative" uses of includes like
443multiply-included files.
444
445Typically, a `.gni` would define build arguments and templates. See `gn
446help import` for more.
447
448Your `.gni` file can define temporary variables that are not exported files
449that include it by using a preceding underscore in the name like `_this`.
450
451### Path processing
452
453Often you will want to make a file name or a list of file names relative
454to a different directory. This is especially common when running
455scripts, which are executed with the build output directory as the
456current directory, while build files usually refer to files relative to
457their containing directory.
458
459You can use `rebase_path` to convert directories. See `gn help
460rebase_path` for more help and examples. Typical usage to convert a file
461name relative to the current directory to be relative to the root build
462directory would be: ``` new_paths = rebase_path("myfile.c",
463root_build_dir) ```
464
465### Patterns
466
467Patterns are used to generate the output file names for a given set of
468inputs for custom target types, and to automatically remove files from
469the list values (see `gn help filter_include` and `gn help filter_exclude`).
470
471They are like simple regular expressions. See `gn help label_pattern`
472for more.
473
474### Executing scripts
475
476There are two ways to execute scripts. All external scripts in GN are in
477Python. The first way is as a build step. Such a script would take some
478input and generate some output as part of the build. Targets that invoke
479scripts are declared with the "action" target type (see `gn help
480action`).
481
482The second way to execute scripts is synchronously during build file
483execution. This is necessary in some cases to determine the set of files
484to compile, or to get certain system configurations that the build file
485might depend on. The build file can read the stdout of the script and
486act on it in different ways.
487
488Synchronous script execution is done by the `exec_script` function (see
489`gn help exec_script` for details and examples). Because synchronously
490executing a script requires that the current buildfile execution be
491suspended until a Python process completes execution, relying on
492external scripts is slow and should be minimized.
493
494To prevent abuse, files permitted to call `exec_script` can be whitelisted in
495the toplevel `.gn` file. Chrome does this to require additional code review
496for such additions. See `gn help dotfile`.
497
498You can synchronously read and write files which is discouraged but
499occasionally necessary when synchronously running scripts. The typical use-case
500would be to pass a list of file names longer than the command-line limits of
501the current platform. See `gn help read_file` and `gn help write_file` for how
502to read and write files. These functions should be avoided if at all possible.
503
504Actions that exceed command-line length limits can use response files to
505get around this limitation without synchronously writing files. See
506`gn help response_file_contents`.
507
508# Differences and similarities to Blaze
509
510Blaze is Google's internal build system, now publicly released as
511[Bazel](http://bazel.io/). It has inspired a number of other systems such as
512[Pants](http://www.pantsbuild.org/) and [Buck](http://facebook.github.io/buck/).
513
514In Google's homogeneous environment, the need for conditionals is very
515low and they can get by with a few hacks (`abi_deps`). Chrome uses
516conditionals all over the place and the need to add these is the main
517reason for the files looking different.
518
519GN also adds the concept of "configs" to manage some of the trickier
520dependency and configuration problems which likewise don't arise on the
521server. Blaze has a concept of a "configuration" which is like a GN
522toolchain, but built into the tool itself. The way that toolchains work
523in GN is a result of trying to separate this concept out into the build
524files in a clean way.
525
526GN keeps some GYP concept like "all dependent" settings which work a bit
527differently in Blaze. This is partially to make conversion from the existing
528GYP code easier, and the GYP constructs generally offer more fine-grained
529control (which is either good or bad, depending on the situation).
530
531GN also uses GYP names like "sources" instead of "srcs" since
532abbreviating this seems needlessly obscure, although it uses Blaze's
533"deps" since "dependencies" is so hard to type. Chromium also compiles
534multiple languages in one target so specifying the language type on the
535target name prefix was dropped (e.g. from `cc_library`).
536