Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit compile_commands.json #497

Open
thedataking opened this issue May 13, 2020 · 4 comments · May be fixed by #682
Open

Emit compile_commands.json #497

thedataking opened this issue May 13, 2020 · 4 comments · May be fixed by #682

Comments

@thedataking
Copy link

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.

@alexeyr
Copy link
Contributor

alexeyr commented Nov 16, 2020

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.

@schrieveslaach
Copy link

@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 added a commit to schrieveslaach/cc-rs that referenced this issue May 26, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
@schrieveslaach schrieveslaach linked a pull request May 26, 2022 that will close this issue
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue May 26, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 4, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 4, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 7, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 7, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 7, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 23, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 23, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 23, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
schrieveslaach added a commit to schrieveslaach/cc-rs that referenced this issue Jul 23, 2022
This commit adds API to record the compilation invocations in order to
emit compile_commands.json a.k.a. JSON compilation database.

Fixes rust-lang#497
@vext01
Copy link

vext01 commented Mar 16, 2023

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

@sporksmith
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants