#                                                                    -*-perl-*-

$description = "Test parallelism (-j) option.";


$details = "This test creates a makefile with two double-colon default
rules.  The first rule has a series of sleep and echo commands
intended to run in series.  The second and third have just an
echo statement.  When make is called in this test, it is given
the -j option with a value of 4.  This tells make that it may
start up to four jobs simultaneously.  In this case, since the
first command is a sleep command, the output of the second
and third commands will appear before the first if indeed
make is running all of these commands in parallel.";

if (!$parallel_jobs) {
  return -1;
}

if ($vos) {
  $sleep_command = "sleep -seconds";
}
else {
  $sleep_command = "sleep";
}


run_make_test("
all : def_1 def_2 def_3
def_1 : ; \@echo ONE; $sleep_command 3 ; echo TWO
def_2 : ; \@$sleep_command 2 ; echo THREE
def_3 : ; \@$sleep_command 1 ; echo FOUR",
              '-j4', "ONE\nFOUR\nTHREE\nTWO");

# Test parallelism with included files.  Here we sleep/echo while
# building the included files, to test that they are being built in
# parallel.
run_make_test("
all: 1 2; \@echo success
-include 1.inc 2.inc
1.inc: ; \@echo ONE.inc; $sleep_command 2; echo TWO.inc; echo '1: ; \@echo ONE; $sleep_command 2; echo TWO' > \$\@
2.inc: ; \@$sleep_command 1; echo THREE.inc; echo '2: ; \@$sleep_command 1; echo THREE' > \$\@",
              "-j4",
              "ONE.inc\nTHREE.inc\nTWO.inc\nONE\nTHREE\nTWO\nsuccess\n");

rmfiles(qw(1.inc 2.inc));


# Test parallelism with included files--this time recurse first and make
# sure the jobserver works.
run_make_test("
recurse: ; \@\$(MAKE) --no-print-directory -f #MAKEFILE# INC=yes all
all: 1 2; \@echo success

INC = no
ifeq (\$(INC),yes)
-include 1.inc 2.inc
endif

1.inc: ; \@echo ONE.inc; $sleep_command 2; echo TWO.inc; echo '1: ; \@echo ONE; $sleep_command 2; echo TWO' > \$\@
2.inc: ; \@$sleep_command 1; echo THREE.inc; echo '2: ; \@$sleep_command 1; echo THREE' > \$\@",
              "-j4",
              "ONE.inc\nTHREE.inc\nTWO.inc\nONE\nTHREE\nTWO\nsuccess\n");

rmfiles(qw(1.inc 2.inc));

# Grant Taylor reports a problem where tokens can be lost (not written back
# to the pipe when they should be): this happened when there is a $(shell ...)
# function in an exported recursive variable.  I added some code to check
# for this situation and print a message if it occurred.  This test used
# to trigger this code when I added it but no longer does after the fix.
# We have to increase the timeout from the default (5s) on this test.

run_make_test("
export HI = \$(shell \$(\$\@.CMD))
first.CMD = echo hi
second.CMD = $sleep_command 4; echo hi

.PHONY: all first second
all: first second

first second: ; \@echo \$\@; $sleep_command 1; echo \$\@",
              '-j2', "first\nfirst\nsecond\nsecond", 0, 7);

# Michael Matz <matz@suse.de> reported a bug where if make is running in
# parallel without -k and two jobs die in a row, but not too close to each
# other, then make will quit without waiting for the rest of the jobs to die.

run_make_test("
.PHONY: all fail.1 fail.2 fail.3 ok
all: fail.1 ok fail.2 fail.3

fail.1 fail.2 fail.3:
	\@sleep \$(patsubst fail.%,%,\$\@)
	\@echo Fail
	\@exit 1

ok:
	\@sleep 4
	\@echo Ok done",
              '-rR -j5', 'Fail
#MAKE#: *** [fail.1] Error 1
#MAKE#: *** Waiting for unfinished jobs....
Fail
#MAKE#: *** [fail.2] Error 1
Fail
#MAKE#: *** [fail.3] Error 1
Ok done',
             512);


# Test for Savannah bug #15641.
#
run_make_test('
.PHONY: all
all:; @:

-include foo.d

foo.d: comp
	@echo building $@

comp: mod_a.o mod_b.o; @:

mod_a.o mod_b.o:
	@exit 1
', '-j2', '');


# TEST #9 -- Savannah bugs 3330 and 15919
# In earlier versions of make this will either give the wrong answer, or hang.

utouch(-10, 'target');
run_make_test('target: intermed ; touch $@

.INTERMEDIATE: intermed
intermed: | phony ; touch $@

.PHONY: phony
phony: ; : phony', '-rR -j', ': phony');
rmfiles('target');

# TEST #10: Don't put --jobserver-fds into a re-exec'd MAKEFLAGS.
# We can't test this directly because there's no way a makefile can
# show the value of MAKEFLAGS we were re-exec'd with.  We can intuit it
# by looking for "disabling jobserver mode" warnings; we should only
# get one from the original invocation and none from the re-exec.
# See Savannah bug #18124

run_make_test(q!
-include inc.mk
recur:
#	@echo 'MAKEFLAGS = $(MAKEFLAGS)'
	@rm -f inc.mk
	@$(MAKE) -j2 -f #MAKEFILE# all
all:
#	@echo 'MAKEFLAGS = $(MAKEFLAGS)'
	@echo $@
inc.mk:
#	@echo 'MAKEFLAGS = $(MAKEFLAGS)'
	@echo 'FOO = bar' > $@
!,
              '--no-print-directory -j2', "#MAKE#[1]: warning: -jN forced in submake: disabling jobserver mode.\nall\n");

rmfiles('inc.mk');

if ($all_tests) {
    # Implicit files aren't properly recreated during parallel builds
    # Savannah bug #26864

    # The first run works fine
    run_make_test(q!
%.bar: %.x foo.y ; cat $^ > $@
%.x: ; touch $@
foo.y: foo.y.in ; cp $< $@
foo.y.in: ; touch $@
!,
                  '-j2 main.bar',
                  "touch foo.y.in
touch main.x
cp foo.y.in foo.y
cat main.x foo.y > main.bar
rm main.x");

    # Now we touch the .in file and make sure it still works
    touch('foo.y.in');

    run_make_test(undef, '-j2 main.bar', "cp foo.y.in foo.y
touch main.x
cat main.x foo.y > main.bar
rm main.x");

    # Clean up
    rmfiles(qw(foo.y foo.y.in main.bar));
}

if ($all_tests) {
    # Jobserver FD handling is messed up in some way.
    # Savannah bug #28189
    # It doesn't look like that bug anymore but this is the code it runs

    run_make_test(q!
ifdef EXTRA
vpath %.dst /
xxx.dst: ; true
yyy.dst: ; true
endif

M := $(MAKE)
xx: ; $M --no-print-directory -j2 -f $(MAKEFILE_LIST) xxx.dst yyy.dst EXTRA=1
!,
                  '-j2',
                  '#MAKE#[1]: warning: -jN forced in submake: disabling jobserver mode.
true
true
');
}

# Make sure that all jobserver FDs are closed if we need to re-exec the
# master copy.
#
# First, find the "default" file descriptors we normally use
# Then make sure they're still used.
#
# Right now we don't have a way to run a makefile and capture the output
# without checking it, so we can't really write this test.

# run_make_test('
# submake: ; @$(MAKE) --no-print-directory -f #MAKEFILE# fdprint 5>output

# dependfile: ; @echo FOO=bar > $@

# INCL := true

# FOO=foo
# ifeq ($(INCL),true)
# -include dependfile
# endif

# fdprint: ; @echo $(filter --jobserver%,$(MAKEFLAGS))

# recurse: ; @$(MAKE) --no-print-directory -f #MAKEFILE# submake INCL=true',
#               '-j2 INCL=false fdprint',
#               'bar');

# rmfiles(qw(dependfile output));


# # Do it again, this time where the include is done by the non-master make.
# run_make_test(undef, '-j2 recurse INCL=false', 'bar');

# rmfiles(qw(dependfile output));

1;
