cond-cmp-string.mk revision 1.18 1 1.18 rillig # $NetBSD: cond-cmp-string.mk,v 1.18 2023/11/19 21:47:52 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for string comparisons in .if conditions.
4 1.1 rillig
5 1.3 rillig # This is a simple comparison of string literals.
6 1.3 rillig # Nothing surprising here.
7 1.3 rillig .if "str" != "str"
8 1.6 rillig . error
9 1.3 rillig .endif
10 1.1 rillig
11 1.3 rillig # The right-hand side of the comparison may be written without quotes.
12 1.3 rillig .if "str" != str
13 1.6 rillig . error
14 1.3 rillig .endif
15 1.3 rillig
16 1.3 rillig # The left-hand side of the comparison must be enclosed in quotes.
17 1.3 rillig # This one is not enclosed in quotes and thus generates an error message.
18 1.17 rillig # expect+1: Malformed conditional (str != str)
19 1.3 rillig .if str != str
20 1.6 rillig . error
21 1.3 rillig .endif
22 1.3 rillig
23 1.18 rillig # The left-hand side of the comparison requires that any expression
24 1.13 rillig # is defined.
25 1.13 rillig #
26 1.13 rillig # The variable named "" is never defined, nevertheless it can be used as a
27 1.18 rillig # starting point for expressions. Applying the :U modifier to such
28 1.13 rillig # an undefined expression turns it into a defined expression.
29 1.13 rillig #
30 1.15 rillig # See ApplyModifier_Defined and DEF_DEFINED.
31 1.3 rillig .if ${:Ustr} != "str"
32 1.6 rillig . error
33 1.3 rillig .endif
34 1.3 rillig
35 1.3 rillig # Any character in a string literal may be escaped using a backslash.
36 1.3 rillig # This means that "\n" does not mean a newline but a simple "n".
37 1.3 rillig .if "string" != "\s\t\r\i\n\g"
38 1.6 rillig . error
39 1.3 rillig .endif
40 1.3 rillig
41 1.3 rillig # It is not possible to concatenate two string literals to form a single
42 1.13 rillig # string. In C, Python and the shell this is possible, but not in make.
43 1.17 rillig # expect+1: Malformed conditional ("string" != "str""ing")
44 1.3 rillig .if "string" != "str""ing"
45 1.6 rillig . error
46 1.13 rillig .else
47 1.13 rillig . error
48 1.3 rillig .endif
49 1.4 rillig
50 1.5 rillig # There is no = operator for strings.
51 1.17 rillig # expect+1: Malformed conditional (!("value" = "value"))
52 1.4 rillig .if !("value" = "value")
53 1.4 rillig . error
54 1.4 rillig .else
55 1.4 rillig . error
56 1.4 rillig .endif
57 1.4 rillig
58 1.4 rillig # There is no === operator for strings either.
59 1.17 rillig # expect+1: Malformed conditional (!("value" === "value"))
60 1.4 rillig .if !("value" === "value")
61 1.4 rillig . error
62 1.4 rillig .else
63 1.4 rillig . error
64 1.4 rillig .endif
65 1.4 rillig
66 1.18 rillig # An expression can be enclosed in double quotes.
67 1.7 rillig .if ${:Uword} != "${:Uword}"
68 1.7 rillig . error
69 1.7 rillig .endif
70 1.7 rillig
71 1.10 rillig # Between 2003-01-01 (maybe even earlier) and 2020-10-30, adding one of the
72 1.18 rillig # characters " \t!=><" directly after an expression resulted in a
73 1.10 rillig # "Malformed conditional", even though the string was well-formed.
74 1.10 rillig .if ${:Uword } != "${:Uword} "
75 1.7 rillig . error
76 1.7 rillig .endif
77 1.11 rillig # Some other characters worked though, and some didn't.
78 1.11 rillig # Those that are mentioned in is_separator didn't work.
79 1.9 rillig .if ${:Uword0} != "${:Uword}0"
80 1.9 rillig . error
81 1.9 rillig .endif
82 1.9 rillig .if ${:Uword&} != "${:Uword}&"
83 1.9 rillig . error
84 1.9 rillig .endif
85 1.9 rillig .if ${:Uword!} != "${:Uword}!"
86 1.9 rillig . error
87 1.9 rillig .endif
88 1.9 rillig .if ${:Uword<} != "${:Uword}<"
89 1.9 rillig . error
90 1.9 rillig .endif
91 1.9 rillig
92 1.18 rillig # Adding another expression to the string literal works though.
93 1.8 rillig .if ${:Uword} != "${:Uwo}${:Urd}"
94 1.8 rillig . error
95 1.8 rillig .endif
96 1.8 rillig
97 1.18 rillig # Adding a space at the beginning of the quoted expression works
98 1.7 rillig # though.
99 1.7 rillig .if ${:U word } != " ${:Uword} "
100 1.7 rillig . error
101 1.7 rillig .endif
102 1.12 rillig
103 1.12 rillig # If at least one side of the comparison is a string literal, the string
104 1.12 rillig # comparison is performed.
105 1.12 rillig .if 12345 != "12345"
106 1.12 rillig . error
107 1.12 rillig .endif
108 1.12 rillig
109 1.12 rillig # If at least one side of the comparison is a string literal, the string
110 1.12 rillig # comparison is performed. The ".0" in the left-hand side makes the two
111 1.12 rillig # sides of the equation unequal.
112 1.12 rillig .if 12345.0 == "12345"
113 1.12 rillig . error
114 1.12 rillig .endif
115 1.14 rillig
116 1.14 rillig # Strings cannot be compared relationally, only for equality.
117 1.17 rillig # expect+1: Comparison with '<' requires both operands 'string' and 'string' to be numeric
118 1.14 rillig .if "string" < "string"
119 1.14 rillig . error
120 1.14 rillig .else
121 1.14 rillig . error
122 1.14 rillig .endif
123 1.14 rillig
124 1.14 rillig # Strings cannot be compared relationally, only for equality.
125 1.17 rillig # expect+1: Comparison with '<=' requires both operands 'string' and 'string' to be numeric
126 1.14 rillig .if "string" <= "string"
127 1.14 rillig . error
128 1.14 rillig .else
129 1.14 rillig . error
130 1.14 rillig .endif
131 1.14 rillig
132 1.14 rillig # Strings cannot be compared relationally, only for equality.
133 1.17 rillig # expect+1: Comparison with '>' requires both operands 'string' and 'string' to be numeric
134 1.14 rillig .if "string" > "string"
135 1.14 rillig . error
136 1.14 rillig .else
137 1.14 rillig . error
138 1.14 rillig .endif
139 1.14 rillig
140 1.14 rillig # Strings cannot be compared relationally, only for equality.
141 1.17 rillig # expect+1: Comparison with '>=' requires both operands 'string' and 'string' to be numeric
142 1.14 rillig .if "string" >= "string"
143 1.14 rillig . error
144 1.14 rillig .else
145 1.14 rillig . error
146 1.14 rillig .endif
147 1.16 rillig
148 1.16 rillig # Two variables with different values compare unequal.
149 1.16 rillig VAR1= value1
150 1.16 rillig VAR2= value2
151 1.16 rillig .if ${VAR1} != ${VAR2}
152 1.16 rillig .else
153 1.16 rillig . error
154 1.16 rillig .endif
155