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