coverity-scan.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. . packaging/installer/functions.sh
  41. JOBS=$(find_processors)
  42. [ -z "${JOBS}" ] && JOBS=1
  43. if command -v ninja 2>&1; then
  44. ninja="$(command -v ninja)"
  45. fi
  46. CMAKE_OPTS="${ninja:+-G Ninja}"
  47. BUILD_OPTS="VERBOSE=1"
  48. [ -n "${ninja}" ] && BUILD_OPTS="-v"
  49. NETDATA_BUILD_DIR="${NETDATA_BUILD_DIR:-./build/}"
  50. if [ -f ".coverity-scan.conf" ]; then
  51. source ".coverity-scan.conf"
  52. fi
  53. repo="${REPOSITORY}"
  54. if [ -z "${repo}" ]; then
  55. fatal "export variable REPOSITORY or set it in .coverity-scan.conf"
  56. fi
  57. repo="${repo//\//%2F}"
  58. email="${COVERITY_SCAN_SUBMIT_MAIL}"
  59. if [ -z "${email}" ]; then
  60. fatal "export variable COVERITY_SCAN_SUBMIT_MAIL or set it in .coverity-scan.conf"
  61. fi
  62. token="${COVERITY_SCAN_TOKEN}"
  63. if [ -z "${token}" ]; then
  64. fatal "export variable COVERITY_SCAN_TOKEN or set it in .coverity-scan.conf"
  65. fi
  66. if ! command -v curl > /dev/null 2>&1; then
  67. fatal "CURL is required for coverity scan to work"
  68. fi
  69. # only print the output of a command
  70. # when debugging is enabled
  71. # used to hide the token when debugging is not enabled
  72. debugrun() {
  73. if [ "${COVERITY_SUBMIT_DEBUG}" = "1" ]; then
  74. run "${@}"
  75. return $?
  76. else
  77. "${@}"
  78. return $?
  79. fi
  80. }
  81. scanit() {
  82. progress "Scanning using coverity"
  83. COVERITY_PATH=$(find "${INSTALL_DIR}" -maxdepth 1 -name 'cov*linux*')
  84. export PATH=${PATH}:${COVERITY_PATH}/bin/
  85. covbuild="${COVERITY_BUILD_PATH}"
  86. [ -z "${covbuild}" ] && covbuild="$(which cov-build 2> /dev/null || command -v cov-build 2> /dev/null)"
  87. if [ -z "${covbuild}" ]; then
  88. fatal "Cannot find 'cov-build' binary in \$PATH. Export variable COVERITY_BUILD_PATH or set it in .coverity-scan.conf"
  89. elif [ ! -x "${covbuild}" ]; then
  90. fatal "The command '${covbuild}' is not executable. Export variable COVERITY_BUILD_PATH or set it in .coverity-scan.conf"
  91. fi
  92. version="$(grep "^#define PACKAGE_VERSION" config.h | cut -d '"' -f 2)"
  93. progress "Working on netdata version: ${version}"
  94. progress "Cleaning up old builds..."
  95. rm -rf "${NETDATA_BUILD_DIR}"
  96. [ -d "cov-int" ] && rm -rf "cov-int"
  97. [ -f netdata-coverity-analysis.tgz ] && run rm netdata-coverity-analysis.tgz
  98. progress "Configuring netdata source..."
  99. USE_SYSTEM_PROTOBUF=1
  100. prepare_cmake_options
  101. run cmake ${NETDATA_CMAKE_OPTIONS}
  102. progress "Analyzing netdata..."
  103. run "${covbuild}" --dir cov-int cmake --build "${NETDATA_BUILD_DIR}" --parallel ${JOBS} -- ${BUILD_OPTS}
  104. echo >&2 "Compressing analysis..."
  105. run tar czvf netdata-coverity-analysis.tgz cov-int
  106. echo >&2 "Sending analysis to coverity for netdata version ${version} ..."
  107. COVERITY_SUBMIT_RESULT=$(debugrun curl --progress-bar \
  108. --form token="${token}" \
  109. --form email="${email}" \
  110. --form file=@netdata-coverity-analysis.tgz \
  111. --form version="${version}" \
  112. --form description="netdata, monitor everything, in real-time." \
  113. https://scan.coverity.com/builds?project="${repo}")
  114. echo "${COVERITY_SUBMIT_RESULT}" | grep -q -e 'Build successfully submitted' || echo >&2 "scan results were not pushed to coverity. Message was: ${COVERITY_SUBMIT_RESULT}"
  115. progress "Coverity scan completed"
  116. }
  117. installit() {
  118. ORIGINAL_DIR="${PWD}"
  119. TMP_DIR="$(mktemp -d /tmp/netdata-coverity-scan-XXXXX)"
  120. progress "Downloading coverity in ${TMP_DIR}..."
  121. cd "${TMP_DIR}"
  122. debugrun curl --remote-name --remote-header-name --show-error --location --data "token=${token}&project=${repo}" https://scan.coverity.com/download/linux64
  123. if [ -f "${COVERITY_BUILD_VERSION}.tar.gz" ]; then
  124. progress "Installing coverity..."
  125. cd "${INSTALL_DIR}"
  126. run sudo tar -z -x -f "${TMP_DIR}/${COVERITY_BUILD_VERSION}.tar.gz" || exit 1
  127. rm "${TMP_DIR}/${COVERITY_BUILD_VERSION}.tar.gz"
  128. COVERITY_PATH=$(find "${INSTALL_DIR}" -maxdepth 1 -name 'cov*linux*')
  129. export PATH=${PATH}:${COVERITY_PATH}/bin/
  130. elif find . -name "*.tar.gz" > /dev/null 2>&1; then
  131. ls ./*.tar.gz
  132. fatal "Downloaded coverity tool tarball does not appear to be the version we were expecting, exiting."
  133. else
  134. fatal "Failed to download coverity tool tarball!"
  135. fi
  136. # Validate the installation
  137. covbuild="$(which cov-build 2> /dev/null || command -v cov-build 2> /dev/null)"
  138. if [ -z "$covbuild" ]; then
  139. fatal "Failed to install coverity."
  140. fi
  141. progress "Coverity scan tools are installed."
  142. cd "$ORIGINAL_DIR"
  143. # Clean temp directory
  144. [ -n "${TMP_DIR}" ] && rm -rf "${TMP_DIR}"
  145. return 0
  146. }
  147. FOUND_OPTS="NO"
  148. while [ -n "${1}" ]; do
  149. if [ "${1}" = "--with-install" ]; then
  150. progress "Running coverity install"
  151. installit
  152. shift 1
  153. elif [ -n "${1}" ]; then
  154. # Clear the default arguments, once you bump into the first argument
  155. if [ "${FOUND_OPTS}" = "NO" ]; then
  156. OTHER_OPTIONS="${1}"
  157. FOUND_OPTS="YES"
  158. else
  159. OTHER_OPTIONS+=" ${1}"
  160. fi
  161. shift 1
  162. else
  163. break
  164. fi
  165. done
  166. echo "Running coverity scan with extra options ${OTHER_OPTIONS}"
  167. scanit "${OTHER_OPTIONS}"