xref: /aosp_15_r20/external/libbpf/scripts/coverity.sh (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker#!/bin/bash
2*f7c14bbaSAndroid Build Coastguard Worker# Taken from: https://scan.coverity.com/scripts/travisci_build_coverity_scan.sh
3*f7c14bbaSAndroid Build Coastguard Worker# Local changes are annotated with "#[local]"
4*f7c14bbaSAndroid Build Coastguard Worker
5*f7c14bbaSAndroid Build Coastguard Workerset -e
6*f7c14bbaSAndroid Build Coastguard Worker
7*f7c14bbaSAndroid Build Coastguard Worker# Environment check
8*f7c14bbaSAndroid Build Coastguard Workerecho -e "\033[33;1mNote: COVERITY_SCAN_PROJECT_NAME and COVERITY_SCAN_TOKEN are available on Project Settings page on scan.coverity.com\033[0m"
9*f7c14bbaSAndroid Build Coastguard Worker[ -z "$COVERITY_SCAN_PROJECT_NAME" ] && echo "ERROR: COVERITY_SCAN_PROJECT_NAME must be set" && exit 1
10*f7c14bbaSAndroid Build Coastguard Worker[ -z "$COVERITY_SCAN_NOTIFICATION_EMAIL" ] && echo "ERROR: COVERITY_SCAN_NOTIFICATION_EMAIL must be set" && exit 1
11*f7c14bbaSAndroid Build Coastguard Worker[ -z "$COVERITY_SCAN_BRANCH_PATTERN" ] && echo "ERROR: COVERITY_SCAN_BRANCH_PATTERN must be set" && exit 1
12*f7c14bbaSAndroid Build Coastguard Worker[ -z "$COVERITY_SCAN_BUILD_COMMAND" ] && echo "ERROR: COVERITY_SCAN_BUILD_COMMAND must be set" && exit 1
13*f7c14bbaSAndroid Build Coastguard Worker[ -z "$COVERITY_SCAN_TOKEN" ] && echo "ERROR: COVERITY_SCAN_TOKEN must be set" && exit 1
14*f7c14bbaSAndroid Build Coastguard Worker
15*f7c14bbaSAndroid Build Coastguard WorkerPLATFORM=`uname`
16*f7c14bbaSAndroid Build Coastguard Worker#[local] Use /var/tmp for TOOL_ARCHIVE and TOOL_BASE, as on certain systems
17*f7c14bbaSAndroid Build Coastguard Worker# /tmp is tmpfs and is sometimes too small to handle all necessary tooling
18*f7c14bbaSAndroid Build Coastguard WorkerTOOL_ARCHIVE=/var//tmp/cov-analysis-${PLATFORM}.tgz
19*f7c14bbaSAndroid Build Coastguard WorkerTOOL_URL=https://scan.coverity.com/download/${PLATFORM}
20*f7c14bbaSAndroid Build Coastguard WorkerTOOL_BASE=/var/tmp/coverity-scan-analysis
21*f7c14bbaSAndroid Build Coastguard WorkerUPLOAD_URL="https://scan.coverity.com/builds"
22*f7c14bbaSAndroid Build Coastguard WorkerSCAN_URL="https://scan.coverity.com"
23*f7c14bbaSAndroid Build Coastguard Worker
24*f7c14bbaSAndroid Build Coastguard Worker# Do not run on pull requests
25*f7c14bbaSAndroid Build Coastguard Workerif [ "${TRAVIS_PULL_REQUEST}" = "true" ]; then
26*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\033[33;1mINFO: Skipping Coverity Analysis: branch is a pull request.\033[0m"
27*f7c14bbaSAndroid Build Coastguard Worker  exit 0
28*f7c14bbaSAndroid Build Coastguard Workerfi
29*f7c14bbaSAndroid Build Coastguard Worker
30*f7c14bbaSAndroid Build Coastguard Worker# Verify this branch should run
31*f7c14bbaSAndroid Build Coastguard WorkerIS_COVERITY_SCAN_BRANCH=`ruby -e "puts '${TRAVIS_BRANCH}' =~ /\\A$COVERITY_SCAN_BRANCH_PATTERN\\z/ ? 1 : 0"`
32*f7c14bbaSAndroid Build Coastguard Workerif [ "$IS_COVERITY_SCAN_BRANCH" = "1" ]; then
33*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\033[33;1mCoverity Scan configured to run on branch ${TRAVIS_BRANCH}\033[0m"
34*f7c14bbaSAndroid Build Coastguard Workerelse
35*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\033[33;1mCoverity Scan NOT configured to run on branch ${TRAVIS_BRANCH}\033[0m"
36*f7c14bbaSAndroid Build Coastguard Worker  exit 1
37*f7c14bbaSAndroid Build Coastguard Workerfi
38*f7c14bbaSAndroid Build Coastguard Worker
39*f7c14bbaSAndroid Build Coastguard Worker# Verify upload is permitted
40*f7c14bbaSAndroid Build Coastguard WorkerAUTH_RES=`curl -s --form project="$COVERITY_SCAN_PROJECT_NAME" --form token="$COVERITY_SCAN_TOKEN" $SCAN_URL/api/upload_permitted`
41*f7c14bbaSAndroid Build Coastguard Workerif [ "$AUTH_RES" = "Access denied" ]; then
42*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\033[33;1mCoverity Scan API access denied. Check COVERITY_SCAN_PROJECT_NAME and COVERITY_SCAN_TOKEN.\033[0m"
43*f7c14bbaSAndroid Build Coastguard Worker  exit 1
44*f7c14bbaSAndroid Build Coastguard Workerelse
45*f7c14bbaSAndroid Build Coastguard Worker  AUTH=`echo $AUTH_RES | ruby -e "require 'rubygems'; require 'json'; puts JSON[STDIN.read]['upload_permitted']"`
46*f7c14bbaSAndroid Build Coastguard Worker  if [ "$AUTH" = "true" ]; then
47*f7c14bbaSAndroid Build Coastguard Worker    echo -e "\033[33;1mCoverity Scan analysis authorized per quota.\033[0m"
48*f7c14bbaSAndroid Build Coastguard Worker  else
49*f7c14bbaSAndroid Build Coastguard Worker    WHEN=`echo $AUTH_RES | ruby -e "require 'rubygems'; require 'json'; puts JSON[STDIN.read]['next_upload_permitted_at']"`
50*f7c14bbaSAndroid Build Coastguard Worker    echo -e "\033[33;1mCoverity Scan analysis NOT authorized until $WHEN.\033[0m"
51*f7c14bbaSAndroid Build Coastguard Worker    exit 0
52*f7c14bbaSAndroid Build Coastguard Worker  fi
53*f7c14bbaSAndroid Build Coastguard Workerfi
54*f7c14bbaSAndroid Build Coastguard Worker
55*f7c14bbaSAndroid Build Coastguard Workerif [ ! -d $TOOL_BASE ]; then
56*f7c14bbaSAndroid Build Coastguard Worker  # Download Coverity Scan Analysis Tool
57*f7c14bbaSAndroid Build Coastguard Worker  if [ ! -e $TOOL_ARCHIVE ]; then
58*f7c14bbaSAndroid Build Coastguard Worker    echo -e "\033[33;1mDownloading Coverity Scan Analysis Tool...\033[0m"
59*f7c14bbaSAndroid Build Coastguard Worker    wget -nv -O $TOOL_ARCHIVE $TOOL_URL --post-data "project=$COVERITY_SCAN_PROJECT_NAME&token=$COVERITY_SCAN_TOKEN"
60*f7c14bbaSAndroid Build Coastguard Worker  fi
61*f7c14bbaSAndroid Build Coastguard Worker
62*f7c14bbaSAndroid Build Coastguard Worker  # Extract Coverity Scan Analysis Tool
63*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\033[33;1mExtracting Coverity Scan Analysis Tool...\033[0m"
64*f7c14bbaSAndroid Build Coastguard Worker  mkdir -p $TOOL_BASE
65*f7c14bbaSAndroid Build Coastguard Worker  pushd $TOOL_BASE
66*f7c14bbaSAndroid Build Coastguard Worker  tar xzf $TOOL_ARCHIVE
67*f7c14bbaSAndroid Build Coastguard Worker  popd
68*f7c14bbaSAndroid Build Coastguard Workerfi
69*f7c14bbaSAndroid Build Coastguard Worker
70*f7c14bbaSAndroid Build Coastguard WorkerTOOL_DIR=`find $TOOL_BASE -type d -name 'cov-analysis*'`
71*f7c14bbaSAndroid Build Coastguard Workerexport PATH=$TOOL_DIR/bin:$PATH
72*f7c14bbaSAndroid Build Coastguard Worker
73*f7c14bbaSAndroid Build Coastguard Worker# Build
74*f7c14bbaSAndroid Build Coastguard Workerecho -e "\033[33;1mRunning Coverity Scan Analysis Tool...\033[0m"
75*f7c14bbaSAndroid Build Coastguard WorkerCOV_BUILD_OPTIONS=""
76*f7c14bbaSAndroid Build Coastguard Worker#COV_BUILD_OPTIONS="--return-emit-failures 8 --parse-error-threshold 85"
77*f7c14bbaSAndroid Build Coastguard WorkerRESULTS_DIR="cov-int"
78*f7c14bbaSAndroid Build Coastguard Workereval "${COVERITY_SCAN_BUILD_COMMAND_PREPEND}"
79*f7c14bbaSAndroid Build Coastguard WorkerCOVERITY_UNSUPPORTED=1 cov-build --dir $RESULTS_DIR $COV_BUILD_OPTIONS $COVERITY_SCAN_BUILD_COMMAND
80*f7c14bbaSAndroid Build Coastguard Workercov-import-scm --dir $RESULTS_DIR --scm git --log $RESULTS_DIR/scm_log.txt 2>&1
81*f7c14bbaSAndroid Build Coastguard Worker
82*f7c14bbaSAndroid Build Coastguard Worker# Upload results
83*f7c14bbaSAndroid Build Coastguard Workerecho -e "\033[33;1mTarring Coverity Scan Analysis results...\033[0m"
84*f7c14bbaSAndroid Build Coastguard WorkerRESULTS_ARCHIVE=analysis-results.tgz
85*f7c14bbaSAndroid Build Coastguard Workertar czf $RESULTS_ARCHIVE $RESULTS_DIR
86*f7c14bbaSAndroid Build Coastguard WorkerSHA=`git rev-parse --short HEAD`
87*f7c14bbaSAndroid Build Coastguard Worker
88*f7c14bbaSAndroid Build Coastguard Workerecho -e "\033[33;1mUploading Coverity Scan Analysis results...\033[0m"
89*f7c14bbaSAndroid Build Coastguard Workerresponse=$(curl \
90*f7c14bbaSAndroid Build Coastguard Worker  --silent --write-out "\n%{http_code}\n" \
91*f7c14bbaSAndroid Build Coastguard Worker  --form project=$COVERITY_SCAN_PROJECT_NAME \
92*f7c14bbaSAndroid Build Coastguard Worker  --form token=$COVERITY_SCAN_TOKEN \
93*f7c14bbaSAndroid Build Coastguard Worker  --form email=$COVERITY_SCAN_NOTIFICATION_EMAIL \
94*f7c14bbaSAndroid Build Coastguard Worker  --form file=@$RESULTS_ARCHIVE \
95*f7c14bbaSAndroid Build Coastguard Worker  --form version=$SHA \
96*f7c14bbaSAndroid Build Coastguard Worker  --form description="Travis CI build" \
97*f7c14bbaSAndroid Build Coastguard Worker  $UPLOAD_URL)
98*f7c14bbaSAndroid Build Coastguard Workerstatus_code=$(echo "$response" | sed -n '$p')
99*f7c14bbaSAndroid Build Coastguard Worker#[local] Coverity used to return 201 on success, but it's 200 now
100*f7c14bbaSAndroid Build Coastguard Worker# See https://github.com/systemd/systemd/blob/master/tools/coverity.sh#L145
101*f7c14bbaSAndroid Build Coastguard Workerif [ "$status_code" != "200" ]; then
102*f7c14bbaSAndroid Build Coastguard Worker  TEXT=$(echo "$response" | sed '$d')
103*f7c14bbaSAndroid Build Coastguard Worker  echo -e "\033[33;1mCoverity Scan upload failed: $TEXT.\033[0m"
104*f7c14bbaSAndroid Build Coastguard Worker  exit 1
105*f7c14bbaSAndroid Build Coastguard Workerfi
106