# Generate a compile\_commands.json for a specific target

**URL:** <https://discourse.cmake.org/t/generate-a-compile-commands-json-for-a-specific-target/6389>\
**Category:** Code\
**Tags:** gen:makefiles, gen:ninja\
**Created:** [September 2, 2022, 6:23am UTC](https://discourse.cmake.org/t/generate-a-compile-commands-json-for-a-specific-target/6389 "2022-09-02T06:23:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![itavero](https://discourse.cmake.org/user_avatar/discourse.cmake.org/itavero/32/2547_2.png) [@itavero](https://discourse.cmake.org/u/itavero)\
**Post date:** [September 2, 2022, 6:23am UTC](https://discourse.cmake.org/t/generate-a-compile-commands-json-for-a-specific-target/6389/1 "2022-09-02T06:23:18Z")

</div>

For running PC Lint Plus, we currently use the `compile_commands.json`.  
This works, but had some caveats when it comes to excluding supplied SDKs and such from our analysis, as they would be included in the JSON as well.

Recently I found out you can actually configure this per target using EXPORT\_COMPILE\_COMMANDS.  
That would improve things, but the downside is that some tooling (e.g. VS Code CMake Tools) turns this feature ON globally when calling CMake.  
This would mean that we would have to exclude targets explicitly, but that becomes a pain to manage when we pull in other pieces of code (for our own code we could add a CMake option probably, but with third party code I’d will be another story).

Therefor I was wondering if I can somehow generate such a JSON file per target, next to the “global” one.  
This would allow us to create specific targets for running PCLP on other specific “code” targets.  
An additional advantage would be that it could allow us to only analyze part of the code in a project instead of all, if we are working on a specific component.

I’d love to hear what the possibilities are.

---

<div class="post-metadata">

**Author:** ![itavero](https://discourse.cmake.org/user_avatar/discourse.cmake.org/itavero/32/2547_2.png) [@itavero](https://discourse.cmake.org/u/itavero)\
**Post date:** [September 2, 2022, 7:16am UTC](https://discourse.cmake.org/t/generate-a-compile-commands-json-for-a-specific-target/6389/2 "2022-09-02T07:16:49Z")

</div>

I was thinking that if there is a generator expression that could somehow give me all the parts of these commands, that would probably also work to create PC Lint Plus targets that only analyze specific targets.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [September 2, 2022, 12:23pm UTC](https://discourse.cmake.org/t/generate-a-compile-commands-json-for-a-specific-target/6389/3 "2022-09-02T12:23:55Z")

</div>

Does PC Lint Plus work as a compiler launcher? You can then set `<LANG>_COMPILER_LAUNCHER` properties to use PC Lint Plus for just those targets.

---

<div class="post-metadata">

**Author:** ![itavero](https://discourse.cmake.org/user_avatar/discourse.cmake.org/itavero/32/2547_2.png) [@itavero](https://discourse.cmake.org/u/itavero)\
**Post date:** [September 2, 2022, 12:45pm UTC](https://discourse.cmake.org/t/generate-a-compile-commands-json-for-a-specific-target/6389/4 "2022-09-02T12:45:02Z")

</div>

I could potentially use that, but then I’d have to fully “copy” an existing target and probably add a post “build” command as well.

PC Lint Plus comes helper scripts to configure it either via this JSON file, or by running an “imposter” compiler, which basically logs all compiler invocations to a text file.  
I didn’t go with the “imposter”, because that would require me to reconfigure the CMake project with a special option set to use that fake compiler and I already had the `compile_commands.json` from CMake.

Let’s say I have a target created with `add_library` or `add_executable`, is there an easy way to create a target that copies all the configuration for those targets (e.g. source files, compiler defines, etc.) to which I can then add an override for the `<LANG>_COMPILER_LAUNCHER`?  
The thing I really want to avoid is having our developers need to reconfigure just to run the SCA tools.

Another option I was considering was simply calling `ninja -t compdb <rule>` from a CMake script to generate the `compile_commands.json` for a specific target.  
The downside is that I’d have to replicate the name of the `<rule>` based on the CMake target name and configuration.

Final thing I came across thus far is `<LANG>_CPPCHECK` which I think keeps all (or at least most) of the needed compiler arguments in tact.  
Only downside is that it seems to always be combined with compiling a target.  
In our CI pipeline, we typically build (mostly cross-compile for ARM Cortex-M targets) first in one container and run the SCA in another container afterwards (without reusing the cache and such). It would be a waste to recompile just to be able to run the SCA tooling.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [September 2, 2022, 12:57pm UTC](https://discourse.cmake.org/t/generate-a-compile-commands-json-for-a-specific-target/6389/5 "2022-09-02T12:57:20Z")

</div>

> [@itavero](#):
>
> The thing I really want to avoid is having our developers need to reconfigure just to run the SCA tools.

Is it expensive to reconfigure? What’s wrong with having two build trees, one with and one without?

> [@itavero](#):
>
> It would be a waste to recompile just to be able to run the SCA tooling.

Using something like `ccache` would help here; the actual compiles would be cached at least.

That said, there’s no mechanism currently available to have different compilation databases. This might be a reasonable request (though something has to “lose” and not get `compile_commands.json` as the filename).

---

<div class="post-metadata">

**Author:** ![itavero](https://discourse.cmake.org/user_avatar/discourse.cmake.org/itavero/32/2547_2.png) [@itavero](https://discourse.cmake.org/u/itavero)\
**Post date:** [September 6, 2022, 6:50am UTC](https://discourse.cmake.org/t/generate-a-compile-commands-json-for-a-specific-target/6389/6 "2022-09-06T06:50:44Z")

</div>

> [@ben.boeckel](#):
>
> Is it expensive to reconfigure? What’s wrong with having two build trees, one with and one without?

Reconfiguring is not that bad, but it does take time and interrupts the flow.  
Multiple build trees could work, but I’m not sure how we’d integrate that into Visual Studio Code.  
Currently we can just start the PC Lint Plus task, regardless of the configuration and such being selected.

> [@ben.boeckel](#):
>
> That said, there’s no mechanism currently available to have different compilation databases. This might be a reasonable request (though something has to “lose” and not get `compile_commands.json` as the filename).

I think something like `<target>.compile_commands.json` would do. Preferably the name/path should be retrievable via a generator expression, as this can then be used to pass the path to the configuration script provided with PC Lint Plus. Where should I propose such a feature request?
