4 ## Naming and ordering within the file
6 ### Location of build files
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.
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
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
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.
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.
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
53 Example for the `src/foo/BUILD.gn` file:
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.
60 # Config for foo is named foo_config and immediately precedes it in the file.
61 config("foo_config") {
64 # Target matching path name is the first target.
68 # Test for foo follows it.
69 test("foo_unittests") {
72 config("bar_config") {
79 ## Ordering within a target
81 1. `output_name` / `visibility` / `testonly`
83 3. `cflags`, `include_dirs`, `defines`, `configs` etc. in whatever
84 order makes sense to you.
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.
95 Conditions should be written to minimize the number of conditional blocks.
97 ## Formatting and indenting
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
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.
117 "bar.cc", # Note trailing comma on last element.
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).
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:
130 all_dependent_configs = [ ":foo_config" ] # No trailing comma.
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
147 sources += [ "thing_aura.cc" ]
150 sources += [ "thing_gtk.cc" ]
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.
166 "//foo/bar:other_thing",
167 "//foo/baz:that_thing",
173 Use fully-qualified paths for imports:
176 import("//foo/bar/baz.gni") # Even if this file is in the foo/bar directory
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.