Home | History | Annotate | Line # | Download | only in unit-tests
varmod-assign.mk revision 1.20.2.1
      1  1.20.2.1  perseant # $NetBSD: varmod-assign.mk,v 1.20.2.1 2024/07/01 01:01:15 perseant Exp $
      2       1.1    rillig #
      3       1.2    rillig # Tests for the obscure ::= variable modifiers, which perform variable
      4       1.2    rillig # assignments during evaluation, just like the = operator in C.
      5       1.1    rillig 
      6      1.19    rillig .if !make(target)
      7      1.19    rillig 
      8       1.4    rillig all:	mod-assign-empty
      9       1.4    rillig all:	mod-assign-parse
     10       1.5    rillig all:	mod-assign-shell-error
     11       1.1    rillig 
     12      1.17    rillig # In the following loop expression,
     13      1.17    rillig # the '::?=' modifier applies the assignment operator '?=' 3 times. The
     14      1.13    rillig # operator '?=' only has an effect for the first time, therefore the variable
     15      1.13    rillig # FIRST ends up with the value 1.
     16      1.13    rillig .if "${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}" != " first=1"
     17      1.13    rillig .  error
     18      1.13    rillig .endif
     19      1.13    rillig 
     20      1.17    rillig # In the following loop expression,
     21      1.17    rillig # the modifier '::=' applies the assignment operator '=' 3 times. The
     22      1.13    rillig # operator '=' overwrites the previous value, therefore the variable LAST ends
     23      1.13    rillig # up with the value 3.
     24      1.13    rillig .if "${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}" != " last=3"
     25      1.13    rillig .  error
     26      1.13    rillig .endif
     27      1.13    rillig 
     28      1.17    rillig # In the following loop expression,
     29      1.17    rillig # the modifier '::+=' applies the assignment operator '+=' 3 times. The
     30      1.13    rillig # operator '+=' appends 3 times to the variable, therefore the variable
     31      1.13    rillig # APPENDED ends up with the value "1 2 3".
     32      1.13    rillig .if "${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}" != " appended=1 2 3"
     33      1.13    rillig .  error
     34      1.13    rillig .endif
     35      1.13    rillig 
     36      1.17    rillig # In the following loop expression,
     37      1.17    rillig # the modifier '::!=' applies the assignment operator '!=' 3 times. Just as
     38      1.13    rillig # with the modifier '::=', the last value is stored in the RAN variable.
     39      1.13    rillig .if "${1 2 3:L:@i@${RAN::!=${i:%=echo '<%>';}}@} ran=${RAN}" != " ran=<3>"
     40      1.13    rillig .  error
     41      1.13    rillig .endif
     42      1.13    rillig 
     43      1.17    rillig # When a '::=' modifier is evaluated as part of an .if condition, it happens
     44      1.15    rillig # in the command line scope.
     45      1.13    rillig .if "${FIRST}, ${LAST}, ${APPENDED}, ${RAN}" != "1, 3, 1 2 3, <3>"
     46      1.13    rillig .  error
     47      1.13    rillig .endif
     48      1.13    rillig 
     49      1.13    rillig # Tests for nested assignments, which are hard to read and therefore seldom
     50      1.13    rillig # used in practice.
     51      1.13    rillig 
     52      1.13    rillig # The condition "1" is true, therefore THEN1 gets assigned a value,
     53      1.14    rillig # and the inner IT1 as well.  Nothing surprising here.
     54      1.14    rillig .if "${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}} ${THEN1}${ELSE1}${IT1}${IE1}" != " then1t1"
     55      1.13    rillig .  error
     56      1.13    rillig .endif
     57      1.13    rillig 
     58      1.14    rillig # The condition "0" is false, therefore ELSE2 gets assigned a value,
     59      1.14    rillig # and the inner IE2 as well.  Nothing surprising here as well.
     60      1.14    rillig .if "${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}} ${THEN2}${ELSE2}${IT2}${IE2}" != " else2e2"
     61      1.13    rillig .  error
     62      1.13    rillig .endif
     63      1.13    rillig 
     64      1.13    rillig # The same effects happen when the variables are defined elsewhere.
     65      1.14    rillig SINK3:=	${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}} ${THEN3}${ELSE3}${IT3}${IE3}
     66      1.14    rillig SINK4:=	${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}} ${THEN4}${ELSE4}${IT4}${IE4}
     67      1.14    rillig .if ${SINK3} != " then3t3"
     68      1.13    rillig .  error
     69      1.13    rillig .endif
     70      1.14    rillig .if ${SINK4} != " else4e4"
     71      1.13    rillig .  error
     72      1.13    rillig .endif
     73       1.4    rillig 
     74       1.4    rillig mod-assign-empty:
     75       1.7    rillig 	# Assigning to the empty variable would obviously not work since that
     76       1.7    rillig 	# variable is write-protected.  Therefore it is rejected early with a
     77       1.7    rillig 	# "Bad modifier" message.
     78       1.7    rillig 	@echo $@: ${::=value}
     79       1.7    rillig 
     80       1.7    rillig 	# In this variant, it is not as obvious that the name of the
     81       1.7    rillig 	# expression is empty.  Assigning to it is rejected as well, with the
     82       1.7    rillig 	# same "Bad modifier" message.
     83       1.4    rillig 	@echo $@: ${:Uvalue::=overwritten}
     84       1.4    rillig 
     85       1.7    rillig 	# The :L modifier sets the value of the expression to its variable
     86       1.7    rillig 	# name.  The name of the expression is "VAR", therefore assigning to
     87       1.7    rillig 	# that variable works.
     88       1.4    rillig 	@echo $@: ${VAR:L::=overwritten} VAR=${VAR}
     89       1.4    rillig 
     90       1.4    rillig mod-assign-parse:
     91       1.4    rillig 	# The modifier for assignment operators starts with a ':'.
     92       1.4    rillig 	# An 'x' after that is an invalid modifier.
     93      1.20    rillig 	# expect: make: in target "mod-assign-parse": while evaluating variable "ASSIGN": Unknown modifier ":x"
     94      1.15    rillig 	@echo ${ASSIGN::x}
     95       1.4    rillig 
     96       1.4    rillig 	# When parsing an assignment operator fails because the operator is
     97       1.4    rillig 	# incomplete, make falls back to the SysV modifier.
     98  1.20.2.1  perseant # expect: make: in target "mod-assign-parse": while evaluating variable "ASSIGN": Unfinished modifier ('}' missing)
     99       1.4    rillig 	@echo ${SYSV::=sysv\:x}${SYSV::x=:y}
    100       1.4    rillig 
    101       1.4    rillig 	@echo ${ASSIGN::=value	# missing closing brace
    102       1.5    rillig 
    103       1.5    rillig mod-assign-shell-error:
    104       1.5    rillig 	# If the command succeeds, the variable is assigned.
    105       1.6    rillig 	@${SH_OK::!= echo word; true } echo ok=${SH_OK}
    106       1.5    rillig 
    107       1.5    rillig 	# If the command fails, the variable keeps its previous value.
    108       1.5    rillig 	@${SH_ERR::=previous}
    109  1.20.2.1  perseant 	@${SH_ERR::!= echo word; (exit 13) } echo err=${SH_ERR}
    110       1.8    rillig 
    111      1.10    rillig # XXX: The ::= modifier expands its right-hand side exactly once.
    112       1.8    rillig # This differs subtly from normal assignments such as '+=' or '=', which copy
    113       1.8    rillig # their right-hand side literally.
    114       1.8    rillig APPEND.prev=		previous
    115       1.8    rillig APPEND.var=		${APPEND.prev}
    116       1.8    rillig APPEND.indirect=	indirect $${:Unot expanded}
    117       1.8    rillig APPEND.dollar=		$${APPEND.indirect}
    118       1.8    rillig .if ${APPEND.var::+=${APPEND.dollar}} != ""
    119       1.8    rillig .  error
    120       1.8    rillig .endif
    121       1.8    rillig .if ${APPEND.var} != "previous indirect \${:Unot expanded}"
    122       1.8    rillig .  error
    123       1.8    rillig .endif
    124      1.10    rillig 
    125      1.10    rillig 
    126      1.16    rillig # The assignment modifier can be used in an expression that is
    127      1.10    rillig # enclosed in parentheses.  In such a case, parsing stops at the first ')',
    128      1.10    rillig # not at the first '}'.
    129      1.10    rillig VAR=	previous
    130      1.10    rillig _:=	$(VAR::=current})
    131      1.10    rillig .if ${VAR} != "current}"
    132      1.10    rillig .  error
    133      1.10    rillig .endif
    134      1.11    rillig 
    135      1.11    rillig 
    136      1.11    rillig # Before var.c 1.888 from 2021-03-15, an expression using the modifier '::='
    137      1.11    rillig # expanded its variable name once too often during evaluation.  This was only
    138      1.11    rillig # relevant for variable names containing a '$' sign in their actual name, not
    139      1.11    rillig # the usual VAR.${param}.
    140      1.11    rillig .MAKEFLAGS: -dv
    141      1.11    rillig param=		twice
    142      1.11    rillig VARNAME=	VAR.$${param}	# Indirect variable name because of the '$',
    143      1.11    rillig 				# to avoid difficult escaping rules.
    144      1.11    rillig 
    145      1.11    rillig ${VARNAME}=	initial-value	# Sets 'VAR.${param}' to 'expanded'.
    146      1.11    rillig .if defined(VAR.twice)		# At this point, the '$$' is not expanded.
    147      1.11    rillig .  error
    148      1.11    rillig .endif
    149      1.11    rillig .if ${${VARNAME}::=assigned-value} # Here the variable name gets expanded once
    150      1.11    rillig .  error			# too often.
    151      1.11    rillig .endif
    152      1.12    rillig .if defined(VAR.twice)
    153      1.12    rillig .  error The variable name in the '::=' modifier is expanded once too often.
    154      1.11    rillig .endif
    155      1.12    rillig .if ${${VARNAME}} != "assigned-value"
    156      1.11    rillig .  error
    157      1.11    rillig .endif
    158      1.11    rillig .MAKEFLAGS: -d0
    159      1.17    rillig 
    160      1.17    rillig 
    161      1.18    rillig # Conditional directives are evaluated in command line scope.  An assignment
    162      1.18    rillig # modifier that creates a new variable creates it in the command line scope.
    163      1.18    rillig # Existing variables are updated in their previous scope, and environment
    164      1.18    rillig # variables are created in the global scope, as in other situations.
    165      1.17    rillig .MAKEFLAGS: CMD_CMD_VAR=cmd-value
    166      1.17    rillig CMD_GLOBAL_VAR=global-value
    167      1.17    rillig export CMD_ENV_VAR=env-value
    168      1.17    rillig .MAKEFLAGS: -dv
    169      1.17    rillig # expect-reset
    170      1.17    rillig # expect: Command: CMD_CMD_VAR = new-value
    171      1.17    rillig # expect: Global: CMD_GLOBAL_VAR = new-value
    172      1.17    rillig # expect: Global: CMD_ENV_VAR = new-value
    173      1.17    rillig # expect: Global: ignoring delete 'CMD_NEW_VAR' as it is not found
    174      1.17    rillig # expect: Command: CMD_NEW_VAR = new-value
    175      1.17    rillig .if ${CMD_CMD_VAR::=new-value} \
    176      1.17    rillig   || ${CMD_GLOBAL_VAR::=new-value} \
    177      1.17    rillig   || ${CMD_ENV_VAR::=new-value} \
    178      1.17    rillig   || "${CMD_NEW_VAR::=new-value}"
    179      1.17    rillig .  error
    180      1.17    rillig .endif
    181      1.17    rillig .MAKEFLAGS: -d0
    182      1.17    rillig 
    183      1.19    rillig # Run the 'target' test in a separate sub-make, with reduced debug logging.
    184      1.19    rillig all: run-target
    185      1.19    rillig run-target: .PHONY
    186      1.19    rillig 	@${MAKE} -r -f ${MAKEFILE} -dv target 2>&1 | grep ': TARGET_'
    187      1.17    rillig 
    188      1.19    rillig .else # make(target)
    189      1.19    rillig 
    190      1.19    rillig # The commands of a target are evaluated in target scope.  An assignment
    191      1.19    rillig # modifier that creates a new variable creates it in the target scope.
    192      1.19    rillig # Existing variables are updated in their previous scope, and environment
    193      1.19    rillig # variables are created in the global scope, as in other situations.
    194      1.17    rillig #
    195      1.19    rillig # expect: target: TARGET_TARGET_VAR = new-value
    196      1.19    rillig # expect: Global: TARGET_GLOBAL_VAR = new-value
    197      1.19    rillig # expect: Global: TARGET_ENV_VAR = new-value
    198      1.19    rillig # expect: target: TARGET_NEW_VAR = new-value
    199      1.17    rillig .MAKEFLAGS: TARGET_CMD_VAR=cmd-value
    200      1.17    rillig TARGET_GLOBAL_VAR=global-value
    201      1.17    rillig export TARGET_ENV_VAR=env-value
    202      1.18    rillig target: .PHONY TARGET_TARGET_VAR=target-value
    203      1.17    rillig 	: ${TARGET_TARGET_VAR::=new-value}
    204      1.17    rillig 	: ${TARGET_CMD_VAR::=new-value}
    205      1.17    rillig 	: ${TARGET_GLOBAL_VAR::=new-value}
    206      1.17    rillig 	: ${TARGET_ENV_VAR::=new-value}
    207      1.17    rillig 	: ${TARGET_NEW_VAR::=new-value}
    208      1.19    rillig 
    209      1.19    rillig .endif
    210