1*d5e0a182SSimon J. Gerraty# $NetBSD: var-op-append.mk,v 1.12 2023/11/02 05:46:26 rillig Exp $ 22c3632d1SSimon J. Gerraty# 3148ee845SSimon J. Gerraty# Tests for the '+=' variable assignment operator, which appends to a 4148ee845SSimon J. Gerraty# variable, creating it if necessary. 5148ee845SSimon J. Gerraty# 6148ee845SSimon J. Gerraty# See also 7148ee845SSimon J. Gerraty# var-op.mk 8148ee845SSimon J. Gerraty# 9148ee845SSimon J. Gerraty# Standards 10148ee845SSimon J. Gerraty# The '+=' variable assignment operator is planned to be added in 11148ee845SSimon J. Gerraty# POSIX.1-202x. 12148ee845SSimon J. Gerraty# 13148ee845SSimon J. Gerraty# This implementation does not support the immediate-expansion macros 14148ee845SSimon J. Gerraty# specified in POSIX.1-202x. All variables are delayed-expansion. 15148ee845SSimon J. Gerraty# 16148ee845SSimon J. Gerraty# History 17148ee845SSimon J. Gerraty# The '+=' variable assignment operator was added before 1993-03-21. 182c3632d1SSimon J. Gerraty 19956e45f6SSimon J. Gerraty# Appending to an undefined variable is possible. 20956e45f6SSimon J. Gerraty# The variable is created, and no extra space is added before the value. 21956e45f6SSimon J. GerratyVAR+= one 22956e45f6SSimon J. Gerraty.if ${VAR} != "one" 23956e45f6SSimon J. Gerraty. error 24956e45f6SSimon J. Gerraty.endif 25956e45f6SSimon J. Gerraty 26956e45f6SSimon J. Gerraty# Appending to an existing variable adds a single space and the value. 27956e45f6SSimon J. GerratyVAR+= two 28956e45f6SSimon J. Gerraty.if ${VAR} != "one two" 29956e45f6SSimon J. Gerraty. error 30956e45f6SSimon J. Gerraty.endif 31956e45f6SSimon J. Gerraty 32956e45f6SSimon J. Gerraty# Appending an empty string nevertheless adds a single space. 33956e45f6SSimon J. GerratyVAR+= # empty 34956e45f6SSimon J. Gerraty.if ${VAR} != "one two " 35956e45f6SSimon J. Gerraty. error 36956e45f6SSimon J. Gerraty.endif 37956e45f6SSimon J. Gerraty 38956e45f6SSimon J. Gerraty# Variable names may contain '+', and this character is also part of the 39956e45f6SSimon J. Gerraty# '+=' assignment operator. As far as possible, the '+' is interpreted as 40956e45f6SSimon J. Gerraty# part of the assignment operator. 41956e45f6SSimon J. Gerraty# 42*d5e0a182SSimon J. Gerraty# See Parse_Var, AdjustVarassignOp. 43956e45f6SSimon J. GerratyC++= value 44956e45f6SSimon J. Gerraty.if ${C+} != "value" || defined(C++) 45956e45f6SSimon J. Gerraty. error 46956e45f6SSimon J. Gerraty.endif 47956e45f6SSimon J. Gerraty 48dba7b0efSSimon J. Gerraty# Before var.c 1.793 from 2021-02-03, the variable name of a newly created 49dba7b0efSSimon J. Gerraty# variable was expanded two times in a row, which was unexpected but 50dba7b0efSSimon J. Gerraty# irrelevant in practice since variable names containing dollars lead to 51dba7b0efSSimon J. Gerraty# strange side effects in several other places as well. 52956e45f6SSimon J. Gerraty.MAKEFLAGS: -dv 53956e45f6SSimon J. GerratyVAR.${:U\$\$\$\$\$\$\$\$}+= dollars 54956e45f6SSimon J. Gerraty.MAKEFLAGS: -d0 55dba7b0efSSimon J. Gerraty.if ${VAR.${:U\$\$\$\$\$\$\$\$}} != "dollars" 56956e45f6SSimon J. Gerraty. error 57956e45f6SSimon J. Gerraty.endif 582c3632d1SSimon J. Gerraty 59*d5e0a182SSimon J. Gerraty 60*d5e0a182SSimon J. Gerraty# Appending to an environment variable in the global scope creates a global 61*d5e0a182SSimon J. Gerraty# variable of the same name, taking its initial value from the environment 62*d5e0a182SSimon J. Gerraty# variable. After the assignment, the environment variable is left as-is, 63*d5e0a182SSimon J. Gerraty# the value of the global variable is not synced back to the environment 64*d5e0a182SSimon J. Gerraty# variable. 65*d5e0a182SSimon J. Gerratyexport ENV_PLUS_GLOBAL=from-env-value 66*d5e0a182SSimon J. GerratyENV_PLUS_GLOBAL+= appended-value 67*d5e0a182SSimon J. Gerraty.if ${ENV_PLUS_GLOBAL} != "from-env-value appended-value" 68*d5e0a182SSimon J. Gerraty. error 69*d5e0a182SSimon J. Gerraty.endif 70*d5e0a182SSimon J. GerratyEXPORTED!= echo "$$ENV_PLUS_GLOBAL" 71*d5e0a182SSimon J. Gerraty.if ${EXPORTED} != "from-env-value" 72*d5e0a182SSimon J. Gerraty. error 73*d5e0a182SSimon J. Gerraty.endif 74*d5e0a182SSimon J. Gerraty 75*d5e0a182SSimon J. Gerraty# Appending to an environment variable in the command line scope ignores the 76*d5e0a182SSimon J. Gerraty# environment variable. 77*d5e0a182SSimon J. Gerratyexport ENV_PLUS_COMMAND=from-env-value 78*d5e0a182SSimon J. Gerraty.MAKEFLAGS: ENV_PLUS_COMMAND+=appended-command 79*d5e0a182SSimon J. Gerraty.if ${ENV_PLUS_COMMAND} != "appended-command" 80*d5e0a182SSimon J. Gerraty. error ${ENV_PLUS_COMMAND} 81*d5e0a182SSimon J. Gerraty.endif 82*d5e0a182SSimon J. GerratyEXPORTED!= echo "$$ENV_PLUS_GLOBAL" 83*d5e0a182SSimon J. Gerraty.if ${EXPORTED} != "from-env-value" 84*d5e0a182SSimon J. Gerraty. error 85*d5e0a182SSimon J. Gerraty.endif 86*d5e0a182SSimon J. Gerraty 87*d5e0a182SSimon J. Gerraty 882c3632d1SSimon J. Gerratyall: 89