blob: 737bbc8f6949f9a65d100c77992192d10d0e41b6 [file] [log] [blame]
David Chen8fe82a32016-08-24 10:55:41 +00001---
2layout: documentation
Greg Estrenc0816752020-02-20 13:04:29 -08003title: User manual
David Chen8fe82a32016-08-24 10:55:41 +00004---
Googler9e7ae8d2021-01-08 19:01:23 -08005<h1>Commands and Options</h1>
David Chen8fe82a32016-08-24 10:55:41 +00006
Googler630941e2020-12-23 17:13:31 -08007This page covers the options that are available with various Bazel commands,
8such a `bazel build`, `bazel run`, and `bazel test`. This page is a companion
9to the <a href="https://docs.bazel.build/versions/master/guide.html">Bazel user guide</a>,
10which lists Bazel's commands.
Greg Estrenc0816752020-02-20 13:04:29 -080011<h2 id='target-patterns'>Target syntax</h2>
laurentlbadf36072018-11-05 16:20:20 -080012
13Some commands, like <code>build</code> or <code>test</code>, can operate
14on a list of targets. They use a syntax more flexible than labels, which is
Googlerbb6ecd72020-05-28 12:52:28 -070015documented in the "<a href="guide.html#specifying-targets-to-build">Specifying
laurentlbadf36072018-11-05 16:20:20 -080016targets to build</a>" section of the User's Guide.
17
David Chen8fe82a32016-08-24 10:55:41 +000018<h2>Options</h2>
19
20<p>
21 The following sections describe the options available during a
Googler0e692fb2021-01-22 18:38:24 +010022 build. When <code class='flag'>--long</code> is used on a help command, the on-line
David Chen8fe82a32016-08-24 10:55:41 +000023 help messages provide summary information about the meaning, type and
24 default value for each option.
25</p>
26
27<p>
28 Most options can only be specified once. When specified multiple times, the
29 last instance wins. Options that can be specified multiple times are
30 identified in the on-line help with the text 'may be used multiple times'.
31</p>
32
brandjoncf0390b2020-02-20 10:37:40 -080033<h3>Package location</h3>
David Chen8fe82a32016-08-24 10:55:41 +000034
David Chen8fe82a32016-08-24 10:55:41 +000035<h4 id='flag--package_path'><code class='flag'>--package_path</code></h4>
36<p>
37 This option specifies the set of directories that are searched to
38 find the BUILD file for a given package.
David Chen8fe82a32016-08-24 10:55:41 +000039</p>
40
David Chen8fe82a32016-08-24 10:55:41 +000041<p>
Googler0e692fb2021-01-22 18:38:24 +010042 Bazel finds its packages by searching the package path. This is a colon
laurentlb15918182018-10-17 11:44:25 -070043 separated ordered list of bazel directories, each being the root of a
44 partial source tree.
David Chen8fe82a32016-08-24 10:55:41 +000045</p>
46
laurentlb15918182018-10-17 11:44:25 -070047<p>
48 <i>To specify a custom package path</i> using the
49 <code class='flag'>--package_path</code> option:
50</p>
51<pre>
52 % bazel build --package_path %workspace%:/some/other/root
53</pre>
54<p>
55Package path elements may be specified in three formats:
56</p>
57<ol>
58 <li>
59 If the first character is <code>/</code>, the path is absolute.
60 </li>
61 <li>
62 If the path starts with <code>%workspace%</code>, the path is taken relative
63 to the nearest enclosing bazel directory.<br>
64 For instance, if your working directory
65 is <code>/home/bob/clients/bob_client/bazel/foo</code>, then the
66 string <code>%workspace%</code> in the package-path is expanded
67 to <code>/home/bob/clients/bob_client/bazel</code>.
68 </li>
69 <li>
70 Anything else is taken relative to the working directory.<br> This is usually not what you mean to do,
71 and may behave unexpectedly if you use Bazel from directories below the bazel workspace.
72 For instance, if you use the package-path element <code>.</code>,
73 and then cd into the directory
74 <code>/home/bob/clients/bob_client/bazel/foo</code>, packages
75 will be resolved from the
76 <code>/home/bob/clients/bob_client/bazel/foo</code> directory.
77 </li>
78</ol>
79<p>
80 If you use a non-default package path, we recommend that you specify
Vladimir Chebotarev66885ff2018-12-04 14:08:16 -080081 it in your <a href='guide.html#bazelrc'>Bazel configuration file</a> for
laurentlb15918182018-10-17 11:44:25 -070082 convenience.
83</p>
84<p>
85 <i>Bazel doesn't require any packages to be in the
86 current directory</i>, so you can do a build from an empty bazel
87 workspace if all the necessary packages can be found somewhere else
88 on the package path.
89</p>
90<p>
91 <i>Example</i>: Building from an empty client
92</p>
93<pre>
94 % mkdir -p foo/bazel
95 % cd foo/bazel
96 % touch WORKSPACE
97 % bazel build --package_path /some/other/path //foo
98</pre>
brandjoncf0390b2020-02-20 10:37:40 -080099<h3 id='checking-options'>Error checking</h3>
David Chen8fe82a32016-08-24 10:55:41 +0000100<p>
101 These options control Bazel's error-checking and/or warnings.
102</p>
103
104<h4 id='flag--check_constraint'><code class='flag'>--check_constraint <var>constraint</var></code></h4>
105<p>
106 This option takes an argument that specifies which constraint
107 should be checked.
108</p>
109<p>
110 Bazel performs special checks on each rule that is annotated with the
111 given constraint.
112</p>
113<p>
114 The supported constraints and their checks are as follows:
115</p>
116<ul>
117
118 <li><code>public</code>: Verify that all java_libraries marked with
119 <code>constraints = ['public']</code> only depend on java_libraries
120 that are marked as <code>constraints = ['public']</code> too. If bazel
121 finds a dependency that does not conform to this rule, bazel will issue
122 an error.
123 </li>
124</ul>
125
126<h4 id='flag--check_visibility'><code class='flag'>--[no]check_visibility</code></h4>
127<p>
128 If this option is set to false, visibility checks are demoted to warnings.
129 The default value of this option is true, so that by default, visibility
130 checking is done.
131
132</p>
David Chen8fe82a32016-08-24 10:55:41 +0000133<h4 id='flag--output_filter'><code class='flag'>--output_filter <var>regex</var></code></h4>
134<p>
135 The <code class='flag'>--output_filter</code> option will only show build and compilation
136 warnings for targets that match the regular expression. If a target does not
137 match the given regular expression and its execution succeeds, its standard
ulfjack171314b2017-06-02 09:09:43 +0200138 output and standard error are thrown away.
139</p>
140<p>
141 Here are some typical values for this option:
David Chen8fe82a32016-08-24 10:55:41 +0000142</p>
143<table>
144 <tr>
David Chen8fe82a32016-08-24 10:55:41 +0000145 <td><code class='flag'>--output_filter='^//(first/project|second/project):'</code></td>
146 <td>Show the output for the specified packages.</td>
147 </tr>
148 <tr>
149 <td><code class='flag'>--output_filter='^//((?!(first/bad_project|second/bad_project):).)*$'</code></td>
150 <td>Don't show output for the specified packages.</td>
151 </tr>
152 <tr>
ulfjack171314b2017-06-02 09:09:43 +0200153 <td><code class='flag'>--output_filter=</code></td>
Googler4da2f162017-07-17 21:36:56 +0200154 <td>Show everything.
ulfjack171314b2017-06-02 09:09:43 +0200155 </td>
156 </tr>
157 <tr>
David Chen8fe82a32016-08-24 10:55:41 +0000158 <td><code class='flag'>--output_filter=DONT_MATCH_ANYTHING</code></td>
ulfjack171314b2017-06-02 09:09:43 +0200159 <td>Show nothing.
160 </td>
David Chen8fe82a32016-08-24 10:55:41 +0000161 </tr>
162</table>
163
brandjoncf0390b2020-02-20 10:37:40 -0800164<h3 id='flags-options'>Tool flags</h3>
David Chen8fe82a32016-08-24 10:55:41 +0000165<p>
166 These options control which options Bazel will pass to other tools.
167</p>
168
Googler9cfa4cb2018-06-04 22:19:11 -0700169<h4 id='flag--copt'><code class='flag'>--copt <var>cc-option</var></code></h4>
David Chen8fe82a32016-08-24 10:55:41 +0000170<p>
Googler9cfa4cb2018-06-04 22:19:11 -0700171 This option takes an argument which is to be passed to the compiler.
172 The argument will be passed to the compiler whenever it is invoked
David Chen8fe82a32016-08-24 10:55:41 +0000173 for preprocessing, compiling, and/or assembling C, C++, or
Googler0e692fb2021-01-22 18:38:24 +0100174 assembler code. It will not be passed when linking.
David Chen8fe82a32016-08-24 10:55:41 +0000175</p>
176<p>
177 This option can be used multiple times.
178 For example:
179</p>
180<pre>
181 % bazel build --copt="-g0" --copt="-fpic" //foo
182</pre>
183<p>
184 will compile the <code>foo</code> library without debug tables, generating
185 position-independent code.
186</p>
187<p>
188 Note that changing <code class='flag'>--copt</code> settings will force a recompilation
189 of all affected object files. Also note that copts values listed in specific
Googler9cfa4cb2018-06-04 22:19:11 -0700190 cc_library or cc_binary build rules will be placed on the compiler command line
David Chen8fe82a32016-08-24 10:55:41 +0000191 <em>after</em> these options.
192</p>
193<p>
194 Warning: C++-specific options (such as <code>-fno-implicit-templates</code>)
195 should be specified in <code class='flag'>--cxxopt</code>, not in
Googler0e692fb2021-01-22 18:38:24 +0100196 <code class='flag'>--copt</code>. Likewise, C-specific options (such as -Wstrict-prototypes)
David Chen8fe82a32016-08-24 10:55:41 +0000197 should be specified in <code class='flag'>--conlyopt</code>, not in <code>copt</code>.
Googler9cfa4cb2018-06-04 22:19:11 -0700198 Similarly, compiler options that only have an
David Chen8fe82a32016-08-24 10:55:41 +0000199 effect at link time (such as <code>-l</code>) should be specified in
200 <code class='flag'>--linkopt</code>, not in <code class='flag'>--copt</code>.
201</p>
202
Googler9cfa4cb2018-06-04 22:19:11 -0700203<h4 id='flag--host_copt'><code class='flag'>--host_copt <var>cc-option</var></code></h4>
David Chen8fe82a32016-08-24 10:55:41 +0000204<p>
Googler9cfa4cb2018-06-04 22:19:11 -0700205 This option takes an argument which is to be passed to the compiler for source files
David Chen8fe82a32016-08-24 10:55:41 +0000206 that are compiled in the host configuration. This is analogous to
207 the <a href='#flag--copt'><code class='flag'>--copt</code></a> option, but applies only to the
208 host configuration.
209</p>
210
Googler9cfa4cb2018-06-04 22:19:11 -0700211<h4 id='flag--host_cxxopt'><code class='flag'>--host_cxxopt <var>cc-option</var></code></h4>
Marcel Hlopko4c1b1fd2017-02-24 12:54:17 +0000212<p>
Googler9cfa4cb2018-06-04 22:19:11 -0700213 This option takes an argument which is to be passed to the compiler for C++ source files
Marcel Hlopko4c1b1fd2017-02-24 12:54:17 +0000214 that are compiled in the host configuration. This is analogous to
215 the <a href='#flag--cxxopt'><code class='flag'>--cxxopt</code></a> option, but applies only to the
216 host configuration.
217</p>
218
Googler9cfa4cb2018-06-04 22:19:11 -0700219<h4 id='flag--conlyopt'><code class='flag'>--conlyopt <var>cc-option</var></code></h4>
David Chen8fe82a32016-08-24 10:55:41 +0000220<p>
Googler9cfa4cb2018-06-04 22:19:11 -0700221 This option takes an argument which is to be passed to the compiler when compiling C source files.
David Chen8fe82a32016-08-24 10:55:41 +0000222</p>
223<p>
224 This is similar to <code class='flag'>--copt</code>, but only applies to C compilation,
Googler0e692fb2021-01-22 18:38:24 +0100225 not to C++ compilation or linking. So you can pass C-specific options
David Chen8fe82a32016-08-24 10:55:41 +0000226 (such as <code>-Wno-pointer-sign</code>) using <code class='flag'>--conlyopt</code>.
227</p>
228<p>
229 Note that copts parameters listed in specific cc_library or cc_binary build rules
Googler9cfa4cb2018-06-04 22:19:11 -0700230 will be placed on the compiler command line <em>after</em> these options.
David Chen8fe82a32016-08-24 10:55:41 +0000231</p>
232
Googler9cfa4cb2018-06-04 22:19:11 -0700233<h4 id='flag--cxxopt'><code class='flag'>--cxxopt <var>cc-option</var></code></h4>
David Chen8fe82a32016-08-24 10:55:41 +0000234<p>
Googler9cfa4cb2018-06-04 22:19:11 -0700235 This option takes an argument which is to be passed to the compiler when compiling C++ source
236 files.
David Chen8fe82a32016-08-24 10:55:41 +0000237</p>
238<p>
239 This is similar to <code class='flag'>--copt</code>, but only applies to C++ compilation,
Googler0e692fb2021-01-22 18:38:24 +0100240 not to C compilation or linking. So you can pass C++-specific options
David Chen8fe82a32016-08-24 10:55:41 +0000241 (such as <code>-fpermissive</code> or <code>-fno-implicit-templates</code>) using <code class='flag'>--cxxopt</code>.
242 For example:
243</p>
244<pre>
245 % bazel build --cxxopt="-fpermissive" --cxxopt="-Wno-error" //foo/cruddy_code
246</pre>
247<p>
248 Note that copts parameters listed in specific cc_library or cc_binary build rules
Googler9cfa4cb2018-06-04 22:19:11 -0700249 will be placed on the compiler command line <em>after</em> these options.
David Chen8fe82a32016-08-24 10:55:41 +0000250</p>
251
252<h4 id='flag--linkopt'><code class='flag'>--linkopt <var>linker-option</var></code></h4>
253<p>
Googler9cfa4cb2018-06-04 22:19:11 -0700254 This option takes an argument which is to be passed to the compiler when linking.
David Chen8fe82a32016-08-24 10:55:41 +0000255</p>
256<p>
257 This is similar to <code class='flag'>--copt</code>, but only applies to linking,
Googler0e692fb2021-01-22 18:38:24 +0100258 not to compilation. So you can pass compiler options that only make sense
David Chen8fe82a32016-08-24 10:55:41 +0000259 at link time (such as <code>-lssp</code> or <code>-Wl,--wrap,abort</code>)
Googler0e692fb2021-01-22 18:38:24 +0100260 using <code class='flag'>--linkopt</code>. For example:
David Chen8fe82a32016-08-24 10:55:41 +0000261</p>
262<pre>
263 % bazel build --copt="-fmudflap" --linkopt="-lmudflap" //foo/buggy_code
264</pre>
265<p>
266 Build rules can also specify link options in their attributes. This option's
267 settings always take precedence. Also see
268 <a href="be/c-cpp.html#cc_library.linkopts">cc_library.linkopts</a>.
269</p>
270
271<h4 id='flag--strip'><code class='flag'>--strip (always|never|sometimes)</code></h4>
272<p>
273 This option determines whether Bazel will strip debugging information from
274 all binaries and shared libraries, by invoking the linker with the <code>-Wl,--strip-debug</code> option.
275 <code class='flag'>--strip=always</code> means always strip debugging information.
276 <code class='flag'>--strip=never</code> means never strip debugging information.
Googler77e5b922019-10-23 12:09:20 -0700277 The default value of <code class='flag'>--strip=sometimes</code> means strip if the <code class='flag'>--compilation_mode</code>
David Chen8fe82a32016-08-24 10:55:41 +0000278 is <code>fastbuild</code>.
279</p>
280<pre>
281 % bazel build --strip=always //foo:bar
282</pre>
283<p>
284 will compile the target while stripping debugging information from all generated
285 binaries.
286</p>
287<p>
288 Note that if you want debugging information, it's not enough to disable stripping; you also need to make
289 sure that the debugging information was generated by the compiler, which you can do by using either
290 <code>-c dbg</code> or <code class='flag'>--copt -g</code>.
291</p>
292<p>
293 Note also that Bazel's <code class='flag'>--strip</code> option corresponds with ld's <code>--strip-debug</code> option:
Googler0e692fb2021-01-22 18:38:24 +0100294 it only strips debugging information. If for some reason you want to strip <em>all</em> symbols,
David Chen8fe82a32016-08-24 10:55:41 +0000295 not just <em>debug</em> symbols, you would need to use ld's <code>--strip-all</code> option,
Googler87598722020-06-10 08:54:40 -0700296 which you can do by passing <code class='flag'>--linkopt=-Wl,--strip-all</code> to Bazel. Also be
297 aware that setting Bazel's <code class='flag'>--strip</code> flag will override
298 <code class='flag'>--linkopt=-Wl,--strip-all</code>, so you should only set one or the other.
David Chen8fe82a32016-08-24 10:55:41 +0000299</p>
300
301<h4 id='flag--stripopt'><code class='flag'>--stripopt <var>strip-option</var></code></h4>
302<p>
303 An additional option to pass to the <code>strip</code> command when generating
304 a <a href="be/c-cpp.html#cc_binary_implicit_outputs"><code>*.stripped</code>
305 binary</a>. The default is <code>-S -p</code>. This option can be used
306 multiple times.
307</p>
308<p>
309 Note that <code class='flag'>--stripopt</code> does not apply to the stripping of the main
310 binary with <code><a href='#flag--strip'>--strip</a>=(always|sometimes)</code>.
311</p>
312
313<h4 id='flag--fdo_instrument'><code class='flag'>--fdo_instrument <var>profile-output-dir</var></code></h4>
314<p>
315 The <code class='flag'>--fdo_instrument</code> option enables the generation of
316 FDO (feedback directed optimization) profile output when the
317 built C/C++ binary is executed. For GCC, the argument provided is used as a
318 directory prefix for a per-object file directory tree of .gcda files
319 containing profile information for each .o file.
320</p>
321<p>
322 Once the profile data tree has been generated, the profile tree
323 should be zipped up, and provided to the
324 <code class='flag'>--fdo_optimize=<var>profile-zip</var></code>
Googler66663572020-11-09 09:58:27 -0800325 Bazel option to enable the FDO-optimized compilation.
David Chen8fe82a32016-08-24 10:55:41 +0000326
327</p>
328<p>
329 For the LLVM compiler the argument is also the directory under which the raw LLVM profile
330 data file(s) is dumped, e.g.
331 <code class='flag'>--fdo_instrument=<var>/path/to/rawprof/dir/</var></code>.
332</p>
333<p>
334 The options <code class='flag'>--fdo_instrument</code> and <code class='flag'>--fdo_optimize</code>
335 cannot be used at the same time.
336</p>
337
Googler66663572020-11-09 09:58:27 -0800338
David Chen8fe82a32016-08-24 10:55:41 +0000339<h4 id='flag--fdo_optimize'><code class='flag'>--fdo_optimize <var>profile-zip</var></code></h4>
340<p>
341 The <code class='flag'>--fdo_optimize</code> option enables the use of the
342 per-object file profile information to perform FDO (feedback
343 directed optimization) optimizations when compiling. For GCC, the argument
344 provided is the zip file containing the previously-generated file tree
345 of .gcda files containing profile information for each .o file.
346</p>
347<p>
348 Alternatively, the argument provided can point to an auto profile
349 identified by the extension .afdo.
350
351</p>
352<p>
353 Note that this option also accepts labels that resolve to source files. You
354 may need to add an <code>exports_files</code> directive to the corresponding package to
355 make the file visible to Bazel.
356</p>
357<p>
358 For the LLVM compiler the argument provided should point to the indexed LLVM
359 profile output file prepared by the llvm-profdata tool, and should have a .profdata
360 extension.
361</p>
362<p>
363 The options <code class='flag'>--fdo_instrument</code> and <code class='flag'>
364 --fdo_optimize</code> cannot be used at the same time.
365</p>
366
David Chen8fe82a32016-08-24 10:55:41 +0000367
368<h4 id='flag--output_symbol_counts'><code class='flag'>--[no]output_symbol_counts</code></h4>
369<p>
Googlerd7273272017-05-02 18:56:29 +0200370 If enabled, each gold-invoked link of a C++ executable binary will output
David Chen8fe82a32016-08-24 10:55:41 +0000371 a <i>symbol counts</i> file (via the <code>--print-symbol-counts</code> gold
Googlerd7273272017-05-02 18:56:29 +0200372 option). For each linker input, the file logs the number of symbols that were
373 defined and the number of symbols that were used in the binary.
374 This information can be used to track unnecessary link dependencies.
375 The symbol counts file is written to the binary's output path with the name
David Chen8fe82a32016-08-24 10:55:41 +0000376 <code>[targetname].sc</code>.
377</p>
378<p>
Ivo List2fb9f3c2020-12-17 10:19:09 -0800379 This option is disabled by default.
David Chen8fe82a32016-08-24 10:55:41 +0000380</p>
381
Ivo List2fb9f3c2020-12-17 10:19:09 -0800382<h4 id='flag--java_language_version'><code class='flag'>--java_language_version=<var>version</var></code></h4>
383<p>
384 This option specifies the version of Java sources. For example:
385</p>
386<pre>
387 % bazel build --java_language_version=8 java/com/example/common/foo:all
388</pre>
389<p>
390 compiles and allows only constructs compatible with Java 8 specification.
391 Default value is 11. -->
392 Possible values are: 8, 9, 10, --> 11 <!--begin-block:external , 14, and 15 and may be extended by registering custom Java
393 toolchains using <code>default_java_toolchain</code> macro-->.
394</p>
395
396<h4 id='flag--tool_java_language_version'><code class='flag'>--tool_java_language_version=<var>version</var></code>
397</h4>
398<p>
399 The Java language version used to build tools that are executed during a build.
400 Default value is 11.
401</p>
402
403<h4 id='flag--java_runtime_version'><code class='flag'>--java_runtime_version=<var>version</var></code></h4>
404<p>
405 This option specifies the version of JVM to use to execute the code and run the tests. For
406 example:
407</p>
408<pre>
409 % bazel run --java_runtime_version=remotejdk_11 java/com/example/common/foo:java_application
410</pre>
411<p>
412 downloads JDK 11 from a remote repository and run the Java application using it.
413</p>
414<p>
415 Default value is <code>localjdk</code>.
416 Possible values are: <code>localjdk</code>, <code>localjdk_<var>version</var></code>,
417 <code>remotejdk_11</code>,
418 <code>remotejdk_14</code>, and <code>remote_jdk15</code>.
419 You can extend the values by registering custom JVM using either
420 <code>local_java_repository</code> or
421 <code>remote_java_repostory</code> repository rules.
422</p>
423
424<h4 id='flag--tool_java_runtime_version'><code class='flag'>--tool_java_runtime_version=<var>version</var></code></h4>
425<p>
426 The version of JVM used to execute tools that are needed during a build.
427 Default value is <code>remotejdk_11</code>.
428</p>
429-->
430
David Chen8fe82a32016-08-24 10:55:41 +0000431<h4 id='flag--jvmopt'><code class='flag'>--jvmopt <var>jvm-option</var></code></h4>
432<p>
433 This option allows option arguments to be passed to the Java VM. It can be used
434 with one big argument, or multiple times with individual arguments. For example:
435</p>
436<pre>
437 % bazel build --jvmopt="-server -Xms256m" java/com/example/common/foo:all
438</pre>
439<p>
440 will use the server VM for launching all Java binaries and set the
441 startup heap size for the VM to 256 MB.
442</p>
443
444<h4 id='flag--javacopt'><code class='flag'>--javacopt <var>javac-option</var></code></h4>
445<p>
446 This option allows option arguments to be passed to javac. It can be used
447 with one big argument, or multiple times with individual arguments. For example:
448</p>
449<pre>
450 % bazel build --javacopt="-g:source,lines" //myprojects:prog
451</pre>
452<p>
453 will rebuild a java_binary with the javac default debug info
454 (instead of the bazel default).
455</p>
456<p>
457 The option is passed to javac after the Bazel built-in default options for
458 javac and before the per-rule options. The last specification of
459 any option to javac wins. The default options for javac are:
460</p>
461
462<pre>
463 -source 8 -target 8 -encoding UTF-8
464</pre>
465<p>
466 Note that changing <code class='flag'>--javacopt</code> settings will force a recompilation
467 of all affected classes. Also note that javacopts parameters listed in
468 specific java_library or java_binary build rules will be placed on the javac
469 command line <em>after</em> these options.
470</p>
471
472<h5 id='-extra_checks'><code>-extra_checks[:(off|on)]</code></h5>
473
474<p>
475 This javac option enables extra correctness checks. Any problems found will
476 be presented as errors.
477 Either <code>-extra_checks</code> or <code>-extra_checks:on</code> may be used
478 to force the checks to be turned on. <code>-extra_checks:off</code> completely
479 disables the analysis.
480 When this option is not specified, the default behavior is used.
481</p>
482
483<h4 id='flag--strict_java_deps'><code class='flag'>--strict_java_deps
484 (default|strict|off|warn|error)</code></h4>
485<p>
486 This option controls whether javac checks for missing direct dependencies.
487 Java targets must explicitly declare all directly used targets as
488 dependencies. This flag instructs javac to determine the jars actually used
489 for type checking each java file, and warn/error if they are not the output
490 of a direct dependency of the current target.
491</p>
492
493<ul>
494 <li> <code>off</code> means checking is disabled.
495 </li>
496 <li> <code>warn</code> means javac will generate standard java warnings of
497 type <code>[strict]</code> for each missing direct dependency.
498 </li>
499 <li> <code>default</code>, <code>strict</code> and <code>error</code> all
500 mean javac will generate errors instead of warnings, causing the current
501 target to fail to build if any missing direct dependencies are found.
502 This is also the default behavior when the flag is unspecified.
503 </li>
504</ul>
505
brandjoncf0390b2020-02-20 10:37:40 -0800506<h3 id='semantics-options'>Build semantics</h3>
David Chen8fe82a32016-08-24 10:55:41 +0000507<p>
508 These options affect the build commands and/or the output file contents.
509</p>
510
511<h4 id='flag--compilation_mode'><code class='flag'>--compilation_mode (fastbuild|opt|dbg)</code> (-c)</h4>
512<p>
Googlerd96e8db2020-07-09 20:38:37 -0700513 The <code class="flag">--compilation_mode</code> option (often shortened to <code>-c</code>,
514 especially <code>-c opt</code>) takes an argument of <code>fastbuild</code>, <code>dbg</code>
David Chen8fe82a32016-08-24 10:55:41 +0000515 or <code>opt</code>, and affects various C/C++ code-generation
516 options, such as the level of optimization and the completeness of
Googler0e692fb2021-01-22 18:38:24 +0100517 debug tables. Bazel uses a different output directory for each
David Chen8fe82a32016-08-24 10:55:41 +0000518 different compilation mode, so you can switch between modes without
519 needing to do a full rebuild <i>every</i> time.
520</p>
521<ul>
522 <li> <code>fastbuild</code> means build as fast as possible:
523 generate minimal debugging information (<code>-gmlt
Googler0e692fb2021-01-22 18:38:24 +0100524 -Wl,-S</code>), and don't optimize. This is the
David Chen8fe82a32016-08-24 10:55:41 +0000525 default. Note: <code>-DNDEBUG</code> will <b>not</b> be set.
526 </li>
527 <li> <code>dbg</code> means build with debugging enabled (<code>-g</code>),
528 so that you can use gdb (or another debugger).
529 </li>
530 <li> <code>opt</code> means build with optimization enabled and
531 with <code>assert()</code> calls disabled (<code>-O2 -DNDEBUG</code>).
532 Debugging information will not be generated in <code>opt</code> mode
533 unless you also pass <code class='flag'>--copt -g</code>.
534 </li>
535</ul>
536
537<h4 id='flag--cpu'><code class='flag'>--cpu <var>cpu</var></code></h4>
538<p>
539This option specifies the target CPU architecture to be used for
540the compilation of binaries during the build.
541</p>
542<p>
543
544</p>
Googlerf12e8022018-09-11 07:51:31 -0700545<p>
546 Note that a particular combination of crosstool version, compiler version,
547 and target CPU is allowed only if it has been specified in the currently
548 used CROSSTOOL file.
549</p>
550
jingwen38b1fee2020-05-11 09:19:25 -0700551<h4 id='flag--action_env'>
552 <code class='flag'>--action_env=<var>VAR=VALUE</var></code>
553</h4>
554<p>
555 Specifies the set of environment variables available during the execution of all actions.
556 Variables can be either specified by name, in which case the value will be taken from the
557 invocation environment, or by the `name=value` pair which sets the value independent of the
558 invocation environment.
559
560 This `--action_env` flag can be specified multiple times. If a value is assigned to the same
561 variable across multiple `--action_env` flags, the latest assignment wins.
562</p>
David Chen8fe82a32016-08-24 10:55:41 +0000563
Benjamin Peterson0b318542017-12-04 10:46:48 -0800564<h4 id='flag--experimental_action_listener'>
565 <code class='flag'>--experimental_action_listener=<var>label</var></code>
566</h4>
567<p>
568 The <code>experimental_action_listener</code> option instructs Bazel to use
569 details from the <a href="be/extra-actions.html#action_listener"
570 ><code>action_listener</code></a> rule specified by <var>label</var> to
571 insert <a href="be/extra-actions.html#extra_action"
572 ><code>extra_actions</code></a> into the build graph.
573</p>
574
575<h4 id='flag--experimental_extra_action_top_level_only'>
576 <code class='flag'>--[no]experimental_extra_action_top_level_only</code>
577</h4>
578<p>
579 If this option is set to true, extra actions specified by the
580 <a href='#flag--experimental_action_listener'> <code>
581 --experimental_action_listener</code></a> command line option will only be
582 scheduled for top level targets.
583</p>
584
585<h4 id='flag--experimental_extra_action_filter'>
586 <code class='flag'>--experimental_extra_action_filter=<var>regex</var></code>
587</h4>
588<p>
589 The <code>experimental_extra_action_filter</code> option instructs Bazel to
590 filter the set of targets to schedule <code>extra_actions</code> for.
591</p>
592<p>
593 This flag is only applicable in combination with the
594 <a href='#flag--experimental_action_listener'
595 ><code>--experimental_action_listener</code></a> flag.
596</p>
597<p>
598 By default all <code>extra_actions</code> in the transitive closure of the
599 requested targets-to-build get scheduled for execution.
600 <code>--experimental_extra_action_filter</code> will restrict scheduling to
601 <code>extra_actions</code> of which the owner's label matches the specified
602 regular expression.
603</p>
604<p>
605 The following example will limit scheduling of <code>extra_actions</code>
606 to only apply to actions of which the owner's label contains '/bar/':
607</p>
608<pre>% bazel build --experimental_action_listener=//test:al //foo/... \
609 --experimental_extra_action_filter=.*/bar/.*
610</pre>
611
David Chen8fe82a32016-08-24 10:55:41 +0000612<h4 id='flag--host_cpu'><code class='flag'>--host_cpu <var>cpu</var></code></h4>
613<p>
614 This option specifies the name of the CPU architecture that should be
615 used to build host tools.
616</p>
617
Alex Humesky105e6612017-01-06 19:03:10 +0000618<h4 id='flag--fat_apk_cpu'><code class='flag'>--fat_apk_cpu <var>cpu[,cpu]*</var></code></h4>
619<p>
620 The CPUs to build C/C++ libraries for in the transitive <code>deps</code> of
621 <code>android_binary</code>
622
623 rules. Other C/C++ rules are not affected. For example, if a <code>cc_library</code>
624 appears in the transitive <code>deps</code> of an <code>android_binary</code> rule and a
625 <code>cc_binary</code> rule, the <code>cc_library</code> will be built at least twice:
626 once for each CPU specified with <code class='flag'>--fat_apk_cpu</code> for the
627 <code>android_binary</code> rule, and once for the CPU specified with
628 <code class='flag'>--cpu</code> for the <code>cc_binary</code> rule.
Ivo List2fb9f3c2020-12-17 10:19:09 -0800629</p>
Alex Humesky105e6612017-01-06 19:03:10 +0000630
631<p>
Ivo List2fb9f3c2020-12-17 10:19:09 -0800632 The default is <code>armeabi-v7a</code>.
633</p>
634<p>
635 One <code>.so</code> file is created and packaged in the APK for
636 each CPU specified with <code class='flag'>--fat_apk_cpu</code>. The <code>.so</code> file's name
637 prefixes the name of the <code>android_binary</code> rule with "lib". For example, if the name of
638 the <code>android_binary</code> is "foo", then the file is <code>libfoo.so</code>.
Alex Humesky105e6612017-01-06 19:03:10 +0000639</p>
640 <p>
641 One <code>.so</code> file will be created and packaged in the APK for
642 each CPU specified with <code class='flag'>--fat_apk_cpu</code>. The name of the <code>.so</code>
643 file will be the name of the <code>android_binary</code> rule prefixed with "lib", e.g., if the name
644 of the <code>android_binary</code> is "foo", then the file will be <code>libfoo.so</code>.
645 </p>
646
647 <p>
648 Note that an Android-compatible crosstool must be selected.
649 If an <code>android_ndk_repository</code> rule is defined in the
650 WORKSPACE file, an Android-compatible crosstool is automatically selected.
651 Otherwise, the crostool can be selected using the
652 <a href='#flag--android_crosstool_top'><code class='flag'>--android_crosstool_top</code></a>
653 or <a href='#flag--crosstool_top'><code class='flag'>--crosstool_top</code></a> flags.
654 </p>
Ivo List2fb9f3c2020-12-17 10:19:09 -0800655
656
657<h4 id='flag--per_file_copt'><code class='flag'>--per_file_copt
658 <var>[+-]regex[,[+-]regex]...@option[,option]...</var></code></h4>
Alex Humesky105e6612017-01-06 19:03:10 +0000659</p>
660
David Chen8fe82a32016-08-24 10:55:41 +0000661<h4 id='flag--per_file_copt'><code class='flag'>--per_file_copt
662 <var>[+-]regex[,[+-]regex]...@option[,option]...</var></code></h4>
663<p>
664 When present, any C++ file with a label or an execution path matching one of the inclusion regex
665 expressions and not matching any of the exclusion expressions will be built
666 with the given options. The label matching uses the canonical form of the label
667 (i.e //<code>package</code>:<code>label_name</code>).
668
669 The execution path is the relative path to your workspace directory including the base name
670 (including extension) of the C++ file. It also includes any platform dependent prefixes.
671 Note, that if only one of the label or the execution path matches the options will be used.
672</p>
673<p>
674 <b>Notes</b>:
675 To match the generated files (e.g. genrule outputs)
676 Bazel can only use the execution path. In this case the regexp shouldn't start with '//'
677 since that doesn't match any execution paths. Package names can be used like this:
678 <code class='flag'>--per_file_copt=base/.*\.pb\.cc@-g0</code>. This will match every
679 <code>.pb.cc</code> file under a directory called <code>base</code>.
680</p>
681<p>
682 This option can be used multiple times.
683</p>
684<p>
685 The option is applied regardless of the compilation mode used. I.e. it is possible
686 to compile with <code class='flag'>--compilation_mode=opt</code> and selectively compile some
687 files with stronger optimization turned on, or with optimization disabled.
688</p>
689<p>
690 <b>Caveat</b>: If some files are selectively compiled with debug symbols the symbols
691 might be stripped during linking. This can be prevented by setting
692 <code class='flag'>--strip=never</code>.
693</p>
694<p>
695 <b>Syntax</b>: <code>[+-]regex[,[+-]regex]...@option[,option]...</code> Where
696 <code>regex</code> stands for a regular expression that can be prefixed with
697 a <code>+</code> to identify include patterns and with <code>-</code> to identify
698 exclude patterns. <code>option</code> stands for an arbitrary option that is passed
699 to the C++ compiler. If an option contains a <code>,</code> it has to be quoted like so
700 <code>\,</code>. Options can also contain <code>@</code>, since only the first
701 <code>@</code> is used to separate regular expressions from options.
702</p>
703<p>
704 <b>Example</b>:
705 <code class='flag'>--per_file_copt=//foo:.*\.cc,-//foo:file\.cc@-O0,-fprofile-arcs</code>
706 adds the <code>-O0</code> and the <code>-fprofile-arcs</code> options to the command
707 line of the C++ compiler for all <code>.cc</code> files in <code>//foo/</code> except
708 <code>file.cc</code>.
709</p>
710<h4 id='flag--dynamic_mode'><code class='flag'>--dynamic_mode <var>mode</var></code></h4>
711<p>
712 Determines whether C++ binaries will be linked dynamically, interacting with
713 the <a href='be/c-cpp.html#cc_binary.linkstatic'>linkstatic
714 attribute</a> on build rules.
715</p>
716
717<p>
718 Modes:
719</p>
720<ul>
721 <li><code>auto</code>: Translates to a platform-dependent mode;
722 <code>default</code> for linux and <code>off</code> for cygwin.</li>
723 <li><code>default</code>: Allows bazel to choose whether to link dynamically.
724 See <a href='be/c-cpp.html#cc_binary.linkstatic'>linkstatic</a> for more
725 information.</li>
Googler0e692fb2021-01-22 18:38:24 +0100726 <li><code>fully</code>: Links all targets dynamically. This will speed up
David Chen8fe82a32016-08-24 10:55:41 +0000727 linking time, and reduce the size of the resulting binaries.
728
729 </li>
730 <li><code>off</code>: Links all targets in
731 <a href='be/c-cpp.html#cc_binary.linkstatic'>mostly static</a> mode.
732 If <code>-static</code> is set in linkopts, targets will change to fully
733 static.</li>
734</ul>
735
736<h4 id='flag--fission'><code class='flag'>--fission (yes|no|[dbg][,opt][,fastbuild])</code></h4>
737<p>
738 Enables
739
740 <a href='https://gcc.gnu.org/wiki/DebugFission'>Fission</a>,
741 which writes C++ debug information to dedicated .dwo files instead of .o files, where it would
742 otherwise go. This substantially reduces the input size to links and can reduce link times.
743
744</p>
745<p>
746 When set to <code class='flag'>[dbg][,opt][,fastbuild]</code> (example:
747 <code class='flag'>--fission=dbg,fastbuild</code>), Fission is enabled
748 only for the specified set of compilation modes. This is useful for bazelrc
749 settings. When set to <code class='flag'>yes</code>, Fission is enabled
750 universally. When set to <code class='flag'>no</code>, Fission is disabled
751 universally. Default is <code class='flag'>dbg</code>.
752</p>
753
754<h4 id='flag--force_ignore_dash_static'><code class='flag'>--force_ignore_dash_static</code></h4>
755<p>
756 If this flag is set, any <code>-static</code> options in linkopts of
757 <code>cc_*</code> rules BUILD files are ignored. This is only intended as a
758 workaround for C++ hardening builds.
759</p>
760
761<h4 id='flag--force_pic'><code class='flag'>--[no]force_pic</code></h4>
762<p>
763 If enabled, all C++ compilations produce position-independent code ("-fPIC"),
764 links prefer PIC pre-built libraries over non-PIC libraries, and links produce
765 position-independent executables ("-pie"). Default is disabled.
766</p>
767<p>
768 Note that dynamically linked binaries (i.e. <code>--dynamic_mode fully</code>)
769 generate PIC code regardless of this flag's setting. So this flag is for cases
770 where users want PIC code explicitly generated for static links.
771</p>
772
Andrew Pellegrini8d2c1662017-01-26 16:04:12 +0000773<h4 id='flag--android_resource_shrinking'><code class='flag'>--android_resource_shrinking</code></h4>
774<p>
775 Selects whether to perform resource shrinking for android_binary rules. Sets the default for the
776 <a href='be/android.html#android_binary.shrink_resources'>shrink_resources attribute</a> on
777 android_binary rules; see the documentation for that rule for further details. Defaults to off.
778</p>
779
David Chen8fe82a32016-08-24 10:55:41 +0000780<h4 id='flag--custom_malloc'><code class='flag'>--custom_malloc <var>malloc-library-target</var></code></h4>
781<p>
782 When specified, always use the given malloc implementation, overriding all
783 <code>malloc="target"</code> attributes, including in those targets that use the
784 default (by not specifying any <code>malloc</code>).
785</p>
786
787<h4 id='flag--crosstool_top'><code class='flag'>--crosstool_top <var>label</var></code></h4>
788<p>
789 This option specifies the location of the crosstool compiler suite
790 to be used for all C++ compilation during a build. Bazel will look in that
791 location for a CROSSTOOL file and uses that to automatically determine
792 settings for
793
794 <code class='flag'>--compiler</code>.
795</p>
796
797<h4 id='flag--host_crosstool_top'><code class='flag'>--host_crosstool_top <var>label</var></code></h4>
798<p>
799 If not specified, bazel uses the value of <code class='flag'>--crosstool_top</code> to compile
800 code in the host configuration, i.e., tools run during the build. The main purpose of this flag
801 is to enable cross-compilation.
802</p>
803
Cal Peyserf1a15c02017-01-17 20:26:00 +0000804<h4 id='flag--apple_crosstool_top'><code class='flag'>--apple_crosstool_top <var>label</var></code></h4>
805<p>
806 The crosstool to use for compiling C/C++ rules in the transitive <code>deps</code> of
Googler0e692fb2021-01-22 18:38:24 +0100807 objc_*, ios__*, and apple_* rules. For those targets, this flag overwrites
Cal Peyserf1a15c02017-01-17 20:26:00 +0000808 <code class='flag'>--crosstool_top</code>.
809</p>
810
Alex Humesky105e6612017-01-06 19:03:10 +0000811<h4 id='flag--android_crosstool_top'><code class='flag'>--android_crosstool_top <var>label</var></code></h4>
812<p>
813 The crosstool to use for compiling C/C++ rules in the transitive <code>deps</code> of
814 <code>android_binary</code> rules. This is useful if other targets in the
815 build require a different crosstool. The default is to use the crosstool
816 generated by the <code>android_ndk_repository</code> rule in the WORKSPACE file.
817 See also <a href='#flag--fat_apk_cpu'><code class='flag'>--fat_apk_cpu</code></a>.
818</p>
David Chen8fe82a32016-08-24 10:55:41 +0000819<h4 id='flag--compiler'><code class='flag'>--compiler <var>version</var></code></h4>
820<p>
821 This option specifies the C/C++ compiler version (e.g. <code>gcc-4.1.0</code>)
822 to be used for the compilation of binaries during the build. If you want to
823 build with a custom crosstool, you should use a CROSSTOOL file instead of
824 specifying this flag.
825</p>
826<p>
827 Note that only certain combinations of crosstool version, compiler version,
rosica25ae1992018-06-04 10:41:53 -0700828 and target CPU are allowed.
David Chen8fe82a32016-08-24 10:55:41 +0000829</p>
830
Alex Humesky105e6612017-01-06 19:03:10 +0000831<h4 id='flag--android_sdk'><code class='flag'>--android_sdk <var>label</var></code></h4>
832<p>
833 This option specifies the Android SDK/platform toolchain
834 and Android runtime library that will be used to build any Android-related
835 rule.
836
837 The Android SDK will be automatically selected if an <code>android_sdk_repository</code>
838 rule is defined in the WORKSPACE file.
839</p>
840
David Chen8fe82a32016-08-24 10:55:41 +0000841<h4 id='flag--java_toolchain'><code class='flag'>--java_toolchain <var>label</var></code></h4>
842<p>
843 This option specifies the label of the java_toolchain used to compile Java
844 source files.
845</p>
846
cushon41375ac42017-10-30 17:03:18 -0400847<h4 id='flag--host_java_toolchain'><code class='flag'>--host_java_toolchain <var>label</var></code></h4>
848<p>
849 If not specified, bazel uses the value of <code class='flag'>--java_toolchain</code> to compile
850 code in the host configuration, i.e., tools run during the build. The main purpose of this flag
851 is to enable cross-compilation.
852</p>
853
Gregg Donovanfb915e82018-02-20 09:10:21 -0800854<h4 id='flag--javabase'><code class='flag'>--javabase (<var>label</var>)</code></h4>
David Chen8fe82a32016-08-24 10:55:41 +0000855<p>
cushon7d9596c2018-07-23 13:55:14 -0700856 This option sets the <i>label</i> of the base Java installation to use for <i>bazel run</i>,
857 <i>bazel test</i>, and for Java binaries built by <code>java_binary</code> and
858 <code>java_test</code> rules. The <code>JAVABASE</code> and <code>JAVA</code>
859 <a href='be/make-variables.html'>"Make" variables</a> are derived from this option.
David Chen8fe82a32016-08-24 10:55:41 +0000860</p>
861
cushon41375ac42017-10-30 17:03:18 -0400862<h4 id='flag--host_javabase'><code class='flag'>--host_javabase <var>label</var></code></h4>
863<p>
cushon2a8b6572018-07-25 10:33:40 -0700864 This option sets the <i>label</i> of the base Java installation to use in the host configuration,
cushon7d9596c2018-07-23 13:55:14 -0700865 for example for host build tools including JavaBuilder and Singlejar.
cushon41375ac42017-10-30 17:03:18 -0400866</p>
David Chen8fe82a32016-08-24 10:55:41 +0000867<p>
868 This does not select the Java compiler that is used to compile Java
869 source files. The compiler can be selected by settings the
870 <a href="#flag--java_toolchain"><code class='flag'>--java_toolchain</code></a>
871 option.
872</p>
873
brandjoncf0390b2020-02-20 10:37:40 -0800874<h3 id='strategy-options'>Execution strategy</h3>
David Chen8fe82a32016-08-24 10:55:41 +0000875<p>
876 These options affect how Bazel will execute the build.
877 They should not have any significant effect on the output files
Googler0e692fb2021-01-22 18:38:24 +0100878 generated by the build. Typically their main effect is on the
Googler8d6ac072021-02-12 13:25:32 -0800879 speed of the build.
David Chen8fe82a32016-08-24 10:55:41 +0000880</p>
881
882<h4 id='flag--spawn_strategy'><code class='flag'>--spawn_strategy <var>strategy</var></code></h4>
883<p>
884 This option controls where and how commands are executed.
885</p>
886<ul>
887
888 <li>
ulfjacka42d1892019-02-12 08:34:30 -0800889 <code>standalone</code> causes commands to be executed as local subprocesses. This value is
890 deprecated. Please use <code>local</code> instead.
David Chen8fe82a32016-08-24 10:55:41 +0000891 </li>
892 <li>
893 <code>sandboxed</code> causes commands to be executed inside a sandbox on the local machine.
894 This requires that all input files, data dependencies and tools are listed as direct
895 dependencies in the <code>srcs</code>, <code>data</code> and <code>tools</code> attributes.
896 This is the default on systems that support sandboxed execution.
897 </li>
898
ulfjacka42d1892019-02-12 08:34:30 -0800899 <li>
900 <code>local</code> causes commands to be executed as local subprocesses.
901 </li>
902
ulfjacka42d1892019-02-12 08:34:30 -0800903 <li>
Dave Leebc67b362019-10-02 04:29:41 -0700904 <code>worker</code> causes commands to be executed using a persistent worker, if available.
905 </li>
906
907 <li>
ulfjacka42d1892019-02-12 08:34:30 -0800908 <code>docker</code> causes commands to be executed inside a docker sandbox on the local machine.
909 This requires that docker is installed.
910 </li>
911
912 <li>
913 <code>remote</code> causes commands to be executed remotely; this is only available if a
914 remote executor has been configured separately.
915 </li>
ulfjacka42d1892019-02-12 08:34:30 -0800916</ul>
917
918<h4 id='flag--strategy'><code class='flag'>--strategy <var>mnemonic</var>=<var>strategy</var></code></h4>
919<p>
920 This option controls where and how commands are executed, overriding the default setting on a
921 per-mnemonic basis. See
922 <code class='flag'><a href="#flag--spawn_strategy">--spawn_strategy</a></code> for the supported
923 strategies and their effects.
924</p>
925
926<h4 id='flag--strategy_regexp'><code class='flag'>--strategy_regexp <var>&lt;filter,filter,...&gt;=&lt;strategy&gt;</var></code></h4>
927<p>
928 This option specifies which strategy should be used to execute commands that have descriptions
929 matching a certain <code>regex_filter</code>. See
930 <code class='flag'><a href="#flag--per_file_copt">--per_file_copt</a></code> for details on
931 regex_filter matching. See
932 <code class='flag'><a href="#flag--spawn_strategy">--spawn_strategy</a></code> for the supported
933 strategies and their effects.
934</p>
935<p>
936The first <code>regex_filter</code> that matches the description is used. This option overrides
937other flags for specifying strategy.
938</p>
939<ul>
940 <li>
941 Example: <code>--strategy_regexp=//foo.*\\.cc,-//foo/bar=local</code> means to run actions using
942 <code>local</code> strategy if their descriptions match //foo.*.cc but not //foo/bar.
943 </li>
944 <li>
945 Example:
946 <code>--strategy_regexp='Compiling.*/bar=local' --strategy_regexp=Compiling=sandboxed</code>
947 will run 'Compiling //foo/bar/baz' with the <code>local</code> strategy, but reversing the order
948 would run it with <code>sandboxed</code>.
949 </li>
David Chen8fe82a32016-08-24 10:55:41 +0000950</ul>
951
952<h4 id='flag--genrule_strategy'><code class='flag'>--genrule_strategy <var>strategy</var></code></h4>
953<p>
ulfjacka42d1892019-02-12 08:34:30 -0800954 This is a deprecated short-hand for
955 <code class='flag'>--strategy=Genrule=<var>strategy</var></code>.
David Chen8fe82a32016-08-24 10:55:41 +0000956</p>
David Chen8fe82a32016-08-24 10:55:41 +0000957
David Chen8fe82a32016-08-24 10:55:41 +0000958<h4 id='flag--jobs'><code class='flag'>--jobs <var>n</var></code> (-j)</h4>
959<p>
960 This option, which takes an integer argument, specifies a limit on
961 the number of jobs that should be executed concurrently during the
Googlerb6857d52017-09-22 14:03:21 -0400962 execution phase of the build.
David Chen8fe82a32016-08-24 10:55:41 +0000963</p>
964<p>
965 Note that the number of concurrent jobs that Bazel will run
966 is determined not only by the <code class='flag'>--jobs</code> setting, but also
967 by Bazel's scheduler, which tries to avoid running concurrent jobs
968 that will use up more resources (RAM or CPU) than are available,
969 based on some (very crude) estimates of the resource consumption
Googler0e692fb2021-01-22 18:38:24 +0100970 of each job. The behavior of the scheduler can be controlled by
steinman7f530c82020-04-15 14:13:42 -0700971 the <code class='flag'>--local_ram_resources</code> option.
David Chen8fe82a32016-08-24 10:55:41 +0000972</p>
973
974<h4 id='flag--progress_report_interval'><code class='flag'>--progress_report_interval <var>n</var></code></h4>
975<p>
976
977 Bazel periodically prints a progress report on jobs that are not
Googler0e692fb2021-01-22 18:38:24 +0100978 finished yet (e.g. long running tests). This option sets the
David Chen8fe82a32016-08-24 10:55:41 +0000979 reporting frequency, progress will be printed every <code>n</code>
980 seconds.
981</p>
982<p>
983 The default is 0, that means an incremental algorithm: the first
984 report will be printed after 10 seconds, then 30 seconds and after
985 that progress is reported once every minute.
986</p>
987
steinmancd1783d2019-03-08 08:09:42 -0800988<h4 id='flag--local_{ram,cpu}_resources'><code class='flag'>--local_{ram,cpu}_resources</code>
989 <var>resources or resource expression</var></h4>
David Chen8fe82a32016-08-24 10:55:41 +0000990<p>
steinmancd1783d2019-03-08 08:09:42 -0800991 These options specify the amount of local resources (RAM in MB and number of CPU logical cores)
992 that Bazel can take into consideration when scheduling build and test activities. They take
Ivo List2fb9f3c2020-12-17 10:19:09 -0800993 an integer, or a keyword (HOST_RAM or HOST_CPUS) optionally followed by [-|*<code>float</code>]
steinmancd1783d2019-03-08 08:09:42 -0800994 (for example, <code class='flag'>--local_cpu_resources=2</code>,
995 <code class='flag'>--local_ram_resources=HOST_RAM*.5</code>,
996 <code class='flag'>--local_cpu_resources=HOST_CPUS-1</code>).
997 The flags are independent; one or both may be set. By default Bazel will estimate amount of RAM
998 and number of CPU cores directly from system configuration.
David Chen8fe82a32016-08-24 10:55:41 +0000999</p>
David Chen8fe82a32016-08-24 10:55:41 +00001000
1001<h4 id='flag--build_runfile_links'><code class='flag'>--[no]build_runfile_links</code></h4>
1002<p>
Googler5a495842017-08-22 01:30:36 +02001003 This option, which is enabled by default, specifies whether the runfiles
1004 symlinks for tests and binaries should be built in the output directory.
David Chen8fe82a32016-08-24 10:55:41 +00001005 Using <code class='flag'>--nobuild_runfile_links</code> can be useful
1006 to validate if all targets compile without incurring the overhead
1007 for building the runfiles trees.
1008
David Chen8fe82a32016-08-24 10:55:41 +00001009</p>
1010
1011<p>
Googler5a495842017-08-22 01:30:36 +02001012 When tests (or applications) are executed, their run-time data
Googler0e692fb2021-01-22 18:38:24 +01001013 dependencies are gathered together in one place. Within Bazel's
Googler5a495842017-08-22 01:30:36 +02001014 output tree, this "runfiles" tree is typically rooted as a sibling of
1015 the corresponding binary or test.
1016 During test execution, runfiles may be accessed using paths of the form
David Chen8fe82a32016-08-24 10:55:41 +00001017 <code>$TEST_SRCDIR/workspace/<var>packagename</var>/<var>filename</var></code>.
Googler5a495842017-08-22 01:30:36 +02001018 The runfiles tree ensures that tests have access to all the files
Googler0e692fb2021-01-22 18:38:24 +01001019 upon which they have a declared dependence, and nothing more. By
David Chen8fe82a32016-08-24 10:55:41 +00001020 default, the runfiles tree is implemented by constructing a set of
Googler0e692fb2021-01-22 18:38:24 +01001021 symbolic links to the required files. As the set of links grows, so
David Chen8fe82a32016-08-24 10:55:41 +00001022 does the cost of this operation, and for some large builds it can
1023 contribute significantly to overall build time, particularly because
1024 each individual test (or application) requires its own runfiles tree.
1025</p>
David Chen8fe82a32016-08-24 10:55:41 +00001026
Googler01f659d2017-08-22 02:53:02 +02001027<h4 id='flag--build_runfile_manifests'><code class='flag'>--[no]build_runfile_manifests</code></h4>
1028<p>
1029 This option, which is enabled by default, specifies whether runfiles manifests
1030 should be written to the output tree.
1031 Disabling it implies <code class='flag'>--nobuild_runfile_links</code>.
1032
Tobias Werth78e27f42019-08-12 00:49:58 -07001033 It can be disabled when executing tests remotely, as runfiles trees will
Googler01f659d2017-08-22 02:53:02 +02001034 be created remotely from in-memory manifests.
1035
David Chen8fe82a32016-08-24 10:55:41 +00001036<h4 id='flag--discard_analysis_cache'>
1037 <code class='flag'>--[no]discard_analysis_cache</code></h4>
1038<p>
1039 When this option is enabled, Bazel will discard the analysis cache
1040 right before execution starts, thus freeing up additional memory
Googler59830672019-01-03 10:33:49 -08001041 (around 10%) for the <a href="guide.html#execution-phase">execution phase</a>.
janakr428c5e12019-05-06 15:35:54 -07001042 The drawback is that further incremental builds will be slower. See also
janakr69781dd52019-05-07 09:04:41 -07001043 <a href="/versions/{{ current_version }}/memory-saving-mode.html">
Googlera05b7f02019-05-21 08:13:04 -07001044memory-saving mode</a>.
David Chen8fe82a32016-08-24 10:55:41 +00001045</p>
1046
1047<h4 id='flag--keep_going'><code class='flag'>--[no]keep_going</code> (-k)</h4>
1048<p>
1049 As in GNU Make, the execution phase of a build stops when the first
Googler0e692fb2021-01-22 18:38:24 +01001050 error is encountered. Sometimes it is useful to try to build as
1051 much as possible even in the face of errors. This option enables
David Chen8fe82a32016-08-24 10:55:41 +00001052 that behavior, and when it is specified, the build will attempt to
1053 build every target whose prerequisites were successfully built, but
1054 will ignore errors.
1055</p>
1056<p>
1057 While this option is usually associated with the execution phase of
Googler8d6ac072021-02-12 13:25:32 -08001058 a build, it also affects the analysis phase: if several targets are
David Chen8fe82a32016-08-24 10:55:41 +00001059 specified in a build command, but only some of them can be
1060 successfully analyzed, the build will stop with an error
1061 unless <code class='flag'>--keep_going</code> is specified, in which case the
1062 build will proceed to the execution phase, but only for the targets
1063 that were successfully analyzed.
1064</p>
1065
David Chen8fe82a32016-08-24 10:55:41 +00001066<h4 id='flag--use_ijars'><code class='flag'>--[no]use_ijars</code></h4>
1067<p>
1068 This option changes the way <code>java_library</code> targets are
1069 compiled by Bazel. Instead of using the output of a
1070 <code>java_library</code> for compiling dependent
1071 <code>java_library</code> targets, Bazel will create interface jars
1072 that contain only the signatures of non-private members (public,
1073 protected, and default (package) access methods and fields) and use
Googler0e692fb2021-01-22 18:38:24 +01001074 the interface jars to compile the dependent targets. This makes it
David Chen8fe82a32016-08-24 10:55:41 +00001075 possible to avoid recompilation when changes are only made to
1076 method bodies or private members of a class.
1077</p>
1078<p>
1079 Note that using <code class='flag'>--use_ijars</code> might give you a different
1080 error message when you are accidentally referring to a non visible
1081 member of another class: Instead of getting an error that the member
1082 is not visible you will get an error that the member does not exist.
1083</p>
1084<p>
1085 Note that changing the <code class='flag'>--use_ijars</code> setting will force
1086 a recompilation of all affected classes.
1087</p>
1088
1089<h4 id='flag--interface_shared_objects'>
1090 <code class='flag'>--[no]interface_shared_objects</code>
1091</h4>
1092<p>
1093 This option enables <i>interface shared objects</i>, which makes binaries and
1094 other shared libraries depend on the <i>interface</i> of a shared object,
1095 rather than its implementation. When only the implementation changes, Bazel
1096 can avoid rebuilding targets that depend on the changed shared library
1097 unnecessarily.
1098</p>
1099
brandjoncf0390b2020-02-20 10:37:40 -08001100<h3 id='output-selection-options'>Output selection</h3>
David Chen8fe82a32016-08-24 10:55:41 +00001101<p>
1102 These options determine what to build or test.
1103</p>
1104
1105<h4 id="nobuild"><code class='flag'>--[no]build</code></h4>
1106<p>
1107 This option causes the execution phase of the build to occur; it is
Googler0e692fb2021-01-22 18:38:24 +01001108 on by default. When it is switched off, the execution phase is
David Chen8fe82a32016-08-24 10:55:41 +00001109 skipped, and only the first two phases, loading and analysis, occur.
1110</p>
1111<p>
1112 This option can be useful for validating BUILD files and detecting
1113 errors in the inputs, without actually building anything.
1114</p>
1115
1116<h4 id='flag--build_tests_only'><code class='flag'>--[no]build_tests_only</code></h4>
1117<p>
1118 If specified, Bazel will build only what is necessary to run the *_test
1119 and test_suite rules that were not filtered due to their
1120 <a href='#flag--test_size_filters'>size</a>,
1121 <a href='#flag--test_timeout_filters'>timeout</a>,
1122 <a href='#flag--test_tag_filters'>tag</a>, or
1123 <a href='#flag--test_lang_filters'>language</a>.
1124 If specified, Bazel will ignore other targets specified on the command line.
1125 By default, this option is disabled and Bazel will build everything
1126 requested, including *_test and test_suite rules that are filtered out from
1127 testing. This is useful because running
1128 <code>bazel test --build_tests_only foo/...</code> may not detect all build
1129 breakages in the <code>foo</code> tree.
1130</p>
1131
1132<h4 id='flag--check_up_to_date'><code class='flag'>--[no]check_up_to_date</code></h4>
1133<p>
1134 This option causes Bazel not to perform a build, but merely check
Googler0e692fb2021-01-22 18:38:24 +01001135 whether all specified targets are up-to-date. If so, the build
1136 completes successfully, as usual. However, if any files are out of
David Chen8fe82a32016-08-24 10:55:41 +00001137 date, instead of being built, an error is reported and the build
Googler0e692fb2021-01-22 18:38:24 +01001138 fails. This option may be useful to determine whether a build has
David Chen8fe82a32016-08-24 10:55:41 +00001139 been performed more recently than a source edit (e.g. for pre-submit
1140 checks) without incurring the cost of a build.
1141</p>
1142<p>
1143 See also <a href="#flag--check_tests_up_to_date"><code class='flag'>--check_tests_up_to_date</code></a>.
1144</p>
1145
1146<h4 id='flag--compile_one_dependency'><code class='flag'>--[no]compile_one_dependency</code></h4>
1147<p>
Googler0e692fb2021-01-22 18:38:24 +01001148 Compile a single dependency of the argument files. This is useful for
David Chen8fe82a32016-08-24 10:55:41 +00001149 syntax checking source files in IDEs, for example, by rebuilding a single
1150 target that depends on the source file to detect errors as early as
Googler0e692fb2021-01-22 18:38:24 +01001151 possible in the edit/build/test cycle. This argument affects the way all
David Chen8fe82a32016-08-24 10:55:41 +00001152 non-flag arguments are interpreted: for each source filename, one
1153 rule that depends on it will be built. For
1154
1155 C++ and Java
1156 sources, rules in the same language space are preferentially chosen. For
1157 multiple rules with the same preference, the one that appears first in the
1158 BUILD file is chosen. An explicitly named target pattern which does not
1159 reference a source file results in an error.
1160</p>
1161
1162<h4 id='flag--save_temps'><code class='flag'>--save_temps</code></h4>
1163<p>
Googler9cfa4cb2018-06-04 22:19:11 -07001164 The <code class='flag'>--save_temps</code> option causes temporary outputs from the compiler to be
1165 saved. These include .s files (assembler code), .i (preprocessed C) and .ii
Googler0e692fb2021-01-22 18:38:24 +01001166 (preprocessed C++) files. These outputs are often useful for debugging. Temps will only be
David Chen8fe82a32016-08-24 10:55:41 +00001167 generated for the set of targets specified on the command line.
1168</p>
1169<p>
Googler9cfa4cb2018-06-04 22:19:11 -07001170 Note that our implementation of <code class='flag'>--save_temps</code> does not use the compiler's
Googler0e692fb2021-01-22 18:38:24 +01001171 <code>-save-temps</code> flag. Instead, we do two passes, one with <code>-S</code>
1172 and one with <code>-E</code>. A consequence of this is that if your build fails,
David Chen8fe82a32016-08-24 10:55:41 +00001173 Bazel may not yet have produced the ".i" or ".ii" and ".s" files.
1174 If you're trying to use <code class='flag'>--save_temps</code> to debug a failed compilation,
1175 you may need to also use <code class='flag'>--keep_going</code> so that Bazel will still try to
1176 produce the preprocessed files after the compilation fails.
1177</p>
1178<p>
1179 The <code class='flag'>--save_temps</code> flag currently works only for cc_* rules.
1180</p>
1181<p>
1182 To ensure that Bazel prints the location of the additional output files, check that
1183 your <a href='#flag--show_result'><code class='flag'>--show_result <var>n</var></code></a>
1184 setting is high enough.
1185</p>
1186
Googlerd9db1692017-01-05 19:34:30 +00001187<h4 id='flag--build_tag_filters'><code class='flag'>--build_tag_filters <var>tag[,tag]*</var></code></h4>
1188<p>
1189 If specified, Bazel will build only targets that have at least one required tag
1190 (if any of them are specified) and does not have any excluded tags. Build tag
1191 filter is specified as comma delimited list of tag keywords, optionally
1192 preceded with '-' sign used to denote excluded tags. Required tags may also
1193 have a preceding '+' sign.
1194</p>
janakrc63128d2020-08-05 15:42:49 -07001195<p>
1196 When running tests, Bazel ignores <code class='flag'>--build_tag_filters</code> for test targets,
1197 which are built and run even if they do not match this filter. To avoid building them, filter
1198 test targets using <code class='flag'>--test_tag_filters</code> or by explicitly excluding them.
1199</p>
Googlerd9db1692017-01-05 19:34:30 +00001200
David Chen8fe82a32016-08-24 10:55:41 +00001201<h4 id='flag--test_size_filters'><code class='flag'>--test_size_filters <var>size[,size]*</var></code></h4>
1202<p>
1203 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
1204 is also specified) only test targets with the given size. Test size filter
1205 is specified as comma delimited list of allowed test size values (small,
1206 medium, large or enormous), optionally preceded with '-' sign used to denote
1207 excluded test sizes. For example,
1208</p>
1209<pre>
1210 % bazel test --test_size_filters=small,medium //foo:all
1211</pre>
1212 and
1213<pre>
1214 % bazel test --test_size_filters=-large,-enormous //foo:all
1215</pre>
1216<p>
1217 will test only small and medium tests inside //foo.
1218</p>
1219<p>
1220 By default, test size filtering is not applied.
1221</p>
1222
1223<h4 id='flag--test_timeout_filters'><code class='flag'>--test_timeout_filters <var>timeout[,timeout]*</var></code></h4>
1224<p>
1225 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
1226 is also specified) only test targets with the given timeout. Test timeout filter
1227 is specified as comma delimited list of allowed test timeout values (short,
1228 moderate, long or eternal), optionally preceded with '-' sign used to denote
1229 excluded test timeouts. See <a href='#flag--test_size_filters'>--test_size_filters</a>
1230 for example syntax.
1231</p>
1232<p>
1233 By default, test timeout filtering is not applied.
1234</p>
1235
1236
1237<h4 id='flag--test_tag_filters'><code class='flag'>--test_tag_filters <var>tag[,tag]*</var></code></h4>
1238<p>
1239 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
1240 is also specified) only test targets that have at least one required tag
1241 (if any of them are specified) and does not have any excluded tags. Test tag
1242 filter is specified as comma delimited list of tag keywords, optionally
1243 preceded with '-' sign used to denote excluded tags. Required tags may also
1244 have a preceding '+' sign.
1245</p>
1246<p>
1247 For example,
1248<pre>
1249 % bazel test --test_tag_filters=performance,stress,-flaky //myproject:all
1250</pre>
1251<p>
1252 will test targets that are tagged with either <code>performance</code> or
1253 <code>stress</code> tag but are <b>not</b> tagged with the <code>flaky</code>
1254 tag.
1255</p>
1256<p>
Googler0e692fb2021-01-22 18:38:24 +01001257 By default, test tag filtering is not applied. Note that you can also filter
David Chen8fe82a32016-08-24 10:55:41 +00001258 on test's <code>size</code> and <code>local</code> tags in
1259 this manner.
1260</p>
1261
1262<h4 id='flag--test_lang_filters'><code class='flag'>--test_lang_filters <var>lang[,lang]*</var></code></h4>
1263<p>
1264 Specifies a comma-separated list of test languages for languages with an official <code>*_test</code> rule the
1265 (see <a href="be/overview.html">build encyclopedia</a> for a full list of these). Each
1266 language can be optionally preceded with '-' to specify excluded
Googler0e692fb2021-01-22 18:38:24 +01001267 languages. The name used for each language should be the same as
David Chen8fe82a32016-08-24 10:55:41 +00001268 the language prefix in the <code>*_test</code> rule, for example,
1269 <code>cc</code>, <code>java</code> or <code>sh</code>.
1270</p>
1271<p>
1272 If specified, Bazel will test (or build if <code class='flag'>--build_tests_only</code>
1273 is also specified) only test targets of the specified language(s).
1274</p>
1275<p>
1276 For example,
1277</p>
1278<pre>
1279 % bazel test --test_lang_filters=cc,java foo/...
1280</pre>
1281<p>
1282 will test only the C/C++ and Java tests (defined using
1283 <code>cc_test</code> and <code>java_test</code> rules, respectively)
1284 in <code>foo/...</code>, while
1285</p>
1286<pre>
1287 % bazel test --test_lang_filters=-sh,-java foo/...
1288</pre>
1289<p>
1290 will run all of the tests in <code>foo/...</code> except for the
1291 <code>sh_test</code> and <code>java_test</code> tests.
1292</p>
1293<p>
1294 By default, test language filtering is not applied.
1295</p>
1296
1297<h4 id="flag--test_filter"><code class='flag'>--test_filter=<var>filter-expression</var></code></h4>
1298<p>
1299 Specifies a filter that the test runner may use to pick a subset of tests for
michajlo20971eb2020-03-12 08:00:09 -07001300 running. All targets specified in the invocation are built, but depending on
David Chen8fe82a32016-08-24 10:55:41 +00001301 the expression only some of them may be executed; in some cases, only certain
1302 test methods are run.
1303</p>
1304<p>
1305 The particular interpretation of <var>filter-expression</var> is up to
1306 the test framework responsible for running the test. It may be a glob,
1307 substring, or regexp. <code class='flag'>--test_filter</code> is a convenience
1308 over passing different <code class='flag'>--test_arg</code> filter arguments,
1309 but not all frameworks support it.
1310</p>
1311
brandjoncf0390b2020-02-20 10:37:40 -08001312<h3 id='verbosity'>Verbosity</h3>
David Chen8fe82a32016-08-24 10:55:41 +00001313
1314These options control the verbosity of Bazel's output,
1315either to the terminal, or to additional log files.
1316
1317<h4 id='flag--explain'><code class='flag'>--explain <var>logfile</var></code></h4>
1318<p>
1319 This option, which requires a filename argument, causes the
1320 dependency checker in <code>bazel build</code>'s execution phase to
1321 explain, for each build step, either why it is being executed, or
Googler0e692fb2021-01-22 18:38:24 +01001322 that it is up-to-date. The explanation is written
David Chen8fe82a32016-08-24 10:55:41 +00001323 to <i>logfile</i>.
1324</p>
1325<p>
1326 If you are encountering unexpected rebuilds, this option can help to
Googler0e692fb2021-01-22 18:38:24 +01001327 understand the reason. Add it to your <code>.bazelrc</code> so that
David Chen8fe82a32016-08-24 10:55:41 +00001328 logging occurs for all subsequent builds, and then inspect the log
Googler0e692fb2021-01-22 18:38:24 +01001329 when you see an execution step executed unexpectedly. This option
David Chen8fe82a32016-08-24 10:55:41 +00001330 may carry a small performance penalty, so you might want to remove
1331 it when it is no longer needed.
1332</p>
1333
1334<h4 id='flag--verbose_explanations'><code class='flag'>--verbose_explanations</code></h4>
1335<p>
1336 This option increases the verbosity of the explanations generated
1337 when the <a href='#flag--explain'>--explain</a> option is enabled.
1338</p>
1339<p>
1340 In particular, if verbose explanations are enabled,
1341 and an output file is rebuilt because the command used to
1342 build it has changed, then the output in the explanation file will
1343 include the full details of the new command (at least for most
1344 commands).
1345</p>
1346<p>
1347 Using this option may significantly increase the length of the
1348 generated explanation file and the performance penalty of using
1349 <code class='flag'>--explain</code>.
1350</p>
1351<p>
1352 If <code class='flag'>--explain</code> is not enabled, then
1353 <code class='flag'>--verbose_explanations</code> has no effect.
1354</p>
1355
1356<h4 id='flag--profile'><code class='flag'>--profile <var>file</var></code></h4>
1357<p>
1358 This option, which takes a filename argument, causes Bazel to write
1359 profiling data into a file. The data then can be analyzed or parsed using the
1360 <code>bazel analyze-profile</code> command. The Build profile can be useful in
1361 understanding where Bazel's <code>build</code> command is spending its time.
1362</p>
1363
1364<h4 id='flag--show_loading_progress'><code class='flag'>--[no]show_loading_progress</code></h4>
1365<p>
1366 This option causes Bazel to output package-loading progress
Googler0e692fb2021-01-22 18:38:24 +01001367 messages. If it is disabled, the messages won't be shown.
David Chen8fe82a32016-08-24 10:55:41 +00001368</p>
1369
1370<h4 id='flag--show_progress'><code class='flag'>--[no]show_progress</code></h4>
1371<p>
1372 This option causes progress messages to be displayed; it is on by
Googler0e692fb2021-01-22 18:38:24 +01001373 default. When disabled, progress messages are suppressed.
David Chen8fe82a32016-08-24 10:55:41 +00001374</p>
1375
1376<h4 id='flag--show_progress_rate_limit'><code class='flag'>--show_progress_rate_limit
1377 <var>n</var></code></h4>
1378<p>
1379 This option causes bazel to display only
1380 one progress message per <code>n</code> seconds, where <var>n</var> is a real number.
1381 If <code>n</code> is -1, all progress messages will be displayed. The default value for
1382 this option is 0.03, meaning bazel will limit the progress messages to one per every
1383 0.03 seconds.
1384</p>
1385
1386<h4 id='flag--show_result'><code class='flag'>--show_result <var>n</var></code></h4>
1387<p>
1388 This option controls the printing of result information at the end
Googler0e692fb2021-01-22 18:38:24 +01001389 of a <code>bazel build</code> command. By default, if a single
David Chen8fe82a32016-08-24 10:55:41 +00001390 build target was specified, Bazel prints a message stating whether
1391 or not the target was successfully brought up-to-date, and if so,
Googler0e692fb2021-01-22 18:38:24 +01001392 the list of output files that the target created. If multiple
David Chen8fe82a32016-08-24 10:55:41 +00001393 targets were specified, result information is not displayed.
1394</p>
1395<p>
1396 While the result information may be useful for builds of a single
1397 target or a few targets, for large builds (e.g. an entire top-level
1398 project tree), this information can be overwhelming and distracting;
Googler0e692fb2021-01-22 18:38:24 +01001399 this option allows it to be controlled. <code class='flag'>--show_result</code>
David Chen8fe82a32016-08-24 10:55:41 +00001400 takes an integer argument, which is the maximum number of targets
Googler0e692fb2021-01-22 18:38:24 +01001401 for which full result information should be printed. By default,
1402 the value is 1. Above this threshold, no result information is
1403 shown for individual targets. Thus zero causes the result
David Chen8fe82a32016-08-24 10:55:41 +00001404 information to be suppressed always, and a very large value causes
1405 the result to be printed always.
1406</p>
1407<p>
1408 Users may wish to choose a value in-between if they regularly
1409 alternate between building a small group of targets (for example,
1410 during the compile-edit-test cycle) and a large group of targets
1411 (for example, when establishing a new workspace or running
Googler0e692fb2021-01-22 18:38:24 +01001412 regression tests). In the former case, the result information is
1413 very useful whereas in the latter case it is less so. As with all
David Chen8fe82a32016-08-24 10:55:41 +00001414 options, this can be specified implicitly via
Vladimir Chebotarev66885ff2018-12-04 14:08:16 -08001415 the <a href='guide.html#bazelrc'><code>.bazelrc</code></a> file.
David Chen8fe82a32016-08-24 10:55:41 +00001416</p>
1417<p>
1418 The files are printed so as to make it easy to copy and paste the
Googler0e692fb2021-01-22 18:38:24 +01001419 filename to the shell, to run built executables. The "up-to-date"
David Chen8fe82a32016-08-24 10:55:41 +00001420 or "failed" messages for each target can be easily parsed by scripts
1421 which drive a build.
1422</p>
1423
1424<h4 id='flag--subcommands'><code class='flag'>--subcommands</code> (<code>-s</code>)</h4>
1425<p>
1426 This option causes Bazel's execution phase to print the full command line
1427 for each command prior to executing it.
1428</p>
1429
1430<pre>
1431 &gt;&gt;&gt;&gt;&gt; # //examples/cpp:hello-world [action 'Linking examples/cpp/hello-world']
lberkif071a232017-12-07 04:21:34 -08001432 (cd /home/johndoe/.cache/bazel/_bazel_johndoe/4c084335afceb392cfbe7c31afee3a9f/bazel && \
David Chen8fe82a32016-08-24 10:55:41 +00001433 exec env - \
lberkicbac3282017-12-07 05:47:43 -08001434 /usr/bin/gcc -o bazel-out/local-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 +00001435</pre>
1436<p>
1437 Where possible, commands are printed in a Bourne shell compatible syntax,
1438 so that they can be easily copied and pasted to a shell command prompt.
1439 (The surrounding parentheses are provided to protect your shell from the
1440 <code>cd</code> and <code>exec</code> calls; be sure to copy them!)
1441 However some commands are implemented internally within Bazel, such as
1442 creating symlink trees. For these there's no command line to display.
1443
1444</p>
1445
1446<p>
ahumeskybe31bb82018-07-26 13:37:45 -07001447 <code class='flag'>--subcommands=pretty_print</code> may be passed to print
1448 the arguments of the command as a list rather than as a single line. This may
1449 help make long command lines more readable.
1450</p>
1451
1452<p>
David Chen8fe82a32016-08-24 10:55:41 +00001453 See also <a href="#flag--verbose_failures">--verbose_failures</a>, below.
1454</p>
1455
1456<h4 id='flag--verbose_failures'><code class='flag'>--verbose_failures</code></h4>
1457<p>
1458 This option causes Bazel's execution phase to print the full command line
Googler0e692fb2021-01-22 18:38:24 +01001459 for commands that failed. This can be invaluable for debugging a
David Chen8fe82a32016-08-24 10:55:41 +00001460 failing build.
1461</p>
1462<p>
1463 Failing commands are printed in a Bourne shell compatible syntax, suitable
1464 for copying and pasting to a shell prompt.
1465</p>
1466
brandjoncf0390b2020-02-20 10:37:40 -08001467<h3 id='workspace_status'>Workspace status</h3>
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001468
1469<p>
1470 Use these options to "stamp" Bazel-built binaries: to embed additional information into the
1471 binaries, such as the source control revision or other workspace-related information. You can use
1472 this mechanism with rules that support the <code>stamp</code> attribute, such as
1473 <code>genrule</code>, <code>cc_binary</code>, and more.
1474</p>
1475
1476<h4 id='flag--workspace_status_command'><code class='flag'>--workspace_status_command <var>program</var></code></h4>
1477<p>
1478 This flag lets you specify a binary that Bazel runs before each build. The program can report
1479 information about the status of the workspace, such as the current source control revision.
1480</p>
1481<p>
1482 The flag's value must be a path to a native program. On Linux/macOS this may be any executable.
1483 On Windows this must be a native binary, typically an ".exe", ".bat", or a ".cmd" file.
1484</p>
1485
1486<p>
1487 The program should print zero or more key/value pairs to standard output, one entry on each line,
1488 then exit with zero (otherwise the build fails). The key names can be anything but they may only
1489 use upper case letters and underscores. The first space after the key name separates it from the
jhorvitz2a5bf222020-11-30 14:05:02 -08001490 value. The value is the rest of the line (including additional whitespaces). Neither the key nor
1491 the value may span multiple lines. Keys must not be duplicated.
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001492</p>
1493<p>
1494 Bazel partitions the keys into two buckets: "stable" and "volatile". (The names "stable" and
1495 "volatile" are a bit counter-intuitive, so don't think much about them.)
1496</p>
1497
1498<p>Bazel then writes the key-value pairs into two files:</p>
1499<ul>
1500 <li>
ahumesky5a6d8b62018-11-15 14:05:34 -08001501 <code>bazel-out/stable-status.txt</code>
1502 contains all keys and values where the key's name starts with <code>STABLE_</code>
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001503 </li>
ahumesky5a6d8b62018-11-15 14:05:34 -08001504 <li>
1505 <code>bazel-out/volatile-status.txt</code>
1506 contains the rest of the keys and their values
1507 </li>
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001508</ul>
1509
1510<p>The contract is:</p>
1511<ul>
1512 <li>
1513 <p>
1514 "stable" keys' values should change rarely, if possible. If the contents of
ahumesky5a6d8b62018-11-15 14:05:34 -08001515 <code>bazel-out/stable-status.txt</code>
1516 change, Bazel invalidates the actions that depend on them. In
1517 other words, if a stable key's value changes, Bazel will rerun stamped actions.
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001518 Therefore the stable status should not contain things like timestamps, because they change all
ahumesky5a6d8b62018-11-15 14:05:34 -08001519 the time, and would make Bazel rerun stamped actions with each build.
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001520 </p>
1521 <p>Bazel always outputs the following stable keys:</p>
1522 <ul>
1523 <li><code>BUILD_EMBED_LABEL</code>: value of <code class='flag'>--embed_label</code></li>
1524 <li><code>BUILD_HOST</code>: the name of the host machine that Bazel is running on</li>
1525 <li><code>BUILD_USER</code>: the name of the user that Bazel is running as</li>
1526 </ul>
1527 </li>
1528 <li>
1529 <p>
1530 "volatile" keys' values may change often. Bazel expects them to change all the time, like
ahumesky5a6d8b62018-11-15 14:05:34 -08001531 timestamps do, and duly updates the
David McNettfa388c32019-05-16 13:16:56 -07001532 <code>bazel-out/volatile-status.txt</code>
ahumesky5a6d8b62018-11-15 14:05:34 -08001533 file. In order to avoid
1534 rerunning stamped actions all the time though, <b>Bazel pretends that the volatile file never
1535 changes</b>. In other words, if the volatile status file is the only file whose contents has
1536 changed, Bazel will not invalidate actions that depend on it. If other inputs of the actions
1537 have changed, then Bazel reruns that action, and the action will see the updated volatile
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001538 status, but just the volatile status changing alone will not invalidate the action.
1539 </p>
1540 <p>Bazel always outputs the following volatile keys:</p>
1541 <ul>
1542 <li>
lberki911c7c52018-04-25 07:56:27 -07001543 <code>BUILD_TIMESTAMP</code>: time of the build in seconds since the Unix Epoch (the value
1544 of <code>System.currentTimeMillis()</code> divided by a thousand)
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001545 </li>
1546 </ul>
1547 </li>
1548</ul>
1549
1550<p>
1551 On Linux/macOS you can pass <code class='flag'>--workspace_status_command=/bin/true</code> to
Googler956ecf62018-11-26 10:15:05 -08001552 disable retrieving workspace status, because <code>true</code> does nothing, successfully (exits
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001553 with zero) and prints no output. On Windows you can pass the path of MSYS's <code>true.exe</code>
1554 for the same effect.
1555</p>
1556
1557<p>If the workspace status command fails (exits non-zero) for any reason, the build will fail.</p>
1558
1559<p>Example program on Linux using Git:</p>
1560
1561<pre>
1562#!/bin/bash
1563echo "CURRENT_TIME $(date +%s)"
Googler98982a02020-07-29 10:16:10 -07001564echo "RANDOM_HASH $(cat /proc/sys/kernel/random/uuid)"
laszlocsomor45a0ffe2018-02-05 00:48:35 -08001565echo "STABLE_GIT_COMMIT $(git rev-parse HEAD)"
1566echo "STABLE_USER_NAME $USER"
1567</pre>
1568
1569<p>
1570 Pass this program's path with <code>--workspace_status_command</code>, and the stable status file
1571 will include the STABLE lines and the volatile status file will include the rest of the lines.
1572</p>
1573
David Chen8fe82a32016-08-24 10:55:41 +00001574<h4 id='flag--stamp'><code class='flag'>--[no]stamp</code></h4>
1575<p>
1576 This option controls whether stamping is enabled for
1577 rule types that support it. For most of the supported rule types stamping is
1578 enabled by default (e.g. <code>cc_binary</code>).
1579
1580 By default, stamping is disabled for all tests. Specifying
1581 <code class='flag'>--stamp</code> does not force affected targets to be rebuilt,
1582 if their dependencies have not changed.
1583</p>
1584
1585<p>
1586 Stamping can be enabled or disabled explicitly in BUILD using
1587 the <code>stamp</code> attribute of certain rule types, please refer to
1588 the <a href="be/overview.html">build encyclopedia</a> for details. For
1589 rules that are neither explicitly or implicitly configured as <code>stamp =
1590 0</code> or <code>stamp = 1</code>, the <code class='flag'>--[no]stamp</code> option
1591 selects whether stamping is enabled. Bazel never stamps binaries that are
1592 built for the host configuration, regardless of the stamp attribute.
1593</p>
1594
brandjoncf0390b2020-02-20 10:37:40 -08001595<h3 id='platform_build_options'>Platform</h3>
jcaterbf57a0a2018-06-13 10:16:31 -07001596
1597<p>
1598 Use these options to control the host and target platforms that configure how builds work, and to
1599 control what execution platforms and toolchains are available to Bazel rules.
1600</p>
1601
1602<p>
1603 Please see background information on
1604 <a href="platforms.html">Platforms</a> and <a href="toolchains.html">Toolchains</a>.
1605</p>
1606
1607<h4 id="flag--platforms"><code class='flag'>--platforms <var>labels</var></code></h4>
1608<p>
1609 The labels of the platform rules describing the target platforms for the
1610 current command.
1611</p>
1612
1613<h4 id="flag--host_platform"><code class='flag'>--host_platform <var>label</var></code></h4>
1614<p>
1615 The label of a platform rule that describes the host system.
1616</p>
1617
1618<h4 id="flag--extra_execution_platforms"><code class='flag'>--extra_execution_platforms <var>labels</var></code></h4>
1619<p>
1620 The platforms that are available as execution platforms to run actions.
1621 Platforms can be specified by exact target, or as a target pattern. These
1622 platforms will be considered before those declared in the WORKSPACE file by
Jingwen Chen0f4544d2018-12-14 16:28:16 -08001623 <a href="skylark/lib/globals.html#register_execution_platforms">
jcaterbf57a0a2018-06-13 10:16:31 -07001624 register_execution_platforms()</a>.
1625</p>
1626
1627<h4 id="flag--extra_toolchains"><code class='flag'>--extra_toolchains <var>labels</var></code></h4>
1628<p>
1629 The toolchain rules to be considered during toolchain resolution. Toolchains
1630 can be specified by exact target, or as a target pattern. These toolchains will
1631 be considered before those declared in the WORKSPACE file by
Jingwen Chen0f4544d2018-12-14 16:28:16 -08001632 <a href="skylark/lib/globals.html#register_toolchains">
jcaterbf57a0a2018-06-13 10:16:31 -07001633 register_toolchains()</a>.
1634</p>
1635
1636<h4 id="flag--toolchain_resolution_debug"><code class='flag'>--toolchain_resolution_debug=false</code></h4>
1637<p>
1638 Print debug information while finding toolchains for a rule. This might help
gregceca48e9a2020-04-14 08:54:38 -07001639 developers of Bazel or Starlark rules with debugging failures due to missing
jcaterbf57a0a2018-06-13 10:16:31 -07001640 toolchains.
1641</p>
1642
brandjoncf0390b2020-02-20 10:37:40 -08001643<h3 id='misc_build_options'>Miscellaneous</h3>
David Chen8fe82a32016-08-24 10:55:41 +00001644
warkahscott057ae962020-10-16 13:21:25 -07001645<h4 id='flag--flag_alias'><code class='flag'>--flag_alias <var>alias_name=target_path</var></code></h4>
1646<p>
1647 A convenience flag used to bind longer Starlark build settings to a shorter name. For more
1648 details, see the
1649 <a href=https://docs.bazel.build/versions/master/skylark/config.html#using-build-setting-aliases">Starlark Configurations</a>.
1650</p>
1651
David Chen8fe82a32016-08-24 10:55:41 +00001652<h4 id='flag--symlink_prefix'><code class='flag'>--symlink_prefix <var>string</var></code></h4>
1653<p>
1654 Changes the prefix of the generated convenience symlinks. The
1655 default value for the symlink prefix is <code>bazel-</code> which
1656 will create the symlinks <code>bazel-bin</code>, <code>bazel-testlogs</code>, and
1657 <code>bazel-genfiles</code>.
1658</p>
1659<p>
1660 If the symbolic links cannot be created for any reason, a warning is
Googler0e692fb2021-01-22 18:38:24 +01001661 issued but the build is still considered a success. In particular,
David Chen8fe82a32016-08-24 10:55:41 +00001662 this allows you to build in a read-only directory or one that you have no
Googler0e692fb2021-01-22 18:38:24 +01001663 permission to write into. Any paths printed in informational
David Chen8fe82a32016-08-24 10:55:41 +00001664 messages at the conclusion of a build will only use the
1665 symlink-relative short form if the symlinks point to the expected
1666 location; in other words, you can rely on the correctness of those
1667 paths, even if you cannot rely on the symlinks being created.
1668</p>
1669<p>
1670 Some common values of this option:
1671</p>
1672<ul>
1673
1674 <li>
1675 <p><b>Suppress symlink creation:</b>
1676 <code class='flag'>--symlink_prefix=/</code> will cause Bazel to not
1677 create or update any symlinks, including the <code>bazel-out</code> and
1678
1679 <code>bazel-&lt;workspace&gt;</code>
Googler0e692fb2021-01-22 18:38:24 +01001680 symlinks. Use this option to suppress symlink creation entirely.
David Chen8fe82a32016-08-24 10:55:41 +00001681 </p>
1682 </li>
1683 <li>
1684 <p><b>Reduce clutter:</b>
1685 <code class='flag'>--symlink_prefix=.bazel/</code> will cause Bazel to create
1686 symlinks called <code>bin</code> (etc) inside a hidden directory <code>.bazel</code>.
1687 </p>
1688 </li>
1689</ul>
1690
1691<h4 id='flag--platform_suffix'><code class='flag'>--platform_suffix <var>string</var></code></h4>
1692<p>
1693 Adds a suffix to the configuration short name, which is used to determine the
1694 output directory. Setting this option to different values puts the files into
1695 different directories, for example to improve cache hit rates for builds that
1696 otherwise clobber each others output files, or to keep the output files around
1697 for comparisons.
1698</p>
1699
1700<h4 id='flag--default_visibility'><code class='flag'>--default_visibility=<var>(private|public)</var></code></h4>
1701<p>
Googler0e692fb2021-01-22 18:38:24 +01001702 Temporary flag for testing bazel default visibility changes. Not intended for general use
David Chen8fe82a32016-08-24 10:55:41 +00001703 but documented for completeness' sake.
1704</p>
1705
Googler23471692017-06-02 11:26:16 -04001706<h4 id='flag--use_action_cache'><code class='flag'>--[no]use_action_cache</code></h4>
1707<p>
Googler0e692fb2021-01-22 18:38:24 +01001708 This option is enabled by default. If disabled, Bazel will not use its local action cache.
Googler23471692017-06-02 11:26:16 -04001709 Disabling the local action cache saves memory and disk space for clean builds, but will make
1710 incremental builds slower.
1711</p>
1712
adonovan329ec6d2020-02-06 15:34:49 -08001713<h4 id='flag--starlark_cpu_profile'><code class='flag'>--starlark_cpu_profile=<i>file</i></code></h4>
1714<p>
1715 This flag, whose value is the name of a file, causes Bazel to gather
1716 statistics about CPU usage by all Starlark threads,
1717 and write the profile, in <a href='https://github.com/google/pprof'>pprof</a> format,
1718 to the named file.
1719
1720 Use this option to help identify Starlark functions that
1721 make loading and analysis slow due to excessive computation. For example:
1722</p>
1723<pre>
1724$ bazel build --nobuild --starlark_cpu_profile=/tmp/pprof.gz my/project/...
1725$ pprof /tmp/pprof.gz
1726(pprof) top
1727Type: CPU
1728Time: Feb 6, 2020 at 12:06pm (PST)
1729Duration: 5.26s, Total samples = 3.34s (63.55%)
1730Showing nodes accounting for 3.34s, 100% of 3.34s total
1731 flat flat% sum% cum cum%
1732 1.86s 55.69% 55.69% 1.86s 55.69% sort_source_files
1733 1.02s 30.54% 86.23% 1.02s 30.54% expand_all_combinations
1734 0.44s 13.17% 99.40% 0.44s 13.17% range
1735 0.02s 0.6% 100% 3.34s 100% sorted
1736 0 0% 100% 1.38s 41.32% my/project/main/BUILD
1737 0 0% 100% 1.96s 58.68% my/project/library.bzl
1738 0 0% 100% 3.34s 100% main
1739</pre>
1740<p>
1741 For different views of the same data, try the <code>pprof</code> commands <code>svg</code>,
1742 <code>web</code>, and <code>list</code>.
1743</p>
1744
David Chen8fe82a32016-08-24 10:55:41 +00001745<h2 id='bazel-releng'>Using Bazel for releases</h2>
1746<p>
1747 Bazel is used both by software engineers during the development
1748 cycle, and by release engineers when preparing binaries for deployment
Googler0e692fb2021-01-22 18:38:24 +01001749 to production. This section provides a list of tips for release
David Chen8fe82a32016-08-24 10:55:41 +00001750 engineers using Bazel.
1751
1752</p>
1753
1754<h3>Significant options</h3>
1755
1756<p>
1757 When using Bazel for release builds, the same issues arise as for
1758 other scripts that perform a build, so you should read
Googler59830672019-01-03 10:33:49 -08001759 the <a href='guide.html#scripting'>scripting</a> section of this manual.
David Chen8fe82a32016-08-24 10:55:41 +00001760 In particular, the following options are strongly recommended:
1761</p>
1762<ul>
Vladimir Chebotarev66885ff2018-12-04 14:08:16 -08001763 <li><a href='guide.html#bazelrc'><code class='flag'>--bazelrc=/dev/null</code></a></li>
ccalvarinaf8b7722018-06-06 11:54:00 -07001764 <li><a href='#flag--keep_state_after_build'><code class='flag'>--nokeep_state_after_build</code></a></li>
David Chen8fe82a32016-08-24 10:55:41 +00001765</ul>
1766
1767<p>
brandjoncf0390b2020-02-20 10:37:40 -08001768 These options are also important:
David Chen8fe82a32016-08-24 10:55:41 +00001769</p>
1770
1771<ul>
1772
1773 <li><a href='#flag--package_path'><code class='flag'>--package_path</code></a></li>
1774 <li><a href='#flag--symlink_prefix'><code class='flag'>--symlink_prefix</code></a>:
1775 for managing builds for multiple configurations,
1776 it may be convenient to distinguish each build
Googler0e692fb2021-01-22 18:38:24 +01001777 with a distinct identifier, e.g. "64bit" vs. "32bit". This option
David Chen8fe82a32016-08-24 10:55:41 +00001778 differentiates the <code>bazel-bin</code> (etc.) symlinks.
1779 </li>
1780</ul>
1781
brandjoncf0390b2020-02-20 10:37:40 -08001782<h2 id='test'>Running tests</h2>
David Chen8fe82a32016-08-24 10:55:41 +00001783<p>
1784 To build and run tests with bazel, type <code>bazel test</code> followed by
1785 the name of the test targets.
1786</p>
1787<p>
1788 By default, this command performs simultaneous build and test
1789 activity, building all specified targets (including any non-test
1790 targets specified on the command line) and testing
1791 <code>*_test</code> and <code>test_suite</code> targets as soon as
1792 their prerequisites are built, meaning that test execution is
1793 interleaved with building. Doing so usually results in significant
1794 speed gains.
1795
1796</p>
1797
1798<h3>Options for <code>bazel test</code></h3>
1799
1800<h4 id="flag--cache_test_results"><code class='flag'>--cache_test_results=(yes|no|auto)</code> (<code>-t</code>)</h4>
1801<p>
1802 If this option is set to 'auto' (the default) then Bazel will only rerun a test if any of the
1803 following conditions applies:
1804</p>
1805<ul>
1806 <li>Bazel detects changes in the test or its dependencies</li>
1807 <li>the test is marked as <code>external</code></li>
1808 <li>multiple test runs were requested with <code class='flag'>--runs_per_test</code></li>
1809 <li>the test failed.</li>
1810</ul>
1811<p>
1812 If 'no', all tests will be executed unconditionally.
1813</p>
1814<p>
1815 If 'yes', the caching behavior will be the same as auto
1816 except that it may cache test failures and test runs with
1817 <code class='flag'>--runs_per_test</code>.
1818</p>
1819<p>
1820 Note that test results are <em>always</em> saved in Bazel's output tree,
1821 regardless of whether this option is enabled, so
1822 you needn't have used <code class='flag'>--cache_test_results</code> on the
1823 prior run(s) of <code>bazel test</code> in order to get cache hits.
1824 The option only affects whether Bazel will <em>use</em> previously
1825 saved results, not whether it will save results of the current run.
1826</p>
1827<p>
1828 Users who have enabled this option by default in
1829 their <code>.bazelrc</code> file may find the
1830 abbreviations <code>-t</code> (on) or <code>-t-</code> (off)
1831 convenient for overriding the default on a particular run.
1832</p>
1833
1834<h4 id="flag--check_tests_up_to_date"><code class='flag'>--check_tests_up_to_date</code></h4>
1835<p>
1836 This option tells Bazel not to run the tests, but to merely check and report
Googler0e692fb2021-01-22 18:38:24 +01001837 the cached test results. If there are any tests which have not been
David Chen8fe82a32016-08-24 10:55:41 +00001838 previously built and run, or whose tests results are out-of-date (e.g. because
1839 the source code or the build options have changed), then Bazel will report
1840 an error message ("test result is not up-to-date"), will record the test's
1841 status as "NO STATUS" (in red, if color output is enabled), and will return
1842 a non-zero exit code.
1843</p>
1844<p>
1845 This option also implies
1846 <code><a href="#flag--check_up_to_date">--check_up_to_date</a></code> behavior.
1847</p>
1848<p>
1849 This option may be useful for pre-submit checks.
1850</p>
1851
1852<h4 id="flag--test_verbose_timeout_warnings"><code class='flag'>--test_verbose_timeout_warnings</code></h4>
1853<p>
1854 This option tells Bazel to explicitly warn the user if a test's timeout is
Googler0e692fb2021-01-22 18:38:24 +01001855significantly longer than the test's actual execution time. While a test's
David Chen8fe82a32016-08-24 10:55:41 +00001856timeout should be set such that it is not flaky, a test that has a highly
1857over-generous timeout can hide real problems that crop up unexpectedly.
1858</p>
1859<p>
1860For instance, a test that normally executes in a minute or two should not have
1861a timeout of ETERNAL or LONG as these are much, much too generous.
1862
1863 This option is useful to help users decide on a good timeout value or
1864 sanity check existing timeout values.
1865</p>
1866<p>
1867Note that each test shard is allotted the timeout of the entire
Googler0e692fb2021-01-22 18:38:24 +01001868<code>XX_test</code> target. Using this option does not affect a test's timeout
David Chen8fe82a32016-08-24 10:55:41 +00001869value, merely warns if Bazel thinks the timeout could be restricted further.
1870</p>
1871
1872<h4 id='flag--test_keep_going'><code class='flag'>--[no]test_keep_going</code></h4>
1873<p>
1874 By default, all tests are run to completion. If this flag is disabled,
1875 however, the build is aborted on any non-passing test. Subsequent build steps
1876 and test invocations are not run, and in-flight invocations are canceled.
1877 Do not specify both <code class='flag'>--notest_keep_going</code> and
1878 <code class='flag'>--keep_going</code>.
1879</p>
1880
1881<h4 id='flag--flaky_test_attempts'><code class='flag'>--flaky_test_attempts <var>attempts</var></code></h4>
1882<p>
1883 This option specifies the maximum number of times a test should be attempted
1884 if it fails for any reason. A test that initially fails but eventually
1885 succeeds is reported as <code>FLAKY</code> on the test summary. It is,
1886 however, considered to be passed when it comes to identifying Bazel exit code
1887 or total number of passed tests. Tests that fail all allowed attempts are
1888 considered to be failed.
1889</p>
1890<p>
1891 By default (when this option is not specified, or when it is set to
1892 &quot;default&quot;), only a single attempt is allowed for regular tests, and
1893 3 for test rules with the <code>flaky</code> attribute set. You can specify
1894 an integer value to override the maximum limit of test attempts. Bazel allows
1895 a maximum of 10 test attempts in order to prevent abuse of the system.
1896</p>
1897
1898<h4 id='flag--runs_per_test'><code class='flag'>--runs_per_test <var>[regex@]number</var></code></h4>
1899<p>
1900 This option specifies the number of times each test should be executed. All
1901 test executions are treated as separate tests (e.g. fallback functionality
1902 will apply to each of them independently).
1903</p>
1904<p>
1905 The status of a target with failing runs depends on the value of the
1906 <code>--runs_per_test_detects_flakes</code> flag:
1907</p>
1908<ul>
1909 <li>If absent, any failing run causes the entire test to fail.</li>
1910 <li>If present and two runs from the same shard return PASS and FAIL, the test
1911 will receive a status of flaky (unless other failing runs cause it to
1912 fail).</li>
1913</ul>
1914
1915<p>
1916 If a single number is specified, all tests will run that many times.
1917 Alternatively, a regular expression may be specified using the syntax
1918 regex@number. This constrains the effect of --runs_per_test to targets
1919 which match the regex (e.g. "--runs_per_test=^//pizza:.*@4" runs all tests
1920 under //pizza/ 4 times).
1921 This form of --runs_per_test may be specified more than once.
1922</p>
1923
1924<h4 id='flag--runs_per_test_detects_flakes'><code
1925 class='flag'>--[no]runs_per_test_detects_flakes</code></h4>
1926<p>
1927 If this option is specified (by default it is not), Bazel will detect flaky
1928 test shards through --runs_per_test. If one or more runs for a single shard
1929 fail and one or more runs for the same shard pass, the target will be
1930 considered flaky with the flag. If unspecified, the target will report a
1931 failing status.
1932</p>
1933
1934<h4 id='flag--test_summary'><code class='flag'>--test_summary <var>output_style</var></code></h4>
1935<p>
1936 Specifies how the test result summary should be displayed.
1937</p>
1938<ul>
1939 <li><code>short</code> prints the results of each test along with the name of
1940 the file containing the test output if the test failed. This is the default
1941 value.
1942 </li>
1943 <li><code>terse</code> like <code>short</code>, but even shorter: only print
1944 information about tests which did not pass.
1945 </li>
1946 <li><code>detailed</code> prints each individual test case that failed, not
1947 only each test. The names of test output files are omitted.
1948 </li>
1949 <li><code>none</code> does not print test summary.
1950 </li>
1951</ul>
1952
1953<h4 id='flag--test_output'><code class='flag'>--test_output <var>output_style</var></code></h4>
1954<p>
1955 Specifies how test output should be displayed:
1956</p>
1957<ul>
1958 <li><code>summary</code> shows a summary of whether each test passed or
1959 failed. Also shows the output log file name for failed tests. The summary
1960 will be printed at the end of the build (during the build, one would see
1961 just simple progress messages when tests start, pass or fail).
1962 This is the default behavior.
1963 </li>
1964 <li><code>errors</code> sends combined stdout/stderr output from failed tests
1965 only into the stdout immediately after test is completed, ensuring that
1966 test output from simultaneous tests is not interleaved with each other.
1967 Prints a summary at the build as per summary output above.
1968 </li>
1969 <li><code>all</code> is similar to <code>errors</code> but prints output for
1970 all tests, including those which passed.
1971 </li>
1972 <li><code>streamed</code> streams stdout/stderr output from each test in
1973 real-time.
1974
1975 </li>
1976</ul>
1977
1978<h4 id='flag--java_debug'><code class='flag'>--java_debug</code></h4>
1979<p>
1980 This option causes the Java virtual machine of a java test to wait for a connection from a
1981 JDWP-compliant debugger before starting the test. This option implies --test_output=streamed.
1982</p>
1983
1984<h4 id='flag--verbose_test_summary'><code class='flag'>--[no]verbose_test_summary</code></h4>
1985<p>
1986 By default this option is enabled, causing test times and other additional
1987 information (such as test attempts) to be printed to the test summary. If
1988 <code class='flag'>--noverbose_test_summary</code> is specified, test summary will
1989 include only test name, test status and cached test indicator and will
1990 be formatted to stay within 80 characters when possible.
1991</p>
1992
1993<h4 id='flag--test_tmpdir'><code class='flag'>--test_tmpdir <var>path</var></code></h4>
1994<p>
1995 Specifies temporary directory for tests executed locally. Each test will be
1996 executed in a separate subdirectory inside this directory. The directory will
1997 be cleaned at the beginning of the each <code>bazel test</code> command.
1998 By default, bazel will place this directory under Bazel output base directory.
1999 Note that this is a directory for running tests, not storing test results
2000 (those are always stored under the <code>bazel-out</code> directory).
2001</p>
2002
2003<h4 id='flag--test_timeout'>
2004 <code class='flag'>--test_timeout
2005 <var>seconds</var></code>
2006 OR
2007 <code class='flag'>--test_timeout
2008 <var>seconds</var>,<var>seconds</var>,<var>seconds</var>,<var>seconds</var>
2009 </code>
2010</h4>
2011<p>
2012 Overrides the timeout value for all tests by using specified number of
2013 seconds as a new timeout value. If only one value is provided, then it will
2014 be used for all test timeout categories.
2015 </p>
2016 <p>
2017 Alternatively, four comma-separated values may be provided, specifying
2018 individual timeouts for short, moderate, long and eternal tests (in that
2019 order).
2020 In either form, zero or a negative value for any of the test sizes will
2021 be substituted by the default timeout for the given timeout categories as
2022 defined by the page
2023 <a href="test-encyclopedia.html">Writing Tests</a>.
2024 By default, Bazel will use these timeouts for all tests by
2025 inferring the timeout limit from the test's size whether the size is
2026 implicitly or explicitly set.
2027</p>
2028<p>
2029 Tests which explicitly state their timeout category as distinct from their
2030 size will receive the same value as if that timeout had been implicitly set by
Googler0e692fb2021-01-22 18:38:24 +01002031 the size tag. So a test of size 'small' which declares a 'long' timeout will
David Chen8fe82a32016-08-24 10:55:41 +00002032 have the same effective timeout that a 'large' tests has with no explicit
2033 timeout.
2034</p>
2035
2036<h4 id='flag--test_arg'><code class='flag'>--test_arg <var>arg</var></code></h4>
2037<p>
Michael Staib3df106e2017-02-23 23:08:40 +00002038 Passes command-line options/flags/arguments to each test process. This
David Chen8fe82a32016-08-24 10:55:41 +00002039 option can be used multiple times to pass several arguments, e.g.
2040 <code class='flag'>--test_arg=--logtostderr --test_arg=--v=3</code>.
2041</p>
2042
2043<h4 id='flag--test_env'><code class='flag'>--test_env <var>variable</var>=<i>value</i></code>
2044 OR
2045 <code class='flag'>--test_env <var>variable</var></code></h4>
2046<p>
2047 Specifies additional variables that must be injected into the test
2048 environment for each test. If <var>value</var> is not specified it will be
2049 inherited from the shell environment used to start the <code>bazel test</code>
2050 command.
2051</p>
2052<p>
2053 The environment can be accessed from within a test by using
2054 <code>System.getenv("var")</code> (Java),
2055 <code>getenv("var")</code> (C or C++),
2056
2057</p>
2058
2059<h4 id="flag--run_under"><code class='flag'>--run_under=<var>command-prefix</var></code></h4>
2060<p>
2061 This specifies a prefix that the test runner will insert in front
Googler0e692fb2021-01-22 18:38:24 +01002062 of the test command before running it. The
David Chen8fe82a32016-08-24 10:55:41 +00002063 <var>command-prefix</var> is split into words using Bourne shell
2064 tokenization rules, and then the list of words is prepended to the
2065 command that will be executed.
2066</p>
2067<p>
Googlera0f79b12018-11-29 06:31:16 -08002068 If the first word is a fully-qualified label (i.e. starts with
David Chen8fe82a32016-08-24 10:55:41 +00002069 <code>//</code>) it is built. Then the label is substituted by the
2070 corresponding executable location that is prepended to the command
2071 that will be executed along with the other words.
2072</p>
2073
2074<p>
2075Some caveats apply:
2076</p>
2077<ul>
2078 <li>
2079 The PATH used for running tests may be different than the PATH in your environment,
2080 so you may need to use an <b>absolute path</b> for the <code class='flag'>--run_under</code>
2081 command (the first word in <var>command-prefix</var>).
2082 </li>
2083 <li>
2084 <b><code>stdin</code> is not connected</b>, so <code class='flag'>--run_under</code>
2085 can't be used for interactive commands.
2086 </li>
2087
2088</ul>
2089<p>
2090Examples:
2091</p>
2092<pre>
David Chen8fe82a32016-08-24 10:55:41 +00002093 --run_under=/usr/bin/strace
2094 --run_under='/usr/bin/strace -c'
ckennellyaee19e12020-04-15 10:15:47 -07002095 --run_under=/usr/bin/valgrind
David Chen8fe82a32016-08-24 10:55:41 +00002096 --run_under='/usr/bin/valgrind --quiet --num-callers=20'
David Chen8fe82a32016-08-24 10:55:41 +00002097</pre>
2098
2099<h4>Test selection</h4>
2100<p>
2101 As documented under <a href='#output-selection-options'>Output selection options</a>,
2102 you can filter tests by <a href='#flag--test_size_filters'>size</a>,
2103 <a href='#flag--test_timeout_filters'>timeout</a>,
2104 <a href='#flag--test_tag_filters'>tag</a>, or
2105 <a href='#flag--test_lang_filters'>language</a>. A convenience
2106 <a href='#flag--test_filter'>general name filter</a> can forward particular
2107 filter args to the test runner.
2108</p>
2109
kchodorowfae5c742017-03-28 16:53:24 +00002110<h4 id="other_options_for_bazel_test">Other options for <code>bazel test</code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00002111<p>
2112 The syntax and the remaining options are exactly like
Googler59830672019-01-03 10:33:49 -08002113 <a href='guide.html#build'>bazel build</a>.
David Chen8fe82a32016-08-24 10:55:41 +00002114</p>
2115
2116
brandjoncf0390b2020-02-20 10:37:40 -08002117<h3 id='clean'>Cleaning build outputs</h3>
laurentlbc85de9c2018-11-05 08:30:36 -08002118
2119<h4>The <code>clean</code> command</h4>
2120
2121<p>
2122 Bazel has a <code>clean</code> command, analogous to that of Make.
2123 It deletes the output directories for all build configurations performed
2124 by this Bazel instance, or the entire working tree created by this
2125 Bazel instance, and resets internal caches. If executed without any
2126 command-line options, then the output directory for all configurations
2127 will be cleaned.
2128</p>
2129
2130<p>Recall that each Bazel instance is associated with a single workspace, thus the
2131 <code>clean</code> command will delete all outputs from all builds you've done
2132 with that Bazel instance in that workspace.
2133</p>
2134<p>
2135 To completely remove the entire working tree created by a Bazel
2136 instance, you can specify the <code class='flag'>--expunge</code> option. When
2137 executed with <code class='flag'>--expunge</code>, the clean command simply
2138 removes the entire output base tree which, in addition to the build
2139 output, contains all temp files created by Bazel. It also
2140 stops the Bazel server after the clean, equivalent to the <a
2141 href='#shutdown'><code>shutdown</code></a> command. For example, to
2142 clean up all disk and memory traces of a Bazel instance, you could
2143 specify:
2144</p>
2145<pre>
2146 % bazel clean --expunge
2147</pre>
2148<p>
2149 Alternatively, you can expunge in the background by using
2150 <code class='flag'>--expunge_async</code>. It is safe to invoke a Bazel command
2151 in the same client while the asynchronous expunge continues to run.
2152 Note, however, that this may introduce IO contention.
2153</p>
2154
2155<p>
2156 The <code>clean</code> command is provided primarily as a means of
2157 reclaiming disk space for workspaces that are no longer needed.
2158 However, we recognize that Bazel's incremental rebuilds might not be
2159 perfect; <code>clean</code> may be used to recover a consistent
2160 state when problems arise.
2161</p>
2162<p>
2163 Bazel's design is such that these problems are fixable; we consider
Googler0e692fb2021-01-22 18:38:24 +01002164 such bugs a high priority, and will do our best fix them. If you
laurentlbc85de9c2018-11-05 08:30:36 -08002165 ever find an incorrect incremental build, please file a bug report.
2166 We encourage developers to get out of the habit of
2167 using <code>clean</code> and into that of reporting bugs in the
2168 tools.
2169</p>
2170
David Chen8fe82a32016-08-24 10:55:41 +00002171
brandjoncf0390b2020-02-20 10:37:40 -08002172<h2 id='run'>Running executables</h2>
David Chen8fe82a32016-08-24 10:55:41 +00002173<p>
2174 The <code>bazel run</code> command is similar to <code>bazel build</code>, except
Googler0e692fb2021-01-22 18:38:24 +01002175 it is used to build <em>and run</em> a single target. Here is a typical session:
David Chen8fe82a32016-08-24 10:55:41 +00002176</p>
2177<pre>
2178 % bazel run -- java/myapp:myapp --arg1 --arg2
2179 Welcome to Bazel
2180 INFO: Loading package: java/myapp
2181 INFO: Loading package: foo/bar
2182 INFO: Loading complete. Analyzing...
2183 INFO: Found 1 target...
2184 ...
2185 Target //java/myapp:myapp up-to-date:
2186 bazel-bin/java/myapp:myapp
2187 INFO: Elapsed time: 0.638s, Critical Path: 0.34s
2188
2189 INFO: Running command line: bazel-bin/java/myapp:myapp --arg1 --arg2
2190 Hello there
2191 $EXEC_ROOT/java/myapp/myapp
2192 --arg1
2193 --arg2
2194</pre>
2195
2196<p>
Googler0e692fb2021-01-22 18:38:24 +01002197 Note the use of the <code>--</code>. This is needed so that Bazel
David Chen8fe82a32016-08-24 10:55:41 +00002198 does not interpret <code>--arg1</code> and <code>--arg2</code> as
2199 Bazel options, but rather as part of the command line for running the binary.
2200 (The program being run simply says hello and prints out its args.)
2201</p>
2202
diamondme42913a2019-01-24 16:11:30 -08002203<p><code>bazel run</code> is similar, but not identical, to directly invoking
lberki749e8052019-02-12 04:23:43 -08002204the binary built by Bazel and its behavior is different depending on whether the
2205binary to be invoked is a test or not.
2206
2207When the binary is not a test, the current working directory will be the
2208runfiles tree of the binary.
2209
2210When the binary is a test, the current working directory will be the exec root
2211and a good-faith attempt is made to replicate the environment tests are usually
2212run in. The emulation is not perfect, though, and tests that have multiple
2213shards cannot be run this way (the
2214<code>--test_sharding_strategy=disabled</code> command line option can be used
2215to work around this)
2216
2217The following extra environment variables are also available to the binary:
2218<ul>
2219 <li>
2220 <code>BUILD_WORKSPACE_DIRECTORY</code>: the root of the workspace where the
2221 build was run.
2222 </li>
2223 <li>
2224 <code>BUILD_WORKING_DIRECTORY</code>: the current working directory where
2225 Bazel was run from.
2226 </li>
2227</ul>
2228
2229These can be used, for example, to interpret file names on the command line in
2230a user-friendly way.
diamondme42913a2019-01-24 16:11:30 -08002231
David Chen8fe82a32016-08-24 10:55:41 +00002232<h3>Options for <code>bazel run</code></h3>
2233
2234<h4 id='flag--run_under_run'><code class='flag'>--run_under=<var>command-prefix</var></code></h4>
2235<p>
2236 This has the same effect as the <code class='flag'>--run_under</code> option for
2237 <code>bazel test</code> (<a href='#flag--run_under'>see above</a>),
2238 except that it applies to the command being run by <code>bazel
2239 run</code> rather than to the tests being run by <code>bazel test</code>
2240 and cannot run under label.
2241</p>
2242
2243<h3>Executing tests</h3>
2244
2245<p>
2246 <code>bazel run</code> can also execute test binaries, which has the effect of
lberki5a2238d2018-05-18 03:15:32 -07002247 running the test in a close approximation of the environment described at
2248 <a href='test-encyclopedia.html'>Writing Tests</a>. Note that none of the
Googlere5197f72020-02-25 16:38:17 -08002249 <code>--test_*</code> arguments have an effect when running a test in this manner except
lberki5a2238d2018-05-18 03:15:32 -07002250 <code>--test_arg</code> .
David Chen8fe82a32016-08-24 10:55:41 +00002251</p>
2252
brandjoncf0390b2020-02-20 10:37:40 -08002253<h2 id='query'>Querying the dependency graph</h2>
David Chen8fe82a32016-08-24 10:55:41 +00002254
2255<p>
2256 Bazel includes a query language for asking questions about the
Googler0e692fb2021-01-22 18:38:24 +01002257 dependency graph used during the build. The query language is used
juliexxiaa8082fc2018-03-15 09:58:51 -07002258 by two commands: query and cquery. The major difference between the
Googler59830672019-01-03 10:33:49 -08002259 two commands is that query runs after the <a href='guide.html#loading-phase'>loading phase</a>
2260 and cquery runs after the <a href='guide.html#analysis-phase'>analysis phase</a>. These tools are an
David Chen8fe82a32016-08-24 10:55:41 +00002261 invaluable aid to many software engineering tasks.
2262</p>
2263<p>
2264 The query language is based on the idea of
2265 algebraic operations over graphs; it is documented in detail in
2266
2267 <a href="query.html">Bazel Query Reference</a>.
2268 Please refer to that document for reference, for
2269 examples, and for query-specific command-line options.
2270</p>
2271
2272<p>
2273 The query tool accepts several command-line
Googler0e692fb2021-01-22 18:38:24 +01002274 option. <code class='flag'>--output</code> selects the output format.
David Chen8fe82a32016-08-24 10:55:41 +00002275 <code class='flag'>--[no]keep_going</code> (disabled by default) causes the query
2276 tool to continue to make progress upon errors; this behavior may be
2277 disabled if an incomplete result is not acceptable in case of errors.
2278</p>
2279<p>
wtroberts5a0cf9b2019-09-06 07:55:15 -07002280 The <code class='flag'>--[no]tool_deps</code> option,
2281 enabled by default, causes dependencies in non-target configurations to be included in the
2282 dependency graph over which the query operates.
David Chen8fe82a32016-08-24 10:55:41 +00002283
2284</p>
2285<p>
2286 The <code class='flag'>--[no]implicit_deps</code> option, enabled by default, causes
2287 implicit dependencies to be included in the dependency graph over which the query operates. An
2288 implicit dependency is one that is not explicitly specified in the BUILD file
2289 but added by bazel.
2290</p>
2291<p>
2292 Example: "Show the locations of the definitions (in BUILD files) of
2293 all genrules required to build all the tests in the PEBL tree."
2294</p>
2295<pre>
2296 bazel query --output location 'kind(genrule, deps(kind(".*_test rule", foo/bar/pebl/...)))'
2297</pre>
2298
brandjoncf0390b2020-02-20 10:37:40 -08002299<h2 id='aquery'>Querying the action graph</h2>
twerthe7fcab32018-08-09 08:03:41 -07002300
2301<b>Caution</b>: The aquery command is still experimental and its API will change.
2302
2303<p>
2304 The <code>aquery</code> command allows you to query for actions in your build graph.
2305 It operates on the post-analysis configured target graph and exposes
2306 information about actions, artifacts and their relationships.
2307</p>
2308
2309<p>
2310 The tool accepts several command-line options.
twerth420b9532018-12-14 06:16:58 -08002311 <code class='flag'>--output</code> selects the output format. The default output format
2312 (<code>text</code>) is human-readable, use <code>proto</code> or <code>textproto</code> for
2313 machine-readable format.
twerthe7fcab32018-08-09 08:03:41 -07002314 Notably, the aquery command runs on top of a regular Bazel build and inherits
2315 the set of options available during a build.
2316</p>
2317
2318<p>
2319 It supports the same set of functions that is also available to traditional
2320 <code>query</code> but <code>siblings</code>, <code>buildfiles</code> and
2321 <code>tests</code>.
2322</p>
David Chen8fe82a32016-08-24 10:55:41 +00002323
leba367d42d2019-01-10 02:30:47 -08002324<p>
2325 More details on aquery can be found <a href="aquery.html">here</a>.
2326</p>
2327
brandjoncf0390b2020-02-20 10:37:40 -08002328<h2 id='misc'>Miscellaneous commands and options</h2>
David Chen8fe82a32016-08-24 10:55:41 +00002329
brandjoncf0390b2020-02-20 10:37:40 -08002330<h3 id='help'><code>help</code></h3>
David Chen8fe82a32016-08-24 10:55:41 +00002331
2332<p>
Googler0e692fb2021-01-22 18:38:24 +01002333 The <code>help</code> command provides on-line help. By default, it
David Chen8fe82a32016-08-24 10:55:41 +00002334 shows a summary of available commands and help topics, as shown in
Googler59830672019-01-03 10:33:49 -08002335 the <a href='guide.html#overview'><i>Bazel overview</i></a> section above.
David Chen8fe82a32016-08-24 10:55:41 +00002336 Specifying an argument displays detailed help for a particular
Googler0e692fb2021-01-22 18:38:24 +01002337 topic. Most topics are Bazel commands, e.g. <code>build</code>
David Chen8fe82a32016-08-24 10:55:41 +00002338 or <code>query</code>, but there are some additional help topics
2339 that do not correspond to commands.
2340</p>
2341
2342<h4 id='flag--long'><code class='flag'>--[no]long</code> (<code>-l</code>)</h4>
2343<p>
2344 By default, <code>bazel help [<var>topic</var>]</code> prints only a
Googler0e692fb2021-01-22 18:38:24 +01002345 summary of the relevant options for a topic. If
David Chen8fe82a32016-08-24 10:55:41 +00002346 the <code class='flag'>--long</code> option is specified, the type, default value
2347 and full description of each option is also printed.
2348</p>
2349
brandjoncf0390b2020-02-20 10:37:40 -08002350<h3 id='shutdown'><code>shutdown</code></h3>
David Chen8fe82a32016-08-24 10:55:41 +00002351
2352<p>
Googler59830672019-01-03 10:33:49 -08002353 Bazel server processes (see <a href='guide.html#client/server'>Client/server
David Chen8fe82a32016-08-24 10:55:41 +00002354 implementation</a>) may be stopped by using the <code>shutdown</code>
Googler0e692fb2021-01-22 18:38:24 +01002355 command. This command causes the Bazel server to exit as soon as it
David Chen8fe82a32016-08-24 10:55:41 +00002356 becomes idle (i.e. after the completion of any builds or other
2357 commands that are currently in progress).
2358
2359 Bazel servers stop themselves after an idle timeout, so this command
2360 is rarely necessary; however, it can be useful in scripts when it is
2361 known that no further builds will occur in a given workspace.
2362</p>
2363<p>
2364 <code>shutdown</code> accepts one
2365 option, <code class='flag'>--iff_heap_size_greater_than <i>n</i></code>, which
Googler0e692fb2021-01-22 18:38:24 +01002366 requires an integer argument (in MB). If specified, this makes the shutdown
2367 conditional on the amount of memory already consumed. This is
David Chen8fe82a32016-08-24 10:55:41 +00002368 useful for scripts that initiate a lot of builds, as any memory
2369 leaks in the Bazel server could cause it to crash spuriously on
2370 occasion; performing a conditional restart preempts this condition.
2371</p>
2372
brandjoncf0390b2020-02-20 10:37:40 -08002373<h3 id='info'><code>info</code></h3>
David Chen8fe82a32016-08-24 10:55:41 +00002374
2375<p>
2376 The <code>info</code> command prints various values associated with
2377 the Bazel server instance, or with a specific build configuration.
2378 (These may be used by scripts that drive a build.)
2379</p>
2380
2381<p>
2382 The <code>info</code> command also permits a single (optional)
2383 argument, which is the name of one of the keys in the list below.
2384 In this case, <code>bazel info <var>key</var></code> will print only
Googler0e692fb2021-01-22 18:38:24 +01002385 the value for that one key. (This is especially convenient when
David Chen8fe82a32016-08-24 10:55:41 +00002386 scripting Bazel, as it avoids the need to pipe the result
2387 through <code>sed -ne /key:/s/key://p</code>:
2388</p>
2389
2390<h4>Configuration-independent data</h4>
2391<ul>
2392 <li><code>release</code>: the release label for this Bazel
2393 instance, or "development version" if this is not a released
2394 binary.
2395 </li>
2396 <li><code>workspace</code> the absolute path to the base workspace
2397 directory.
2398 </li>
2399 <li><code>install_base</code>: the absolute path to the installation
2400 directory used by this Bazel instance for the current user. Bazel
2401 installs its internally required executables below this directory.
2402
2403 </li>
2404 <li><code>output_base</code>: the absolute path to the base output
2405 directory used by this Bazel instance for the current user and
2406 workspace combination. Bazel puts all of its scratch and build
2407 output below this directory.
2408 </li>
2409 <li><code>execution_root</code>: the absolute path to the execution
2410 root directory under output_base. This directory is the root for all files
2411 accessible to commands executed during the build, and is the working
2412 directory for those commands. If the workspace directory is writable, a
2413 symlink named
2414
2415 <code>bazel-&lt;workspace&gt;</code>
2416 is placed there pointing to this directory.
2417 </li>
2418 <li><code>output_path</code>: the absolute path to the output
2419 directory beneath the execution root used for all files actually
2420 generated as a result of build commands. If the workspace directory is
2421 writable, a symlink named <code>bazel-out</code> is placed there pointing
2422 to this directory.
nharmataa3a7dad2020-03-30 10:42:05 -07002423 </li>
David Chen8fe82a32016-08-24 10:55:41 +00002424 <li><code>server_pid</code>: the process ID of the Bazel server
nharmataa3a7dad2020-03-30 10:42:05 -07002425 process.
2426 </li>
2427 <li><code>server_log</code>: the absolute path to the Bazel server's debug log file.
2428 This file contains debugging information for all commands over the lifetime of the
2429 Bazel server, and is intended for human consumption by Bazel developers and power users.
2430 </li>
David Chen8fe82a32016-08-24 10:55:41 +00002431 <li><code>command_log</code>: the absolute path to the command log file;
2432 this contains the interleaved stdout and stderr streams of the most recent
2433 Bazel command. Note that running <code>bazel info</code> will overwrite the
2434 contents of this file, since it then becomes the most recent Bazel command.
2435 However, the location of the command log file will not change unless you
2436 change the setting of the <code class='flag'>--output_base</code> or
2437 <code class='flag'>--output_user_root</code> options.
2438 </li>
2439
2440 <li><code>used-heap-size</code>,
Googler82085c42018-08-16 08:01:39 -07002441 <code>committed-heap-size</code>,
David Chen8fe82a32016-08-24 10:55:41 +00002442 <code>max-heap-size</code>: reports various JVM heap size
Googler0e692fb2021-01-22 18:38:24 +01002443 parameters. Respectively: memory currently used, memory currently
David Chen8fe82a32016-08-24 10:55:41 +00002444 guaranteed to be available to the JVM from the system, maximum
2445 possible allocation.
2446 </li>
2447 <li><code>gc-count</code>, <code>gc-time</code>: The cumulative count of
2448 garbage collections since the start of this Bazel server and the time spent
2449 to perform them. Note that these values are not reset at the start of every
2450 build.
2451 </li>
2452 <li><code>package_path</code>: A colon-separated list of paths which would be
2453 searched for packages by bazel. Has the same format as the
2454 <code class='flag'>--package_path</code> build command line argument.
2455 </li>
2456</ul>
2457<p>
2458 Example: the process ID of the Bazel server.
2459</p>
2460<pre>% bazel info server_pid
24611285
2462</pre>
2463
2464<h4>Configuration-specific data</h4>
2465<p>
2466 These data may be affected by the configuration options passed
2467 to <code>bazel info</code>, for
2468 example <code class='flag'>--cpu</code>, <code class='flag'>--compilation_mode</code>,
Googler0e692fb2021-01-22 18:38:24 +01002469 etc. The <code>info</code> command accepts all
David Chen8fe82a32016-08-24 10:55:41 +00002470 the <a href='#analysis-options'>options that control dependency
2471 analysis</a>, since some of these determine the location of the
2472 output directory of a build, the choice of compiler, etc.
2473</p>
2474<ul>
2475 <li>
2476 <code>bazel-bin</code>, <code>bazel-testlogs</code>,
2477 <code>bazel-genfiles</code>: reports the absolute path to
2478 the <code>bazel-*</code> directories in which programs generated by the
2479 build are located. This is usually, though not always, the same as
2480 the <code>bazel-*</code> symlinks created in the base workspace directory after a
2481 successful build. However, if the workspace directory is read-only,
2482 no <code>bazel-*</code> symlinks can be created. Scripts that use
2483 the value reported by <code>bazel info</code>, instead of assuming the
2484 existence of the symlink, will be more robust.
2485 </li>
2486 <li>
2487 The complete
2488 <a href='be/make-variables.html'
2489 >"Make" environment</a>. If the <code class='flag'>--show_make_env</code> flag is
2490 specified, all variables in the current configuration's "Make" environment
2491 are also displayed (e.g. <code>CC</code>, <code>GLIBC_VERSION</code>, etc).
2492 These are the variables accessed using the <code>$(CC)</code>
2493 or <code>varref("CC")</code> syntax inside BUILD files.
2494 </li>
2495</ul>
2496
2497<p>
2498 Example: the C++ compiler for the current configuration.
2499 This is the <code>$(CC)</code> variable in the "Make" environment,
2500 so the <code class='flag'>--show_make_env</code> flag is needed.
2501</p>
2502
2503<pre>
Ulf Adamse0437b12016-08-29 14:48:47 +00002504 % bazel info --show_make_env -c opt COMPILATION_MODE
2505 opt
David Chen8fe82a32016-08-24 10:55:41 +00002506</pre>
2507
2508<p>
2509 Example: the <code>bazel-bin</code> output directory for the current
Googler0e692fb2021-01-22 18:38:24 +01002510 configuration. This is guaranteed to be correct even in cases where
David Chen8fe82a32016-08-24 10:55:41 +00002511 the <code>bazel-bin</code> symlink cannot be created for some reason
2512 (e.g. you are building from a read-only directory).
2513</p>
2514
gregce29bc5802020-05-18 11:13:41 -07002515<pre>% bazel info --cpu=piii bazel-bin
2516/var/tmp/_bazel_johndoe/fbd0e8a34f61ce5d491e3da69d959fe6/execroot/io_bazel/bazel-out/piii-opt/bin
2517% bazel info --cpu=k8 bazel-bin
2518/var/tmp/_bazel_johndoe/fbd0e8a34f61ce5d491e3da69d959fe6/execroot/io_bazel/bazel-out/k8-opt/bin
gregce2083aad2020-05-18 15:49:30 -07002519</pre>
brandjoncf0390b2020-02-20 10:37:40 -08002520<h3 id='version'><code>version</code> and <code>--version</code></h3>
David Chen8fe82a32016-08-24 10:55:41 +00002521
2522<p>
2523 The version command prints version details about the built Bazel
2524 binary, including the changelist at which it was built and the date.
2525 These are particularly useful in determining if you have the latest
2526 Bazel, or if you are reporting bugs. Some of the interesting values
2527 are:
2528</p>
2529<ul>
2530 <li><code>changelist</code>: the changelist at which this version of
2531 Bazel was released.
2532 </li>
2533 <li><code>label</code>: the release label for this Bazel
2534 instance, or "development version" if this is not a released
2535 binary. Very useful when reporting bugs.
2536 </li>
2537</ul>
2538
michajlo52a0bcf2019-08-29 08:55:21 -07002539<p>
2540 <code>bazel --version</code>, with no other args, will emit the same output as
2541 <code>bazel version --gnu_format</code>, except without the side-effect of potentially starting
brandjoncf0390b2020-02-20 10:37:40 -08002542 a Bazel server or unpacking the server archive. <code>bazel --version</code> can be run from
2543 anywhere - it does not require a workspace directory.
michajlo52a0bcf2019-08-29 08:55:21 -07002544</p>
2545
brandjoncf0390b2020-02-20 10:37:40 -08002546<h3 id='mobile-install'><code>mobile-install</code></h3>
David Chen8fe82a32016-08-24 10:55:41 +00002547<p>
2548 The <code>mobile-install</code> command installs apps to mobile devices.
2549 Currently only Android devices running ART are supported.
Alex Humesky105e6612017-01-06 19:03:10 +00002550
2551 See <a href="mobile-install.html">bazel mobile-install</a>
2552 for more information.
David Chen8fe82a32016-08-24 10:55:41 +00002553</p>
2554<p>
2555 Note that this command does not install the same thing that
2556 <code>bazel build</code> produces: Bazel tweaks the app so that it can be
2557 built, installed and re-installed quickly. This should, however, be mostly
2558 transparent to the app.
2559</p>
2560<p>
2561 The following options are supported:
2562</p>
2563<h4 id='flag--incremental'><code class='flag'>--incremental</code></h4>
2564<p>
2565 If set, Bazel tries to install the app incrementally, that is, only those
2566 parts that have changed since the last build. This cannot update resources
2567 referenced from <code>AndroidManifest.xml</code>, native code or Java
2568 resources (i.e. ones referenced by <code>Class.getResource()</code>). If these
2569 things change, this option must be omitted. Contrary to the spirit of Bazel
2570 and due to limitations of the Android platform, it is the
2571 <b>responsibility of the user</b> to know when this command is good enough and
Alex Humesky105e6612017-01-06 19:03:10 +00002572 when a full install is needed.
2573
2574 If you are using a device with Marshmallow or later, consider the
2575 <a href='#flag--split_apks'><code class='flag'>--split_apks</code></a> flag.
David Chen8fe82a32016-08-24 10:55:41 +00002576</p>
Alex Humesky105e6612017-01-06 19:03:10 +00002577<h4 id='flag--split_apks'><code class='flag'>--split_apks</code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00002578<p>
Alex Humesky105e6612017-01-06 19:03:10 +00002579 Whether to use split apks to install and update the application on the device.
2580 Works only with devices with Marshmallow or later. Note that the
2581 <a href='#flag--incremental'><code class='flag'>--incremental</code></a> flag
2582 is not necessary when using <code class='flag'>--split_apks</code>.
David Chen8fe82a32016-08-24 10:55:41 +00002583</p>
Alex Humesky105e6612017-01-06 19:03:10 +00002584<h4 id='flag--start_app'><code class='flag'>--start_app</code></h4>
2585<p>
2586 Starts the app in a clean state after installing. Equivalent to
2587 <code>--start=COLD</code>.
2588</p>
Googler2b88f622017-03-16 21:52:14 +00002589<h4 id='flag--debug_app'><code class='flag'>--debug_app</code></h4>
Alex Humesky105e6612017-01-06 19:03:10 +00002590<p>
Googler2b88f622017-03-16 21:52:14 +00002591 Waits for debugger to be attached before starting the app in a clean state after installing.
2592 Equivalent to <code>--start=DEBUG</code>.
2593</p>
2594<h4 id='flag--start'><code class='flag'>--start=<i>start_type</i></code></h4>
2595<p>
2596 How the app should be started after installing it. Supported <i>start_type</i>s are:
Ivo List2fb9f3c2020-12-17 10:19:09 -08002597</p>
2598<ul>
2599 <li><code>NO</code> Does not start the app. This is the default.</li>
2600 <li><code>COLD</code> Starts the app from a clean state after install.</li>
2601 <li><code>WARM</code> Preserves and restores the application state on incremental installs.</li>
2602 <li><code>DEBUG</code> Waits for the debugger before starting the app in a clean state after
2603 install.
2604 </li>
2605</ul>
2606<p>
Googler2b88f622017-03-16 21:52:14 +00002607 Note that if more than one of <code class='flag'>--start=<i>start_type</i></code>,
2608 <code class='flag'>--start_app</code> or
Ivo List2fb9f3c2020-12-17 10:19:09 -08002609 <code class='flag'>--debug_app</code> is set, the last value is used.
Alex Humesky105e6612017-01-06 19:03:10 +00002610</p>
2611<h4 id='flag--adb'><code class='flag'>--adb <var>path</var></code></h4>
2612<p>
2613 Indicates the <code>adb</code> binary to be used.
2614
2615 The default is to use the adb in the Android SDK specified by
2616 <a href='#flag--android_sdk'><code class='flag'>--android_sdk</code></a>.
2617
2618</p>
Ivo List2fb9f3c2020-12-17 10:19:09 -08002619<h4 id='flag--adb_arg'><code class='flag'>--adb_arg <var>serial</var></code></h4>
David Chen8fe82a32016-08-24 10:55:41 +00002620<p>
2621 Extra arguments to <code>adb</code>. These come before the subcommand in the
2622 command line and are typically used to specify which device to install to.
Alex Humesky105e6612017-01-06 19:03:10 +00002623 For example, to select the Android device or emulator to use:
Ivo List2fb9f3c2020-12-17 10:19:09 -08002624</p>
David Chen8fe82a32016-08-24 10:55:41 +00002625<pre>% bazel mobile-install --adb_arg=-s --adb_arg=deadbeef
2626</pre>
Ivo List2fb9f3c2020-12-17 10:19:09 -08002627<p>
2628 invokes <code>adb</code> as
2629</p>
David Chen8fe82a32016-08-24 10:55:41 +00002630<pre>
2631adb -s deadbeef install ...
2632</pre>
Alex Humesky105e6612017-01-06 19:03:10 +00002633<h4 id='flag--incremental_install_verbosity'><code class='flag'>--incremental_install_verbosity <var>number</var></code></h4>
2634<p>
2635 The verbosity for incremental install. Set to 1 for debug logging to be
2636 printed to the console.
David Chen8fe82a32016-08-24 10:55:41 +00002637</p>
2638
gregceca8ee232018-03-08 11:35:22 -08002639
brandjoncf0390b2020-02-20 10:37:40 -08002640<h3 id='dump'><code>dump</code></h3>
gregceca8ee232018-03-08 11:35:22 -08002641
2642<p>
2643 The <code>dump</code> command prints to stdout a dump of the
Googler0e692fb2021-01-22 18:38:24 +01002644 internal state of the Bazel server. This command is intended
gregceca8ee232018-03-08 11:35:22 -08002645 primarily for use by Bazel developers, so the output of this command
2646 is not specified, and is subject to change.
2647</p>
2648
tomlu72642a22017-10-18 06:23:14 +02002649<p>
2650 By default, command will just print help message outlining possible
2651 options to dump specific areas of the Bazel state. In order to dump
2652 internal state, at least one of the options must be specified.
2653</p>
2654<p>
2655 Following options are supported:
2656</p>
2657<ul>
2658 <li><code class='flag'>--action_cache</code> dumps action cache content.</li>
2659 <li><code class='flag'>--packages</code> dumps package cache content.</li>
tomlu72642a22017-10-18 06:23:14 +02002660 <li><code class='flag'>--skyframe</code> dumps state of internal Bazel dependency graph.</li>
2661 <li><code class='flag'>--rules</code> dumps rule summary for each rule and aspect class,
gregceca48e9a2020-04-14 08:54:38 -07002662 including counts and action counts. This includes both native and Starlark rules.
tomlu72642a22017-10-18 06:23:14 +02002663 If memory tracking is enabled, then the rules' memory consumption is also printed.</li>
2664 <li><code class='flag'>--skylark_memory</code> dumps a
Ivo List2fb9f3c2020-12-17 10:19:09 -08002665 <a href=https://github.com/google/pprof>pprof</a> compatible .gz file to the specified path.
2666 You must enable memory tracking for this to work.
2667 </li>
twerth700bc132018-03-05 01:32:50 -08002668 <li><code class='flag'>--action_graph=/path/to/file</code> dumps the state of
2669 the internal Bazel action graph in proto format to
2670 <code>/path/to/file</code>. You have to run (at least) the analysis phase
2671 for the targets you are interested in (for example, <code>bazel build --nobuild
2672 //foo:bar</code>). Note that this feature is still experimental, subject to
2673 change and will probably be integrated into <code>cquery</code> in the
2674 future.
2675 <li><code class='flag'>--action_graph:targets=target1,target2,...</code>
2676 filters the actions to the comma-separated list of targets when dumping the
2677 action graph.</li>
ahumeskya0f3a652018-09-17 09:23:44 -07002678 <li><code class='flag'>--action_graph:include_cmdline</code> Include the command lines of actions
2679 in the action graph dump.</li>
tomlu72642a22017-10-18 06:23:14 +02002680</ul>
2681
tomlu5af61382017-10-30 18:01:48 -04002682 <h4 id='memory-tracking'>Memory tracking</h4>
2683 <p>
2684 Some <code>dump</code> commands require memory tracking. To turn this on, you have to pass
2685 startup flags to Bazel:
2686 </p>
2687 <ul>
2688 <li><code>--host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.0.1.jar</code></li>
tomlu4cad14a2017-10-31 09:08:32 -04002689 <li><code>--host_jvm_args=-DRULE_MEMORY_TRACKER=1</code></li>
tomlu5af61382017-10-30 18:01:48 -04002690 </ul>
2691 <p>
Googlerbc321af2020-11-30 14:49:00 -08002692 The java-agent is checked into Bazel at
tomlu5af61382017-10-30 18:01:48 -04002693 third_party/allocation_instrumenter/java-allocation-instrumenter-3.0.1.jar, so make
Googlerbc321af2020-11-30 14:49:00 -08002694 sure you adjust <code>$BAZEL</code> for where you keep your Bazel repository.
tomlu72642a22017-10-18 06:23:14 +02002695
tomlu5af61382017-10-30 18:01:48 -04002696 Do not forget to keep passing these options to Bazel for every command or the server will
2697 restart.
2698 </p>
2699 <p>Example:</p>
2700 <pre>
2701 % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.0.1.jar \
2702 --host_jvm_args=-DRULE_MEMORY_TRACKER=1 \
2703 build --nobuild &lt;targets&gt;
tomlu72642a22017-10-18 06:23:14 +02002704
tomlu5af61382017-10-30 18:01:48 -04002705 # Dump rules
2706 % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.0.1.jar \
2707 --host_jvm_args=-DRULE_MEMORY_TRACKER=1 \
2708 dump --rules
tomlu72642a22017-10-18 06:23:14 +02002709
gregceca48e9a2020-04-14 08:54:38 -07002710 # Dump Starlark heap and analyze it with pprof
tomlu5af61382017-10-30 18:01:48 -04002711 % bazel --host_jvm_args=-javaagent:$BAZEL/third_party/allocation_instrumenter/java-allocation-instrumenter-3.0.1.jar \
2712 --host_jvm_args=-DRULE_MEMORY_TRACKER=1 \
2713 dump --skylark_memory=$HOME/prof.gz
2714 % pprof -flame $HOME/prof.gz
2715 </pre>
brandjoncf0390b2020-02-20 10:37:40 -08002716<h3 id='analyze-profile'><code>analyze-profile</code></h3>
tomlu72642a22017-10-18 06:23:14 +02002717
2718<p>
2719 The <code>analyze-profile</code> command analyzes data previously gathered
2720 during the build using <code class='flag'>--profile</code> option. It provides several
2721 options to either perform analysis of the build execution or export data in
2722 the specified format.
2723
2724</p>
2725<p>
2726 The following options are supported:
2727</p>
2728<ul>
Eric Cousineau334925f2020-10-14 07:12:30 -07002729 <li><code id='flag--dump'>--dump</code> displays all gathered data in a
2730 <a href='guide.html#dump-text-format'>human-readable format</a>. However,
2731 this it does not support other formats yet.</li>
tomlu72642a22017-10-18 06:23:14 +02002732</ul>
2733
2734<p>
Googler59830672019-01-03 10:33:49 -08002735 See the section on <a href='guide.html#profiling'>Troubleshooting performance by profiling</a> for
tomlu72642a22017-10-18 06:23:14 +02002736 format details and usage help.
2737
David Chen8fe82a32016-08-24 10:55:41 +00002738</p>
2739
brandjoncf0390b2020-02-20 10:37:40 -08002740<h3 id='canonicalize'><code>canonicalize-flags</code></h3>
David Chen8fe82a32016-08-24 10:55:41 +00002741
2742<p>
Eric Cousineaubb99f9d2020-10-19 18:07:27 -07002743 The <a href="https://docs.bazel.build/command-line-reference.html#canonicalize-flags-options">
2744 <code>canonicalize-flags</code></a> command, which takes a list of options for
David Chen8fe82a32016-08-24 10:55:41 +00002745 a Bazel command and returns a list of options that has the same effect. The
2746 new list of options is canonical, i.e., two lists of options with the same
2747 effect are canonicalized to the same new list.
2748</p>
2749<p>
2750 The <code class='flag'>--for_command</code> option can be used to select between different
2751 commands. At this time, only <code>build</code> and <code>test</code> are
2752 supported. Options that the given command does not support cause an error.
2753</p>
2754<p>
2755 Note that a small number of options cannot be reordered, because Bazel cannot
Eric Cousineaubb99f9d2020-10-19 18:07:27 -07002756 ensure that the effect is identical. Also note that this command
2757 <i>does not</i> expand flags from <code>--config</code>.
David Chen8fe82a32016-08-24 10:55:41 +00002758</p>
Eric Cousineaubb99f9d2020-10-19 18:07:27 -07002759<p>
2760 As an example:
2761</p>
2762<pre>
2763 % bazel canonicalize-flags -- --config=any_name --test_tag_filters="-lint"
2764 --config=any_name
2765 --test_tag_filters=-lint
2766</pre>
David Chen8fe82a32016-08-24 10:55:41 +00002767
brandjoncf0390b2020-02-20 10:37:40 -08002768<h3 id='startup_options'>Startup options</h3>
David Chen8fe82a32016-08-24 10:55:41 +00002769
2770<p>
2771 The options described in this section affect the startup of the Java
2772 virtual machine used by Bazel server process, and they apply to all
2773 subsequent commands handled by that server. If there is an already
2774 running Bazel server and the startup options do not match, it will
2775 be restarted.
2776</p>
2777<p>
2778 All of the options described in this section must be specified using the
2779 <code class='flag'>--key=value</code> or <code class='flag'>--key value</code>
2780 syntax. Also, these options must appear <i>before</i> the name of the Bazel
2781 command.
2782</p>
2783
2784<h4 id='flag--output_base'><code class='flag'>--output_base=<var>dir</var></code></h4>
2785<p>
2786 This option requires a path argument, which must specify a
Googler0e692fb2021-01-22 18:38:24 +01002787 writable directory. Bazel will use this location to write all its
2788 output. The output base is also the key by which the client locates
2789 the Bazel server. By changing the output base, you change the server
David Chen8fe82a32016-08-24 10:55:41 +00002790 which will handle the command.
2791</p>
2792<p>
2793 By default, the output base is derived from the user's login name,
2794 and the name of the workspace directory (actually, its MD5 digest),
2795 so a typical value looks like:
2796
lberkif071a232017-12-07 04:21:34 -08002797 <code>/var/tmp/google/_bazel_johndoe/d41d8cd98f00b204e9800998ecf8427e</code>.
David Chen8fe82a32016-08-24 10:55:41 +00002798 Note that the client uses the output base to find the Bazel server
2799 instance, so if you specify a different output base in a Bazel
2800 command, a different server will be found (or started) to handle the
Googler0e692fb2021-01-22 18:38:24 +01002801 request. It's possible to perform two concurrent builds in the same
David Chen8fe82a32016-08-24 10:55:41 +00002802 workspace directory by varying the output base.
2803</p>
2804
2805<p>For example:</p>
sxlijin90f244f2020-05-12 06:08:15 -07002806<pre><code>
2807 OUTPUT_BASE=/var/tmp/google/_bazel_johndoe/custom_output_base
laurentlbdad21ed2020-05-13 16:00:41 -07002808 % bazel --output_base $OUTPUT_BASE build //foo &amp; bazel --output_base $OUTPUT_BASE build //bar
2809</code></pre>
2810<p>
2811 In this command, the two Bazel commands run concurrently (because of
2812 the shell <code>&amp;</code> operator), each using a different Bazel
2813 server instance (because of the different output bases).
2814 In contrast, if the default output base was used in both commands,
2815 then both requests would be sent to the same server, which would
2816 handle them sequentially: building <code>//foo</code> first, followed
2817 by an incremental build of <code>//bar</code>.
2818</p>
2819<p>
2820 We recommend you do not use NFS locations for the output base, as
2821 the higher access latency of NFS will cause noticeably slower
2822 builds.
2823</p>
2824
David Chen8fe82a32016-08-24 10:55:41 +00002825<h4 id='flag--output_user_root'><code class='flag'>--output_user_root=<var>dir</var></code></h4>
2826<p>
jmmv0f8cecf2020-05-15 07:54:17 -07002827 Points to the root directory where output bases are created unless
2828 overriden with <code class='flag'>--output_base</code>. The directory
2829 must either not exist or be owned by the calling user. In the past,
2830 this was allowed to point to a directory shared among various users
2831 but it's not allowed any longer. (We can reconsider allowing this once
2832 <a href='https://github.com/bazelbuild/bazel/issues/11100'>#11100</a>
2833 is addressed.)
David Chen8fe82a32016-08-24 10:55:41 +00002834</p>
2835<p>
2836 If the <code class='flag'>--output_base</code> option is specified, it overrides
2837 using <code class='flag'>--output_user_root</code> to calculate the output base.
2838</p>
2839<p>
2840 The install base location is also calculated based on
2841 <code class='flag'>--output_user_root</code>, plus the MD5 identity of the Bazel embedded
2842 binaries.
2843</p>
2844<p>
2845 You can also use the <code class='flag'>--output_user_root</code> option to choose an
2846 alternate base location for all of Bazel's output (install base and output
2847 base) if there is a better location in your filesystem layout.
2848</p>
2849
cushon2a8b6572018-07-25 10:33:40 -07002850<a name="startup_flag--host_javabase"></a>
2851<h4 id='startup_flag--server_javabase'><code class='flag'>--server_javabase=<var>dir</var></code></h4>
Googlerba66e722017-12-04 12:15:36 -08002852<p>
2853 Specifies the Java virtual machine in which <i>Bazel itself</i> runs. The value must be a path to
2854 the directory containing a JDK or JRE. It should not be a label.
Googlera37711b2018-09-11 10:25:01 -07002855 This option should appear before any Bazel command, for example:
Googlerba66e722017-12-04 12:15:36 -08002856</p>
2857<pre>
Googler1523e3b2020-04-17 09:31:34 -07002858 % bazel --server_javabase=/usr/local/buildtools/java/jdk11 build //foo
Googlerba66e722017-12-04 12:15:36 -08002859</pre>
2860<p>
2861 This flag does <i>not</i> affect the JVMs used by Bazel subprocesses such as applications, tests,
2862 tools, and so on. Use build options <a href='#flag--javabase'>--javabase</a> or
2863 <a href='#flag--host_javabase'>--host_javabase</a> instead.
2864</p>
cushon2a8b6572018-07-25 10:33:40 -07002865<p>
2866 This flag was previously named <code>--host_javabase</code> (sometimes referred to as the
2867 'left-hand side' <code>--host_javabase</code>), but was renamed to avoid confusion with the
2868 build flag <a href='#flag--host_javabase'>--host_javabase</a> (sometimes referred to as the
2869 'right-hand side' <code>--host_javabase</code>).
2870</p>
Googlerba66e722017-12-04 12:15:36 -08002871
David Chen8fe82a32016-08-24 10:55:41 +00002872<h4 id='flag--host_jvm_args'><code class='flag'>--host_jvm_args=<var>string</var></code></h4>
2873<p>
2874 Specifies a startup option to be passed to the Java virtual machine in which <i>Bazel itself</i>
Googler0e692fb2021-01-22 18:38:24 +01002875 runs. This can be used to set the stack size, for example:
David Chen8fe82a32016-08-24 10:55:41 +00002876</p>
2877<pre>
2878 % bazel --host_jvm_args="-Xss256K" build //foo
2879</pre>
2880<p>
2881 This option can be used multiple times with individual arguments. Note that
2882 setting this flag should rarely be needed. You can also pass a space-separated list of strings,
2883 each of which will be interpreted as a separate JVM argument, but this feature will soon be
2884 deprecated.
2885
2886</p>
2887<p>
2888 That this does <i>not</i> affect any JVMs used by
Googler0e692fb2021-01-22 18:38:24 +01002889 subprocesses of Bazel: applications, tests, tools, and so on. To pass
David Chen8fe82a32016-08-24 10:55:41 +00002890 JVM options to executable Java programs, whether run by <code>bazel
2891 run</code> or on the command-line, you should use
2892 the <code>--jvm_flags</code> argument which
2893 all <code>java_binary</code> and <code>java_test</code> programs
Googler0e692fb2021-01-22 18:38:24 +01002894 support. Alternatively for tests, use <code>bazel
David Chen8fe82a32016-08-24 10:55:41 +00002895 test --test_arg=--jvm_flags=foo ...</code>.
2896</p>
2897
2898<h4 id='flag--host_jvm_debug'><code class='flag'>--host_jvm_debug</code></h4>
2899<p>
2900 This option causes the Java virtual machine to wait for a connection
2901 from a JDWP-compliant debugger before
Googler0e692fb2021-01-22 18:38:24 +01002902 calling the main method of <i>Bazel itself</i>. This is primarily
David Chen8fe82a32016-08-24 10:55:41 +00002903 intended for use by Bazel developers.
2904</p>
2905<p>
2906 (Please note that this does <i>not</i> affect any JVMs used by
2907 subprocesses of Bazel: applications, tests, tools, etc.)
2908</p>
2909
Austin Schuh07400c02020-12-14 10:12:31 -08002910<h4 id='flag--autodetect_server_javabase'><code class='flag'>--autodetect_server_javabase</code></h4>
2911<p>
2912 This option causes Bazel to automatically search for an installed JDK on startup,
2913 and to fall back to the installed JRE if the embedded JRE isn't available.
2914 <code>--explicit_server_javabase</code> can be used to pick an explicit JRE to
2915 run bazel with.
2916</p>
2917
David Chen8fe82a32016-08-24 10:55:41 +00002918<h4 id='flag--batch'><code class='flag'>--batch</code></h4>
ccalvarinaf8b7722018-06-06 11:54:00 -07002919
David Chen8fe82a32016-08-24 10:55:41 +00002920<p>
ccalvarinaf8b7722018-06-06 11:54:00 -07002921 Batch mode causes Bazel to not use the standard client/server mode described
Googler59830672019-01-03 10:33:49 -08002922 <a href='guide.html#client/server'>above</a>, instead running a bazel java process for a
ccalvarinaf8b7722018-06-06 11:54:00 -07002923 single command, which has been used for more predictable semantics with respect
2924 to signal handling, job control, and environment variable inheritance, and is
2925 necessary for running bazel in a chroot jail.
David Chen8fe82a32016-08-24 10:55:41 +00002926</p>
2927
2928<p>
2929 Batch mode retains proper queueing semantics within the same output_base.
2930 That is, simultaneous invocations will be processed in order, without overlap.
ccalvarinaf8b7722018-06-06 11:54:00 -07002931 If a batch mode Bazel is run on a client with a running server, it first
David Chen8fe82a32016-08-24 10:55:41 +00002932 kills the server before processing the command.
2933</p>
2934
2935<p>
ccalvarinaf8b7722018-06-06 11:54:00 -07002936 Bazel will run slower in batch mode, or with the alternatives described above.
2937 This is because, among other things, the build file cache is memory-resident, so it is not
David Chen8fe82a32016-08-24 10:55:41 +00002938 preserved between sequential batch invocations.
2939 Therefore, using batch mode often makes more sense in cases where performance
2940 is less critical, such as continuous builds.
2941</p>
2942
michajlod72eb8d2019-11-19 10:58:53 -08002943<p>
2944 <i>WARNING:</i> <code class='flag'>--batch</code> is sufficiently slower than standard
2945 client/server mode. Additionally it might not support all of the features and optimizations which
2946 are made possible by a persistent Bazel server. If you're using <code class='flag'>--batch</code>
2947 for the purpose of build isolation, we recommend using the command option
2948 <code class='flag'>--nokeep_state_after_build</code>, which guarantees that no incremental
2949 in-memory state is kept between builds. In order to restart the Bazel server and JVM after a
2950 build, please explicitly do so using the "shutdown" command.
2951</p>
2952
2953
David Chen8fe82a32016-08-24 10:55:41 +00002954<h4 id='flag--max_idle_secs'><code class='flag'>--max_idle_secs <var>n</var></code></h4>
2955<p>
2956 This option specifies how long, in seconds, the Bazel server process
Googler0e692fb2021-01-22 18:38:24 +01002957 should wait after the last client request, before it exits. The
dchaicf988cd2019-08-23 06:35:03 -07002958 default value is 10800 (3 hours). <code class='flag'>--max_idle_secs=0</code> will cause the
diamondm2decd3e2020-07-21 16:54:59 -07002959 Bazel server process to persist indefinitely. <i>NOTE:</i> this flag is only read if Bazel needs
2960 to start a new server. Changing this option will not cause the server to restart.
David Chen8fe82a32016-08-24 10:55:41 +00002961</p>
2962<p>
2963 This option may be used by scripts that invoke Bazel to ensure that
2964 they do not leave Bazel server processes on a user's machine when they
2965 would not be running otherwise.
2966 For example, a presubmit script might wish to
2967 invoke <code>bazel query</code> to ensure that a user's pending
Googler0e692fb2021-01-22 18:38:24 +01002968 change does not introduce unwanted dependencies. However, if the
David Chen8fe82a32016-08-24 10:55:41 +00002969 user has not done a recent build in that workspace, it would be
2970 undesirable for the presubmit script to start a Bazel server just
2971 for it to remain idle for the rest of the day.
2972 By specifying a small value of <code class='flag'>--max_idle_secs</code> in the
2973 query request, the script can ensure that <i>if</i> it caused a new
2974 server to start, that server will exit promptly, but if instead
2975 there was already a server running, that server will continue to run
Googler0e692fb2021-01-22 18:38:24 +01002976 until it has been idle for the usual time. Of course, the existing
David Chen8fe82a32016-08-24 10:55:41 +00002977 server's idle timer will be reset.
2978</p>
shreyax5f2d5642018-10-29 15:01:24 -07002979<h4 id='flag--shutdown_on_low_sys_mem'><code class='flag'>--[no]shutdown_on_low_sys_mem</code></h4>
2980<p>
2981 If enabled and <code class='flag'>--max_idle_secs</code> is set to a positive duration,
2982 after the build server has been idle for a while, shut down the server when the system is
2983 low on memory. Linux only.
2984</p>
2985<p>
2986 In addition to running an idle check corresponding to max_idle_secs, the build server will
2987 starts monitoring available system memory after the server has been idle for some time.
2988 If the available system memory becomes critically low, the server will exit.
2989</p>
David Chen8fe82a32016-08-24 10:55:41 +00002990
2991<h4 id='flag--block_for_lock'><code class='flag'>--[no]block_for_lock</code></h4>
2992<p>
2993 If enabled, Bazel will wait for other Bazel commands holding the
2994 server lock to complete before progressing. If disabled, Bazel will
2995 exit in error if it cannot immediately acquire the lock and
2996 proceed.
2997
2998 Developers might use this in presubmit checks to avoid long waits caused
2999 by another Bazel command in the same client.
3000</p>
3001
3002<h4 id='flag--io_nice_level'><code class='flag'>--io_nice_level <var>n</var></code></h4>
3003<p>
3004 Sets a level from 0-7 for best-effort IO scheduling. 0 is highest priority,
3005 7 is lowest. The anticipatory scheduler may only honor up to priority 4.
3006 Negative values are ignored.
3007</p>
3008
3009<h4 id='flag--batch_cpu_scheduling'><code class='flag'>--batch_cpu_scheduling</code></h4>
3010<p>
3011 Use <code>batch</code> CPU scheduling for Bazel. This policy is useful for
3012 workloads that are non-interactive, but do not want to lower their nice value.
3013 See 'man 2 sched_setscheduler'. This policy may provide for better system
3014 interactivity at the expense of Bazel throughput.
3015</p>
3016
3017<h3 id='misc_options'>Miscellaneous options</h3>
3018
3019<h4 id='flag--announce_rc'><code class='flag'>--[no]announce_rc</code></h4>
3020<p>
3021 Controls whether Bazel announces command options read from the bazelrc file when
3022 starting up. (Startup options are unconditionally announced.)
3023</p>
3024
3025<h4 id='flag--color'><code class='flag'>--color (yes|no|auto)</code></h4>
3026<p>
3027 This option determines whether Bazel will use colors to highlight
3028 its output on the screen.
3029</p>
3030<p>
3031 If this option is set to <code>yes</code>, color output is enabled.
3032 If this option is set to <code>auto</code>, Bazel will use color output only if
3033 the output is being sent to a terminal and the TERM environment variable
3034 is set to a value other than <code>dumb</code>, <code>emacs</code>, or <code>xterm-mono</code>.
3035 If this option is set to <code>no</code>, color output is disabled,
3036 regardless of whether the output is going to a terminal and regardless
3037 of the setting of the TERM environment variable.
3038</p>
3039
3040<h4 id='flag--config'><code class='flag'>--config <var>name</var></code></h4>
3041<p>
3042 Selects additional config section from the rc files; for the current
3043 <code>command</code>, it also pulls in the options from
Googlerc848bf32017-02-14 12:15:42 +00003044 <code>command:name</code> if such a section exists. Can be specified multiple
Googler0e692fb2021-01-22 18:38:24 +01003045 times to add flags from several config sections. Expansions can refer to other
Googlerc848bf32017-02-14 12:15:42 +00003046 definitions (i.e. expansions can be chained).
David Chen8fe82a32016-08-24 10:55:41 +00003047</p>
3048
3049<h4 id='flag--curses'><code class='flag'>--curses (yes|no|auto)</code></h4>
3050<p>
3051 This option determines whether Bazel will use cursor controls
3052 in its screen output. This results in less scrolling data, and a more
3053 compact, easy-to-read stream of output from Bazel. This works well with
3054 <code class='flag'>--color</code>.
3055</p>
3056<p>
3057 If this option is set to <code>yes</code>, use of cursor controls is enabled.
3058 If this option is set to <code>no</code>, use of cursor controls is disabled.
3059 If this option is set to <code>auto</code>, use of cursor controls will be
3060 enabled under the same conditions as for <code class='flag'>--color=auto</code>.
3061</p>
3062
3063<h4 id='flag--show_timestamps'><code class='flag'>--[no]show_timestamps</code></h4>
3064<p>
3065 If specified, a timestamp is added to each message generated by
3066 Bazel specifying the time at which the message was displayed.
3067</p>