Delete historical data usage from store when user clears data usage.
[chromium-blink-merge.git] / tools / gn / docs / quick_start.md
blobae06b96826d8683aab640080a3a0cc469f8b5b1d
1 # GN Quick Start guide
3 [TOC]
5 ## Running GN
7 You just run `gn` from the command line. There is a script in
8 depot\_tools (which is presumably on your path) with this name. The
9 script will find the binary in the source tree containing the current
10 directory and run it.
12 ## Setting up a build
14 In GYP, the system would generate `Debug` and `Release` build
15 directories for you and configure them accordingly. GN doesn't do this.
16 Instead, you set up whatever build directory you want with whatever
17 configuration you want. The Ninja files will be automatically
18 regenerated if they're out of date when you build in that directory.
20 To make a build directory:
22 ```
23 gn gen out/my_build
24 ```
26 ## Passing build arguments
28 Set build arguments on your build directory by running:
30 ```
31 gn args out/my_build
32 ```
34 This will bring up an editor. Type build args into that file like this:
36 ```
37 is_component_build = true
38 is_debug = false
39 ```
41 You can see the list of available arguments and their default values by
42 typing
44 ```
45 gn args --list out/my_build
46 ```
48 on the command line. See "Taking build arguments" below for information
49 on how to use these in your code. (Note that you have to specify the
50 build directory for this command because the available arguments can
51 change according to what's set.
53 Chrome developers can also read the [Chrome-specific build
54 configuration](http://www.chromium.org/developers/gn-build-configuration)
55 instructions for more information.
57 ## Cross-compiling to a target OS or architecture
59 Run `gn args out/Default` (substituting your build directory as needed) and
60 add one or more of the following lines for common cross-compiling options.
62 ```
63 target_os = "chromeos"
64 target_os = "android"
66 target_cpu = "arm"
67 target_cpu = "x86"
68 target_cpu = "x64"
69 ```
71 See [GNCrossCompiles](cross_compiles.md) for more info.
73 ## Configuring goma
76 Run `gn args out/Default` (substituting your build directory as needed).
77 Add:
79 ```
80 use_goma = true
81 goma_dir = "~/foo/bar/goma"
82 ```
84 If your goma is in the default location (`~/goma`) then you can omit the
85 `goma_dir` line.
87 ## Configuring component mode
89 This is a build arg like the goma flags. run `gn args out/Default` and add:
91 ```
92 is_component_build = true
93 ```
95 ## Step-by-step
97 ### Adding a build file
99 Create a `tools/gn/tutorial/BUILD.gn` file and enter the following:
102 executable("hello_world") {
103   sources = [
104     "hello_world.cc",
105   ]
109 There should already be a `hello_world.cc` file in that directory,
110 containing what you expect. That's it! Now we just need to tell the
111 build about this file. Open the `BUILD.gn` file in the root directory
112 and add the label of this target to the dependencies of one of the root
113 groups (a "group" target is a meta-target that is just a collection of
114 other targets):
117 group("root") {
118   deps = [
119     ...
120     "//url",
121     "//tools/gn/tutorial:hello_world",
122   ]
126 You can see the label of your target is "//" (indicating the source
127 root), followed by the directory name, a colon, and the target name.
129 ### Testing your addition
131 From the command line in the source root directory:
134 gn gen out/Default
135 ninja -C out/Default hello_world
136 out/Default/hello_world
139 GN encourages target names for static libraries that aren't globally
140 unique. To build one of these, you can pass the label with no leading
141 "//" to ninja:
144 ninja -C out/Default tools/gn/tutorial:hello_world
147 ### Declaring dependencies
149 Let's make a static library that has a function to say hello to random
150 people. There is a source file `hello.cc` in that directory which has a
151 function to do this. Open the `tools/gn/tutorial/BUILD.gn` file and add
152 the static library to the bottom of the existing file:
155 static_library("hello") {
156   sources = [
157     "hello.cc",
158   ]
162 Now let's add an executable that depends on this library:
165 executable("say_hello") {
166   sources = [
167     "say_hello.cc",
168   ]
169   deps = [
170     ":hello",
171   ]
175 This executable includes one source file and depends on the previous
176 static library. The static library is referenced by its label in the
177 `deps`. You could have used the full label `//tools/gn/tutorial:hello`
178 but if you're referencing a target in the same build file, you can use
179 the shortcut `:hello`.
181 ### Test the static library version
183 From the command line in the source root directory:
186 ninja -C out/Default say_hello
187 out/Default/say_hello
190 Note that you **didn't** need to re-run GN. GN will automatically rebuild
191 the ninja files when any build file has changed. You know this happens
192 when ninja prints `[1/1] Regenerating ninja files` at the beginning of
193 execution.
195 ### Compiler settings
197 Our hello library has a new feature, the ability to say hello to two
198 people at once. This feature is controlled by defining `TWO_PEOPLE`. We
199 can add defines like so:
202 static_library("hello") {
203   sources = [
204     "hello.cc",
205   ]
206   defines = [
207     "TWO_PEOPLE",
208   ]
212 ### Putting settings in a config
214 However, users of the library also need to know about this define, and
215 putting it in the static library target defines it only for the files
216 there. If somebody else includes `hello.h`, they won't see the new
217 definition. To see the new definition, everybody will have to define
218 `TWO_PEOPLE`.
220 GN has a concept called a "config" which encapsulates settings. Let's
221 create one that defines our preprocessor define:
224 config("hello_config") {
225   defines = [
226     "TWO_PEOPLE",
227   ]
231 To apply these settings to your target, you only need to add the
232 config's label to the list of configs in the target:
235 static_library("hello") {
236   ...
237   configs += [
238     ":hello_config",
239   ]
243 Note that you need "+=" here instead of "=" since the build
244 configuration has a default set of configs applied to each target that
245 set up the default build stuff. You want to add to this list rather than
246 overwrite it. To see the default configs, you can use the `print`
247 function in the build file or the `desc` command-line subcommand (see
248 below for examples of both).
250 ### Dependent configs
252 This nicely encapsulates our settings, but still requires everybody that
253 uses our library to set the config on themselves. It would be nice if
254 everybody that depends on our `hello` library can get this
255 automatically. Change your library definition to:
258 static_library("hello") {
259   sources = [
260     "hello.cc",
261   ]
262   all_dependent_configs = [
263     ":hello_config"
264   ]
268 This applies the `hello_config` to the `hello` target itself, plus all
269 targets that depend on transitively depend on the current one. Now
270 everybody that depends on us will get our settings. You can also set
271 `public_configs` which applies only to targets that directly
272 depend on your target (not transitively).
274 Now if you compile and run, you'll see the new version with two people:
277 > ninja -C out/Default say_hello
278 ninja: Entering directory 'out/Default'
279 [1/1] Regenerating ninja files
280 [4/4] LINK say_hello
281 > out/Default/say_hello
282 Hello, Bill and Joy.
285 ## Don't know what's going on?
287 You can run GN in verbose mode to see lots of messages about what it's
288 doing. Use `-v` for this.
290 ### Print debugging
292 There is a `print` command which just writes to stdout:
295 static_library("hello") {
296   ...
297   print(configs)
301 This will print all of the configs applying to your target (including
302 the default ones).
304 ### The "desc" command
306 You can run `gn desc <build_dir> <targetname>` to get information about
307 a given target:
310 gn desc out/Default //tools/gn/tutorial:say_hello
313 will print out lots of exciting information. You can also print just one
314 section. Lets say you wanted to know where your `TWO_PEOPLE` define
315 came from on the `say_hello` target:
318 > gn desc out/Default //tools/gn/tutorial:say_hello defines --blame
319 ...lots of other stuff omitted...
320   From //tools/gn/tutorial:hello_config
321        (Added by //tools/gn/tutorial/BUILD.gn:12)
322     TWO_PEOPLE
325 You can see that `TWO_PEOPLE` was defined by a config, and you can also
326 see the which like caused that config to be applied to your target (in
327 this case, the `all_dependent_configs` line).
329 Another particularly interesting variation:
332 gn desc out/Default //base:base_i18n deps --tree
335 See `gn help desc` for more.
337 ### Performance
339 You can see what took a long time by running it with the --time command
340 line flag. This will output a summary of timings for various things.
342 You can also make a trace of how the build files were executed:
345 gn --tracelog=mylog.trace
348 and you can load the resulting file in Chrome's `about:tracing` page to
349 look at everything.