escape.mk revision 1.7 1 1.7 apb # $Id: escape.mk,v 1.7 2014/08/24 16:08:14 apb 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.7 apb # Also, our practice is that an even number of backslashes before a newline
36 1.7 apb # in a variable assignment simply stores the backslashes as part of the
37 1.7 apb # value, and treats the newline as though it was not escaped. This
38 1.7 apb # is compatible with GNU make.
39 1.1 apb
40 1.1 apb all: .PHONY
41 1.1 apb # We will add dependencies like "all: yet-another-test" later.
42 1.1 apb
43 1.1 apb # Some variables to be expanded in tests
44 1.1 apb #
45 1.1 apb a = aaa
46 1.1 apb A = ${a}
47 1.1 apb
48 1.1 apb # Backslash at end of line in a comment\
49 1.1 apb should continue the comment. \
50 1.1 apb # This is also tested in comment.mk.
51 1.1 apb
52 1.5 apb __printvars: .USE .MADE
53 1.5 apb @echo ${.TARGET}
54 1.5 apb @${.ALLSRC:@v@ printf "%s=:%s:\n" ${v:Q} ${${v}:Q}; @}
55 1.5 apb
56 1.1 apb # Embedded backslash in variable should be taken literally.
57 1.1 apb #
58 1.1 apb VAR1BS = 111\111
59 1.1 apb VAR1BSa = 111\${a}
60 1.1 apb VAR1BSA = 111\${A}
61 1.1 apb VAR1BSda = 111\$${a}
62 1.1 apb VAR1BSdA = 111\$${A}
63 1.6 apb VAR1BSc = 111\# backslash escapes comment char, so this is part of the value
64 1.6 apb VAR1BSsc = 111\ # This is a comment. Value ends with <backslash><space>
65 1.1 apb
66 1.1 apb all: var-1bs
67 1.6 apb var-1bs: .PHONY __printvars VAR1BS VAR1BSa VAR1BSA VAR1BSda VAR1BSdA \
68 1.6 apb VAR1BSc VAR1BSsc
69 1.1 apb
70 1.1 apb # Double backslash in variable should be taken as two literal backslashes.
71 1.1 apb #
72 1.1 apb VAR2BS = 222\\222
73 1.1 apb VAR2BSa = 222\\${a}
74 1.1 apb VAR2BSA = 222\\${A}
75 1.1 apb VAR2BSda = 222\\$${a}
76 1.1 apb VAR2BSdA = 222\\$${A}
77 1.6 apb VAR2BSc = 222\\# backslash does not escape comment char, so this is a comment
78 1.6 apb VAR2BSsc = 222\\ # This is a comment. Value ends with <backslash><backslash>
79 1.1 apb
80 1.1 apb all: var-2bs
81 1.6 apb var-2bs: .PHONY __printvars VAR2BS VAR2BSa VAR2BSA VAR2BSda VAR2BSdA \
82 1.6 apb VAR2BSc VAR2BSsc
83 1.1 apb
84 1.1 apb # Backslash-newline in a variable setting is replaced by a single space.
85 1.1 apb #
86 1.1 apb VAR1BSNL = 111\
87 1.1 apb 111
88 1.1 apb VAR1BSNLa = 111\
89 1.1 apb ${a}
90 1.1 apb VAR1BSNLA = 111\
91 1.1 apb ${A}
92 1.1 apb VAR1BSNLda = 111\
93 1.1 apb $${a}
94 1.1 apb VAR1BSNLdA = 111\
95 1.1 apb $${A}
96 1.1 apb VAR1BSNLc = 111\
97 1.4 apb # this should be processed as a comment
98 1.6 apb VAR1BSNLsc = 111\
99 1.6 apb # this should be processed as a comment
100 1.1 apb
101 1.1 apb all: var-1bsnl
102 1.1 apb var-1bsnl: .PHONY
103 1.5 apb var-1bsnl: .PHONY __printvars \
104 1.6 apb VAR1BSNL VAR1BSNLa VAR1BSNLA VAR1BSNLda VAR1BSNLdA \
105 1.6 apb VAR1BSNLc VAR1BSNLsc
106 1.1 apb
107 1.1 apb # Double-backslash-newline in a variable setting.
108 1.7 apb # Both backslashes should be taken literally, and the newline is NOT escaped.
109 1.1 apb #
110 1.4 apb # The second lines below each end with '=' so that they will not
111 1.4 apb # generate syntax errors regardless of whether or not they are
112 1.4 apb # treated as part of the value.
113 1.1 apb #
114 1.1 apb VAR2BSNL = 222\\
115 1.4 apb 222=
116 1.1 apb VAR2BSNLa = 222\\
117 1.4 apb ${a}=
118 1.1 apb VAR2BSNLA = 222\\
119 1.4 apb ${A}=
120 1.1 apb VAR2BSNLda = 222\\
121 1.4 apb $${a}=
122 1.1 apb VAR2BSNLdA = 222\\
123 1.4 apb $${A}=
124 1.1 apb VAR2BSNLc = 222\\
125 1.4 apb # this should be processed as a comment
126 1.6 apb VAR2BSNLsc = 222\\
127 1.6 apb # this should be processed as a comment
128 1.1 apb
129 1.1 apb all: var-2bsnl
130 1.5 apb var-2bsnl: .PHONY __printvars \
131 1.6 apb VAR2BSNL VAR2BSNLa VAR2BSNLA VAR2BSNLda VAR2BSNLdA \
132 1.7 apb VAR2BSNLc VAR2BSNLsc
133 1.1 apb
134 1.1 apb # Triple-backslash-newline in a variable setting.
135 1.1 apb # First two should be taken literally, and last should escape the newline.
136 1.1 apb #
137 1.4 apb # The second lines below each end with '=' so that they will not
138 1.4 apb # generate syntax errors regardless of whether or not they are
139 1.4 apb # treated as part of the value.
140 1.1 apb #
141 1.1 apb VAR3BSNL = 333\\\
142 1.4 apb 333=
143 1.1 apb VAR3BSNLa = 333\\\
144 1.4 apb ${a}=
145 1.1 apb VAR3BSNLA = 333\\\
146 1.4 apb ${A}=
147 1.1 apb VAR3BSNLda = 333\\\
148 1.4 apb $${a}=
149 1.1 apb VAR3BSNLdA = 333\\\
150 1.4 apb $${A}=
151 1.1 apb VAR3BSNLc = 333\\\
152 1.4 apb # this should be processed as a comment
153 1.6 apb VAR3BSNLsc = 333\\\
154 1.6 apb # this should be processed as a comment
155 1.1 apb
156 1.1 apb all: var-3bsnl
157 1.5 apb var-3bsnl: .PHONY __printvars \
158 1.6 apb VAR3BSNL VAR3BSNLa VAR3BSNLA VAR3BSNLda VAR3BSNLdA \
159 1.6 apb VAR3BSNLc VAR3BSNLsc
160 1.1 apb
161 1.1 apb # Backslash-newline in a variable setting, plus any amount of white space
162 1.1 apb # on the next line, is replaced by a single space.
163 1.1 apb #
164 1.1 apb VAR1BSNL00= first line\
165 1.1 apb
166 1.1 apb # above line is entirely empty, and this is a comment
167 1.1 apb VAR1BSNL0= first line\
168 1.1 apb no space on second line
169 1.1 apb VAR1BSNLs= first line\
170 1.1 apb one space on second line
171 1.1 apb VAR1BSNLss= first line\
172 1.1 apb two spaces on second line
173 1.1 apb VAR1BSNLt= first line\
174 1.1 apb one tab on second line
175 1.1 apb VAR1BSNLtt= first line\
176 1.1 apb two tabs on second line
177 1.1 apb VAR1BSNLxx= first line\
178 1.1 apb many spaces and tabs [ ] on second line
179 1.1 apb
180 1.1 apb all: var-1bsnl-space
181 1.5 apb var-1bsnl-space: .PHONY __printvars \
182 1.5 apb VAR1BSNL00 VAR1BSNL0 VAR1BSNLs VAR1BSNLss VAR1BSNLt VAR1BSNLtt \
183 1.5 apb VAR1BSNLxx
184 1.1 apb
185 1.1 apb # Backslash-newline in a command is retained.
186 1.1 apb #
187 1.1 apb # The "#" in "# second line without space" makes it a comment instead
188 1.1 apb # of a syntax error if the preceding line is parsed incorretly.
189 1.1 apb # The ":" in "third line':" makes it look like the start of a
190 1.1 apb # target instead of a syntax error if the first line is parsed incorrectly.
191 1.1 apb #
192 1.1 apb all: cmd-1bsnl
193 1.1 apb cmd-1bsnl: .PHONY
194 1.1 apb @echo ${.TARGET}
195 1.1 apb @echo :'first line\
196 1.1 apb #second line without space\
197 1.1 apb third line':
198 1.1 apb @echo :'first line\
199 1.1 apb second line spaces should be retained':
200 1.1 apb @echo :'first line\
201 1.1 apb second line tab should be elided':
202 1.1 apb @echo :'first line\
203 1.1 apb only one tab should be elided, second tab remains'
204 1.1 apb
205 1.1 apb # Double-backslash-newline in a command is retained.
206 1.1 apb #
207 1.1 apb all: cmd-2bsnl
208 1.1 apb cmd-2bsnl: .PHONY
209 1.1 apb @echo ${.TARGET}
210 1.1 apb @echo :'first line\\
211 1.1 apb #second line without space\\
212 1.1 apb third line':
213 1.1 apb @echo :'first line\\
214 1.1 apb second line spaces should be retained':
215 1.1 apb @echo :'first line\\
216 1.1 apb second line tab should be elided':
217 1.1 apb @echo :'first line\\
218 1.1 apb only one tab should be elided, second tab remains'
219 1.1 apb
220 1.1 apb # Triple-backslash-newline in a command is retained.
221 1.1 apb #
222 1.1 apb all: cmd-3bsnl
223 1.1 apb cmd-3bsnl: .PHONY
224 1.1 apb @echo ${.TARGET}
225 1.1 apb @echo :'first line\\\
226 1.1 apb #second line without space\\\
227 1.1 apb third line':
228 1.1 apb @echo :'first line\\\
229 1.1 apb second line spaces should be retained':
230 1.1 apb @echo :'first line\\\
231 1.1 apb second line tab should be elided':
232 1.1 apb @echo :'first line\\\
233 1.1 apb only one tab should be elided, second tab remains'
234