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.
|
---|
12 | var_exists1 = 1
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 | ##
|
---|
17 | # A simple define that is $(eval)uated.
|
---|
18 | define def_test1
|
---|
19 |
|
---|
20 | # check that the existing variable is accessible.
|
---|
21 | ifneq ($(var_exists1),1)
|
---|
22 | $(error var_exists1=$(var_exists1) (def_test1/1))
|
---|
23 | endif
|
---|
24 |
|
---|
25 | # Quick check with a couple of local variables.
|
---|
26 | local var_local1 = 2
|
---|
27 | ifneq ($(var_local1),2)
|
---|
28 | $(error var_local1=$(var_local1) (def_test1/2))
|
---|
29 | endif
|
---|
30 | local var_local2 = 3
|
---|
31 | ifneq ($(var_local2),3)
|
---|
32 | $(error var_local2=$(var_local2) (def_test1/3))
|
---|
33 | endif
|
---|
34 |
|
---|
35 | # var_local1 and var_local2 should remain unchanged, var_local3 shouldn't exist.
|
---|
36 | $(evalctx $(value def_test2))
|
---|
37 |
|
---|
38 | ifneq ($(var_local1),2)
|
---|
39 | $(error var_local1=$(var_local1) (def_test1/4))
|
---|
40 | endif
|
---|
41 | ifneq ($(var_local2),3)
|
---|
42 | $(error var_local2=$(var_local2) (def_test1/5))
|
---|
43 | endif
|
---|
44 | ifneq ($(var_local3),)
|
---|
45 | $(error var_local3=$(var_local3) (def_test1/6))
|
---|
46 | endif
|
---|
47 |
|
---|
48 | endef # 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.
|
---|
56 | define def_test2
|
---|
57 |
|
---|
58 | # check that the existing variables are accessible, including the def_test1 ones.
|
---|
59 | ifneq ($(var_exists1),1)
|
---|
60 | $(error var_exists1=$(var_exists1) (def_test2/1))
|
---|
61 | endif
|
---|
62 | ifneq ($(var_local1),2)
|
---|
63 | $(error var_local1=$(var_local1) (def_test2/2))
|
---|
64 | endif
|
---|
65 | ifneq ($(var_local2),3)
|
---|
66 | $(error var_local2=$(var_local2) (def_test2/3))
|
---|
67 | endif
|
---|
68 |
|
---|
69 | # Make a local var_local1 that hides the one in def_test1.
|
---|
70 | local var_local1 = 20
|
---|
71 | ifneq ($(var_local1),20)
|
---|
72 | $(error var_local1=$(var_local1) (def_test2/4))
|
---|
73 | endif
|
---|
74 |
|
---|
75 | # FEATURE: Update the var_local2 variable, this should be visible in the global space and not the local.
|
---|
76 | var_local2 = 30
|
---|
77 | ifneq ($(var_local2),3)
|
---|
78 | $(error var_local2=$(var_local2) (def_test2/5))
|
---|
79 | endif
|
---|
80 |
|
---|
81 | # create a new local variable that isn't accessible from def_test1.
|
---|
82 | local var_local3 = 4
|
---|
83 | ifneq ($(var_local3),4)
|
---|
84 | $(error var_local3=$(var_local3) (def_test2/6))
|
---|
85 | endif
|
---|
86 |
|
---|
87 | endef # def_test2
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | #
|
---|
92 | # The test body
|
---|
93 | #
|
---|
94 |
|
---|
95 | # None of the local variables should exist.
|
---|
96 | ifneq ($(var_local1),)
|
---|
97 | $(error var_local1=$(var_local1))
|
---|
98 | endif
|
---|
99 | ifneq ($(var_local2),)
|
---|
100 | $(error var_local2=$(var_local2))
|
---|
101 | endif
|
---|
102 | ifneq ($(var_local3),)
|
---|
103 | $(error var_local3=$(var_local3))
|
---|
104 | endif
|
---|
105 |
|
---|
106 | # Evaluate the function in a local context.
|
---|
107 | $(evalctx $(value def_test1))
|
---|
108 |
|
---|
109 | # FEATURE: see var_local2 = 30 in def_test2.
|
---|
110 | ifneq ($(var_local2),30)
|
---|
111 | $(error var_local2=$(var_local2))
|
---|
112 | endif
|
---|
113 |
|
---|
114 | # None of the other local variables should exist.
|
---|
115 | ifneq ($(var_local1),)
|
---|
116 | $(error var_local1=$(var_local1))
|
---|
117 | endif
|
---|
118 | ifneq ($(var_local3),)
|
---|
119 | $(error var_local3=$(var_local3))
|
---|
120 | endif
|
---|
121 |
|
---|
122 |
|
---|
123 |
|
---|
124 | # dummy
|
---|
125 | all:
|
---|
126 | echo local variables works.
|
---|
127 |
|
---|