1

I'd like to see all copts used by my bazel build //my/target/... build command. How can I do so? I tried adding -s --verbose_explanations --explain=out.txt:

bazel build -s --verbose_explanations --explain=out.txt //my/target/...

...but it just output all bazel build options into "out.txt", NOT C options passed to the compiler.

References:

  1. Bazel build verbose compiler commands logging
  2. Bazel user manual: https://docs.bazel.build/versions/master/user-manual.html
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265

1 Answers1

3

It turns out the answer is to use the -s option:

bazel build -s //my/target/...

You may also want to use --color=no to disable color output to the bazel command log file, so it doesn't have the special color escape chars in it.

bazel build -s --color=no //my/target/...

The reason I didn't see any useful output, however, is simply because bazel had already built all this stuff and cached its output! Once Bazel builds something, if it is cached and unchanged, it won't build it again, so running bazel build //my/target/... followed by bazel build -s //my/target/... will show NOTHING NEW!

The only fix I know of (without deleting your entire build cache) to force Bazel to rebuild is to make a real code change to a file to be built and then build again. The easiest way to do this is to change a single char in the file of interest so that the build is broken--ex:

// In a file of interest, change this:
int i;

// to this:
intt i;

// really, ANY change which breaks the build will do

then build:

bazel build -s --color=no //my/target/...

The file is changed so the cache isn't valid so bazel will rebuild, and now the -s will take effect and you'll see all the extra, detailed bazel build output. Note that -s means --subcommands. See here: https://docs.bazel.build/versions/master/user-manual.html#flag--subcommands.

Now, fix the file above, and build again:

bazel build -s --color=no //my/target/...

The build will be re-done again since a build file just changed, it will pass this time, and you'll get to see the detailed subcommands of each bazel build command, each of which shows the copts (C or C++ build options) in use for that build command.

Look in your Bazel command log (command.log file), found some place like here:

home/username/.cache/bazel/_bazel_username/3e8af127f8b488324cdf41111355ff4c/command.log

or here perhaps, relative to your git repo root dir:

build/bin/../../../../../command.log

Tweak this path above as required. Note that build/bin is a symbolic link to the longer path above in your ~/.cache/bazel dir.

Anyway, all output will be found in this command.log file. View this file in an editor with line wrapping OFF to make it easier to look at, and find your compilation command for your *.cpp source file of interest. Once you find that compile command, copy that one line into a new file, turn line wrapping back ON, and do a regex search in the file for the pattern -W[^ ]* (source).

This one line may be hundreds of lines of commands and options with line wrapping on, and this regex search pattern will find all compiler warnings, for instance, which match a space followed by -W followed by any number of chars EXCEPT a space. In other words, it will find all compiler warning options, like -Werror=switch, -Wno-error=switch, -Wall, -Weverything, -Werror, etc.

Summary:

Anyway, with the -s or --subcommands bazel build option ON, on a fresh build where you just CHANGED THE SOURCE FILE OF INTEREST, your Bazel command.log file above contains all final compilation commands used by Bazel, showing all final options passed to gcc or clang or whatever compiler you're using, and you can look through this file to see what exactly were all your settings during compilation for any given file of interest which you just changed and are now building through this build command:

bazel build --subcommands --color=no //my/target/...
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265