xref: /freebsd/contrib/bmake/unit-tests/Makefile (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1# $Id: Makefile,v 1.230 2025/01/30 18:04:20 sjg Exp $
2#
3# $NetBSD: Makefile,v 1.355 2025/01/25 22:46:30 sjg Exp $
4#
5# Unit tests for make(1)
6#
7# The main targets are:
8#
9# all:
10#	run all the tests
11# test:
12#	run 'all', and compare to expected results
13# accept:
14#	move generated output to expected results
15#
16# Settable variables
17#
18# TEST_MAKE
19#	The make program to be tested.
20#
21#
22# Adding a test case
23#
24# Each feature should get its own set of tests in its own suitably
25# named makefile (*.mk), with its own set of expected results (*.exp),
26# and it should be added to the TESTS list.
27#
28
29.MAIN: all
30
31# we use these below but we might be an older make
32.MAKE.OS?= ${uname -s:L:sh}
33.MAKE.UID?= ${id -u:L:sh}
34
35# for many tests we need a TMPDIR that will not collide
36# with other users.
37.if ${.OBJDIR} != ${.CURDIR}
38# easy
39TMPDIR:=	${.OBJDIR}/tmp
40.elif defined(TMPDIR)
41TMPDIR:=	${TMPDIR}/uid${.MAKE.UID}
42.else
43TMPDIR:=	/tmp/uid${.MAKE.UID}
44.endif
45# make sure it exists
46.if !exist(${TMPDIR})
47_!= mkdir -p ${TMPDIR}
48.endif
49# and clean it up - outside the context of
50# any target that might be using it.
51.END: rm-tmpdir
52rm-tmpdir:	.NOMETA
53	@rm -rf ${TMPDIR}
54
55# Each test is in a sub-makefile.
56# Keep the list sorted.
57# Any test that is commented out must be ignored in
58# src/tests/usr.bin/make/t_make.sh as well.
59#TESTS+=		archive
60#TESTS+=		archive-suffix
61TESTS+=		cmd-errors
62TESTS+=		cmd-errors-jobs
63TESTS+=		cmd-errors-lint
64TESTS+=		cmd-interrupt
65TESTS+=		cmdline
66TESTS+=		cmdline-redirect-stdin
67TESTS+=		cmdline-undefined
68TESTS+=		comment
69TESTS+=		compat-error
70TESTS+=		cond-cmp-numeric
71TESTS+=		cond-cmp-numeric-eq
72TESTS+=		cond-cmp-numeric-ge
73TESTS+=		cond-cmp-numeric-gt
74TESTS+=		cond-cmp-numeric-le
75TESTS+=		cond-cmp-numeric-lt
76TESTS+=		cond-cmp-numeric-ne
77TESTS+=		cond-cmp-string
78TESTS+=		cond-cmp-unary
79TESTS+=		cond-eof
80TESTS+=		cond-func
81TESTS+=		cond-func-commands
82TESTS+=		cond-func-defined
83TESTS+=		cond-func-empty
84TESTS+=		cond-func-exists
85TESTS+=		cond-func-make
86TESTS+=		cond-func-make-main
87TESTS+=		cond-func-target
88TESTS+=		cond-late
89TESTS+=		cond-op
90TESTS+=		cond-op-and
91TESTS+=		cond-op-and-lint
92TESTS+=		cond-op-not
93TESTS+=		cond-op-or
94TESTS+=		cond-op-or-lint
95TESTS+=		cond-op-parentheses
96TESTS+=		cond-short
97TESTS+=		cond-token-number
98TESTS+=		cond-token-plain
99TESTS+=		cond-token-string
100TESTS+=		cond-token-var
101TESTS+=		cond-undef-lint
102TESTS+=		counter
103TESTS+=		counter-append
104TESTS+=		dep
105TESTS+=		dep-colon
106TESTS+=		dep-colon-bug-cross-file
107TESTS+=		dep-double-colon
108TESTS+=		dep-double-colon-indep
109TESTS+=		dep-duplicate
110TESTS+=		dep-exclam
111TESTS+=		dep-none
112TESTS+=		dep-op-missing
113TESTS+=		dep-percent
114TESTS+=		dep-var
115TESTS+=		dep-wildcards
116TESTS+=		depsrc
117TESTS+=		depsrc-end
118TESTS+=		depsrc-exec
119TESTS+=		depsrc-ignore
120TESTS+=		depsrc-made
121TESTS+=		depsrc-make
122TESTS+=		depsrc-meta
123TESTS+=		depsrc-nometa
124TESTS+=		depsrc-nometa_cmp
125TESTS+=		depsrc-nopath
126TESTS+=		depsrc-notmain
127TESTS+=		depsrc-optional
128TESTS+=		depsrc-phony
129TESTS+=		depsrc-precious
130TESTS+=		depsrc-recursive
131TESTS+=		depsrc-silent
132TESTS+=		depsrc-use
133TESTS+=		depsrc-usebefore
134TESTS+=		depsrc-usebefore-double-colon
135TESTS+=		depsrc-wait
136TESTS+=		deptgt
137TESTS+=		deptgt-begin
138TESTS+=		deptgt-begin-fail
139TESTS+=		deptgt-begin-fail-indirect
140TESTS+=		deptgt-default
141TESTS+=		deptgt-delete_on_error
142TESTS+=		deptgt-end
143TESTS+=		deptgt-end-fail
144TESTS+=		deptgt-end-fail-all
145TESTS+=		deptgt-end-fail-indirect
146TESTS+=		deptgt-end-jobs
147TESTS+=		deptgt-error
148TESTS+=		deptgt-ignore
149TESTS+=		deptgt-interrupt
150TESTS+=		deptgt-main
151TESTS+=		deptgt-makeflags
152TESTS+=		deptgt-no_parallel
153TESTS+=		deptgt-nopath
154TESTS+=		deptgt-notparallel
155TESTS+=		deptgt-objdir
156TESTS+=		deptgt-order
157TESTS+=		deptgt-path
158TESTS+=		deptgt-path-suffix
159TESTS+=		deptgt-phony
160TESTS+=		deptgt-posix
161TESTS+=		deptgt-precious
162TESTS+=		deptgt-shell
163TESTS+=		deptgt-silent
164TESTS+=		deptgt-silent-jobs
165TESTS+=		deptgt-stale
166TESTS+=		deptgt-suffixes
167TESTS+=		dir
168TESTS+=		dir-expand-path
169TESTS+=		directive
170TESTS+=		directive-dinclude
171TESTS+=		directive-elif
172TESTS+=		directive-elifdef
173TESTS+=		directive-elifmake
174TESTS+=		directive-elifndef
175TESTS+=		directive-elifnmake
176TESTS+=		directive-else
177TESTS+=		directive-endfor
178TESTS+=		directive-endif
179TESTS+=		directive-error
180TESTS+=		directive-export
181TESTS+=		directive-export-env
182TESTS+=		directive-export-impl
183TESTS+=		directive-export-gmake
184TESTS+=		directive-export-literal
185TESTS+=		directive-for
186TESTS+=		directive-for-break
187TESTS+=		directive-for-empty
188TESTS+=		directive-for-errors
189TESTS+=		directive-for-escape
190TESTS+=		directive-for-generating-endif
191TESTS+=		directive-for-if
192TESTS+=		directive-for-lines
193TESTS+=		directive-for-null
194TESTS+=		directive-hyphen-include
195TESTS+=		directive-if
196TESTS+=		directive-if-nested
197TESTS+=		directive-ifdef
198TESTS+=		directive-ifmake
199TESTS+=		directive-ifndef
200TESTS+=		directive-ifnmake
201TESTS+=		directive-include
202TESTS+=		directive-include-fatal
203TESTS+=		directive-include-guard
204TESTS+=		directive-info
205TESTS+=		directive-misspellings
206TESTS+=		directive-sinclude
207TESTS+=		directive-undef
208TESTS+=		directive-unexport
209TESTS+=		directive-unexport-env
210TESTS+=		directive-warning
211TESTS+=		dollar
212TESTS+=		doterror
213TESTS+=		dotwait
214TESTS+=		error
215TESTS+=		# escape	# broken by reverting POSIX changes
216TESTS+=		export
217TESTS+=		export-all
218TESTS+=		export-env
219TESTS+=		export-variants
220TESTS+=		gnode-submake
221TESTS+=		hanoi-include
222TESTS+=		impsrc
223TESTS+=		include-main
224TESTS+=		job-flags
225TESTS+=		job-output-long-lines
226TESTS+=		job-output-null
227TESTS+=		jobs-empty-commands
228TESTS+=		jobs-empty-commands-error
229TESTS+=		jobs-error-indirect
230TESTS+=		jobs-error-nested
231TESTS+=		jobs-error-nested-make
232TESTS+=		lint
233TESTS+=		make-exported
234TESTS+=		meta-cmd-cmp
235TESTS+=		moderrs
236TESTS+=		modmisc
237.if ${.MAKE.UID} > 0
238TESTS+=		objdir-writable
239.endif
240TESTS+=		opt
241TESTS+=		opt-backwards
242TESTS+=		opt-chdir
243TESTS+=		opt-debug
244TESTS+=		opt-debug-all
245TESTS+=		opt-debug-archive
246TESTS+=		opt-debug-curdir
247TESTS+=		opt-debug-cond
248TESTS+=		opt-debug-dir
249TESTS+=		opt-debug-errors
250TESTS+=		opt-debug-errors-jobs
251TESTS+=		opt-debug-file
252TESTS+=		opt-debug-for
253TESTS+=		opt-debug-graph1
254TESTS+=		opt-debug-graph2
255TESTS+=		opt-debug-graph3
256TESTS+=		opt-debug-hash
257TESTS+=		opt-debug-jobs
258TESTS+=		opt-debug-lint
259TESTS+=		opt-debug-loud
260TESTS+=		opt-debug-meta
261TESTS+=		opt-debug-making
262TESTS+=		opt-debug-no-rm
263TESTS+=		opt-debug-parse
264TESTS+=		opt-debug-suff
265TESTS+=		opt-debug-targets
266TESTS+=		opt-debug-varraw
267TESTS+=		opt-debug-var
268TESTS+=		opt-debug-x-trace
269TESTS+=		opt-define
270TESTS+=		opt-env
271TESTS+=		opt-file
272TESTS+=		opt-ignore
273TESTS+=		opt-include-dir
274TESTS+=		opt-jobs
275TESTS+=		opt-jobs-internal
276TESTS+=		opt-jobs-no-action
277TESTS+=		opt-keep-going
278TESTS+=		opt-keep-going-indirect
279TESTS+=		opt-keep-going-multiple
280TESTS+=		opt-m-include-dir
281TESTS+=		opt-no-action
282TESTS+=		opt-no-action-at-all
283TESTS+=		opt-no-action-runflags
284TESTS+=		opt-no-action-touch
285TESTS+=		opt-query
286TESTS+=		opt-raw
287TESTS+=		opt-silent
288TESTS+=		opt-touch
289TESTS+=		opt-touch-jobs
290TESTS+=		opt-tracefile
291TESTS+=		opt-var-expanded
292TESTS+=		opt-var-literal
293TESTS+=		opt-version
294TESTS+=		opt-warnings-as-errors
295TESTS+=		opt-where-am-i
296TESTS+=		opt-x-reduce-exported
297TESTS+=		order
298TESTS+=		parse
299TESTS+=		parse-var
300TESTS+=		phony-end
301TESTS+=		posix
302TESTS+=		# posix1	# broken by reverting POSIX changes
303TESTS+=		recursive
304TESTS+=		sh
305TESTS+=		sh-dots
306TESTS+=		sh-errctl
307TESTS+=		sh-flags
308TESTS+=		sh-jobs
309TESTS+=		sh-jobs-error
310TESTS+=		sh-leading-at
311TESTS+=		sh-leading-hyphen
312TESTS+=		sh-leading-plus
313TESTS+=		sh-meta-chars
314TESTS+=		sh-multi-line
315TESTS+=		sh-single-line
316TESTS+=		shell-csh
317TESTS+=		shell-custom
318.if exists(/bin/ksh)
319TESTS+=		shell-ksh
320.endif
321TESTS+=		shell-sh
322TESTS+=		suff
323TESTS+=		suff-add-later
324TESTS+=		suff-clear-regular
325TESTS+=		suff-clear-single
326TESTS+=		suff-incomplete
327TESTS+=		suff-lookup
328TESTS+=		suff-main
329TESTS+=		suff-main-several
330TESTS+=		suff-phony
331TESTS+=		suff-rebuild
332TESTS+=		suff-self
333TESTS+=		suff-transform-debug
334TESTS+=		suff-transform-endless
335TESTS+=		suff-transform-expand
336TESTS+=		suff-transform-select
337TESTS+=		suff-use
338TESTS+=		sunshcmd
339TESTS+=		ternary
340TESTS+=		unexport
341TESTS+=		unexport-env
342TESTS+=		use-inference
343TESTS+=		var-readonly
344TESTS+=		var-scope
345TESTS+=		var-scope-cmdline
346TESTS+=		var-scope-env
347TESTS+=		var-scope-global
348TESTS+=		var-scope-local
349TESTS+=		var-scope-local-legacy
350TESTS+=		var-eval-short
351TESTS+=		var-op
352TESTS+=		var-op-append
353TESTS+=		var-op-assign
354TESTS+=		var-op-default
355TESTS+=		var-op-expand
356TESTS+=		var-op-shell
357TESTS+=		var-op-sunsh
358TESTS+=		var-recursive
359TESTS+=		varcmd
360TESTS+=		vardebug
361TESTS+=		varfind
362TESTS+=		varmisc
363TESTS+=		varmod
364TESTS+=		varmod-assign
365TESTS+=		varmod-assign-shell
366TESTS+=		varmod-defined
367TESTS+=		varmod-edge
368TESTS+=		varmod-exclam-shell
369TESTS+=		varmod-extension
370TESTS+=		varmod-gmtime
371TESTS+=		varmod-hash
372TESTS+=		varmod-head
373TESTS+=		varmod-ifelse
374TESTS+=		varmod-indirect
375TESTS+=		varmod-l-name-to-value
376TESTS+=		varmod-localtime
377TESTS+=		varmod-loop
378TESTS+=		varmod-loop-delete
379TESTS+=		varmod-loop-varname
380TESTS+=		varmod-match
381TESTS+=		varmod-match-escape
382TESTS+=		varmod-mtime
383TESTS+=		varmod-no-match
384TESTS+=		varmod-order
385TESTS+=		varmod-order-numeric
386TESTS+=		varmod-order-reverse
387TESTS+=		varmod-order-shuffle
388TESTS+=		varmod-order-string
389TESTS+=		varmod-path
390TESTS+=		varmod-quote
391TESTS+=		varmod-quote-dollar
392TESTS+=		varmod-range
393TESTS+=		varmod-remember
394TESTS+=		varmod-root
395TESTS+=		varmod-select-words
396TESTS+=		varmod-shell
397TESTS+=		varmod-subst
398TESTS+=		varmod-subst-regex
399TESTS+=		varmod-sun-shell
400TESTS+=		varmod-sysv
401TESTS+=		varmod-tail
402TESTS+=		varmod-to-abs
403TESTS+=		varmod-to-lower
404TESTS+=		varmod-to-many-words
405TESTS+=		varmod-to-one-word
406TESTS+=		varmod-to-separator
407TESTS+=		varmod-to-title
408TESTS+=		varmod-to-upper
409TESTS+=		varmod-undefined
410TESTS+=		varmod-unique
411TESTS+=		varname
412TESTS+=		varname-dollar
413TESTS+=		varname-dot-alltargets
414TESTS+=		varname-dot-curdir
415TESTS+=		varname-dot-includes
416TESTS+=		varname-dot-includedfromdir
417TESTS+=		varname-dot-includedfromfile
418TESTS+=		varname-dot-libs
419TESTS+=		varname-dot-make-dependfile
420TESTS+=		varname-dot-make-expand_variables
421TESTS+=		varname-dot-make-exported
422TESTS+=		varname-dot-make-jobs
423TESTS+=		varname-dot-make-jobs-prefix
424TESTS+=		varname-dot-make-level
425TESTS+=		varname-dot-make-makefile_preference
426TESTS+=		varname-dot-make-makefiles
427TESTS+=		varname-dot-make-meta-bailiwick
428TESTS+=		varname-dot-make-meta-created
429TESTS+=		varname-dot-make-meta-files
430.if ${.MAKE.PATH_FILEMON:Uno:Nktrace:N/dev*} == "" && ${TMPDIR:N/tmp*:N/var/tmp*} != ""
431# these tests will not work if TMPDIR is or is a subdir of
432# /tmp or /var/tmp
433.if ${.MAKE.PATH_FILEMON:N/dev/*} != "" || exists(${.MAKE.PATH_FILEMON})
434TESTS+=		varname-dot-make-meta-ignore_filter
435TESTS+=		varname-dot-make-meta-ignore_paths
436TESTS+=		varname-dot-make-meta-ignore_patterns
437TESTS+=		varname-dot-make-path_filemon
438.else
439.warning Skipping tests that require ${.MAKE.PATH_FILEMON}
440.endif
441.endif
442TESTS+=		varname-dot-make-meta-prefix
443TESTS+=		varname-dot-make-mode
444TESTS+=		varname-dot-make-pid
445TESTS+=		varname-dot-make-ppid
446TESTS+=		varname-dot-make-save_dollars
447TESTS+=		varname-dot-makeflags
448TESTS+=		varname-dot-makeoverrides
449TESTS+=		varname-dot-newline
450TESTS+=		varname-dot-objdir
451TESTS+=		varname-dot-parsedir
452TESTS+=		varname-dot-parsefile
453TESTS+=		varname-dot-path
454TESTS+=		varname-dot-shell
455TESTS+=		varname-dot-suffixes
456TESTS+=		varname-dot-targets
457TESTS+=		varname-empty
458TESTS+=		varname-make
459TESTS+=		varname-make_print_var_on_error
460TESTS+=		varname-make_print_var_on_error-jobs
461TESTS+=		varname-makefile
462TESTS+=		varname-makeflags
463TESTS+=		varname-pwd
464TESTS+=		varname-vpath
465TESTS+=		varparse-dynamic
466TESTS+=		varparse-errors
467TESTS+=		varparse-mod
468TESTS+=		varparse-undef-partial
469
470# some shells have quirks
471_shell := ${.SHELL:tA:T}
472.if ${_shell} == "dash"
473# dash fails -x output
474BROKEN_TESTS+= opt-debug-x-trace
475.elif ${_shell:N*ksh*} == ""
476BROKEN_TESTS+= \
477	deptgt-silent-jobs \
478	job-flags \
479	job-output-long-lines \
480	opt-debug-x-trace \
481	sh-flags \
482	var-op-shell \
483
484.if ${_shell:Nmksh} == ""
485# more broken that pdksh
486BROKEN_TESTS+= \
487	opt-jobs-no-action \
488	sh-errctl \
489	sh-leading-hyphen \
490
491.endif
492.endif
493
494.if ${UTC_1:Uno} == ""
495# this will not work if UTC_1 is set empty
496BROKEN_TESTS+= varmod-localtime
497.endif
498
499.if ${.MAKE.OS:NDarwin} == ""
500BROKEN_TESTS+= shell-ksh
501.endif
502
503.if ${.MAKE.OS:NIRIX*} == ""
504BROKEN_TESTS+= \
505	cmd-interrupt \
506	deptgt-interrupt \
507	job-output-null \
508	opt-chdir \
509	opt-debug-x-trace \
510	sh-leading-hyphen \
511
512.endif
513
514.if ${.MAKE.OS} == "SCO_SV"
515BROKEN_TESTS+= \
516	opt-debug-graph[23] \
517	varmod-localtime \
518	varmod-to-separator \
519
520.if ${_shell} == "bash"
521BROKEN_TESTS+= job-output-null
522.else
523BROKEN_TESTS+= \
524	cmd-interrupt \
525	job-flags \
526
527.endif
528.endif
529
530# Some tests just do not work on some platforms or environments
531# so allow for some filtering.
532.if !empty(BROKEN_TESTS)
533.warning Skipping broken tests: ${BROKEN_TESTS:O:u}
534TESTS:= ${TESTS:${BROKEN_TESTS:S,^,N,:ts:}}
535.endif
536
537# Ideas for more tests:
538#	char-0020-space.mk
539#	char-005C-backslash.mk
540#	escape-cond-str.mk
541#	escape-cond-func-arg.mk
542#	escape-varmod.mk
543#	escape-varmod-define.mk
544#	escape-varmod-match.mk
545#	escape-varname.mk
546#	escape-varassign-varname.mk
547#	escape-varassign-varname-cmdline.mk
548#	escape-varassign-value.mk
549#	escape-varassign-value-cmdline.mk
550#	escape-dependency-source.mk
551#	escape-dependency-target.mk
552#	escape-for-varname.mk
553#	escape-for-item.mk
554#	posix-*.mk (see posix.mk and posix1.mk)
555
556# Additional environment variables for some of the tests.
557# The base environment is -i PATH="$PATH".
558ENV.depsrc-optional+=   TZ=UTC
559ENV.deptgt-phony+=	MAKESYSPATH=.
560ENV.directive-undef=	ENV_VAR=env-value
561ENV.opt-env=		FROM_ENV=value-from-env
562ENV.opt-m-include-dir=	${MAKEOBJDIR:DMAKEOBJDIR=${MAKEOBJDIR}}
563ENV.varmisc=		FROM_ENV=env
564ENV.varmisc+=		FROM_ENV_BEFORE=env
565ENV.varmisc+=		FROM_ENV_AFTER=env
566ENV.varmod-localtime+=	TZ=${UTC_1:UEurope/Berlin}
567ENV.varname-vpath+=	VPATH=varname-vpath.dir:varname-vpath.dir2
568
569# Override make flags for some of the tests; default is -k.
570# If possible, write ".MAKEFLAGS: -dv" in the test .mk file instead of
571# settings FLAGS.test=-dv here, since that is closer to the test code.
572FLAGS.cond-func-make=		via-cmdline
573FLAGS.doterror=			# none, especially not -k
574FLAGS.jobs-error-indirect=	# none, especially not -k
575FLAGS.jobs-error-nested=	# none, especially not -k
576FLAGS.jobs-error-nested-make=	# none, especially not -k
577FLAGS.varname-empty=		-dv '$${:U}=cmdline-u' '=cmdline-plain'
578
579# Some tests need extra postprocessing.
580SED_CMDS.deptgt-phony=	${STD_SED_CMDS.dd}
581SED_CMDS.dir=		${STD_SED_CMDS.dd}
582SED_CMDS.directive-include-guard= \
583	-e '/\.MAKEFLAGS/d' \
584	-e '/^Parsing .*:[1-9][0-9]*:/d' \
585	-e '/^SetFilenameVars:/d' \
586	-e '/^ParseDependency/d' \
587	-e '/^ParseEOF:/d'
588SED_CMDS.export=	-e '/^[^=_A-Za-z0-9]*=/d'
589.if ${.MAKE.OS:NCygwin} == ""
590SED_CMDS.export+=	-e '/^WINDIR=/d' -e '/^SYSTEMROOT=/d'
591.endif
592SED_CMDS.export-all=	${SED_CMDS.export}
593SED_CMDS.export-env=	${SED_CMDS.export}
594SED_CMDS.cmdline=	-e 's,uid${.MAKE.UID}/,,'
595SED_CMDS.job-output-long-lines= \
596	${:D Job separators on their own line are ok. } \
597	-e '/^--- job-[ab] ---$$/d' \
598	${:D Plain output lines are ok as well. } \
599	${:D They may come in multiples of 1024 or as 10000. } \
600	-e '/^aa*$$/d' \
601	-e '/^bb*$$/d' \
602	${:D The following lines should rather not occur since the job } \
603	${:D marker should always be at the beginning of the line. } \
604	-e '/^aa*--- job-b ---$$/d' \
605	-e '/^bb*--- job-a ---$$/d'
606SED_CMDS.opt-chdir=		-e 's,\(nonexistent\).[1-9][0-9]*,\1,' \
607	-e '/name/s,file,File,' \
608	-e 's,no such,No such,' \
609	-e 's,Filename,File name,'
610
611# meta line numbers can vary based on filemon implementation
612SED_CMDS.meta-ignore= -e 's,\(\.meta:\) [1-9][0-9]*:,\1 <line>:,'
613SED_CMDS.opt-debug-graph1=	${STD_SED_CMDS.dg1}
614SED_CMDS.opt-debug-graph2=	${STD_SED_CMDS.dg2}
615SED_CMDS.opt-debug-graph3=	${STD_SED_CMDS.dg3}
616SED_CMDS.opt-debug-hash=	-e 's,\(entries\)=[1-9][0-9],\1=<entries>,'
617SED_CMDS.opt-debug-jobs=	-e 's,([0-9][0-9]*),(<pid>),'
618SED_CMDS.opt-debug-jobs+=	-e 's,pid [0-9][0-9]*,pid <pid>,'
619SED_CMDS.opt-debug-jobs+=	-e 's,Process [0-9][0-9]*,Process <pid>,'
620SED_CMDS.opt-debug-jobs+=	-e 's,JobFinish: [0-9][0-9]*,JobFinish: <pid>,'
621SED_CMDS.opt-debug-jobs+=	-e 's,Command: ${.SHELL:T},Command: <shell>,'
622# The "-q" may be there or not, see jobs.c, variable shells.
623SED_CMDS.opt-debug-jobs+=	-e 's,^\(.Command: <shell>\) -q,\1,'
624SED_CMDS.opt-debug-lint+=	${STD_SED_CMDS.regex}
625SED_CMDS.opt-jobs-no-action=	${STD_SED_CMDS.hide-from-output}
626SED_CMDS.opt-no-action-runflags= ${STD_SED_CMDS.hide-from-output}
627SED_CMDS.opt-where-am-i=	-e '/usr.obj/d'
628# For Compat_RunCommand, useShell == false.
629SED_CMDS.sh-dots=		-e 's,^.*\.\.\.:.*,<not found: ...>,'
630# For Compat_RunCommand, useShell == true.
631SED_CMDS.sh-dots+=		-e 's,^make: exec(\(.*\)) failed (.*)$$,<not found: \1>,'
632SED_CMDS.sh-dots+=		-e 's,^\(\*\*\* Error code \)[1-9][0-9]*,\1<nonzero>,'
633SED_CMDS.sh-errctl=		${STD_SED_CMDS.dj}
634SED_CMDS.sh-flags=		${STD_SED_CMDS.hide-from-output}
635SED_CMDS.shell-csh=		${STD_SED_CMDS.white-space}
636SED_CMDS.sh-leading-hyphen=	${STD_SED_CMDS.shell}
637SED_CMDS.suff-main+=		${STD_SED_CMDS.dg1}
638SED_CMDS.suff-main-several+=	${STD_SED_CMDS.dg1}
639SED_CMDS.suff-transform-debug+=	${STD_SED_CMDS.dg1}
640SED_CMDS.var-op-shell+=		${STD_SED_CMDS.shell}
641SED_CMDS.var-op-shell+=		-e '/command/s,No such.*,not found,'
642SED_CMDS.var-op-shell+=		${STD_SED_CMDS.white-space}
643SED_CMDS.vardebug+=		-e 's,${.SHELL},</path/to/shell>,'
644SED_CMDS.varmod-mtime+=		-e "s,\(.*\)': .*,\1': <ENOENT>,"
645SED_CMDS.varmod-subst-regex+=	${STD_SED_CMDS.regex}
646SED_CMDS.varparse-errors+=	${STD_SED_CMDS.timestamp}
647SED_CMDS.varname-dot-make-meta-ignore_filter+= ${SED_CMDS.meta-ignore}
648SED_CMDS.varname-dot-make-meta-ignore_paths+= ${SED_CMDS.meta-ignore}
649SED_CMDS.varname-dot-make-meta-ignore_patterns+= ${SED_CMDS.meta-ignore}
650SED_CMDS.varname-dot-parsedir=	-e '/in some cases/ s,^make: "[^"]*,make: "<normalized>,'
651SED_CMDS.varname-dot-parsefile=	-e '/in some cases/ s,^make: "[^"]*,make: "<normalized>,'
652SED_CMDS.varname-dot-shell=	-e 's, = /[^ ]*, = (details omitted),g'
653SED_CMDS.varname-dot-shell+=	-e 's,"/[^" ]*","(details omitted)",g'
654SED_CMDS.varname-dot-shell+=	-e 's,\[/[^] ]*\],[(details omitted)],g'
655SED_CMDS.varname-empty=		${.OBJDIR .PARSEDIR .PATH .SHELL .SYSPATH:L:@v@-e '/\\$v/d'@}
656
657# Some tests need an additional round of postprocessing.
658POSTPROC.depsrc-wait=		sed -e '/^---/d' -e 's,^\(: Making 3[abc]\)[123]$$,\1,'
659POSTPROC.deptgt-suffixes=	awk '/^\#\*\*\* Suffixes/,/^never-stop/'
660POSTPROC.gnode-submake=		awk '/Input graph/, /^$$/'
661POSTPROC.varname-dot-make-mode=	sed 's,^\(: Making [abc]\)[123]$$,\1,'
662
663# Some tests reuse other tests, which makes them unnecessarily fragile.
664export-all.rawout: export.mk
665unexport.rawout: export.mk
666unexport-env.rawout: export.mk
667
668# End of the configuration section.
669
670# Some standard sed commands, to be used in the SED_CMDS above.
671
672# In tests that use the debugging option -dd, ignore debugging output that is
673# only logged in -DCLEANUP mode.
674STD_SED_CMDS.dd=	-e '/^OpenDirs_Done:/d'
675STD_SED_CMDS.dd+=	-e '/^CachedDir /d'
676STD_SED_CMDS.dd+=	-e 's,  ${DEFSYSPATH:U/usr/share/mk} ,  <defsyspath> ,'
677
678# Omit details such as process IDs from the output of the -dg1 option.
679STD_SED_CMDS.dg1=	-e '/\#.* \.$$/d'
680STD_SED_CMDS.dg1+=	-e '/\.MAKE.PATH_FILEMON/d'
681STD_SED_CMDS.dg1+=	-e '/^\#.*\/mk/d'
682STD_SED_CMDS.dg1+=	-e 's,  ${DEFSYSPATH:U/usr/share/mk}$$,  <defsyspath>,'
683STD_SED_CMDS.dg1+=	-e 's,^\(\.MAKE *=\) .*,\1 <details omitted>,'
684STD_SED_CMDS.dg1+=	-e 's,^\(\.MAKE\.[A-Z_]* *=\) .*,\1 <details omitted>,'
685STD_SED_CMDS.dg1+=	-e 's,^\(\.MAKE\.JOBS\.C *=\) .*,\1 <details omitted>,'
686STD_SED_CMDS.dg1+=	-e 's,^\(MACHINE[_ARCH]* *=\) .*,\1 <details omitted>,'
687STD_SED_CMDS.dg1+=	-e 's,^\(MAKE *=\) .*,\1 <details omitted>,'
688STD_SED_CMDS.dg1+=	-e 's,^\(\.SHELL *=\) .*,\1 <details omitted>,'
689STD_SED_CMDS.dg1+=	-e '/\.SYSPATH/d'
690
691STD_SED_CMDS.dg2=	${STD_SED_CMDS.dg1}
692STD_SED_CMDS.dg2+=	-e 's,\(last modified\) ..:..:.. ... ..\, ....,\1 <timestamp>,'
693STD_SED_CMDS.dg3=	${STD_SED_CMDS.dg2}
694
695# Omit details such as process IDs from the output of the -dj option.
696STD_SED_CMDS.dj= \
697	-e '/Process/d;/JobFinish:/d' \
698	-e 's,^\(Job_TokenWithdraw\)([0-9]*),\1(<pid>),' \
699	-e 's,^([0-9][0-9]*) \(withdrew token\),(<pid>) \1,' \
700	-e 's, \(pid\) [0-9][0-9]*, \1 <pid>,' \
701	-e 's,^\(	Command:\) .*,\1 <shell>,'
702
703# Reduce the noise for tests running with the -n option, since there is no
704# other way to suppress the echoing of the commands.
705STD_SED_CMDS.hide-from-output= \
706	-e '/^echo hide-from-output/d' \
707	-e 's,hide-from-output ,,' \
708	-e 's,hide-from-output,,'
709
710# Normalize the output for error messages from the shell.
711#
712# $shell -c '...'
713#	NetBSD sh	...: not found
714#	NetBSD ksh	ksh: ...: not found
715#	bash 5.0.18	bash: ...: command not found
716#	bash 5.1.0	bash: line 1: ...: command not found
717#	dash		dash: 1: ...: not found
718#
719# $shell -c '< /nonexistent'
720#	NetBSD sh	sh: cannot open /nonexistent: no such file
721#	NetBSD ksh	ksh: cannot open /nonexistent: No such file or directory
722#	bash 5.0.18	bash: /nonexistent: No such file or directory
723#	bash 5.1.0	bash: line 1: /nonexistent: No such file or directory
724#	dash		dash: 1: cannot open /nonexistent: No such file
725#
726# echo '< /nonexistent' | $shell
727#	NetBSD sh	sh: cannot open /nonexistent: no such file
728#	NetBSD ksh	ksh: <stdin>[1]: cannot open /nonexistent: No such file or directory
729#	bash 5.0.18	bash: line 1: /nonexistent: No such file or directory
730#	bash 5.1.0	bash: line 1: /nonexistent: No such file or directory
731#	dash		dash: 1: cannot open /nonexistent: No such file
732#
733STD_SED_CMDS.shell+=		-e 's,^${.SHELL},${.SHELL:T},'
734STD_SED_CMDS.shell+=		-e 's,^${.SHELL:T}: line [0-9][0-9]*: ,,'
735STD_SED_CMDS.shell+=		-e 's,^${.SHELL:T}: [0-9][0-9]*: ,,'
736STD_SED_CMDS.shell+=		-e 's,^${.SHELL:T}: ,,'
737STD_SED_CMDS.shell+=		-e 's,: command not found,: not found,'
738
739STD_SED_CMDS.white-space=	-e 's,   *, ,g' -e 's,  *$$,,'
740
741# The actual error messages for a failed regcomp or regexec differ between the
742# implementations.
743STD_SED_CMDS.regex= \
744	-e 's,\(Regex compilation error:\).*,\1 (details omitted),'
745
746# Normalize timestamps from ':gmtime' or ':localtime' to '<timestamp>'.
747# See STD_SED_CMDS.dg2 for timestamps from the debug log.
748STD_SED_CMDS.timestamp= \
749	-e 's,[A-Z][a-z][a-z] [A-Z][a-z][a-z] [ 0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [12][0-9][0-9][0-9],<timestamp>,'
750
751# End of the configuration helpers section.
752
753.-include "Makefile.inc"
754.-include "Makefile.config"
755
756UNIT_TESTS:=	${.PARSEDIR}
757.PATH: ${UNIT_TESTS}
758
759.if ${USE_ABSOLUTE_TESTNAMES:Uno} == yes
760OUTFILES=	${TESTS:@test@${.CURDIR:tA}/${test}.out@}
761.else
762OUTFILES=	${TESTS:=.out}
763.endif
764
765all: ${OUTFILES}
766
767CLEANFILES=		*.rawout *.out *.status *.tmp *.core *.tmp
768CLEANFILES+=		obj*.[och] lib*.a	# posix1.mk
769CLEANFILES+=		issue* .[ab]*		# suffixes.mk
770CLEANDIRS=		dir dummy *.tmp		# posix1.mk
771
772clean:
773	rm -rf ${CLEANDIRS}
774	rm -f ${CLEANFILES}
775
776TEST_MAKE?=	${.MAKE}
777TOOL_SED?=	sed
778TOOL_TR?=	tr
779TOOL_DIFF?=	diff
780DIFF_FLAGS?=	-u
781
782# ensure consistent results from sort(1)
783LC_ALL=		C
784LANG=		C
785.export LANG LC_ALL
786
787.if ${.MAKE.MODE:Unormal:Mmeta} != ""
788# we don't need the noise
789_MKMSG_TEST= :
790.endif
791
792# Some Linux systems such as Fedora have deprecated egrep in favor of grep -E.
793.if ${.MAKE.OS:NLinux} == ""
794EGREP= grep -E
795.endif
796# Keep the classical definition for all other systems.  Just as the bmake code
797# is kept compatible with C90, the tests are kept compatible with systems that
798# are several decades old and don't follow modern POSIX standards.
799EGREP?= egrep
800
801MAKE_TEST_ENV=  EGREP="${EGREP}"
802MAKE_TEST_ENV+=	MALLOC_OPTIONS="JA"	# for jemalloc 100
803MAKE_TEST_ENV+=	MALLOC_CONF="junk:true"	# for jemalloc 510
804MAKE_TEST_ENV+= TMPDIR=${TMPDIR}
805
806.if ${.MAKE.OS} == "NetBSD"
807LIMIT_RESOURCES?=	ulimit -v 300000
808.endif
809LIMIT_RESOURCES?=	:
810
811# Each test is run in a sub-make, to keep the tests from interfering with
812# each other, and because they use different environment variables and
813# command line options.
814.SUFFIXES: .mk .rawout .out
815.mk.rawout:
816	@${_MKMSG_TEST:Uecho '#      test '} ${.PREFIX}
817	@set -eu; \
818	${LIMIT_RESOURCES}; \
819	cd ${.OBJDIR}; \
820	env -i PATH="$$PATH" ${MAKE_TEST_ENV} ${ENV.${.PREFIX:T}} \
821	  ${TEST_MAKE} \
822	    -r -C ${.CURDIR} -f ${.IMPSRC} \
823	    ${FLAGS.${.PREFIX:T}:U-k} \
824	    > ${.TARGET}.tmp 2>&1 \
825	&& status=$$? || status=$$?; \
826	echo $$status > ${.TARGET:R}.status
827	@mv ${.TARGET}.tmp ${.TARGET}
828
829# Postprocess the test output to make the output platform-independent.
830#
831# Replace anything after 'stopped in' with unit-tests
832_SED_CMDS+=	-e '/stopped/s, in /.*, in unit-tests,'
833# Allow the test files to be placed anywhere.
834_SED_CMDS+=	-e 's,\(\.PARSEDIR}\) = `'"/[^']*'"',\1 = <some-dir>,'
835_SED_CMDS+=	-e 's,\(\.INCLUDEDFROMDIR}\) = `'"/[^']*'"',\1 = <some-dir>,'
836_SED_CMDS+=	-e 's,${TMPDIR},<tmpdir>,g' -e 's,${TMPDIR:tA},<tmpdir>,g'
837# canonicalize ${.OBJDIR} and ${.CURDIR}
838_SED_CMDS+=	-e 's,${.CURDIR},<curdir>,g'
839.if ${.OBJDIR} != ${.CURDIR}
840# yes this is inaccurate but none of the tests expect <objdir> anywhere
841# which we get depending on how MAKEOBJDIR is set.
842_SED_CMDS+=	-e 's,${.OBJDIR},<curdir>,g' -e 's,${.OBJDIR:tA},<curdir>,g'
843.endif
844# always pretend .MAKE was called 'make'
845_SED_CMDS+=	-e 's,^${TEST_MAKE:T:S,.,\\.,g}[][0-9]*:,make:,'
846_SED_CMDS+=	-e 's,${TEST_MAKE:S,.,\\.,g},make,'
847_SED_CMDS+=	-e 's,^usage: ${TEST_MAKE:T:S,.,\\.,g} ,usage: make ,'
848_SED_CMDS+=	-e 's,${TEST_MAKE:T:S,.,\\.,g}\(\[[1-9][0-9]*\]:\),make\1,'
849_SED_CMDS+=	-e 's,<curdir>/,,g'
850_SED_CMDS+=	-e 's,${UNIT_TESTS:S,.,\\.,g}/,,g'
851_SED_CMDS+=	-e '/MAKE_VERSION/d'
852_SED_CMDS+=	-e '/EGREP=/d'
853
854# on AT&T derived systems: false exits 255 not 1
855.if ${.MAKE.OS:N*BSD} != ""
856_SED_CMDS+=	-e 's,\(Error code\) 255,\1 1,'
857.endif
858.if ${_shell:N*ksh*} == ""
859_SED_CMDS+=	-e '/^set [+-]v/d'
860SED_CMDS.opt-debug-jobs+= -e 's,Command: ksh -v,Command: <shell>,'
861SED_CMDS.opt-debug-jobs+= -e 's,Command: <shell> -v,Command: <shell>,'
862.endif
863
864.rawout.out:
865	@${TOOL_SED} ${_SED_CMDS} ${SED_CMDS.${.PREFIX:T}} \
866	  < ${.IMPSRC} > ${.TARGET}.tmp
867	@${POSTPROC.${.PREFIX:T}:D \
868	    ${POSTPROC.${.PREFIX:T}} < ${.TARGET}.tmp > ${.TARGET}.post \
869	    && mv ${.TARGET}.post ${.TARGET}.tmp}
870	@echo "exit status `cat ${.TARGET:R}.status`" >> ${.TARGET}.tmp
871	@mv ${.TARGET}.tmp ${.TARGET}
872
873.if empty(DIFF_FLAGS)
874DIFF_ECHO= echo
875.else
876DIFF_ECHO= :
877.endif
878
879# Compare all output files
880test:	${OUTFILES} .PHONY
881	@failed= ; \
882	for test in ${TESTS}; do \
883	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out && continue || \
884	  ${DIFF_ECHO} diff ${UNIT_TESTS}/$${test}.exp $${test}.out; \
885	  ${TOOL_DIFF} ${DIFF_FLAGS} ${UNIT_TESTS}/$${test}.exp $${test}.out \
886	  || failed="$${failed}$${failed:+ }$${test}" ; \
887	done ; \
888	if [ -n "$${failed}" ]; then \
889	  echo "Failed tests: $${failed}" ; false ; \
890	else \
891	  echo "All tests passed" ; \
892	  lua=${LUA:Ulua} ; \
893	  have_lua=$$("$$lua" -e 'print "yes"' 2>&1) ; \
894	  if [ "$$have_lua" = "yes" -a -s ${.CURDIR}/check-expect.lua ]; then \
895	    (cd ${.CURDIR} && "$$lua" ./check-expect.lua *.mk); \
896	  fi; \
897	fi
898
899accept:
900	@for test in ${TESTS}; do \
901	  cmp -s ${UNIT_TESTS}/$${test}.exp $${test}.out \
902	  || { echo "Replacing $${test}.exp" ; \
903	       cp $${test}.out ${UNIT_TESTS}/$${test}.exp ; } \
904	done
905
906.if exists(${TEST_MAKE})
907${TESTS:=.rawout}: ${TEST_MAKE}
908# in meta mode, we *know* if a target script is impacted
909# by a makefile change.
910.if ${.MAKE.MODE:Unormal:Mmeta} == ""
911${TESTS:=.rawout}: ${.PARSEDIR}/Makefile
912.endif
913.endif
914
915.-include <obj.mk>
916