varmod-mtime.mk revision 1.2
11.2Srillig# $NetBSD: varmod-mtime.mk,v 1.2 2023/08/17 18:48:32 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.2Srilligstart:=	${%s:L:gmtime}
71.1Ssjg
81.1Ssjg
91.2Srillig# Ensure that this makefile exists and has a modification time.  If the file
101.2Srillig# didn't exist, the ':mtime' modifier would return the current time.
111.2Srillig.if ${MAKEFILE:mtime} >= ${start}
121.1Ssjg.  error
131.1Ssjg.endif
141.1Ssjg
151.2Srillig
161.2Srillig# For a file that doesn't exist, the ':mtime' modifier returns the current
171.2Srillig# time, without an error or warning message.  The returned timestamp differs
181.2Srillig# from the 'now' that is used when updating the timestamps in archives or for
191.2Srillig# touching files using the '-t' option, which is taken once when make is
201.2Srillig# started.
211.2Srillignot_found_mtime:=	${no/such/file:L:mtime}
221.2Srillig.if ${not_found_mtime} < ${start}
231.2Srillig.  error
241.2Srillig.endif
251.2Srillig
261.2Srillig
271.2Srillig# The ':mtime' modifier accepts a timestamp in seconds as an optional
281.2Srillig# argument.  This timestamp is used as a fallback in case the file's time
291.2Srillig# cannot be determined, without any error or warning message.
301.2Srillig.if ${no/such/file:L:mtime=0} != "0"
311.2Srillig.  error
321.2Srillig.endif
331.2Srillig
341.2Srillig
351.2Srillig# The fallback timestamp must start with a digit, and it is interpreted as a
361.2Srillig# decimal integer.
371.2Srillig.if ${no/such/file:L:mtime=00042} != "42"
381.2Srillig.  error
391.2Srillig.endif
401.2Srillig
411.2Srillig
421.2Srillig# The timestamp of a newly created file must be at least as great as the
431.2Srillig# timestamp when parsing of this makefile started.
441.2SrilligCOOKIE=	${TMPDIR:U/tmp}/varmod-mtime.cookie
451.2Srillig_!=	touch ${COOKIE}
461.2Srillig.if ${COOKIE:mtime=0} < ${start}
471.2Srillig.   error ${COOKIE:mtime=0} < ${start}
481.2Srillig.endif
491.2Srillig_!=	rm -f ${COOKIE}
501.2Srillig
511.2Srillig
521.2Srillig# If the optional argument of the ':mtime' modifier is the word 'error', the
531.2Srillig# modifier fails with an error message, once for each affected file.
541.2Srillig#
551.2Srillig# expect+3: Cannot determine mtime for 'no/such/file1': <ENOENT>
561.2Srillig# expect+2: Cannot determine mtime for 'no/such/file2': <ENOENT>
571.2Srillig# expect+1: Malformed conditional (${no/such/file1 no/such/file2:L:mtime=error})
581.2Srillig.if ${no/such/file1 no/such/file2:L:mtime=error}
591.2Srillig.  error
601.2Srillig.else
611.1Ssjg.  error
621.1Ssjg.endif
631.1Ssjg
641.2Srillig
651.2Srillig# TODO: Only the word 'error' is a special argument to the ':mtime' modifier,
661.2Srillig# all other words should result in a parse error.
671.2Srillig# expect+2: Unknown modifier "handler-no"
681.2Srillig# expect+1: Malformed conditional (${MAKEFILE:mtime=errorhandler-no} > 0)
691.2Srillig.if ${MAKEFILE:mtime=errorhandler-no} > 0
701.2Srillig.else
711.2Srillig.  error
721.1Ssjg.endif
731.1Ssjg
741.2Srillig
751.2Srillig# Ensure that the fallback for a missing modification time is indeed the
761.2Srillig# current time, and not any later time.
771.2Srilligend:=	${%s:L:gmtime}
781.2Srillig.if ${not_found_mtime} > ${end}
791.2Srillig.  error
801.1Ssjg.endif
81