# Whether to generate a coverage report after running the tests.
USE_COVERAGE?= no # works only with gcc; clang9 fails to link
if ${USE_COVERAGE} == "yes"
GCOV?= gcov
CPPFLAGS+= -DFORK_FUNCTION=fork
COPTS+= --coverage -O0 -ggdb
GCOV_PERL= if (/^File '(?:.*\/)?(\S+)'/) {
GCOV_PERL+= $$file = $$1; $$func = "";
GCOV_PERL+= } elsif (/^Function '(\S+)'/) {
GCOV_PERL+= $$func = $$1;
GCOV_PERL+= } elsif (/^Lines executed:(\d+\.\d+)% of (\d+)/ && defined($$file)) {
GCOV_PERL+= my ($$percent, $$lines) = ($$1, $$2);
GCOV_PERL+= my $$uncovered =
GCOV_PERL+= $$percent eq '100.00' ? '0'
GCOV_PERL+= : $$file =~ /\.h$$/ ? '?'
GCOV_PERL+= : `grep -c '\#\#\#\#\#:' < \$$(basename $$file.gcov)`;
GCOV_PERL+= chomp($$uncovered);
GCOV_PERL+= printf("%7.2f %4s/%4d %s%s\n",
GCOV_PERL+= $$percent, $$uncovered, $$lines, $$file, $$func);
GCOV_PERL+= $$file = undef;
GCOV_PERL+= }
LDADD+= --coverage
endif
CLEANFILES+= *.gcda *.gcno *.gcov
# Whether to compile using the Undefined Behavior Sanitizer (GCC, Clang).
USE_UBSAN?= no
if ${USE_UBSAN} == "yes"
COPTS+= -fsanitize=undefined
LDADD+= -fsanitize=undefined
endif
if defined(TOOLDIR)
# This is a native NetBSD build, use libutil rather than the local emalloc etc.
CPPFLAGS+= -DUSE_EMALLOC
LDADD+= -lutil
DPADD+= ${LIBUTIL}
endif
COPTS+= -Wdeclaration-after-statement
# A simple unit-test driver to help catch regressions
TEST_MAKE ?= ${.OBJDIR}/${PROG:T}
test: .MAKE
cd ${.CURDIR}/unit-tests \
&& MAKEFLAGS= ${TEST_MAKE} -r -m / TEST_MAKE=${TEST_MAKE} ${TESTS:DTESTS=${TESTS:Q}} ${.TARGET}
if ${USE_COVERAGE} == yes
${MAKE} report-coverage
endif
accept sync-mi: .MAKE
cd ${.CURDIR}/unit-tests && ${.MAKE} ${.TARGET}
retest:
${.MAKE} -C ${.CURDIR}/unit-tests cleandir
if ${USE_COVERAGE} == yes
rm -f *.gcov *.gcda
endif
${.MAKE} test
# Just out of curiosity, during development.
SUFFIXES: .cpre .casm
c.cpre:
${COMPILE.c:S,^-c$,-E,} ${COPTS.${.IMPSRC}} ${.IMPSRC} -o ${.TARGET}
c.casm:
${COMPILE.c:S,^-c$,-S,} ${COPTS.${.IMPSRC}} ${.IMPSRC} -o ${.TARGET}
test-coverage: .PHONY
@make -s clean cleandir
@env USE_COVERAGE=yes COPTS="-O0 -ggdb" USER_CPPFLAGS="-DCLEANUP" \
sh -c 'make -s all -j8 && make -s test'
@env USE_COVERAGE=yes make report-coverage > coverage.txt