[CMake] Individualized compile_commands.json per executable target?

Eric Noulard eric.noulard at gmail.com
Tue Jun 11 03:42:09 EDT 2019


Le mar. 11 juin 2019 à 05:16, Staley, Martin Frank via CMake <
cmake at cmake.org> a écrit :

> 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?
>

This is not exactly what you are looking for but you may certainly use the
CMake file-api:
https://cmake.org/cmake/help/v3.14/manual/cmake-file-api.7.html#manual:cmake-file-api(7)
in order to get what you want (namelly the list of files for a given target)

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


More information about the CMake mailing list