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