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