1# GN Style Guide 2 3[TOC] 4 5## Naming and ordering within the file 6 7### Location of build files 8 9It usually makes sense to have more build files closer to the code than 10fewer ones at the top level; this is in contrast with what we did with 11GYP. This makes things easier to find, and also makes the set of owners 12required for reviews smaller since changes are more focused to particular 13subdirectories. 14 15### Targets 16 17 * Most BUILD files should have a target with the same name as the 18 directory. This target should be the first target. 19 * Other targets should be in some logical order -- usually 20 more important targets will be first, and unit tests will follow the 21 corresponding target. If there's no clear ordering, consider 22 alphabetical order. 23 * Test support libraries should be static libraries named "test\_support". 24 For example, "//ui/compositor:test\_support". Test support libraries should 25 include as public deps the non-test-support version of the library 26 so tests need only depend on the test\_support target (rather than 27 both). 28 29Naming advice 30 31 * Targets and configs should be named using lowercase with underscores 32 separating words, unless there is a strong reason to do otherwise. 33 * Source sets, groups, and static libraries do not need globally unique names. 34 Prefer to give such targets short, non-redundant names without worrying 35 about global uniqueness. For example, it looks much better to write a 36 dependency as `"//mojo/public/bindings"` rather than 37 `"//mojo/public/bindings:mojo_bindings"` 38 * Shared libraries (and by extension, components) must have globally unique 39 output names. Give such targets short non-unique names above, and then 40 provide a globally unique `output_name` for that target. 41 * Executables and tests should be given a globally unique name. Technically 42 only the output names must be unique, but since only the output names 43 appear in the shell and on bots, it's much less confusing if the name 44 matches the other places the executable appears. 45 46### Configs 47 48 * A config associated with a single target should be named the same as 49 the target with `_config` following it. 50 * A config should appear immediately before the corresponding target 51 that uses it. 52 53### Example 54 55Example for the `src/foo/BUILD.gn` file: 56 57``` 58# Copyright 2016 The Chromium Authors. All rights reserved. 59# Use of this source code is governed by a BSD-style license that can be 60# found in the LICENSE file. 61 62# Config for foo is named foo_config and immediately precedes it in the file. 63config("foo_config") { 64} 65 66# Target matching path name is the first target. 67executable("foo") { 68} 69 70# Test for foo follows it. 71test("foo_unittests") { 72} 73 74config("bar_config") { 75} 76 77source_set("bar") { 78} 79``` 80 81## Ordering within a target 82 83 1. `output_name` / `visibility` / `testonly` 84 2. `sources` 85 3. `cflags`, `include_dirs`, `defines`, `configs` etc. in whatever 86 order makes sense to you. 87 4. `public_deps` 88 5. `deps` 89 90### Conditions 91 92Simple conditions affecting just one variable (e.g. adding a single 93source or adding a flag for one particular OS) can go beneath the 94variable they affect. More complicated conditions affecting more than 95one thing should go at the bottom. 96 97Conditions should be written to minimize the number of conditional blocks. 98 99## Formatting and indenting 100 101GN contains a built-in code formatter which defines the formatting style. 102Some additional notes: 103 104 * Variables are `lower_case_with_underscores`. 105 * Comments should be complete sentences with periods at the end. 106 * Compiler flags and such should always be commented with what they do 107 and why the flag is needed. 108 109### Sources 110 111Prefer to list sources only once. It is OK to conditionally include sources 112rather than listing them all at the top and then conditionally excluding them 113when they don't apply. Conditional inclusion is often clearer since a file is 114only listed once and it's easier to reason about when reading. 115 116``` 117 sources = [ 118 "main.cc", 119 ] 120 if (use_aura) { 121 sources += [ "thing_aura.cc" ] 122 } 123 if (use_gtk) { 124 sources += [ "thing_gtk.cc" ] 125 } 126``` 127 128### Deps 129 130 * Deps should be in alphabetical order. 131 * Deps within the current file should be written first and not 132 qualified with the file name (just `:foo`). 133 * Other deps should always use fully-qualified path names unless 134 relative ones are required for some reason. 135 * Prefer to omit the colon and name when possible. See the [GN implicit 136 names](reference.md#implicit-names). 137 138``` 139 deps = [ 140 ":a_thing", 141 ":mystatic", 142 "//foo/bar:other_thing", 143 "//foo/baz:that_thing", 144 ] 145``` 146 147### Import 148 149Use fully-qualified paths for imports: 150 151``` 152import("//foo/bar/baz.gni") # Even if this file is in the foo/bar directory 153``` 154 155## Usage 156 157### Source sets versus static libraries 158 159Source sets and static libraries can be used interchangeably in most cases. If 160you're unsure what to use, a source set is almost never wrong and is less likely 161to cause problems, but on a large project using the right kind of target can 162be important, so you should know about the following tradeoffs. 163 164Static libraries follow different linking rules. When a static library is 165included in a link, only the object files that contain unresolved symbols will 166be brought into the build. Source sets result in every object file being added 167to the link line of the final binary. 168 169 * If you're eventually linking code into a component, shared library, or 170 loadable module, you normally need to use source sets. This is because 171 object files with no symbols referenced from within the shared library will 172 not be linked into the final library at all. This omission will happen even 173 if that object file has a symbol marked for export that targets dependent 174 on that shared library need. This will result in undefined symbols when 175 linking later targets. 176 177 * Unit tests (and anything else with static initializers with side effects) 178 must use source sets. The gtest TEST macros create static initializers 179 that register the test. But since no code references symbols in the object 180 file, linking a test into a static library and then into a test executable 181 means the tests will get stripped. 182 183 * On some platforms, static libraries may involve duplicating all of the 184 data in the object files that comprise it. This takes more disk space and 185 for certain very large libraries in configurations with very large object 186 files can cause internal limits on the size of static libraries to be 187 exceeded. Source sets do not have this limitation. Some targets switch 188 between source sets and static libraries depending on the build 189 configuration to avoid this problem. Some platforms (or toolchains) may 190 support something called "thin archives" which don't have this problem; 191 but you can't rely on this as a portable solution. 192 193 * Source sets can have no sources, while static libraries will give strange 194 platform-specific errors if they have no sources. If a target has only 195 headers (for include checking purposes) or conditionally has no sources on 196 some platforms, use a source set. 197 198 * In cases where a lot of the symbols are not needed for a particular link 199 (this especially happens when linking test binaries), putting that code in 200 a static library can dramatically increase linking performance. This is 201 because the object files not needed for the link are never considered in 202 the first place, rather than forcing the linker to strip the unused code 203 in a later pass when nothing references it. 204 205### Components versus shared libraries versus source sets 206 207A component is a Chrome template (rather than a built-in GN concept) that 208expands either to a shared library or a static library / source set depending 209on the value of the `is_component_build` variable. This allows release builds 210to be linked statically in a large binary, but for developers to use shared 211libraries for most operations. Chrome developers should almost always use 212a component instead of shared library directly. 213 214Much like the source set versus static library tradeoff, there's no hard 215and fast rule as to when you should use a component or not. Using 216components can significantly speed up incremental builds by making 217linking much faster, but they require you to have to think about which 218symbols need to be exported from the target. 219 220### Loadable modules versus shared libraries 221 222A shared library will be listed on the link line of dependent targets and will 223be loaded automatically by the operating system when the application starts 224and symbols automatically resolved. A loadable module will not be linked 225directly and the application must manually load it. 226 227On Windows and Linux shared libraries and loadable modules result in the same 228type of file (`.dll` and `.so`, respectively). The only difference is in how 229they are linked to dependent targets. On these platforms, having a `deps` 230dependency on a loadable module is the same as having a `data_deps` 231(non-linked) dependency on a shared library. 232 233On Mac, these targets have different formats: a shared library will generate a 234`.dylib` file and a loadable module will generate a `.so` file. 235 236Use loadable modules for things like plugins. In the case of plugin-like 237libraries, it's good practice to use both a loadable module for the target type 238(even for platforms where it doesn't matter) and data deps for targets that 239depend on it so it's clear from both places that how the library will be linked 240and loaded. 241 242## Build arguments 243 244### Scope 245 246Build arguments should be scoped to a unit of behavior, e.g. enabling a feature. 247Typically an argument would be declared in an imported file to share it with 248the subset of the build that could make use of it. 249 250Chrome has many legacy flags in `//build/config/features.gni`, 251`//build/config/ui.gni`. These locations are deprecated. Feature flags should 252go along with the code for the feature. Many browser-level features can go 253somewhere in `//chrome/` without lower-level code knowing about it. Some 254UI environment flags can go into `//ui/`, and many flags can also go with 255the corresponding code in `//components/`. You can write a `.gni` file in 256components and have build files in chrome or content import it if necessary. 257 258The way to think about things in the `//build` directory is that this is 259DEPSed into various projects like V8 and WebRTC. Build flags specific to 260code outside of the build directory shouldn't be in the build directory, and 261V8 shouldn't get feature defines for Chrome features. 262 263New feature defines should use the buildflag system. See 264`//build/buildflag_header.gni` which allows preprocessor defines to be 265modularized without many of the disadvantages that made us use global defines 266in the past. 267 268### Type 269 270Arguments support all the [GN language types](language.md#Language). 271 272In the vast majority of cases `boolean` is the preferred type, since most 273arguments are enabling or disabling features or includes. 274 275`String`s are typically used for filepaths. They are also used for enumerated 276types, though `integer`s are sometimes used as well. 277 278### Naming conventions 279 280While there are no hard and fast rules around argument naming there are 281many common conventions. If you ever want to see the current list of argument 282names and default values for your current checkout use 283`gn args out/Debug --list --short`. 284 285`use_foo` - indicates dependencies or major codepaths to include (e.g. 286`use_open_ssl`, `use_ozone`, `use_cups`) 287 288`enable_foo` - indicates feature or tools to be enabled (e.g. 289`enable_google_now`, `enable_nacl`, `enable_remoting`, `enable_pdf`) 290 291`disable_foo` - _NOT_ recommended, use `enable_foo` instead with swapped default 292value 293 294`is_foo` - usually a global state descriptor (e.g. `is_chrome_branded`, 295`is_desktop_linux`); poor choice for non-globals 296 297`foo_use_bar` - prefixes can be used to indicate a limited scope for an argument 298(e.g. `rtc_use_h264`, `v8_use_snapshot`) 299 300#### Variables 301 302Prefix top-level local variables within `.gni` files with an underscore. This 303prefix causes variables to be unavailable to importing scripts. 304 305``` 306_this_var_will_not_be_exported = 1 307but_this_one_will = 2 308``` 309