Debian Codegraph @

tests/scripts/features/patternrules

raw
1 # -*-perl-*-
2
3 $description = "Test pattern rules.";
4
5 $details = "";
6
7 use Cwd;
8
9 $dir = cwd;
10 $dir =~ s,.*/([^/]+)$,../$1,;
11
12
13 # TEST #0: Make sure that multiple patterns where the same target
14 # can be built are searched even if the first one fails
15 # to match properly.
16 #
17
18 run_make_test(q!
19 .PHONY: all
20
21 all: case.1 case.2 case.3 case.4
22
23 # We can't have this, due to "Implicit Rule Search Algorithm" step 5c
24 #xxx: void
25
26 # 1 - existing file
27 %.1: void ; @exit 1
28 %.1: #MAKEFILE# ; @exit 0
29
30 # 2 - phony
31 %.2: void ; @exit 1
32 %.2: 2.phony ; @exit 0
33 .PHONY: 2.phony
34
35 # 3 - implicit-phony
36 %.3: void ; @exit 1
37 %.3: 3.implicit-phony ; @exit 0
38
39 3.implicit-phony:
40
41 # 4 - explicitly mentioned file made by an implicit rule
42 %.4: void ; @exit 1
43 %.4: test.x ; @exit 0
44 %.x: ;
45 !,
46 '', '');
47
48 # TEST #1: make sure files that are built via implicit rules are marked
49 # as targets (Savannah bug #12202).
50 #
51 run_make_test('
52 TARGETS := foo foo.out
53
54 .PHONY: all foo.in
55
56 all: $(TARGETS)
57
58 %: %.in ; @echo $@
59
60 %.out: % ; @echo $@
61
62 foo.in: ; @:
63
64 ',
65 '', "foo\nfoo.out");
66
67
68 # TEST #2: make sure intermediate files that also happened to be
69 # prerequisites are not removed (Savannah bug #12267).
70 #
71 run_make_test('
72 $(dir)/foo.o:
73
74 $(dir)/foo.y: ; @echo $@
75
76 %.c: %.y ; touch $@
77
78 %.o: %.c ; @echo $@
79
80 .PHONY: install
81 install: $(dir)/foo.c
82
83 ',
84 "dir=$dir", "$dir/foo.y\ntouch $dir/foo.c\n$dir/foo.o");
85
86 unlink("$dir/foo.c");
87
88
89 # TEST #3: make sure precious flag is set properly for targets
90 # that are built via implicit rules (Savannah bug #13218).
91 #
92 run_make_test('
93 .DELETE_ON_ERROR:
94
95 .PRECIOUS: %.bar
96
97 %.bar:; @touch $@ && exit 1
98
99 $(dir)/foo.bar:
100
101 ',
102 "dir=$dir",
103 "#MAKE#: *** [#MAKEFILE#:6: $dir/foo.bar] Error 1", 512);
104
105 unlink("$dir/foo.bar");
106
107
108 # TEST #4: make sure targets of a matched implicit pattern rule are
109 # never considered intermediate (Savannah bug #13022).
110 #
111 run_make_test('
112 .PHONY: all
113 all: foo.c foo.o
114
115 %.h %.c: %.in ; touch $*.h ; touch $*.c
116
117 %.o: %.c %.h ; echo $+ >$@
118
119 %.o: %.c ; @echo wrong rule
120
121 foo.in: ; touch $@
122
123 ',
124 '', "touch foo.in\ntouch foo.h ; touch foo.c\necho foo.c foo.h >foo.o\nrm foo.h");
125
126 unlink('foo.in', 'foo.h', 'foo.c', 'foo.o');
127
128 # TEST #5: make sure both prefix and suffix patterns work with multiple
129 # target patterns (Savannah bug #26593).
130 #
131 run_make_test('
132 all: foo.s1 foo.s2 p1.foo p2.foo
133
134 p1.% p2.%: %.orig ; @echo $@
135 %.s1 %.s2: %.orig ; @echo $@
136
137 .PHONY: foo.orig
138 ',
139 '', "foo.s1\np1.foo\n");
140
141 # TEST 6: Make sure that non-target files are still eligible to be created
142 # as part of implicit rule chaining. Savannah bug #17752.
143
144 run_make_test(sprintf(q!
145 BIN = xyz
146 COPY = $(BIN).cp
147 SRC = $(BIN).c
148 allbroken: $(COPY) $(BIN) ; @echo ok
149 $(SRC): ; @echo 'main(){}' > $@
150 %%.cp: %% ; @cp $< $@
151 %% : %%.c ; @cp $< $@
152 clean: ; @%s $(SRC) $(COPY) $(BIN)
153 !, $CMD_rmfile),
154 '', "ok\n");
155
156 unlink(qw(xyz xyz.cp xyz.c));
157
158 # TEST 7: Make sure that all prereqs of all "also_make" targets get created
159 # before any of the things that depend on any of them. Savannah bug #19108.
160
161 run_make_test(q!
162 final: x ; @echo $@
163 x: x.t1 x.t2 ; @echo $@
164 x.t2: dep
165 dep: ; @echo $@
166 %.t1 %.t2: ; @echo $*.t1 ; echo $*.t2
167 !,
168 '', "dep\nx.t1\nx.t2\nx\nfinal\n");
169
170
171 # TEST 8: Verify we can remove pattern rules. Savannah bug #18622.
172
173 my @f = (qw(foo.w foo.ch));
174 touch(@f);
175
176 run_make_test(q!
177 CWEAVE := :
178
179 # Disable builtin rules
180 %.tex : %.w
181 %.tex : %.w %.ch
182 !,
183 'foo.tex',
184 "#MAKE#: *** No rule to make target 'foo.tex'. Stop.", 512);
185
186 unlink(@f);
187
188 # TEST #9: Test shortest stem selection in pattern rules.
189
190 run_make_test('
191 %.x: ;@echo one
192 %-mt.x: ;@echo two
193
194 all: foo.x foo-mt.x
195 ',
196 '', "one\ntwo");
197
198 # Test pattern rules building the same targets
199 # See SV 54233.
200
201 touch('a.c');
202
203 # a.lnk isn't listed as removed, because it's not actually created
204 run_make_test(q!
205 all: a.elf a.dbg
206
207 %.elf %.lnk: %.c ; : $*.elf $*.lnk
208
209 %.elf %.dbg: %.lnk ; : $*.elf $*.dbg
210 !,
211 '-j2', ": a.elf a.lnk\n: a.elf a.dbg\n");
212
213 # SV 60435 : a.lnk is removed, because it is intermediate.
214 run_make_test(q!
215 all: a.elf a.dbg
216
217 %.elf %.lnk: %.c ; touch $*.elf $*.lnk
218
219 %.elf %.dbg: %.lnk ; touch $*.elf $*.dbg
220 !,
221 '-j2', "touch a.elf a.lnk\ntouch a.elf a.dbg\nrm a.lnk\n");
222
223 unlink('a.elf', 'a.dbg');
224
225 # SV 60435 : a.lnk is not intermediate, because it is explicitly mentioned.
226 run_make_test(q!
227 all: a.elf a.dbg
228
229 %.elf %.lnk: %.c ; touch $*.elf $*.lnk
230
231 %.elf %.dbg: %.lnk ; touch $*.elf $*.dbg
232
233 install: a.lnk
234 .PHONY: install
235 !,
236 '-j2', "touch a.elf a.lnk\ntouch a.elf a.dbg\n");
237
238 unlink('a.c', 'a.elf', 'a.dbg', 'a.lnk');
239
240 # SV 56655: Test patterns matching files containing whitespace
241 touch('some file.yy');
242 run_make_test(q!
243 %.xx : %.yy ; @echo matched
244 !,
245 '"some file.xx"', "matched\n");
246
247 unlink('some file.xx', 'some file.yy');
248
249
250 # sv 60188.
251 # Test that a file explicitly mentioned by the user and made by an implicit
252 # rule is not considered intermediate.
253
254 touch('hello.z');
255 unlink('hello.x', 'test.x');
256
257 # subtest 1
258 # hello.x is not explicitly mentioned and thus is an intermediate file.
259 run_make_test(q!
260 all: hello.z
261 %.z: %.x ; touch $@
262 %.x: ;
263 !,
264 '', "#MAKE#: Nothing to be done for 'all'.\n");
265
266 # subtest 2
267 # test.x is explicitly mentioned and thus is not an intermediate file.
268 run_make_test(q!
269 all: hello.z
270 %.z: %.x test.x ; touch $@
271 %.x: ;
272 !,
273 '', "touch hello.z");
274
275 # subtest 3
276 # hello.x is explicitly mentioned on an unrelated rule and thus is not an
277 # intermediate file.
278 touch('hello.z');
279 run_make_test(q!
280 all: hello.z
281 %.z: %.x; touch $@
282 %.x: ;
283 unrelated: hello.x
284 !,
285 '', "touch hello.z");
286
287 unlink('hello.z');
288
289 # sv 60188.
290 # Test that a file explicitly mentioned by the user and made by an implicit
291 # rule is not considered intermediate, even when the builtin rules are used.
292
293 touch('hello.x');
294 touch('test.x');
295 touch('hello.tsk');
296
297 # subtest 1
298 # hello.o is not explicitly mentioned and thus is an intermediate file.
299 run_make_test(q!
300 all: hello.tsk
301 %.tsk: %.z ; @echo $@
302 %.z : %.x ; @echo $@
303 !,
304 '', "#MAKE#: Nothing to be done for 'all'.\n");
305
306 # subtest 2
307 # test.z is explicitly mentioned and thus is not an intermediate file.
308 # test.z is built first because until it's built we don't know if we
309 # need to rebuild the intermediate hello.z
310 run_make_test(q!
311 all: hello.tsk
312 %.tsk: %.z test.z ; @echo $@
313 %.z : %.x ; @echo $@
314 !,
315 '', "test.z\nhello.z\nhello.tsk\n");
316
317 # subtest 3
318 # hello.o is not explicitly mentioned and thus is an intermediate file.
319 run_make_test(q!
320 all: hello.tsk
321 dep:=%.o
322 %.tsk: $(dep) ; @echo $@
323 !,
324 '', "#MAKE#: Nothing to be done for 'all'.\n");
325
326 # subtest 4
327 # Even when test.z is constructed from 2 variables it is still explicitly
328 # mentioned and thus is not an intermediate file.
329 # test.z is built first because until it's built we don't know if we
330 # need to rebuild the intermediate hello.z
331 run_make_test(q!
332 all: hello.tsk
333 name:=test
334 suf:=.z
335 %.tsk: %.z $(name)$(suf) ; @echo $@
336 %.z: %.x ; @echo $@
337 !,
338 '', "test.z\nhello.z\nhello.tsk\n");
339
340 unlink('hello.x', 'test.x', 'hello.tsk');
341
342 # Test that chained pattern rules with multiple targets remove all intermediate
343 # files.
344 # sv 60435.
345
346 # subtest 1.
347 # a.1 and a.2 are intermediate and should be removed.
348
349 run_make_test(q!
350 a.4:
351 %.4: %.1 %.15 ; cat $^ >$@
352 %.1 %.15: ; touch $*.1 $*.15
353 !,
354 '', "touch a.1 a.15\ncat a.1 a.15 >a.4\nrm a.15 a.1");
355
356 unlink('a.4');
357
358 # subtest 2.
359 # a.1 and a.2 are intermediate and should be removed.
360 # a.3 is explicit and should not be removed.
361 run_make_test(q!
362 a.4:
363 %.4: %.1 %.15 a.3 ; cat $^ >$@
364 %.1 %.15: ; touch $*.1 $*.15
365 %.3: ; touch $@
366 !,
367 '', "touch a.3\ntouch a.1 a.15\ncat a.1 a.15 a.3 >a.4\nrm a.15 a.1");
368
369 unlink('a.3', 'a.4');
370
371 # subtest 3.
372 # a.1 and a.2 are intermediate and should be removed.
373 # a.3 is explicit and should not be removed.
374 run_make_test(q!
375 a.4:
376 %.4: %.1 %.15 a.3 ; cat $^ >$@
377 %.1 %.15 %.3: ; touch $*.1 $*.15 $*.3
378 !,
379 '', "touch a.1 a.15 a.3\ncat a.1 a.15 a.3 >a.4\nrm a.15 a.1");
380
381 unlink('a.3', 'a.4');
382
383 # subtest 4.
384 # a.1 and a.2 are intermediate and should be removed.
385 # a.3 is explicit and should not be removed.
386 run_make_test(q!
387 a.4:
388 %.4: %.1 %.15 a.3 ; cat $^ >$@
389 %.3 %.1 %.15: ; touch $*.1 $*.15 $*.3
390 !,
391 '', "touch a.1 a.15 a.3\ncat a.1 a.15 a.3 >a.4\nrm a.15 a.1");
392
393 unlink('a.3', 'a.4');
394
395 # subtest 5.
396 # a.1 and a.2 are intermediate and should be removed.
397 # a.3 is explicit and should not be removed.
398 run_make_test(q!
399 a.4:
400 %.4: a.3 %.1 %.15 ; cat $^ >$@
401 %.1 %.15 %.3: ; touch $*.1 $*.15 $*.3
402 !,
403 '', "touch a.1 a.15 a.3\ncat a.3 a.1 a.15 >a.4\nrm a.15 a.1");
404
405 unlink('a.3', 'a.4');
406
407 # subtest 6.
408 # a.2 is intermediate and should be removed.
409 # a.1 is mentioned explicitly on an unrelated rule and should not be removed.
410 run_make_test(q!
411 a.3:
412 %.3: %.1 %.2 ; cat $^ >$@
413 %.1 %.2: ; touch $*.1 $*.2
414 install: a.1
415 .PHONY: install
416 !,
417 '', "touch a.1 a.2\ncat a.1 a.2 >a.3\nrm a.2");
418
419 unlink('a.1', 'a.3');
420
421 # Test removal of intermediate files.
422
423 # subtest 1.
424 # hello.x is removed, because it is intermediate.
425 run_make_test(q!
426 hello.tsk:
427 %.tsk: %.x; touch $@
428 %.x: ; touch $@
429 !,
430 '', "touch hello.x\ntouch hello.tsk\nrm hello.x");
431
432 unlink('hello.tsk');
433
434 # subtest 2.
435 # Even though hello.x is intermediate, it is not removed, because it is not
436 # created.
437 touch('hello.x');
438
439 run_make_test(q!
440 hello.tsk:
441 %.tsk: %.x; touch $@
442 %.x: ; touch $@
443 !,
444 '', "touch hello.tsk");
445
446 unlink('hello.x', 'hello.tsk');
447
448 # subtest 2.
449 # Even though hello.x is intermediate, it is not removed, because it is not
450 # created.
451 run_make_test(q!
452 hello.tsk:
453 %.tsk: %.x; touch $@
454 %.x: ; : $@
455 !,
456 '', ": hello.x\ntouch hello.tsk");
457
458 unlink('hello.tsk');
459
460 # A target explicitly listed as a prerequisite of a pattern rule, is still
461 # considered mentioned and "ought to exist".
462
463 run_make_test(q!
464 1.all: 1.q ; touch $@
465 %.q: 1.r ; touch $@
466 %.r: ; touch $@
467 !,
468 '', "touch 1.r\ntouch 1.q\ntouch 1.all\n");
469
470 unlink('1.all', '1.q', '1.r');
471
472 # SV 63098: Verify that missing also_made in pattern rules gives a warning but
473 # doesn't fail.
474
475 run_make_test(q!
476 %a %b : ; touch $*a
477 !,
478 'gta', "touch gta\n#MAKEFILE#:2: warning: pattern recipe did not update peer target 'gtb'.\n");
479 unlink(qw(gta));
480
481 # We don't warn if we didn't update the file
482 utouch(-10, qw(gta));
483 run_make_test(q!
484 %a %b : xyzzy ; $(OP)
485 xyzzy: ;
486 ifdef RUN
487 OP = @echo no
488 endif
489 !,
490 '-rR gta', "#MAKE#: 'gta' is up to date.\n");
491
492 run_make_test(undef, '-rR gta RUN=1', "no\n");
493 unlink(qw(gta));
494
495 run_make_test(q!
496 all:;
497 include gta
498 %a %b : ; touch $*a
499 !,
500 '', "touch gta\n#MAKEFILE#:4: warning: pattern recipe did not update peer target 'gtb'.\n#MAKE#: 'all' is up to date.");
501 unlink(qw(gta));
502
503 run_make_test(q!
504 %.c %.h : %.y; touch $*.c
505 %.o: %.c; touch $@
506 foo.y: ; touch $@
507 !,
508 'foo.o', "touch foo.y\ntouch foo.c\n#MAKEFILE#:2: warning: pattern recipe did not update peer target 'foo.h'.\ntouch foo.o\nrm foo.c");
509 unlink(qw(foo.y foo.c foo.o));
510
511 if (0) {
512 # SV 12078: Missing grouped pattern peer causes remake regardless of which
513 # target caused the rule to run.
514 touch(qw(gta)); # but not gtb
515 run_make_test(q!
516 %a %b : ; touch $*a $*b
517 !,
518 'gta', "touch gta gtb\n");
519 unlink(qw(gta gtb));
520
521 # Ensure both goal targets are built if they depend on a grouped pattern
522 touch(qw(gta)); # but not gtb
523 run_make_test(q!
524 x y: ; touch $@
525
526 x: gta
527 y: gtb
528
529 %a %b : ; touch $*a $*b
530 !,
531 'x y', "touch gta gtb\ntouch x\ntouch y\n");
532
533 # Now everything should be up to date
534 run_make_test(undef, 'x y',
535 "#MAKE#: 'x' is up to date.\n#MAKE#: 'y' is up to date.");
536
537 unlink(qw(x y gta gtb));
538
539 # sv 12078 : make sure we notice when all targets need to be rebuilt
540 # a.1st exists but b.1st doesn't: make sure a.2nd is out of date as well
541
542 utouch(-20, 'a.1st');
543 utouch(-10, 'a.2nd', 'b.2nd');
544
545 run_make_test(q!
546 1st := a.1st b.1st
547 2nd := ${1st:.1st=.2nd}
548 .PHONY: all
549 all: ${2nd}
550 a.% b.% : ; touch a.$* b.$*
551 ${2nd}: %.2nd: %.1st ; cp $< $@
552 !
553 , '', "touch a.1st b.1st\ncp a.1st a.2nd\ncp b.1st b.2nd\n");
554
555 unlink(qw(a.1st b.1st a.2nd b.2nd));
556
557 # Variation: b.1st exists but is newer
558
559 utouch(-20, 'a.1st');
560 utouch(-10, 'a.2nd', 'b.2nd');
561 touch(qw(b.1st));
562
563 run_make_test(undef, '', "cp b.1st b.2nd\n");
564
565 unlink(qw(a.1st b.1st a.2nd b.2nd));
566 }
567
568 # sv 62206.
569
570 # The following combinations are generated with and without second expansion.
571 # 1.
572 # all: bye.x
573 # %.x: ...
574 #
575 # 2.
576 # all: lib/bye.x
577 # %.x: ...
578 #
579 # 3.
580 # all: lib/bye.x
581 # lib/%.x: ...
582 #
583 # The following combination is not generated, because there is no rule to
584 # build bye.x, no stem substitution takes place, not of interest of this test.
585 # 4.
586 # all: bye.x
587 # lib/%.x: ...
588
589 my @dir = ('', 'lib/'); # With and without last slash.
590 my @secondexpansion = ('', '.SECONDEXPANSION:');
591
592 for my $se (@secondexpansion) {
593 for my $d (@dir) { # The directory of the prerequisite of 'all'.
594 for my $r (@dir) { # The directory of the target in the rule definition.
595 (!$d && $r) && next; # Combination 4.
596 my $dollar = $se ? '$' : '';
597
598 # The prerequisite should only have directory if the prerequisite of 'all' has
599 # it and if the prerequisite pattern in the rule definition does not have it.
600 # That is combination 2.
601 my $pdir = $d && !$r ? $d : '';
602
603 my $prereqs = "${pdir}bye.1";
604
605 # One func, one %.
606 run_make_test("
607 $se
608 all: ${d}bye.x
609 $r%.x: $dollar\$(firstword %.1); \$(info \$@ from \$^)
610 .PHONY: $prereqs
611 ", '', "${d}bye.x from $prereqs\n#MAKE#: Nothing to be done for 'all'.\n");
612
613 $prereqs = "${pdir}bye.1 ${pdir}bye.2";
614
615 # Multiple funcs, each has one %.
616 run_make_test("
617 $se
618 all: ${d}bye.x
619 $r%.x: $dollar\$(firstword %.1) $dollar\$(firstword %.2); \$(info \$@ from \$^)
620 .PHONY: $prereqs
621 ", '', "${d}bye.x from $prereqs\n#MAKE#: Nothing to be done for 'all'.\n");
622
623 $prereqs = "${pdir}bye.1 ${pdir}bye.2 ${pdir}bye.3 ${pdir}bye.4";
624
625 # Multiple funcs, each has multiple %.
626 run_make_test("
627 $se
628 all: ${d}bye.x
629 $r%.x: $dollar\$(wordlist 1, 99, %.1 %.2) $dollar\$(wordlist 1, 99, %.3 %.4); \$(info \$@ from \$^)
630 .PHONY: $prereqs
631 ", '', "${d}bye.x from $prereqs\n#MAKE#: Nothing to be done for 'all'.\n");
632
633 $prereqs = "${pdir}bye.1 ${pdir}bye.2 ${pdir}bye.3 ${pdir}bye.4";
634
635 # Nested functions.
636 run_make_test("
637 $se
638 all: ${d}bye.x
639 $r%.x: $dollar\$(wordlist 1, 99, $dollar\$(wordlist 1, 99, %.1 %.2)) $dollar\$(wordlist 1, 99, $dollar\$(wordlist 1,99, %.3 %.4)); \$(info \$@ from \$^)
640 .PHONY: $prereqs
641 ", '', "${d}bye.x from $prereqs\n#MAKE#: Nothing to be done for 'all'.\n");
642
643 $prereqs = "${pdir}bye1%2% ${pdir}bye ${pdir}3bye4%5 ${pdir}6bye ${pdir}bye7%8 ${pdir}bye9 ${pdir}bye10% ${pdir}11bye12 13";
644
645 # Multiple funcs, each has multiple words, each word has multiple %, sole %,
646 # various corner cases.
647 # Make should substitute the first % and only the first % in each word with the
648 # stem.
649 run_make_test("
650 $se
651 all: ${d}bye.x
652 $r%.x: $dollar\$(wordlist 1, 99, %1%2% % 3%4%5 6%) %7%8 %9 $dollar\$(wordlist 1, 99, %10% 11%12) 13; \$(info \$@ from \$^)
653 .PHONY: $prereqs
654 ", '', "${d}bye.x from $prereqs\n#MAKE#: Nothing to be done for 'all'.\n");
655
656 if ($port_type eq 'UNIX') {
657 # Test that make does not use some hardcoded array of a finite size on stack.
658 # Long prerequisite name. This prerequisite name is over 66K long.
659 my $prefix = 'abcdefgh' x 128 x 33; # 33K long.
660 my $suffix = 'stuvwxyz' x 128 x 33; # 33K long.
661 $prereqs = "${pdir}${prefix}bye${suffix}.1 ${pdir}${prefix}bye${suffix}.2";
662
663 run_make_test("
664 $se
665 all: ${d}bye.x
666 $r%.x: $dollar\$(wordlist 1, 99, ${prefix}%${suffix}.1 ${prefix}%${suffix}.2); \$(info \$@ from \$^)
667 .PHONY: $prereqs
668 ", '', "${d}bye.x from $prereqs\n#MAKE#: Nothing to be done for 'all'.\n");
669 }
670
671 }
672 }
673 }
674
675 # This tells the test driver that the perl test script executed properly.
676 1;