1# $NetBSD: bsd.test.mk,v 1.26 2019/01/21 21:11:54 christos Exp $
2#
3
4.include <bsd.init.mk>
5
6_TESTS:=	# empty
7
8.if defined(TESTS_SUBDIRS)
9SUBDIR+=	${TESTS_SUBDIRS}
10.endif
11.if defined(TESTS_SUBDIR_INSTALL_ONLY)
12SUBDIR+=	${TESTS_SUBDIR_INSTALL_ONLY}
13.endif
14
15.include <bsd.subdir.mk>
16
17.if defined(TESTS_C)
18_TESTS+=	${TESTS_C}
19PROGS+=		${TESTS_C}
20.  for _T in ${TESTS_C}
21BINDIR.${_T}=	${TESTSDIR}
22LDADD.${_T}+=	-latf-c
23DPADD.${_T}+=	${LIBATF_C}
24MAN.${_T}?=	# empty
25.  endfor
26.endif
27
28.if defined(TESTS_CXX)
29_TESTS+=	${TESTS_CXX}
30PROGS_CXX+=	${TESTS_CXX}
31.  for _T in ${TESTS_CXX}
32BINDIR.${_T}=	${TESTSDIR}
33LDADD.${_T}+=	-latf-c++ -latf-c
34DPADD.${_T}+=	${LIBATF_CXX} ${LIBATF_C}
35MAN.${_T}?=	# empty
36.  endfor
37.endif
38
39.if defined(TESTS_SH)
40_TESTS+=		${TESTS_SH}
41CLEANDIRFILES+=		${TESTS_SH}
42
43.  for _T in ${TESTS_SH}
44SCRIPTS+=		${_T}
45SCRIPTSDIR_${_T}=	${TESTSDIR}
46
47CLEANDIRFILES+=		${_T}.tmp
48
49TESTS_SH_SRC_${_T}?=	${_T}.sh
50${_T}: ${TESTS_SH_SRC_${_T}}
51	${_MKTARGET_BUILD}
52	echo '#! /usr/bin/atf-sh' >${.TARGET}.tmp
53	cat ${.ALLSRC} >>${.TARGET}.tmp
54	chmod +x ${.TARGET}.tmp
55	${MV} ${.TARGET}.tmp ${.TARGET}
56.  endfor
57.endif
58
59ATFFILE?=	auto
60.if ${MKKYUA} != "no"
61KYUAFILE?=	${ATFFILE}
62.else
63KYUAFILE=	no
64.endif
65
66# Additional list of 'tp' entries to add to the Atffile when ATFFILE=auto.
67# These entries must all correspond to subdirectories to descend into.
68#
69# This is useful in the cases where the tests in a single directory come
70# from various sources (e.g. src/tests and src/external/.../tests) and
71# the installation of some of those tests rely on MK* variables being set.
72ATFFILE_EXTRA_SUBDIRS?=
73
74.if ${ATFFILE:tl} != "no"
75FILES+=			Atffile
76FILESDIR_Atffile=	${TESTSDIR}
77
78.  if ${ATFFILE:tl} == "auto"
79CLEANDIRFILES+=	Atffile Atffile.tmp
80
81realall: Atffile
82Atffile: Makefile
83	${_MKTARGET_CREATE}
84	@{ echo 'Content-Type: application/X-atf-atffile; version="1"'; \
85	echo; \
86	echo '# Automatically generated by bsd.test.mk.'; \
87	echo; \
88	echo 'prop: test-suite = "NetBSD"'; \
89	echo; \
90	for tp in ${TESTS_SUBDIRS:N.WAIT} ${_TESTS} ${ATFFILE_EXTRA_SUBDIRS}; \
91	do \
92	    echo "tp: $${tp}"; \
93	done; } >Atffile.tmp
94	@${MV} Atffile.tmp Atffile
95.  endif
96.endif
97
98.if ${KYUAFILE:tl} != "no"
99FILES+=			Kyuafile
100FILESDIR_Kyuafile=	${TESTSDIR}
101
102.  if ${KYUAFILE:tl} == "auto"
103CLEANDIRFILES+=	Kyuafile Kyuafile.tmp
104
105realall: Kyuafile
106Kyuafile: Makefile
107	${_MKTARGET_CREATE}
108	@{ \
109	    echo '-- Automatically generated by bsd.test.mk.'; \
110	    echo; \
111	    echo 'syntax(2)'; \
112	    echo; \
113	    echo 'test_suite("NetBSD")'; \
114	    if [ -n "${_TESTS}" ]; then \
115	        echo; \
116	        for tp in ${_TESTS}; do \
117	            echo "atf_test_program{name=\"$${tp}\"}"; \
118	        done; \
119	    fi; \
120	    if [ -n "${TESTS_SUBDIRS:N.WAIT}" -o \
121	         -n "${ATFFILE_EXTRA_SUBDIRS}" ]; then \
122	        echo; \
123	        for subdir in ${TESTS_SUBDIRS:N.WAIT} \
124	                      ${ATFFILE_EXTRA_SUBDIRS}; do \
125	            echo "include(\"$${subdir}/Kyuafile\")"; \
126	        done; \
127	    fi; \
128	} >Kyuafile.tmp
129	@${MV} Kyuafile.tmp Kyuafile
130.  endif
131.endif
132
133.if ${ATFFILE:tl} != "no" || ${KYUAFILE:tl} != "no"
134.  include <bsd.files.mk>
135.endif
136
137.if !empty(SCRIPTS) || !empty(PROGS) || !empty(PROGS_CXX)
138.  include <bsd.prog.mk>
139.endif
140
141#
142# Definition of the "make test" target and supporting variables.
143#
144# This target, by necessity, can only work for native builds (i.e. a NetBSD
145# host building a release for the same system).  The target runs ATF, which is
146# not in the toolchain, and the tests execute code built for the target host.
147#
148# Due to the dependencies of the binaries built by the source tree and how they
149# are used by tests, it is highly possible for a execution of "make test" to
150# report bogus results unless the new binaries are put in place.
151#
152
153TESTS_PATH += ${DESTDIR}/bin ${DESTDIR}/sbin ${DESTDIR}/usr/bin ${DESTDIR}/usr/sbin
154TESTS_LD_LIBRARY_PATH += ${DESTDIR}/lib ${DESTDIR}/usr/lib
155
156TESTS_ENV += ATF_BUILD_CC=${DESTDIR}/usr/bin/cc
157TESTS_ENV += ATF_BUILD_CPP=${DESTDIR}/usr/bin/cpp
158TESTS_ENV += ATF_BUILD_CXX=${DESTDIR}/usr/bin/c++
159TESTS_ENV += ATF_CONFDIR=${DESTDIR}/etc
160TESTS_ENV += ATF_INCLUDEDIR=${DESTDIR}/usr/include
161TESTS_ENV += ATF_LIBDIR=${DESTDIR}/usr/lib
162TESTS_ENV += ATF_LIBEXECDIR=${DESTDIR}/usr/libexec
163TESTS_ENV += ATF_PKGDATADIR=${DESTDIR}/usr/share/atf
164TESTS_ENV += ATF_SHELL=${DESTDIR}/bin/sh
165TESTS_ENV += LD_LIBRARY_PATH=${TESTS_LD_LIBRARY_PATH:tW:S/ /:/g}
166TESTS_ENV += PATH=${TESTS_PATH:tW:S/ /:/g}
167
168_TESTS_FIFO = ${.OBJDIR}/atf-run.fifo
169_TESTS_LOG = ${.OBJDIR}/atf-run.log
170CLEANDIRFILES += ${_TESTS_FIFO} ${_TESTS_LOG}
171
172.PHONY: test
173.if defined(TESTSDIR)
174.  if ${TESTSDIR} == ${TESTSBASE}
175# Forbid this case.  It is likely to cause false positives/negatives and it
176# does not cover all the tests (e.g. it misses testing software in external).
177test:
178	@echo "*** Sorry, you cannot use make test from src/tests.  Install the"
179	@echo "*** tests into their final location and run them from /usr/tests"
180	@false
181.  else
182test:
183	@echo "*** WARNING: make test is experimental"
184	@echo "***"
185	@echo "*** Using this test does not preclude you from running the tests"
186	@echo "*** installed in /usr/tests.  This test run may raise false"
187	@echo "*** positives and/or false negatives."
188	@echo
189	@set -e; \
190	cd ${DESTDIR}${TESTSDIR}; \
191	rm -f ${_TESTS_FIFO}; \
192	mkfifo ${_TESTS_FIFO}; \
193	cat ${_TESTS_FIFO} | tee ${_TESTS_LOG} | \
194	    ${TESTS_ENV} ${DESTDIR}/usr/bin/atf-report & \
195	result=0; \
196	${TESTS_ENV} ${DESTDIR}/usr/bin/atf-run >>${_TESTS_FIFO} || result=1; \
197	wait; \
198	rm -f ${_TESTS_FIFO}; \
199	echo; \
200	echo "*** The verbatim output of atf-run has been saved to ${_TESTS_LOG}"; \
201	echo "*** Once again, note that "make test" is unsupported."; \
202	test $${result} -eq 0
203.  endif
204.else
205test:
206	@echo "*** No TESTSDIR defined; nothing to do."
207.endif
208
209##### Pull in related .mk logic
210.include <bsd.clean.mk>
211