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