coverity-scan.sh 6.5 KB

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