Add support to generate compile_commands.json for improved C/C++ experience
The JSON Compilation Database is a file format that stores information about files compiled in a build.
This database can be then consumed by tooling like clangd to provide auto-completion and refactorings to your C/C++ code base.
It would be very nice to expose the clang command line flag -MJ argument from the zig compiler to support generating this file.
I can imagine this in a flag like -femit-clang-compile-db=<path>
The optimal use case would be that all std.Build.Step.Compile collect information about their builds and zig build will then emit a merged version of the compile_commands.json emitted by each individual compilation.
duplicate https://github.com/ziglang/zig/issues/9323 ?
duplicate #9323 ?
I guess this proposal is a more sophisticated version and basically proposes a solution to the linked issue
+1
Generating compile_commands.json would enable developer to
- use the LLVM LSP
clangd(useful for people using [neo]Vim, emacs, sublime, vscode) - use static analyzers like clang-check, clang-tidy, sonarlint, pvs studio
Anecdotally, I know some people that use CMake exclusively because of this feature.
+1. Please make this happen. If I had the time, I definitely would love to contribute this feature myself. I love the Zig build system, but the lack of this feature alone makes it unusable for several of my use cases. When using other tools/scripts, I usually get around lack of support for the compilation database by using bear, but for some reason, bear struggles to work with the Zig build system.
For inspiration, it might be good to look at the CMake implementation (in particular, the way it seems to always correctly figure out all of the indirect dependencies).
Hey, please check out my PR for this feature: #18391. We can continue the discussion there.
Thanks for your PR @jonathanmarvens, I'm looking forward to using the new -fcompdb flag!
I opened a PR in clangd to support zig c++ as a compiler out of the box, reviews would be appreciated: https://github.com/llvm/llvm-project/pull/100759