coverity-scan.sh 6.5 KB

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