coverity-scan.sh 6.7 KB

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