blob: 782f5ef7b97099e28803e012c3d4ae6665f7296e [file] [log] [blame]
David Chen8fe82a32016-08-24 10:55:41 +00001---
2layout: documentation
3title: User Manual
4---
5<h1>A User's Guide to Bazel</h1>
6
7<h2 id='overview'>Bazel overview</h2>
8
9<p>
10 To run Bazel, go to
11
12 your base <a href="/docs/build-ref.html#workspaces">workspace</a> directory
13 or any of its subdirectories and type <code>bazel</code>.
14</p>
15
16<pre>
17 % bazel help
18 [Bazel release bazel-&lt;<i>version</i>&gt;]
19 Usage: bazel &lt;command&gt; &lt;options&gt; ...
20
21 Available commands:
22 <a href='#analyze-profile'>analyze-profile</a> Analyzes build profile data.
23 <a href='#build'>build</a> Builds the specified targets.
24
25 <a href='#canonicalize'>canonicalize-flags</a> Canonicalize Bazel flags.
26 <a href='#clean'>clean</a> Removes output files and optionally stops the server.
27
28 <a href='#help'>help</a> Prints help for commands, or the index.
29
30 <a href='#info'>info</a> Displays runtime info about the bazel server.
31
32 <a href='#fetch'>fetch</a> Fetches all external dependencies of a target.
33 <a href='#mobile-install'>mobile-install</a> Installs apps on mobile devices.
34
35 <a href='#query'>query</a> Executes a dependency graph query.
36
37 <a href='#run'>run</a> Runs the specified target.
38 <a href='#shutdown'>shutdown</a> Stops the Bazel server.
39 <a href='#test'>test</a> Builds and runs the specified test targets.
40 <a href='#version'>version</a> Prints version information for Bazel.
41
42 Getting more help:
43 bazel help &lt;command&gt;
44 Prints help and options for &lt;command&gt;.
45 bazel help <a href='#startup_options'>startup_options</a>
46 Options for the JVM hosting Bazel.
47 bazel help <a href='#target-patterns'>target-syntax</a>
48 Explains the syntax for specifying targets.
49 bazel help info-keys
50 Displays a list of keys used by the info command.
51
52</pre>
53<p>
54 The <code>bazel</code> tool performs many functions, called
55 commands; users of CVS and Subversion will be familiar
56 with this "Swiss army knife" arrangement. The most commonly used one is of
57 course <code>bazel build</code>. You can browse the online help
58 messages using <code>bazel help</code>.
59</p>
60
61<h3 id='client/server'>Client/server implementation</h3>
62
63<p>
64 The Bazel system is implemented as a long-lived server process.
65 This allows it to perform many optimizations not possible with a
66 batch-oriented implementation, such as caching of BUILD files,
67 dependency graphs, and other metadata from one build to the
68 next. This improves the speed of incremental builds, and allows
69 different commands, such as <code>build</code>
70 and <code>query</code> to share the same cache of loaded packages,
71 making queries very fast.
72</p>
73<p>
74 When you run <code>bazel</code>, you're running the client. The
Shreya Bhattaraic5724bc2016-12-19 22:51:25 +000075 client finds the server based on the output base, which by default is
76 determined by the path of the base workspace directory and your
77 userid, so if you build in multiple workspaces, you'll have multiple
78 output bases and thus multiple Bazel server processes. Multiple
79 users on the same workstation can build concurrently in the same
80 workspace because their output bases will differ (different userids).
81 If the client cannot find a running server instance, it starts a new
82 one. The server process will stop after a period of inactivity (3 hours,
David Chen8fe82a32016-08-24 10:55:41 +000083 by default).
84</p>
85<p>
86 For the most part, the fact that there is a server running is
87 invisible to the user, but sometimes it helps to bear this in mind.
88 For example, if you're running scripts that perform a lot of
89 automated builds in different directories, it's important to ensure
90 that you don't accumulate a lot of idle servers; you can do this by
91 explicitly shutting them down when you're finished with them, or by
92 specifying a short timeout period.
93</p>
94<p>
95 The name of a Bazel server process appears in the output of <code>ps
96 x</code> or <code>ps -e f</code> as
97 <code>bazel(<i>dirname</i>)</code>, where <i>dirname</i> is the
98 basename of the directory enclosing the root your workspace directory.
99 For example:
100</p>
101<pre>
102 % ps -e f
103 16143 ? Sl 3:00 bazel(src-jrluser2) -server -Djava.library.path=...
104</pre>
105<p>
106 This makes it easier to find out which server process belongs to a
107 given workspace. (Beware that with certain other options
108 to <code>ps</code>, Bazel server processes may be named just
109 <code>java</code>.) Bazel servers can be stopped using
110 the <a href='#shutdown'>shutdown</a> command.
111</p>
112<p>
113 You can also run Bazel in batch mode using the <code>--batch</code>
114 startup flag. This will immediately shut down the process after the
115 command (build, test, etc.) has finished and not keep a server process
116 around.
117</p>
118
119<p>
120 When running <code>bazel</code>, the client first checks that the
121 server is the appropriate version; if not, the server is stopped and
122 a new one started. This ensures that the use of a long-running
123 server process doesn't interfere with proper versioning.
124</p>
125
126<h3 id='bazelrc'><code>.bazelrc</code>, the Bazel configuration file,
127the <code class='flag'>--bazelrc=<var>file</var></code> option, and the
128<code class='flag'>--config=<var>value</var></code> option</h3>
129
130<p>
131 Bazel accepts many options. Typically, some of these are varied
132 frequently (e.g. <code class='flag'>--subcommands</code>) while others stay the
133 same across several builds (e.g. <code class='flag'>--package_path</code>).
134 To avoid having to specify these constant options every time you do
135 a build or run some other Bazel command, Bazel allows you to
136 specify options in a configuration file.
137</p>
138<p>
139 Bazel looks for an optional configuration file in the location
140 specified by the <code class='flag'>--bazelrc=<var>file</var></code> option. If
141 this option is not specified then, by default, Bazel looks for the
142 file called <code>.bazelrc</code> in one of two directories: first,
143 in your base workspace directory, then in your home directory. If
144 it finds a file in the first (workspace-specific) location, it will
145 not look at the second (global) location.
146</p>
147<p>
148 The <code class='flag'>--bazelrc=<var>file</var></code> option must
149 appear <em>before</em> the command name (e.g. <code>build</code>).
150</p>
151<p>
152 The option <code class='flag'>--bazelrc=/dev/null</code> effectively disables the
153 use of a configuration file. We strongly recommend that you use
154 this option when performing release builds, or automated tests that
155 invoke Bazel.
156</p>
157
158<p>
159 Aside from the configuration file described above, Bazel also looks
160 for a master configuration file next to the binary, in the workspace
161 at <code>tools/bazel.rc</code> or system-wide at
162 <code>/etc/bazel.bazelrc</code>. These files are here to support
163 installation-wide options or options shared between users.
164</p>
165<p>
166 Like all UNIX "rc" files, the <code>.bazelrc</code> file is a text
167 file with a line-based grammar. Lines starting <code>#</code> are
168 considered comments and are ignored, as are blank lines. Each line
169 contains a sequence of words, which are tokenized according to the
170 same rules as the Bourne shell.
171 The first word on each line is the name of a Bazel command, such
172 as <code>build</code> or <code>query</code>. The remaining words
173 are the default options that apply to that command.
174 More than one line may be used for a command; the options are combined
175 as if they had appeared on a single line.
176 (Users of CVS, another tool with a "Swiss army knife" command-line
177 interface, will find the syntax familiar to that of <code>.cvsrc</code>.)
178</p>
179<p>
180 Startup options may be specified in the
181 <code>.bazelrc</code> file using the command <code>startup</code>.
182 These options are described in the interactive help
183 at <code>bazel help startup_options</code>.
184</p>
185<p>
186 Options specified in the command line always take precedence over
187 those from a configuration file. In configuration files, lines for a more specific command take
188 precedence over lines for a less specific command (e.g. the 'test' command inherits all the
189 options from the 'build' command, so a 'test --foo=bar' line takes precedence over a
190 'build --foo=baz' line, regardless of which configuration files these two lines are in) and lines
191 equally specific for which command they apply have precedence based on the configuration file they
192 are in, with the user-specific configuration file taking precedence over the master one.
193</p>
194<p>
195 Options may include words other than flags, such as the names of
196 build targets, etc; these are always prepended to the explicit
197 argument list provided on the command-line, if any.
198</p>
199<p>
200 Common command options may be specified in the
201 <code>.bazelrc</code> file using the command <code>common</code>.
202</p>
203<p>
204 In addition, commands may have <code>:name</code> suffixes. These
205 options are ignored by default, but can be pulled in through the
206 <code>--config=<var>name</var></code> option, either on the command line or in
207 a <code>.bazelrc</code> file. The intention is that these bundle command line
208 options that are commonly used together, for example
209 <code>--config=memcheck</code>.
210</p>
211<p>
212 Note that some config sections are defined in the master bazelrc file.
213 To avoid conflicts, user-defined sections
214 should start with the '_' (underscore) character.
215</p>
216<p>
217 The command named <code>import</code> is special: if Bazel encounters such
218 a line in a <code>.bazelrc</code> file, it parses the contents of the file
219 referenced by the import statement, too. Options specified in an imported file
220 take precedence over ones specified before the import statement, options
221 specified after the import statement take precedence over the ones in the
222 imported file, and options in files imported later take precedence over files
223 imported earlier.
224</p>
225<p>
226 Here's an example <code>~/.bazelrc</code> file:
227</p>
228<pre>
229 # Bob's Bazel option defaults
230
231 startup --batch --host_jvm_args=-XX:-UseParallelGC
232 import /home/bobs_project/bazelrc
233 build --show_timestamps --keep_going --jobs 600
234 build --color=yes
235 query --keep_going
236
237 build:memcheck --strip=never --test_timeout=3600
238</pre>
239
240<h2 id='build'>Building programs with Bazel</h2>
241<h3>The <code>build</code> command</h3>
242
243<p>
244 The most important function of Bazel is, of course, building code. Type
245 <code>bazel build</code> followed by the name of the
246 <a href="#target-patterns">target</a> you wish to build. Here's a typical
247 session:
248</p>
249<pre>
250 % bazel build //foo
251 ____Loading package: foo
252 ____Loading package: bar
253 ____Loading package: baz
254 ____Loading complete. Analyzing...
255 ____Building 1 target...
256 ____[0 / 3] Executing Genrule //bar:helper_rule
257 ____[1 / 3] Executing Genrule //baz:another_helper_rule
258 ____[2 / 3] Building foo/foo.bin
259 Target //foo:foo up-to-date:
260 bazel-bin/foo/foo.bin
261 bazel-bin/foo/foo
262 ____Elapsed time: 9.905s
263</pre>
264<p>
265 Bazel prints the progress messages as it loads all the
266 packages in the transitive closure of dependencies of the requested
267 target, then analyzes them for correctness and to create the build actions,
268 finally executing the compilers and other tools of the build.
269</p>
270<p>
271 Bazel prints progress messages during
272 the <a href='#execution-phase'>execution phase</a> of the build, showing the
273 current build step (compiler, linker, etc.) that is being started,
274 and the number of completed over total number of build actions. As the
275 build starts the number of total actions will often increase as Bazel
276 discovers the entire action graph, but the number will usually stabilize
277 within a few seconds.
278</p>
279<p>
280 At the end of the build Bazel
281 prints which targets were requested, whether or not they were
282 successfully built, and if so, where the output files can be found.
283 Scripts that run builds can reliably parse this output; see <a
284 href='#flag--show_result'><code class='flag'>--show_result</code></a> for more
285 details.
286</p>
287<p>
288 Typing the same command again:
289</p>
290<pre>
291 % bazel build //foo
292 ____Loading...
293 ____Found 1 target...
294 ____Building complete.
295 Target //foo:foo up-to-date:
296 bazel-bin/foo/foo.bin
297 bazel-bin/foo/foo
298 ____Elapsed time: 0.280s
299</pre>
300<p>
301 we see a "null" build: in this case, there are no packages to
302 re-load, since nothing has changed, and no build steps to execute.
303 (If something had changed in "foo" or some of its dependencies, resulting in the
304 reexecution of some build actions, we would call it an "incremental" build, not a
305 "null" build.)
306</p>
307
308<p>
309 Before you can start a build, you will need a Bazel workspace. This is
310 simply a directory tree that contains all the source files needed to build
311 your application.
312 Bazel allows you to perform a build from a completely read-only volume.
313</p>
314
315<h4 id='flag--package_path'>Setting up a <code class='flag'>--package_path</code></h4>
316<p>
317 Bazel finds its packages by searching the package path. This is a colon
318 separated ordered list of bazel directories, each being the root of a
319 partial source tree.
320</p>
321
322<p>
323 <i>To specify a custom package path</i> using the
324 <code class='flag'>--package_path</code> option:
325</p>
326<pre>
327 % bazel build --package_path %workspace%:/some/other/root
328</pre>
329<p>
330Package path elements may be specified in three formats:
331</p>
332<ol>
333 <li>
334 If the first character is <code>/</code>, the path is absolute.
335 </li>
336 <li>
337 If the path starts with <code>%workspace%</code>, the path is taken relative
338 to the nearest enclosing bazel directory.<br>
339 For instance, if your working directory
340 is <code>/home/bob/clients/bob_client/bazel/foo</code>, then the
341 string <code>%workspace%</code> in the package-path is expanded
342 to <code>/home/bob/clients/bob_client/bazel</code>.
343 </li>
344 <li>
345 Anything else is taken relative to the working directory.<br> This is usually not what you mean to do,
346 and may behave unexpectedly if you use Bazel from directories below the bazel workspace.
347 For instance, if you use the package-path element <code>.</code>,
348 and then cd into the directory
349 <code>/home/bob/clients/bob_client/bazel/foo</code>, packages
350 will be resolved from the
351 <code>/home/bob/clients/bob_client/bazel/foo</code> directory.
352 </li>
353</ol>
354<p>
355 If you use a non-default package path, we recommend that you specify
356 it in your <a href='#bazelrc'>Bazel configuration file</a> for
357 convenience.
358</p>
359<p>
360 <i>Bazel doesn't require any packages to be in the
361 current directory</i>, so you can do a build from an empty bazel
362 workspace if all the necessary packages can be found somewhere else
363 on the package path.
364</p>
365<p>
366 <i>Example</i>: Building from an empty client
367</p>
368<pre>
369 % mkdir -p foo/bazel
370 % cd foo/bazel
371 % bazel build --package_path /some/other/path //foo
372</pre>
373<h3 id='target-patterns'>Specifying targets to build</h3>
374<p>
375 Bazel allows a number of ways to specify the targets to be built.
376 Collectively, these are known as <i>target patterns</i>.
377 The on-line help displays a summary of supported patterns:
378</p>
379<pre>
380% bazel help target-syntax
381
382Target pattern syntax
383=====================
384
385The BUILD file label syntax is used to specify a single target. Target
386patterns generalize this syntax to sets of targets, and also support
387working-directory-relative forms, recursion, subtraction and filtering.
388Examples:
389
390Specifying a single target:
391
392 //foo/bar:wiz The single target '//foo/bar:wiz'.
393 foo/bar/wiz Equivalent to:
394 '//foo/bar/wiz:wiz' if foo/bar/wiz is a package,
395 '//foo/bar:wiz' if foo/bar is a package,
396 '//foo:bar/wiz' otherwise.
397 //foo/bar Equivalent to '//foo/bar:bar'.
398
399Specifying all rules in a package:
400
401 //foo/bar:all Matches all rules in package 'foo/bar'.
402
403Specifying all rules recursively beneath a package:
404
405 //foo/...:all Matches all rules in all packages beneath directory 'foo'.
406 //foo/... (ditto)
407
408 By default, directory symlinks are followed when performing this recursive traversal, except
409 those that point to under the output base (for example, the convenience symlinks that are created
410 in the root directory of the workspace) But we understand that your workspace may intentionally
411 contain directories with unusual symlink structures that you don't want consumed. As such, if a
412 directory has a file named
413 'DONT_FOLLOW_SYMLINKS_WHEN_TRAVERSING_THIS_DIRECTORY_VIA_A_RECURSIVE_TARGET_PATTERN' then symlinks
414 in that directory won't be followed when evaluating recursive target patterns.
415
416Working-directory relative forms: (assume cwd = 'workspace/foo')
417
418 Target patterns which do not begin with '//' are taken relative to
419 the working directory. Patterns which begin with '//' are always
420 absolute.
421
422 ...:all Equivalent to '//foo/...:all'.
423 ... (ditto)
424
425 bar/...:all Equivalent to '//foo/bar/...:all'.
426 bar/... (ditto)
427
428 bar:wiz Equivalent to '//foo/bar:wiz'.
429 :foo Equivalent to '//foo:foo'.
430
431 bar Equivalent to '//foo/bar:bar'.
432 foo/bar Equivalent to '//foo/foo/bar:bar'.
433
434 bar:all Equivalent to '//foo/bar:all'.
435 :all Equivalent to '//foo:all'.
436
437Summary of target wildcards:
438
439 :all, Match all rules in the specified packages.
440 :*, :all-targets Match all targets (rules and files) in the specified
441 packages, including ones not built by default, such
442 as _deploy.jar files.
443
444Subtractive patterns:
445
446 Target patterns may be preceded by '-', meaning they should be
447 subtracted from the set of targets accumulated by preceding
448 patterns. (Note that this means order matters.) For example:
449
450 % bazel build -- foo/... -foo/contrib/...
451
452 builds everything in 'foo', except 'contrib'. In case a target not
453 under 'contrib' depends on something under 'contrib' though, in order to
454 build the former bazel has to build the latter too. As usual, the '--' is
455 required to prevent '-f' from being interpreted as an option.
456</pre>
457<p>
458 Whereas <a href="build-ref.html#labels">labels</a> are used
459 to specify individual targets, e.g. for declaring dependencies in
460 BUILD files, Bazel's target patterns are a syntax for specifying
461 multiple targets: they are a generalization of the label syntax
462 for <i>sets</i> of targets, using wildcards. In the simplest case,
463 any valid label is also a valid target pattern, identifying a set of
464 exactly one target.
465</p>
466<p>
467 <code>foo/...</code> is a wildcard over <em>packages</em>,
468 indicating all packages recursively beneath
469 directory <code>foo</code> (for all roots of the package
470 path). <code>:all</code> is a wildcard
471 over <em>targets</em>, matching all rules within a package. These two may be
472 combined, as in <code>foo/...:all</code>, and when both wildcards
473 are used, this may be abbreviated to <code>foo/...</code>.
474</p>
475<p>
476 In addition, <code>:*</code> (or <code>:all-targets</code>) is a
477 wildcard that matches <em>every target</em> in the matched packages,
478 including files that aren't normally built by any rule, such
479 as <code>_deploy.jar</code> files associated
480 with <code>java_binary</code> rules.
481</p>
482<p>
483 This implies that <code>:*</code> denotes a <em>superset</em>
484 of <code>:all</code>; while potentially confusing, this syntax does
485 allow the familiar <code>:all</code> wildcard to be used for
486 typical builds, where building targets like the <code>_deploy.jar</code>
487 is not desired.
488</p>
489<p>
490 In addition, Bazel allows a slash to be used instead of the colon
491 required by the label syntax; this is often convenient when using
492 Bash filename expansion. For example, <code>foo/bar/wiz</code> is
493 equivalent to <code>//foo/bar:wiz</code> (if there is a
494 package <code>foo/bar</code>) or to <code>//foo:bar/wiz</code> (if
495 there is a package <code>foo</code>).
496</p>
497<p>
498 Many Bazel commands accept a list of target patterns as arguments,
499 and they all honor the prefix negation operator `<code>-</code>'.
500 This can be used to subtract a set of targets from the set specified
501 by the preceding arguments. (Note that this means order matters.)
502 For example,
503</p>
504<pre>
505 bazel build foo/... bar/...
506</pre>
507<p>
508 means "build all
509 targets beneath <code>foo</code> <i>and</i> all targets
510 beneath <code>bar</code>", whereas
511</p>
512<pre>
513 bazel build -- foo/... -foo/bar/...
514</pre>
515<p>
516 means "build all targets beneath <code>foo</code> <i>except</i>
517 those beneath <code>foo/bar</code>".
518
519 (The <code>--</code> argument is required to prevent the subsequent
520 arguments starting with <code>-</code> from being interpreted as
521 additional options.)
522</p>
523<p>
524 It's important to point out though that subtracting targets this way will not
525 guarantee that they are not built, since they may be dependencies of targets
526 that weren't subtracted. For example, if there were a target
527 <code>//foo:all-apis</code> that among others depended on
528 <code>//foo/bar:api</code>, then the latter would be built as part of
529 building the former.
530</p>
531<p>
532 Targets with <code>tags=["manual"]</code> will not be included in wildcard target patterns (...,
533 :*, :all, etc). You should specify such test targets with explicit target patterns on the command
534 line if you want Bazel to build/test them.
535</p>
536
537<h3 id='fetch'>Fetching external dependencies</h3>
538
539<p>
540 By default, Bazel will download and symlink external dependencies during the
541 build. However, this can be undesirable, either because you'd like to know
542 when new external dependendencies are added or because you'd like to
543 "prefetch" dependencies (say, before a flight where you'll be offline). If you
544 would like to prevent new dependencies from being added during builds, you
545 can specify the <code>--fetch=false</code> flag. Note that this flag only
546 applies to repository rules that do not point to a directory in the local
547 file system. Changes, for example, to <code>local_repository</code>,
548 <code>new_local_repository</code> and Android SDK and NDK repository rules
549 will always take effect regardless of the value <code>--fetch</code> .
550</p>
551
552<p>
553 If you disallow fetching during builds and Bazel finds new external
554 dependencies, your build will fail.
555</p>
556
557<p>
558 You can manually fetch dependencies by running <code>bazel fetch</code>. If
559 you disallow during-build fetching, you'll need to run <code>bazel
560 fetch</code>:
561 <ol>
562 <li>Before you build for the first time.
563 <li>After you add a new external dependency.
564 </ol>
565 Once it has been run, you should not need to run it again until the WORKSPACE
566 file changes.
567</p>
568
569<p>
570 <code>fetch</code> takes a list of targets to fetch dependencies for. For
571 example, this would fetch dependencies needed to build <code>//foo:bar</code>
572 and <code>//bar:baz</code>:
573<pre>
574$ bazel fetch //foo:bar //bar:baz
575</pre>
576</p>
577
578<p>
579 To fetch all external dependencies for a workspace, run:
580<pre>
581$ bazel fetch //...
582</pre>
583</p>
584
585<p>
586 You do not need to run bazel fetch at all if you have all of the tools you are
587 using (from library jars to the JDK itself) under your workspace root.
588 However, if you're using anything outside of the workspace directory then you
589 will need to run <code>bazel fetch</code> before running
590 <code>bazel build</code>.
591</p>
592
593<h3 id='configurations'>Build configurations and cross-compilation</h3>
594
595<p>
596 All the inputs that specify the behavior and result of a given
597 build can be divided into two distinct categories.
598 The first kind is the intrinsic information stored in the BUILD
599 files of your project: the build rule, the values of its attributes,
600 and the complete set of its transitive dependencies.
601 The second kind is the external or environmental data, supplied by
602 the user or by the build tool: the choice of target architecture,
603 compilation and linking options, and other toolchain configuration
604 options. We refer to a complete set of environmental data as
605 a <b>configuration</b>.
606</p>
607<p>
608 In any given build, there may be more than one configuration.
609 Consider a cross-compile, in which you build
610 a <code>//foo:bin</code> executable for a 64-bit architecture,
611 but your workstation is a 32-bit machine. Clearly, the build
612 will require building <code>//foo:bin</code> using a toolchain
613 capable of creating 64-bit executables, but the build system must
614 also build various tools used during the build itself&mdash;for example
615 tools that are built from source, then subsequently used in, say, a
616 genrule&mdash;and these must be built to run on your workstation.
617 Thus we can identify two configurations: the <b>host
618 configuration</b>, which is used for building tools that run during
619 the build, and the <b>target configuration</b> (or <i>request
620 configuration</i>, but we say "target configuration" more often even
621 though that word already has many meanings), which is
622 used for building the binary you ultimately requested.
623</p>
624<p>
625 Typically, there are many libraries that are prerequisites of both
626 the requested build target (<code>//foo:bin</code>) and one or more of
627 the host tools, for example some base libraries. Such libraries must be built
628 twice, once for the host configuration, and once for the target
629 configuration.<br/>
630 Bazel takes care of ensuring that both variants are built, and that
631 the derived files are kept separate to avoid interference; usually
632 such targets can be built concurrently, since they are independent
633 of each other. If you see progress messages indicating that a given
634 target is being built twice, this is most likely the explanation.
635</p>
636<p>
637 Bazel uses one of two ways to select the host configuration, based
638 on the <code class='flag'>--distinct_host_configuration</code> option. This
639 boolean option is somewhat subtle, and the setting may improve (or
640 worsen) the speed of your builds.
641</p>
642
643<h4><code class='flag'>--distinct_host_configuration=false</code></h4>
644<p>
645 When this option is false, the host and
646 request configurations are identical: all tools required during the
647 build will be built in exactly the same way as target programs.
648 This setting means that no libraries need to be built twice during a
649 single build, so it keeps builds short.
650 However, it does mean that any change to your request configuration
651 also affects your host configuration, causing all the tools to be
652 rebuilt, and then anything that depends on the tool output to be
653 rebuilt too. Thus, for example, simply changing a linker option
654 between builds might cause all tools to be re-linked, and then all
655 actions using them reexecuted, and so on, resulting in a very large rebuild.
656 Also, please note: if your host architecture is not capable of
657 running your target binaries, your build will not work.
658</p>
659<p>
660 If you frequently make changes to your request configuration, such
661 as alternating between <code>-c opt</code> and <code>-c dbg</code>
662 builds, or between simple- and cross-compilation, we do not
663 recommend this option, as you will typically rebuild the majority of
664 your codebase each time you switch.
665</p>
666
667<h4><code class='flag'>--distinct_host_configuration=true</code> <i>(default)</i></h4>
668<p>
669 If this option is true, then instead of using the same configuration
670 for the host and request, a completely distinct host configuration
671 is used. The host configuration is derived from the target
672 configuration as follows:
673</p>
674<ul>
675 <li>Use the same version of Crosstool
676 (<code class='flag'>--crosstool_top</code>) as specified in the request
677 configuration, unless <code class='flag'>--host_crosstool_top</code> is
678 specified.
679 </li>
680 <li>
681 Use the value of <code class="flag">--host_cpu</code> for
682 <code class='flag'>--cpu</code>
683
684 (default: <code>k8</code>).
685 </li>
686 <li>Use the same values of these options as specified in the request
687 configuration:
688 <code class='flag'>--compiler</code>,
David Chen8fe82a32016-08-24 10:55:41 +0000689 <code class='flag'>--use_ijars</code>,
690 <code class='flag'>--java_toolchain</code>,
691 If <code class='flag'>--host_crosstool_top</code> is used, then the value of
692 <code class='flag'>--host_cpu</code> is used to look up a
693 <code>default_toolchain</code> in the Crosstool
694 (ignoring <code class='flag'>--compiler</code>) for the host configuration.
695 </li>
696 <li>Use optimized builds for C++ code (<code>-c opt</code>).
697 </li>
698 <li>Generate no debugging information (<code class='flag'>--copt=-g0</code>).
699 </li>
700 <li>Strip debug information from executables and shared libraries
701 (<code class='flag'>--strip=always</code>).
702 </li>
703 <li>Place all derived files in a special location, distinct from
704 that used by any possible request configuration.
705 </li>
706 <li>Suppress stamping of binaries with build data
707 (see <code class='flag'>--embed_*</code> options).
708 </li>
709 <li>All other values remain at their defaults.
710 </li>
711</ul>
712<p>
713 There are many reasons why it might be preferable to select a
714 distinct host configuration from the request configuration.
715 Some are too esoteric to mention here, but two of them are worth
716 pointing out.
717</p>
718<p>
719 Firstly, by using stripped, optimized binaries, you reduce the time
720 spent linking and executing the tools, the disk space occupied by
721 the tools, and the network I/O time in distributed builds.
722</p>
723<p>
724 Secondly, by decoupling the host and request configurations in all
725 builds, you avoid very expensive rebuilds that would result from
726 minor changes to the request configuration (such as changing a linker options
727 does), as described earlier.
728</p>
729<p>
730 That said, for certain builds, this option may be a hindrance. In
731 particular, builds in which changes of configuration are infrequent
732 (especially certain Java builds), and builds where the amount of code that
733 must be built in both host and target configurations is large, may
734 not benefit.
735</p>
736
737<h3 id='correctness'>Correct incremental rebuilds</h3>
738
739<p>
740 One of the primary goals of the Bazel project is to ensure correct
741 incremental rebuilds. Previous build tools, especially those based
742 on Make, make several unsound assumptions in their implementation of
743 incremental builds.
744</p>
745<p>
746 Firstly, that timestamps of files increase monotonically. While
747 this is the typical case, it is very easy to fall afoul of this
748 assumption; syncing to an earlier revision of a file causes that file's
749 modification time to decrease; Make-based systems will not rebuild.
750</p>
751<p>
752 More generally, while Make detects changes to files, it does
753 not detect changes to commands. If you alter the options passed to
754 the compiler in a given build step, Make will not re-run the
755 compiler, and it is necessary to manually discard the invalid
756 outputs of the previous build using <code>make clean</code>.
757</p>
758<p>
759 Also, Make is not robust against the unsuccessful termination of one
760 of its subprocesses after that subprocess has started writing to
761 its output file. While the current execution of Make will fail, the
762 subsequent invocation of Make will blindly assume that the truncated
763 output file is valid (because it is newer than its inputs), and it
764 will not be rebuilt. Similarly, if the Make process is killed, a
765 similar situation can occur.
766</p>
767<p>
768 Bazel avoids these assumptions, and others. Bazel maintains a database
769 of all work previously done, and will only omit a build step if it
770 finds that the set of input files (and their timestamps) to that
771 build step, and the compilation command for that build step, exactly
772 match one in the database, and, that the set of output files (and
773 their timestamps) for the database entry exactly match the
774 timestamps of the files on disk. Any change to the input files or
775 output files, or to the command itself, will cause re-execution of
776 the build step.
777</p>
778<p>
779 The benefit to users of correct incremental builds is: less time
780 wasted due to confusion. (Also, less time spent waiting for
781 rebuilds caused by use of <code>make clean</code>, whether necessary
782 or pre-emptive.)
783</p>
784
785<h4>Build consistency and incremental builds</h4>
786<p>
787 Formally, we define the state of a build as <i>consistent</i> when
788 all the expected output files exist, and their contents are correct,
789 as specified by the steps or rules required to create them. When
790 you edit a source file, the state of the build is said to
791 be <i>inconsistent</i>, and remains inconsistent until you next run
792 the build tool to successful completion. We describe this situation
793 as <i>unstable inconsistency</i>, because it is only temporary, and
794 consistency is restored by running the build tool.
795</p>
796<p>
797 There is another kind of inconsistency that is pernicious: <i>stable
798 inconsistency</i>. If the build reaches a stable inconsistent
799 state, then repeated successful invocation of the build tool does
800 not restore consistency: the build has gotten "stuck", and the
801 outputs remain incorrect. Stable inconsistent states are the main
802 reason why users of Make (and other build tools) type <code>make
803 clean</code>. Discovering that the build tool has failed in this
804 manner (and then recovering from it) can be time consuming and very
805 frustrating.
806</p>
807<p>
808 Conceptually, the simplest way to achieve a consistent build is to
809 throw away all the previous build outputs and start again: make
810 every build a clean build. This approach is obviously too
811 time-consuming to be practical (except perhaps for release
812 engineers), and therefore to be useful, the build tool must be able
813 to perform incremental builds without compromising consistency.
814</p>
815<p>
816 Correct incremental dependency analysis is hard, and as described
817 above, many other build tools do a poor job of avoiding stable
818 inconsistent states during incremental builds. In contrast, Bazel
819 offers the following guarantee: after a successful invocation of the
820 build tool during which you made no edits, the build will be in a
821 consistent state. (If you edit your source files during a build,
822 Bazel makes no guarantee about the consistency of the result of the
823 current build. But it does guarantee that the results of
824 the <i>next</i> build will restore consistency.)
825</p>
826<p>
827 As with all guarantees, there comes some fine print: there are some
828 known ways of getting into a stable inconsistent state with Bazel.
829 We won't guarantee to investigate such problems arising from deliberate
830 attempts to find bugs in the incremental dependency analysis, but we
831 will investigate and do our best to fix all stable inconsistent
832 states arising from normal or "reasonable" use of the build tool.
833</p>
834<p>
835 If you ever detect a stable inconsistent state with Bazel, please report a bug.
836
837</p>
838
839<h4 id='sandboxing'>Sandboxed execution</h4>
840<p>
841 Bazel uses sandboxes to guarantee that actions run hermetically<sup>1</sup> and correctly.
842 Bazel runs <i>Spawn</i>s (loosely speaking: actions) in sandboxes that only contain the minimal
843 set of files the tool requires to do its job. Currently sandboxing works on Linux 3.12 or newer
Yue Ganfd0e41a2016-11-24 10:58:17 +0000844 with the <code>CONFIG_USER_NS</code> option enabled, and also on Mac OS 10.11 for newer.
David Chen8fe82a32016-08-24 10:55:41 +0000845</p>
846<p>
847 Bazel will print a warning if your system does not support sandboxing to alert you to the fact
848 that builds are not guaranteed to be hermetic and might affect the host system in unknown ways.
849 To disable this warning you can pass the <code>--ignore_unsupported_sandboxing</code> flag to
850 Bazel.
851</p>
852
853<p>
854 On some platforms such as <a href="https://cloud.google.com/container-engine/">Google Container
855 Engine</a> cluster nodes or Debian, user namespaces are deactivated by default due to security
856 concerns. This can be checked by looking at the file
857 <code>/proc/sys/kernel/unprivileged_userns_clone</code>: if it exists and contains a 0, then
858 user namespaces can be activated with <code>sudo sysctl kernel.unprivileged_userns_clone=1</code>.
859</p>
860<p>
861 In some cases, the Bazel sandbox fails to execute rules because of the system setup. The symptom
862 is generally a failure that output a message similar to
863 <code>namespace-sandbox.c:633: execvp(argv[0], argv): No such file or directory</code>. In that
864 case, try to deactivate the sandbox for genrules with <code>--genrule_strategy=standalone</code>
865 and for other rules with <code>--spawn_strategy=standalone</code>. Also please report a bug on our
866 issue tracker and mention which Linux distribution you're using so that we can investigate and
867 provide a fix in a subsequent release.
868</p>
869
870<p>
871 <sup>1</sup>: Hermeticity means that the action only uses its declared input files and no other
872 files in the filesystem, and it only produces its declared output files.
873</p>
874
875<h3 id='clean'>Deleting the outputs of a build</h3>
876
877<h4>The <code>clean</code> command</h4>
878
879<p>
880 Bazel has a <code>clean</code> command, analogous to that of Make.
881 It deletes the output directories for all build configurations performed
882 by this Bazel instance, or the entire working tree created by this
883 Bazel instance, and resets internal caches. If executed without any
884 command-line options, then the output directory for all configurations
885 will be cleaned.
886</p>
887
888<p>Recall that each Bazel instance is associated with a single workspace, thus the
889 <code>clean</code> command will delete all outputs from all builds you've done
890 with that Bazel instance in that workspace.
891</p>
892<p>
893 To completely remove the entire working tree created by a Bazel
894 instance, you can specify the <code class='flag'>--expunge</code> option. When
895 executed with <code class='flag'>--expunge</code>, the clean command simply
896 removes the entire output base tree which, in addition to the build
897 output, contains all temp files created by Bazel. It also
898 stops the Bazel server after the clean, equivalent to the <a
899 href='#shutdown'><code>shutdown</code></a> command. For example, to
900 clean up all disk and memory traces of a Bazel instance, you could
901 specify:
902</p>
903<pre>
904 % bazel clean --expunge
905</pre>
906<p>
907 Alternatively, you can expunge in the background by using
908 <code class='flag'>--expunge_async</code>. It is safe to invoke a Bazel command
909 in the same client while the asynchronous expunge continues to run.
910 Note, however, that this may introduce IO contention.
911</p>
912
913<p>
914 The <code>clean</code> command is provided primarily as a means of
915 reclaiming disk space for workspaces that are no longer needed.
916 However, we recognize that Bazel's incremental rebuilds might not be
917 perfect; <code>clean</code> may be used to recover a consistent
918 state when problems arise.
919</p>
920<p>
921 Bazel's design is such that these problems are fixable; we consider
922 such bugs a high priority, and will do our best fix them. If you
923 ever find an incorrect incremental build, please file a bug report.
924 We encourage developers to get out of the habit of
925 using <code>clean</code> and into that of reporting bugs in the
926 tools.
927</p>
928
929<h3 id='phases'>Phases of a build</h3>
930
931<p>
932 In Bazel, a build occurs in three distinct phases; as a user,
933 understanding the difference between them provides insight into the
934 options which control a build (see below).
935</p>
936
937<h4 id='loading-phase'>Loading phase</h4>
938<p>
939 The first is <b>loading</b> during which all the necessary BUILD
940 files for the initial targets, and their transitive closure of
941 dependencies, are loaded, parsed, evaluated and cached.
942</p>
943<p>
944 For the first build after a Bazel server is started, the loading
945 phase typically takes many seconds as many BUILD files are loaded
946 from the file system. In subsequent builds, especially if no BUILD
947 files have changed, loading occurs very quickly.
948</p>
949<p>
950 Errors reported during this phase include: package not found, target
951 not found, lexical and grammatical errors in a BUILD file,
952 and evaluation errors.
953</p>
954
955<h4 id='analysis-phase'>Analysis phase</h4>
956<p>
957 The second phase, <b>analysis</b>, involves the semantic analysis
958 and validation of each build rule, the construction of a build
959 dependency graph, and the determination of exactly what work is to
960 be done in each step of the build.
961</p>
962<p>
963 Like loading, analysis also takes several seconds when computed in
964 its entirety. However, Bazel caches the dependency graph from
965 one build to the next and only reanalyzes what it has to, which can
966 make incremental builds extremely fast in the case where the
967 packages haven't changed since the previous build.
968</p>
969<p>
970 Errors reported at this stage include: inappropriate dependencies,
971 invalid inputs to a rule, and all rule-specific error messages.
972</p>
973<p>
974 The loading and analysis phases are fast because
975 Bazel avoids unnecessary file I/O at this stage, reading only BUILD
976 files in order to determine the work to be done. This is by design,
977 and makes Bazel a good foundation for analysis tools, such as
978 Bazel's <a href='#query'>query</a> command, which is implemented
979 atop the loading phase.
980</p>
981
982<h4 id='execution-phase'>Execution phase</h4>
983<p>
984 The third and final phase of the build is <b>execution</b>. This
985 phase ensures that the outputs of each step in the build are
986 consistent with its inputs, re-running compilation/linking/etc. tools as
987 necessary. This step is where the build spends the majority of
988 its time, ranging from a few seconds to over an hour for a large
989 build. Errors reported during this phase include: missing source
990 files, errors in a tool executed by some build action, or failure of a tool to
991 produce the expected set of outputs.
992</p>
993
994
995<h2>Options</h2>
996
997<p>
998 The following sections describe the options available during a
999 build. When <code class='flag'>--long</code> is used on a help command, the on-line
1000 help messages provide summary information about the meaning, type and
1001 default value for each option.
1002</p>
1003
1004<p>
1005 Most options can only be specified once. When specified multiple times, the
1006 last instance wins. Options that can be specified multiple times are
1007 identified in the on-line help with the text 'may be used multiple times'.
1008</p>
1009
1010<h3>Options that affect how packages are located</h3>
1011
1012<p>
1013 See also the <a href='#flag--show_package_location'><code class='flag'>--show_package_location</code></a>
1014 option.
1015</p>
1016
1017<h4 id='flag--package_path'><code class='flag'>--package_path</code></h4>
1018<p>
1019 This option specifies the set of directories that are searched to
1020 find the BUILD file for a given package.
1021
1022</p>
1023
1024<h4 id='flag--deleted_packages'><code class='flag'>--deleted_packages</code></h4>
1025<p>
1026 This option specifies a comma-separated list of packages which Bazel
1027 should consider deleted, and not attempt to load from any directory
1028 on the package path. This can be used to simulate the deletion of packages without
1029 actually deleting them.
1030</p>
1031
1032<h3 id='checking-options'>Error checking options</h3>
1033<p>
1034 These options control Bazel's error-checking and/or warnings.
1035</p>
1036
1037<h4 id='flag--check_constraint'><code class='flag'>--check_constraint <var>constraint</var></code></h4>
1038<p>
1039 This option takes an argument that specifies which constraint
1040 should be checked.
1041</p>
1042<p>
1043 Bazel performs special checks on each rule that is annotated with the
1044 given constraint.
1045</p>
1046<p>
1047 The supported constraints and their checks are as follows:
1048</p>
1049<ul>
1050
1051 <li><code>public</code>: Verify that all java_libraries marked with
1052 <code>constraints = ['public']</code> only depend on java_libraries
1053 that are marked as <code>constraints = ['public']</code> too. If bazel
1054 finds a dependency that does not conform to this rule, bazel will issue
1055 an error.
1056 </li>
1057</ul>
1058
1059<h4 id='flag--check_visibility'><code class='flag'>--[no]check_visibility</code></h4>
1060<p>
1061 If this option is set to false, visibility checks are demoted to warnings.
1062 The default value of this option is true, so that by default, visibility
1063 checking is done.
1064
1065</p>
1066
1067<h4 id='flag--experimental_action_listener'>
1068 <code class='flag'>--experimental_action_listener=<var>label</var></code>
1069</h4>
1070<p>
1071 The <code>experimental_action_listener</code> option instructs Bazel to use
1072 details from the <a href="be/extra-actions.html#action_listener"
1073 ><code>action_listener</code></a> rule specified by <var>label</var> to
1074 insert <a href="be/extra-actions.html#extra_action"
1075 ><code>extra_actions</code></a> into the build graph.
1076</p>
1077
1078<h4 id='flag--experimental_extra_action_filter'>
1079 <code class='flag'>--experimental_extra_action_filter=<var>regex</var></code>
1080</h4>
1081<p>
1082 The <code>experimental_extra_action_filter</code> option instructs Bazel to
1083 filter the set of targets to schedule <code>extra_actions</code> for.
1084</p>
1085<p>
1086 This flag is only applicable in combination with the
1087 <a href='#flag--experimental_action_listener'
1088 ><code>--experimental_action_listener</code></a> flag.
1089</p>
1090<p>
1091 By default all <code>extra_actions</code> in the transitive closure of the
1092 requested targets-to-build get scheduled for execution.
1093 <code>--experimental_extra_action_filter</code> will restrict scheduling to
1094 <code>extra_actions</code> of which the owner's label matches the specified
1095 regular expression.
1096</p>
1097<p>
1098 The following example will limit scheduling of <code>extra_actions</code>
1099 to only apply to actions of which the owner's label contains '/bar/':
1100</p>
1101<pre>% bazel build --experimental_action_listener=//test:al //foo/... \
1102 --experimental_extra_action_filter=.*/bar/.*
1103</pre>
1104
1105<h4 id='flag--output_filter'><code class='flag'>--output_filter <var>regex</var></code></h4>
1106<p>
1107 The <code class='flag'>--output_filter</code> option will only show build and compilation
1108 warnings for targets that match the regular expression. If a target does not
1109 match the given regular expression and its execution succeeds, its standard
1110 output and standard error are thrown away. This option is intended to be used
1111 to help focus efforts on fixing warnings in packages under development. Here
1112 are some typical values for this option:
1113</p>
1114<table>
1115 <tr>
1116 <td><code class='flag'>--output_filter=</code></td>
1117 <td>Show all output.</td>
1118 </tr>
1119 <tr>
1120 <td><code class='flag'>--output_filter='^//(first/project|second/project):'</code></td>
1121 <td>Show the output for the specified packages.</td>
1122 </tr>
1123 <tr>
1124 <td><code class='flag'>--output_filter='^//((?!(first/bad_project|second/bad_project):).)*$'</code></td>
1125 <td>Don't show output for the specified packages.</td>
1126 </tr>
1127 <tr>
1128 <td><code class='flag'>--output_filter=DONT_MATCH_ANYTHING</code></td>
1129 <td>Don't show output.</td>
1130 </tr>
1131</table>
1132
1133<h4 id='flag--analysis_warnings_as_errors'><code>--[no]analysis_warnings_as_errors</code></h4>
1134<p>
1135 When this option is enabled, visible analysis warnings (as specified by
1136 the output filter) are treated as errors, effectively preventing the build
1137 phase from starting. This feature can be used to enable strict builds that
1138 do not allow new warnings to creep into a project.
1139</p>
1140
1141<h3 id='flags-options'>Flags options</h3>
1142<p>
1143 These options control which options Bazel will pass to other tools.
1144</p>
1145
1146<h4 id='flag--copt'><code class='flag'>--copt <var>gcc-option</var></code></h4>
1147<p>
1148 This option takes an argument which is to be passed to gcc.
1149 The argument will be passed to gcc whenever gcc is invoked
1150 for preprocessing, compiling, and/or assembling C, C++, or
1151 assembler code. It will not be passed when linking.
1152</p>
1153<p>
1154 This option can be used multiple times.
1155 For example:
1156</p>
1157<pre>
1158 % bazel build --copt="-g0" --copt="-fpic" //foo
1159</pre>
1160<p>
1161 will compile the <code>foo</code> library without debug tables, generating
1162 position-independent code.
1163</p>
1164<p>
1165 Note that changing <code class='flag'>--copt</code> settings will force a recompilation
1166 of all affected object files. Also note that copts values listed in specific
1167 cc_library or cc_binary build rules will be placed on the gcc command line
1168 <em>after</em> these options.
1169</p>
1170<p>
1171 Warning: C++-specific options (such as <code>-fno-implicit-templates</code>)
1172 should be specified in <code class='flag'>--cxxopt</code>, not in
1173 <code class='flag'>--copt</code>. Likewise, C-specific options (such as -Wstrict-prototypes)
1174 should be specified in <code class='flag'>--conlyopt</code>, not in <code>copt</code>.
1175 Similarly, gcc options that only have an
1176 effect at link time (such as <code>-l</code>) should be specified in
1177 <code class='flag'>--linkopt</code>, not in <code class='flag'>--copt</code>.
1178</p>
1179
1180<h4 id='flag--host_copt'><code class='flag'>--host_copt <var>gcc-option</var></code></h4>
1181<p>
1182 This option takes an argument which is to be passed to gcc for source files
1183 that are compiled in the host configuration. This is analogous to
1184 the <a href='#flag--copt'><code class='flag'>--copt</code></a> option, but applies only to the
1185 host configuration.
1186</p>
1187
Marcel Hlopko4c1b1fd2017-02-24 12:54:17 +00001188<h4 id='flag--host_cxxopt'><code class='flag'>--host_cxxopt <var>gcc-option</var></code></h4>
1189<p>
1190 This option takes an argument which is to be passed to gcc for source files
1191 that are compiled in the host configuration. This is analogous to
1192 the <a href='#flag--cxxopt'><code class='flag'>--cxxopt</code></a> option, but applies only to the
1193 host configuration.
1194</p>
1195
David Chen8fe82a32016-08-24 10:55:41 +00001196<h4 id='flag--conlyopt'><code class='flag'>--conlyopt <var>gcc-option</var></code></h4>
1197<p>
1198 This option takes an argument which is to be passed to gcc when compiling C source files.
1199</p>
1200<p>
1201 This is similar to <code class='flag'>--copt</code>, but only applies to C compilation,
1202 not to C++ compilation or linking. So you can pass C-specific options
1203 (such as <code>-Wno-pointer-sign</code>) using <code class='flag'>--conlyopt</code>.
1204</p>
1205<p>
1206 Note that copts parameters listed in specific cc_library or cc_binary build rules
1207 will be placed on the gcc command line <em>after</em> these options.
1208</p>
1209
1210<h4 id='flag--cxxopt'><code class='flag'>--cxxopt <var>gcc-option</var></code></h4>
1211<p>
1212 This option takes an argument which is to be passed to gcc when compiling C++ source files.
1213</p>
1214<p>
1215 This is similar to <code class='flag'>--copt</code>, but only applies to C++ compilation,
1216 not to C compilation or linking. So you can pass C++-specific options
1217 (such as <code>-fpermissive</code> or <code>-fno-implicit-templates</code>) using <code class='flag'>--cxxopt</code>.
1218 For example:
1219</p>
1220<pre>
1221 % bazel build --cxxopt="-fpermissive" --cxxopt="-Wno-error" //foo/cruddy_code
1222</pre>
1223<p>
1224 Note that copts parameters listed in specific cc_library or cc_binary build rules
1225 will be placed on the gcc command line <em>after</em> these options.
1226</p>
1227
1228<h4 id='flag--linkopt'><code class='flag'>--linkopt <var>linker-option</var></code></h4>
1229<p>
1230 This option takes an argument which is to be passed to gcc when linking.
1231</p>
1232<p>
1233 This is similar to <code class='flag'>--copt</code>, but only applies to linking,
1234 not to compilation. So you can pass gcc options that only make sense
1235 at link time (such as <code>-lssp</code> or <code>-Wl,--wrap,abort</code>)
1236 using <code class='flag'>--linkopt</code>. For example:
1237</p>
1238<pre>
1239 % bazel build --copt="-fmudflap" --linkopt="-lmudflap" //foo/buggy_code
1240</pre>
1241<p>
1242 Build rules can also specify link options in their attributes. This option's
1243 settings always take precedence. Also see
1244 <a href="be/c-cpp.html#cc_library.linkopts">cc_library.linkopts</a>.
1245</p>
1246
1247<h4 id='flag--strip'><code class='flag'>--strip (always|never|sometimes)</code></h4>
1248<p>
1249 This option determines whether Bazel will strip debugging information from
1250 all binaries and shared libraries, by invoking the linker with the <code>-Wl,--strip-debug</code> option.
1251 <code class='flag'>--strip=always</code> means always strip debugging information.
1252 <code class='flag'>--strip=never</code> means never strip debugging information.
1253 The default value of <code class='flag'>--strip=sometimes</code> means strip iff the <code class='flag'>--compilation_mode</code>
1254 is <code>fastbuild</code>.
1255</p>
1256<pre>
1257 % bazel build --strip=always //foo:bar
1258</pre>
1259<p>
1260 will compile the target while stripping debugging information from all generated
1261 binaries.
1262</p>
1263<p>
1264 Note that if you want debugging information, it's not enough to disable stripping; you also need to make
1265 sure that the debugging information was generated by the compiler, which you can do by using either
1266 <code>-c dbg</code> or <code class='flag'>--copt -g</code>.
1267</p>
1268<p>
1269 Note also that Bazel's <code class='flag'>--strip</code> option corresponds with ld's <code>--strip-debug</code> option:
1270 it only strips debugging information. If for some reason you want to strip <em>all</em> symbols,
1271 not just <em>debug</em> symbols, you would need to use ld's <code>--strip-all</code> option,
1272 which you can do by passing <code class='flag'>--linkopt=-Wl,--strip-all</code> to Bazel.
1273</p>
1274
1275<h4 id='flag--stripopt'><code class='flag'>--stripopt <var>strip-option</var></code></h4>
1276<p>
1277 An additional option to pass to the <code>strip</code> command when generating
1278 a <a href="be/c-cpp.html#cc_binary_implicit_outputs"><code>*.stripped</code>
1279 binary</a>. The default is <code>-S -p</code>. This option can be used
1280 multiple times.
1281</p>
1282<p>
1283 Note that <code class='flag'>--stripopt</code> does not apply to the stripping of the main
1284 binary with <code><a href='#flag--strip'>--strip</a>=(always|sometimes)</code>.
1285</p>
1286
1287<h4 id='flag--fdo_instrument'><code class='flag'>--fdo_instrument <var>profile-output-dir</var></code></h4>
1288<p>
1289 The <code class='flag'>--fdo_instrument</code> option enables the generation of
1290 FDO (feedback directed optimization) profile output when the
1291 built C/C++ binary is executed. For GCC, the argument provided is used as a
1292 directory prefix for a per-object file directory tree of .gcda files
1293 containing profile information for each .o file.
1294</p>
1295<p>
1296 Once the profile data tree has been generated, the profile tree
1297 should be zipped up, and provided to the
1298 <code class='flag'>--fdo_optimize=<var>profile-zip</var></code>
1299 Bazel option to enable the FDO optimized compilation.
1300
1301</p>
1302<p>
1303 For the LLVM compiler the argument is also the directory under which the raw LLVM profile
1304 data file(s) is dumped, e.g.
1305 <code class='flag'>--fdo_instrument=<var>/path/to/rawprof/dir/</var></code>.
1306</p>
1307<p>
1308 The options <code class='flag'>--fdo_instrument</code> and <code class='flag'>--fdo_optimize</code>
1309 cannot be used at the same time.
1310</p>
1311
1312<h4 id='flag--fdo_optimize'><code class='flag'>--fdo_optimize <var>profile-zip</var></code></h4>
1313<p>
1314 The <code class='flag'>--fdo_optimize</code> option enables the use of the
1315 per-object file profile information to perform FDO (feedback
1316 directed optimization) optimizations when compiling. For GCC, the argument
1317 provided is the zip file containing the previously-generated file tree
1318 of .gcda files containing profile information for each .o file.
1319</p>
1320<p>
1321 Alternatively, the argument provided can point to an auto profile
1322 identified by the extension .afdo.
1323
1324</p>
1325<p>
1326 Note that this option also accepts labels that resolve to source files. You
1327 may need to add an <code>exports_files</code> directive to the corresponding package to
1328 make the file visible to Bazel.
1329</p>
1330<p>
1331 For the LLVM compiler the argument provided should point to the indexed LLVM
1332 profile output file prepared by the llvm-profdata tool, and should have a .profdata
1333 extension.
1334</p>
1335<p>
1336 The options <code class='flag'>--fdo_instrument</code> and <code class='flag'>
1337 --fdo_optimize</code> cannot be used at the same time.
1338</p>
1339
1340<h4 id='flag--lipo'><code class='flag'>--lipo (off|binary)</code></h4>
1341<p>
1342 The <code class='flag'>--lipo=binary</code> option enables
1343
1344 LIPO
1345 (Lightweight Inter-Procedural Optimization). LIPO is an extended C/C++ optimization technique
1346 that optimizes code across different object files. It involves compiling each C/C++ source
1347 file differently for every binary. This is in contrast to normal compilation where compilation
1348 outputs are reused. This means that LIPO is more expensive than normal compilation.
1349</p>
1350<p>
1351 This option only has an effect when FDO is also enabled (see the
1352 <a href="#flag--fdo_instrument">--fdo_instrument</a> and
1353 <a href="#flag--fdo_optimize">--fdo_options</a>).
1354 Currently LIPO is only supported when building a single <code>cc_binary</code> rule.
1355</p>
1356<p>Setting <code>--lipo=binary</code> implicitly sets
1357 <code><a href="#flag--dynamic_mode">--dynamic_mode</a>=off</code>.
1358</p>
1359
1360<h4 id='flag--lipo_context'><code class='flag'>--lipo_context
1361 <var>context-binary</var></code></h4>
1362<p>
1363 Specifies the label of a <code>cc_binary</code> rule that was used to generate
1364 the profile information for LIPO that was given to
1365 the <a href='#flag--fdo_optimize'><code class='flag'>--fdo_optimize</code></a> option.
1366</p>
1367<p>
1368 Specifying the context is mandatory when <code>--lipo=binary</code> is set.
1369 Using this option implicitly also sets
1370 <code><a href="#flag--linkopt">--linkopt</a>=-Wl,--warn-unresolved-symbols</code>.
1371</p>
1372
1373<h4 id='flag--output_symbol_counts'><code class='flag'>--[no]output_symbol_counts</code></h4>
1374<p>
Googlerd7273272017-05-02 18:56:29 +02001375 If enabled, each gold-invoked link of a C++ executable binary will output
David Chen8fe82a32016-08-24 10:55:41 +00001376 a <i>symbol counts</i> file (via the <code>--print-symbol-counts</code> gold
Googlerd7273272017-05-02 18:56:29 +02001377 option). For each linker input, the file logs the number of symbols that were
1378 defined and the number of symbols that were used in the binary.
1379 This information can be used to track unnecessary link dependencies.
1380 The symbol counts file is written to the binary's output path with the name
David Chen8fe82a32016-08-24 10:55:41 +00001381 <code>[targetname].sc</code>.
1382</p>
1383<p>
1384 This option is disabled by default.
1385</p>
1386
1387<h4 id='flag--jvmopt'><code class='flag'>--jvmopt <var>jvm-option</var></code></h4>
1388<p>
1389 This option allows option arguments to be passed to the Java VM. It can be used
1390 with one big argument, or multiple times with individual arguments. For example:
1391</p>
1392<pre>
1393 % bazel build --jvmopt="-server -Xms256m" java/com/example/common/foo:all
1394</pre>
1395<p>
1396 will use the server VM for launching all Java binaries and set the
1397 startup heap size for the VM to 256 MB.
1398</p>
1399
1400<h4 id='flag--javacopt'><code class='flag'>--javacopt <var>javac-option</var></code></h4>
1401<p>
1402 This option allows option arguments to be passed to javac. It can be used
1403 with one big argument, or multiple times with individual arguments. For example:
1404</p>
1405<pre>
1406 % bazel build --javacopt="-g:source,lines" //myprojects:prog
1407</pre>
1408<p>
1409 will rebuild a java_binary with the javac default debug info
1410 (instead of the bazel default).
1411</p>
1412<p>
1413 The option is passed to javac after the Bazel built-in default options for
1414 javac and before the per-rule options. The last specification of
1415 any option to javac wins. The default options for javac are:
1416</p>
1417
1418<pre>
1419 -source 8 -target 8 -encoding UTF-8
1420</pre>
1421<p>
1422 Note that changing <code class='flag'>--javacopt</code> settings will force a recompilation
1423 of all affected classes. Also note that javacopts parameters listed in
1424 specific java_library or java_binary build rules will be placed on the javac
1425 command line <em>after</em> these options.
1426</p>
1427
1428<h5 id='-extra_checks'><code>-extra_checks[:(off|on)]</code></h5>
1429
1430<p>
1431 This javac option enables extra correctness checks. Any problems found will
1432 be presented as errors.
1433 Either <code>-extra_checks</code> or <code>-extra_checks:on</code> may be used
1434 to force the checks to be turned on. <code>-extra_checks:off</code> completely
1435 disables the analysis.
1436 When this option is not specified, the default behavior is used.
1437</p>
1438
1439<h4 id='flag--strict_java_deps'><code class='flag'>--strict_java_deps
1440 (default|strict|off|warn|error)</code></h4>
1441<p>
1442 This option controls whether javac checks for missing direct dependencies.
1443 Java targets must explicitly declare all directly used targets as
1444 dependencies. This flag instructs javac to determine the jars actually used
1445 for type checking each java file, and warn/error if they are not the output
1446 of a direct dependency of the current target.
1447</p>
1448
1449<ul>
1450 <li> <code>off</code> means checking is disabled.
1451 </li>
1452 <li> <code>warn</code> means javac will generate standard java warnings of
1453 type <code>[strict]</code> for each missing direct dependency.
1454 </li>
1455 <li> <code>default</code>, <code>strict</code> and <code>error</code> all
1456 mean javac will generate errors instead of warnings, causing the current
1457 target to fail to build if any missing direct dependencies are found.
1458 This is also the default behavior when the flag is unspecified.
1459 </li>
1460</ul>
1461
David Chen8fe82a32016-08-24 10:55:41 +00001462<h3 id='semantics-options'>Semantics options</h3>
1463<p>
1464 These options affect the build commands and/or the output file contents.
1465</p>
1466
1467<h4 id='flag--compilation_mode'><code class='flag'>--compilation_mode (fastbuild|opt|dbg)</code> (-c)</h4>
1468<p>
1469 This option takes an argument of <code>fastbuild</code>, <code>dbg</code>
1470 or <code>opt</code>, and affects various C/C++ code-generation
1471 options, such as the level of optimization and the completeness of
1472 debug tables. Bazel uses a different output directory for each
1473 different compilation mode, so you can switch between modes without
1474 needing to do a full rebuild <i>every</i> time.
1475</p>
1476<ul>
1477 <li> <code>fastbuild</code> means build as fast as possible:
1478 generate minimal debugging information (<code>-gmlt
1479 -Wl,-S</code>), and don't optimize. This is the
1480 default. Note: <code>-DNDEBUG</code> will <b>not</b> be set.
1481 </li>
1482 <li> <code>dbg</code> means build with debugging enabled (<code>-g</code>),
1483 so that you can use gdb (or another debugger).
1484 </li>
1485 <li> <code>opt</code> means build with optimization enabled and
1486 with <code>assert()</code> calls disabled (<code>-O2 -DNDEBUG</code>).
1487 Debugging information will not be generated in <code>opt</code> mode
1488 unless you also pass <code class='flag'>--copt -g</code>.
1489 </li>
1490</ul>
1491
1492<h4 id='flag--cpu'><code class='flag'>--cpu <var>cpu</var></code></h4>
1493<p>
1494This option specifies the target CPU architecture to be used for
1495the compilation of binaries during the build.
1496</p>
1497<p>
1498
1499</p>
1500
1501<p>
1502 Note that a particular combination of crosstool version, compiler version,
1503 libc version, and target CPU is allowed only if it has been specified
1504 in the currently used CROSSTOOL file.
1505</p>
1506
1507<h4 id='flag--host_cpu'><code class='flag'>--host_cpu <var>cpu</var></code></h4>
1508<p>
1509 This option specifies the name of the CPU architecture that should be
1510 used to build host tools.
1511</p>
1512
Alex Humesky105e6612017-01-06 19:03:10 +00001513<h4 id='flag--fat_apk_cpu'><code class='flag'>--fat_apk_cpu <var>cpu[,cpu]*</var></code></h4>
1514<p>
1515 The CPUs to build C/C++ libraries for in the transitive <code>deps</code> of
1516 <code>android_binary</code>
1517
1518 rules. Other C/C++ rules are not affected. For example, if a <code>cc_library</code>
1519 appears in the transitive <code>deps</code> of an <code>android_binary</code> rule and a
1520 <code>cc_binary</code> rule, the <code>cc_library</code> will be built at least twice:
1521 once for each CPU specified with <code class='flag'>--fat_apk_cpu</code> for the
1522 <code>android_binary</code> rule, and once for the CPU specified with
1523 <code class='flag'>--cpu</code> for the <code>cc_binary</code> rule.
1524
1525<p>
1526The default is <code>armeabi-v7a</code>.
1527</p>
1528 <p>
1529 One <code>.so</code> file will be created and packaged in the APK for
1530 each CPU specified with <code class='flag'>--fat_apk_cpu</code>. The name of the <code>.so</code>
1531 file will be the name of the <code>android_binary</code> rule prefixed with "lib", e.g., if the name
1532 of the <code>android_binary</code> is "foo", then the file will be <code>libfoo.so</code>.
1533 </p>
1534
1535 <p>
1536 Note that an Android-compatible crosstool must be selected.
1537 If an <code>android_ndk_repository</code> rule is defined in the
1538 WORKSPACE file, an Android-compatible crosstool is automatically selected.
1539 Otherwise, the crostool can be selected using the
1540 <a href='#flag--android_crosstool_top'><code class='flag'>--android_crosstool_top</code></a>
1541 or <a href='#flag--crosstool_top'><code class='flag'>--crosstool_top</code></a> flags.
1542 </p>
1543</p>
1544
David Chen8fe82a32016-08-24 10:55:41 +00001545<h4 id='flag--experimental_skip_static_outputs'><code class='flag'>--experimental_skip_static_outputs</code></h4>
1546<p>
1547 The <code class='flag'>--experimental_skip_static_outputs</code> option causes all
1548 statically-linked C++ binaries to <b>not</b> be output in any meaningful
1549 way.
1550
1551</p>
1552<p>
1553 If you set this flag, you must also
1554 set <a href="#flag--distinct_host_configuration"><code class='flag'>--distinct_host_configuration</code></a>.
1555 It is also inherently incompatible with running tests &mdash; don't use it for
1556 that. This option is experimental and may go away at any time.
1557</p>
1558
1559<h4 id='flag--per_file_copt'><code class='flag'>--per_file_copt
1560 <var>[+-]regex[,[+-]regex]...@option[,option]...</var></code></h4>
1561<p>
1562 When present, any C++ file with a label or an execution path matching one of the inclusion regex
1563 expressions and not matching any of the exclusion expressions will be built
1564 with the given options. The label matching uses the canonical form of the label
1565 (i.e //<code>package</code>:<code>label_name</code>).
1566
1567 The execution path is the relative path to your workspace directory including the base name
1568 (including extension) of the C++ file. It also includes any platform dependent prefixes.
1569 Note, that if only one of the label or the execution path matches the options will be used.
1570</p>
1571<p>
1572 <b>Notes</b>:
1573 To match the generated files (e.g. genrule outputs)
1574 Bazel can only use the execution path. In this case the regexp shouldn't start with '//'
1575 since that doesn't match any execution paths. Package names can be used like this:
1576 <code class='flag'>--per_file_copt=base/.*\.pb\.cc@-g0</code>. This will match every
1577 <code>.pb.cc</code> file under a directory called <code>base</code>.
1578</p>
1579<p>
1580 This option can be used multiple times.
1581</p>
1582<p>
1583 The option is applied regardless of the compilation mode used. I.e. it is possible
1584 to compile with <code class='flag'>--compilation_mode=opt</code> and selectively compile some
1585 files with stronger optimization turned on, or with optimization disabled.
1586</p>
1587<p>
1588 <b>Caveat</b>: If some files are selectively compiled with debug symbols the symbols
1589 might be stripped during linking. This can be prevented by setting
1590 <code class='flag'>--strip=never</code>.
1591</p>
1592<p>
1593 <b>Syntax</b>: <code>[+-]regex[,[+-]regex]...@option[,option]...</code> Where
1594 <code>regex</code> stands for a regular expression that can be prefixed with
1595 a <code>+</code> to identify include patterns and with <code>-</code> to identify
1596 exclude patterns. <code>option</code> stands for an arbitrary option that is passed
1597 to the C++ compiler. If an option contains a <code>,</code> it has to be quoted like so
1598 <code>\,</code>. Options can also contain <code>@</code>, since only the first
1599 <code>@</code> is used to separate regular expressions from options.
1600</p>
1601<p>
1602 <b>Example</b>:
1603 <code class='flag'>--per_file_copt=//foo:.*\.cc,-//foo:file\.cc@-O0,-fprofile-arcs</code>
1604 adds the <code>-O0</code> and the <code>-fprofile-arcs</code> options to the command
1605 line of the C++ compiler for all <code>.cc</code> files in <code>//foo/</code> except
1606 <code>file.cc</code>.
1607</p>
1608<h4 id='flag--dynamic_mode'><code class='flag'>--dynamic_mode <var>mode</var></code></h4>
1609<p>
1610 Determines whether C++ binaries will be linked dynamically, interacting with
1611 the <a href='be/c-cpp.html#cc_binary.linkstatic'>linkstatic
1612 attribute</a> on build rules.
1613</p>
1614
1615<p>
1616 Modes:
1617</p>
1618<ul>
1619 <li><code>auto</code>: Translates to a platform-dependent mode;
1620 <code>default</code> for linux and <code>off</code> for cygwin.</li>
1621 <li><code>default</code>: Allows bazel to choose whether to link dynamically.
1622 See <a href='be/c-cpp.html#cc_binary.linkstatic'>linkstatic</a> for more
1623 information.</li>
1624 <li><code>fully</code>: Links all targets dynamically. This will speed up
1625 linking time, and reduce the size of the resulting binaries.
1626
1627 </li>
1628 <li><code>off</code>: Links all targets in
1629 <a href='be/c-cpp.html#cc_binary.linkstatic'>mostly static</a> mode.
1630 If <code>-static</code> is set in linkopts, targets will change to fully
1631 static.</li>
1632</ul>
1633
1634<h4 id='flag--fission'><code class='flag'>--fission (yes|no|[dbg][,opt][,fastbuild])</code></h4>
1635<p>
1636 Enables
1637
1638 <a href='https://gcc.gnu.org/wiki/DebugFission'>Fission</a>,
1639 which writes C++ debug information to dedicated .dwo files instead of .o files, where it would
1640 otherwise go. This substantially reduces the input size to links and can reduce link times.
1641
1642</p>
1643<p>
1644 When set to <code class='flag'>[dbg][,opt][,fastbuild]</code> (example:
1645 <code class='flag'>--fission=dbg,fastbuild</code>), Fission is enabled
1646 only for the specified set of compilation modes. This is useful for bazelrc
1647 settings. When set to <code class='flag'>yes</code>, Fission is enabled
1648 universally. When set to <code class='flag'>no</code>, Fission is disabled
1649 universally. Default is <code class='flag'>dbg</code>.
1650</p>
1651
1652<h4 id='flag--force_ignore_dash_static'><code class='flag'>--force_ignore_dash_static</code></h4>
1653<p>
1654 If this flag is set, any <code>-static</code> options in linkopts of
1655 <code>cc_*</code> rules BUILD files are ignored. This is only intended as a
1656 workaround for C++ hardening builds.
1657</p>
1658
1659<h4 id='flag--force_pic'><code class='flag'>--[no]force_pic</code></h4>
1660<p>
1661 If enabled, all C++ compilations produce position-independent code ("-fPIC"),
1662 links prefer PIC pre-built libraries over non-PIC libraries, and links produce
1663 position-independent executables ("-pie"). Default is disabled.
1664</p>
1665<p>
1666 Note that dynamically linked binaries (i.e. <code>--dynamic_mode fully</code>)
1667 generate PIC code regardless of this flag's setting. So this flag is for cases
1668 where users want PIC code explicitly generated for static links.
1669</p>
1670
Andrew Pellegrini8d2c1662017-01-26 16:04:12 +00001671<h4 id='flag--android_resource_shrinking'><code class='flag'>--android_resource_shrinking</code></h4>
1672<p>
1673 Selects whether to perform resource shrinking for android_binary rules. Sets the default for the
1674 <a href='be/android.html#android_binary.shrink_resources'>shrink_resources attribute</a> on
1675 android_binary rules; see the documentation for that rule for further details. Defaults to off.
1676</p>
1677
David Chen8fe82a32016-08-24 10:55:41 +00001678<h4 id='flag--custom_malloc'><code class='flag'>--custom_malloc <var>malloc-library-target</var></code></h4>
1679<p>
1680 When specified, always use the given malloc implementation, overriding all
1681 <code>malloc="target"</code> attributes, including in those targets that use the
1682 default (by not specifying any <code>malloc</code>).
1683</p>
1684
1685<h4 id='flag--crosstool_top'><code class='flag'>--crosstool_top <var>label</var></code></h4>
1686<p>
1687 This option specifies the location of the crosstool compiler suite
1688 to be used for all C++ compilation during a build. Bazel will look in that
1689 location for a CROSSTOOL file and uses that to automatically determine
1690 settings for
1691
1692 <code class='flag'>--compiler</code>.
1693</p>
1694
1695<h4 id='flag--host_crosstool_top'><code class='flag'>--host_crosstool_top <var>label</var></code></h4>
1696<p>
1697 If not specified, bazel uses the value of <code class='flag'>--crosstool_top</code> to compile
1698 code in the host configuration, i.e., tools run during the build. The main purpose of this flag
1699 is to enable cross-compilation.
1700</p>
1701
Cal Peyserf1a15c02017-01-17 20:26:00 +00001702<h4 id='flag--apple_crosstool_top'><code class='flag'>--apple_crosstool_top <var>label</var></code></h4>
1703<p>
1704 The crosstool to use for compiling C/C++ rules in the transitive <code>deps</code> of
1705 objc_*, ios__*, and apple_* rules. For those targets, this flag overwrites
1706 <code class='flag'>--crosstool_top</code>.
1707</p>
1708
Alex Humesky105e6612017-01-06 19:03:10 +00001709<h4 id='flag--android_crosstool_top'><code class='flag'>--android_crosstool_top <var>label</var></code></h4>
1710<p>
1711 The crosstool to use for compiling C/C++ rules in the transitive <code>deps</code> of
1712 <code>android_binary</code> rules. This is useful if other targets in the
1713 build require a different crosstool. The default is to use the crosstool
1714 generated by the <code>android_ndk_repository</code> rule in the WORKSPACE file.
1715 See also <a href='#flag--fat_apk_cpu'><code class='flag'>--fat_apk_cpu</code></a>.
1716</p>
David Chen8fe82a32016-08-24 10:55:41 +00001717<h4 id='flag--compiler'><code class='flag'>--compiler <var>version</var></code></h4>
1718<p>
1719 This option specifies the C/C++ compiler version (e.g. <code>gcc-4.1.0</code>)
1720 to be used for the compilation of binaries during the build. If you want to
1721 build with a custom crosstool, you should use a CROSSTOOL file instead of
1722 specifying this flag.
1723</p>
1724<p>
1725 Note that only certain combinations of crosstool version, compiler version,
1726 libc version, and target CPU are allowed.
1727</p>
1728
1729<h4 id='flag--glibc'><code class='flag'>--glibc <var>version</var></code></h4>
1730<p>
1731 This option specifies the version of glibc that the target should be linked
1732 against. If you want to build with a custom crosstool, you should use a
1733 CROSSTOOL file instead of specifying this flag. In that case, Bazel will use
1734 the CROSSTOOL file and the following options where appropriate:
1735 <ul>
1736 <li><a href="#flag--cpu"><code class='flag'>--cpu</code></a></li>
1737
1738 </ul>
1739</p>
1740<p>
1741 Note that only certain combinations of crosstool version, compiler version,
1742 glibc version, and target CPU are allowed.
1743</p>
1744
Alex Humesky105e6612017-01-06 19:03:10 +00001745<h4 id='flag--android_sdk'><code class='flag'>--android_sdk <var>label</var></code></h4>
1746<p>
1747 This option specifies the Android SDK/platform toolchain
1748 and Android runtime library that will be used to build any Android-related
1749 rule.
1750
1751 The Android SDK will be automatically selected if an <code>android_sdk_repository</code>
1752 rule is defined in the WORKSPACE file.
1753</p>
1754
David Chen8fe82a32016-08-24 10:55:41 +00001755<h4 id='flag--java_toolchain'><code class='flag'>--java_toolchain <var>label</var></code></h4>
1756<p>
1757 This option specifies the label of the java_toolchain used to compile Java
1758 source files.
1759</p>
1760
1761<h4 id='flag--javabase'><code class='flag'>--javabase (<var>path</var>|<var>label</var>)</code></h4>
1762<p>
Googlerae4895a2016-08-30 17:16:37 +00001763 This option sets the <i>label</i> or the <i>path</i> of the base Java installation to
1764 use for running JavaBuilder, SingleJar, for <i>bazel run</i> and <i>bazel
1765 test</i>, and for Java binaries built by <code>java_binary</code> and <code>java_test</code>
1766 rules. A path must be to a JDK or JRE directory that contains <code>bin/java</code>.
1767 The various <a href='be/make-variables.html'>"Make" variables</a> for
David Chen8fe82a32016-08-24 10:55:41 +00001768 Java (<code>JAVABASE</code>, <code>JAVA</code>, <code>JAVAC</code> and
1769 <code>JAR</code>) are derived from this option.
1770</p>
1771
1772<p>
1773 This does not select the Java compiler that is used to compile Java
1774 source files. The compiler can be selected by settings the
1775 <a href="#flag--java_toolchain"><code class='flag'>--java_toolchain</code></a>
1776 option.
1777</p>
1778
1779<h3 id='strategy-options'>Build strategy options</h3>
1780<p>
1781 These options affect how Bazel will execute the build.
1782 They should not have any significant effect on the output files
1783 generated by the build. Typically their main effect is on the
1784 speed on the build.
1785</p>
1786
1787<h4 id='flag--spawn_strategy'><code class='flag'>--spawn_strategy <var>strategy</var></code></h4>
1788<p>
1789 This option controls where and how commands are executed.
1790</p>
1791<ul>
1792
1793 <li>
1794 <code>standalone</code> causes commands to be executed as local subprocesses.
1795 </li>
1796 <li>
1797 <code>sandboxed</code> causes commands to be executed inside a sandbox on the local machine.
1798 This requires that all input files, data dependencies and tools are listed as direct
1799 dependencies in the <code>srcs</code>, <code>data</code> and <code>tools</code> attributes.
1800 This is the default on systems that support sandboxed execution.
1801 </li>
1802
1803</ul>
1804
1805<h4 id='flag--genrule_strategy'><code class='flag'>--genrule_strategy <var>strategy</var></code></h4>
1806<p>
1807 This option controls where and how genrules are executed.
1808</p>
1809<ul>
1810
1811 <li>
1812 <code>standalone</code> causes genrules to run as local subprocesses.
1813 </li>
1814 <li>
1815 <code>sandboxed</code> causes genrules to run inside a sandbox on the local machine.
1816 This requires that all input files are listed as direct dependencies in
1817 the <code>srcs</code> attribute, and the program(s) executed are listed
1818 in the <code>tools</code> attribute.
1819 This is the default for Bazel on systems that support sandboxed execution.
1820 </li>
1821
1822</ul>
1823
1824<h4 id='flag--local_genrule_timeout_seconds'><code class='flag'>--local_genrule_timeout_seconds <var>seconds</var></code></h4>
1825<p>Sets a timeout value for local genrules with the given number of seconds.</p>
1826
1827<h4 id='flag--jobs'><code class='flag'>--jobs <var>n</var></code> (-j)</h4>
1828<p>
1829 This option, which takes an integer argument, specifies a limit on
1830 the number of jobs that should be executed concurrently during the
1831 execution phase of the build. The default is 200.
1832</p>
1833<p>
1834 Note that the number of concurrent jobs that Bazel will run
1835 is determined not only by the <code class='flag'>--jobs</code> setting, but also
1836 by Bazel's scheduler, which tries to avoid running concurrent jobs
1837 that will use up more resources (RAM or CPU) than are available,
1838 based on some (very crude) estimates of the resource consumption
1839 of each job. The behavior of the scheduler can be controlled by
1840 the <code class='flag'>--ram_utilization_factor</code> option.
1841</p>
1842
1843<h4 id='flag--progress_report_interval'><code class='flag'>--progress_report_interval <var>n</var></code></h4>
1844<p>
1845
1846 Bazel periodically prints a progress report on jobs that are not
1847 finished yet (e.g. long running tests). This option sets the
1848 reporting frequency, progress will be printed every <code>n</code>
1849 seconds.
1850</p>
1851<p>
1852 The default is 0, that means an incremental algorithm: the first
1853 report will be printed after 10 seconds, then 30 seconds and after
1854 that progress is reported once every minute.
1855</p>
1856
1857<h4 id='flag--ram_utilization_factor'><code class='flag'>--ram_utilization_factor</code> <var>percentage</var></h4>
1858<p>
1859 This option, which takes an integer argument, specifies what percentage
1860 of the system's RAM Bazel should try to use for its subprocesses.
1861 This option affects how many processes Bazel will try to run
1862 in parallel. The default value is 67.
1863 If you run several Bazel builds in parallel, using a lower
1864 value for this option may avoid thrashing and thus improve overall
1865 throughput. Using a value higher than the default is NOT recommended. Note
1866 that Bazel's estimates are very coarse, so the actual RAM usage may be much
1867 higher or much lower than specified. Note also that this option does not
1868 affect the amount of memory that the Bazel server itself will use.
1869</p>
1870
1871<h4 id='flag--local_resources'><code class='flag'>--local_resources</code> <var>availableRAM,availableCPU,availableIO</var></h4>
1872<p>
1873 This option, which takes three comma-separated floating point arguments,
1874specifies the amount of local resources that Bazel can take into
1875consideration when scheduling build and test activities. Option expects amount of
1876available RAM (in MB), number of CPU cores (with 1.0 representing single full
1877core) and workstation I/O capability (with 1.0 representing average
1878workstation). By default Bazel will estimate amount of RAM and number of CPU
1879cores directly from system configuration and will assume 1.0 I/O resource.
1880</p>
1881<p>
1882 If this option is used, Bazel will ignore --ram_utilization_factor.
1883</p>
1884
1885<h4 id='flag--build_runfile_links'><code class='flag'>--[no]build_runfile_links</code></h4>
1886<p>
1887 This option, which is currently enabled by default, specifies
1888 whether the runfiles symlinks for tests and
1889 <code>cc_binary</code> targets should be built in the output directory.
1890 Using <code class='flag'>--nobuild_runfile_links</code> can be useful
1891 to validate if all targets compile without incurring the overhead
1892 for building the runfiles trees.
1893
1894 Within Bazel's output tree, the
1895 runfiles symlink tree is typically rooted as a sibling of the corresponding
1896 binary or test.
1897</p>
1898
1899<p>
1900 When tests (or applications) are executed, their
1901 run-time data dependencies are gathered together in one place, and
1902 may be accessed by the test using paths of the form
1903 <code>$TEST_SRCDIR/workspace/<var>packagename</var>/<var>filename</var></code>.
1904 The "runfiles" tree ensures that tests have access to all the files
1905 upon which they have a declared dependence, and nothing more. By
1906 default, the runfiles tree is implemented by constructing a set of
1907 symbolic links to the required files. As the set of links grows, so
1908 does the cost of this operation, and for some large builds it can
1909 contribute significantly to overall build time, particularly because
1910 each individual test (or application) requires its own runfiles tree.
1911</p>
1912<p>
1913 The <code class='flag'>--build_runfile_links</code> flag controls the
1914 construction of the tree of symbolic links (for C++ applications and
1915 tests only). The reasons only C++ non-test rules are affected are numerous
1916 and subtle: C++ builds are more likely to be slower due to runfiles;
1917 no C++ host tools (tools that run during the build) need their runfiles,
1918 so this option can be used by the host configuration; and other rules
1919 (notably Python) need their runfiles for other purposes besides test
1920 execution.
1921</p>
1922
1923<h4 id='flag--discard_analysis_cache'>
1924 <code class='flag'>--[no]discard_analysis_cache</code></h4>
1925<p>
1926 When this option is enabled, Bazel will discard the analysis cache
1927 right before execution starts, thus freeing up additional memory
1928 (around 10%) for the <a href="#execution-phase">execution phase</a>.
1929 The drawback is that further incremental builds will be slower.
1930</p>
1931
1932<h4 id='flag--keep_going'><code class='flag'>--[no]keep_going</code> (-k)</h4>
1933<p>
1934 As in GNU Make, the execution phase of a build stops when the first
1935 error is encountered. Sometimes it is useful to try to build as
1936 much as possible even in the face of errors. This option enables
1937 that behavior, and when it is specified, the build will attempt to
1938 build every target whose prerequisites were successfully built, but
1939 will ignore errors.
1940</p>
1941<p>
1942 While this option is usually associated with the execution phase of
1943 a build, it also effects the analysis phase: if several targets are
1944 specified in a build command, but only some of them can be
1945 successfully analyzed, the build will stop with an error
1946 unless <code class='flag'>--keep_going</code> is specified, in which case the
1947 build will proceed to the execution phase, but only for the targets
1948 that were successfully analyzed.
1949</p>
1950
David Chen8fe82a32016-08-24 10:55:41 +00001951<h4 id='flag--use_ijars'><code class='flag'>--[no]use_ijars</code></h4>
1952<p>
1953 This option changes the way <code>java_library</code> targets are
1954 compiled by Bazel. Instead of using the output of a
1955 <code>java_library</code> for compiling dependent
1956 <code>java_library</code> targets, Bazel will create interface jars
1957 that contain only the signatures of non-private members (public,
1958 protected, and default (package) access methods and fields) and use
1959 the interface jars to compile the dependent targets. This makes it
1960 possible to avoid recompilation when changes are only made to
1961 method bodies or private members of a class.
1962</p>
1963<p>
1964 Note that using <code class='flag'>--use_ijars</code> might give you a different
1965 error message when you are accidentally referring to a non visible
1966 member of another class: Instead of getting an error that the member
1967 is not visible you will get an error that the member does not exist.
1968</p>
1969<p>
1970 Note that changing the <code class='flag'>--use_ijars</code> setting will force
1971 a recompilation of all affected classes.
1972</p>
1973
1974<h4 id='flag--interface_shared_objects'>
1975 <code class='flag'>--[no]interface_shared_objects</code>
1976</h4>
1977<p>
1978 This option enables <i>interface shared objects</i>, which makes binaries and
1979 other shared libraries depend on the <i>interface</i> of a shared object,
1980 rather than its implementation. When only the implementation changes, Bazel
1981 can avoid rebuilding targets that depend on the changed shared library
1982 unnecessarily.
1983</p>
1984
1985<h3 id='output-selection-options'>Output selection options</h3>
1986<p>
1987 These options determine what to build or test.
1988</p>
1989
1990<h4 id="nobuild"><code class='flag'>--[no]build</code></h4>
1991<p>
1992 This option causes the execution phase of the build to occur; it is
1993 on by default. When it is switched off, the execution phase is
1994 skipped, and only the first two phases, loading and analysis, occur.
1995</p>
1996<p>
1997 This option can be useful for validating BUILD files and detecting
1998 errors in the inputs, without actually building anything.
1999</p>
2000
2001<h4 id='flag--build_tests_only'><code class='flag'>--[no]build_tests_only</code></h4>
2002<p>
2003 If specified, Bazel will build only what is necessary to run the *_test
2004 and test_suite rules that were not filtered due to their
2005 <a href='#flag--test_size_filters'>size</a>,
2006 <a href='#flag--test_timeout_filters'>timeout</a>,
2007 <a href='#flag--test_tag_filters'>tag</a>, or
2008 <a href='#flag--test_lang_filters'>language</a>.
2009 If specified, Bazel will ignore other targets specified on the command line.
2010 By default, this option is disabled and Bazel will build everything
2011 requested, including *_test and test_suite rules that are filtered out from
2012 testing. This is useful because running
2013 <code>bazel test --build_tests_only foo/...</code> may not detect all build
2014 breakages in the <code>foo</code> tree.
2015</p>
2016
2017<h4 id='flag--check_up_to_date'><code class='flag'>--[no]check_up_to_date</code></h4>
2018<p>
2019 This option causes Bazel not to perform a build, but merely check
2020 whether all specified targets are up-to-date. If so, the build
2021 completes successfully, as usual. However, if any files are out of
2022 date, instead of being built, an error is reported and the build
2023 fails. This option may be useful to determine whether a build has
2024 been performed more recently than a source edit (e.g. for pre-submit
2025 checks) without incurring the cost of a build.
2026</p>
2027<p>
2028 See also <a href="#flag--check_tests_up_to_date"><code class='flag'>--check_tests_up_to_date</code></a>.
2029</p>
2030
2031<h4 id='flag--compile_one_dependency'><code class='flag'>--[no]compile_one_dependency</code></h4>
2032<p>
2033 Compile a single dependency of the argument files. This is useful for
2034 syntax checking source files in IDEs, for example, by rebuilding a single
2035 target that depends on the source file to detect errors as early as
2036 possible in the edit/build/test cycle. This argument affects the way all
2037 non-flag arguments are interpreted: for each source filename, one
2038 rule that depends on it will be built. For
2039
2040 C++ and Java
2041 sources, rules in the same language space are preferentially chosen. For
2042 multiple rules with the same preference, the one that appears first in the
2043 BUILD file is chosen. An explicitly named target pattern which does not
2044 reference a source file results in an error.
2045</p>
2046
2047<h4 id='flag--save_temps'><code class='flag'>--save_temps</code></h4>
2048<p>
2049 The <code class='flag'>--save_temps</code> option causes temporary outputs from gcc to be saved.
2050 These include .s files (assembler code), .i (preprocessed C) and .ii
2051 (preprocessed C++) files. These outputs are often useful for debugging. Temps will only be
2052 generated for the set of targets specified on the command line.
2053</p>
2054<p>
2055 Note that our implementation of <code class='flag'>--save_temps</code> does not use gcc's
2056 <code>-save-temps</code> flag. Instead, we do two passes, one with <code>-S</code>
2057 and one with <code>-E</code>. A consequence of this is that if your build fails,
2058 Bazel may not yet have produced the ".i" or ".ii" and ".s" files.
2059 If you're trying to use <code class='flag'>--save_temps</code> to debug a failed compilation,
2060 you may need to also use <code class='flag'>--keep_going</code> so that Bazel will still try to
2061 produce the preprocessed files after the compilation fails.
2062</p>
2063<p>
2064 The <code class='flag'>--save_temps</code> flag currently works only for cc_* rules.
2065</p>
2066<p>
2067 To ensure that Bazel prints the location of the additional output files, check that
2068 your <a href='#flag--show_result'><code class='flag'>--show_result <var>n</var></code></a>
2069 setting is high enough.
2070</p>
2071
Googlerd9db1692017-01-05 19:34:30 +00002072<h4 id='flag--build_tag_filters'><code class='flag'>--build_tag_filters <var>tag[,tag]*</var></code></h4>
2073<p>
2074 If specified, Bazel will build only targets that have at least one required tag
2075 (if any of them are specified) and does not have any excluded tags. Build tag
2076 filter is specified as comma delimited list of tag keywords, optionally
2077 preceded with '-' sign used to denote excluded tags. Required tags may also
2078 have a preceding '+' sign.
2079</p>
2080
David Chen8fe82a32016-08-24 10:55:41 +00002081<h4 id='flag--test_size_filters'><code class='flag'>--test_size_filters <var>size[,size]*</var></code></h4>
2082<p>
2083 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
2084 is also specified) only test targets with the given size. Test size filter
2085 is specified as comma delimited list of allowed test size values (small,
2086 medium, large or enormous), optionally preceded with '-' sign used to denote
2087 excluded test sizes. For example,
2088</p>
2089<pre>
2090 % bazel test --test_size_filters=small,medium //foo:all
2091</pre>
2092 and
2093<pre>
2094 % bazel test --test_size_filters=-large,-enormous //foo:all
2095</pre>
2096<p>
2097 will test only small and medium tests inside //foo.
2098</p>
2099<p>
2100 By default, test size filtering is not applied.
2101</p>
2102
2103<h4 id='flag--test_timeout_filters'><code class='flag'>--test_timeout_filters <var>timeout[,timeout]*</var></code></h4>
2104<p>
2105 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
2106 is also specified) only test targets with the given timeout. Test timeout filter
2107 is specified as comma delimited list of allowed test timeout values (short,
2108 moderate, long or eternal), optionally preceded with '-' sign used to denote
2109 excluded test timeouts. See <a href='#flag--test_size_filters'>--test_size_filters</a>
2110 for example syntax.
2111</p>
2112<p>
2113 By default, test timeout filtering is not applied.
2114</p>
2115
2116
2117<h4 id='flag--test_tag_filters'><code class='flag'>--test_tag_filters <var>tag[,tag]*</var></code></h4>
2118<p>
2119 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
2120 is also specified) only test targets that have at least one required tag
2121 (if any of them are specified) and does not have any excluded tags. Test tag
2122 filter is specified as comma delimited list of tag keywords, optionally
2123 preceded with '-' sign used to denote excluded tags. Required tags may also
2124 have a preceding '+' sign.
2125</p>
2126<p>
2127 For example,
2128<pre>
2129 % bazel test --test_tag_filters=performance,stress,-flaky //myproject:all
2130</pre>
2131<p>
2132 will test targets that are tagged with either <code>performance</code> or
2133 <code>stress</code> tag but are <b>not</b> tagged with the <code>flaky</code>
2134 tag.
2135</p>
2136<p>
2137 By default, test tag filtering is not applied. Note that you can also filter
2138 on test's <code>size</code> and <code>local</code> tags in
2139 this manner.
2140</p>
2141
2142<h4 id='flag--test_lang_filters'><code class='flag'>--test_lang_filters <var>lang[,lang]*</var></code></h4>
2143<p>
2144 Specifies a comma-separated list of test languages for languages with an official <code>*_test</code> rule the
2145 (see <a href="be/overview.html">build encyclopedia</a> for a full list of these). Each
2146 language can be optionally preceded with '-' to specify excluded
2147 languages. The name used for each language should be the same as
2148 the language prefix in the <code>*_test</code> rule, for example,
2149 <code>cc</code>, <code>java</code> or <code>sh</code>.
2150</p>
2151<p>
2152 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
2153 is also specified) only test targets of the specified language(s).
2154</p>
2155<p>
2156 For example,
2157</p>
2158<pre>
2159 % bazel test --test_lang_filters=cc,java foo/...
2160</pre>
2161<p>
2162 will test only the C/C++ and Java tests (defined using
2163 <code>cc_test</code> and <code>java_test</code> rules, respectively)
2164 in <code>foo/...</code>, while
2165</p>
2166<pre>
2167 % bazel test --test_lang_filters=-sh,-java foo/...
2168</pre>
2169<p>
2170 will run all of the tests in <code>foo/...</code> except for the
2171 <code>sh_test</code> and <code>java_test</code> tests.
2172</p>
2173<p>
2174 By default, test language filtering is not applied.
2175</p>
2176
2177<h4 id="flag--test_filter"><code class='flag'>--test_filter=<var>filter-expression</var></code></h4>
2178<p>
2179 Specifies a filter that the test runner may use to pick a subset of tests for
2180 running. All targets specified in the invocation are built, but depending on
2181 the expression only some of them may be executed; in some cases, only certain
2182 test methods are run.
2183</p>
2184<p>
2185 The particular interpretation of <var>filter-expression</var> is up to
2186 the test framework responsible for running the test. It may be a glob,
2187 substring, or regexp. <code class='flag'>--test_filter</code> is a convenience
2188 over passing different <code class='flag'>--test_arg</code> filter arguments,
2189 but not all frameworks support it.
2190</p>
2191
2192<h3>Verbosity options: options that control what Bazel prints</h3>
2193
2194These options control the verbosity of Bazel's output,
2195either to the terminal, or to additional log files.
2196
2197<h4 id='flag--explain'><code class='flag'>--explain <var>logfile</var></code></h4>
2198<p>
2199 This option, which requires a filename argument, causes the
2200 dependency checker in <code>bazel build</code>'s execution phase to
2201 explain, for each build step, either why it is being executed, or
2202 that it is up-to-date. The explanation is written
2203 to <i>logfile</i>.
2204</p>
2205<p>
2206 If you are encountering unexpected rebuilds, this option can help to
2207 understand the reason. Add it to your <code>.bazelrc</code> so that
2208 logging occurs for all subsequent builds, and then inspect the log
2209 when you see an execution step executed unexpectedly. This option
2210 may carry a small performance penalty, so you might want to remove
2211 it when it is no longer needed.
2212</p>
2213
2214<h4 id='flag--verbose_explanations'><code class='flag'>--verbose_explanations</code></h4>
2215<p>
2216 This option increases the verbosity of the explanations generated
2217 when the <a href='#flag--explain'>--explain</a> option is enabled.
2218</p>
2219<p>
2220 In particular, if verbose explanations are enabled,
2221 and an output file is rebuilt because the command used to
2222 build it has changed, then the output in the explanation file will
2223 include the full details of the new command (at least for most
2224 commands).
2225</p>
2226<p>
2227 Using this option may significantly increase the length of the
2228 generated explanation file and the performance penalty of using
2229 <code class='flag'>--explain</code>.
2230</p>
2231<p>
2232 If <code class='flag'>--explain</code> is not enabled, then
2233 <code class='flag'>--verbose_explanations</code> has no effect.
2234</p>
2235
2236<h4 id='flag--profile'><code class='flag'>--profile <var>file</var></code></h4>
2237<p>
2238 This option, which takes a filename argument, causes Bazel to write
2239 profiling data into a file. The data then can be analyzed or parsed using the
2240 <code>bazel analyze-profile</code> command. The Build profile can be useful in
2241 understanding where Bazel's <code>build</code> command is spending its time.
2242</p>
2243
2244<h4 id='flag--show_loading_progress'><code class='flag'>--[no]show_loading_progress</code></h4>
2245<p>
2246 This option causes Bazel to output package-loading progress
2247 messages. If it is disabled, the messages won't be shown.
2248</p>
2249
2250<h4 id='flag--show_progress'><code class='flag'>--[no]show_progress</code></h4>
2251<p>
2252 This option causes progress messages to be displayed; it is on by
2253 default. When disabled, progress messages are suppressed.
2254</p>
2255
2256<h4 id='flag--show_progress_rate_limit'><code class='flag'>--show_progress_rate_limit
2257 <var>n</var></code></h4>
2258<p>
2259 This option causes bazel to display only
2260 one progress message per <code>n</code> seconds, where <var>n</var> is a real number.
2261 If <code>n</code> is -1, all progress messages will be displayed. The default value for
2262 this option is 0.03, meaning bazel will limit the progress messages to one per every
2263 0.03 seconds.
2264</p>
2265
2266<h4 id='flag--show_result'><code class='flag'>--show_result <var>n</var></code></h4>
2267<p>
2268 This option controls the printing of result information at the end
2269 of a <code>bazel build</code> command. By default, if a single
2270 build target was specified, Bazel prints a message stating whether
2271 or not the target was successfully brought up-to-date, and if so,
2272 the list of output files that the target created. If multiple
2273 targets were specified, result information is not displayed.
2274</p>
2275<p>
2276 While the result information may be useful for builds of a single
2277 target or a few targets, for large builds (e.g. an entire top-level
2278 project tree), this information can be overwhelming and distracting;
2279 this option allows it to be controlled. <code class='flag'>--show_result</code>
2280 takes an integer argument, which is the maximum number of targets
2281 for which full result information should be printed. By default,
2282 the value is 1. Above this threshold, no result information is
2283 shown for individual targets. Thus zero causes the result
2284 information to be suppressed always, and a very large value causes
2285 the result to be printed always.
2286</p>
2287<p>
2288 Users may wish to choose a value in-between if they regularly
2289 alternate between building a small group of targets (for example,
2290 during the compile-edit-test cycle) and a large group of targets
2291 (for example, when establishing a new workspace or running
2292 regression tests). In the former case, the result information is
2293 very useful whereas in the latter case it is less so. As with all
2294 options, this can be specified implicitly via
2295 the <a href='#bazelrc'><code>.bazelrc</code></a> file.
2296</p>
2297<p>
2298 The files are printed so as to make it easy to copy and paste the
2299 filename to the shell, to run built executables. The "up-to-date"
2300 or "failed" messages for each target can be easily parsed by scripts
2301 which drive a build.
2302</p>
2303
2304<h4 id='flag--subcommands'><code class='flag'>--subcommands</code> (<code>-s</code>)</h4>
2305<p>
2306 This option causes Bazel's execution phase to print the full command line
2307 for each command prior to executing it.
2308</p>
2309
2310<pre>
2311 &gt;&gt;&gt;&gt;&gt; # //examples/cpp:hello-world [action 'Linking examples/cpp/hello-world']
2312 (cd /home/jrluser/.cache/bazel/_bazel_jrluser/4c084335afceb392cfbe7c31afee3a9f/bazel && \
2313 exec env - \
lberki724706b2017-04-13 08:54:10 +00002314 /usr/bin/gcc -o bazel-out/local_linux-fastbuild/bin/examples/cpp/hello-world -B/usr/bin/ -Wl,-z,relro,-z,now -no-canonical-prefixes -pass-exit-codes -Wl,-S -Wl,@bazel-out/local_linux-fastbuild/bin/examples/cpp/hello-world-2.params)
David Chen8fe82a32016-08-24 10:55:41 +00002315</pre>
2316<p>
2317 Where possible, commands are printed in a Bourne shell compatible syntax,
2318 so that they can be easily copied and pasted to a shell command prompt.
2319 (The surrounding parentheses are provided to protect your shell from the
2320 <code>cd</code> and <code>exec</code> calls; be sure to copy them!)
2321 However some commands are implemented internally within Bazel, such as
2322 creating symlink trees. For these there's no command line to display.
2323
2324</p>
2325
2326<p>
2327 See also <a href="#flag--verbose_failures">--verbose_failures</a>, below.
2328</p>
2329
2330<h4 id='flag--verbose_failures'><code class='flag'>--verbose_failures</code></h4>
2331<p>
2332 This option causes Bazel's execution phase to print the full command line
2333 for commands that failed. This can be invaluable for debugging a
2334 failing build.
2335</p>
2336<p>
2337 Failing commands are printed in a Bourne shell compatible syntax, suitable
2338 for copying and pasting to a shell prompt.
2339</p>
2340
2341<h4 id='flag--stamp'><code class='flag'>--[no]stamp</code></h4>
2342<p>
2343 This option controls whether stamping is enabled for
2344 rule types that support it. For most of the supported rule types stamping is
2345 enabled by default (e.g. <code>cc_binary</code>).
2346
2347 By default, stamping is disabled for all tests. Specifying
2348 <code class='flag'>--stamp</code> does not force affected targets to be rebuilt,
2349 if their dependencies have not changed.
2350</p>
2351
2352<p>
2353 Stamping can be enabled or disabled explicitly in BUILD using
2354 the <code>stamp</code> attribute of certain rule types, please refer to
2355 the <a href="be/overview.html">build encyclopedia</a> for details. For
2356 rules that are neither explicitly or implicitly configured as <code>stamp =
2357 0</code> or <code>stamp = 1</code>, the <code class='flag'>--[no]stamp</code> option
2358 selects whether stamping is enabled. Bazel never stamps binaries that are
2359 built for the host configuration, regardless of the stamp attribute.
2360</p>
2361
2362<h3 id='misc_build_options'>Miscellaneous options</h3>
2363
2364<h4 id='flag--symlink_prefix'><code class='flag'>--symlink_prefix <var>string</var></code></h4>
2365<p>
2366 Changes the prefix of the generated convenience symlinks. The
2367 default value for the symlink prefix is <code>bazel-</code> which
2368 will create the symlinks <code>bazel-bin</code>, <code>bazel-testlogs</code>, and
2369 <code>bazel-genfiles</code>.
2370</p>
2371<p>
2372 If the symbolic links cannot be created for any reason, a warning is
2373 issued but the build is still considered a success. In particular,
2374 this allows you to build in a read-only directory or one that you have no
2375 permission to write into. Any paths printed in informational
2376 messages at the conclusion of a build will only use the
2377 symlink-relative short form if the symlinks point to the expected
2378 location; in other words, you can rely on the correctness of those
2379 paths, even if you cannot rely on the symlinks being created.
2380</p>
2381<p>
2382 Some common values of this option:
2383</p>
2384<ul>
2385
2386 <li>
2387 <p><b>Suppress symlink creation:</b>
2388 <code class='flag'>--symlink_prefix=/</code> will cause Bazel to not
2389 create or update any symlinks, including the <code>bazel-out</code> and
2390
2391 <code>bazel-&lt;workspace&gt;</code>
2392 symlinks. Use this option to suppress symlink creation entirely.
2393 </p>
2394 </li>
2395 <li>
2396 <p><b>Reduce clutter:</b>
2397 <code class='flag'>--symlink_prefix=.bazel/</code> will cause Bazel to create
2398 symlinks called <code>bin</code> (etc) inside a hidden directory <code>.bazel</code>.
2399 </p>
2400 </li>
2401</ul>
2402
2403<h4 id='flag--platform_suffix'><code class='flag'>--platform_suffix <var>string</var></code></h4>
2404<p>
2405 Adds a suffix to the configuration short name, which is used to determine the
2406 output directory. Setting this option to different values puts the files into
2407 different directories, for example to improve cache hit rates for builds that
2408 otherwise clobber each others output files, or to keep the output files around
2409 for comparisons.
2410</p>
2411
2412<h4 id='flag--default_visibility'><code class='flag'>--default_visibility=<var>(private|public)</var></code></h4>
2413<p>
2414 Temporary flag for testing bazel default visibility changes. Not intended for general use
2415 but documented for completeness' sake.
2416</p>
2417
2418<h2 id='bazel-releng'>Using Bazel for releases</h2>
2419<p>
2420 Bazel is used both by software engineers during the development
2421 cycle, and by release engineers when preparing binaries for deployment
2422 to production. This section provides a list of tips for release
2423 engineers using Bazel.
2424
2425</p>
2426
2427<h3>Significant options</h3>
2428
2429<p>
2430 When using Bazel for release builds, the same issues arise as for
2431 other scripts that perform a build, so you should read
2432 the <a href='#scripting'>scripting</a> section of this manual.
2433 In particular, the following options are strongly recommended:
2434</p>
2435<ul>
2436 <li><a href='#bazelrc'><code class='flag'>--bazelrc=/dev/null</code></a></li>
2437 <li><a href='#flag--batch'><code class='flag'>--batch</code></a></li>
2438</ul>
2439
2440<p>
2441 These options (q.v.) are also important:
2442</p>
2443
2444<ul>
2445
2446 <li><a href='#flag--package_path'><code class='flag'>--package_path</code></a></li>
2447 <li><a href='#flag--symlink_prefix'><code class='flag'>--symlink_prefix</code></a>:
2448 for managing builds for multiple configurations,
2449 it may be convenient to distinguish each build
2450 with a distinct identifier, e.g. "64bit" vs. "32bit". This option
2451 differentiates the <code>bazel-bin</code> (etc.) symlinks.
2452 </li>
2453</ul>
2454
2455<h2 id='test'>Running tests with Bazel</h2>
2456<p>
2457 To build and run tests with bazel, type <code>bazel test</code> followed by
2458 the name of the test targets.
2459</p>
2460<p>
2461 By default, this command performs simultaneous build and test
2462 activity, building all specified targets (including any non-test
2463 targets specified on the command line) and testing
2464 <code>*_test</code> and <code>test_suite</code> targets as soon as
2465 their prerequisites are built, meaning that test execution is
2466 interleaved with building. Doing so usually results in significant
2467 speed gains.
2468
2469</p>
2470
2471<h3>Options for <code>bazel test</code></h3>
2472
2473<h4 id="flag--cache_test_results"><code class='flag'>--cache_test_results=(yes|no|auto)</code> (<code>-t</code>)</h4>
2474<p>
2475 If this option is set to 'auto' (the default) then Bazel will only rerun a test if any of the
2476 following conditions applies:
2477</p>
2478<ul>
2479 <li>Bazel detects changes in the test or its dependencies</li>
2480 <li>the test is marked as <code>external</code></li>
2481 <li>multiple test runs were requested with <code class='flag'>--runs_per_test</code></li>
2482 <li>the test failed.</li>
2483</ul>
2484<p>
2485 If 'no', all tests will be executed unconditionally.
2486</p>
2487<p>
2488 If 'yes', the caching behavior will be the same as auto
2489 except that it may cache test failures and test runs with
2490 <code class='flag'>--runs_per_test</code>.
2491</p>
2492<p>
2493 Note that test results are <em>always</em> saved in Bazel's output tree,
2494 regardless of whether this option is enabled, so
2495 you needn't have used <code class='flag'>--cache_test_results</code> on the
2496 prior run(s) of <code>bazel test</code> in order to get cache hits.
2497 The option only affects whether Bazel will <em>use</em> previously
2498 saved results, not whether it will save results of the current run.
2499</p>
2500<p>
2501 Users who have enabled this option by default in
2502 their <code>.bazelrc</code> file may find the
2503 abbreviations <code>-t</code> (on) or <code>-t-</code> (off)
2504 convenient for overriding the default on a particular run.
2505</p>
2506
2507<h4 id="flag--check_tests_up_to_date"><code class='flag'>--check_tests_up_to_date</code></h4>
2508<p>
2509 This option tells Bazel not to run the tests, but to merely check and report
2510 the cached test results. If there are any tests which have not been
2511 previously built and run, or whose tests results are out-of-date (e.g. because
2512 the source code or the build options have changed), then Bazel will report
2513 an error message ("test result is not up-to-date"), will record the test's
2514 status as "NO STATUS" (in red, if color output is enabled), and will return
2515 a non-zero exit code.
2516</p>
2517<p>
2518 This option also implies
2519 <code><a href="#flag--check_up_to_date">--check_up_to_date</a></code> behavior.
2520</p>
2521<p>
2522 This option may be useful for pre-submit checks.
2523</p>
2524
2525<h4 id="flag--test_verbose_timeout_warnings"><code class='flag'>--test_verbose_timeout_warnings</code></h4>
2526<p>
2527 This option tells Bazel to explicitly warn the user if a test's timeout is
2528significantly longer then the test's actual execution time. While a test's
2529timeout should be set such that it is not flaky, a test that has a highly
2530over-generous timeout can hide real problems that crop up unexpectedly.
2531</p>
2532<p>
2533For instance, a test that normally executes in a minute or two should not have
2534a timeout of ETERNAL or LONG as these are much, much too generous.
2535
2536 This option is useful to help users decide on a good timeout value or
2537 sanity check existing timeout values.
2538</p>
2539<p>
2540Note that each test shard is allotted the timeout of the entire
2541<code>XX_test</code> target. Using this option does not affect a test's timeout
2542value, merely warns if Bazel thinks the timeout could be restricted further.
2543</p>
2544
2545<h4 id='flag--test_keep_going'><code class='flag'>--[no]test_keep_going</code></h4>
2546<p>
2547 By default, all tests are run to completion. If this flag is disabled,
2548 however, the build is aborted on any non-passing test. Subsequent build steps
2549 and test invocations are not run, and in-flight invocations are canceled.
2550 Do not specify both <code class='flag'>--notest_keep_going</code> and
2551 <code class='flag'>--keep_going</code>.
2552</p>
2553
2554<h4 id='flag--flaky_test_attempts'><code class='flag'>--flaky_test_attempts <var>attempts</var></code></h4>
2555<p>
2556 This option specifies the maximum number of times a test should be attempted
2557 if it fails for any reason. A test that initially fails but eventually
2558 succeeds is reported as <code>FLAKY</code> on the test summary. It is,
2559 however, considered to be passed when it comes to identifying Bazel exit code
2560 or total number of passed tests. Tests that fail all allowed attempts are
2561 considered to be failed.
2562</p>
2563<p>
2564 By default (when this option is not specified, or when it is set to
2565 &quot;default&quot;), only a single attempt is allowed for regular tests, and
2566 3 for test rules with the <code>flaky</code> attribute set. You can specify
2567 an integer value to override the maximum limit of test attempts. Bazel allows
2568 a maximum of 10 test attempts in order to prevent abuse of the system.
2569</p>
2570
2571<h4 id='flag--runs_per_test'><code class='flag'>--runs_per_test <var>[regex@]number</var></code></h4>
2572<p>
2573 This option specifies the number of times each test should be executed. All
2574 test executions are treated as separate tests (e.g. fallback functionality
2575 will apply to each of them independently).
2576</p>
2577<p>
2578 The status of a target with failing runs depends on the value of the
2579 <code>--runs_per_test_detects_flakes</code> flag:
2580</p>
2581<ul>
2582 <li>If absent, any failing run causes the entire test to fail.</li>
2583 <li>If present and two runs from the same shard return PASS and FAIL, the test
2584 will receive a status of flaky (unless other failing runs cause it to
2585 fail).</li>
2586</ul>
2587
2588<p>
2589 If a single number is specified, all tests will run that many times.
2590 Alternatively, a regular expression may be specified using the syntax
2591 regex@number. This constrains the effect of --runs_per_test to targets
2592 which match the regex (e.g. "--runs_per_test=^//pizza:.*@4" runs all tests
2593 under //pizza/ 4 times).
2594 This form of --runs_per_test may be specified more than once.
2595</p>
2596
2597<h4 id='flag--runs_per_test_detects_flakes'><code
2598 class='flag'>--[no]runs_per_test_detects_flakes</code></h4>
2599<p>
2600 If this option is specified (by default it is not), Bazel will detect flaky
2601 test shards through --runs_per_test. If one or more runs for a single shard
2602 fail and one or more runs for the same shard pass, the target will be
2603 considered flaky with the flag. If unspecified, the target will report a
2604 failing status.
2605</p>
2606
2607<h4 id='flag--test_summary'><code class='flag'>--test_summary <var>output_style</var></code></h4>
2608<p>
2609 Specifies how the test result summary should be displayed.
2610</p>
2611<ul>
2612 <li><code>short</code> prints the results of each test along with the name of
2613 the file containing the test output if the test failed. This is the default
2614 value.
2615 </li>
2616 <li><code>terse</code> like <code>short</code>, but even shorter: only print
2617 information about tests which did not pass.
2618 </li>
2619 <li><code>detailed</code> prints each individual test case that failed, not
2620 only each test. The names of test output files are omitted.
2621 </li>
2622 <li><code>none</code> does not print test summary.
2623 </li>
2624</ul>
2625
2626<h4 id='flag--test_output'><code class='flag'>--test_output <var>output_style</var></code></h4>
2627<p>
2628 Specifies how test output should be displayed:
2629</p>
2630<ul>
2631 <li><code>summary</code> shows a summary of whether each test passed or
2632 failed. Also shows the output log file name for failed tests. The summary
2633 will be printed at the end of the build (during the build, one would see
2634 just simple progress messages when tests start, pass or fail).
2635 This is the default behavior.
2636 </li>
2637 <li><code>errors</code> sends combined stdout/stderr output from failed tests
2638 only into the stdout immediately after test is completed, ensuring that
2639 test output from simultaneous tests is not interleaved with each other.
2640 Prints a summary at the build as per summary output above.
2641 </li>
2642 <li><code>all</code> is similar to <code>errors</code> but prints output for
2643 all tests, including those which passed.
2644 </li>
2645 <li><code>streamed</code> streams stdout/stderr output from each test in
2646 real-time.
2647
2648 </li>
2649</ul>
2650
2651<h4 id='flag--java_debug'><code class='flag'>--java_debug</code></h4>
2652<p>
2653 This option causes the Java virtual machine of a java test to wait for a connection from a
2654 JDWP-compliant debugger before starting the test. This option implies --test_output=streamed.
2655</p>
2656
2657<h4 id='flag--verbose_test_summary'><code class='flag'>--[no]verbose_test_summary</code></h4>
2658<p>
2659 By default this option is enabled, causing test times and other additional
2660 information (such as test attempts) to be printed to the test summary. If
2661 <code class='flag'>--noverbose_test_summary</code> is specified, test summary will
2662 include only test name, test status and cached test indicator and will
2663 be formatted to stay within 80 characters when possible.
2664</p>
2665
2666<h4 id='flag--test_tmpdir'><code class='flag'>--test_tmpdir <var>path</var></code></h4>
2667<p>
2668 Specifies temporary directory for tests executed locally. Each test will be
2669 executed in a separate subdirectory inside this directory. The directory will
2670 be cleaned at the beginning of the each <code>bazel test</code> command.
2671 By default, bazel will place this directory under Bazel output base directory.
2672 Note that this is a directory for running tests, not storing test results
2673 (those are always stored under the <code>bazel-out</code> directory).
2674</p>
2675
2676<h4 id='flag--test_timeout'>
2677 <code class='flag'>--test_timeout
2678 <var>seconds</var></code>
2679 OR
2680 <code class='flag'>--test_timeout
2681 <var>seconds</var>,<var>seconds</var>,<var>seconds</var>,<var>seconds</var>
2682 </code>
2683</h4>
2684<p>
2685 Overrides the timeout value for all tests by using specified number of
2686 seconds as a new timeout value. If only one value is provided, then it will
2687 be used for all test timeout categories.
2688 </p>
2689 <p>
2690 Alternatively, four comma-separated values may be provided, specifying
2691 individual timeouts for short, moderate, long and eternal tests (in that
2692 order).
2693 In either form, zero or a negative value for any of the test sizes will
2694 be substituted by the default timeout for the given timeout categories as
2695 defined by the page
2696 <a href="test-encyclopedia.html">Writing Tests</a>.
2697 By default, Bazel will use these timeouts for all tests by
2698 inferring the timeout limit from the test's size whether the size is
2699 implicitly or explicitly set.
2700</p>
2701<p>
2702 Tests which explicitly state their timeout category as distinct from their
2703 size will receive the same value as if that timeout had been implicitly set by
2704 the size tag. So a test of size 'small' which declares a 'long' timeout will
2705 have the same effective timeout that a 'large' tests has with no explicit
2706 timeout.
2707</p>
2708
2709<h4 id='flag--test_arg'><code class='flag'>--test_arg <var>arg</var></code></h4>
2710<p>
Michael Staib3df106e2017-02-23 23:08:40 +00002711 Passes command-line options/flags/arguments to each test process. This
David Chen8fe82a32016-08-24 10:55:41 +00002712 option can be used multiple times to pass several arguments, e.g.
2713 <code class='flag'>--test_arg=--logtostderr --test_arg=--v=3</code>.
2714</p>
2715
2716<h4 id='flag--test_env'><code class='flag'>--test_env <var>variable</var>=<i>value</i></code>
2717 OR
2718 <code class='flag'>--test_env <var>variable</var></code></h4>
2719<p>
2720 Specifies additional variables that must be injected into the test
2721 environment for each test. If <var>value</var> is not specified it will be
2722 inherited from the shell environment used to start the <code>bazel test</code>
2723 command.
2724</p>
2725<p>
2726 The environment can be accessed from within a test by using
2727 <code>System.getenv("var")</code> (Java),
2728 <code>getenv("var")</code> (C or C++),
2729
2730</p>
2731
2732<h4 id="flag--run_under"><code class='flag'>--run_under=<var>command-prefix</var></code></h4>
2733<p>
2734 This specifies a prefix that the test runner will insert in front
2735 of the test command before running it. The
2736 <var>command-prefix</var> is split into words using Bourne shell
2737 tokenization rules, and then the list of words is prepended to the
2738 command that will be executed.
2739</p>
2740<p>
2741 If the first word is a fully qualified label (i.e. starts with
2742 <code>//</code>) it is built. Then the label is substituted by the
2743 corresponding executable location that is prepended to the command
2744 that will be executed along with the other words.
2745</p>
2746
2747<p>
2748Some caveats apply:
2749</p>
2750<ul>
2751 <li>
2752 The PATH used for running tests may be different than the PATH in your environment,
2753 so you may need to use an <b>absolute path</b> for the <code class='flag'>--run_under</code>
2754 command (the first word in <var>command-prefix</var>).
2755 </li>
2756 <li>
2757 <b><code>stdin</code> is not connected</b>, so <code class='flag'>--run_under</code>
2758 can't be used for interactive commands.
2759 </li>
2760
2761</ul>
2762<p>
2763Examples:
2764</p>
2765<pre>
2766 --run_under=/usr/bin/valgrind
2767 --run_under=/usr/bin/strace
2768 --run_under='/usr/bin/strace -c'
2769 --run_under='/usr/bin/valgrind --quiet --num-callers=20'
2770
2771</pre>
2772
2773<h4>Test selection</h4>
2774<p>
2775 As documented under <a href='#output-selection-options'>Output selection options</a>,
2776 you can filter tests by <a href='#flag--test_size_filters'>size</a>,
2777 <a href='#flag--test_timeout_filters'>timeout</a>,
2778 <a href='#flag--test_tag_filters'>tag</a>, or
2779 <a href='#flag--test_lang_filters'>language</a>. A convenience
2780 <a href='#flag--test_filter'>general name filter</a> can forward particular
2781 filter args to the test runner.
2782</p>
2783
kchodorowfae5c742017-03-28 16:53:24 +00002784<h4 id="other_options_for_bazel_test">Other options for <code>bazel test</code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00002785<p>
2786 The syntax and the remaining options are exactly like
2787 <a href='#build'>bazel build</a>.
2788</p>
2789
2790
2791
2792<h2 id='run'>Running executables with Bazel</h2>
2793<p>
2794 The <code>bazel run</code> command is similar to <code>bazel build</code>, except
2795 it is used to build and run a single target. Here is a typical session:
2796</p>
2797<pre>
2798 % bazel run -- java/myapp:myapp --arg1 --arg2
2799 Welcome to Bazel
2800 INFO: Loading package: java/myapp
2801 INFO: Loading package: foo/bar
2802 INFO: Loading complete. Analyzing...
2803 INFO: Found 1 target...
2804 ...
2805 Target //java/myapp:myapp up-to-date:
2806 bazel-bin/java/myapp:myapp
2807 INFO: Elapsed time: 0.638s, Critical Path: 0.34s
2808
2809 INFO: Running command line: bazel-bin/java/myapp:myapp --arg1 --arg2
2810 Hello there
2811 $EXEC_ROOT/java/myapp/myapp
2812 --arg1
2813 --arg2
2814</pre>
2815
2816<p>
2817 Bazel closes stdin, so you can't use <code>bazel run</code>
2818 if you want to start an interactive program or pipe data to it.
2819</p>
2820
2821<p>
2822 Note the use of the <code>--</code>. This is needed so that Bazel
2823 does not interpret <code>--arg1</code> and <code>--arg2</code> as
2824 Bazel options, but rather as part of the command line for running the binary.
2825 (The program being run simply says hello and prints out its args.)
2826</p>
2827
2828<h3>Options for <code>bazel run</code></h3>
2829
2830<h4 id='flag--run_under_run'><code class='flag'>--run_under=<var>command-prefix</var></code></h4>
2831<p>
2832 This has the same effect as the <code class='flag'>--run_under</code> option for
2833 <code>bazel test</code> (<a href='#flag--run_under'>see above</a>),
2834 except that it applies to the command being run by <code>bazel
2835 run</code> rather than to the tests being run by <code>bazel test</code>
2836 and cannot run under label.
2837</p>
2838
2839<h3>Executing tests</h3>
2840
2841<p>
2842 <code>bazel run</code> can also execute test binaries, which has the effect of
2843running the test, but without the setup documented on the page
2844<a href='test-encyclopedia.html'>Writing Tests</a>, so that the test runs
2845in an environment closer to the current shell environment. Note that none of the
2846--test_* arguments have an effect when running a test in this manner.
2847</p>
2848
2849<h2 id='query'>Querying the dependency graph with Bazel</h2>
2850
2851<p>
2852 Bazel includes a query language for asking questions about the
2853 dependency graph used during the build. The query tool is an
2854 invaluable aid to many software engineering tasks.
2855</p>
2856<p>
2857 The query language is based on the idea of
2858 algebraic operations over graphs; it is documented in detail in
2859
2860 <a href="query.html">Bazel Query Reference</a>.
2861 Please refer to that document for reference, for
2862 examples, and for query-specific command-line options.
2863</p>
2864
2865<p>
2866 The query tool accepts several command-line
2867 option. <code class='flag'>--output</code> selects the output format.
2868 <code class='flag'>--[no]keep_going</code> (disabled by default) causes the query
2869 tool to continue to make progress upon errors; this behavior may be
2870 disabled if an incomplete result is not acceptable in case of errors.
2871</p>
2872<p>
2873 The <code class='flag'>--[no]host_deps</code> option,
2874 enabled by default, causes dependencies on "host
2875 configuration" targets to be included in the dependency graph over
2876 which the query operates.
2877
2878</p>
2879<p>
2880 The <code class='flag'>--[no]implicit_deps</code> option, enabled by default, causes
2881 implicit dependencies to be included in the dependency graph over which the query operates. An
2882 implicit dependency is one that is not explicitly specified in the BUILD file
2883 but added by bazel.
2884</p>
2885<p>
2886 Example: "Show the locations of the definitions (in BUILD files) of
2887 all genrules required to build all the tests in the PEBL tree."
2888</p>
2889<pre>
2890 bazel query --output location 'kind(genrule, deps(kind(".*_test rule", foo/bar/pebl/...)))'
2891</pre>
2892
2893
2894<h2 id='misc'>Miscellaneous Bazel commands and options</h2>
2895
2896<h3 id='help'>The <code>help</code> command</h3>
2897
2898<p>
2899 The <code>help</code> command provides on-line help. By default, it
2900 shows a summary of available commands and help topics, as shown in
2901 the <a href='#overview'><i>Bazel overview</i></a> section above.
2902 Specifying an argument displays detailed help for a particular
2903 topic. Most topics are Bazel commands, e.g. <code>build</code>
2904 or <code>query</code>, but there are some additional help topics
2905 that do not correspond to commands.
2906</p>
2907
2908<h4 id='flag--long'><code class='flag'>--[no]long</code> (<code>-l</code>)</h4>
2909<p>
2910 By default, <code>bazel help [<var>topic</var>]</code> prints only a
2911 summary of the relevant options for a topic. If
2912 the <code class='flag'>--long</code> option is specified, the type, default value
2913 and full description of each option is also printed.
2914</p>
2915
2916<h3 id='shutdown'>The <code>shutdown</code> command</h3>
2917
2918<p>
2919 Bazel server processes (see <a href='#client/server'>Client/server
2920 implementation</a>) may be stopped by using the <code>shutdown</code>
2921 command. This command causes the Bazel server to exit as soon as it
2922 becomes idle (i.e. after the completion of any builds or other
2923 commands that are currently in progress).
2924
2925 Bazel servers stop themselves after an idle timeout, so this command
2926 is rarely necessary; however, it can be useful in scripts when it is
2927 known that no further builds will occur in a given workspace.
2928</p>
2929<p>
2930 <code>shutdown</code> accepts one
2931 option, <code class='flag'>--iff_heap_size_greater_than <i>n</i></code>, which
2932 requires an integer argument (in MB). If specified, this makes the shutdown
2933 conditional on the amount of memory already consumed. This is
2934 useful for scripts that initiate a lot of builds, as any memory
2935 leaks in the Bazel server could cause it to crash spuriously on
2936 occasion; performing a conditional restart preempts this condition.
2937</p>
2938
2939<h3 id='info'>The <code>info</code> command</h3>
2940
2941<p>
2942 The <code>info</code> command prints various values associated with
2943 the Bazel server instance, or with a specific build configuration.
2944 (These may be used by scripts that drive a build.)
2945</p>
2946
2947<p>
2948 The <code>info</code> command also permits a single (optional)
2949 argument, which is the name of one of the keys in the list below.
2950 In this case, <code>bazel info <var>key</var></code> will print only
2951 the value for that one key. (This is especially convenient when
2952 scripting Bazel, as it avoids the need to pipe the result
2953 through <code>sed -ne /key:/s/key://p</code>:
2954</p>
2955
2956<h4>Configuration-independent data</h4>
2957<ul>
2958 <li><code>release</code>: the release label for this Bazel
2959 instance, or "development version" if this is not a released
2960 binary.
2961 </li>
2962 <li><code>workspace</code> the absolute path to the base workspace
2963 directory.
2964 </li>
2965 <li><code>install_base</code>: the absolute path to the installation
2966 directory used by this Bazel instance for the current user. Bazel
2967 installs its internally required executables below this directory.
2968
2969 </li>
2970 <li><code>output_base</code>: the absolute path to the base output
2971 directory used by this Bazel instance for the current user and
2972 workspace combination. Bazel puts all of its scratch and build
2973 output below this directory.
2974 </li>
2975 <li><code>execution_root</code>: the absolute path to the execution
2976 root directory under output_base. This directory is the root for all files
2977 accessible to commands executed during the build, and is the working
2978 directory for those commands. If the workspace directory is writable, a
2979 symlink named
2980
2981 <code>bazel-&lt;workspace&gt;</code>
2982 is placed there pointing to this directory.
2983 </li>
2984 <li><code>output_path</code>: the absolute path to the output
2985 directory beneath the execution root used for all files actually
2986 generated as a result of build commands. If the workspace directory is
2987 writable, a symlink named <code>bazel-out</code> is placed there pointing
2988 to this directory.
2989 </li>
2990 <li><code>server_pid</code>: the process ID of the Bazel server
2991 process. </li>
2992 <li><code>command_log</code>: the absolute path to the command log file;
2993 this contains the interleaved stdout and stderr streams of the most recent
2994 Bazel command. Note that running <code>bazel info</code> will overwrite the
2995 contents of this file, since it then becomes the most recent Bazel command.
2996 However, the location of the command log file will not change unless you
2997 change the setting of the <code class='flag'>--output_base</code> or
2998 <code class='flag'>--output_user_root</code> options.
2999 </li>
3000
3001 <li><code>used-heap-size</code>,
3002 <code>committed-size</code>,
3003 <code>max-heap-size</code>: reports various JVM heap size
3004 parameters. Respectively: memory currently used, memory currently
3005 guaranteed to be available to the JVM from the system, maximum
3006 possible allocation.
3007 </li>
3008 <li><code>gc-count</code>, <code>gc-time</code>: The cumulative count of
3009 garbage collections since the start of this Bazel server and the time spent
3010 to perform them. Note that these values are not reset at the start of every
3011 build.
3012 </li>
3013 <li><code>package_path</code>: A colon-separated list of paths which would be
3014 searched for packages by bazel. Has the same format as the
3015 <code class='flag'>--package_path</code> build command line argument.
3016 </li>
3017</ul>
3018<p>
3019 Example: the process ID of the Bazel server.
3020</p>
3021<pre>% bazel info server_pid
30221285
3023</pre>
3024
3025<h4>Configuration-specific data</h4>
3026<p>
3027 These data may be affected by the configuration options passed
3028 to <code>bazel info</code>, for
3029 example <code class='flag'>--cpu</code>, <code class='flag'>--compilation_mode</code>,
3030 etc. The <code>info</code> command accepts all
3031 the <a href='#analysis-options'>options that control dependency
3032 analysis</a>, since some of these determine the location of the
3033 output directory of a build, the choice of compiler, etc.
3034</p>
3035<ul>
3036 <li>
3037 <code>bazel-bin</code>, <code>bazel-testlogs</code>,
3038 <code>bazel-genfiles</code>: reports the absolute path to
3039 the <code>bazel-*</code> directories in which programs generated by the
3040 build are located. This is usually, though not always, the same as
3041 the <code>bazel-*</code> symlinks created in the base workspace directory after a
3042 successful build. However, if the workspace directory is read-only,
3043 no <code>bazel-*</code> symlinks can be created. Scripts that use
3044 the value reported by <code>bazel info</code>, instead of assuming the
3045 existence of the symlink, will be more robust.
3046 </li>
3047 <li>
3048 The complete
3049 <a href='be/make-variables.html'
3050 >"Make" environment</a>. If the <code class='flag'>--show_make_env</code> flag is
3051 specified, all variables in the current configuration's "Make" environment
3052 are also displayed (e.g. <code>CC</code>, <code>GLIBC_VERSION</code>, etc).
3053 These are the variables accessed using the <code>$(CC)</code>
3054 or <code>varref("CC")</code> syntax inside BUILD files.
3055 </li>
3056</ul>
3057
3058<p>
3059 Example: the C++ compiler for the current configuration.
3060 This is the <code>$(CC)</code> variable in the "Make" environment,
3061 so the <code class='flag'>--show_make_env</code> flag is needed.
3062</p>
3063
3064<pre>
Ulf Adamse0437b12016-08-29 14:48:47 +00003065 % bazel info --show_make_env -c opt COMPILATION_MODE
3066 opt
David Chen8fe82a32016-08-24 10:55:41 +00003067</pre>
3068
3069<p>
3070 Example: the <code>bazel-bin</code> output directory for the current
3071 configuration. This is guaranteed to be correct even in cases where
3072 the <code>bazel-bin</code> symlink cannot be created for some reason
3073 (e.g. you are building from a read-only directory).
3074</p>
3075
3076<h3 id='version'>The <code>version</code> command</h3>
3077
3078<p>
3079 The version command prints version details about the built Bazel
3080 binary, including the changelist at which it was built and the date.
3081 These are particularly useful in determining if you have the latest
3082 Bazel, or if you are reporting bugs. Some of the interesting values
3083 are:
3084</p>
3085<ul>
3086 <li><code>changelist</code>: the changelist at which this version of
3087 Bazel was released.
3088 </li>
3089 <li><code>label</code>: the release label for this Bazel
3090 instance, or "development version" if this is not a released
3091 binary. Very useful when reporting bugs.
3092 </li>
3093</ul>
3094
3095<h3 id='mobile-install'>The <code>mobile-install</code> command</h3>
3096<p>
3097 The <code>mobile-install</code> command installs apps to mobile devices.
3098 Currently only Android devices running ART are supported.
Alex Humesky105e6612017-01-06 19:03:10 +00003099
3100 See <a href="mobile-install.html">bazel mobile-install</a>
3101 for more information.
David Chen8fe82a32016-08-24 10:55:41 +00003102</p>
3103<p>
3104 Note that this command does not install the same thing that
3105 <code>bazel build</code> produces: Bazel tweaks the app so that it can be
3106 built, installed and re-installed quickly. This should, however, be mostly
3107 transparent to the app.
3108</p>
3109<p>
3110 The following options are supported:
3111</p>
3112<h4 id='flag--incremental'><code class='flag'>--incremental</code></h4>
3113<p>
3114 If set, Bazel tries to install the app incrementally, that is, only those
3115 parts that have changed since the last build. This cannot update resources
3116 referenced from <code>AndroidManifest.xml</code>, native code or Java
3117 resources (i.e. ones referenced by <code>Class.getResource()</code>). If these
3118 things change, this option must be omitted. Contrary to the spirit of Bazel
3119 and due to limitations of the Android platform, it is the
3120 <b>responsibility of the user</b> to know when this command is good enough and
Alex Humesky105e6612017-01-06 19:03:10 +00003121 when a full install is needed.
3122
3123 If you are using a device with Marshmallow or later, consider the
3124 <a href='#flag--split_apks'><code class='flag'>--split_apks</code></a> flag.
David Chen8fe82a32016-08-24 10:55:41 +00003125</p>
Alex Humesky105e6612017-01-06 19:03:10 +00003126<h4 id='flag--split_apks'><code class='flag'>--split_apks</code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00003127<p>
Alex Humesky105e6612017-01-06 19:03:10 +00003128 Whether to use split apks to install and update the application on the device.
3129 Works only with devices with Marshmallow or later. Note that the
3130 <a href='#flag--incremental'><code class='flag'>--incremental</code></a> flag
3131 is not necessary when using <code class='flag'>--split_apks</code>.
David Chen8fe82a32016-08-24 10:55:41 +00003132</p>
Alex Humesky105e6612017-01-06 19:03:10 +00003133<h4 id='flag--start_app'><code class='flag'>--start_app</code></h4>
3134<p>
3135 Starts the app in a clean state after installing. Equivalent to
3136 <code>--start=COLD</code>.
3137</p>
Googler2b88f622017-03-16 21:52:14 +00003138<h4 id='flag--debug_app'><code class='flag'>--debug_app</code></h4>
Alex Humesky105e6612017-01-06 19:03:10 +00003139<p>
Googler2b88f622017-03-16 21:52:14 +00003140 Waits for debugger to be attached before starting the app in a clean state after installing.
3141 Equivalent to <code>--start=DEBUG</code>.
3142</p>
3143<h4 id='flag--start'><code class='flag'>--start=<i>start_type</i></code></h4>
3144<p>
3145 How the app should be started after installing it. Supported <i>start_type</i>s are:
3146 <ul>
3147 <li><code>NO</code> Does not start the app. This is the default.</li>
3148 <li><code>COLD</code> Starts the app from a clean state after install.</li>
3149 <li><code>WARM</code> Preserves and restores the application state on incremental installs.</li>
3150 <li><code>DEBUG</code> Waits for the debugger before starting the app in a clean state after install.</li>
3151 </ul>
3152 Note that if more than one of <code class='flag'>--start=<i>start_type</i></code>,
3153 <code class='flag'>--start_app</code> or
3154 <code class='flag'>--debug_app</code> is set, the last value will be used.
Alex Humesky105e6612017-01-06 19:03:10 +00003155</p>
3156<h4 id='flag--adb'><code class='flag'>--adb <var>path</var></code></h4>
3157<p>
3158 Indicates the <code>adb</code> binary to be used.
3159
3160 The default is to use the adb in the Android SDK specified by
3161 <a href='#flag--android_sdk'><code class='flag'>--android_sdk</code></a>.
3162
3163</p>
3164<h4 id='flag--adb_arg'><code class='flag'>--adb_arg <var>arg</var></code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00003165<p>
3166 Extra arguments to <code>adb</code>. These come before the subcommand in the
3167 command line and are typically used to specify which device to install to.
Alex Humesky105e6612017-01-06 19:03:10 +00003168 For example, to select the Android device or emulator to use:
David Chen8fe82a32016-08-24 10:55:41 +00003169<pre>% bazel mobile-install --adb_arg=-s --adb_arg=deadbeef
3170</pre>
3171will invoke <code>adb</code> as
3172<pre>
3173adb -s deadbeef install ...
3174</pre>
Alex Humesky105e6612017-01-06 19:03:10 +00003175</p>
3176<h4 id='flag--adb_jobs'><code class='flag'>--adb_jobs <var>number</var></code></h4>
3177<p>
3178 The number of instances of adb to use in parallel to update files on the
3179 device.
3180</p>
3181<h4 id='flag--incremental_install_verbosity'><code class='flag'>--incremental_install_verbosity <var>number</var></code></h4>
3182<p>
3183 The verbosity for incremental install. Set to 1 for debug logging to be
3184 printed to the console.
David Chen8fe82a32016-08-24 10:55:41 +00003185</p>
3186
3187<h3 id='analyze-profile'>The <code>analyze-profile</code> command</h3>
3188
3189<p>
3190 The <code>analyze-profile</code> command analyzes data previously gathered
3191 during the build using <code class='flag'>--profile</code> option. It provides several
3192 options to either perform analysis of the build execution or export data in
3193 the specified format.
3194
3195</p>
3196<p>
3197 The following options are supported:
3198</p>
3199<ul>
3200 <li><code id='flag--dump'>--dump=text</code> displays all gathered data in a
3201 <a href='#dump-text-format'>human-readable format</a></li>
3202 <li><code>--dump=raw</code> displays all gathered data in a
3203 <a href='#dump-raw-format'>script-friendly format</a></li>
3204 <li><code id='flag--html'>--html</code> generates an <a href='#dump-html-format'>HTML file</a> visualizing the
3205 actions and rules executed in the build, as well as summary statistics for the build
3206 <ul>
3207 <li><code id='flag--html_details'>--html_details</code> adds more fine-grained
3208 information on actions and rules to the HTML visualization</li>
Dmitry Shevchenkob99c1cf2016-12-28 17:09:20 +00003209 <ul>
3210 <li><code id='flag--html_histograms'>--html_histograms</code> adds histograms for Skylark
3211 functions clicked in the statistics table. This will increase file size massively</li>
3212 <li><code id='flag--nochart'>--nochart</code> hides the task chart from generated HTML
3213 </li>
3214 </ul>
3215 </ul>
3216 </li>
3217 <li><code id='flag--combine'>--combine</code> combines multiple profile data files into a single
3218 report. Does not generate HTML task charts</li>
3219 <li><code id='flag--task_tree'>--task_tree</code> prints the tree of tasks matching the given
3220 regular expression
3221 <ul>
3222 <li><code id='flag--task_tree_threshold'>--task_tree_threshold</code> skip tasks with duration
3223 less than threshhold, in milliseconds. Default is 50ms</li>
David Chen8fe82a32016-08-24 10:55:41 +00003224 </ul>
3225 </li>
3226</ul>
3227
3228<p>
3229 See the section on <a href='#profiling'>Troubleshooting performance by profiling</a> for
3230 format details and usage help.
3231
3232</p>
3233
3234<h3 id='canonicalize'>The <code>canonicalize-flags</code> command</h3>
3235
3236<p>
3237 The <code>canonicalize-flags</code> command, which takes a list of options for
3238 a Bazel command and returns a list of options that has the same effect. The
3239 new list of options is canonical, i.e., two lists of options with the same
3240 effect are canonicalized to the same new list.
3241</p>
3242<p>
3243 The <code class='flag'>--for_command</code> option can be used to select between different
3244 commands. At this time, only <code>build</code> and <code>test</code> are
3245 supported. Options that the given command does not support cause an error.
3246</p>
3247<p>
3248 Note that a small number of options cannot be reordered, because Bazel cannot
3249 ensure that the effect is identical.
3250</p>
3251
3252<h3 id='startup_options'>Bazel startup options</h3>
3253
3254<p>
3255 The options described in this section affect the startup of the Java
3256 virtual machine used by Bazel server process, and they apply to all
3257 subsequent commands handled by that server. If there is an already
3258 running Bazel server and the startup options do not match, it will
3259 be restarted.
3260</p>
3261<p>
3262 All of the options described in this section must be specified using the
3263 <code class='flag'>--key=value</code> or <code class='flag'>--key value</code>
3264 syntax. Also, these options must appear <i>before</i> the name of the Bazel
3265 command.
3266</p>
3267
3268<h4 id='flag--output_base'><code class='flag'>--output_base=<var>dir</var></code></h4>
3269<p>
3270 This option requires a path argument, which must specify a
3271 writable directory. Bazel will use this location to write all its
3272 output. The output base is also the key by which the client locates
3273 the Bazel server. By changing the output base, you change the server
3274 which will handle the command.
3275</p>
3276<p>
3277 By default, the output base is derived from the user's login name,
3278 and the name of the workspace directory (actually, its MD5 digest),
3279 so a typical value looks like:
3280
3281 <code>/var/tmp/google/_bazel_jrluser/d41d8cd98f00b204e9800998ecf8427e</code>.
3282 Note that the client uses the output base to find the Bazel server
3283 instance, so if you specify a different output base in a Bazel
3284 command, a different server will be found (or started) to handle the
3285 request. It's possible to perform two concurrent builds in the same
3286 workspace directory by varying the output base.
3287</p>
3288
3289<p>For example:</p>
3290<pre>
3291 % bazel --output_base /tmp/1 build //foo &amp; bazel --output_base /tmp/2 build //bar
3292</pre>
3293<p>
3294 In this command, the two Bazel commands run concurrently (because of
3295 the shell <code>&amp;</code> operator), each using a different Bazel
3296 server instance (because of the different output bases).
3297 In contrast, if the default output base was used in both commands,
3298 then both requests would be sent to the same server, which would
3299 handle them sequentially: building <code>//foo</code> first, followed
3300 by an incremental build of <code>//bar</code>.
3301</p>
3302<p>
3303 We recommend you do not use NFS locations for the output base, as
3304 the higher access latency of NFS will cause noticeably slower
3305 builds.
3306</p>
3307
3308<h4 id='flag--output_user_root'><code class='flag'>--output_user_root=<var>dir</var></code></h4>
3309<p>
3310 By default, the <code>output_base</code> value is chosen to as to
3311 avoid conflicts between multiple users building in the same workspace directory.
3312 In some situations, though, it is desirable to build from a directory
3313 shared between multiple users; release engineers often do this. In
3314 those cases it may be useful to deliberately override the default so
3315 as to ensure "conflicts" (i.e., sharing) between multiple users.
3316 Use the <code class='flag'>--output_user_root</code> option to achieve this: the
3317 output base is placed in a subdirectory of the output user root,
3318 with a unique name based on the workspace, so the result of using an
3319 output user root that is not a function of <code>$USER</code> is
3320 sharing. Of course, it is important to ensure (via umask and group
3321 membership) that all the cooperating users can read/write each
3322 others files.
3323</p>
3324<p>
3325 If the <code class='flag'>--output_base</code> option is specified, it overrides
3326 using <code class='flag'>--output_user_root</code> to calculate the output base.
3327</p>
3328<p>
3329 The install base location is also calculated based on
3330 <code class='flag'>--output_user_root</code>, plus the MD5 identity of the Bazel embedded
3331 binaries.
3332</p>
3333<p>
3334 You can also use the <code class='flag'>--output_user_root</code> option to choose an
3335 alternate base location for all of Bazel's output (install base and output
3336 base) if there is a better location in your filesystem layout.
3337</p>
3338
3339<h4 id='flag--host_jvm_args'><code class='flag'>--host_jvm_args=<var>string</var></code></h4>
3340<p>
3341 Specifies a startup option to be passed to the Java virtual machine in which <i>Bazel itself</i>
3342 runs. This can be used to set the stack size, for example:
3343</p>
3344<pre>
3345 % bazel --host_jvm_args="-Xss256K" build //foo
3346</pre>
3347<p>
3348 This option can be used multiple times with individual arguments. Note that
3349 setting this flag should rarely be needed. You can also pass a space-separated list of strings,
3350 each of which will be interpreted as a separate JVM argument, but this feature will soon be
3351 deprecated.
3352
3353</p>
3354<p>
3355 That this does <i>not</i> affect any JVMs used by
3356 subprocesses of Bazel: applications, tests, tools, etc. To pass
3357 JVM options to executable Java programs, whether run by <code>bazel
3358 run</code> or on the command-line, you should use
3359 the <code>--jvm_flags</code> argument which
3360 all <code>java_binary</code> and <code>java_test</code> programs
3361 support. Alternatively for tests, use <code>bazel
3362 test --test_arg=--jvm_flags=foo ...</code>.
3363</p>
3364
3365<h4 id='flag--host_jvm_debug'><code class='flag'>--host_jvm_debug</code></h4>
3366<p>
3367 This option causes the Java virtual machine to wait for a connection
3368 from a JDWP-compliant debugger before
3369 calling the main method of <i>Bazel itself</i>. This is primarily
3370 intended for use by Bazel developers.
3371</p>
3372<p>
3373 (Please note that this does <i>not</i> affect any JVMs used by
3374 subprocesses of Bazel: applications, tests, tools, etc.)
3375</p>
3376
3377<h4 id='flag--batch'><code class='flag'>--batch</code></h4>
3378<p>
3379 This switch will cause bazel to be run in batch mode, instead of the
3380 standard client/server mode described <a href='#client/server'>above</a>.
3381 Doing so provides more predictable semantics with respect to signal handling,
3382 job control, and environment variable inheritance, and is necessary for running
3383 bazel in a chroot jail.
3384</p>
3385
3386<p>
3387 Batch mode retains proper queueing semantics within the same output_base.
3388 That is, simultaneous invocations will be processed in order, without overlap.
3389 If a batch mode bazel is run on a client with a running server, it first
3390 kills the server before processing the command.
3391</p>
3392
3393<p>
3394 Bazel will run slower in batch mode, compared to client/server mode.
3395 Among other things, the build file cache is memory-resident, so it is not
3396 preserved between sequential batch invocations.
3397 Therefore, using batch mode often makes more sense in cases where performance
3398 is less critical, such as continuous builds.
3399</p>
3400
3401<h4 id='flag--max_idle_secs'><code class='flag'>--max_idle_secs <var>n</var></code></h4>
3402<p>
3403 This option specifies how long, in seconds, the Bazel server process
3404 should wait after the last client request, before it exits. The
3405 default value is 10800 (3 hours).
3406</p>
3407<p>
3408 This option may be used by scripts that invoke Bazel to ensure that
3409 they do not leave Bazel server processes on a user's machine when they
3410 would not be running otherwise.
3411 For example, a presubmit script might wish to
3412 invoke <code>bazel query</code> to ensure that a user's pending
3413 change does not introduce unwanted dependencies. However, if the
3414 user has not done a recent build in that workspace, it would be
3415 undesirable for the presubmit script to start a Bazel server just
3416 for it to remain idle for the rest of the day.
3417 By specifying a small value of <code class='flag'>--max_idle_secs</code> in the
3418 query request, the script can ensure that <i>if</i> it caused a new
3419 server to start, that server will exit promptly, but if instead
3420 there was already a server running, that server will continue to run
3421 until it has been idle for the usual time. Of course, the existing
3422 server's idle timer will be reset.
3423</p>
3424
3425<h4 id='flag--block_for_lock'><code class='flag'>--[no]block_for_lock</code></h4>
3426<p>
3427 If enabled, Bazel will wait for other Bazel commands holding the
3428 server lock to complete before progressing. If disabled, Bazel will
3429 exit in error if it cannot immediately acquire the lock and
3430 proceed.
3431
3432 Developers might use this in presubmit checks to avoid long waits caused
3433 by another Bazel command in the same client.
3434</p>
3435
3436<h4 id='flag--io_nice_level'><code class='flag'>--io_nice_level <var>n</var></code></h4>
3437<p>
3438 Sets a level from 0-7 for best-effort IO scheduling. 0 is highest priority,
3439 7 is lowest. The anticipatory scheduler may only honor up to priority 4.
3440 Negative values are ignored.
3441</p>
3442
3443<h4 id='flag--batch_cpu_scheduling'><code class='flag'>--batch_cpu_scheduling</code></h4>
3444<p>
3445 Use <code>batch</code> CPU scheduling for Bazel. This policy is useful for
3446 workloads that are non-interactive, but do not want to lower their nice value.
3447 See 'man 2 sched_setscheduler'. This policy may provide for better system
3448 interactivity at the expense of Bazel throughput.
3449</p>
3450
3451<h3 id='misc_options'>Miscellaneous options</h3>
3452
3453<h4 id='flag--announce_rc'><code class='flag'>--[no]announce_rc</code></h4>
3454<p>
3455 Controls whether Bazel announces command options read from the bazelrc file when
3456 starting up. (Startup options are unconditionally announced.)
3457</p>
3458
3459<h4 id='flag--color'><code class='flag'>--color (yes|no|auto)</code></h4>
3460<p>
3461 This option determines whether Bazel will use colors to highlight
3462 its output on the screen.
3463</p>
3464<p>
3465 If this option is set to <code>yes</code>, color output is enabled.
3466 If this option is set to <code>auto</code>, Bazel will use color output only if
3467 the output is being sent to a terminal and the TERM environment variable
3468 is set to a value other than <code>dumb</code>, <code>emacs</code>, or <code>xterm-mono</code>.
3469 If this option is set to <code>no</code>, color output is disabled,
3470 regardless of whether the output is going to a terminal and regardless
3471 of the setting of the TERM environment variable.
3472</p>
3473
3474<h4 id='flag--config'><code class='flag'>--config <var>name</var></code></h4>
3475<p>
3476 Selects additional config section from the rc files; for the current
3477 <code>command</code>, it also pulls in the options from
Googlerc848bf32017-02-14 12:15:42 +00003478 <code>command:name</code> if such a section exists. Can be specified multiple
3479 times to add flags from several config sections. Expansions can refer to other
3480 definitions (i.e. expansions can be chained).
David Chen8fe82a32016-08-24 10:55:41 +00003481</p>
3482
3483<h4 id='flag--curses'><code class='flag'>--curses (yes|no|auto)</code></h4>
3484<p>
3485 This option determines whether Bazel will use cursor controls
3486 in its screen output. This results in less scrolling data, and a more
3487 compact, easy-to-read stream of output from Bazel. This works well with
3488 <code class='flag'>--color</code>.
3489</p>
3490<p>
3491 If this option is set to <code>yes</code>, use of cursor controls is enabled.
3492 If this option is set to <code>no</code>, use of cursor controls is disabled.
3493 If this option is set to <code>auto</code>, use of cursor controls will be
3494 enabled under the same conditions as for <code class='flag'>--color=auto</code>.
3495</p>
3496
3497<h4 id='flag--show_timestamps'><code class='flag'>--[no]show_timestamps</code></h4>
3498<p>
3499 If specified, a timestamp is added to each message generated by
3500 Bazel specifying the time at which the message was displayed.
3501</p>
3502
3503<h2 id='scripting'>Calling Bazel from scripts</h2>
3504
3505<p>
3506 Bazel can be called from scripts in order to perform a build, run
3507 tests or query the dependency graph. Bazel has been designed to
3508 enable effective scripting, but this section lists some details to
3509 bear in mind to make your scripts more robust.
3510</p>
3511
3512<h3>Choosing the output base</h3>
3513
3514<p>
3515 The <code class='flag'>--output_base</code> option controls where the Bazel process should
3516 write the outputs of a build to, as well as various working files used
3517 internally by Bazel, one of which is a lock that guards against
3518 concurrent mutation of the output base by multiple Bazel processes.
3519</p>
3520<p>
3521 Choosing the correct output base directory for your script depends
3522 on several factors. If you need to put the build outputs in a
3523 specific location, this will dictate the output base you need to
3524 use. If you are making a "read only" call to Bazel
3525 (e.g. <code>bazel query</code>), the locking factors will be more important.
3526 In particular, if you need to run multiple instances of your script
3527 concurrently, you will need to give each one a different (or random) output
3528 base.
3529</p>
3530<p>
3531 If you use the default output base value, you will be contending for
3532 the same lock used by the user's interactive Bazel commands. If the
3533 user issues long-running commands such as builds, your script will
3534 have to wait for those commands to complete before it can continue.
3535</p>
3536
3537<h3>Server or no server?</h3>
3538
3539<p>
3540 By default, Bazel uses a long-running <a
3541 href='#client/server'>server process</a> as an optimization; this
3542 behavior can be disabled using the <a
3543 href='#flag--batch'><code class='flag'>--batch</code></a> option. There's no hard and
3544 fast rule about whether or not your script should use a server, but
3545 in general, the trade-off is between performance and reliability.
3546 The server mode makes a sequence of builds, especially incremental
3547 builds, faster, but its behavior is more complex and prone to
3548 failure. We recommend in most cases that you use batch mode unless
3549 the performance advantage is critical.
3550</p>
3551<p>
3552 If you do use the server, don't forget to call <code>shutdown</code>
3553 when you're finished with it, or, specify
3554 <code class='flag'>--max_idle_secs=5</code> so that idle servers shut themselves
3555 down promptly.
3556</p>
3557
3558<h3>What exit code will I get?</h3>
3559
3560<p>
3561 Bazel attempts to differentiate failures due to the source code under
3562consideration from external errors that prevent Bazel from executing properly.
3563Bazel execution can result in following exit codes:
3564</p>
3565
3566<b>Exit Codes common to all commands:</b>
3567<ul>
3568 <li><code>0</code> - Success</li>
3569 <li><code>2</code> - Command Line Problem, Bad or Illegal flags or command
3570 combination, or Bad Environment Variables. Your command line must be
3571 modified.</li>
3572 <li><code>8</code> - Build Interrupted but we terminated with an orderly shutdown.</li>
3573 <li><code>32</code> - External Environment Failure not on this machine.</li>
3574 <li><code>33</code> - OOM failure. You need to modify your command line.</li>
3575
3576 <li><code>34</code> - Reserved for Google-internal use.</li>
3577 <li><code>35</code> - Reserved for Google-internal use.</li>
3578 <li><code>36</code> - Local Environmental Issue, suspected permanent.</li>
3579 <li><code>37</code> - Unhandled Exception / Internal Bazel Error.</li>
3580 <li><code>38</code> - Reserved for Google-internal use.</li>
3581 <li><code>40-44</code> - Reserved for errors in Bazel's command line launcher,
3582 <code>bazel.cc</code> that are not command line
3583 related. Typically these are related to bazel server
3584 being unable to launch itself.</li>
3585</ul>
3586
3587<b>Return codes for commands <code>bazel build</code>, <code>bazel test</code>.</b>
3588<ul>
3589 <li><code>1</code> - Build failed.</li>
3590 <li><code>3</code> - Build OK, but some tests failed or timed out.</li>
3591 <li><code>4</code> - Build successful but no tests were found even though
3592 testing was requested.</li>
3593</ul>
3594
3595<b>For <code>bazel run</code>:</b>
3596<ul>
3597 <li><code>1</code> - Build failed.</li>
3598 <li><code>6</code> - Run command failure. The executed subprocess returned a
3599 non-zero exit code. The actual subprocess exit code is
3600 given in stderr.</li>
3601</ul>
3602
3603
3604<b>For
3605
3606 <code>bazel query</code>:</b>
3607<ul>
3608 <li><code>3</code> - Partial success, but the query encountered 1 or more
3609 errors in the input BUILD file set and therefore the
3610 results of the operation are not 100% reliable.
3611 This is likely due to a <code class='flag'>--keep_going</code> option
3612 on the command line.</li>
3613 <li><code>7</code> - Command failure.</li>
3614</ul>
3615
3616<p>
3617 Future Bazel versions may add additional exit codes, replacing generic failure
3618 exit code <code>1</code> with a different non-zero value with a particular
3619 meaning. However, all non-zero exit values will always constitute an error.
3620</p>
3621
3622<h3>Reading the .bazelrc file</h3>
3623
3624<p>
3625 By default, Bazel will read the <a
3626 href='#bazelrc'><code>.bazelrc</code> file</a> from the base workspace
3627 directory or the user's home directory. Whether or not this is
3628 desirable is a choice for your script; if your script needs to be
3629 perfectly hermetic (e.g. when doing release builds), you should
3630 disable reading the .bazelrc file by using the option
3631 <code class='flag'>--bazelrc=/dev/null</code>. If you want to perform a build
3632 using the user's preferred settings, the default behavior is better.
3633</p>
3634
3635<h3>Command log</h3>
3636
3637<p>
3638 The Bazel output is also available in a command log file which you can
3639 find with the following command:
3640</p>
3641
3642<pre>
3643% bazel info command_log
3644</pre>
3645
3646<p>
3647 The command log file contains the interleaved stdout and stderr streams
3648 of the most recent Bazel command. Note that running <code>bazel info</code>
3649 will overwrite the contents of this file, since it then becomes the most
3650 recent Bazel command. However, the location of the command log file will
3651 not change unless you change the setting of the <code class='flag'>--output_base</code>
3652 or <code class='flag'>--output_user_root</code> options.
3653</p>
3654
3655<h3>Parsing output</h3>
3656
3657<p>
3658 The Bazel output is quite easy to parse for many purposes. Two
3659 options that may be helpful for your script are
3660 <code class='flag'>--noshow_progress</code> which suppresses progress messages,
3661 and <code class='flag'>--show_result <var>n</var></code>, which controls whether
3662 or not "build up-to-date" messages are printed; these messages may
3663 be parsed to discover which targets were successfully built, and the
3664 location of the output files they created. Be sure to specify a
3665 very large value of <i>n</i> if you rely on these messages.
3666</p>
3667
3668<h2 id='profiling'>Troubleshooting performance by profiling</h2>
3669
3670<p>
3671 The first step in analyzing the performance of your build is to profile your build with the
3672 <a href='#flag--profile'><code class='flag'>--profile</code></a> option.
3673</p>
3674
3675<p>
3676 The file generated by the <a href='#flag--profile'><code class='flag'>--profile</code></a>
3677 command is a binary file. Once you have generated this binary profile, you can analyze it using
3678 Bazel's <a href='#analyze-profile'><code>analyze-profile</code></a> command. By default, it will
3679 print out summary analysis information for each of the specified profile datafiles. This includes
3680 cumulative statistics for different task types for each build phase and an analysis of the
3681 critical execution path.
3682</p>
3683
3684<p>
3685 The first section of the default output describes an overview of the time spent on the different
3686 build phases:
3687</p>
3688<pre>
3689=== PHASE SUMMARY INFORMATION ===
3690
3691Total launch phase time 6.00 ms 0.01%
3692Total init phase time 864 ms 1.11%
3693Total loading phase time 21.841 s 28.05%
3694Total analysis phase time 5.444 s 6.99%
3695Total preparation phase time 155 ms 0.20%
3696Total execution phase time 49.473 s 63.54%
3697Total finish phase time 83.9 ms 0.11%
3698Total run time 77.866 s 100.00%
3699</pre>
3700
3701<p>
3702 The following sections show the execution time of different tasks happening during a particular
3703 phase:
3704</p>
3705<pre>
3706=== INIT PHASE INFORMATION ===
3707
3708Total init phase time 864 ms
3709
3710Total time (across all threads) spent on:
3711 Type Total Count Average
3712 VFS_STAT 2.72% 1 23.5 ms
3713 VFS_READLINK 32.19% 1 278 ms
3714
3715=== LOADING PHASE INFORMATION ===
3716
3717Total loading phase time 21.841 s
3718
3719Total time (across all threads) spent on:
3720 Type Total Count Average
3721 SPAWN 3.26% 154 475 ms
3722 VFS_STAT 10.81% 65416 3.71 ms
3723[...]
3724SKYLARK_BUILTIN_FN 13.12% 45138 6.52 ms
3725
3726=== ANALYSIS PHASE INFORMATION ===
3727
3728Total analysis phase time 5.444 s
3729
3730Total time (across all threads) spent on:
3731 Type Total Count Average
3732 SKYFRAME_EVAL 9.35% 1 4.782 s
3733 SKYFUNCTION 89.36% 43332 1.06 ms
3734
3735=== EXECUTION PHASE INFORMATION ===
3736
3737Total preparation time 155 ms
3738Total execution phase time 49.473 s
3739Total time finalizing build 83.9 ms
3740
3741Action dependency map creation 0.00 ms
3742Actual execution time 49.473 s
3743
3744Total time (across all threads) spent on:
3745 Type Total Count Average
3746 ACTION 2.25% 12229 10.2 ms
3747[...]
3748 SKYFUNCTION 1.87% 236131 0.44 ms
3749</pre>
3750
3751<p>
3752 The last section shows the critical path:
3753</p>
3754<pre>
3755Critical path (32.078 s):
3756 Id Time Percentage Description
37571109746 5.171 s 16.12% Building [...]
37581109745 164 ms 0.51% Extracting interface [...]
37591109744 4.615 s 14.39% Building [...]
3760[...]
37611109639 2.202 s 6.86% Executing genrule [...]
37621109637 2.00 ms 0.01% Symlinking [...]
37631109636 163 ms 0.51% Executing genrule [...]
3764 4.00 ms 0.01% [3 middleman actions]
3765</pre>
3766
3767<p>
3768 You can use the following options to display more detailed information:
3769</p>
3770
3771<ul>
3772 <li id='dump-text-format'><a href='#flag--dump'><code>--dump=text</code></a>
3773 <p>
3774 This option prints all recorded tasks in the order they occurred. Nested tasks are indented
3775 relative to the parent. For each task, output includes the following information:
3776 </p>
3777<pre>
3778[task type] [task description]
3779Thread: [thread id] Id: [task id] Parent: [parent task id or 0 for top-level tasks]
3780Start time: [time elapsed from the profiling session start] Duration: [task duration]
3781[aggregated statistic for nested tasks, including count and total duration for each nested task]
3782</pre>
3783 </li>
3784 <li id='dump-raw-format'><a href='#flag--dump'><code>--dump=raw</code></a>
3785 <p>
3786 This option is most useful for automated analysis with scripts. It outputs each task record on
3787 a single line using '|' delimiter between fields. Fields are printed in the following order:
3788 </p>
3789 <ol>
3790 <li>thread id - integer positive number, identifies owner thread for the task</li>
3791 <li>task id - integer positive number, identifies specific task</li>
3792 <li>parent task id for nested tasks or 0 for root tasks</li>
3793 <li>task start time in ns, relative to the start of the profiling session</li>
3794 <li>task duration in ns. Please note that this will include duration of all subtasks.</li>
3795 <li>aggregated statistic for immediate subtasks per type. This will include type name (lower
3796 case), number of subtasks for that type and their cumulative duration. Types are
3797 space-delimited and information for single type is comma-delimited.</li>
3798 <li>task type (upper case)</li>
3799 <li>task description</li>
3800 </ol>
3801
3802 Example:
3803<pre>
38041|1|0|0|0||PHASE|Launch Bazel
38051|2|0|6000000|0||PHASE|Initialize command
38061|3|0|168963053|278111411||VFS_READLINK|/[...]
38071|4|0|571055781|23495512||VFS_STAT|/[...]
38081|5|0|869955040|0||PHASE|Load packages
3809[...]
3810</pre>
3811 </li>
3812 <li id='dump-html-format'><a href='#flag--html'><code>--html</code></a>
3813 <p>
3814 This option writes a file called <code>&lt;profile-file&gt;.html</code> in the directory of the
3815 profile file. Open it in your browser to see the visualization of the actions in your build.
3816 Note that the file can be quite large and may push the capabilities of your browser &ndash;
3817 please wait for the file to load.
3818 </p>
3819 <p>
3820 In most cases, the HTML output from <a href='#flag--html'><code>--html</code></a> is easier to
3821 read than the <a href='#flag--dump'><code>--dump</code></a> output.
3822 It includes a Gantt chart that displays time on the horizontal axis and
3823 threads of execution along the vertical axis. If you click on the Statistics link in the top
3824 right corner of the page, you will jump to a section that lists summary analysis information
3825 from your build.
3826 </p>
3827 <ul>
3828 <li><a href='#flag--html_details'><code>--html_details</code></a>
3829 <p>
3830 Additionally passing this option will render a more detailed execution chart and additional
3831 tables on the performance of built-in and user-defined Skylark functions. Beware that this
3832 increases the file size and the load on the browser considerably.
3833 </p>
3834 </li>
3835 </ul></li>
3836</ul>
3837
3838<p>If Bazel appears to be hung, you can hit <kbd><kbd>ctrl</kbd> + <kbd>\</kbd></kbd> or send
3839 Bazel a <code>SIGQUIT</code> signal (<code>kill -3 $(bazel info server_pid)</code>) to get a
3840 thread dump in the file <code>$(bazel info output_base)/server/jvm.out</code>.
3841</p>
3842
3843<p>
3844 Since you may not be able to run <code>bazel info</code> if bazel is hung, the
3845 <code>output_base</code> directory is usually the parent of the <code>bazel-&lt;workspace&gt;</code>
3846 symlink in your workspace directory.
3847</p>