coverity-scan.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/env bash
  2. #
  3. # Coverity scan script
  4. #
  5. # Copyright: SPDX-License-Identifier: GPL-3.0-or-later
  6. #
  7. # Author : Costa Tsaousis (costa@netdata.cloud)
  8. # Author : Pawel Krupa (paulfantom)
  9. # Author : Pavlos Emm. Katsoulakis (paul@netdata.cloud)
  10. # shellcheck disable=SC1091,SC2230,SC2086
  11. # To run manually, save configuration to .coverity-scan.conf like this:
  12. #
  13. # the repository to report to coverity - devs can set here their own fork
  14. # REPOSITORY="netdata/netdata"
  15. #
  16. # the email of the developer, as given to coverity
  17. # COVERITY_SCAN_SUBMIT_MAIL="you@example.com"
  18. #
  19. # the token given by coverity to the developer
  20. # COVERITY_SCAN_TOKEN="TOKEN taken from Coverity site"
  21. #
  22. # the absolute path of the cov-build - optional
  23. # COVERITY_BUILD_PATH="/opt/cov-analysis-linux64-2021.12/bin/cov-build"
  24. #
  25. # when set, the script will print on screen the curl command that submits the build to coverity
  26. # this includes the token, so the default is not to print it.
  27. # COVERITY_SUBMIT_DEBUG=1
  28. #
  29. # Override the standard coverity build version we know is supported
  30. # COVERITY_BUILD_VERSION="cov-analysis-linux64-2019.03"
  31. #
  32. # All these variables can also be exported before running this script.
  33. #
  34. # If the first parameter of this script is "install",
  35. # coverity build tools will be downloaded and installed in /opt/coverity
  36. set -e
  37. INSTALL_DIR="/opt"
  38. # the version of coverity to use
  39. COVERITY_BUILD_VERSION="${COVERITY_BUILD_VERSION:-cov-analysis-linux64-2023.6.2}"
  40. SCRIPT_SOURCE="$(
  41. self=${0}
  42. while [ -L "${self}" ]
  43. do
  44. cd "${self%/*}" || exit 1
  45. self=$(readlink "${self}")
  46. done
  47. cd "${self%/*}" || exit 1
  48. echo "$(pwd -P)/${self##*/}"
  49. )"
  50. REPO_ROOT="$(dirname "${SCRIPT_SOURCE}")/../.."
  51. . "${REPO_ROOT}/packaging/installer/functions.sh"
  52. JOBS=$(find_processors)
  53. [ -z "${JOBS}" ] && JOBS=1
  54. if command -v ninja 2>&1; then
  55. ninja="$(command -v ninja)"
  56. fi
  57. CMAKE_OPTS="${ninja:+-G Ninja}"
  58. BUILD_OPTS="VERBOSE=1"
  59. [ -n "${ninja}" ] && BUILD_OPTS="-v"
  60. NETDATA_BUILD_DIR="${NETDATA_BUILD_DIR:-./build/}"
  61. if [ -f ".coverity-scan.conf" ]; then
  62. source ".coverity-scan.conf"
  63. fi
  64. repo="${REPOSITORY}"
  65. if [ -z "${repo}" ]; then
  66. fatal "export variable REPOSITORY or set it in .coverity-scan.conf"
  67. fi
  68. repo="${repo//\//%2F}"
  69. email="${COVERITY_SCAN_SUBMIT_MAIL}"
  70. if [ -z "${email}" ]; then
  71. fatal "export variable COVERITY_SCAN_SUBMIT_MAIL or set it in .coverity-scan.conf"
  72. fi
  73. token="${COVERITY_SCAN_TOKEN}"
  74. if [ -z "${token}" ]; then
  75. fatal "export variable COVERITY_SCAN_TOKEN or set it in .coverity-scan.conf"
  76. fi
  77. if ! command -v curl > /dev/null 2>&1; then
  78. fatal "CURL is required for coverity scan to work"
  79. fi
  80. # only print the output of a command
  81. # when debugging is enabled
  82. # used to hide the token when debugging is not enabled
  83. debugrun() {
  84. if [ "${COVERITY_SUBMIT_DEBUG}" = "1" ]; then
  85. run "${@}"
  86. return $?
  87. else
  88. "${@}"
  89. return $?
  90. fi
  91. }
  92. scanit() {
  93. progress "Scanning using coverity"
  94. COVERITY_PATH=$(find "${INSTALL_DIR}" -maxdepth 1 -name 'cov*linux*')
  95. export PATH=${PATH}:${COVERITY_PATH}/bin/
  96. covbuild="${COVERITY_BUILD_PATH}"
  97. [ -z "${covbuild}" ] && covbuild="$(which cov-build 2> /dev/null || command -v cov-build 2> /dev/null)"
  98. if [ -z "${covbuild}" ]; then
  99. fatal "Cannot find 'cov-build' binary in \$PATH. Export variable COVERITY_BUILD_PATH or set it in .coverity-scan.conf"
  100. elif [ ! -x "${covbuild}" ]; then
  101. fatal "The command '${covbuild}' is not executable. Export variable COVERITY_BUILD_PATH or set it in .coverity-scan.conf"
  102. fi
  103. cd "${REPO_ROOT}" || exit 1
  104. version="$(grep "^#define PACKAGE_VERSION" config.h | cut -d '"' -f 2)"
  105. progress "Working on netdata version: ${version}"
  106. progress "Cleaning up old builds..."
  107. rm -rf "${NETDATA_BUILD_DIR}"
  108. [ -d "cov-int" ] && rm -rf "cov-int"
  109. [ -f netdata-coverity-analysis.tgz ] && run rm netdata-coverity-analysis.tgz
  110. progress "Configuring netdata source..."
  111. USE_SYSTEM_PROTOBUF=1
  112. ENABLE_GO=0
  113. prepare_cmake_options
  114. run cmake ${NETDATA_CMAKE_OPTIONS}
  115. progress "Analyzing netdata..."
  116. run "${covbuild}" --dir cov-int cmake --build "${NETDATA_BUILD_DIR}" --parallel ${JOBS} -- ${BUILD_OPTS}
  117. echo >&2 "Compressing analysis..."
  118. run tar czvf netdata-coverity-analysis.tgz cov-int
  119. echo >&2 "Sending analysis to coverity for netdata version ${version} ..."
  120. COVERITY_SUBMIT_RESULT=$(debugrun curl --progress-bar \
  121. --form token="${token}" \
  122. --form email="${email}" \
  123. --form file=@netdata-coverity-analysis.tgz \
  124. --form version="${version}" \
  125. --form description="netdata, monitor everything, in real-time." \
  126. https://scan.coverity.com/builds?project="${repo}")
  127. echo "${COVERITY_SUBMIT_RESULT}" | grep -q -e 'Build successfully submitted' || echo >&2 "scan results were not pushed to coverity. Message was: ${COVERITY_SUBMIT_RESULT}"
  128. progress "Coverity scan completed"
  129. }
  130. installit() {
  131. ORIGINAL_DIR="${PWD}"
  132. TMP_DIR="$(mktemp -d /tmp/netdata-coverity-scan-XXXXX)"
  133. progress "Downloading coverity in ${TMP_DIR}..."
  134. cd "${TMP_DIR}"
  135. debugrun curl --remote-name --remote-header-name --show-error --location --data "token=${token}&project=${repo}" https://scan.coverity.com/download/linux64
  136. if [ -f "${COVERITY_BUILD_VERSION}.tar.gz" ]; then
  137. progress "Installing coverity..."
  138. cd "${INSTALL_DIR}"
  139. run sudo tar -z -x -f "${TMP_DIR}/${COVERITY_BUILD_VERSION}.tar.gz" || exit 1
  140. rm "${TMP_DIR}/${COVERITY_BUILD_VERSION}.tar.gz"
  141. COVERITY_PATH=$(find "${INSTALL_DIR}" -maxdepth 1 -name 'cov*linux*')
  142. export PATH=${PATH}:${COVERITY_PATH}/bin/
  143. elif find . -name "*.tar.gz" > /dev/null 2>&1; then
  144. ls ./*.tar.gz
  145. fatal "Downloaded coverity tool tarball does not appear to be the version we were expecting, exiting."
  146. else
  147. fatal "Failed to download coverity tool tarball!"
  148. fi
  149. # Validate the installation
  150. covbuild="$(which cov-build 2> /dev/null || command -v cov-build 2> /dev/null)"
  151. if [ -z "$covbuild" ]; then
  152. fatal "Failed to install coverity."
  153. fi
  154. progress "Coverity scan tools are installed."
  155. cd "$ORIGINAL_DIR"
  156. # Clean temp directory
  157. [ -n "${TMP_DIR}" ] && rm -rf "${TMP_DIR}"
  158. return 0
  159. }
  160. FOUND_OPTS="NO"
  161. while [ -n "${1}" ]; do
  162. if [ "${1}" = "--with-install" ]; then
  163. progress "Running coverity install"
  164. installit
  165. shift 1
  166. elif [ -n "${1}" ]; then
  167. # Clear the default arguments, once you bump into the first argument
  168. if [ "${FOUND_OPTS}" = "NO" ]; then
  169. OTHER_OPTIONS="${1}"
  170. FOUND_OPTS="YES"
  171. else
  172. OTHER_OPTIONS+=" ${1}"
  173. fi
  174. shift 1
  175. else
  176. break
  177. fi
  178. done
  179. echo "Running coverity scan with extra options ${OTHER_OPTIONS}"
  180. scanit "${OTHER_OPTIONS}"