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

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

Issue 1052883002: migrate GN docs from the wiki to the repo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: split md_browser out into its own dir Created 5 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
OLDNEW
(Empty)
1 # GN Style Guide
2
3 [TOC]
4 ## Naming and ordering within the file
5
6 ### Location of build files
7
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
10 GYP). This makes things easier to find and owners reviews easier since
11 changes are more focused.
12
13 ### Targets
14
15 * Most BUILD files should have a target with the same name of the
16 directory. This target should be the first target.
17 * Other targets should be in "some order that makes sense." Usually
18 more important targets will be first, and unit tests will follow the
19 corresponding target. If there's no clear ordering, consider
20 alphabetical order.
21 * Targets and configs should be named using lowercase with underscores
22 separating words, unless there is a strong reason to do otherwise.
23 * Test support libraries should be source sets named "test\_support".
24 So "//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
29 Output names (the part after the colon in a label) of executables and
30 shared libraries must be globally unique since they all go in the root
31 directory. Prefer to do this by giving a target a short (possibly
32 non-unique) name that makes writing dependencies clearer, and setting
33 the `output_name` variable to something unique.
34
35 For example, it looks much better to write a dependency as
36 `"//mojo/public/bindings"` rather than
37 `"//mojo/public/bindings:mojo_bindings"`. So in the file
38 `//mojo/public/bindings/BUILD.gn`: ``` shared_library("bindings") { #
39 Very non-unique name "bindings" makes the most sense in this context.
40 output_name = "mojo_bindings" # Give target a unique output name to
41 avoid collisions. ... } ``` This will produce a file
42 `mojo_bindings.so` in the root build directory.
43
44 ### Configs
45
46 * A config associated with a single target should be named the same as
47 the target with `_config` following it.
48 * A config should appear immediately before the corresponding target
49 that uses it.
50
51 ### Example
52
53 Example for the `src/foo/BUILD.gn` file:
54
55 ```
56 # Copyright 2013 The Chromium Authors. All rights reserved.
57 # Use of this source code is governed by a BSD-style license that can be
58 # found in the LICENSE file.
59
60 # Config for foo is named foo_config and immediately precedes it in the file.
61 config("foo_config") {
62 }
63
64 # Target matching path name is the first target.
65 executable("foo") {
66 }
67
68 # Test for foo follows it.
69 test("foo_unittests") {
70 }
71
72 config("bar_config") {
73 }
74
75 source_set("bar") {
76 }
77 ```
78
79 ## Ordering within a target
80
81 1. `output_name` / `visibility` / `testonly`
82 2. `sources`
83 3. `cflags`, `include_dirs`, `defines`, `configs` etc. in whatever
84 order makes sense to you.
85 4. `public_deps`
86 5. `deps`
87
88 ### Conditions
89
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
92 variable they affect. More complicated conditions affecting more than
93 one thing should go at the bottom.
94
95 Conditions should be written to minimize the number of conditional blocks.
96
97 ## Formatting and indenting
98
99 * Indents are two spaces, both for indented blocks and wrapped lines.
100 * 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.
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
107 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
133 ### Sources
134
135 * Sources should always be alphabetized within a given list.
136 * List sources only once. It is OK to conditionally include sources
137 rather than listing them all at the top and then conditionally
138 excluding them when they don't apply. Conditional inclusion is often
139 clearer since a file is only listed once and it's easier to reason
140 about when reading.
141
142 ```
143 sources = [
144 "main.cc",
145 ]
146 if (use_aura) {
147 sources += [ "thing_aura.cc" ]
148 }
149 if (use_gtk) {
150 sources += [ "thing_gtk.cc" ]
151 }
152 ```
153
154 ### Deps
155
156 * Deps should be in alphabetical order.
157 * Deps within the current file should be written first and not
158 qualified with the file name (just `:foo`).
159 * Other deps should always use fully-qualified path names unless
160 relative ones are required for some reason.
161
162 ```
163 deps = [
164 ":a_thing",
165 ":mystatic",
166 "//foo/bar:other_thing",
167 "//foo/baz:that_thing",
168 ]
169 ```
170
171 ### Import
172
173 Use fully-qualified paths for imports:
174
175 ```
176 import("//foo/bar/baz.gni") # Even if this file is in the foo/bar directory
177 ```
178
179 ## Usage
180
181 Use `source_set` rather than `static_library` unless you have a reason
182 to do otherwise. A static library is a standalone library which can be
183 slow to generate. A source set just links all the object files from that
184 target into the targets depending on it, which saves the "lib" step.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698