1 1.14 rillig # $NetBSD: cond-token-var.mk,v 1.14 2025/06/28 22:39:28 rillig Exp $ 2 1.1 rillig # 3 1.8 rillig # Tests for expressions in .if conditions. 4 1.5 rillig # 5 1.8 rillig # Note the fine distinction between a variable and an expression. 6 1.8 rillig # A variable has a name and a value. To access the value, one writes an 7 1.8 rillig # expression of the form ${VAR}. This is a simple 8 1.8 rillig # expression. Expressions can get more complicated by adding 9 1.5 rillig # variable modifiers such as in ${VAR:Mpattern}. 10 1.5 rillig # 11 1.5 rillig # XXX: Strictly speaking, variable modifiers should be called expression 12 1.5 rillig # modifiers instead since they only modify the expression, not the variable. 13 1.5 rillig # Well, except for the assignment modifiers, these do indeed change the value 14 1.5 rillig # of the variable. 15 1.1 rillig 16 1.10 rillig D= defined 17 1.3 rillig DEF= defined 18 1.1 rillig 19 1.10 rillig 20 1.3 rillig # A defined variable may appear on either side of the comparison. 21 1.3 rillig .if ${DEF} == ${DEF} 22 1.7 rillig # expect+1: ok 23 1.4 rillig . info ok 24 1.3 rillig .else 25 1.4 rillig . error 26 1.3 rillig .endif 27 1.3 rillig 28 1.3 rillig # A variable that appears on the left-hand side must be defined. 29 1.11 rillig # expect+1: Variable "UNDEF" is undefined 30 1.3 rillig .if ${UNDEF} == ${DEF} 31 1.4 rillig . error 32 1.3 rillig .endif 33 1.3 rillig 34 1.3 rillig # A variable that appears on the right-hand side must be defined. 35 1.11 rillig # expect+1: Variable "UNDEF" is undefined 36 1.3 rillig .if ${DEF} == ${UNDEF} 37 1.4 rillig . error 38 1.3 rillig .endif 39 1.3 rillig 40 1.3 rillig # A defined variable may appear as an expression of its own. 41 1.3 rillig .if ${DEF} 42 1.3 rillig .endif 43 1.3 rillig 44 1.5 rillig # An undefined variable on its own generates a parse error. 45 1.11 rillig # expect+1: Variable "UNDEF" is undefined 46 1.3 rillig .if ${UNDEF} 47 1.3 rillig .endif 48 1.3 rillig 49 1.5 rillig # The :U modifier turns an undefined expression into a defined expression. 50 1.5 rillig # Since the expression is defined now, it doesn't generate any parse error. 51 1.3 rillig .if ${UNDEF:U} 52 1.3 rillig .endif 53 1.6 rillig 54 1.10 rillig 55 1.10 rillig # The same as above, for single-letter variables without braces or 56 1.10 rillig # parentheses. 57 1.10 rillig 58 1.10 rillig # A defined variable may appear on either side of the comparison. 59 1.10 rillig .if $D == $D 60 1.10 rillig .endif 61 1.10 rillig 62 1.10 rillig # A variable on the left-hand side must be defined. 63 1.11 rillig # expect+1: Variable "U" is undefined 64 1.10 rillig .if $U == $D 65 1.10 rillig .endif 66 1.10 rillig 67 1.10 rillig # A variable on the right-hand side must be defined. 68 1.11 rillig # expect+1: Variable "U" is undefined 69 1.10 rillig .if $D == $U 70 1.10 rillig .endif 71 1.10 rillig 72 1.10 rillig # A defined variable may appear as an expression of its own. 73 1.10 rillig .if $D 74 1.10 rillig .endif 75 1.10 rillig 76 1.10 rillig # An undefined variable without a comparison operator generates a parse error. 77 1.11 rillig # expect+1: Variable "U" is undefined 78 1.10 rillig .if $U 79 1.10 rillig .endif 80 1.10 rillig 81 1.10 rillig 82 1.8 rillig # If the value of the expression is a number, it is compared against 83 1.6 rillig # zero. 84 1.6 rillig .if ${:U0} 85 1.6 rillig . error 86 1.6 rillig .endif 87 1.6 rillig .if !${:U1} 88 1.6 rillig . error 89 1.6 rillig .endif 90 1.6 rillig 91 1.8 rillig # If the value of the expression is not a number, any non-empty 92 1.6 rillig # value evaluates to true, even if there is only whitespace. 93 1.6 rillig .if ${:U} 94 1.6 rillig . error 95 1.6 rillig .endif 96 1.6 rillig .if !${:U } 97 1.6 rillig . error 98 1.6 rillig .endif 99 1.6 rillig .if !${:Uanything} 100 1.6 rillig . error 101 1.6 rillig .endif 102 1.10 rillig 103 1.10 rillig .MAKEFLAGS: -dv 104 1.11 rillig # The left-hand side of a comparison must not be an unquoted word. 105 1.14 rillig # expect+1: Malformed conditional "x${UNDEF1}y == "${UNDEF2}" || 0x${UNDEF3}" 106 1.10 rillig .if x${UNDEF1}y == "${UNDEF2}" || 0x${UNDEF3} 107 1.10 rillig .endif 108 1.10 rillig 109 1.11 rillig # The left-hand side of a comparison must not be an unquoted word. 110 1.14 rillig # expect+1: Malformed conditional "x${DEF}y == "${UNDEF2}" || 0x${UNDEF3}" 111 1.10 rillig .if x${DEF}y == "${UNDEF2}" || 0x${UNDEF3} 112 1.10 rillig .endif 113 1.10 rillig 114 1.11 rillig # The left-hand side of a comparison must not be an unquoted word. 115 1.14 rillig # expect+1: Malformed conditional "x${DEF}y == "${DEF}" || 0x${UNDEF3}" 116 1.10 rillig .if x${DEF}y == "${DEF}" || 0x${UNDEF3} 117 1.10 rillig .endif 118 1.11 rillig 119 1.11 rillig # An expression in a condition must not be based on an undefined variable, 120 1.11 rillig # but undefined variables may occur in the variable name or in modifiers. 121 1.11 rillig # 122 1.11 rillig # expect: Var_Parse: ${VAR.param$U} (eval-defined-loud) 123 1.11 rillig # expect: Var_Parse: $U} (eval) 124 1.11 rillig VAR.param= value of VAR.param 125 1.11 rillig .if ${VAR.param$U} 126 1.11 rillig .endif 127 1.11 rillig 128 1.10 rillig .MAKEFLAGS: -d0 129 1.12 rillig 130 1.12 rillig 131 1.12 rillig # An expression in a comparison must not be undefined and have modifiers. 132 1.13 rillig # expect+1: Variable "UNDEF" is undefined 133 1.12 rillig .if ${UNDEF:M*} 134 1.12 rillig . error 135 1.12 rillig .else 136 1.12 rillig . error 137 1.12 rillig .endif 138 1.12 rillig 139 1.12 rillig # The left-hand side of a comparison must not be an undefined expression with 140 1.12 rillig # modifiers. 141 1.13 rillig # expect+1: Variable "UNDEF" is undefined 142 1.12 rillig .if ${UNDEF:M*} != "" 143 1.12 rillig . error 144 1.12 rillig .else 145 1.12 rillig . error 146 1.12 rillig .endif 147 1.12 rillig 148 1.12 rillig # The right-hand side of a comparison must not be an undefined expression with 149 1.12 rillig # modifiers. 150 1.13 rillig # expect+1: Variable "UNDEF" is undefined 151 1.12 rillig .if ${:U} != ${UNDEF:M*} 152 1.12 rillig . error 153 1.12 rillig .else 154 1.12 rillig . error 155 1.12 rillig .endif 156