1 1.17 rillig # $NetBSD: directive-undef.mk,v 1.17 2025/03/29 19:08:52 rillig Exp $ 2 1.1 rillig # 3 1.2 rillig # Tests for the .undef directive. 4 1.6 rillig # 5 1.6 rillig # See also: 6 1.6 rillig # directive-misspellings.mk 7 1.1 rillig 8 1.9 rillig # Before var.c 1.737 from 2020-12-19, .undef only undefined the first 9 1.9 rillig # variable, silently skipping all further variable names. 10 1.9 rillig # 11 1.9 rillig # Before var.c 1.761 from 2020-12-22, .undef complained about too many 12 1.9 rillig # arguments. 13 1.9 rillig # 14 1.9 rillig # Since var.c 1.761 from 2020-12-22, .undef handles multiple variable names 15 1.9 rillig # just like the .export directive. 16 1.3 rillig 1= 1 17 1.3 rillig 2= 2 18 1.3 rillig 3= 3 19 1.3 rillig .undef 1 2 3 20 1.9 rillig .if ${1:U_}${2:U_}${3:U_} != ___ 21 1.4 rillig . warning $1$2$3 22 1.3 rillig .endif 23 1.1 rillig 24 1.8 rillig 25 1.8 rillig # Without any arguments, until var.c 1.736 from 2020-12-19, .undef tried 26 1.8 rillig # to delete the variable with the empty name, which never exists; see 27 1.8 rillig # varname-empty.mk. Since var.c 1.737 from 2020-12-19, .undef complains 28 1.8 rillig # about a missing argument. 29 1.13 rillig # expect+1: The .undef directive requires an argument 30 1.8 rillig .undef 31 1.8 rillig 32 1.8 rillig 33 1.8 rillig # Trying to delete the variable with the empty name is ok, it just won't 34 1.8 rillig # ever do anything since that variable is never defined. 35 1.8 rillig .undef ${:U} 36 1.8 rillig 37 1.8 rillig 38 1.9 rillig # The argument of .undef is first expanded exactly once and then split into 39 1.9 rillig # words, just like everywhere else. This prevents variables whose names 40 1.9 rillig # contain spaces or unbalanced 'single' or "double" quotes from being 41 1.9 rillig # undefined, but these characters do not appear in variables names anyway. 42 1.8 rillig 1= 1 43 1.8 rillig 2= 2 44 1.8 rillig 3= 3 45 1.8 rillig ${:U1 2 3}= one two three 46 1.8 rillig VARNAMES= 1 2 3 47 1.11 rillig .undef ${VARNAMES} # undefines the variables "1", "2" and "3" 48 1.11 rillig .if ${${:U1 2 3}} != "one two three" # still there 49 1.8 rillig . error 50 1.8 rillig .endif 51 1.11 rillig .if ${1:U_}${2:U_}${3:U_} != "___" # these have been undefined 52 1.8 rillig . error 53 1.8 rillig .endif 54 1.5 rillig 55 1.7 rillig 56 1.9 rillig # A variable named " " cannot be undefined. There's no practical use case 57 1.9 rillig # for such variables anyway. 58 1.7 rillig SPACE= ${:U } 59 1.7 rillig ${SPACE}= space 60 1.7 rillig .if !defined(${SPACE}) 61 1.7 rillig . error 62 1.7 rillig .endif 63 1.7 rillig .undef ${SPACE} 64 1.9 rillig .if !defined(${SPACE}) 65 1.7 rillig . error 66 1.7 rillig .endif 67 1.7 rillig 68 1.7 rillig 69 1.9 rillig # A variable named "$" can be undefined since the argument to .undef is 70 1.9 rillig # expanded exactly once, before being split into words. 71 1.7 rillig DOLLAR= $$ 72 1.7 rillig ${DOLLAR}= dollar 73 1.7 rillig .if !defined(${DOLLAR}) 74 1.7 rillig . error 75 1.7 rillig .endif 76 1.7 rillig .undef ${DOLLAR} 77 1.7 rillig .if defined(${DOLLAR}) 78 1.7 rillig . error 79 1.7 rillig .endif 80 1.7 rillig 81 1.7 rillig 82 1.9 rillig # Since var.c 1.762 from 2020-12-22, parse errors in the argument should be 83 1.9 rillig # properly detected and should stop the .undef directive from doing any work. 84 1.9 rillig # 85 1.9 rillig # As of var.c 1.762, this doesn't happen though because the error handling 86 1.9 rillig # in Var_Parse and Var_Subst is not done properly. 87 1.17 rillig # expect+1: Unknown modifier ":Z" 88 1.9 rillig .undef ${VARNAMES:L:Z} 89 1.9 rillig 90 1.9 rillig 91 1.10 rillig UT_EXPORTED= exported-value 92 1.10 rillig .export UT_EXPORTED 93 1.10 rillig .if ${:!echo "\${UT_EXPORTED:-not-exported}"!} != "exported-value" 94 1.10 rillig . error 95 1.10 rillig .endif 96 1.10 rillig .if !${.MAKE.EXPORTED:MUT_EXPORTED} 97 1.10 rillig . error 98 1.10 rillig .endif 99 1.10 rillig .undef UT_EXPORTED # XXX: does not update .MAKE.EXPORTED 100 1.10 rillig .if ${:!echo "\${UT_EXPORTED:-not-exported}"!} != "not-exported" 101 1.10 rillig . error 102 1.10 rillig .endif 103 1.10 rillig .if ${.MAKE.EXPORTED:MUT_EXPORTED} 104 1.13 rillig # expect+1: warning: UT_EXPORTED is still listed in .MAKE.EXPORTED even though spaceit is not exported anymore. 105 1.10 rillig . warning UT_EXPORTED is still listed in .MAKE.EXPORTED even though $\ 106 1.10 rillig it is not exported anymore. 107 1.10 rillig .endif 108 1.10 rillig 109 1.10 rillig 110 1.11 rillig # When an exported variable is undefined, the variable is removed both from 111 1.11 rillig # the global scope as well as from the environment. 112 1.11 rillig DIRECT= direct 113 1.11 rillig INDIRECT= in-${DIRECT} 114 1.11 rillig .export DIRECT INDIRECT 115 1.11 rillig .if ${DIRECT} != "direct" 116 1.11 rillig . error 117 1.11 rillig .endif 118 1.11 rillig .if ${INDIRECT} != "in-direct" 119 1.11 rillig . error 120 1.11 rillig .endif 121 1.11 rillig 122 1.11 rillig # Deletes the variables from the global scope and also from the environment. 123 1.11 rillig # This applies to both variables, even though 'INDIRECT' is not actually 124 1.11 rillig # exported yet since it refers to another variable. 125 1.11 rillig .undef DIRECT # Separate '.undef' directives, 126 1.11 rillig .undef INDIRECT # for backwards compatibility. 127 1.11 rillig 128 1.11 rillig .if ${DIRECT:Uundefined} != "undefined" 129 1.11 rillig . error 130 1.11 rillig .endif 131 1.11 rillig .if ${INDIRECT:Uundefined} != "undefined" 132 1.11 rillig . error 133 1.11 rillig .endif 134 1.11 rillig 135 1.11 rillig 136 1.12 rillig # Since var.c 1.570 from 2020-10-06 and before var.c 1.1014 from 2022-03-26, 137 1.12 rillig # make ran into an assertion failure when trying to undefine a variable that 138 1.12 rillig # was based on an environment variable. 139 1.12 rillig .if ${ENV_VAR} != "env-value" # see ./Makefile, ENV.directive-undef 140 1.12 rillig . error 141 1.12 rillig .endif 142 1.12 rillig ENV_VAR+= appended # moves the short-lived variable to the 143 1.12 rillig # global scope 144 1.12 rillig .undef ENV_VAR # removes the variable from both the global 145 1.12 rillig # scope and from the environment 146 1.12 rillig 147 1.12 rillig 148 1.1 rillig all: 149