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