Home | History | Annotate | Line # | Download | only in unit-tests
deptgt-makeflags.mk revision 1.8
      1  1.8  rillig # $NetBSD: deptgt-makeflags.mk,v 1.8 2023/11/19 21:47:52 rillig Exp $
      2  1.1  rillig #
      3  1.3  rillig # Tests for the special target .MAKEFLAGS in dependency declarations,
      4  1.3  rillig # which adds command line options later, at parse time.
      5  1.6  rillig #
      6  1.6  rillig # In these unit tests, it is often used to temporarily toggle the debug log
      7  1.6  rillig # during parsing.
      8  1.1  rillig 
      9  1.3  rillig # The -D option sets a variable in the "Global" scope and thus can be
     10  1.3  rillig # undefined later.
     11  1.3  rillig .MAKEFLAGS: -D VAR
     12  1.3  rillig .if ${VAR} != 1
     13  1.3  rillig .  error
     14  1.3  rillig .endif
     15  1.3  rillig 
     16  1.5  rillig # Variables that are set via the -D command line option are normal global
     17  1.5  rillig # variables and can thus be undefined later.
     18  1.3  rillig .undef VAR
     19  1.3  rillig .if defined(VAR)
     20  1.3  rillig .  error
     21  1.3  rillig .endif
     22  1.3  rillig 
     23  1.5  rillig # The -D command line option can define a variable again, after it has been
     24  1.5  rillig # undefined.
     25  1.3  rillig .MAKEFLAGS: -D VAR
     26  1.3  rillig .if ${VAR} != 1
     27  1.3  rillig .  error
     28  1.3  rillig .endif
     29  1.3  rillig 
     30  1.5  rillig # The "dependency" for .MAKEFLAGS is split into words, interpreting the usual
     31  1.5  rillig # quotes and escape sequences from the backslash.
     32  1.3  rillig .MAKEFLAGS: VAR="value"' with'\ spaces
     33  1.3  rillig .if ${VAR} != "value with spaces"
     34  1.3  rillig .  error
     35  1.3  rillig .endif
     36  1.3  rillig 
     37  1.3  rillig # Variables set on the command line as VAR=value are placed in the
     38  1.3  rillig # "Command" scope and thus cannot be undefined.
     39  1.3  rillig .undef VAR
     40  1.3  rillig .if ${VAR} != "value with spaces"
     41  1.3  rillig .  error
     42  1.3  rillig .endif
     43  1.1  rillig 
     44  1.4  rillig # When parsing this line, each '$$' becomes '$', resulting in '$$$$'.
     45  1.4  rillig # This is assigned to the variable DOLLAR.
     46  1.4  rillig # In the condition, that variable is expanded, and at that point, each '$$'
     47  1.4  rillig # becomes '$' again, the final expression is thus '$$'.
     48  1.4  rillig .MAKEFLAGS: -dcv
     49  1.4  rillig .MAKEFLAGS: DOLLAR=$$$$$$$$
     50  1.4  rillig .if ${DOLLAR} != "\$\$"
     51  1.4  rillig .endif
     52  1.4  rillig .MAKEFLAGS: -d0
     53  1.4  rillig 
     54  1.5  rillig # An empty command line is skipped.
     55  1.5  rillig .MAKEFLAGS: # none
     56  1.5  rillig 
     57  1.5  rillig # Escape sequences like \n are interpreted.
     58  1.5  rillig # The following line looks as if it assigned a newline to nl, but it doesn't.
     59  1.5  rillig # Instead, the \n ends up as a line that is then interpreted as a variable
     60  1.5  rillig # assignment.  At that point, the line is simply "nl=\n", and the \n is
     61  1.5  rillig # skipped since it is whitespace (see Parse_IsVar).
     62  1.5  rillig .MAKEFLAGS: nl="\n"
     63  1.5  rillig .if ${nl} != ""
     64  1.5  rillig .  error
     65  1.5  rillig .endif
     66  1.5  rillig 
     67  1.5  rillig # Next try at defining another newline variable.  Since whitespace around the
     68  1.8  rillig # variable value is trimmed, two empty expressions ${:U} surround the
     69  1.5  rillig # literal newline now.  This prevents the newline from being skipped during
     70  1.5  rillig # parsing.  The ':=' assignment operator expands the empty variable
     71  1.5  rillig # expressions, leaving only the newline as the variable value.
     72  1.5  rillig #
     73  1.5  rillig # This is one of the very few ways (maybe even the only one) to inject literal
     74  1.5  rillig # newlines into a line that is being parsed.  This may confuse the parser.
     75  1.5  rillig # For example, in cond.c the parser only expects horizontal whitespace (' '
     76  1.5  rillig # and '\t'), but no newlines.
     77  1.5  rillig #.MAKEFLAGS: -dcpv
     78  1.5  rillig .MAKEFLAGS: nl:="$${:U}\n$${:U}"
     79  1.5  rillig .if ${nl} != ${.newline}
     80  1.5  rillig .  error
     81  1.5  rillig .endif
     82  1.5  rillig #.MAKEFLAGS: -d0
     83  1.5  rillig 
     84  1.7  rillig # Now do the same for the other escape sequences; see Substring_Words.
     85  1.7  rillig .MAKEFLAGS: CHAR_BS:="$${:U}\b$${:U}"
     86  1.7  rillig .MAKEFLAGS: CHAR_FF:="$${:U}\f$${:U}"
     87  1.7  rillig .MAKEFLAGS: CHAR_NL:="$${:U}\n$${:U}"
     88  1.7  rillig .MAKEFLAGS: CHAR_CR:="$${:U}\r$${:U}"
     89  1.7  rillig .MAKEFLAGS: CHAR_TAB:="$${:U}\t$${:U}"
     90  1.7  rillig 
     91  1.7  rillig # Note: backspace is not whitespace, it is a control character.
     92  1.7  rillig .if ${CHAR_BS:C,^[[:cntrl:]]$,found,W} != "found"
     93  1.7  rillig .  error
     94  1.7  rillig .endif
     95  1.7  rillig .if ${CHAR_FF:C,^[[:space:]]$,found,W} != "found"
     96  1.7  rillig .  error
     97  1.7  rillig .endif
     98  1.7  rillig .if ${CHAR_NL:C,^[[:space:]]$,found,W} != "found"
     99  1.7  rillig .  error
    100  1.7  rillig .endif
    101  1.7  rillig .if ${CHAR_CR:C,^[[:space:]]$,found,W} != "found"
    102  1.7  rillig .  error
    103  1.7  rillig .endif
    104  1.7  rillig .if ${CHAR_TAB:C,^[[:space:]]$,found,W} != "found"
    105  1.7  rillig .  error
    106  1.7  rillig .endif
    107  1.7  rillig 
    108  1.7  rillig 
    109  1.5  rillig # Unbalanced quotes produce an error message.  If they occur anywhere in the
    110  1.5  rillig # command line, the whole command line is skipped.
    111  1.5  rillig .MAKEFLAGS: VAR=previous
    112  1.5  rillig .MAKEFLAGS: VAR=initial UNBALANCED='
    113  1.5  rillig .if ${VAR} != "previous"
    114  1.5  rillig .  error
    115  1.5  rillig .endif
    116  1.5  rillig 
    117  1.1  rillig all:
    118  1.1  rillig 	@:;
    119