CLion plugin: Get access to compilation database



I'm trying to write a CLion plugin that invokes a custom libtooling-based tool, which needs a compilation database. Since CLion uses clangd internally I initially assumed that it always adds the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON CMake command-line argument when configuring a CMake project, but to my surprise I found that it does not, so the compile_commands.json file is not in the build directory unless the user explicitly requested the compilation database generation themselves. 

Since I'd like my plugin to work regardless of the configuration options set by the user for their CMake project I tried to locate the compile_commands.json file used by clangd but have been unable to find it. Is there any way to access it? Or is there another way to access the compiler flags for a single source file so that I can at least generate a compilation database manually in my plugin?




Hi Manuel!

CLion doesn't use compilation database (`compile_commands.json`) when working with CMake project. To read project model from CMake, CLion uses CMake file-based API. And interaction with clangd happens directly via RPC, where we send all the required information. So there is no need for CLion to use compilation database for CMake projects.

Note, that CLion does support compilation database, but as a separate project model. You can open `compile_commands.json` file as project, and CLion will automatically read all the information from the file.

As for, how to access compiler flags from CLion, you should look at the `OCWorkspace` and `OCResolveConfiguration` classes. It provides all the language information you might need. E.g. take a look at `OCWorkspace#getConfigurationsForFile()`. It should give you resolve configuration for the provided file. From this configuration you can get  compiler settings (including compiler flags) using `OCResolveConfiguration#getCompilerSettings()`