[CMake] Individualized compile_commands.json per executable target?

Staley, Martin Frank staley at lanl.gov
Mon Jun 10 22:31:02 EDT 2019


I'm using the Clang/LLMV APIs to build a code analysis tool that gives my users some additional analysis of their C++ codes, above and beyond what the compiler, proper, provides.

With a set(CMAKE_EXPORT_COMPILE_COMMANDS ON) in CMakeLists.txt, cmake emits a "compilation database" file, compile_commands.json, that contains exact compilation commands for all relevant source files. The .json can be consumed by my analysis tool to get what it needs. Except....

Here's the problem. (And I've tried various googles, but nothing panned out.) compile_commands.json contains data for *all* source files involved in *any* executable for which cmake emits Makefile rules. My analysis tool needs input that reflects those (and only those) source files that are involved in any *one* of the (generally more than one) executables.

EXAMPLE. Say I have some general C++ constructs in a.cc, b.cc, and c.cc, and two C++ executables (roughly speaking, with a main()): foo.cc and bar.cc. Assume further that, linkage-wise, foo.cc needs the material from a.cc and b.cc (but not c.cc), while bar.cc needs the material from b.cc and c.cc (but not a.cc). These dependencies are of course programmed into a CMakeLists.txt.

In this situation, cmake will emit a compile_commands.json that has commands for all five sources: {a.cc, b.cc, c.cc, foo.cc, bar.cc}. But my analysis tool, run thereon, doesn't know how to divvy up the various elements, and it ends up reporting, among other things, the moral equivalent of multiply-defined symbol errors.

Ideally, I'd love to have a way that Cmake could emit, say, foo.json containing only the compile commands for {a.cc, b.cc, foo.cc}, and a bar.json containing only the commands for {b.cc, c.cc, bar.cc}. Basically, individualized compilation databases for each executable target.

There are, of course, alternatives. Given additional input, my tool could pull (only) the proper entries from the current, aggregated compile_commands.json. Or, post-cmake, I could allow for a dry-run verbose make of a single executable, and parse out the requisite information. For users, however, and for simplicity's sake, I'm hoping for something more direct. CMake has what I need, in some form - it must, in order to create the Makefile! So, life would be absolutely fabulous if this existing knowledge could be used to create executable-specific .json databases as outlined above.

Is this in fact available, and my googling skills aren't up to par? If it isn't, might it be considered for a future CMake release?


--
Martin Staley
CCS-7, Applied Computer Science
Mail Stop B287
Los Alamos National Laboratory
Los Alamos, NM 87545
Office: 03/508/122
Phone: 1-505-667-7108

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190611/69f9a8d0/attachment.html>


More information about the CMake mailing list