util/sconfig: Change __pci*|__pnp* device pointers to const
[coreboot.git] / tests / Makefile.inc
blob53c0edf2a50ed8eebb7aac4e02d6410913962fc4
1 # SPDX-License-Identifier: GPL-2.0-only
3 testsrc = $(top)/tests
4 testobj = $(obj)/tests
5 cmockasrc = 3rdparty/cmocka
6 cmockaobj = $(objutil)/cmocka
8 CMOCKA_LIB := $(cmockaobj)/src/libcmocka.so
10 CMAKE:= cmake
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
34 # unintentional.
35 TEST_CFLAGS += -Wall -Werror -Wundef -Wstrict-prototypes
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
47 # Link against Cmocka
48 TEST_LDFLAGS = -L$(cmockaobj)/src -lcmocka -Wl,-rpath=$(cmockaobj)/src
49 TEST_LDFLAGS += -Wl,--gc-sections
51 # Extra attributes for unit tests, declared per test
52 attributes:= srcs cflags mocks stage
54 stages:= decompressor bootblock romstage smm verstage
55 stages+= ramstage rmodule postcar libagesa
57 alltests:=
58 subdirs:= tests/arch tests/commonlib tests/console tests/cpu tests/device
59 subdirs+= tests/drivers tests/ec tests/lib tests/mainboard
60 subdirs+= tests/northbridge tests/security tests/soc tests/southbridge
61 subdirs+= tests/superio tests/vendorcode
63 define tests-handler
64 alltests += $(1)$(2)
65 $(foreach attribute,$(attributes),
66         $(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
67 $(foreach attribute,$(attributes),
68         $(eval $(2)-$(attribute):=))
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))
75 endef
77 $(call add-special-class, tests)
78 $(call evaluate_subdirs)
80 # Create actual targets for unit test binaries
81 # $1 - test name
82 define 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 $$@
90 $($(1)-bin): TEST_LDFLAGS+= $$(foreach mock,$$($(1)-mocks),-Wl,--wrap=$$(mock))
91 $($(1)-bin): $($(1)-objs) $(CMOCKA_LIB)
92         $(HOSTCC) $$^ $($(1)-cflags) $$(TEST_LDFLAGS) -o $$@
94 endef
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))))
104 $(foreach test, $(alltests), \
105         $(eval all-test-objs+=$($(test)-objs)))
106 $(foreach test, $(alltests), \
107         $(eval test-bins+=$($(test)-bin)))
109 DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
110 -include $(DEPENDENCIES)
112 # Build cmocka
113 $(CMOCKA_LIB):
114         echo "*** Building CMOCKA ***"
115         mkdir -p $(cmockaobj)
116         cd $(cmockaobj) && $(CMAKE) $(abspath $(cmockasrc))
117         $(MAKE) -C $(cmockaobj)
119 # Kconfig targets
120 $(TEST_DOTCONFIG):
121         mkdir -p $(dir $@)
122         cp $(TEST_DEFAULT_CONFIG) $(TEST_DOTCONFIG)
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)
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
139 $(TEST_KCONFIG_AUTOCONFIG): $(TEST_KCONFIG_AUTOHEADER)
140         true
142 .PHONY: $(alltests) $(addprefix clean-,$(alltests))
143 .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
145 ifeq ($(JUNIT_OUTPUT),y)
146 $(alltests): export CMOCKA_MESSAGE_OUTPUT=xml
147 $(alltests): export CMOCKA_XML_FILE=$(testobj)/junit-$(subst /,_,$^).xml
148 endif
150 $(alltests): $$($$(@)-bin)
151         rm -f $(testobj)/junit-$(subst /,_,$^).xml $(testobj)/$(subst /,_,$^).failed
152         -./$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
154 unit-tests: build-unit-tests run-unit-tests
156 build-unit-tests: $(test-bins)
158 run-unit-tests: $(alltests)
159         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
171 $(addprefix clean-,$(alltests)): clean-%:
172         rm -rf $(obj)/$*
174 clean-unit-tests:
175         rm -rf $(testobj)