Home | History | Annotate | Line # | Download | only in unit-tests
var-op-assign.mk revision 1.3
      1  1.3  rillig # $NetBSD: var-op-assign.mk,v 1.3 2020/08/25 16:07:39 rillig Exp $
      2  1.1  rillig #
      3  1.2  rillig # Tests for the = variable assignment operator, which overwrites an existing
      4  1.2  rillig # variable or creates it.
      5  1.1  rillig 
      6  1.3  rillig # This is a simple variable assignment.
      7  1.3  rillig # To the left of the assignment operator '=' there is the variable name,
      8  1.3  rillig # and to the right is the variable value.
      9  1.3  rillig #
     10  1.3  rillig VAR=	value
     11  1.3  rillig 
     12  1.3  rillig # This condition demonstrates that whitespace around the assignment operator
     13  1.3  rillig # is discarded.  Otherwise the value would start with a single tab.
     14  1.3  rillig #
     15  1.3  rillig .if ${VAR} != "value"
     16  1.3  rillig .error
     17  1.3  rillig .endif
     18  1.3  rillig 
     19  1.3  rillig # Whitespace to the left of the assignment operator is ignored as well.
     20  1.3  rillig # The variable value can contain arbitrary characters.
     21  1.3  rillig #
     22  1.3  rillig # The '#' needs to be escaped with a backslash, this happens in a very
     23  1.3  rillig # early stage of parsing and applies to all line types, except for the
     24  1.3  rillig # commands, which are indented with a tab.
     25  1.3  rillig #
     26  1.3  rillig # The '$' needs to be escaped with another '$', otherwise it would refer to
     27  1.3  rillig # another variable.
     28  1.3  rillig #
     29  1.3  rillig VAR	=new value and \# some $$ special characters	# comment
     30  1.3  rillig 
     31  1.3  rillig # When a string literal appears in a condition, the escaping rules are
     32  1.3  rillig # different.  Run make with the -dc option to see the details.
     33  1.3  rillig .if ${VAR} != "new value and \# some \$ special characters"
     34  1.3  rillig .error ${VAR}
     35  1.3  rillig .endif
     36  1.3  rillig 
     37  1.3  rillig # The variable value may contain references to other variables.
     38  1.3  rillig # In this example, the reference is to the variable with the empty name,
     39  1.3  rillig # which always expands to an empty string.  This alone would not produce
     40  1.3  rillig # any side-effects, therefore the variable has a :!...! modifier that
     41  1.3  rillig # executes a shell command.
     42  1.3  rillig VAR=	${:! echo 'not yet evaluated' 1>&2 !}
     43  1.3  rillig VAR=	${:! echo 'this will be evaluated later' 1>&2 !}
     44  1.3  rillig 
     45  1.3  rillig # Now force the variable to be evaluated.
     46  1.3  rillig # This outputs the line to stderr.
     47  1.3  rillig .if ${VAR}
     48  1.3  rillig .endif
     49  1.1  rillig 
     50  1.1  rillig all:
     51  1.1  rillig 	@:;
     52