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