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