#                                                              -*-mode: perl-*-

$description = "Test GNU make's auto-reinvocation feature.";

$details = "\
If the makefile or one it includes can be rebuilt then it is, and make
is reinvoked.  We create a rule to rebuild the makefile from a temp
file, then touch the temp file to make it newer than the makefile.";

$omkfile = $makefile;

&utouch(-600, 'incl.mk');
# For some reason if we don't do this then the test fails for systems
# with sub-second timestamps, maybe + NFS?  Not sure.
&utouch(-1, 'incl-1.mk');

run_make_test('
all: ; @echo running rules.

#MAKEFILE# incl.mk: incl-1.mk ; @echo rebuilding $@; echo >> $@

include incl.mk',
              '', "rebuilding incl.mk\nrunning rules.\n");

# Make sure updating the makefile itself also works

&utouch(-600, $omkfile);

run_make_test(undef, '', "rebuilding #MAKEFILE#\nrunning rules.\n");

&rmfiles('incl.mk', 'incl-1.mk');


# In this test we create an included file that's out-of-date, but then
# the rule doesn't update it.  Make shouldn't re-exec.

&utouch(-600, 'b','a');
#&utouch(-10, 'a');
&touch('c');

run_make_test('
all: ; @echo hello

a : b ; echo >> $@

b : c ; test -f $@ || echo >> $@

c: ; echo >> $@

include $(F)',
              'F=a', "test -f b || echo >> b\nhello\n");

# Now try with the file we're not updating being the actual file we're
# including: this and the previous one test different parts of the code.

run_make_test(undef, 'F=b', "test -f b || echo >> b\nhello\n");

&rmfiles('a','b','c');

# Ensure command line variables are preserved properly across re-exec
# Tests for Savannah bug #30723

run_make_test('
ifdef RECURSE
-include foo30723
endif
recurse: ; @$(MAKE) -f $(MAKEFILE_LIST) RECURSE=1 test
test: ; @echo F.O=$(F.O)
foo30723: ; @touch $@
',
              '--no-print-directory F.O=bar', "F.O=bar\n");

unlink('foo30723');

# If ANY makefile is rebuilt then we should re-exec

run_make_test('
all: ; @echo RESTARTS=$(MAKE_RESTARTS)

m1.d: ; @echo $@; touch $@

m2.d: m1.d ; @test -f $< || { echo $@; touch $@; }

include m1.d
-include m2.d
',
              '', "m1.d\nRESTARTS=1\n");

unlink('m1.d', 'm2.d');

# Same as before but be sure we get error messages for un-created makefiles
run_make_test('
all: ; @echo RESTARTS=$(MAKE_RESTARTS)

m1.d: ; @echo $@; touch $@

m2.d: m1.d ; @test -f $< || { echo $@; touch $@; }

include m1.d m2.d
', '',
              # This runs afoul of https://savannah.gnu.org/bugs/?61226
              0 ? "m1.d\n#MAKEFILE#:8: m2.d: $ERR_no_such_file"
                  : "m1.d\nRESTARTS=1",
              0 ? 512 : 0);

unlink('m1.d', 'm2.d');

# sv 61226.
# This set of four cases tests two aspects of make.
#
# 1. If a rule has no prerequisites or recipe, and the target of the rule is a
# nonexistent file, then make imagines this target to have been updated
# whenever its rule is run.
#
# 2. Make does not re-execute itself in this case of imagined target.
#
# Test case 1.
# Make imagines hello.d was updated by a rule without recipe and without
# prereqs.
# This should succeed.
# Make should not re-execute itself.
run_make_test('
hello.o: hello.d; $(info RESTARTS=$(MAKE_RESTARTS))
hello.d:
include hello.d
', '', "RESTARTS=\n#MAKE#: 'hello.o' is up to date.");

# Test case 2.
# Make imagines hello.d was updated by a rule with a recipe and without
# prereqs.
# This should succeed.
# Make should not re-execute itself.
run_make_test('
hello.o: hello.d; $(info RESTARTS=$(MAKE_RESTARTS))
hello.d:; $(info $@)
include hello.d
', '', "hello.d\nRESTARTS=\n#MAKE#: 'hello.o' is up to date.");

&touch('hello.td');
# Test case 3.
# Make imagines hello.d was updated by a rule without a recipe and with
# prereqs.
# This should succeed.
# Make should not re-execute itself.
run_make_test('
hello.o: hello.d; $(info RESTARTS=$(MAKE_RESTARTS))
hello.d: hello.td
include hello.d
', '', "RESTARTS=\n#MAKE#: 'hello.o' is up to date.");

# Test case 4.
# Same test as three tests above, but the rule has both recipe and prereqs.
# Make should report this error.
run_make_test('
hello.o: hello.d; $(info $@)
hello.d: hello.td; $(info $@)
include hello.d
', '',
              # This runs afoul of https://savannah.gnu.org/bugs/?61226
              0 ? "hello.d\n#MAKEFILE#:4: hello.d: $ERR_no_such_file"
                  : "hello.d\nhello.o\n#MAKE#: 'hello.o' is up to date.",
              0 ? 512 : 0);

unlink('hello.td');

# Test DV 62088 : make sure we don't re-invoke for stdin makefiles.
# The test framework doesn't seem to have a good way to do this.

create_file('input.mk', "all:;\$(info all)\n");

close(STDIN);
open(STDIN, "<", 'input.mk') || die "input.mk: $!\n";

run_make_test('', '-sf -', "all\n");

close(STDIN);
unlink('input.mk');

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