install_cppcheck.sh
#!/bin/bash
AUTO_SH_PATH=$(pwd)
CPPCHECK_FN=cppcheck-1.89.zip
DIR_CPPCHECK_FN=cppcheck-1.89
DIR_INSTALLATION=cppcheck
MODI_MAKEFILE_SH_FN=setcc.sh
########################################################################################
function check_zip_files {
clear
if [ -f ${CPPCHECK_FN} ];then
echo -e "zip file ready:[${CPPCHECK_FN}]......"
echo
else
echo "file[${CPPCHECK_FN}] not exist!"
exit -1
fi
}
function decompression_zip_files {
clear
if [ ! -d ${DIR_CPPCHECK_FN} ]; then
unzip ${CPPCHECK_FN}
fi
ls_res=$(ls)
echo -e "${ls_res}\n"
echo -e "dir ready:[${DIR_CPPCHECK_FN}]......"
}
set_source_bashrc() {
sh_type=$(ls -l `which sh` | awk -F '>' '{print $2}')
if [[ ${sh_type} = " bash" ]]; then
source ${HOME}/.bashrc
else
echo -e "\nATTENTION: !!!!!!!!!!!!"
echo -e "\ncurrent sh is [${sh_type}]"
echo "you need exec [source \${HOME}/.bashrc] out of install_CPPCHECK.sh......"
echo "if you are dash......"
echo "if you have root authority......"
echo "you can exec[sudo dpkg-reconfigure dash] then choice[no]......"
fi
}
function set_cppcheck_env_config {
clear
cd ${HOME}
if [ -f .bashrc ];then
str_e4_line=$(tac .bashrc | sed -n '4p')
if [[ ${str_e4_line} = "# for cppcheck" ]];then
echo "set cppcheck env success......"
else
sed -i '$a\# for cppcheck' .bashrc
# set make install value for cppcheck makefile
str_e1_e2_line=${AUTO_SH_PATH}/${DIR_INSTALLATION}/
str_eval_e2="sed -i '\$a\\export PREFIX=${str_e1_e2_line}' .bashrc"
str_eval_e1="sed -i '\$a\\export FILESDIR=${str_e1_e2_line}' .bashrc"
echo ${str_eval_e2}
eval ${str_eval_e2}
echo ${str_eval_e1}
eval ${str_eval_e1}
set_source_bashrc
# set path
str_e3_line=${AUTO_SH_PATH}/${DIR_INSTALLATION}/bin
str_eval_e3="sed -i '\$a\\export PATH=${str_e3_line}:\$PATH' .bashrc"
echo ${str_eval_e3}
eval ${str_eval_e3}
echo -e "\nshow [.bashrc] content......."
echo "########################################################################################"
cat ${HOME}/.bashrc
echo "########################################################################################"
fi
fi
}
set_profile_bin_path_process() {
cd ${1}
pwd
make clean
make install
echo
read -n1 -p ">>> install ok[y/n]?" install_ok
if [[ ${install_ok} = "y" ]]; then
echo -e "\ninstall ${2} success ......"
else
echo -e "\ninstall ${2} failure ......"
exit -1
fi
echo
cd ${AUTO_SH_PATH}
}
function set_profile_bin_path {
clear
# mkdir install dir
cd ${AUTO_SH_PATH}
pwd
mkdir -p ${DIR_INSTALLATION}
echo -e "executable program will install in [${HOME}/${DIR_INSTALLATION}]......\n"
#set process automake CPPCHECK
set_profile_bin_path_process ${AUTO_SH_PATH}/${DIR_CPPCHECK_FN} CPPCHECK
# source .bashrc
set_source_bashrc
}
function set_cppcheck_make_sh {
clear
echo "set_cppcheck_make_sh......"
if [ -f ${MODI_MAKEFILE_SH_FN} ];then
chmod +x ${MODI_MAKEFILE_SH_FN}
cp ${MODI_MAKEFILE_SH_FN} ${AUTO_SH_PATH}/${DIR_INSTALLATION}/bin/
else
echo "set_cppcheck_make_sh failure......"
fi
}
function __is_continue {
echo
echo ">>>>>>continue? input any key to continue......"
local con=""
read -n 1 con
}
function auto_install {
check_zip_files
__is_continue
decompression_zip_files
__is_continue
set_cppcheck_env_config
__is_continue
set_profile_bin_path
__is_continue
set_cppcheck_make_sh
__is_continue
# last source .bashrc
set_source_bashrc
}
########################################################################################
########################################################################################
function my_menu {
clear
echo
echo -e "\t\t/** auto install CPPCHECK tool **/"
echo
echo -e "\ta.auto install"
echo -e "\t0.quit"
echo -e "\t1.check zip files"
echo -e "\t2.decompression zip files"
echo -e "\t3.set cppcheck env config"
echo -e "\t4.set profile and bin path"
echo -e "\t5.set cppcheck make sh"
echo -en "\n\tEnter option:"
read -n 1 option
}
function start_menu {
while [ 1 ]
do
my_menu
case $option in
0) break;;
1) check_zip_files;;
2) decompression_zip_files;;
3) set_cppcheck_env_config;;
4) set_profile_bin_path;;
5) set_cppcheck_make_sh;;
a) auto_install;;
*) clear
echo "sorry, wrong selection [$option]!!!"
esac
echo -en "\n\n\t\tHit any key to continue"
read -n1 continue_option
done
clear
}
start_menu
setcc.sh
#!/bin/bash
cur_pwd=$(pwd)
MAKE_FN=Makefile
r_head=
r_check_op=
op_str=
is_default_cfg=
function pre_check_make {
# if had make cppcheck , delete config
if [ -s ${MAKE_FN} ];then
str_e5_line=$(tac Makefile | sed -n '5p')
if [[ ${str_e5_line} = "# for cppcheck" ]];then
sed -i '/# for cppcheck/,/cat result.txt/d' ${MAKE_FN}
fi
else
echo "pwd:[${cur_pwd}] can't find Makefile or Makefile is NULL!!!"
exit -1;
fi
}
set_cppcheck_makefile_rules () {
sed -i '$a\# for cppcheck' ${MAKE_FN}
sed -i '$a\TARG_CPPCHECK = check' ${MAKE_FN}
sed -i '$a\$(TARG_CPPCHECK):' ${MAKE_FN}
if [[ ${r_head} = 'y' ]];then
str_eval_op="sed -i '\$a\\\\tcppcheck --enable=${op_str} \$(INCPATH) \*.c 2>result.txt' ${MAKE_FN}"
eval ${str_eval_op}
else
str_eval_op="sed -i '\$a\\\\tcppcheck --enable=${op_str} \*.c 2>result.txt' ${MAKE_FN}"
eval ${str_eval_op}
fi
sed -i '$a\\tcat result.txt' ${MAKE_FN}
}
check_op(){
clear
echo
echo -e "\tcheck options: [with head file[${r_head}]]"
echo
echo -e "\t1) all"
echo -e "\t2) warning"
echo -e "\t3) style"
echo -e "\t4) performance"
echo -e "\t5) portability"
echo -e "\t6) information"
echo -e "\t7) unusedFunction"
echo -e "\t8) missingInclude"
echo -e "\t\t view details: [cppcheck --help] "
echo
read -n1 -p " choice: " r_check_op
echo
}
set_check_op (){
while [ 1 ]
do
read -n1 -p "check with head files[y/n]?" r_head
check_op
case ${r_check_op} in
1) op_str=all
break;;
2) op_str=warning
break;;
3) op_str=style
break;;
4) op_str=performance
break;;
5) op_str=portability
break;;
6) op_str=information
break;;
7) op_str=unusedFunction
break;;
8) op_str=missingInclude
break;;
*) clear
echo "sorry, wrong selection [$r_check_op]!!!"
esac
echo -en "\n\n\t\tHit any key to continue"
read -n1 continue_option
done
clear
}
# control is default run
function is_default_config {
if [[ ${is_default_cfg} = "n" ]]; then
r_head=y
op_str=all
pre_check_make
set_cppcheck_makefile_rules
echo "you can use [make check] to start cppcheck......"
elif [[ ${is_default_cfg} = "y" ]]; then
pre_check_make
set_check_op
set_cppcheck_makefile_rules
echo "you can use [make check] to start cppcheck......"
elif [[ ${is_default_cfg} = "clean" ]]; then
pre_check_make
fi
}
if [[ $1 = "" ]];then
is_default_cfg=n
elif [[ $1 = "config" ]];then
is_default_cfg=y
elif [[ $1 = "clean" ]];then
is_default_cfg=clean
fi
is_default_config
1、在自己服务器上创建目录 cppcheck_tool (目录名无限制), 将三个文件拷贝到该目录下
2、执行 install_cppcheck.sh, 顺序安装[a],按照提示完成安装即可
3、如何使用
1)单个文件 cppcheck XXX.c 即可
2)嵌入Makefile: 到检测源码目录下执行如下命令(setcc.sh 会在当前目录下的Makefile中加入cppcheck配置):
sh setcc.sh 默认加入头文件,并检查所有检查项目。 一般我们用这个。
sh setcc.sh clean 将Makefile中的cppcheck配置删除。上传svn前,需要执行这个。
sh setcc.sh config 进入扫描配置,根据自己的需求选择那些扫描选项。 根据个人需求是否使用。
然后运行[make check]可以开始静态代码扫描,扫描结果保存在[result.txt]中
3)cppcheck --help 可以查看如何使用更多选项
使用注意事项
- 如果install_cppcheck.sh 自动安装失败了,可以在选项中该步骤重新安装
- 如果未生效,执行source ~/.bashrc
- shell 版本为dash时,如何安装可以咨询我,一般都为bash
官方网站:http://cppcheck.net/