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 · 7 comments · May be fixed by #682
Open

Emit compile_commands.json #497

thedataking opened this issue May 13, 2020 · 7 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.

@jeteve
Copy link

jeteve commented Jun 21, 2024

@sporksmith Could you point to how you setup the bear wrapper for cc::Build?

@sporksmith
Copy link

@sporksmith Could you point to how you setup the bear wrapper for cc::Build?

https://github.com/shadow/shadow/blob/main/setup#L249

Our build system has accumulated a number of layers. We use a custom python script setup, which invokes cmake, which generates makefiles, which have both pure C targets and Rust targets, some of which have their own build scripts that compile C code.

Instead of running just make to build everything from the top-level generated makefiles, we run bear --append -- make, which adds an LD_PRELOAD to the environment before invoking make, which in turn intercepts and records compiler commands.

For a project that doesn't have the cmake layer, I think you could run bear --append -- cargo build.

@harry-van-haaren
Copy link

bear --append -- cargo build works perfectly here - thanks @sporksmith!

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.

7 participants