• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# GN Quick Start guide
2
3[TOC]
4
5## Running GN
6
7You just run `gn` from the command line. For large projects, GN is versioned
8and distributed with the source checkout.
9
10  * For Chromium and Chromium-based projects, there is a script in
11    `depot_tools`, which is presumably in your PATH, with this name. The script
12    will find the binary in the source tree containing the current directory and
13    run it.
14
15  * For Fuchsia in-tree development, run `fx gn ...` which will find the right
16    GN binary and run it with the given arguments.
17
18  * For other projects, see your project's documentation.
19
20## Setting up a build
21
22Unlike some other build systems, with GN you set up your own build directories
23with the settings you want. This lets you maintain as many different builds in
24parallel as you need.
25
26Once you set up a build directory, the Ninja files will be automatically
27regenerated if they're out of date when you build in that directory so you
28should not have to re-run GN.
29
30To make a build directory:
31
32```
33gn gen out/my_build
34```
35
36## Passing build arguments
37
38Set build arguments on your build directory by running:
39
40```
41gn args out/my_build
42```
43
44This will bring up an editor. Type build args into that file like this:
45
46```
47is_component_build = true
48is_debug = false
49```
50
51The available variables will depend on your build (this example is from
52Chromium). You can see the list of available arguments and their default values
53by typing
54
55```
56gn args --list out/my_build
57```
58
59on the command line. Note that you have to specify the build directory
60for this command because the available arguments can change according
61to the build.
62
63Chrome developers can also read the [Chrome-specific build
64configuration](http://www.chromium.org/developers/gn-build-configuration)
65instructions for more information.
66
67## Cross-compiling to a target OS or architecture
68
69Run `gn args out/Default` (substituting your build directory as needed) and
70add one or more of the following lines for common cross-compiling options.
71
72```
73target_os = "chromeos"
74target_os = "android"
75
76target_cpu = "arm"
77target_cpu = "x86"
78target_cpu = "x64"
79```
80
81See [GN cross compiles](cross_compiles.md) for more info.
82
83## Step-by-step
84
85### Adding a build file
86
87Go to the directory `examples/simple_build`. This is the root of a minimal GN
88repository.
89
90In that directory there is a `tutorial` directory. There is already a
91`tutorial.cc` file that's not hooked up to the build. Create a new `BUILD.gn`
92file in that directory for our new target.
93
94```
95executable("tutorial") {
96  sources = [
97    "tutorial.cc",
98  ]
99}
100```
101
102Now we just need to tell the build about this new target. Open the `BUILD.gn`
103file in the parent (`simple_build`) directory. GN starts by loading this root
104file, and then loads all dependencies ourward from here, so we just need to add
105a reference to our new target from this file.
106
107You could add our new target as a dependency from one of the existing targets in
108the `simple_build/BUILD.gn` file, but it usually doesn't make a lot of sense to
109have an executable as a depdency of another executable (they can't be linked).
110So let's make a "tools" group. In GN, a "group" is just a collection of
111dependencies that's not complied or linked:
112
113```
114group("tools") {
115  deps = [
116    # This will expand to the name "//tutorial:tutorial" which is the full name
117    # of our new target. Run "gn help labels" for more.
118    "//tutorial",
119  ]
120}
121```
122
123### Testing your addition
124
125From the command line in the `simple_build` directory:
126
127```
128gn gen out
129ninja -C out tutorial
130out/tutorial
131```
132
133You should see "Hello, world." output to the console.
134
135Side note: GN encourages target names for static libraries that aren't globally
136unique. To build one of these, you can pass the label with its path (but no leading
137"//") to ninja:
138
139```
140ninja -C out some/path/to/target:my_target
141```
142
143### Declaring dependencies
144
145Let's look at the targets defined in
146[examples/simple_build/BUILD.gn](../examples/simple_build/BUILD.gn). There is a
147static library that defines one function, `GetStaticText()`:
148
149```
150static_library("hello_static") {
151  sources = [
152    "hello_static.cc",
153    "hello_static.h",
154  ]
155}
156```
157
158There is also a shared library that defines one function `GetSharedText()`:
159
160```
161shared_library("hello_shared") {
162  sources = [
163    "hello_shared.cc",
164    "hello_shared.h",
165  ]
166
167  defines = [ "HELLO_SHARED_IMPLEMENTATION" ]
168}
169```
170
171This also illustrates how to set preprocessor defines for a target. To set more
172than one or to assign values, use this form:
173
174```
175defines = [
176  "HELLO_SHARED_IMPLEMENTATION",
177  "ENABLE_DOOM_MELON=0",
178]
179```
180
181Now let's look at the executable that depends on these two libraries:
182
183```
184executable("hello") {
185  sources = [
186    "hello.cc",
187  ]
188
189  deps = [
190    ":hello_shared",
191    ":hello_static",
192  ]
193}
194```
195
196This executable includes one source file and depends on the previous
197two libraries. Labels starting with a colon refer to a target with that name in
198the current BUILD.gn file.
199
200### Test the binary
201
202From the command line in the `simple_build` directory:
203
204```
205ninja -C out hello
206out/hello
207```
208
209Note that you **didn't** need to re-run GN. GN will automatically rebuild
210the ninja files when any build file has changed. You know this happens
211when ninja prints `[1/1] Regenerating ninja files` at the beginning of
212execution.
213
214### Putting settings in a config
215
216Users of a library often need compiler flags, defines, and include directories
217applied to them. To do this, put all such settings into a "config" which is a
218named collection of settings (but not sources or dependencies):
219
220```
221config("my_lib_config") {
222  defines = [ "ENABLE_DOOM_MELON" ]
223  include_dirs = [ "//third_party/something" ]
224}
225```
226
227To apply a config's settings to a target, add it to the `configs` list:
228
229```
230static_library("hello_shared") {
231  ...
232  # Note "+=" here is usually required, see "default configs" below.
233  configs += [
234    ":my_lib_config",
235  ]
236}
237```
238
239A config can be applied to all targets that depend on the current one by putting
240its label in the `public_configs` list:
241
242```
243static_library("hello_shared") {
244  ...
245  public_configs = [
246    ":my_lib_config",
247  ]
248}
249```
250
251The `public_configs` also applies to the current target, so there's no need to
252list a config in both places.
253
254### Default configs
255
256The build configuration will set up some settings that apply to every target by
257default. These will normally be set as a default list of configs. You can see
258this using the "print" command which is useful for debugging:
259
260```
261executable("hello") {
262  print(configs)
263}
264```
265
266Running GN will print something like:
267
268```
269$ gn gen out
270["//build:compiler_defaults", "//build:executable_ldconfig"]
271Done. Made 5 targets from 5 files in 9ms
272```
273
274Targets can modify this list to change their defaults. For example, the build
275setup might turn off exceptions by default by adding a `no_exceptions` config,
276but a target might re-enable them by replacing it with a different one:
277
278```
279executable("hello") {
280  ...
281  configs -= [ "//build:no_exceptions" ]  # Remove global default.
282  configs += [ "//build:exceptions" ]  # Replace with a different one.
283}
284```
285
286Our print command from above could also be expressed using string interpolation.
287This is a way to convert values to strings. It uses the symbol "$" to refer to a
288variable:
289
290```
291print("The configs for the target $target_name are $configs")
292```
293
294## Add a new build argument
295
296You declare which arguments you accept and specify default values via
297`declare_args`.
298
299```
300declare_args() {
301  enable_teleporter = true
302  enable_doom_melon = false
303}
304```
305
306See `gn help buildargs` for an overview of how this works.
307See `gn help declare_args` for specifics on declaring them.
308
309It is an error to declare a given argument more than once in a given scope, so
310care should be used in scoping and naming arguments.
311
312## Don't know what's going on?
313
314You can run GN in verbose mode to see lots of messages about what it's
315doing. Use `-v` for this.
316
317### The "desc" command
318
319You can run `gn desc <build_dir> <targetname>` to get information about
320a given target:
321
322```
323gn desc out/Default //foo/bar:say_hello
324```
325
326will print out lots of exciting information. You can also print just one
327section. Lets say you wanted to know where your `TWO_PEOPLE` define
328came from on the `say_hello` target:
329
330```
331> gn desc out/Default //foo/bar:say_hello defines --blame
332...lots of other stuff omitted...
333  From //foo/bar:hello_config
334       (Added by //foo/bar/BUILD.gn:12)
335    TWO_PEOPLE
336```
337
338Another particularly interesting variation:
339
340```
341gn desc out/Default //base:base_i18n deps --tree
342```
343
344See `gn help desc` for more.
345