blob: 53c0edf2a50ed8eebb7aac4e02d6410913962fc4 [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.
35TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes
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
51# Extra attributes for unit tests, declared per test
52attributes:= srcs cflags mocks stage
53
54stages:= decompressor bootblock romstage smm verstage
55stages+= ramstage rmodule postcar libagesa
56
57alltests:=
58subdirs:= tests/arch tests/commonlib tests/console tests/cpu tests/device
59subdirs+= tests/drivers tests/ec tests/lib tests/mainboard
60subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
61subdirs+= tests/superio tests/vendorcode
62
63define tests-handler
64alltests += $(1)$(2)
65$(foreach attribute,$(attributes),
66 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
67$(foreach attribute,$(attributes),
68 $(eval $(2)-$(attribute):=))
69
70# Sanity check for stage attribute value
71$(eval $(1)$(2)-stage:=$(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
72$(if $(findstring $($(1)$(2)-stage), $(stages)),,
73 $(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
74 Check your $(dir $(1)$(2))Makefile.inc))
75endef
76
77$(call add-special-class, tests)
78$(call evaluate_subdirs)
79
80# Create actual targets for unit test binaries
81# $1 - test name
82define TEST_CC_template
83$($(1)-objs): TEST_CFLAGS+= \
84 -D__$$(shell echo $$($(1)-stage) | tr '[:lower:]' '[:upper:]')__
85$($(1)-objs): $(obj)/$(1)/%.o: $$$$*.c $(TEST_KCONFIG_AUTOHEADER)
86 mkdir -p $$(dir $$@)
87 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
88 -MT $$@ -c $$< -o $$@
89
90$($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
Jan Dabros7f00dba2020-05-22 09:57:17 +020091$($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
Jan Dabrosef1c9682020-03-28 00:15:03 +010092 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
93
94endef
95
96$(foreach test, $(alltests), \
97 $(eval $(test)-objs:=$(addprefix $(obj)/$(test)/, \
98 $(patsubst %.c,%.o,$($(test)-srcs)))))
99$(foreach test, $(alltests), \
100 $(eval $(test)-bin:=$(obj)/$(test)/run))
101$(foreach test, $(alltests), \
102 $(eval $(call TEST_CC_template,$(test))))
103
104$(foreach test, $(alltests), \
105 $(eval all-test-objs+=$($(test)-objs)))
106$(foreach test, $(alltests), \
107 $(eval test-bins+=$($(test)-bin)))
108
109DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
110-include $(DEPENDENCIES)
111
Jan Dabros7f00dba2020-05-22 09:57:17 +0200112# Build cmocka
113$(CMOCKA_LIB):
114 echo "*** Building CMOCKA ***"
115 mkdir -p $(cmockaobj)
116 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
117 $(MAKE) -C $(cmockaobj)
118
Jan Dabrosef1c9682020-03-28 00:15:03 +0100119# Kconfig targets
120$(TEST_DOTCONFIG):
121 mkdir -p $(dir $@)
122 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
123
124# Don't override default Kconfig variables, since this will affect all
125# Kconfig targets. Change them only when calling sub-make instead.
126$(TEST_KCONFIG_AUTOHEADER): TEST_KCONFIG_FLAGS:= DOTCONFIG=$(TEST_DOTCONFIG) \
127 KCONFIG_AUTOHEADER=$(TEST_KCONFIG_AUTOHEADER) \
128 KCONFIG_AUTOCONFIG=$(TEST_KCONFIG_AUTOCONFIG) \
129 KCONFIG_DEPENDENCIES=$(TEST_KCONFIG_DEPENDENCIES) \
130 KCONFIG_SPLITCONFIG=$(TEST_KCONFIG_SPLITCONFIG) \
131 KCONFIG_TRISTATE=$(TEST_KCONFIG_TRISTATE) \
132 KBUILD_DEFCONFIG=$(TEST_DEFAULT_CONFIG)
133
134$(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
135 mkdir -p $(dir $@)
136 +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
137 +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig
138
139$(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
140 true
141
142.PHONY: $(alltests) $(addprefix clean-,$(alltests))
143.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
144
Patrick Georgia0a198f2020-05-27 10:59:09 +0200145ifeq ($(JUNIT_OUTPUT),y)
146$(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
147$(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^).xml
148endif
149
Jan Dabrosef1c9682020-03-28 00:15:03 +0100150$(alltests): $$($$(@)-bin)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200151 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
152 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
Jan Dabrosef1c9682020-03-28 00:15:03 +0100153
154unit-tests: build-unit-tests run-unit-tests
155
156build-unit-tests: $(test-bins)
157
158run-unit-tests: $(alltests)
Patrick Georgi1b35ec92020-05-27 11:39:32 +0200159 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
160 echo "**********************"; \
161 echo " TESTS FAILED"; \
162 echo "**********************"; \
163 exit 1; \
164 else \
165 echo "**********************"; \
166 echo " ALL TESTS PASSED"; \
167 echo "**********************"; \
168 exit 0; \
169 fi
Jan Dabrosef1c9682020-03-28 00:15:03 +0100170
171$(addprefix clean-,$(alltests)): clean-%:
172 rm -rf $(obj)/$*
173
174clean-unit-tests:
175 rm -rf $(testobj)