简体   繁体   English

makefile 并行运行目标

[英]makefile run targets in parallel

I've the following makefile, with two rules.我有以下 makefile,有两个规则。

Is there a way to run these two rules in parallel, I mean to maximize the core's capabilities?有没有办法并行运行这两个规则,我的意思是最大化核心的能力? I see this section but not sure that I got how to use it for my purpose, since I want to handle it within the makefile and not from the command line.我看到了这一部分,但不确定我是否知道如何将它用于我的目的,因为我想在 makefile 中而不是在命令行中处理它。

Ie run module1 & 2 targets in parallel.即并行运行 module1 和 2 个目标。

This is the makefile:这是生成文件:

all: module1 module2

.PHONY: module1
module1:
    @echo "run module 1"
    DIR=$(PWD)
    @echo $(DIR)

.PHONY: module2
module2:
    @echo "run module2”

cleanup: 
    fzr clean $(DIR)

You can set make options that you usually pass to make via its command line invokation in the makefile itself.您可以设置通常通过 make 文件本身中的命令行调用传递给 make 的make选项。 Add this line to your makefile将此行添加到您的 makefile

MAKEFLAGS += -j2

and you can invoke make without the -j flag, it will still spawn two processes to build targets in parallel, when they aren't dependent on each other.并且您可以make没有-j标志的情况下调用make ,当它们不相互依赖时,它仍然会产生两个进程来并行构建目标。 To automatically determine the number of jobs to spawn, you can use this on linux要自动确定要生成的作业数量,您可以在 linux 上使用它

NPROCS = $(shell grep -c 'processor' /proc/cpuinfo)
MAKEFLAGS += -j$(NPROCS)

and on MacOS在 MacOS 上

NPROCS = $(shell sysctl hw.ncpu  | grep -o '[0-9]\+')
MAKEFLAGS += -j$(NPROCS)

Even a MAKEFLAGS += -j$(NPROCS) would not work if you have '::' instead of ':' for your rule, as illustrated by that recent fix in Git 2.25.2 (March 2020)即使是MAKEFLAGS += -j$(NPROCS)会,如果你有“::”,而不是工作“:”为您的规则,Git中2.25.2(2020日)说明通过最新修补程序

See commit 2607d39 (18 Feb 2020) by Jeff King ( peff ) .请参阅Jeff King ( peff ) 的commit 2607d39 (2020 年 2 月 18 日
(Merged by Junio C Hamano -- gitster -- in commit 29b09c5 , 02 Mar 2020) (由Junio C gitster -- gitster --commit 29b09c5 中合并,2020 年 3 月 2 日)

doc-diff : use single-colon rule in rendering Makefile doc-diff : 在渲染 Makefile 时使用单冒号规则

Signed-off-by: Jeff King签字人:Jeff King

When rendering the troff manpages to text via "man", we create an ad-hoc Makefile and feed it to " make ".当通过“man”将 troff 联机帮助页渲染为文本时,我们创建了一个临时 Makefile 并将其提供给“ make ”。
The purpose here is two-fold:这里的目的有两个:

  • reuse results from a prior interrupted render of the same tree重用来自同一棵树的先前中断渲染的结果
  • use make's -j option to build in parallel使用 make 的-j选项并行构建

But the second part doesn't seem to work (at least with my version of GNU make, 4.2.1).但是第二部分似乎不起作用(至少对于我的 GNU make 版本,4.2.1)。 It just runs one render at a time.它一次只运行一个渲染。

We use a double-colon " all " rule for each file, like:我们对每个文件使用双冒号“ all ”规则,例如:

 all:: foo foo: ...actual render recipe... all:: bar bar: ...actual render recipe... ...and so on...

And it's this double-colon that seems to inhibit the parallelism .正是这个双冒号似乎抑制了并行性
We can just switch to a regular single-colon rule.我们可以切换到常规的单冒号规则。

Even though we do have multiple rules for "all" here, we don't have any recipe to execute for "all" (we only care about triggering its dependencies), so the distinction is irrelevant.尽管我们在这里对“all”确实有多个规则,但我们没有为“all”执行任何配方(我们只关心触发它的依赖项),所以区别是无关紧要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM