| 1 | # -*- Mode: makefile -*- |
| 2 | # |
| 3 | # TCG tests |
| 4 | # |
| 5 | # These are complicated by the fact we want to build them for guest |
| 6 | # systems. This requires knowing what guests we are building and which |
| 7 | # ones we have cross-compilers for or docker images with |
| 8 | # cross-compilers. |
| 9 | # |
| 10 | # The tests themselves should be as minimal as possible as |
| 11 | # cross-compilers don't always have a large amount of libraries |
| 12 | # available. |
| 13 | # |
| 14 | # We only include the host build system for SRC_PATH and we don't |
| 15 | # bother with the common rules.mk. We expect the following: |
| 16 | # |
| 17 | # CC - the C compiler command |
| 18 | # EXTRA_CFLAGS - any extra CFLAGS |
| 19 | # BUILD_STATIC - are we building static binaries |
| 20 | # |
| 21 | # By default all tests are statically compiled but some host systems |
| 22 | # may not package static libraries by default. If an external |
| 23 | # cross-compiler can only build dynamic libraries the user might need |
| 24 | # to make extra efforts to ensure ld.so can link at runtime when the |
| 25 | # tests are run. |
| 26 | # |
| 27 | # We also accept SPEED=slow to enable slower running tests |
| 28 | # |
| 29 | # We also expect to be in the tests build dir for the FOO-(linux-user|softmmu). |
| 30 | # |
| 31 | |
| 32 | all: |
| 33 | -include ../config-host.mak |
| 34 | -include config-target.mak |
| 35 | |
| 36 | # Get semihosting definitions for user-mode emulation |
| 37 | ifeq ($(filter %-softmmu, $(TARGET)),) |
| 38 | -include $(SRC_PATH)/configs/targets/$(TARGET).mak |
| 39 | endif |
| 40 | |
| 41 | # for including , in command strings |
| 42 | COMMA := , |
| 43 | NULL := |
| 44 | SPACE := $(NULL) # |
| 45 | TARGET_PREFIX=tests/tcg/$(TARGET):$(SPACE) |
| 46 | |
| 47 | quiet-@ = $(if $(V),,@$(if $1,printf " %-7s %s\n" "$(strip $1)" "$(strip $2)" && )) |
| 48 | quiet-command = $(call quiet-@,$2,$3)$1 |
| 49 | |
| 50 | cc-test = $(CC) -Werror $1 -c -o /dev/null -xc /dev/null >/dev/null 2>&1 |
| 51 | cc-option = if $(call cc-test, $1); then \ |
| 52 | echo "$(TARGET_PREFIX)$1 detected" && echo "$(strip $2)=$(strip $1)" >&3; else \ |
| 53 | echo "$(TARGET_PREFIX)$1 not detected"; fi |
| 54 | |
| 55 | # $1 = test name, $2 = cmd, $3 = desc |
| 56 | ifeq ($(filter %-softmmu, $(TARGET)),) |
| 57 | run-test = $(call quiet-command, timeout -s KILL --foreground $(TIMEOUT) $2 > $1.out, \ |
| 58 | TEST,$(or $3, $*, $<) on $(TARGET_NAME)) |
| 59 | else |
| 60 | run-test = $(call quiet-command, timeout -s KILL --foreground $(TIMEOUT) $2, \ |
| 61 | TEST,$(or $3, $*, $<) on $(TARGET_NAME)) |
| 62 | endif |
| 63 | |
| 64 | # $1 = test name, $2 = reference |
| 65 | # to work around the pipe squashing the status we only pipe the result if |
| 66 | # we know it failed and then force failure at the end. |
| 67 | diff-out = $(call quiet-command, diff -q $1.out $2 || \ |
| 68 | (diff -u $1.out $2 | head -n 10 && false), \ |
| 69 | DIFF,$1.out with $2) |
| 70 | |
| 71 | # $1 = test name, $2 = reason |
| 72 | skip-test = @printf " SKIPPED %s on $(TARGET_NAME) because %s\n" $1 $2 |
| 73 | |
| 74 | # $1 = test name, $2 = reference |
| 75 | # As above but only diff if reference file exists, otherwise the test |
| 76 | # passes if it managed to complete with a status of zero |
| 77 | conditional-diff-out = \ |
| 78 | $(if $(wildcard $2), \ |
| 79 | $(call diff-out,$1,$2), \ |
| 80 | $(call skip-test,"$1 check","no reference")) |
| 81 | |
| 82 | |
| 83 | # Tests we are building |
| 84 | TESTS= |
| 85 | # additional tests which may re-use existing binaries |
| 86 | EXTRA_TESTS= |
| 87 | |
| 88 | # Start with a blank slate, the build targets get to add stuff first |
| 89 | CFLAGS= |
| 90 | LDFLAGS= |
| 91 | |
| 92 | QEMU_OPTS= |
| 93 | CHECK_PLUGIN_OUTPUT_COMMAND= |
| 94 | |
| 95 | |
| 96 | # If TCG debugging, or TCI is enabled things are a lot slower |
| 97 | # so we have to set our timeout for that. The current worst case |
| 98 | # offender is the system memory test running under TCI. |
| 99 | TIMEOUT=120 |
| 100 | |
| 101 | ifeq ($(filter %-softmmu, $(TARGET)),) |
| 102 | # The order we include is important. We include multiarch first and |
| 103 | # then the target. If there are common tests shared between |
| 104 | # sub-targets (e.g. ARM & AArch64) then it is up to |
| 105 | # $(TARGET_NAME)/Makefile.target to include the common parent |
| 106 | # architecture in its VPATH. However some targets are so minimal we |
| 107 | # can't even build the multiarch tests. |
| 108 | ifneq ($(filter $(TARGET_NAME),aarch64_be),) |
| 109 | -include $(SRC_PATH)/tests/tcg/$(TARGET_NAME)/Makefile.target |
| 110 | else |
| 111 | -include $(SRC_PATH)/tests/tcg/multiarch/Makefile.target |
| 112 | -include $(SRC_PATH)/tests/tcg/$(TARGET_NAME)/Makefile.target |
| 113 | endif |
| 114 | |
| 115 | # Add the common build options |
| 116 | CFLAGS+=-Wall -Werror -O0 -g -fno-strict-aliasing |
| 117 | ifeq ($(BUILD_STATIC),y) |
| 118 | LDFLAGS+=-static |
| 119 | endif |
| 120 | |
| 121 | %: %.c |
| 122 | $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) |
| 123 | %: %.S |
| 124 | $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -Wa,--noexecstack $< -o $@ $(LDFLAGS) |
| 125 | else |
| 126 | # For system targets we include a different Makefile fragment as the |
| 127 | # build options for bare programs are usually pretty different. They |
| 128 | # are expected to provide their own build recipes. |
| 129 | EXTRA_CFLAGS += -ffreestanding -fno-stack-protector |
| 130 | |
| 131 | # We skip the multiarch tests if the target hasn't provided a boot.S |
| 132 | MULTIARCH_SOFTMMU_TARGETS = i386 alpha aarch64 arm loongarch64 s390x x86_64 |
| 133 | |
| 134 | ifneq ($(filter $(TARGET_NAME),$(MULTIARCH_SOFTMMU_TARGETS)),) |
| 135 | -include $(SRC_PATH)/tests/tcg/minilib/Makefile.target |
| 136 | -include $(SRC_PATH)/tests/tcg/multiarch/system/Makefile.softmmu-target |
| 137 | endif |
| 138 | -include $(SRC_PATH)/tests/tcg/$(TARGET_NAME)/Makefile.softmmu-target |
| 139 | |
| 140 | endif |
| 141 | |
| 142 | all: $(TESTS) $(EXTRA_TESTS) |
| 143 | |
| 144 | # |
| 145 | # Test Runners |
| 146 | # |
| 147 | # By default we just run the test with the appropriate QEMU for the |
| 148 | # target. More advanced tests may want to override the runner in their |
| 149 | # specific make rules. Additional runners for the same binary should |
| 150 | # be added to EXTRA_RUNS. |
| 151 | # |
| 152 | |
| 153 | RUN_TESTS=$(patsubst %,run-%, $(TESTS)) |
| 154 | |
| 155 | # If plugins exist also include those in the tests |
| 156 | ifeq ($(CONFIG_PLUGIN),y) |
| 157 | PLUGIN_SRC=$(SRC_PATH)/tests/tcg/plugins |
| 158 | PLUGIN_LIB=../plugins |
| 159 | VPATH+=$(PLUGIN_LIB) |
| 160 | # Some plugins need to be disabled for all tests to avoid exponential explosion. |
| 161 | # For example, libpatch.so only needs to run against the arch-specific patch |
| 162 | # target test, so we explicitly run it in the arch-specific Makefile. |
| 163 | DISABLE_PLUGINS=libpatch.so |
| 164 | |
| 165 | # Likewise don't bother with the syscall plugin for softmmu |
| 166 | ifneq ($(filter %-softmmu, $(TARGET)),) |
| 167 | DISABLE_PLUGINS += libsyscall.so |
| 168 | endif |
| 169 | |
| 170 | PLUGINS=$(filter-out $(DISABLE_PLUGINS), \ |
| 171 | $(patsubst %.c, lib%.so, $(notdir $(wildcard $(PLUGIN_SRC)/*.c)))) |
| 172 | |
| 173 | strip-plugin = $(wordlist 1, 1, $(subst -with-, ,$1)) |
| 174 | extract-plugin = $(wordlist 2, 2, $(subst -with-, ,$1)) |
| 175 | extract-test = $(subst run-plugin-,,$(wordlist 1, 1, $(subst -with-, ,$1))) |
| 176 | |
| 177 | # We need to ensure expand the run-plugin-TEST-with-PLUGIN |
| 178 | # pre-requistes manually here as we can't use stems to handle it. We |
| 179 | # only expand MULTIARCH_TESTS which are common on most of our targets |
| 180 | # and rotate the plugins so we don't grow too out of control as new |
| 181 | # tests are added. Plugins that need to run with a specific test |
| 182 | # should ensure they add their combination to EXTRA_RUNS. |
| 183 | |
| 184 | ifneq ($(MULTIARCH_TESTS),) |
| 185 | |
| 186 | # Extract extra tests from the extra test+plugin combination. |
| 187 | EXTRA_TESTS_WITH_PLUGIN=$(foreach test, \ |
| 188 | $(EXTRA_RUNS_WITH_PLUGIN),$(call extract-test,$(test))) |
| 189 | # Exclude tests that were specified to run with specific plugins from the tests |
| 190 | # which can run with any plugin combination, so we don't run it twice. |
| 191 | MULTIARCH_TESTS:=$(filter-out $(EXTRA_TESTS_WITH_PLUGIN), $(MULTIARCH_TESTS)) |
| 192 | |
| 193 | NUM_PLUGINS := $(words $(PLUGINS)) |
| 194 | NUM_TESTS := $(words $(MULTIARCH_TESTS)) |
| 195 | |
| 196 | define mod_plus_one |
| 197 | $(shell $(PYTHON) -c "print( ($(1) % $(2)) + 1 )") |
| 198 | endef |
| 199 | |
| 200 | # Rules for running tests with any plugin combination, i.e., no specific plugin. |
| 201 | $(foreach _idx, $(shell seq 1 $(NUM_TESTS)), \ |
| 202 | $(eval _test := $(word $(_idx), $(MULTIARCH_TESTS))) \ |
| 203 | $(eval _plugin := $(word $(call mod_plus_one, $(_idx), $(NUM_PLUGINS)), $(PLUGINS))) \ |
| 204 | $(eval run-plugin-$(_test)-with-$(_plugin): $(_test) $(_plugin)) \ |
| 205 | $(eval RUN_TESTS+=run-plugin-$(_test)-with-$(_plugin))) |
| 206 | |
| 207 | # Rules for running extra tests with specific plugins. |
| 208 | $(foreach f,$(EXTRA_RUNS_WITH_PLUGIN), \ |
| 209 | $(eval $(f): $(call extract-test,$(f)) $(call extract-plugin,$(f)))) |
| 210 | |
| 211 | endif # MULTIARCH_TESTS |
| 212 | endif # CONFIG_PLUGIN |
| 213 | |
| 214 | RUN_TESTS+=$(EXTRA_RUNS) |
| 215 | RUN_TESTS+=$(EXTRA_RUNS_WITH_PLUGIN) |
| 216 | |
| 217 | # Some plugins need additional arguments above the default to fully |
| 218 | # exercise things. We can define them on a per-test basis here. |
| 219 | run-plugin-%-with-libmem.so: PLUGIN_ARGS=$(COMMA)inline=true |
| 220 | |
| 221 | ifeq ($(filter %-softmmu, $(TARGET)),) |
| 222 | run-%: % |
| 223 | $(call run-test, $<, env QEMU=$(QEMU) $(QEMU) $(QEMU_OPTS) $<) |
| 224 | |
| 225 | run-plugin-%: |
| 226 | $(call run-test, $@, env QEMU=$(QEMU) $(QEMU) $(QEMU_OPTS) \ |
| 227 | -plugin $(PLUGIN_LIB)/$(call extract-plugin,$@)$(PLUGIN_ARGS) \ |
| 228 | -d plugin -D $*.pout \ |
| 229 | $(call strip-plugin,$<)) |
| 230 | $(if $(CHECK_PLUGIN_OUTPUT_COMMAND), \ |
| 231 | $(call quiet-command, $(CHECK_PLUGIN_OUTPUT_COMMAND) $*.pout, \ |
| 232 | TEST, check plugin $(call extract-plugin,$@) output \ |
| 233 | with $(call strip-plugin,$<))) |
| 234 | else |
| 235 | run-%: % |
| 236 | $(call run-test, $<, \ |
| 237 | $(QEMU) -monitor none -display none \ |
| 238 | -chardev file$(COMMA)path=$<.out$(COMMA)id=output \ |
| 239 | $(QEMU_OPTS) $<) |
| 240 | |
| 241 | run-plugin-%: |
| 242 | $(call run-test, $@, \ |
| 243 | $(QEMU) -monitor none -display none \ |
| 244 | -chardev file$(COMMA)path=$@.out$(COMMA)id=output \ |
| 245 | -plugin $(PLUGIN_LIB)/$(call extract-plugin,$@)$(PLUGIN_ARGS) \ |
| 246 | -d plugin -D $*.pout \ |
| 247 | $(QEMU_OPTS) $(call strip-plugin,$<)) |
| 248 | $(if $(CHECK_PLUGIN_OUTPUT_COMMAND), \ |
| 249 | $(call quiet-command, $(CHECK_PLUGIN_OUTPUT_COMMAND) $*.pout, \ |
| 250 | TEST, check plugin $(call extract-plugin,$@) output \ |
| 251 | with $(call strip-plugin,$<))) |
| 252 | endif |
| 253 | |
| 254 | gdb-%: % |
| 255 | gdb --args $(QEMU) $(QEMU_OPTS) $< |
| 256 | |
| 257 | # Filter tests based on TCG_TEST_FILTER if set |
| 258 | ifdef TCG_TEST_FILTER |
| 259 | FILTERED_RUN_TESTS=$(foreach test,$(RUN_TESTS),$(if $(findstring $(TCG_TEST_FILTER),$(test)),$(test))) |
| 260 | else |
| 261 | FILTERED_RUN_TESTS=$(RUN_TESTS) |
| 262 | endif |
| 263 | |
| 264 | .PHONY: run |
| 265 | run: $(FILTERED_RUN_TESTS) |
| 266 | |
| 267 | clean: |
| 268 | rm -f $(TESTS) *.o $(CLEANFILES) |
| 269 | |
| 270 | distclean: |
| 271 | rm -f config-cc.mak config-target.mak ../config-$(TARGET).mak |
| 272 | |
| 273 | .PHONY: help |
| 274 | help: |
| 275 | @echo "TCG tests help $(TARGET_NAME)" |
| 276 | @echo "Built with $(CC)" |
| 277 | @echo "Available tests:" |
| 278 | @$(foreach t,$(RUN_TESTS),echo " $t";) |
| 279 | @echo "" |
| 280 | @echo "Environment variables:" |
| 281 | @echo " TCG_TEST_FILTER=<pattern> Filter tests matching pattern" |