xref: /freebsd/contrib/bmake/unit-tests/varmod-mtime.mk (revision 759b177aecbfc49ebc900739954ac56b1aa5fc53)
1*759b177aSSimon J. Gerraty# $NetBSD: varmod-mtime.mk,v 1.15 2025/03/29 19:08:52 rillig Exp $
2c1d01b5fSSimon J. Gerraty#
398875883SSimon J. Gerraty# Tests for the ':mtime' variable modifier, which maps each word of the
498875883SSimon J. Gerraty# expression to that file's modification time.
5c1d01b5fSSimon J. Gerraty
698875883SSimon J. Gerraty# Note: strftime() uses mktime() for %s and mktime() assumes localtime
798875883SSimon J. Gerraty# so this should match time()
898875883SSimon J. Gerratystart:=	${%s:L:localtime}	# see varmod-gmtime.mk, keyword '%s'
9c1d01b5fSSimon J. Gerraty
10c1d01b5fSSimon J. Gerraty
1198875883SSimon J. Gerraty# Ensure that this makefile exists and has a modification time.  If the file
1298875883SSimon J. Gerraty# didn't exist, the ':mtime' modifier would return the current time.
1398875883SSimon J. Gerraty.if ${MAKEFILE:mtime} >= ${start}
14c1d01b5fSSimon J. Gerraty.  error
15c1d01b5fSSimon J. Gerraty.endif
16c1d01b5fSSimon J. Gerraty
1798875883SSimon J. Gerraty
1898875883SSimon J. Gerraty# For a file that doesn't exist, the ':mtime' modifier returns the current
1998875883SSimon J. Gerraty# time, without an error or warning message.  The returned timestamp differs
2098875883SSimon J. Gerraty# from the 'now' that is used when updating the timestamps in archives or for
2198875883SSimon J. Gerraty# touching files using the '-t' option, which is taken once when make is
2298875883SSimon J. Gerraty# started.
2398875883SSimon J. Gerratynot_found_mtime:=	${no/such/file:L:mtime}
2498875883SSimon J. Gerraty.if ${not_found_mtime} < ${start}
25c1d01b5fSSimon J. Gerraty.  error
26c1d01b5fSSimon J. Gerraty.endif
27c1d01b5fSSimon J. Gerraty
2898875883SSimon J. Gerraty
2998875883SSimon J. Gerraty# The ':mtime' modifier accepts a timestamp in seconds as an optional
3098875883SSimon J. Gerraty# argument.  This timestamp is used as a fallback in case the file's time
3198875883SSimon J. Gerraty# cannot be determined, without any error or warning message.
3298875883SSimon J. Gerraty.if ${no/such/file:L:mtime=0} != "0"
3398875883SSimon J. Gerraty.  error
34c1d01b5fSSimon J. Gerraty.endif
35c1d01b5fSSimon J. Gerraty
3698875883SSimon J. Gerraty
3798875883SSimon J. Gerraty# The fallback timestamp must start with a digit, and it is interpreted as a
3898875883SSimon J. Gerraty# decimal integer.
3998875883SSimon J. Gerraty.if ${no/such/file:L:mtime=00042} != "42"
4098875883SSimon J. Gerraty.  error
4198875883SSimon J. Gerraty.endif
4298875883SSimon J. Gerraty
4398875883SSimon J. Gerraty
44d5e0a182SSimon J. Gerraty# The fallback timestamp must only be an integer, without trailing characters.
456a7405f5SSimon J. Gerraty# expect+1: Invalid argument '123x' for modifier ':mtime'
46d5e0a182SSimon J. Gerraty.if ${no/such/file:L:mtime=123x}
47d5e0a182SSimon J. Gerraty.  error
48d5e0a182SSimon J. Gerraty.else
49d5e0a182SSimon J. Gerraty.  error
50d5e0a182SSimon J. Gerraty.endif
51d5e0a182SSimon J. Gerraty
52d5e0a182SSimon J. Gerraty
5398875883SSimon J. Gerraty# The timestamp of a newly created file must be at least as great as the
5498875883SSimon J. Gerraty# timestamp when parsing of this makefile started.
5598875883SSimon J. GerratyCOOKIE=	${TMPDIR:U/tmp}/varmod-mtime.cookie
5698875883SSimon J. Gerraty_!=	touch ${COOKIE}
5798875883SSimon J. Gerraty.if ${COOKIE:mtime=0} < ${start}
5898875883SSimon J. Gerraty.   error ${COOKIE:mtime=0} < ${start}
5998875883SSimon J. Gerraty.endif
6098875883SSimon J. Gerraty_!=	rm -f ${COOKIE}
6198875883SSimon J. Gerraty
6298875883SSimon J. Gerraty
6398875883SSimon J. Gerraty# If the optional argument of the ':mtime' modifier is the word 'error', the
6498875883SSimon J. Gerraty# modifier fails with an error message, once for each affected file.
6598875883SSimon J. Gerraty#
666a7405f5SSimon J. Gerraty# expect+2: Cannot determine mtime for 'no/such/file1': <ENOENT>
676a7405f5SSimon J. Gerraty# expect+1: Cannot determine mtime for 'no/such/file2': <ENOENT>
6898875883SSimon J. Gerraty.if ${no/such/file1 no/such/file2:L:mtime=error}
6998875883SSimon J. Gerraty.  error
7098875883SSimon J. Gerraty.else
7198875883SSimon J. Gerraty.  error
7298875883SSimon J. Gerraty.endif
7398875883SSimon J. Gerraty
7498875883SSimon J. Gerraty
7598875883SSimon J. Gerraty# Only the word 'error' is a special argument to the ':mtime' modifier, all
7698875883SSimon J. Gerraty# other words result in a parse error.
776a7405f5SSimon J. Gerraty# expect+1: Invalid argument 'errorhandler-no' for modifier ':mtime'
7898875883SSimon J. Gerraty.if ${MAKEFILE:mtime=errorhandler-no} > 0
7998875883SSimon J. Gerraty.else
8098875883SSimon J. Gerraty.  error
8198875883SSimon J. Gerraty.endif
8298875883SSimon J. Gerraty
8398875883SSimon J. Gerraty
84d5e0a182SSimon J. Gerraty# Only the word 'error' can be used as a fallback argument to the modifier.
856a7405f5SSimon J. Gerraty# expect+1: Invalid argument 'warn' for modifier ':mtime'
86d5e0a182SSimon J. Gerraty.if ${MAKEFILE:mtime=warn} > 0
87d5e0a182SSimon J. Gerraty.  error
88d5e0a182SSimon J. Gerraty.else
89d5e0a182SSimon J. Gerraty.  error
90d5e0a182SSimon J. Gerraty.endif
91d5e0a182SSimon J. Gerraty
92d5e0a182SSimon J. Gerraty
9398875883SSimon J. Gerraty# Ensure that the fallback for a missing modification time is indeed the
9498875883SSimon J. Gerraty# current time, and not any later time.
9598875883SSimon J. Gerratyend:=	${%s:L:gmtime}
9698875883SSimon J. Gerraty.if ${not_found_mtime} > ${end}
9798875883SSimon J. Gerraty.  error
98c1d01b5fSSimon J. Gerraty.endif
99d5e0a182SSimon J. Gerraty
100d5e0a182SSimon J. Gerraty
101d5e0a182SSimon J. Gerraty# If the expression is irrelevant, the ':mtime' modifier is only parsed, it
102d5e0a182SSimon J. Gerraty# does not perform any filesystem operations.
103d5e0a182SSimon J. Gerraty.if 0 && ${no/such/file:L:mtime=error}
104d5e0a182SSimon J. Gerraty.  error
105d5e0a182SSimon J. Gerraty.endif
106d5e0a182SSimon J. Gerraty
107d5e0a182SSimon J. Gerraty
108d5e0a182SSimon J. Gerraty# If there is a typo in the modifier name, it does not match.
109*759b177aSSimon J. Gerraty# expect+1: Unknown modifier ":mtim"
110d5e0a182SSimon J. Gerraty.if ${anything:L:mtim}
111d5e0a182SSimon J. Gerraty.  error
112d5e0a182SSimon J. Gerraty.else
113d5e0a182SSimon J. Gerraty.  error
114d5e0a182SSimon J. Gerraty.endif
115d5e0a182SSimon J. Gerraty
116d5e0a182SSimon J. Gerraty
117d5e0a182SSimon J. Gerraty# An empty word list results in an empty mtime list.
118d5e0a182SSimon J. Gerraty.if ${:U:mtime} != ""
119d5e0a182SSimon J. Gerraty.  error
120d5e0a182SSimon J. Gerraty.endif
121