Module Name:    src
Committed By:   sjg
Date:           Tue May  9 20:14:28 UTC 2023

Modified Files:
        src/distrib/sets/lists/tests: mi
        src/usr.bin/make: make.1 var.c
        src/usr.bin/make/unit-tests: Makefile
Added Files:
        src/usr.bin/make/unit-tests: varmod-mtime.exp varmod-mtime.mk

Log Message:
make: add :mtime to provide mtime of file

The value of the variable is passed to stat(2)
and st_mtime is new value.
An optional arg can be used if stat(2) fails, otherwise
the current time is used.

See varmod-mtime.mk for usage examples.


To generate a diff of this commit:
cvs rdiff -u -r1.1258 -r1.1259 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.363 -r1.364 src/usr.bin/make/make.1
cvs rdiff -u -r1.1050 -r1.1051 src/usr.bin/make/var.c
cvs rdiff -u -r1.333 -r1.334 src/usr.bin/make/unit-tests/Makefile
cvs rdiff -u -r0 -r1.1 src/usr.bin/make/unit-tests/varmod-mtime.exp \
    src/usr.bin/make/unit-tests/varmod-mtime.mk

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sets/lists/tests/mi
diff -u src/distrib/sets/lists/tests/mi:1.1258 src/distrib/sets/lists/tests/mi:1.1259
--- src/distrib/sets/lists/tests/mi:1.1258	Sun Apr 23 08:47:27 2023
+++ src/distrib/sets/lists/tests/mi	Tue May  9 20:14:28 2023
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1258 2023/04/23 08:47:27 rillig Exp $
+# $NetBSD: mi,v 1.1259 2023/05/09 20:14:28 sjg Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -6099,6 +6099,8 @@
 ./usr/tests/usr.bin/make/unit-tests/varmod-match-escape.mk			tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/varmod-match.exp				tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/varmod-match.mk				tests-usr.bin-tests	compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/varmod-mtime.exp				tests-usr.bin-tests	compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/varmod-mtime.mk				tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/varmod-no-match.exp				tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/varmod-no-match.mk				tests-usr.bin-tests	compattestfile,atf
 ./usr/tests/usr.bin/make/unit-tests/varmod-order-numeric.exp			tests-usr.bin-tests	compattestfile,atf

Index: src/usr.bin/make/make.1
diff -u src/usr.bin/make/make.1:1.363 src/usr.bin/make/make.1:1.364
--- src/usr.bin/make/make.1:1.363	Sun May  7 16:43:50 2023
+++ src/usr.bin/make/make.1	Tue May  9 20:14:27 2023
@@ -1,4 +1,4 @@
-.\"	$NetBSD: make.1,v 1.363 2023/05/07 16:43:50 sjg Exp $
+.\"	$NetBSD: make.1,v 1.364 2023/05/09 20:14:27 sjg Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	from: @(#)make.1	8.4 (Berkeley) 3/19/94
 .\"
-.Dd May 6, 2023
+.Dd May 9, 2023
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -1510,6 +1510,18 @@ producing the formatted timestamp.
 If a
 .Ar timestamp
 value is not provided or is 0, the current time is used.
+.It Cm \&:mtime Ns Oo Cm = Ns Ar timestamp Oc
+call
+.Xr stat 2
+with the value as pathname;
+use
+.Ql st_mtime
+as the new value.
+If
+.Xr stat 2
+fails; use
+.Ar timestamp
+or current time.
 .It Cm \&:tA
 Attempts to convert the value to an absolute path using
 .Xr realpath 3 .

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.1050 src/usr.bin/make/var.c:1.1051
--- src/usr.bin/make/var.c:1.1050	Tue May  9 16:26:59 2023
+++ src/usr.bin/make/var.c	Tue May  9 20:14:27 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.1050 2023/05/09 16:26:59 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.1051 2023/05/09 20:14:27 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -139,7 +139,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1050 2023/05/09 16:26:59 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1051 2023/05/09 20:14:27 sjg Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -2833,6 +2833,39 @@ ApplyModifier_Match(const char **pp, Mod
 	return AMR_OK;
 }
 
+/* :mtime */
+static ApplyModifierResult
+ApplyModifier_Mtime(const char **pp, ModChain *ch)
+{
+	char buf[BUFSIZ];
+	Expr *expr = ch->expr;
+	const char *args, *mod = *pp;
+	struct stat st;
+	int i = -1;
+
+	if (!ModMatchEq(mod, "mtime", ch))
+		return AMR_UNKNOWN;
+	*pp += 5;
+	args = *pp;
+	if (args[0] == '=') {
+		args++;
+		if (!TryParseIntBase0(&args, &i))
+			return AMR_BAD;
+		*pp = args;
+	}
+	if (!ModChain_ShouldEval(ch))
+		return AMR_OK;
+	if (stat(Expr_Str(expr), &st) < 0) {
+		if (i < 0)
+			time(&st.st_mtime);
+		else
+			st.st_mtime = (time_t)i;
+	}
+	snprintf(buf, sizeof(buf), "%u", (unsigned)st.st_mtime);
+	Expr_SetValueOwn(expr, bmake_strdup(buf));
+	return AMR_OK;
+}
+
 static void
 ParsePatternFlags(const char **pp, PatternFlags *pflags, bool *oneBigWord)
 {
@@ -3815,6 +3848,8 @@ ApplyModifier(const char **pp, ModChain 
 	case 'M':
 	case 'N':
 		return ApplyModifier_Match(pp, ch);
+	case 'm':
+		return ApplyModifier_Mtime(pp, ch);
 	case 'O':
 		return ApplyModifier_Order(pp, ch);
 	case 'P':

Index: src/usr.bin/make/unit-tests/Makefile
diff -u src/usr.bin/make/unit-tests/Makefile:1.333 src/usr.bin/make/unit-tests/Makefile:1.334
--- src/usr.bin/make/unit-tests/Makefile:1.333	Sat Feb 25 19:30:32 2023
+++ src/usr.bin/make/unit-tests/Makefile	Tue May  9 20:14:27 2023
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.333 2023/02/25 19:30:32 sjg Exp $
+# $NetBSD: Makefile,v 1.334 2023/05/09 20:14:27 sjg Exp $
 #
 # Unit tests for make(1)
 #
@@ -374,6 +374,7 @@ TESTS+=		varmod-loop-delete
 TESTS+=		varmod-loop-varname
 TESTS+=		varmod-match
 TESTS+=		varmod-match-escape
+TESTS+=		varmod-mtime
 TESTS+=		varmod-no-match
 TESTS+=		varmod-order
 TESTS+=		varmod-order-numeric

Added files:

Index: src/usr.bin/make/unit-tests/varmod-mtime.exp
diff -u /dev/null src/usr.bin/make/unit-tests/varmod-mtime.exp:1.1
--- /dev/null	Tue May  9 20:14:28 2023
+++ src/usr.bin/make/unit-tests/varmod-mtime.exp	Tue May  9 20:14:27 2023
@@ -0,0 +1 @@
+exit status 0
Index: src/usr.bin/make/unit-tests/varmod-mtime.mk
diff -u /dev/null src/usr.bin/make/unit-tests/varmod-mtime.mk:1.1
--- /dev/null	Tue May  9 20:14:28 2023
+++ src/usr.bin/make/unit-tests/varmod-mtime.mk	Tue May  9 20:14:27 2023
@@ -0,0 +1,30 @@
+# $NetBSD: varmod-mtime.mk,v 1.1 2023/05/09 20:14:27 sjg Exp $
+#
+# Tests for the :mtime variable modifier, which provides mtime
+# of variable value assumed to be a pathname.
+
+all:
+
+# mtime of this makefile
+mtime:= ${MAKEFILE:mtime}
+
+# if pathname does not exist and timestamp is provided
+# that is the result
+.if ${no/such:L:mtime=0} != "0"
+.  error
+.endif
+
+.if ${no/such:L:mtime=42} != "42"
+.  error
+.endif
+
+# if no timestamp is provided and stat(2) fails use current time
+.if ${no/such:L:mtime} < ${mtime}
+.   error no/such:L:mtime ${no/such:L:mtime} < ${mtime}
+.endif
+
+COOKIE = ${TMPDIR}/varmod-mtime.cookie
+x!= touch ${COOKIE}
+.if ${COOKIE:mtime=0} < ${mtime}
+.   error COOKIE:mtime=0 ${COOKIE:mtime=0} < ${mtime}
+.endif

Reply via email to