getting list of compile commands

1,133 views
Skip to first unread message

Brian Silverman

unread,
Dec 5, 2015, 12:35:54 AM12/5/15
to bazel-discuss
Hi,

I would like to get the compile command lines out of Bazel. Specifically, I want to create a compile_commands.json file so I can use clang-tidy. That seems like the kind of thing `bazel query` should be able to output, but I don't see the information about command lines in any of the output formats. Is there any to do this?

I think running clang-tidy next to the real compiler using a wrapper script would work, but I would like to run clang-tidy with different options without rebuilding all the dependents of the rule containing the relevant .cc file, which is hard with that.

Thanks,
Brian

Damien Martin-guillerez

unread,
Dec 5, 2015, 4:11:51 AM12/5/15
to Brian Silverman, bazel-discuss

Hi

Is `bazel build --nobuild -s //target` enough?

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAP01z6JqcuQg%2B_DvVJ_CABDYquOvVVfn1r4QfWv5Y1QcRusGXg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Janak Ramakrishnan

unread,
Dec 5, 2015, 8:19:12 AM12/5/15
to Damien Martin-guillerez, bazel-discuss, Brian Silverman

Not exactly sure what your question is, but --compile_one_dependency might be handy.

Brian Silverman

unread,
Dec 5, 2015, 7:55:16 PM12/5/15
to Janak Ramakrishnan, Damien Martin-guillerez, bazel-discuss
`bazel build --nobuild -s` doesn't seem to actually print anything, but if it did I think it would work.

If there was some way to "fake" a build (running the execution phase without actually executing any commands?), I think that (along with setting --output_base to a directory without any cached output) would answer my question. Other tools (make and rsync for example) have --dry-run/-n to do that.

--compile_one_dependency is indeed handy for part of this. Thanks!

Damien Martin-guillerez

unread,
Dec 6, 2015, 3:15:22 AM12/6/15
to Brian Silverman, Janak Ramakrishnan, bazel-discuss
-s print the command to be executed
--nobuild prevent the commend to actually be executed

So it should be what you want but it won't print anything if you target is up to date (i.e. it will print the command only if you target actually needs something to be rebuild)

Janak Ramakrishnan

unread,
Dec 6, 2015, 5:23:01 AM12/6/15
to Damien Martin-guillerez, bazel-discuss, Brian Silverman

-s only prints the command when it's about to be run. If you specify --nobuild, the build isn't done, so -s has no effect. We could make it a warning to specify -s with --nobuild :)

Very hacky solution to the wrapper script approach: don't use sandboxing, and have the script get its options from some other hard-coded file that Bazel doesn't know about. Then you can modify the flags however you wish in that other file and redo just one action.

Brian Silverman

unread,
Dec 10, 2015, 12:27:36 AM12/10/15
to Janak Ramakrishnan, Damien Martin-guillerez, bazel-discuss
Thanks for the suggestions. I ended up using an extra_action, which seems like it's designed for this kind of thing. It even avoids having to parse the input file and arguments out of the bash wrapper. It works really nicely, including getting re-run when it should!!!

The only part I'm not completely happy with is finding the outputs from all the invocations of the action. I want to generate a single file aggregating them all together, and the only way I see to do that is by going through all the files in the extra_actions folder (next to bin and genfiles under bazel-out/*). Matching files by name isn't actually too bad, so I'm pretty satisfied with it. I'd appreciate it if anybody knows of a better way though.
Reply all
Reply to author
Forward
0 new messages