#                                                                    -*-perl-*-
$description = "Test second expansion in static pattern rules.";

$details = "";

# Test #1: automatic variables.
#
run_make_test(q!
.SECONDEXPANSION:
.DEFAULT: ; @echo '$@'

foo.a foo.b: foo.%: bar.% baz.%
foo.a foo.b: foo.%: biz.% | buz.%

foo.a foo.b: foo.%: $$@.1 \
                    $$<.2 \
                    $$(addsuffix .3,$$^) \
                    $$(addsuffix .4,$$+) \
                    $$|.5 \
                    $$*.6
!,
              '', 'bar.a
baz.a
biz.a
buz.a
foo.a.1
bar.a.2
bar.a.3
baz.a.3
biz.a.3
bar.a.4
baz.a.4
biz.a.4
buz.a.5
a.6
');


# Test #2: target/pattern -specific variables.
#
run_make_test(q!
.SECONDEXPANSION:
.DEFAULT: ; @echo '$@'

foo.x foo.y: foo.%: $$(%_a) $$($$*_b)

foo.x: x_a := bar

%.x: x_b := baz
!,
              '', "bar\nbaz\n");


# Test #3: order of prerequisites.
#
run_make_test(q!
.SECONDEXPANSION:
.DEFAULT: ; @echo '$@'

all: foo.a bar.a baz.a

# Subtest #1
foo.a foo.b: foo.%: foo.%.1; @:
foo.a foo.b: foo.%: foo.%.2
foo.a foo.b: foo.%: foo.%.3


# Subtest #2
bar.a bar.b: bar.%: bar.%.2
bar.a bar.b: bar.%: bar.%.1; @:
bar.a bar.b: bar.%: bar.%.3


# Subtest #3
baz.a baz.b: baz.%: baz.%.1
baz.a baz.b: baz.%: baz.%.2
baz.a baz.b: ; @:
!,
             '', 'foo.a.1
foo.a.2
foo.a.3
bar.a.1
bar.a.2
bar.a.3
baz.a.1
baz.a.2
');


# Test #4: Make sure stem triple-expansion does not happen.
#
run_make_test(q!
.SECONDEXPANSION:
foo$$bar: f%r: % $$*.1 ; @echo '$*'

oo$$ba oo$$ba.1: ; @echo '$@'
!,
              '', 'oo$ba
oo$ba.1
oo$ba
');

# sv 62324.
# Integrity self check.
run_make_test(q!
.SECONDEXPANSION:
all: bye.x
bye.x: %.x: $$(firstword %.1;
!, '', "#MAKEFILE#:4: *** unterminated call to function 'firstword': missing ')'.  Stop.", 512);

#unlink('hello.tsk', 'bye.tsk', 'hello.o', 'hello.q', 'bye.o');

# sv 62706.
# Test that makes avoids second expanding prerequisites of the targets which
# are not built.
# Here, hello.tsk is built and its prerequisites are second expanded.
# bye.tsk is not built and its prerequisites are not second expanded.

# Static pattern rules.
run_make_test(q!
.SECONDEXPANSION:
hello.tsk: %.tsk: %.o $$(info second expansion of $$@ prereqs); $(info $@ from $<)
bye.tsk: %.tsk: %.o $$(info second expansion of $$@ prereqs); $(info $@ from $<)
hello.o: $$(info second expansion of $$@ prereqs); $(info $@)
bye.o: $$(info second expansion of $$@ prereqs); $(info $@)
!, 'hello.tsk',
"second expansion of hello.tsk prereqs
second expansion of hello.o prereqs
hello.o
hello.tsk from hello.o
#MAKE#: 'hello.tsk' is up to date.\n");

# sv 62706.
# Order only prereqs.
run_make_test(q!
.SECONDEXPANSION:
hello.tsk: %.tsk:| %.o $$(info second expansion of $$@ prereqs); $(info $@ from $|)
bye.tsk: %.tsk:| %.o $$(info second expansion of $$@ prereqs); $(info $@ from $|)
hello.o:| $$(info second expansion of $$@ prereqs); $(info $@)
bye.o:| $$(info second expansion of $$@ prereqs); $(info $@)
!, 'hello.tsk',
"second expansion of hello.tsk prereqs
second expansion of hello.o prereqs
hello.o
hello.tsk from hello.o
#MAKE#: 'hello.tsk' is up to date.\n");

# sv 62706.
# Double colon. 1 rule per target.
run_make_test(q!
.SECONDEXPANSION:
hello.tsk:: %.tsk: %.o $$(info second expansion of $$@ prereqs); $(info $@ from $<)
bye.tsk:: %.tsk: %.o $$(info second expansion of $$@ prereqs); $(info $@ from $<)
hello.o:: $$(info second expansion of $$@ prereqs); $(info $@)
bye.o:: $$(info second expansion of $$@ prereqs); $(info $@)
!, 'hello.tsk',
"second expansion of hello.tsk prereqs
second expansion of hello.o prereqs
hello.o
hello.tsk from hello.o
#MAKE#: 'hello.tsk' is up to date.\n");

# sv 62706.
# Double colon. 2 rules per target.
run_make_test(q!
.SECONDEXPANSION:
hello.tsk:: %.tsk: %.o $$(info 1 second expansion of $$@ prereqs); $(info 1 $@ from $<)
hello.tsk:: %.tsk: %.o $$(info 2 second expansion of $$@ prereqs); $(info 2 $@ from $<)
bye.tsk:: %.tsk: %.o $$(info 1 second expansion of $$@ prereqs); $(info 1 $@ from $<)
bye.tsk:: %.tsk: %.o $$(info 2 second expansion of $$@ prereqs); $(info 2 $@ from $<)
hello.o:: $$(info 1 second expansion of $$@ prereqs); $(info 1 $@)
hello.o:: $$(info 2 second expansion of $$@ prereqs); $(info 2 $@)
bye.o:: $$(info 1 second expansion of $$@ prereqs); $(info 1 $@)
bye.o:: $$(info 2 second expansion of $$@ prereqs); $(info 2 $@)
!, 'hello.tsk',
"1 second expansion of hello.tsk prereqs
1 second expansion of hello.o prereqs
1 hello.o
2 second expansion of hello.o prereqs
2 hello.o
1 hello.tsk from hello.o
2 second expansion of hello.tsk prereqs
2 hello.tsk from hello.o
#MAKE#: 'hello.tsk' is up to date.\n");

# sv 62706.
# Test that the prerequisites of 'hello.tsk' are second expanded once.
run_make_test(q!
.SECONDEXPANSION:
all: hello.tsk hello.q
hello.tsk: %.tsk: %.o $$(info second expansion of $$@ prereqs); $(info $@ from $^)
hello.o: $$(info second expansion of $$@ prereqs); $(info $@)
hello.q: %.q: %.tsk $$(info second expansion of $$@ prereqs); $(info $@ from $^)
!, '',
"second expansion of hello.tsk prereqs
second expansion of hello.o prereqs
hello.o
hello.tsk from hello.o
second expansion of hello.q prereqs
hello.q from hello.tsk
#MAKE#: Nothing to be done for 'all'.\n");

unlink('hello.1');

# sv 62706.
# No side effects from second expansion of unrelated rules.
run_make_test(q!
.SECONDEXPANSION:
all: hello.tsk
hello.tsk: %.tsk: %.o; exit 1
hello.o:;
bye.tsk: %.tsk: $$(shell touch hello.1);
!, '',
"exit 1
#MAKE#: *** [#MAKEFILE#:4: hello.tsk] Error 1\n", 512);

# sv 62706.
# Second expansion of intermediate prerequisites.
# The rule to build hello.x is static pattern.
# .SECONDARY marks hello.x as intermediate.
# Test that $$(deps) is secondary expanded.
run_make_test(q!
deps:=hello.h
.SECONDEXPANSION:
.SECONDARY: hello.x
all: hello.x
hello.x: %.x: $$(deps); $(info $@)
hello.h:; $(info $@)
!, '', "hello.h\nhello.x\n#MAKE#: Nothing to be done for 'all'.\n");

# This tells the test driver that the perl test script executed properly.
1;
