blob: 558ef7a6656042a26c04715601a0611a8107186c [file] [log] [blame]
David Chen8fe82a32016-08-24 10:55:41 +00001---
2layout: documentation
Greg Estrenc0816752020-02-20 13:04:29 -08003title: Concepts and terminology
daroberts223aebd2021-02-18 17:53:30 -08004category: getting-started
David Chen8fe82a32016-08-24 10:55:41 +00005---
Googler458daaa2021-01-08 18:55:00 -08006<h1>Concepts and Terminology</h1>
David Chen8fe82a32016-08-24 10:55:41 +00007<p>
Googler47849d42021-01-15 17:43:25 -08008 This page provides an overview of the source tree layout and the
David Chen8fe82a32016-08-24 10:55:41 +00009 terminology used in Bazel.
10</p>
David Chen8fe82a32016-08-24 10:55:41 +000011<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 Abrahamsb3b2fea2021-02-25 20:00:34 +010019<h2 id="packages_targets">Workspaces, repositories, packages, and targets</h2>
David Chen8fe82a32016-08-24 10:55:41 +000020<h3 id="workspace">Workspace</h3>
21
22<p>A <em>workspace</em> is a directory on your filesystem that contains the
ranjanihe2226392021-03-11 09:12:47 -080023 source files for the software you want to build. Each workspace directory has
aehlig7bdfb572019-08-08 05:08:37 -070024 a text file named <code>WORKSPACE</code> which may be empty, or may contain
dzc205125b2017-06-26 11:01:47 +020025 references to <a href="external.html">external dependencies</a>
aehlig7bdfb572019-08-08 05:08:37 -070026 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 Peng0af987b2019-11-11 07:19:54 -080034<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
aehlig7bdfb572019-08-08 05:08:37 -070037<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 Abrahams36de45a2021-02-16 15:31:50 -080044 <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 Kumara0673352020-05-18 11:06:51 -070046 <a href="repo/index.html">embedded Starlark repository rules</a>.</p>
aehlig7bdfb572019-08-08 05:08:37 -070047
panzhongxian28fe62e2019-08-21 07:36:32 -070048<p>As external repositories are repositories themselves, they often contain
aehlig7bdfb572019-08-08 05:08:37 -070049 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 Chen8fe82a32016-08-24 10:55:41 +000053<h3 id="packages">Packages</h3>
54<p>
aehlig7bdfb572019-08-08 05:08:37 -070055 The primary unit of code organization in a repository is
Googler25a3c7d2021-01-22 18:38:49 +010056 the <i>package</i>. A package is a collection of related files and a
David Chen8fe82a32016-08-24 10:55:41 +000057 specification of the dependencies among them.
58</p>
laurentlb5ec875a2018-10-18 11:22:28 -070059
David Chen8fe82a32016-08-24 10:55:41 +000060<p>
61 A package is defined as a directory containing a file
laurentlb49939052018-10-18 13:02:55 -070062 named <code>BUILD</code> or <code>BUILD.bazel</code>,
laurentlb5ec875a2018-10-18 11:22:28 -070063 residing beneath the top-level directory in the
Googler25a3c7d2021-01-22 18:38:49 +010064 workspace. A package includes all files in its directory, plus all
David Chen8fe82a32016-08-24 10:55:41 +000065 subdirectories beneath it, except those which themselves contain a BUILD
66 file.
67</p>
laurentlb5ec875a2018-10-18 11:22:28 -070068
David Chen8fe82a32016-08-24 10:55:41 +000069<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>
78src/my/app/BUILD
79src/my/app/app.cc
80src/my/app/data/input.txt
81src/my/app/tests/BUILD
82src/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 Chen8fe82a32016-08-24 10:55:41 +000094<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.
philomathe3ecd4e2021-04-26 09:57:11 -070098 <i>Generated files</i>, sometimes called derived files or output files,
David Chen8fe82a32016-08-24 10:55:41 +000099 are not checked in, but are generated by the build tool from source
100 files according to specific rules.
101</p>
102
103<p>
Googler25a3c7d2021-01-22 18:38:49 +0100104 The second kind of target is the <i>rule</i>. A rule specifies the
David Chen8fe82a32016-08-24 10:55:41 +0000105 relationship between a set of input and a set of output files,
106 including the necessary steps to derive the outputs from the inputs.
Googler25a3c7d2021-01-22 18:38:49 +0100107 The outputs of a rule are always generated files. The inputs to a
David Chen8fe82a32016-08-24 10:55:41 +0000108 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
Googler25a3c7d2021-01-22 18:38:49 +0100116 file. This fact makes it easy to replace a complex source file with
David Chen8fe82a32016-08-24 10:55:41 +0000117 a generated file produced by a rule, such as happens when the burden
118 of manually maintaining a highly structured file becomes too
Googler25a3c7d2021-01-22 18:38:49 +0100119 tiresome, and someone writes a program to derive it. No change is
120 required to the consumers of that file. Conversely, a generated
David Chen8fe82a32016-08-24 10:55:41 +0000121 file may easily be replaced by a source file with only local
122 changes.
123</p>
124
125<p>
Googler25a3c7d2021-01-22 18:38:49 +0100126 The inputs to a rule may also include <i>other rules</i>. The
David Chen8fe82a32016-08-24 10:55:41 +0000127 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.
Googler793b18d2019-05-09 08:52:01 -0700130 The effect of this dependency is that B's header files are
David Chen8fe82a32016-08-24 10:55:41 +0000131 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
Googler25a3c7d2021-01-22 18:38:49 +0100139 possible to generate files into another package. It is not uncommon
David Chen8fe82a32016-08-24 10:55:41 +0000140 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
philomathe3ecd4e2021-04-26 09:57:11 -0700146 <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 Chen8fe82a32016-08-24 10:55:41 +0000148 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 Abrahamsaf3a5562021-02-16 16:54:06 -0800151 to the <a
152 href='be/functions.html#package_group'><code>package_group</code>
153 documentation</a>.
David Chen8fe82a32016-08-24 10:55:41 +0000154</p>
155
156
157<h3 id="labels">Labels</h3>
158
159<p>
Googler25a3c7d2021-01-22 18:38:49 +0100160 All targets belong to exactly one package. The name of a target is
Dave Abrahams804bcc142021-02-22 09:51:27 -0800161 called its <em>label</em>. Every label uniquely identifies a target. A
162 typical label in canonical form looks like:
David Chen8fe82a32016-08-24 10:55:41 +0000163</p>
164
aehlig7bdfb572019-08-08 05:08:37 -0700165
David Chen8fe82a32016-08-24 10:55:41 +0000166<pre>
aehlig7bdfb572019-08-08 05:08:37 -0700167@myrepo//my/app/main:app_binary
David Chen8fe82a32016-08-24 10:55:41 +0000168</pre>
169
170<p>
Dave Abrahams804bcc142021-02-22 09:51:27 -0800171 The first part of the label is the repository name, <code>@myrepo//</code>.
Dave Abrahamsff348f42021-03-03 12:02:30 -0800172 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
aehlig7bdfb572019-08-08 05:08:37 -0700175</p>
David Chen8fe82a32016-08-24 10:55:41 +0000176
aehligc2efa6f2019-08-08 08:18:44 -0700177<pre>
178//my/app/main:app_binary
179</pre>
180
181<p>
182
Dave Abrahams804bcc142021-02-22 09:51:27 -0800183 The second part of the label is the un-qualified package name
184 <code>my/app/main</code>, the path to the package
Dave Abrahams080ef6e2021-04-22 07:57:30 -0700185 relative to the repository root. Together, the repository name and the
Niek Peeters00196b22021-05-11 10:28:24 -0700186 un-qualified package name form the fully-qualified package name
Dave Abrahams080ef6e2021-04-22 07:57:30 -0700187 <code>@myrepo//my/app/main</code>. When the label refers to the same
Dave Abrahams804bcc142021-02-22 09:51:27 -0800188 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>
195app_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 Chen8fe82a32016-08-24 10:55:41 +0000209 equivalent:
210</p>
211
212<pre>
Dave Abrahams804bcc142021-02-22 09:51:27 -0800213//my/app/lib
214//my/app/lib:lib
David Chen8fe82a32016-08-24 10:55:41 +0000215</pre>
216
217<p>
Dave Abrahams804bcc142021-02-22 09:51:27 -0800218 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 Chen8fe82a32016-08-24 10:55:41 +0000235
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
Googlercfae2702021-08-12 17:21:40 -0700238 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 Chen8fe82a32016-08-24 10:55:41 +0000240</p>
241
242<p>
243 Relative labels cannot be used to refer to targets in other
Dave Abrahams804bcc142021-02-22 09:51:27 -0800244 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 Chen8fe82a32016-08-24 10:55:41 +0000247 <code>my/app</code> and the package
248 <code>my/app/testdata</code> (i.e., each of these two
philomathe3ecd4e2021-04-26 09:57:11 -0700249 directories has its own BUILD file). The latter package contains a
Googler25a3c7d2021-01-22 18:38:49 +0100250 file named <code>testdepot.zip</code>. Here are two ways (one
David Chen8fe82a32016-08-24 10:55:41 +0000251 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 McLoughlin896391b2020-02-03 15:44:51 -0800261 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
philomathe3ecd4e2021-04-26 09:57:11 -0700269 used from external repositories.
270</p>
271
272<p>
Googler27829042021-05-03 10:36:33 -0700273For information about the different ways you can refer to targets, see
philomath269e86c2021-05-12 06:17:26 -0700274<a href="guide.md#specifying-targets-to-build">target patterns</a>.
Michael McLoughlin896391b2020-02-03 15:44:51 -0800275</p>
276
Googler64047ac2021-03-08 13:05:15 -0800277
David Chen8fe82a32016-08-24 10:55:41 +0000278<h3 id="lexi">Lexical specification of a label</h3>
279
280<p>
philomathe3ecd4e2021-04-26 09:57:11 -0700281 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>.
dmartingdffa6362017-08-30 19:23:32 +0200284
twiggd0aac3d2019-10-25 09:42:55 -0700285 The precise details of allowed target names are below.
David Chen8fe82a32016-08-24 10:55:41 +0000286</p>
287
Philipp Schrader2dd2ecc2021-04-05 08:17:52 -0700288<h4 id="name">Target names, <code>//&lt;package name&gt;:<b>target-name</b></code></h4>
David Chen8fe82a32016-08-24 10:55:41 +0000289
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>
Googler98384492019-05-14 11:26:47 -0700292 attribute in the rule's declaration in a BUILD file; the name
David Chen8fe82a32016-08-24 10:55:41 +0000293 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>,
twiggd0aac3d2019-10-25 09:42:55 -0700298 and the punctuation symbols <code>!%-@^_` "#$&'()*-+,;<=>?[]{|}~/.</code>.
David Chen8fe82a32016-08-24 10:55:41 +0000299 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
Googler25a3c7d2021-01-22 18:38:49 +0100305 (e.g. <code>foo//bar</code>). Similarly, up-level references
David Chen8fe82a32016-08-24 10:55:41 +0000306 (<code>..</code>) and current-directory references
philomathe3ecd4e2021-04-26 09:57:11 -0700307 (<code>./</code>) are forbidden.
David Chen8fe82a32016-08-24 10:55:41 +0000308</p>
309
310<p>While it is common to use <code>/</code> in the name of a file
Googlera1a29ec2021-02-26 12:19:25 -0800311 target, it is recommended that you avoid the use of <code>/</code> in the
Googler25a3c7d2021-01-22 18:38:49 +0100312 names of rules. Especially when the shorthand form of a label is
313 used, it may confuse the reader. The
David Chen8fe82a32016-08-24 10:55:41 +0000314 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
Googler25a3c7d2021-01-22 18:38:49 +0100320 convenient, or sometimes even necessary. For example, the name of
David Chen8fe82a32016-08-24 10:55:41 +0000321 certain rules must match their principal source file, which may
322 reside in a subdirectory of the package.</p>
323
Philipp Schrader2dd2ecc2021-04-05 08:17:52 -0700324<h4>Package names, <code>//<b>package-name</b>:&lt;target name&gt;</code></h4>
David Chen8fe82a32016-08-24 10:55:41 +0000325<p>
326 The name of a package is the name of the directory containing its
327
Dave Abrahams976d9b02021-02-22 00:02:48 -0800328 BUILD file, relative to the top-level directory of the containing
329 repository. For example: <code>my/app</code>.
David Chen8fe82a32016-08-24 10:55:41 +0000330
Googler0bcc9842016-09-15 14:06:13 +0000331 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 Chen8fe82a32016-08-24 10:55:41 +0000336<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 Abrahamsb62ec6f2021-02-22 12:55:12 -0800343 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 Chen8fe82a32016-08-24 10:55:41 +0000346</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
Googler25a3c7d2021-01-22 18:38:49 +0100356 steps to build the outputs. Rules can be of one of many different
David Chen8fe82a32016-08-24 10:55:41 +0000357 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,
Googler25a3c7d2021-01-22 18:38:49 +0100365 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 Chen8fe82a32016-08-24 10:55:41 +0000367 somewhat arbitrary, and more interesting are the names of the files
Googler0ef04542021-03-09 08:38:20 -0800368 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 Chen8fe82a32016-08-24 10:55:41 +0000373 and <code>*_test</code> rules, for example, the rule name determines
374 the name of the executable produced by the build.
375</p>
376
laurentlb5ec875a2018-10-18 11:22:28 -0700377<pre>
378cc_binary(
379 name = "my_app",
380 srcs = ["my_app.cc"],
381 deps = [
382 "//absl/base",
383 "//absl/strings",
384 ],
385)
386</pre>
387
David Chen8fe82a32016-08-24 10:55:41 +0000388<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
laurentlb5ec875a2018-10-18 11:22:28 -0700393 Encyclopedia</a> for a list of rules and their
Googler25a3c7d2021-01-22 18:38:49 +0100394 corresponding attributes. Each attribute has a name and a
395 type. Some of the common types an attribute can have are integer,
David Chen8fe82a32016-08-24 10:55:41 +0000396 label, list of labels, string, list of strings, output label,
Googler25a3c7d2021-01-22 18:38:49 +0100397 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 Chen8fe82a32016-08-24 10:55:41 +0000399 optional, typed values.
400</p>
401
402<p>
403 The <code>srcs</code> attribute present in many rules has type "list
Googlerf772a692019-07-09 11:12:25 -0700404 of labels"; its value, if present, is a list of labels, each being
David Chen8fe82a32016-08-24 10:55:41 +0000405 the name of a target that is an input to this rule.
406</p>
407
408<p>
David Chen8fe82a32016-08-24 10:55:41 +0000409 This directed acyclic graph over targets is called the
410 "target graph" or "build dependency graph", and is the domain over
dmartingdffa6362017-08-30 19:23:32 +0200411 which the <a href='query.html'>Bazel Query tool</a> operates.
David Chen8fe82a32016-08-24 10:55:41 +0000412</p>
413
414
Greg Estrenc0816752020-02-20 13:04:29 -0800415<h2 id="BUILD_files">BUILD files</h2>
David Chen8fe82a32016-08-24 10:55:41 +0000416
417<p>
418 The previous section described packages, targets and labels, and the
Googlera1a29ec2021-02-26 12:19:25 -0800419 build dependency graph abstractly. This section describes the
420 concrete syntax used to define a package.
David Chen8fe82a32016-08-24 10:55:41 +0000421</p>
422
423<p>
424 By definition, every package contains a BUILD file, which is a short
laurentlb5ec875a2018-10-18 11:22:28 -0700425 program.
426 BUILD files are evaluated using an imperative language,
laurentlb353dd742018-10-03 15:25:02 -0700427 <a href="https://github.com/bazelbuild/starlark/">Starlark</a>.
David Chen8fe82a32016-08-24 10:55:41 +0000428
laurentlb5ec875a2018-10-18 11:22:28 -0700429 They are interpreted as a sequential list of statements.
laurentlb353dd742018-10-03 15:25:02 -0700430</p>
431
432<p>
laurentlb5ec875a2018-10-18 11:22:28 -0700433 In general, order does matter: variables must be defined before they are used, for
Googler25a3c7d2021-01-22 18:38:49 +0100434 example. However, most BUILD files consist only of declarations of
laurentlb5ec875a2018-10-18 11:22:28 -0700435 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
laurentlb353dd742018-10-03 15:25:02 -0700450 contain function definitions, <code>for</code> statements or
451 <code>if</code> statements (but list comprehensions and <code>if</code>
philomathe3ecd4e2021-04-26 09:57:11 -0700452 expressions are allowed). Functions can be declared in <code>.bzl</code>
laurentlb4b6f3622019-02-11 09:42:49 -0800453 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.
laurentlb353dd742018-10-03 15:25:02 -0700456</p>
457
458<p>
laurentlb5ec875a2018-10-18 11:22:28 -0700459 Crucially, programs in Starlark are unable to perform
Googler25a3c7d2021-01-22 18:38:49 +0100460 arbitrary I/O. This invariant makes the
laurentlb353dd742018-10-03 15:25:02 -0700461 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 Chen8fe82a32016-08-24 10:55:41 +0000472<p>
laurentlb5ec875a2018-10-18 11:22:28 -0700473 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 Chen8fe82a32016-08-24 10:55:41 +0000479</p>
480
laurentlb78f245d2018-10-23 10:58:16 -0700481<h3 id="load">Loading an extension</h3>
482
483Bazel extensions are files ending in <code>.bzl</code>. Use
484the <code>load</code> statement to import a symbol from an extension.
485
486<pre>
487load("//foo/bar:file.bzl", "some_library")
laurentlb51482d72018-10-23 15:33:01 -0700488</pre>
laurentlb78f245d2018-10-23 10:58:16 -0700489
490This 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
492new rules, functions or constants (e.g. a string, a list, etc.). Multiple
493symbols can be imported by using additional arguments to the call
494to <code>load</code>. Arguments must be string literals (no variable)
495and <code>load</code> statements must appear at top-level, i.e. they cannot be
496in a function body.
497
498The first argument of <code>load</code> is a <a href="#labels">label</a>
499identifying a <code>.bzl</code> file. If it is a relative label, it is resolved
500with respect to the package (not directory) containing the current
501<code>bzl</code> file. Relative labels in <code>load</code> statements should
502use a leading <code>:</code>.
503
504<code>load</code> also supports aliases, i.e. you can assign different names to
505the imported symbols.
506
507<pre>
508load("//foo/bar:file.bzl", library_alias = "some_library")
509</pre>
510
511You can define multiple aliases within one <code>load</code> statement.
512Moreover, the argument list can contain both aliases and regular symbol names.
513The following example is perfectly legal (please note when to use quotation
514marks).
515
516<pre>
517load(":my_rules.bzl", "some_rule", nice_alias = "some_other_rule")
518</pre>
519
520In a <code>.bzl</code> file, symbols starting with <code>_</code> are not
521exported and cannot be loaded from another file. Visibility doesn't affect
522loading (yet): you don't need to use <code>exports_files</code> to make
523a <code>.bzl</code> file visible.
524
Googler1933da52021-01-20 10:04:49 -0800525<h2 id="funcs">Types of build rules</h2>
David Chen8fe82a32016-08-24 10:55:41 +0000526
527<p>
528 The majority of build rules come in families, grouped together by
Googler25a3c7d2021-01-22 18:38:49 +0100529 language. For example, <code>cc_binary</code>, <code>cc_library</code>
David Chen8fe82a32016-08-24 10:55:41 +0000530 and <code>cc_test</code> are the build rules for C++ binaries,
Googler25a3c7d2021-01-22 18:38:49 +0100531 libraries, and tests, respectively. Other languages use the same
David Chen8fe82a32016-08-24 10:55:41 +0000532 naming scheme, with a different prefix, e.g. <code>java_*</code> for
laurentlb5ec875a2018-10-18 11:22:28 -0700533 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 Chen8fe82a32016-08-24 10:55:41 +0000536</p>
537
538<ul>
539 <li><p><code>*_binary</code>
Googler25a3c7d2021-01-22 18:38:49 +0100540 rules build executable programs in a given language. After a
David Chen8fe82a32016-08-24 10:55:41 +0000541 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
philomathe3ecd4e2021-04-26 09:57:11 -0700546 <p>In some languages, such rules also create a runfiles directory
David Chen8fe82a32016-08-24 10:55:41 +0000547
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
Googler25a3c7d2021-01-22 18:38:49 +0100556 testing. Tests are simply programs that return zero on success.
David Chen8fe82a32016-08-24 10:55:41 +0000557
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
Googler25a3c7d2021-01-22 18:38:49 +0100563 at runtime. For example, a program <code>cc_test(name='x',
David Chen8fe82a32016-08-24 10:55:41 +0000564 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
Googler25a3c7d2021-01-22 18:38:49 +0100578 programming language. Libraries can depend on other libraries,
David Chen8fe82a32016-08-24 10:55:41 +0000579 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
Googler25a3c7d2021-01-22 18:38:49 +0100589 build or execution time. The <i>depends upon</i> relation induces a
laurentlb5ec875a2018-10-18 11:22:28 -0700590 <a href="https://en.wikipedia.org/wiki/Directed_acyclic_graph">Directed
Googlera1a29ec2021-02-26 12:19:25 -0800591 Acyclic Graph</a> (DAG) over targets, and it is called a
David Chen8fe82a32016-08-24 10:55:41 +0000592 <em>dependency graph</em>.
593
594 A target's <em>direct</em> dependencies are those other targets
Googler25a3c7d2021-01-22 18:38:49 +0100595 reachable by a path of length 1 in the dependency graph. A target's
David Chen8fe82a32016-08-24 10:55:41 +0000596 <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
Googler25a3c7d2021-01-22 18:38:49 +0100603 <em>declared dependencies</em>. Most of the time, the two graphs
David Chen8fe82a32016-08-24 10:55:41 +0000604 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
Googler11b0b1f2019-10-08 02:03:02 -0700612 <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 Chen8fe82a32016-08-24 10:55:41 +0000614 "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
Googler11b0b1f2019-10-08 02:03:02 -0700621 <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 Chen8fe82a32016-08-24 10:55:41 +0000623</p>
624
625<p>
626 For correct builds, the graph of actual dependencies <i>A</i> must
Googler25a3c7d2021-01-22 18:38:49 +0100627 be a subgraph of the graph of declared dependencies <i>D</i>. That
David Chen8fe82a32016-08-24 10:55:41 +0000628 is, every pair of directly-connected nodes <code>x --&gt; y</code>
Googlera1a29ec2021-02-26 12:19:25 -0800629 in <i>A</i> must also be directly connected in <i>D</i>. It can be said that
David Chen8fe82a32016-08-24 10:55:41 +0000630 <i>D</i> is an <em>overapproximation</em> of <i>A</i>.
631</p>
632
633<p>
Ryota2ab1be82020-04-03 03:05:26 -0700634 It is important that it is not too much of an overapproximation,
David Chen8fe82a32016-08-24 10:55:41 +0000635 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
Ryota2ab1be82020-04-03 03:05:26 -0700646 operations, or upon transitive declared dependencies the target
Googler25a3c7d2021-01-22 18:38:49 +0100647 happens to have. The build tool attempts aggressively to check for
David Chen8fe82a32016-08-24 10:55:41 +0000648 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
Googler25a3c7d2021-01-22 18:38:49 +0100669 edge in the declared dependency graph. Indirect dependencies do not
670 appear in the BUILD file. Since the rule doesn't
David Chen8fe82a32016-08-24 10:55:41 +0000671 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>.
679The code in package <code>b</code> uses code in package <code>c</code>,
680and 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;
694b.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;
709function foo() {
710 c.bar();
711}</b>
712</pre>
713</div>
Googler11b0b1f2019-10-08 02:03:02 -0700714<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 Chen8fe82a32016-08-24 10:55:41 +0000736The declared dependencies overapproximate the actual dependencies.
737All 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
Ryota2ab1be82020-04-03 03:05:26 -0700744 actual dependency on <code>c</code>, but forgets to declare it in
745 the build file <code>a/BUILD</code>.
David Chen8fe82a32016-08-24 10:55:41 +0000746</p>
747<div style="float:left; width: 49%; margin-top: -20px; ">
748<p><code>a/a.in</code></p>
749<pre class="code">
750import b;
751<b>import c;</b>
752b.foo();
753<b>c.garply();</b>
754</pre>
755</div>
756
Googler11b0b1f2019-10-08 02:03:02 -0700757<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 Chen8fe82a32016-08-24 10:55:41 +0000780The declared dependencies no longer overapproximate the actual
Googler25a3c7d2021-01-22 18:38:49 +0100781dependencies. This may build ok, because the transitive closures of
David Chen8fe82a32016-08-24 10:55:41 +0000782the two graphs are equal, but masks a problem: <code>a</code> has an
783actual 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">
796rule(
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>
805function foo() {
806 <b>d.baz();</b>
807}
808</pre>
809</div>
Googler11b0b1f2019-10-08 02:03:02 -0700810<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 Chen8fe82a32016-08-24 10:55:41 +0000835<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
dannark27486a62017-06-26 06:36:31 +0200852 to all rules</a> in the Build Encyclopedia.
David Chen8fe82a32016-08-24 10:55:41 +0000853</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>,
Googler25a3c7d2021-01-22 18:38:49 +0100858 etc. These are detailed in the Build Encyclopedia.
David Chen8fe82a32016-08-24 10:55:41 +0000859</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>
Googler25a3c7d2021-01-22 18:38:49 +0100873<p>A build target might need some data files to run correctly. These
David Chen8fe82a32016-08-24 10:55:41 +0000874 data files aren't source code: they don't affect how the target is
Googler25a3c7d2021-01-22 18:38:49 +0100875 built. For example, a unit test might compare a function's output
Googlera1a29ec2021-02-26 12:19:25 -0800876 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 Chen8fe82a32016-08-24 10:55:41 +0000878 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:
888java_binary(
889 name = "setenv",
890 ...
891 data = [":env/default_env.txt"],
892)
893
894# I need test data from another directory
895sh_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
908them by joining the paths of the test's source directory and the workspace-relative
909path, e.g.
910
911<code>${TEST_SRCDIR}/workspace/path/to/data/file</code>.
Greg Estrenc0816752020-02-20 13:04:29 -0800912 <h3 id="label_directory">Using labels to reference directories</h3>
David Chen8fe82a32016-08-24 10:55:41 +0000913
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>
922or 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>
929or 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
Googler25a3c7d2021-01-22 18:38:49 +0100939 <p>But try not to do this. In order to ensure correct incremental rebuilds (and
David Chen8fe82a32016-08-24 10:55:41 +0000940 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
Googler25a3c7d2021-01-22 18:38:49 +0100942 test). When you specify a directory, the build system will perform
David Chen8fe82a32016-08-24 10:55:41 +0000943 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>
955data = 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
philomathe3ecd4e2021-04-26 09:57:11 -0700960 names do not conform to the <a href='#lexi'>label syntax</a>, then explicit
David Chen8fe82a32016-08-24 10:55:41 +0000961 enumeration of files, or use of the
962 <a href='be/functions.html#glob'><code>glob()</code></a> function will
Googler25a3c7d2021-01-22 18:38:49 +0100963 produce an invalid labels error. You must use directory labels in this case,
David Chen8fe82a32016-08-24 10:55:41 +0000964 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
Googler25a3c7d2021-01-22 18:38:49 +0100972 <p>Note that directory labels are only valid for data dependencies. If you try to use
David Chen8fe82a32016-08-24 10:55:41 +0000973 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
ranjanihbf22b112021-06-01 17:25:01 -0700977 <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