1

In my makefile I have a recipe that produces two files. If either of those two files are out of date, the recipe should run, once. So something like this:

file1 file2 : deps
    make_files

Note that the make_files command always produces both files, and its invocation does not depend on the target (i.e., we don't use the target name or any stem in its invocation).

The problem is that make interprets this as two rules, one for file1 and one for file2, causing it to potentially run make_files twice if both are missing (e.g., when using make -j), which blows up (make_files is actually invoking make in another subdirectory and this doesn't work if it is invoked twice concurrently).

Any fix, for example to tell make to just run make_files once?

Right now the hack I'm using is to make file2 depend on file1, even though it doesn't:

file2 : file1

This causes make to serialize the make_files invocations. This doesn't always work well, however: in the case that both files are missing, it will run make_files once to make them, but then if file2 has a later timestamp than file1, it will make run make_files again.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386

1 Answers1

2

If your two files really do share a common stem as in your example, then you can use:

%1 %2: deps
        make_files

Pattern rules, unlike explicit rules, assume that a single invocation of the recipe will create all target patterns.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • The files have a common "middle part". The actual names are `libpfc.so` and `pfc.ko`. So I guess that doesn't work? – BeeOnRope Jul 18 '18 at 21:27
  • @Beta - ok, right, but only if I don't have any other `.so` or `.ko` files being built in this Makefile, right? – BeeOnRope Jul 18 '18 at 23:40
  • If you use explicit or static pattern rules to build your other `.so` / `.ko` targets then they won't interfere. – MadScientist Jul 19 '18 at 05:27