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