Headers files inside compile_commands.json
Hello cmake devs,
A large number of c/c++ projects use cmake's generated compile_commands.json to obtain the flags from a specific file, specially libclang based ones. A very common problem in these scenarios is to find out the flags from a header file since the json file will only have the source ones, resulting in a lot of hacky and error prone ways to guess it.
In my case, I use the compile_commands.json data in various packages in my emacs configuration.
What I can't understand is why cmake doesn't have a option to enable the insertion of the projects headers inside compile_commands.json, my guess is that this file is somewhat standard so it is spected to only have the implementation files. But even if this is the case, why not generate another file (ex. headers_compile_commands.json) with it, or only enable it in compile_commands.json when explicitly setting a specific cmake flag?
As a proof of concept, I've implemented it in the cmake code, it's a little hacky and have some code duplication, but it only took me 1 hour to do it, so it isn't very complicated to implement it.
What I want to ask is if there is interest to have this funcionality inside cmake, I think it would help tremendously a lot of projects. If there is, I'm willing to make a better implementation of it and send a merge request, I would only ask for how would you like it to be implemented.
as a example, bellow is a snippet of a part of what my change to the code generate inside compile_commands.json, note that obviously the user needs to explicitly add the header files to the commands add_executable or add_library for it to work:
[
{
"directory": "/home/ebarreto/Downloads/cmake-3.6.1/test/build",
"command": "/usr/bin/clang++ -I/home/ebarreto/Downloads/cmake-3.6.1/test/include -o CMakeFiles/SLIC.dir/test.hpp.gch -x c++-header -c /home/ebarreto/Downloads/cmake-3.6.1/test/include/test.hpp",
"file": "/home/ebarreto/Downloads/cmake-3.6.1/test/include/test.hpp"
},
{.
"directory": "/home/ebarreto/Downloads/cmake-3.6.1/test/build",
"command": "/usr/bin/c++ -I/home/ebarreto/Downloads/cmake-3.6.1/test/include -o CMakeFiles/SLIC.dir/src/test.cpp.o -c /home/ebarreto/Downloads/cmake-3.6.1/test/src/test.cpp",
"file": "/home/ebarreto/Downloads/cmake-3.6.1/test/src/test.cpp"
},
{
"directory": "/home/ebarreto/Downloads/cmake-3.6.1/test/build",
"command": "/usr/bin/c++ -I/home/ebarreto/Downloads/cmake-3.6.1/test/include -o CMakeFiles/SLIC.dir/src/main.cpp.o -c /home/ebarreto/Downloads/cmake-3.6.1/test/src/main.cpp",
"file": "/home/ebarreto/Downloads/cmake-3.6.1/test/src/main.cpp"
}
]
Thanks!