Home | History | Annotate | Line # | Download | only in unit-tests
escape.mk revision 1.11
      1 # $Id: escape.mk,v 1.11 2020/01/07 20:50:12 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 
     42 all: .PHONY
     43 # We will add dependencies like "all: yet-another-test" later.
     44 
     45 # Some variables to be expanded in tests
     46 #
     47 a = aaa
     48 A = ${a}
     49 
     50 # Backslash at end of line in a comment\
     51 should 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 #
     60 VAR1BS = 111\111
     61 VAR1BSa = 111\${a}
     62 VAR1BSA = 111\${A}
     63 VAR1BSda = 111\$${a}
     64 VAR1BSdA = 111\$${A}
     65 VAR1BSc = 111\# backslash escapes comment char, so this is part of the value
     66 VAR1BSsc = 111\ # This is a comment.  Value ends with <backslash><space>
     67 
     68 all: var-1bs
     69 var-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 #
     74 VAR2BS = 222\\222
     75 VAR2BSa = 222\\${a}
     76 VAR2BSA = 222\\${A}
     77 VAR2BSda = 222\\$${a}
     78 VAR2BSdA = 222\\$${A}
     79 VAR2BSc = 222\\# backslash does not escape comment char, so this is a comment
     80 VAR2BSsc = 222\\ # This is a comment.  Value ends with <backslash><backslash>
     81 
     82 all: var-2bs
     83 var-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 #
     88 VAR1BSNL = 111\
     89 111
     90 VAR1BSNLa = 111\
     91 ${a}
     92 VAR1BSNLA = 111\
     93 ${A}
     94 VAR1BSNLda = 111\
     95 $${a}
     96 VAR1BSNLdA = 111\
     97 $${A}
     98 VAR1BSNLc = 111\
     99 # this should be processed as a comment
    100 VAR1BSNLsc = 111\
    101  # this should be processed as a comment
    102 
    103 all: var-1bsnl
    104 var-1bsnl:	.PHONY
    105 var-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 #
    116 VAR2BSNL = 222\\
    117 222=
    118 VAR2BSNLa = 222\\
    119 ${a}=
    120 VAR2BSNLA = 222\\
    121 ${A}=
    122 VAR2BSNLda = 222\\
    123 $${a}=
    124 VAR2BSNLdA = 222\\
    125 $${A}=
    126 VAR2BSNLc = 222\\
    127 # this should be processed as a comment
    128 VAR2BSNLsc = 222\\
    129  # this should be processed as a comment
    130 
    131 all: var-2bsnl
    132 var-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 #
    143 VAR3BSNL = 333\\\
    144 333=
    145 VAR3BSNLa = 333\\\
    146 ${a}=
    147 VAR3BSNLA = 333\\\
    148 ${A}=
    149 VAR3BSNLda = 333\\\
    150 $${a}=
    151 VAR3BSNLdA = 333\\\
    152 $${A}=
    153 VAR3BSNLc = 333\\\
    154 # this should be processed as a comment
    155 VAR3BSNLsc = 333\\\
    156  # this should be processed as a comment
    157 
    158 all: var-3bsnl
    159 var-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 #
    166 VAR1BSNL00= first line\
    167 
    168 # above line is entirely empty, and this is a comment
    169 VAR1BSNL0= first line\
    170 no space on second line
    171 VAR1BSNLs= first line\
    172  one space on second line
    173 VAR1BSNLss= first line\
    174   two spaces on second line
    175 VAR1BSNLt= first line\
    176 	one tab on second line
    177 VAR1BSNLtt= first line\
    178 		two tabs on second line
    179 VAR1BSNLxx= first line\
    180   	 	 	 many spaces and tabs [  	 ] on second line
    181 
    182 all: var-1bsnl-space
    183 var-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 incorretly.
    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 #
    194 all: cmd-1bsnl
    195 cmd-1bsnl: .PHONY
    196 	@echo ${.TARGET}
    197 	echo :'first line\
    198 #second line without space\
    199 third 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 #
    211 all: cmd-1bsnl-eof
    212 cmd-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 commant.
    224 #
    225 all: cmd-2bsnl
    226 cmd-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 #
    235 all: cmd-3bsnl
    236 cmd-3bsnl: .PHONY
    237 	@echo ${.TARGET}
    238 	echo :'first line\\\
    239 #second line without space\\\
    240 third 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