ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.16k stars 2.56k forks source link

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

Open ikskuh opened 1 year ago

ikskuh commented 1 year ago

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.

kassane commented 1 year ago

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

ikskuh commented 1 year ago

duplicate #9323 ?

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

g-berthiaume commented 1 year ago

+1

Generating compile_commands.json would enable developer to

Anecdotally, I know some people that use CMake exclusively because of this feature.

jonathanmarvens commented 11 months ago

+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 commented 11 months ago

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

g-berthiaume commented 8 months ago

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

mrexodia commented 3 months ago

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