Add support for "::=" simple assignment operator.
[make.git] / tests / scripts / variables / define
blob68d493b1b387273ac079a4f32547b88bc1ce910a
1 #                                                                    -*-perl-*-
3 $description = "Test define/endef variable assignments.";
5 $details = "";
7 # TEST 0: old-style basic define/endef
9 run_make_test('
10 define multi
11 @echo hi
12 echo there
13 endef
15 all: ; $(multi)
17               '', "hi\necho there\nthere\n");
19 # TEST 1: Various new-style define/endef
21 run_make_test('
22 FOO = foo
24 define multi =
25 echo hi
26 @echo $(FOO)
27 endef # this is the end
29 define simple :=
30 @echo $(FOO)
31 endef
33 define posix ::=
34 @echo $(FOO)
35 endef
37 append = @echo a
39 define append +=
41 @echo b
42 endef
44 define cond ?= # this is a conditional
45 @echo first
46 endef
48 define cond ?=
49 @echo second
50 endef
52 FOO = there
54 all: ; $(multi)
55         $(simple)
56         $(posix)
57         $(append)
58         $(cond)
60               '', "echo hi\nhi\nthere\nfoo\nfoo\na\nb\nfirst\n");
62 # TEST 1a: Various new-style define/endef, with no spaces
64 run_make_test('
65 FOO = foo
67 define multi=
68 echo hi
69 @echo $(FOO)
70 endef # this is the end
72 define simple:=
73 @echo $(FOO)
74 endef
76 define posix::=
77 @echo $(FOO)
78 endef
80 append = @echo a
82 define append+=
84 @echo b
85 endef
87 define cond?= # this is a conditional
88 @echo first
89 endef
91 define cond?=
92 @echo second
93 endef
95 FOO = there
97 all: ; $(multi)
98         $(simple)
99         $(posix)
100         $(append)
101         $(cond)
103               '', "echo hi\nhi\nthere\nfoo\nfoo\na\nb\nfirst\n");
105 # TEST 2: define in true section of conditional (containing conditional)
107 run_make_test('
108 FOO = foo
109 NAME = def
110 def =
111 ifdef BOGUS
112  define  $(subst e,e,$(NAME))     =
113   ifeq (1,1)
114    FOO = bar
115   endif
116  endef
117 endif
119 $(eval $(def))
120 all: ; @echo $(FOO)
122               'BOGUS=1', "bar\n");
124 # TEST 3: define in false section of conditional (containing conditional)
126 run_make_test(undef, '', "foo\n");
128 # TEST 4: nested define (supported?)
130 run_make_test('
131 define outer
132  define inner
133   A = B
134  endef
135 endef
137 $(eval $(outer))
139 outer: ; @echo $(inner)
141               '', "A = B\n");
143 # TEST 5: NEGATIVE: Missing variable name
145 run_make_test('
146 NAME =
147 define $(NAME)  =
148 ouch
149 endef
150 all: ; @echo ouch
152               '', "#MAKEFILE#:3: *** empty variable name.  Stop.\n", 512);
154 # TEST 6: NEGATIVE: extra text after define
156 run_make_test('
157 NAME =
158 define NAME = $(NAME)
159 ouch
160 endef
161 all: ; @echo ok
163               '', "#MAKEFILE#:3: extraneous text after `define' directive\nok\n");
165 # TEST 7: NEGATIVE: extra text after endef
167 run_make_test('
168 NAME =
169 define NAME =
170 ouch
171 endef $(NAME)
172 all: ; @echo ok
174               '', "#MAKEFILE#:5: extraneous text after `endef' directive\nok\n");
176 # TEST 8: NEGATIVE: missing endef
178 run_make_test('
179 NAME =
180 all: ; @echo ok
181 define NAME =
182 ouch
183 endef$(NAME)
185               '', "#MAKEFILE#:4: *** missing `endef', unterminated `define'.  Stop.\n", 512);
187 # -------------------------
188 # Make sure that prefix characters apply properly to define/endef values.
190 # There's a bit of oddness here if you try to use a variable to hold the
191 # prefix character for a define.  Even though something like this:
193 #       define foo
194 #       echo bar
195 #       endef
197 #       all: ; $(V)$(foo)
199 # (where V=@) can be seen by the user to be obviously different than this:
201 #       define foo
202 #       $(V)echo bar
203 #       endef
205 #       all: ; $(foo)
207 # and the user thinks it should behave the same as when the "@" is literal
208 # instead of in a variable, that can't happen because by the time make
209 # expands the variables for the command line and sees it begins with a "@" it
210 # can't know anymore whether the prefix character came before the variable
211 # reference or was included in the first line of the variable reference.
213 # TEST #5
214 # -------
216 run_make_test('
217 define FOO
218 $(V1)echo hello
219 $(V2)echo world
220 endef
221 all: ; @$(FOO)
222 ', '', 'hello
223 world');
225 # TEST #6
226 # -------
228 run_make_test(undef, 'V1=@ V2=@', 'hello
229 world');
231 # TEST #7
232 # -------
234 run_make_test('
235 define FOO
236 $(V1)echo hello
237 $(V2)echo world
238 endef
239 all: ; $(FOO)
240 ', 'V1=@', 'hello
241 echo world
242 world');
244 # TEST #8
245 # -------
247 run_make_test(undef, 'V2=@', 'echo hello
248 hello
249 world');
251 # TEST #9
252 # -------
254 run_make_test(undef, 'V1=@ V2=@', 'hello
255 world');
257 # TEST #10
258 # -------
259 # Test the basics; a "@" internally to the variable applies to only one line.
260 # A "@" before the variable applies to the entire variable.
262 run_make_test('
263 define FOO
264 @echo hello
265 echo world
266 endef
267 define BAR
268 echo hello
269 echo world
270 endef
272 all: foo bar
273 foo: ; $(FOO)
274 bar: ; @$(BAR)
275 ', '', 'hello
276 echo world
277 world
278 hello
279 world