blob: a10e9bf73b12a07eba78b043a196ea455aa44486 [file] [log] [blame]
Jan Dabrosef1c9682020-03-28 00:15:03 +01001# SPDX-License-Identifier: GPL-2.0-only
Jan Dabrosef1c9682020-03-28 00:15:03 +01002
Julius Werner21744812020-05-04 17:44:04 -07003testsrc = $(top)/tests
Jan Dabrosef1c9682020-03-28 00:15:03 +01004testobj = $(obj)/tests
Jan Dabros7f00dba2020-05-22 09:57:17 +02005cmockasrc = 3rdparty/cmocka
Patrick Georgia3402972020-05-27 10:53:01 +02006cmockaobj = $(objutil)/cmocka
Jan Dabros7f00dba2020-05-22 09:57:17 +02007
8CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
9
10CMAKE:= cmake
Jan Dabrosef1c9682020-03-28 00:15:03 +010011
12TEST_DEFAULT_CONFIG = $(top)/configs/config.emulation_qemu_x86_i440fx
13TEST_DOTCONFIG = $(testobj)/.config
14TEST_KCONFIG_AUTOHEADER := $(testobj)/config.h
15TEST_KCONFIG_AUTOCONFIG := $(testobj)/auto.conf
16TEST_KCONFIG_DEPENDENCIES := $(testobj)/auto.conf.cmd
17TEST_KCONFIG_SPLITCONFIG := $(testobj)/config
18TEST_KCONFIG_TRISTATE := $(testobj)/tristate.conf
19
Julius Werner21744812020-05-04 17:44:04 -070020TEST_CFLAGS = -include $(src)/include/kconfig.h \
21 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
22 -include $(src)/include/rules.h
Jan Dabrosef1c9682020-03-28 00:15:03 +010023
24# Include generic test mock headers, before original ones
Julius Werner21744812020-05-04 17:44:04 -070025TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
Jan Dabrosef1c9682020-03-28 00:15:03 +010026
27TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \
Julius Werner1e14de82020-06-10 15:35:08 -070028 -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 Wernere54d7842021-02-16 19:32:24 -080035TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
Jan Dabrosef1c9682020-03-28 00:15:03 +010036
37# Path for Kconfig autoheader
38TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
39
Julius Werner25400372020-05-04 17:46:31 -070040TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
Jan Dabrosef1c9682020-03-28 00:15:03 +010041
Jan Dabros7f00dba2020-05-22 09:57:17 +020042# Checkout Cmocka repository
43forgetthis:=$(shell git submodule update --init --checkout 3rdparty/cmocka)
44
45TEST_CFLAGS += -I$(cmockasrc)/include
46
Jan Dabrosef1c9682020-03-28 00:15:03 +010047# Link against Cmocka
Jan Dabros7f00dba2020-05-22 09:57:17 +020048TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
49TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010050
Julius Werner84446e62021-02-12 17:37:27 -080051# Some memlayout symbols don't work with userspace relocation -- disable it.
52TEST_CFLAGS += -fno-pie -fno-pic
53TEST_LDFLAGS += -no-pie
54
Jan Dabrosef1c9682020-03-28 00:15:03 +010055# Extra attributes for unit tests, declared per test
56attributes:= srcs cflags mocks stage
57
58stages:= decompressor bootblock romstage smm verstage
59stages+= ramstage rmodule postcar libagesa
60
61alltests:=
62subdirs:= tests/arch tests/commonlib tests/console tests/cpu tests/device
63subdirs+= tests/drivers tests/ec tests/lib tests/mainboard
64subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
65subdirs+= tests/superio tests/vendorcode
66
67define tests-handler
68alltests += $(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))
79endef
80
81$(call add-special-class, tests)
82$(call evaluate_subdirs)
83
84# Create actual targets for unit test binaries
85# $1 - test name
86define 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 Dabros7f00dba2020-05-22 09:57:17 +020095$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +010096 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
97
98endef
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
113DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
114-include $(DEPENDENCIES)
115
Jan Dabros7f00dba2020-05-22 09:57:17 +0200116# 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 Dabrosef1c9682020-03-28 00:15:03 +0100123# 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 Georgia0a198f2020-05-27 10:59:09 +0200149ifeq ($(JUNIT_OUTPUT),y)
150$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
151$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^).xml
152endif
153
Jan Dabrosef1c9682020-03-28 00:15:03 +0100154$(alltests): $$($$(@)-bin)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200155 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
156 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100157
158unit-tests: build-unit-tests run-unit-tests
159
160build-unit-tests: $(test-bins)
161
162run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200163 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 Dabrosef1c9682020-03-28 00:15:03 +0100174
175$(addprefix clean-,$(alltests)): clean-%:
176 rm -rf $(obj)/$*
177
178clean-unit-tests:
179 rm -rf $(testobj)