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)”

SonarQube+cppcheck实现C++代码扫描_第1张图片

7、修改质量配置

sonar平台质量配置,新增C++的质量配置项

SonarQube+cppcheck实现C++代码扫描_第2张图片

激活更过规则

SonarQube+cppcheck实现C++代码扫描_第3张图片

SonarQube+cppcheck实现C++代码扫描_第4张图片

SonarQube+cppcheck实现C++代码扫描_第5张图片

激活完成后,将该规则项设置为默认。

8、安装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  

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: ------------------------------------------------------------------------

检测完之后,登录平台查看

SonarQube+cppcheck实现C++代码扫描_第6张图片

SonarQube+cppcheck实现C++代码扫描_第7张图片

后期打算把Jenkins自动化结合sonar一起部署一套

你可能感兴趣的:(sonar)