/ tests / Makefile.mk
Makefile.mk
  1  # SPDX-License-Identifier: GPL-2.0-only
  2  
  3  # Place the build output in one of two places depending on COV, so that code
  4  # built with code coverage never mixes with code built without code coverage.
  5  ifeq ($(COV),1)
  6  testobj := $(obj)/coverage
  7  else
  8  testobj := $(obj)/tests
  9  endif
 10  
 11  objutil := $(testobj)/util
 12  
 13  include $(top)/tests/Makefile.common
 14  
 15  # Enable GDB debug build if requested
 16  GDB_DEBUG ?= 0
 17  ifneq ($(GDB_DEBUG),0)
 18  TEST_CFLAGS += -g -Og
 19  endif
 20  
 21  # Enable code coverage if requested
 22  ifeq ($(COV),1)
 23  TEST_CFLAGS += --coverage
 24  TEST_LDFLAGS += --coverage
 25  endif
 26  
 27  stages := decompressor bootblock romstage smm verstage
 28  stages += ramstage rmodule postcar libagesa
 29  
 30  alltests :=
 31  subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu
 32  subdirs += tests/device tests/drivers tests/ec tests/lib
 33  subdirs += tests/mainboard tests/northbridge tests/security tests/soc
 34  subdirs += tests/southbridge tests/superio tests/vendorcode
 35  
 36  define tests-handler
 37  alltests += $(1)$(2)
 38  $(foreach attribute,$(attributes),
 39  	$(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute))))
 40  $(foreach attribute,$(attributes),
 41  	$(eval $(2)-$(attribute) := ))
 42  
 43  # Sanity check for stage attribute value
 44  $(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage))
 45  $(if $(findstring $($(1)$(2)-stage), $(stages)),,
 46  	$(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \
 47  		Check your $(dir $(1)$(2))Makefile.mk))
 48  endef
 49  
 50  $(call add-special-class, tests)
 51  $(call evaluate_subdirs)
 52  
 53  $(foreach test, $(alltests), \
 54  	$(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \
 55  		$(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \
 56  	$(eval $(test)-sysobjs := $(addprefix $(testobj)/$(test)/, \
 57  		$(patsubst %.c,%.o,$($(test)-syssrcs)))) \
 58  	$(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \
 59  		$(patsubst %.c,%.o,$($(test)-srcs)))))
 60  $(foreach test, $(alltests), \
 61  	$(eval $(test)-bin := $(testobj)/$(test)/run))
 62  $(foreach test, $(alltests), \
 63  	$(eval $(call TEST_CC_template,$(test))))
 64  
 65  $(foreach test, $(alltests), \
 66  	$(eval all-test-objs += $($(test)-objs)))
 67  $(foreach test, $(alltests), \
 68  	$(eval test-bins += $($(test)-bin)))
 69  
 70  DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs)))
 71  -include $(DEPENDENCIES)
 72  
 73  .PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests))
 74  .PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests))
 75  .PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests
 76  .PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests
 77  
 78  # %g in CMOCKA_XML_FILE will be replaced with "__TEST_NAME__(<test-group-name>)"
 79  # by macro cb_run_group_tests(), which should be used for running tests.
 80  # __TEST_NAME__ contains test name including path e.g. tests_lib_rtc-test
 81  ifeq ($(JUNIT_OUTPUT),y)
 82  $(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml
 83  $(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml
 84  endif
 85  
 86  $(addprefix run-,$(alltests)): run-%: $$(%-bin)
 87  	rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml
 88  	rm -f $(testobj)/$(subst /,_,$^).failed
 89  	-$^ || echo failed > $(testobj)/$(subst /,_,$^).failed
 90  
 91  $(addprefix build-,$(alltests)): build-%: $$(%-bin)
 92  
 93  $(alltests): run-$$(@)
 94  
 95  $(addprefix try-,$(alltests)): try-%: clean-% $(TEST_COMMON_DEPENDENCIES)
 96  	mkdir -p $(testobj)/$*
 97  	echo "<testcase classname='coreboot_build_unit_test' name='$*'>" >> $(testobj)/$*.tmp; \
 98  	$(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "build-$*" >> $(testobj)/$*.tmp.2 2>&1 \
 99  		&& type="system-out" || type="failure"; \
100  	if [ $$type = "failure" ]; then \
101  		echo "<failure type='buildFailed'>" >> $(testobj)/$*.tmp; \
102  	else \
103  		echo "<$$type>" >> $(testobj)/$*.tmp; \
104  	fi; \
105  	echo '<![CDATA[' >> $(testobj)/$*.tmp; \
106  	cat $(testobj)/$*.tmp.2 >> $(testobj)/$*.tmp; \
107  	echo "]]></$$type>" >> $(testobj)/$*.tmp; \
108  	rm -f $(testobj)/$*.tmp.2; \
109  	echo "</testcase>" >> $(testobj)/$*.tmp; \
110  	if [ $$type != 'failure' ]; then \
111  		$(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "run-$*"; \
112  	fi
113  
114  
115  TESTS_BUILD_XML_FILE := $(testobj)/junit-tests-build.xml
116  
117  $(TESTS_BUILD_XML_FILE): clean-junit.xml-unit-tests $(addprefix try-,$(alltests))
118  	mkdir -p $(dir $@)
119  	echo '<?xml version="1.0" encoding="utf-8"?><testsuite>' > $@
120  	for tst in $(alltests); do \
121  		cat $(testobj)/$$tst.tmp >> $@; \
122  	done
123  	echo "</testsuite>" >> $@
124  
125  junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE)
126  
127  clean-junit.xml-unit-tests:
128  	rm -f $(TESTS_BUILD_XML_FILE)
129  
130  
131  # Build a code coverage report by collecting all the gcov files into a single
132  # report. If COV is not set, this might be a user error, and they're trying
133  # to generate a coverage report without first having built and run the code
134  # with code coverage. So instead of silently correcting it by adding COV=1,
135  # let's flag it to the user so they can be sure they're doing the thing they
136  # want to do.
137  
138  .PHONY: coverage-report clean-coverage-report
139  
140  ifeq ($(COV),1)
141  coverage-report:
142  	lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*'
143  	genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \
144  	-s $(testobj)/tests.info
145  
146  clean-coverage-report:
147  	rm -Rf $(testobj)/$(coverage_dir)
148  else
149  coverage-report:
150  	COV=1 V=$(V) $(MAKE) coverage-report
151  
152  clean-coverage-report:
153  	COV=1 V=$(V) $(MAKE) clean-coverage-report
154  endif
155  
156  unit-tests: build-unit-tests run-unit-tests
157  
158  build-unit-tests: $(test-bins)
159  
160  run-unit-tests: $(alltests)
161  	if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \
162  		echo "**********************"; \
163  		echo "     TESTS FAILED"; \
164  		echo "**********************"; \
165  		exit 1; \
166  	else \
167  		echo "**********************"; \
168  		echo "   ALL TESTS PASSED"; \
169  		echo "**********************"; \
170  		exit 0; \
171  	fi
172  
173  $(addprefix clean-,$(alltests)): clean-%:
174  	rm -rf $(testobj)/$*
175  
176  clean-unit-tests:
177  	rm -rf $(testobj)
178  
179  list-unit-tests:
180  	@echo "unit-tests:"
181  	for t in $(sort $(alltests)); do \
182  		echo "  $$t"; \
183  	done
184  
185  help-unit-tests help::
186  	@echo  '*** coreboot unit-tests targets ***'
187  	@echo  '  Use "COV=1 make [target]" to enable code coverage for unit tests'
188  	@echo  '  Use "GDB_DEBUG=1 make [target]" to build with debug symbols'
189  	@echo  '  unit-tests            - Run all unit-tests from tests/'
190  	@echo  '  clean-unit-tests      - Remove unit-tests build artifacts'
191  	@echo  '  list-unit-tests       - List all unit-tests'
192  	@echo  '  <unit-test>           - Build and run single unit-test'
193  	@echo  '  clean-<unit-test>     - Remove single unit-test build artifacts'
194  	@echo  '  coverage-report       - Generate a code coverage report'
195  	@echo  '  clean-coverage-report - Remove the code coverage report'
196  	@echo