1# $NetBSD: opt-query.mk,v 1.7 2022/08/18 05:37:05 rillig Exp $
2#
3# Tests for the -q command line option.
4#
5# The -q option only looks at the dependencies between the targets.
6# None of the commands in the targets are run, not even those that are
7# prefixed with '+'.
8
9# This test consists of several parts:
10#
11#	main		Delegates to the actual tests.
12#
13#	commands	Ensures that none of the targets is made.
14#
15#	variants	Ensures that the up-to-date status is correctly
16#			reported in both compat and jobs mode, and for several
17#			kinds of make targets.
18PART?=	main
19
20.if ${PART} == "main"
21
22all: .PHONY variants cleanup
23
24_!=	touch -f opt-query-file.up-to-date
25
26variants: .PHONY
27
28.  for target in commands
29	@echo 'Making ${target}':
30	@${MAKE} -r -f ${MAKEFILE} -q ${mode:Mjobs:%=-j1} ${target} PART=commands \
31	&& echo "${target}: query status $$?" \
32	|| echo "${target}: query status $$?"
33	@echo
34.  endfor
35
36.  for mode in compat jobs
37.    for target in opt-query-file.out-of-date opt-query-file.up-to-date phony
38	@echo 'Making ${target} in ${mode} mode':
39	@${MAKE} -r -f ${MAKEFILE} -q ${mode:Mjobs:%=-j1} ${target} PART=variants \
40	&& echo "${target} in ${mode} mode: query status $$?" \
41	|| echo "${target} in ${mode} mode: query status $$?"
42	@echo
43.    endfor
44.  endfor
45
46# Between 1994 and before 2022-08-17, the exit status for '-q' was always 1,
47# the cause for that exit code varied over time though.
48#
49# expect: opt-query-file.out-of-date in compat mode: query status 1
50# expect: opt-query-file.up-to-date in compat mode: query status 0
51# expect: phony in compat mode: query status 1
52# expect: opt-query-file.out-of-date in jobs mode: query status 1
53# expect: opt-query-file.up-to-date in jobs mode: query status 0
54# expect: phony in jobs mode: query status 1
55
56cleanup: .PHONY
57	@rm -f opt-query-file.up-to-date
58
59.elif ${PART} == "commands"
60
61# This command cannot be prevented from being run since it is used at parse
62# time, and any later variable assignments may depend on its result.
63!=	echo 'command during parsing' 1>&2; echo
64
65# None of these commands are run.
66.BEGIN:
67	@echo '$@: hidden command'
68	@+echo '$@: run always'
69
70# None of these commands are run.
71commands:
72	@echo '$@: hidden command'
73	@+echo '$@: run always'
74# The exit status 1 is because the "commands" target has to be made, that is,
75# it is not up-to-date.
76
77.elif ${PART} == "variants"
78
79opt-query-file.out-of-date: ${MAKEFILE}
80opt-query-file.up-to-date: ${MAKEFILE}
81phony: .PHONY
82
83.else
84.  error Invalid part '${PART}'
85.endif
86