]> git.ipfire.org Git - thirdparty/systemd.git/blob - tools/coverity.sh
Merge pull request #15167 from ssahani/address-gen-mode
[thirdparty/systemd.git] / tools / coverity.sh
1 #!/usr/bin/env bash
2
3 # The official unmodified version of the script can be found at
4 # https://scan.coverity.com/scripts/travisci_build_coverity_scan.sh
5
6 set -e
7
8 # Declare build command
9 COVERITY_SCAN_BUILD_COMMAND="ninja -C cov-build"
10
11 # Environment check
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"}
16
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
24
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"
28 exit 0
29 fi
30
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"
34 else
35 echo -e "\033[33;1mCoverity Scan NOT configured to run on branch ${TRAVIS_BRANCH}\033[0m"
36 exit 1
37 fi
38
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"
43 exit 1
44 else
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"
48 else
49 WHEN=`echo $AUTH_RES | jq .next_upload_permitted_at`
50 echo -e "\033[33;1mCoverity Scan analysis NOT authorized until $WHEN.\033[0m"
51 exit 1
52 fi
53 fi
54
55 TOOL_DIR=`find $TOOL_BASE -type d -name 'cov-analysis*'`
56 export PATH="$TOOL_DIR/bin:$PATH"
57
58 # Disable CCACHE for cov-build to compilation units correctly
59 export CCACHE_DISABLE=1
60
61 # FUNCTION DEFINITIONS
62 # --------------------
63 _help()
64 {
65 # displays help and exits
66 cat <<-EOF
67 USAGE: $0 [CMD] [OPTIONS]
68
69 CMD
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.
75
76 OPTIONS
77 -h,--help Display this menu and exits
78
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)
83
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'
89 EOF
90 return;
91 }
92
93 _pack()
94 {
95 RESULTS_ARCHIVE=${RESULTS_ARCHIVE:-'analysis-results.tgz'}
96
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`
100
101 PACKED=true
102 }
103
104
105 _build()
106 {
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
113
114 if [ $? != 0 ]; then
115 echo -e "\033[33;1mCoverity Scan Build failed: $TEXT.\033[0m"
116 return 1
117 fi
118
119 [ -z $TAR ] || [ $TAR = false ] && return 0
120
121 if [ "$TAR" = true ]; then
122 _pack
123 fi
124 }
125
126
127 _upload()
128 {
129 # pack results
130 [ -z $PACKED ] || [ $PACKED = false ] && _pack
131
132 # Upload results
133 echo -e "\033[33;1mUploading Coverity Scan Analysis results...\033[0m"
134 response=$(curl \
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" \
142 $UPLOAD_URL)
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"
151 exit 1
152 fi
153
154 echo -e "\n\033[33;1mCoverity Scan Analysis completed successfully.\033[0m"
155 exit 0
156 }
157
158 # PARSE COMMAND LINE OPTIONS
159 # --------------------------
160
161 case $1 in
162 -h|--help)
163 _help
164 exit 0
165 ;;
166 build)
167 CMD='build'
168 TEMP=`getopt -o ho:t --long help,out-dir:,tar -n '$0' -- "$@"`
169 _ec=$?
170 [[ $_ec -gt 0 ]] && _help && exit $_ec
171 shift
172 ;;
173 upload)
174 CMD='upload'
175 TEMP=`getopt -o hd:t: --long help,result-dir:tar: -n '$0' -- "$@"`
176 _ec=$?
177 [[ $_ec -gt 0 ]] && _help && exit $_ec
178 shift
179 ;;
180 *)
181 _help && exit 1 ;;
182 esac
183
184 RESULTS_DIR='cov-int'
185
186 eval set -- "$TEMP"
187 if [ $? != 0 ] ; then exit 1 ; fi
188
189 # extract options and their arguments into variables.
190 if [[ $CMD == 'build' ]]; then
191 TAR=false
192 while true ; do
193 case $1 in
194 -h|--help)
195 _help
196 exit 0
197 ;;
198 -o|--out-dir)
199 RESULTS_DIR="$2"
200 shift 2
201 ;;
202 -t|--tar)
203 TAR=true
204 shift
205 ;;
206 --) _build; shift ; break ;;
207 *) echo "Internal error" ; _help && exit 6 ;;
208 esac
209 done
210
211 elif [[ $CMD == 'upload' ]]; then
212 while true ; do
213 case $1 in
214 -h|--help)
215 _help
216 exit 0
217 ;;
218 -d|--result-dir)
219 CHANGE_DEFAULT_DIR=true
220 RESULTS_DIR="$2"
221 shift 2
222 ;;
223 -t|--tar)
224 RESULTS_ARCHIVE="$2"
225 [ -z $CHANGE_DEFAULT_DIR ] || [ $CHANGE_DEFAULT_DIR = false ] && PACKED=true
226 shift 2
227 ;;
228 --) _upload; shift ; break ;;
229 *) echo "Internal error" ; _help && exit 6 ;;
230 esac
231 done
232
233 fi