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