Analysis of CFamily using multiple compile_commands.json


First of all I am thanking all of the SonarSource community, especially SonarSourcers for helping out during difficult times.

  • SonarQube 9.1.0 version
  • What we are trying to achieve: is to analyze C code using multiple CMake output compile_commands.json of several to say modules/samples. See all of them analyzed in the server.
  • The problem: when passing each compile_commands.json one by one to the SonarScanner. Only the latest compilation data base can be seen analyzed in the SonarQube server, detected bugs from other samples gets removed.
  • What was tried: was to copy output from both samples of compile_commands.json files into one. This way I was able to see analyzed both code samples as one in the SonarQube server. However, this does not sound like a good idea.

The project structure is depicted down below. Each sample is being built separately, therefore has it’s own compile_commands.json file, which is later passed to SonarScanner. There are no samples, which consist of all includes.

 |        |
 |        └───GNSS
 |        |    └───gnss_sample_main.c
 |        |    └───CMakeLists.txt
 |        |    └───build
 |        |        └───compile_commands.json	
 |        └───USB
 |            └───usb_sample_main.c
 |            └───CMakeLists.txt
 |            └───build
 |                      └───compile_commands.json	
 |        |
 |        └───gps.h
 |        └───glonas.h
 |        └───clock.h
 |        └───led.h
         |         └───gnss_driver.c
         |         └───usb_driver.c
  • Could you suggest what we could do in this situation?
  • A question from a side: is it possible to use unix command such as “nproc” to pass number of processors to parameter “sonar.cfamily.threads=” ? For example: sonar.cfamily.threads=$nproc. I was not able to do this. Is there any other way? Or should we input just an integer?


Hi @Ovidijus ,

you can pass the property by command line with something like:

sonar-scanner ... -Dsonar.cfamily.threads=$(nproc) ...

Would you like to see them as one project in the server or is it ok to see them as distinct projects?

Hello Massimo,

We would like to see them as a single project.

Hi @Ovidijus ,

what you can do is to use jq to merge the two compile_commands.json so you can run a single analysis passing a single file.

1 Like

@mpaladin ,
Thank you. This looks like a solution we could use.

Is this a standard solution for other companies as well, who has huge repository with several to be built projects within?

Are there anything we should be aware of when combining compile_commands.json files? For example, can the same source be repeated?

Hi @Ovidijus ,

on my knowledge you are the first which is trying to combine them together, and at the same time Compilation Database support was recently added to the analyzer.

The same source can be repeated but the first one is going to be taken from the analysis. So, it is preferable to keep the order stable when merging multiple files, to keep the analysis result stable. I cannot think of other things you should be aware.

Hello @mpaladin ,

We have tried with jq and had several issues.
However, this solutions seems to work fine for finding and merging multiple files compilation databases.
In case anyone would need it in the future:

COMPILE_DATABASES=$(find $source_code/samples -name "compile_commands.json" -printf "%p ")
jq -s 'add' $COMPILE_DATABASES > $SONAR_QUBE_HOME/compile_commands.json

@mpaladin thank you for the help.

1 Like

Hi @Ovidijus ,

thank you for the update and for sharing back.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.