Makefile: Replace functions by single RUN_TEST var

Use a single RUN_TEST parametric variable to call the run_test.sh
script, similiarly to what is done for COMPILE.c in GNU Make. Rules
using that variable are then expected to specialize the various
parameters it contains (RUN_TEST_SH, RUN_TEST_ARGS, RUN_TEST_QEMU_ARGS,
...). This makes the code easier to extend to new test targets and more
straightforward to read through fewer indirections and intermediate
variables.

Bug: 195395681
Test: make test
Change-Id: I5cbddacc00ae9f2fa1f0004124c25d900ad6999c
diff --git a/Makefile b/Makefile
index cfed8e1..cb2583e 100644
--- a/Makefile
+++ b/Makefile
@@ -346,6 +346,15 @@
 KVM_MODES := vhe nvhe pkvm
 hyphensuffix = $(lastword $(subst -, ,$(1)))
 
+RUN_TEST_SH := $(SCRIPTS_DIR)/kvm-unit-tests/run_test.sh
+RUN_TEST = $(RUN_TEST_SH) \
+		-d $@ \
+		-M $(call hyphensuffix,$@) \
+		-k "$(KERNEL_IMAGE)" \
+		-o "$(RUN_TEST_LOG_DIR)/$@.log" \
+		$(RUN_TEST_ARGS) "$(RUN_TEST_HOST_SH)" \
+		$(if $(RUN_TEST_QEMU_ARGS),-- ,)$(RUN_TEST_QEMU_ARGS)
+
 # Root test targets. Dynamically generated per-test targets
 # will add themselves as dependencies of these.
 .PHONY: test test-list
@@ -356,7 +365,7 @@
 # kvm-unit-tests
 #
 
-KUT_RUN_TEST := $(SCRIPTS_DIR)/kvm-unit-tests/run_test.sh
+KUT_RUN_TEST = $(RUN_TEST_SH)
 KUT_GEN_MAKEFILE := $(SCRIPTS_DIR)/kvm-unit-tests/gen_makefile.sh
 KUT_MAKEFILE := $(OUT_DIR)/Makefile.kvm-unit-tests
 KUT_TEST_DIR := $(KUT_OUT)/tests
@@ -381,18 +390,8 @@
 # Basic boot tests in the various arm64 KVM modes
 #
 
-BOOT_TEST_SCRIPT_DIR := $(SCRIPTS_DIR)/aarch64/
-BOOT_TEST_LOG_DIR := $(DIST_DIR)/logs/boot/
 BOOT_TESTS := $(addprefix test-boot-,$(KVM_MODES))
 
-define gen_boot_test
-	$(KUT_RUN_TEST) -d $(1) -M $(2) \
-		-k $(KERNEL_IMAGE) \
-		-o $(BOOT_TEST_LOG_DIR)/$(1).log \
-		$(BOOT_TEST_SCRIPT_DIR)/test_boot.sh -- \
-		-a "boot_test_mode=$(2)"
-endef
-
 .PHONY: test-boot
 test-boot: $(BOOT_TESTS)
 
@@ -400,30 +399,16 @@
 test-list-boot: LISTED_TESTS = $(BOOT_TESTS)
 
 TESTS += $(BOOT_TESTS)
-$(BOOT_TESTS):
-	@ $(call gen_boot_test,$@,$(call hyphensuffix,$@))
+$(BOOT_TESTS): RUN_TEST_LOG_DIR = $(DIST_DIR)/logs/boot
+$(BOOT_TESTS): RUN_TEST_HOST_SH = $(SCRIPTS_DIR)/aarch64/test_boot.sh
+$(BOOT_TESTS): RUN_TEST_QEMU_ARGS = -a "boot_test_mode=$(call hyphensuffix,$@)"
 
 #
 # CrosVM Hello World test
 #
 
-CROSVM_HELLOWORLD_SCRIPT_DIR := $(SCRIPTS_DIR)/crosvm/helloworld/
-CROSVM_HELLOWORLD_LOG_DIR := $(DIST_DIR)/logs/crosvm/helloworld/
 CROSVM_HELLOWORLD_TESTS := $(addprefix test-crosvm-helloworld-,$(KVM_MODES))
 
-define gen_crosvm_helloworld_test
-	$(KUT_RUN_TEST) -d $(1) -M $(2) -t 300s \
-		-k $(KERNEL_IMAGE) \
-		-F $(UBOOT_BIN) \
-		-o $(CROSVM_HELLOWORLD_LOG_DIR)/$(1).log \
-		$(CROSVM_HELLOWORLD_SCRIPT_DIR)/host.sh -- \
-		-s 4 \
-		-R $(CROSVM_HELLOWORLD_SCRIPT_DIR)/guest.sh \
-		-R $(KERNEL_IMAGE) \
-		-R $(PREBUILTS_ROOTFS_EXT4) \
-		-R $(PREBUILTS_CROSVM_EXT4)
-endef
-
 .PHONY: test-crosvm-helloworld
 test-crosvm-helloworld: $(CROSVM_HELLOWORLD_TESTS)
 
@@ -432,10 +417,21 @@
 
 TESTS += $(CROSVM_HELLOWORLD_TESTS)
 $(CROSVM_HELLOWORLD_TESTS): $(UBOOT_BIN)
-	@ $(call gen_crosvm_helloworld_test,$@,$(call hyphensuffix,$@))
+$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_LOG_DIR = $(DIST_DIR)/logs/crosvm/helloworld
+$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_HOST_SH = $(SCRIPTS_DIR)/crosvm/helloworld/host.sh
+$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_ARGS += \
+		-t 300s \
+		-F $(UBOOT_BIN)
+$(CROSVM_HELLOWORLD_TESTS): RUN_TEST_QEMU_ARGS = \
+		-s 4 \
+		-R $(SCRIPTS_DIR)/crosvm/helloworld/guest.sh \
+		-R $(KERNEL_IMAGE) \
+		-R $(PREBUILTS_ROOTFS_EXT4) \
+		-R $(PREBUILTS_CROSVM_EXT4)
 
 .PHONY test: $(TESTS)
 $(TESTS): $(KERNEL_IMAGE)
+	@ $(RUN_TEST)
 .PHONY test-list: $(TEST_LISTS)
 $(TEST_LISTS):
 	@ for x in $(LISTED_TESTS); do echo $$x; done