虚假目标,用于并行执行make [英] phony targets for parallel execution of make

查看:56
本文介绍了虚假目标,用于并行执行make的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

make实用程序的手册页说-

the man page for the make utility says -

伪造目标有用性的另一个示例是与make的递归调用结合使用(有关更多信息,请参见make的递归使用).在这种情况下,makefile通常会包含一个变量,该变量列出了要构建的多个子目录.一种解决方法是使用一条规则,该规则的配方是子目录上的shell循环,如下所示:

Another example of the usefulness of phony targets is in conjunction with recursive invocations of make (for more information, see Recursive Use of make). In this case the makefile will often contain a variable which lists a number of sub-directories to be built. One way to handle this is with one rule whose recipe is a shell loop over the sub-directories, like this:

 SUBDIRS = foo bar baz

 subdirs:
         for dir in $(SUBDIRS); do \
           $(MAKE) -C $$dir; \
         done

但是,此方法存在问题.首先,此规则将忽略在子制造商中检测到的任何错误,因此即使一个失败,它也将继续构建其余目录.可以通过添加shell命令来记录错误并退出来克服此问题,但是即使使用-k选项调用make也会这样做,这是不幸的.其次,也许更重要的是,由于只有一条规则,因此您无法利用make的能力来并行构建目标(请参阅并行执行).

There are problems with this method, however. First, any error detected in a sub-make is ignored by this rule, so it will continue to build the rest of the directories even when one fails. This can be overcome by adding shell commands to note the error and exit, but then it will do so even if make is invoked with the -k option, which is unfortunate. Second, and perhaps more importantly, you cannot take advantage of make's ability to build targets in parallel (see Parallel Execution), since there is only one rule.

通过将子目录声明为伪目标(必须执行此操作,因为子目录显然始终存在;否则将不会构建),可以消除以下问题:

By declaring the sub-directories as phony targets (you must do this as the sub-directory obviously always exists; otherwise it won't be built) you can remove these problems:

 SUBDIRS = foo bar baz

 .PHONY: subdirs $(SUBDIRS)

 subdirs: $(SUBDIRS)

 $(SUBDIRS):
         $(MAKE) -C $@

 foo: baz

在这里,我们还声明只有在baz子目录完成后才能构建foo子目录.尝试并行构建时,这种关系声明特别重要.

Here we've also declared that the foo sub-directory cannot be built until after the baz sub-directory is complete; this kind of relationship declaration is particularly important when attempting parallel builds.

有人可以解释一下上面提到的第二个代码如何实现并行执行吗? 我不明白Phony Targets在这里有何帮助?

Could someone explain as to how the second code mentioned above enables parallel execution? I fail to understand how Phony Targets has helped here?

推荐答案

让我简单地说一下,以便您有所了解

Let me tell in simple words so that you will get some idea

,可以给出-j2选项,以告诉make文件运行两个并行线程. 如果两个目标是独立的,则对目标及其依赖项进行检查,然后可以并行构建它们.

as arved mentioned -j2 option can be given to tell make file run two parallel threads. make checks for the targets and its dependencies if two targets are independent then they can be built parallely.

让我扩展第二个makefile并将其编写简单

Let me expand the second makefile and write it simple

SUBDIRS = foo bar baz

 .PHONY: subdirs $(SUBDIRS)

 subdirs: $(SUBDIRS)

 bar:
          $(MAKE) -C bar
 baz:
          $(MAKE) -C baz 

 foo: baz
          $(MAKE) -C foo

现在看到以上情况

  1. bar and baz无关紧要,因此可以并行构建

  1. bar and baz depends on nothing so they can be built paralelly

foo取决于baz,因此无法与bar或baz并行构建

foo depends on baz so it cant be built parallel to bar or baz

因此,在上述情况下,请确保make文件使用并行构建文件的功能.

So in above case you ensure that make file uses the ability to built files parallelly.

现在获取您提到的第一个make文件

Now take the first make file you mentioned

 SUBDIRS = foo bar baz

 subdirs:
         for dir in $(SUBDIRS); do \
           $(MAKE) -C $$dir; \
         done

因此,如果您不提及.PHONY目标,那么上面的代码对您来说是个窍门,但是会失去并行构建的能力. 仔细观察.只有一个目标subdirs可以运行三个循环.所有这三个循环都将一个接一个地运行,因此没有并行性.

so if you don't mention as .PHONY targets above code is trick for you, but with a penalty of loosing ability to build parallel. Observe closely. There is only one target subdirs which runs three loops. all the three loops will be run one after the other so no parallelism.

只记得拇指法则.如果目标彼此独立,Make可以使用并行构建,因此它可以并行构建独立的目标而没有任何依赖性问题

Just remember the thumb rule. Make can use the parallel build if the targets are independent of each other so it can build independent target parallel without any dependency issues

这篇关于虚假目标,用于并行执行make的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆