1# 2# local variables: 3# o The keyword will make sure the variable is defined in 4# current variable context instead of the global one. 5# o Local variables are readable by children but not writable, 6# writes goes to the globle space (a sideeffect / feature). 7# o Local variables hides global and parent variables. 8# 9 10 11# global variable. 12var_exists1 = 1 13 14 15 16## 17# A simple define that is $(eval)uated. 18define def_test1 19 20# check that the existing variable is accessible. 21ifneq ($(var_exists1),1) 22 $(error var_exists1=$(var_exists1) (def_test1/1)) 23endif 24 25# Quick check with a couple of local variables. 26local var_local1 = 2 27ifneq ($(var_local1),2) 28 $(error var_local1=$(var_local1) (def_test1/2)) 29endif 30local var_local2 = 3 31ifneq ($(var_local2),3) 32 $(error var_local2=$(var_local2) (def_test1/3)) 33endif 34 35# var_local1 and var_local2 should remain unchanged, var_local3 shouldn't exist. 36$(evalctx $(value def_test2)) 37 38ifneq ($(var_local1),2) 39 $(error var_local1=$(var_local1) (def_test1/4)) 40endif 41ifneq ($(var_local2),3) 42 $(error var_local2=$(var_local2) (def_test1/5)) 43endif 44ifneq ($(var_local3),) 45 $(error var_local3=$(var_local3) (def_test1/6)) 46endif 47 48endef # def_test1 49 50 51 52## 53# Called by def_test1, this checks that the locals of def_test1 54# are accessible and can be hidden by another local variable 55# or updated if assigned to. 56define def_test2 57 58# check that the existing variables are accessible, including the def_test1 ones. 59ifneq ($(var_exists1),1) 60 $(error var_exists1=$(var_exists1) (def_test2/1)) 61endif 62ifneq ($(var_local1),2) 63 $(error var_local1=$(var_local1) (def_test2/2)) 64endif 65ifneq ($(var_local2),3) 66 $(error var_local2=$(var_local2) (def_test2/3)) 67endif 68 69# Make a local var_local1 that hides the one in def_test1. 70local var_local1 = 20 71ifneq ($(var_local1),20) 72 $(error var_local1=$(var_local1) (def_test2/4)) 73endif 74 75# FEATURE: Update the var_local2 variable, this should be visible in the global space and not the local. 76var_local2 = 30 77ifneq ($(var_local2),3) 78 $(error var_local2=$(var_local2) (def_test2/5)) 79endif 80 81# create a new local variable that isn't accessible from def_test1. 82local var_local3 = 4 83ifneq ($(var_local3),4) 84 $(error var_local3=$(var_local3) (def_test2/6)) 85endif 86 87endef # def_test2 88 89 90 91# 92# The test body 93# 94 95# None of the local variables should exist. 96ifneq ($(var_local1),) 97 $(error var_local1=$(var_local1)) 98endif 99ifneq ($(var_local2),) 100 $(error var_local2=$(var_local2)) 101endif 102ifneq ($(var_local3),) 103 $(error var_local3=$(var_local3)) 104endif 105 106# Evaluate the function in a local context. 107$(evalctx $(value def_test1)) 108 109# FEATURE: see var_local2 = 30 in def_test2. 110ifneq ($(var_local2),30) 111 $(error var_local2=$(var_local2)) 112endif 113 114# None of the other local variables should exist. 115ifneq ($(var_local1),) 116 $(error var_local1=$(var_local1)) 117endif 118ifneq ($(var_local3),) 119 $(error var_local3=$(var_local3)) 120endif 121 122 123 124# dummy 125all: 126 echo local variables works. 127 128