3 # The official unmodified version of the script can be found at
4 # https://scan.coverity.com/scripts/travisci_build_coverity_scan.sh
8 # Declare build command
9 COVERITY_SCAN_BUILD_COMMAND
="ninja -C cov-build"
12 # Use default values if not set
13 SCAN_URL
=${SCAN_URL:="https://scan.coverity.com"}
14 TOOL_BASE
=${TOOL_BASE:="/tmp/coverity-scan-analysis"}
15 UPLOAD_URL
=${UPLOAD_URL:="https://scan.coverity.com/builds"}
17 # These must be set by environment
18 echo -e "\033[33;1mNote: COVERITY_SCAN_PROJECT_NAME and COVERITY_SCAN_TOKEN are available on Project Settings page on scan.coverity.com\033[0m"
19 [ -z "$COVERITY_SCAN_PROJECT_NAME" ] && echo "ERROR: COVERITY_SCAN_PROJECT_NAME must be set" && exit 1
20 [ -z "$COVERITY_SCAN_NOTIFICATION_EMAIL" ] && echo "ERROR: COVERITY_SCAN_NOTIFICATION_EMAIL must be set" && exit 1
21 [ -z "$COVERITY_SCAN_BRANCH_PATTERN" ] && echo "ERROR: COVERITY_SCAN_BRANCH_PATTERN must be set" && exit 1
22 [ -z "$COVERITY_SCAN_BUILD_COMMAND" ] && echo "ERROR: COVERITY_SCAN_BUILD_COMMAND must be set" && exit 1
23 [ -z "$COVERITY_SCAN_TOKEN" ] && echo "ERROR: COVERITY_SCAN_TOKEN must be set" && exit 1
25 # Do not run on pull requests
26 if [ "${TRAVIS_PULL_REQUEST}" = "true" ]; then
27 echo -e "\033[33;1mINFO: Skipping Coverity Analysis: branch is a pull request.\033[0m"
31 # Verify this branch should run
32 if [[ "${TRAVIS_BRANCH^^}" =~
"${COVERITY_SCAN_BRANCH_PATTERN^^}" ]]; then
33 echo -e "\033[33;1mCoverity Scan configured to run on branch ${TRAVIS_BRANCH}\033[0m"
35 echo -e "\033[33;1mCoverity Scan NOT configured to run on branch ${TRAVIS_BRANCH}\033[0m"
39 # Verify upload is permitted
40 AUTH_RES
=`curl -s --form project="$COVERITY_SCAN_PROJECT_NAME" --form token="$COVERITY_SCAN_TOKEN" $SCAN_URL/api/upload_permitted`
41 if [ "$AUTH_RES" = "Access denied" ]; then
42 echo -e "\033[33;1mCoverity Scan API access denied. Check COVERITY_SCAN_PROJECT_NAME and COVERITY_SCAN_TOKEN.\033[0m"
45 AUTH
=`echo $AUTH_RES | jq .upload_permitted`
46 if [ "$AUTH" = "true" ]; then
47 echo -e "\033[33;1mCoverity Scan analysis authorized per quota.\033[0m"
49 WHEN
=`echo $AUTH_RES | jq .next_upload_permitted_at`
50 echo -e "\033[33;1mCoverity Scan analysis NOT authorized until $WHEN.\033[0m"
55 TOOL_DIR
=`find $TOOL_BASE -type d -name 'cov-analysis*'`
56 export PATH
="$TOOL_DIR/bin:$PATH"
58 # Disable CCACHE for cov-build to compilation units correctly
59 export CCACHE_DISABLE
=1
61 # FUNCTION DEFINITIONS
62 # --------------------
65 # displays help and exits
67 USAGE: $0 [CMD] [OPTIONS]
70 build Issue Coverity build
71 upload Upload coverity archive for analysis
72 Note: By default, archive is created from default results directory.
73 To provide custom archive or results directory, see --result-dir
74 and --tar options below.
77 -h,--help Display this menu and exits
79 Applicable to build command
80 ---------------------------
81 -o,--out-dir Specify Coverity intermediate directory (defaults to 'cov-int')
82 -t,--tar bool, archive the output to .tgz file (defaults to false)
84 Applicable to upload command
85 ----------------------------
86 -d, --result-dir Specify result directory if different from default ('cov-int')
87 -t, --tar ARCHIVE Use custom .tgz archive instead of intermediate directory or pre-archived .tgz
88 (by default 'analysis-result.tgz'
95 RESULTS_ARCHIVE
=${RESULTS_ARCHIVE:-'analysis-results.tgz'}
97 echo -e "\033[33;1mTarring Coverity Scan Analysis results...\033[0m"
98 tar czf
$RESULTS_ARCHIVE $RESULTS_DIR
99 SHA
=`git rev-parse --short HEAD`
107 echo -e "\033[33;1mRunning Coverity Scan Analysis Tool...\033[0m"
108 local _cov_build_options
=""
109 #local _cov_build_options="--return-emit-failures 8 --parse-error-threshold 85"
110 eval "${COVERITY_SCAN_BUILD_COMMAND_PREPEND}"
111 COVERITY_UNSUPPORTED
=1 cov-build
--dir $RESULTS_DIR $_cov_build_options sh
-c "$COVERITY_SCAN_BUILD_COMMAND"
112 cov-import-scm
--dir $RESULTS_DIR --scm git
--log $RESULTS_DIR/scm_log.txt
115 echo -e "\033[33;1mCoverity Scan Build failed: $TEXT.\033[0m"
119 [ -z $TAR ] ||
[ $TAR = false
] && return 0
121 if [ "$TAR" = true
]; then
130 [ -z $PACKED ] ||
[ $PACKED = false
] && _pack
133 echo -e "\033[33;1mUploading Coverity Scan Analysis results...\033[0m"
135 --silent --write-out "\n%{http_code}\n" \
136 --form project
=$COVERITY_SCAN_PROJECT_NAME \
137 --form token
=$COVERITY_SCAN_TOKEN \
138 --form email
=$COVERITY_SCAN_NOTIFICATION_EMAIL \
139 --form file=@
$RESULTS_ARCHIVE \
140 --form version
=$SHA \
141 --form description
="Travis CI build" \
143 printf "\033[33;1mThe response is\033[0m\n%s\n" "$response"
144 status_code
=$
(echo "$response" |
sed -n '$p')
145 # Coverity Scan used to respond with 201 on successfully receiving analysis results.
146 # Now for some reason it sends 200 and may change back in the foreseeable future.
147 # See https://github.com/pmem/pmdk/commit/7b103fd2dd54b2e5974f71fb65c81ab3713c12c5
148 if [ "$status_code" != "200" ]; then
149 TEXT
=$
(echo "$response" |
sed '$d')
150 echo -e "\033[33;1mCoverity Scan upload failed: $TEXT.\033[0m"
154 echo -e "\n\033[33;1mCoverity Scan Analysis completed successfully.\033[0m"
158 # PARSE COMMAND LINE OPTIONS
159 # --------------------------
168 TEMP
=`getopt -o ho:t --long help,out-dir:,tar -n '$0' -- "$@"`
170 [[ $_ec -gt 0 ]] && _help
&& exit $_ec
175 TEMP
=`getopt -o hd:t: --long help,result-dir:tar: -n '$0' -- "$@"`
177 [[ $_ec -gt 0 ]] && _help
&& exit $_ec
184 RESULTS_DIR
='cov-int'
187 if [ $?
!= 0 ] ; then exit 1 ; fi
189 # extract options and their arguments into variables.
190 if [[ $CMD == 'build' ]]; then
206 --) _build
; shift ; break ;;
207 *) echo "Internal error" ; _help
&& exit 6 ;;
211 elif [[ $CMD == 'upload' ]]; then
219 CHANGE_DEFAULT_DIR
=true
225 [ -z $CHANGE_DEFAULT_DIR ] ||
[ $CHANGE_DEFAULT_DIR = false
] && PACKED
=true
228 --) _upload
; shift ; break ;;
229 *) echo "Internal error" ; _help
&& exit 6 ;;