varmod-mtime.mk revision 1.2
1# $NetBSD: varmod-mtime.mk,v 1.2 2023/08/17 18:48:32 rillig Exp $
2#
3# Tests for the ':mtime' variable modifier, which maps each word of the
4# expression to that file's modification time.
5
6start:=	${%s:L:gmtime}
7
8
9# Ensure that this makefile exists and has a modification time.  If the file
10# didn't exist, the ':mtime' modifier would return the current time.
11.if ${MAKEFILE:mtime} >= ${start}
12.  error
13.endif
14
15
16# For a file that doesn't exist, the ':mtime' modifier returns the current
17# time, without an error or warning message.  The returned timestamp differs
18# from the 'now' that is used when updating the timestamps in archives or for
19# touching files using the '-t' option, which is taken once when make is
20# started.
21not_found_mtime:=	${no/such/file:L:mtime}
22.if ${not_found_mtime} < ${start}
23.  error
24.endif
25
26
27# The ':mtime' modifier accepts a timestamp in seconds as an optional
28# argument.  This timestamp is used as a fallback in case the file's time
29# cannot be determined, without any error or warning message.
30.if ${no/such/file:L:mtime=0} != "0"
31.  error
32.endif
33
34
35# The fallback timestamp must start with a digit, and it is interpreted as a
36# decimal integer.
37.if ${no/such/file:L:mtime=00042} != "42"
38.  error
39.endif
40
41
42# The timestamp of a newly created file must be at least as great as the
43# timestamp when parsing of this makefile started.
44COOKIE=	${TMPDIR:U/tmp}/varmod-mtime.cookie
45_!=	touch ${COOKIE}
46.if ${COOKIE:mtime=0} < ${start}
47.   error ${COOKIE:mtime=0} < ${start}
48.endif
49_!=	rm -f ${COOKIE}
50
51
52# If the optional argument of the ':mtime' modifier is the word 'error', the
53# modifier fails with an error message, once for each affected file.
54#
55# expect+3: Cannot determine mtime for 'no/such/file1': <ENOENT>
56# expect+2: Cannot determine mtime for 'no/such/file2': <ENOENT>
57# expect+1: Malformed conditional (${no/such/file1 no/such/file2:L:mtime=error})
58.if ${no/such/file1 no/such/file2:L:mtime=error}
59.  error
60.else
61.  error
62.endif
63
64
65# TODO: Only the word 'error' is a special argument to the ':mtime' modifier,
66# all other words should result in a parse error.
67# expect+2: Unknown modifier "handler-no"
68# expect+1: Malformed conditional (${MAKEFILE:mtime=errorhandler-no} > 0)
69.if ${MAKEFILE:mtime=errorhandler-no} > 0
70.else
71.  error
72.endif
73
74
75# Ensure that the fallback for a missing modification time is indeed the
76# current time, and not any later time.
77end:=	${%s:L:gmtime}
78.if ${not_found_mtime} > ${end}
79.  error
80.endif
81