SonarQube+cppcheck實現C++代碼掃描

背景

最近接收到C++代碼檢測的需求,一開始上網找了很久,sonar的檢測都是掃描java、web等,很少有C++的,現將自己調研結果記錄於此。

工具準備

  • SonarQube工具:展示掃描報告,版本6.7以上
  • sonar_scanner工具:代碼掃描工具
  • Cppcheck工具:Cppcheck是一種C/C++代碼缺陷靜態檢查工具,不同於C/C++編譯器及其它分析工具,Cppcheck只檢查編譯器檢查不出來的bug,不檢查語法錯誤。

安裝步驟

1、sonar我們可以去官網直接下載https://www.sonarqube.org/downloads/ ,下載解壓之後顯示的是如下的目錄結構

[sonar@bogon sonarqube-7.1]$ ls
bin  conf  COPYING  data  elasticsearch  extensions  lib  logs  temp  web

2、SonarQube的默認C/C++插件CFamily是收費的,但有個作者自己寫了個開源的cxx插件,下載地址https://github.com/SonarOpenCommunity/sonar-cxx/releases,把jar文件下載下來,然後放到你的sonarqube目錄/extensions/plugins目錄下

 

[sonar@bogon plugins]$ ls
sonar-csharp-plugin-7.0.1.4822.jar      sonar-l10n-zh                        sonar-scm-svn-plugin-1.7.0.1017.jar
sonar-cxx-plugin-1.1.0.jar              sonar-l10n-zh-plugin-1.21.jar        sonar-typescript-plugin-1.6.0.2388.jar
sonar-flex-plugin-2.4.0.1222.jar        sonar-php-plugin-2.13.0.3107.jar     sonar-xml-plugin-1.5.0.1373.jar
sonar-java-plugin-5.6.0.15032.jar       sonar-python-plugin-1.9.1.2080.jar
sonar-javascript-plugin-5.1.1.7506.jar  sonar-scm-git-plugin-1.4.0.1037.jar

多了個sonar-cxx-plugin-1.1.0.jar的插件

3、進入到sonar的解壓目錄中的conf中,找到sonar.properties文件中 ,修改裏面配置數據庫信息(依據自己的數據庫修改,Mysql版本不要使用8.0以上的,不然後續sonar服務會啓動不起來,具體原因未知,當時配了好幾次才發現數據庫版本問題導致服務啓動失敗)

sonar.jdbc.username=root
sonar.jdbc.password=Temp#1234
sonar.sorceEncoding=UTF-8
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonardb?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=true

4、mysql裏新增數據庫

CREATE DATABASE sonardb DEFAULT CHARACTER SET utf8;

5、進入bin目錄下啓動服務(根據自己的系統選擇對應的啓動腳本,我的是Linux_64)

[sonar@bogon sonarqube-7.1]$ cd bin/
[sonar@bogon bin]$ ls
jsw-license  linux-x86-32  linux-x86-64  macosx-universal-64  windows-x86-32  windows-x86-64
[sonar@bogon bin]$ cd linux-x86-64/
[sonar@bogon linux-x86-64]$ ls
lib  SonarQube.pid  sonar.sh  wrapper
[sonar@bogon linux-x86-64]$ ./sonar.sh start

6、界面訪問

​ http://10.206.142.87:9000(默認登錄賬號密碼都是admin)

登錄之後點擊“代碼規則”,便可在頁面看到“C++ (Community)”

7、安裝Cppcheck和sonar-scanner

wget https://github.com/danmar/cppcheck/archive/1.89.zip
wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.3.778-linux.zip

下載完後,解壓出來,並重新命名sonar_scanner和配置sonar.properties

#重命名爲sonar-scanner
mv sonar-scanner-cli-3.0.3.778-linux sonar-scanner
# 修改sonar_scanner.properties
[sonar@bogon ~]$ cd sonar-scanner/conf/
[sonar@bogon conf]$ ls
sonar-scanner.properties
[sonar@bogon conf]$vim sonar-scanner.properties
#把註釋去掉就行
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8

最後將添加sonar、cppcheck、sonar-scanner添加到環境變量中

vim /etc/profile

#添加一下代碼(根據自己路徑修改)
export SONAR_HOME=/home/sonar/sonarqube-7.1
export SONAR_SCANNER_HOME=/home/sonar/sonar-scanner
export CPPCHECK_HOME=/home/sonar/cppcheck-1.88
export PATH=$PATH:$SONAR_HOME:$SONAR_SCANNER_HOME/bin:$CPPCHECK_HOME

source /etc/profile  

8、修改質量配置

sonar平臺質量配置,新增C++的質量配置項

激活更過規則

激活完成後,將該規則項設置爲默認。

9、到此sonar的安裝,已全部完成,接下來我們寫個例子測試下吧

[chenpk@/home/chenpk/sippro]$vim sonar-project.properties
sonar.projectKey=sippro    #sonar平臺中相對應項目的key
sonar.projectName=sippro   #sonar平臺中相對應項目的名字
sonar.projectVersion=1.0   #sonar平臺中相對應項目的項目版本
sonar.sources=./           #sonar檢測的源文件目錄,‘.’表示當前根目錄下的所有文件目錄
sonar.language=c++         #sonar檢測的編程語言種類
sonar.sourceEncoding=UTF-8 #sonar平臺中相對應項目的編碼格式

[chenpk@/home/chenpk/sippro]$ls
build  cmake  CMakeLists.txt  eva  han  help  muduo  MysqlInterface  README.md  sonar-project.properties

執行sonar-scanner命令進行檢測

[chenpk@/home/chenpk/sippro]$sonar-scanner
INFO: Scanner configuration file: /usr/local/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/chenpk/sippro/sonar-project.properties
INFO: SonarQube Scanner 3.2.0.1227
INFO: Java 1.8.0_121 Oracle Corporation (64-bit)
INFO: Linux 3.10.0-862.el7.x86_64 amd64
INFO: User cache: /home/chenpk/.sonar/cache
INFO: SonarQube server 7.1.0
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Publish mode
INFO: Load global settings
INFO: Load global settings (done) | time=193ms
INFO: Server id: AW0e9nx7Z8oUve48CK6d
INFO: User cache: /home/chenpk/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=98ms
INFO: Load/download plugins
INFO: Plugin [l10nzh] defines 'l10nen' as base plugin. This metadata can be removed from manifest of l10n plugins since version 5.2.
.......
INFO: 93 files had no CPD blocks
INFO: Calculating CPD for 150 files
INFO: CPD calculation finished
INFO: Analysis report generated in 690ms, dir size=3 MB
INFO: Analysis reports compressed in 610ms, zip size=1 MB
INFO: Analysis report uploaded in 333ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://10.206.142.87:19000/dashboard/index/sippro
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://10.206.142.87:19000/api/ce/task?id=AW0fYZa2lfgfkyZw33vG
INFO: Task total time: 19.459 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 21.966s
INFO: Final Memory: 98M/771M
INFO: ------------------------------------------------------------------------

檢測完之後,登錄平臺查看

後期打算把Jenkins自動化結合sonar一起部署一套

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章