da433b29c0b5542f7b29fdc354cd8e1599da4e3f
[releng/global-jjb.git] / shell / cmake-sonarqube.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2020 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-sonarqube.sh"
12
13 # Runs cmake then make using a wrapper for the SonarQube Jenkins plug-in
14 # https://docs.sonarqube.org/latest/analysis/languages/cfamily/
15
16 set -eux -o pipefail
17
18 # Add missing sctp.h header file to prevent cmake/build failure
19 if (grep Ubuntu /etc/os-release > /dev/null 2>&1); then
20     echo "Installing libsctp-dev dependency for Ubuntu"
21     sudo apt-get update
22     sudo apt-get install -y libsctp-dev
23 elif (grep RedHat /etc/os-release > /dev/null 2>&1) || \
24      (grep CentOS /etc/os-release > /dev/null 2>&1); then
25     echo "Installing lksctp-tools-devel dependency for CentOS/RedHat"
26     sudo yum install -y lksctp-tools-devel
27 else
28     echo "Unmatched OS/Distribution"
29     echo "Missing sctp library not installed"
30 fi
31
32 build_dir="${BUILD_DIR:-$WORKSPACE/build}"
33 build_wrap_dir="${BUILD_WRAP_DIR:-$WORKSPACE/bw-output}"
34 cmake_opts="${CMAKE_OPTS:-}"
35 make_opts="${MAKE_OPTS:-}"
36
37 cd /tmp || exit 1
38 wget -q -O bw.zip https://sonarcloud.io/static/cpp/build-wrapper-linux-x86.zip
39 unzip -q bw.zip
40 sudo mv build-wrapper-* /opt/build-wrapper
41
42
43 export SET_JDK_VERSION="${SET_JDK_VERSION:-openjdk11}"
44 echo "$SET_JDK_VERSION"
45 GITHUB_RAW_BASE_URL="https://raw.githubusercontent.com"
46 GITHUB_FILE="lfit/releng-global-jjb/master/shell/update-java-alternatives.sh"
47 bash <(curl -s "${GITHUB_RAW_BASE_URL}/${GITHUB_FILE}")
48 # shellcheck disable=SC1091
49 source /tmp/java.env
50
51 mkdir -p "$build_dir"
52 cd "$build_dir" || exit 1
53
54 # $cmake_opts needs to wordsplit to pass options.
55 # shellcheck disable=SC2086
56 eval cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX" $cmake_opts ..
57
58 # $make_opts may be empty.
59 # shellcheck disable=SC2086
60 /opt/build-wrapper/build-wrapper-linux-x86-64 --out-dir "$build_wrap_dir" \
61     make $make_opts
62
63 echo "---> cmake-sonarqube.sh ends"