David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 1 | --- |
| 2 | layout: documentation |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 3 | title: Concepts and terminology |
daroberts | 223aebd | 2021-02-18 17:53:30 -0800 | [diff] [blame] | 4 | category: getting-started |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 5 | --- |
Googler | 458daaa | 2021-01-08 18:55:00 -0800 | [diff] [blame] | 6 | <h1>Concepts and Terminology</h1> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 7 | <p> |
Googler | 47849d4 | 2021-01-15 17:43:25 -0800 | [diff] [blame] | 8 | This page provides an overview of the source tree layout and the |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 9 | terminology used in Bazel. |
| 10 | </p> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 11 | <h2 id="intro">Introduction</h2> |
| 12 | |
| 13 | <p>Bazel builds software from source code organized in a directory called |
| 14 | a workspace. Source files in the workspace are organized in a nested |
| 15 | hierarchy of packages, where each package is a directory that contains a set |
| 16 | of related source files and one BUILD file. The BUILD file specifies what |
| 17 | software outputs can be built from the source. |
| 18 | </p> |
Dave Abrahams | b3b2fea | 2021-02-25 20:00:34 +0100 | [diff] [blame] | 19 | <h2 id="packages_targets">Workspaces, repositories, packages, and targets</h2> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 20 | <h3 id="workspace">Workspace</h3> |
| 21 | |
| 22 | <p>A <em>workspace</em> is a directory on your filesystem that contains the |
ranjanih | e222639 | 2021-03-11 09:12:47 -0800 | [diff] [blame] | 23 | source files for the software you want to build. Each workspace directory has |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 24 | a text file named <code>WORKSPACE</code> which may be empty, or may contain |
dzc | 205125b | 2017-06-26 11:01:47 +0200 | [diff] [blame] | 25 | references to <a href="external.html">external dependencies</a> |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 26 | required to build the outputs.</p> |
| 27 | |
| 28 | <p>Directories containing a file called |
| 29 | <code>WORKSPACE</code> are considered the root of a workspace. |
| 30 | Therefore, Bazel ignores any directory trees in a workspace rooted |
| 31 | at a subdirectory containing a <code>WORKSPACE</code> file (as they form |
| 32 | another workspace).</p> |
| 33 | |
Yun Peng | 0af987b | 2019-11-11 07:19:54 -0800 | [diff] [blame] | 34 | <p>Bazel also supports <code>WORKSPACE.bazel</code> file as an alias of <code>WORKSPACE</code> file. |
| 35 | If both files exist, <code>WORKSPACE.bazel</code> will take the priority.</p> |
| 36 | |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 37 | <h3 id="repositories">Repositories</h3> |
| 38 | <p>Code is organized in <em>repositories</em>. The directory containing |
| 39 | the <code>WORKSPACE</code> file is the root of the main repository, also |
| 40 | called <code>@</code>. Other, (external) repositories |
| 41 | are defined in the <code>WORKSPACE</code> file using workspace rules. |
| 42 | |
| 43 | <p>The workspace rules bundled with Bazel are documented in the |
Dave Abrahams | 36de45a | 2021-02-16 15:31:50 -0800 | [diff] [blame] | 44 | <a href="be/workspace.html">Workspace Rules</a> section in the |
| 45 | <a href="be/overview.html">Build Encyclopedia</a> and the documentation on |
Abhishek Kumar | a067335 | 2020-05-18 11:06:51 -0700 | [diff] [blame] | 46 | <a href="repo/index.html">embedded Starlark repository rules</a>.</p> |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 47 | |
panzhongxian | 28fe62e | 2019-08-21 07:36:32 -0700 | [diff] [blame] | 48 | <p>As external repositories are repositories themselves, they often contain |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 49 | a <code>WORKSPACE</code> file as well. However, these additional |
| 50 | <code>WORKSPACE</code> files are ignored by Bazel. In particular, |
| 51 | repositories depended upon transitively are not added automatically.</p> |
| 52 | |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 53 | <h3 id="packages">Packages</h3> |
| 54 | <p> |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 55 | The primary unit of code organization in a repository is |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 56 | the <i>package</i>. A package is a collection of related files and a |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 57 | specification of the dependencies among them. |
| 58 | </p> |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 59 | |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 60 | <p> |
| 61 | A package is defined as a directory containing a file |
laurentlb | 4993905 | 2018-10-18 13:02:55 -0700 | [diff] [blame] | 62 | named <code>BUILD</code> or <code>BUILD.bazel</code>, |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 63 | residing beneath the top-level directory in the |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 64 | workspace. A package includes all files in its directory, plus all |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 65 | subdirectories beneath it, except those which themselves contain a BUILD |
| 66 | file. |
| 67 | </p> |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 68 | |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 69 | <p> |
| 70 | For example, in the following directory tree |
| 71 | there are two packages, <code>my/app</code>, |
| 72 | and the subpackage <code>my/app/tests</code>. |
| 73 | Note that <code>my/app/data</code> is not a package, but a directory |
| 74 | belonging to package <code>my/app</code>. |
| 75 | </p> |
| 76 | |
| 77 | <pre> |
| 78 | src/my/app/BUILD |
| 79 | src/my/app/app.cc |
| 80 | src/my/app/data/input.txt |
| 81 | src/my/app/tests/BUILD |
| 82 | src/my/app/tests/test.cc |
| 83 | </pre> |
| 84 | <h3 id="targets">Targets</h3> |
| 85 | |
| 86 | <p> |
| 87 | A package is a container. The elements of a package are called |
| 88 | <i>targets</i>. Most targets are one of two principal kinds, <i>files</i> |
| 89 | and <i>rules</i>. Additionally, there is another kind of target, |
| 90 | <a href="be/functions.html#package_group">package groups</a>, |
| 91 | but they are far less numerous. |
| 92 | </p> |
| 93 | |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 94 | <p> |
| 95 | Files are further divided into two kinds. |
| 96 | <i>Source files</i> are usually written by the efforts of people, |
| 97 | and checked in to the repository. |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 98 | <i>Generated files</i>, sometimes called derived files or output files, |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 99 | are not checked in, but are generated by the build tool from source |
| 100 | files according to specific rules. |
| 101 | </p> |
| 102 | |
| 103 | <p> |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 104 | The second kind of target is the <i>rule</i>. A rule specifies the |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 105 | relationship between a set of input and a set of output files, |
| 106 | including the necessary steps to derive the outputs from the inputs. |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 107 | The outputs of a rule are always generated files. The inputs to a |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 108 | rule may be source files, but they may be generated files also; |
| 109 | consequently, outputs of one rule may be the inputs to another, |
| 110 | allowing long chains of rules to be constructed. |
| 111 | </p> |
| 112 | |
| 113 | <p> |
| 114 | Whether the input to a rule is a source file or a generated file is |
| 115 | in most cases immaterial; what matters is only the contents of that |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 116 | file. This fact makes it easy to replace a complex source file with |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 117 | a generated file produced by a rule, such as happens when the burden |
| 118 | of manually maintaining a highly structured file becomes too |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 119 | tiresome, and someone writes a program to derive it. No change is |
| 120 | required to the consumers of that file. Conversely, a generated |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 121 | file may easily be replaced by a source file with only local |
| 122 | changes. |
| 123 | </p> |
| 124 | |
| 125 | <p> |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 126 | The inputs to a rule may also include <i>other rules</i>. The |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 127 | precise meaning of such relationships is often quite complex and |
| 128 | language- or rule-dependent, but intuitively it is simple: a C++ |
| 129 | library rule A might have another C++ library rule B for an input. |
Googler | 793b18d | 2019-05-09 08:52:01 -0700 | [diff] [blame] | 130 | The effect of this dependency is that B's header files are |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 131 | available to A during compilation, B's symbols are available to A |
| 132 | during linking, and B's runtime data is available to A during |
| 133 | execution. |
| 134 | </p> |
| 135 | |
| 136 | <p> |
| 137 | An invariant of all rules is that the files generated by a rule |
| 138 | always belong to the same package as the rule itself; it is not |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 139 | possible to generate files into another package. It is not uncommon |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 140 | for a rule's inputs to come from another package, though. |
| 141 | </p> |
| 142 | |
| 143 | <p> |
| 144 | Package groups are sets of packages whose purpose is to limit accessibility |
| 145 | of certain rules. Package groups are defined by the |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 146 | <code>package_group</code> function. They have three properties: the list of |
| 147 | packages they contain, their name, and other package groups they include. The only allowed ways to refer to them |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 148 | are from the <code>visibility</code> attribute of rules or from the |
| 149 | <code>default_visibility</code> attribute of the <code>package</code> |
| 150 | function; they do not generate or consume files. For more information, refer |
Dave Abrahams | af3a556 | 2021-02-16 16:54:06 -0800 | [diff] [blame] | 151 | to the <a |
| 152 | href='be/functions.html#package_group'><code>package_group</code> |
| 153 | documentation</a>. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 154 | </p> |
| 155 | |
| 156 | |
| 157 | <h3 id="labels">Labels</h3> |
| 158 | |
| 159 | <p> |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 160 | All targets belong to exactly one package. The name of a target is |
Dave Abrahams | 804bcc14 | 2021-02-22 09:51:27 -0800 | [diff] [blame] | 161 | called its <em>label</em>. Every label uniquely identifies a target. A |
| 162 | typical label in canonical form looks like: |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 163 | </p> |
| 164 | |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 165 | |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 166 | <pre> |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 167 | @myrepo//my/app/main:app_binary |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 168 | </pre> |
| 169 | |
| 170 | <p> |
Dave Abrahams | 804bcc14 | 2021-02-22 09:51:27 -0800 | [diff] [blame] | 171 | The first part of the label is the repository name, <code>@myrepo//</code>. |
Dave Abrahams | ff348f4 | 2021-03-03 12:02:30 -0800 | [diff] [blame] | 172 | In the typical case that a label refers to the same repository from which |
| 173 | it is used, the repository identifier may be abbreviated as <code>//</code>. |
| 174 | So, inside <code>@myrepo</code> this label is usually written as |
aehlig | 7bdfb57 | 2019-08-08 05:08:37 -0700 | [diff] [blame] | 175 | </p> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 176 | |
aehlig | c2efa6f | 2019-08-08 08:18:44 -0700 | [diff] [blame] | 177 | <pre> |
| 178 | //my/app/main:app_binary |
| 179 | </pre> |
| 180 | |
| 181 | <p> |
| 182 | |
Dave Abrahams | 804bcc14 | 2021-02-22 09:51:27 -0800 | [diff] [blame] | 183 | The second part of the label is the un-qualified package name |
| 184 | <code>my/app/main</code>, the path to the package |
Dave Abrahams | 080ef6e | 2021-04-22 07:57:30 -0700 | [diff] [blame] | 185 | relative to the repository root. Together, the repository name and the |
Niek Peeters | 00196b2 | 2021-05-11 10:28:24 -0700 | [diff] [blame] | 186 | un-qualified package name form the fully-qualified package name |
Dave Abrahams | 080ef6e | 2021-04-22 07:57:30 -0700 | [diff] [blame] | 187 | <code>@myrepo//my/app/main</code>. When the label refers to the same |
Dave Abrahams | 804bcc14 | 2021-02-22 09:51:27 -0800 | [diff] [blame] | 188 | package it is used in, the package name (and optionally, the colon) |
| 189 | may be omitted. So, inside <code>@myrepo//my/app/main</code>, |
| 190 | this label may be written either of the following ways: |
| 191 | |
| 192 | </p> |
| 193 | |
| 194 | <pre> |
| 195 | app_binary |
| 196 | :app_binary |
| 197 | </pre> |
| 198 | |
| 199 | <p> |
| 200 | It is a matter of convention that the colon is omitted for files, |
| 201 | but retained for rules, but it is not otherwise significant. |
| 202 | </p> |
| 203 | |
| 204 | <p> |
| 205 | The part of the label after |
| 206 | the colon, <code>app_binary</code> is the un-qualified target name. |
| 207 | When it matches the last component of the package path, it, and |
| 208 | the colon, may be omitted. So, these two labels are |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 209 | equivalent: |
| 210 | </p> |
| 211 | |
| 212 | <pre> |
Dave Abrahams | 804bcc14 | 2021-02-22 09:51:27 -0800 | [diff] [blame] | 213 | //my/app/lib |
| 214 | //my/app/lib:lib |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 215 | </pre> |
| 216 | |
| 217 | <p> |
Dave Abrahams | 804bcc14 | 2021-02-22 09:51:27 -0800 | [diff] [blame] | 218 | The name of a file target in a subdirectory of the |
| 219 | package is the file's path relative to the package |
| 220 | root (the directory containing the <code>BUILD</code> file). |
| 221 | So, this file is in the <code>my/app/main/testdata</code> |
| 222 | subdirectory of the repository: |
| 223 | </p> |
| 224 | |
| 225 | <pre> |
| 226 | //my/app/main:testdata/input.txt |
| 227 | </pre> |
| 228 | |
| 229 | <p> |
| 230 | Don't confuse labels like <code>//my/app</code> with package names. Labels |
| 231 | always start with a repository identifier (often abbreviated <code>//</code>) |
| 232 | but package names never do. Thus, <code>my/app</code> is the package |
| 233 | containing <code>//my/app/lib</code> (a.k.a. |
| 234 | <code>//my/app/lib:lib</code>). |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 235 | |
| 236 | (A common misconception is that <code>//my/app</code> refers |
| 237 | to a package, or to <em>all</em> the targets in a package; neither |
Googler | cfae270 | 2021-08-12 17:21:40 -0700 | [diff] [blame] | 238 | is true. Remember, it is equivalent to <code>//my/app:app</code>, so it names |
| 239 | the <code>app</code> target in the <code>my/app</code> package of the current repository). |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 240 | </p> |
| 241 | |
| 242 | <p> |
| 243 | Relative labels cannot be used to refer to targets in other |
Dave Abrahams | 804bcc14 | 2021-02-22 09:51:27 -0800 | [diff] [blame] | 244 | packages; the repository identifier and package name must |
| 245 | always be specified in this case. For example, if the source |
| 246 | tree contains both the package |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 247 | <code>my/app</code> and the package |
| 248 | <code>my/app/testdata</code> (i.e., each of these two |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 249 | directories has its own BUILD file). The latter package contains a |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 250 | file named <code>testdepot.zip</code>. Here are two ways (one |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 251 | wrong, one correct) to refer to this file within |
| 252 | <code>//my/app:BUILD</code>: |
| 253 | </p> |
| 254 | |
| 255 | <pre> |
| 256 | <span class="discouraged">testdata/testdepot.zip</span> # Wrong: testdata is a different package. |
| 257 | //my/app/testdata:testdepot.zip # Right. |
| 258 | </pre> |
| 259 | |
| 260 | <p> |
Michael McLoughlin | 896391b | 2020-02-03 15:44:51 -0800 | [diff] [blame] | 261 | Labels starting with <code>@//</code> are references to the main |
| 262 | repository, which will still work even from external repositories. |
| 263 | Therefore <code>@//a/b/c</code> is different from |
| 264 | <code>//a/b/c</code> when referenced from an external repository. |
| 265 | The former refers back to the main repository, while the latter |
| 266 | looks for <code>//a/b/c</code> in the external repository itself. |
| 267 | This is especially relevant when writing rules in the main |
| 268 | repository that refer to targets in the main repository, and will be |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 269 | used from external repositories. |
| 270 | </p> |
| 271 | |
| 272 | <p> |
Googler | 2782904 | 2021-05-03 10:36:33 -0700 | [diff] [blame] | 273 | For information about the different ways you can refer to targets, see |
philomath | 269e86c | 2021-05-12 06:17:26 -0700 | [diff] [blame] | 274 | <a href="guide.md#specifying-targets-to-build">target patterns</a>. |
Michael McLoughlin | 896391b | 2020-02-03 15:44:51 -0800 | [diff] [blame] | 275 | </p> |
| 276 | |
Googler | 64047ac | 2021-03-08 13:05:15 -0800 | [diff] [blame] | 277 | |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 278 | <h3 id="lexi">Lexical specification of a label</h3> |
| 279 | |
| 280 | <p> |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 281 | Label syntax discourages use of metacharacters that have special meaning to the shell. This helps |
| 282 | to avoid inadvertent quoting problems, and makes it easier to construct tools and scripts that |
| 283 | manipulate labels, such as the <a href='query.html'>Bazel Query Language</a>. |
dmarting | dffa636 | 2017-08-30 19:23:32 +0200 | [diff] [blame] | 284 | |
twigg | d0aac3d | 2019-10-25 09:42:55 -0700 | [diff] [blame] | 285 | The precise details of allowed target names are below. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 286 | </p> |
| 287 | |
Philipp Schrader | 2dd2ecc | 2021-04-05 08:17:52 -0700 | [diff] [blame] | 288 | <h4 id="name">Target names, <code>//<package name>:<b>target-name</b></code></h4> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 289 | |
| 290 | <p><code>target-name</code> is the name of the target within the package. |
| 291 | The name of a rule is the value of the <code>name</code> |
Googler | 9838449 | 2019-05-14 11:26:47 -0700 | [diff] [blame] | 292 | attribute in the rule's declaration in a BUILD file; the name |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 293 | of a file is its pathname relative to the directory containing |
| 294 | the BUILD file. |
| 295 | Target names must be composed entirely of |
| 296 | characters drawn from the set <code>a</code>–<code>z</code>, |
| 297 | <code>A</code>–<code>Z</code>, <code>0</code>–<code>9</code>, |
twigg | d0aac3d | 2019-10-25 09:42:55 -0700 | [diff] [blame] | 298 | and the punctuation symbols <code>!%-@^_` "#$&'()*-+,;<=>?[]{|}~/.</code>. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 299 | Do not use <code>..</code> to refer to files in other packages; use |
| 300 | <code>//<var>packagename</var>:<var>filename</var></code> instead. |
| 301 | Filenames must be relative pathnames in normal form, which means |
| 302 | they must neither start nor end with a slash |
| 303 | (e.g. <code>/foo</code> and <code>foo/</code> are forbidden) nor |
| 304 | contain multiple consecutive slashes as path separators |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 305 | (e.g. <code>foo//bar</code>). Similarly, up-level references |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 306 | (<code>..</code>) and current-directory references |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 307 | (<code>./</code>) are forbidden. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 308 | </p> |
| 309 | |
| 310 | <p>While it is common to use <code>/</code> in the name of a file |
Googler | a1a29ec | 2021-02-26 12:19:25 -0800 | [diff] [blame] | 311 | target, it is recommended that you avoid the use of <code>/</code> in the |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 312 | names of rules. Especially when the shorthand form of a label is |
| 313 | used, it may confuse the reader. The |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 314 | label <code>//foo/bar/wiz</code> is always a shorthand |
| 315 | for <code>//foo/bar/wiz:wiz</code>, even if there is no such package |
| 316 | <code>foo/bar/wiz</code>; it never refers to <code>//foo:bar/wiz</code>, |
| 317 | even if that target exists.</p> |
| 318 | |
| 319 | <p>However, there are some situations where use of a slash is |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 320 | convenient, or sometimes even necessary. For example, the name of |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 321 | certain rules must match their principal source file, which may |
| 322 | reside in a subdirectory of the package.</p> |
| 323 | |
Philipp Schrader | 2dd2ecc | 2021-04-05 08:17:52 -0700 | [diff] [blame] | 324 | <h4>Package names, <code>//<b>package-name</b>:<target name></code></h4> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 325 | <p> |
| 326 | The name of a package is the name of the directory containing its |
| 327 | |
Dave Abrahams | 976d9b0 | 2021-02-22 00:02:48 -0800 | [diff] [blame] | 328 | BUILD file, relative to the top-level directory of the containing |
| 329 | repository. For example: <code>my/app</code>. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 330 | |
Googler | 0bcc984 | 2016-09-15 14:06:13 +0000 | [diff] [blame] | 331 | Package names must be composed entirely of characters drawn from |
| 332 | the set <code>A</code>-<code>Z</code>, <code>a</code>–<code>z</code>, |
| 333 | <code>0</code>–<code>9</code>, '<code>/</code>', '<code>-</code>', |
| 334 | '<code>.</code>', and '<code>_</code>', and cannot start with |
| 335 | a slash. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 336 | <p> |
| 337 | For a language with a directory structure that is significant |
| 338 | to its module system (e.g. Java), it is important to choose directory names |
| 339 | that are valid identifiers in the language. |
| 340 | </p> |
| 341 | |
| 342 | <p> |
Dave Abrahams | b62ec6f | 2021-02-22 12:55:12 -0800 | [diff] [blame] | 343 | Although Bazel supports targets in the workspace's root package |
| 344 | (e.g. <code>//:foo</code>), it's best to leave that package empty |
| 345 | so all meaningful packages have descriptive names. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 346 | </p> |
| 347 | <p> |
| 348 | Package names may not contain the substring <code>//</code>, nor |
| 349 | end with a slash. |
| 350 | </p> |
| 351 | |
| 352 | <h3 id="rules">Rules</h3> |
| 353 | |
| 354 | <p> |
| 355 | A rule specifies the relationship between inputs and outputs, and the |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 356 | steps to build the outputs. Rules can be of one of many different |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 357 | kinds or <i>classes</i>, which produce compiled |
| 358 | executables and libraries, test executables and other supported |
| 359 | outputs as described in the |
| 360 | <a href="be/overview.html">Build Encyclopedia</a>. |
| 361 | </p> |
| 362 | |
| 363 | <p> |
| 364 | Every rule has a name, specified by the <code>name</code> attribute, |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 365 | of type string. The name must be a syntactically valid target name, |
| 366 | as specified <a href='#name'>above</a>. In some cases, the name is |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 367 | somewhat arbitrary, and more interesting are the names of the files |
Googler | 0ef0454 | 2021-03-09 08:38:20 -0800 | [diff] [blame] | 368 | generated by the rule, and this is true of genrules. For more information, see |
| 369 | <a href="be/general.html#genrule"> |
| 370 | General Rules: genrule</a>. |
| 371 | |
| 372 | In other cases, the name is significant: for <code>*_binary</code> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 373 | and <code>*_test</code> rules, for example, the rule name determines |
| 374 | the name of the executable produced by the build. |
| 375 | </p> |
| 376 | |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 377 | <pre> |
| 378 | cc_binary( |
| 379 | name = "my_app", |
| 380 | srcs = ["my_app.cc"], |
| 381 | deps = [ |
| 382 | "//absl/base", |
| 383 | "//absl/strings", |
| 384 | ], |
| 385 | ) |
| 386 | </pre> |
| 387 | |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 388 | <p> |
| 389 | Every rule has a set of <i>attributes</i>; the applicable attributes |
| 390 | for a given rule, and the significance and semantics of each |
| 391 | attribute are a function of the rule's class; see |
| 392 | the <a href='be/overview.html'>Build |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 393 | Encyclopedia</a> for a list of rules and their |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 394 | corresponding attributes. Each attribute has a name and a |
| 395 | type. Some of the common types an attribute can have are integer, |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 396 | label, list of labels, string, list of strings, output label, |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 397 | list of output labels. Not all attributes need to be specified in |
| 398 | every rule. Attributes thus form a dictionary from keys (names) to |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 399 | optional, typed values. |
| 400 | </p> |
| 401 | |
| 402 | <p> |
| 403 | The <code>srcs</code> attribute present in many rules has type "list |
Googler | f772a69 | 2019-07-09 11:12:25 -0700 | [diff] [blame] | 404 | of labels"; its value, if present, is a list of labels, each being |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 405 | the name of a target that is an input to this rule. |
| 406 | </p> |
| 407 | |
| 408 | <p> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 409 | This directed acyclic graph over targets is called the |
| 410 | "target graph" or "build dependency graph", and is the domain over |
dmarting | dffa636 | 2017-08-30 19:23:32 +0200 | [diff] [blame] | 411 | which the <a href='query.html'>Bazel Query tool</a> operates. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 412 | </p> |
| 413 | |
| 414 | |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 415 | <h2 id="BUILD_files">BUILD files</h2> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 416 | |
| 417 | <p> |
| 418 | The previous section described packages, targets and labels, and the |
Googler | a1a29ec | 2021-02-26 12:19:25 -0800 | [diff] [blame] | 419 | build dependency graph abstractly. This section describes the |
| 420 | concrete syntax used to define a package. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 421 | </p> |
| 422 | |
| 423 | <p> |
| 424 | By definition, every package contains a BUILD file, which is a short |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 425 | program. |
| 426 | BUILD files are evaluated using an imperative language, |
laurentlb | 353dd74 | 2018-10-03 15:25:02 -0700 | [diff] [blame] | 427 | <a href="https://github.com/bazelbuild/starlark/">Starlark</a>. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 428 | |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 429 | They are interpreted as a sequential list of statements. |
laurentlb | 353dd74 | 2018-10-03 15:25:02 -0700 | [diff] [blame] | 430 | </p> |
| 431 | |
| 432 | <p> |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 433 | In general, order does matter: variables must be defined before they are used, for |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 434 | example. However, most BUILD files consist only of declarations of |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 435 | build rules, and the relative order of these statements is |
| 436 | immaterial; all that matters is <em>which</em> rules were declared, |
| 437 | and with what values, by the time package evaluation completes. |
| 438 | |
| 439 | When a build rule function, such as <code>cc_library</code>, is |
| 440 | executed, it creates a new target in the graph. This target can later be |
| 441 | referred using a label. |
| 442 | |
| 443 | So, in simple BUILD files, rule declarations can be re-ordered |
| 444 | freely without changing the behavior. |
| 445 | </p> |
| 446 | |
| 447 | |
| 448 | <p> |
| 449 | To encourage a clean separation between code and data, BUILD files cannot |
laurentlb | 353dd74 | 2018-10-03 15:25:02 -0700 | [diff] [blame] | 450 | contain function definitions, <code>for</code> statements or |
| 451 | <code>if</code> statements (but list comprehensions and <code>if</code> |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 452 | expressions are allowed). Functions can be declared in <code>.bzl</code> |
laurentlb | 4b6f362 | 2019-02-11 09:42:49 -0800 | [diff] [blame] | 453 | files instead. Additionally, <code>*args</code> and <code>**kwargs</code> |
| 454 | arguments are not allowed in BUILD files; instead list all the arguments |
| 455 | explicitly. |
laurentlb | 353dd74 | 2018-10-03 15:25:02 -0700 | [diff] [blame] | 456 | </p> |
| 457 | |
| 458 | <p> |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 459 | Crucially, programs in Starlark are unable to perform |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 460 | arbitrary I/O. This invariant makes the |
laurentlb | 353dd74 | 2018-10-03 15:25:02 -0700 | [diff] [blame] | 461 | interpretation of BUILD files hermetic, i.e. dependent only on a |
| 462 | known set of inputs, which is essential for ensuring that builds are |
| 463 | reproducible. |
| 464 | </p> |
| 465 | |
| 466 | <p> |
| 467 | BUILD files should be written using only ASCII characters, |
| 468 | although technically they are interpreted using the Latin-1 |
| 469 | character set. |
| 470 | </p> |
| 471 | |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 472 | <p> |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 473 | Since BUILD files need to be updated whenever the dependencies of the |
| 474 | underlying code change, they are typically maintained by multiple |
| 475 | people on a team. BUILD file authors are encouraged to use comments |
| 476 | liberally to document the role of each build target, whether or not it |
| 477 | is intended for public use, and to document the role of the package |
| 478 | itself. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 479 | </p> |
| 480 | |
laurentlb | 78f245d | 2018-10-23 10:58:16 -0700 | [diff] [blame] | 481 | <h3 id="load">Loading an extension</h3> |
| 482 | |
| 483 | Bazel extensions are files ending in <code>.bzl</code>. Use |
| 484 | the <code>load</code> statement to import a symbol from an extension. |
| 485 | |
| 486 | <pre> |
| 487 | load("//foo/bar:file.bzl", "some_library") |
laurentlb | 51482d7 | 2018-10-23 15:33:01 -0700 | [diff] [blame] | 488 | </pre> |
laurentlb | 78f245d | 2018-10-23 10:58:16 -0700 | [diff] [blame] | 489 | |
| 490 | This code will load the file <code>foo/bar/file.bzl</code> and add the |
| 491 | <code>some_library</code> symbol to the environment. This can be used to load |
| 492 | new rules, functions or constants (e.g. a string, a list, etc.). Multiple |
| 493 | symbols can be imported by using additional arguments to the call |
| 494 | to <code>load</code>. Arguments must be string literals (no variable) |
| 495 | and <code>load</code> statements must appear at top-level, i.e. they cannot be |
| 496 | in a function body. |
| 497 | |
| 498 | The first argument of <code>load</code> is a <a href="#labels">label</a> |
| 499 | identifying a <code>.bzl</code> file. If it is a relative label, it is resolved |
| 500 | with respect to the package (not directory) containing the current |
| 501 | <code>bzl</code> file. Relative labels in <code>load</code> statements should |
| 502 | use a leading <code>:</code>. |
| 503 | |
| 504 | <code>load</code> also supports aliases, i.e. you can assign different names to |
| 505 | the imported symbols. |
| 506 | |
| 507 | <pre> |
| 508 | load("//foo/bar:file.bzl", library_alias = "some_library") |
| 509 | </pre> |
| 510 | |
| 511 | You can define multiple aliases within one <code>load</code> statement. |
| 512 | Moreover, the argument list can contain both aliases and regular symbol names. |
| 513 | The following example is perfectly legal (please note when to use quotation |
| 514 | marks). |
| 515 | |
| 516 | <pre> |
| 517 | load(":my_rules.bzl", "some_rule", nice_alias = "some_other_rule") |
| 518 | </pre> |
| 519 | |
| 520 | In a <code>.bzl</code> file, symbols starting with <code>_</code> are not |
| 521 | exported and cannot be loaded from another file. Visibility doesn't affect |
| 522 | loading (yet): you don't need to use <code>exports_files</code> to make |
| 523 | a <code>.bzl</code> file visible. |
| 524 | |
Googler | 1933da5 | 2021-01-20 10:04:49 -0800 | [diff] [blame] | 525 | <h2 id="funcs">Types of build rules</h2> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 526 | |
| 527 | <p> |
| 528 | The majority of build rules come in families, grouped together by |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 529 | language. For example, <code>cc_binary</code>, <code>cc_library</code> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 530 | and <code>cc_test</code> are the build rules for C++ binaries, |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 531 | libraries, and tests, respectively. Other languages use the same |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 532 | naming scheme, with a different prefix, e.g. <code>java_*</code> for |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 533 | Java. Some of these functions are documented in the |
| 534 | <a href="be/overview.html">Build Encyclopedia</a>, but it is possible |
| 535 | for anyone to create new rules. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 536 | </p> |
| 537 | |
| 538 | <ul> |
| 539 | <li><p><code>*_binary</code> |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 540 | rules build executable programs in a given language. After a |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 541 | build, the executable will reside in the build tool's binary |
| 542 | output tree at the corresponding name for the rule's label, |
| 543 | so <code>//my:program</code> would appear at |
| 544 | (e.g.) <code>$(BINDIR)/my/program</code>. </p> |
| 545 | |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 546 | <p>In some languages, such rules also create a runfiles directory |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 547 | |
| 548 | containing all the files mentioned in a <code>data</code> |
| 549 | attribute belonging to the rule, or any rule in its transitive |
| 550 | closure of dependencies; this set of files is gathered together in |
| 551 | one place for ease of deployment to production.</p> |
| 552 | </li> |
| 553 | |
| 554 | <li><p><code>*_test</code> |
| 555 | rules are a specialization of a <code>*_binary</code> rule, used for automated |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 556 | testing. Tests are simply programs that return zero on success. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 557 | |
| 558 | </p> |
| 559 | |
| 560 | <p> |
| 561 | Like binaries, tests also have runfiles trees, and the files |
| 562 | beneath it are the only files that a test may legitimately open |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 563 | at runtime. For example, a program <code>cc_test(name='x', |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 564 | data=['//foo:bar'])</code> may open and |
| 565 | |
| 566 | read <code>$TEST_SRCDIR/workspace/foo/bar</code> during execution. |
| 567 | (Each programming language has its own utility function for |
| 568 | accessing the value of <code>$TEST_SRCDIR</code>, but they are all |
| 569 | equivalent to using the environment variable directly.) |
| 570 | Failure to observe the rule will cause the test to fail when it is |
| 571 | executed on a remote testing host. |
| 572 | |
| 573 | </p> |
| 574 | </li> |
| 575 | |
| 576 | <li><code>*_library</code> |
| 577 | rules specify separately-compiled modules in the given |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 578 | programming language. Libraries can depend on other libraries, |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 579 | and binaries and tests can depend on libraries, with the expected |
| 580 | separate-compilation behavior. |
| 581 | </li> |
| 582 | </ul> |
| 583 | |
| 584 | <h2 id="dependencies">Dependencies</h2> |
| 585 | |
| 586 | <p> |
| 587 | A target <code>A</code> <i>depends upon</i> a target |
| 588 | <code>B</code> if <code>B</code> is needed by <code>A</code> at |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 589 | build or execution time. The <i>depends upon</i> relation induces a |
laurentlb | 5ec875a | 2018-10-18 11:22:28 -0700 | [diff] [blame] | 590 | <a href="https://en.wikipedia.org/wiki/Directed_acyclic_graph">Directed |
Googler | a1a29ec | 2021-02-26 12:19:25 -0800 | [diff] [blame] | 591 | Acyclic Graph</a> (DAG) over targets, and it is called a |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 592 | <em>dependency graph</em>. |
| 593 | |
| 594 | A target's <em>direct</em> dependencies are those other targets |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 595 | reachable by a path of length 1 in the dependency graph. A target's |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 596 | <em>transitive</em> dependencies are those targets upon which it |
| 597 | depends via a path of any length through the graph. |
| 598 | </p> |
| 599 | |
| 600 | <p> |
| 601 | In fact, in the context of builds, there are two dependency graphs, |
| 602 | the graph of <em>actual dependencies</em> and the graph of |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 603 | <em>declared dependencies</em>. Most of the time, the two graphs |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 604 | are so similar that this distinction need not be made, but it is |
| 605 | useful for the discussion below. |
| 606 | </p> |
| 607 | |
| 608 | <h3 id="actual_and_declared_dependencies">Actual and declared dependencies</h3> |
| 609 | |
| 610 | <p> |
| 611 | A target <code>X</code> is <i>actually dependent</i> on target |
Googler | 11b0b1f | 2019-10-08 02:03:02 -0700 | [diff] [blame] | 612 | <code>Y</code> if and only if <code>Y</code> must be present, built |
| 613 | and up-to-date in order for <code>X</code> to be built correctly. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 614 | "Built" could mean generated, processed, compiled, linked, |
| 615 | archived, compressed, executed, or any of the other kinds of tasks |
| 616 | that routinely occur during a build. |
| 617 | </p> |
| 618 | |
| 619 | <p> |
| 620 | A target <code>X</code> has a <i>declared dependency</i> on target |
Googler | 11b0b1f | 2019-10-08 02:03:02 -0700 | [diff] [blame] | 621 | <code>Y</code> if and only if there is a dependency edge from <code>X</code> |
| 622 | to <code>Y</code> in the package of <code>X</code>. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 623 | </p> |
| 624 | |
| 625 | <p> |
| 626 | For correct builds, the graph of actual dependencies <i>A</i> must |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 627 | be a subgraph of the graph of declared dependencies <i>D</i>. That |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 628 | is, every pair of directly-connected nodes <code>x --> y</code> |
Googler | a1a29ec | 2021-02-26 12:19:25 -0800 | [diff] [blame] | 629 | in <i>A</i> must also be directly connected in <i>D</i>. It can be said that |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 630 | <i>D</i> is an <em>overapproximation</em> of <i>A</i>. |
| 631 | </p> |
| 632 | |
| 633 | <p> |
Ryota | 2ab1be8 | 2020-04-03 03:05:26 -0700 | [diff] [blame] | 634 | It is important that it is not too much of an overapproximation, |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 635 | though, since redundant declared dependencies can make builds slower and |
| 636 | binaries larger. |
| 637 | </p> |
| 638 | |
| 639 | <p> |
| 640 | What this means for BUILD file writers is that every rule must |
| 641 | explicitly declare all of its actual direct dependencies to the |
| 642 | build system, and no more. |
| 643 | |
| 644 | Failure to observe this principle causes undefined behavior: the |
| 645 | build may fail, but worse, the build may depend on some prior |
Ryota | 2ab1be8 | 2020-04-03 03:05:26 -0700 | [diff] [blame] | 646 | operations, or upon transitive declared dependencies the target |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 647 | happens to have. The build tool attempts aggressively to check for |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 648 | missing dependencies and report errors, but it is not possible for |
| 649 | this checking to be complete in all cases. |
| 650 | </p> |
| 651 | |
| 652 | <p> |
| 653 | |
| 654 | You need not (and should not) attempt to list everything indirectly imported, |
| 655 | even if it is "needed" by A at execution time. |
| 656 | </p> |
| 657 | |
| 658 | <p> |
| 659 | During a build of target <code>X</code>, the build tool inspects the |
| 660 | entire transitive closure of dependencies of <code>X</code> to ensure that |
| 661 | any changes in those targets are reflected in the final result, |
| 662 | rebuilding intermediates as needed. |
| 663 | </p> |
| 664 | |
| 665 | <p> |
| 666 | The transitive nature of dependencies leads to a common mistake. |
| 667 | Through careless programming, code in one file may use code provided |
| 668 | by an <em>indirect</em> dependency, i.e. a transitive but not direct |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 669 | edge in the declared dependency graph. Indirect dependencies do not |
| 670 | appear in the BUILD file. Since the rule doesn't |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 671 | directly depend on the provider, there is no way to track changes, |
| 672 | as shown in the following example timeline: |
| 673 | </p> |
| 674 | |
| 675 | <div class="greenbox"> |
| 676 | <p><b>1. At first, everything works</b></p> |
| 677 | |
| 678 | <p>The code in package <code>a</code> uses code in package <code>b</code>. |
| 679 | The code in package <code>b</code> uses code in package <code>c</code>, |
| 680 | and thus <code>a</code> transitively depends on <code>c</code>.</p> |
| 681 | |
| 682 | <div style="float:left; width: 49%; margin-top: -20px;"> |
| 683 | <p><code>a/BUILD</code></p> |
| 684 | <pre class="code"> |
| 685 | <b>rule( |
| 686 | name = "a", |
| 687 | srcs = "a.in", |
| 688 | deps = "//b:b", |
| 689 | )</b> |
| 690 | </pre> |
| 691 | <p><code>a/a.in</code></p> |
| 692 | <pre class="code"> |
| 693 | <b>import b; |
| 694 | b.foo();</b> |
| 695 | </pre> |
| 696 | </div> |
| 697 | <div style="float:right; width: 49%; margin-top: -20px; "> |
| 698 | <p><code>b/BUILD</code></p> |
| 699 | <pre class="code"> |
| 700 | <b>rule( |
| 701 | name = "b", |
| 702 | srcs = "b.in", |
| 703 | deps = "//c:c", |
| 704 | )</b> |
| 705 | </pre> |
| 706 | <p><code>b/b.in</code></p> |
| 707 | <pre class="code"> |
| 708 | <b>import c; |
| 709 | function foo() { |
| 710 | c.bar(); |
| 711 | }</b> |
| 712 | </pre> |
| 713 | </div> |
Googler | 11b0b1f | 2019-10-08 02:03:02 -0700 | [diff] [blame] | 714 | <table style='margin: auto; width: 100%'><tr> |
| 715 | <td style='padding: 10px; text-align: center'> |
| 716 | <!-- digraph G { |
| 717 | graph [size="4,4"]; |
| 718 | node [shape=circle]; |
| 719 | rankdir="LR"; |
| 720 | a -> b -> c; |
| 721 | } --> |
| 722 | <img src="images/a_b_c.svg" alt="a_b_c.svg" style="margin-left=10;" /> |
| 723 | <p><i>Declared dependency graph</i></p> |
| 724 | </td> |
| 725 | <td style='padding: 10px; text-align: center'> |
| 726 | <!-- digraph G { |
| 727 | graph [size="4,4"]; |
| 728 | node [shape=circle]; |
| 729 | rankdir="LR"; |
| 730 | a -> b -> c; |
| 731 | } --> |
| 732 | <img src="images/a_b_c.svg" alt="a_b_c.svg" style="margin-left=10;" /> |
| 733 | <p><i>Actual dependency graph</i></p> |
| 734 | </td> |
| 735 | </tr></table> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 736 | The declared dependencies overapproximate the actual dependencies. |
| 737 | All is well. |
| 738 | </div> |
| 739 | |
| 740 | <div class="greenbox"> |
| 741 | <p><b>2. A latent hazard is introduced.</b></p> |
| 742 | <p> |
| 743 | Someone carelessly adds code to <code>a</code> that creates a direct |
Ryota | 2ab1be8 | 2020-04-03 03:05:26 -0700 | [diff] [blame] | 744 | actual dependency on <code>c</code>, but forgets to declare it in |
| 745 | the build file <code>a/BUILD</code>. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 746 | </p> |
| 747 | <div style="float:left; width: 49%; margin-top: -20px; "> |
| 748 | <p><code>a/a.in</code></p> |
| 749 | <pre class="code"> |
| 750 | import b; |
| 751 | <b>import c;</b> |
| 752 | b.foo(); |
| 753 | <b>c.garply();</b> |
| 754 | </pre> |
| 755 | </div> |
| 756 | |
Googler | 11b0b1f | 2019-10-08 02:03:02 -0700 | [diff] [blame] | 757 | <table style='margin: auto; width: 100%'><tr> |
| 758 | <td style='padding: 10px; text-align: center'> |
| 759 | <!-- digraph G { |
| 760 | graph [size="4,4"]; |
| 761 | node [shape=circle]; |
| 762 | rankdir="LR"; |
| 763 | a -> b -> c; |
| 764 | } --> |
| 765 | <img src="images/a_b_c.svg" alt="a_b_c.svg" style="margin-left=10;" /> |
| 766 | <p><i>Declared dependency graph</i></p> |
| 767 | </td> |
| 768 | <td style='padding: 10; text-align: center'> |
| 769 | <!-- digraph G { |
| 770 | graph [size="4,4"]; |
| 771 | node [shape=circle]; |
| 772 | rankdir="LR"; |
| 773 | a -> b -> c; |
| 774 | a -> c [constraint=false]; |
| 775 | } --> |
| 776 | <img src="images/a_b_c_ac.svg" alt="a_b_c_ac.svg" style="margin-left=10;" /> |
| 777 | <p><i>Actual dependency graph</i></p> |
| 778 | </td> |
| 779 | </tr></table> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 780 | The declared dependencies no longer overapproximate the actual |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 781 | dependencies. This may build ok, because the transitive closures of |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 782 | the two graphs are equal, but masks a problem: <code>a</code> has an |
| 783 | actual but undeclared dependency on <code>c</code>. |
| 784 | </div> |
| 785 | |
| 786 | <div class="greenbox"> |
| 787 | <p><b>3. The hazard is revealed</b> </p> |
| 788 | <p> |
| 789 | Someone refactors <code>b</code> so that it no longer depends on |
| 790 | <code>c</code>, inadvertently breaking <code>a</code> through no |
| 791 | fault of their own. |
| 792 | </p> |
| 793 | <div style="float:right; width: 49%; margin-top: -20px; "> |
| 794 | <p><code>b/BUILD</code></p> |
| 795 | <pre class="code"> |
| 796 | rule( |
| 797 | name = "b", |
| 798 | srcs = "b.in", |
| 799 | <b>deps = "//d:d"</b>, |
| 800 | ) |
| 801 | </pre> |
| 802 | <p><code>b/b.in</code></p> |
| 803 | <pre class="code"> |
| 804 | <b>import d;</b> |
| 805 | function foo() { |
| 806 | <b>d.baz();</b> |
| 807 | } |
| 808 | </pre> |
| 809 | </div> |
Googler | 11b0b1f | 2019-10-08 02:03:02 -0700 | [diff] [blame] | 810 | <table style='margin: auto; width: 100%'><tr> |
| 811 | <td style='padding: 10px; text-align: center'> |
| 812 | <!-- digraph G { |
| 813 | graph [size="4,4"]; |
| 814 | node [shape=circle]; |
| 815 | rankdir="LR"; |
| 816 | a -> b; |
| 817 | b -> c [style=invis]; |
| 818 | } --> |
| 819 | <img src="images/ab_c.svg" alt="ab_c.svg" style="margin-left=10;" /> |
| 820 | <p><i>Declared dependency graph</i></p> |
| 821 | </td> |
| 822 | <td style='padding: 10; text-align: center'> |
| 823 | <!-- digraph G { |
| 824 | graph [size="4,4"]; |
| 825 | node [shape=circle]; |
| 826 | rankdir="LR"; |
| 827 | a -> b; |
| 828 | b -> c [style=invis]; |
| 829 | a -> c [constraint=false]; |
| 830 | } --> |
| 831 | <img src="images/a_b_a_c.svg" alt="a_b_a_c.svg" style="margin-left=10;" /> |
| 832 | <p><i>Actual dependency graph</i></p> |
| 833 | </td> |
| 834 | </tr></table> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 835 | <p> |
| 836 | The declared dependency graph is now an underapproximation of the |
| 837 | actual dependencies, even when transitively closed; the build is |
| 838 | likely to fail. |
| 839 | |
| 840 | The problem could have been averted by ensuring that the actual |
| 841 | dependency from <code>a</code> to <code>c</code> introduced in Step |
| 842 | 2 was properly declared in the BUILD file. |
| 843 | </div> |
| 844 | |
| 845 | <h3 id="types_of_dependencies">Types of dependencies</h3> |
| 846 | |
| 847 | <p> |
| 848 | Most build rules have three attributes for specifying different kinds |
| 849 | of generic dependencies: <code>srcs</code>, <code>deps</code> and |
| 850 | <code>data</code>. These are explained below. See also |
| 851 | <a href='be/common-definitions.html'>Attributes common |
dannark | 27486a6 | 2017-06-26 06:36:31 +0200 | [diff] [blame] | 852 | to all rules</a> in the Build Encyclopedia. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 853 | </p> |
| 854 | |
| 855 | <p> |
| 856 | Many rules also have additional attributes for rule-specific kinds |
| 857 | of dependency, e.g. <code>compiler</code>, <code>resources</code>, |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 858 | etc. These are detailed in the Build Encyclopedia. |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 859 | </p> |
| 860 | |
| 861 | <h4 id="srcs"><code>srcs</code> dependencies</h4> |
| 862 | <p> |
| 863 | Files consumed directly by the rule or rules that output source files. |
| 864 | </p> |
| 865 | |
| 866 | <h4 id="deps"><code>deps</code> dependencies</h4> |
| 867 | <p> |
| 868 | Rule pointing to separately-compiled modules providing header files, |
| 869 | symbols, libraries, data, etc. |
| 870 | </p> |
| 871 | |
| 872 | <h4 id="data"><code>data</code> dependencies</h4> |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 873 | <p>A build target might need some data files to run correctly. These |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 874 | data files aren't source code: they don't affect how the target is |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 875 | built. For example, a unit test might compare a function's output |
Googler | a1a29ec | 2021-02-26 12:19:25 -0800 | [diff] [blame] | 876 | to the contents of a file. When you build the unit test you |
| 877 | don't need the file, but you do need it when you run the test. The |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 878 | same applies to tools that are launched during execution. |
| 879 | |
| 880 | <p>The build system runs tests in an isolated directory where only files |
| 881 | listed as "data" are available. Thus, if a binary/library/test |
| 882 | needs some files to run, specify them (or a build rule containing |
| 883 | them) in data. For example: |
| 884 | </p> |
| 885 | |
| 886 | <pre> |
| 887 | # I need a config file from a directory named env: |
| 888 | java_binary( |
| 889 | name = "setenv", |
| 890 | ... |
| 891 | data = [":env/default_env.txt"], |
| 892 | ) |
| 893 | |
| 894 | # I need test data from another directory |
| 895 | sh_test( |
| 896 | name = "regtest", |
| 897 | srcs = ["regtest.sh"], |
| 898 | data = [ |
| 899 | "//data:file1.txt", |
| 900 | "//data:file2.txt", |
| 901 | ... |
| 902 | ], |
| 903 | ) |
| 904 | </pre> |
| 905 | |
| 906 | <p>These files are available using the relative path |
| 907 | <code>path/to/data/file</code>. In tests, it is also possible to refer to |
| 908 | them by joining the paths of the test's source directory and the workspace-relative |
| 909 | path, e.g. |
| 910 | |
| 911 | <code>${TEST_SRCDIR}/workspace/path/to/data/file</code>. |
Greg Estren | c081675 | 2020-02-20 13:04:29 -0800 | [diff] [blame] | 912 | <h3 id="label_directory">Using labels to reference directories</h3> |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 913 | |
| 914 | <p>As you look over our <code>BUILD</code> files, you might notice |
| 915 | that some <code>data</code> labels refer to directories. |
| 916 | These labels end with <code>/.</code> or <code>/</code> like so: |
| 917 | |
| 918 | <pre> |
| 919 | <span style="text-decoration: line-through">data = ["//data/regression:unittest/."]</span> # don't use this |
| 920 | </pre> |
| 921 | <p> |
| 922 | or like so: |
| 923 | </p> |
| 924 | <pre> |
| 925 | <span style="text-decoration: line-through">data = ["testdata/."]</span> # don't use this |
| 926 | </pre> |
| 927 | |
| 928 | <p> |
| 929 | or like so: |
| 930 | </p> |
| 931 | |
| 932 | <pre> |
| 933 | <span style="text-decoration: line-through">data = ["testdata/"]</span> # don't use this |
| 934 | </pre> |
| 935 | <p>This seems convenient, particularly for tests (since it allows a test to |
| 936 | use all the data files in the directory). |
| 937 | </p> |
| 938 | |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 939 | <p>But try not to do this. In order to ensure correct incremental rebuilds (and |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 940 | re-execution of tests) after a change, the build system must be |
| 941 | aware of the complete set of files that are inputs to the build (or |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 942 | test). When you specify a directory, the build system will perform |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 943 | a rebuild only when the directory itself changes (due to addition or |
| 944 | deletion of files), but won't be able to detect edits to individual |
| 945 | files as those changes do not affect the enclosing directory. |
| 946 | Rather than specifying directories as inputs to the build system, |
| 947 | you should enumerate the set of files contained within them, either |
| 948 | explicitly or using the |
| 949 | <a href='be/functions.html#glob'><code>glob()</code></a> function. |
| 950 | (Use <code>**</code> to force the <a href='be/functions.html#glob'> |
| 951 | <code>glob()</code></a> to be recursive.) |
| 952 | </p> |
| 953 | |
| 954 | <pre> |
| 955 | data = glob(["testdata/**"]) # use this instead |
| 956 | </pre> |
| 957 | |
| 958 | <p>Unfortunately, there are some scenarios where directory labels must be used. |
| 959 | For example, if the <code>testdata</code> directory contains files whose |
philomath | e3ecd4e | 2021-04-26 09:57:11 -0700 | [diff] [blame] | 960 | names do not conform to the <a href='#lexi'>label syntax</a>, then explicit |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 961 | enumeration of files, or use of the |
| 962 | <a href='be/functions.html#glob'><code>glob()</code></a> function will |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 963 | produce an invalid labels error. You must use directory labels in this case, |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 964 | but beware of the concomitant risk of incorrect rebuilds described above. |
| 965 | </p> |
| 966 | |
| 967 | <p>If you must use directory labels, keep in mind that you can't refer to the parent |
| 968 | package with a relative "<code>../</code>" path; instead, use an absolute path like |
| 969 | "<code>//data/regression:unittest/.</code>". |
| 970 | </p> |
| 971 | |
Googler | 25a3c7d | 2021-01-22 18:38:49 +0100 | [diff] [blame] | 972 | <p>Note that directory labels are only valid for data dependencies. If you try to use |
David Chen | 8fe82a3 | 2016-08-24 10:55:41 +0000 | [diff] [blame] | 973 | a directory as a label in an argument other than <code>data</code>, it |
| 974 | will fail and you will get a (probably cryptic) error message. |
| 975 | </p> |
| 976 | |
ranjanih | bf22b11 | 2021-06-01 17:25:01 -0700 | [diff] [blame] | 977 | <p> |
| 978 | Any external rule, such as a test, that needs to use multiple files must explicitly |
| 979 | declare its dependence on all of them. You can use <code>filegroup()</code> to group files |
| 980 | together in the BUILD file: |
| 981 | </p> |
| 982 | |
| 983 | <pre> |
| 984 | filegroup( |
| 985 | name = 'my_data', |
| 986 | srcs = glob(['my_unittest_data/*']) |
| 987 | ) |
| 988 | </pre> |
| 989 | |
| 990 | <p> |
| 991 | You can then reference the label <code>my_data</code> as the data dependency in your test. |
| 992 | </p> |
| 993 | |