diff --git a/runCFSscript.sh b/runCFSscript.sh index 3ca80d032..c8e921c06 100755 --- a/runCFSscript.sh +++ b/runCFSscript.sh @@ -1,7 +1,7 @@ #!/bin/bash cfs_apps=( apps/sch_lab apps/ci_lab apps/to_lab apps/sample_app tools/cFS-GroundSystem tools/elf2cfetbl tools/tblCRCTool apps/cf apps/cs apps/ds apps/fm apps/hk apps/hs apps/lc apps/md apps/mm apps/sc ) -scriptNums=( 1 2 3 4 5 6 7 8 ) +scriptNums=( 1 2 3 4 5 6 7 8 9 ) # Process each element to get the substring after the last '/' for app in "${cfs_apps[@]}"; do @@ -23,14 +23,15 @@ cat << EOF 4) format-check.sh 5) run-clang-format.sh 6) dox.sh - 7) cfe_functionaltests.sh - 8) cfe_lcov.sh + 7) static-analysis.sh + 8) cfe_functionaltests.sh + 9) cfe_lcov.sh EOF read userNum while [[ ! " ${scriptNums[*]} " =~ " ${userNum} " ]]; do echo "try again"; read userNum; done -if [[ "$userNum" -ge 2 && "$userNum" -le 6 ]]; then +if [[ "$userNum" -ge 2 && "$userNum" -le 7 ]]; then # get user cFS application echo -e "\n\ Enter the cFS application name that you want to run the script on\n\ @@ -63,9 +64,12 @@ case $userNum in ./scripts/dox.sh "${userApp##*/}" ;; 7) - ./scripts/cfe_functionaltests.sh + ./scripts/static-analysis.sh "${userApp##*/}" ;; 8) + ./scripts/cfe_functionaltests.sh + ;; + 9) ./scripts/cfe_lcov.sh ;; esac \ No newline at end of file diff --git a/scripts/static-analysis.sh b/scripts/static-analysis.sh new file mode 100755 index 000000000..cbd65c43b --- /dev/null +++ b/scripts/static-analysis.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +app=$1 + +errorDir="${app}_staticAnalysis_errors" +DIR=$(pwd) # Save the current working directory to DIR +cppcheck_xslt_path="nasa/cFS/main/.github/scripts" +mkdir ${errorDir} + +# software dependencies +sudo apt-get install cppcheck xsltproc -y +npm install @microsoft/sarif-multitool + +if [[ " $applist " =~ " $app " ]]; then + # apps + source="apps/${app}" + strict_dir_list="./fsw" + cmake_project_options="" +elif [ ${app} = "cfe" ]; then + # cfe + source="cfe" + strict_dir_list="./modules/core_api/fsw ./modules/core_private/fsw ./modules/es/fsw ./modules/evs/fsw ./modules/fs/fsw ./modules/msg/fsw ./modules/resourceid/fsw ./modules/sb/fsw ./modules/sbr/fsw ./modules/tbl/fsw ./modules/time/fsw -UCFE_PLATFORM_TIME_CFG_CLIENT -DCFE_PLATFORM_TIME_CFG_SERVER" + cmake_project_options="" +elif [ ${app} = "osal" ]; then + # osal + source="osal" + strict_dir_list="./src/bsp ./src/os" + cmake_project_options="-DENABLE_UNIT_TESTS=TRUE -DOSAL_OMIT_DEPRECATED=TRUE -DOSAL_SYSTEM_BSPTYPE=generic-linux" +elif [ ${app} = "psp" ]; then + # psp + source="psp" + strict_dir_list="./fsw" + cmake_project_options="" +fi + +# Fetch conversion XSLT +wget -O cppcheck-xml2text.xslt https://raw.githubusercontent.com/${cppcheck_xslt_path}/cppcheck-xml2text.xslt +wget -O cppcheck-merge.xslt https://raw.githubusercontent.com/${cppcheck_xslt_path}/cppcheck-merge.xslt + +# For a CMake-based project, get the list of files by setting up a build with CMAKE_EXPORT_COMPILE_COMMANDS=ON and +# referencing the compile_commands.json file produced by the tool. This will capture the correct include paths and +# compile definitions based on how the source is actually compiled. +if [ -n "$cmake_project_options" ]; then + # CMake Setup + cmake -DCMAKE_INSTALL_PREFIX=$DIR/staging -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=debug ${cmake_project_options} -S ${source} -B build + export CPPCHECK_OPTS="--project=${DIR}/build/compile_commands.json" +else + # Non-CMake Setup + export CPPCHECK_OPTS="${DIR}/${source}" +fi + +# Run general cppcheck +cppcheck --force --inline-suppr --xml ${CPPCHECK_OPTS} 2> ${errorDir}/cppcheck_err.xml + +# Run strict static analysis for selected portions of source code +if [ -n "$strict_dir_list" ]; then + + cd ${source} + + # Run stric cpp check + cppcheck --force --inline-suppr --std=c99 --language=c --enable=warning,performance,portability,style --suppress=variableScope --inconclusive --xml ${strict_dir_list} 2> ${DIR}/${errorDir}/strict_cppcheck_err.xml + + cd "${DIR}" + + # Merge cppcheck results + mv ${errorDir}/cppcheck_err.xml ${errorDir}/general_cppcheck_err.xml + xsltproc --stringparam merge_file ${errorDir}/strict_cppcheck_err.xml cppcheck-merge.xslt ${errorDir}/general_cppcheck_err.xml > ${errorDir}/cppcheck_err.xml +fi + +# Convert cppcheck results to SARIF +npx "@microsoft/sarif-multitool" convert "${errorDir}/cppcheck_err.xml" --tool "CppCheck" --output "${errorDir}/cppcheck_err.sarif" + +# Convert cppcheck results to Markdown +xsltproc cppcheck-xml2text.xslt ${errorDir}/cppcheck_err.xml | tee ${errorDir}/cppcheck_err.txt + +# Check for reported errors +tail -n 1 ${errorDir}/cppcheck_err.txt | grep -q '^\*\*0 error(s) reported\*\*$'