blob: fc99e861ba9e2bdd0b5a9581294e7a3ed880d4cd [file] [log] [blame] [view]
dprankec04440d2015-04-06 11:42:23 -07001# GN Quick Start guide
2
3[TOC]
4
5## Running GN
6
Brett Wilson2a181332020-03-20 13:38:18 -07007You 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.
dprankec04440d2015-04-06 11:42:23 -070019
20## Setting up a build
21
Brett Wilson2a181332020-03-20 13:38:18 -070022Unlike 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.
dprankec04440d2015-04-06 11:42:23 -070029
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
Brett Wilson2a181332020-03-20 13:38:18 -070051The 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
dprankec04440d2015-04-06 11:42:23 -070054
55```
56gn args --list out/my_build
57```
58
Quinten Yearsley030b7292017-07-18 16:13:31 +000059on the command line. Note that you have to specify the build directory
60for this command because the available arguments can change according
Brett Wilson2a181332020-03-20 13:38:18 -070061to the build.
dprankec04440d2015-04-06 11:42:23 -070062
brettwb6392ef2015-09-11 13:09:27 -070063Chrome developers can also read the [Chrome-specific build
64configuration](http://www.chromium.org/developers/gn-build-configuration)
65instructions for more information.
66
dmazzoni6fe6eb92015-07-16 12:46:05 -070067## 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
Brett Wilson2a181332020-03-20 13:38:18 -070081See [GN cross compiles](cross_compiles.md) for more info.
dprankec04440d2015-04-06 11:42:23 -070082
83## Step-by-step
84
85### Adding a build file
86
Brett Wilson2a181332020-03-20 13:38:18 -070087Go 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.
dprankec04440d2015-04-06 11:42:23 -070093
94```
Brett Wilson2a181332020-03-20 13:38:18 -070095executable("tutorial") {
dprankec04440d2015-04-06 11:42:23 -070096 sources = [
Brett Wilson2a181332020-03-20 13:38:18 -070097 "tutorial.cc",
dprankec04440d2015-04-06 11:42:23 -070098 ]
99}
100```
101
Brett Wilson2a181332020-03-20 13:38:18 -0700102Now 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:
dprankec04440d2015-04-06 11:42:23 -0700112
113```
Brett Wilson2a181332020-03-20 13:38:18 -0700114group("tools") {
dprankec04440d2015-04-06 11:42:23 -0700115 deps = [
Brett Wilson2a181332020-03-20 13:38:18 -0700116 # 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",
dprankec04440d2015-04-06 11:42:23 -0700119 ]
120}
121```
122
dprankec04440d2015-04-06 11:42:23 -0700123### Testing your addition
124
Brett Wilson2a181332020-03-20 13:38:18 -0700125From the command line in the `simple_build` directory:
dprankec04440d2015-04-06 11:42:23 -0700126
127```
Brett Wilson2a181332020-03-20 13:38:18 -0700128gn gen out
129ninja -C out tutorial
130out/tutorial
dprankec04440d2015-04-06 11:42:23 -0700131```
132
Drew Stonebraker4dc0ce42019-01-18 15:29:38 -0800133You 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:
dprankec04440d2015-04-06 11:42:23 -0700138
139```
Brett Wilson2a181332020-03-20 13:38:18 -0700140ninja -C out some/path/to/target:my_target
dprankec04440d2015-04-06 11:42:23 -0700141```
142
143### Declaring dependencies
144
Brett Wilson2a181332020-03-20 13:38:18 -0700145Let'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()`:
dprankec04440d2015-04-06 11:42:23 -0700148
149```
Brett Wilson2a181332020-03-20 13:38:18 -0700150static_library("hello_static") {
dprankec04440d2015-04-06 11:42:23 -0700151 sources = [
Brett Wilson2a181332020-03-20 13:38:18 -0700152 "hello_static.cc",
153 "hello_static.h",
dprankec04440d2015-04-06 11:42:23 -0700154 ]
155}
156```
157
Brett Wilson2a181332020-03-20 13:38:18 -0700158There is also a shared library that defines one function `GetSharedText()`:
dprankec04440d2015-04-06 11:42:23 -0700159
160```
Brett Wilson2a181332020-03-20 13:38:18 -0700161shared_library("hello_shared") {
dprankec04440d2015-04-06 11:42:23 -0700162 sources = [
Brett Wilson2a181332020-03-20 13:38:18 -0700163 "hello_shared.cc",
164 "hello_shared.h",
dprankec04440d2015-04-06 11:42:23 -0700165 ]
Brett Wilson2a181332020-03-20 13:38:18 -0700166
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
dprankec04440d2015-04-06 11:42:23 -0700189 deps = [
Brett Wilson2a181332020-03-20 13:38:18 -0700190 ":hello_shared",
191 ":hello_static",
dprankec04440d2015-04-06 11:42:23 -0700192 ]
193}
194```
195
andybons696d8472015-07-30 15:38:01 -0700196This executable includes one source file and depends on the previous
Brett Wilson2a181332020-03-20 13:38:18 -0700197two libraries. Labels starting with a colon refer to a target with that name in
198the current BUILD.gn file.
dprankec04440d2015-04-06 11:42:23 -0700199
Brett Wilson2a181332020-03-20 13:38:18 -0700200### Test the binary
dprankec04440d2015-04-06 11:42:23 -0700201
Brett Wilson2a181332020-03-20 13:38:18 -0700202From the command line in the `simple_build` directory:
dprankec04440d2015-04-06 11:42:23 -0700203
204```
Brett Wilson2a181332020-03-20 13:38:18 -0700205ninja -C out hello
206out/hello
dprankec04440d2015-04-06 11:42:23 -0700207```
208
andybons696d8472015-07-30 15:38:01 -0700209Note that you **didn't** need to re-run GN. GN will automatically rebuild
dprankec04440d2015-04-06 11:42:23 -0700210the 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
dprankec04440d2015-04-06 11:42:23 -0700214### Putting settings in a config
215
Brett Wilson2a181332020-03-20 13:38:18 -0700216Users 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):
dprankec04440d2015-04-06 11:42:23 -0700219
220```
Brett Wilson2a181332020-03-20 13:38:18 -0700221config("my_lib_config") {
222 defines = [ "ENABLE_DOOM_MELON" ]
223 include_dirs = [ "//third_party/something" ]
dprankec04440d2015-04-06 11:42:23 -0700224}
225```
226
Brett Wilson2a181332020-03-20 13:38:18 -0700227To apply a config's settings to a target, add it to the `configs` list:
dprankec04440d2015-04-06 11:42:23 -0700228
229```
Brett Wilson2a181332020-03-20 13:38:18 -0700230static_library("hello_shared") {
dprankec04440d2015-04-06 11:42:23 -0700231 ...
Brett Wilson2a181332020-03-20 13:38:18 -0700232 # Note "+=" here is usually required, see "default configs" below.
dprankec04440d2015-04-06 11:42:23 -0700233 configs += [
Brett Wilson2a181332020-03-20 13:38:18 -0700234 ":my_lib_config",
dprankec04440d2015-04-06 11:42:23 -0700235 ]
236}
237```
238
Brett Wilson2a181332020-03-20 13:38:18 -0700239A config can be applied to all targets that depend on the current one by putting
240its label in the `public_configs` list:
dprankec04440d2015-04-06 11:42:23 -0700241
242```
Brett Wilson2a181332020-03-20 13:38:18 -0700243static_library("hello_shared") {
244 ...
245 public_configs = [
246 ":my_lib_config",
dprankec04440d2015-04-06 11:42:23 -0700247 ]
248}
249```
250
Brett Wilson2a181332020-03-20 13:38:18 -0700251The `public_configs` also applies to the current target, so there's no need to
252list a config in both places.
dprankec04440d2015-04-06 11:42:23 -0700253
Brett Wilson2a181332020-03-20 13:38:18 -0700254### 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:
dprankec04440d2015-04-06 11:42:23 -0700259
260```
Brett Wilson2a181332020-03-20 13:38:18 -0700261executable("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")
dprankec04440d2015-04-06 11:42:23 -0700292```
293
jessicag2ab47422016-03-11 11:38:17 -0800294## 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
dprankec04440d2015-04-06 11:42:23 -0700312## 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
dprankec04440d2015-04-06 11:42:23 -0700317### The "desc" command
318
319You can run `gn desc <build_dir> <targetname>` to get information about
320a given target:
321
322```
Brett Wilson2a181332020-03-20 13:38:18 -0700323gn desc out/Default //foo/bar:say_hello
dprankec04440d2015-04-06 11:42:23 -0700324```
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```
Brett Wilson2a181332020-03-20 13:38:18 -0700331> gn desc out/Default //foo/bar:say_hello defines --blame
dprankec04440d2015-04-06 11:42:23 -0700332...lots of other stuff omitted...
Brett Wilson2a181332020-03-20 13:38:18 -0700333 From //foo/bar:hello_config
334 (Added by //foo/bar/BUILD.gn:12)
dprankec04440d2015-04-06 11:42:23 -0700335 TWO_PEOPLE
336```
337
dprankec04440d2015-04-06 11:42:23 -0700338Another particularly interesting variation:
339
340```
341gn desc out/Default //base:base_i18n deps --tree
342```
343
344See `gn help desc` for more.