Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/make/unit-tests make(1): add test that explains how ...
details: https://anonhg.NetBSD.org/src/rev/7924395201a1
branches: trunk
changeset: 958223:7924395201a1
user: rillig <rillig%NetBSD.org@localhost>
date: Tue Dec 29 01:45:06 2020 +0000
description:
make(1): add test that explains how variables are exported
Exporting the variables at the right time and with the correct values is
a subtle issue. The current implementation carefully marks variables as
ready to be exported, then exports them and at the same time tries to
export as few variables as possible, to avoid memory leaks. This test
describes and explains how all this works in detail.
This test also justifies that the call to Var_ReexportVars happens in
the make process itself, not in the child processes, no matter whether
these are created with vfork or (only theoretically) with plain fork.
This has changed in compat.c 1.217, job.c 1.390 and main.c 1.504 from
2020-12-27.
diffstat:
distrib/sets/lists/tests/mi | 4 +-
usr.bin/make/unit-tests/Makefile | 3 +-
usr.bin/make/unit-tests/directive-export-impl.exp | 56 ++++++++++++++++++++
usr.bin/make/unit-tests/directive-export-impl.mk | 62 +++++++++++++++++++++++
4 files changed, 123 insertions(+), 2 deletions(-)
diffs (161 lines):
diff -r de40aef00006 -r 7924395201a1 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi Tue Dec 29 00:26:51 2020 +0000
+++ b/distrib/sets/lists/tests/mi Tue Dec 29 01:45:06 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.999 2020/12/28 12:47:39 rillig Exp $
+# $NetBSD: mi,v 1.1000 2020/12/29 01:45:06 rillig Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -5050,6 +5050,8 @@
./usr/tests/usr.bin/make/unit-tests/directive-export-env.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/directive-export-gmake.exp tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/directive-export-gmake.mk tests-usr.bin-tests compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/directive-export-impl.exp tests-usr.bin-tests compattestfile,atf
+./usr/tests/usr.bin/make/unit-tests/directive-export-impl.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/directive-export-literal.exp tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/directive-export-literal.mk tests-usr.bin-tests compattestfile,atf
./usr/tests/usr.bin/make/unit-tests/directive-export.exp tests-usr.bin-tests compattestfile,atf
diff -r de40aef00006 -r 7924395201a1 usr.bin/make/unit-tests/Makefile
--- a/usr.bin/make/unit-tests/Makefile Tue Dec 29 00:26:51 2020 +0000
+++ b/usr.bin/make/unit-tests/Makefile Tue Dec 29 01:45:06 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.257 2020/12/27 05:11:40 rillig Exp $
+# $NetBSD: Makefile,v 1.258 2020/12/29 01:45:06 rillig Exp $
#
# Unit tests for make(1)
#
@@ -157,6 +157,7 @@
TESTS+= directive-error
TESTS+= directive-export
TESTS+= directive-export-env
+TESTS+= directive-export-impl
TESTS+= directive-export-gmake
TESTS+= directive-export-literal
TESTS+= directive-for
diff -r de40aef00006 -r 7924395201a1 usr.bin/make/unit-tests/directive-export-impl.exp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/make/unit-tests/directive-export-impl.exp Tue Dec 29 01:45:06 2020 +0000
@@ -0,0 +1,56 @@
+ParseReadLine (21): 'UT_VAR= <${REF}>'
+Global:UT_VAR = <${REF}>
+ParseReadLine (28): '.export UT_VAR'
+Global:.MAKE.EXPORTED = UT_VAR
+ParseReadLine (32): ': ${UT_VAR:N*}'
+Var_Parse: ${UT_VAR:N*} with VARE_UNDEFERR|VARE_WANTRES
+Var_Parse: ${REF}> with VARE_UNDEFERR|VARE_WANTRES
+Applying ${UT_VAR:N...} to "<>" (VARE_UNDEFERR|VARE_WANTRES, VAR_EXPORTED|VAR_REEXPORT, none)
+Pattern[UT_VAR] for [<>] is [*]
+ModifyWords: split "<>" into 1 words
+Result of ${UT_VAR:N*} is "" (VARE_UNDEFERR|VARE_WANTRES, VAR_EXPORTED|VAR_REEXPORT, none)
+ParseDoDependency(: )
+CondParser_Eval: ${:!echo "\$UT_VAR"!} != "<>"
+Var_Parse: ${:!echo "\$UT_VAR"!} != "<>" with VARE_UNDEFERR|VARE_WANTRES
+Applying ${:!...} to "" (VARE_UNDEFERR|VARE_WANTRES, none, VEF_UNDEF)
+Modifier part: "echo "$UT_VAR""
+Var_Parse: ${.MAKE.EXPORTED:O:u} with VARE_WANTRES
+Applying ${.MAKE.EXPORTED:O} to "UT_VAR" (VARE_WANTRES, none, none)
+Result of ${.MAKE.EXPORTED:O} is "UT_VAR" (VARE_WANTRES, none, none)
+Applying ${.MAKE.EXPORTED:u} to "UT_VAR" (VARE_WANTRES, none, none)
+Result of ${.MAKE.EXPORTED:u} is "UT_VAR" (VARE_WANTRES, none, none)
+Var_Parse: ${UT_VAR} with VARE_WANTRES
+Var_Parse: ${REF}> with VARE_WANTRES
+Result of ${:!echo "\$UT_VAR"!} is "<>" (VARE_UNDEFERR|VARE_WANTRES, none, VEF_UNDEF|VEF_DEF)
+lhs = "<>", rhs = "<>", op = !=
+ParseReadLine (49): ': ${UT_VAR:N*}'
+Var_Parse: ${UT_VAR:N*} with VARE_UNDEFERR|VARE_WANTRES
+Var_Parse: ${REF}> with VARE_UNDEFERR|VARE_WANTRES
+Applying ${UT_VAR:N...} to "<>" (VARE_UNDEFERR|VARE_WANTRES, VAR_EXPORTED|VAR_REEXPORT, none)
+Pattern[UT_VAR] for [<>] is [*]
+ModifyWords: split "<>" into 1 words
+Result of ${UT_VAR:N*} is "" (VARE_UNDEFERR|VARE_WANTRES, VAR_EXPORTED|VAR_REEXPORT, none)
+ParseDoDependency(: )
+ParseReadLine (53): 'REF= defined'
+Global:REF = defined
+CondParser_Eval: ${:!echo "\$UT_VAR"!} != "<defined>"
+Var_Parse: ${:!echo "\$UT_VAR"!} != "<defined>" with VARE_UNDEFERR|VARE_WANTRES
+Applying ${:!...} to "" (VARE_UNDEFERR|VARE_WANTRES, none, VEF_UNDEF)
+Modifier part: "echo "$UT_VAR""
+Var_Parse: ${.MAKE.EXPORTED:O:u} with VARE_WANTRES
+Applying ${.MAKE.EXPORTED:O} to "UT_VAR" (VARE_WANTRES, none, none)
+Result of ${.MAKE.EXPORTED:O} is "UT_VAR" (VARE_WANTRES, none, none)
+Applying ${.MAKE.EXPORTED:u} to "UT_VAR" (VARE_WANTRES, none, none)
+Result of ${.MAKE.EXPORTED:u} is "UT_VAR" (VARE_WANTRES, none, none)
+Var_Parse: ${UT_VAR} with VARE_WANTRES
+Var_Parse: ${REF}> with VARE_WANTRES
+Result of ${:!echo "\$UT_VAR"!} is "<defined>" (VARE_UNDEFERR|VARE_WANTRES, none, VEF_UNDEF|VEF_DEF)
+lhs = "<defined>", rhs = "<defined>", op = !=
+ParseReadLine (61): 'all:'
+ParseDoDependency(all:)
+Global:.ALLTARGETS = all
+ParseReadLine (62): '.MAKEFLAGS: -d0'
+ParseDoDependency(.MAKEFLAGS: -d0)
+Global:.MAKEFLAGS = -r -k -d cpv -d
+Global:.MAKEFLAGS = -r -k -d cpv -d 0
+exit status 0
diff -r de40aef00006 -r 7924395201a1 usr.bin/make/unit-tests/directive-export-impl.mk
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/make/unit-tests/directive-export-impl.mk Tue Dec 29 01:45:06 2020 +0000
@@ -0,0 +1,62 @@
+# $NetBSD: directive-export-impl.mk,v 1.1 2020/12/29 01:45:06 rillig Exp $
+#
+# Test for the implementation of exporting variables to child processes.
+# This involves marking variables for export, actually exporting them,
+# or marking them for being re-exported.
+#
+# See also:
+# Var_Export
+# ExportVar
+# VarExportedMode (global)
+# VAR_EXPORTED (per variable)
+# VAR_REEXPORT (per variable)
+# VarExportMode (per call of Var_Export and ExportVar)
+
+: ${:U:sh} # side effect: initialize .SHELL
+
+.MAKEFLAGS: -dcpv
+
+# This is a variable that references another variable. At this point, the
+# other variable is still undefined.
+UT_VAR= <${REF}>
+
+# At this point, ExportVar("UT_VAR", VEM_PLAIN) is called. Since the
+# variable value refers to another variable, ExportVar does not actually
+# export the variable but only marks it as VAR_EXPORTED and VAR_REEXPORT.
+# After that, ExportVars registers the variable name in .MAKE.EXPORTED.
+# That's all for now.
+.export UT_VAR
+
+# Evaluating this expression shows the variable flags in the debug log,
+# which are VAR_EXPORTED|VAR_REEXPORT.
+: ${UT_VAR:N*}
+
+# At the last moment before actually forking off the child process for the
+# :!...! modifier, Cmd_Exec calls Var_ReexportVars to have all relevant
+# variables exported. Since this variable has both of the above-mentioned
+# flags set, it is actually exported to the environment. The variable flags
+# are not modified though, since the next time the :!...! modifier is
+# evaluated, the referenced variables could have changed, therefore the
+# variable will be exported anew for each ':sh' modifier, ':!...!' modifier,
+# '!=' variable assignment.
+.if ${:!echo "\$UT_VAR"!} != "<>"
+. error
+.endif
+
+# Evaluating this expression shows the variable flags in the debug log,
+# which are still VAR_EXPORTED|VAR_REEXPORT, which means that the variable
+# is still marked as being re-exported for each child process.
+: ${UT_VAR:N*}
+
+# Now the referenced variable gets defined. This does not influence anything
+# in the process of exporting the variable value, though.
+REF= defined
+
+# Nothing surprising here. The variable UT_VAR gets exported, and this time,
+# REF is defined and gets expanded into the exported environment variable.
+.if ${:!echo "\$UT_VAR"!} != "<defined>"
+. error
+.endif
+
+all:
+.MAKEFLAGS: -d0
Home |
Main Index |
Thread Index |
Old Index