1 # SPDX-License-Identifier: GPL-2.0-only
5 cmockasrc = 3rdparty/cmocka
6 cmockaobj = $(objutil)/cmocka
8 CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
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
20 TEST_CFLAGS = -include $(src)/include/kconfig.h \
21 -include $(src)/commonlib/bsd/include/commonlib/bsd/compiler.h \
22 -include $(src)/include/rules.h
24 # Include generic test mock headers, before original ones
25 TEST_CFLAGS += -I$(testsrc)/include/mocks -I$(testsrc)/include
27 TEST_CFLAGS += -I$(src)/include -I$(src)/commonlib/include \
28 -I$(src)/commonlib/bsd/include -I$(src)/arch/x86/include
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
35 TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes -Wno-inline-asm
37 # Path for Kconfig autoheader
38 TEST_CFLAGS += -I$(dir $(TEST_KCONFIG_AUTOHEADER))
40 TEST_CFLAGS += -std=gnu11 -Os -ffunction-sections -fdata-sections -fno-builtin
42 # Checkout Cmocka repository
43 forgetthis:=$(shell git submodule update --init --checkout 3rdparty/cmocka)
45 TEST_CFLAGS += -I$(cmockasrc)/include
48 TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
49 TEST_LDFLAGS += -Wl,--gc-sections
51 # Some memlayout symbols don't work with userspace relocation -- disable it.
52 TEST_CFLAGS += -fno-pie -fno-pic
53 TEST_LDFLAGS += -no-pie
55 # Extra attributes for unit tests, declared per test
56 attributes:= srcs cflags mocks stage
58 stages:= decompressor bootblock romstage smm verstage
59 stages+= ramstage rmodule postcar libagesa
62 subdirs:= tests/arch tests/acpi tests/commonlib tests/console tests/cpu
63 subdirs+= tests/device tests/drivers tests/ec tests/lib tests/mainboard
64 subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
65 subdirs+= tests/superio tests/vendorcode
69 $(foreach attribute,$(attributes),
70 $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
71 $(foreach attribute,$(attributes),
72 $(eval $(2)-$(attribute):=))
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))
81 $(call add-special-class, tests)
82 $(call evaluate_subdirs)
84 # Create actual targets for unit test binaries
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)
91 $(HOSTCC) $(HOSTCFLAGS) $$(TEST_CFLAGS) $($(1)-cflags) -MMD \
94 $($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
95 $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
96 $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
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))))
108 $(foreach test, $(alltests), \
109 $(eval all-test-objs+=$($(test)-objs)))
110 $(foreach test, $(alltests), \
111 $(eval test-bins+=$($(test)-bin)))
113 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
114 -include $(DEPENDENCIES)
118 echo "*** Building CMOCKA ***"
119 mkdir -p $(cmockaobj)
120 cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
121 $(MAKE) -C $(cmockaobj)
126 cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
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)
138 $(TEST_KCONFIG_AUTOHEADER): $(TEST_DOTCONFIG) $(objutil)/kconfig/conf
140 +$(MAKE) $(TEST_KCONFIG_FLAGS) olddefconfig
141 +$(MAKE) $(TEST_KCONFIG_FLAGS) silentoldconfig
143 $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
146 .PHONY: $(alltests) $(addprefix clean-,$(alltests))
147 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
149 ifeq ($(JUNIT_OUTPUT),y)
150 $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
151 $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^).xml
154 $(alltests): $$($$(@)-bin)
155 rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
156 -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
158 unit-tests: build-unit-tests run-unit-tests
160 build-unit-tests: $(test-bins)
162 run-unit-tests: $(alltests)
163 if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
164 echo "**********************"; \
165 echo " TESTS FAILED"; \
166 echo "**********************"; \
169 echo "**********************"; \
170 echo " ALL TESTS PASSED"; \
171 echo "**********************"; \
175 $(addprefix clean-,$(alltests)): clean-%: