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