make/tests/scripts/features/patternrules
Dmitry Goncharov 510e5ce801 [SV 60188] Explicit prereqs cannot be intermediate files
If a prereq of a pattern is an explicit target, it should not be
considered an intermediate file.

(Minor tweaks by Paul Smith <psmith@gnu.org>)

* src/dep.h (struct nameseq): Add is_explicit flag.
* src/implicit.c (struct patdeps): Ditto.
(pattern_search): Set the is_explicit flag appropriately for each
prerequisite, based on whether it contained a pattern or not.
Update the help output to note implicit vs. explicit prereqs.
* tests/scripts/features/double_colon: Add tests.
* tests/scripts/features/grouped_targets: Ditto.
* tests/scripts/features/patternrules: Ditto.
* tests/scripts/features/se_implicit: Ditto.
* tests/scripts/features/statipattrules: Ditto.
2021-03-15 02:10:49 -04:00

339 lines
6.2 KiB
Perl

# -*-perl-*-
$description = "Test pattern rules.";
$details = "";
use Cwd;
$dir = cwd;
$dir =~ s,.*/([^/]+)$,../$1,;
# TEST #0: Make sure that multiple patterns where the same target
# can be built are searched even if the first one fails
# to match properly.
#
run_make_test(q!
.PHONY: all
all: case.1 case.2 case.3 case.4
# We can't have this, due to "Implicit Rule Search Algorithm" step 5c
#xxx: void
# 1 - existing file
%.1: void
@exit 1
%.1: #MAKEFILE#
@exit 0
# 2 - phony
%.2: void
@exit 1
%.2: 2.phony
@exit 0
.PHONY: 2.phony
# 3 - implicit-phony
%.3: void
@exit 1
%.3: 3.implicit-phony
@exit 0
3.implicit-phony:
# 4 - explicitly mentioned file made by an implicit rule
%.4: void
@exit 1
%.4: test.x
@exit 0
%.x: ;
!, '', '');
# TEST #1: make sure files that are built via implicit rules are marked
# as targets (Savannah bug #12202).
#
run_make_test('
TARGETS := foo foo.out
.PHONY: all foo.in
all: $(TARGETS)
%: %.in
@echo $@
%.out: %
@echo $@
foo.in: ; @:
',
'',
'foo
foo.out');
# TEST #2: make sure intermediate files that also happened to be
# prerequisites are not removed (Savannah bug #12267).
#
run_make_test('
$(dir)/foo.o:
$(dir)/foo.y:
@echo $@
%.c: %.y
touch $@
%.o: %.c
@echo $@
.PHONY: install
install: $(dir)/foo.c
',
"dir=$dir",
"$dir/foo.y
touch $dir/foo.c
$dir/foo.o");
unlink("$dir/foo.c");
# TEST #3: make sure precious flag is set properly for targets
# that are built via implicit rules (Savannah bug #13218).
#
run_make_test('
.DELETE_ON_ERROR:
.PRECIOUS: %.bar
%.bar:; @touch $@ && exit 1
$(dir)/foo.bar:
',
"dir=$dir",
"#MAKE#: *** [#MAKEFILE#:6: $dir/foo.bar] Error 1",
512);
unlink("$dir/foo.bar");
# TEST #4: make sure targets of a matched implicit pattern rule are
# never considered intermediate (Savannah bug #13022).
#
run_make_test('
.PHONY: all
all: foo.c foo.o
%.h %.c: %.in
touch $*.h
touch $*.c
%.o: %.c %.h
echo $+ >$@
%.o: %.c
@echo wrong rule
foo.in:
touch $@
',
'',
'touch foo.in
touch foo.h
touch foo.c
echo foo.c foo.h >foo.o');
unlink('foo.in', 'foo.h', 'foo.c', 'foo.o');
# TEST #5: make sure both prefix and suffix patterns work with multiple
# target patterns (Savannah bug #26593).
#
run_make_test('
all: foo.s1 foo.s2 p1.foo p2.foo
p1.% p2.%: %.orig
@echo $@
%.s1 %.s2: %.orig
@echo $@
.PHONY: foo.orig
',
'', "foo.s1\np1.foo\n");
# TEST 6: Make sure that non-target files are still eligible to be created
# as part of implicit rule chaining. Savannah bug #17752.
run_make_test(sprintf(q!
BIN = xyz
COPY = $(BIN).cp
SRC = $(BIN).c
allbroken: $(COPY) $(BIN) ; @echo ok
$(SRC): ; @echo 'main(){}' > $@
%%.cp: %% ; @cp $< $@
%% : %%.c ; @cp $< $@
clean: ; @%s $(SRC) $(COPY) $(BIN)
!, $CMD_rmfile),
'', "ok\n");
unlink(qw(xyz xyz.cp xyz.c));
# TEST 7: Make sure that all prereqs of all "also_make" targets get created
# before any of the things that depend on any of them. Savannah bug #19108.
run_make_test(q!
final: x ; @echo $@
x: x.t1 x.t2 ; @echo $@
x.t2: dep
dep: ; @echo $@
%.t1 %.t2: ; @echo $*.t1 ; echo $*.t2
!,
'', "dep\nx.t1\nx.t2\nx\nfinal\n");
# TEST 8: Verify we can remove pattern rules. Savannah bug #18622.
my @f = (qw(foo.w foo.ch));
touch(@f);
run_make_test(q!
CWEAVE := :
# Disable builtin rules
%.tex : %.w
%.tex : %.w %.ch
!,
'foo.tex',
"#MAKE#: *** No rule to make target 'foo.tex'. Stop.", 512);
unlink(@f);
# TEST #9: Test shortest stem selection in pattern rules.
run_make_test('
%.x: ;@echo one
%-mt.x: ;@echo two
all: foo.x foo-mt.x
',
'',
"one\ntwo");
1;
# Test pattern rules building the same targets
# See SV 54233. Rely on our standard test timeout to break the loop
touch('a.c');
run_make_test(q!
all: a.elf a.dbg
%.elf %.lnk: %.c ; : $*.elf $*.lnk
%.elf %.dbg: %.lnk ; : $*.elf $*.dbg
!,
'-j2', ": a.elf a.lnk\n: a.elf a.dbg\n");
unlink('a.c');
# SV 56655: Test patterns matching files containing whitespace
touch('some file.yy');
run_make_test(q!
%.xx : %.yy ; @echo matched
!, '"some file.xx"', "matched\n");
unlink('some file.xx', 'some file.yy');
# sv 60188.
# Test that a file explicitly mentioned by the user and made by an implicit
# rule is not considered intermediate.
touch('hello.z');
unlink('hello.x');
unlink('test.x');
# subtest 1
# hello.x is not explicitly mentioned and thus is an intermediate file.
run_make_test(q!
all: hello.z
%.z: %.x
touch $@
%.x: ;
!, '', "#MAKE#: Nothing to be done for 'all'.\n");
# subtest 2
# test.x is explicitly mentioned and thus is not an intermediate file.
run_make_test(q!
all: hello.z
%.z: %.x test.x
touch $@
%.x: ;
!, '', "touch hello.z");
unlink('hello.z');
# sv 60188.
# Test that a file explicitly mentioned by the user and made by an implicit
# rule is not considered intermediate, even when the builtin rules are used.
touch('hello.x');
touch('test.x');
touch('hello.tsk');
# subtest 1
# hello.o is not explicitly mentioned and thus is an intermediate file.
run_make_test(q!
all: hello.tsk
%.tsk: %.z ; @echo $@
%.z : %.x ; @echo $@
!, '', "#MAKE#: Nothing to be done for 'all'.\n");
# subtest 2
# test.z is explicitly mentioned and thus is not an intermediate file.
# test.z is built first because until it's built we don't know if we
# need to rebuild the intermediate hello.z
run_make_test(q!
all: hello.tsk
%.tsk: %.z test.z ; @echo $@
%.z : %.x ; @echo $@
!, '', "test.z\nhello.z\nhello.tsk\n");
# subtest 3
# hello.o is not explicitly mentioned and thus is an intermediate file.
run_make_test(q!
all: hello.tsk
dep:=%.o
%.tsk: $(dep) ; @echo $@
!, '', "#MAKE#: Nothing to be done for 'all'.\n");
# subtest 4
# Even when test.z is constructed from 2 variables it is still explicitly
# mentioned and thus is not an intermediate file.
# test.z is built first because until it's built we don't know if we
# need to rebuild the intermediate hello.z
run_make_test(q!
all: hello.tsk
name:=test
suf:=.z
%.tsk: %.z $(name)$(suf) ; @echo $@
%.z: %.x ; @echo $@
!, '', "test.z\nhello.z\nhello.tsk\n");
unlink('hello.x');
unlink('test.x');
# This tells the test driver that the perl test script executed properly.
1;
### Local Variables:
### eval: (setq whitespace-action (delq 'auto-cleanup whitespace-action))
### End: