Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0-only |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 2 | |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 3 | testsrc = $(top)/tests |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 4 | testobj = $(obj)/tests |
| 5 | |
| 6 | TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx |
| 7 | TEST_DOTCONFIG = $(testobj)/.config |
| 8 | TEST_KCONFIG_AUTOHEADER := $(testobj)/config.h |
| 9 | TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf |
| 10 | TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd |
| 11 | TEST_KCONFIG_SPLITCONFIG := $(testobj)/config |
| 12 | TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf |
| 13 | |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 14 | TEST_CFLAGS = -include $(src)/include/kconfig.h \ |
| 15 | -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \ |
| 16 | -include $(src)/include/rules.h |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 17 | |
| 18 | # Include generic test mock headers, before original ones |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 19 | TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 20 | |
| 21 | TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \ |
| 22 | -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include \ |
| 23 | |
| 24 | # Path for Kconfig autoheader |
| 25 | TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER)) |
| 26 | |
Julius Werner | 2540037 | 2020-05-04 17:46:31 -0700 | [diff] [blame^] | 27 | TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 28 | |
| 29 | # Link against Cmocka |
Julius Werner | 2174481 | 2020-05-04 17:44:04 -0700 | [diff] [blame] | 30 | TEST_LDFLAGS = -lcmocka -Wl,--gc-sections |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 31 | |
| 32 | # Extra attributes for unit tests, declared per test |
| 33 | attributes:= srcs cflags mocks stage |
| 34 | |
| 35 | stages:= decompressor bootblock romstage smm verstage |
| 36 | stages+= ramstage rmodule postcar libagesa |
| 37 | |
| 38 | alltests:= |
| 39 | subdirs:= tests/arch tests/commonlib tests/console tests/cpu tests/device |
| 40 | subdirs+= tests/drivers tests/ec tests/lib tests/mainboard |
| 41 | subdirs+= tests/northbridge tests/security tests/soc tests/southbridge |
| 42 | subdirs+= tests/superio tests/vendorcode |
| 43 | |
| 44 | define tests-handler |
| 45 | alltests += $(1)$(2) |
| 46 | $(foreach attribute,$(attributes), |
| 47 | $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute)))) |
| 48 | $(foreach attribute,$(attributes), |
| 49 | $(eval $(2)-$(attribute):=)) |
| 50 | |
| 51 | # Sanity check for stage attribute value |
| 52 | $(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage)) |
| 53 | $(if $(findstring $($(1)$(2)-stage), $(stages)),, |
| 54 | $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \ |
| 55 | Check your $(dir $(1)$(2))Makefile.inc)) |
| 56 | endef |
| 57 | |
| 58 | $(call add-special-class, tests) |
| 59 | $(call evaluate_subdirs) |
| 60 | |
| 61 | # Create actual targets for unit test binaries |
| 62 | # $1 - test name |
| 63 | define TEST_CC_template |
| 64 | $($(1)-objs): TEST_CFLAGS+= \ |
| 65 | -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ |
| 66 | $($(1)-objs): $(obj)/$(1)/%.o: $$$$*.c $(TEST_KCONFIG_AUTOHEADER) |
| 67 | mkdir -p $$(dir $$@) |
| 68 | $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \ |
| 69 | -MT $$@ -c $$< -o $$@ |
| 70 | |
| 71 | $($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock)) |
| 72 | $($(1)-bin): $($(1)-objs) |
| 73 | $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@ |
| 74 | |
| 75 | endef |
| 76 | |
| 77 | $(foreach test, $(alltests), \ |
| 78 | $(eval $(test)-objs:=$(addprefix $(obj)/$(test)/, \ |
| 79 | $(patsubst %.c,%.o,$($(test)-srcs))))) |
| 80 | $(foreach test, $(alltests), \ |
| 81 | $(eval $(test)-bin:=$(obj)/$(test)/run)) |
| 82 | $(foreach test, $(alltests), \ |
| 83 | $(eval $(call TEST_CC_template,$(test)))) |
| 84 | |
| 85 | $(foreach test, $(alltests), \ |
| 86 | $(eval all-test-objs+=$($(test)-objs))) |
| 87 | $(foreach test, $(alltests), \ |
| 88 | $(eval test-bins+=$($(test)-bin))) |
| 89 | |
| 90 | DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs))) |
| 91 | -include $(DEPENDENCIES) |
| 92 | |
| 93 | # Kconfig targets |
| 94 | $(TEST_DOTCONFIG): |
| 95 | mkdir -p $(dir $@) |
| 96 | cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG) |
| 97 | |
| 98 | # Don't override default Kconfig variables, since this will affect all |
| 99 | # Kconfig targets. Change them only when calling sub-make instead. |
| 100 | $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \ |
| 101 | KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \ |
| 102 | KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \ |
| 103 | KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \ |
| 104 | KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \ |
| 105 | KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \ |
| 106 | KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG) |
| 107 | |
| 108 | $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf |
| 109 | mkdir -p $(dir $@) |
| 110 | +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig |
| 111 | +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig |
| 112 | |
| 113 | $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER) |
| 114 | true |
| 115 | |
| 116 | .PHONY: $(alltests) $(addprefix clean-,$(alltests)) |
| 117 | .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests |
| 118 | |
| 119 | $(alltests): $$($$(@)-bin) |
| 120 | ./$^ |
| 121 | |
| 122 | unit-tests: build-unit-tests run-unit-tests |
| 123 | |
| 124 | build-unit-tests: $(test-bins) |
| 125 | |
| 126 | run-unit-tests: $(alltests) |
| 127 | echo "**********************" |
| 128 | echo " ALL TESTS PASSED" |
| 129 | echo "**********************" |
| 130 | |
| 131 | $(addprefix clean-,$(alltests)): clean-%: |
| 132 | rm -rf $(obj)/$* |
| 133 | |
| 134 | clean-unit-tests: |
| 135 | rm -rf $(testobj) |