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