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