xref: /freebsd/contrib/bmake/unit-tests/cmd-interrupt.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1*6a7405f5SSimon J. Gerraty# $NetBSD: cmd-interrupt.mk,v 1.5 2024/07/13 15:10:06 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for interrupting a command.
42c3632d1SSimon J. Gerraty#
52c3632d1SSimon J. Gerraty# If a command is interrupted (usually by the user, here by itself), the
62c3632d1SSimon J. Gerraty# target is removed.  This is to avoid having an unfinished target that
72c3632d1SSimon J. Gerraty# would be newer than all of its sources and would therefore not be
82c3632d1SSimon J. Gerraty# tried again in the next run.
92c3632d1SSimon J. Gerraty#
102c3632d1SSimon J. Gerraty# This happens for ordinary targets as well as for .PHONY targets, even
112c3632d1SSimon J. Gerraty# though the .PHONY targets usually do not correspond to a file.
122c3632d1SSimon J. Gerraty#
132c3632d1SSimon J. Gerraty# To protect the target from being removed, the target has to be marked with
142c3632d1SSimon J. Gerraty# the special source .PRECIOUS.  These targets need to ensure for themselves
152c3632d1SSimon J. Gerraty# that interrupting them does not leave an inconsistent state behind.
162c3632d1SSimon J. Gerraty#
172c3632d1SSimon J. Gerraty# See also:
182c3632d1SSimon J. Gerraty#	CompatDeleteTarget
192c3632d1SSimon J. Gerraty
20*6a7405f5SSimon J. Gerratyall: clean-before
21*6a7405f5SSimon J. Gerratyall: interrupt-ordinary
22*6a7405f5SSimon J. Gerratyall: interrupt-phony
23*6a7405f5SSimon J. Gerratyall: interrupt-precious
24*6a7405f5SSimon J. Gerratyall: interrupt-compat
25*6a7405f5SSimon J. Gerratyall: clean-after
262c3632d1SSimon J. Gerraty
272c3632d1SSimon J. Gerratyclean-before clean-after: .PHONY
28*6a7405f5SSimon J. Gerraty	@rm -f cmd-interrupt-ordinary cmd-interrupt-phony
29*6a7405f5SSimon J. Gerraty	@rm -f cmd-interrupt-precious cmd-interrupt-compat
302c3632d1SSimon J. Gerraty
31e2eeea75SSimon J. Gerratyinterrupt-ordinary:
322c3632d1SSimon J. Gerraty	@${.MAKE} ${MAKEFLAGS} -f ${MAKEFILE} cmd-interrupt-ordinary || true
332c3632d1SSimon J. Gerraty	# The ././ is necessary to work around the file cache.
342c3632d1SSimon J. Gerraty	@echo ${.TARGET}: ${exists(././cmd-interrupt-ordinary) :? error : ok }
352c3632d1SSimon J. Gerraty
362c3632d1SSimon J. Gerratyinterrupt-phony: .PHONY
372c3632d1SSimon J. Gerraty	@${.MAKE} ${MAKEFLAGS} -f ${MAKEFILE} cmd-interrupt-phony || true
382c3632d1SSimon J. Gerraty	# The ././ is necessary to work around the file cache.
398c973ee2SSimon J. Gerraty	@echo ${.TARGET}: ${exists(././cmd-interrupt-phony) :? ok : error }
402c3632d1SSimon J. Gerraty
412c3632d1SSimon J. Gerratyinterrupt-precious: .PRECIOUS
422c3632d1SSimon J. Gerraty	@${.MAKE} ${MAKEFLAGS} -f ${MAKEFILE} cmd-interrupt-precious || true
432c3632d1SSimon J. Gerraty	# The ././ is necessary to work around the file cache.
442c3632d1SSimon J. Gerraty	@echo ${.TARGET}: ${exists(././cmd-interrupt-precious) :? ok : error }
452c3632d1SSimon J. Gerraty
46*6a7405f5SSimon J. Gerratyinterrupt-compat:
47*6a7405f5SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} cmd-interrupt-compat || true
48*6a7405f5SSimon J. Gerraty	@echo ${.TARGET} ${exists(././cmd-interrupt-compat) :? expected-fail : unexpected-ok }
49*6a7405f5SSimon J. Gerraty
502c3632d1SSimon J. Gerratycmd-interrupt-ordinary:
512c3632d1SSimon J. Gerraty	> ${.TARGET}
522c3632d1SSimon J. Gerraty	@kill -INT ${.MAKE.PID}
532c3632d1SSimon J. Gerraty
542c3632d1SSimon J. Gerratycmd-interrupt-phony: .PHONY
552c3632d1SSimon J. Gerraty	> ${.TARGET}
562c3632d1SSimon J. Gerraty	@kill -INT ${.MAKE.PID}
572c3632d1SSimon J. Gerraty
582c3632d1SSimon J. Gerratycmd-interrupt-precious: .PRECIOUS
592c3632d1SSimon J. Gerraty	> ${.TARGET}
602c3632d1SSimon J. Gerraty	@kill -INT ${.MAKE.PID}
61*6a7405f5SSimon J. Gerraty
62*6a7405f5SSimon J. Gerraty# When the make process (and not the process group) is interrupted in compat
63*6a7405f5SSimon J. Gerraty# mode, it first tries to interrupt the process group of the currently running
64*6a7405f5SSimon J. Gerraty# child command, but that fails since there is no such process group, rather
65*6a7405f5SSimon J. Gerraty# the child command runs in the same process group as make itself.  The child
66*6a7405f5SSimon J. Gerraty# command then continues, and after sleeping a bit creates the target file.
67*6a7405f5SSimon J. Gerratycmd-interrupt-compat:
68*6a7405f5SSimon J. Gerraty	@kill -INT ${.MAKE.PID} && sleep 1 && > ${.TARGET}
69