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