1*759b177aSSimon J. Gerraty# $NetBSD: varmod-assign.mk,v 1.28 2025/03/30 01:09:41 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 8*759b177aSSimon J. Gerratyall: mod-assign-empty-{1,2,3,4} 96a7405f5SSimon J. Gerratyall: mod-assign-parse-{1,2,3} 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 746a7405f5SSimon J. Gerratymod-assign-empty-1: 75956e45f6SSimon J. Gerraty # Assigning to the empty variable would obviously not work since that 766a7405f5SSimon J. Gerraty # variable is write-protected. 77*759b177aSSimon J. Gerraty# expect: make: Invalid attempt to assign "value" to variable "" via modifier "::=" 78956e45f6SSimon J. Gerraty @echo $@: ${::=value} 79956e45f6SSimon J. Gerraty 806a7405f5SSimon J. Gerratymod-assign-empty-2: 81956e45f6SSimon J. Gerraty # In this variant, it is not as obvious that the name of the 826a7405f5SSimon J. Gerraty # expression is empty. 83*759b177aSSimon J. Gerraty# expect: make: Invalid attempt to assign "overwritten" to variable "" via modifier "::=" 842c3632d1SSimon J. Gerraty @echo $@: ${:Uvalue::=overwritten} 852c3632d1SSimon J. Gerraty 866a7405f5SSimon J. Gerratymod-assign-empty-3: 87*759b177aSSimon J. Gerraty # In this variant, it is not as obvious that the name of the 88*759b177aSSimon J. Gerraty # expression is empty. 89*759b177aSSimon J. Gerraty# expect: make: Invalid attempt to assign "appended" to variable "" via modifier "::+=" 90*759b177aSSimon J. Gerraty @echo $@: ${:Uvalue::+=appended} 91*759b177aSSimon J. Gerraty 92*759b177aSSimon J. Gerratymod-assign-empty-4: 93956e45f6SSimon J. Gerraty # The :L modifier sets the value of the expression to its variable 94956e45f6SSimon J. Gerraty # name. The name of the expression is "VAR", therefore assigning to 95956e45f6SSimon J. Gerraty # that variable works. 96*759b177aSSimon J. Gerraty# expect: mod-assign-empty-4: VAR=overwritten 972c3632d1SSimon J. Gerraty @echo $@: ${VAR:L::=overwritten} VAR=${VAR} 982c3632d1SSimon J. Gerraty 996a7405f5SSimon J. Gerratymod-assign-parse-1: 1002c3632d1SSimon J. Gerraty # The modifier for assignment operators starts with a ':'. 1012c3632d1SSimon J. Gerraty # An 'x' after that is an invalid modifier. 102*759b177aSSimon J. Gerraty# expect: make: Unknown modifier "::x" 1031d3f2ddcSSimon J. Gerraty @echo ${ASSIGN::x} 1042c3632d1SSimon J. Gerraty 1056a7405f5SSimon J. Gerratymod-assign-parse-2: 1062c3632d1SSimon J. Gerraty # When parsing an assignment operator fails because the operator is 1072c3632d1SSimon J. Gerraty # incomplete, make falls back to the SysV modifier. 1082c3632d1SSimon J. Gerraty @echo ${SYSV::=sysv\:x}${SYSV::x=:y} 1092c3632d1SSimon J. Gerraty 1106a7405f5SSimon J. Gerratymod-assign-parse-3: 111*759b177aSSimon J. Gerraty# expect: make: Unfinished modifier after "value # missing closing brace", expecting "}" 1122c3632d1SSimon J. Gerraty @echo ${ASSIGN::=value # missing closing brace 1132c3632d1SSimon J. Gerraty 1142c3632d1SSimon J. Gerratymod-assign-shell-error: 1152c3632d1SSimon J. Gerraty # If the command succeeds, the variable is assigned. 1162c3632d1SSimon J. Gerraty @${SH_OK::!= echo word; true } echo ok=${SH_OK} 1172c3632d1SSimon J. Gerraty 1182c3632d1SSimon J. Gerraty # If the command fails, the variable keeps its previous value. 1192c3632d1SSimon J. Gerraty @${SH_ERR::=previous} 12022619282SSimon J. Gerraty @${SH_ERR::!= echo word; (exit 13) } echo err=${SH_ERR} 121956e45f6SSimon J. Gerraty 122b0c40a00SSimon J. Gerraty# XXX: The ::= modifier expands its right-hand side exactly once. 123956e45f6SSimon J. Gerraty# This differs subtly from normal assignments such as '+=' or '=', which copy 124956e45f6SSimon J. Gerraty# their right-hand side literally. 125956e45f6SSimon J. GerratyAPPEND.prev= previous 126956e45f6SSimon J. GerratyAPPEND.var= ${APPEND.prev} 127956e45f6SSimon J. GerratyAPPEND.indirect= indirect $${:Unot expanded} 128956e45f6SSimon J. GerratyAPPEND.dollar= $${APPEND.indirect} 129956e45f6SSimon J. Gerraty.if ${APPEND.var::+=${APPEND.dollar}} != "" 130956e45f6SSimon J. Gerraty. error 131956e45f6SSimon J. Gerraty.endif 132956e45f6SSimon J. Gerraty.if ${APPEND.var} != "previous indirect \${:Unot expanded}" 133956e45f6SSimon J. Gerraty. error 134956e45f6SSimon J. Gerraty.endif 135b0c40a00SSimon J. Gerraty 136b0c40a00SSimon J. Gerraty 137d5e0a182SSimon J. Gerraty# The assignment modifier can be used in an expression that is 138b0c40a00SSimon J. Gerraty# enclosed in parentheses. In such a case, parsing stops at the first ')', 139b0c40a00SSimon J. Gerraty# not at the first '}'. 140b0c40a00SSimon J. GerratyVAR= previous 141b0c40a00SSimon J. Gerraty_:= $(VAR::=current}) 142b0c40a00SSimon J. Gerraty.if ${VAR} != "current}" 143b0c40a00SSimon J. Gerraty. error 144b0c40a00SSimon J. Gerraty.endif 145b0c40a00SSimon J. Gerraty 146b0c40a00SSimon J. Gerraty 147b0c40a00SSimon J. Gerraty# Before var.c 1.888 from 2021-03-15, an expression using the modifier '::=' 148b0c40a00SSimon J. Gerraty# expanded its variable name once too often during evaluation. This was only 149b0c40a00SSimon J. Gerraty# relevant for variable names containing a '$' sign in their actual name, not 150b0c40a00SSimon J. Gerraty# the usual VAR.${param}. 151b0c40a00SSimon J. Gerraty.MAKEFLAGS: -dv 152b0c40a00SSimon J. Gerratyparam= twice 153b0c40a00SSimon J. GerratyVARNAME= VAR.$${param} # Indirect variable name because of the '$', 154b0c40a00SSimon J. Gerraty # to avoid difficult escaping rules. 155b0c40a00SSimon J. Gerraty 156b0c40a00SSimon J. Gerraty${VARNAME}= initial-value # Sets 'VAR.${param}' to 'expanded'. 157b0c40a00SSimon J. Gerraty.if defined(VAR.twice) # At this point, the '$$' is not expanded. 158b0c40a00SSimon J. Gerraty. error 159b0c40a00SSimon J. Gerraty.endif 160b0c40a00SSimon J. Gerraty.if ${${VARNAME}::=assigned-value} # Here the variable name gets expanded once 161b0c40a00SSimon J. Gerraty. error # too often. 162b0c40a00SSimon J. Gerraty.endif 163b0c40a00SSimon J. Gerraty.if defined(VAR.twice) 164b0c40a00SSimon J. Gerraty. error The variable name in the '::=' modifier is expanded once too often. 165b0c40a00SSimon J. Gerraty.endif 166b0c40a00SSimon J. Gerraty.if ${${VARNAME}} != "assigned-value" 167b0c40a00SSimon J. Gerraty. error 168b0c40a00SSimon J. Gerraty.endif 169b0c40a00SSimon J. Gerraty.MAKEFLAGS: -d0 170d5e0a182SSimon J. Gerraty 171d5e0a182SSimon J. Gerraty 172d5e0a182SSimon J. Gerraty# Conditional directives are evaluated in command line scope. An assignment 173d5e0a182SSimon J. Gerraty# modifier that creates a new variable creates it in the command line scope. 174d5e0a182SSimon J. Gerraty# Existing variables are updated in their previous scope, and environment 175d5e0a182SSimon J. Gerraty# variables are created in the global scope, as in other situations. 176d5e0a182SSimon J. Gerraty.MAKEFLAGS: CMD_CMD_VAR=cmd-value 177d5e0a182SSimon J. GerratyCMD_GLOBAL_VAR=global-value 178d5e0a182SSimon J. Gerratyexport CMD_ENV_VAR=env-value 179d5e0a182SSimon J. Gerraty.MAKEFLAGS: -dv 180d5e0a182SSimon J. Gerraty# expect-reset 181d5e0a182SSimon J. Gerraty# expect: Command: CMD_CMD_VAR = new-value 182d5e0a182SSimon J. Gerraty# expect: Global: CMD_GLOBAL_VAR = new-value 183d5e0a182SSimon J. Gerraty# expect: Global: CMD_ENV_VAR = new-value 184d5e0a182SSimon J. Gerraty# expect: Global: ignoring delete 'CMD_NEW_VAR' as it is not found 185d5e0a182SSimon J. Gerraty# expect: Command: CMD_NEW_VAR = new-value 186d5e0a182SSimon J. Gerraty.if ${CMD_CMD_VAR::=new-value} \ 187d5e0a182SSimon J. Gerraty || ${CMD_GLOBAL_VAR::=new-value} \ 188d5e0a182SSimon J. Gerraty || ${CMD_ENV_VAR::=new-value} \ 189d5e0a182SSimon J. Gerraty || "${CMD_NEW_VAR::=new-value}" 190d5e0a182SSimon J. Gerraty. error 191d5e0a182SSimon J. Gerraty.endif 192d5e0a182SSimon J. Gerraty.MAKEFLAGS: -d0 193d5e0a182SSimon J. Gerraty 194d5e0a182SSimon J. Gerraty# Run the 'target' test in a separate sub-make, with reduced debug logging. 195d5e0a182SSimon J. Gerratyall: run-target 196d5e0a182SSimon J. Gerratyrun-target: .PHONY 197d5e0a182SSimon J. Gerraty @${MAKE} -r -f ${MAKEFILE} -dv target 2>&1 | grep ': TARGET_' 198d5e0a182SSimon J. Gerraty 199d5e0a182SSimon J. Gerraty.else # make(target) 200d5e0a182SSimon J. Gerraty 201d5e0a182SSimon J. Gerraty# The commands of a target are evaluated in target scope. An assignment 202d5e0a182SSimon J. Gerraty# modifier that creates a new variable creates it in the target scope. 203d5e0a182SSimon J. Gerraty# Existing variables are updated in their previous scope, and environment 204d5e0a182SSimon J. Gerraty# variables are created in the global scope, as in other situations. 205d5e0a182SSimon J. Gerraty# 206d5e0a182SSimon J. Gerraty# expect: target: TARGET_TARGET_VAR = new-value 207d5e0a182SSimon J. Gerraty# expect: Global: TARGET_GLOBAL_VAR = new-value 208d5e0a182SSimon J. Gerraty# expect: Global: TARGET_ENV_VAR = new-value 209d5e0a182SSimon J. Gerraty# expect: target: TARGET_NEW_VAR = new-value 210d5e0a182SSimon J. Gerraty.MAKEFLAGS: TARGET_CMD_VAR=cmd-value 211d5e0a182SSimon J. GerratyTARGET_GLOBAL_VAR=global-value 212d5e0a182SSimon J. Gerratyexport TARGET_ENV_VAR=env-value 213d5e0a182SSimon J. Gerratytarget: .PHONY TARGET_TARGET_VAR=target-value 214d5e0a182SSimon J. Gerraty : ${TARGET_TARGET_VAR::=new-value} 215d5e0a182SSimon J. Gerraty : ${TARGET_CMD_VAR::=new-value} 216d5e0a182SSimon J. Gerraty : ${TARGET_GLOBAL_VAR::=new-value} 217d5e0a182SSimon J. Gerraty : ${TARGET_ENV_VAR::=new-value} 218d5e0a182SSimon J. Gerraty : ${TARGET_NEW_VAR::=new-value} 219d5e0a182SSimon J. Gerraty 220d5e0a182SSimon J. Gerraty.endif 221