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