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