Search code examples
c#c++msbuildsonarqubesonarscanner

How to analyze C# and C++ projects using SonarQube in one pipeline build?


I have a project consists of 3 C++ sub projects and a C# main project, Below is my Yaml file to analyze it in azure pipeline using SonarQube. As you can see in the YAML file, I analyze all of 4 projects using configuration for c++, so how can I analyze first 3 project using configuration for c++ and analyze the last one using configuration for .NET(C#)? Thanks.

Here is a warning from analyzing log:

WARN: Your project contains C# files which cannot be analyzed with the scanner you are using. To analyze C# or VB.NET, you must use the SonarScanner for .NET 5.x or higher, see SonarScanner for .NET

resources:
  repositories:
  - repository: Dependency
    type: githubenterprise
    endpoint: https://github.azc.ext.xxx.com
    name: cloud-client/Dependency

trigger: none

pool: 'Windows2022-DEV'

variables:
  buildPlatform: 'x64'
  buildConfiguration: 'Release'

steps:
- checkout: self
- checkout: Dependency

- script: |
     move Dependency/KHPLib $(Build.SourcesDirectory)

- task: NuGetToolInstaller@1


- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Invoke-WebRequest -Uri 'https://sq.corp.xxxcloud.net/static/cpp/build-wrapper-win-x86.zip' -OutFile 'build-wrapper.zip'
      Expand-Archive -Path 'build-wrapper.zip' -DestinationPath '.'

- task: SonarQubePrepare@5
  inputs:
    SonarQube: 'https://sq.corp.xxxcloud.net'
    scannerMode: 'CLI'
    configMode: 'manual'
    cliProjectKey: 'CS_SW_WinIoT_main_KConnectionWizardWin'
    cliSources: '.'
    extraProperties: |
      sonar.cfamily.build-wrapper-output=bw-output
      sonar.java.file.suffixes=-

- task: CmdLine@2  # C++ sub project
  inputs:
    script: 'build-wrapper-win-x86\build-wrapper-win-x86-64.exe --out-dir bw-output "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe" KHPLib\src\KHPLib\KHPLib.vcxproj -t:Rebuild /p:configuration=release /p:platform=x64'

# - task: VSBuild@1
#   inputs:
#     solution: 'KHPLib\src\KHPLib\KHPLib.vcxproj'
#     platform: '$(buildPlatform)'
#     configuration: '$(buildConfiguration)'

- task: CmdLine@2  # C++ sub project
  inputs:
    script: 'build-wrapper-win-x86\build-wrapper-win-x86-64.exe --out-dir bw-output "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe" KConnectionWizardWin\src\KConnectionWizardSvc\KConnectionWizardSvc.vcxproj -t:Rebuild /p:configuration=release /p:platform=x64'

# - task: VSBuild@1
#   inputs:
#     solution: 'KConnectionWizardWin\src\KConnectionWizardSvc\KConnectionWizardSvc.vcxproj'
#     platform: '$(buildPlatform)'
#     configuration: '$(buildConfiguration)'

- task: CmdLine@2 # C++ sub project
  inputs:
    script: 'build-wrapper-win-x86\build-wrapper-win-x86-64.exe --out-dir bw-output "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe" KConnectionWizardWin\src\KConnectionWizardLauncher\KConnectionWizardLauncher.vcxproj -t:Rebuild /p:configuration=release /p:platform=x64'

# - task: VSBuild@1
#   inputs:
#     solution: 'KConnectionWizardWin\src\KConnectionWizardLauncher\KConnectionWizardLauncher.vcxproj'
#     platform: '$(buildPlatform)'
#     configuration: '$(buildConfiguration)'

- task: CmdLine@2 # C# main project
  inputs:
    script: 'build-wrapper-win-x86\build-wrapper-win-x86-64.exe --out-dir bw-output "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe" KConnectionWizardWin\KConnectionWizardWin.sln -t:Rebuild /p:configuration=release /p:platform=x64'

# - task: VSBuild@1 
#   inputs:
#     solution: 'KConnectionWizardWin\KConnectionWizardWin.sln'
#     platform: '$(buildPlatform)'
#     configuration: '$(buildConfiguration)'

- task: SonarQubeAnalyze@5

- task: SonarQubePublish@5
  inputs:
    pollingTimeoutSec: '300'

- task: sonar-buildbreaker@8
  inputs:
    SonarQube: 'https://sq.corp.hpicloud.net'

And this is how I analyze .net(C#) project: enter image description here

And this is how I analyze C++ project: enter image description here


Solution

  • For anyone interested

    Below is a correct setup for analysing projects with mix of C++ and C# in Azure pipeline.

    It would be nice if this analysis setup is added to the following documentation under .NET section: https://docs.sonarqube.org/9.9/devops-platform-integration/azure-devops-integration/

    trigger: none
    
    pool: 'Windows2022'
    
    variables:
      buildPlatform: 'x64'
      buildConfiguration: 'Release'
    
    steps:
    - checkout: self
    
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          Invoke-WebRequest -Uri 'https://sq.corp.cloud.net/static/cpp/build-wrapper-win-x86.zip' -OutFile 'build-wrapper.zip'
    .      Expand-Archive -Path 'build-wrapper.zip' -DestinationPath '.' -Force
     
    - task: SonarQubePrepare@5
      inputs:
        SonarQube: 'https://sq.corp.cloud.net'
        scannerMode: 'MSBuild'
        projectKey: 'xxxxx'
        extraProperties: |
          sonar.cfamily.build-wrapper-output=$(Build.SourcesDirectory)\bw_output
          sonar.verbose=true
    
    - task: CmdLine@2
      inputs:
        script: |
          build-wrapper-win-x86\build-wrapper-win-x86-64.exe --out-dir $(Build.SourcesDirectory)\bw_output "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe" CEMConnectionWizardWin\CEMConnectionWizardWin.sln -t:Rebuild /p:configuration=release /p:platform=x64
          
    - task: SonarQubeAnalyze@5
    
    - task: SonarQubePublish@5
      inputs:
        pollingTimeoutSec: '300'
    
    - task: sonar-buildbreaker@8
      inputs:
        SonarQube: 'https://sq.corp.cloud.net'