Is it possible to produce a (clang compatible) compile_commands database while targeting MSVC?

We would like to support a Windows development workflow on Vim. As far as we know, the most convenient LSP option would be clangd, since vscode-cpptools LSP is not licensed for use outside VSCode.

As per the documentation, compile_commands are not generated fo VS generators.
Even if we were to use Ninja Generators, the compilation database would contain cl command lines, not clang (which makes a lot of sense).

Sadly, this prevents us from using the very valuable Clang tools for Cpp development when the projects are compiled with MSVC.

Is there currently any way to produce a compile_commands.json file containing Clang commands, while generating a MSVC ready project?

1 Like

CMake only understands a single compiler per language in a given build. This is unlikely to change anytime soon. Is it possible to use clang-cl to interpret the command lines instead? AFAIK, clang-tidy can read compile_commands.json even if it has a gcc-based command line as the flags are “similar enough”. I’d expect something similar for clang-cl and MSVC.

Thank you for your response.

It makes sense that there is a single compiler in a given build, which is aligned with the single generator approach of the configure step. On the other hand, we could argue that by introducing a variable such as CMAKE_EXPORT_COMPILE_COMMANDS, CMake is now showing the ability to go beyond its original design to only output build projects, since a compilation database is essentially targeted at the tooling (which is great, Cpp needs to catch up on tooling). It is also clear that currently the tooling and the compiler are not necessarily by the same vendor, so any help from CMake in this field could be a major stepping stone in advancing the tooling ecosystem.

Thank you for mentioning clang-cl, we were not aware of this program. Do you know if it can be used with clangd? Any pointers helping to complete such a configuration would be greatly appreciated!

I don’t know the extent of clangd support with clang-cl. If no one has done it yet or it isn’t yet supported, asking for support from LLVM is more likely a faster path to such support than via CMake.

Note that CMake would need to detect support for the toolchain in question and rerun the configure/generate step to compute flags as things like CMAKE_CXX_FLAGS can accumulate compiler-specific bits and generator expressions could change based on compiler IDs.

Actually, clang-cl works pretty good in my own case of one work project (which uses clang-cl on Windows and Ninja as generator)

Agreed for this feature, sometimes I want to use my Ye Olde Vim 9.2 on Windows with project generated for Visual Studio, and the YouCompleteMe don’t properly work with these projects as it requires compile_commands.json. I have to modify the ycm config for folder…
So, this feature is really very good to have :blush:

Note that the description here is a bit inaccurate as we can generate them for MSVC…if you use a Ninja or Makefiles generator. The problem with Visual Studio (and Xcode) is that the command line is not known to CMake; the IDE’s underlying build tool crafts the actual command line from the flags CMake knows about and project settings. Both of these generators provide ways to inject extra properties that CMake has no idea about how they affect any given source file’s compilation line. To be truly accurate, it really needs compile_commands.json exporters to exist in the IDEs themselves (and then CMake could use that to make them through its mechanisms).

1 Like

To be truly accurate, it really needs compile_commands.json exporters to exist in the IDEs themselves (and then CMake could use that to make them through its mechanisms).

I might be misunderstanding this statement, but the intent of the question was to know if there was a way to generate an MSVC build (via Visual Studio or Ninja generator), while also generating a compile_commands.json that would be equivalent to the compile_commands that would be generated by the Makefiles generator and Clang as the CXX compiler. (Because this is exactly what I have to do right now in a separate build folder just to output the compile_commands needed by Clang tooling, and having to keep both build folders in sync is a PITA).

So I do not think that this specific question really depends on the ability to talk to any IDE (as I suppose the Makefiles is able to output compile_commands.json without any IDE involved).

Ah. Yeah, there’s no current support for CMake understanding more than one toolchain for a language in a build at a time. Because things like:

  string(APPEND CMAKE_CXX_FLAGS " -fsomething-weird")
endif ()

exist, CMake would need to configure twice and hope that the builds are comparable.

You could create a sub-build of your own project for clang and use that.