escape.mk revision 1.15 1 1.15 rillig # $NetBSD: escape.mk,v 1.15 2023/10/19 18:24:33 rillig Exp $
2 1.1 apb #
3 1.1 apb # Test backslash escaping.
4 1.1 apb
5 1.1 apb # Extracts from the POSIX 2008 specification
6 1.1 apb # <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html>:
7 1.1 apb #
8 1.1 apb # Comments start with a <number-sign> ( '#' ) and continue until an
9 1.1 apb # unescaped <newline> is reached.
10 1.1 apb #
11 1.1 apb # When an escaped <newline> (one preceded by a <backslash>) is found
12 1.1 apb # anywhere in the makefile except in a command line, an include
13 1.1 apb # line, or a line immediately preceding an include line, it shall
14 1.1 apb # be replaced, along with any leading white space on the following
15 1.1 apb # line, with a single <space>.
16 1.1 apb #
17 1.1 apb # When an escaped <newline> is found in a command line in a
18 1.1 apb # makefile, the command line shall contain the <backslash>, the
19 1.1 apb # <newline>, and the next line, except that the first character of
20 1.1 apb # the next line shall not be included if it is a <tab>.
21 1.1 apb #
22 1.1 apb # When an escaped <newline> is found in an include line or in a
23 1.1 apb # line immediately preceding an include line, the behavior is
24 1.1 apb # unspecified.
25 1.1 apb #
26 1.1 apb # Notice that the behaviour of <backslash><backslash> or
27 1.2 apb # <backslash><anything other than newline> is not mentioned. I think
28 1.1 apb # this implies that <backslash> should be taken literally everywhere
29 1.1 apb # except before <newline>.
30 1.6 apb #
31 1.6 apb # Our practice, despite what POSIX might say, is that "\#"
32 1.6 apb # in a variable assignment stores "#" as part of the value.
33 1.6 apb # The "\" is not taken literally, and the "#" does not begin a comment.
34 1.7 apb #
35 1.8 apb # Also, our practice is that an even number of backslashes before a
36 1.8 apb # newline in a variable assignment simply stores the backslashes as part
37 1.8 apb # of the value, and treats the newline as though it was not escaped.
38 1.11 rillig # Similarly, an even number of backslashes before a newline in a
39 1.11 rillig # command simply uses the backslashes as part of the command, but
40 1.8 apb # does not escape the newline. This is compatible with GNU make.
41 1.1 apb
42 1.1 apb all: .PHONY
43 1.1 apb # We will add dependencies like "all: yet-another-test" later.
44 1.1 apb
45 1.1 apb # Some variables to be expanded in tests
46 1.1 apb #
47 1.13 rillig a= aaa
48 1.13 rillig A= ${a}
49 1.1 apb
50 1.1 apb # Backslash at end of line in a comment\
51 1.1 apb should continue the comment. \
52 1.1 apb # This is also tested in comment.mk.
53 1.1 apb
54 1.5 apb __printvars: .USE .MADE
55 1.5 apb @echo ${.TARGET}
56 1.15 rillig @${.ALLSRC:@v@ printf "%s=:%s:\n" ${v:Q} ${${v}:Q}; @}
57 1.5 apb
58 1.1 apb # Embedded backslash in variable should be taken literally.
59 1.1 apb #
60 1.13 rillig VAR1BS= 111\111
61 1.13 rillig VAR1BSa= 111\${a}
62 1.13 rillig VAR1BSA= 111\${A}
63 1.13 rillig VAR1BSda= 111\$${a}
64 1.13 rillig VAR1BSdA= 111\$${A}
65 1.13 rillig VAR1BSc= 111\# backslash escapes comment char, so this is part of the value
66 1.13 rillig VAR1BSsc= 111\ # This is a comment. Value ends with <backslash><space>
67 1.1 apb
68 1.1 apb all: var-1bs
69 1.6 apb var-1bs: .PHONY __printvars VAR1BS VAR1BSa VAR1BSA VAR1BSda VAR1BSdA \
70 1.6 apb VAR1BSc VAR1BSsc
71 1.1 apb
72 1.1 apb # Double backslash in variable should be taken as two literal backslashes.
73 1.1 apb #
74 1.13 rillig VAR2BS= 222\\222
75 1.13 rillig VAR2BSa= 222\\${a}
76 1.13 rillig VAR2BSA= 222\\${A}
77 1.13 rillig VAR2BSda= 222\\$${a}
78 1.13 rillig VAR2BSdA= 222\\$${A}
79 1.13 rillig VAR2BSc= 222\\# backslash does not escape comment char, so this is a comment
80 1.13 rillig VAR2BSsc= 222\\ # This is a comment. Value ends with <backslash><backslash>
81 1.1 apb
82 1.1 apb all: var-2bs
83 1.6 apb var-2bs: .PHONY __printvars VAR2BS VAR2BSa VAR2BSA VAR2BSda VAR2BSdA \
84 1.6 apb VAR2BSc VAR2BSsc
85 1.1 apb
86 1.15 rillig # In a variable assignment, when the sequence <backslash><newline> occurs at
87 1.15 rillig # the end of a physical line, it is replaced with a single space.
88 1.1 apb #
89 1.13 rillig VAR1BSNL= 111\
90 1.1 apb 111
91 1.13 rillig VAR1BSNLa= 111\
92 1.1 apb ${a}
93 1.13 rillig VAR1BSNLA= 111\
94 1.1 apb ${A}
95 1.13 rillig VAR1BSNLda= 111\
96 1.1 apb $${a}
97 1.13 rillig VAR1BSNLdA= 111\
98 1.1 apb $${A}
99 1.13 rillig VAR1BSNLc= 111\
100 1.4 apb # this should be processed as a comment
101 1.13 rillig VAR1BSNLsc= 111\
102 1.6 apb # this should be processed as a comment
103 1.1 apb
104 1.1 apb all: var-1bsnl
105 1.1 apb var-1bsnl: .PHONY
106 1.5 apb var-1bsnl: .PHONY __printvars \
107 1.6 apb VAR1BSNL VAR1BSNLa VAR1BSNLA VAR1BSNLda VAR1BSNLdA \
108 1.6 apb VAR1BSNLc VAR1BSNLsc
109 1.1 apb
110 1.1 apb # Double-backslash-newline in a variable setting.
111 1.7 apb # Both backslashes should be taken literally, and the newline is NOT escaped.
112 1.1 apb #
113 1.4 apb # The second lines below each end with '=' so that they will not
114 1.4 apb # generate syntax errors regardless of whether or not they are
115 1.4 apb # treated as part of the value.
116 1.1 apb #
117 1.13 rillig VAR2BSNL= 222\\
118 1.4 apb 222=
119 1.13 rillig VAR2BSNLa= 222\\
120 1.4 apb ${a}=
121 1.13 rillig VAR2BSNLA= 222\\
122 1.4 apb ${A}=
123 1.13 rillig VAR2BSNLda= 222\\
124 1.4 apb $${a}=
125 1.13 rillig VAR2BSNLdA= 222\\
126 1.4 apb $${A}=
127 1.13 rillig VAR2BSNLc= 222\\
128 1.4 apb # this should be processed as a comment
129 1.13 rillig VAR2BSNLsc= 222\\
130 1.6 apb # this should be processed as a comment
131 1.1 apb
132 1.1 apb all: var-2bsnl
133 1.5 apb var-2bsnl: .PHONY __printvars \
134 1.6 apb VAR2BSNL VAR2BSNLa VAR2BSNLA VAR2BSNLda VAR2BSNLdA \
135 1.7 apb VAR2BSNLc VAR2BSNLsc
136 1.1 apb
137 1.1 apb # Triple-backslash-newline in a variable setting.
138 1.1 apb # First two should be taken literally, and last should escape the newline.
139 1.1 apb #
140 1.4 apb # The second lines below each end with '=' so that they will not
141 1.4 apb # generate syntax errors regardless of whether or not they are
142 1.4 apb # treated as part of the value.
143 1.1 apb #
144 1.13 rillig VAR3BSNL= 333\\\
145 1.4 apb 333=
146 1.13 rillig VAR3BSNLa= 333\\\
147 1.4 apb ${a}=
148 1.13 rillig VAR3BSNLA= 333\\\
149 1.4 apb ${A}=
150 1.13 rillig VAR3BSNLda= 333\\\
151 1.4 apb $${a}=
152 1.13 rillig VAR3BSNLdA= 333\\\
153 1.4 apb $${A}=
154 1.13 rillig VAR3BSNLc= 333\\\
155 1.4 apb # this should be processed as a comment
156 1.13 rillig VAR3BSNLsc= 333\\\
157 1.6 apb # this should be processed as a comment
158 1.1 apb
159 1.1 apb all: var-3bsnl
160 1.5 apb var-3bsnl: .PHONY __printvars \
161 1.6 apb VAR3BSNL VAR3BSNLa VAR3BSNLA VAR3BSNLda VAR3BSNLdA \
162 1.6 apb VAR3BSNLc VAR3BSNLsc
163 1.1 apb
164 1.1 apb # Backslash-newline in a variable setting, plus any amount of white space
165 1.1 apb # on the next line, is replaced by a single space.
166 1.1 apb #
167 1.13 rillig VAR1BSNL00= first line\
168 1.1 apb
169 1.1 apb # above line is entirely empty, and this is a comment
170 1.13 rillig VAR1BSNL0= first line\
171 1.1 apb no space on second line
172 1.13 rillig VAR1BSNLs= first line\
173 1.1 apb one space on second line
174 1.13 rillig VAR1BSNLss= first line\
175 1.1 apb two spaces on second line
176 1.13 rillig VAR1BSNLt= first line\
177 1.1 apb one tab on second line
178 1.13 rillig VAR1BSNLtt= first line\
179 1.1 apb two tabs on second line
180 1.13 rillig VAR1BSNLxx= first line\
181 1.1 apb many spaces and tabs [ ] on second line
182 1.1 apb
183 1.1 apb all: var-1bsnl-space
184 1.5 apb var-1bsnl-space: .PHONY __printvars \
185 1.5 apb VAR1BSNL00 VAR1BSNL0 VAR1BSNLs VAR1BSNLss VAR1BSNLt VAR1BSNLtt \
186 1.5 apb VAR1BSNLxx
187 1.1 apb
188 1.1 apb # Backslash-newline in a command is retained.
189 1.1 apb #
190 1.1 apb # The "#" in "# second line without space" makes it a comment instead
191 1.14 rillig # of a syntax error if the preceding line is parsed incorrectly.
192 1.1 apb # The ":" in "third line':" makes it look like the start of a
193 1.1 apb # target instead of a syntax error if the first line is parsed incorrectly.
194 1.1 apb #
195 1.1 apb all: cmd-1bsnl
196 1.1 apb cmd-1bsnl: .PHONY
197 1.1 apb @echo ${.TARGET}
198 1.9 apb echo :'first line\
199 1.1 apb #second line without space\
200 1.1 apb third line':
201 1.9 apb echo :'first line\
202 1.1 apb second line spaces should be retained':
203 1.9 apb echo :'first line\
204 1.1 apb second line tab should be elided':
205 1.9 apb echo :'first line\
206 1.1 apb only one tab should be elided, second tab remains'
207 1.1 apb
208 1.10 apb # When backslash-newline appears at the end of a command script,
209 1.10 apb # both the backslash and the newline should be passed to the shell.
210 1.10 apb # The shell should elide the backslash-newline.
211 1.10 apb #
212 1.10 apb all: cmd-1bsnl-eof
213 1.10 apb cmd-1bsnl-eof:
214 1.10 apb @echo ${.TARGET}
215 1.10 apb echo :'command ending with backslash-newline'; \
216 1.10 apb
217 1.10 apb # above line must be blank
218 1.10 apb
219 1.8 apb # Double-backslash-newline in a command.
220 1.8 apb # Both backslashes are retained, but the newline is not escaped.
221 1.8 apb # XXX: This may differ from POSIX, but matches gmake.
222 1.8 apb #
223 1.8 apb # When make passes two backslashes to the shell, the shell will pass one
224 1.14 rillig # backslash to the echo command.
225 1.1 apb #
226 1.1 apb all: cmd-2bsnl
227 1.1 apb cmd-2bsnl: .PHONY
228 1.1 apb @echo ${.TARGET}
229 1.9 apb echo take one\\
230 1.8 apb # this should be a comment
231 1.9 apb echo take two\\
232 1.9 apb echo take three\\
233 1.1 apb
234 1.1 apb # Triple-backslash-newline in a command is retained.
235 1.1 apb #
236 1.1 apb all: cmd-3bsnl
237 1.1 apb cmd-3bsnl: .PHONY
238 1.1 apb @echo ${.TARGET}
239 1.9 apb echo :'first line\\\
240 1.1 apb #second line without space\\\
241 1.1 apb third line':
242 1.9 apb echo :'first line\\\
243 1.1 apb second line spaces should be retained':
244 1.9 apb echo :'first line\\\
245 1.1 apb second line tab should be elided':
246 1.9 apb echo :'first line\\\
247 1.1 apb only one tab should be elided, second tab remains'
248