Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: tools/gn/docs/style_guide.md

Issue 1841013003: Update GN documentation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/gn/docs/reference.md ('k') | tools/gn/function_foreach.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # GN Style Guide 1 # GN Style Guide
2 2
3 [TOC] 3 [TOC]
4 ## Naming and ordering within the file 4 ## Naming and ordering within the file
5 5
6 ### Location of build files 6 ### Location of build files
7 7
8 It usually makes sense to have more build files closer to the code than 8 It usually makes sense to have more build files closer to the code than
9 fewer ones at the toplevel (this is in contrast with what we did with 9 fewer ones at the toplevel (this is in contrast with what we did with
10 GYP). This makes things easier to find and owners reviews easier since 10 GYP). This makes things easier to find and owners reviews easier since
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 89
90 Simple conditions affecting just one variable (e.g. adding a single 90 Simple conditions affecting just one variable (e.g. adding a single
91 source or adding a flag for one particular OS) can go beneath the 91 source or adding a flag for one particular OS) can go beneath the
92 variable they affect. More complicated conditions affecting more than 92 variable they affect. More complicated conditions affecting more than
93 one thing should go at the bottom. 93 one thing should go at the bottom.
94 94
95 Conditions should be written to minimize the number of conditional blocks. 95 Conditions should be written to minimize the number of conditional blocks.
96 96
97 ## Formatting and indenting 97 ## Formatting and indenting
98 98
99 * Indents are two spaces, both for indented blocks and wrapped lines. 99 GN contains a built-in code formatter which defines the formatting style.
100 Some additional notes:
101
100 * Variables are `lower_case_with_underscores` 102 * Variables are `lower_case_with_underscores`
101 * Complicated conditions and if statements should generally follow the
102 Google C++ style guide for formatting.
103 * Comments should be complete sentences with periods at the end. 103 * Comments should be complete sentences with periods at the end.
104 * End-of-line-comments should have two spaces separating them and the
105 code.
106 * Compiler flags and such should always be commented with what they do 104 * Compiler flags and such should always be commented with what they do
107 and why the flag is needed. 105 and why the flag is needed.
108 * Try to keep lines under 80 columns. If a file name or similar string
109 puts you beyond 80 with reasonable indenting, it's OK, but most
110 things can be wrapped nicely under that for the code review tool.
111
112 ### List formatting
113
114 ```
115 deps = [
116 "afile.cc",
117 "bar.cc", # Note trailing comma on last element.
118 ]
119 ```
120
121 Alphabetize the list elements unless there is a more obvious ordering.
122 In some cases, it makes more sense to put more than one list member on a
123 line if they clearly go together (for example, two short compiler flags
124 that must go next to each other).
125
126 Prefer use the multi-line style for lists of more than one elements.
127 Lists with single-elements can be written on one line if desired:
128
129 ```
130 all_dependent_configs = [ ":foo_config" ] # No trailing comma.
131 ```
132 106
133 ### Sources 107 ### Sources
134 108
135 * Sources should always be alphabetized within a given list. 109 Prefer to list sources only once. It is OK to conditionally include sources
136 * List sources only once. It is OK to conditionally include sources 110 rather than listing them all at the top and then conditionally excluding them
137 rather than listing them all at the top and then conditionally 111 when they don't apply. Conditional inclusion is often clearer since a file is
138 excluding them when they don't apply. Conditional inclusion is often 112 only listed once and it's easier to reason about when reading.
139 clearer since a file is only listed once and it's easier to reason
140 about when reading.
141 113
142 ``` 114 ```
143 sources = [ 115 sources = [
144 "main.cc", 116 "main.cc",
145 ] 117 ]
146 if (use_aura) { 118 if (use_aura) {
147 sources += [ "thing_aura.cc" ] 119 sources += [ "thing_aura.cc" ]
148 } 120 }
149 if (use_gtk) { 121 if (use_gtk) {
150 sources += [ "thing_gtk.cc" ] 122 sources += [ "thing_gtk.cc" ]
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 target into the targets depending on it, which saves the "lib" step. 156 target into the targets depending on it, which saves the "lib" step.
185 157
186 ## Build arguments 158 ## Build arguments
187 159
188 ### Scope 160 ### Scope
189 161
190 Build arguments should be scoped to a unit of behavior, e.g. enabling a feature. 162 Build arguments should be scoped to a unit of behavior, e.g. enabling a feature.
191 Typically an argument would be declared in an imported file to share it with 163 Typically an argument would be declared in an imported file to share it with
192 the subset of the build that could make use of it. 164 the subset of the build that could make use of it.
193 165
166 Chrome has many legacy flags in `//build/config/features.gni`,
167 `//build/config/ui.gni`. These locations are deprecated. Feature flags should
168 go along with the code for the feature. Many browser-level features can go
169 somewhere in `//chrome/` without lower-level code knowing about it. Some
170 UI environment flags can go into `//ui/`, and many flags can also go with
171 the corresponding code in `//components/`. You can write a `.gni` file in
172 components and have build files in chrome or content import it if necessary.
173
174 The way to think about things in the `//build` directory is that this is
175 DEPSed into various projects like V8 and WebRTC. Build flags specific to
176 code outside of the build directory shouldn't be in the build directory, and
177 V8 shouldn't get feature defines for Chrome features.
178
179 New feature defines should use the buildflag system. See
180 `//build/buildflag_header.gni` which allows preprocessor defines to be
181 modularized without many of the disadvantages that made us use global defines
182 in the past.
183
194 ### Type 184 ### Type
195 185
196 Arguments support all the [GN language types](language.md#Language). 186 Arguments support all the [GN language types](language.md#Language).
197 187
198 In the vast majority of cases `boolean` is the preferred type, since most 188 In the vast majority of cases `boolean` is the preferred type, since most
199 arguments are enabling or disabling features or includes. 189 arguments are enabling or disabling features or includes.
200 190
201 `String`s are typically used for filepaths. They are also used for enumerated 191 `String`s are typically used for filepaths. They are also used for enumerated
202 types, though `integer`s are sometimes used as well. 192 types, though `integer`s are sometimes used as well.
203 193
(...skipping 11 matching lines...) Expand all
215 `enable_google_now`, `enable_nacl`, `enable_remoting`, `enable_pdf`) 205 `enable_google_now`, `enable_nacl`, `enable_remoting`, `enable_pdf`)
216 206
217 `disable_foo` - _NOT_ recommended, use `enable_foo` instead with swapped default 207 `disable_foo` - _NOT_ recommended, use `enable_foo` instead with swapped default
218 value 208 value
219 209
220 `is_foo` - usually a global state descriptor (e.g. `is_chrome_branded`, 210 `is_foo` - usually a global state descriptor (e.g. `is_chrome_branded`,
221 `is_desktop_linux`); poor choice for non-globals 211 `is_desktop_linux`); poor choice for non-globals
222 212
223 `foo_use_bar` - prefixes can be used to indicate a limited scope for an argument 213 `foo_use_bar` - prefixes can be used to indicate a limited scope for an argument
224 (e.g. `rtc_use_h264`, `v8_use_snapshot`) 214 (e.g. `rtc_use_h264`, `v8_use_snapshot`)
OLDNEW
« no previous file with comments | « tools/gn/docs/reference.md ('k') | tools/gn/function_foreach.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698