Imported Upstream version 4.4
[platform/upstream/make.git] / tests / scripts / variables / define
1 #                                                                    -*-perl-*-
2
3 $description = "Test define/endef variable assignments.";
4
5 $details = "";
6
7 # TEST 0: old-style basic define/endef
8
9 run_make_test('
10 define multi
11 @echo hi
12 echo there
13 endef
14
15 all: ; $(multi)
16 ',
17               '', "hi\necho there\nthere\n");
18
19 # TEST 1: Various new-style define/endef
20
21 run_make_test('
22 FOO = foo
23
24 define multi =
25 echo hi
26 @echo $(FOO)
27 endef # this is the end
28
29 define simple :=
30 @echo $(FOO)
31 endef
32
33 define posix ::=
34 @echo $(FOO)
35 endef
36
37 append = @echo a
38
39 define append +=
40
41 @echo b
42 endef
43
44 define cond ?= # this is a conditional
45 @echo first
46 endef
47
48 define cond ?=
49 @echo second
50 endef
51
52 FOO = there
53
54 all: ; $(multi)
55         $(simple)
56         $(posix)
57         $(append)
58         $(cond)
59 ',
60               '', "echo hi\nhi\nthere\nfoo\nfoo\na\nb\nfirst\n");
61
62 # TEST 1a: Various new-style define/endef, with no spaces
63
64 run_make_test(q!
65 FOO = foo
66
67 define multi=
68 echo hi
69 @echo $(FOO)
70 endef # this is the end
71
72 define simple:=
73 @echo $(FOO)
74 endef
75
76 define posix::=
77 @echo $(FOO)
78 endef
79
80 define posixbsd:::=
81 @echo '$(FOO)$$bar'
82 endef
83
84 append = @echo a
85
86 define append+=
87
88 @echo b
89 endef
90
91 define cond?= # this is a conditional
92 @echo first
93 endef
94
95 define cond?=
96 @echo second
97 endef
98
99 FOO = there
100
101 all: ; $(multi)
102         $(simple)
103         $(posix)
104         $(posixbsd)
105         $(append)
106         $(cond)
107 !,
108               '', "echo hi\nhi\nthere\nfoo\nfoo\nfoo\$bar\na\nb\nfirst\n");
109
110 # TEST 2: define in true section of conditional (containing conditional)
111
112 run_make_test('
113 FOO = foo
114 NAME = def
115 def =
116 ifdef BOGUS
117  define  $(subst e,e,$(NAME))     =
118   ifeq (1,1)
119    FOO = bar
120   endif
121  endef
122 endif
123
124 $(eval $(def))
125 all: ; @echo $(FOO)
126 ',
127               'BOGUS=1', "bar\n");
128
129 # TEST 3: define in false section of conditional (containing conditional)
130
131 run_make_test(undef, '', "foo\n");
132
133 # TEST 4: nested define (supported?)
134
135 run_make_test('
136 define outer
137  define inner
138   A = B
139  endef
140 endef
141
142 $(eval $(outer))
143
144 outer: ; @echo $(inner)
145 ',
146               '', "A = B\n");
147
148 # TEST 5: NEGATIVE: Missing variable name
149
150 run_make_test('
151 NAME =
152 define $(NAME)  =
153 ouch
154 endef
155 all: ; @echo ouch
156 ',
157               '', "#MAKEFILE#:3: *** empty variable name.  Stop.\n", 512);
158
159 # TEST 6: NEGATIVE: extra text after define
160
161 run_make_test('
162 NAME =
163 define NAME = $(NAME)
164 ouch
165 endef
166 all: ; @echo ok
167 ',
168               '', "#MAKEFILE#:3: extraneous text after 'define' directive\nok\n");
169
170 # TEST 7: NEGATIVE: extra text after endef
171
172 run_make_test('
173 NAME =
174 define NAME =
175 ouch
176 endef $(NAME)
177 all: ; @echo ok
178 ',
179               '', "#MAKEFILE#:5: extraneous text after 'endef' directive\nok\n");
180
181 # TEST 8: NEGATIVE: missing endef
182
183 run_make_test('
184 NAME =
185 all: ; @echo ok
186 define NAME =
187 ouch
188 endef$(NAME)
189 ',
190               '', "#MAKEFILE#:4: *** missing 'endef', unterminated 'define'.  Stop.\n", 512);
191
192 # -------------------------
193 # Make sure that prefix characters apply properly to define/endef values.
194 #
195 # There's a bit of oddness here if you try to use a variable to hold the
196 # prefix character for a define.  Even though something like this:
197 #
198 #       define foo
199 #       echo bar
200 #       endef
201 #
202 #       all: ; $(V)$(foo)
203 #
204 # (where V=@) can be seen by the user to be obviously different than this:
205 #
206 #       define foo
207 #       $(V)echo bar
208 #       endef
209 #
210 #       all: ; $(foo)
211 #
212 # and the user thinks it should behave the same as when the "@" is literal
213 # instead of in a variable, that can't happen because by the time make
214 # expands the variables for the command line and sees it begins with a "@" it
215 # can't know anymore whether the prefix character came before the variable
216 # reference or was included in the first line of the variable reference.
217
218 # TEST #5
219 # -------
220
221 run_make_test('
222 define FOO
223 $(V1)echo hello
224 $(V2)echo world
225 endef
226 all: ; @$(FOO)
227 ', '', 'hello
228 world');
229
230 # TEST #6
231 # -------
232
233 run_make_test(undef, 'V1=@ V2=@', 'hello
234 world');
235
236 # TEST #7
237 # -------
238
239 run_make_test('
240 define FOO
241 $(V1)echo hello
242 $(V2)echo world
243 endef
244 all: ; $(FOO)
245 ', 'V1=@', 'hello
246 echo world
247 world');
248
249 # TEST #8
250 # -------
251
252 run_make_test(undef, 'V2=@', 'echo hello
253 hello
254 world');
255
256 # TEST #9
257 # -------
258
259 run_make_test(undef, 'V1=@ V2=@', 'hello
260 world');
261
262 # TEST #10
263 # -------
264 # Test the basics; a "@" internally to the variable applies to only one line.
265 # A "@" before the variable applies to the entire variable.
266
267 run_make_test('
268 define FOO
269 @echo hello
270 echo world
271 endef
272 define BAR
273 echo hello
274 echo world
275 endef
276
277 all: foo bar
278 foo: ; $(FOO)
279 bar: ; @$(BAR)
280 ', '', 'hello
281 echo world
282 world
283 hello
284 world
285 ');
286
287 # Ensure that define can be a target when not appearing in a variable
288 # definition context.  See SV 59870
289
290 run_make_test(q!
291 define = define
292
293 $(define) : ;@echo $@
294
295 %:define
296
297 all: define foo
298
299 %.x : define
300
301 foo:;
302 !,
303     '', "define\n");
304
305 1;