blob: d589e4d800dd641153484604e825b3d4f27c4883 [file] [log] [blame]
David Brazdilef8506d2020-07-27 19:56:16 +00001#!/usr/bin/env bash
2
3# Copyright 2020 The Android KVM Authors
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17source "$(dirname "${BASH_SOURCE[0]}")/../common.inc"
18
19# Directory containing kvm-unit-tests' binaries
20TEST_DIR="$1"
21
22# Regex for excluding known-to-fail or irrelevant tests.
23EXCLUDE="$2"
24
25# Variable/target names from the root Makefile.
26VAR_KERNEL_IMAGE="$3"
27VAR_LOG_DIR="$4"
28GLOBAL_TEST_TARGET="$5"
29GLOBAL_TEST_LIST_TARGET="$6"
30
31# Generates a Make target name for a test case.
32# Args:
33# 1) name of test case
34# 2) target name of the test's group.
35function target_name {
36 local target="$1"
37 local parent="$2"
38 echo "${parent}-${target}"
39}
40
41# Generates target name for a group of given parameters.
42# Args:
43# 1) Root target name
David Brazdil47ce1a62020-11-09 09:45:21 +000044# 2) KVM mode (pkvm/nvhe/vhe)
David Brazdilef8506d2020-07-27 19:56:16 +000045function group_target_name {
46 local group="$1"
David Brazdil47ce1a62020-11-09 09:45:21 +000047 local mode="$2"
David Brazdilcd5f5c02020-07-29 11:43:26 +000048
David Brazdilef8506d2020-07-27 19:56:16 +000049 group="$(target_name kut "${group}")"
David Brazdil47ce1a62020-11-09 09:45:21 +000050 group="$(target_name "${mode}" "${group}")"
David Brazdilef8506d2020-07-27 19:56:16 +000051 echo "${group}"
52}
53
54# Generates target name for a target printing list of all test targets.
55# Args:
56# 1) Root target name
57function list_target_name {
58 local name="$1"
59 name="$(target_name kut "${name}")"
60 echo "${name}"
61}
62
63# Emits a Make target and a dependency of its parent on the target.
64# Args:
65# 1) Target name
66# 2) Parent target name
67function dependency_target {
68 local target="$1"
69 local parent="$2"
70 cat <<EOF
71.PHONY: ${target}
72${target}:
73${parent}: ${target}
74EOF
75}
76
77# Emits a Make target for a test case.
78# Args:
79# 1) Target name
80# 2) Test binary path
David Brazdil47ce1a62020-11-09 09:45:21 +000081# 3) KVM mode (pkvm/nvhe/vhe)
David Brazdilcd5f5c02020-07-29 11:43:26 +000082# 4) is GDB (0/1)
David Brazdilef8506d2020-07-27 19:56:16 +000083function test_target {
84 local target="$1"
85 local binary="$2"
David Brazdil47ce1a62020-11-09 09:45:21 +000086 local mode="$3"
David Brazdilcd5f5c02020-07-29 11:43:26 +000087 local gdb="$4"
David Brazdilef8506d2020-07-27 19:56:16 +000088
David Brazdil7dac2452021-03-10 21:24:19 +000089 local extra_args=(-M "${mode}")
David Brazdilcd5f5c02020-07-29 11:43:26 +000090 if [ "${gdb}" -eq 1 ]; then
91 extra_args+=(-G)
92 fi
David Brazdilef8506d2020-07-27 19:56:16 +000093
David Brazdil93ab73d2020-08-24 10:06:37 +000094 # Note: Due to a bug in older versions of Bash, use '${array[@]+"${array[@]}"}'
95 # to expand potentially empty arrays. '${array[@]}' is treated as undefined.
David Brazdilef8506d2020-07-27 19:56:16 +000096 cat <<EOF
97.PHONY: ${target}
98${target}: \$(${VAR_KERNEL_IMAGE}) ${binary}
99 @ mkdir -p "\$(${VAR_LOG_DIR})"
100 @ "${SCRIPT_RUN_KUT}" \
101 -k \$(${VAR_KERNEL_IMAGE}) \
102 -d "${target}" \
103 -o \$(${VAR_LOG_DIR})/${target}.log \
David Brazdil93ab73d2020-08-24 10:06:37 +0000104 ${extra_args[@]+"${extra_args[@]}"} \
David Brazdilef8506d2020-07-27 19:56:16 +0000105 "${binary}"
106EOF
107}
108
109# Emits a Make target which prints all provided target names,
110# one per line.
111# Args:
112# 1) target name
113# ...) list of target names to print
114function test_list_target {
115 local target="$1"
116 shift 1
117 cat <<EOF
118.PHONY: ${target}
119${target}:
120 @ for T in $@; do echo "\$\$T"; done
121EOF
122}
123
124if [ ! -d "${TEST_DIR}" ]; then
125 echo "ERROR: Test directory does not exist" 1>&2
126 exit 1
127fi
128
129TARGET_LIST=()
David Brazdil47ce1a62020-11-09 09:45:21 +0000130for MODE in pkvm nvhe vhe; do
David Brazdilef8506d2020-07-27 19:56:16 +0000131 # Emit a target for this test group.
132 ROOT="${GLOBAL_TEST_TARGET}"
David Brazdil47ce1a62020-11-09 09:45:21 +0000133 GROUP="$(group_target_name "${ROOT}" "${MODE}")"
David Brazdilef8506d2020-07-27 19:56:16 +0000134 dependency_target "${GROUP}" "${ROOT}"
135
136 for TEST_PATH in "${TEST_DIR}"/*; do
137 NAME=$(basename "${TEST_PATH}")
138 if [[ ${NAME} =~ ${EXCLUDE} ]]; then
139 continue
140 fi
141
142 # Emit a target for this test case.
143 TARGET="$(target_name ${NAME} ${GROUP})"
David Brazdilcd5f5c02020-07-29 11:43:26 +0000144 GDB_TARGET="$(target_name gdb ${TARGET})"
145 TARGET_LIST+=("${TARGET}" "${GDB_TARGET}")
146
David Brazdil47ce1a62020-11-09 09:45:21 +0000147 test_target "${TARGET}" "${TEST_PATH}" "${MODE}" 0
148 test_target "${GDB_TARGET}" "${TEST_PATH}" "${MODE}" 1
David Brazdilef8506d2020-07-27 19:56:16 +0000149 dependency_target "${TARGET}" "${GROUP}"
David Brazdilef8506d2020-07-27 19:56:16 +0000150 done
151done
152
153# Emit a target which prints all test targets from the generated Makefile.
154ROOT="${GLOBAL_TEST_LIST_TARGET}"
155TARGET="$(list_target_name "${ROOT}")"
David Brazdil93ab73d2020-08-24 10:06:37 +0000156# Note: Due to a bug in older versions of Bash, use '${array[@]+"${array[@]}"}'
157# to expand potentially empty arrays. '${array[@]}' is treated as undefined.
158test_list_target "${TARGET}" ${TARGET_LIST[@]+"${TARGET_LIST[@]}"}
David Brazdilef8506d2020-07-27 19:56:16 +0000159dependency_target "${TARGET}" "${ROOT}"