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