Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / cmake-sonar.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2019 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> cmake-sonar.sh"
12
13 build_dir="${BUILD_DIR:-$WORKSPACE/target}"
14 cmake_opts="${CMAKE_OPTS:-}"
15 make_opts="${MAKE_OPTS:-}"
16
17 ################
18 # Script start #
19 ################
20
21 set -ex -o pipefail
22
23 export SET_JDK_VERSION="${SET_JDK_VERSION:-openjdk11}"
24 echo "$SET_JDK_VERSION"
25 GITHUB_RAW_BASE_URL="https://raw.githubusercontent.com"
26 GITHUB_FILE="lfit/releng-global-jjb/master/shell/update-java-alternatives.sh"
27 bash <(curl -s "${GITHUB_RAW_BASE_URL}/${GITHUB_FILE}")
28 # shellcheck disable=SC1091
29 source /tmp/java.env
30
31 cd /tmp || exit 1
32 SONAR_BASE_URL="https://binaries.sonarsource.com"
33 SONAR_SCANER_PATH="Distribution/sonar-scanner-cli"
34 SONAR_SCANER_CLI="sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip"
35 wget -q -O /tmp/sonar-scan.zip \
36     "${SONAR_BASE_URL}/${SONAR_SCANER_PATH}/${SONAR_SCANER_CLI}"
37 unzip -q sonar-scan.zip
38 sudo mv sonar-scanner-* /opt/sonar-scanner
39
40 wget -q -O /tmp/bw.zip \
41     "https://sonarcloud.io/static/cpp/build-wrapper-linux-x86.zip"
42 unzip -q bw.zip
43 sudo mv build-wrapper-* /opt/build-wrapper
44
45 mkdir -p "$build_dir"
46 cd "$build_dir" || exit 1
47 # $cmake_opts needs to wordsplit to pass options.
48 # shellcheck disable=SC2086
49 eval cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX" $cmake_opts ..
50
51 # $make_opts may be empty.
52 # shellcheck disable=SC2086
53 /opt/build-wrapper/build-wrapper-linux-x86-64 --out-dir \
54     "$WORKSPACE/bw-output" make $make_opts
55
56 /opt/sonar-scanner/bin/sonar-scanner \
57     -Dsonar.projectKey="${PROJECT_KEY}" \
58     -Dsonar.organization="${PROJECT_ORGANIZATION}" \
59     -Dsonar.sources=. \
60     -Dsonar.cfamily.build-wrapper-output="$WORKSPACE/bw-output" \
61     -Dsonar.host.url="${SONAR_HOST_URL}" \
62     -Dsonar.login="${API_TOKEN}"