blob: 6bb3e119269ffd0668e5f9ebc27640e816668376 [file] [log] [blame]
David Chenc4af8282016-01-20 11:06:35 +00001---
2layout: documentation
Tobias Werthd57a6532016-06-27 14:12:23 +00003title: BUILD files
David Chenc4af8282016-01-20 11:06:35 +00004---
Florian Weikert5e546d52016-06-30 14:08:04 +00005<h1>Bazel: Build Files and Terminology</h1>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01006<p>
Florian Weikert5e546d52016-06-30 14:08:04 +00007 This document provides an overview of the source tree layout and the
8 terminology used in Bazel.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01009</p>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010010<h2>Table of Contents</h2>
Laszlo Csomor529f3fd32015-03-24 13:22:21 +000011
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010012<ul>
13 <li><a href="#intro">Introduction</a></li>
Googlerc061bed2015-08-30 17:17:57 +000014
15 <li><a href="#packages_targets">Workspace, Packages and Targets</a>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016 <ul>
Tobias Werthd57a6532016-06-27 14:12:23 +000017 <li><a href="#workspace">Workspace</a></li>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018 <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>
27 <li><a href="#core_build_language">The Core Build Language</a></li>
Laszlo Csomor529f3fd32015-03-24 13:22:21 +000028
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029 <li><a href="#declaring_build_rules">Declaring Build Rules</a></li>
30 </ul>
31 </li>
32 <li><a href="#funcs">Types of Build Rules</a></li>
Laszlo Csomor529f3fd32015-03-24 13:22:21 +000033
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034 <li><a href="#dependencies">Dependencies</a>
35 <ul>
36 <li><a href="#actual_and_declared_dependencies">Actual and Declared Dependencies</a></li>
37 <li><a href="#types_of_dependencies">Types of Dependencies</a></li>
38 <li><a href="#label_directory">Using Labels to Reference Directories</a></li>
39 </ul>
40 </li>
41</ul>
Laszlo Csomor529f3fd32015-03-24 13:22:21 +000042
43<h2 id="intro">Introduction</h2>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044
Googlerc061bed2015-08-30 17:17:57 +000045<p>Bazel builds software from source code organized in a directory called
46 a workspace. Source files in the workspace are organized in a nested
47 hierarchy of packages, where each package is a directory that contains a set
Tobias Werthd57a6532016-06-27 14:12:23 +000048 of related source files and one BUILD file. The BUILD file specifies what
Googlerc061bed2015-08-30 17:17:57 +000049 software outputs can be built from the source.
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000050</p>
Googlerc061bed2015-08-30 17:17:57 +000051<h2 id="packages_targets">Workspace, Packages and Targets</h2>
Tobias Werthd57a6532016-06-27 14:12:23 +000052<h3 id="workspace">Workspace</h3>
Laszlo Csomor529f3fd32015-03-24 13:22:21 +000053
Googlerc061bed2015-08-30 17:17:57 +000054<p>A <em>workspace</em> is a directory on your filesystem that contains the
55 source files for the software you want to build, as well as symbolic links
56 to directories that contain the build outputs. Each workspace directory has
57 a text file named <code>WORKSPACE</code> which may be empty, or may contain
58 references to <a href="/docs/external.html">external dependencies</a>
Tobias Werthd57a6532016-06-27 14:12:23 +000059 required to build the outputs. See also the <a
60 href="/docs/be/workspace.html">Workspace Rules</a> section in the Build
61 Encyclopedia.
Googlerc061bed2015-08-30 17:17:57 +000062</p>
Laszlo Csomor529f3fd32015-03-24 13:22:21 +000063<h3 id="packages">Packages</h3>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010064<p>
Mark Schaller86eeb8c2015-09-14 14:36:37 +000065 The primary unit of code organization in a workspace is
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010066 the <i>package</i>. A package is collection of related files and a
67 specification of the dependencies among them.
68</p>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069<p>
70 A package is defined as a directory containing a file
71 named <code>BUILD</code>, residing beneath the top-level directory in the
Googlerc061bed2015-08-30 17:17:57 +000072 workspace. A package includes all files in its directory, plus all
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 subdirectories beneath it, except those which themselves contain a BUILD
74 file.
75</p>
Laszlo Csomord9b2b862015-03-25 12:01:43 +000076<p>
Tobias Werthd57a6532016-06-27 14:12:23 +000077 For example, in the following directory tree
78 there are two packages, <code>my/app</code>,
79 and the subpackage <code>my/app/tests</code>.
80 Note that <code>my/app/data</code> is not a package, but a directory
81 belonging to package <code>my/app</code>.
Laszlo Csomord9b2b862015-03-25 12:01:43 +000082</p>
83
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010084<pre>
85src/my/app/BUILD
86src/my/app/app.cc
87src/my/app/data/input.txt
88src/my/app/tests/BUILD
89src/my/app/tests/test.cc
90</pre>
Laszlo Csomor529f3fd32015-03-24 13:22:21 +000091<h3 id="targets">Targets</h3>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010092
93<p>
Florian Weikert5e546d52016-06-30 14:08:04 +000094 A package is a container. The elements of a package are called
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010095 <i>targets</i>. Most targets are one of two principal kinds, <i>files</i>
96 and <i>rules</i>. Additionally, there is another kind of target,
David Chenc23d6612015-11-02 22:56:13 +000097 <a href="be/functions.html#package_group">package groups</a>,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010098 but they are far less numerous.
99</p>
100
101<div style='margin:auto; text-align: center'>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000102<svg width="582pt" height="188pt"
103 viewBox="0.00 0.00 581.89 188.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000104<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 184)">
105<title>G1</title>
Chris Parsons591d1e12016-06-14 13:34:30 +0000106<polygon fill="white" stroke="transparent" points="-4,4 -4,-184 577.888,-184 577.888,4 -4,4"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000107<!-- Target -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000108<g id="node1" class="node">
109<title>Target</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000110<ellipse fill="none" stroke="black" cx="376.795" cy="-162" rx="40.0939" ry="18"/>
111<text text-anchor="middle" x="376.795" y="-158.3" font-family="arial" font-size="14.00">Target</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000112</g>
113<!-- Rule -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000114<g id="node2" class="node">
115<title>Rule</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000116<ellipse fill="none" stroke="black" cx="241.795" cy="-90" rx="30.5947" ry="18"/>
117<text text-anchor="middle" x="241.795" y="-86.3" font-family="arial" font-size="14.00">Rule</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000118</g>
119<!-- Target&#45;&gt;Rule -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000120<g id="edge1" class="edge">
121<title>Target&#45;&gt;Rule</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000122<path fill="none" stroke="black" d="M351.402,-147.834C329.151,-136.296 296.735,-119.487 272.926,-107.142"/>
123<polygon fill="black" stroke="black" points="274.432,-103.98 263.943,-102.484 271.21,-110.195 274.432,-103.98"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000124</g>
125<!-- File -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000126<g id="node6" class="node">
127<title>File</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000128<ellipse fill="none" stroke="black" cx="376.795" cy="-90" rx="30.5947" ry="18"/>
129<text text-anchor="middle" x="376.795" y="-86.3" font-family="arial" font-size="14.00">File</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000130</g>
131<!-- Target&#45;&gt;File -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000132<g id="edge5" class="edge">
133<title>Target&#45;&gt;File</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000134<path fill="none" stroke="black" d="M376.795,-143.697C376.795,-135.983 376.795,-126.712 376.795,-118.112"/>
135<polygon fill="black" stroke="black" points="380.295,-118.104 376.795,-108.104 373.295,-118.104 380.295,-118.104"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000136</g>
137<!-- Package group -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000138<g id="node9" class="node">
139<title>Package group</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000140<ellipse fill="none" stroke="black" cx="499.795" cy="-90" rx="74.187" ry="18"/>
141<text text-anchor="middle" x="499.795" y="-86.3" font-family="arial" font-size="14.00">Package group</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000142</g>
143<!-- Target&#45;&gt;Package group -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000144<g id="edge8" class="edge">
145<title>Target&#45;&gt;Package group</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000146<path fill="none" stroke="black" d="M400.802,-147.337C418.506,-137.262 442.911,-123.373 463.059,-111.906"/>
147<polygon fill="black" stroke="black" points="465.018,-114.819 471.978,-106.831 461.555,-108.735 465.018,-114.819"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000148</g>
149<!-- cc_library -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000150<g id="node3" class="node">
151<title>cc_library</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000152<ellipse fill="none" stroke="black" cx="59.7947" cy="-18" rx="59.5901" ry="18"/>
153<text text-anchor="middle" x="59.7947" y="-14.3" font-family="arial" font-size="14.00">cc_library</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000154</g>
155<!-- Rule&#45;&gt;cc_library -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000156<g id="edge2" class="edge">
157<title>Rule&#45;&gt;cc_library</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000158<path fill="none" stroke="black" d="M216.87,-79.4136C188.038,-68.3243 140.006,-49.8505 104.633,-36.2453"/>
159<polygon fill="black" stroke="black" points="105.607,-32.8704 95.0176,-32.5473 103.095,-39.4038 105.607,-32.8704"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000160</g>
161<!-- java_test -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000162<g id="node4" class="node">
163<title>java_test</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000164<ellipse fill="none" stroke="black" cx="191.795" cy="-18" rx="54.6905" ry="18"/>
165<text text-anchor="middle" x="191.795" y="-14.3" font-family="arial" font-size="14.00">java_test</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000166</g>
167<!-- Rule&#45;&gt;java_test -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000168<g id="edge3" class="edge">
169<title>Rule&#45;&gt;java_test</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000170<path fill="none" stroke="black" d="M230.449,-73.1159C224.298,-64.5051 216.557,-53.6674 209.621,-43.9567"/>
171<polygon fill="black" stroke="black" points="212.453,-41.9004 203.793,-35.7973 206.757,-45.9691 212.453,-41.9004"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000172</g>
173<!-- ... -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000174<g id="node5" class="node">
175<title>...</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000176<ellipse fill="none" stroke="black" cx="291.795" cy="-18" rx="27" ry="18"/>
177<text text-anchor="middle" x="291.795" y="-14.3" font-family="arial" font-size="14.00">...</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000178</g>
179<!-- Rule&#45;&gt;... -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000180<g id="edge4" class="edge">
181<title>Rule&#45;&gt;...</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000182<path fill="none" stroke="black" d="M253.14,-73.1159C259.469,-64.2555 267.482,-53.0373 274.57,-43.1152"/>
183<polygon fill="black" stroke="black" points="277.529,-44.9929 280.494,-34.8212 271.833,-40.9242 277.529,-44.9929"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000184</g>
185<!-- Source -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000186<g id="node7" class="node">
187<title>Source</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000188<ellipse fill="none" stroke="black" cx="376.795" cy="-18" rx="40.0939" ry="18"/>
189<text text-anchor="middle" x="376.795" y="-14.3" font-family="arial" font-size="14.00">Source</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000190</g>
191<!-- File&#45;&gt;Source -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000192<g id="edge6" class="edge">
193<title>File&#45;&gt;Source</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000194<path fill="none" stroke="black" d="M376.795,-71.6966C376.795,-63.9827 376.795,-54.7125 376.795,-46.1124"/>
195<polygon fill="black" stroke="black" points="380.295,-46.1043 376.795,-36.1043 373.295,-46.1044 380.295,-46.1043"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000196</g>
197<!-- Generated -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000198<g id="node8" class="node">
199<title>Generated</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000200<ellipse fill="none" stroke="black" cx="489.795" cy="-18" rx="54.6905" ry="18"/>
201<text text-anchor="middle" x="489.795" y="-14.3" font-family="arial" font-size="14.00">Generated</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000202</g>
203<!-- File&#45;&gt;Generated -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000204<g id="edge7" class="edge">
205<title>File&#45;&gt;Generated</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000206<path fill="none" stroke="black" d="M396.997,-76.4854C413.512,-66.2547 437.203,-51.579 456.513,-39.6169"/>
207<polygon fill="black" stroke="black" points="458.378,-42.579 465.036,-34.3375 454.691,-36.6283 458.378,-42.579"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000208</g>
209</g>
210</svg>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100211<p><i>Hierarchy of targets.</i></p>
212</div>
213
214<p>
215 Files are further divided into two kinds.
216 <i>Source files</i> are usually written by the efforts of people,
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000217 and checked in to the repository.
218 <i>Generated files</i>, sometimes called derived files,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100219 are not checked in, but are generated by the build tool from source
220 files according to specific rules.
221</p>
222
223<p>
Tobias Werthd57a6532016-06-27 14:12:23 +0000224 The second kind of target is the <i>rule</i>. A rule specifies the
225 relationship between a set of input and a set of output files,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100226 including the necessary steps to derive the outputs from the inputs.
227 The outputs of a rule are always generated files. The inputs to a
228 rule may be source files, but they may be generated files also;
229 consequently, outputs of one rule may be the inputs to another,
230 allowing long chains of rules to be constructed.
231</p>
232
233<p>
234 Whether the input to a rule is a source file or a generated file is
235 in most cases immaterial; what matters is only the contents of that
236 file. This fact makes it easy to replace a complex source file with
237 a generated file produced by a rule, such as happens when the burden
238 of manually maintaining a highly structured file becomes too
239 tiresome, and someone writes a program to derive it. No change is
240 required to the consumers of that file. Conversely, a generated
241 file may easily be replaced by a source file with only local
242 changes.
243</p>
244
245<p>
246 The inputs to a rule may also include <i>other rules</i>. The
247 precise meaning of such relationships is often quite complex and
248 language- or rule-dependent, but intuitively it is simple: a C++
249 library rule A might have another C++ library rule B for an input.
250 The effect of this dependency is that the B's header files are
251 available to A during compilation, B's symbols are available to A
252 during linking, and B's runtime data is available to A during
253 execution.
254</p>
255
256<p>
257 An invariant of all rules is that the files generated by a rule
258 always belong to the same package as the rule itself; it is not
259 possible to generate files into another package. It is not uncommon
260 for a rule's inputs to come from another package, though.
261</p>
262
263<p>
264 Package groups are sets of packages whose purpose is to limit accessibility
Tobias Werthd57a6532016-06-27 14:12:23 +0000265 of certain rules. Package groups are defined by the
266 <code>package_group</code> function. They have two properties: the list of
267 packages they contain and their name. The only allowed ways to refer to them
268 are from the <code>visibility</code> attribute of rules or from the
269 <code>default_visibility</code> attribute of the <code>package</code>
270 function; they do not generate or consume files. For more information, refer
271 to the appropriate section of the <a
272 href='be/functions.html#package_group'>Build Encyclopedia</a>.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100273</p>
274
275
276<h3 id="labels">Labels</h3>
277
278<p>
279 All targets belong to exactly one package. The name of a target is
280 called its <em>label</em>, and a typical label in canonical form
281 looks like this:
282</p>
283
284<pre>
Googlere91e17b2015-03-30 12:45:30 +0000285//my/app/main:app_binary
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100286</pre>
287
288<p>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000289
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000290 Each label has two parts, a package name (<code>my/app/main</code>)
291 and a target name (<code>app_binary</code>). Every label uniquely
292 identifies a target. Labels sometimes appear in other forms; when
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100293 the colon is omitted, the target name is assumed to be the same as
294 the last component of the package name, so these two labels are
295 equivalent:
296</p>
297
298<pre>
Googlere91e17b2015-03-30 12:45:30 +0000299//my/app
300//my/app:app
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100301</pre>
302
303<p>
304 Short-form labels such as <code>//my/app</code> are not to
305 be confused with package names. Labels start with <code>//</code>,
306 but package names never do, thus <code>my/app</code> is the
307 package containing <code>//my/app</code>.
308
309 (A common misconception is that <code>//my/app</code> refers
310 to a package, or to <em>all</em> the targets in a package; neither
311 is true.)
312</p>
313
314<p>
315 Within a BUILD file, the package-name part of label may be omitted,
316 and optionally the colon too. So within the BUILD file for package
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000317 <code>my/app</code> (i.e. <code>//my/app:BUILD</code>),
318 the following "relative" labels are all equivalent:
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100319</p>
320
321<pre>
Googlere91e17b2015-03-30 12:45:30 +0000322//my/app:app
323//my/app
324:app
325app
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100326</pre>
327
328<p>
329 (It is a matter of convention that the colon is omitted for files,
330 but retained for rules, but it is not otherwise significant.)
331</p>
332
333<p>
334 Similarly, within a BUILD file, files belonging to the package may
335 be referenced by their unadorned name relative to the package
336 directory:
337</p>
338
339
340<pre>
Googlere91e17b2015-03-30 12:45:30 +0000341generate.cc
342testdata/input.txt
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100343</pre>
344
345<p>
Tobias Werthd57a6532016-06-27 14:12:23 +0000346 But from other packages, or from the command-line, these file
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100347 targets must always be referred to by their complete label, e.g.
348 <code>//my/app:generate.cc</code>.
349</p>
350
351<p>
352 Relative labels cannot be used to refer to targets in other
353 packages; the complete package name must always be specified in this
354 case. For example, if the source tree contains both the package
355 <code>my/app</code> and the package
356 <code>my/app/testdata</code> (i.e., each of these two
357 packages has its own BUILD file). The latter package contains a
358 file named <code>testdepot.zip</code>. Here are two ways (one
359 wrong, one correct) to refer to this file within
360 <code>//my/app:BUILD</code>:
361</p>
362
363<pre>
Googler8232d9b2015-03-30 14:39:58 +0000364<span class="discouraged">testdata/testdepot.zip</span> # Wrong: testdata is a different package.
Googlere91e17b2015-03-30 12:45:30 +0000365//my/app/testdata:testdepot.zip # Right.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100366</pre>
367
368<p>
369 If, by mistake, you refer to <code>testdepot.zip</code> by the wrong
370 label, such as <code>//my/app:testdata/testdepot.zip</code>
371 or <code>//my:app/testdata/testdepot.zip</code>, you will get an
372 error from the build tool saying that the label "crosses a package
373 boundary". You should correct the label by putting the colon after
374 the directory containing the innermost enclosing BUILD file, i.e.,
375 <code>//my/app/testdata:testdepot.zip</code>.
376</p>
377
Laszlo Csomor529f3fd32015-03-24 13:22:21 +0000378<h3 id="lexi">Lexical specification of a label</h3>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100379
380<p>
381 The syntax of labels is intentionally strict, so as to
382 forbid metacharacters that have special meaning to the shell. This
383 helps to avoid inadvertent quoting problems, and makes it easier to
Laszlo Csomorea3c2542015-03-23 14:24:17 +0000384 construct tools and scripts that manipulate labels, such as the
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100385
Laszlo Csomorea3c2542015-03-23 14:24:17 +0000386 <a href="query.html">Bazel Query Language</a>.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100387 All of the following are forbidden in labels: any sort of white
388 space, braces, brackets, or parentheses; wildcards such
389 as <code>*</code>; shell metacharacters such
390 as <code>&gt;</code>, <code>&amp;</code> and <code>|</code>; etc.
391 This list is not comprehensive; the precise details are below.
392</p>
393
394<h4 id="name">Target names, <code>//...:<b>target-name</b></code></h4>
395
396<p><code>target-name</code> is the name of the target within the package.
397 The name of a rule is the value of the <code>name</code>
398 parameter in the rule's declaration in a BUILD file; the name
399 of a file is its pathname relative to the directory containing
400 the BUILD file.
401 Target names must be composed entirely of
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000402 characters drawn from the set <code>a</code>–<code>z</code>,
403 <code>A</code>–<code>Z</code>, <code>0</code>–<code>9</code>,
404 and the punctuation symbols <code>_/.+-=,@~</code>.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100405 Do not use <code>..</code> to refer to files in other packages; use
406 <code>//<var>packagename</var>:<var>filename</var></code> instead.
407 Filenames must be relative pathnames in normal form, which means
408 they must neither start nor end with a slash
409 (e.g. <code>/foo</code> and <code>foo/</code> are forbidden) nor
410 contain multiple consecutive slashes as path separators
411 (e.g. <code>foo//bar</code>). Similarly, up-level references
412 (<code>..</code>) and current-directory references
413 (<code>./</code>) are forbidden. The sole exception to this
414 rule is that a target name may consist of exactly
415 '<code>.</code>'.
416</p>
417
418<p>While it is common to use <code>/</code> in the name of a file
419 target, we recommend that you avoid the use of <code>/</code> in the
420 names of rules. Especially when the shorthand form of a label is
421 used, it may confuse the reader. The
422 label <code>//foo/bar/wiz</code> is always a shorthand
423 for <code>//foo/bar/wiz:wiz</code>, even if there is no such package
424 <code>foo/bar/wiz</code>; it never refers to <code>//foo:bar/wiz</code>,
425 even if that target exists.</p>
426
427<p>However, there are some situations where use of a slash is
428 convenient, or sometimes even necessary. For example, the name of
429 certain rules must match their principal source file, which may
430 reside in a subdirectory of the package.</p>
431
432<h4>Package names, <code>//<b>package-name</b>:...</code></h4>
433<p>
434 The name of a package is the name of the directory containing its
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000435
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100436 BUILD file, relative to the top-level directory of the source tree.
437 For example: <code>my/app</code>.
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000438 Package names must start with a lower-case ASCII letter
439 (<code>a</code>–<code>z</code>),
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100440 and must be composed entirely of characters drawn from the set
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000441 <code>a</code>–<code>z</code>, <code>0</code>–<code>9</code>,
442 '<code>_</code>', and '<code>/</code>'.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100443</p>
444
Kristina Chodorow5284f322015-03-06 19:17:00 +0000445<p>
Laszlo Csomord9b2b862015-03-25 12:01:43 +0000446 For a language with a directory structure that is significant
447 to its module system (e.g. Java), it is important to choose directory names
448 that are valid identifiers in the language.
Kristina Chodorow5284f322015-03-06 19:17:00 +0000449</p>
450
Laszlo Csomord9b2b862015-03-25 12:01:43 +0000451<p>
452 Although Bazel allows a package at the build root (e.g. <code>//:foo</code>), this
453 is not advised and projects should attempt to use more descriptively named
454 packages.
455</p>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100456<p>
457 Package names may not contain the substring <code>//</code>, nor
458 end with a slash.
459</p>
460
Laszlo Csomor529f3fd32015-03-24 13:22:21 +0000461<h3 id="rules">Rules</h3>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100462
463<p>
Tobias Werthd57a6532016-06-27 14:12:23 +0000464 A rule specifies the relationship between inputs and outputs, and the
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100465 steps to build the outputs. Rules can be of one of many different
466 kinds or <i>classes</i>, which produce compiled
467 executables and libraries, test executables and other supported
468 outputs as described in the
David Chenc23d6612015-11-02 22:56:13 +0000469 <a href="be/overview.html">Build Encyclopedia</a>.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100470</p>
471
472<p>
473 Every rule has a name, specified by the <code>name</code> attribute,
474 of type string. The name must be a syntactically valid target name,
475 as specified <a href='#name'>above</a>. In some cases, the name is
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000476 somewhat arbitrary, and more interesting are the names of the files
477 generated by the rule; this is true of genrules. In other
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100478 cases, the name is significant: for <code>*_binary</code>
479 and <code>*_test</code> rules, for example, the rule name determines
480 the name of the executable produced by the build.
481</p>
482
483<p>
484 Every rule has a set of <i>attributes</i>; the applicable attributes
485 for a given rule, and the significance and semantics of each
486 attribute are a function of the rule's class; see
David Chenc23d6612015-11-02 22:56:13 +0000487 the <a href='be/overview.html'>Build
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100488 Encyclopedia</a> for the full list of supported rules and their
489 corresponding attributes. Each attribute has a name and a
490 type. The full set of types that an attribute can have is: integer,
491 label, list of labels, string, list of strings, output label,
492 list of output labels. Not all attributes need to be specified in
493 every rule. Attributes thus form a dictionary from keys (names) to
494 optional, typed values.
495</p>
496
497<p>
498 The <code>srcs</code> attribute present in many rules has type "list
499 of label"; its value, if present, is a list of labels, each being
500 the name of a target that is an input to this rule.
501</p>
502
503<p>
504 The <code>outs</code> attribute present in many rules has type "list
505 of output labels"; this is similar to the type of
506 the <code>srcs</code> attribute, but differs in two significant
507 ways. Firstly, due to the invariant that the outputs of a rule
508 belong to the same package as the rule itself, output labels cannot
509 include a package component; they must be in one of the "relative"
510 forms shown above. Secondly, the relationship implied by an
511 (ordinary) label attribute is inverse to that implied by an output
Tobias Werthd57a6532016-06-27 14:12:23 +0000512 label: a rule <i>depends on</i> its <code>srcs</code>, whereas a rule <i>is
513 depended on by</i> its <code>outs</code>. The two types of label attributes
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100514 thus assign direction to the edges between targets, giving rise to a
515 dependency graph.
516</p>
517
518<p>
519 The figure below represents an example fragment of the build
520 dependency graph, and illustrates: files (circles) and rules
521 (boxes); dependencies from generated files to rules; dependencies
522 from rules to files, and from rules to other rules. Conventionally,
523 dependency arrows are represented as pointing from a target towards
524 its prerequisites.
525</p>
526
527<div style="margin:auto; text-align:center">
Laszlo Csomor9b895922015-03-04 10:49:54 +0000528<svg width="157pt" height="246pt"
529 viewBox="0.00 0.00 156.50 246.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
530<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 242)">
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000531<title>G1</title>
Chris Parsons591d1e12016-06-14 13:34:30 +0000532<polygon fill="white" stroke="transparent" points="-4,4 -4,-242 152.5,-242 152.5,4 -4,4"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000533<!-- r1 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000534<g id="node1" class="node">
535<title>r1</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000536<polygon fill="none" stroke="black" points="88.5,-173 34.5,-173 34.5,-137 88.5,-137 88.5,-173"/>
537<text text-anchor="middle" x="61.5" y="-152.5" font-family="arial" font-size="10.00" fill="#006000">rule</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000538</g>
539<!-- s1 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000540<g id="node3" class="node">
541<title>s1</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000542<ellipse fill="none" stroke="black" cx="14.5" cy="-223.5" rx="14.5" ry="14.5"/>
543<text text-anchor="middle" x="14.5" y="-221" font-family="arial" font-size="10.00" fill="#006000">in</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000544</g>
545<!-- r1&#45;&gt;s1 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000546<g id="edge1" class="edge">
547<title>r1&#45;&gt;s1</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000548<path fill="none" stroke="black" d="M49.3963,-173.125C42.9655,-182.224 35.0324,-193.449 28.3559,-202.895"/>
549<polygon fill="black" stroke="black" points="25.3432,-201.094 22.4297,-211.28 31.0596,-205.134 25.3432,-201.094"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000550</g>
551<!-- s2 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000552<g id="node4" class="node">
553<title>s2</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000554<ellipse fill="none" stroke="black" cx="61.5" cy="-223.5" rx="14.5" ry="14.5"/>
555<text text-anchor="middle" x="61.5" y="-221" font-family="arial" font-size="10.00" fill="#006000">in</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000556</g>
557<!-- r1&#45;&gt;s2 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000558<g id="edge2" class="edge">
559<title>r1&#45;&gt;s2</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000560<path fill="none" stroke="black" d="M61.5,-173.125C61.5,-180.918 61.5,-190.27 61.5,-198.729"/>
561<polygon fill="black" stroke="black" points="58.0001,-198.782 61.5,-208.782 65.0001,-198.782 58.0001,-198.782"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000562</g>
563<!-- s3 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000564<g id="node5" class="node">
565<title>s3</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000566<ellipse fill="none" stroke="black" cx="108.5" cy="-223.5" rx="14.5" ry="14.5"/>
567<text text-anchor="middle" x="108.5" y="-221" font-family="arial" font-size="10.00" fill="#006000">in</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000568</g>
569<!-- r1&#45;&gt;s3 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000570<g id="edge3" class="edge">
571<title>r1&#45;&gt;s3</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000572<path fill="none" stroke="black" d="M73.6037,-173.125C80.0345,-182.224 87.9676,-193.449 94.6441,-202.895"/>
573<polygon fill="black" stroke="black" points="91.9404,-205.134 100.57,-211.28 97.6568,-201.094 91.9404,-205.134"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000574</g>
575<!-- r2 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000576<g id="node2" class="node">
577<title>r2</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000578<polygon fill="none" stroke="black" points="148.5,-101 94.5,-101 94.5,-65 148.5,-65 148.5,-101"/>
579<text text-anchor="middle" x="121.5" y="-80.5" font-family="arial" font-size="10.00" fill="#006000">rule</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000580</g>
581<!-- r2&#45;&gt;r1 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000582<g id="edge5" class="edge">
583<title>r2&#45;&gt;r1</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000584<path fill="none" stroke="black" d="M106.669,-101.303C99.4753,-109.695 90.7033,-119.93 82.8097,-129.139"/>
585<polygon fill="black" stroke="black" points="80.0114,-127.025 76.1609,-136.896 85.3262,-131.581 80.0114,-127.025"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000586</g>
587<!-- s4 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000588<g id="node6" class="node">
589<title>s4</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000590<ellipse fill="none" stroke="black" cx="121.5" cy="-155" rx="14.5" ry="14.5"/>
591<text text-anchor="middle" x="121.5" y="-152.5" font-family="arial" font-size="10.00" fill="#006000">in</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000592</g>
593<!-- r2&#45;&gt;s4 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000594<g id="edge4" class="edge">
595<title>r2&#45;&gt;s4</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000596<path fill="none" stroke="black" d="M121.5,-101.303C121.5,-110.01 121.5,-120.7 121.5,-130.171"/>
597<polygon fill="black" stroke="black" points="118,-130.175 121.5,-140.175 125,-130.176 118,-130.175"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000598</g>
599<!-- o1 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000600<g id="node7" class="node">
601<title>o1</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000602<ellipse fill="none" stroke="black" cx="61.5" cy="-83" rx="14.5" ry="14.5"/>
603<text text-anchor="middle" x="61.5" y="-80.5" font-family="arial" font-size="10.00" fill="#006000">out</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000604</g>
605<!-- o1&#45;&gt;r1 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000606<g id="edge6" class="edge">
607<title>o1&#45;&gt;r1</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000608<path fill="none" stroke="black" d="M61.5,-97.8297C61.5,-106.081 61.5,-116.847 61.5,-126.744"/>
609<polygon fill="black" stroke="black" points="58.0001,-126.981 61.5,-136.981 65.0001,-126.981 58.0001,-126.981"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000610</g>
611<!-- o2 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000612<g id="node8" class="node">
613<title>o2</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000614<ellipse fill="none" stroke="black" cx="121.5" cy="-14.5" rx="14.5" ry="14.5"/>
615<text text-anchor="middle" x="121.5" y="-12" font-family="arial" font-size="10.00" fill="#006000">out</text>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000616</g>
617<!-- o2&#45;&gt;r2 -->
Chris Parsons591d1e12016-06-14 13:34:30 +0000618<g id="edge7" class="edge">
619<title>o2&#45;&gt;r2</title>
Laszlo Csomor9b895922015-03-04 10:49:54 +0000620<path fill="none" stroke="black" d="M121.5,-29.2788C121.5,-36.6355 121.5,-45.9556 121.5,-54.7067"/>
621<polygon fill="black" stroke="black" points="118,-54.9286 121.5,-64.9286 125,-54.9286 118,-54.9286"/>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000622</g>
623</g>
624</svg>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100625<p><i>Source files, rules, and generated files.</i></p>
626</div>
627
628<p>
629 This directed acyclic graph over targets is called the
630 "target graph" or "build dependency graph", and is the domain over
Laszlo Csomorea3c2542015-03-23 14:24:17 +0000631 which the
632
Florian Weikert5e546d52016-06-30 14:08:04 +0000633 <a href="query.html">Bazel Query tool</a></li>
Laszlo Csomorea3c2542015-03-23 14:24:17 +0000634 operates.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100635</p>
636
637
638<h2 id="BUILD_files">BUILD Files</h2>
639
640<p>
641 The previous section described packages, targets and labels, and the
642 build dependency graph abstractly. In this section, we'll look at
643 the concrete syntax used to define a package.
644</p>
645
646<p>
647 By definition, every package contains a BUILD file, which is a short
Laurent Le Brunb4d69c62016-06-21 14:23:44 +0000648 program written in the Build Language. Most BUILD files
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100649 appear to be little more than a series of declarations of build
650 rules; indeed, the declarative style is strongly encouraged when
651 writing BUILD files.
652</p>
653
654<p>
655 However, the build language is in fact an imperative language, and
656 BUILD files are interpreted as a sequential list of statements.
657 Build rule functions, such as <code>cc_library</code>, are procedures whose
658 side-effect is to create an abstract build rule inside the build tool.
659</p>
660
661<p>
662 The concrete syntax of BUILD files is a subset of Python.
663 Originally, the syntax <i>was</i> that of Python, but experience
664 showed that users rarely used more than a tiny subset of Python's
665 features, and when they did, it often resulted in complex and
666 fragile BUILD files. In many cases, the use of such features was
667 unnecessary, and the same result could be achieved by using an
Laurent Le Brunb4d69c62016-06-21 14:23:44 +0000668 external program, e.g. via a <code>genrule</code> build rule.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100669</p>
670
671<p>
672 Crucially, programs in the build language are unable to perform
673 arbitrary I/O (though many users try!). This invariant makes the
674 interpretation of BUILD files hermetic, i.e. dependent only on a
675 known set of inputs, which is essential for ensuring that builds are
676 reproducible.
677</p>
678
679<h3 id="core_build_language">The Core Build Language</h3>
680
681<p>
682 <b>Lexemes</b>: the lexical syntax of the core language is a strict
683 subset of Python 2.6, and we refer the reader to the <a
684 href='http://docs.python.org/reference/lexical_analysis.html'>Python
685 specification</a> for details.
686 Lexical features of Python that are not
687 supported include: floating-point literals, hexadecimal and Unicode
688 escapes within string literals.
689</p>
690
691<p>
692 BUILD files should be written using only ASCII characters,
693 although technically they are interpreted using the Latin-1
694 character set. The use
695 of <a href='http://www.python.org/dev/peps/pep-0263/'><code>coding:</code></a>
696 declarations is forbidden.
697</p>
698
699<p>
700 <b>Grammar</b>: the grammar of the core language is shown below,
701 using EBNF notation. Ambiguity is resolved using precedence, which
702 is defined as for Python.
703</p>
704
Googler8232d9b2015-03-30 14:39:58 +0000705<pre>
Googlere91e17b2015-03-30 12:45:30 +0000706file_input ::= (simple_stmt? '\n')*
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100707
Googlere91e17b2015-03-30 12:45:30 +0000708simple_stmt ::= small_stmt (';' small_stmt)* ';'?
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100709
Googlere91e17b2015-03-30 12:45:30 +0000710small_stmt ::= expr
711 | assign_stmt
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100712
Googlere91e17b2015-03-30 12:45:30 +0000713assign_stmt ::= IDENTIFIER '=' expr
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100714
Googlere91e17b2015-03-30 12:45:30 +0000715expr ::= INTEGER
716 | STRING+
717 | IDENTIFIER
718 | IDENTIFIER '(' arg_list? ')'
719 | expr '.' IDENTIFIER
720 | expr '.' IDENTIFIER '(' arg_list? ')'
721 | '[' expr_list? ']'
722 | '[' expr ('for' IDENTIFIER 'in' expr)+ ']'
723 | '(' expr_list? ')'
724 | '{' dict_entry_list? '}'
Laurent Le Brunb4d69c62016-06-21 14:23:44 +0000725 | '{' dict_entry ('for' IDENTIFIER 'in' expr)+ '}'
Googlere91e17b2015-03-30 12:45:30 +0000726 | expr '+' expr
727 | expr '-' expr
728 | expr '%' expr
729 | '-' expr
730 | expr '[' expr? ':' expr? ']'
731 | expr '[' expr ']'
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100732
Googlere91e17b2015-03-30 12:45:30 +0000733expr_list ::= (expr ',')* expr ','?
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100734
Googlere91e17b2015-03-30 12:45:30 +0000735dict_entry_list ::= (dict_entry ',')* dict_entry ','?
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100736
Googlere91e17b2015-03-30 12:45:30 +0000737dict_entry ::= expr ':' expr
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100738
Googlere91e17b2015-03-30 12:45:30 +0000739arg_list ::= (arg ',')* arg ','?
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100740
Googlere91e17b2015-03-30 12:45:30 +0000741arg ::= IDENTIFIER '=' expr
742 | expr
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100743</pre>
744
745<p>
746 For each expression of the core language, the semantics are
747 identical to the corresponding Python semantics, except in the
748 following cases:
749</p>
750<ul>
751 <li>certain overloads of the binary <code>%</code> operator are not
752 supported. Only the <code>int % int</code> and <code>str %
753 tuple</code> forms are supported. Only the <code>%s</code>
754 and <code>%d</code> format specifiers may be
755 used; <code>%(var)s</code> is illegal.</li>
756
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100757</ul>
758
759<p>
Laurent Le Brunf4d51172015-09-09 13:19:24 +0000760 Many Python features are missing: control-flow constructs (loops,
761 conditionals, exceptions), basic datatypes (floating-point numbers, big
762 integers), <code>import</code> and the module system, support for
763 definition of classes, some Python's built-in functions. Function
764 definitions and <code>for</code> statements are allowed only in
765 extension files (<code>.bzl</code>).
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100766
Laurent Le Brunf4d51172015-09-09 13:19:24 +0000767 Available functions are documented in
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100768
Laurent Le Brunf4d51172015-09-09 13:19:24 +0000769 the <a href="skylark/lib/globals.html">library section</a>.
Laszlo Csomor529f3fd32015-03-24 13:22:21 +0000770<h3 id="declaring_build_rules">Declaring build rules</h3>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100771
772<p>
773 The build language is an imperative language, so in general, order
774 does matter: variables must be defined before they are used, for
775 example. However, most BUILD files consist only of declarations of
776 build rules, and the relative order of these statements is
777 immaterial; all that matters is <em>which</em> rules were declared,
778 and with what values, by the time package evaluation completes.
779
780 So, in simple BUILD files, rule declarations can be re-ordered
781 freely without changing the behavior.
782</p>
783
784<p>
Laurent Le Brunb4d69c62016-06-21 14:23:44 +0000785 BUILD file authors are encouraged to use comments liberally to
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100786 document the role of each build target, whether it is intended for
787 public use, and anything else that would help users and future
788 maintainers, including a <code># Description:</code> comment at the
789 top, explaining the role of the package.
790</p>
791
792<p>
793 The Python comment syntax of <code>#...</code> is supported.
794 Triple-quoted string literals may span multiple lines, and can be used
795 for multi-line comments.
796</p>
797
798<h2 id="funcs">Types of build rule</h2>
799
800<p>
801 The majority of build rules come in families, grouped together by
802 language. For
803 example, <code>cc_binary</code>, <code>cc_library</code>
804 and <code>cc_test</code> are the build rules for C++ binaries,
805 libraries, and tests, respectively. Other languages use the same
806 naming scheme, with a different prefix, e.g. <code>java_*</code> for
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000807 Java. These functions are all documented in the
David Chenc23d6612015-11-02 22:56:13 +0000808 <a href="be/overview.html">Build Encyclopedia</a>.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100809</p>
810
811<ul>
Tobias Werthd57a6532016-06-27 14:12:23 +0000812 <li><p><code>*_binary</code>
813 rules build executable programs in a given language. After a
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100814 build, the executable will reside in the build tool's binary
815 output tree at the corresponding name for the rule's label,
816 so <code>//my:program</code> would appear at
817 (e.g.) <code>$(BINDIR)/my/program</code>. </p>
818
819 <p>Such rules also create a runfiles directory
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000820
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100821 containing all the files mentioned in a <code>data</code>
822 attribute belonging to the rule, or any rule in its transitive
823 closure of dependencies; this set of files is gathered together in
824 one place for ease of deployment to production.</p>
825 </li>
826
Tobias Werthd57a6532016-06-27 14:12:23 +0000827 <li><p><code>*_test</code>
828 rules are a specialization of a <code>*_binary</code> rule, used for automated
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000829 testing. Tests are simply programs that return zero on success.
830
831 </p>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100832
833 <p>
834 Like binaries, tests also have runfiles trees, and the files
835 beneath it are the only files that a test may legitimately open
836 at runtime. For example, a program <code>cc_test(name='x',
837 data=['//foo:bar])</code> may open and
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000838
839 read <code>$TEST_SRCDIR/workspace/foo/bar</code> during execution.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100840 (Each programming language has its own utility function for
841 accessing the value of <code>$TEST_SRCDIR</code>, but they are all
842 equivalent to using the environment variable directly.)
843 Failure to observe the rule will cause the test to fail when it is
844 executed on a remote testing host.
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000845
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100846 </p>
847 </li>
848
Tobias Werthd57a6532016-06-27 14:12:23 +0000849 <li><code>*_library</code>
850 rules specify separately-compiled modules in the given
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100851 programming language. Libraries can depend on other libraries,
852 and binaries and tests can depend on libraries, with the expected
853 separate-compilation behavior.
854 </li>
855</ul>
856
857<h2 id="dependencies">Dependencies</h2>
858
859<p>
860 A target <code>A</code> <i>depends upon</i> a target
861 <code>B</code> if <code>B</code> is needed by <code>A</code> at
862 build or execution time. The <i>depends upon</i> relation induces a
863 directed acyclic graph (DAG) over targets, and we call this a
864 <em>dependency graph</em>.
865
866 A target's <em>direct</em> dependencies are those other targets
867 reachable by a path of length 1 in the dependency graph. A target's
868 <em>transitive</em> dependencies are those targets upon which it
869 depends via a path of any length through the graph.
870</p>
871
872<p>
873 In fact, in the context of builds, there are two dependency graphs,
874 the graph of <em>actual dependencies</em> and the graph of
875 <em>declared dependencies</em>. Most of the time, the two graphs
876 are so similar that this distinction need not be made, but it is
877 useful for the discussion below.
878</p>
879
880<h3 id="actual_and_declared_dependencies">Actual and declared dependencies</h3>
881
882<p>
883 A target <code>X</code> is <i>actually dependent</i> on target
884 <code>Y</code> iff <code>Y</code> must be present, built and
885 up-to-date in order for <code>X</code> to be built correctly.
886 "Built" could mean generated, processed, compiled, linked,
887 archived, compressed, executed, or any of the other kinds of tasks
888 that routinely occur during a build.
889</p>
890
891<p>
892 A target <code>X</code> has a <i>declared dependency</i> on target
893 <code>Y</code> iff there is a dependency edge from <code>X</code> to
894 <code>Y</code> in the package of <code>X</code>.
895</p>
896
897<p>
898 For correct builds, the graph of actual dependencies <i>A</i> must
899 be a subgraph of the graph of declared dependencies <i>D</i>. That
900 is, every pair of directly-connected nodes <code>x --&gt; y</code>
901 in <i>A</i> must also be directly connected in <i>D</i>. We say
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000902 <i>D</i> is an <em>overapproximation</em> of <i>A</i>.
903</p>
904
905<p>
906 It is important that it not be too much of an overapproximation,
907 though, since redundant declared dependencies can make builds slower and
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100908 binaries larger.
909</p>
910
911<p>
Laurent Le Brunb4d69c62016-06-21 14:23:44 +0000912 What this means for BUILD file writers is that every rule must
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100913 explicitly declare all of its actual direct dependencies to the
914 build system, and no more.
915
916 Failure to observe this principle causes undefined behavior: the
917 build may fail, but worse, the build may depend on some prior
918 operations, or upon which transitive declared dependencies the target
919 happens to have. The build tool attempts aggressively to check for
920 missing dependencies and report errors, but it is not possible for
921 this checking to be complete in all cases.
922</p>
923
924<p>
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000925
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100926 You need not (and should not) attempt to list everything indirectly imported,
927 even if it is "needed" by A at execution time.
928</p>
929
930<p>
931 During a build of target <code>X</code>, the build tool inspects the
932 entire transitive closure of dependencies of <code>X</code> to ensure that
933 any changes in those targets are reflected in the final result,
934 rebuilding intermediates as needed.
935</p>
936
937<p>
938 The transitive nature of dependencies leads to a common mistake.
939 Through careless programming, code in one file may use code provided
940 by an <em>indirect</em> dependency, i.e. a transitive but not direct
941 edge in the declared dependency graph. Indirect dependencies do not
Laurent Le Brunb4d69c62016-06-21 14:23:44 +0000942 appear in the BUILD file. Since the rule doesn't
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100943 directly depend on the provider, there is no way to track changes,
944 as shown in the following example timeline:
945</p>
946
947<div class="greenbox">
948<p><b>1. At first, everything works</b></p>
949
950<p>The code in package <code>a</code> uses code in package <code>b</code>.
951The code in package <code>b</code> uses code in package <code>c</code>,
952and thus <code>a</code> transitively depends on <code>c</code>.</p>
953
954<div style="float:left; width: 49%; margin-top: -20px;">
955<p><code>a/BUILD</code></p>
956<pre class="code">
957<b>rule(
958 name = "a",
959 srcs = "a.in",
960 deps = "//b:b",
961)</b>
962</pre>
963<p><code>a/a.in</code></p>
964<pre class="code">
965<b>import b;
966b.foo();</b>
967</pre>
968</div>
969<div style="float:right; width: 49%; margin-top: -20px; ">
970<p><code>b/BUILD</code></p>
971<pre class="code">
972<b>rule(
973 name = "b",
974 srcs = "b.in",
975 deps = "//c:c",
976)</b>
977</pre>
978<p><code>b/b.in</code></p>
979<pre class="code">
980<b>import c;
981function foo() {
982 c.bar();
983}</b>
984</pre>
985</div>
986<pre style="clear: both;">
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000987Declared dependency graph: a --&gt; b --&gt; c
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100988
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000989Actual dependency graph: a --&gt; b --&gt; c
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100990</pre>
991The declared dependencies overapproximate the actual dependencies.
992All is well.
993</div>
994
995<div class="greenbox">
996<p><b>2. A latent hazard is introduced.</b></p>
997<p>
998 Someone carelessly adds code to <code>a</code> that creates a direct
999 actual dependency on <code>c</code>, but forgets to declare it.
1000</p>
1001<div style="float:left; width: 49%; margin-top: -20px; ">
1002<p><code>a/a.in</code></p>
1003<pre class="code">
1004import b;
1005<b>import c;</b>
1006b.foo();
1007<b>c.garply();</b>
1008</pre>
1009</div>
1010
1011<pre style="clear: both;">
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00001012Declared dependency graph: a --&gt; b --&gt; c
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001013
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00001014Actual dependency graph: a --&gt; b --&gt;_c
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001015 \_________/|
1016</pre>
1017The declared dependencies no longer overapproximate the actual
1018dependencies. This may build ok, because the transitive closures of
1019the two graphs are equal, but masks a problem: <code>a</code> has an
1020actual but undeclared dependency on <code>c</code>.
1021</div>
1022
1023<div class="greenbox">
1024<p><b>3. The hazard is revealed</b> </p>
1025<p>
1026 Someone refactors <code>b</code> so that it no longer depends on
1027 <code>c</code>, inadvertently breaking <code>a</code> through no
1028 fault of their own.
1029</p>
1030<div style="float:right; width: 49%; margin-top: -20px; ">
1031<p><code>b/BUILD</code></p>
1032<pre class="code">
1033rule(
1034 name = "b",
1035 srcs = "b.in",
1036 <b>deps = "//d:d"</b>,
1037)
1038</pre>
1039<p><code>b/b.in</code></p>
1040<pre class="code">
1041<b>import d;</b>
1042function foo() {
1043 <b>d.baz();</b>
1044}
1045</pre>
1046</div>
1047<pre style="clear: both;">
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00001048Declared dependency graph: a --&gt; b c
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001049
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00001050Actual dependency graph: a --&gt; b _c
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001051 \_________/|
1052</pre>
1053<p>
1054 The declared dependency graph is now an underapproximation of the
1055 actual dependencies, even when transitively closed; the build is
1056 likely to fail.
1057
1058 The problem could have been averted by ensuring that the actual
1059 dependency from <code>a</code> to <code>c</code> introduced in Step
1060 2 was properly declared in the BUILD file.
1061</div>
1062
Laszlo Csomor529f3fd32015-03-24 13:22:21 +00001063<h3 id="types_of_dependencies">Types of dependencies</h3>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001064
1065<p>
1066 Most build rules have three attributes for specifying different kinds
1067 of generic dependencies: <code>srcs</code>, <code>deps</code> and
1068 <code>data</code>. These are explained below. See also
David Chenc23d6612015-11-02 22:56:13 +00001069 <a href='be/common-definitions.html'>Attributes common
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001070 to all rules</a> in the Build Encyclopedia.)
1071</p>
1072
1073<p>
1074 Many rules also have additional attributes for rule-specific kinds
1075 of dependency, e.g. <code>compiler</code>, <code>resources</code>,
1076 etc. These are detailed in the Build Encyclopedia.
1077</p>
1078
1079<h4 id="srcs"><code>srcs</code> dependencies</h4>
1080<p>
1081 Files consumed directly by the rule or rules that output source files.
1082</p>
1083
1084<h4 id="deps"><code>deps</code> dependencies</h4>
1085<p>
1086 Rule pointing to separately-compiled modules providing header files,
1087 symbols, libraries, data, etc.
1088</p>
1089
1090<h4 id="data"><code>data</code> dependencies</h4>
1091<p>A build target might need some data files to run correctly. These
1092 data files aren't source code: they don't affect how the target is
1093 built. For example, a unit test might compare a function's output
1094 to the contents of a file. When we build the unit test, we
1095 don't need the file; but we do need it when we run the test. The
1096 same applies to tools that are launched during execution.
1097
1098<p>The build system runs tests in an isolated directory where only files
1099 listed as "data" are available. Thus, if a binary/library/test
Laurent Le Brunb4d69c62016-06-21 14:23:44 +00001100 needs some files to run, specify them (or a build rule containing
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001101 them) in data. For example:
1102</p>
1103
1104<pre>
Tobias Werthd57a6532016-06-27 14:12:23 +00001105# I need a config file from a directory named env:
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001106java_binary(
1107 name = "setenv",
1108 ...
Tobias Werthd57a6532016-06-27 14:12:23 +00001109 data = [":env/default_env.txt"],
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001110)
1111
1112# I need test data from another directory
1113sh_test(
1114 name = "regtest",
1115 srcs = ["regtest.sh"],
1116 data = [
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00001117 "//data:file1.txt",
1118 "//data:file2.txt",
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001119 ...
1120 ],
1121)
1122</pre>
1123
1124<p>These files are available using the relative path
Tobias Werthd57a6532016-06-27 14:12:23 +00001125<code>path/to/data/file</code>. In tests, it is also possible to refer to
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00001126them by joining the paths of the test's source directory and the workspace-relative
Googler33197802016-06-08 15:47:31 +00001127path, e.g.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001128
Googler33197802016-06-08 15:47:31 +00001129<code>${TEST_SRCDIR}/workspace/path/to/data/file</code>.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001130 <h3 id="label_directory">Using Labels to Reference Directories</h3>
1131
1132 <p>As you look over our <code>BUILD</code> files, you might notice
1133 that some <code>data</code> labels refer to directories.
Tobias Werthd57a6532016-06-27 14:12:23 +00001134 These labels end with <code>/.</code> or <code>/</code> like so:
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001135
1136<pre>
1137<span style="text-decoration: line-through">data = ["//data/regression:unittest/."]</span> # don't use this
1138</pre>
1139<p>
1140or like so:
1141</p>
1142<pre>
1143<span style="text-decoration: line-through">data = ["testdata/."]</span> # don't use this
1144</pre>
1145
1146<p>
1147or like so:
1148</p>
1149
1150<pre>
1151<span style="text-decoration: line-through">data = ["testdata/"]</span> # don't use this
1152</pre>
1153 <p>This seems convenient, particularly for tests (since it allows a test to
1154 use all the data files in the directory).
1155 </p>
1156
1157 <p>But try not to do this. In order to ensure correct incremental rebuilds (and
1158 re-execution of tests) after a change, the build system must be
1159 aware of the complete set of files that are inputs to the build (or
1160 test). When you specify a directory, the build system will perform
1161 a rebuild only when the directory itself changes (due to addition or
1162 deletion of files), but won't be able to detect edits to individual
1163 files as those changes do not affect the enclosing directory.
1164 Rather than specifying directories as inputs to the build system,
1165 you should enumerate the set of files contained within them, either
1166 explicitly or using the
David Chenc23d6612015-11-02 22:56:13 +00001167 <a href='be/functions.html#glob'><code>glob()</code></a> function.
1168 (Use <code>**</code> to force the <a href='be/functions.html#glob'>
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001169 <code>glob()</code></a> to be recursive.)
1170 </p>
1171
1172<pre>
1173data = glob(["testdata/**"]) # use this instead
1174</pre>
1175
1176 <p>Unfortunately, there are some scenarios where directory labels must be used.
1177 For example, if the <code>testdata</code> directory contains files whose
1178 names do not conform to the strict <a href='#lexi'>label syntax</a>
1179 (e.g. they contain certain punctuation symbols), then explicit
1180 enumeration of files, or use of the
David Chenc23d6612015-11-02 22:56:13 +00001181 <a href='be/functions.html#glob'><code>glob()</code></a> function will
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001182 produce an invalid labels error. You must use directory labels in this case,
1183 but beware of the concomitant risk of incorrect rebuilds described above.
1184 </p>
1185
1186 <p>If you must use directory labels, keep in mind that you can't refer to the parent
1187 package with a relative "<code>../</code>" path; instead, use an absolute path like
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00001188 "<code>//data/regression:unittest/.</code>".
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001189 </p>
1190
1191 <p>Note that directory labels are only valid for data dependencies. If you try to use
1192 a directory as a label in an argument other than <code>data</code>, it
1193 will fail and you will get a (probably cryptic) error message.
1194 </p>
1195