cc-rs icon indicating copy to clipboard operation
cc-rs copied to clipboard

Emit compile_commands.json

Open thedataking opened this issue 4 years ago • 4 comments

Many editors and IDEs work better on C code when provided with a compilation database (compile_commands.json) because it tells the editor where to look for include files, the values of defines, etc.. Here are tools to generate compilation databases when using Make or cmake to build a C code base. For projects where the C code is embedded a Rust project and built with this crate, it would be helpful to be able to get a compilation database.

thedataking avatar May 13 '20 14:05 thedataking

I was thinking and the problem is, what happens with multiple Builds? They need to combine their commands into a single JSON array, so just appending to a file doesn't work.

Option 1: be explicit. Create a CompilationDatabase, pass it to each Build, emit with a method on CompilationDatabase. Maybe on Drop as well to deal with panicking commands? Option 2: Create a static CompilationDatabase, each Build (optionally) adds itself to it, on Drop removes itself. When all are removed, the database is written.
Option 3: before executing every new command read the current compilation database from the file, parse it, add the new command entry and write back.

All options have something to dislike, I am not sure which is better. Or maybe there is an option 4.

alexeyr avatar Nov 16 '20 18:11 alexeyr

@alexeyr, what do you think about following option:

let mut database = CompilationDatabase::new();

cc::Build::new().file("blobstore.c").compile_and_store("blobstore", &mut database);
// … more builds

database.store(/* maybe with a path */);

If we find a nice API I'm happy to contribute that API.

schrieveslaach avatar May 21 '22 05:05 schrieveslaach

Came here to request this very same feature!

In the meantime I think I may be able to capture the compiler commands by wrapping the compiler with a shell script...

vext01 avatar Mar 16 '23 11:03 vext01

In the meantime I think I may be able to capture the compiler commands by wrapping the compiler with a shell script...

That was ultimately our solution in shadow, which is in the process of incrementally migrating from C to Rust. We already had a setup script to wrap our build process, so we changed it to use the bear wrapper to generate compile_commands.json.

sporksmith avatar Mar 16 '23 14:03 sporksmith