Imported Upstream version 4.4
[platform/upstream/make.git] / tests / scripts / features / implicit_search
1 #                                                                    -*-perl-*-
2
3 $description = "Test implicit rule search.";
4
5 $details = "";
6
7 # sv 48643
8 # Each test has a %.c rule ahead of %.f rule.
9 # hello.f exists and hello.c is missing.
10
11 unlink('hello.c', 'hello.tsk', 'hello.o', 'hello.x');
12
13 # Run every test with and without a suffix.
14 my @suffixes = ('', '.o');
15 # Run every test with single and double colon rules.
16 my @rules = ('', ':');
17
18 for my $s (@suffixes) {
19 for my $r (@rules) {
20 touch('hello.f');
21
22 # Test that make finds the intended implicit rule based on existence of a
23 # prerequisite in the filesystem.
24 #
25 # '%.o: %.c' rule is skipped and '%.o: %.f' rule is chosen.
26 run_make_test("
27 all: hello$s
28 %$s:$r %.c; \$(info hello.c)
29 %$s:$r %.f; \$(info hello.f)
30 ", '-r', "hello.f\n#MAKE#: Nothing to be done for 'all'.");
31
32 # Test that make finds the intended implicit rule based on the explicit
33 # prerequisite of the top goal and despite the existence of a
34 # prerequisite in the filesystem.
35 #
36 # hello.c is an explicit prerequisite of the top target (hello.o or hello).
37 # hello.c ought to exist.
38 # hello.c prerequisite causes '%.o: %.c' rule to be chosen.
39 run_make_test("
40 hello$s: hello.c
41 %$s:$r %.c; \$(info hello.c)
42 %$s:$r %.f; \$(info hello.f)
43 ", '-r',
44 "#MAKE#: *** No rule to make target 'hello.c', needed by 'hello$s'.  Stop.\n",
45 512);
46
47 # Test that make finds the intended implicit rule when the implicit
48 # prerequisite matches a target of an unrelated rule and despite the existence
49 # of a prerequisite of the other rule candidate in the filesystem.
50 #
51 # hello.c matches 'hello.c:' rule. This makes hello.c a target and thus ought
52 # to exist.
53 # hello.c prerequisite causes '%.o: %.c' rule to be chosen.
54 run_make_test("
55 all: hello$s
56 %$s:$r %.c; \$(info hello.c)
57 %$s:$r %.f; \$(info hello.f)
58 hello.c:; @#HELPER# fail 1
59 ", '-r', "fail 1\n#MAKE#: *** [#MAKEFILE#:5: hello.c] Error 1\n", 512);
60
61 # Test that make finds the intended implicit rule based on existence of a
62 # prerequisite in the filesystem, even when the prerequisite of another
63 # candidate rule is mentioned explicitly on an unrelated rule.
64 #
65 # '%.o: %.c' rule is skipped and '%.o: %.f' rule is chosen, even though hello.c
66 # is mentioned explicitly on 'unrelated: hello.c'.
67 # ought-to-exist does not apply to hello.c.
68 run_make_test("
69 all: hello$s
70 %$s:$r %.c; \$(info hello.c)
71 %$s:$r %.f; \$(info hello.f)
72 unrelated: hello.c
73 ", '-r', "hello.f\n#MAKE#: Nothing to be done for 'all'.");
74
75 # Test that make finds the intended implicit rule based on existence of a
76 # prerequisite in the filesystem.
77 #
78 # '%.o: %.c' rule is skipped and '%.o: %.f' rule is chosen.
79 # Despite '%.o: %.c hello.c' rule having explicit prerequisite hello.c.
80 # ought-to-exist does not apply to hello.c.
81 run_make_test("
82 all: hello$s
83 %$s:$r %.c hello.c; \$(info hello.c)
84 %$s:$r %.f; \$(info hello.f)
85 ", '-r', "hello.f\n#MAKE#: Nothing to be done for 'all'.");
86
87 # '%.o: %.c' rule is skipped and '%.o: %.f' rule is chosen.
88 # '%.o: %.f hello.f' rule has explicit prerequisite hello.f.
89 # ought-to-exist does not apply to hello.c.
90 run_make_test("
91 all: hello$s
92 %$s:$r %.c; \$(info hello.c)
93 %$s:$r %.f hello.f; \$(info hello.f)
94 ", '-r', "hello.f\n#MAKE#: Nothing to be done for 'all'.");
95
96 # Rule '%: %.f' is chosen, because '%: %.f' requires no intermediates.
97 # '%: %.c', on the other hand, requires intemediate hello.c to be built by the
98 # default rule.
99 run_make_test("
100 all: hello$s
101 %$s:$r %.c; \$(info \$<)
102 %$s:$r %.f; \$(info \$<)
103 .DEFAULT:; \$(info \$\@) true
104 unrelated: hello.c
105 ", '-r', "hello.f\n#MAKE#: Nothing to be done for 'all'.");
106
107
108 # hello.f is missing.
109 # This time both hello.c and hello.f are missing and both '%: %.c' and '%: %.f'
110 # require an intermediate.
111 # The default rule builds intemerdiate hello.c.
112 # '%: %.c' rule is chosen to build hello.
113 unlink('hello.f');
114 run_make_test("
115 all: hello$s
116 %$s:$r %.c; \$(info \$<)
117 %$s:$r %.f; \$(info \$<)
118 .DEFAULT:; \@\$(info \$\@) #HELPER# fail 1
119 unrelated: hello.c
120 ", '-r', "hello.c\nfail 1\n#MAKE#: *** [#MAKEFILE#:5: hello.c] Error 1\n", 512);
121
122 # hello.f is missing.
123 # No rule is found, because hello.c is not mentioned explicitly.
124 run_make_test("
125 all: hello$s
126 %$s:$r %.c; \$(info \$<)
127 %$s:$r %.f; \$(info \$<)
128 .DEFAULT:; \@\$(info \$\@) #HELPER# fail 1
129 ", '-r', "hello$s\nfail 1\n#MAKE#: *** [#MAKEFILE#:5: hello$s] Error 1\n", 512);
130
131 }
132 }
133
134 # Almost the same tests as above, but this time an intermediate is built.
135
136 touch('hello.f');
137 for my $s (@suffixes) {
138 for my $r (@rules) {
139
140 my $result = "#MAKE#: *** No rule to make target 'hello.tsk', needed by 'all'.  Stop.\n";
141 my $rcode = 512;
142 if ($s or $r) {
143     $result = "hello.f\nhello.tsk\n#MAKE#: Nothing to be done for 'all'.";
144     $rcode = 0;
145 }
146
147 run_make_test("
148 all: hello.tsk
149 %.tsk: %$s; \$(info hello.tsk)
150 %$s:$r %.c; \$(info hello.c)
151 %$s:$r %.f; \$(info hello.f)
152 ", '-r', "$result", $rcode);
153
154 run_make_test("
155 all: hello.tsk
156 %.tsk: %$s hello$s; \$(info hello.tsk)
157 %$s:$r %.c; \$(info hello.c)
158 %$s:$r %.f; \$(info hello.f)
159 ", '-r', $result, $rcode);
160
161 run_make_test("
162 all: hello.tsk
163 %.tsk: %$s; \$(info hello.tsk)
164 %$s:$r %.c hello$s; \$(info hello.c)
165 %$s:$r %.f; \$(info hello.f)
166 ", '-r', $result, $rcode);
167
168 }
169 }
170
171 for my $r (@rules) {
172
173 # Circular dependency hello.o <- hello.tsk is dropped.
174 run_make_test("
175 all: hello.tsk
176 %.tsk: %.o; \$(info hello.tsk)
177 %.o:$r %.c; \$(info hello.c)
178 %.o:$r %.f %.tsk; \$(info hello.f)
179 ", '-r',
180 "#MAKE#: Circular hello.o <- hello.tsk dependency dropped.\nhello.f\nhello.tsk\n#MAKE#: Nothing to be done for 'all'.");
181
182 }
183
184
185 for my $s (@suffixes) {
186 for my $r (@rules) {
187
188 run_make_test("
189 all: hello.tsk
190 hello$s: hello.c
191 %.tsk: %$s; \$(info hello.tsk)
192 %$s:$r %.c; \$(info hello.c)
193 %$s:$r %.f; \$(info hello.f)
194 ", '-r',
195 "#MAKE#: *** No rule to make target 'hello.c', needed by 'hello$s'.  Stop.\n",
196 512);
197 }
198 }
199
200 for my $s (@suffixes) {
201 for my $r (@rules) {
202
203 my $result = "#MAKE#: *** No rule to make target 'hello.tsk', needed by 'all'.  Stop.\n";
204 if ($s or $r) {
205     $result = "fail 1\n#MAKE#: *** [#MAKEFILE#:6: hello.c] Error 1\n";
206 }
207
208 run_make_test("
209 all: hello.tsk
210 %.tsk: %$s; \$(info hello.tsk)
211 %$s:$r %.c; \$(info hello.c)
212 %$s:$r %.f; \$(info hello.f)
213 hello.c:; @#HELPER# fail 1
214 ", '-r', $result, 512);
215 }
216 }
217
218
219 for my $s (@suffixes) {
220 for my $r (@rules) {
221
222 run_make_test("
223 all: hello.tsk
224 %.tsk: %$s; \$(info hello.tsk)
225 %$s:$r %.c; \$(info hello.c)
226 %$s:$r %.f; \$(info hello.f)
227 unrelated: hello$s
228 ", '-r', "hello.f\nhello.tsk\n#MAKE#: Nothing to be done for 'all'.");
229 }
230 }
231
232 for my $s (@suffixes) {
233 for my $r (@rules) {
234
235 my $result = "#MAKE#: *** No rule to make target 'hello.tsk', needed by 'all'.  Stop.\n";
236 my $rcode = 512;
237 if ($s or $r) {
238     $result = "hello.f\nhello.tsk\n#MAKE#: Nothing to be done for 'all'.";
239     $rcode = 0;
240 }
241
242 run_make_test("
243 all: hello.tsk
244 %.tsk: %$s; \$(info hello.tsk)
245 %$s:$r %.c; \$(info hello.c)
246 %$s:$r %.f hello.f; \$(info hello.f)
247 ", '-r', $result, $rcode);
248 }
249 }
250
251 # One of the implicit rules has two prerequisites, hello.c and hello.x
252 # hello.c does not qualify as ought to exit.
253 # hello.x can be made from hello.z.
254 # This test exersizes the break, which prevents making hello.x as an
255 # intermediate from hello.z during compatibility search.
256 unlink('hello.f');
257 touch('hello.z');
258 for my $s (@suffixes) {
259 for my $r (@rules) {
260
261 run_make_test("
262 all: hello.tsk
263 %.tsk: %$s; \$(info hello.tsk)
264 %$s:$r %.c %.x; \$(info hello.c)
265 %$s:$r %.f; \$(info hello.f)
266 unrelated: hello$s
267 %.x:$r %.z; \$(info hello.z)
268 ", '-r',
269 "#MAKE#: *** No rule to make target 'hello$s', needed by 'hello.tsk'.  Stop.\n",
270 512);
271 }
272 }
273
274 # Test that prerequisite 'hello.x' mentioned explicitly on an unrelated rule is
275 # not considered intermediate.
276 touch('hello.tsk');
277 unlink('hello.x');
278 run_make_test("
279 all: hello.tsk
280 %.tsk: %.x; touch hello.tsk
281 %.x: ;
282 unrelated: hello.x
283 ", '-r', "touch hello.tsk\n");
284 unlink('hello.tsk');
285
286 touch ('hello.f');
287 # Test implicit search of builtin rules.
288
289 # %: %.c (and other builtin rules) are skipped.
290 # %: %.f is chosen.
291 run_make_test(q!
292 all: hello
293 !, 'FC="@echo f77" OUTPUT_OPTION=', "f77 hello.f -o hello\n");
294
295 # %.o: %.c (and other builtin rules) are skipped.
296 # %.o: %.f is chosen.
297 run_make_test(q!
298 all: hello.o
299 !, 'FC="@echo f77" OUTPUT_OPTION=', "f77 -c hello.f\n");
300
301
302 # %: %.c is chosen.
303 # hello.c is an explicit prerequisite of the top target hello.
304 # hello.c ought to exist.
305 # hello.c prerequisite causes '%: %.c' rule to be chosen.
306 run_make_test(q!
307 hello: hello.c
308 !, 'FC="@echo f77" OUTPUT_OPTION=',
309 "#MAKE#: *** No rule to make target 'hello.c', needed by 'hello'.  Stop.\n",
310 512);
311
312 # %.o: %.c is chosen.
313 # hello.c is an explicit prerequisite of the top target hello.o.
314 # hello.c ought to exist.
315 # hello.c prerequisite causes '%.o: %.c' rule to be chosen.
316 run_make_test(q!
317 hello.o: hello.c
318 !, 'FC="@echo f77" OUTPUT_OPTION=',
319 "#MAKE#: *** No rule to make target 'hello.c', needed by 'hello.o'.  Stop.\n",
320 512);
321
322 # %: %.c (and other builtin rules) are skipped.
323 # %: %.f is chosen.
324 # ought-to-exist does not apply to hello.c.
325 run_make_test(q!
326 all: hello
327 unrelated: hello.c
328 !, 'FC="@echo f77" OUTPUT_OPTION=', "f77 hello.f -o hello\n");
329
330 # %.o: %.c (and other builtin rules) are skipped.
331 # %.o: %.f is chosen.
332 # ought-to-exist does not apply to hello.c.
333 run_make_test(q!
334 all: hello.o
335 unrelated: hello.c
336 !, 'FC="@echo f77" OUTPUT_OPTION=', "f77 -c hello.f\n");
337
338 # builtin rule %.o: %.f is removed.
339 # %.o: %.c (and other builtin rules) are skipped, because hello.c is missing.
340 # ought-to-exist does not apply to hello.c.
341 # %.o: %.c is chosen as a compatibility rule, because of hello.c.
342 run_make_test(q!
343 all: hello.o
344 unrelated: hello.c
345 %.o: %.f
346 !, '',
347 "#MAKE#: *** No rule to make target 'hello.c', needed by 'hello.o'.  Stop.\n",
348 512);
349
350
351 # sv 17752.
352 # In this test the builtin match-anything rule '%: %.f' is used to build
353 # intermediate hello from hello.f, because hello is mentioned explicitly in
354 # the makefile.
355 run_make_test(q!
356 all: hello.tsk
357 %.tsk: %; $(info $@ from $<)
358 unrelated: hello
359 !, 'FC="@echo f77" OUTPUT_OPTION=',
360 "f77 hello.f -o hello\nhello.tsk from hello\n");
361
362 # In this test the builtin match-anything rule %: %.f cannot be used to build
363 # intermediate hello from hello.f, because hello is not mentioned explicitly in
364 # the makefile.
365 run_make_test(q!
366 all: hello.tsk
367 %.tsk: %; $(info $@ from $<)
368 !, 'FC="@echo f77" OUTPUT_OPTION=',
369 "#MAKE#: *** No rule to make target 'hello.tsk', needed by 'all'.  Stop.\n",
370 512);
371
372 # This is just like the one above, but compatibility rule '%.tsk: % %.x' has 2
373 # prerequisites, '%' and '%.x'.
374 # '%' expands to 'hello' and matches the explicit 'hello' on the unrelated rule.
375 # '%.x' is an intermediate built from 'hello.xx' by rule '%.x: %.xx' during the
376 # second pass (intermed_ok == 1) of compatibility search.
377 # This test validates that compatibility search performs both intermed_ok == 0
378 # and intermed_ok == 1 passes.
379 unlink('hello.x');
380 touch('hello.xx');
381 run_make_test(q!
382 all: hello.tsk
383 %.tsk: % %.x; $(info $@ from $^)
384 unrelated: hello
385 %.x: %.xx; $(info $@ from $<)
386 !, 'FC="@echo f77" OUTPUT_OPTION=',
387 "f77 hello.f -o hello\nhello.x from hello.xx\nhello.tsk from hello hello.x\n");
388
389 unlink('bye.o', 'bye.tsk', 'bye.x');
390 # sv 21670.
391 # Default recipe is used to build bye.o.
392 run_make_test(q!
393 all: bye.tsk
394 %.tsk: %.o; $(info $@ from $<)
395 .DEFAULT:; $(info bye.o)
396 unrelated: bye.o
397 !, '-r', "bye.o\nbye.tsk from bye.o\n#MAKE#: Nothing to be done for 'all'.");
398
399 touch('bye.xx');
400 # This is just like the one above, but compatibility rule '%.tsk: %.o %.x' has 2
401 # prerequisites, '%.o' and '%.x'.
402 # '%.o' expands to 'bye.o' and matches the explicit 'bye.o' on the unrelated rule.
403 # '%.x' is an intermediate built from 'bye.xx' by rule '%.x: %.xx' during the
404 # second pass (intermed_ok == 1) of compatibility search.
405 # This test validates that compatibility search performs both intermed_ok == 0
406 # and intermed_ok == 1 passes.
407 run_make_test(q!
408 all: bye.tsk
409 %.tsk: %.o %.x; $(info $@ from $^)
410 .DEFAULT:; $(info bye.o)
411 unrelated: bye.o
412 %.x: %.xx; $(info $@ from $<)
413 !, '-r',
414 "bye.o\nbye.x from bye.xx\nbye.tsk from bye.o bye.x\n#MAKE#: Nothing to be done for 'all'.");
415
416 unlink('hello.f', 'hello.z', 'hello.xx', 'bye.xx');
417
418
419 # A target specific variable causes the file to be entered to the database as a
420 # prerequisite. Implicit search then treats this file as explicitly mentioned.
421 # Test that implicit search keeps target specific variables of this file intact.
422 # In this series of tests prerequisite 'hello.x' has a target specific variable
423 # and is built as an intermediate. Implicit search treats 'hello.x' as
424 # explicitly mentioned, but 'hello.x' does not qualify as ought-to-exist.
425 unlink('hello.x', 'hello.tsk');
426
427 # 'hello.x' is mentioned explicitly on the same implicit rule.
428 run_make_test(q!
429 all: hello.tsk
430 %.tsk: hello.x; $(info $@)
431 %.x:; $(flags)
432 hello.x: flags:=true
433 !, '-r', "true\nhello.tsk\n");
434
435 # Similar to the one above, but this time 'hello.x' is derived from the stem.
436 run_make_test(q!
437 all: hello.tsk
438 %.tsk: %.x; $(info $@)
439 %.x:; $(flags)
440 hello.x: flags:=true
441 !, '-r', "true\nhello.tsk\n");
442
443 # Similar to the one above, this time 'hello.x' is also mentioned explicitly on
444 # an unrelated rule.
445 run_make_test(q!
446 all: hello.tsk
447 %.tsk: %.x; $(info $@)
448 %.x:; $(flags)
449 hello.x: flags:=true
450 unrelated: hello.x
451 !, '-r', "true\nhello.tsk\n");
452
453 # 'hello.x' has a pattern specific variable.
454 run_make_test(q!
455 all: hello.tsk
456 %.tsk: %.x; $(info $@)
457 %.x:; $(flags)
458 %.x: flags:=true
459 !, '-r', "true\nhello.tsk\n");
460
461 # 'hello.x' has a target specific variable and a pattern specific variable.
462 run_make_test(q!
463 all: hello.tsk
464 %.tsk: %.x; $(info $@)
465 %.x:; $(flags)
466 hello.x: flags+=good
467 %.x: flags:=true
468 !, '-r', "true good\nhello.tsk\n");
469
470 # Intermediate prerequisite 'hello.x' has a target specific variable, a pattern
471 # specfic variable, matches on both rules '%.tsk: %.x' and 'big_%.tsk: %.x'.
472 run_make_test(q!
473 all: hello.tsk big_hello.tsk
474 %.tsk: %.x; $(info $@)
475 big_%.tsk: %.x; $(info $@)
476 %.x:; $(flags)
477 hello.x: flags+=good
478 %.x: flags:=true
479 !, '-r', "true good\nhello.tsk\nbig_hello.tsk\n");
480
481
482 # This tells the test driver that the perl test script executed properly.
483 1;