Module Name: src
Committed By: rillig
Date: Tue Dec 1 22:16:36 UTC 2020
Modified Files:
src/distrib/sets/lists/tests: mi
src/usr.bin/make/unit-tests: Makefile
Added Files:
src/usr.bin/make/unit-tests: varmod-indirect.exp varmod-indirect.mk
Log Message:
make(1): add test and tutorial for indirect modifiers
To generate a diff of this commit:
cvs rdiff -u -r1.981 -r1.982 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.233 -r1.234 src/usr.bin/make/unit-tests/Makefile
cvs rdiff -u -r0 -r1.1 src/usr.bin/make/unit-tests/varmod-indirect.exp \
src/usr.bin/make/unit-tests/varmod-indirect.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.981 src/distrib/sets/lists/tests/mi:1.982
--- src/distrib/sets/lists/tests/mi:1.981 Tue Dec 1 20:37:30 2020
+++ src/distrib/sets/lists/tests/mi Tue Dec 1 22:16:36 2020
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.981 2020/12/01 20:37:30 rillig Exp $
+# $NetBSD: mi,v 1.982 2020/12/01 22:16:36 rillig Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -5387,6 +5387,8 @@
./usr/tests/usr.bin/make/unit-tests/varmod-head.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/varmod-ifelse.exp tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/varmod-ifelse.mk tests-usr.bin-tests compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/varmod-indirect.exp tests-usr.bin-tests compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/varmod-indirect.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/varmod-l-name-to-value.exp tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/varmod-l-name-to-value.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/varmod-localtime.exp tests-usr.bin-tests compattestfile,atf
Index: src/usr.bin/make/unit-tests/Makefile
diff -u src/usr.bin/make/unit-tests/Makefile:1.233 src/usr.bin/make/unit-tests/Makefile:1.234
--- src/usr.bin/make/unit-tests/Makefile:1.233 Tue Dec 1 20:37:30 2020
+++ src/usr.bin/make/unit-tests/Makefile Tue Dec 1 22:16:36 2020
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.233 2020/12/01 20:37:30 rillig Exp $
+# $NetBSD: Makefile,v 1.234 2020/12/01 22:16:36 rillig Exp $
#
# Unit tests for make(1)
#
@@ -318,6 +318,7 @@ TESTS+= varmod-gmtime
TESTS+= varmod-hash
TESTS+= varmod-head
TESTS+= varmod-ifelse
+TESTS+= varmod-indirect
TESTS+= varmod-l-name-to-value
TESTS+= varmod-localtime
TESTS+= varmod-loop
Added files:
Index: src/usr.bin/make/unit-tests/varmod-indirect.exp
diff -u /dev/null src/usr.bin/make/unit-tests/varmod-indirect.exp:1.1
--- /dev/null Tue Dec 1 22:16:36 2020
+++ src/usr.bin/make/unit-tests/varmod-indirect.exp Tue Dec 1 22:16:36 2020
@@ -0,0 +1 @@
+exit status 0
Index: src/usr.bin/make/unit-tests/varmod-indirect.mk
diff -u /dev/null src/usr.bin/make/unit-tests/varmod-indirect.mk:1.1
--- /dev/null Tue Dec 1 22:16:36 2020
+++ src/usr.bin/make/unit-tests/varmod-indirect.mk Tue Dec 1 22:16:36 2020
@@ -0,0 +1,72 @@
+# $NetBSD: varmod-indirect.mk,v 1.1 2020/12/01 22:16:36 rillig Exp $
+#
+# Tests for indirect variable modifiers, such as in ${VAR:${M_modifiers}}.
+# These can be used for very basic purposes like converting a string to either
+# uppercase or lowercase, as well as for fairly advanced modifiers that first
+# look like line noise and are hard to decipher.
+#
+# TODO: Since when are indirect modifiers supported?
+
+
+# The nested variable expression expands to "tu", and this is interpreted as
+# a variable modifier for the value "Upper", resulting in "UPPER".
+.if ${Upper:L:${:Utu}} != "UPPER"
+. error
+.endif
+
+# The nested variable expression expands to "tl", and this is interpreted as
+# a variable modifier for the value "Lower", resulting in "lower".
+.if ${Lower:L:${:Utl}} != "lower"
+. error
+.endif
+
+
+# The nested variable expression is ${1 != 1:?Z:tl}, consisting of the
+# condition "1 != 1", the then-branch "Z" and the else-branch "tl". Since
+# the condition evaluates to false, the then-branch is ignored (it would
+# have been an unknown modifier anyway) and the ":tl" modifier is applied.
+.if ${Mixed:L:${1 != 1:?Z:tl}} != "mixed"
+. error
+.endif
+
+
+# The indirect modifier can also replace an ':L' modifier, which allows for
+# brain twisters since by reading the expression alone, it is not possible
+# to say whether the variable name will be evaluated as a variable name or
+# as the immediate value of the expression.
+VAR= value
+M_ExpandVar= # an empty modifier
+M_VarAsValue= L
+#
+.if ${VAR:${M_ExpandVar}} != "value"
+. error
+.endif
+.if ${VAR:${M_VarAsValue}} != "VAR"
+. error
+.endif
+
+# The indirect modifier M_ListToSkip, when applied to a list of patterns,
+# expands to a sequence of ':N' modifiers, each of which filters one of the
+# patterns. This list of patterns can then be applied to another variable
+# to actually filter that variable.
+#
+M_ListToSkip= @pat@N$${pat}@:ts:
+#
+# The dollar signs need to be doubled in the above modifier expression,
+# otherwise they would be expanded too early, that is, when parsing the
+# modifier itself.
+#
+# In the following example, M_NoPrimes expands to 'N2:N3:N5:N7:N1[1379]'.
+# The 'N' comes from the expression 'N${pat}', the separating colons come
+# from the modifier ':ts:'.
+#
+#.MAKEFLAGS: -dcv # Uncomment this line to see the details
+#
+PRIMES= 2 3 5 7 1[1379]
+M_NoPrimes= ${PRIMES:${M_ListToSkip}}
+.if ${:U:range=20:${M_NoPrimes}} != "1 4 6 8 9 10 12 14 15 16 18 20"
+. error
+.endif
+.MAKEFLAGS: -d0
+
+all: