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