varmod-mtime.mk revision 1.5
11.5Srillig# $NetBSD: varmod-mtime.mk,v 1.5 2023/08/19 08:19:25 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.2Srillig# The timestamp of a newly created file must be at least as great as the
451.2Srillig# timestamp when parsing of this makefile started.
461.2SrilligCOOKIE=	${TMPDIR:U/tmp}/varmod-mtime.cookie
471.2Srillig_!=	touch ${COOKIE}
481.2Srillig.if ${COOKIE:mtime=0} < ${start}
491.2Srillig.   error ${COOKIE:mtime=0} < ${start}
501.2Srillig.endif
511.2Srillig_!=	rm -f ${COOKIE}
521.2Srillig
531.2Srillig
541.2Srillig# If the optional argument of the ':mtime' modifier is the word 'error', the
551.2Srillig# modifier fails with an error message, once for each affected file.
561.2Srillig#
571.2Srillig# expect+3: Cannot determine mtime for 'no/such/file1': <ENOENT>
581.2Srillig# expect+2: Cannot determine mtime for 'no/such/file2': <ENOENT>
591.2Srillig# expect+1: Malformed conditional (${no/such/file1 no/such/file2:L:mtime=error})
601.2Srillig.if ${no/such/file1 no/such/file2:L:mtime=error}
611.2Srillig.  error
621.2Srillig.else
631.1Ssjg.  error
641.1Ssjg.endif
651.1Ssjg
661.2Srillig
671.3Srillig# Only the word 'error' is a special argument to the ':mtime' modifier, all
681.3Srillig# other words result in a parse error.
691.3Srillig# expect+2: Invalid argument 'errorhandler-no' for modifier ':mtime'
701.2Srillig# expect+1: Malformed conditional (${MAKEFILE:mtime=errorhandler-no} > 0)
711.2Srillig.if ${MAKEFILE:mtime=errorhandler-no} > 0
721.2Srillig.else
731.2Srillig.  error
741.1Ssjg.endif
751.1Ssjg
761.2Srillig
771.2Srillig# Ensure that the fallback for a missing modification time is indeed the
781.2Srillig# current time, and not any later time.
791.2Srilligend:=	${%s:L:gmtime}
801.2Srillig.if ${not_found_mtime} > ${end}
811.2Srillig.  error
821.1Ssjg.endif
83