xref: /freebsd-src/contrib/bmake/unit-tests/directive-export-impl.mk (revision b0c40a00a67f611868fc0f10bde6b28eb75931be)
1*b0c40a00SSimon J. Gerraty# $NetBSD: directive-export-impl.mk,v 1.3 2021/04/03 23:08:30 rillig Exp $
206b9b3e0SSimon J. Gerraty#
306b9b3e0SSimon J. Gerraty# Test for the implementation of exporting variables to child processes.
406b9b3e0SSimon J. Gerraty# This involves marking variables for export, actually exporting them,
506b9b3e0SSimon J. Gerraty# or marking them for being re-exported.
606b9b3e0SSimon J. Gerraty#
706b9b3e0SSimon J. Gerraty# See also:
806b9b3e0SSimon J. Gerraty#	Var_Export
906b9b3e0SSimon J. Gerraty#	ExportVar
1006b9b3e0SSimon J. Gerraty#	VarExportedMode (global)
11*b0c40a00SSimon J. Gerraty#	VarFlags.exported (per variable)
12*b0c40a00SSimon J. Gerraty#	VarFlags.reexport (per variable)
1306b9b3e0SSimon J. Gerraty#	VarExportMode (per call of Var_Export and ExportVar)
1406b9b3e0SSimon J. Gerraty
1506b9b3e0SSimon J. Gerraty: ${:U:sh}			# side effect: initialize .SHELL
1606b9b3e0SSimon J. Gerraty
1706b9b3e0SSimon J. Gerraty.MAKEFLAGS: -dcpv
1806b9b3e0SSimon J. Gerraty
1906b9b3e0SSimon J. Gerraty# This is a variable that references another variable.  At this point, the
2006b9b3e0SSimon J. Gerraty# other variable is still undefined.
2106b9b3e0SSimon J. GerratyUT_VAR=		<${REF}>
2206b9b3e0SSimon J. Gerraty
2306b9b3e0SSimon J. Gerraty# At this point, ExportVar("UT_VAR", VEM_PLAIN) is called.  Since the
2406b9b3e0SSimon J. Gerraty# variable value refers to another variable, ExportVar does not actually
25*b0c40a00SSimon J. Gerraty# export the variable but only marks it as VarFlags.exported and
26*b0c40a00SSimon J. Gerraty# VarFlags.reexport.  After that, ExportVars registers the variable name in
27*b0c40a00SSimon J. Gerraty# .MAKE.EXPORTED.  That's all for now.
2806b9b3e0SSimon J. Gerraty.export UT_VAR
2906b9b3e0SSimon J. Gerraty
30*b0c40a00SSimon J. Gerraty# The following expression has both flags 'exported' and 'reexport' set.
31*b0c40a00SSimon J. Gerraty# These flags do not show up anywhere, not even in the debug log.
3206b9b3e0SSimon J. Gerraty: ${UT_VAR:N*}
3306b9b3e0SSimon J. Gerraty
3406b9b3e0SSimon J. Gerraty# At the last moment before actually forking off the child process for the
3506b9b3e0SSimon J. Gerraty# :!...! modifier, Cmd_Exec calls Var_ReexportVars to have all relevant
3606b9b3e0SSimon J. Gerraty# variables exported.  Since this variable has both of the above-mentioned
3706b9b3e0SSimon J. Gerraty# flags set, it is actually exported to the environment.  The variable flags
3806b9b3e0SSimon J. Gerraty# are not modified though, since the next time the :!...! modifier is
3906b9b3e0SSimon J. Gerraty# evaluated, the referenced variables could have changed, therefore the
4006b9b3e0SSimon J. Gerraty# variable will be exported anew for each ':sh' modifier, ':!...!' modifier,
4106b9b3e0SSimon J. Gerraty# '!=' variable assignment.
4206b9b3e0SSimon J. Gerraty.if ${:!echo "\$UT_VAR"!} != "<>"
4306b9b3e0SSimon J. Gerraty.  error
4406b9b3e0SSimon J. Gerraty.endif
4506b9b3e0SSimon J. Gerraty
46*b0c40a00SSimon J. Gerraty# The following expression still has 'exported' and 'reexport' set.
47*b0c40a00SSimon J. Gerraty# These flags do not show up anywhere though, not even in the debug log.
48*b0c40a00SSimon J. Gerraty# These flags means that the variable is still marked as being re-exported
49*b0c40a00SSimon J. Gerraty# for each child process.
5006b9b3e0SSimon J. Gerraty: ${UT_VAR:N*}
5106b9b3e0SSimon J. Gerraty
5206b9b3e0SSimon J. Gerraty# Now the referenced variable gets defined.  This does not influence anything
5306b9b3e0SSimon J. Gerraty# in the process of exporting the variable value, though.
5406b9b3e0SSimon J. GerratyREF=		defined
5506b9b3e0SSimon J. Gerraty
5606b9b3e0SSimon J. Gerraty# Nothing surprising here.  The variable UT_VAR gets exported, and this time,
5706b9b3e0SSimon J. Gerraty# REF is defined and gets expanded into the exported environment variable.
5806b9b3e0SSimon J. Gerraty.if ${:!echo "\$UT_VAR"!} != "<defined>"
5906b9b3e0SSimon J. Gerraty.  error
6006b9b3e0SSimon J. Gerraty.endif
6106b9b3e0SSimon J. Gerraty
6206b9b3e0SSimon J. Gerratyall:
6306b9b3e0SSimon J. Gerraty.MAKEFLAGS: -d0
64