C++/CMake Analysis with Code Variants

Hi all,

I am building an open-source project with Travis and followed the guides to also scan it with SonarCloud.
The project is written in C++ and CMake.
There are basically two variants of the software, that are chose by a CMake define (cmake -D…).
Depending on this define, different Source Files are build.
What I managed to do is to run two jobs in Travis consecutively (one for each variant) and wrap the build with the sonar scanner.
In the Travis logs both variants complete and it seems both sonar results are uploaded.
However, in the Sonar Project I do only see results (bugs, smells, etc.) for the second variant, the code of the first seems not to be checked.

Is it the correct approach to analyze such a project structure?
Or do I need to analyze both variants within one build?
That would not make any sense from functional point of view, but I could manage to do so using wrappers/mocks, etc.
Infos:

  • ALM used: GitHub
  • CI system used: Travis CI
  • Scanner command used: sonar-scanner -Dsonar.cfamily.cache.enabled=true -Dsonar.cfamily.cache.path={TRAVIS_HOME}/.cfamily -Dsonar.cfamily.threads={NUMBER_OF_PROCESSORS}
  • Languages of the repository: C/C++
  • Only if the SonarCloud project is public, the URL: https://sonarcloud.io/dashboard?id=tiolan_imqtt

Hi @tiolan,

I guess you are talking about https://github.com/tiolan/imqtt/blob/dae989c01a943144cc28296933016915bb8122c8/.travis.yml.

Are the set of source files disjoint between the two variants?

If yes, you can do something like:

create a file build.sh with something like:

NUMBER_OF_PROCESSORS=$(nproc --all)
cmake -B buildfirst -S . -DIMQTT_USE_PAHO:BOOL=ON -DIMQTT_BUILD_SAMPLE:BOOL=ON -DIMQTT_INSTALL:BOOL=OFF -DIMQTT_BUILD_DOC:BOOL=ON -DBUILD_SHARED_LIBS=OFF
cmake --build buildfirst -j ${NUMBER_OF_PROCESSORS}

cmake -B buildsecond -S . -DIMQTT_USE_MOSQ:BOOL=ON -DIMQTT_BUILD_SAMPLE:BOOL=ON -DIMQTT_INSTALL:BOOL=OFF -DIMQTT_BUILD_DOC:BOOL=ON -DBUILD_SHARED_LIBS=OFF
cmake --build buildsecond -j ${NUMBER_OF_PROCESSORS}

and in your .travis.yml do something like:

build-wrapper-linux-x86-64 --out-dir bw-output bash build.sh
sonar-scanner -Dsonar.cfamily.cache.enabled=true -Dsonar.cfamily.cache.path=${TRAVIS_HOME}/.cfamily -Dsonar.cfamily.threads=${NUMBER_OF_PROCESSORS}

This did the trick! Many thanks for the fast support.

1 Like

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