xref: /freebsd/share/mk/atf.test.mk (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
1# $FreeBSD$
2#
3# You must include bsd.test.mk instead of this file from your Makefile.
4#
5# Logic to build and install ATF test programs; i.e. test programs linked
6# against the ATF libraries.
7
8.if !target(__<bsd.test.mk>__)
9.error atf.test.mk cannot be included directly.
10.endif
11
12# List of C, C++ and shell test programs to build.
13#
14# Programs listed here are built using PROGS, PROGS_CXX and SCRIPTS,
15# respectively, from bsd.prog.mk.  However, the build rules are tweaked to
16# require the ATF libraries.
17#
18# Test programs registered in this manner are set to be installed into TESTSDIR
19# (which should be overriden by the Makefile) and are not required to provide a
20# manpage.
21ATF_TESTS_C?=
22ATF_TESTS_CXX?=
23ATF_TESTS_SH?=
24
25# Whether to allow using the deprecated ATF tools or not.
26#
27# If 'yes', this file will generate Atffiles when requested and will also
28# support using the deprecated atf-run tool to execute the tests.
29ALLOW_DEPRECATED_ATF_TOOLS?= no
30
31# Knob to control the handling of the Atffile for this Makefile.
32#
33# If 'yes', an Atffile exists in the source tree and is installed into
34# TESTSDIR.
35#
36# If 'auto', an Atffile is automatically generated based on the list of test
37# programs built by the Makefile and is installed into TESTSDIR.  This is the
38# default and is sufficient in the majority of the cases.
39#
40# If 'no', no Atffile is installed.
41ATFFILE?= auto
42
43# Path to the prefix of the installed ATF tools, if any.
44#
45# If atf-run and atf-report are installed from ports, we automatically define a
46# realtest target below to run the tests using these tools.  The tools are
47# searched for in the hierarchy specified by this variable.
48ATF_PREFIX?= /usr/local
49
50# C compiler passed to ATF tests that need to build code.
51ATF_BUILD_CC?= ${DESTDIR}/usr/bin/cc
52TESTS_ENV+= ATF_BUILD_CC=${ATF_BUILD_CC}
53
54# C preprocessor passed to ATF tests that need to build code.
55ATF_BUILD_CPP?= ${DESTDIR}/usr/bin/cpp
56TESTS_ENV+= ATF_BUILD_CPP=${ATF_BUILD_CPP}
57
58# C++ compiler passed to ATF tests that need to build code.
59ATF_BUILD_CXX?= ${DESTDIR}/usr/bin/c++
60TESTS_ENV+= ATF_BUILD_CXX=${ATF_BUILD_CXX}
61
62# Shell interpreter used to run atf-sh(1) based tests.
63ATF_SHELL?= ${DESTDIR}/bin/sh
64TESTS_ENV+= ATF_SHELL=${ATF_SHELL}
65
66.if !empty(ATF_TESTS_C)
67PROGS+= ${ATF_TESTS_C}
68_TESTS+= ${ATF_TESTS_C}
69.for _T in ${ATF_TESTS_C}
70BINDIR.${_T}= ${TESTSDIR}
71MAN.${_T}?= # empty
72SRCS.${_T}?= ${_T}.c
73DPADD.${_T}+= ${LIBATF_C}
74.if empty(LDFLAGS:M-static) && empty(LDFLAGS.${_T}:M-static)
75LDADD.${_T}+= ${LDATF_C}
76.else
77LDADD.${_T}+= ${LIBATF_C}
78.endif
79USEPRIVATELIB+= atf-c
80TEST_INTERFACE.${_T}= atf
81.endfor
82.endif
83
84.if !empty(ATF_TESTS_CXX)
85PROGS_CXX+= ${ATF_TESTS_CXX}
86_TESTS+= ${ATF_TESTS_CXX}
87.for _T in ${ATF_TESTS_CXX}
88BINDIR.${_T}= ${TESTSDIR}
89MAN.${_T}?= # empty
90SRCS.${_T}?= ${_T}${CXX_SUFFIX:U.cc}
91DPADD.${_T}+= ${LIBATF_CXX} ${LIBATF_C}
92.if empty(LDFLAGS:M-static) && empty(LDFLAGS.${_T}:M-static)
93LDADD.${_T}+= ${LDATF_CXX} ${LDATF_C}
94.else
95LDADD.${_T}+= ${LIBATF_CXX} ${LIBATF_C}
96.endif
97USEPRIVATELIB+= atf-c++
98TEST_INTERFACE.${_T}= atf
99.endfor
100.endif
101
102.if !empty(ATF_TESTS_SH)
103SCRIPTS+= ${ATF_TESTS_SH}
104_TESTS+= ${ATF_TESTS_SH}
105.for _T in ${ATF_TESTS_SH}
106SCRIPTSDIR_${_T}= ${TESTSDIR}
107TEST_INTERFACE.${_T}= atf
108CLEANFILES+= ${_T} ${_T}.tmp
109# TODO(jmmv): It seems to me that this SED and SRC functionality should
110# exist in bsd.prog.mk along the support for SCRIPTS.  Move it there if
111# this proves to be useful within the tests.
112ATF_TESTS_SH_SED_${_T}?= # empty
113ATF_TESTS_SH_SRC_${_T}?= ${_T}.sh
114${_T}: ${ATF_TESTS_SH_SRC_${_T}}
115	echo '#! /usr/libexec/atf-sh' > ${.TARGET}.tmp
116.if empty(ATF_TESTS_SH_SED_${_T})
117	cat ${.ALLSRC:N*Makefile*} >>${.TARGET}.tmp
118.else
119	cat ${.ALLSRC:N*Makefile*} \
120	    | sed ${ATF_TESTS_SH_SED_${_T}} >>${.TARGET}.tmp
121.endif
122	chmod +x ${.TARGET}.tmp
123	mv ${.TARGET}.tmp ${.TARGET}
124.endfor
125.endif
126
127.if ${ALLOW_DEPRECATED_ATF_TOOLS} != "no"
128
129.if ${ATFFILE:tl} != "no"
130FILES+=	Atffile
131FILESDIR_Atffile= ${TESTSDIR}
132
133.if ${ATFFILE:tl} == "auto"
134CLEANFILES+= Atffile Atffile.tmp
135
136Atffile: Makefile
137	@{ echo 'Content-Type: application/X-atf-atffile; version="1"'; \
138	echo; \
139	echo '# Automatically generated by atf-test.mk.'; \
140	echo; \
141	echo 'prop: test-suite = "'${TESTSUITE}'"'; \
142	echo; \
143	for tp in ${ATF_TESTS_C} ${ATF_TESTS_CXX} ${ATF_TESTS_SH} \
144	    ${TESTS_SUBDIRS}; \
145	do \
146	    echo "tp: $${tp}"; \
147	done; } >Atffile.tmp
148	@mv Atffile.tmp Atffile
149.endif
150.endif
151
152ATF_REPORT?= ${ATF_PREFIX}/bin/atf-report
153ATF_RUN?= ${ATF_PREFIX}/bin/atf-run
154.if exists(${ATF_RUN}) && exists(${ATF_REPORT})
155# Definition of the "make test" target and supporting variables.
156#
157# This target, by necessity, can only work for native builds (i.e. a freeBSD
158# host building a release for the same system).  The target runs ATF, which is
159# not in the toolchain, and the tests execute code built for the target host.
160#
161# Due to the dependencies of the binaries built by the source tree and how they
162# are used by tests, it is highly possible for a execution of "make test" to
163# report bogus results unless the new binaries are put in place.
164_TESTS_FIFO= ${.OBJDIR}/atf-run.fifo
165_TESTS_LOG= ${.OBJDIR}/atf-run.log
166CLEANFILES+= ${_TESTS_FIFO} ${_TESTS_LOG}
167realtest: .PHONY
168	@set -e; \
169	if [ -z "${TESTSDIR}" ]; then \
170	    echo "*** No TESTSDIR defined; nothing to do."; \
171	    exit 0; \
172	fi; \
173	cd ${DESTDIR}${TESTSDIR}; \
174	rm -f ${_TESTS_FIFO}; \
175	mkfifo ${_TESTS_FIFO}; \
176	tee ${_TESTS_LOG} < ${_TESTS_FIFO} | ${TESTS_ENV} ${ATF_REPORT} & \
177	set +e; \
178	${TESTS_ENV} ${ATF_RUN} >> ${_TESTS_FIFO}; \
179	result=$${?}; \
180	wait; \
181	rm -f ${_TESTS_FIFO}; \
182	echo; \
183	echo "*** The verbatim output of atf-run has been saved to ${_TESTS_LOG}"; \
184	echo "***"; \
185	echo "*** WARNING: atf-run is deprecated; please install kyua instead"; \
186	exit $${result}
187.endif
188
189.endif
190