在递归 make 中,防止 -j 向下级联

In recursive make, prevent -j from cascading down

我有一个顶级 makefile,它为并行构建指定了 -jn。这个顶级 makefile 调用许多不同的组件 makefile。不同的组件来自不同的存储库,其中一些支持并行构建,而另一些则不支持。我可以在顶层指定 -j1 以使所有组件正常工作。但是有没有办法在顶层指定 -jn,让标志级联到某些组件,但对其他组件强制执行 -j1?如果我在选定的较低级别的 makefile 中使用 NOTPARALLEL 虚假目标,是否会仅对这些组件强制执行 -j1?

If I use NOTPARALLEL phony target in selected lower-level makefiles, will that enforce -j1 only for those components?

是的。来自 GNU Make Special Targets 页面:

.NOTPARALLEL

If .NOTPARALLEL is mentioned as a target, then this invocation of make will be run serially, even if the ‘-j’ option is given. Any recursively invoked make command will still run recipes in parallel (unless its makefile also contains this target)

值得一提的是,对于那些没有正确设置 .NOTPARALLEL 的讨厌的第 3 方库,有一种方法可以通过递归 make 命令传递多个文件来实现:

  1. 创建一个内容为“.NOTPARALLEL”的文件,例如notparallel.mk
  2. 在制作图书馆的配方中,做$(MAKE) -f lib.mk -f notparallel.mk

这将确保 .NOTPARALLEL 被提及为该 make 调用的目标,而无需您修改该库本身。

用单行创建一个文件

.NOTPARALLEL:

在其中,像 Andreas 提议的那样说 notparallel.mk。然而,不要简单地将它包含在对第一个子 makefile 的调用中,因为并行性问题将随着该 makefile 调用的下一级子子 makefile 重新出现。而是将其添加到特殊变量 MAKEFILES 中,该变量包含 makefile 列表,此后每次调用 make 时都会自动包含这些文件:

MAKEFILES += notparallel.mk

maintarget:
      $(MAKE) -f sub-makefile.mk