1#!/bin/bash
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright(c) 2021 Intel Corporation. All rights reserved.
4
5
6# Stop on most errors
7set -e
8
9SOFTOP=$(cd "$(dirname "$0")"/.. && pwd)
10
11usage()
12{
13    cat <<EOF
14Usage: [ -v ] [ -c platform_defconfig ]
15
16Re-compiles unit tests with the host toolchain and runs them one by
17one, optionally with valgrind. If you don't need valgrind it's faster
18and better looking to run "make -j test". See
19https://thesofproject.github.io/latest/developer_guides/unit_tests.html
20EOF
21    exit 1
22}
23
24parse_opts()
25{
26	# default config if none supplied
27	CONFIG="tgph_defconfig"
28	VALGRIND_CMD=""
29
30	while getopts "vc:" flag; do
31	    case "${flag}" in
32		c) CONFIG=${OPTARG}
33		   ;;
34		v) VALGRIND_CMD="valgrind --tool=memcheck --track-origins=yes \
35--leak-check=full --show-leak-kinds=all --error-exitcode=1"
36		   ;;
37		?) usage
38		;;
39	    esac
40	done
41
42	shift $((OPTIND -1 ))
43	# anything left?
44	test -z "$1" || usage
45}
46
47rebuild_ut()
48{
49        # -DINIT_CONFIG is ignored after the first time. Invoke make (or
50        # -ninja) after this for faster, incremental builds.
51	rm -rf build_ut/
52
53	cmake -S "$SOFTOP" -B build_ut -DBUILD_UNIT_TESTS=ON -DBUILD_UNIT_TESTS_HOST=ON \
54	      -DINIT_CONFIG="${CONFIG}"
55
56	cmake --build build_ut -- -j"$(nproc --all)"
57}
58
59run_ut()
60{
61	local TESTS; TESTS=$(find build_ut/test -type f -executable -print)
62	echo test are "${TESTS}"
63	for test in ${TESTS}
64	do
65	    if [ x"$test" = x'build_ut/test/cmocka/src/lib/alloc/alloc' ]; then
66		printf 'SKIP alloc test until it is fixed on HOST (passes with xt-run)'
67		continue
68	    fi
69
70	    printf 'Running %s\n' "${test}"
71	    ( set -x
72	      ${VALGRIND_CMD} ./"${test}"
73	    )
74	done
75}
76
77main()
78{
79	parse_opts "$@"
80	rebuild_ut
81	run_ut
82}
83
84main "$@"
85