zig icon indicating copy to clipboard operation
zig copied to clipboard

Add support to generate compile_commands.json for improved C/C++ experience

Open ikskuh opened this issue 2 years ago • 7 comments

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.

ikskuh avatar Oct 02 '23 06:10 ikskuh

duplicate https://github.com/ziglang/zig/issues/9323 ?

kassane avatar Oct 02 '23 12:10 kassane

duplicate #9323 ?

I guess this proposal is a more sophisticated version and basically proposes a solution to the linked issue

ikskuh avatar Oct 03 '23 20:10 ikskuh

+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.

g-berthiaume avatar Oct 08 '23 21:10 g-berthiaume

+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).

jonathanmarvens avatar Dec 27 '23 17:12 jonathanmarvens

Hey, please check out my PR for this feature: #18391. We can continue the discussion there.

jonathanmarvens avatar Dec 28 '23 10:12 jonathanmarvens

Thanks for your PR @jonathanmarvens, I'm looking forward to using the new -fcompdb flag!

g-berthiaume avatar Mar 10 '24 19:03 g-berthiaume

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

mrexodia avatar Aug 09 '24 11:08 mrexodia