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