blob: 027091c5e69070ad8565f8bbb05d7251ce4cb611 [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
Jakub Czapiga084ad932021-03-25 13:10:31 +010042TEST_CFLAGS += -D__TEST__
43
Jan Dabros7f00dba2020-05-22 09:57:17 +020044# Checkout Cmocka repository
45forgetthis:=$(shell git submodule update --init --checkout 3rdparty/cmocka)
46
47TEST_CFLAGS += -I$(cmockasrc)/include
48
Jan Dabrosef1c9682020-03-28 00:15:03 +010049# Link against Cmocka
Jan Dabros7f00dba2020-05-22 09:57:17 +020050TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
51TEST_LDFLAGS += -Wl,--gc-sections
Jan Dabrosef1c9682020-03-28 00:15:03 +010052
Julius Werner84446e62021-02-12 17:37:27 -080053# Some memlayout symbols don't work with userspace relocation -- disable it.
54TEST_CFLAGS += -fno-pie -fno-pic
55TEST_LDFLAGS += -no-pie
56
Jan Dabrosef1c9682020-03-28 00:15:03 +010057# Extra attributes for unit tests, declared per test
58attributes:= srcs cflags mocks stage
59
60stages:= decompressor bootblock romstage smm verstage
61stages+= ramstage rmodule postcar libagesa
62
63alltests:=
Jakub Czapiga168b38b2021-02-11 12:59:51 +010064subdirs:= tests/arch tests/acpi tests/commonlib tests/console tests/cpu
65subdirs+= tests/device tests/drivers tests/ec tests/lib tests/mainboard
Jan Dabrosef1c9682020-03-28 00:15:03 +010066subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
67subdirs+= tests/superio tests/vendorcode
68
69define tests-handler
70alltests += $(1)$(2)
71$(foreach attribute,$(attributes),
72 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
73$(foreach attribute,$(attributes),
74 $(eval $(2)-$(attribute):=))
75
76# Sanity check for stage attribute value
77$(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
78$(if $(findstring $($(1)$(2)-stage), $(stages)),,
79 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
80 Check your $(dir $(1)$(2))Makefile.inc))
81endef
82
83$(call add-special-class, tests)
84$(call evaluate_subdirs)
85
86# Create actual targets for unit test binaries
87# $1 - test name
88define TEST_CC_template
89$($(1)-objs): TEST_CFLAGS+= \
90 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
91$($(1)-objs): $(obj)/$(1)/%.o: $$$$*.c $(TEST_KCONFIG_AUTOHEADER)
92 mkdir -p $$(dir $$@)
93 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
94 -MT $$@ -c $$< -o $$@
95
96$($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
Jan Dabros7f00dba2020-05-22 09:57:17 +020097$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +010098 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
99
100endef
101
102$(foreach test, $(alltests), \
103 $(eval $(test)-objs:=$(addprefix $(obj)/$(test)/, \
104 $(patsubst %.c,%.o,$($(test)-srcs)))))
105$(foreach test, $(alltests), \
106 $(eval $(test)-bin:=$(obj)/$(test)/run))
107$(foreach test, $(alltests), \
108 $(eval $(call TEST_CC_template,$(test))))
109
110$(foreach test, $(alltests), \
111 $(eval all-test-objs+=$($(test)-objs)))
112$(foreach test, $(alltests), \
113 $(eval test-bins+=$($(test)-bin)))
114
115DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
116-include $(DEPENDENCIES)
117
Jan Dabros7f00dba2020-05-22 09:57:17 +0200118# Build cmocka
119$(CMOCKA_LIB):
120 echo "*** Building CMOCKA ***"
121 mkdir -p $(cmockaobj)
122 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
123 $(MAKE) -C $(cmockaobj)
124
Jan Dabrosef1c9682020-03-28 00:15:03 +0100125# Kconfig targets
126$(TEST_DOTCONFIG):
127 mkdir -p $(dir $@)
128 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
129
130# Don't override default Kconfig variables, since this will affect all
131# Kconfig targets. Change them only when calling sub-make instead.
132$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \
133 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
134 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
135 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
136 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
137 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
138 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
139
140$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
141 mkdir -p $(dir $@)
142 +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
143 +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig
144
145$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
146 true
147
148.PHONY: $(alltests) $(addprefix clean-,$(alltests))
149.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
150
Patrick Georgia0a198f2020-05-27 10:59:09 +0200151ifeq ($(JUNIT_OUTPUT),y)
152$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
Jakub Czapiga1add4832021-03-05 11:37:23 +0100153$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^)-%g.xml
Patrick Georgia0a198f2020-05-27 10:59:09 +0200154endif
155
Jan Dabrosef1c9682020-03-28 00:15:03 +0100156$(alltests): $$($$(@)-bin)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200157 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
158 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100159
160unit-tests: build-unit-tests run-unit-tests
161
162build-unit-tests: $(test-bins)
163
164run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200165 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
166 echo "**********************"; \
167 echo " TESTS FAILED"; \
168 echo "**********************"; \
169 exit 1; \
170 else \
171 echo "**********************"; \
172 echo " ALL TESTS PASSED"; \
173 echo "**********************"; \
174 exit 0; \
175 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100176
177$(addprefix clean-,$(alltests)): clean-%:
178 rm -rf $(obj)/$*
179
180clean-unit-tests:
181 rm -rf $(testobj)