xref: /freebsd/contrib/bmake/unit-tests/cmd-interrupt.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1# $NetBSD: cmd-interrupt.mk,v 1.5 2024/07/13 15:10:06 rillig Exp $
2#
3# Tests for interrupting a command.
4#
5# If a command is interrupted (usually by the user, here by itself), the
6# target is removed.  This is to avoid having an unfinished target that
7# would be newer than all of its sources and would therefore not be
8# tried again in the next run.
9#
10# This happens for ordinary targets as well as for .PHONY targets, even
11# though the .PHONY targets usually do not correspond to a file.
12#
13# To protect the target from being removed, the target has to be marked with
14# the special source .PRECIOUS.  These targets need to ensure for themselves
15# that interrupting them does not leave an inconsistent state behind.
16#
17# See also:
18#	CompatDeleteTarget
19
20all: clean-before
21all: interrupt-ordinary
22all: interrupt-phony
23all: interrupt-precious
24all: interrupt-compat
25all: clean-after
26
27clean-before clean-after: .PHONY
28	@rm -f cmd-interrupt-ordinary cmd-interrupt-phony
29	@rm -f cmd-interrupt-precious cmd-interrupt-compat
30
31interrupt-ordinary:
32	@${.MAKE} ${MAKEFLAGS} -f ${MAKEFILE} cmd-interrupt-ordinary || true
33	# The ././ is necessary to work around the file cache.
34	@echo ${.TARGET}: ${exists(././cmd-interrupt-ordinary) :? error : ok }
35
36interrupt-phony: .PHONY
37	@${.MAKE} ${MAKEFLAGS} -f ${MAKEFILE} cmd-interrupt-phony || true
38	# The ././ is necessary to work around the file cache.
39	@echo ${.TARGET}: ${exists(././cmd-interrupt-phony) :? ok : error }
40
41interrupt-precious: .PRECIOUS
42	@${.MAKE} ${MAKEFLAGS} -f ${MAKEFILE} cmd-interrupt-precious || true
43	# The ././ is necessary to work around the file cache.
44	@echo ${.TARGET}: ${exists(././cmd-interrupt-precious) :? ok : error }
45
46interrupt-compat:
47	@${MAKE} -f ${MAKEFILE} cmd-interrupt-compat || true
48	@echo ${.TARGET} ${exists(././cmd-interrupt-compat) :? expected-fail : unexpected-ok }
49
50cmd-interrupt-ordinary:
51	> ${.TARGET}
52	@kill -INT ${.MAKE.PID}
53
54cmd-interrupt-phony: .PHONY
55	> ${.TARGET}
56	@kill -INT ${.MAKE.PID}
57
58cmd-interrupt-precious: .PRECIOUS
59	> ${.TARGET}
60	@kill -INT ${.MAKE.PID}
61
62# When the make process (and not the process group) is interrupted in compat
63# mode, it first tries to interrupt the process group of the currently running
64# child command, but that fails since there is no such process group, rather
65# the child command runs in the same process group as make itself.  The child
66# command then continues, and after sleeping a bit creates the target file.
67cmd-interrupt-compat:
68	@kill -INT ${.MAKE.PID} && sleep 1 && > ${.TARGET}
69