Home | History | Annotate | Line # | Download | only in unit-tests
      1  1.6  rillig # $NetBSD: varname-dot-make-level.mk,v 1.6 2025/03/22 12:23:00 rillig Exp $
      2  1.1  rillig #
      3  1.3  rillig # Tests for the special .MAKE.LEVEL variable, which informs about the
      4  1.3  rillig # recursion level.  It is related to the environment variable MAKELEVEL,
      5  1.3  rillig # even though they don't have the same value.
      6  1.1  rillig 
      7  1.6  rillig all: .PHONY level_1 set-env-same set-env-different
      8  1.4  rillig 
      9  1.6  rillig # expect: level 1: variable 0, env 1
     10  1.3  rillig level_1: .PHONY
     11  1.3  rillig 	@printf 'level 1: variable %s, env %s\n' ${.MAKE.LEVEL} "$$${.MAKE.LEVEL.ENV}"
     12  1.3  rillig 	@${MAKE} -f ${MAKEFILE} level_2
     13  1.1  rillig 
     14  1.6  rillig # expect: level 2: variable 1, env 2
     15  1.3  rillig level_2: .PHONY
     16  1.3  rillig 	@printf 'level 2: variable %s, env %s\n' ${.MAKE.LEVEL} "$$${.MAKE.LEVEL.ENV}"
     17  1.3  rillig 	@${MAKE} -f ${MAKEFILE} level_3
     18  1.3  rillig 
     19  1.3  rillig # The .unexport-env directive clears the environment, except for the
     20  1.6  rillig # .MAKE.LEVEL.ENV make variable, which by default refers to the MAKELEVEL
     21  1.6  rillig # environment variable.
     22  1.3  rillig .if make(level_2)
     23  1.3  rillig .unexport-env
     24  1.3  rillig .endif
     25  1.4  rillig 
     26  1.6  rillig # expect: level 3: variable 2, env 3
     27  1.6  rillig level_3: .PHONY
     28  1.6  rillig 	@printf 'level 3: variable %s, env %s\n' ${.MAKE.LEVEL} "$$${.MAKE.LEVEL.ENV}"
     29  1.6  rillig 
     30  1.6  rillig 
     31  1.6  rillig # When a variable assignment from the command line tries to override a
     32  1.6  rillig # read-only global variable with the same value as before, ignore the
     33  1.6  rillig # assignment, as the variable value would not change.
     34  1.6  rillig #
     35  1.6  rillig # This special case allows older versions of make to coexist with newer
     36  1.6  rillig # versions of make. Older version of make (up to NetBSD 9) stored the internal
     37  1.6  rillig # .MAKE.LEVEL.ENV variable in the scope for command line variables, and these
     38  1.6  rillig # variables were passed to sub-makes via .MAKEOVERRIDES and the MAKEFLAGS
     39  1.6  rillig # environment variable. Newer versions of make (since NetBSD 11) store the
     40  1.6  rillig # internal .MAKE.LEVEL.ENV variable in the global scope but make it read-only
     41  1.6  rillig # and prevent any attempts to override it.
     42  1.6  rillig #
     43  1.6  rillig # https://gnats.netbsd.org/59184
     44  1.6  rillig set-env-same: .PHONY
     45  1.6  rillig 	: ${.TARGET}
     46  1.6  rillig 	@${MAKE} -f ${MAKEFILE} ok .MAKE.LEVEL.ENV=${.MAKE.LEVEL.ENV} || echo "${.TARGET}: exit $$?"
     47  1.6  rillig 
     48  1.4  rillig 
     49  1.5  rillig # expect: make: Cannot override read-only global variable ".MAKE.LEVEL.ENV" with a command line variable
     50  1.6  rillig set-env-different: .PHONY
     51  1.6  rillig 	: ${.TARGET}
     52  1.6  rillig 	@${MAKE} -f ${MAKEFILE} ok .MAKE.LEVEL.ENV=CUSTOM || echo "${.TARGET}: exit $$?"
     53  1.6  rillig 
     54  1.6  rillig ok: .PHONY
     55  1.6  rillig 	@echo ok
     56