1*22619282SSimon J. Gerraty# $NetBSD: varmod-assign.mk,v 1.23 2024/07/04 17:47:54 rillig Exp $ 22c3632d1SSimon J. Gerraty# 32c3632d1SSimon J. Gerraty# Tests for the obscure ::= variable modifiers, which perform variable 42c3632d1SSimon J. Gerraty# assignments during evaluation, just like the = operator in C. 52c3632d1SSimon J. Gerraty 6d5e0a182SSimon J. Gerraty.if !make(target) 7d5e0a182SSimon J. Gerraty 82c3632d1SSimon J. Gerratyall: mod-assign-empty 92c3632d1SSimon J. Gerratyall: mod-assign-parse 102c3632d1SSimon J. Gerratyall: mod-assign-shell-error 112c3632d1SSimon J. Gerraty 12d5e0a182SSimon J. Gerraty# In the following loop expression, 13d5e0a182SSimon J. Gerraty# the '::?=' modifier applies the assignment operator '?=' 3 times. The 1412904384SSimon J. Gerraty# operator '?=' only has an effect for the first time, therefore the variable 1512904384SSimon J. Gerraty# FIRST ends up with the value 1. 1612904384SSimon J. Gerraty.if "${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}" != " first=1" 1712904384SSimon J. Gerraty. error 1812904384SSimon J. Gerraty.endif 192c3632d1SSimon J. Gerraty 20d5e0a182SSimon J. Gerraty# In the following loop expression, 21d5e0a182SSimon J. Gerraty# the modifier '::=' applies the assignment operator '=' 3 times. The 2212904384SSimon J. Gerraty# operator '=' overwrites the previous value, therefore the variable LAST ends 2312904384SSimon J. Gerraty# up with the value 3. 2412904384SSimon J. Gerraty.if "${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}" != " last=3" 2512904384SSimon J. Gerraty. error 2612904384SSimon J. Gerraty.endif 272c3632d1SSimon J. Gerraty 28d5e0a182SSimon J. Gerraty# In the following loop expression, 29d5e0a182SSimon J. Gerraty# the modifier '::+=' applies the assignment operator '+=' 3 times. The 3012904384SSimon J. Gerraty# operator '+=' appends 3 times to the variable, therefore the variable 3112904384SSimon J. Gerraty# APPENDED ends up with the value "1 2 3". 3212904384SSimon J. Gerraty.if "${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}" != " appended=1 2 3" 3312904384SSimon J. Gerraty. error 3412904384SSimon J. Gerraty.endif 352c3632d1SSimon J. Gerraty 36d5e0a182SSimon J. Gerraty# In the following loop expression, 37d5e0a182SSimon J. Gerraty# the modifier '::!=' applies the assignment operator '!=' 3 times. Just as 3812904384SSimon J. Gerraty# with the modifier '::=', the last value is stored in the RAN variable. 3912904384SSimon J. Gerraty.if "${1 2 3:L:@i@${RAN::!=${i:%=echo '<%>';}}@} ran=${RAN}" != " ran=<3>" 4012904384SSimon J. Gerraty. error 4112904384SSimon J. Gerraty.endif 422c3632d1SSimon J. Gerraty 43d5e0a182SSimon J. Gerraty# When a '::=' modifier is evaluated as part of an .if condition, it happens 441d3f2ddcSSimon J. Gerraty# in the command line scope. 4512904384SSimon J. Gerraty.if "${FIRST}, ${LAST}, ${APPENDED}, ${RAN}" != "1, 3, 1 2 3, <3>" 4612904384SSimon J. Gerraty. error 4712904384SSimon J. Gerraty.endif 482c3632d1SSimon J. Gerraty 4912904384SSimon J. Gerraty# Tests for nested assignments, which are hard to read and therefore seldom 5012904384SSimon J. Gerraty# used in practice. 5112904384SSimon J. Gerraty 522c3632d1SSimon J. Gerraty# The condition "1" is true, therefore THEN1 gets assigned a value, 5312904384SSimon J. Gerraty# and the inner IT1 as well. Nothing surprising here. 5412904384SSimon J. Gerraty.if "${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}} ${THEN1}${ELSE1}${IT1}${IE1}" != " then1t1" 5512904384SSimon J. Gerraty. error 5612904384SSimon J. Gerraty.endif 572c3632d1SSimon J. Gerraty 5812904384SSimon J. Gerraty# The condition "0" is false, therefore ELSE2 gets assigned a value, 5912904384SSimon J. Gerraty# and the inner IE2 as well. Nothing surprising here as well. 6012904384SSimon J. Gerraty.if "${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}} ${THEN2}${ELSE2}${IT2}${IE2}" != " else2e2" 6112904384SSimon J. Gerraty. error 6212904384SSimon J. Gerraty.endif 632c3632d1SSimon J. Gerraty 642c3632d1SSimon J. Gerraty# The same effects happen when the variables are defined elsewhere. 652c3632d1SSimon J. GerratySINK3:= ${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}} ${THEN3}${ELSE3}${IT3}${IE3} 662c3632d1SSimon J. GerratySINK4:= ${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}} ${THEN4}${ELSE4}${IT4}${IE4} 6712904384SSimon J. Gerraty.if ${SINK3} != " then3t3" 6812904384SSimon J. Gerraty. error 6912904384SSimon J. Gerraty.endif 7012904384SSimon J. Gerraty.if ${SINK4} != " else4e4" 7112904384SSimon J. Gerraty. error 7212904384SSimon J. Gerraty.endif 732c3632d1SSimon J. Gerraty 742c3632d1SSimon J. Gerratymod-assign-empty: 75956e45f6SSimon J. Gerraty # Assigning to the empty variable would obviously not work since that 76956e45f6SSimon J. Gerraty # variable is write-protected. Therefore it is rejected early with a 77956e45f6SSimon J. Gerraty # "Bad modifier" message. 78956e45f6SSimon J. Gerraty @echo $@: ${::=value} 79956e45f6SSimon J. Gerraty 80956e45f6SSimon J. Gerraty # In this variant, it is not as obvious that the name of the 81956e45f6SSimon J. Gerraty # expression is empty. Assigning to it is rejected as well, with the 82956e45f6SSimon J. Gerraty # same "Bad modifier" message. 832c3632d1SSimon J. Gerraty @echo $@: ${:Uvalue::=overwritten} 842c3632d1SSimon J. Gerraty 85956e45f6SSimon J. Gerraty # The :L modifier sets the value of the expression to its variable 86956e45f6SSimon J. Gerraty # name. The name of the expression is "VAR", therefore assigning to 87956e45f6SSimon J. Gerraty # that variable works. 882c3632d1SSimon J. Gerraty @echo $@: ${VAR:L::=overwritten} VAR=${VAR} 892c3632d1SSimon J. Gerraty 902c3632d1SSimon J. Gerratymod-assign-parse: 912c3632d1SSimon J. Gerraty # The modifier for assignment operators starts with a ':'. 922c3632d1SSimon J. Gerraty # An 'x' after that is an invalid modifier. 93*22619282SSimon J. Gerraty # expect: make: in target "mod-assign-parse": while evaluating variable "ASSIGN" with value "": Unknown modifier ":x" 941d3f2ddcSSimon J. Gerraty @echo ${ASSIGN::x} 952c3632d1SSimon J. Gerraty 962c3632d1SSimon J. Gerraty # When parsing an assignment operator fails because the operator is 972c3632d1SSimon J. Gerraty # incomplete, make falls back to the SysV modifier. 982c3632d1SSimon J. Gerraty @echo ${SYSV::=sysv\:x}${SYSV::x=:y} 992c3632d1SSimon J. Gerraty 100*22619282SSimon J. Gerraty# expect: make: in target "mod-assign-parse": while evaluating variable "ASSIGN" with value "": Unfinished modifier ('}' missing) 1012c3632d1SSimon J. Gerraty @echo ${ASSIGN::=value # missing closing brace 1022c3632d1SSimon J. Gerraty 1032c3632d1SSimon J. Gerratymod-assign-shell-error: 1042c3632d1SSimon J. Gerraty # If the command succeeds, the variable is assigned. 1052c3632d1SSimon J. Gerraty @${SH_OK::!= echo word; true } echo ok=${SH_OK} 1062c3632d1SSimon J. Gerraty 1072c3632d1SSimon J. Gerraty # If the command fails, the variable keeps its previous value. 1082c3632d1SSimon J. Gerraty @${SH_ERR::=previous} 109*22619282SSimon J. Gerraty @${SH_ERR::!= echo word; (exit 13) } echo err=${SH_ERR} 110956e45f6SSimon J. Gerraty 111b0c40a00SSimon J. Gerraty# XXX: The ::= modifier expands its right-hand side exactly once. 112956e45f6SSimon J. Gerraty# This differs subtly from normal assignments such as '+=' or '=', which copy 113956e45f6SSimon J. Gerraty# their right-hand side literally. 114956e45f6SSimon J. GerratyAPPEND.prev= previous 115956e45f6SSimon J. GerratyAPPEND.var= ${APPEND.prev} 116956e45f6SSimon J. GerratyAPPEND.indirect= indirect $${:Unot expanded} 117956e45f6SSimon J. GerratyAPPEND.dollar= $${APPEND.indirect} 118956e45f6SSimon J. Gerraty.if ${APPEND.var::+=${APPEND.dollar}} != "" 119956e45f6SSimon J. Gerraty. error 120956e45f6SSimon J. Gerraty.endif 121956e45f6SSimon J. Gerraty.if ${APPEND.var} != "previous indirect \${:Unot expanded}" 122956e45f6SSimon J. Gerraty. error 123956e45f6SSimon J. Gerraty.endif 124b0c40a00SSimon J. Gerraty 125b0c40a00SSimon J. Gerraty 126d5e0a182SSimon J. Gerraty# The assignment modifier can be used in an expression that is 127b0c40a00SSimon J. Gerraty# enclosed in parentheses. In such a case, parsing stops at the first ')', 128b0c40a00SSimon J. Gerraty# not at the first '}'. 129b0c40a00SSimon J. GerratyVAR= previous 130b0c40a00SSimon J. Gerraty_:= $(VAR::=current}) 131b0c40a00SSimon J. Gerraty.if ${VAR} != "current}" 132b0c40a00SSimon J. Gerraty. error 133b0c40a00SSimon J. Gerraty.endif 134b0c40a00SSimon J. Gerraty 135b0c40a00SSimon J. Gerraty 136b0c40a00SSimon J. Gerraty# Before var.c 1.888 from 2021-03-15, an expression using the modifier '::=' 137b0c40a00SSimon J. Gerraty# expanded its variable name once too often during evaluation. This was only 138b0c40a00SSimon J. Gerraty# relevant for variable names containing a '$' sign in their actual name, not 139b0c40a00SSimon J. Gerraty# the usual VAR.${param}. 140b0c40a00SSimon J. Gerraty.MAKEFLAGS: -dv 141b0c40a00SSimon J. Gerratyparam= twice 142b0c40a00SSimon J. GerratyVARNAME= VAR.$${param} # Indirect variable name because of the '$', 143b0c40a00SSimon J. Gerraty # to avoid difficult escaping rules. 144b0c40a00SSimon J. Gerraty 145b0c40a00SSimon J. Gerraty${VARNAME}= initial-value # Sets 'VAR.${param}' to 'expanded'. 146b0c40a00SSimon J. Gerraty.if defined(VAR.twice) # At this point, the '$$' is not expanded. 147b0c40a00SSimon J. Gerraty. error 148b0c40a00SSimon J. Gerraty.endif 149b0c40a00SSimon J. Gerraty.if ${${VARNAME}::=assigned-value} # Here the variable name gets expanded once 150b0c40a00SSimon J. Gerraty. error # too often. 151b0c40a00SSimon J. Gerraty.endif 152b0c40a00SSimon J. Gerraty.if defined(VAR.twice) 153b0c40a00SSimon J. Gerraty. error The variable name in the '::=' modifier is expanded once too often. 154b0c40a00SSimon J. Gerraty.endif 155b0c40a00SSimon J. Gerraty.if ${${VARNAME}} != "assigned-value" 156b0c40a00SSimon J. Gerraty. error 157b0c40a00SSimon J. Gerraty.endif 158b0c40a00SSimon J. Gerraty.MAKEFLAGS: -d0 159d5e0a182SSimon J. Gerraty 160d5e0a182SSimon J. Gerraty 161d5e0a182SSimon J. Gerraty# Conditional directives are evaluated in command line scope. An assignment 162d5e0a182SSimon J. Gerraty# modifier that creates a new variable creates it in the command line scope. 163d5e0a182SSimon J. Gerraty# Existing variables are updated in their previous scope, and environment 164d5e0a182SSimon J. Gerraty# variables are created in the global scope, as in other situations. 165d5e0a182SSimon J. Gerraty.MAKEFLAGS: CMD_CMD_VAR=cmd-value 166d5e0a182SSimon J. GerratyCMD_GLOBAL_VAR=global-value 167d5e0a182SSimon J. Gerratyexport CMD_ENV_VAR=env-value 168d5e0a182SSimon J. Gerraty.MAKEFLAGS: -dv 169d5e0a182SSimon J. Gerraty# expect-reset 170d5e0a182SSimon J. Gerraty# expect: Command: CMD_CMD_VAR = new-value 171d5e0a182SSimon J. Gerraty# expect: Global: CMD_GLOBAL_VAR = new-value 172d5e0a182SSimon J. Gerraty# expect: Global: CMD_ENV_VAR = new-value 173d5e0a182SSimon J. Gerraty# expect: Global: ignoring delete 'CMD_NEW_VAR' as it is not found 174d5e0a182SSimon J. Gerraty# expect: Command: CMD_NEW_VAR = new-value 175d5e0a182SSimon J. Gerraty.if ${CMD_CMD_VAR::=new-value} \ 176d5e0a182SSimon J. Gerraty || ${CMD_GLOBAL_VAR::=new-value} \ 177d5e0a182SSimon J. Gerraty || ${CMD_ENV_VAR::=new-value} \ 178d5e0a182SSimon J. Gerraty || "${CMD_NEW_VAR::=new-value}" 179d5e0a182SSimon J. Gerraty. error 180d5e0a182SSimon J. Gerraty.endif 181d5e0a182SSimon J. Gerraty.MAKEFLAGS: -d0 182d5e0a182SSimon J. Gerraty 183d5e0a182SSimon J. Gerraty# Run the 'target' test in a separate sub-make, with reduced debug logging. 184d5e0a182SSimon J. Gerratyall: run-target 185d5e0a182SSimon J. Gerratyrun-target: .PHONY 186d5e0a182SSimon J. Gerraty @${MAKE} -r -f ${MAKEFILE} -dv target 2>&1 | grep ': TARGET_' 187d5e0a182SSimon J. Gerraty 188d5e0a182SSimon J. Gerraty.else # make(target) 189d5e0a182SSimon J. Gerraty 190d5e0a182SSimon J. Gerraty# The commands of a target are evaluated in target scope. An assignment 191d5e0a182SSimon J. Gerraty# modifier that creates a new variable creates it in the target scope. 192d5e0a182SSimon J. Gerraty# Existing variables are updated in their previous scope, and environment 193d5e0a182SSimon J. Gerraty# variables are created in the global scope, as in other situations. 194d5e0a182SSimon J. Gerraty# 195d5e0a182SSimon J. Gerraty# expect: target: TARGET_TARGET_VAR = new-value 196d5e0a182SSimon J. Gerraty# expect: Global: TARGET_GLOBAL_VAR = new-value 197d5e0a182SSimon J. Gerraty# expect: Global: TARGET_ENV_VAR = new-value 198d5e0a182SSimon J. Gerraty# expect: target: TARGET_NEW_VAR = new-value 199d5e0a182SSimon J. Gerraty.MAKEFLAGS: TARGET_CMD_VAR=cmd-value 200d5e0a182SSimon J. GerratyTARGET_GLOBAL_VAR=global-value 201d5e0a182SSimon J. Gerratyexport TARGET_ENV_VAR=env-value 202d5e0a182SSimon J. Gerratytarget: .PHONY TARGET_TARGET_VAR=target-value 203d5e0a182SSimon J. Gerraty : ${TARGET_TARGET_VAR::=new-value} 204d5e0a182SSimon J. Gerraty : ${TARGET_CMD_VAR::=new-value} 205d5e0a182SSimon J. Gerraty : ${TARGET_GLOBAL_VAR::=new-value} 206d5e0a182SSimon J. Gerraty : ${TARGET_ENV_VAR::=new-value} 207d5e0a182SSimon J. Gerraty : ${TARGET_NEW_VAR::=new-value} 208d5e0a182SSimon J. Gerraty 209d5e0a182SSimon J. Gerraty.endif 210