coverity-scan.sh 6.3 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-2019.03/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-2019.03}"
  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" ]
  45. then
  46. source ".coverity-scan.conf"
  47. fi
  48. repo="${REPOSITORY}"
  49. if [ -z "${repo}" ]; then
  50. fatal "export variable REPOSITORY or set it in .coverity-scan.conf"
  51. fi
  52. repo="${repo//\//%2F}"
  53. email="${COVERITY_SCAN_SUBMIT_MAIL}"
  54. if [ -z "${email}" ]; then
  55. fatal "export variable COVERITY_SCAN_SUBMIT_MAIL or set it in .coverity-scan.conf"
  56. fi
  57. token="${COVERITY_SCAN_TOKEN}"
  58. if [ -z "${token}" ]; then
  59. fatal "export variable COVERITY_SCAN_TOKEN or set it in .coverity-scan.conf"
  60. fi
  61. if ! command -v curl >/dev/null 2>&1; then
  62. fatal "CURL is required for coverity scan to work"
  63. fi
  64. # only print the output of a command
  65. # when debugging is enabled
  66. # used to hide the token when debugging is not enabled
  67. debugrun() {
  68. if [ "${COVERITY_SUBMIT_DEBUG}" = "1" ]
  69. then
  70. run "${@}"
  71. return $?
  72. else
  73. "${@}"
  74. return $?
  75. fi
  76. }
  77. scanit() {
  78. progress "Scanning using coverity"
  79. export PATH="${PATH}:${INSTALL_DIR}/${COVERITY_BUILD_VERSION}/bin/"
  80. covbuild="${COVERITY_BUILD_PATH}"
  81. [ -z "${covbuild}" ] && covbuild="$(which cov-build 2>/dev/null || command -v cov-build 2>/dev/null)"
  82. if [ -z "${covbuild}" ]; then
  83. fatal "Cannot find 'cov-build' binary in \$PATH. Export variable COVERITY_BUILD_PATH or set it in .coverity-scan.conf"
  84. elif [ ! -x "${covbuild}" ]; then
  85. fatal "The command '${covbuild}' is not executable. Export variable COVERITY_BUILD_PATH or set it in .coverity-scan.conf"
  86. fi
  87. version="$(grep "^#define PACKAGE_VERSION" config.h | cut -d '"' -f 2)"
  88. progress "Working on netdata version: ${version}"
  89. progress "Cleaning up old builds..."
  90. run make clean || echo >&2 "Nothing to clean"
  91. [ -d "cov-int" ] && rm -rf "cov-int"
  92. [ -f netdata-coverity-analysis.tgz ] && run rm netdata-coverity-analysis.tgz
  93. progress "Configuring netdata source..."
  94. run autoreconf -ivf
  95. run ./configure ${OTHER_OPTIONS}
  96. progress "Analyzing netdata..."
  97. run "${covbuild}" --dir cov-int make -j${cpus}
  98. echo >&2 "Compressing analysis..."
  99. run tar czvf netdata-coverity-analysis.tgz cov-int
  100. echo >&2 "Sending analysis to coverity for netdata version ${version} ..."
  101. COVERITY_SUBMIT_RESULT=$(debugrun curl --progress-bar \
  102. --form token="${token}" \
  103. --form email="${email}" \
  104. --form file=@netdata-coverity-analysis.tgz \
  105. --form version="${version}" \
  106. --form description="netdata, monitor everything, in real-time." \
  107. https://scan.coverity.com/builds?project="${repo}")
  108. echo "${COVERITY_SUBMIT_RESULT}" | grep -q -e 'Build successfully submitted' || echo >&2 "scan results were not pushed to coverity. Message was: ${COVERITY_SUBMIT_RESULT}"
  109. progress "Coverity scan completed"
  110. }
  111. installit() {
  112. ORIGINAL_DIR="${PWD}"
  113. TMP_DIR="$(mktemp -d /tmp/netdata-coverity-scan-XXXXX)"
  114. progress "Downloading coverity in ${TMP_DIR}..."
  115. cd "${TMP_DIR}"
  116. debugrun curl --remote-name --remote-header-name --show-error --location --data "token=${token}&project=${repo}" https://scan.coverity.com/download/linux64
  117. if [ -f "${COVERITY_BUILD_VERSION}.tar.gz" ]; then
  118. progress "Installing coverity..."
  119. cd "${INSTALL_DIR}"
  120. run sudo tar -z -x -f "${TMP_DIR}/${COVERITY_BUILD_VERSION}.tar.gz" || exit 1
  121. rm "${TMP_DIR}/${COVERITY_BUILD_VERSION}.tar.gz"
  122. export PATH=${PATH}:${INSTALL_DIR}/${COVERITY_BUILD_VERSION}/bin/
  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. # Clean temp directory
  132. [ -n "${TMP_DIR}" ] && rm -rf "${TMP_DIR}"
  133. progress "Coverity scan tools are installed."
  134. cd "$ORIGINAL_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-backend-prometheus-remote-write"
  146. # TODO: enable these plugins too
  147. #OTHER_OPTIONS+=" --enable-plugin-xenstat"
  148. #OTHER_OPTIONS+=" --enable-backend-kinesis"
  149. #OTHER_OPTIONS+=" --enable-backend-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}"