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 \ |
Julius Werner | 1e14de8 | 2020-06-10 15:35:08 -0700 | [diff] [blame] | 28 | -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include |
| 29 | |
| 30 | # Note: This is intentionally just a subset of the warnings in the toplevel |
| 31 | # Makefile.inc. We don't need to be as strict with test code, and things like |
| 32 | # -Wmissing-prototypes just make working with the test framework cumbersome. |
| 33 | # Only put conservative warnings here that really detect code that's obviously |
| 34 | # unintentional. |
Julius Werner | e54d784 | 2021-02-16 19:32:24 -0800 | [diff] [blame] | 35 | TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 36 | |
| 37 | # Path for Kconfig autoheader |
| 38 | TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER)) |
| 39 | |
Julius Werner | 2540037 | 2020-05-04 17:46:31 -0700 | [diff] [blame] | 40 | TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 41 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 42 | # Checkout Cmocka repository |
| 43 | forgetthis:=$(shell git submodule update --init --checkout 3rdparty/cmocka) |
| 44 | |
| 45 | TEST_CFLAGS += -I$(cmockasrc)/include |
| 46 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 47 | # Link against Cmocka |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 48 | TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src |
| 49 | TEST_LDFLAGS += -Wl,--gc-sections |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 50 | |
Julius Werner | 84446e6 | 2021-02-12 17:37:27 -0800 | [diff] [blame] | 51 | # Some memlayout symbols don't work with userspace relocation -- disable it. |
| 52 | TEST_CFLAGS += -fno-pie -fno-pic |
| 53 | TEST_LDFLAGS += -no-pie |
| 54 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 55 | # Extra attributes for unit tests, declared per test |
| 56 | attributes:= srcs cflags mocks stage |
| 57 | |
| 58 | stages:= decompressor bootblock romstage smm verstage |
| 59 | stages+= ramstage rmodule postcar libagesa |
| 60 | |
| 61 | alltests:= |
| 62 | subdirs:= tests/arch tests/commonlib tests/console tests/cpu tests/device |
| 63 | subdirs+= tests/drivers tests/ec tests/lib tests/mainboard |
| 64 | subdirs+= tests/northbridge tests/security tests/soc tests/southbridge |
| 65 | subdirs+= tests/superio tests/vendorcode |
| 66 | |
| 67 | define tests-handler |
| 68 | alltests += $(1)$(2) |
| 69 | $(foreach attribute,$(attributes), |
| 70 | $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute)))) |
| 71 | $(foreach attribute,$(attributes), |
| 72 | $(eval $(2)-$(attribute):=)) |
| 73 | |
| 74 | # Sanity check for stage attribute value |
| 75 | $(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage)) |
| 76 | $(if $(findstring $($(1)$(2)-stage), $(stages)),, |
| 77 | $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \ |
| 78 | Check your $(dir $(1)$(2))Makefile.inc)) |
| 79 | endef |
| 80 | |
| 81 | $(call add-special-class, tests) |
| 82 | $(call evaluate_subdirs) |
| 83 | |
| 84 | # Create actual targets for unit test binaries |
| 85 | # $1 - test name |
| 86 | define TEST_CC_template |
| 87 | $($(1)-objs): TEST_CFLAGS+= \ |
| 88 | -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__ |
| 89 | $($(1)-objs): $(obj)/$(1)/%.o: $$$$*.c $(TEST_KCONFIG_AUTOHEADER) |
| 90 | mkdir -p $$(dir $$@) |
| 91 | $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \ |
| 92 | -MT $$@ -c $$< -o $$@ |
| 93 | |
| 94 | $($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock)) |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 95 | $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB) |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 96 | $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@ |
| 97 | |
| 98 | endef |
| 99 | |
| 100 | $(foreach test, $(alltests), \ |
| 101 | $(eval $(test)-objs:=$(addprefix $(obj)/$(test)/, \ |
| 102 | $(patsubst %.c,%.o,$($(test)-srcs))))) |
| 103 | $(foreach test, $(alltests), \ |
| 104 | $(eval $(test)-bin:=$(obj)/$(test)/run)) |
| 105 | $(foreach test, $(alltests), \ |
| 106 | $(eval $(call TEST_CC_template,$(test)))) |
| 107 | |
| 108 | $(foreach test, $(alltests), \ |
| 109 | $(eval all-test-objs+=$($(test)-objs))) |
| 110 | $(foreach test, $(alltests), \ |
| 111 | $(eval test-bins+=$($(test)-bin))) |
| 112 | |
| 113 | DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs))) |
| 114 | -include $(DEPENDENCIES) |
| 115 | |
Jan Dabros | 7f00dba | 2020-05-22 09:57:17 +0200 | [diff] [blame] | 116 | # Build cmocka |
| 117 | $(CMOCKA_LIB): |
| 118 | echo "*** Building CMOCKA ***" |
| 119 | mkdir -p $(cmockaobj) |
| 120 | cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc)) |
| 121 | $(MAKE) -C $(cmockaobj) |
| 122 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 123 | # Kconfig targets |
| 124 | $(TEST_DOTCONFIG): |
| 125 | mkdir -p $(dir $@) |
| 126 | cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG) |
| 127 | |
| 128 | # Don't override default Kconfig variables, since this will affect all |
| 129 | # Kconfig targets. Change them only when calling sub-make instead. |
| 130 | $(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \ |
| 131 | KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \ |
| 132 | KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \ |
| 133 | KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \ |
| 134 | KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \ |
| 135 | KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \ |
| 136 | KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG) |
| 137 | |
| 138 | $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf |
| 139 | mkdir -p $(dir $@) |
| 140 | +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig |
| 141 | +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig |
| 142 | |
| 143 | $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER) |
| 144 | true |
| 145 | |
| 146 | .PHONY: $(alltests) $(addprefix clean-,$(alltests)) |
| 147 | .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests |
| 148 | |
Patrick Georgi | a0a198f | 2020-05-27 10:59:09 +0200 | [diff] [blame] | 149 | ifeq ($(JUNIT_OUTPUT),y) |
| 150 | $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml |
| 151 | $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^).xml |
| 152 | endif |
| 153 | |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 154 | $(alltests): $$($$(@)-bin) |
Patrick Georgi | 1b35ec9 | 2020-05-27 11:39:32 +0200 | [diff] [blame] | 155 | rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed |
| 156 | -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 157 | |
| 158 | unit-tests: build-unit-tests run-unit-tests |
| 159 | |
| 160 | build-unit-tests: $(test-bins) |
| 161 | |
| 162 | run-unit-tests: $(alltests) |
Patrick Georgi | 1b35ec9 | 2020-05-27 11:39:32 +0200 | [diff] [blame] | 163 | if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \ |
| 164 | echo "**********************"; \ |
| 165 | echo " TESTS FAILED"; \ |
| 166 | echo "**********************"; \ |
| 167 | exit 1; \ |
| 168 | else \ |
| 169 | echo "**********************"; \ |
| 170 | echo " ALL TESTS PASSED"; \ |
| 171 | echo "**********************"; \ |
| 172 | exit 0; \ |
| 173 | fi |
Jan Dabros | ef1c968 | 2020-03-28 00:15:03 +0100 | [diff] [blame] | 174 | |
| 175 | $(addprefix clean-,$(alltests)): clean-%: |
| 176 | rm -rf $(obj)/$* |
| 177 | |
| 178 | clean-unit-tests: |
| 179 | rm -rf $(testobj) |