Makefile revision 1.149
1# $NetBSD: Makefile,v 1.149 2020/09/25 05:56:59 rillig Exp $
2#
3# Unit tests for make(1)
4#
5# The main targets are:
6#
7# all:
8#	run all the tests
9# test:
10#	run 'all', and compare to expected results
11# accept:
12#	move generated output to expected results
13#
14# Settable variables
15#
16# TEST_MAKE
17#	The make program to be tested.
18#
19#
20# Adding a test case
21#
22# Each feature should get its own set of tests in its own suitably
23# named makefile (*.mk), with its own set of expected results (*.exp),
24# and it should be added to the TESTS list.
25#
26# Any added files must also be added to src/distrib/sets/lists/tests/mi.
27# To do that, just run "make sync-mi" in this directory.
28#
29# A few *.mk files are helper files for other tests (such as include-sub.mk)
30# and are thus not added to TESTS.  Such files must be ignored in
31# src/tests/usr.bin/make/t_make.sh.
32#
33
34# Each test is in a sub-makefile.
35# Keep the list sorted.
36# Any test that is commented out must be ignored in
37# src/tests/usr.bin/make/t_make.sh as well.
38TESTS+=		archive
39TESTS+=		archive-suffix
40TESTS+=		cmd-interrupt
41TESTS+=		cmdline
42TESTS+=		comment
43TESTS+=		cond-cmp-numeric
44TESTS+=		cond-cmp-numeric-eq
45TESTS+=		cond-cmp-numeric-ge
46TESTS+=		cond-cmp-numeric-gt
47TESTS+=		cond-cmp-numeric-le
48TESTS+=		cond-cmp-numeric-lt
49TESTS+=		cond-cmp-numeric-ne
50TESTS+=		cond-cmp-string
51TESTS+=		cond-cmp-unary
52TESTS+=		cond-func
53TESTS+=		cond-func-commands
54TESTS+=		cond-func-defined
55TESTS+=		cond-func-empty
56TESTS+=		cond-func-exists
57TESTS+=		cond-func-make
58TESTS+=		cond-func-target
59TESTS+=		cond-late
60TESTS+=		cond-op
61TESTS+=		cond-op-and
62TESTS+=		cond-op-not
63TESTS+=		cond-op-or
64TESTS+=		cond-op-parentheses
65TESTS+=		cond-short
66TESTS+=		cond-token-number
67TESTS+=		cond-token-plain
68TESTS+=		cond-token-string
69TESTS+=		cond-token-var
70TESTS+=		cond-undef-lint
71TESTS+=		cond1
72TESTS+=		cond2
73TESTS+=		counter
74TESTS+=		counter-append
75TESTS+=		dep
76TESTS+=		dep-colon
77TESTS+=		dep-double-colon
78TESTS+=		dep-exclam
79TESTS+=		dep-none
80TESTS+=		dep-var
81TESTS+=		dep-wildcards
82TESTS+=		depsrc
83TESTS+=		depsrc-exec
84TESTS+=		depsrc-ignore
85TESTS+=		depsrc-made
86TESTS+=		depsrc-make
87TESTS+=		depsrc-meta
88TESTS+=		depsrc-nometa
89TESTS+=		depsrc-nometa_cmp
90TESTS+=		depsrc-nopath
91TESTS+=		depsrc-notmain
92TESTS+=		depsrc-optional
93TESTS+=		depsrc-phony
94TESTS+=		depsrc-precious
95TESTS+=		depsrc-recursive
96TESTS+=		depsrc-silent
97TESTS+=		depsrc-use
98TESTS+=		depsrc-usebefore
99TESTS+=		depsrc-usebefore-double-colon
100TESTS+=		depsrc-wait
101TESTS+=		deptgt
102TESTS+=		deptgt-begin
103TESTS+=		deptgt-default
104TESTS+=		deptgt-delete_on_error
105TESTS+=		deptgt-end
106TESTS+=		deptgt-end-jobs
107TESTS+=		deptgt-error
108TESTS+=		deptgt-ignore
109TESTS+=		deptgt-interrupt
110TESTS+=		deptgt-main
111TESTS+=		deptgt-makeflags
112TESTS+=		deptgt-no_parallel
113TESTS+=		deptgt-nopath
114TESTS+=		deptgt-notparallel
115TESTS+=		deptgt-objdir
116TESTS+=		deptgt-order
117TESTS+=		deptgt-path
118TESTS+=		deptgt-path-suffix
119TESTS+=		deptgt-phony
120TESTS+=		deptgt-precious
121TESTS+=		deptgt-shell
122TESTS+=		deptgt-silent
123TESTS+=		deptgt-stale
124TESTS+=		deptgt-suffixes
125TESTS+=		dir
126TESTS+=		dir-expand-path
127TESTS+=		directive
128TESTS+=		directive-dinclude
129TESTS+=		directive-elif
130TESTS+=		directive-elifdef
131TESTS+=		directive-elifmake
132TESTS+=		directive-elifndef
133TESTS+=		directive-elifnmake
134TESTS+=		directive-else
135TESTS+=		directive-endif
136TESTS+=		directive-error
137TESTS+=		directive-export
138TESTS+=		directive-export-env
139TESTS+=		directive-export-literal
140TESTS+=		directive-for
141TESTS+=		directive-for-generating-endif
142TESTS+=		directive-hyphen-include
143TESTS+=		directive-if
144TESTS+=		directive-ifdef
145TESTS+=		directive-ifmake
146TESTS+=		directive-ifndef
147TESTS+=		directive-ifnmake
148TESTS+=		directive-include
149TESTS+=		directive-include-fatal
150TESTS+=		directive-info
151TESTS+=		directive-sinclude
152TESTS+=		directive-undef
153TESTS+=		directive-unexport
154TESTS+=		directive-unexport-env
155TESTS+=		directive-warning
156TESTS+=		directives
157TESTS+=		dollar
158TESTS+=		doterror
159TESTS+=		dotwait
160TESTS+=		envfirst
161TESTS+=		error
162TESTS+=		# escape	# broken by reverting POSIX changes
163TESTS+=		export
164TESTS+=		export-all
165TESTS+=		export-env
166TESTS+=		export-variants
167TESTS+=		forloop
168TESTS+=		forsubst
169TESTS+=		impsrc
170TESTS+=		include-main
171TESTS+=		lint
172TESTS+=		make-exported
173TESTS+=		misc
174TESTS+=		moderrs
175TESTS+=		modmatch
176TESTS+=		modmisc
177TESTS+=		modts
178TESTS+=		modword
179TESTS+=		opt
180TESTS+=		opt-backwards
181TESTS+=		opt-chdir
182TESTS+=		opt-debug
183TESTS+=		opt-debug-all
184TESTS+=		opt-debug-archive
185TESTS+=		opt-debug-curdir
186TESTS+=		opt-debug-cond
187TESTS+=		opt-debug-dir
188TESTS+=		opt-debug-errors
189TESTS+=		opt-debug-file
190TESTS+=		opt-debug-for
191TESTS+=		opt-debug-graph1
192TESTS+=		opt-debug-graph2
193TESTS+=		opt-debug-graph3
194TESTS+=		opt-debug-hash
195TESTS+=		opt-debug-jobs
196TESTS+=		opt-debug-lint
197TESTS+=		opt-debug-loud
198TESTS+=		opt-debug-meta
199TESTS+=		opt-debug-making
200TESTS+=		opt-debug-no-rm
201TESTS+=		opt-debug-parse
202TESTS+=		opt-debug-suff
203TESTS+=		opt-debug-targets
204TESTS+=		opt-debug-varraw
205TESTS+=		opt-debug-var
206TESTS+=		opt-debug-x-trace
207TESTS+=		opt-define
208TESTS+=		opt-env
209TESTS+=		opt-file
210TESTS+=		opt-ignore
211TESTS+=		opt-include-dir
212TESTS+=		opt-jobs
213TESTS+=		opt-jobs-internal
214TESTS+=		opt-keep-going
215TESTS+=		opt-m-include-dir
216TESTS+=		opt-no-action
217TESTS+=		opt-no-action-at-all
218TESTS+=		opt-query
219TESTS+=		opt-raw
220TESTS+=		opt-silent
221TESTS+=		opt-touch
222TESTS+=		opt-tracefile
223TESTS+=		opt-var-expanded
224TESTS+=		opt-var-literal
225TESTS+=		opt-warnings-as-errors
226TESTS+=		opt-where-am-i
227TESTS+=		opt-x-reduce-exported
228TESTS+=		order
229TESTS+=		phony-end
230TESTS+=		posix
231TESTS+=		# posix1	# broken by reverting POSIX changes
232TESTS+=		qequals
233TESTS+=		recursive
234TESTS+=		sh
235TESTS+=		sh-dots
236TESTS+=		sh-jobs
237TESTS+=		sh-jobs-error
238TESTS+=		sh-leading-at
239TESTS+=		sh-leading-hyphen
240TESTS+=		sh-leading-plus
241TESTS+=		sh-meta-chars
242TESTS+=		sh-multi-line
243TESTS+=		sh-single-line
244TESTS+=		# suffixes	# runs into an endless loop (try -dA)
245TESTS+=		sunshcmd
246TESTS+=		sysv
247TESTS+=		ternary
248TESTS+=		unexport
249TESTS+=		unexport-env
250TESTS+=		use-inference
251TESTS+=		var-class
252TESTS+=		var-class-cmdline
253TESTS+=		var-class-env
254TESTS+=		var-class-global
255TESTS+=		var-class-local
256TESTS+=		var-class-local-legacy
257TESTS+=		var-op
258TESTS+=		var-op-append
259TESTS+=		var-op-assign
260TESTS+=		var-op-default
261TESTS+=		var-op-expand
262TESTS+=		var-op-shell
263TESTS+=		varcmd
264TESTS+=		vardebug
265TESTS+=		varfind
266TESTS+=		varmisc
267TESTS+=		varmod
268TESTS+=		varmod-assign
269TESTS+=		varmod-defined
270TESTS+=		varmod-edge
271TESTS+=		varmod-exclam-shell
272TESTS+=		varmod-extension
273TESTS+=		varmod-gmtime
274TESTS+=		varmod-hash
275TESTS+=		varmod-head
276TESTS+=		varmod-ifelse
277TESTS+=		varmod-l-name-to-value
278TESTS+=		varmod-localtime
279TESTS+=		varmod-loop
280TESTS+=		varmod-match
281TESTS+=		varmod-match-escape
282TESTS+=		varmod-no-match
283TESTS+=		varmod-order
284TESTS+=		varmod-order-reverse
285TESTS+=		varmod-order-shuffle
286TESTS+=		varmod-path
287TESTS+=		varmod-quote
288TESTS+=		varmod-quote-dollar
289TESTS+=		varmod-range
290TESTS+=		varmod-remember
291TESTS+=		varmod-root
292TESTS+=		varmod-select-words
293TESTS+=		varmod-shell
294TESTS+=		varmod-subst
295TESTS+=		varmod-subst-regex
296TESTS+=		varmod-sysv
297TESTS+=		varmod-tail
298TESTS+=		varmod-to-abs
299TESTS+=		varmod-to-lower
300TESTS+=		varmod-to-many-words
301TESTS+=		varmod-to-one-word
302TESTS+=		varmod-to-separator
303TESTS+=		varmod-to-upper
304TESTS+=		varmod-undefined
305TESTS+=		varmod-unique
306TESTS+=		varname
307TESTS+=		varname-dollar
308TESTS+=		varname-dot-alltargets
309TESTS+=		varname-dot-curdir
310TESTS+=		varname-dot-includes
311TESTS+=		varname-dot-includedfromdir
312TESTS+=		varname-dot-includedfromfile
313TESTS+=		varname-dot-libs
314TESTS+=		varname-dot-make-dependfile
315TESTS+=		varname-dot-make-expand_variables
316TESTS+=		varname-dot-make-exported
317TESTS+=		varname-dot-make-jobs
318TESTS+=		varname-dot-make-jobs-prefix
319TESTS+=		varname-dot-make-level
320TESTS+=		varname-dot-make-makefile_preference
321TESTS+=		varname-dot-make-makefiles
322TESTS+=		varname-dot-make-meta-bailiwick
323TESTS+=		varname-dot-make-meta-created
324TESTS+=		varname-dot-make-meta-files
325TESTS+=		varname-dot-make-meta-ignore_filter
326TESTS+=		varname-dot-make-meta-ignore_paths
327TESTS+=		varname-dot-make-meta-ignore_patterns
328TESTS+=		varname-dot-make-meta-prefix
329TESTS+=		varname-dot-make-mode
330TESTS+=		varname-dot-make-path_filemon
331TESTS+=		varname-dot-make-pid
332TESTS+=		varname-dot-make-ppid
333TESTS+=		varname-dot-make-save_dollars
334TESTS+=		varname-dot-makeoverrides
335TESTS+=		varname-dot-newline
336TESTS+=		varname-dot-objdir
337TESTS+=		varname-dot-parsedir
338TESTS+=		varname-dot-parsefile
339TESTS+=		varname-dot-path
340TESTS+=		varname-dot-shell
341TESTS+=		varname-dot-targets
342TESTS+=		varname-empty
343TESTS+=		varname-make
344TESTS+=		varname-make_print_var_on_error
345TESTS+=		varname-makefile
346TESTS+=		varname-makeflags
347TESTS+=		varname-pwd
348TESTS+=		varname-vpath
349TESTS+=		varparse-dynamic
350TESTS+=		varparse-undef-partial
351TESTS+=		varquote
352TESTS+=		varshell
353
354# Additional environment variables for some of the tests.
355# The base environment is -i PATH="$PATH".
356ENV.envfirst=		FROM_ENV=value-from-env
357ENV.varmisc=		FROM_ENV=env
358ENV.varmisc+=		FROM_ENV_BEFORE=env
359ENV.varmisc+=		FROM_ENV_AFTER=env
360
361# Override make flags for some of the tests; default is -k.
362# If possible, write ".MAKEFLAGS: -dv" in the test .mk file instead of
363# settings FLAGS.test=-dv here, since that is closer to the test code.
364FLAGS.directive-ifmake=	first second
365FLAGS.doterror=		# none
366FLAGS.envfirst=		-e
367FLAGS.export=		# none
368FLAGS.opt-ignore=	-i
369FLAGS.opt-keep-going=	-k
370FLAGS.opt-no-action=	-n
371FLAGS.opt-query=	-q
372FLAGS.opt-var-expanded=	-v VAR -v VALUE
373FLAGS.opt-var-literal=	-V VAR -V VALUE
374FLAGS.opt-warnings-as-errors= -W
375FLAGS.order=		-j1
376FLAGS.recursive=	-dL
377FLAGS.sh-leading-plus=	-n
378FLAGS.vardebug=		-k -dv FROM_CMDLINE=
379FLAGS.varmod-match-escape= -dv
380FLAGS.varname-dot-shell= -dpv
381FLAGS.varname-empty=	-dv '$${:U}=cmdline-u' '=cmline-plain'
382
383# Some tests need extra postprocessing.
384SED_CMDS.opt-debug-graph1= \
385			-e 's,${.CURDIR},CURDIR,'
386SED_CMDS.opt-debug-graph1+= \
387			-e '/Global Variables:/,/Suffixes:/d'
388SED_CMDS.sh-dots=	-e 's,^.*\.\.\.:.*,<normalized: ...: not found>,'
389SED_CMDS.varmod-subst-regex+= \
390			-e 's,\(Regex compilation error:\).*,\1 (details omitted),'
391SED_CMDS.varmod-edge+=	-e 's, line [0-9]*:, line omitted:,'
392SED_CMDS.varshell+=	-e 's,^${.SHELL:T}: ,,'
393SED_CMDS.varshell+=	-e '/command/s,No such.*,not found,'
394SED_CMDS.varname-dot-parsedir=	-e '/in some cases/ s,^make: "[^"]*,make: "<normalized>,'
395SED_CMDS.varname-dot-parsefile=	-e '/in some cases/ s,^make: "[^"]*,make: "<normalized>,'
396SED_CMDS.varname-dot-shell=	-e 's, = /.*, = (details omitted),'
397SED_CMDS.varname-dot-shell+=	-e 's,"/[^"]*","(details omitted)",'
398SED_CMDS.varname-dot-shell+=	-e 's,\[/[^]]*\],[(details omitted)],'
399
400# Some tests need an additional round of postprocessing.
401POSTPROC.deptgt-suffixes= \
402			${TOOL_SED} -n -e '/^\#\*\*\* Suffixes/,/^\#\*/p'
403POSTPROC.vardebug=	${TOOL_SED} -n -e '/:RELEVANT = yes/,/:RELEVANT = no/p'
404POSTPROC.varmod-match-escape= ${TOOL_SED} -n -e '/^Pattern/p'
405POSTPROC.varname=	${TOOL_SED} -n -e '/^MAGIC/p' -e '/^ORDER_/p'
406POSTPROC.varname-dot-shell= \
407			awk '/\.SHELL/ || /^ParseReadLine/'
408POSTPROC.varname-empty=	${TOOL_SED} -n -e '/^Var_Set/p' -e '/^out:/p'
409
410# Some tests reuse other tests, which makes them unnecessarily fragile.
411export-all.rawout: export.mk
412unexport.rawout: export.mk
413unexport-env.rawout: export.mk
414
415# End of the configuration section.
416
417.MAIN: all
418
419UNIT_TESTS:=	${.PARSEDIR}
420.PATH: ${UNIT_TESTS}
421
422.if ${USE_ABSOLUTE_TESTNAMES:Uno} == yes
423OUTFILES=	${TESTS:@test@${.CURDIR:tA}/${test}.out@}
424.else
425OUTFILES=	${TESTS:=.out}
426.endif
427
428all: ${OUTFILES}
429
430CLEANFILES=		*.rawout *.out *.status *.tmp *.core *.tmp
431CLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
432CLEANFILES+=		issue* .[ab]*		# suffixes.mk
433CLEANDIRS=		dir dummy		# posix1.mk
434
435clean:
436	rm -f ${CLEANFILES}
437	rm -rf ${CLEANDIRS}
438
439TEST_MAKE?=	${.MAKE}
440TOOL_SED?=	sed
441
442# ensure consistent results from sort(1)
443LC_ALL=		C
444LANG=		C
445.export LANG LC_ALL
446
447# Each test is run in a sub-make, to keep the tests for interfering with
448# each other, and because they use different environment variables and
449# command line options.
450.SUFFIXES: .mk .rawout .out
451.mk.rawout:
452	@${_MKMSG_TEST:Uecho '#      test '} ${.PREFIX}
453	@set -eu; \
454	cd ${.OBJDIR}; \
455	env -i PATH="$$PATH" ${ENV.${.PREFIX:T}} \
456	  ${TEST_MAKE} \
457	    -r -C ${.CURDIR} -f ${.IMPSRC} \
458	    ${FLAGS.${.PREFIX:T}:U-k} \
459	    > ${.TARGET}.tmp 2>&1 \
460	&& status=$$? || status=$$?; \
461	echo $$status > ${.TARGET:R}.status
462	@mv ${.TARGET}.tmp ${.TARGET}
463
464# Postprocess the test output so that the results can be compared.
465#
466# always pretend .MAKE was called 'make'
467_SED_CMDS+=	-e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,'
468_SED_CMDS+=	-e 's,${TEST_MAKE:S,.,\\.,g},make,'
469# replace anything after 'stopped in' with unit-tests
470_SED_CMDS+=	-e '/stopped/s, /.*, unit-tests,'
471# strip ${.CURDIR}/ from the output
472_SED_CMDS+=	-e 's,${.CURDIR:S,.,\\.,g}/,,g'
473_SED_CMDS+=	-e 's,${UNIT_TESTS:S,.,\\.,g}/,,g'
474
475.rawout.out:
476	@${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.PREFIX:T}} \
477	  < ${.IMPSRC} > ${.TARGET}.tmp1
478	@${POSTPROC.${.PREFIX:T}:Ucat} < ${.TARGET}.tmp1 > ${.TARGET}.tmp2
479	@rm ${.TARGET}.tmp1
480	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp2
481	@mv ${.TARGET}.tmp2 ${.TARGET}
482
483# Compare all output files
484test:	${OUTFILES} .PHONY
485	@failed= ; \
486	for test in ${TESTS}; do \
487	  diff -u ${UNIT_TESTS}/$${test}.exp $${test}.out \
488	  || failed="$${failed}$${failed:+ }$${test}" ; \
489	done ; \
490	if [ -n "$${failed}" ]; then \
491	  echo "Failed tests: $${failed}" ; false ; \
492	else \
493	  echo "All tests passed" ; \
494	fi
495
496accept:
497	@for test in ${TESTS}; do \
498	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
499	  || { echo "Replacing $${test}.exp" ; \
500	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
501	done
502
503# Note: only works for adding tests.
504# To remove a test, the $$mi file must be edited manually.
505sync-mi:
506	@set -eu;							\
507	cd "${MAKEFILE:tA:H}/../../..";					\
508	mi="distrib/sets/lists/tests/mi";				\
509	cvs update "$$mi";						\
510	testsdir="usr.bin/make/unit-tests";				\
511	fmt="./usr/tests/$$testsdir/%s\ttests-usr.bin-tests\tcompattestfile,atf\\n"; \
512	(cd "$$testsdir" && ls *.exp *.mk) | xargs printf "$$fmt" >> "$$mi"; \
513	distrib/sets/fmt-list "$$mi";					\
514	cvs diff "$$mi" || true
515
516.if exists(${TEST_MAKE})
517${TESTS:=.rawout}: ${TEST_MAKE} ${.PARSEDIR}/Makefile
518.endif
519
520.-include <bsd.obj.mk>
521