From e3d2e555ae32d8705cbec042df0c18ea0941ea42 Mon Sep 17 00:00:00 2001 From: rillig Date: Sun, 15 Nov 2020 14:07:53 +0000 Subject: [PATCH] make(1): clean up some tests Fix some mistakes in comments. Improve some comments to be easier understandable. Inline variables that are irrelevant for the test at hand (RUN, MAKE_CMD). Remove references to functions and constants that have since been removed or renamed. --- usr.bin/make/unit-tests/archive-suffix.mk | 4 +-- usr.bin/make/unit-tests/archive.mk | 30 ++++++++-------- usr.bin/make/unit-tests/cmd-interrupt.mk | 4 +-- usr.bin/make/unit-tests/cmdline.mk | 11 +++--- usr.bin/make/unit-tests/comment.mk | 12 ++++--- usr.bin/make/unit-tests/cond-cmp-string.exp | 8 ++--- usr.bin/make/unit-tests/cond-cmp-string.mk | 17 ++++++--- usr.bin/make/unit-tests/cond-func-commands.mk | 5 +-- usr.bin/make/unit-tests/cond-func-defined.exp | 3 ++ usr.bin/make/unit-tests/cond-func-defined.mk | 14 +++++++- usr.bin/make/unit-tests/cond-func-empty.exp | 4 +-- usr.bin/make/unit-tests/cond-func-empty.mk | 36 ++++++++++--------- usr.bin/make/unit-tests/cond-func.exp | 24 ++++++------- usr.bin/make/unit-tests/cond-func.mk | 6 ++-- usr.bin/make/unit-tests/cond-late.mk | 12 ++++--- 15 files changed, 110 insertions(+), 80 deletions(-) diff --git a/usr.bin/make/unit-tests/archive-suffix.mk b/usr.bin/make/unit-tests/archive-suffix.mk index 3fddb46f3895..a216fd2d3c6a 100644 --- a/usr.bin/make/unit-tests/archive-suffix.mk +++ b/usr.bin/make/unit-tests/archive-suffix.mk @@ -1,11 +1,11 @@ -# $NetBSD: archive-suffix.mk,v 1.2 2020/11/07 00:07:02 rillig Exp $ +# $NetBSD: archive-suffix.mk,v 1.3 2020/11/15 14:07:53 rillig Exp $ # # Between 2020-08-23 and 2020-08-30, the below code produced an assertion # failure in Var_SetWithFlags, triggered by Compat_Make, when setting the # .IMPSRC of an archive node to its .TARGET. # # The code assumed that the .TARGET variable of every node would be set, but -# but that is not guaranteed. +# that is not guaranteed. # # Between 2016-03-15 and 2016-03-16 the behavior of the below code changed. # Until 2016-03-15, it remade the target, starting with 2016-03-16 it says diff --git a/usr.bin/make/unit-tests/archive.mk b/usr.bin/make/unit-tests/archive.mk index 2f91005f988c..f8815cf40a40 100644 --- a/usr.bin/make/unit-tests/archive.mk +++ b/usr.bin/make/unit-tests/archive.mk @@ -1,18 +1,15 @@ -# $NetBSD: archive.mk,v 1.10 2020/10/09 06:44:42 rillig Exp $ +# $NetBSD: archive.mk,v 1.11 2020/11/15 14:07:53 rillig Exp $ # # Very basic demonstration of handling archives, based on the description # in PSD.doc/tutorial.ms. # # This test aims at covering the code, not at being an introduction to -# archive handling. That's why it is more complicated and detailed than -# strictly necessary. +# archive handling. That's why it deviates from the tutorial style of +# several other tests. ARCHIVE= libprog.a FILES= archive.mk modmisc.mk varmisc.mk -MAKE_CMD= ${.MAKE} -f ${MAKEFILE} -RUN?= @set -eu; - all: .if ${.PARSEDIR:tA} != ${.CURDIR:tA} @cd ${MAKEFILE:H} && cp ${FILES} [at]*.mk ${.CURDIR} @@ -20,13 +17,13 @@ all: # The following targets create and remove files. The filesystem cache in # dir.c would probably not handle this correctly, therefore each of the # targets is run in its separate sub-make. - ${RUN} ${MAKE_CMD} remove-archive - ${RUN} ${MAKE_CMD} create-archive - ${RUN} ${MAKE_CMD} list-archive - ${RUN} ${MAKE_CMD} list-archive-wildcard - ${RUN} ${MAKE_CMD} depend-on-existing-member - ${RUN} ${MAKE_CMD} depend-on-nonexistent-member - ${RUN} ${MAKE_CMD} remove-archive + @${MAKE} -f ${MAKEFILE} remove-archive + @${MAKE} -f ${MAKEFILE} create-archive + @${MAKE} -f ${MAKEFILE} list-archive + @${MAKE} -f ${MAKEFILE} list-archive-wildcard + @${MAKE} -f ${MAKEFILE} depend-on-existing-member + @${MAKE} -f ${MAKEFILE} depend-on-nonexistent-member + @${MAKE} -f ${MAKEFILE} remove-archive create-archive: ${ARCHIVE} pre post @@ -43,15 +40,16 @@ list-archive: ${ARCHIVE} pre post # XXX: I had expected that this dependency would select all *.mk files from # the archive. Instead, the globbing is done in the current directory. +# # To prevent an overly long file list, the pattern is restricted to [at]*.mk. list-archive-wildcard: ${ARCHIVE}([at]*.mk) pre post - ${RUN} printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@} + @printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@} depend-on-existing-member: ${ARCHIVE}(archive.mk) pre post - ${RUN} echo $@ + @echo $@ depend-on-nonexistent-member: ${ARCHIVE}(nonexistent.mk) pre post - ${RUN} echo $@ + @echo $@ remove-archive: pre post rm -f ${ARCHIVE} diff --git a/usr.bin/make/unit-tests/cmd-interrupt.mk b/usr.bin/make/unit-tests/cmd-interrupt.mk index 033f3307bd2e..fa0d85fc9063 100644 --- a/usr.bin/make/unit-tests/cmd-interrupt.mk +++ b/usr.bin/make/unit-tests/cmd-interrupt.mk @@ -1,4 +1,4 @@ -# $NetBSD: cmd-interrupt.mk,v 1.2 2020/08/28 18:16:22 rillig Exp $ +# $NetBSD: cmd-interrupt.mk,v 1.3 2020/11/15 14:07:53 rillig Exp $ # # Tests for interrupting a command. # @@ -22,7 +22,7 @@ all: clean-before interrupt-ordinary interrupt-phony interrupt-precious clean-af clean-before clean-after: .PHONY @rm -f cmd-interrupt-ordinary cmd-interrupt-phony cmd-interrupt-precious -interrupt-ordinary: .PHONY +interrupt-ordinary: @${.MAKE} ${MAKEFLAGS} -f ${MAKEFILE} cmd-interrupt-ordinary || true # The ././ is necessary to work around the file cache. @echo ${.TARGET}: ${exists(././cmd-interrupt-ordinary) :? error : ok } diff --git a/usr.bin/make/unit-tests/cmdline.mk b/usr.bin/make/unit-tests/cmdline.mk index c12c31220cb5..cd88cead4558 100644 --- a/usr.bin/make/unit-tests/cmdline.mk +++ b/usr.bin/make/unit-tests/cmdline.mk @@ -1,8 +1,7 @@ -# $NetBSD: cmdline.mk,v 1.1 2020/07/28 22:44:44 rillig Exp $ +# $NetBSD: cmdline.mk,v 1.2 2020/11/15 14:07:53 rillig Exp $ # # Tests for command line parsing and related special variables. -RUN?= @set -eu; TMPBASE?= /tmp SUB1= a7b41170-53f8-4cc2-bc5c-e4c3dd93ec45 # just a random UUID SUB2= 6a8899d2-d227-4b55-9b6b-f3c8eeb83fd5 # just a random UUID @@ -14,14 +13,14 @@ all: prepare-dirs all: makeobjdir-direct makeobjdir-indirect prepare-dirs: - ${RUN} rm -rf ${DIR2} ${DIR12} - ${RUN} mkdir -p ${DIR2} ${DIR12} + @rm -rf ${DIR2} ${DIR12} + @mkdir -p ${DIR2} ${DIR12} # The .OBJDIR can be set via the MAKEOBJDIR command line variable. # It must be a command line variable; an environment variable would not work. makeobjdir-direct: @echo $@: - ${RUN} ${MAKE_CMD} MAKEOBJDIR=${DIR2} show-objdir + @${MAKE_CMD} MAKEOBJDIR=${DIR2} show-objdir # The .OBJDIR can be set via the MAKEOBJDIR command line variable, # and that variable could even contain the usual modifiers. @@ -31,7 +30,7 @@ makeobjdir-direct: # see MAKE_CMD. makeobjdir-indirect: @echo $@: - ${RUN} ${MAKE_CMD} MAKEOBJDIR='$${TMPBASE}/$${SUB2}' show-objdir + @${MAKE_CMD} MAKEOBJDIR='$${TMPBASE}/$${SUB2}' show-objdir show-objdir: @echo $@: ${.OBJDIR:Q} diff --git a/usr.bin/make/unit-tests/comment.mk b/usr.bin/make/unit-tests/comment.mk index 1cdcfcdd86ba..d4fb041104a7 100644 --- a/usr.bin/make/unit-tests/comment.mk +++ b/usr.bin/make/unit-tests/comment.mk @@ -1,4 +1,4 @@ -# $NetBSD: comment.mk,v 1.2 2020/09/07 19:17:36 rillig Exp $ +# $NetBSD: comment.mk,v 1.3 2020/11/15 14:07:53 rillig Exp $ # # Demonstrate how comments are written in makefiles. @@ -12,7 +12,7 @@ that \ goes \ on and on. - # Comments can be indented, but that is rather unusual. + # Comments can be indented with spaces, but that is rather unusual. # Comments can be indented with a tab. # These are not shell commands, they are just makefile comments. @@ -21,6 +21,8 @@ on and on. .endif # And after the closing directive. VAR= # This comment makes the variable value empty. + # ParseGetLine removes any whitespace before the + # comment. .if ${VAR} != "" . error .endif @@ -35,7 +37,9 @@ VAR= value . error .endif -# This is NOT an escaped comment due to the double backslashes \\ +# This comment ends with 2 backslashes. An even number of backslashes does +# not count as a line continuation, therefore the variable assignment that +# follows is actively interpreted. \\ VAR= not part of the comment .if ${VAR} != "not part of the comment" . error @@ -55,7 +59,7 @@ WORDS= ${VAR:[#]} [# . error .endif -# An odd number of comment signs makes a line continuation, \\\ +# An odd number of backslashes makes a line continuation, \\\ no matter if it is 3 or 5 \\\\\ or 9 backslashes. \\\\\\\\\ This is the last line of the comment. diff --git a/usr.bin/make/unit-tests/cond-cmp-string.exp b/usr.bin/make/unit-tests/cond-cmp-string.exp index 735b7cda4430..6e6c218267bc 100644 --- a/usr.bin/make/unit-tests/cond-cmp-string.exp +++ b/usr.bin/make/unit-tests/cond-cmp-string.exp @@ -1,8 +1,8 @@ make: "cond-cmp-string.mk" line 18: Malformed conditional (str != str) -make: "cond-cmp-string.mk" line 37: Malformed conditional ("string" != "str""ing") -make: "cond-cmp-string.mk" line 42: warning: String comparison operator must be either == or != -make: "cond-cmp-string.mk" line 42: Malformed conditional (!("value" = "value")) -make: "cond-cmp-string.mk" line 49: Malformed conditional (!("value" === "value")) +make: "cond-cmp-string.mk" line 42: Malformed conditional ("string" != "str""ing") +make: "cond-cmp-string.mk" line 49: warning: String comparison operator must be either == or != +make: "cond-cmp-string.mk" line 49: Malformed conditional (!("value" = "value")) +make: "cond-cmp-string.mk" line 56: Malformed conditional (!("value" === "value")) make: Fatal errors encountered -- cannot continue make: stopped in unit-tests exit status 1 diff --git a/usr.bin/make/unit-tests/cond-cmp-string.mk b/usr.bin/make/unit-tests/cond-cmp-string.mk index 00d47045e49c..6b5cba83bef7 100644 --- a/usr.bin/make/unit-tests/cond-cmp-string.mk +++ b/usr.bin/make/unit-tests/cond-cmp-string.mk @@ -1,4 +1,4 @@ -# $NetBSD: cond-cmp-string.mk,v 1.12 2020/11/08 23:00:09 rillig Exp $ +# $NetBSD: cond-cmp-string.mk,v 1.13 2020/11/15 14:07:53 rillig Exp $ # # Tests for string comparisons in .if conditions. @@ -19,9 +19,14 @@ . error .endif -# The left-hand side of the comparison requires a defined variable. -# The variable named "" is not defined, but applying the :U modifier to it -# makes it "kind of defined" (see VAR_KEEP). Therefore it is ok here. +# The left-hand side of the comparison requires that any variable expression +# is defined. +# +# The variable named "" is never defined, nevertheless it can be used as a +# starting point for variable expressions. Applying the :U modifier to such +# an undefined expression turns it into a defined expression. +# +# See ApplyModifier_Defined and VEF_DEF. .if ${:Ustr} != "str" . error .endif @@ -33,9 +38,11 @@ .endif # It is not possible to concatenate two string literals to form a single -# string. +# string. In C, Python and the shell this is possible, but not in make. .if "string" != "str""ing" . error +.else +. error .endif # There is no = operator for strings. diff --git a/usr.bin/make/unit-tests/cond-func-commands.mk b/usr.bin/make/unit-tests/cond-func-commands.mk index c6e1724c72f3..e127a8ebdc03 100644 --- a/usr.bin/make/unit-tests/cond-func-commands.mk +++ b/usr.bin/make/unit-tests/cond-func-commands.mk @@ -1,10 +1,11 @@ -# $NetBSD: cond-func-commands.mk,v 1.4 2020/10/24 08:46:08 rillig Exp $ +# $NetBSD: cond-func-commands.mk,v 1.5 2020/11/15 14:07:53 rillig Exp $ # # Tests for the commands() function in .if conditions. .MAIN: all -# The target "target" does not exist yet, therefore it cannot have commands. +# At this point, the target 'target' does not exist yet, therefore it cannot +# have commands. Sounds obvious, but good to know that it is really so. .if commands(target) . error .endif diff --git a/usr.bin/make/unit-tests/cond-func-defined.exp b/usr.bin/make/unit-tests/cond-func-defined.exp index d2e7a68bfea8..caf66e39938f 100644 --- a/usr.bin/make/unit-tests/cond-func-defined.exp +++ b/usr.bin/make/unit-tests/cond-func-defined.exp @@ -2,6 +2,9 @@ make: "cond-func-defined.mk" line 23: warning: Missing closing parenthesis for d make: "cond-func-defined.mk" line 23: Malformed conditional (!defined(A B)) make: "cond-func-defined.mk" line 33: warning: Missing closing parenthesis for defined() make: "cond-func-defined.mk" line 33: Malformed conditional (defined(DEF) +make: "cond-func-defined.mk" line 45: In .for loops, variable expressions for the loop variables are +make: "cond-func-defined.mk" line 46: substituted at evaluation time. There is no actual variable +make: "cond-func-defined.mk" line 47: involved, even if it feels like it. make: Fatal errors encountered -- cannot continue make: stopped in unit-tests exit status 1 diff --git a/usr.bin/make/unit-tests/cond-func-defined.mk b/usr.bin/make/unit-tests/cond-func-defined.mk index af04bbbc04e5..2aa49ccbf147 100644 --- a/usr.bin/make/unit-tests/cond-func-defined.mk +++ b/usr.bin/make/unit-tests/cond-func-defined.mk @@ -1,4 +1,4 @@ -# $NetBSD: cond-func-defined.mk,v 1.6 2020/11/07 14:40:51 rillig Exp $ +# $NetBSD: cond-func-defined.mk,v 1.7 2020/11/15 14:07:53 rillig Exp $ # # Tests for the defined() function in .if conditions. @@ -36,5 +36,17 @@ ${:UA B}= variable name with spaces . error .endif +# Variables from .for loops are not defined. +# See directive-for.mk for more details. +.for var in value +. if defined(var) +. error +. else +. info In .for loops, variable expressions for the loop variables are +. info substituted at evaluation time. There is no actual variable +. info involved, even if it feels like it. +. endif +.endfor + all: @:; diff --git a/usr.bin/make/unit-tests/cond-func-empty.exp b/usr.bin/make/unit-tests/cond-func-empty.exp index af0f44fb8c6c..77a4edd47f49 100644 --- a/usr.bin/make/unit-tests/cond-func-empty.exp +++ b/usr.bin/make/unit-tests/cond-func-empty.exp @@ -1,5 +1,5 @@ -make: "cond-func-empty.mk" line 150: Unclosed variable "WORD" -make: "cond-func-empty.mk" line 150: Malformed conditional (empty(WORD) +make: "cond-func-empty.mk" line 152: Unclosed variable "WORD" +make: "cond-func-empty.mk" line 152: Malformed conditional (empty(WORD) make: Fatal errors encountered -- cannot continue make: stopped in unit-tests exit status 1 diff --git a/usr.bin/make/unit-tests/cond-func-empty.mk b/usr.bin/make/unit-tests/cond-func-empty.mk index 4b4f0612b450..f43d99bf92c5 100644 --- a/usr.bin/make/unit-tests/cond-func-empty.mk +++ b/usr.bin/make/unit-tests/cond-func-empty.mk @@ -1,11 +1,10 @@ -# $NetBSD: cond-func-empty.mk,v 1.9 2020/11/07 14:37:31 rillig Exp $ +# $NetBSD: cond-func-empty.mk,v 1.10 2020/11/15 14:07:53 rillig Exp $ # # Tests for the empty() function in .if conditions, which tests a variable # expression for emptiness. # # Note that the argument in the parentheses is indeed a variable name, -# optionally followed by variable modifiers. This is like the defined() -# function. +# optionally followed by variable modifiers. # .undef UNDEF @@ -25,13 +24,15 @@ WORD= word . error .endif -# The :S modifier replaces the empty value with an actual word, and -# after that the expression is no longer empty. Because the variable -# was undefined in the first place, the expression has the flag VAR_JUNK -# but not VAR_KEEP, therefore it is still considered undefined. -# Only very few variable modifiers turn an undefined variable expression -# into a defined variable expression. The :U and :D modifiers belong to -# that group, but :S doesn't (see VAR_KEEP). +# The :S modifier replaces the empty value with an actual word. The +# expression is now no longer empty, but it is still possible to see whether +# the expression was based on an undefined variable. The expression has the +# flag VEF_UNDEF. +# +# The expression does not have the flag VEF_DEF though, therefore it is still +# considered undefined. Yes, indeed, undefined but not empty. There are a +# few variable modifiers that turn an undefined expression into a defined +# expression, among them :U and :D, but not :S. # # XXX: This is hard to explain to someone who doesn't know these # implementation details. @@ -49,13 +50,14 @@ WORD= word .endif # And now to the surprising part. Applying the following :S modifier to the -# undefined variable makes it non-empty, but the marker VAR_JUNK is preserved -# nevertheless. The :U modifier that follows only looks at VAR_JUNK to decide -# whether the variable is defined or not. This kind of makes sense since the -# :U modifier tests the _variable_, not the _expression_. +# undefined expression makes it non-empty, but the marker VEF_UNDEF is +# preserved nevertheless. The :U modifier that follows only looks at the +# VEF_UNDEF flag to decide whether the variable is defined or not. This kind +# of makes sense since the :U modifier tests the _variable_, not the +# _expression_. # -# But since the variable was undefined to begin with, the fallback value is -# used in this expression. +# But since the variable was undefined to begin with, the fallback value from +# the :U modifier is used in this expression. # .if ${UNDEF:S,^$,value,W:Ufallback} != "fallback" . error @@ -128,7 +130,7 @@ ${:U }= space # If everything goes well, the argument expands to "WORD", and that variable # is defined at the beginning of this file. The surrounding 'W' and 'D' # ensure that the parser in ParseEmptyArg has the correct position, both -# before and after the call to Var_ParsePP. +# before and after the call to Var_Parse. .if empty(W${:UOR}D) . error .endif diff --git a/usr.bin/make/unit-tests/cond-func.exp b/usr.bin/make/unit-tests/cond-func.exp index e434c7cc7996..73b6273d0b09 100644 --- a/usr.bin/make/unit-tests/cond-func.exp +++ b/usr.bin/make/unit-tests/cond-func.exp @@ -1,15 +1,15 @@ -make: "cond-func.mk" line 34: warning: Missing closing parenthesis for defined() -make: "cond-func.mk" line 34: Malformed conditional (!defined(A B)) -make: "cond-func.mk" line 49: warning: Missing closing parenthesis for defined() -make: "cond-func.mk" line 49: Malformed conditional (!defined(A&B)) -make: "cond-func.mk" line 52: warning: Missing closing parenthesis for defined() -make: "cond-func.mk" line 52: Malformed conditional (!defined(A|B)) -make: "cond-func.mk" line 92: The empty variable is never defined. -make: "cond-func.mk" line 100: A plain function name is parsed as !empty(...). -make: "cond-func.mk" line 107: A plain function name is parsed as !empty(...). -make: "cond-func.mk" line 117: Symbols may start with a function name. -make: "cond-func.mk" line 122: Symbols may start with a function name. -make: "cond-func.mk" line 128: Malformed conditional (defined() +make: "cond-func.mk" line 36: warning: Missing closing parenthesis for defined() +make: "cond-func.mk" line 36: Malformed conditional (!defined(A B)) +make: "cond-func.mk" line 51: warning: Missing closing parenthesis for defined() +make: "cond-func.mk" line 51: Malformed conditional (!defined(A&B)) +make: "cond-func.mk" line 54: warning: Missing closing parenthesis for defined() +make: "cond-func.mk" line 54: Malformed conditional (!defined(A|B)) +make: "cond-func.mk" line 94: The empty variable is never defined. +make: "cond-func.mk" line 102: A plain function name is parsed as !empty(...). +make: "cond-func.mk" line 109: A plain function name is parsed as !empty(...). +make: "cond-func.mk" line 119: Symbols may start with a function name. +make: "cond-func.mk" line 124: Symbols may start with a function name. +make: "cond-func.mk" line 130: Malformed conditional (defined() make: Fatal errors encountered -- cannot continue make: stopped in unit-tests exit status 1 diff --git a/usr.bin/make/unit-tests/cond-func.mk b/usr.bin/make/unit-tests/cond-func.mk index cba485002ae8..4ff43b72ef88 100644 --- a/usr.bin/make/unit-tests/cond-func.mk +++ b/usr.bin/make/unit-tests/cond-func.mk @@ -1,10 +1,12 @@ -# $NetBSD: cond-func.mk,v 1.8 2020/11/10 20:44:18 rillig Exp $ +# $NetBSD: cond-func.mk,v 1.9 2020/11/15 14:07:53 rillig Exp $ # # Tests for those parts of the functions in .if conditions that are common # among several functions. # # The below test uses the function defined(...) since it has no side-effects, -# the other functions (except empty(...)) would work equally well. +# the other functions (except empty(...)) would work equally well. The +# function empty is special because it uses a different parsing algorithm for +# its argument. DEF= defined ${:UA B}= variable name with spaces diff --git a/usr.bin/make/unit-tests/cond-late.mk b/usr.bin/make/unit-tests/cond-late.mk index 397f5febd480..4df3df2cf1d4 100644 --- a/usr.bin/make/unit-tests/cond-late.mk +++ b/usr.bin/make/unit-tests/cond-late.mk @@ -1,7 +1,9 @@ -# $NetBSD: cond-late.mk,v 1.2 2020/07/25 20:37:46 rillig Exp $ +# $NetBSD: cond-late.mk,v 1.3 2020/11/15 14:07:53 rillig Exp $ # # Using the :? modifier, variable expressions can contain conditional -# expressions that are evaluated late. Any variables appearing in these +# expressions that are evaluated late, at expansion time. +# +# Any variables appearing in these # conditions are expanded before parsing the condition. This is # different from many other places. # @@ -11,15 +13,15 @@ # They should also not contain operators like == or <, since these are # actually interpreted as these operators. This is demonstrated below. # -# If the order of evaluation were to change to first parse the condition -# and then expand the variables, the output would change from the -# current "yes no" to "yes yes", since both variables are non-empty. all: cond-literal COND.true= "yes" == "yes" COND.false= "yes" != "yes" +# If the order of evaluation were to change to first parse the condition +# and then expand the variables, the output would change from the +# current "yes no" to "yes yes", since both variables are non-empty. cond-literal: @echo ${ ${COND.true} :?yes:no} @echo ${ ${COND.false} :?yes:no}