blob: 7b457dddc1d5a11b522a03431bb5d0078d82d9de [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>
1375 If enabled, each gold-invoked link of a C++ executable binary will also output
1376 a <i>symbol counts</i> file (via the <code>--print-symbol-counts</code> gold
1377 option) that logs the number of symbols from each .o input that were used in
1378 the binary. This can be used to track unnecessary link dependencies. The
1379 symbol counts file is written to the binary's output path with the name
1380 <code>[targetname].sc</code>.
1381</p>
1382<p>
1383 This option is disabled by default.
1384</p>
1385
1386<h4 id='flag--jvmopt'><code class='flag'>--jvmopt <var>jvm-option</var></code></h4>
1387<p>
1388 This option allows option arguments to be passed to the Java VM. It can be used
1389 with one big argument, or multiple times with individual arguments. For example:
1390</p>
1391<pre>
1392 % bazel build --jvmopt="-server -Xms256m" java/com/example/common/foo:all
1393</pre>
1394<p>
1395 will use the server VM for launching all Java binaries and set the
1396 startup heap size for the VM to 256 MB.
1397</p>
1398
1399<h4 id='flag--javacopt'><code class='flag'>--javacopt <var>javac-option</var></code></h4>
1400<p>
1401 This option allows option arguments to be passed to javac. It can be used
1402 with one big argument, or multiple times with individual arguments. For example:
1403</p>
1404<pre>
1405 % bazel build --javacopt="-g:source,lines" //myprojects:prog
1406</pre>
1407<p>
1408 will rebuild a java_binary with the javac default debug info
1409 (instead of the bazel default).
1410</p>
1411<p>
1412 The option is passed to javac after the Bazel built-in default options for
1413 javac and before the per-rule options. The last specification of
1414 any option to javac wins. The default options for javac are:
1415</p>
1416
1417<pre>
1418 -source 8 -target 8 -encoding UTF-8
1419</pre>
1420<p>
1421 Note that changing <code class='flag'>--javacopt</code> settings will force a recompilation
1422 of all affected classes. Also note that javacopts parameters listed in
1423 specific java_library or java_binary build rules will be placed on the javac
1424 command line <em>after</em> these options.
1425</p>
1426
1427<h5 id='-extra_checks'><code>-extra_checks[:(off|on)]</code></h5>
1428
1429<p>
1430 This javac option enables extra correctness checks. Any problems found will
1431 be presented as errors.
1432 Either <code>-extra_checks</code> or <code>-extra_checks:on</code> may be used
1433 to force the checks to be turned on. <code>-extra_checks:off</code> completely
1434 disables the analysis.
1435 When this option is not specified, the default behavior is used.
1436</p>
1437
1438<h4 id='flag--strict_java_deps'><code class='flag'>--strict_java_deps
1439 (default|strict|off|warn|error)</code></h4>
1440<p>
1441 This option controls whether javac checks for missing direct dependencies.
1442 Java targets must explicitly declare all directly used targets as
1443 dependencies. This flag instructs javac to determine the jars actually used
1444 for type checking each java file, and warn/error if they are not the output
1445 of a direct dependency of the current target.
1446</p>
1447
1448<ul>
1449 <li> <code>off</code> means checking is disabled.
1450 </li>
1451 <li> <code>warn</code> means javac will generate standard java warnings of
1452 type <code>[strict]</code> for each missing direct dependency.
1453 </li>
1454 <li> <code>default</code>, <code>strict</code> and <code>error</code> all
1455 mean javac will generate errors instead of warnings, causing the current
1456 target to fail to build if any missing direct dependencies are found.
1457 This is also the default behavior when the flag is unspecified.
1458 </li>
1459</ul>
1460
David Chen8fe82a32016-08-24 10:55:41 +00001461<h3 id='semantics-options'>Semantics options</h3>
1462<p>
1463 These options affect the build commands and/or the output file contents.
1464</p>
1465
1466<h4 id='flag--compilation_mode'><code class='flag'>--compilation_mode (fastbuild|opt|dbg)</code> (-c)</h4>
1467<p>
1468 This option takes an argument of <code>fastbuild</code>, <code>dbg</code>
1469 or <code>opt</code>, and affects various C/C++ code-generation
1470 options, such as the level of optimization and the completeness of
1471 debug tables. Bazel uses a different output directory for each
1472 different compilation mode, so you can switch between modes without
1473 needing to do a full rebuild <i>every</i> time.
1474</p>
1475<ul>
1476 <li> <code>fastbuild</code> means build as fast as possible:
1477 generate minimal debugging information (<code>-gmlt
1478 -Wl,-S</code>), and don't optimize. This is the
1479 default. Note: <code>-DNDEBUG</code> will <b>not</b> be set.
1480 </li>
1481 <li> <code>dbg</code> means build with debugging enabled (<code>-g</code>),
1482 so that you can use gdb (or another debugger).
1483 </li>
1484 <li> <code>opt</code> means build with optimization enabled and
1485 with <code>assert()</code> calls disabled (<code>-O2 -DNDEBUG</code>).
1486 Debugging information will not be generated in <code>opt</code> mode
1487 unless you also pass <code class='flag'>--copt -g</code>.
1488 </li>
1489</ul>
1490
1491<h4 id='flag--cpu'><code class='flag'>--cpu <var>cpu</var></code></h4>
1492<p>
1493This option specifies the target CPU architecture to be used for
1494the compilation of binaries during the build.
1495</p>
1496<p>
1497
1498</p>
1499
1500<p>
1501 Note that a particular combination of crosstool version, compiler version,
1502 libc version, and target CPU is allowed only if it has been specified
1503 in the currently used CROSSTOOL file.
1504</p>
1505
1506<h4 id='flag--host_cpu'><code class='flag'>--host_cpu <var>cpu</var></code></h4>
1507<p>
1508 This option specifies the name of the CPU architecture that should be
1509 used to build host tools.
1510</p>
1511
Alex Humesky105e6612017-01-06 19:03:10 +00001512<h4 id='flag--fat_apk_cpu'><code class='flag'>--fat_apk_cpu <var>cpu[,cpu]*</var></code></h4>
1513<p>
1514 The CPUs to build C/C++ libraries for in the transitive <code>deps</code> of
1515 <code>android_binary</code>
1516
1517 rules. Other C/C++ rules are not affected. For example, if a <code>cc_library</code>
1518 appears in the transitive <code>deps</code> of an <code>android_binary</code> rule and a
1519 <code>cc_binary</code> rule, the <code>cc_library</code> will be built at least twice:
1520 once for each CPU specified with <code class='flag'>--fat_apk_cpu</code> for the
1521 <code>android_binary</code> rule, and once for the CPU specified with
1522 <code class='flag'>--cpu</code> for the <code>cc_binary</code> rule.
1523
1524<p>
1525The default is <code>armeabi-v7a</code>.
1526</p>
1527 <p>
1528 One <code>.so</code> file will be created and packaged in the APK for
1529 each CPU specified with <code class='flag'>--fat_apk_cpu</code>. The name of the <code>.so</code>
1530 file will be the name of the <code>android_binary</code> rule prefixed with "lib", e.g., if the name
1531 of the <code>android_binary</code> is "foo", then the file will be <code>libfoo.so</code>.
1532 </p>
1533
1534 <p>
1535 Note that an Android-compatible crosstool must be selected.
1536 If an <code>android_ndk_repository</code> rule is defined in the
1537 WORKSPACE file, an Android-compatible crosstool is automatically selected.
1538 Otherwise, the crostool can be selected using the
1539 <a href='#flag--android_crosstool_top'><code class='flag'>--android_crosstool_top</code></a>
1540 or <a href='#flag--crosstool_top'><code class='flag'>--crosstool_top</code></a> flags.
1541 </p>
1542</p>
1543
David Chen8fe82a32016-08-24 10:55:41 +00001544<h4 id='flag--experimental_skip_static_outputs'><code class='flag'>--experimental_skip_static_outputs</code></h4>
1545<p>
1546 The <code class='flag'>--experimental_skip_static_outputs</code> option causes all
1547 statically-linked C++ binaries to <b>not</b> be output in any meaningful
1548 way.
1549
1550</p>
1551<p>
1552 If you set this flag, you must also
1553 set <a href="#flag--distinct_host_configuration"><code class='flag'>--distinct_host_configuration</code></a>.
1554 It is also inherently incompatible with running tests &mdash; don't use it for
1555 that. This option is experimental and may go away at any time.
1556</p>
1557
1558<h4 id='flag--per_file_copt'><code class='flag'>--per_file_copt
1559 <var>[+-]regex[,[+-]regex]...@option[,option]...</var></code></h4>
1560<p>
1561 When present, any C++ file with a label or an execution path matching one of the inclusion regex
1562 expressions and not matching any of the exclusion expressions will be built
1563 with the given options. The label matching uses the canonical form of the label
1564 (i.e //<code>package</code>:<code>label_name</code>).
1565
1566 The execution path is the relative path to your workspace directory including the base name
1567 (including extension) of the C++ file. It also includes any platform dependent prefixes.
1568 Note, that if only one of the label or the execution path matches the options will be used.
1569</p>
1570<p>
1571 <b>Notes</b>:
1572 To match the generated files (e.g. genrule outputs)
1573 Bazel can only use the execution path. In this case the regexp shouldn't start with '//'
1574 since that doesn't match any execution paths. Package names can be used like this:
1575 <code class='flag'>--per_file_copt=base/.*\.pb\.cc@-g0</code>. This will match every
1576 <code>.pb.cc</code> file under a directory called <code>base</code>.
1577</p>
1578<p>
1579 This option can be used multiple times.
1580</p>
1581<p>
1582 The option is applied regardless of the compilation mode used. I.e. it is possible
1583 to compile with <code class='flag'>--compilation_mode=opt</code> and selectively compile some
1584 files with stronger optimization turned on, or with optimization disabled.
1585</p>
1586<p>
1587 <b>Caveat</b>: If some files are selectively compiled with debug symbols the symbols
1588 might be stripped during linking. This can be prevented by setting
1589 <code class='flag'>--strip=never</code>.
1590</p>
1591<p>
1592 <b>Syntax</b>: <code>[+-]regex[,[+-]regex]...@option[,option]...</code> Where
1593 <code>regex</code> stands for a regular expression that can be prefixed with
1594 a <code>+</code> to identify include patterns and with <code>-</code> to identify
1595 exclude patterns. <code>option</code> stands for an arbitrary option that is passed
1596 to the C++ compiler. If an option contains a <code>,</code> it has to be quoted like so
1597 <code>\,</code>. Options can also contain <code>@</code>, since only the first
1598 <code>@</code> is used to separate regular expressions from options.
1599</p>
1600<p>
1601 <b>Example</b>:
1602 <code class='flag'>--per_file_copt=//foo:.*\.cc,-//foo:file\.cc@-O0,-fprofile-arcs</code>
1603 adds the <code>-O0</code> and the <code>-fprofile-arcs</code> options to the command
1604 line of the C++ compiler for all <code>.cc</code> files in <code>//foo/</code> except
1605 <code>file.cc</code>.
1606</p>
1607<h4 id='flag--dynamic_mode'><code class='flag'>--dynamic_mode <var>mode</var></code></h4>
1608<p>
1609 Determines whether C++ binaries will be linked dynamically, interacting with
1610 the <a href='be/c-cpp.html#cc_binary.linkstatic'>linkstatic
1611 attribute</a> on build rules.
1612</p>
1613
1614<p>
1615 Modes:
1616</p>
1617<ul>
1618 <li><code>auto</code>: Translates to a platform-dependent mode;
1619 <code>default</code> for linux and <code>off</code> for cygwin.</li>
1620 <li><code>default</code>: Allows bazel to choose whether to link dynamically.
1621 See <a href='be/c-cpp.html#cc_binary.linkstatic'>linkstatic</a> for more
1622 information.</li>
1623 <li><code>fully</code>: Links all targets dynamically. This will speed up
1624 linking time, and reduce the size of the resulting binaries.
1625
1626 </li>
1627 <li><code>off</code>: Links all targets in
1628 <a href='be/c-cpp.html#cc_binary.linkstatic'>mostly static</a> mode.
1629 If <code>-static</code> is set in linkopts, targets will change to fully
1630 static.</li>
1631</ul>
1632
1633<h4 id='flag--fission'><code class='flag'>--fission (yes|no|[dbg][,opt][,fastbuild])</code></h4>
1634<p>
1635 Enables
1636
1637 <a href='https://gcc.gnu.org/wiki/DebugFission'>Fission</a>,
1638 which writes C++ debug information to dedicated .dwo files instead of .o files, where it would
1639 otherwise go. This substantially reduces the input size to links and can reduce link times.
1640
1641</p>
1642<p>
1643 When set to <code class='flag'>[dbg][,opt][,fastbuild]</code> (example:
1644 <code class='flag'>--fission=dbg,fastbuild</code>), Fission is enabled
1645 only for the specified set of compilation modes. This is useful for bazelrc
1646 settings. When set to <code class='flag'>yes</code>, Fission is enabled
1647 universally. When set to <code class='flag'>no</code>, Fission is disabled
1648 universally. Default is <code class='flag'>dbg</code>.
1649</p>
1650
1651<h4 id='flag--force_ignore_dash_static'><code class='flag'>--force_ignore_dash_static</code></h4>
1652<p>
1653 If this flag is set, any <code>-static</code> options in linkopts of
1654 <code>cc_*</code> rules BUILD files are ignored. This is only intended as a
1655 workaround for C++ hardening builds.
1656</p>
1657
1658<h4 id='flag--force_pic'><code class='flag'>--[no]force_pic</code></h4>
1659<p>
1660 If enabled, all C++ compilations produce position-independent code ("-fPIC"),
1661 links prefer PIC pre-built libraries over non-PIC libraries, and links produce
1662 position-independent executables ("-pie"). Default is disabled.
1663</p>
1664<p>
1665 Note that dynamically linked binaries (i.e. <code>--dynamic_mode fully</code>)
1666 generate PIC code regardless of this flag's setting. So this flag is for cases
1667 where users want PIC code explicitly generated for static links.
1668</p>
1669
Andrew Pellegrini8d2c1662017-01-26 16:04:12 +00001670<h4 id='flag--android_resource_shrinking'><code class='flag'>--android_resource_shrinking</code></h4>
1671<p>
1672 Selects whether to perform resource shrinking for android_binary rules. Sets the default for the
1673 <a href='be/android.html#android_binary.shrink_resources'>shrink_resources attribute</a> on
1674 android_binary rules; see the documentation for that rule for further details. Defaults to off.
1675</p>
1676
David Chen8fe82a32016-08-24 10:55:41 +00001677<h4 id='flag--custom_malloc'><code class='flag'>--custom_malloc <var>malloc-library-target</var></code></h4>
1678<p>
1679 When specified, always use the given malloc implementation, overriding all
1680 <code>malloc="target"</code> attributes, including in those targets that use the
1681 default (by not specifying any <code>malloc</code>).
1682</p>
1683
1684<h4 id='flag--crosstool_top'><code class='flag'>--crosstool_top <var>label</var></code></h4>
1685<p>
1686 This option specifies the location of the crosstool compiler suite
1687 to be used for all C++ compilation during a build. Bazel will look in that
1688 location for a CROSSTOOL file and uses that to automatically determine
1689 settings for
1690
1691 <code class='flag'>--compiler</code>.
1692</p>
1693
1694<h4 id='flag--host_crosstool_top'><code class='flag'>--host_crosstool_top <var>label</var></code></h4>
1695<p>
1696 If not specified, bazel uses the value of <code class='flag'>--crosstool_top</code> to compile
1697 code in the host configuration, i.e., tools run during the build. The main purpose of this flag
1698 is to enable cross-compilation.
1699</p>
1700
Cal Peyserf1a15c02017-01-17 20:26:00 +00001701<h4 id='flag--apple_crosstool_top'><code class='flag'>--apple_crosstool_top <var>label</var></code></h4>
1702<p>
1703 The crosstool to use for compiling C/C++ rules in the transitive <code>deps</code> of
1704 objc_*, ios__*, and apple_* rules. For those targets, this flag overwrites
1705 <code class='flag'>--crosstool_top</code>.
1706</p>
1707
Alex Humesky105e6612017-01-06 19:03:10 +00001708<h4 id='flag--android_crosstool_top'><code class='flag'>--android_crosstool_top <var>label</var></code></h4>
1709<p>
1710 The crosstool to use for compiling C/C++ rules in the transitive <code>deps</code> of
1711 <code>android_binary</code> rules. This is useful if other targets in the
1712 build require a different crosstool. The default is to use the crosstool
1713 generated by the <code>android_ndk_repository</code> rule in the WORKSPACE file.
1714 See also <a href='#flag--fat_apk_cpu'><code class='flag'>--fat_apk_cpu</code></a>.
1715</p>
David Chen8fe82a32016-08-24 10:55:41 +00001716<h4 id='flag--compiler'><code class='flag'>--compiler <var>version</var></code></h4>
1717<p>
1718 This option specifies the C/C++ compiler version (e.g. <code>gcc-4.1.0</code>)
1719 to be used for the compilation of binaries during the build. If you want to
1720 build with a custom crosstool, you should use a CROSSTOOL file instead of
1721 specifying this flag.
1722</p>
1723<p>
1724 Note that only certain combinations of crosstool version, compiler version,
1725 libc version, and target CPU are allowed.
1726</p>
1727
1728<h4 id='flag--glibc'><code class='flag'>--glibc <var>version</var></code></h4>
1729<p>
1730 This option specifies the version of glibc that the target should be linked
1731 against. If you want to build with a custom crosstool, you should use a
1732 CROSSTOOL file instead of specifying this flag. In that case, Bazel will use
1733 the CROSSTOOL file and the following options where appropriate:
1734 <ul>
1735 <li><a href="#flag--cpu"><code class='flag'>--cpu</code></a></li>
1736
1737 </ul>
1738</p>
1739<p>
1740 Note that only certain combinations of crosstool version, compiler version,
1741 glibc version, and target CPU are allowed.
1742</p>
1743
Alex Humesky105e6612017-01-06 19:03:10 +00001744<h4 id='flag--android_sdk'><code class='flag'>--android_sdk <var>label</var></code></h4>
1745<p>
1746 This option specifies the Android SDK/platform toolchain
1747 and Android runtime library that will be used to build any Android-related
1748 rule.
1749
1750 The Android SDK will be automatically selected if an <code>android_sdk_repository</code>
1751 rule is defined in the WORKSPACE file.
1752</p>
1753
David Chen8fe82a32016-08-24 10:55:41 +00001754<h4 id='flag--java_toolchain'><code class='flag'>--java_toolchain <var>label</var></code></h4>
1755<p>
1756 This option specifies the label of the java_toolchain used to compile Java
1757 source files.
1758</p>
1759
1760<h4 id='flag--javabase'><code class='flag'>--javabase (<var>path</var>|<var>label</var>)</code></h4>
1761<p>
Googlerae4895a2016-08-30 17:16:37 +00001762 This option sets the <i>label</i> or the <i>path</i> of the base Java installation to
1763 use for running JavaBuilder, SingleJar, for <i>bazel run</i> and <i>bazel
1764 test</i>, and for Java binaries built by <code>java_binary</code> and <code>java_test</code>
1765 rules. A path must be to a JDK or JRE directory that contains <code>bin/java</code>.
1766 The various <a href='be/make-variables.html'>"Make" variables</a> for
David Chen8fe82a32016-08-24 10:55:41 +00001767 Java (<code>JAVABASE</code>, <code>JAVA</code>, <code>JAVAC</code> and
1768 <code>JAR</code>) are derived from this option.
1769</p>
1770
1771<p>
1772 This does not select the Java compiler that is used to compile Java
1773 source files. The compiler can be selected by settings the
1774 <a href="#flag--java_toolchain"><code class='flag'>--java_toolchain</code></a>
1775 option.
1776</p>
1777
1778<h3 id='strategy-options'>Build strategy options</h3>
1779<p>
1780 These options affect how Bazel will execute the build.
1781 They should not have any significant effect on the output files
1782 generated by the build. Typically their main effect is on the
1783 speed on the build.
1784</p>
1785
1786<h4 id='flag--spawn_strategy'><code class='flag'>--spawn_strategy <var>strategy</var></code></h4>
1787<p>
1788 This option controls where and how commands are executed.
1789</p>
1790<ul>
1791
1792 <li>
1793 <code>standalone</code> causes commands to be executed as local subprocesses.
1794 </li>
1795 <li>
1796 <code>sandboxed</code> causes commands to be executed inside a sandbox on the local machine.
1797 This requires that all input files, data dependencies and tools are listed as direct
1798 dependencies in the <code>srcs</code>, <code>data</code> and <code>tools</code> attributes.
1799 This is the default on systems that support sandboxed execution.
1800 </li>
1801
1802</ul>
1803
1804<h4 id='flag--genrule_strategy'><code class='flag'>--genrule_strategy <var>strategy</var></code></h4>
1805<p>
1806 This option controls where and how genrules are executed.
1807</p>
1808<ul>
1809
1810 <li>
1811 <code>standalone</code> causes genrules to run as local subprocesses.
1812 </li>
1813 <li>
1814 <code>sandboxed</code> causes genrules to run inside a sandbox on the local machine.
1815 This requires that all input files are listed as direct dependencies in
1816 the <code>srcs</code> attribute, and the program(s) executed are listed
1817 in the <code>tools</code> attribute.
1818 This is the default for Bazel on systems that support sandboxed execution.
1819 </li>
1820
1821</ul>
1822
1823<h4 id='flag--local_genrule_timeout_seconds'><code class='flag'>--local_genrule_timeout_seconds <var>seconds</var></code></h4>
1824<p>Sets a timeout value for local genrules with the given number of seconds.</p>
1825
1826<h4 id='flag--jobs'><code class='flag'>--jobs <var>n</var></code> (-j)</h4>
1827<p>
1828 This option, which takes an integer argument, specifies a limit on
1829 the number of jobs that should be executed concurrently during the
1830 execution phase of the build. The default is 200.
1831</p>
1832<p>
1833 Note that the number of concurrent jobs that Bazel will run
1834 is determined not only by the <code class='flag'>--jobs</code> setting, but also
1835 by Bazel's scheduler, which tries to avoid running concurrent jobs
1836 that will use up more resources (RAM or CPU) than are available,
1837 based on some (very crude) estimates of the resource consumption
1838 of each job. The behavior of the scheduler can be controlled by
1839 the <code class='flag'>--ram_utilization_factor</code> option.
1840</p>
1841
1842<h4 id='flag--progress_report_interval'><code class='flag'>--progress_report_interval <var>n</var></code></h4>
1843<p>
1844
1845 Bazel periodically prints a progress report on jobs that are not
1846 finished yet (e.g. long running tests). This option sets the
1847 reporting frequency, progress will be printed every <code>n</code>
1848 seconds.
1849</p>
1850<p>
1851 The default is 0, that means an incremental algorithm: the first
1852 report will be printed after 10 seconds, then 30 seconds and after
1853 that progress is reported once every minute.
1854</p>
1855
1856<h4 id='flag--ram_utilization_factor'><code class='flag'>--ram_utilization_factor</code> <var>percentage</var></h4>
1857<p>
1858 This option, which takes an integer argument, specifies what percentage
1859 of the system's RAM Bazel should try to use for its subprocesses.
1860 This option affects how many processes Bazel will try to run
1861 in parallel. The default value is 67.
1862 If you run several Bazel builds in parallel, using a lower
1863 value for this option may avoid thrashing and thus improve overall
1864 throughput. Using a value higher than the default is NOT recommended. Note
1865 that Bazel's estimates are very coarse, so the actual RAM usage may be much
1866 higher or much lower than specified. Note also that this option does not
1867 affect the amount of memory that the Bazel server itself will use.
1868</p>
1869
1870<h4 id='flag--local_resources'><code class='flag'>--local_resources</code> <var>availableRAM,availableCPU,availableIO</var></h4>
1871<p>
1872 This option, which takes three comma-separated floating point arguments,
1873specifies the amount of local resources that Bazel can take into
1874consideration when scheduling build and test activities. Option expects amount of
1875available RAM (in MB), number of CPU cores (with 1.0 representing single full
1876core) and workstation I/O capability (with 1.0 representing average
1877workstation). By default Bazel will estimate amount of RAM and number of CPU
1878cores directly from system configuration and will assume 1.0 I/O resource.
1879</p>
1880<p>
1881 If this option is used, Bazel will ignore --ram_utilization_factor.
1882</p>
1883
1884<h4 id='flag--build_runfile_links'><code class='flag'>--[no]build_runfile_links</code></h4>
1885<p>
1886 This option, which is currently enabled by default, specifies
1887 whether the runfiles symlinks for tests and
1888 <code>cc_binary</code> targets should be built in the output directory.
1889 Using <code class='flag'>--nobuild_runfile_links</code> can be useful
1890 to validate if all targets compile without incurring the overhead
1891 for building the runfiles trees.
1892
1893 Within Bazel's output tree, the
1894 runfiles symlink tree is typically rooted as a sibling of the corresponding
1895 binary or test.
1896</p>
1897
1898<p>
1899 When tests (or applications) are executed, their
1900 run-time data dependencies are gathered together in one place, and
1901 may be accessed by the test using paths of the form
1902 <code>$TEST_SRCDIR/workspace/<var>packagename</var>/<var>filename</var></code>.
1903 The "runfiles" tree ensures that tests have access to all the files
1904 upon which they have a declared dependence, and nothing more. By
1905 default, the runfiles tree is implemented by constructing a set of
1906 symbolic links to the required files. As the set of links grows, so
1907 does the cost of this operation, and for some large builds it can
1908 contribute significantly to overall build time, particularly because
1909 each individual test (or application) requires its own runfiles tree.
1910</p>
1911<p>
1912 The <code class='flag'>--build_runfile_links</code> flag controls the
1913 construction of the tree of symbolic links (for C++ applications and
1914 tests only). The reasons only C++ non-test rules are affected are numerous
1915 and subtle: C++ builds are more likely to be slower due to runfiles;
1916 no C++ host tools (tools that run during the build) need their runfiles,
1917 so this option can be used by the host configuration; and other rules
1918 (notably Python) need their runfiles for other purposes besides test
1919 execution.
1920</p>
1921
1922<h4 id='flag--discard_analysis_cache'>
1923 <code class='flag'>--[no]discard_analysis_cache</code></h4>
1924<p>
1925 When this option is enabled, Bazel will discard the analysis cache
1926 right before execution starts, thus freeing up additional memory
1927 (around 10%) for the <a href="#execution-phase">execution phase</a>.
1928 The drawback is that further incremental builds will be slower.
1929</p>
1930
1931<h4 id='flag--keep_going'><code class='flag'>--[no]keep_going</code> (-k)</h4>
1932<p>
1933 As in GNU Make, the execution phase of a build stops when the first
1934 error is encountered. Sometimes it is useful to try to build as
1935 much as possible even in the face of errors. This option enables
1936 that behavior, and when it is specified, the build will attempt to
1937 build every target whose prerequisites were successfully built, but
1938 will ignore errors.
1939</p>
1940<p>
1941 While this option is usually associated with the execution phase of
1942 a build, it also effects the analysis phase: if several targets are
1943 specified in a build command, but only some of them can be
1944 successfully analyzed, the build will stop with an error
1945 unless <code class='flag'>--keep_going</code> is specified, in which case the
1946 build will proceed to the execution phase, but only for the targets
1947 that were successfully analyzed.
1948</p>
1949
David Chen8fe82a32016-08-24 10:55:41 +00001950<h4 id='flag--use_ijars'><code class='flag'>--[no]use_ijars</code></h4>
1951<p>
1952 This option changes the way <code>java_library</code> targets are
1953 compiled by Bazel. Instead of using the output of a
1954 <code>java_library</code> for compiling dependent
1955 <code>java_library</code> targets, Bazel will create interface jars
1956 that contain only the signatures of non-private members (public,
1957 protected, and default (package) access methods and fields) and use
1958 the interface jars to compile the dependent targets. This makes it
1959 possible to avoid recompilation when changes are only made to
1960 method bodies or private members of a class.
1961</p>
1962<p>
1963 Note that using <code class='flag'>--use_ijars</code> might give you a different
1964 error message when you are accidentally referring to a non visible
1965 member of another class: Instead of getting an error that the member
1966 is not visible you will get an error that the member does not exist.
1967</p>
1968<p>
1969 Note that changing the <code class='flag'>--use_ijars</code> setting will force
1970 a recompilation of all affected classes.
1971</p>
1972
1973<h4 id='flag--interface_shared_objects'>
1974 <code class='flag'>--[no]interface_shared_objects</code>
1975</h4>
1976<p>
1977 This option enables <i>interface shared objects</i>, which makes binaries and
1978 other shared libraries depend on the <i>interface</i> of a shared object,
1979 rather than its implementation. When only the implementation changes, Bazel
1980 can avoid rebuilding targets that depend on the changed shared library
1981 unnecessarily.
1982</p>
1983
1984<h3 id='output-selection-options'>Output selection options</h3>
1985<p>
1986 These options determine what to build or test.
1987</p>
1988
1989<h4 id="nobuild"><code class='flag'>--[no]build</code></h4>
1990<p>
1991 This option causes the execution phase of the build to occur; it is
1992 on by default. When it is switched off, the execution phase is
1993 skipped, and only the first two phases, loading and analysis, occur.
1994</p>
1995<p>
1996 This option can be useful for validating BUILD files and detecting
1997 errors in the inputs, without actually building anything.
1998</p>
1999
2000<h4 id='flag--build_tests_only'><code class='flag'>--[no]build_tests_only</code></h4>
2001<p>
2002 If specified, Bazel will build only what is necessary to run the *_test
2003 and test_suite rules that were not filtered due to their
2004 <a href='#flag--test_size_filters'>size</a>,
2005 <a href='#flag--test_timeout_filters'>timeout</a>,
2006 <a href='#flag--test_tag_filters'>tag</a>, or
2007 <a href='#flag--test_lang_filters'>language</a>.
2008 If specified, Bazel will ignore other targets specified on the command line.
2009 By default, this option is disabled and Bazel will build everything
2010 requested, including *_test and test_suite rules that are filtered out from
2011 testing. This is useful because running
2012 <code>bazel test --build_tests_only foo/...</code> may not detect all build
2013 breakages in the <code>foo</code> tree.
2014</p>
2015
2016<h4 id='flag--check_up_to_date'><code class='flag'>--[no]check_up_to_date</code></h4>
2017<p>
2018 This option causes Bazel not to perform a build, but merely check
2019 whether all specified targets are up-to-date. If so, the build
2020 completes successfully, as usual. However, if any files are out of
2021 date, instead of being built, an error is reported and the build
2022 fails. This option may be useful to determine whether a build has
2023 been performed more recently than a source edit (e.g. for pre-submit
2024 checks) without incurring the cost of a build.
2025</p>
2026<p>
2027 See also <a href="#flag--check_tests_up_to_date"><code class='flag'>--check_tests_up_to_date</code></a>.
2028</p>
2029
2030<h4 id='flag--compile_one_dependency'><code class='flag'>--[no]compile_one_dependency</code></h4>
2031<p>
2032 Compile a single dependency of the argument files. This is useful for
2033 syntax checking source files in IDEs, for example, by rebuilding a single
2034 target that depends on the source file to detect errors as early as
2035 possible in the edit/build/test cycle. This argument affects the way all
2036 non-flag arguments are interpreted: for each source filename, one
2037 rule that depends on it will be built. For
2038
2039 C++ and Java
2040 sources, rules in the same language space are preferentially chosen. For
2041 multiple rules with the same preference, the one that appears first in the
2042 BUILD file is chosen. An explicitly named target pattern which does not
2043 reference a source file results in an error.
2044</p>
2045
2046<h4 id='flag--save_temps'><code class='flag'>--save_temps</code></h4>
2047<p>
2048 The <code class='flag'>--save_temps</code> option causes temporary outputs from gcc to be saved.
2049 These include .s files (assembler code), .i (preprocessed C) and .ii
2050 (preprocessed C++) files. These outputs are often useful for debugging. Temps will only be
2051 generated for the set of targets specified on the command line.
2052</p>
2053<p>
2054 Note that our implementation of <code class='flag'>--save_temps</code> does not use gcc's
2055 <code>-save-temps</code> flag. Instead, we do two passes, one with <code>-S</code>
2056 and one with <code>-E</code>. A consequence of this is that if your build fails,
2057 Bazel may not yet have produced the ".i" or ".ii" and ".s" files.
2058 If you're trying to use <code class='flag'>--save_temps</code> to debug a failed compilation,
2059 you may need to also use <code class='flag'>--keep_going</code> so that Bazel will still try to
2060 produce the preprocessed files after the compilation fails.
2061</p>
2062<p>
2063 The <code class='flag'>--save_temps</code> flag currently works only for cc_* rules.
2064</p>
2065<p>
2066 To ensure that Bazel prints the location of the additional output files, check that
2067 your <a href='#flag--show_result'><code class='flag'>--show_result <var>n</var></code></a>
2068 setting is high enough.
2069</p>
2070
Googlerd9db1692017-01-05 19:34:30 +00002071<h4 id='flag--build_tag_filters'><code class='flag'>--build_tag_filters <var>tag[,tag]*</var></code></h4>
2072<p>
2073 If specified, Bazel will build only targets that have at least one required tag
2074 (if any of them are specified) and does not have any excluded tags. Build tag
2075 filter is specified as comma delimited list of tag keywords, optionally
2076 preceded with '-' sign used to denote excluded tags. Required tags may also
2077 have a preceding '+' sign.
2078</p>
2079
David Chen8fe82a32016-08-24 10:55:41 +00002080<h4 id='flag--test_size_filters'><code class='flag'>--test_size_filters <var>size[,size]*</var></code></h4>
2081<p>
2082 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
2083 is also specified) only test targets with the given size. Test size filter
2084 is specified as comma delimited list of allowed test size values (small,
2085 medium, large or enormous), optionally preceded with '-' sign used to denote
2086 excluded test sizes. For example,
2087</p>
2088<pre>
2089 % bazel test --test_size_filters=small,medium //foo:all
2090</pre>
2091 and
2092<pre>
2093 % bazel test --test_size_filters=-large,-enormous //foo:all
2094</pre>
2095<p>
2096 will test only small and medium tests inside //foo.
2097</p>
2098<p>
2099 By default, test size filtering is not applied.
2100</p>
2101
2102<h4 id='flag--test_timeout_filters'><code class='flag'>--test_timeout_filters <var>timeout[,timeout]*</var></code></h4>
2103<p>
2104 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
2105 is also specified) only test targets with the given timeout. Test timeout filter
2106 is specified as comma delimited list of allowed test timeout values (short,
2107 moderate, long or eternal), optionally preceded with '-' sign used to denote
2108 excluded test timeouts. See <a href='#flag--test_size_filters'>--test_size_filters</a>
2109 for example syntax.
2110</p>
2111<p>
2112 By default, test timeout filtering is not applied.
2113</p>
2114
2115
2116<h4 id='flag--test_tag_filters'><code class='flag'>--test_tag_filters <var>tag[,tag]*</var></code></h4>
2117<p>
2118 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
2119 is also specified) only test targets that have at least one required tag
2120 (if any of them are specified) and does not have any excluded tags. Test tag
2121 filter is specified as comma delimited list of tag keywords, optionally
2122 preceded with '-' sign used to denote excluded tags. Required tags may also
2123 have a preceding '+' sign.
2124</p>
2125<p>
2126 For example,
2127<pre>
2128 % bazel test --test_tag_filters=performance,stress,-flaky //myproject:all
2129</pre>
2130<p>
2131 will test targets that are tagged with either <code>performance</code> or
2132 <code>stress</code> tag but are <b>not</b> tagged with the <code>flaky</code>
2133 tag.
2134</p>
2135<p>
2136 By default, test tag filtering is not applied. Note that you can also filter
2137 on test's <code>size</code> and <code>local</code> tags in
2138 this manner.
2139</p>
2140
2141<h4 id='flag--test_lang_filters'><code class='flag'>--test_lang_filters <var>lang[,lang]*</var></code></h4>
2142<p>
2143 Specifies a comma-separated list of test languages for languages with an official <code>*_test</code> rule the
2144 (see <a href="be/overview.html">build encyclopedia</a> for a full list of these). Each
2145 language can be optionally preceded with '-' to specify excluded
2146 languages. The name used for each language should be the same as
2147 the language prefix in the <code>*_test</code> rule, for example,
2148 <code>cc</code>, <code>java</code> or <code>sh</code>.
2149</p>
2150<p>
2151 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
2152 is also specified) only test targets of the specified language(s).
2153</p>
2154<p>
2155 For example,
2156</p>
2157<pre>
2158 % bazel test --test_lang_filters=cc,java foo/...
2159</pre>
2160<p>
2161 will test only the C/C++ and Java tests (defined using
2162 <code>cc_test</code> and <code>java_test</code> rules, respectively)
2163 in <code>foo/...</code>, while
2164</p>
2165<pre>
2166 % bazel test --test_lang_filters=-sh,-java foo/...
2167</pre>
2168<p>
2169 will run all of the tests in <code>foo/...</code> except for the
2170 <code>sh_test</code> and <code>java_test</code> tests.
2171</p>
2172<p>
2173 By default, test language filtering is not applied.
2174</p>
2175
2176<h4 id="flag--test_filter"><code class='flag'>--test_filter=<var>filter-expression</var></code></h4>
2177<p>
2178 Specifies a filter that the test runner may use to pick a subset of tests for
2179 running. All targets specified in the invocation are built, but depending on
2180 the expression only some of them may be executed; in some cases, only certain
2181 test methods are run.
2182</p>
2183<p>
2184 The particular interpretation of <var>filter-expression</var> is up to
2185 the test framework responsible for running the test. It may be a glob,
2186 substring, or regexp. <code class='flag'>--test_filter</code> is a convenience
2187 over passing different <code class='flag'>--test_arg</code> filter arguments,
2188 but not all frameworks support it.
2189</p>
2190
2191<h3>Verbosity options: options that control what Bazel prints</h3>
2192
2193These options control the verbosity of Bazel's output,
2194either to the terminal, or to additional log files.
2195
2196<h4 id='flag--explain'><code class='flag'>--explain <var>logfile</var></code></h4>
2197<p>
2198 This option, which requires a filename argument, causes the
2199 dependency checker in <code>bazel build</code>'s execution phase to
2200 explain, for each build step, either why it is being executed, or
2201 that it is up-to-date. The explanation is written
2202 to <i>logfile</i>.
2203</p>
2204<p>
2205 If you are encountering unexpected rebuilds, this option can help to
2206 understand the reason. Add it to your <code>.bazelrc</code> so that
2207 logging occurs for all subsequent builds, and then inspect the log
2208 when you see an execution step executed unexpectedly. This option
2209 may carry a small performance penalty, so you might want to remove
2210 it when it is no longer needed.
2211</p>
2212
2213<h4 id='flag--verbose_explanations'><code class='flag'>--verbose_explanations</code></h4>
2214<p>
2215 This option increases the verbosity of the explanations generated
2216 when the <a href='#flag--explain'>--explain</a> option is enabled.
2217</p>
2218<p>
2219 In particular, if verbose explanations are enabled,
2220 and an output file is rebuilt because the command used to
2221 build it has changed, then the output in the explanation file will
2222 include the full details of the new command (at least for most
2223 commands).
2224</p>
2225<p>
2226 Using this option may significantly increase the length of the
2227 generated explanation file and the performance penalty of using
2228 <code class='flag'>--explain</code>.
2229</p>
2230<p>
2231 If <code class='flag'>--explain</code> is not enabled, then
2232 <code class='flag'>--verbose_explanations</code> has no effect.
2233</p>
2234
2235<h4 id='flag--profile'><code class='flag'>--profile <var>file</var></code></h4>
2236<p>
2237 This option, which takes a filename argument, causes Bazel to write
2238 profiling data into a file. The data then can be analyzed or parsed using the
2239 <code>bazel analyze-profile</code> command. The Build profile can be useful in
2240 understanding where Bazel's <code>build</code> command is spending its time.
2241</p>
2242
2243<h4 id='flag--show_loading_progress'><code class='flag'>--[no]show_loading_progress</code></h4>
2244<p>
2245 This option causes Bazel to output package-loading progress
2246 messages. If it is disabled, the messages won't be shown.
2247</p>
2248
2249<h4 id='flag--show_progress'><code class='flag'>--[no]show_progress</code></h4>
2250<p>
2251 This option causes progress messages to be displayed; it is on by
2252 default. When disabled, progress messages are suppressed.
2253</p>
2254
2255<h4 id='flag--show_progress_rate_limit'><code class='flag'>--show_progress_rate_limit
2256 <var>n</var></code></h4>
2257<p>
2258 This option causes bazel to display only
2259 one progress message per <code>n</code> seconds, where <var>n</var> is a real number.
2260 If <code>n</code> is -1, all progress messages will be displayed. The default value for
2261 this option is 0.03, meaning bazel will limit the progress messages to one per every
2262 0.03 seconds.
2263</p>
2264
2265<h4 id='flag--show_result'><code class='flag'>--show_result <var>n</var></code></h4>
2266<p>
2267 This option controls the printing of result information at the end
2268 of a <code>bazel build</code> command. By default, if a single
2269 build target was specified, Bazel prints a message stating whether
2270 or not the target was successfully brought up-to-date, and if so,
2271 the list of output files that the target created. If multiple
2272 targets were specified, result information is not displayed.
2273</p>
2274<p>
2275 While the result information may be useful for builds of a single
2276 target or a few targets, for large builds (e.g. an entire top-level
2277 project tree), this information can be overwhelming and distracting;
2278 this option allows it to be controlled. <code class='flag'>--show_result</code>
2279 takes an integer argument, which is the maximum number of targets
2280 for which full result information should be printed. By default,
2281 the value is 1. Above this threshold, no result information is
2282 shown for individual targets. Thus zero causes the result
2283 information to be suppressed always, and a very large value causes
2284 the result to be printed always.
2285</p>
2286<p>
2287 Users may wish to choose a value in-between if they regularly
2288 alternate between building a small group of targets (for example,
2289 during the compile-edit-test cycle) and a large group of targets
2290 (for example, when establishing a new workspace or running
2291 regression tests). In the former case, the result information is
2292 very useful whereas in the latter case it is less so. As with all
2293 options, this can be specified implicitly via
2294 the <a href='#bazelrc'><code>.bazelrc</code></a> file.
2295</p>
2296<p>
2297 The files are printed so as to make it easy to copy and paste the
2298 filename to the shell, to run built executables. The "up-to-date"
2299 or "failed" messages for each target can be easily parsed by scripts
2300 which drive a build.
2301</p>
2302
2303<h4 id='flag--subcommands'><code class='flag'>--subcommands</code> (<code>-s</code>)</h4>
2304<p>
2305 This option causes Bazel's execution phase to print the full command line
2306 for each command prior to executing it.
2307</p>
2308
2309<pre>
2310 &gt;&gt;&gt;&gt;&gt; # //examples/cpp:hello-world [action 'Linking examples/cpp/hello-world']
2311 (cd /home/jrluser/.cache/bazel/_bazel_jrluser/4c084335afceb392cfbe7c31afee3a9f/bazel && \
2312 exec env - \
2313 /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,--build-id=md5' '-Wl,--hash-style=gnu' -Wl,-S -Wl,@bazel-out/local_linux-fastbuild/bin/examples/cpp/hello-world-2.params)
2314</pre>
2315<p>
2316 Where possible, commands are printed in a Bourne shell compatible syntax,
2317 so that they can be easily copied and pasted to a shell command prompt.
2318 (The surrounding parentheses are provided to protect your shell from the
2319 <code>cd</code> and <code>exec</code> calls; be sure to copy them!)
2320 However some commands are implemented internally within Bazel, such as
2321 creating symlink trees. For these there's no command line to display.
2322
2323</p>
2324
2325<p>
2326 See also <a href="#flag--verbose_failures">--verbose_failures</a>, below.
2327</p>
2328
2329<h4 id='flag--verbose_failures'><code class='flag'>--verbose_failures</code></h4>
2330<p>
2331 This option causes Bazel's execution phase to print the full command line
2332 for commands that failed. This can be invaluable for debugging a
2333 failing build.
2334</p>
2335<p>
2336 Failing commands are printed in a Bourne shell compatible syntax, suitable
2337 for copying and pasting to a shell prompt.
2338</p>
2339
2340<h4 id='flag--stamp'><code class='flag'>--[no]stamp</code></h4>
2341<p>
2342 This option controls whether stamping is enabled for
2343 rule types that support it. For most of the supported rule types stamping is
2344 enabled by default (e.g. <code>cc_binary</code>).
2345
2346 By default, stamping is disabled for all tests. Specifying
2347 <code class='flag'>--stamp</code> does not force affected targets to be rebuilt,
2348 if their dependencies have not changed.
2349</p>
2350
2351<p>
2352 Stamping can be enabled or disabled explicitly in BUILD using
2353 the <code>stamp</code> attribute of certain rule types, please refer to
2354 the <a href="be/overview.html">build encyclopedia</a> for details. For
2355 rules that are neither explicitly or implicitly configured as <code>stamp =
2356 0</code> or <code>stamp = 1</code>, the <code class='flag'>--[no]stamp</code> option
2357 selects whether stamping is enabled. Bazel never stamps binaries that are
2358 built for the host configuration, regardless of the stamp attribute.
2359</p>
2360
2361<h3 id='misc_build_options'>Miscellaneous options</h3>
2362
2363<h4 id='flag--symlink_prefix'><code class='flag'>--symlink_prefix <var>string</var></code></h4>
2364<p>
2365 Changes the prefix of the generated convenience symlinks. The
2366 default value for the symlink prefix is <code>bazel-</code> which
2367 will create the symlinks <code>bazel-bin</code>, <code>bazel-testlogs</code>, and
2368 <code>bazel-genfiles</code>.
2369</p>
2370<p>
2371 If the symbolic links cannot be created for any reason, a warning is
2372 issued but the build is still considered a success. In particular,
2373 this allows you to build in a read-only directory or one that you have no
2374 permission to write into. Any paths printed in informational
2375 messages at the conclusion of a build will only use the
2376 symlink-relative short form if the symlinks point to the expected
2377 location; in other words, you can rely on the correctness of those
2378 paths, even if you cannot rely on the symlinks being created.
2379</p>
2380<p>
2381 Some common values of this option:
2382</p>
2383<ul>
2384
2385 <li>
2386 <p><b>Suppress symlink creation:</b>
2387 <code class='flag'>--symlink_prefix=/</code> will cause Bazel to not
2388 create or update any symlinks, including the <code>bazel-out</code> and
2389
2390 <code>bazel-&lt;workspace&gt;</code>
2391 symlinks. Use this option to suppress symlink creation entirely.
2392 </p>
2393 </li>
2394 <li>
2395 <p><b>Reduce clutter:</b>
2396 <code class='flag'>--symlink_prefix=.bazel/</code> will cause Bazel to create
2397 symlinks called <code>bin</code> (etc) inside a hidden directory <code>.bazel</code>.
2398 </p>
2399 </li>
2400</ul>
2401
2402<h4 id='flag--platform_suffix'><code class='flag'>--platform_suffix <var>string</var></code></h4>
2403<p>
2404 Adds a suffix to the configuration short name, which is used to determine the
2405 output directory. Setting this option to different values puts the files into
2406 different directories, for example to improve cache hit rates for builds that
2407 otherwise clobber each others output files, or to keep the output files around
2408 for comparisons.
2409</p>
2410
2411<h4 id='flag--default_visibility'><code class='flag'>--default_visibility=<var>(private|public)</var></code></h4>
2412<p>
2413 Temporary flag for testing bazel default visibility changes. Not intended for general use
2414 but documented for completeness' sake.
2415</p>
2416
2417<h2 id='bazel-releng'>Using Bazel for releases</h2>
2418<p>
2419 Bazel is used both by software engineers during the development
2420 cycle, and by release engineers when preparing binaries for deployment
2421 to production. This section provides a list of tips for release
2422 engineers using Bazel.
2423
2424</p>
2425
2426<h3>Significant options</h3>
2427
2428<p>
2429 When using Bazel for release builds, the same issues arise as for
2430 other scripts that perform a build, so you should read
2431 the <a href='#scripting'>scripting</a> section of this manual.
2432 In particular, the following options are strongly recommended:
2433</p>
2434<ul>
2435 <li><a href='#bazelrc'><code class='flag'>--bazelrc=/dev/null</code></a></li>
2436 <li><a href='#flag--batch'><code class='flag'>--batch</code></a></li>
2437</ul>
2438
2439<p>
2440 These options (q.v.) are also important:
2441</p>
2442
2443<ul>
2444
2445 <li><a href='#flag--package_path'><code class='flag'>--package_path</code></a></li>
2446 <li><a href='#flag--symlink_prefix'><code class='flag'>--symlink_prefix</code></a>:
2447 for managing builds for multiple configurations,
2448 it may be convenient to distinguish each build
2449 with a distinct identifier, e.g. "64bit" vs. "32bit". This option
2450 differentiates the <code>bazel-bin</code> (etc.) symlinks.
2451 </li>
2452</ul>
2453
2454<h2 id='test'>Running tests with Bazel</h2>
2455<p>
2456 To build and run tests with bazel, type <code>bazel test</code> followed by
2457 the name of the test targets.
2458</p>
2459<p>
2460 By default, this command performs simultaneous build and test
2461 activity, building all specified targets (including any non-test
2462 targets specified on the command line) and testing
2463 <code>*_test</code> and <code>test_suite</code> targets as soon as
2464 their prerequisites are built, meaning that test execution is
2465 interleaved with building. Doing so usually results in significant
2466 speed gains.
2467
2468</p>
2469
2470<h3>Options for <code>bazel test</code></h3>
2471
2472<h4 id="flag--cache_test_results"><code class='flag'>--cache_test_results=(yes|no|auto)</code> (<code>-t</code>)</h4>
2473<p>
2474 If this option is set to 'auto' (the default) then Bazel will only rerun a test if any of the
2475 following conditions applies:
2476</p>
2477<ul>
2478 <li>Bazel detects changes in the test or its dependencies</li>
2479 <li>the test is marked as <code>external</code></li>
2480 <li>multiple test runs were requested with <code class='flag'>--runs_per_test</code></li>
2481 <li>the test failed.</li>
2482</ul>
2483<p>
2484 If 'no', all tests will be executed unconditionally.
2485</p>
2486<p>
2487 If 'yes', the caching behavior will be the same as auto
2488 except that it may cache test failures and test runs with
2489 <code class='flag'>--runs_per_test</code>.
2490</p>
2491<p>
2492 Note that test results are <em>always</em> saved in Bazel's output tree,
2493 regardless of whether this option is enabled, so
2494 you needn't have used <code class='flag'>--cache_test_results</code> on the
2495 prior run(s) of <code>bazel test</code> in order to get cache hits.
2496 The option only affects whether Bazel will <em>use</em> previously
2497 saved results, not whether it will save results of the current run.
2498</p>
2499<p>
2500 Users who have enabled this option by default in
2501 their <code>.bazelrc</code> file may find the
2502 abbreviations <code>-t</code> (on) or <code>-t-</code> (off)
2503 convenient for overriding the default on a particular run.
2504</p>
2505
2506<h4 id="flag--check_tests_up_to_date"><code class='flag'>--check_tests_up_to_date</code></h4>
2507<p>
2508 This option tells Bazel not to run the tests, but to merely check and report
2509 the cached test results. If there are any tests which have not been
2510 previously built and run, or whose tests results are out-of-date (e.g. because
2511 the source code or the build options have changed), then Bazel will report
2512 an error message ("test result is not up-to-date"), will record the test's
2513 status as "NO STATUS" (in red, if color output is enabled), and will return
2514 a non-zero exit code.
2515</p>
2516<p>
2517 This option also implies
2518 <code><a href="#flag--check_up_to_date">--check_up_to_date</a></code> behavior.
2519</p>
2520<p>
2521 This option may be useful for pre-submit checks.
2522</p>
2523
2524<h4 id="flag--test_verbose_timeout_warnings"><code class='flag'>--test_verbose_timeout_warnings</code></h4>
2525<p>
2526 This option tells Bazel to explicitly warn the user if a test's timeout is
2527significantly longer then the test's actual execution time. While a test's
2528timeout should be set such that it is not flaky, a test that has a highly
2529over-generous timeout can hide real problems that crop up unexpectedly.
2530</p>
2531<p>
2532For instance, a test that normally executes in a minute or two should not have
2533a timeout of ETERNAL or LONG as these are much, much too generous.
2534
2535 This option is useful to help users decide on a good timeout value or
2536 sanity check existing timeout values.
2537</p>
2538<p>
2539Note that each test shard is allotted the timeout of the entire
2540<code>XX_test</code> target. Using this option does not affect a test's timeout
2541value, merely warns if Bazel thinks the timeout could be restricted further.
2542</p>
2543
2544<h4 id='flag--test_keep_going'><code class='flag'>--[no]test_keep_going</code></h4>
2545<p>
2546 By default, all tests are run to completion. If this flag is disabled,
2547 however, the build is aborted on any non-passing test. Subsequent build steps
2548 and test invocations are not run, and in-flight invocations are canceled.
2549 Do not specify both <code class='flag'>--notest_keep_going</code> and
2550 <code class='flag'>--keep_going</code>.
2551</p>
2552
2553<h4 id='flag--flaky_test_attempts'><code class='flag'>--flaky_test_attempts <var>attempts</var></code></h4>
2554<p>
2555 This option specifies the maximum number of times a test should be attempted
2556 if it fails for any reason. A test that initially fails but eventually
2557 succeeds is reported as <code>FLAKY</code> on the test summary. It is,
2558 however, considered to be passed when it comes to identifying Bazel exit code
2559 or total number of passed tests. Tests that fail all allowed attempts are
2560 considered to be failed.
2561</p>
2562<p>
2563 By default (when this option is not specified, or when it is set to
2564 &quot;default&quot;), only a single attempt is allowed for regular tests, and
2565 3 for test rules with the <code>flaky</code> attribute set. You can specify
2566 an integer value to override the maximum limit of test attempts. Bazel allows
2567 a maximum of 10 test attempts in order to prevent abuse of the system.
2568</p>
2569
2570<h4 id='flag--runs_per_test'><code class='flag'>--runs_per_test <var>[regex@]number</var></code></h4>
2571<p>
2572 This option specifies the number of times each test should be executed. All
2573 test executions are treated as separate tests (e.g. fallback functionality
2574 will apply to each of them independently).
2575</p>
2576<p>
2577 The status of a target with failing runs depends on the value of the
2578 <code>--runs_per_test_detects_flakes</code> flag:
2579</p>
2580<ul>
2581 <li>If absent, any failing run causes the entire test to fail.</li>
2582 <li>If present and two runs from the same shard return PASS and FAIL, the test
2583 will receive a status of flaky (unless other failing runs cause it to
2584 fail).</li>
2585</ul>
2586
2587<p>
2588 If a single number is specified, all tests will run that many times.
2589 Alternatively, a regular expression may be specified using the syntax
2590 regex@number. This constrains the effect of --runs_per_test to targets
2591 which match the regex (e.g. "--runs_per_test=^//pizza:.*@4" runs all tests
2592 under //pizza/ 4 times).
2593 This form of --runs_per_test may be specified more than once.
2594</p>
2595
2596<h4 id='flag--runs_per_test_detects_flakes'><code
2597 class='flag'>--[no]runs_per_test_detects_flakes</code></h4>
2598<p>
2599 If this option is specified (by default it is not), Bazel will detect flaky
2600 test shards through --runs_per_test. If one or more runs for a single shard
2601 fail and one or more runs for the same shard pass, the target will be
2602 considered flaky with the flag. If unspecified, the target will report a
2603 failing status.
2604</p>
2605
2606<h4 id='flag--test_summary'><code class='flag'>--test_summary <var>output_style</var></code></h4>
2607<p>
2608 Specifies how the test result summary should be displayed.
2609</p>
2610<ul>
2611 <li><code>short</code> prints the results of each test along with the name of
2612 the file containing the test output if the test failed. This is the default
2613 value.
2614 </li>
2615 <li><code>terse</code> like <code>short</code>, but even shorter: only print
2616 information about tests which did not pass.
2617 </li>
2618 <li><code>detailed</code> prints each individual test case that failed, not
2619 only each test. The names of test output files are omitted.
2620 </li>
2621 <li><code>none</code> does not print test summary.
2622 </li>
2623</ul>
2624
2625<h4 id='flag--test_output'><code class='flag'>--test_output <var>output_style</var></code></h4>
2626<p>
2627 Specifies how test output should be displayed:
2628</p>
2629<ul>
2630 <li><code>summary</code> shows a summary of whether each test passed or
2631 failed. Also shows the output log file name for failed tests. The summary
2632 will be printed at the end of the build (during the build, one would see
2633 just simple progress messages when tests start, pass or fail).
2634 This is the default behavior.
2635 </li>
2636 <li><code>errors</code> sends combined stdout/stderr output from failed tests
2637 only into the stdout immediately after test is completed, ensuring that
2638 test output from simultaneous tests is not interleaved with each other.
2639 Prints a summary at the build as per summary output above.
2640 </li>
2641 <li><code>all</code> is similar to <code>errors</code> but prints output for
2642 all tests, including those which passed.
2643 </li>
2644 <li><code>streamed</code> streams stdout/stderr output from each test in
2645 real-time.
2646
2647 </li>
2648</ul>
2649
2650<h4 id='flag--java_debug'><code class='flag'>--java_debug</code></h4>
2651<p>
2652 This option causes the Java virtual machine of a java test to wait for a connection from a
2653 JDWP-compliant debugger before starting the test. This option implies --test_output=streamed.
2654</p>
2655
2656<h4 id='flag--verbose_test_summary'><code class='flag'>--[no]verbose_test_summary</code></h4>
2657<p>
2658 By default this option is enabled, causing test times and other additional
2659 information (such as test attempts) to be printed to the test summary. If
2660 <code class='flag'>--noverbose_test_summary</code> is specified, test summary will
2661 include only test name, test status and cached test indicator and will
2662 be formatted to stay within 80 characters when possible.
2663</p>
2664
2665<h4 id='flag--test_tmpdir'><code class='flag'>--test_tmpdir <var>path</var></code></h4>
2666<p>
2667 Specifies temporary directory for tests executed locally. Each test will be
2668 executed in a separate subdirectory inside this directory. The directory will
2669 be cleaned at the beginning of the each <code>bazel test</code> command.
2670 By default, bazel will place this directory under Bazel output base directory.
2671 Note that this is a directory for running tests, not storing test results
2672 (those are always stored under the <code>bazel-out</code> directory).
2673</p>
2674
2675<h4 id='flag--test_timeout'>
2676 <code class='flag'>--test_timeout
2677 <var>seconds</var></code>
2678 OR
2679 <code class='flag'>--test_timeout
2680 <var>seconds</var>,<var>seconds</var>,<var>seconds</var>,<var>seconds</var>
2681 </code>
2682</h4>
2683<p>
2684 Overrides the timeout value for all tests by using specified number of
2685 seconds as a new timeout value. If only one value is provided, then it will
2686 be used for all test timeout categories.
2687 </p>
2688 <p>
2689 Alternatively, four comma-separated values may be provided, specifying
2690 individual timeouts for short, moderate, long and eternal tests (in that
2691 order).
2692 In either form, zero or a negative value for any of the test sizes will
2693 be substituted by the default timeout for the given timeout categories as
2694 defined by the page
2695 <a href="test-encyclopedia.html">Writing Tests</a>.
2696 By default, Bazel will use these timeouts for all tests by
2697 inferring the timeout limit from the test's size whether the size is
2698 implicitly or explicitly set.
2699</p>
2700<p>
2701 Tests which explicitly state their timeout category as distinct from their
2702 size will receive the same value as if that timeout had been implicitly set by
2703 the size tag. So a test of size 'small' which declares a 'long' timeout will
2704 have the same effective timeout that a 'large' tests has with no explicit
2705 timeout.
2706</p>
2707
2708<h4 id='flag--test_arg'><code class='flag'>--test_arg <var>arg</var></code></h4>
2709<p>
Michael Staib3df106e2017-02-23 23:08:40 +00002710 Passes command-line options/flags/arguments to each test process. This
David Chen8fe82a32016-08-24 10:55:41 +00002711 option can be used multiple times to pass several arguments, e.g.
2712 <code class='flag'>--test_arg=--logtostderr --test_arg=--v=3</code>.
2713</p>
2714
2715<h4 id='flag--test_env'><code class='flag'>--test_env <var>variable</var>=<i>value</i></code>
2716 OR
2717 <code class='flag'>--test_env <var>variable</var></code></h4>
2718<p>
2719 Specifies additional variables that must be injected into the test
2720 environment for each test. If <var>value</var> is not specified it will be
2721 inherited from the shell environment used to start the <code>bazel test</code>
2722 command.
2723</p>
2724<p>
2725 The environment can be accessed from within a test by using
2726 <code>System.getenv("var")</code> (Java),
2727 <code>getenv("var")</code> (C or C++),
2728
2729</p>
2730
2731<h4 id="flag--run_under"><code class='flag'>--run_under=<var>command-prefix</var></code></h4>
2732<p>
2733 This specifies a prefix that the test runner will insert in front
2734 of the test command before running it. The
2735 <var>command-prefix</var> is split into words using Bourne shell
2736 tokenization rules, and then the list of words is prepended to the
2737 command that will be executed.
2738</p>
2739<p>
2740 If the first word is a fully qualified label (i.e. starts with
2741 <code>//</code>) it is built. Then the label is substituted by the
2742 corresponding executable location that is prepended to the command
2743 that will be executed along with the other words.
2744</p>
2745
2746<p>
2747Some caveats apply:
2748</p>
2749<ul>
2750 <li>
2751 The PATH used for running tests may be different than the PATH in your environment,
2752 so you may need to use an <b>absolute path</b> for the <code class='flag'>--run_under</code>
2753 command (the first word in <var>command-prefix</var>).
2754 </li>
2755 <li>
2756 <b><code>stdin</code> is not connected</b>, so <code class='flag'>--run_under</code>
2757 can't be used for interactive commands.
2758 </li>
2759
2760</ul>
2761<p>
2762Examples:
2763</p>
2764<pre>
2765 --run_under=/usr/bin/valgrind
2766 --run_under=/usr/bin/strace
2767 --run_under='/usr/bin/strace -c'
2768 --run_under='/usr/bin/valgrind --quiet --num-callers=20'
2769
2770</pre>
2771
2772<h4>Test selection</h4>
2773<p>
2774 As documented under <a href='#output-selection-options'>Output selection options</a>,
2775 you can filter tests by <a href='#flag--test_size_filters'>size</a>,
2776 <a href='#flag--test_timeout_filters'>timeout</a>,
2777 <a href='#flag--test_tag_filters'>tag</a>, or
2778 <a href='#flag--test_lang_filters'>language</a>. A convenience
2779 <a href='#flag--test_filter'>general name filter</a> can forward particular
2780 filter args to the test runner.
2781</p>
2782
kchodorowfae5c742017-03-28 16:53:24 +00002783<h4 id="other_options_for_bazel_test">Other options for <code>bazel test</code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00002784<p>
2785 The syntax and the remaining options are exactly like
2786 <a href='#build'>bazel build</a>.
2787</p>
2788
2789
2790
2791<h2 id='run'>Running executables with Bazel</h2>
2792<p>
2793 The <code>bazel run</code> command is similar to <code>bazel build</code>, except
2794 it is used to build and run a single target. Here is a typical session:
2795</p>
2796<pre>
2797 % bazel run -- java/myapp:myapp --arg1 --arg2
2798 Welcome to Bazel
2799 INFO: Loading package: java/myapp
2800 INFO: Loading package: foo/bar
2801 INFO: Loading complete. Analyzing...
2802 INFO: Found 1 target...
2803 ...
2804 Target //java/myapp:myapp up-to-date:
2805 bazel-bin/java/myapp:myapp
2806 INFO: Elapsed time: 0.638s, Critical Path: 0.34s
2807
2808 INFO: Running command line: bazel-bin/java/myapp:myapp --arg1 --arg2
2809 Hello there
2810 $EXEC_ROOT/java/myapp/myapp
2811 --arg1
2812 --arg2
2813</pre>
2814
2815<p>
2816 Bazel closes stdin, so you can't use <code>bazel run</code>
2817 if you want to start an interactive program or pipe data to it.
2818</p>
2819
2820<p>
2821 Note the use of the <code>--</code>. This is needed so that Bazel
2822 does not interpret <code>--arg1</code> and <code>--arg2</code> as
2823 Bazel options, but rather as part of the command line for running the binary.
2824 (The program being run simply says hello and prints out its args.)
2825</p>
2826
2827<h3>Options for <code>bazel run</code></h3>
2828
2829<h4 id='flag--run_under_run'><code class='flag'>--run_under=<var>command-prefix</var></code></h4>
2830<p>
2831 This has the same effect as the <code class='flag'>--run_under</code> option for
2832 <code>bazel test</code> (<a href='#flag--run_under'>see above</a>),
2833 except that it applies to the command being run by <code>bazel
2834 run</code> rather than to the tests being run by <code>bazel test</code>
2835 and cannot run under label.
2836</p>
2837
2838<h3>Executing tests</h3>
2839
2840<p>
2841 <code>bazel run</code> can also execute test binaries, which has the effect of
2842running the test, but without the setup documented on the page
2843<a href='test-encyclopedia.html'>Writing Tests</a>, so that the test runs
2844in an environment closer to the current shell environment. Note that none of the
2845--test_* arguments have an effect when running a test in this manner.
2846</p>
2847
2848<h2 id='query'>Querying the dependency graph with Bazel</h2>
2849
2850<p>
2851 Bazel includes a query language for asking questions about the
2852 dependency graph used during the build. The query tool is an
2853 invaluable aid to many software engineering tasks.
2854</p>
2855<p>
2856 The query language is based on the idea of
2857 algebraic operations over graphs; it is documented in detail in
2858
2859 <a href="query.html">Bazel Query Reference</a>.
2860 Please refer to that document for reference, for
2861 examples, and for query-specific command-line options.
2862</p>
2863
2864<p>
2865 The query tool accepts several command-line
2866 option. <code class='flag'>--output</code> selects the output format.
2867 <code class='flag'>--[no]keep_going</code> (disabled by default) causes the query
2868 tool to continue to make progress upon errors; this behavior may be
2869 disabled if an incomplete result is not acceptable in case of errors.
2870</p>
2871<p>
2872 The <code class='flag'>--[no]host_deps</code> option,
2873 enabled by default, causes dependencies on "host
2874 configuration" targets to be included in the dependency graph over
2875 which the query operates.
2876
2877</p>
2878<p>
2879 The <code class='flag'>--[no]implicit_deps</code> option, enabled by default, causes
2880 implicit dependencies to be included in the dependency graph over which the query operates. An
2881 implicit dependency is one that is not explicitly specified in the BUILD file
2882 but added by bazel.
2883</p>
2884<p>
2885 Example: "Show the locations of the definitions (in BUILD files) of
2886 all genrules required to build all the tests in the PEBL tree."
2887</p>
2888<pre>
2889 bazel query --output location 'kind(genrule, deps(kind(".*_test rule", foo/bar/pebl/...)))'
2890</pre>
2891
2892
2893<h2 id='misc'>Miscellaneous Bazel commands and options</h2>
2894
2895<h3 id='help'>The <code>help</code> command</h3>
2896
2897<p>
2898 The <code>help</code> command provides on-line help. By default, it
2899 shows a summary of available commands and help topics, as shown in
2900 the <a href='#overview'><i>Bazel overview</i></a> section above.
2901 Specifying an argument displays detailed help for a particular
2902 topic. Most topics are Bazel commands, e.g. <code>build</code>
2903 or <code>query</code>, but there are some additional help topics
2904 that do not correspond to commands.
2905</p>
2906
2907<h4 id='flag--long'><code class='flag'>--[no]long</code> (<code>-l</code>)</h4>
2908<p>
2909 By default, <code>bazel help [<var>topic</var>]</code> prints only a
2910 summary of the relevant options for a topic. If
2911 the <code class='flag'>--long</code> option is specified, the type, default value
2912 and full description of each option is also printed.
2913</p>
2914
2915<h3 id='shutdown'>The <code>shutdown</code> command</h3>
2916
2917<p>
2918 Bazel server processes (see <a href='#client/server'>Client/server
2919 implementation</a>) may be stopped by using the <code>shutdown</code>
2920 command. This command causes the Bazel server to exit as soon as it
2921 becomes idle (i.e. after the completion of any builds or other
2922 commands that are currently in progress).
2923
2924 Bazel servers stop themselves after an idle timeout, so this command
2925 is rarely necessary; however, it can be useful in scripts when it is
2926 known that no further builds will occur in a given workspace.
2927</p>
2928<p>
2929 <code>shutdown</code> accepts one
2930 option, <code class='flag'>--iff_heap_size_greater_than <i>n</i></code>, which
2931 requires an integer argument (in MB). If specified, this makes the shutdown
2932 conditional on the amount of memory already consumed. This is
2933 useful for scripts that initiate a lot of builds, as any memory
2934 leaks in the Bazel server could cause it to crash spuriously on
2935 occasion; performing a conditional restart preempts this condition.
2936</p>
2937
2938<h3 id='info'>The <code>info</code> command</h3>
2939
2940<p>
2941 The <code>info</code> command prints various values associated with
2942 the Bazel server instance, or with a specific build configuration.
2943 (These may be used by scripts that drive a build.)
2944</p>
2945
2946<p>
2947 The <code>info</code> command also permits a single (optional)
2948 argument, which is the name of one of the keys in the list below.
2949 In this case, <code>bazel info <var>key</var></code> will print only
2950 the value for that one key. (This is especially convenient when
2951 scripting Bazel, as it avoids the need to pipe the result
2952 through <code>sed -ne /key:/s/key://p</code>:
2953</p>
2954
2955<h4>Configuration-independent data</h4>
2956<ul>
2957 <li><code>release</code>: the release label for this Bazel
2958 instance, or "development version" if this is not a released
2959 binary.
2960 </li>
2961 <li><code>workspace</code> the absolute path to the base workspace
2962 directory.
2963 </li>
2964 <li><code>install_base</code>: the absolute path to the installation
2965 directory used by this Bazel instance for the current user. Bazel
2966 installs its internally required executables below this directory.
2967
2968 </li>
2969 <li><code>output_base</code>: the absolute path to the base output
2970 directory used by this Bazel instance for the current user and
2971 workspace combination. Bazel puts all of its scratch and build
2972 output below this directory.
2973 </li>
2974 <li><code>execution_root</code>: the absolute path to the execution
2975 root directory under output_base. This directory is the root for all files
2976 accessible to commands executed during the build, and is the working
2977 directory for those commands. If the workspace directory is writable, a
2978 symlink named
2979
2980 <code>bazel-&lt;workspace&gt;</code>
2981 is placed there pointing to this directory.
2982 </li>
2983 <li><code>output_path</code>: the absolute path to the output
2984 directory beneath the execution root used for all files actually
2985 generated as a result of build commands. If the workspace directory is
2986 writable, a symlink named <code>bazel-out</code> is placed there pointing
2987 to this directory.
2988 </li>
2989 <li><code>server_pid</code>: the process ID of the Bazel server
2990 process. </li>
2991 <li><code>command_log</code>: the absolute path to the command log file;
2992 this contains the interleaved stdout and stderr streams of the most recent
2993 Bazel command. Note that running <code>bazel info</code> will overwrite the
2994 contents of this file, since it then becomes the most recent Bazel command.
2995 However, the location of the command log file will not change unless you
2996 change the setting of the <code class='flag'>--output_base</code> or
2997 <code class='flag'>--output_user_root</code> options.
2998 </li>
2999
3000 <li><code>used-heap-size</code>,
3001 <code>committed-size</code>,
3002 <code>max-heap-size</code>: reports various JVM heap size
3003 parameters. Respectively: memory currently used, memory currently
3004 guaranteed to be available to the JVM from the system, maximum
3005 possible allocation.
3006 </li>
3007 <li><code>gc-count</code>, <code>gc-time</code>: The cumulative count of
3008 garbage collections since the start of this Bazel server and the time spent
3009 to perform them. Note that these values are not reset at the start of every
3010 build.
3011 </li>
3012 <li><code>package_path</code>: A colon-separated list of paths which would be
3013 searched for packages by bazel. Has the same format as the
3014 <code class='flag'>--package_path</code> build command line argument.
3015 </li>
3016</ul>
3017<p>
3018 Example: the process ID of the Bazel server.
3019</p>
3020<pre>% bazel info server_pid
30211285
3022</pre>
3023
3024<h4>Configuration-specific data</h4>
3025<p>
3026 These data may be affected by the configuration options passed
3027 to <code>bazel info</code>, for
3028 example <code class='flag'>--cpu</code>, <code class='flag'>--compilation_mode</code>,
3029 etc. The <code>info</code> command accepts all
3030 the <a href='#analysis-options'>options that control dependency
3031 analysis</a>, since some of these determine the location of the
3032 output directory of a build, the choice of compiler, etc.
3033</p>
3034<ul>
3035 <li>
3036 <code>bazel-bin</code>, <code>bazel-testlogs</code>,
3037 <code>bazel-genfiles</code>: reports the absolute path to
3038 the <code>bazel-*</code> directories in which programs generated by the
3039 build are located. This is usually, though not always, the same as
3040 the <code>bazel-*</code> symlinks created in the base workspace directory after a
3041 successful build. However, if the workspace directory is read-only,
3042 no <code>bazel-*</code> symlinks can be created. Scripts that use
3043 the value reported by <code>bazel info</code>, instead of assuming the
3044 existence of the symlink, will be more robust.
3045 </li>
3046 <li>
3047 The complete
3048 <a href='be/make-variables.html'
3049 >"Make" environment</a>. If the <code class='flag'>--show_make_env</code> flag is
3050 specified, all variables in the current configuration's "Make" environment
3051 are also displayed (e.g. <code>CC</code>, <code>GLIBC_VERSION</code>, etc).
3052 These are the variables accessed using the <code>$(CC)</code>
3053 or <code>varref("CC")</code> syntax inside BUILD files.
3054 </li>
3055</ul>
3056
3057<p>
3058 Example: the C++ compiler for the current configuration.
3059 This is the <code>$(CC)</code> variable in the "Make" environment,
3060 so the <code class='flag'>--show_make_env</code> flag is needed.
3061</p>
3062
3063<pre>
Ulf Adamse0437b12016-08-29 14:48:47 +00003064 % bazel info --show_make_env -c opt COMPILATION_MODE
3065 opt
David Chen8fe82a32016-08-24 10:55:41 +00003066</pre>
3067
3068<p>
3069 Example: the <code>bazel-bin</code> output directory for the current
3070 configuration. This is guaranteed to be correct even in cases where
3071 the <code>bazel-bin</code> symlink cannot be created for some reason
3072 (e.g. you are building from a read-only directory).
3073</p>
3074
3075<h3 id='version'>The <code>version</code> command</h3>
3076
3077<p>
3078 The version command prints version details about the built Bazel
3079 binary, including the changelist at which it was built and the date.
3080 These are particularly useful in determining if you have the latest
3081 Bazel, or if you are reporting bugs. Some of the interesting values
3082 are:
3083</p>
3084<ul>
3085 <li><code>changelist</code>: the changelist at which this version of
3086 Bazel was released.
3087 </li>
3088 <li><code>label</code>: the release label for this Bazel
3089 instance, or "development version" if this is not a released
3090 binary. Very useful when reporting bugs.
3091 </li>
3092</ul>
3093
3094<h3 id='mobile-install'>The <code>mobile-install</code> command</h3>
3095<p>
3096 The <code>mobile-install</code> command installs apps to mobile devices.
3097 Currently only Android devices running ART are supported.
Alex Humesky105e6612017-01-06 19:03:10 +00003098
3099 See <a href="mobile-install.html">bazel mobile-install</a>
3100 for more information.
David Chen8fe82a32016-08-24 10:55:41 +00003101</p>
3102<p>
3103 Note that this command does not install the same thing that
3104 <code>bazel build</code> produces: Bazel tweaks the app so that it can be
3105 built, installed and re-installed quickly. This should, however, be mostly
3106 transparent to the app.
3107</p>
3108<p>
3109 The following options are supported:
3110</p>
3111<h4 id='flag--incremental'><code class='flag'>--incremental</code></h4>
3112<p>
3113 If set, Bazel tries to install the app incrementally, that is, only those
3114 parts that have changed since the last build. This cannot update resources
3115 referenced from <code>AndroidManifest.xml</code>, native code or Java
3116 resources (i.e. ones referenced by <code>Class.getResource()</code>). If these
3117 things change, this option must be omitted. Contrary to the spirit of Bazel
3118 and due to limitations of the Android platform, it is the
3119 <b>responsibility of the user</b> to know when this command is good enough and
Alex Humesky105e6612017-01-06 19:03:10 +00003120 when a full install is needed.
3121
3122 If you are using a device with Marshmallow or later, consider the
3123 <a href='#flag--split_apks'><code class='flag'>--split_apks</code></a> flag.
David Chen8fe82a32016-08-24 10:55:41 +00003124</p>
Alex Humesky105e6612017-01-06 19:03:10 +00003125<h4 id='flag--split_apks'><code class='flag'>--split_apks</code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00003126<p>
Alex Humesky105e6612017-01-06 19:03:10 +00003127 Whether to use split apks to install and update the application on the device.
3128 Works only with devices with Marshmallow or later. Note that the
3129 <a href='#flag--incremental'><code class='flag'>--incremental</code></a> flag
3130 is not necessary when using <code class='flag'>--split_apks</code>.
David Chen8fe82a32016-08-24 10:55:41 +00003131</p>
Alex Humesky105e6612017-01-06 19:03:10 +00003132<h4 id='flag--start_app'><code class='flag'>--start_app</code></h4>
3133<p>
3134 Starts the app in a clean state after installing. Equivalent to
3135 <code>--start=COLD</code>.
3136</p>
Googler2b88f622017-03-16 21:52:14 +00003137<h4 id='flag--debug_app'><code class='flag'>--debug_app</code></h4>
Alex Humesky105e6612017-01-06 19:03:10 +00003138<p>
Googler2b88f622017-03-16 21:52:14 +00003139 Waits for debugger to be attached before starting the app in a clean state after installing.
3140 Equivalent to <code>--start=DEBUG</code>.
3141</p>
3142<h4 id='flag--start'><code class='flag'>--start=<i>start_type</i></code></h4>
3143<p>
3144 How the app should be started after installing it. Supported <i>start_type</i>s are:
3145 <ul>
3146 <li><code>NO</code> Does not start the app. This is the default.</li>
3147 <li><code>COLD</code> Starts the app from a clean state after install.</li>
3148 <li><code>WARM</code> Preserves and restores the application state on incremental installs.</li>
3149 <li><code>DEBUG</code> Waits for the debugger before starting the app in a clean state after install.</li>
3150 </ul>
3151 Note that if more than one of <code class='flag'>--start=<i>start_type</i></code>,
3152 <code class='flag'>--start_app</code> or
3153 <code class='flag'>--debug_app</code> is set, the last value will be used.
Alex Humesky105e6612017-01-06 19:03:10 +00003154</p>
3155<h4 id='flag--adb'><code class='flag'>--adb <var>path</var></code></h4>
3156<p>
3157 Indicates the <code>adb</code> binary to be used.
3158
3159 The default is to use the adb in the Android SDK specified by
3160 <a href='#flag--android_sdk'><code class='flag'>--android_sdk</code></a>.
3161
3162</p>
3163<h4 id='flag--adb_arg'><code class='flag'>--adb_arg <var>arg</var></code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00003164<p>
3165 Extra arguments to <code>adb</code>. These come before the subcommand in the
3166 command line and are typically used to specify which device to install to.
Alex Humesky105e6612017-01-06 19:03:10 +00003167 For example, to select the Android device or emulator to use:
David Chen8fe82a32016-08-24 10:55:41 +00003168<pre>% bazel mobile-install --adb_arg=-s --adb_arg=deadbeef
3169</pre>
3170will invoke <code>adb</code> as
3171<pre>
3172adb -s deadbeef install ...
3173</pre>
Alex Humesky105e6612017-01-06 19:03:10 +00003174</p>
3175<h4 id='flag--adb_jobs'><code class='flag'>--adb_jobs <var>number</var></code></h4>
3176<p>
3177 The number of instances of adb to use in parallel to update files on the
3178 device.
3179</p>
3180<h4 id='flag--incremental_install_verbosity'><code class='flag'>--incremental_install_verbosity <var>number</var></code></h4>
3181<p>
3182 The verbosity for incremental install. Set to 1 for debug logging to be
3183 printed to the console.
David Chen8fe82a32016-08-24 10:55:41 +00003184</p>
3185
3186<h3 id='analyze-profile'>The <code>analyze-profile</code> command</h3>
3187
3188<p>
3189 The <code>analyze-profile</code> command analyzes data previously gathered
3190 during the build using <code class='flag'>--profile</code> option. It provides several
3191 options to either perform analysis of the build execution or export data in
3192 the specified format.
3193
3194</p>
3195<p>
3196 The following options are supported:
3197</p>
3198<ul>
3199 <li><code id='flag--dump'>--dump=text</code> displays all gathered data in a
3200 <a href='#dump-text-format'>human-readable format</a></li>
3201 <li><code>--dump=raw</code> displays all gathered data in a
3202 <a href='#dump-raw-format'>script-friendly format</a></li>
3203 <li><code id='flag--html'>--html</code> generates an <a href='#dump-html-format'>HTML file</a> visualizing the
3204 actions and rules executed in the build, as well as summary statistics for the build
3205 <ul>
3206 <li><code id='flag--html_details'>--html_details</code> adds more fine-grained
3207 information on actions and rules to the HTML visualization</li>
Dmitry Shevchenkob99c1cf2016-12-28 17:09:20 +00003208 <ul>
3209 <li><code id='flag--html_histograms'>--html_histograms</code> adds histograms for Skylark
3210 functions clicked in the statistics table. This will increase file size massively</li>
3211 <li><code id='flag--nochart'>--nochart</code> hides the task chart from generated HTML
3212 </li>
3213 </ul>
3214 </ul>
3215 </li>
3216 <li><code id='flag--combine'>--combine</code> combines multiple profile data files into a single
3217 report. Does not generate HTML task charts</li>
3218 <li><code id='flag--task_tree'>--task_tree</code> prints the tree of tasks matching the given
3219 regular expression
3220 <ul>
3221 <li><code id='flag--task_tree_threshold'>--task_tree_threshold</code> skip tasks with duration
3222 less than threshhold, in milliseconds. Default is 50ms</li>
David Chen8fe82a32016-08-24 10:55:41 +00003223 </ul>
3224 </li>
3225</ul>
3226
3227<p>
3228 See the section on <a href='#profiling'>Troubleshooting performance by profiling</a> for
3229 format details and usage help.
3230
3231</p>
3232
3233<h3 id='canonicalize'>The <code>canonicalize-flags</code> command</h3>
3234
3235<p>
3236 The <code>canonicalize-flags</code> command, which takes a list of options for
3237 a Bazel command and returns a list of options that has the same effect. The
3238 new list of options is canonical, i.e., two lists of options with the same
3239 effect are canonicalized to the same new list.
3240</p>
3241<p>
3242 The <code class='flag'>--for_command</code> option can be used to select between different
3243 commands. At this time, only <code>build</code> and <code>test</code> are
3244 supported. Options that the given command does not support cause an error.
3245</p>
3246<p>
3247 Note that a small number of options cannot be reordered, because Bazel cannot
3248 ensure that the effect is identical.
3249</p>
3250
3251<h3 id='startup_options'>Bazel startup options</h3>
3252
3253<p>
3254 The options described in this section affect the startup of the Java
3255 virtual machine used by Bazel server process, and they apply to all
3256 subsequent commands handled by that server. If there is an already
3257 running Bazel server and the startup options do not match, it will
3258 be restarted.
3259</p>
3260<p>
3261 All of the options described in this section must be specified using the
3262 <code class='flag'>--key=value</code> or <code class='flag'>--key value</code>
3263 syntax. Also, these options must appear <i>before</i> the name of the Bazel
3264 command.
3265</p>
3266
3267<h4 id='flag--output_base'><code class='flag'>--output_base=<var>dir</var></code></h4>
3268<p>
3269 This option requires a path argument, which must specify a
3270 writable directory. Bazel will use this location to write all its
3271 output. The output base is also the key by which the client locates
3272 the Bazel server. By changing the output base, you change the server
3273 which will handle the command.
3274</p>
3275<p>
3276 By default, the output base is derived from the user's login name,
3277 and the name of the workspace directory (actually, its MD5 digest),
3278 so a typical value looks like:
3279
3280 <code>/var/tmp/google/_bazel_jrluser/d41d8cd98f00b204e9800998ecf8427e</code>.
3281 Note that the client uses the output base to find the Bazel server
3282 instance, so if you specify a different output base in a Bazel
3283 command, a different server will be found (or started) to handle the
3284 request. It's possible to perform two concurrent builds in the same
3285 workspace directory by varying the output base.
3286</p>
3287
3288<p>For example:</p>
3289<pre>
3290 % bazel --output_base /tmp/1 build //foo &amp; bazel --output_base /tmp/2 build //bar
3291</pre>
3292<p>
3293 In this command, the two Bazel commands run concurrently (because of
3294 the shell <code>&amp;</code> operator), each using a different Bazel
3295 server instance (because of the different output bases).
3296 In contrast, if the default output base was used in both commands,
3297 then both requests would be sent to the same server, which would
3298 handle them sequentially: building <code>//foo</code> first, followed
3299 by an incremental build of <code>//bar</code>.
3300</p>
3301<p>
3302 We recommend you do not use NFS locations for the output base, as
3303 the higher access latency of NFS will cause noticeably slower
3304 builds.
3305</p>
3306
3307<h4 id='flag--output_user_root'><code class='flag'>--output_user_root=<var>dir</var></code></h4>
3308<p>
3309 By default, the <code>output_base</code> value is chosen to as to
3310 avoid conflicts between multiple users building in the same workspace directory.
3311 In some situations, though, it is desirable to build from a directory
3312 shared between multiple users; release engineers often do this. In
3313 those cases it may be useful to deliberately override the default so
3314 as to ensure "conflicts" (i.e., sharing) between multiple users.
3315 Use the <code class='flag'>--output_user_root</code> option to achieve this: the
3316 output base is placed in a subdirectory of the output user root,
3317 with a unique name based on the workspace, so the result of using an
3318 output user root that is not a function of <code>$USER</code> is
3319 sharing. Of course, it is important to ensure (via umask and group
3320 membership) that all the cooperating users can read/write each
3321 others files.
3322</p>
3323<p>
3324 If the <code class='flag'>--output_base</code> option is specified, it overrides
3325 using <code class='flag'>--output_user_root</code> to calculate the output base.
3326</p>
3327<p>
3328 The install base location is also calculated based on
3329 <code class='flag'>--output_user_root</code>, plus the MD5 identity of the Bazel embedded
3330 binaries.
3331</p>
3332<p>
3333 You can also use the <code class='flag'>--output_user_root</code> option to choose an
3334 alternate base location for all of Bazel's output (install base and output
3335 base) if there is a better location in your filesystem layout.
3336</p>
3337
3338<h4 id='flag--host_jvm_args'><code class='flag'>--host_jvm_args=<var>string</var></code></h4>
3339<p>
3340 Specifies a startup option to be passed to the Java virtual machine in which <i>Bazel itself</i>
3341 runs. This can be used to set the stack size, for example:
3342</p>
3343<pre>
3344 % bazel --host_jvm_args="-Xss256K" build //foo
3345</pre>
3346<p>
3347 This option can be used multiple times with individual arguments. Note that
3348 setting this flag should rarely be needed. You can also pass a space-separated list of strings,
3349 each of which will be interpreted as a separate JVM argument, but this feature will soon be
3350 deprecated.
3351
3352</p>
3353<p>
3354 That this does <i>not</i> affect any JVMs used by
3355 subprocesses of Bazel: applications, tests, tools, etc. To pass
3356 JVM options to executable Java programs, whether run by <code>bazel
3357 run</code> or on the command-line, you should use
3358 the <code>--jvm_flags</code> argument which
3359 all <code>java_binary</code> and <code>java_test</code> programs
3360 support. Alternatively for tests, use <code>bazel
3361 test --test_arg=--jvm_flags=foo ...</code>.
3362</p>
3363
3364<h4 id='flag--host_jvm_debug'><code class='flag'>--host_jvm_debug</code></h4>
3365<p>
3366 This option causes the Java virtual machine to wait for a connection
3367 from a JDWP-compliant debugger before
3368 calling the main method of <i>Bazel itself</i>. This is primarily
3369 intended for use by Bazel developers.
3370</p>
3371<p>
3372 (Please note that this does <i>not</i> affect any JVMs used by
3373 subprocesses of Bazel: applications, tests, tools, etc.)
3374</p>
3375
3376<h4 id='flag--batch'><code class='flag'>--batch</code></h4>
3377<p>
3378 This switch will cause bazel to be run in batch mode, instead of the
3379 standard client/server mode described <a href='#client/server'>above</a>.
3380 Doing so provides more predictable semantics with respect to signal handling,
3381 job control, and environment variable inheritance, and is necessary for running
3382 bazel in a chroot jail.
3383</p>
3384
3385<p>
3386 Batch mode retains proper queueing semantics within the same output_base.
3387 That is, simultaneous invocations will be processed in order, without overlap.
3388 If a batch mode bazel is run on a client with a running server, it first
3389 kills the server before processing the command.
3390</p>
3391
3392<p>
3393 Bazel will run slower in batch mode, compared to client/server mode.
3394 Among other things, the build file cache is memory-resident, so it is not
3395 preserved between sequential batch invocations.
3396 Therefore, using batch mode often makes more sense in cases where performance
3397 is less critical, such as continuous builds.
3398</p>
3399
3400<h4 id='flag--max_idle_secs'><code class='flag'>--max_idle_secs <var>n</var></code></h4>
3401<p>
3402 This option specifies how long, in seconds, the Bazel server process
3403 should wait after the last client request, before it exits. The
3404 default value is 10800 (3 hours).
3405</p>
3406<p>
3407 This option may be used by scripts that invoke Bazel to ensure that
3408 they do not leave Bazel server processes on a user's machine when they
3409 would not be running otherwise.
3410 For example, a presubmit script might wish to
3411 invoke <code>bazel query</code> to ensure that a user's pending
3412 change does not introduce unwanted dependencies. However, if the
3413 user has not done a recent build in that workspace, it would be
3414 undesirable for the presubmit script to start a Bazel server just
3415 for it to remain idle for the rest of the day.
3416 By specifying a small value of <code class='flag'>--max_idle_secs</code> in the
3417 query request, the script can ensure that <i>if</i> it caused a new
3418 server to start, that server will exit promptly, but if instead
3419 there was already a server running, that server will continue to run
3420 until it has been idle for the usual time. Of course, the existing
3421 server's idle timer will be reset.
3422</p>
3423
3424<h4 id='flag--block_for_lock'><code class='flag'>--[no]block_for_lock</code></h4>
3425<p>
3426 If enabled, Bazel will wait for other Bazel commands holding the
3427 server lock to complete before progressing. If disabled, Bazel will
3428 exit in error if it cannot immediately acquire the lock and
3429 proceed.
3430
3431 Developers might use this in presubmit checks to avoid long waits caused
3432 by another Bazel command in the same client.
3433</p>
3434
3435<h4 id='flag--io_nice_level'><code class='flag'>--io_nice_level <var>n</var></code></h4>
3436<p>
3437 Sets a level from 0-7 for best-effort IO scheduling. 0 is highest priority,
3438 7 is lowest. The anticipatory scheduler may only honor up to priority 4.
3439 Negative values are ignored.
3440</p>
3441
3442<h4 id='flag--batch_cpu_scheduling'><code class='flag'>--batch_cpu_scheduling</code></h4>
3443<p>
3444 Use <code>batch</code> CPU scheduling for Bazel. This policy is useful for
3445 workloads that are non-interactive, but do not want to lower their nice value.
3446 See 'man 2 sched_setscheduler'. This policy may provide for better system
3447 interactivity at the expense of Bazel throughput.
3448</p>
3449
3450<h3 id='misc_options'>Miscellaneous options</h3>
3451
3452<h4 id='flag--announce_rc'><code class='flag'>--[no]announce_rc</code></h4>
3453<p>
3454 Controls whether Bazel announces command options read from the bazelrc file when
3455 starting up. (Startup options are unconditionally announced.)
3456</p>
3457
3458<h4 id='flag--color'><code class='flag'>--color (yes|no|auto)</code></h4>
3459<p>
3460 This option determines whether Bazel will use colors to highlight
3461 its output on the screen.
3462</p>
3463<p>
3464 If this option is set to <code>yes</code>, color output is enabled.
3465 If this option is set to <code>auto</code>, Bazel will use color output only if
3466 the output is being sent to a terminal and the TERM environment variable
3467 is set to a value other than <code>dumb</code>, <code>emacs</code>, or <code>xterm-mono</code>.
3468 If this option is set to <code>no</code>, color output is disabled,
3469 regardless of whether the output is going to a terminal and regardless
3470 of the setting of the TERM environment variable.
3471</p>
3472
3473<h4 id='flag--config'><code class='flag'>--config <var>name</var></code></h4>
3474<p>
3475 Selects additional config section from the rc files; for the current
3476 <code>command</code>, it also pulls in the options from
Googlerc848bf32017-02-14 12:15:42 +00003477 <code>command:name</code> if such a section exists. Can be specified multiple
3478 times to add flags from several config sections. Expansions can refer to other
3479 definitions (i.e. expansions can be chained).
David Chen8fe82a32016-08-24 10:55:41 +00003480</p>
3481
3482<h4 id='flag--curses'><code class='flag'>--curses (yes|no|auto)</code></h4>
3483<p>
3484 This option determines whether Bazel will use cursor controls
3485 in its screen output. This results in less scrolling data, and a more
3486 compact, easy-to-read stream of output from Bazel. This works well with
3487 <code class='flag'>--color</code>.
3488</p>
3489<p>
3490 If this option is set to <code>yes</code>, use of cursor controls is enabled.
3491 If this option is set to <code>no</code>, use of cursor controls is disabled.
3492 If this option is set to <code>auto</code>, use of cursor controls will be
3493 enabled under the same conditions as for <code class='flag'>--color=auto</code>.
3494</p>
3495
3496<h4 id='flag--show_timestamps'><code class='flag'>--[no]show_timestamps</code></h4>
3497<p>
3498 If specified, a timestamp is added to each message generated by
3499 Bazel specifying the time at which the message was displayed.
3500</p>
3501
3502<h2 id='scripting'>Calling Bazel from scripts</h2>
3503
3504<p>
3505 Bazel can be called from scripts in order to perform a build, run
3506 tests or query the dependency graph. Bazel has been designed to
3507 enable effective scripting, but this section lists some details to
3508 bear in mind to make your scripts more robust.
3509</p>
3510
3511<h3>Choosing the output base</h3>
3512
3513<p>
3514 The <code class='flag'>--output_base</code> option controls where the Bazel process should
3515 write the outputs of a build to, as well as various working files used
3516 internally by Bazel, one of which is a lock that guards against
3517 concurrent mutation of the output base by multiple Bazel processes.
3518</p>
3519<p>
3520 Choosing the correct output base directory for your script depends
3521 on several factors. If you need to put the build outputs in a
3522 specific location, this will dictate the output base you need to
3523 use. If you are making a "read only" call to Bazel
3524 (e.g. <code>bazel query</code>), the locking factors will be more important.
3525 In particular, if you need to run multiple instances of your script
3526 concurrently, you will need to give each one a different (or random) output
3527 base.
3528</p>
3529<p>
3530 If you use the default output base value, you will be contending for
3531 the same lock used by the user's interactive Bazel commands. If the
3532 user issues long-running commands such as builds, your script will
3533 have to wait for those commands to complete before it can continue.
3534</p>
3535
3536<h3>Server or no server?</h3>
3537
3538<p>
3539 By default, Bazel uses a long-running <a
3540 href='#client/server'>server process</a> as an optimization; this
3541 behavior can be disabled using the <a
3542 href='#flag--batch'><code class='flag'>--batch</code></a> option. There's no hard and
3543 fast rule about whether or not your script should use a server, but
3544 in general, the trade-off is between performance and reliability.
3545 The server mode makes a sequence of builds, especially incremental
3546 builds, faster, but its behavior is more complex and prone to
3547 failure. We recommend in most cases that you use batch mode unless
3548 the performance advantage is critical.
3549</p>
3550<p>
3551 If you do use the server, don't forget to call <code>shutdown</code>
3552 when you're finished with it, or, specify
3553 <code class='flag'>--max_idle_secs=5</code> so that idle servers shut themselves
3554 down promptly.
3555</p>
3556
3557<h3>What exit code will I get?</h3>
3558
3559<p>
3560 Bazel attempts to differentiate failures due to the source code under
3561consideration from external errors that prevent Bazel from executing properly.
3562Bazel execution can result in following exit codes:
3563</p>
3564
3565<b>Exit Codes common to all commands:</b>
3566<ul>
3567 <li><code>0</code> - Success</li>
3568 <li><code>2</code> - Command Line Problem, Bad or Illegal flags or command
3569 combination, or Bad Environment Variables. Your command line must be
3570 modified.</li>
3571 <li><code>8</code> - Build Interrupted but we terminated with an orderly shutdown.</li>
3572 <li><code>32</code> - External Environment Failure not on this machine.</li>
3573 <li><code>33</code> - OOM failure. You need to modify your command line.</li>
3574
3575 <li><code>34</code> - Reserved for Google-internal use.</li>
3576 <li><code>35</code> - Reserved for Google-internal use.</li>
3577 <li><code>36</code> - Local Environmental Issue, suspected permanent.</li>
3578 <li><code>37</code> - Unhandled Exception / Internal Bazel Error.</li>
3579 <li><code>38</code> - Reserved for Google-internal use.</li>
3580 <li><code>40-44</code> - Reserved for errors in Bazel's command line launcher,
3581 <code>bazel.cc</code> that are not command line
3582 related. Typically these are related to bazel server
3583 being unable to launch itself.</li>
3584</ul>
3585
3586<b>Return codes for commands <code>bazel build</code>, <code>bazel test</code>.</b>
3587<ul>
3588 <li><code>1</code> - Build failed.</li>
3589 <li><code>3</code> - Build OK, but some tests failed or timed out.</li>
3590 <li><code>4</code> - Build successful but no tests were found even though
3591 testing was requested.</li>
3592</ul>
3593
3594<b>For <code>bazel run</code>:</b>
3595<ul>
3596 <li><code>1</code> - Build failed.</li>
3597 <li><code>6</code> - Run command failure. The executed subprocess returned a
3598 non-zero exit code. The actual subprocess exit code is
3599 given in stderr.</li>
3600</ul>
3601
3602
3603<b>For
3604
3605 <code>bazel query</code>:</b>
3606<ul>
3607 <li><code>3</code> - Partial success, but the query encountered 1 or more
3608 errors in the input BUILD file set and therefore the
3609 results of the operation are not 100% reliable.
3610 This is likely due to a <code class='flag'>--keep_going</code> option
3611 on the command line.</li>
3612 <li><code>7</code> - Command failure.</li>
3613</ul>
3614
3615<p>
3616 Future Bazel versions may add additional exit codes, replacing generic failure
3617 exit code <code>1</code> with a different non-zero value with a particular
3618 meaning. However, all non-zero exit values will always constitute an error.
3619</p>
3620
3621<h3>Reading the .bazelrc file</h3>
3622
3623<p>
3624 By default, Bazel will read the <a
3625 href='#bazelrc'><code>.bazelrc</code> file</a> from the base workspace
3626 directory or the user's home directory. Whether or not this is
3627 desirable is a choice for your script; if your script needs to be
3628 perfectly hermetic (e.g. when doing release builds), you should
3629 disable reading the .bazelrc file by using the option
3630 <code class='flag'>--bazelrc=/dev/null</code>. If you want to perform a build
3631 using the user's preferred settings, the default behavior is better.
3632</p>
3633
3634<h3>Command log</h3>
3635
3636<p>
3637 The Bazel output is also available in a command log file which you can
3638 find with the following command:
3639</p>
3640
3641<pre>
3642% bazel info command_log
3643</pre>
3644
3645<p>
3646 The command log file contains the interleaved stdout and stderr streams
3647 of the most recent Bazel command. Note that running <code>bazel info</code>
3648 will overwrite the contents of this file, since it then becomes the most
3649 recent Bazel command. However, the location of the command log file will
3650 not change unless you change the setting of the <code class='flag'>--output_base</code>
3651 or <code class='flag'>--output_user_root</code> options.
3652</p>
3653
3654<h3>Parsing output</h3>
3655
3656<p>
3657 The Bazel output is quite easy to parse for many purposes. Two
3658 options that may be helpful for your script are
3659 <code class='flag'>--noshow_progress</code> which suppresses progress messages,
3660 and <code class='flag'>--show_result <var>n</var></code>, which controls whether
3661 or not "build up-to-date" messages are printed; these messages may
3662 be parsed to discover which targets were successfully built, and the
3663 location of the output files they created. Be sure to specify a
3664 very large value of <i>n</i> if you rely on these messages.
3665</p>
3666
3667<h2 id='profiling'>Troubleshooting performance by profiling</h2>
3668
3669<p>
3670 The first step in analyzing the performance of your build is to profile your build with the
3671 <a href='#flag--profile'><code class='flag'>--profile</code></a> option.
3672</p>
3673
3674<p>
3675 The file generated by the <a href='#flag--profile'><code class='flag'>--profile</code></a>
3676 command is a binary file. Once you have generated this binary profile, you can analyze it using
3677 Bazel's <a href='#analyze-profile'><code>analyze-profile</code></a> command. By default, it will
3678 print out summary analysis information for each of the specified profile datafiles. This includes
3679 cumulative statistics for different task types for each build phase and an analysis of the
3680 critical execution path.
3681</p>
3682
3683<p>
3684 The first section of the default output describes an overview of the time spent on the different
3685 build phases:
3686</p>
3687<pre>
3688=== PHASE SUMMARY INFORMATION ===
3689
3690Total launch phase time 6.00 ms 0.01%
3691Total init phase time 864 ms 1.11%
3692Total loading phase time 21.841 s 28.05%
3693Total analysis phase time 5.444 s 6.99%
3694Total preparation phase time 155 ms 0.20%
3695Total execution phase time 49.473 s 63.54%
3696Total finish phase time 83.9 ms 0.11%
3697Total run time 77.866 s 100.00%
3698</pre>
3699
3700<p>
3701 The following sections show the execution time of different tasks happening during a particular
3702 phase:
3703</p>
3704<pre>
3705=== INIT PHASE INFORMATION ===
3706
3707Total init phase time 864 ms
3708
3709Total time (across all threads) spent on:
3710 Type Total Count Average
3711 VFS_STAT 2.72% 1 23.5 ms
3712 VFS_READLINK 32.19% 1 278 ms
3713
3714=== LOADING PHASE INFORMATION ===
3715
3716Total loading phase time 21.841 s
3717
3718Total time (across all threads) spent on:
3719 Type Total Count Average
3720 SPAWN 3.26% 154 475 ms
3721 VFS_STAT 10.81% 65416 3.71 ms
3722[...]
3723SKYLARK_BUILTIN_FN 13.12% 45138 6.52 ms
3724
3725=== ANALYSIS PHASE INFORMATION ===
3726
3727Total analysis phase time 5.444 s
3728
3729Total time (across all threads) spent on:
3730 Type Total Count Average
3731 SKYFRAME_EVAL 9.35% 1 4.782 s
3732 SKYFUNCTION 89.36% 43332 1.06 ms
3733
3734=== EXECUTION PHASE INFORMATION ===
3735
3736Total preparation time 155 ms
3737Total execution phase time 49.473 s
3738Total time finalizing build 83.9 ms
3739
3740Action dependency map creation 0.00 ms
3741Actual execution time 49.473 s
3742
3743Total time (across all threads) spent on:
3744 Type Total Count Average
3745 ACTION 2.25% 12229 10.2 ms
3746[...]
3747 SKYFUNCTION 1.87% 236131 0.44 ms
3748</pre>
3749
3750<p>
3751 The last section shows the critical path:
3752</p>
3753<pre>
3754Critical path (32.078 s):
3755 Id Time Percentage Description
37561109746 5.171 s 16.12% Building [...]
37571109745 164 ms 0.51% Extracting interface [...]
37581109744 4.615 s 14.39% Building [...]
3759[...]
37601109639 2.202 s 6.86% Executing genrule [...]
37611109637 2.00 ms 0.01% Symlinking [...]
37621109636 163 ms 0.51% Executing genrule [...]
3763 4.00 ms 0.01% [3 middleman actions]
3764</pre>
3765
3766<p>
3767 You can use the following options to display more detailed information:
3768</p>
3769
3770<ul>
3771 <li id='dump-text-format'><a href='#flag--dump'><code>--dump=text</code></a>
3772 <p>
3773 This option prints all recorded tasks in the order they occurred. Nested tasks are indented
3774 relative to the parent. For each task, output includes the following information:
3775 </p>
3776<pre>
3777[task type] [task description]
3778Thread: [thread id] Id: [task id] Parent: [parent task id or 0 for top-level tasks]
3779Start time: [time elapsed from the profiling session start] Duration: [task duration]
3780[aggregated statistic for nested tasks, including count and total duration for each nested task]
3781</pre>
3782 </li>
3783 <li id='dump-raw-format'><a href='#flag--dump'><code>--dump=raw</code></a>
3784 <p>
3785 This option is most useful for automated analysis with scripts. It outputs each task record on
3786 a single line using '|' delimiter between fields. Fields are printed in the following order:
3787 </p>
3788 <ol>
3789 <li>thread id - integer positive number, identifies owner thread for the task</li>
3790 <li>task id - integer positive number, identifies specific task</li>
3791 <li>parent task id for nested tasks or 0 for root tasks</li>
3792 <li>task start time in ns, relative to the start of the profiling session</li>
3793 <li>task duration in ns. Please note that this will include duration of all subtasks.</li>
3794 <li>aggregated statistic for immediate subtasks per type. This will include type name (lower
3795 case), number of subtasks for that type and their cumulative duration. Types are
3796 space-delimited and information for single type is comma-delimited.</li>
3797 <li>task type (upper case)</li>
3798 <li>task description</li>
3799 </ol>
3800
3801 Example:
3802<pre>
38031|1|0|0|0||PHASE|Launch Bazel
38041|2|0|6000000|0||PHASE|Initialize command
38051|3|0|168963053|278111411||VFS_READLINK|/[...]
38061|4|0|571055781|23495512||VFS_STAT|/[...]
38071|5|0|869955040|0||PHASE|Load packages
3808[...]
3809</pre>
3810 </li>
3811 <li id='dump-html-format'><a href='#flag--html'><code>--html</code></a>
3812 <p>
3813 This option writes a file called <code>&lt;profile-file&gt;.html</code> in the directory of the
3814 profile file. Open it in your browser to see the visualization of the actions in your build.
3815 Note that the file can be quite large and may push the capabilities of your browser &ndash;
3816 please wait for the file to load.
3817 </p>
3818 <p>
3819 In most cases, the HTML output from <a href='#flag--html'><code>--html</code></a> is easier to
3820 read than the <a href='#flag--dump'><code>--dump</code></a> output.
3821 It includes a Gantt chart that displays time on the horizontal axis and
3822 threads of execution along the vertical axis. If you click on the Statistics link in the top
3823 right corner of the page, you will jump to a section that lists summary analysis information
3824 from your build.
3825 </p>
3826 <ul>
3827 <li><a href='#flag--html_details'><code>--html_details</code></a>
3828 <p>
3829 Additionally passing this option will render a more detailed execution chart and additional
3830 tables on the performance of built-in and user-defined Skylark functions. Beware that this
3831 increases the file size and the load on the browser considerably.
3832 </p>
3833 </li>
3834 </ul></li>
3835</ul>
3836
3837<p>If Bazel appears to be hung, you can hit <kbd><kbd>ctrl</kbd> + <kbd>\</kbd></kbd> or send
3838 Bazel a <code>SIGQUIT</code> signal (<code>kill -3 $(bazel info server_pid)</code>) to get a
3839 thread dump in the file <code>$(bazel info output_base)/server/jvm.out</code>.
3840</p>
3841
3842<p>
3843 Since you may not be able to run <code>bazel info</code> if bazel is hung, the
3844 <code>output_base</code> directory is usually the parent of the <code>bazel-&lt;workspace&gt;</code>
3845 symlink in your workspace directory.
3846</p>