Compilation database without compiling?

I’m building a tool with libclang/libtooling, and I need a compilation database for that. Clang can emit then by passing -MJ, but that only happens during a real build process. I’m building a reflection tool with codegen for my project, and as such the tool should be run before I compile the project. Problem here is there’s now a chicken & egg situation, where I need to compile my project to get the compilation database out of clang, but I need the compilation database before I compile the project. Is there a way to do a “dry run” clang compile? Or a way to emit this database json without invoking a full compile?

I’ve found the -### clang flag, which is frustratingly close to what I want. It emits all the info I need without actually compiling, so I could hackily parse that, but it would be nice to have it in the compile cmds json format - fundamentally, these two options do the same thing, so if there’s a way to get the benefits of both I’d love to know.

Do you want exactly clang to generate compilation database?
You can use -DCMAKE_EXPORT_COMPILE_COMMANDS for CMAKE or Bear tool. Bear docs says it should work like bear -- <your-build-command> which I guess what you need.

I’m not using cmake. Also doesn’t bear just intercept build commands? That wouldn’t solve the problem, unless I’m misunderstanding how bear works

On Sun, Aug 24, 2025, 8:51 AM Baranov Victor via LLVM Discussion Forums <notifications@llvm.discoursemail.com> wrote:

vbvictor
August 24

Do you want exactly clang to generate compilation database?
You can use -DCMAKE_EXPORT_COMPILE_COMMANDS for CMAKE or Bear tool. Bear docs says it should work like bear -- <your-build-command> which I guess what you need.


Visit Topic or reply to this email to respond.

To unsubscribe from these emails, click here.

I think compiler is at the wrong level to solve this issue for you. Instead, you should ask your build system how translation units are compiled. CMake’s `CMAKE_EXPORT_COMPILE_COMMANDS` is a good example of build system (generator) providing support for that. Make’s `–dry-run` might help.

Practically speaking, what I think might work is combining Bear with a wrapper script over compiler that either passes everything to compiler or simply returns 0 depending on the value of an environment variable.

Generally the compilation database is created by the build system, or by something that intercepts the build commands emitted by a build system.

I’ve hacked up compilation database support in an environment that uses GNU make to build components, but it piggy-backed off the response files that the build system generated on Windows to get around command-line length restrictions. While I was doing that I found out that both clang and gcc also accept response files where command-line arguments are written into a file and then referenced on the command-line. So if your build system is based on GNU make (or suitable make) then you may be able to adjust your makefiles to output the compilation database directly from the information used to construct build commands.