1#!/bin/bash
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright(c) 2020 Intel Corporation. All rights reserved.
4# Author: Janusz Jankowski <janusz.jankowski@linux.intel.com>
5
6# This script configures & performs build for clang's static analyzer.
7
8set -eu
9
10function usage {
11	echo "Usage: $0 -t <toolchain> -c <config> -r <root_dir> [options]"
12	echo "        -t        Toolchain's name."
13	echo "        -c        Name of defconfig."
14	echo "        -r        Xtensa root dir."
15	echo "        [-j n]    Set number of make build jobs."
16	echo "        [-v]      Verbose output."
17	echo "Examples:"
18	echo "        $0 -t xt -c apollolake \\"
19	echo "                -r \$CONFIG_PATH/xtensa-elf"
20	echo "        $0 -t xtensa-cnl-elf -c cannonlake \\"
21	echo "                -r \`pwd\`/../xtensa-cnl-elf"
22}
23
24if [ "$#" -eq 0 ]
25then
26	usage
27	exit 1
28fi
29
30TOOLCHAIN=""
31DEFCONFIG=""
32ROOT_DIR=""
33JOBS=$(nproc --all)
34VERBOSE_CMAKE=OFF
35VERBOSE_SCAN_BUILD=""
36
37while getopts "t:c:r:j:v" OPTION; do
38	case $OPTION in
39		t)
40			TOOLCHAIN=$OPTARG
41			;;
42		c)
43			DEFCONFIG=$OPTARG
44			;;
45		r)
46			ROOT_DIR=$OPTARG
47			;;
48		j)
49			JOBS=$OPTARG
50			;;
51		v)
52			VERBOSE_CMAKE=ON
53			VERBOSE_SCAN_BUILD="-v -v"
54			;;
55		*)
56			echo "Invalid options"
57			usage
58			exit 1
59			;;
60	esac
61done
62
63WORKDIR=`pwd`
64SCRIPT_DIR=$(realpath $(dirname $0))
65PROXY_COMPILER=$SCRIPT_DIR/proxy-gcc.sh
66
67BUILD_DIR=build_clang_scan_${DEFCONFIG}_${TOOLCHAIN}
68
69rm -fr $BUILD_DIR
70mkdir $BUILD_DIR
71cd $BUILD_DIR
72
73scan-build $VERBOSE_SCAN_BUILD cmake \
74	-DBUILD_CLANG_SCAN=ON \
75	-DTOOLCHAIN=$TOOLCHAIN \
76	-DROOT_DIR=$ROOT_DIR \
77	-DCMAKE_VERBOSE_MAKEFILE=$VERBOSE_CMAKE \
78	..
79
80make ${DEFCONFIG}_defconfig
81
82if [[ "$TOOLCHAIN" == "xt" ]]
83then
84	COMPILER=$TOOLCHAIN-xcc
85else
86	COMPILER=$TOOLCHAIN-gcc
87fi
88
89# There are flags that are needed by clang but don't work for xtensa compiler.
90# However clang tries to pass all flags to original compiler, so we need to put
91# proxy compiler in the middle that is going to filter out unsupported flags.
92PROXY_CC=$COMPILER PROXY_REMOVE_FLAGS=-m32 \
93	scan-build $VERBOSE_SCAN_BUILD --use-cc="$PROXY_COMPILER" -o report \
94	make sof -j$JOBS
95
96cd "$WORKDIR"
97