11.17Srillig# $NetBSD: varmod-mtime.mk,v 1.17 2025/06/28 22:39:29 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.17Srillig# expect+1: Invalid argument "123x" for modifier ":mtime" 461.6Srillig.if ${no/such/file:L:mtime=123x} 471.6Srillig. error 481.6Srillig.else 491.6Srillig. error 501.6Srillig.endif 511.6Srillig 521.6Srillig 531.2Srillig# The timestamp of a newly created file must be at least as great as the 541.2Srillig# timestamp when parsing of this makefile started. 551.2SrilligCOOKIE= ${TMPDIR:U/tmp}/varmod-mtime.cookie 561.2Srillig_!= touch ${COOKIE} 571.2Srillig.if ${COOKIE:mtime=0} < ${start} 581.2Srillig. error ${COOKIE:mtime=0} < ${start} 591.2Srillig.endif 601.2Srillig_!= rm -f ${COOKIE} 611.2Srillig 621.2Srillig 631.2Srillig# If the optional argument of the ':mtime' modifier is the word 'error', the 641.2Srillig# modifier fails with an error message, once for each affected file. 651.2Srillig# 661.16Srillig# expect+2: Cannot determine mtime for "no/such/file1": <ENOENT> 671.16Srillig# expect+1: Cannot determine mtime for "no/such/file2": <ENOENT> 681.2Srillig.if ${no/such/file1 no/such/file2:L:mtime=error} 691.2Srillig. error 701.2Srillig.else 711.1Ssjg. error 721.1Ssjg.endif 731.1Ssjg 741.2Srillig 751.3Srillig# Only the word 'error' is a special argument to the ':mtime' modifier, all 761.3Srillig# other words result in a parse error. 771.17Srillig# expect+1: Invalid argument "errorhandler-no" for modifier ":mtime" 781.2Srillig.if ${MAKEFILE:mtime=errorhandler-no} > 0 791.2Srillig.else 801.2Srillig. error 811.1Ssjg.endif 821.1Ssjg 831.2Srillig 841.8Srillig# Only the word 'error' can be used as a fallback argument to the modifier. 851.17Srillig# expect+1: Invalid argument "warn" for modifier ":mtime" 861.8Srillig.if ${MAKEFILE:mtime=warn} > 0 871.8Srillig. error 881.8Srillig.else 891.8Srillig. error 901.8Srillig.endif 911.8Srillig 921.8Srillig 931.2Srillig# Ensure that the fallback for a missing modification time is indeed the 941.2Srillig# current time, and not any later time. 951.2Srilligend:= ${%s:L:gmtime} 961.2Srillig.if ${not_found_mtime} > ${end} 971.2Srillig. error 981.1Ssjg.endif 991.8Srillig 1001.8Srillig 1011.8Srillig# If the expression is irrelevant, the ':mtime' modifier is only parsed, it 1021.8Srillig# does not perform any filesystem operations. 1031.9Srillig.if 0 && ${no/such/file:L:mtime=error} 1041.8Srillig. error 1051.8Srillig.endif 1061.8Srillig 1071.8Srillig 1081.8Srillig# If there is a typo in the modifier name, it does not match. 1091.15Srillig# expect+1: Unknown modifier ":mtim" 1101.8Srillig.if ${anything:L:mtim} 1111.8Srillig. error 1121.8Srillig.else 1131.8Srillig. error 1141.8Srillig.endif 1151.9Srillig 1161.9Srillig 1171.9Srillig# An empty word list results in an empty mtime list. 1181.9Srillig.if ${:U:mtime} != "" 1191.9Srillig. error 1201.9Srillig.endif 121