blob: 557bb891f76c6c6b594b614dd1d3e316f77ddb4e [file] [log] [blame]
laurentlb27453bf2018-10-08 11:23:18 -07001---
2layout: documentation
laurentlbadf36072018-11-05 16:20:20 -08003title: User's Guide
laurentlb27453bf2018-10-08 11:23:18 -07004---
5<h1>A User's Guide to Bazel</h1>
6
laurentlb27453bf2018-10-08 11:23:18 -07007
8<p>
9 To run Bazel, go to
10
laurentlbc68d2642018-10-18 16:01:03 -070011 your base <a href="build-ref.html#workspace">workspace</a> directory
laurentlb27453bf2018-10-08 11:23:18 -070012 or any of its subdirectories and type <code>bazel</code>.
13</p>
14
15<pre>
16 % bazel help
17 [Bazel release bazel-&lt;<i>version</i>&gt;]
18 Usage: bazel &lt;command&gt; &lt;options&gt; ...
19
20 Available commands:
laurentlbc85de9c2018-11-05 08:30:36 -080021 <a href='user-manual.html#analyze-profile'>analyze-profile</a> Analyzes build profile data.
Googler0c88d352019-04-02 02:41:46 -070022 <a href='user-manual.html#aquery'>aquery</a> Executes a query on the <a href='#analysis-phase'>post-analysis</a> action graph.
laurentlb27453bf2018-10-08 11:23:18 -070023 <a href='#build'>build</a> Builds the specified targets.
laurentlbc85de9c2018-11-05 08:30:36 -080024 <a href='user-manual.html#canonicalize'>canonicalize-flags</a> Canonicalize Bazel flags.
25 <a href='user-manual.html#clean'>clean</a> Removes output files and optionally stops the server.
laurentlb27453bf2018-10-08 11:23:18 -070026
laurentlbc85de9c2018-11-05 08:30:36 -080027 <a href='user-manual.html#query'>cquery</a> Executes a <a href='#analysis-phase'>post-analysis</a> dependency graph query.
laurentlb27453bf2018-10-08 11:23:18 -070028
laurentlbc85de9c2018-11-05 08:30:36 -080029 <a href='user-manual.html#dump'>dump</a> Dumps the internal state of the Bazel server process.
laurentlb27453bf2018-10-08 11:23:18 -070030
laurentlbc85de9c2018-11-05 08:30:36 -080031 <a href='user-manual.html#help'>help</a> Prints help for commands, or the index.
32 <a href='user-manual.html#info'>info</a> Displays runtime info about the bazel server.
laurentlb27453bf2018-10-08 11:23:18 -070033
34 <a href='#fetch'>fetch</a> Fetches all external dependencies of a target.
laurentlbc85de9c2018-11-05 08:30:36 -080035 <a href='user-manual.html#mobile-install'>mobile-install</a> Installs apps on mobile devices.
laurentlb27453bf2018-10-08 11:23:18 -070036
laurentlbc85de9c2018-11-05 08:30:36 -080037 <a href='user-manual.html#query'>query</a> Executes a dependency graph query.
laurentlb27453bf2018-10-08 11:23:18 -070038
laurentlbc85de9c2018-11-05 08:30:36 -080039 <a href='user-manual.html#run'>run</a> Runs the specified target.
40 <a href='user-manual.html#shutdown'>shutdown</a> Stops the Bazel server.
41 <a href='user-manual.html#test'>test</a> Builds and runs the specified test targets.
42 <a href='user-manual.html#version'>version</a> Prints version information for Bazel.
laurentlb27453bf2018-10-08 11:23:18 -070043
44 Getting more help:
45 bazel help &lt;command&gt;
46 Prints help and options for &lt;command&gt;.
laurentlbc85de9c2018-11-05 08:30:36 -080047 bazel help <a href='user-manual.html#startup_options'>startup_options</a>
laurentlb27453bf2018-10-08 11:23:18 -070048 Options for the JVM hosting Bazel.
49 bazel help <a href='#target-patterns'>target-syntax</a>
50 Explains the syntax for specifying targets.
51 bazel help info-keys
52 Displays a list of keys used by the info command.
53
54</pre>
55<p>
56 The <code>bazel</code> tool performs many functions, called
laurentlb15918182018-10-17 11:44:25 -070057 commands. The most commonly used ones are <code>bazel build</code>
58 and <code>bazel test</code>. You can browse the online help
laurentlb27453bf2018-10-08 11:23:18 -070059 messages using <code>bazel help</code>.
60</p>
61
laurentlb27453bf2018-10-08 11:23:18 -070062
63<h2 id='build'>Building programs with Bazel</h2>
64<h3>The <code>build</code> command</h3>
65
66<p>
laurentlbc85de9c2018-11-05 08:30:36 -080067 Type <code>bazel build</code> followed by the name of the
laurentlb27453bf2018-10-08 11:23:18 -070068 <a href="#target-patterns">target</a> you wish to build. Here's a typical
69 session:
70</p>
71<pre>
72 % bazel build //foo
73 ____Loading package: foo
74 ____Loading package: bar
75 ____Loading package: baz
76 ____Loading complete. Analyzing...
77 ____Building 1 target...
78 ____[0 / 3] Executing Genrule //bar:helper_rule
79 ____[1 / 3] Executing Genrule //baz:another_helper_rule
80 ____[2 / 3] Building foo/foo.bin
81 Target //foo:foo up-to-date:
82 bazel-bin/foo/foo.bin
83 bazel-bin/foo/foo
84 ____Elapsed time: 9.905s
85</pre>
86<p>
87 Bazel prints the progress messages as it loads all the
88 packages in the transitive closure of dependencies of the requested
89 target, then analyzes them for correctness and to create the build actions,
90 finally executing the compilers and other tools of the build.
91</p>
92<p>
93 Bazel prints progress messages during
94 the <a href='#execution-phase'>execution phase</a> of the build, showing the
95 current build step (compiler, linker, etc.) that is being started,
96 and the number completed over the total number of build actions. As the
97 build starts the number of total actions will often increase as Bazel
98 discovers the entire action graph, but the number will usually stabilize
99 within a few seconds.
100</p>
101<p>
102 At the end of the build Bazel
103 prints which targets were requested, whether or not they were
104 successfully built, and if so, where the output files can be found.
105 Scripts that run builds can reliably parse this output; see <a
106 href='#flag--show_result'><code class='flag'>--show_result</code></a> for more
107 details.
108</p>
109<p>
110 Typing the same command again:
111</p>
112<pre>
113 % bazel build //foo
114 ____Loading...
115 ____Found 1 target...
116 ____Building complete.
117 Target //foo:foo up-to-date:
118 bazel-bin/foo/foo.bin
119 bazel-bin/foo/foo
120 ____Elapsed time: 0.280s
121</pre>
122<p>
123 we see a "null" build: in this case, there are no packages to
124 re-load, since nothing has changed, and no build steps to execute.
125 (If something had changed in "foo" or some of its dependencies, resulting in the
126 reexecution of some build actions, we would call it an "incremental" build, not a
127 "null" build.)
128</p>
129
130<p>
131 Before you can start a build, you will need a Bazel workspace. This is
132 simply a directory tree that contains all the source files needed to build
133 your application.
134 Bazel allows you to perform a build from a completely read-only volume.
135</p>
laurentlb27453bf2018-10-08 11:23:18 -0700136<h3 id='target-patterns'>Specifying targets to build</h3>
137<p>
138 Bazel allows a number of ways to specify the targets to be built.
laurentlbc68d2642018-10-18 16:01:03 -0700139 Collectively, these are known as <i>target patterns</i>. This syntax is
140 used in commands like <code>build</code>, <code>test</code>,
141 or <code>query</code>.
laurentlb27453bf2018-10-08 11:23:18 -0700142</p>
laurentlb27453bf2018-10-08 11:23:18 -0700143
laurentlb27453bf2018-10-08 11:23:18 -0700144<p>
145 Whereas <a href="build-ref.html#labels">labels</a> are used
146 to specify individual targets, e.g. for declaring dependencies in
147 BUILD files, Bazel's target patterns are a syntax for specifying
148 multiple targets: they are a generalization of the label syntax
149 for <i>sets</i> of targets, using wildcards. In the simplest case,
150 any valid label is also a valid target pattern, identifying a set of
151 exactly one target.
152</p>
laurentlbc68d2642018-10-18 16:01:03 -0700153
154<p>
laurentlb8f77d4e2018-11-05 08:37:49 -0800155All target patterns starting with <code>//</code> are resolved relative to the current
laurentlbc68d2642018-10-18 16:01:03 -0700156workspace.
157<table>
158<tr>
159 <td><code>//foo/bar:wiz</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800160 <td>Just the single target <code>//foo/bar:wiz</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700161</tr>
162<tr>
163 <td><code>//foo/bar</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800164 <td>Equivalent to <code>//foo/bar:bar</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700165</tr>
166<tr>
167 <td><code>//foo/bar:all</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800168 <td>All rules in the package <code>foo/bar</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700169</tr>
170<tr>
171 <td><code>//foo/...</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800172 <td>All rules in all packages beneath the directory <code>foo</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700173</tr>
174<tr>
175 <td><code>//foo/...:all</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800176 <td>All rules in all packages beneath the directory <code>foo</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700177</tr>
178<tr>
179 <td><code>//foo/...:*</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800180 <td>All targets (rules and files) in all packages beneath the directory <code>foo</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700181</tr>
182<tr>
183 <td><code>//foo/...:all-targets</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800184 <td>All targets (rules and files) in all packages beneath the directory <code>foo</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700185</tr>
186</table>
187</p>
188
189<p>
laurentlb8f77d4e2018-11-05 08:37:49 -0800190Target patterns which do not begin with <code>//</code> are resolved relative to the
laurentlbc68d2642018-10-18 16:01:03 -0700191current <em>working directory</em>. These examples assume a working directory of
laurentlb8f77d4e2018-11-05 08:37:49 -0800192<code>foo</code>:
laurentlbc68d2642018-10-18 16:01:03 -0700193<table>
194<tr>
195 <td><code>:foo</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800196 <td>Equivalent to <code>//foo:foo</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700197</tr>
198<tr>
199 <td><code>bar:wiz</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800200 <td>Equivalent to <code>//foo/bar:wiz</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700201</tr>
202<tr>
203 <td><code>bar/wiz</code></td>
204 <td>Equivalent to:
laurentlb8f77d4e2018-11-05 08:37:49 -0800205 <code>//foo/bar/wiz:wiz</code> if <code>foo/bar/wiz</code> is a package,
206 <code>//foo/bar:wiz</code> if <code>foo/bar</code> is a package,
207 <code>//foo:bar/wiz</code> otherwise.
laurentlbc68d2642018-10-18 16:01:03 -0700208 </td>
209</tr>
210<tr>
211 <td><code>bar:all</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800212 <td>Equivalent to <code>//foo/bar:all</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700213</tr>
214<tr>
215 <td><code>:all</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800216 <td>Equivalent to <code>//foo:all</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700217</tr>
218<tr>
219 <td><code>...:all</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800220 <td>Equivalent to <code>//foo/...:all</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700221</tr>
222<tr>
223 <td><code>...</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800224 <td>Equivalent to <code>//foo/...:all</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700225</tr>
226<tr>
227 <td><code>bar/...:all</code></td>
laurentlb8f77d4e2018-11-05 08:37:49 -0800228 <td>Equivalent to <code>//foo/bar/...:all</code>.</td>
laurentlbc68d2642018-10-18 16:01:03 -0700229</tr>
230</table>
231</p>
232
233<p>
234By default, directory symlinks are followed for recursive target patterns,
235except those that point to under the output base, such as the convenience
236symlinks that are created in the root directory of the workspace.
237</p>
238
239<p>
240In addition, Bazel does not follow symlinks when evaluating recursive target
241patterns in any directory that contains a file named as follows:
242<pre>
243DONT_FOLLOW_SYMLINKS_WHEN_TRAVERSING_THIS_DIRECTORY_VIA_A_RECURSIVE_TARGET_PATTERN
244</pre>
245</p>
246
laurentlb27453bf2018-10-08 11:23:18 -0700247<p>
248 <code>foo/...</code> is a wildcard over <em>packages</em>,
249 indicating all packages recursively beneath
250 directory <code>foo</code> (for all roots of the package
251 path). <code>:all</code> is a wildcard
252 over <em>targets</em>, matching all rules within a package. These two may be
253 combined, as in <code>foo/...:all</code>, and when both wildcards
254 are used, this may be abbreviated to <code>foo/...</code>.
255</p>
256<p>
257 In addition, <code>:*</code> (or <code>:all-targets</code>) is a
258 wildcard that matches <em>every target</em> in the matched packages,
259 including files that aren't normally built by any rule, such
260 as <code>_deploy.jar</code> files associated
261 with <code>java_binary</code> rules.
262</p>
263<p>
264 This implies that <code>:*</code> denotes a <em>superset</em>
265 of <code>:all</code>; while potentially confusing, this syntax does
266 allow the familiar <code>:all</code> wildcard to be used for
267 typical builds, where building targets like the <code>_deploy.jar</code>
268 is not desired.
269</p>
270<p>
271 In addition, Bazel allows a slash to be used instead of the colon
272 required by the label syntax; this is often convenient when using
273 Bash filename expansion. For example, <code>foo/bar/wiz</code> is
274 equivalent to <code>//foo/bar:wiz</code> (if there is a
275 package <code>foo/bar</code>) or to <code>//foo:bar/wiz</code> (if
276 there is a package <code>foo</code>).
277</p>
278<p>
279 Many Bazel commands accept a list of target patterns as arguments,
laurentlb8f77d4e2018-11-05 08:37:49 -0800280 and they all honor the prefix negation operator <code>-</code>.
laurentlb27453bf2018-10-08 11:23:18 -0700281 This can be used to subtract a set of targets from the set specified
laurentlb8f77d4e2018-11-05 08:37:49 -0800282 by the preceding arguments. Note that this means order matters.
laurentlb27453bf2018-10-08 11:23:18 -0700283 For example,
284</p>
285<pre>
286 bazel build foo/... bar/...
287</pre>
288<p>
289 means "build all
290 targets beneath <code>foo</code> <i>and</i> all targets
291 beneath <code>bar</code>", whereas
292</p>
293<pre>
294 bazel build -- foo/... -foo/bar/...
295</pre>
296<p>
297 means "build all targets beneath <code>foo</code> <i>except</i>
298 those beneath <code>foo/bar</code>".
299
300 (The <code>--</code> argument is required to prevent the subsequent
301 arguments starting with <code>-</code> from being interpreted as
302 additional options.)
303</p>
304<p>
305 It's important to point out though that subtracting targets this way will not
306 guarantee that they are not built, since they may be dependencies of targets
307 that weren't subtracted. For example, if there were a target
308 <code>//foo:all-apis</code> that among others depended on
309 <code>//foo/bar:api</code>, then the latter would be built as part of
310 building the former.
311</p>
312<p>
laurentlb8f77d4e2018-11-05 08:37:49 -0800313 Targets with <code>tags = ["manual"]</code> will not be included in wildcard target patterns (
314 <code>...</code>, <code>:*</code>, <code>:all</code>, etc.).
315 You should specify such test targets with explicit target patterns on the command
laurentlb27453bf2018-10-08 11:23:18 -0700316 line if you want Bazel to build/test them.
317</p>
318
319<h3 id='fetch'>Fetching external dependencies</h3>
320
321<p>
322 By default, Bazel will download and symlink external dependencies during the
323 build. However, this can be undesirable, either because you'd like to know
324 when new external dependendencies are added or because you'd like to
325 "prefetch" dependencies (say, before a flight where you'll be offline). If you
326 would like to prevent new dependencies from being added during builds, you
327 can specify the <code>--fetch=false</code> flag. Note that this flag only
328 applies to repository rules that do not point to a directory in the local
329 file system. Changes, for example, to <code>local_repository</code>,
330 <code>new_local_repository</code> and Android SDK and NDK repository rules
331 will always take effect regardless of the value <code>--fetch</code> .
332</p>
333
334<p>
335 If you disallow fetching during builds and Bazel finds new external
336 dependencies, your build will fail.
337</p>
338
339<p>
340 You can manually fetch dependencies by running <code>bazel fetch</code>. If
341 you disallow during-build fetching, you'll need to run <code>bazel
342 fetch</code>:
343 <ol>
344 <li>Before you build for the first time.
345 <li>After you add a new external dependency.
346 </ol>
347 Once it has been run, you should not need to run it again until the WORKSPACE
348 file changes.
349</p>
350
351<p>
352 <code>fetch</code> takes a list of targets to fetch dependencies for. For
353 example, this would fetch dependencies needed to build <code>//foo:bar</code>
354 and <code>//bar:baz</code>:
355<pre>
356$ bazel fetch //foo:bar //bar:baz
357</pre>
358</p>
359
360<p>
361 To fetch all external dependencies for a workspace, run:
362<pre>
363$ bazel fetch //...
364</pre>
365</p>
366
367<p>
368 You do not need to run bazel fetch at all if you have all of the tools you are
369 using (from library jars to the JDK itself) under your workspace root.
370 However, if you're using anything outside of the workspace directory then Bazel
371 will automatically run <code>bazel fetch</code> before running
372 <code>bazel build</code>.
373</p>
374
375<h4 id='repository-cache'>The repository cache</h4>
376
377<p>
378 Bazel tries to avoid fetching the same file several times, even if the same
379 file is needed in different workspaces, or if the definition of an external
380 repository changed but it still needs the same file to download. To do so,
381 bazel caches all files downloaded in the repository cache which, by default,
382 is located at <code>~/.cache/bazel/_bazel_$USER/cache/repos/v1/</code>. The
383 location can be changed by the <code>--repository_cache</code> option. The
384 cache is shared between all workspaces and installed versions of bazel.
385 An entry is taken from the cache if
386 Bazel knows for sure that it has a copy of the correct file, that is, if the
387 download request has a SHA256 sum of the file specified and a file with that
388 hash is in the cache. So specifying a hash for each external file is
389 not only a good idea from a security perspective; it also helps avoiding
390 unnecessary downloads.
391</p>
392
393<p>
394 Upon each cache hit, the modification time of the file in the cache is
jingwen74065202018-11-21 11:34:55 -0800395 updated. In this way, the last use of a file in the cache directory can easily
laurentlb27453bf2018-10-08 11:23:18 -0700396 be determined, for example to manually clean up the cache. The cache is never
397 cleaned up automatically, as it might contain a copy of a file that is no
398 longer available upstream.
399</p>
400
401<h4 id='distdir'>Distribution files directories</h4>
402
403<p>
404 Besides the repository cache, Bazel has a second mechanism to avoid
405 unnecessary downloads. Using the <code>--distdir</code> option, you can
406 specify additional read-only directories to look for files instead of fetching
407 them. A file is taken from such a directory if the file name is equal to
408 the base name of the URL and additionally the hash of the file is equal to the
409 one specified in the download request (again, no file is taken from a distdir,
410 if no hash is specified). While the condition on the file name is not
411 necessary for correctness, it reduces the number of candidate files to one per
412 specified directory. In this way, specifying distribution files directories
413 remains efficient, even if the number of files in such a directory grows
414 large.
415</p>
416
417<h3 id='configurations'>Build configurations and cross-compilation</h3>
418
419<p>
420 All the inputs that specify the behavior and result of a given
421 build can be divided into two distinct categories.
422 The first kind is the intrinsic information stored in the BUILD
423 files of your project: the build rule, the values of its attributes,
424 and the complete set of its transitive dependencies.
425 The second kind is the external or environmental data, supplied by
426 the user or by the build tool: the choice of target architecture,
427 compilation and linking options, and other toolchain configuration
428 options. We refer to a complete set of environmental data as
429 a <b>configuration</b>.
430</p>
431<p>
432 In any given build, there may be more than one configuration.
433 Consider a cross-compile, in which you build
434 a <code>//foo:bin</code> executable for a 64-bit architecture,
435 but your workstation is a 32-bit machine. Clearly, the build
436 will require building <code>//foo:bin</code> using a toolchain
437 capable of creating 64-bit executables, but the build system must
438 also build various tools used during the build itself&mdash;for example
439 tools that are built from source, then subsequently used in, say, a
440 genrule&mdash;and these must be built to run on your workstation.
441 Thus we can identify two configurations: the <b>host
442 configuration</b>, which is used for building tools that run during
443 the build, and the <b>target configuration</b> (or <i>request
444 configuration</i>, but we say "target configuration" more often even
445 though that word already has many meanings), which is
446 used for building the binary you ultimately requested.
447</p>
448<p>
449 Typically, there are many libraries that are prerequisites of both
450 the requested build target (<code>//foo:bin</code>) and one or more of
451 the host tools, for example some base libraries. Such libraries must be built
452 twice, once for the host configuration, and once for the target
453 configuration.<br/>
454 Bazel takes care of ensuring that both variants are built, and that
455 the derived files are kept separate to avoid interference; usually
456 such targets can be built concurrently, since they are independent
457 of each other. If you see progress messages indicating that a given
458 target is being built twice, this is most likely the explanation.
459</p>
460<p>
461 Bazel uses one of two ways to select the host configuration, based
462 on the <code class='flag'>--distinct_host_configuration</code> option. This
463 boolean option is somewhat subtle, and the setting may improve (or
464 worsen) the speed of your builds.
465</p>
466
467<h4><code class='flag'>--distinct_host_configuration=false</code></h4>
468<p>
469 When this option is false, the host and
470 request configurations are identical: all tools required during the
471 build will be built in exactly the same way as target programs.
472 This setting means that no libraries need to be built twice during a
473 single build, so it keeps builds short.
474 However, it does mean that any change to your request configuration
475 also affects your host configuration, causing all the tools to be
476 rebuilt, and then anything that depends on the tool output to be
477 rebuilt too. Thus, for example, simply changing a linker option
478 between builds might cause all tools to be re-linked, and then all
479 actions using them reexecuted, and so on, resulting in a very large rebuild.
480 Also, please note: if your host architecture is not capable of
481 running your target binaries, your build will not work.
482</p>
483<p>
484 If you frequently make changes to your request configuration, such
485 as alternating between <code>-c opt</code> and <code>-c dbg</code>
486 builds, or between simple- and cross-compilation, we do not
487 recommend this option, as you will typically rebuild the majority of
488 your codebase each time you switch.
489</p>
490
491<h4><code class='flag'>--distinct_host_configuration=true</code> <i>(default)</i></h4>
492<p>
493 If this option is true, then instead of using the same configuration
494 for the host and request, a completely distinct host configuration
495 is used. The host configuration is derived from the target
496 configuration as follows:
497</p>
498<ul>
499 <li>Use the same version of Crosstool
500 (<code class='flag'>--crosstool_top</code>) as specified in the request
501 configuration, unless <code class='flag'>--host_crosstool_top</code> is
502 specified.
503 </li>
504 <li>
505 Use the value of <code class="flag">--host_cpu</code> for
506 <code class='flag'>--cpu</code>
507
508 (default: <code>k8</code>).
509 </li>
510 <li>Use the same values of these options as specified in the request
511 configuration:
512 <code class='flag'>--compiler</code>,
513 <code class='flag'>--use_ijars</code>,
514 If <code class='flag'>--host_crosstool_top</code> is used, then the value of
515 <code class='flag'>--host_cpu</code> is used to look up a
516 <code>default_toolchain</code> in the Crosstool
517 (ignoring <code class='flag'>--compiler</code>) for the host configuration.
518 </li>
519 <li>
520 Use the value of <code class="flag">--host_javabase</code> for
521 <code class='flag'>--javabase</code>
522 </li>
523 <li>
524 Use the value of <code class="flag">--host_java_toolchain</code> for
525 <code class='flag'>--java_toolchain</code>
526 </li>
527 <li>Use optimized builds for C++ code (<code>-c opt</code>).
528 </li>
529 <li>Generate no debugging information (<code class='flag'>--copt=-g0</code>).
530 </li>
531 <li>Strip debug information from executables and shared libraries
532 (<code class='flag'>--strip=always</code>).
533 </li>
534 <li>Place all derived files in a special location, distinct from
535 that used by any possible request configuration.
536 </li>
537 <li>Suppress stamping of binaries with build data
538 (see <code class='flag'>--embed_*</code> options).
539 </li>
540 <li>All other values remain at their defaults.
541 </li>
542</ul>
543<p>
544 There are many reasons why it might be preferable to select a
545 distinct host configuration from the request configuration.
546 Some are too esoteric to mention here, but two of them are worth
547 pointing out.
548</p>
549<p>
550 Firstly, by using stripped, optimized binaries, you reduce the time
551 spent linking and executing the tools, the disk space occupied by
552 the tools, and the network I/O time in distributed builds.
553</p>
554<p>
555 Secondly, by decoupling the host and request configurations in all
556 builds, you avoid very expensive rebuilds that would result from
557 minor changes to the request configuration (such as changing a linker options
558 does), as described earlier.
559</p>
560<p>
561 That said, for certain builds, this option may be a hindrance. In
562 particular, builds in which changes of configuration are infrequent
563 (especially certain Java builds), and builds where the amount of code that
564 must be built in both host and target configurations is large, may
565 not benefit.
566</p>
567
568<h3 id='correctness'>Correct incremental rebuilds</h3>
569
570<p>
571 One of the primary goals of the Bazel project is to ensure correct
572 incremental rebuilds. Previous build tools, especially those based
573 on Make, make several unsound assumptions in their implementation of
574 incremental builds.
575</p>
576<p>
577 Firstly, that timestamps of files increase monotonically. While
578 this is the typical case, it is very easy to fall afoul of this
579 assumption; syncing to an earlier revision of a file causes that file's
580 modification time to decrease; Make-based systems will not rebuild.
581</p>
582<p>
583 More generally, while Make detects changes to files, it does
584 not detect changes to commands. If you alter the options passed to
585 the compiler in a given build step, Make will not re-run the
586 compiler, and it is necessary to manually discard the invalid
587 outputs of the previous build using <code>make clean</code>.
588</p>
589<p>
590 Also, Make is not robust against the unsuccessful termination of one
591 of its subprocesses after that subprocess has started writing to
592 its output file. While the current execution of Make will fail, the
593 subsequent invocation of Make will blindly assume that the truncated
594 output file is valid (because it is newer than its inputs), and it
595 will not be rebuilt. Similarly, if the Make process is killed, a
596 similar situation can occur.
597</p>
598<p>
599 Bazel avoids these assumptions, and others. Bazel maintains a database
600 of all work previously done, and will only omit a build step if it
601 finds that the set of input files (and their timestamps) to that
602 build step, and the compilation command for that build step, exactly
603 match one in the database, and, that the set of output files (and
604 their timestamps) for the database entry exactly match the
605 timestamps of the files on disk. Any change to the input files or
606 output files, or to the command itself, will cause re-execution of
607 the build step.
608</p>
609<p>
610 The benefit to users of correct incremental builds is: less time
611 wasted due to confusion. (Also, less time spent waiting for
612 rebuilds caused by use of <code>make clean</code>, whether necessary
613 or pre-emptive.)
614</p>
615
616<h4>Build consistency and incremental builds</h4>
617<p>
618 Formally, we define the state of a build as <i>consistent</i> when
619 all the expected output files exist, and their contents are correct,
620 as specified by the steps or rules required to create them. When
621 you edit a source file, the state of the build is said to
622 be <i>inconsistent</i>, and remains inconsistent until you next run
623 the build tool to successful completion. We describe this situation
624 as <i>unstable inconsistency</i>, because it is only temporary, and
625 consistency is restored by running the build tool.
626</p>
627<p>
628 There is another kind of inconsistency that is pernicious: <i>stable
629 inconsistency</i>. If the build reaches a stable inconsistent
630 state, then repeated successful invocation of the build tool does
631 not restore consistency: the build has gotten "stuck", and the
632 outputs remain incorrect. Stable inconsistent states are the main
633 reason why users of Make (and other build tools) type <code>make
634 clean</code>. Discovering that the build tool has failed in this
635 manner (and then recovering from it) can be time consuming and very
636 frustrating.
637</p>
638<p>
639 Conceptually, the simplest way to achieve a consistent build is to
640 throw away all the previous build outputs and start again: make
641 every build a clean build. This approach is obviously too
642 time-consuming to be practical (except perhaps for release
643 engineers), and therefore to be useful, the build tool must be able
644 to perform incremental builds without compromising consistency.
645</p>
646<p>
647 Correct incremental dependency analysis is hard, and as described
648 above, many other build tools do a poor job of avoiding stable
649 inconsistent states during incremental builds. In contrast, Bazel
650 offers the following guarantee: after a successful invocation of the
651 build tool during which you made no edits, the build will be in a
652 consistent state. (If you edit your source files during a build,
653 Bazel makes no guarantee about the consistency of the result of the
654 current build. But it does guarantee that the results of
655 the <i>next</i> build will restore consistency.)
656</p>
657<p>
658 As with all guarantees, there comes some fine print: there are some
659 known ways of getting into a stable inconsistent state with Bazel.
660 We won't guarantee to investigate such problems arising from deliberate
661 attempts to find bugs in the incremental dependency analysis, but we
662 will investigate and do our best to fix all stable inconsistent
663 states arising from normal or "reasonable" use of the build tool.
664</p>
665<p>
666 If you ever detect a stable inconsistent state with Bazel, please report a bug.
667
668</p>
669
670<h4 id='sandboxing'>Sandboxed execution</h4>
671<p>
672 Bazel uses sandboxes to guarantee that actions run hermetically<sup>1</sup> and correctly.
673 Bazel runs <i>Spawn</i>s (loosely speaking: actions) in sandboxes that only contain the minimal
674 set of files the tool requires to do its job. Currently sandboxing works on Linux 3.12 or newer
675 with the <code>CONFIG_USER_NS</code> option enabled, and also on macOS 10.11 or newer.
676</p>
677<p>
678 Bazel will print a warning if your system does not support sandboxing to alert you to the fact
679 that builds are not guaranteed to be hermetic and might affect the host system in unknown ways.
680 To disable this warning you can pass the <code>--ignore_unsupported_sandboxing</code> flag to
681 Bazel.
682</p>
683
684<p>
685 On some platforms such as <a href="https://cloud.google.com/kubernetes-engine/">Google Kubernetes
686 Engine</a> cluster nodes or Debian, user namespaces are deactivated by default due to security
687 concerns. This can be checked by looking at the file
688 <code>/proc/sys/kernel/unprivileged_userns_clone</code>: if it exists and contains a 0, then
689 user namespaces can be activated with <code>sudo sysctl kernel.unprivileged_userns_clone=1</code>.
690</p>
691<p>
692 In some cases, the Bazel sandbox fails to execute rules because of the system setup. The symptom
693 is generally a failure that output a message similar to
694 <code>namespace-sandbox.c:633: execvp(argv[0], argv): No such file or directory</code>. In that
ulfjack4b8cb052018-11-21 08:50:51 -0800695 case, try to deactivate the sandbox for genrules with <code>--strategy=Genrule=standalone</code>
laurentlb27453bf2018-10-08 11:23:18 -0700696 and for other rules with <code>--spawn_strategy=standalone</code>. Also please report a bug on our
697 issue tracker and mention which Linux distribution you're using so that we can investigate and
698 provide a fix in a subsequent release.
699</p>
700
701<p>
702 <sup>1</sup>: Hermeticity means that the action only uses its declared input files and no other
703 files in the filesystem, and it only produces its declared output files.
704</p>
705
laurentlb27453bf2018-10-08 11:23:18 -0700706
707<h3 id='phases'>Phases of a build</h3>
708
709<p>
710 In Bazel, a build occurs in three distinct phases; as a user,
711 understanding the difference between them provides insight into the
712 options which control a build (see below).
713</p>
714
715<h4 id='loading-phase'>Loading phase</h4>
716<p>
717 The first is <b>loading</b> during which all the necessary BUILD
718 files for the initial targets, and their transitive closure of
719 dependencies, are loaded, parsed, evaluated and cached.
720</p>
721<p>
722 For the first build after a Bazel server is started, the loading
723 phase typically takes many seconds as many BUILD files are loaded
724 from the file system. In subsequent builds, especially if no BUILD
725 files have changed, loading occurs very quickly.
726</p>
727<p>
728 Errors reported during this phase include: package not found, target
729 not found, lexical and grammatical errors in a BUILD file,
730 and evaluation errors.
731</p>
732
733<h4 id='analysis-phase'>Analysis phase</h4>
734<p>
735 The second phase, <b>analysis</b>, involves the semantic analysis
736 and validation of each build rule, the construction of a build
737 dependency graph, and the determination of exactly what work is to
738 be done in each step of the build.
739</p>
740<p>
741 Like loading, analysis also takes several seconds when computed in
742 its entirety. However, Bazel caches the dependency graph from
743 one build to the next and only reanalyzes what it has to, which can
744 make incremental builds extremely fast in the case where the
745 packages haven't changed since the previous build.
746</p>
747<p>
748 Errors reported at this stage include: inappropriate dependencies,
749 invalid inputs to a rule, and all rule-specific error messages.
750</p>
751<p>
752 The loading and analysis phases are fast because
753 Bazel avoids unnecessary file I/O at this stage, reading only BUILD
754 files in order to determine the work to be done. This is by design,
755 and makes Bazel a good foundation for analysis tools, such as
756 Bazel's <a href='#query'>query</a> command, which is implemented
757 atop the loading phase.
758</p>
759
760<h4 id='execution-phase'>Execution phase</h4>
761<p>
762 The third and final phase of the build is <b>execution</b>. This
763 phase ensures that the outputs of each step in the build are
764 consistent with its inputs, re-running compilation/linking/etc. tools as
765 necessary. This step is where the build spends the majority of
766 its time, ranging from a few seconds to over an hour for a large
767 build. Errors reported during this phase include: missing source
768 files, errors in a tool executed by some build action, or failure of a tool to
769 produce the expected set of outputs.
770</p>
771
772
laurentlb15918182018-10-17 11:44:25 -0700773
774<h2 id='client/server'>Client/server implementation</h2>
775
776<p>
777 The Bazel system is implemented as a long-lived server process.
778 This allows it to perform many optimizations not possible with a
779 batch-oriented implementation, such as caching of BUILD files,
780 dependency graphs, and other metadata from one build to the
781 next. This improves the speed of incremental builds, and allows
782 different commands, such as <code>build</code>
783 and <code>query</code> to share the same cache of loaded packages,
784 making queries very fast.
785</p>
786<p>
787 When you run <code>bazel</code>, you're running the client. The
788 client finds the server based on the output base, which by default is
789 determined by the path of the base workspace directory and your
790 userid, so if you build in multiple workspaces, you'll have multiple
791 output bases and thus multiple Bazel server processes. Multiple
792 users on the same workstation can build concurrently in the same
793 workspace because their output bases will differ (different userids).
794 If the client cannot find a running server instance, it starts a new
795 one. The server process will stop after a period of inactivity (3 hours,
796 by default, which can be modified using the startup option <code>--max_idle_secs</code>).
797</p>
798<p>
799 For the most part, the fact that there is a server running is
800 invisible to the user, but sometimes it helps to bear this in mind.
801 For example, if you're running scripts that perform a lot of
802 automated builds in different directories, it's important to ensure
803 that you don't accumulate a lot of idle servers; you can do this by
804 explicitly shutting them down when you're finished with them, or by
805 specifying a short timeout period.
806</p>
807<p>
808 The name of a Bazel server process appears in the output of <code>ps
809 x</code> or <code>ps -e f</code> as
810 <code>bazel(<i>dirname</i>)</code>, where <i>dirname</i> is the
811 basename of the directory enclosing the root of your workspace directory.
812 For example:
813</p>
814<pre>
815 % ps -e f
816 16143 ? Sl 3:00 bazel(src-johndoe2) -server -Djava.library.path=...
817</pre>
818<p>
819 This makes it easier to find out which server process belongs to a
820 given workspace. (Beware that with certain other options
821 to <code>ps</code>, Bazel server processes may be named just
822 <code>java</code>.) Bazel servers can be stopped using
823 the <a href='#shutdown'>shutdown</a> command.
824</p>
825
826<p>
827 When running <code>bazel</code>, the client first checks that the
828 server is the appropriate version; if not, the server is stopped and
829 a new one started. This ensures that the use of a long-running
830 server process doesn't interfere with proper versioning.
831</p>
832
833<h2 id='bazelrc'>
834 <code>.bazelrc</code>, the Bazel configuration file,
835 the <code class='flag'>--bazelrc=<var>file</var></code> option, and
836 the <code class='flag'>--config=<var>value</var></code> option
837</h2>
838
839<p>
840 Bazel accepts many options. Typically, some of these are varied
841 frequently (for example, <code class='flag'>--subcommands</code>) while others stay the
842 same across several builds (e.g. <code class='flag'>--package_path</code>).
843 To avoid having to specify these unchanged options for every build (and other commands)
844 Bazel allows you to specify options in a configuration file.
845</p>
846
847
848<h4>Where are the <code>.bazelrc</code> files?</h4>
849<p>
850 Bazel looks for an optional configuration file in the following locations,
851 in the order shown below. The options are interpreted in this order, so
852 options in later files can override a value from an earlier file if a
853 conflict arises. All options to control which of these files are loaded are
Garrett Hopper8787d442019-02-21 11:46:57 -0800854 startup options, which means they must occur after <code>bazel</code> and
laurentlb15918182018-10-17 11:44:25 -0700855 before the command (<code>build</code>, etc).
856</p>
857<ol>
858 <li>
859 Unless the <code class='flag'>--nosystem_rc</code> is present, Bazel looks for
860 the system .bazelrc file: on Unix, it lives at <code>/etc/bazel.bazelrc</code>,
Andrew Suffield49107ad2019-03-08 09:57:38 -0800861 and on Windows at <code>%ProgramData%/bazel.bazelrc</code>.
laurentlb15918182018-10-17 11:44:25 -0700862
863 If another system-specified location is required, this value can be
864 changed by setting <code>BAZEL_SYSTEM_BAZELRC_PATH</code> in
865 <code>src/main/cpp:option_processor</code> and using this custom Bazel binary.
Andrew Suffield49107ad2019-03-08 09:57:38 -0800866
867 The system-specified location may contain environment variable
868 substitutions, as <code>%var_name%</code> on Windows
869 or <code>${var_name}</code> on Unix.
laurentlb15918182018-10-17 11:44:25 -0700870 </li>
871 <li>
872 Unless the <code class='flag'>--noworkspace_rc</code> is present, Bazel looks
873 for the <code>.bazelrc</code> file in your workspace directory.
874 </li>
875 <li>
876 Unless the <code class='flag'>--nohome_rc</code> is present, Bazel looks for
877 the home, or user, bazelrc: the file <code>.bazelrc</code> in your home
878 directory.
879 </li>
880 <li>
881 An additional .rc file can be specified by the
882 <code class='flag'>--bazelrc=<var>file</var></code> startup option. If this
883 option is not present, no additional file is loaded. Unlike in the three
884 default locations specified above, an incorrect path or non-existent file
885 will fail if passed explicitly.
886 </li>
887</ol>
888<h4><code>.bazelrc</code> syntax and semantics</h4>
889<p>
890 Like all UNIX "rc" files, the <code>.bazelrc</code> file is a text file with
891 a line-based grammar. Lines starting <code>#</code> are considered comments
892 and are ignored, as are blank lines. Each line contains a sequence of words,
893 which are tokenized according to the same rules as the Bourne shell.
894</p>
895
896<h5>Imports</h5>
897<p>
898 Lines that start with <code>import</code> are special: if Bazel encounters such
899 a line in a <code>.bazelrc</code> file, it parses the contents of the file
900 referenced by the import statement, too. Options specified in an imported file
901 take precedence over options specified before the import statement. Options
902 specified after the import statement take precedence over the options in the
903 imported file. Options in files imported later take precedence over files
904 imported earlier. To specify a path that is relative to the workspace root,
905 write <code>import %workspace%/path/to/bazelrc</code>.
906</p>
907
908<h5>Option defaults</h5>
909<p>
910 Most lines of a bazelrc define default option values. The first word on each
911 line specifies when these defaults are applied:
912</p>
913<ol>
914 <li>
915 <code>startup</code>: startup options, which go before the command, and
916 are described in <code>bazel help startup_options</code>.
917 </li>
918 <li>
919 <code>common</code>: options that apply to all Bazel commands.
920 </li>
921 <li>
922 <code><i>command</i></code>: Bazel command, such as <code>build</code>
923 or <code>query</code> to which the options apply. These options also apply
924 to all commands that inherit from the specified command. (For example,
925 <code>test</code> inherits from <code>build</code>.)
926 </li>
927</ol>
928<p>
929 Each of these lines may be used more than once and the arguments that follow
930 the first word are combined as if they had appeared on a single line.
931 (Users of CVS, another tool with a "Swiss army knife" command-line interface,
932 will find the syntax similar to that of <code>.cvsrc</code>.)
933</p>
934<p>
935 Options specified in the command line always take precedence over those from
936 a configuration file. Within the configuration file, precedence is
937 given by specificity. This means that lines for a more specific command take
938 precedence over lines for a less specific command, with <code>common</code>
939 getting lowest precedence (for example, the <code>test</code> command inherits
940 all the options from the <code>build</code> command, so the line
941 <code>test --foo=bar</code> takes precedence over the line
942 <code>build --foo=baz</code>, regardless of which rc file or what order
943 these two lines are in). Two lines specifying options for the same command at
944 equal specificity are parsed in the order in which they appear within the file.
945 The user-specific configuration file takes precedence over the master file.
946</p>
947<p>
948 Because this precedence rule does not match the file order, we recommend
949 that the file follows the same order, with <code>common</code> options at the
950 top, and most-specific commands near the bottom. This way, the order in which
951 the options are read is the same as the order in which they are applied,
952 which is more intuitive.
953</p>
954<p>
955 The arguments specified on a line of an rc file may include arguments that are
956 not options, such as the names of build targets, and so on. These, like the
957 options specified in the same files, have lower precedence than their siblings
958 on the command line, and are always prepended to the explicit list of non-
959 option arguments.
960</p>
961<h5><code>--config</code></h5>
962<p>
963 In addition to setting option defaults, the rc file can be used to group
964 options and provide a shorthand for common groupings. This is done by adding
965 a <code>:name</code> suffix to the command. These options are ignored by
966 default, but will be included when the option
967 <code>--config=<var>name</var></code> is present, either on the command line
968 or in a <code>.bazelrc</code> file, recursively, even inside of another
969 config definition. The options specified by <code>command:name</code> will
970 only be expanded for applicable commands, in the precedence order described
971 above.
972</p>
973<p>
974 Note that configs can be defined in any <code>.bazelrc</code> file, and that
975 all lines of the form <code>command:name</code> (for applicable commands)
976 will be expanded, across the different rc files. In order to avoid name
977 conflicts, we suggest that configs defined in personal rc files start
978 with an underscore ('_') to avoid unintentional name sharing.
979</p>
980<p>
981 <code>--config=foo</code> expands to the options defined in the rc files
982 "in-place" so that the options specified for the config have the same
983 precedence that the <code>--config=foo</code> option had.
984</p>
985
986<h5>Example</h5>
987<p>
988 Here's an example <code>~/.bazelrc</code> file:
989</p>
990<pre>
991 # Bob's Bazel option defaults
992
993 startup --host_jvm_args=-XX:-UseParallelGC
994 import /home/bobs_project/bazelrc
995 build --show_timestamps --keep_going --jobs 600
996 build --color=yes
997 query --keep_going
998
999 # Definition of --config=memcheck
1000 build:memcheck --strip=never --test_timeout=3600
1001</pre>
1002
1003
1004<h3 id='startup files'>
1005 Other files governing Bazel's behavior
1006</h3>
1007
1008<h4 id='.bazelignore'><code>.bazelignore</code></h4>
1009<p>
1010 You can specify directories within the workspace
1011 that you want Bazel to ignore, such as related projects
1012 that use other build systems. Place a file called
1013 <code>.bazelignore</code> at the root of the workspace
1014 and add the directories you want Bazel to ignore, one per
1015 line. Entries are relative to the workspace root.
1016</p>
1017
laurentlb27453bf2018-10-08 11:23:18 -07001018<h2 id='scripting'>Calling Bazel from scripts</h2>
1019
1020<p>
1021 Bazel can be called from scripts in order to perform a build, run
1022 tests or query the dependency graph. Bazel has been designed to
1023 enable effective scripting, but this section lists some details to
1024 bear in mind to make your scripts more robust.
1025</p>
1026
1027<h3>Choosing the output base</h3>
1028
1029<p>
1030 The <code class='flag'>--output_base</code> option controls where the Bazel process should
1031 write the outputs of a build to, as well as various working files used
1032 internally by Bazel, one of which is a lock that guards against
1033 concurrent mutation of the output base by multiple Bazel processes.
1034</p>
1035<p>
1036 Choosing the correct output base directory for your script depends
1037 on several factors. If you need to put the build outputs in a
1038 specific location, this will dictate the output base you need to
1039 use. If you are making a "read only" call to Bazel
1040 (e.g. <code>bazel query</code>), the locking factors will be more important.
1041 In particular, if you need to run multiple instances of your script
1042 concurrently, you will need to give each one a different (or random) output
1043 base.
1044</p>
1045<p>
1046 If you use the default output base value, you will be contending for
1047 the same lock used by the user's interactive Bazel commands. If the
1048 user issues long-running commands such as builds, your script will
1049 have to wait for those commands to complete before it can continue.
1050</p>
1051
1052<h3>Notes about Server Mode</h3>
1053
1054<p>
1055 By default, Bazel uses a long-running <a
1056 href='#client/server'>server process</a> as an optimization. When running Bazel
1057 in a script, don't forget to call <code>shutdown</code> when you're finished
1058 with the server, or, specify <code class='flag'>--max_idle_secs=5</code> so
1059 that idle servers shut themselves down promptly.
1060</p>
1061
1062<h3>What exit code will I get?</h3>
1063
1064<p>
1065 Bazel attempts to differentiate failures due to the source code under
1066consideration from external errors that prevent Bazel from executing properly.
1067Bazel execution can result in following exit codes:
1068</p>
1069
1070<b>Exit Codes common to all commands:</b>
1071<ul>
1072 <li><code>0</code> - Success</li>
1073 <li><code>2</code> - Command Line Problem, Bad or Illegal flags or command
1074 combination, or Bad Environment Variables. Your command line must be
1075 modified.</li>
1076 <li><code>8</code> - Build Interrupted but we terminated with an orderly shutdown.</li>
1077 <li><code>32</code> - External Environment Failure not on this machine.</li>
1078 <li><code>33</code> - OOM failure. You need to modify your command line.</li>
1079
1080 <li><code>34</code> - Reserved for Google-internal use.</li>
1081 <li><code>35</code> - Reserved for Google-internal use.</li>
1082 <li><code>36</code> - Local Environmental Issue, suspected permanent.</li>
1083 <li><code>37</code> - Unhandled Exception / Internal Bazel Error.</li>
1084 <li><code>38</code> - Reserved for Google-internal use.</li>
1085 <li><code>40-44</code> - Reserved for errors in Bazel's command line launcher,
1086 <code>bazel.cc</code> that are not command line
1087 related. Typically these are related to bazel server
1088 being unable to launch itself.</li>
1089</ul>
1090
1091<b>Return codes for commands <code>bazel build</code>, <code>bazel test</code>.</b>
1092<ul>
1093 <li><code>1</code> - Build failed.</li>
1094 <li><code>3</code> - Build OK, but some tests failed or timed out.</li>
1095 <li><code>4</code> - Build successful but no tests were found even though
1096 testing was requested.</li>
1097</ul>
1098
1099<b>For <code>bazel run</code>:</b>
1100<ul>
1101 <li><code>1</code> - Build failed.</li>
1102 <li>If the build succeeds but the executed subprocess returns a non-zero exit code it will be the
1103 exit code of the command as well.</li>
1104</ul>
1105
1106
1107<b>For
1108
1109 <code>bazel query</code>:</b>
1110<ul>
1111 <li><code>3</code> - Partial success, but the query encountered 1 or more
1112 errors in the input BUILD file set and therefore the
1113 results of the operation are not 100% reliable.
1114 This is likely due to a <code class='flag'>--keep_going</code> option
1115 on the command line.</li>
1116 <li><code>7</code> - Command failure.</li>
1117</ul>
1118
1119<p>
1120 Future Bazel versions may add additional exit codes, replacing generic failure
1121 exit code <code>1</code> with a different non-zero value with a particular
1122 meaning. However, all non-zero exit values will always constitute an error.
1123</p>
1124
1125<h3>Reading the .bazelrc file</h3>
1126
1127<p>
1128 By default, Bazel will read the <a
1129 href='#bazelrc'><code>.bazelrc</code> file</a> from the base workspace
1130 directory or the user's home directory. Whether or not this is
1131 desirable is a choice for your script; if your script needs to be
1132 perfectly hermetic (e.g. when doing release builds), you should
1133 disable reading the .bazelrc file by using the option
1134 <code class='flag'>--bazelrc=/dev/null</code>. If you want to perform a build
1135 using the user's preferred settings, the default behavior is better.
1136</p>
1137
1138<h3>Command log</h3>
1139
1140<p>
1141 The Bazel output is also available in a command log file which you can
1142 find with the following command:
1143</p>
1144
1145<pre>
1146% bazel info command_log
1147</pre>
1148
1149<p>
1150 The command log file contains the interleaved stdout and stderr streams
1151 of the most recent Bazel command. Note that running <code>bazel info</code>
1152 will overwrite the contents of this file, since it then becomes the most
1153 recent Bazel command. However, the location of the command log file will
1154 not change unless you change the setting of the <code class='flag'>--output_base</code>
1155 or <code class='flag'>--output_user_root</code> options.
1156</p>
1157
1158<h3>Parsing output</h3>
1159
1160<p>
1161 The Bazel output is quite easy to parse for many purposes. Two
1162 options that may be helpful for your script are
1163 <code class='flag'>--noshow_progress</code> which suppresses progress messages,
1164 and <code class='flag'>--show_result <var>n</var></code>, which controls whether
1165 or not "build up-to-date" messages are printed; these messages may
1166 be parsed to discover which targets were successfully built, and the
1167 location of the output files they created. Be sure to specify a
1168 very large value of <i>n</i> if you rely on these messages.
1169</p>
1170
1171<h2 id='profiling'>Troubleshooting performance by profiling</h2>
1172
1173<p>
1174 The first step in analyzing the performance of your build is to profile your build with the
1175 <a href='#flag--profile'><code class='flag'>--profile</code></a> option.
1176</p>
1177
1178<p>
1179 The file generated by the <a href='#flag--profile'><code class='flag'>--profile</code></a>
1180 command is a binary file. Once you have generated this binary profile, you can analyze it using
1181 Bazel's <a href='#analyze-profile'><code>analyze-profile</code></a> command. By default, it will
1182 print out summary analysis information for each of the specified profile datafiles. This includes
1183 cumulative statistics for different task types for each build phase and an analysis of the
1184 critical execution path.
1185</p>
1186
1187<p>
1188 The first section of the default output describes an overview of the time spent on the different
1189 build phases:
1190</p>
1191<pre>
1192=== PHASE SUMMARY INFORMATION ===
1193
1194Total launch phase time 6.00 ms 0.01%
1195Total init phase time 864 ms 1.11%
1196Total loading phase time 21.841 s 28.05%
1197Total analysis phase time 5.444 s 6.99%
1198Total preparation phase time 155 ms 0.20%
1199Total execution phase time 49.473 s 63.54%
1200Total finish phase time 83.9 ms 0.11%
1201Total run time 77.866 s 100.00%
1202</pre>
1203
1204<p>
1205 The following sections show the execution time of different tasks happening during a particular
1206 phase:
1207</p>
1208<pre>
1209=== INIT PHASE INFORMATION ===
1210
1211Total init phase time 864 ms
1212
1213Total time (across all threads) spent on:
1214 Type Total Count Average
1215 VFS_STAT 2.72% 1 23.5 ms
1216 VFS_READLINK 32.19% 1 278 ms
1217
1218=== LOADING PHASE INFORMATION ===
1219
1220Total loading phase time 21.841 s
1221
1222Total time (across all threads) spent on:
1223 Type Total Count Average
1224 SPAWN 3.26% 154 475 ms
1225 VFS_STAT 10.81% 65416 3.71 ms
1226[...]
1227SKYLARK_BUILTIN_FN 13.12% 45138 6.52 ms
1228
1229=== ANALYSIS PHASE INFORMATION ===
1230
1231Total analysis phase time 5.444 s
1232
1233Total time (across all threads) spent on:
1234 Type Total Count Average
1235 SKYFRAME_EVAL 9.35% 1 4.782 s
1236 SKYFUNCTION 89.36% 43332 1.06 ms
1237
1238=== EXECUTION PHASE INFORMATION ===
1239
1240Total preparation time 155 ms
1241Total execution phase time 49.473 s
1242Total time finalizing build 83.9 ms
1243
1244Action dependency map creation 0.00 ms
1245Actual execution time 49.473 s
1246
1247Total time (across all threads) spent on:
1248 Type Total Count Average
1249 ACTION 2.25% 12229 10.2 ms
1250[...]
1251 SKYFUNCTION 1.87% 236131 0.44 ms
1252</pre>
1253
1254<p>
1255 The last section shows the critical path:
1256</p>
1257<pre>
1258Critical path (32.078 s):
1259 Id Time Percentage Description
12601109746 5.171 s 16.12% Building [...]
12611109745 164 ms 0.51% Extracting interface [...]
12621109744 4.615 s 14.39% Building [...]
1263[...]
12641109639 2.202 s 6.86% Executing genrule [...]
12651109637 2.00 ms 0.01% Symlinking [...]
12661109636 163 ms 0.51% Executing genrule [...]
1267 4.00 ms 0.01% [3 middleman actions]
1268</pre>
1269
1270<p>
1271 You can use the following options to display more detailed information:
1272</p>
1273
1274<ul>
1275 <li id='dump-text-format'><a href='#flag--dump'><code>--dump=text</code></a>
1276 <p>
1277 This option prints all recorded tasks in the order they occurred. Nested tasks are indented
1278 relative to the parent. For each task, output includes the following information:
1279 </p>
1280<pre>
1281[task type] [task description]
1282Thread: [thread id] Id: [task id] Parent: [parent task id or 0 for top-level tasks]
1283Start time: [time elapsed from the profiling session start] Duration: [task duration]
1284[aggregated statistic for nested tasks, including count and total duration for each nested task]
1285</pre>
1286 </li>
1287 <li id='dump-raw-format'><a href='#flag--dump'><code>--dump=raw</code></a>
1288 <p>
1289 This option is most useful for automated analysis with scripts. It outputs each task record on
1290 a single line using '|' delimiter between fields. Fields are printed in the following order:
1291 </p>
1292 <ol>
1293 <li>thread id - integer positive number, identifies owner thread for the task</li>
1294 <li>task id - integer positive number, identifies specific task</li>
1295 <li>parent task id for nested tasks or 0 for root tasks</li>
1296 <li>task start time in ns, relative to the start of the profiling session</li>
1297 <li>task duration in ns. Please note that this will include duration of all subtasks.</li>
1298 <li>aggregated statistic for immediate subtasks per type. This will include type name (lower
1299 case), number of subtasks for that type and their cumulative duration. Types are
1300 space-delimited and information for single type is comma-delimited.</li>
1301 <li>task type (upper case)</li>
1302 <li>task description</li>
1303 </ol>
1304
1305 Example:
1306<pre>
13071|1|0|0|0||PHASE|Launch Bazel
13081|2|0|6000000|0||PHASE|Initialize command
13091|3|0|168963053|278111411||VFS_READLINK|/[...]
13101|4|0|571055781|23495512||VFS_STAT|/[...]
13111|5|0|869955040|0||PHASE|Load packages
1312[...]
1313</pre>
1314 </li>
1315 <li id='dump-html-format'><a href='#flag--html'><code>--html</code></a>
1316 <p>
1317 This option writes a file called <code>&lt;profile-file&gt;.html</code> in the directory of the
1318 profile file. Open it in your browser to see the visualization of the actions in your build.
1319 Note that the file can be quite large and may push the capabilities of your browser &ndash;
1320 please wait for the file to load.
1321 </p>
1322 <p>
1323 In most cases, the HTML output from <a href='#flag--html'><code>--html</code></a> is easier to
1324 read than the <a href='#flag--dump'><code>--dump</code></a> output.
1325 It includes a Gantt chart that displays time on the horizontal axis and
1326 threads of execution along the vertical axis. If you click on the Statistics link in the top
1327 right corner of the page, you will jump to a section that lists summary analysis information
1328 from your build.
1329 </p>
1330 <ul>
1331 <li><a href='#flag--html_details'><code>--html_details</code></a>
1332 <p>
1333 Additionally passing this option will render a more detailed execution chart and additional
1334 tables on the performance of built-in and user-defined Skylark functions. Beware that this
1335 increases the file size and the load on the browser considerably.
1336 </p>
1337 </li>
1338 </ul></li>
1339</ul>
1340
1341<p>If Bazel appears to be hung, you can hit <kbd><kbd>ctrl</kbd> + <kbd>\</kbd></kbd> or send
1342 Bazel a <code>SIGQUIT</code> signal (<code>kill -3 $(bazel info server_pid)</code>) to get a
1343 thread dump in the file <code>$(bazel info output_base)/server/jvm.out</code>.
1344</p>
1345
1346<p>
1347 Since you may not be able to run <code>bazel info</code> if bazel is hung, the
1348 <code>output_base</code> directory is usually the parent of the <code>bazel-&lt;workspace&gt;</code>
1349 symlink in your workspace directory.
1350</p>