| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 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 |
|
| 16 |
GDB_DEBUG ?= 0 |
| 17 |
ifneq ($(GDB_DEBUG),0) |
| 18 |
TEST_CFLAGS += -g -Og |
| 19 |
endif |
| 20 |
|
| 21 |
|
| 22 |
ifeq ($(COV),1) |
| 23 |
TEST_CFLAGS += --coverage |
| 24 |
TEST_LDFLAGS += --coverage |
| 25 |
endif |
| 26 |
|
| 27 |
|
| 28 |
USE_SYSTEM_CMOCKA ?= 1 |
| 29 |
ifeq ($(USE_SYSTEM_CMOCKA),1) |
| 30 |
ifeq ($(shell $(HOSTPKG_CONFIG) --exists cmocka || echo 1),1) |
| 31 |
$(warning No system cmocka, build from 3rdparty instead...) |
| 32 |
USE_SYSTEM_CMOCKA=0 |
| 33 |
endif |
| 34 |
endif |
| 35 |
|
| 36 |
stages := decompressor bootblock romstage smm verstage |
| 37 |
stages += ramstage rmodule postcar libagesa |
| 38 |
|
| 39 |
alltests := |
| 40 |
subdirs := tests/arch tests/acpi tests/commonlib tests/console tests/cpu |
| 41 |
subdirs += tests/device tests/drivers tests/ec tests/lib |
| 42 |
subdirs += tests/mainboard tests/northbridge tests/security tests/soc |
| 43 |
subdirs += tests/southbridge tests/superio tests/vendorcode |
| 44 |
|
| 45 |
define tests-handler |
| 46 |
alltests += $(1)$(2) |
| 47 |
$(foreach attribute,$(attributes), |
| 48 |
$(eval $(1)$(2)-$(attribute) += $($(2)-$(attribute)))) |
| 49 |
$(foreach attribute,$(attributes), |
| 50 |
$(eval $(2)-$(attribute) := )) |
| 51 |
|
| 52 |
# Sanity check for stage attribute value |
| 53 |
$(eval $(1)$(2)-stage := $(if $($(1)$(2)-stage),$($(1)$(2)-stage),ramstage)) |
| 54 |
$(if $(findstring $($(1)$(2)-stage), $(stages)),, |
| 55 |
$(error Wrong $(1)$(2)-stage value $($(1)$(2)-stage). \ |
| 56 |
Check your $(dir $(1)$(2))Makefile.mk)) |
| 57 |
endef |
| 58 |
|
| 59 |
$(call add-special-class, tests) |
| 60 |
$(call evaluate_subdirs) |
| 61 |
|
| 62 |
$(foreach test, $(alltests), \ |
| 63 |
$(eval $(test)-srcobjs := $(addprefix $(testobj)/$(test)/, \ |
| 64 |
$(patsubst %.c,%.o,$(filter src/%,$($(test)-srcs))))) \ |
| 65 |
$(eval $(test)-sysobjs := $(addprefix $(testobj)/$(test)/, \ |
| 66 |
$(patsubst %.c,%.o,$($(test)-syssrcs)))) \ |
| 67 |
$(eval $(test)-objs := $(addprefix $(testobj)/$(test)/, \ |
| 68 |
$(patsubst %.c,%.o,$($(test)-srcs))))) |
| 69 |
$(foreach test, $(alltests), \ |
| 70 |
$(eval $(test)-bin := $(testobj)/$(test)/run)) |
| 71 |
$(foreach test, $(alltests), \ |
| 72 |
$(eval $(call TEST_CC_template,$(test)))) |
| 73 |
|
| 74 |
$(foreach test, $(alltests), \ |
| 75 |
$(eval all-test-objs += $($(test)-objs))) |
| 76 |
$(foreach test, $(alltests), \ |
| 77 |
$(eval test-bins += $($(test)-bin))) |
| 78 |
|
| 79 |
DEPENDENCIES += $(addsuffix .d,$(basename $(all-test-objs))) |
| 80 |
-include $(DEPENDENCIES) |
| 81 |
|
| 82 |
.PHONY: $(alltests) $(addprefix clean-,$(alltests)) $(addprefix try-,$(alltests)) |
| 83 |
.PHONY: $(addprefix build-,$(alltests)) $(addprefix run-,$(alltests)) |
| 84 |
.PHONY: unit-tests build-unit-tests run-unit-tests clean-unit-tests |
| 85 |
.PHONY: junit.xml-unit-tests clean-junit.xml-unit-tests |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
ifeq ($(JUNIT_OUTPUT),y) |
| 91 |
$(addprefix run-,$(alltests)): export CMOCKA_MESSAGE_OUTPUT=xml |
| 92 |
$(addprefix run-,$(alltests)): export CMOCKA_XML_FILE=$(testobj)/junit-%g.xml |
| 93 |
endif |
| 94 |
|
| 95 |
$(addprefix run-,$(alltests)): run-%: $$(%-bin) |
| 96 |
rm -f $(testobj)/junit-$(subst /,_,$(patsubst $(testobj)/%/,%,$(dir $^)))\(*\).xml |
| 97 |
rm -f $(testobj)/$(subst /,_,$^).failed |
| 98 |
-$^ || echo failed > $(testobj)/$(subst /,_,$^).failed |
| 99 |
|
| 100 |
$(addprefix build-,$(alltests)): build-%: $$(%-bin) |
| 101 |
|
| 102 |
$(alltests): run-$$(@) |
| 103 |
|
| 104 |
$(addprefix try-,$(alltests)): try-%: clean-% $(TEST_COMMON_DEPENDENCIES) |
| 105 |
mkdir -p $(testobj)/$* |
| 106 |
echo "<testcase classname='coreboot_build_unit_test' name='$*'>" >> $(testobj)/$*.tmp; \ |
| 107 |
$(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "build-$*" >> $(testobj)/$*.tmp.2 2>&1 \ |
| 108 |
&& type="system-out" || type="failure"; \ |
| 109 |
if [ $$type = "failure" ]; then \ |
| 110 |
echo "<failure type='buildFailed'>" >> $(testobj)/$*.tmp; \ |
| 111 |
else \ |
| 112 |
echo "<$$type>" >> $(testobj)/$*.tmp; \ |
| 113 |
fi; \ |
| 114 |
echo '<![CDATA[' >> $(testobj)/$*.tmp; \ |
| 115 |
cat $(testobj)/$*.tmp.2 >> $(testobj)/$*.tmp; \ |
| 116 |
echo "]]></$$type>" >> $(testobj)/$*.tmp; \ |
| 117 |
rm -f $(testobj)/$*.tmp.2; \ |
| 118 |
echo "</testcase>" >> $(testobj)/$*.tmp; \ |
| 119 |
if [ $$type != 'failure' ]; then \ |
| 120 |
$(MAKE) V=$(V) Q=$(Q) COV=$(COV) JUNIT_OUTPUT=y "run-$*"; \ |
| 121 |
fi |
| 122 |
|
| 123 |
|
| 124 |
TESTS_BUILD_XML_FILE := $(testobj)/junit-tests-build.xml |
| 125 |
|
| 126 |
$(TESTS_BUILD_XML_FILE): clean-junit.xml-unit-tests $(addprefix try-,$(alltests)) |
| 127 |
mkdir -p $(dir $@) |
| 128 |
echo '<?xml version="1.0" encoding="utf-8"?><testsuite>' > $@ |
| 129 |
for tst in $(alltests); do \ |
| 130 |
cat $(testobj)/$$tst.tmp >> $@; \ |
| 131 |
done |
| 132 |
echo "</testsuite>" >> $@ |
| 133 |
|
| 134 |
junit.xml-unit-tests: $(TESTS_BUILD_XML_FILE) |
| 135 |
|
| 136 |
clean-junit.xml-unit-tests: |
| 137 |
rm -f $(TESTS_BUILD_XML_FILE) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
.PHONY: coverage-report clean-coverage-report |
| 148 |
|
| 149 |
ifeq ($(COV),1) |
| 150 |
coverage-report: |
| 151 |
lcov -o $(testobj)/tests.info -c -d $(testobj) --exclude '$(testsrc)/*' |
| 152 |
genhtml -q -o $(testobj)/$(coverage_dir) -t "coreboot unit tests" \ |
| 153 |
-s $(testobj)/tests.info |
| 154 |
|
| 155 |
clean-coverage-report: |
| 156 |
rm -Rf $(testobj)/$(coverage_dir) |
| 157 |
else |
| 158 |
coverage-report: |
| 159 |
COV=1 V=$(V) $(MAKE) coverage-report |
| 160 |
|
| 161 |
clean-coverage-report: |
| 162 |
COV=1 V=$(V) $(MAKE) clean-coverage-report |
| 163 |
endif |
| 164 |
|
| 165 |
unit-tests: build-unit-tests run-unit-tests |
| 166 |
|
| 167 |
build-unit-tests: $(test-bins) |
| 168 |
|
| 169 |
run-unit-tests: $(alltests) |
| 170 |
if [ `find $(testobj) -name '*.failed' | wc -l` -gt 0 ]; then \ |
| 171 |
echo "**********************"; \ |
| 172 |
echo " TESTS FAILED"; \ |
| 173 |
echo "**********************"; \ |
| 174 |
exit 1; \ |
| 175 |
else \ |
| 176 |
echo "**********************"; \ |
| 177 |
echo " ALL TESTS PASSED"; \ |
| 178 |
echo "**********************"; \ |
| 179 |
exit 0; \ |
| 180 |
fi |
| 181 |
|
| 182 |
$(addprefix clean-,$(alltests)): clean-%: |
| 183 |
rm -rf $(testobj)/$* |
| 184 |
|
| 185 |
clean-unit-tests: |
| 186 |
rm -rf $(testobj) |
| 187 |
|
| 188 |
list-unit-tests: |
| 189 |
@echo "unit-tests:" |
| 190 |
for t in $(sort $(alltests)); do \ |
| 191 |
echo " $$t"; \ |
| 192 |
done |
| 193 |
|
| 194 |
help-unit-tests help:: |
| 195 |
@echo '*** coreboot unit-tests targets ***' |
| 196 |
@echo ' Use "COV=1 make [target]" to enable code coverage for unit tests' |
| 197 |
@echo ' Use "GDB_DEBUG=1 make [target]" to build with debug symbols' |
| 198 |
@echo ' Use "USE_SYSTEM_CMOCKA=0 make [target]" to build Cmocka from source' |
| 199 |
@echo ' unit-tests - Run all unit-tests from tests/' |
| 200 |
@echo ' clean-unit-tests - Remove unit-tests build artifacts' |
| 201 |
@echo ' list-unit-tests - List all unit-tests' |
| 202 |
@echo ' <unit-test> - Build and run single unit-test' |
| 203 |
@echo ' clean-<unit-test> - Remove single unit-test build artifacts' |
| 204 |
@echo ' coverage-report - Generate a code coverage report' |
| 205 |
@echo ' clean-coverage-report - Remove the code coverage report' |
| 206 |
@echo |