David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 1 | #!/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 | |
| 17 | source "$(dirname "${BASH_SOURCE[0]}")/../common.inc" |
| 18 | |
| 19 | # Directory containing kvm-unit-tests' binaries |
| 20 | TEST_DIR="$1" |
| 21 | |
| 22 | # Regex for excluding known-to-fail or irrelevant tests. |
| 23 | EXCLUDE="$2" |
| 24 | |
| 25 | # Variable/target names from the root Makefile. |
| 26 | VAR_KERNEL_IMAGE="$3" |
| 27 | VAR_LOG_DIR="$4" |
| 28 | GLOBAL_TEST_TARGET="$5" |
| 29 | GLOBAL_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. |
| 35 | function 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 Brazdil | 47ce1a6 | 2020-11-09 09:45:21 +0000 | [diff] [blame] | 44 | # 2) KVM mode (pkvm/nvhe/vhe) |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 45 | function group_target_name { |
| 46 | local group="$1" |
David Brazdil | 47ce1a6 | 2020-11-09 09:45:21 +0000 | [diff] [blame] | 47 | local mode="$2" |
David Brazdil | cd5f5c0 | 2020-07-29 11:43:26 +0000 | [diff] [blame] | 48 | |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 49 | group="$(target_name kut "${group}")" |
David Brazdil | 47ce1a6 | 2020-11-09 09:45:21 +0000 | [diff] [blame] | 50 | group="$(target_name "${mode}" "${group}")" |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 51 | echo "${group}" |
| 52 | } |
| 53 | |
| 54 | # Generates target name for a target printing list of all test targets. |
| 55 | # Args: |
| 56 | # 1) Root target name |
| 57 | function 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 |
| 67 | function dependency_target { |
| 68 | local target="$1" |
| 69 | local parent="$2" |
| 70 | cat <<EOF |
| 71 | .PHONY: ${target} |
| 72 | ${target}: |
| 73 | ${parent}: ${target} |
| 74 | EOF |
| 75 | } |
| 76 | |
| 77 | # Emits a Make target for a test case. |
| 78 | # Args: |
| 79 | # 1) Target name |
| 80 | # 2) Test binary path |
David Brazdil | 47ce1a6 | 2020-11-09 09:45:21 +0000 | [diff] [blame] | 81 | # 3) KVM mode (pkvm/nvhe/vhe) |
David Brazdil | cd5f5c0 | 2020-07-29 11:43:26 +0000 | [diff] [blame] | 82 | # 4) is GDB (0/1) |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 83 | function test_target { |
| 84 | local target="$1" |
| 85 | local binary="$2" |
David Brazdil | 47ce1a6 | 2020-11-09 09:45:21 +0000 | [diff] [blame] | 86 | local mode="$3" |
David Brazdil | cd5f5c0 | 2020-07-29 11:43:26 +0000 | [diff] [blame] | 87 | local gdb="$4" |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 88 | |
David Brazdil | 7dac245 | 2021-03-10 21:24:19 +0000 | [diff] [blame] | 89 | local extra_args=(-M "${mode}") |
David Brazdil | cd5f5c0 | 2020-07-29 11:43:26 +0000 | [diff] [blame] | 90 | if [ "${gdb}" -eq 1 ]; then |
| 91 | extra_args+=(-G) |
| 92 | fi |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 93 | |
David Brazdil | 93ab73d | 2020-08-24 10:06:37 +0000 | [diff] [blame] | 94 | # 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 Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 96 | 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 Brazdil | 93ab73d | 2020-08-24 10:06:37 +0000 | [diff] [blame] | 104 | ${extra_args[@]+"${extra_args[@]}"} \ |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 105 | "${binary}" |
| 106 | EOF |
| 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 |
| 114 | function 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 |
| 121 | EOF |
| 122 | } |
| 123 | |
| 124 | if [ ! -d "${TEST_DIR}" ]; then |
| 125 | echo "ERROR: Test directory does not exist" 1>&2 |
| 126 | exit 1 |
| 127 | fi |
| 128 | |
| 129 | TARGET_LIST=() |
David Brazdil | 47ce1a6 | 2020-11-09 09:45:21 +0000 | [diff] [blame] | 130 | for MODE in pkvm nvhe vhe; do |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 131 | # Emit a target for this test group. |
| 132 | ROOT="${GLOBAL_TEST_TARGET}" |
David Brazdil | 47ce1a6 | 2020-11-09 09:45:21 +0000 | [diff] [blame] | 133 | GROUP="$(group_target_name "${ROOT}" "${MODE}")" |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 134 | 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 Brazdil | cd5f5c0 | 2020-07-29 11:43:26 +0000 | [diff] [blame] | 144 | GDB_TARGET="$(target_name gdb ${TARGET})" |
| 145 | TARGET_LIST+=("${TARGET}" "${GDB_TARGET}") |
| 146 | |
David Brazdil | 47ce1a6 | 2020-11-09 09:45:21 +0000 | [diff] [blame] | 147 | test_target "${TARGET}" "${TEST_PATH}" "${MODE}" 0 |
| 148 | test_target "${GDB_TARGET}" "${TEST_PATH}" "${MODE}" 1 |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 149 | dependency_target "${TARGET}" "${GROUP}" |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 150 | done |
| 151 | done |
| 152 | |
| 153 | # Emit a target which prints all test targets from the generated Makefile. |
| 154 | ROOT="${GLOBAL_TEST_LIST_TARGET}" |
| 155 | TARGET="$(list_target_name "${ROOT}")" |
David Brazdil | 93ab73d | 2020-08-24 10:06:37 +0000 | [diff] [blame] | 156 | # Note: Due to a bug in older versions of Bash, use '${array[@]+"${array[@]}"}' |
| 157 | # to expand potentially empty arrays. '${array[@]}' is treated as undefined. |
| 158 | test_list_target "${TARGET}" ${TARGET_LIST[@]+"${TARGET_LIST[@]}"} |
David Brazdil | ef8506d | 2020-07-27 19:56:16 +0000 | [diff] [blame] | 159 | dependency_target "${TARGET}" "${ROOT}" |