Bruno Haible wrote:
> Continuing this thread from May 2019
> <>:
> The problem was:
>   How can a rule that generates multiple files be formulated so
>   that it works with parallel make?
> For example, a rule that invokes bison, or a rule that invokes
> a different Makefile. For simplicity, here, use a rule that
> creates 4 files copy1, copy2, copy3, copy4.
> ===========================================
> all : copy1 copy2 copy3 copy4
> copy1 copy2 copy3 copy4: Makefile
>       install -c -m 644 Makefile copy1
>       install -c -m 644 Makefile copy2
>       install -c -m 644 Makefile copy3
>       install -c -m 644 Makefile copy4
> ===========================================
> Unfortunately, with "make -j8", it invokes the rule multiple times.
> It is possible to change this Makefile so that
>   (A) "rm -f copy?; make" executes the rule once.
>   (B) "rm -f copy?; make -j8" executes the rule once as well.
>   (C) After "make", another "make" just prints "Nothing to be done for 'all'."
>   (D) After removing one of the files copy?, "make" executes the rule once.
>       (This covers also the case of pressing Ctrl-C during "make", then
>       doing "make" again.)
>   (E) After replacing one of the files copy? with a file that is older than
>       Makefile, "make" executes the rule once.
> There are three possibilities:

You're thinking about this the wrong way. Your set of commands is inherently
serial, therefore you need to write serial dependencies.

all: copy1 copy2 copy3 copy4

copy1: Makefile
        install -c -m 644 Makefile $@
copy2: copy1
        install -c -m 644 Makefile $@
copy3: copy2
        install -c -m 644 Makefile $@
copy4: copy3
        install -c -m 644 Makefile $@

This satisfies all your conditions, because it is inherently correct.

More on writing proper parallel Makefiles here

  -- Howard Chu
  CTO, Symas Corp. 
  Director, Highland Sun
  Chief Architect, OpenLDAP

Reply via email to