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