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