varmod-mtime.mk revision 1.8
11.8Srillig# $NetBSD: varmod-mtime.mk,v 1.8 2023/11/19 12:11:34 rillig Exp $
21.1Ssjg#
31.2Srillig# Tests for the ':mtime' variable modifier, which maps each word of the
41.2Srillig# expression to that file's modification time.
51.1Ssjg
61.4Ssjg# Note: strftime() uses mktime() for %s and mktime() assumes localtime
71.4Ssjg# so this should match time()
81.5Srilligstart:=	${%s:L:localtime}	# see varmod-gmtime.mk, keyword '%s'
91.1Ssjg
101.1Ssjg
111.2Srillig# Ensure that this makefile exists and has a modification time.  If the file
121.2Srillig# didn't exist, the ':mtime' modifier would return the current time.
131.2Srillig.if ${MAKEFILE:mtime} >= ${start}
141.1Ssjg.  error
151.1Ssjg.endif
161.1Ssjg
171.2Srillig
181.2Srillig# For a file that doesn't exist, the ':mtime' modifier returns the current
191.2Srillig# time, without an error or warning message.  The returned timestamp differs
201.2Srillig# from the 'now' that is used when updating the timestamps in archives or for
211.2Srillig# touching files using the '-t' option, which is taken once when make is
221.2Srillig# started.
231.2Srillignot_found_mtime:=	${no/such/file:L:mtime}
241.2Srillig.if ${not_found_mtime} < ${start}
251.2Srillig.  error
261.2Srillig.endif
271.2Srillig
281.2Srillig
291.2Srillig# The ':mtime' modifier accepts a timestamp in seconds as an optional
301.2Srillig# argument.  This timestamp is used as a fallback in case the file's time
311.2Srillig# cannot be determined, without any error or warning message.
321.2Srillig.if ${no/such/file:L:mtime=0} != "0"
331.2Srillig.  error
341.2Srillig.endif
351.2Srillig
361.2Srillig
371.2Srillig# The fallback timestamp must start with a digit, and it is interpreted as a
381.2Srillig# decimal integer.
391.2Srillig.if ${no/such/file:L:mtime=00042} != "42"
401.2Srillig.  error
411.2Srillig.endif
421.2Srillig
431.2Srillig
441.6Srillig# The fallback timestamp must only be an integer, without trailing characters.
451.7Srillig# expect+2: Invalid argument '123x' for modifier ':mtime'
461.6Srillig# expect+1: Malformed conditional (${no/such/file:L:mtime=123x})
471.6Srillig.if ${no/such/file:L:mtime=123x}
481.6Srillig.  error
491.6Srillig.else
501.6Srillig.  error
511.6Srillig.endif
521.6Srillig
531.6Srillig
541.2Srillig# The timestamp of a newly created file must be at least as great as the
551.2Srillig# timestamp when parsing of this makefile started.
561.2SrilligCOOKIE=	${TMPDIR:U/tmp}/varmod-mtime.cookie
571.2Srillig_!=	touch ${COOKIE}
581.2Srillig.if ${COOKIE:mtime=0} < ${start}
591.2Srillig.   error ${COOKIE:mtime=0} < ${start}
601.2Srillig.endif
611.2Srillig_!=	rm -f ${COOKIE}
621.2Srillig
631.2Srillig
641.2Srillig# If the optional argument of the ':mtime' modifier is the word 'error', the
651.2Srillig# modifier fails with an error message, once for each affected file.
661.2Srillig#
671.2Srillig# expect+3: Cannot determine mtime for 'no/such/file1': <ENOENT>
681.2Srillig# expect+2: Cannot determine mtime for 'no/such/file2': <ENOENT>
691.2Srillig# expect+1: Malformed conditional (${no/such/file1 no/such/file2:L:mtime=error})
701.2Srillig.if ${no/such/file1 no/such/file2:L:mtime=error}
711.2Srillig.  error
721.2Srillig.else
731.1Ssjg.  error
741.1Ssjg.endif
751.1Ssjg
761.2Srillig
771.3Srillig# Only the word 'error' is a special argument to the ':mtime' modifier, all
781.3Srillig# other words result in a parse error.
791.3Srillig# expect+2: Invalid argument 'errorhandler-no' for modifier ':mtime'
801.2Srillig# expect+1: Malformed conditional (${MAKEFILE:mtime=errorhandler-no} > 0)
811.2Srillig.if ${MAKEFILE:mtime=errorhandler-no} > 0
821.2Srillig.else
831.2Srillig.  error
841.1Ssjg.endif
851.1Ssjg
861.2Srillig
871.8Srillig# Only the word 'error' can be used as a fallback argument to the modifier.
881.8Srillig# expect+2: Invalid argument 'warn' for modifier ':mtime'
891.8Srillig# expect+1: Malformed conditional (${MAKEFILE:mtime=warn} > 0)
901.8Srillig.if ${MAKEFILE:mtime=warn} > 0
911.8Srillig.  error
921.8Srillig.else
931.8Srillig.  error
941.8Srillig.endif
951.8Srillig
961.8Srillig
971.2Srillig# Ensure that the fallback for a missing modification time is indeed the
981.2Srillig# current time, and not any later time.
991.2Srilligend:=	${%s:L:gmtime}
1001.2Srillig.if ${not_found_mtime} > ${end}
1011.2Srillig.  error
1021.1Ssjg.endif
1031.8Srillig
1041.8Srillig
1051.8Srillig# If the expression is irrelevant, the ':mtime' modifier is only parsed, it
1061.8Srillig# does not perform any filesystem operations.
1071.8Srillig.if 0 && ${anything:L:mtime}
1081.8Srillig.  error
1091.8Srillig.endif
1101.8Srillig
1111.8Srillig
1121.8Srillig# If there is a typo in the modifier name, it does not match.
1131.8Srillig# expect+2: Unknown modifier "mtim"
1141.8Srillig# expect+1: Malformed conditional (${anything:L:mtim})
1151.8Srillig.if ${anything:L:mtim}
1161.8Srillig.  error
1171.8Srillig.else
1181.8Srillig.  error
1191.8Srillig.endif
120